Coverage Report

Created: 2026-10-02 09:53

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
79.8k
#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
0
#define NOTE_PSEUDO_SECTION_REG     ".reg"
101
230
#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
97
{
141
97
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
142
97
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
143
97
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
144
97
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
145
97
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
146
97
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
147
97
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
148
97
}
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
9
{
173
9
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
174
9
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
175
9
}
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
258
{
195
258
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
196
258
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
197
258
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
198
258
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
199
258
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
200
258
}
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
168
{
223
168
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
224
168
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
225
168
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
226
168
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
227
168
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
228
168
}
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
970
{
251
970
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252
970
}
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.52M
{
301
3.52M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
302
3.52M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
303
3.52M
  if (abfd->tdata.any == NULL)
304
0
    return false;
305
306
3.52M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
307
3.52M
  if (abfd->direction != read_direction)
308
816
    {
309
816
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
310
816
      if (o == NULL)
311
0
  return false;
312
816
      elf_tdata (abfd)->o = o;
313
816
      elf_program_header_size (abfd) = (bfd_size_type) -1;
314
816
    }
315
3.52M
  return true;
316
3.52M
}
317
318
319
bool
320
bfd_elf_make_object (bfd *abfd)
321
1.72M
{
322
1.72M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
323
1.72M
}
324
325
bool
326
bfd_elf_mkcorefile (bfd *abfd)
327
955k
{
328
  /* I think this can be done just like an object file.  */
329
955k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
330
0
    return false;
331
955k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
332
955k
  return elf_tdata (abfd)->core != NULL;
333
955k
}
334
335
char *
336
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
337
80.4k
{
338
80.4k
  Elf_Internal_Shdr **i_shdrp;
339
80.4k
  bfd_byte *shstrtab = NULL;
340
80.4k
  file_ptr offset;
341
80.4k
  bfd_size_type shstrtabsize;
342
343
80.4k
  i_shdrp = elf_elfsections (abfd);
344
80.4k
  if (i_shdrp == 0
345
80.4k
      || shindex >= elf_numsections (abfd)
346
80.4k
      || i_shdrp[shindex] == 0)
347
0
    return NULL;
348
349
80.4k
  shstrtab = i_shdrp[shindex]->contents;
350
80.4k
  if (shstrtab == NULL)
351
80.4k
    {
352
      /* No cached one, attempt to read, and cache what we read.  */
353
80.4k
      offset = i_shdrp[shindex]->sh_offset;
354
80.4k
      shstrtabsize = i_shdrp[shindex]->sh_size;
355
356
80.4k
      if (shstrtabsize == 0
357
71.5k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
358
71.0k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
359
12.5k
  {
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
12.5k
    i_shdrp[shindex]->sh_size = 0;
364
12.5k
  }
365
67.9k
      else if (shstrtab[shstrtabsize - 1] != 0)
366
19.9k
  {
367
    /* It is an error if a string table isn't terminated.  */
368
19.9k
    _bfd_error_handler
369
      /* xgettext:c-format */
370
19.9k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
371
19.9k
    shstrtab[shstrtabsize - 1] = 0;
372
19.9k
  }
373
80.4k
      i_shdrp[shindex]->contents = shstrtab;
374
80.4k
    }
375
80.4k
  return (char *) shstrtab;
376
80.4k
}
377
378
char *
379
bfd_elf_string_from_elf_section (bfd *abfd,
380
         unsigned int shindex,
381
         unsigned int strindex)
382
1.40M
{
383
1.40M
  Elf_Internal_Shdr *hdr;
384
385
1.40M
  if (strindex == 0)
386
253k
    return "";
387
388
1.15M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
389
582
    return NULL;
390
391
1.14M
  hdr = elf_elfsections (abfd)[shindex];
392
393
1.14M
  if (hdr->contents == NULL)
394
109k
    {
395
109k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
396
29.3k
  {
397
    /* PR 17512: file: f057ec89.  */
398
    /* xgettext:c-format */
399
29.3k
    _bfd_error_handler (_("%pB: attempt to load strings from"
400
29.3k
        " a non-string section (number %d)"),
401
29.3k
            abfd, shindex);
402
29.3k
    return NULL;
403
29.3k
  }
404
405
80.4k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
406
12.5k
  return NULL;
407
80.4k
    }
408
1.04M
  else
409
1.04M
    {
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
1.04M
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
416
0
  return NULL;
417
1.04M
    }
418
419
1.10M
  if (strindex >= hdr->sh_size)
420
32.1k
    {
421
32.1k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
422
32.1k
      _bfd_error_handler
423
  /* xgettext:c-format */
424
32.1k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
425
32.1k
   abfd, strindex, (uint64_t) hdr->sh_size,
426
32.1k
   (shindex == shstrndx && strindex == hdr->sh_name
427
32.1k
    ? ".shstrtab"
428
32.1k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
429
32.1k
      return NULL;
430
32.1k
    }
431
432
1.07M
  return ((char *) hdr->contents) + strindex;
433
1.10M
}
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
18.9k
{
452
18.9k
  Elf_Internal_Shdr *shndx_hdr;
453
18.9k
  void *alloc_ext;
454
18.9k
  const bfd_byte *esym;
455
18.9k
  Elf_External_Sym_Shndx *alloc_extshndx;
456
18.9k
  Elf_External_Sym_Shndx *shndx;
457
18.9k
  Elf_Internal_Sym *alloc_intsym;
458
18.9k
  Elf_Internal_Sym *isym;
459
18.9k
  Elf_Internal_Sym *isymend;
460
18.9k
  elf_backend_data *bed;
461
18.9k
  size_t extsym_size;
462
18.9k
  size_t amt;
463
18.9k
  file_ptr pos;
464
465
18.9k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
466
0
    abort ();
467
468
18.9k
  if (symcount == 0)
469
0
    return intsym_buf;
470
471
18.9k
  if (elf_use_dt_symtab_p (ibfd))
472
4
    {
473
      /* Use dynamic symbol table.  */
474
4
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
475
0
  {
476
0
    bfd_set_error (bfd_error_invalid_operation);
477
0
    return NULL;
478
0
  }
479
4
      return elf_tdata (ibfd)->dt_symtab + symoffset;
480
4
    }
481
482
  /* Normal syms might have section extension entries.  */
483
18.9k
  shndx_hdr = NULL;
484
18.9k
  if (elf_symtab_shndx_list (ibfd) != NULL)
485
3.45k
    {
486
3.45k
      elf_section_list * entry;
487
3.45k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
488
489
      /* Find an index section that is linked to this symtab section.  */
490
5.75k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
491
3.88k
  {
492
    /* PR 20063.  */
493
3.88k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
494
59
      continue;
495
496
3.82k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
497
1.58k
      {
498
1.58k
        shndx_hdr = & entry->hdr;
499
1.58k
        break;
500
2.23k
      };
501
2.23k
  }
502
503
3.45k
      if (shndx_hdr == NULL)
504
1.87k
  {
505
1.87k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
506
      /* Not really accurate, but this was how the old code used
507
         to work.  */
508
1.87k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
509
    /* Otherwise we do nothing.  The assumption is that
510
       the index table will not be needed.  */
511
1.87k
  }
512
3.45k
    }
513
514
  /* Read the symbols.  */
515
18.9k
  alloc_ext = NULL;
516
18.9k
  alloc_extshndx = NULL;
517
18.9k
  alloc_intsym = NULL;
518
18.9k
  bed = get_elf_backend_data (ibfd);
519
18.9k
  extsym_size = bed->s->sizeof_sym;
520
18.9k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
521
0
    {
522
0
      bfd_set_error (bfd_error_file_too_big);
523
0
      return NULL;
524
0
    }
525
18.9k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
526
18.9k
  size_t alloc_ext_size = amt;
527
18.9k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
528
18.5k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
529
18.5k
            &alloc_ext, ibfd, false))
530
1.46k
    {
531
1.46k
      intsym_buf = NULL;
532
1.46k
      goto out2;
533
1.46k
    }
534
535
17.4k
  size_t alloc_extshndx_size = 0;
536
17.4k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
537
15.1k
    extshndx_buf = NULL;
538
2.37k
  else
539
2.37k
    {
540
2.37k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
541
0
  {
542
0
    bfd_set_error (bfd_error_file_too_big);
543
0
    intsym_buf = NULL;
544
0
    goto out1;
545
0
  }
546
2.37k
      alloc_extshndx_size = amt;
547
2.37k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
548
2.37k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
549
2.21k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
550
2.21k
          &alloc_extshndx_size,
551
2.21k
          (void **) &alloc_extshndx,
552
2.21k
          ibfd, false))
553
352
  {
554
352
    intsym_buf = NULL;
555
352
    goto out1;
556
352
  }
557
2.37k
    }
558
559
17.1k
  if (intsym_buf == NULL)
560
8.73k
    {
561
8.73k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
562
0
  {
563
0
    bfd_set_error (bfd_error_file_too_big);
564
0
    goto out1;
565
0
  }
566
8.73k
      alloc_intsym = bfd_malloc (amt);
567
8.73k
      intsym_buf = alloc_intsym;
568
8.73k
      if (intsym_buf == NULL)
569
0
  goto out1;
570
8.73k
    }
571
572
  /* Convert the symbols to internal form.  */
573
17.1k
  isymend = intsym_buf + symcount;
574
17.1k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
575
17.1k
     shndx = extshndx_buf;
576
236k
       isym < isymend;
577
219k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
578
221k
    {
579
221k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
580
499
  {
581
499
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
582
    /* xgettext:c-format */
583
499
    _bfd_error_handler (_("%pB symbol number %lu references"
584
499
        " nonexistent SHT_SYMTAB_SHNDX section"),
585
499
            ibfd, (unsigned long) symoffset);
586
499
    free (alloc_intsym);
587
499
    intsym_buf = NULL;
588
499
    goto out1;
589
499
  }
590
591
      /* PR 33019: Do not accept unsupported binding values - they will
592
   likely cause problems later on.  */
593
220k
      int bind = ELF_ST_BIND (isym->st_info);
594
220k
      if (bind > STB_WEAK && bind < STB_LOOS)
595
1.05k
  { 
596
    /* xgettext:c-format */
597
1.05k
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
598
1.05k
            ibfd, (unsigned long) (isym - intsym_buf), bind);
599
1.05k
    free (alloc_intsym);
600
1.05k
    intsym_buf = NULL;
601
1.05k
    goto out1;
602
1.05k
  }
603
604
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
605
219k
      int t = ELF_ST_TYPE (isym->st_info);
606
219k
      if (t == 7)
607
253
  {
608
    /* xgettext:c-format */
609
253
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
610
253
            ibfd, (unsigned long) (isym - intsym_buf), t);
611
253
    free (alloc_intsym);
612
253
    intsym_buf = NULL;
613
253
    goto out1;
614
253
  }
615
219k
    }
616
617
17.4k
 out1:
618
17.4k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
619
18.9k
 out2:
620
18.9k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
621
622
18.9k
  return intsym_buf;
623
17.4k
}
624
625
/* Look up a symbol name.  */
626
static const char *
627
bfd_elf_sym_name_raw (bfd *abfd,
628
          Elf_Internal_Shdr *symtab_hdr,
629
          Elf_Internal_Sym *isym)
630
203k
{
631
203k
  unsigned int iname = isym->st_name;
632
203k
  unsigned int shindex = symtab_hdr->sh_link;
633
634
203k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
635
      /* Check for a bogus st_shndx to avoid crashing.  */
636
28.2k
      && isym->st_shndx < elf_numsections (abfd))
637
27.6k
    {
638
27.6k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
639
27.6k
      shindex = elf_elfheader (abfd)->e_shstrndx;
640
27.6k
    }
641
642
203k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
643
203k
}
644
645
const char *
646
bfd_elf_sym_name (bfd *abfd,
647
      Elf_Internal_Shdr *symtab_hdr,
648
      Elf_Internal_Sym *isym,
649
      asection *sym_sec)
650
196k
{
651
196k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
652
196k
  if (name == NULL)
653
63.6k
    name = bfd_symbol_error_name;
654
132k
  else if (sym_sec && *name == '\0')
655
0
    name = bfd_section_name (sym_sec);
656
657
196k
  return name;
658
196k
}
659
660
/* Return the name of the group signature symbol.  Why isn't the
661
   signature just a string?  */
662
663
static const char *
664
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
665
10.6k
{
666
10.6k
  Elf_Internal_Shdr *hdr;
667
10.6k
  unsigned char esym[sizeof (Elf64_External_Sym)];
668
10.6k
  Elf_External_Sym_Shndx eshndx;
669
10.6k
  Elf_Internal_Sym isym;
670
671
  /* First we need to ensure the symbol table is available.  Make sure
672
     that it is a symbol table section.  */
673
10.6k
  if (ghdr->sh_link >= elf_numsections (abfd))
674
31
    return NULL;
675
10.5k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
676
10.5k
  if (hdr->sh_type != SHT_SYMTAB
677
9.88k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
678
779
    return NULL;
679
680
  /* Go read the symbol.  */
681
9.81k
  hdr = &elf_symtab_hdr (abfd);
682
9.81k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
683
9.81k
          &isym, esym, &eshndx) == NULL)
684
2.57k
    return NULL;
685
686
7.23k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
687
9.81k
}
688
689
static bool
690
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
691
16.3k
{
692
16.3k
  return (shdr->sh_size >= minsize
693
16.0k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
694
14.6k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
695
14.3k
    && shdr->bfd_section != NULL);
696
16.3k
}
697
698
699
/* Set next_in_group, sec_group list pointers, and group names.  */
700
701
static bool
702
process_sht_group_entries (bfd *abfd,
703
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
704
14.2k
{
705
14.2k
  unsigned char *contents;
706
707
  /* Read the raw contents.  */
708
14.2k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
709
1.10k
    {
710
1.10k
      _bfd_error_handler
711
  /* xgettext:c-format */
712
1.10k
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
713
1.10k
      return false;
714
1.10k
    }
715
716
13.1k
  asection *last_elt = NULL;
717
13.1k
  const char *gname = NULL;
718
13.1k
  unsigned char *p = contents + ghdr->sh_size;
719
610k
  while (1)
720
610k
    {
721
610k
      unsigned int idx;
722
610k
      Elf_Internal_Shdr *shdr;
723
610k
      asection *elt;
724
725
610k
      p -= 4;
726
610k
      idx = H_GET_32 (abfd, p);
727
610k
      if (p == contents)
728
8.45k
  {
729
8.45k
    if ((idx & GRP_COMDAT) != 0)
730
3.05k
      ghdr->bfd_section->flags
731
3.05k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
732
8.45k
    break;
733
8.45k
  }
734
735
602k
      if (idx == 0
736
447k
    || idx >= elf_numsections (abfd)
737
70.0k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
738
62.9k
    || ((elt = shdr->bfd_section) != NULL
739
40.8k
        && elf_sec_group (elt) != NULL
740
19.1k
        && elf_sec_group (elt) != ghdr->bfd_section))
741
542k
  {
742
542k
    _bfd_error_handler
743
542k
      (_("%pB: invalid entry (%#x) in group [%u]"),
744
542k
       abfd, idx, gidx);
745
542k
    continue;
746
542k
  }
747
748
      /* PR binutils/23199: According to the ELF gABI all sections in
749
   a group must be marked with SHF_GROUP, but some tools
750
   generate broken objects.  Fix them up here.  */
751
60.3k
      shdr->sh_flags |= SHF_GROUP;
752
753
60.3k
      if (elt == NULL)
754
22.0k
  {
755
22.0k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
756
9.42k
      {
757
9.42k
        const char *name = bfd_elf_string_from_elf_section
758
9.42k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
759
760
9.42k
        _bfd_error_handler
761
    /* xgettext:c-format */
762
9.42k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
763
9.42k
     abfd, shdr->sh_type, name, gidx);
764
9.42k
      }
765
22.0k
    continue;
766
22.0k
  }
767
768
      /* Don't try to add a section to elf_next_in_group list twice.  */
769
38.3k
      if (elf_sec_group (elt) != NULL)
770
16.5k
  continue;
771
772
21.7k
      if (last_elt == NULL)
773
10.6k
  {
774
    /* Start a circular list with one element.
775
       It will be in reverse order to match what gas does.  */
776
10.6k
    elf_next_in_group (elt) = elt;
777
    /* Point the group section to it.  */
778
10.6k
    elf_next_in_group (ghdr->bfd_section) = elt;
779
10.6k
    gname = group_signature (abfd, ghdr);
780
10.6k
    if (gname == NULL)
781
4.65k
      {
782
4.65k
        free (contents);
783
4.65k
        return false;
784
4.65k
      }
785
10.6k
  }
786
11.1k
      else
787
11.1k
  {
788
11.1k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
789
11.1k
    elf_next_in_group (last_elt) = elt;
790
11.1k
  }
791
17.0k
      last_elt = elt;
792
17.0k
      elf_group_name (elt) = gname;
793
17.0k
      elf_sec_group (elt) = ghdr->bfd_section;
794
17.0k
    }
795
796
8.45k
  free (contents);
797
8.45k
  return last_elt != NULL;
798
13.1k
}
799
800
bool
801
_bfd_elf_setup_sections (bfd *abfd)
802
59.9k
{
803
59.9k
  bool result = true;
804
805
  /* Process SHF_LINK_ORDER.  */
806
564k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
807
504k
    {
808
504k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
809
504k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
810
37.1k
  {
811
37.1k
    unsigned int elfsec = this_hdr->sh_link;
812
    /* An sh_link value of 0 is now allowed.  It indicates that linked
813
       to section has already been discarded, but that the current
814
       section has been retained for some other reason.  This linking
815
       section is still a candidate for later garbage collection
816
       however.  */
817
37.1k
    if (elfsec == 0)
818
30.8k
      {
819
30.8k
        elf_linked_to_section (s) = NULL;
820
30.8k
      }
821
6.31k
    else
822
6.31k
      {
823
6.31k
        asection *linksec = NULL;
824
825
6.31k
        if (elfsec < elf_numsections (abfd))
826
6.16k
    {
827
6.16k
      this_hdr = elf_elfsections (abfd)[elfsec];
828
6.16k
      linksec = this_hdr->bfd_section;
829
6.16k
    }
830
831
        /* PR 1991, 2008:
832
     Some strip/objcopy may leave an incorrect value in
833
     sh_link.  We don't want to proceed.  */
834
6.31k
        if (linksec == NULL)
835
1.94k
    {
836
1.94k
      _bfd_error_handler
837
        /* xgettext:c-format */
838
1.94k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
839
1.94k
         s->owner, elfsec, s);
840
1.94k
      result = false;
841
1.94k
    }
842
843
6.31k
        elf_linked_to_section (s) = linksec;
844
6.31k
      }
845
37.1k
  }
846
504k
    }
847
848
  /* Process section groups.  */
849
816k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
850
756k
    {
851
756k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
852
853
756k
      if (shdr && shdr->sh_type == SHT_GROUP)
854
16.3k
  {
855
16.3k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
856
14.3k
      {
857
14.3k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
858
14.2k
      && !process_sht_group_entries (abfd, shdr, i))
859
8.23k
    result = false;
860
14.3k
      }
861
1.95k
    else
862
1.95k
      {
863
        /* PR binutils/18758: Beware of corrupt binaries with
864
     invalid group data.  */
865
1.95k
        _bfd_error_handler
866
    /* xgettext:c-format */
867
1.95k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
868
1.95k
        result = false;
869
1.95k
      }
870
16.3k
  }
871
756k
    }
872
873
59.9k
  return result;
874
59.9k
}
875
876
bool
877
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
878
0
{
879
0
  return elf_next_in_group (sec) != NULL;
880
0
}
881
882
const char *
883
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
884
0
{
885
0
  if (elf_sec_group (sec) != NULL)
886
0
    return elf_group_name (sec);
887
0
  return NULL;
888
0
}
889
890
/* Make a BFD section from an ELF section.  We store a pointer to the
891
   BFD section in the bfd_section field of the header.  */
892
893
bool
894
_bfd_elf_make_section_from_shdr (bfd *abfd,
895
         Elf_Internal_Shdr *hdr,
896
         const char *name,
897
         int shindex)
898
716k
{
899
716k
  asection *newsect;
900
716k
  flagword flags;
901
716k
  elf_backend_data *bed;
902
716k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
903
904
716k
  if (hdr->bfd_section != NULL)
905
101k
    return true;
906
907
615k
  newsect = bfd_make_section_anyway (abfd, name);
908
615k
  if (newsect == NULL)
909
0
    return false;
910
911
615k
  hdr->bfd_section = newsect;
912
615k
  elf_section_data (newsect)->this_hdr = *hdr;
913
615k
  elf_section_data (newsect)->this_idx = shindex;
914
915
  /* Always use the real type/flags.  */
916
615k
  elf_section_type (newsect) = hdr->sh_type;
917
615k
  elf_section_flags (newsect) = hdr->sh_flags;
918
919
615k
  newsect->filepos = hdr->sh_offset;
920
921
615k
  flags = SEC_NO_FLAGS;
922
615k
  if (hdr->sh_type != SHT_NOBITS)
923
597k
    flags |= SEC_HAS_CONTENTS;
924
615k
  if (hdr->sh_type == SHT_GROUP)
925
22.9k
    flags |= SEC_GROUP;
926
615k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
927
248k
    {
928
248k
      flags |= SEC_ALLOC;
929
248k
      if (hdr->sh_type != SHT_NOBITS)
930
236k
  flags |= SEC_LOAD;
931
248k
    }
932
615k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
933
475k
    flags |= SEC_READONLY;
934
615k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
935
128k
    flags |= SEC_CODE;
936
486k
  else if ((flags & SEC_LOAD) != 0)
937
128k
    flags |= SEC_DATA;
938
615k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
939
101k
    flags |= SEC_MERGE;
940
615k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
941
139k
    flags |= SEC_STRINGS;
942
615k
  if ((hdr->sh_flags & SHF_TLS) != 0)
943
49.2k
    flags |= SEC_THREAD_LOCAL;
944
615k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
945
47.2k
    flags |= SEC_EXCLUDE;
946
947
615k
  newsect->entsize = hdr->sh_entsize;
948
949
615k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
950
615k
    {
951
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
952
   but binutils as of 2019-07-23 did not set the EI_OSABI header
953
   byte.  */
954
173k
    case ELFOSABI_GNU:
955
288k
    case ELFOSABI_FREEBSD:
956
288k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
957
50.0k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
958
      /* Fall through */
959
484k
    case ELFOSABI_NONE:
960
484k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
961
42.5k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
962
484k
      break;
963
615k
    }
964
965
615k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
966
343k
    {
967
      /* The debugging sections appear to be recognized only by name,
968
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
969
343k
      if (name [0] == '.')
970
139k
  {
971
139k
    if (startswith (name, ".debug")
972
86.0k
        || startswith (name, ".gnu.debuglto_.debug_")
973
85.4k
        || startswith (name, ".gnu.linkonce.wi.")
974
84.6k
        || startswith (name, ".zdebug"))
975
56.5k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
976
82.6k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
977
82.3k
       || startswith (name, ".note.gnu"))
978
777
      {
979
777
        flags |= SEC_ELF_OCTETS;
980
777
        opb = 1;
981
777
      }
982
81.9k
    else if (startswith (name, ".line")
983
81.0k
       || startswith (name, ".stab")
984
78.9k
       || streq (name, ".gdb_index"))
985
3.45k
      flags |= SEC_DEBUGGING;
986
139k
  }
987
343k
    }
988
989
615k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
990
615k
      || !bfd_set_section_size (newsect, hdr->sh_size)
991
615k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
992
615k
              & -hdr->sh_addralign)))
993
271
    return false;
994
995
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
996
     only link a single copy of the section.  This is used to support
997
     g++.  g++ will emit each template expansion in its own section.
998
     The symbols will be defined as weak, so that multiple definitions
999
     are permitted.  The GNU linker extension is to actually discard
1000
     all but one of the sections.  */
1001
614k
  if (startswith (name, ".gnu.linkonce")
1002
2.65k
      && elf_next_in_group (newsect) == NULL)
1003
2.65k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1004
1005
614k
  if (!bfd_set_section_flags (newsect, flags))
1006
0
    return false;
1007
1008
614k
  bed = get_elf_backend_data (abfd);
1009
614k
  if (bed->elf_backend_section_flags)
1010
146k
    if (!bed->elf_backend_section_flags (hdr))
1011
0
      return false;
1012
1013
  /* We do not parse the PT_NOTE segments as we are interested even in the
1014
     separate debug info files which may have the segments offsets corrupted.
1015
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1016
614k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1017
10.4k
    {
1018
10.4k
      bfd_byte *contents;
1019
1020
10.4k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1021
1.96k
  return false;
1022
1023
8.44k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1024
8.44k
           hdr->sh_offset, hdr->sh_addralign);
1025
8.44k
      _bfd_elf_munmap_section_contents (newsect, contents);
1026
8.44k
    }
1027
1028
612k
  if ((newsect->flags & SEC_ALLOC) != 0)
1029
248k
    {
1030
248k
      Elf_Internal_Phdr *phdr;
1031
248k
      unsigned int i, nload;
1032
1033
      /* Some ELF linkers produce binaries with all the program header
1034
   p_paddr fields zero.  If we have such a binary with more than
1035
   one PT_LOAD header, then leave the section lma equal to vma
1036
   so that we don't create sections with overlapping lma.  */
1037
248k
      phdr = elf_tdata (abfd)->phdr;
1038
275k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1039
89.0k
  if (phdr->p_paddr != 0)
1040
62.0k
    break;
1041
26.9k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1042
2.26k
    ++nload;
1043
248k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1044
176
  return true;
1045
1046
248k
      phdr = elf_tdata (abfd)->phdr;
1047
3.26M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1048
3.02M
  {
1049
3.02M
    if (((phdr->p_type == PT_LOAD
1050
91.1k
    && (hdr->sh_flags & SHF_TLS) == 0)
1051
2.94M
         || phdr->p_type == PT_TLS)
1052
91.7k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1053
13.3k
      {
1054
13.3k
        if ((newsect->flags & SEC_LOAD) == 0)
1055
910
    newsect->lma = (phdr->p_paddr
1056
910
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1057
12.4k
        else
1058
    /* We used to use the same adjustment for SEC_LOAD
1059
       sections, but that doesn't work if the segment
1060
       is packed with code from multiple VMAs.
1061
       Instead we calculate the section LMA based on
1062
       the segment LMA.  It is assumed that the
1063
       segment will contain sections with contiguous
1064
       LMAs, even if the VMAs are not.  */
1065
12.4k
    newsect->lma = (phdr->p_paddr
1066
12.4k
        + hdr->sh_offset - phdr->p_offset) / opb;
1067
1068
        /* With contiguous segments, we can't tell from file
1069
     offsets whether a section with zero size should
1070
     be placed at the end of one segment or the
1071
     beginning of the next.  Decide based on vaddr.  */
1072
13.3k
        if (hdr->sh_addr >= phdr->p_vaddr
1073
13.3k
      && (hdr->sh_addr + hdr->sh_size
1074
13.3k
          <= phdr->p_vaddr + phdr->p_memsz))
1075
12.0k
    break;
1076
13.3k
      }
1077
3.02M
  }
1078
248k
    }
1079
1080
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1081
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1082
612k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1083
60.0k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1084
59.8k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1085
56.4k
    {
1086
56.4k
      enum { nothing, compress, decompress } action = nothing;
1087
56.4k
      int compression_header_size;
1088
56.4k
      bfd_size_type uncompressed_size;
1089
56.4k
      unsigned int uncompressed_align_power;
1090
56.4k
      enum compression_type ch_type = ch_none;
1091
56.4k
      bool compressed
1092
56.4k
  = bfd_is_section_compressed_info (abfd, newsect,
1093
56.4k
            &compression_header_size,
1094
56.4k
            &uncompressed_size,
1095
56.4k
            &uncompressed_align_power,
1096
56.4k
            &ch_type);
1097
1098
      /* Should we decompress?  */
1099
56.4k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1100
362
  action = decompress;
1101
1102
      /* Should we compress?  Or convert to a different compression?  */
1103
56.0k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1104
3.71k
         && newsect->size != 0
1105
3.56k
         && compression_header_size >= 0
1106
3.46k
         && uncompressed_size > 0)
1107
3.44k
  {
1108
3.44k
    if (!compressed)
1109
3.37k
      action = compress;
1110
76
    else
1111
76
      {
1112
76
        enum compression_type new_ch_type = ch_none;
1113
76
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1114
76
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1115
76
             ? ch_compress_zstd : ch_compress_zlib);
1116
76
        if (new_ch_type != ch_type)
1117
56
    action = compress;
1118
76
      }
1119
3.44k
  }
1120
1121
56.4k
      if (action == compress)
1122
3.42k
  {
1123
3.42k
    if (!bfd_init_section_compress_status (abfd, newsect))
1124
196
      {
1125
196
        _bfd_error_handler
1126
    /* xgettext:c-format */
1127
196
    (_("%pB: unable to compress section %s"), abfd, name);
1128
196
        return false;
1129
196
      }
1130
3.42k
  }
1131
53.0k
      else if (action == decompress)
1132
362
  {
1133
362
    if (!bfd_init_section_decompress_status (abfd, newsect))
1134
229
      {
1135
229
        _bfd_error_handler
1136
    /* xgettext:c-format */
1137
229
    (_("%pB: unable to decompress section %s"), abfd, name);
1138
229
        return false;
1139
229
      }
1140
133
#ifndef HAVE_ZSTD
1141
133
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1142
6
      {
1143
6
        _bfd_error_handler
1144
      /* xgettext:c-format */
1145
6
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1146
6
          "is not built with zstd support"),
1147
6
       abfd, name);
1148
6
        newsect->compress_status = COMPRESS_SECTION_NONE;
1149
6
        return false;
1150
6
      }
1151
127
#endif
1152
127
    if (abfd->is_linker_input
1153
0
        && name[1] == 'z')
1154
0
      {
1155
        /* Rename section from .zdebug_* to .debug_* so that ld
1156
     scripts will see this section as a debug section.  */
1157
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1158
0
        if (new_name == NULL)
1159
0
    return false;
1160
0
        bfd_rename_section (newsect, new_name);
1161
0
      }
1162
127
  }
1163
56.4k
    }
1164
1165
612k
  return true;
1166
612k
}
1167
1168
const char *const bfd_elf_section_type_names[] =
1169
{
1170
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1171
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1172
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1173
};
1174
1175
/* ELF relocs are against symbols.  If we are producing relocatable
1176
   output, and the reloc is against an external symbol, and nothing
1177
   has given us any additional addend, the resulting reloc will also
1178
   be against the same symbol.  In such a case, we don't want to
1179
   change anything about the way the reloc is handled, since it will
1180
   all be done at final link time.  Rather than put special case code
1181
   into bfd_perform_relocation, all the reloc types use this howto
1182
   function, or should call this function for relocatable output.  */
1183
1184
bfd_reloc_status_type
1185
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1186
           arelent *reloc_entry,
1187
           asymbol *symbol,
1188
           void *data ATTRIBUTE_UNUSED,
1189
           asection *input_section,
1190
           bfd *output_bfd,
1191
           char **error_message ATTRIBUTE_UNUSED)
1192
16.0k
{
1193
16.0k
  if (output_bfd != NULL
1194
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1195
0
      && (! reloc_entry->howto->partial_inplace
1196
0
    || reloc_entry->addend == 0))
1197
0
    {
1198
0
      reloc_entry->address += input_section->output_offset;
1199
0
      return bfd_reloc_ok;
1200
0
    }
1201
1202
  /* In some cases the relocation should be treated as output section
1203
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1204
     targets lack section relative relocations and instead use
1205
     ordinary absolute relocations for references between DWARF
1206
     sections.  That is arguably a bug in those targets but it happens
1207
     to work for the usual case of linking to non-loaded ELF debug
1208
     sections with VMAs forced to zero.  PE COFF on the other hand
1209
     doesn't allow a section VMA of zero.  */
1210
16.0k
  if (output_bfd == NULL
1211
16.0k
      && !reloc_entry->howto->pc_relative
1212
15.4k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1213
4.50k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1214
4.33k
    reloc_entry->addend -= symbol->section->output_section->vma;
1215
1216
16.0k
  return bfd_reloc_continue;
1217
16.0k
}
1218

1219
/* Returns TRUE if section A matches section B.
1220
   Names, addresses and links may be different, but everything else
1221
   should be the same.  */
1222
1223
static bool
1224
section_match (const Elf_Internal_Shdr * a,
1225
         const Elf_Internal_Shdr * b)
1226
408
{
1227
408
  if (a->sh_type != b->sh_type
1228
74
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1229
44
      || a->sh_addralign != b->sh_addralign
1230
14
      || a->sh_entsize != b->sh_entsize)
1231
394
    return false;
1232
14
  if (a->sh_type == SHT_SYMTAB
1233
13
      || a->sh_type == SHT_STRTAB)
1234
13
    return true;
1235
1
  return a->sh_size == b->sh_size;
1236
14
}
1237
1238
/* Find a section in OBFD that has the same characteristics
1239
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1240
   none can be found.  Check's section HINT first, as this is likely
1241
   to be the correct section.  */
1242
1243
static unsigned int
1244
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1245
     const unsigned int hint)
1246
47
{
1247
47
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1248
47
  unsigned int i;
1249
1250
47
  BFD_ASSERT (iheader != NULL);
1251
1252
  /* See PR 20922 for a reproducer of the NULL test.  */
1253
47
  if (hint < elf_numsections (obfd)
1254
46
      && oheaders[hint] != NULL
1255
46
      && section_match (oheaders[hint], iheader))
1256
4
    return hint;
1257
1258
395
  for (i = 1; i < elf_numsections (obfd); i++)
1259
362
    {
1260
362
      Elf_Internal_Shdr * oheader = oheaders[i];
1261
1262
362
      if (oheader == NULL)
1263
0
  continue;
1264
362
      if (section_match (oheader, iheader))
1265
  /* FIXME: Do we care if there is a potential for
1266
     multiple matches ?  */
1267
10
  return i;
1268
362
    }
1269
1270
33
  return SHN_UNDEF;
1271
43
}
1272
1273
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1274
   Processor specific section, based upon a matching input section.
1275
   Returns TRUE upon success, FALSE otherwise.  */
1276
1277
static bool
1278
copy_special_section_fields (const bfd *ibfd,
1279
           bfd *obfd,
1280
           const Elf_Internal_Shdr *iheader,
1281
           Elf_Internal_Shdr *oheader,
1282
           const unsigned int secnum)
1283
157
{
1284
157
  elf_backend_data *bed = get_elf_backend_data (obfd);
1285
157
  const Elf_Internal_Shdr **iheaders
1286
157
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1287
157
  bool changed = false;
1288
157
  unsigned int sh_link;
1289
1290
157
  if (oheader->sh_type == SHT_NOBITS)
1291
25
    {
1292
      /* This is a feature for objcopy --only-keep-debug:
1293
   When a section's type is changed to NOBITS, we preserve
1294
   the sh_link and sh_info fields so that they can be
1295
   matched up with the original.
1296
1297
   Note: Strictly speaking these assignments are wrong.
1298
   The sh_link and sh_info fields should point to the
1299
   relevent sections in the output BFD, which may not be in
1300
   the same location as they were in the input BFD.  But
1301
   the whole point of this action is to preserve the
1302
   original values of the sh_link and sh_info fields, so
1303
   that they can be matched up with the section headers in
1304
   the original file.  So strictly speaking we may be
1305
   creating an invalid ELF file, but it is only for a file
1306
   that just contains debug info and only for sections
1307
   without any contents.  */
1308
25
      if (oheader->sh_link == 0)
1309
24
  oheader->sh_link = iheader->sh_link;
1310
25
      if (oheader->sh_info == 0)
1311
25
  oheader->sh_info = iheader->sh_info;
1312
25
      return true;
1313
25
    }
1314
1315
  /* Allow the target a chance to decide how these fields should be set.  */
1316
132
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1317
132
                iheader, oheader))
1318
63
    return true;
1319
1320
  /* We have an iheader which might match oheader, and which has non-zero
1321
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1322
     the section in the output bfd which corresponds to the linked section
1323
     in the input bfd.  */
1324
69
  if (iheader->sh_link != SHN_UNDEF)
1325
46
    {
1326
      /* See PR 20931 for a reproducer.  */
1327
46
      if (iheader->sh_link >= elf_numsections (ibfd))
1328
0
  {
1329
0
    _bfd_error_handler
1330
      /* xgettext:c-format */
1331
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1332
0
       ibfd, iheader->sh_link, secnum);
1333
0
    return false;
1334
0
  }
1335
1336
46
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1337
46
      if (sh_link != SHN_UNDEF)
1338
14
  {
1339
14
    oheader->sh_link = sh_link;
1340
14
    changed = true;
1341
14
  }
1342
32
      else
1343
  /* FIXME: Should we install iheader->sh_link
1344
     if we could not find a match ?  */
1345
32
  _bfd_error_handler
1346
    /* xgettext:c-format */
1347
32
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1348
46
    }
1349
1350
69
  if (iheader->sh_info)
1351
30
    {
1352
      /* The sh_info field can hold arbitrary information, but if the
1353
   SHF_LINK_INFO flag is set then it should be interpreted as a
1354
   section index.  */
1355
30
      if (iheader->sh_flags & SHF_INFO_LINK)
1356
1
  {
1357
1
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1358
1
             iheader->sh_info);
1359
1
    if (sh_link != SHN_UNDEF)
1360
0
      oheader->sh_flags |= SHF_INFO_LINK;
1361
1
  }
1362
29
      else
1363
  /* No idea what it means - just copy it.  */
1364
29
  sh_link = iheader->sh_info;
1365
1366
30
      if (sh_link != SHN_UNDEF)
1367
29
  {
1368
29
    oheader->sh_info = sh_link;
1369
29
    changed = true;
1370
29
  }
1371
1
      else
1372
1
  _bfd_error_handler
1373
    /* xgettext:c-format */
1374
1
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1375
30
    }
1376
1377
69
  return changed;
1378
69
}
1379
1380
/* Copy the program header and other data from one object module to
1381
   another.  */
1382
1383
bool
1384
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1385
387
{
1386
387
  const Elf_Internal_Shdr **iheaders;
1387
387
  Elf_Internal_Shdr **oheaders;
1388
387
  elf_backend_data *bed;
1389
387
  unsigned int i;
1390
1391
387
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1392
0
    return true;
1393
1394
387
  if (!elf_flags_init (obfd))
1395
324
    {
1396
324
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1397
324
      elf_flags_init (obfd) = true;
1398
324
    }
1399
1400
387
  elf_gp (obfd) = elf_gp (ibfd);
1401
1402
  /* Also copy the EI_OSABI field.  */
1403
387
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1404
387
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1405
1406
  /* If set, copy the EI_ABIVERSION field.  */
1407
387
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1408
47
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1409
47
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1410
1411
  /* Copy object attributes.  */
1412
387
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1413
1414
387
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1415
387
  oheaders = elf_elfsections (obfd);
1416
387
  if (iheaders == NULL || oheaders == NULL)
1417
247
    return true;
1418
1419
140
  bed = get_elf_backend_data (obfd);
1420
1421
  /* Possibly copy other fields in the section header.  */
1422
1.82k
  for (i = 1; i < elf_numsections (obfd); i++)
1423
1.68k
    {
1424
1.68k
      unsigned int j;
1425
1.68k
      Elf_Internal_Shdr * oheader = oheaders[i];
1426
1427
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1428
   because of a special case need for generating separate debug info
1429
   files.  See below for more details.  */
1430
1.68k
      if (oheader == NULL
1431
1.68k
    || (oheader->sh_type != SHT_NOBITS
1432
1.65k
        && oheader->sh_type < SHT_LOOS))
1433
1.49k
  continue;
1434
1435
      /* Ignore empty sections, and sections whose
1436
   fields have already been initialised.  */
1437
190
      if (oheader->sh_size == 0
1438
179
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1439
37
  continue;
1440
1441
      /* Scan for the matching section in the input bfd.
1442
   First we try for a direct mapping between the input and
1443
   output sections.  */
1444
1.57k
      for (j = 1; j < elf_numsections (ibfd); j++)
1445
1.57k
  {
1446
1.57k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1447
1448
1.57k
    if (iheader == NULL)
1449
0
      continue;
1450
1451
1.57k
    if (oheader->bfd_section != NULL
1452
1.57k
        && iheader->bfd_section != NULL
1453
1.24k
        && iheader->bfd_section->output_section != NULL
1454
1.24k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1455
153
      {
1456
        /* We have found a connection from the input section to
1457
     the output section.  Attempt to copy the header fields.
1458
     If this fails then do not try any further sections -
1459
     there should only be a one-to-one mapping between
1460
     input and output.  */
1461
153
        if (!copy_special_section_fields (ibfd, obfd,
1462
153
            iheader, oheader, i))
1463
28
    j = elf_numsections (ibfd);
1464
153
        break;
1465
153
      }
1466
1.57k
  }
1467
1468
153
      if (j < elf_numsections (ibfd))
1469
125
  continue;
1470
1471
      /* That failed.  So try to deduce the corresponding input section.
1472
   Unfortunately we cannot compare names as the output string table
1473
   is empty, so instead we check size, address and type.  */
1474
345
      for (j = 1; j < elf_numsections (ibfd); j++)
1475
317
  {
1476
317
    const Elf_Internal_Shdr * iheader = iheaders[j];
1477
1478
317
    if (iheader == NULL)
1479
0
      continue;
1480
1481
    /* Try matching fields in the input section's header.
1482
       Since --only-keep-debug turns all non-debug sections into
1483
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1484
       input type.  */
1485
317
    if ((oheader->sh_type == SHT_NOBITS
1486
317
         || iheader->sh_type == oheader->sh_type)
1487
29
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1488
29
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1489
28
        && iheader->sh_addralign == oheader->sh_addralign
1490
12
        && iheader->sh_entsize == oheader->sh_entsize
1491
12
        && iheader->sh_size == oheader->sh_size
1492
12
        && iheader->sh_addr == oheader->sh_addr
1493
12
        && (iheader->sh_info != oheader->sh_info
1494
12
      || iheader->sh_link != oheader->sh_link))
1495
4
      {
1496
4
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1497
0
    break;
1498
4
      }
1499
317
  }
1500
1501
28
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1502
28
  {
1503
    /* Final attempt.  Call the backend copy function
1504
       with a NULL input section.  */
1505
28
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1506
28
                     NULL, oheader);
1507
28
  }
1508
28
    }
1509
1510
140
  return true;
1511
387
}
1512
1513
static const char *
1514
get_segment_type (unsigned int p_type)
1515
252k
{
1516
252k
  const char *pt;
1517
252k
  switch (p_type)
1518
252k
    {
1519
49.2k
    case PT_NULL: pt = "NULL"; break;
1520
2.87k
    case PT_LOAD: pt = "LOAD"; break;
1521
924
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1522
882
    case PT_INTERP: pt = "INTERP"; break;
1523
321
    case PT_NOTE: pt = "NOTE"; break;
1524
235
    case PT_SHLIB: pt = "SHLIB"; break;
1525
4.80k
    case PT_PHDR: pt = "PHDR"; break;
1526
160
    case PT_TLS: pt = "TLS"; break;
1527
165
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1528
139
    case PT_GNU_STACK: pt = "STACK"; break;
1529
126
    case PT_GNU_RELRO: pt = "RELRO"; break;
1530
1
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1531
192k
    default: pt = NULL; break;
1532
252k
    }
1533
252k
  return pt;
1534
252k
}
1535
1536
/* Print out the program headers.  */
1537
1538
bool
1539
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1540
17.2k
{
1541
17.2k
  FILE *f = (FILE *) farg;
1542
17.2k
  Elf_Internal_Phdr *p;
1543
17.2k
  asection *s;
1544
17.2k
  bfd_byte *dynbuf = NULL;
1545
1546
17.2k
  p = elf_tdata (abfd)->phdr;
1547
17.2k
  if (p != NULL)
1548
11.8k
    {
1549
11.8k
      unsigned int i, c;
1550
1551
11.8k
      fprintf (f, _("\nProgram Header:\n"));
1552
11.8k
      c = elf_elfheader (abfd)->e_phnum;
1553
263k
      for (i = 0; i < c; i++, p++)
1554
252k
  {
1555
252k
    const char *pt = get_segment_type (p->p_type);
1556
252k
    char buf[20];
1557
1558
252k
    if (pt == NULL)
1559
192k
      {
1560
192k
        sprintf (buf, "0x%lx", p->p_type);
1561
192k
        pt = buf;
1562
192k
      }
1563
252k
    fprintf (f, "%8s off    0x", pt);
1564
252k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1565
252k
    fprintf (f, " vaddr 0x");
1566
252k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1567
252k
    fprintf (f, " paddr 0x");
1568
252k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1569
252k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1570
252k
    fprintf (f, "         filesz 0x");
1571
252k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1572
252k
    fprintf (f, " memsz 0x");
1573
252k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1574
252k
    fprintf (f, " flags %c%c%c",
1575
252k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1576
252k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1577
252k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1578
252k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1579
174k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1580
252k
    fprintf (f, "\n");
1581
252k
  }
1582
11.8k
    }
1583
1584
17.2k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1585
17.2k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1586
177
    {
1587
177
      unsigned int elfsec;
1588
177
      unsigned long shlink;
1589
177
      bfd_byte *extdyn, *extdynend;
1590
177
      size_t extdynsize;
1591
177
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1592
1593
177
      fprintf (f, _("\nDynamic Section:\n"));
1594
1595
177
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1596
16
  goto error_return;
1597
1598
161
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1599
161
      if (elfsec == SHN_BAD)
1600
0
  goto error_return;
1601
161
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1602
1603
161
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1604
161
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1605
1606
161
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1607
2.57k
     (size_t) (extdynend - extdyn) >= extdynsize;
1608
2.41k
     extdyn += extdynsize)
1609
2.54k
  {
1610
2.54k
    Elf_Internal_Dyn dyn;
1611
2.54k
    const char *name = "";
1612
2.54k
    char ab[20];
1613
2.54k
    bool stringp;
1614
2.54k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1615
1616
2.54k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1617
1618
2.54k
    if (dyn.d_tag == DT_NULL)
1619
129
      break;
1620
1621
2.41k
    stringp = false;
1622
2.41k
    switch (dyn.d_tag)
1623
2.41k
      {
1624
761
      default:
1625
761
        if (bed->elf_backend_get_target_dtag)
1626
92
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1627
1628
761
        if (streq (name, ""))
1629
761
    {
1630
761
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1631
761
      name = ab;
1632
761
    }
1633
761
        break;
1634
1635
90
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1636
41
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1637
65
      case DT_PLTGOT: name = "PLTGOT"; break;
1638
69
      case DT_HASH: name = "HASH"; break;
1639
91
      case DT_STRTAB: name = "STRTAB"; break;
1640
96
      case DT_SYMTAB: name = "SYMTAB"; break;
1641
61
      case DT_RELA: name = "RELA"; break;
1642
76
      case DT_RELASZ: name = "RELASZ"; break;
1643
63
      case DT_RELAENT: name = "RELAENT"; break;
1644
75
      case DT_STRSZ: name = "STRSZ"; break;
1645
81
      case DT_SYMENT: name = "SYMENT"; break;
1646
69
      case DT_INIT: name = "INIT"; break;
1647
67
      case DT_FINI: name = "FINI"; break;
1648
2
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1649
15
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1650
3
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1651
4
      case DT_REL: name = "REL"; break;
1652
4
      case DT_RELSZ: name = "RELSZ"; break;
1653
5
      case DT_RELENT: name = "RELENT"; break;
1654
5
      case DT_RELR: name = "RELR"; break;
1655
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1656
0
      case DT_RELRENT: name = "RELRENT"; break;
1657
41
      case DT_PLTREL: name = "PLTREL"; break;
1658
62
      case DT_DEBUG: name = "DEBUG"; break;
1659
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1660
36
      case DT_JMPREL: name = "JMPREL"; break;
1661
4
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1662
36
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1663
34
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1664
34
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1665
34
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1666
0
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1667
29
      case DT_FLAGS: name = "FLAGS"; break;
1668
2
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1669
1
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1670
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1671
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1672
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1673
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1674
0
      case DT_FEATURE: name = "FEATURE"; break;
1675
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1676
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1677
1
      case DT_SYMINENT: name = "SYMINENT"; break;
1678
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1679
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1680
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1681
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1682
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1683
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1684
37
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1685
3
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1686
38
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1687
72
      case DT_VERSYM: name = "VERSYM"; break;
1688
0
      case DT_VERDEF: name = "VERDEF"; break;
1689
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1690
71
      case DT_VERNEED: name = "VERNEED"; break;
1691
64
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1692
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1693
0
      case DT_USED: name = "USED"; break;
1694
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1695
69
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1696
2.41k
      }
1697
1698
2.41k
    fprintf (f, "  %-20s ", name);
1699
2.41k
    if (! stringp)
1700
2.30k
      {
1701
2.30k
        fprintf (f, "0x");
1702
2.30k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1703
2.30k
      }
1704
107
    else
1705
107
      {
1706
107
        const char *string;
1707
107
        unsigned int tagv = dyn.d_un.d_val;
1708
1709
107
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1710
107
        if (string == NULL)
1711
4
    goto error_return;
1712
103
        fprintf (f, "%s", string);
1713
103
      }
1714
2.41k
    fprintf (f, "\n");
1715
2.41k
  }
1716
1717
157
      _bfd_elf_munmap_section_contents (s, dynbuf);
1718
157
      dynbuf = NULL;
1719
157
    }
1720
1721
17.2k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1722
17.1k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1723
168
    {
1724
168
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1725
84
  return false;
1726
168
    }
1727
1728
17.1k
  if (elf_dynverdef (abfd) != 0)
1729
12
    {
1730
12
      Elf_Internal_Verdef *t;
1731
1732
12
      fprintf (f, _("\nVersion definitions:\n"));
1733
24
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1734
12
  {
1735
12
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1736
12
       t->vd_flags, t->vd_hash,
1737
12
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1738
12
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1739
0
      {
1740
0
        Elf_Internal_Verdaux *a;
1741
1742
0
        fprintf (f, "\t");
1743
0
        for (a = t->vd_auxptr->vda_nextptr;
1744
0
       a != NULL;
1745
0
       a = a->vda_nextptr)
1746
0
    fprintf (f, "%s ",
1747
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1748
0
        fprintf (f, "\n");
1749
0
      }
1750
12
  }
1751
12
    }
1752
1753
17.1k
  if (elf_dynverref (abfd) != 0)
1754
83
    {
1755
83
      Elf_Internal_Verneed *t;
1756
1757
83
      fprintf (f, _("\nVersion References:\n"));
1758
166
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1759
83
  {
1760
83
    Elf_Internal_Vernaux *a;
1761
1762
83
    fprintf (f, _("  required from %s:\n"),
1763
83
       t->vn_filename ? t->vn_filename : "<corrupt>");
1764
204
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1765
121
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1766
121
         a->vna_flags, a->vna_other,
1767
121
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1768
83
  }
1769
83
    }
1770
1771
17.1k
  return true;
1772
1773
20
 error_return:
1774
20
  _bfd_elf_munmap_section_contents (s, dynbuf);
1775
20
  return false;
1776
17.2k
}
1777
1778
/* Find the file offset corresponding to VMA by using the program
1779
   headers.  */
1780
1781
static file_ptr
1782
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1783
     size_t size, size_t *max_size_p)
1784
3.16k
{
1785
3.16k
  Elf_Internal_Phdr *seg;
1786
3.16k
  size_t i;
1787
1788
11.5k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1789
11.2k
    if (seg->p_type == PT_LOAD
1790
4.49k
  && vma >= (seg->p_vaddr & -seg->p_align)
1791
3.65k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1792
2.80k
      {
1793
2.80k
  if (max_size_p)
1794
35
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1795
2.80k
  return vma - seg->p_vaddr + seg->p_offset;
1796
2.80k
      }
1797
1798
362
  if (max_size_p)
1799
35
    *max_size_p = 0;
1800
362
  bfd_set_error (bfd_error_invalid_operation);
1801
362
  return (file_ptr) -1;
1802
3.16k
}
1803
1804
/* Convert hash table to internal form.  */
1805
1806
static bfd_vma *
1807
get_hash_table_data (bfd *abfd, bfd_size_type number,
1808
         unsigned int ent_size, bfd_size_type filesize)
1809
446
{
1810
446
  unsigned char *e_data = NULL;
1811
446
  bfd_vma *i_data = NULL;
1812
446
  bfd_size_type size;
1813
446
  void *e_data_addr;
1814
446
  size_t e_data_size ATTRIBUTE_UNUSED;
1815
1816
446
  if (ent_size != 4 && ent_size != 8)
1817
0
    return NULL;
1818
1819
446
  if ((size_t) number != number)
1820
0
    {
1821
0
      bfd_set_error (bfd_error_file_too_big);
1822
0
      return NULL;
1823
0
    }
1824
1825
446
  size = ent_size * number;
1826
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1827
     attempting to allocate memory when the read is bound to fail.  */
1828
446
  if (size > filesize
1829
411
      || number >= ~(size_t) 0 / ent_size
1830
411
      || number >= ~(size_t) 0 / sizeof (*i_data))
1831
35
    {
1832
35
      bfd_set_error (bfd_error_file_too_big);
1833
35
      return NULL;
1834
35
    }
1835
1836
411
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1837
411
  if (e_data == NULL)
1838
26
    return NULL;
1839
1840
385
  i_data = bfd_malloc (number * sizeof (*i_data));
1841
385
  if (i_data == NULL)
1842
0
    {
1843
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1844
0
      return NULL;
1845
0
    }
1846
1847
385
  if (ent_size == 4)
1848
203k
    while (number--)
1849
202k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1850
0
  else
1851
0
    while (number--)
1852
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1853
1854
385
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1855
385
  return i_data;
1856
385
}
1857
1858
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1859
   support DT_MIPS_XHASH?  */
1860
115k
#define DT_MIPS_XHASH        0x70000036
1861
1862
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1863
1864
bool
1865
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1866
            Elf_Internal_Phdr *phdrs, size_t phnum,
1867
            bfd_size_type filesize)
1868
3.77k
{
1869
3.77k
  bfd_byte *extdyn, *extdynend;
1870
3.77k
  size_t extdynsize;
1871
3.77k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1872
3.77k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1873
3.77k
        Elf_Internal_Sym *);
1874
3.77k
  Elf_Internal_Dyn dyn;
1875
3.77k
  bfd_vma dt_hash = 0;
1876
3.77k
  bfd_vma dt_gnu_hash = 0;
1877
3.77k
  bfd_vma dt_mips_xhash = 0;
1878
3.77k
  bfd_vma dt_strtab = 0;
1879
3.77k
  bfd_vma dt_symtab = 0;
1880
3.77k
  size_t dt_strsz = 0;
1881
3.77k
  bfd_vma dt_versym = 0;
1882
3.77k
  bfd_vma dt_verdef = 0;
1883
3.77k
  bfd_vma dt_verneed = 0;
1884
3.77k
  bfd_byte *dynbuf = NULL;
1885
3.77k
  char *strbuf = NULL;
1886
3.77k
  bfd_vma *gnubuckets = NULL;
1887
3.77k
  bfd_vma *gnuchains = NULL;
1888
3.77k
  bfd_vma *mipsxlat = NULL;
1889
3.77k
  file_ptr saved_filepos, filepos;
1890
3.77k
  bool res = false;
1891
3.77k
  size_t amt;
1892
3.77k
  bfd_byte *esymbuf = NULL, *esym;
1893
3.77k
  bfd_size_type symcount;
1894
3.77k
  Elf_Internal_Sym *isymbuf = NULL;
1895
3.77k
  Elf_Internal_Sym *isym, *isymend;
1896
3.77k
  bfd_byte *versym = NULL;
1897
3.77k
  bfd_byte *verdef = NULL;
1898
3.77k
  bfd_byte *verneed = NULL;
1899
3.77k
  size_t verdef_size = 0;
1900
3.77k
  size_t verneed_size = 0;
1901
3.77k
  size_t extsym_size;
1902
3.77k
  elf_backend_data *bed;
1903
3.77k
  void *dynbuf_addr = NULL;
1904
3.77k
  void *esymbuf_addr = NULL;
1905
3.77k
  size_t dynbuf_size = 0;
1906
3.77k
  size_t esymbuf_size = 0;
1907
1908
  /* Return TRUE if symbol table is bad.  */
1909
3.77k
  if (elf_bad_symtab (abfd))
1910
0
    return true;
1911
1912
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1913
3.77k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1914
0
    return true;
1915
1916
3.77k
  bed = get_elf_backend_data (abfd);
1917
1918
  /* Save file position for elf_object_p.  */
1919
3.77k
  saved_filepos = bfd_tell (abfd);
1920
1921
3.77k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1922
0
    goto error_return;
1923
1924
3.77k
  dynbuf_size = phdr->p_filesz;
1925
3.77k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1926
3.77k
  if (dynbuf == NULL)
1927
78
    goto error_return;
1928
1929
3.69k
  extsym_size = bed->s->sizeof_sym;
1930
3.69k
  extdynsize = bed->s->sizeof_dyn;
1931
3.69k
  swap_dyn_in = bed->s->swap_dyn_in;
1932
1933
3.69k
  extdyn = dynbuf;
1934
3.69k
  if (phdr->p_filesz < extdynsize)
1935
92
    goto error_return;
1936
3.60k
  extdynend = extdyn + phdr->p_filesz;
1937
70.6k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1938
68.7k
    {
1939
68.7k
      swap_dyn_in (abfd, extdyn, &dyn);
1940
1941
68.7k
      if (dyn.d_tag == DT_NULL)
1942
1.49k
  break;
1943
1944
67.2k
      switch (dyn.d_tag)
1945
67.2k
  {
1946
1.22k
  case DT_HASH:
1947
1.22k
    dt_hash = dyn.d_un.d_val;
1948
1.22k
    break;
1949
1.10k
  case DT_GNU_HASH:
1950
1.10k
    if (bed->elf_machine_code != EM_MIPS
1951
920
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1952
920
      dt_gnu_hash = dyn.d_un.d_val;
1953
1.10k
    break;
1954
1.68k
  case DT_STRTAB:
1955
1.68k
    dt_strtab = dyn.d_un.d_val;
1956
1.68k
    break;
1957
1.55k
  case DT_SYMTAB:
1958
1.55k
    dt_symtab = dyn.d_un.d_val;
1959
1.55k
    break;
1960
1.50k
  case DT_STRSZ:
1961
1.50k
    dt_strsz = dyn.d_un.d_val;
1962
1.50k
    break;
1963
672
  case DT_SYMENT:
1964
672
    if (dyn.d_un.d_val != extsym_size)
1965
184
      goto error_return;
1966
488
    break;
1967
746
  case DT_VERSYM:
1968
746
    dt_versym = dyn.d_un.d_val;
1969
746
    break;
1970
377
  case DT_VERDEF:
1971
377
    dt_verdef = dyn.d_un.d_val;
1972
377
    break;
1973
368
  case DT_VERNEED:
1974
368
    dt_verneed = dyn.d_un.d_val;
1975
368
    break;
1976
57.9k
  default:
1977
57.9k
    if (dyn.d_tag == DT_MIPS_XHASH
1978
815
        && (bed->elf_machine_code == EM_MIPS
1979
395
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1980
420
      {
1981
420
        dt_gnu_hash = dyn.d_un.d_val;
1982
420
        dt_mips_xhash = dyn.d_un.d_val;
1983
420
      }
1984
57.9k
    break;
1985
67.2k
  }
1986
67.2k
    }
1987
1988
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1989
     segment.  */
1990
3.41k
  if ((!dt_hash && !dt_gnu_hash)
1991
1.92k
      || !dt_strtab
1992
1.39k
      || !dt_symtab
1993
1.21k
      || !dt_strsz)
1994
2.37k
    goto error_return;
1995
1996
  /* Get dynamic string table.  */
1997
1.04k
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1998
1.04k
  if (filepos == (file_ptr) -1
1999
925
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2000
174
    goto error_return;
2001
2002
  /* Dynamic string table must be valid until ABFD is closed.  */
2003
867
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2004
867
  if (strbuf == NULL)
2005
22
    goto error_return;
2006
845
  if (strbuf[dt_strsz - 1] != 0)
2007
286
    {
2008
      /* It is an error if a string table is't terminated.  */
2009
286
      _bfd_error_handler
2010
  /* xgettext:c-format */
2011
286
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2012
286
      strbuf[dt_strsz - 1] = 0;
2013
286
    }
2014
2015
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2016
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2017
845
  if (dt_hash)
2018
354
    {
2019
354
      unsigned char nb[16];
2020
354
      unsigned int hash_ent_size;
2021
2022
354
      switch (bed->elf_machine_code)
2023
354
  {
2024
0
  case EM_ALPHA:
2025
2
  case EM_S390:
2026
2
  case EM_S390_OLD:
2027
2
    if (bed->s->elfclass == ELFCLASS64)
2028
0
      {
2029
0
        hash_ent_size = 8;
2030
0
        break;
2031
0
      }
2032
    /* FALLTHROUGH */
2033
354
  default:
2034
354
    hash_ent_size = 4;
2035
354
    break;
2036
354
  }
2037
2038
354
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2039
354
         NULL);
2040
354
      if (filepos == (file_ptr) -1
2041
319
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2042
291
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2043
78
  goto error_return;
2044
2045
      /* The number of dynamic symbol table entries equals the number
2046
   of chains.  */
2047
276
      if (hash_ent_size == 8)
2048
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2049
276
      else
2050
276
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2051
276
    }
2052
491
  else
2053
491
    {
2054
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2055
   bindings are in hash table.  Since in dynamic symbol table,
2056
   all symbols with STB_LOCAL binding are placed before symbols
2057
   with other bindings and all undefined symbols are placed
2058
   before defined ones, the highest symbol index in DT_GNU_HASH
2059
   is the highest dynamic symbol table index.  */
2060
491
      unsigned char nb[16];
2061
491
      bfd_vma ngnubuckets;
2062
491
      bfd_vma gnusymidx;
2063
491
      size_t i, ngnuchains;
2064
491
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2065
491
      bfd_vma buckets_vma;
2066
2067
491
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2068
491
         sizeof (nb), NULL);
2069
491
      if (filepos == (file_ptr) -1
2070
460
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2071
436
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2072
58
  goto error_return;
2073
2074
433
      ngnubuckets = bfd_get_32 (abfd, nb);
2075
433
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2076
433
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2077
433
      buckets_vma = dt_gnu_hash + 16;
2078
433
      if (bed->s->elfclass == ELFCLASS32)
2079
0
  buckets_vma += bitmaskwords * 4;
2080
433
      else
2081
433
  buckets_vma += bitmaskwords * 8;
2082
433
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2083
433
      if (filepos == (file_ptr) -1
2084
406
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2085
72
  goto error_return;
2086
2087
361
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2088
361
      if (gnubuckets == NULL)
2089
61
  goto error_return;
2090
2091
175k
      for (i = 0; i < ngnubuckets; i++)
2092
174k
  if (gnubuckets[i] != 0)
2093
137k
    {
2094
137k
      if (gnubuckets[i] < gnusymidx)
2095
29
        goto error_return;
2096
2097
137k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2098
14.2k
        maxchain = gnubuckets[i];
2099
137k
    }
2100
2101
271
      if (maxchain == 0xffffffff)
2102
58
  {
2103
58
    symcount = 0;
2104
58
    goto empty_gnu_hash;
2105
58
  }
2106
2107
213
      maxchain -= gnusymidx;
2108
213
      filepos = offset_from_vma (phdrs, phnum,
2109
213
         buckets_vma + 4 * (ngnubuckets + maxchain),
2110
213
         4, NULL);
2111
213
      if (filepos == (file_ptr) -1
2112
171
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2113
116
  goto error_return;
2114
2115
97
      do
2116
2.75k
  {
2117
2.75k
    if (bfd_read (nb, 4, abfd) != 4)
2118
12
      goto error_return;
2119
2.73k
    ++maxchain;
2120
2.73k
    if (maxchain == 0)
2121
0
      goto error_return;
2122
2.73k
  }
2123
2.73k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2124
2125
85
      filepos = offset_from_vma (phdrs, phnum,
2126
85
         buckets_vma + 4 * ngnubuckets,
2127
85
         4, NULL);
2128
85
      if (filepos == (file_ptr) -1
2129
85
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2130
0
  goto error_return;
2131
2132
85
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2133
85
      if (gnuchains == NULL)
2134
0
  goto error_return;
2135
85
      ngnuchains = maxchain;
2136
2137
85
      if (dt_mips_xhash)
2138
0
  {
2139
0
    filepos = offset_from_vma (phdrs, phnum,
2140
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2141
0
             4, NULL);
2142
0
    if (filepos == (file_ptr) -1
2143
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2144
0
      goto error_return;
2145
2146
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2147
0
    if (mipsxlat == NULL)
2148
0
      goto error_return;
2149
0
  }
2150
2151
85
      symcount = 0;
2152
3.36k
      for (i = 0; i < ngnubuckets; ++i)
2153
3.28k
  if (gnubuckets[i] != 0)
2154
1.73k
    {
2155
1.73k
      bfd_vma si = gnubuckets[i];
2156
1.73k
      bfd_vma off = si - gnusymidx;
2157
1.73k
      do
2158
8.85k
        {
2159
8.85k
    if (mipsxlat)
2160
0
      {
2161
0
        if (mipsxlat[off] >= symcount)
2162
0
          symcount = mipsxlat[off] + 1;
2163
0
      }
2164
8.85k
    else
2165
8.85k
      {
2166
8.85k
        if (si >= symcount)
2167
2.48k
          symcount = si + 1;
2168
8.85k
      }
2169
8.85k
    si++;
2170
8.85k
        }
2171
8.85k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2172
1.73k
    }
2173
85
    }
2174
2175
  /* Swap in dynamic symbol table.  */
2176
361
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2177
0
    {
2178
0
      bfd_set_error (bfd_error_file_too_big);
2179
0
      goto error_return;
2180
0
    }
2181
2182
361
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2183
361
  if (filepos == (file_ptr) -1
2184
303
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2185
105
    goto error_return;
2186
256
  esymbuf_size = amt;
2187
256
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2188
256
         &esymbuf_addr, &esymbuf_size);
2189
256
  if (esymbuf == NULL)
2190
29
    goto error_return;
2191
2192
227
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2193
0
    {
2194
0
      bfd_set_error (bfd_error_file_too_big);
2195
0
      goto error_return;
2196
0
    }
2197
2198
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2199
227
  isymbuf = bfd_alloc (abfd, amt);
2200
227
  if (isymbuf == NULL)
2201
0
    goto error_return;
2202
2203
227
  swap_symbol_in = bed->s->swap_symbol_in;
2204
2205
  /* Convert the symbols to internal form.  */
2206
227
  isymend = isymbuf + symcount;
2207
227
  for (esym = esymbuf, isym = isymbuf;
2208
915
       isym < isymend;
2209
688
       esym += extsym_size, isym++)
2210
736
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2211
717
  || isym->st_name >= dt_strsz)
2212
48
      {
2213
48
  bfd_set_error (bfd_error_invalid_operation);
2214
48
  goto error_return;
2215
48
      }
2216
2217
179
  if (dt_versym)
2218
120
    {
2219
      /* Swap in DT_VERSYM.  */
2220
120
      if (_bfd_mul_overflow (symcount, 2, &amt))
2221
0
  {
2222
0
    bfd_set_error (bfd_error_file_too_big);
2223
0
    goto error_return;
2224
0
  }
2225
2226
120
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2227
120
      if (filepos == (file_ptr) -1
2228
102
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2229
34
  goto error_return;
2230
2231
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2232
86
      versym = _bfd_mmap_persistent (abfd, amt);
2233
2234
86
      if (dt_verdef)
2235
37
  {
2236
    /* Read in DT_VERDEF.  */
2237
37
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2238
37
             0, &verdef_size);
2239
37
    if (filepos == (file_ptr) -1
2240
14
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2241
34
      goto error_return;
2242
2243
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2244
3
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2245
3
  }
2246
2247
52
      if (dt_verneed)
2248
33
  {
2249
    /* Read in DT_VERNEED.  */
2250
33
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2251
33
             0, &verneed_size);
2252
33
    if (filepos == (file_ptr) -1
2253
21
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2254
15
      goto error_return;
2255
2256
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2257
18
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2258
18
  }
2259
52
    }
2260
2261
154
 empty_gnu_hash:
2262
154
  elf_tdata (abfd)->dt_strtab = strbuf;
2263
154
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2264
154
  elf_tdata (abfd)->dt_symtab = isymbuf;
2265
154
  elf_tdata (abfd)->dt_symtab_count = symcount;
2266
154
  elf_tdata (abfd)->dt_versym = versym;
2267
154
  elf_tdata (abfd)->dt_verdef = verdef;
2268
154
  elf_tdata (abfd)->dt_verneed = verneed;
2269
154
  elf_tdata (abfd)->dt_verdef_count
2270
154
    = verdef_size / sizeof (Elf_External_Verdef);
2271
154
  elf_tdata (abfd)->dt_verneed_count
2272
154
    = verneed_size / sizeof (Elf_External_Verneed);
2273
2274
154
  res = true;
2275
2276
3.77k
 error_return:
2277
  /* Restore file position for elf_object_p.  */
2278
3.77k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2279
0
    res = false;
2280
3.77k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2281
3.77k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2282
3.77k
  free (gnubuckets);
2283
3.77k
  free (gnuchains);
2284
3.77k
  free (mipsxlat);
2285
3.77k
  return res;
2286
154
}
2287
2288
/* Reconstruct section from dynamic symbol.  */
2289
2290
asection *
2291
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2292
            Elf_Internal_Sym *isym)
2293
1
{
2294
1
  asection *sec;
2295
1
  flagword flags;
2296
2297
1
  if (!elf_use_dt_symtab_p (abfd))
2298
0
    return NULL;
2299
2300
1
  flags = SEC_ALLOC | SEC_LOAD;
2301
1
  switch (ELF_ST_TYPE (isym->st_info))
2302
1
    {
2303
0
    case STT_FUNC:
2304
0
    case STT_GNU_IFUNC:
2305
0
      sec = bfd_get_section_by_name (abfd, ".text");
2306
0
      if (sec == NULL)
2307
0
  sec = bfd_make_section_with_flags (abfd,
2308
0
             ".text",
2309
0
             flags | SEC_CODE);
2310
0
      break;
2311
0
    case STT_COMMON:
2312
0
      sec = bfd_com_section_ptr;
2313
0
      break;
2314
0
    case STT_OBJECT:
2315
0
      sec = bfd_get_section_by_name (abfd, ".data");
2316
0
      if (sec == NULL)
2317
0
  sec = bfd_make_section_with_flags (abfd,
2318
0
             ".data",
2319
0
             flags | SEC_DATA);
2320
0
      break;
2321
0
    case STT_TLS:
2322
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2323
0
      if (sec == NULL)
2324
0
  sec = bfd_make_section_with_flags (abfd,
2325
0
             ".tdata",
2326
0
             (flags
2327
0
              | SEC_DATA
2328
0
              | SEC_THREAD_LOCAL));
2329
0
      break;
2330
1
    default:
2331
1
      sec = bfd_abs_section_ptr;
2332
1
      break;
2333
1
    }
2334
2335
1
  return sec;
2336
1
}
2337
2338
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2339
   and return symbol version for symbol version itself.   */
2340
2341
const char *
2342
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2343
            bool base_p,
2344
            bool *hidden)
2345
62.1k
{
2346
62.1k
  const char *version_string = NULL;
2347
62.1k
  if ((elf_dynversym (abfd) != 0
2348
6.41k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2349
55.7k
      || (elf_tdata (abfd)->dt_versym != NULL
2350
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2351
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2352
6.34k
    {
2353
6.34k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2354
2355
6.34k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2356
6.34k
      vernum &= VERSYM_VERSION;
2357
2358
6.34k
      if (vernum == 0)
2359
1.66k
  version_string = "";
2360
4.67k
      else if (vernum == 1
2361
3.55k
         && (vernum > elf_tdata (abfd)->cverdefs
2362
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2363
0
           == VER_FLG_BASE)))
2364
3.55k
  version_string = base_p ? "Base" : "";
2365
1.12k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2366
0
  {
2367
0
    const char *nodename
2368
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2369
0
    version_string = "";
2370
0
    if (base_p
2371
0
        || nodename == NULL
2372
0
        || symbol->name == NULL
2373
0
        || ! streq (symbol->name, nodename))
2374
0
      version_string = nodename;
2375
0
  }
2376
1.12k
      else
2377
1.12k
  {
2378
1.12k
    Elf_Internal_Verneed *t;
2379
2380
1.12k
    version_string = _("<corrupt>");
2381
1.12k
    for (t = elf_tdata (abfd)->verref;
2382
2.24k
         t != NULL;
2383
1.12k
         t = t->vn_nextref)
2384
1.12k
      {
2385
1.12k
        Elf_Internal_Vernaux *a;
2386
2387
2.40k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2388
2.15k
    {
2389
2.15k
      if (a->vna_other == vernum)
2390
873
        {
2391
873
          *hidden = true;
2392
873
          version_string = a->vna_nodename;
2393
873
          break;
2394
873
        }
2395
2.15k
    }
2396
1.12k
      }
2397
1.12k
  }
2398
6.34k
    }
2399
62.1k
  return version_string;
2400
62.1k
}
2401
2402
/* Display ELF-specific fields of a symbol.  */
2403
2404
void
2405
_bfd_elf_print_symbol (bfd *abfd,
2406
           void *filep,
2407
           asymbol *symbol,
2408
           bfd_print_symbol_type how)
2409
0
{
2410
0
  FILE *file = (FILE *) filep;
2411
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2412
0
       ? symbol->name : _("<corrupt>"));
2413
2414
0
  switch (how)
2415
0
    {
2416
0
    case bfd_print_symbol_name:
2417
0
      fprintf (file, "%s", symname);
2418
0
      break;
2419
0
    case bfd_print_symbol_more:
2420
0
      fprintf (file, "elf ");
2421
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2422
0
      fprintf (file, " %x", symbol->flags);
2423
0
      break;
2424
0
    case bfd_print_symbol_all:
2425
0
      {
2426
0
  const char *section_name;
2427
0
  const char *name = NULL;
2428
0
  elf_backend_data *bed;
2429
0
  unsigned char st_other;
2430
0
  bfd_vma val;
2431
0
  const char *version_string;
2432
0
  bool hidden;
2433
2434
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2435
2436
0
  bed = get_elf_backend_data (abfd);
2437
0
  if (bed->elf_backend_print_symbol_all)
2438
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2439
2440
0
  if (name != NULL)
2441
0
    symname = name;
2442
0
  else
2443
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2444
2445
0
  fprintf (file, " %s\t", section_name);
2446
  /* Print the "other" value for a symbol.  For common symbols,
2447
     we've already printed the size; now print the alignment.
2448
     For other symbols, we have no specified alignment, and
2449
     we've printed the address; now print the size.  */
2450
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2451
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2452
0
  else
2453
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2454
0
  bfd_fprintf_vma (abfd, file, val);
2455
2456
  /* If we have version information, print it.  */
2457
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2458
0
                   symbol,
2459
0
                   true,
2460
0
                   &hidden);
2461
0
  if (version_string)
2462
0
    {
2463
0
      if (!hidden)
2464
0
        fprintf (file, "  %-11s", version_string);
2465
0
      else
2466
0
        {
2467
0
    int i;
2468
2469
0
    fprintf (file, " (%s)", version_string);
2470
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2471
0
      putc (' ', file);
2472
0
        }
2473
0
    }
2474
2475
  /* If the st_other field is not zero, print it.  */
2476
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2477
2478
0
  switch (st_other)
2479
0
    {
2480
0
    case 0: break;
2481
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2482
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2483
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2484
0
    default:
2485
      /* Some other non-defined flags are also present, so print
2486
         everything hex.  */
2487
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2488
0
    }
2489
2490
0
  fprintf (file, " %s", symname);
2491
0
      }
2492
0
      break;
2493
0
    }
2494
0
}
2495

2496
/* ELF .o/exec file reading */
2497
2498
/* Create a new bfd section from an ELF section header.  */
2499
2500
bool
2501
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2502
1.15M
{
2503
1.15M
  Elf_Internal_Shdr *hdr;
2504
1.15M
  Elf_Internal_Ehdr *ehdr;
2505
1.15M
  elf_backend_data *bed;
2506
1.15M
  const char *name;
2507
1.15M
  bool ret = true;
2508
2509
1.15M
  if (shindex >= elf_numsections (abfd))
2510
0
    return false;
2511
2512
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2513
     sh_link or sh_info.  Detect this here, by refusing to load a
2514
     section that we are already in the process of loading.  */
2515
1.15M
  if (elf_tdata (abfd)->being_created[shindex])
2516
44
    {
2517
44
      _bfd_error_handler
2518
44
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2519
44
      return false;
2520
44
    }
2521
1.15M
  elf_tdata (abfd)->being_created[shindex] = true;
2522
2523
1.15M
  hdr = elf_elfsections (abfd)[shindex];
2524
1.15M
  ehdr = elf_elfheader (abfd);
2525
1.15M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2526
1.15M
            hdr->sh_name);
2527
1.15M
  if (name == NULL)
2528
9.23k
    goto fail;
2529
2530
1.15M
  bed = get_elf_backend_data (abfd);
2531
1.15M
  switch (hdr->sh_type)
2532
1.15M
    {
2533
85.3k
    case SHT_NULL:
2534
      /* Inactive section. Throw it away.  */
2535
85.3k
      goto success;
2536
2537
342k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2538
359k
    case SHT_NOBITS:   /* .bss section.  */
2539
361k
    case SHT_HASH:   /* .hash section.  */
2540
373k
    case SHT_NOTE:   /* .note section.  */
2541
392k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2542
402k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2543
405k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2544
405k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2545
405k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2546
405k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2547
405k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2548
405k
      goto success;
2549
2550
3.51k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2551
3.51k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2552
49
  goto fail;
2553
2554
3.46k
      if (hdr->sh_link > elf_numsections (abfd))
2555
181
  {
2556
    /* PR 10478: Accept Solaris binaries with a sh_link field
2557
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2558
181
    switch (bfd_get_arch (abfd))
2559
181
      {
2560
121
      case bfd_arch_i386:
2561
181
      case bfd_arch_sparc:
2562
181
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2563
48
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2564
181
    break;
2565
        /* Otherwise fall through.  */
2566
0
      default:
2567
0
        goto fail;
2568
181
      }
2569
181
  }
2570
3.28k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2571
0
  goto fail;
2572
3.28k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2573
1.62k
  {
2574
1.62k
    Elf_Internal_Shdr *dynsymhdr;
2575
2576
    /* The shared libraries distributed with hpux11 have a bogus
2577
       sh_link field for the ".dynamic" section.  Find the
2578
       string table for the ".dynsym" section instead.  */
2579
1.62k
    if (elf_dynsymtab (abfd) != 0)
2580
319
      {
2581
319
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2582
319
        hdr->sh_link = dynsymhdr->sh_link;
2583
319
      }
2584
1.31k
    else
2585
1.31k
      {
2586
1.31k
        unsigned int i, num_sec;
2587
2588
1.31k
        num_sec = elf_numsections (abfd);
2589
22.2k
        for (i = 1; i < num_sec; i++)
2590
21.1k
    {
2591
21.1k
      dynsymhdr = elf_elfsections (abfd)[i];
2592
21.1k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2593
183
        {
2594
183
          hdr->sh_link = dynsymhdr->sh_link;
2595
183
          break;
2596
183
        }
2597
21.1k
    }
2598
1.31k
      }
2599
1.62k
  }
2600
3.46k
      goto success;
2601
2602
145k
    case SHT_SYMTAB:   /* A symbol table.  */
2603
145k
      if (elf_onesymtab (abfd) == shindex)
2604
112k
  goto success;
2605
2606
33.0k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2607
831
  goto fail;
2608
2609
32.2k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2610
1.45k
  {
2611
1.45k
    if (hdr->sh_size != 0)
2612
234
      goto fail;
2613
    /* Some assemblers erroneously set sh_info to one with a
2614
       zero sh_size.  ld sees this as a global symbol count
2615
       of (unsigned) -1.  Fix it here.  */
2616
1.22k
    hdr->sh_info = 0;
2617
1.22k
    goto success;
2618
1.45k
  }
2619
2620
      /* PR 18854: A binary might contain more than one symbol table.
2621
   Unusual, but possible.  Warn, but continue.  */
2622
30.7k
      if (elf_onesymtab (abfd) != 0)
2623
3.20k
  {
2624
3.20k
    _bfd_error_handler
2625
      /* xgettext:c-format */
2626
3.20k
      (_("%pB: warning: multiple symbol tables detected"
2627
3.20k
         " - ignoring the table in section %u"),
2628
3.20k
       abfd, shindex);
2629
3.20k
    goto success;
2630
3.20k
  }
2631
27.5k
      elf_onesymtab (abfd) = shindex;
2632
27.5k
      elf_symtab_hdr (abfd) = *hdr;
2633
27.5k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2634
27.5k
      abfd->flags |= HAS_SYMS;
2635
2636
      /* Sometimes a shared object will map in the symbol table.  If
2637
   SHF_ALLOC is set, and this is a shared object, then we also
2638
   treat this section as a BFD section.  We can not base the
2639
   decision purely on SHF_ALLOC, because that flag is sometimes
2640
   set in a relocatable object file, which would confuse the
2641
   linker.  */
2642
27.5k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2643
1.19k
    && (abfd->flags & DYNAMIC) != 0
2644
283
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2645
283
            shindex))
2646
5
  goto fail;
2647
2648
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2649
   can't read symbols without that section loaded as well.  It
2650
   is most likely specified by the next section header.  */
2651
27.5k
      {
2652
27.5k
  elf_section_list * entry;
2653
27.5k
  unsigned int i, num_sec;
2654
2655
29.8k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2656
5.72k
    if (entry->hdr.sh_link == shindex)
2657
3.39k
      goto success;
2658
2659
24.1k
  num_sec = elf_numsections (abfd);
2660
67.4k
  for (i = shindex + 1; i < num_sec; i++)
2661
43.3k
    {
2662
43.3k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2663
2664
43.3k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2665
567
    && hdr2->sh_link == shindex)
2666
80
        break;
2667
43.3k
    }
2668
2669
24.1k
  if (i == num_sec)
2670
482k
    for (i = 1; i < shindex; i++)
2671
459k
      {
2672
459k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2673
2674
459k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2675
3.52k
      && hdr2->sh_link == shindex)
2676
469
    break;
2677
459k
      }
2678
2679
24.1k
  if (i != shindex)
2680
549
    ret = bfd_section_from_shdr (abfd, i);
2681
  /* else FIXME: we have failed to find the symbol table.
2682
     Should we issue an error?  */
2683
24.1k
  goto success;
2684
27.5k
      }
2685
2686
4.49k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2687
4.49k
      if (elf_dynsymtab (abfd) == shindex)
2688
1.86k
  goto success;
2689
2690
2.63k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2691
415
  goto fail;
2692
2693
2.22k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2694
361
  {
2695
361
    if (hdr->sh_size != 0)
2696
163
      goto fail;
2697
2698
    /* Some linkers erroneously set sh_info to one with a
2699
       zero sh_size.  ld sees this as a global symbol count
2700
       of (unsigned) -1.  Fix it here.  */
2701
198
    hdr->sh_info = 0;
2702
198
    goto success;
2703
361
  }
2704
2705
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2706
   Unusual, but possible.  Warn, but continue.  */
2707
1.86k
      if (elf_dynsymtab (abfd) != 0)
2708
108
  {
2709
108
    _bfd_error_handler
2710
      /* xgettext:c-format */
2711
108
      (_("%pB: warning: multiple dynamic symbol tables detected"
2712
108
         " - ignoring the table in section %u"),
2713
108
       abfd, shindex);
2714
108
    goto success;
2715
108
  }
2716
1.75k
      elf_dynsymtab (abfd) = shindex;
2717
1.75k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2718
1.75k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2719
1.75k
      abfd->flags |= HAS_SYMS;
2720
2721
      /* Besides being a symbol table, we also treat this as a regular
2722
   section, so that objcopy can handle it.  */
2723
1.75k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2724
1.75k
      goto success;
2725
2726
11.3k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2727
11.3k
      {
2728
11.3k
  elf_section_list * entry;
2729
2730
14.1k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2731
3.44k
    if (entry->ndx == shindex)
2732
590
      goto success;
2733
2734
10.7k
  entry = bfd_alloc (abfd, sizeof (*entry));
2735
10.7k
  if (entry == NULL)
2736
0
    goto fail;
2737
10.7k
  entry->ndx = shindex;
2738
10.7k
  entry->hdr = * hdr;
2739
10.7k
  entry->next = elf_symtab_shndx_list (abfd);
2740
10.7k
  elf_symtab_shndx_list (abfd) = entry;
2741
10.7k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2742
10.7k
  goto success;
2743
10.7k
      }
2744
2745
91.3k
    case SHT_STRTAB:   /* A string table.  */
2746
91.3k
      if (hdr->bfd_section != NULL)
2747
514
  goto success;
2748
2749
90.8k
      if (ehdr->e_shstrndx == shindex)
2750
73.9k
  {
2751
73.9k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2752
73.9k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2753
73.9k
    goto success;
2754
73.9k
  }
2755
2756
16.9k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2757
3.88k
  {
2758
8.83k
  symtab_strtab:
2759
8.83k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2760
8.83k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2761
8.83k
    goto success;
2762
3.88k
  }
2763
2764
13.0k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2765
649
  {
2766
1.21k
  dynsymtab_strtab:
2767
1.21k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2768
1.21k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2769
1.21k
    elf_elfsections (abfd)[shindex] = hdr;
2770
    /* We also treat this as a regular section, so that objcopy
2771
       can handle it.  */
2772
1.21k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2773
1.21k
             shindex);
2774
1.21k
    goto success;
2775
649
  }
2776
2777
      /* If the string table isn't one of the above, then treat it as a
2778
   regular section.  We need to scan all the headers to be sure,
2779
   just in case this strtab section appeared before the above.  */
2780
12.3k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2781
12.2k
  {
2782
12.2k
    unsigned int i, num_sec;
2783
2784
12.2k
    num_sec = elf_numsections (abfd);
2785
140k
    for (i = 1; i < num_sec; i++)
2786
134k
      {
2787
134k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2788
134k
        if (hdr2->sh_link == shindex)
2789
11.1k
    {
2790
      /* Prevent endless recursion on broken objects.  */
2791
11.1k
      if (i == shindex)
2792
114
        goto fail;
2793
11.0k
      if (! bfd_section_from_shdr (abfd, i))
2794
361
        goto fail;
2795
10.6k
      if (elf_onesymtab (abfd) == i)
2796
4.95k
        goto symtab_strtab;
2797
5.68k
      if (elf_dynsymtab (abfd) == i)
2798
567
        goto dynsymtab_strtab;
2799
5.68k
    }
2800
134k
      }
2801
12.2k
  }
2802
6.39k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2803
6.39k
      goto success;
2804
2805
27.5k
    case SHT_REL:
2806
135k
    case SHT_RELA:
2807
136k
    case SHT_RELR:
2808
      /* *These* do a lot of work -- but build no sections!  */
2809
136k
      {
2810
136k
  asection *target_sect;
2811
136k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2812
136k
  unsigned int num_sec = elf_numsections (abfd);
2813
136k
  struct bfd_elf_section_data *esdt;
2814
136k
  bfd_size_type size;
2815
2816
136k
  if (hdr->sh_type == SHT_REL)
2817
27.5k
    size = bed->s->sizeof_rel;
2818
108k
  else if (hdr->sh_type == SHT_RELA)
2819
108k
    size = bed->s->sizeof_rela;
2820
464
  else
2821
464
    size = bed->s->arch_size / 8;
2822
136k
  if (hdr->sh_entsize != size)
2823
1.05k
    goto fail;
2824
2825
  /* Check for a bogus link to avoid crashing.  */
2826
135k
  if (hdr->sh_link >= num_sec)
2827
132
    {
2828
132
      _bfd_error_handler
2829
        /* xgettext:c-format */
2830
132
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2831
132
         abfd, hdr->sh_link, name, shindex);
2832
132
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2833
132
      goto success;
2834
132
    }
2835
2836
  /* Get the symbol table.  */
2837
134k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2838
30.3k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2839
106k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2840
449
    goto fail;
2841
2842
  /* If this is an alloc section in an executable or shared
2843
     library, or the reloc section does not use the main symbol
2844
     table we don't treat it as a reloc section.  BFD can't
2845
     adequately represent such a section, so at least for now,
2846
     we don't try.  We just present it as a normal section.  We
2847
     also can't use it as a reloc section if it points to the
2848
     null section, an invalid section, another reloc section, or
2849
     its sh_link points to the null section.  */
2850
134k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2851
7.25k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2852
133k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2853
129k
      || hdr->sh_type == SHT_RELR
2854
129k
      || hdr->sh_link == SHN_UNDEF
2855
126k
      || hdr->sh_link != elf_onesymtab (abfd)
2856
100k
      || hdr->sh_info == SHN_UNDEF
2857
99.5k
      || hdr->sh_info >= num_sec
2858
99.5k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2859
99.1k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2860
36.3k
    {
2861
36.3k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2862
36.3k
      goto success;
2863
36.3k
    }
2864
2865
98.1k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2866
156
    goto fail;
2867
2868
98.0k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2869
98.0k
  if (target_sect == NULL)
2870
184
    goto fail;
2871
2872
97.8k
  esdt = elf_section_data (target_sect);
2873
97.8k
  if (hdr->sh_type == SHT_RELA)
2874
78.8k
    p_hdr = &esdt->rela.hdr;
2875
19.0k
  else
2876
19.0k
    p_hdr = &esdt->rel.hdr;
2877
2878
  /* PR 17512: file: 0b4f81b7.
2879
     Also see PR 24456, for a file which deliberately has two reloc
2880
     sections.  */
2881
97.8k
  if (*p_hdr != NULL)
2882
2.00k
    {
2883
2.00k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2884
262
        {
2885
262
    _bfd_error_handler
2886
      /* xgettext:c-format */
2887
262
      (_("%pB: warning: secondary relocation section '%s' "
2888
262
         "for section %pA found - ignoring"),
2889
262
       abfd, name, target_sect);
2890
262
        }
2891
1.74k
      else
2892
1.74k
        esdt->has_secondary_relocs = true;
2893
2.00k
      goto success;
2894
2.00k
    }
2895
2896
95.8k
  hdr2 = bfd_alloc (abfd, sizeof (*hdr2));
2897
95.8k
  if (hdr2 == NULL)
2898
0
    goto fail;
2899
95.8k
  *hdr2 = *hdr;
2900
95.8k
  *p_hdr = hdr2;
2901
95.8k
  elf_elfsections (abfd)[shindex] = hdr2;
2902
95.8k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2903
95.8k
             * bed->s->int_rels_per_ext_rel);
2904
95.8k
  target_sect->flags |= SEC_RELOC;
2905
95.8k
  target_sect->relocation = NULL;
2906
95.8k
  target_sect->rel_filepos = hdr->sh_offset;
2907
  /* In the section to which the relocations apply, mark whether
2908
     its relocations are of the REL or RELA variety.  */
2909
95.8k
  if (hdr->sh_size != 0)
2910
94.8k
    {
2911
94.8k
      if (hdr->sh_type == SHT_RELA)
2912
76.3k
        target_sect->use_rela_p = 1;
2913
94.8k
    }
2914
95.8k
  abfd->flags |= HAS_RELOC;
2915
95.8k
  goto success;
2916
95.8k
      }
2917
2918
1.15k
    case SHT_GNU_verdef:
2919
1.15k
      if (hdr->sh_info != 0)
2920
904
  elf_dynverdef (abfd) = shindex;
2921
1.15k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2922
1.15k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2923
1.15k
      goto success;
2924
2925
1.46k
    case SHT_GNU_versym:
2926
1.46k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2927
147
  goto fail;
2928
2929
1.31k
      elf_dynversym (abfd) = shindex;
2930
1.31k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2931
1.31k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2932
1.31k
      goto success;
2933
2934
1.46k
    case SHT_GNU_verneed:
2935
1.46k
      if (hdr->sh_info != 0)
2936
1.35k
  elf_dynverref (abfd) = shindex;
2937
1.46k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2938
1.46k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2939
1.46k
      goto success;
2940
2941
1.25k
    case SHT_SHLIB:
2942
1.25k
      goto success;
2943
2944
23.7k
    case SHT_GROUP:
2945
23.7k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2946
32
  goto fail;
2947
2948
23.6k
      goto success;
2949
2950
237k
    default:
2951
      /* Possibly an attributes section.  */
2952
237k
      if (get_elf_backend_data (abfd)->target_os != is_solaris
2953
    /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
2954
231k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2955
224k
        || hdr->sh_type == bed->obj_attrs_section_type))
2956
7.41k
  {
2957
7.41k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2958
40
      goto fail;
2959
7.37k
    _bfd_elf_parse_attributes (abfd, hdr);
2960
7.37k
    goto success;
2961
7.41k
  }
2962
2963
      /* Check for any processor-specific section types.  */
2964
229k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2965
211k
  goto success;
2966
2967
18.3k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2968
7.93k
  {
2969
7.93k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2970
      /* FIXME: How to properly handle allocated section reserved
2971
         for applications?  */
2972
511
      _bfd_error_handler
2973
        /* xgettext:c-format */
2974
511
        (_("%pB: unknown type [%#x] section `%s'"),
2975
511
         abfd, hdr->sh_type, name);
2976
7.41k
    else
2977
7.41k
      {
2978
        /* Allow sections reserved for applications.  */
2979
7.41k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2980
7.41k
        goto success;
2981
7.41k
      }
2982
7.93k
  }
2983
10.4k
      else if (hdr->sh_type >= SHT_LOPROC
2984
1.33k
         && hdr->sh_type <= SHT_HIPROC)
2985
  /* FIXME: We should handle this section.  */
2986
1.33k
  _bfd_error_handler
2987
    /* xgettext:c-format */
2988
1.33k
    (_("%pB: unknown type [%#x] section `%s'"),
2989
1.33k
     abfd, hdr->sh_type, name);
2990
9.06k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2991
5.08k
  {
2992
    /* Unrecognised OS-specific sections.  */
2993
5.08k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2994
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2995
         required to correctly process the section and the file should
2996
         be rejected with an error message.  */
2997
336
      _bfd_error_handler
2998
        /* xgettext:c-format */
2999
336
        (_("%pB: unknown type [%#x] section `%s'"),
3000
336
         abfd, hdr->sh_type, name);
3001
4.75k
    else
3002
4.75k
      {
3003
        /* Otherwise it should be processed.  */
3004
4.75k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3005
4.75k
        goto success;
3006
4.75k
      }
3007
5.08k
  }
3008
3.98k
      else
3009
  /* FIXME: We should handle this section.  */
3010
3.98k
  _bfd_error_handler
3011
    /* xgettext:c-format */
3012
3.98k
    (_("%pB: unknown type [%#x] section `%s'"),
3013
3.98k
     abfd, hdr->sh_type, name);
3014
3015
6.16k
      goto fail;
3016
1.15M
    }
3017
3018
19.6k
 fail:
3019
19.6k
  ret = false;
3020
1.15M
 success:
3021
1.15M
  elf_tdata (abfd)->being_created[shindex] = false;
3022
1.15M
  return ret;
3023
19.6k
}
3024
3025
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3026
3027
Elf_Internal_Sym *
3028
bfd_sym_from_r_symndx (struct sym_cache *cache,
3029
           bfd *abfd,
3030
           unsigned long r_symndx)
3031
0
{
3032
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3033
3034
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3035
0
    {
3036
0
      Elf_Internal_Shdr *symtab_hdr;
3037
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3038
0
      Elf_External_Sym_Shndx eshndx;
3039
3040
0
      symtab_hdr = &elf_symtab_hdr (abfd);
3041
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3042
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3043
0
  return NULL;
3044
3045
0
      if (cache->abfd != abfd)
3046
0
  {
3047
0
    memset (cache->indx, -1, sizeof (cache->indx));
3048
0
    cache->abfd = abfd;
3049
0
  }
3050
0
      cache->indx[ent] = r_symndx;
3051
0
    }
3052
3053
0
  return &cache->sym[ent];
3054
0
}
3055
3056
/* Given an ELF section number, retrieve the corresponding BFD
3057
   section.  */
3058
3059
asection *
3060
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3061
208k
{
3062
208k
  if (sec_index >= elf_numsections (abfd))
3063
38.2k
    return NULL;
3064
170k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3065
208k
}
3066
3067
static const struct bfd_elf_special_section special_sections_b[] =
3068
{
3069
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3070
  { NULL,       0,  0, 0,    0 }
3071
};
3072
3073
static const struct bfd_elf_special_section special_sections_c[] =
3074
{
3075
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3076
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3077
  { NULL,     0, 0, 0,      0 }
3078
};
3079
3080
static const struct bfd_elf_special_section special_sections_d[] =
3081
{
3082
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3083
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3084
  /* There are more DWARF sections than these, but they needn't be added here
3085
     unless you have to cope with broken compilers that don't emit section
3086
     attributes or you want to help the user writing assembler.  */
3087
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3088
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3089
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3090
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3091
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3092
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3093
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3094
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3095
  { NULL,          0,  0, 0,      0 }
3096
};
3097
3098
static const struct bfd_elf_special_section special_sections_f[] =
3099
{
3100
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3101
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3102
  { NULL,        0 , 0, 0,      0 }
3103
};
3104
3105
static const struct bfd_elf_special_section special_sections_g[] =
3106
{
3107
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3108
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3109
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3110
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3111
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3112
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3113
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3114
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3115
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3116
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3117
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3118
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3119
  { NULL,      0,    0, 0,         0 }
3120
};
3121
3122
static const struct bfd_elf_special_section special_sections_h[] =
3123
{
3124
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3125
  { NULL,        0, 0, 0,    0 }
3126
};
3127
3128
static const struct bfd_elf_special_section special_sections_i[] =
3129
{
3130
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3131
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3132
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3133
  { NULL,          0,      0, 0,      0 }
3134
};
3135
3136
static const struct bfd_elf_special_section special_sections_l[] =
3137
{
3138
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3139
  { NULL,        0, 0, 0,    0 }
3140
};
3141
3142
static const struct bfd_elf_special_section special_sections_n[] =
3143
{
3144
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3145
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_NOTE,     0 },
3146
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3147
  { NULL,        0,     0, 0,      0 }
3148
};
3149
3150
static const struct bfd_elf_special_section special_sections_p[] =
3151
{
3152
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3153
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3154
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3155
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3156
  { NULL,       0,      0, 0,     0 }
3157
};
3158
3159
static const struct bfd_elf_special_section special_sections_r[] =
3160
{
3161
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3162
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3163
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3164
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3165
  /* .relro_padding is generated by lld.  It should not be confused with a
3166
     reloc containing section, because otherwise elf_fake_sections() will
3167
     set the entsize to 8, which may not be an actual multiple of the
3168
     section's size.
3169
     Note - this entry must appear before the ".rel" entry below.  */
3170
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3171
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3172
  { NULL,       0,     0, 0,      0 }
3173
};
3174
3175
static const struct bfd_elf_special_section special_sections_s[] =
3176
{
3177
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3178
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3179
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3180
  /* See struct bfd_elf_special_section declaration for the semantics of
3181
     this special case where .prefix_length != strlen (.prefix).  */
3182
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3183
  { NULL,     0,  0, 0,    0 }
3184
};
3185
3186
static const struct bfd_elf_special_section special_sections_t[] =
3187
{
3188
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3189
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3190
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3191
  { NULL,         0,  0, 0,      0 }
3192
};
3193
3194
static const struct bfd_elf_special_section special_sections_z[] =
3195
{
3196
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3197
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3198
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3199
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3200
  { NULL,         0,  0, 0,      0 }
3201
};
3202
3203
static const struct bfd_elf_special_section * const special_sections[] =
3204
{
3205
  special_sections_b,   /* 'b' */
3206
  special_sections_c,   /* 'c' */
3207
  special_sections_d,   /* 'd' */
3208
  NULL,       /* 'e' */
3209
  special_sections_f,   /* 'f' */
3210
  special_sections_g,   /* 'g' */
3211
  special_sections_h,   /* 'h' */
3212
  special_sections_i,   /* 'i' */
3213
  NULL,       /* 'j' */
3214
  NULL,       /* 'k' */
3215
  special_sections_l,   /* 'l' */
3216
  NULL,       /* 'm' */
3217
  special_sections_n,   /* 'n' */
3218
  NULL,       /* 'o' */
3219
  special_sections_p,   /* 'p' */
3220
  NULL,       /* 'q' */
3221
  special_sections_r,   /* 'r' */
3222
  special_sections_s,   /* 's' */
3223
  special_sections_t,   /* 't' */
3224
  NULL,       /* 'u' */
3225
  NULL,       /* 'v' */
3226
  NULL,       /* 'w' */
3227
  NULL,       /* 'x' */
3228
  NULL,       /* 'y' */
3229
  special_sections_z    /* 'z' */
3230
};
3231
3232
const struct bfd_elf_special_section *
3233
_bfd_elf_get_special_section (const char *name,
3234
            const struct bfd_elf_special_section *spec,
3235
            unsigned int rela)
3236
1.06M
{
3237
1.06M
  int i;
3238
1.06M
  int len;
3239
3240
1.06M
  len = strlen (name);
3241
3242
6.10M
  for (i = 0; spec[i].prefix != NULL; i++)
3243
5.21M
    {
3244
5.21M
      int suffix_len;
3245
5.21M
      int prefix_len = spec[i].prefix_length;
3246
3247
5.21M
      if (len < prefix_len)
3248
2.83M
  continue;
3249
2.37M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3250
2.11M
  continue;
3251
3252
257k
      suffix_len = spec[i].suffix_length;
3253
257k
      if (suffix_len <= 0)
3254
254k
  {
3255
254k
    if (name[prefix_len] != 0)
3256
148k
      {
3257
148k
        if (suffix_len == 0)
3258
76.1k
    continue;
3259
72.0k
        if (name[prefix_len] != '.'
3260
12.9k
      && (suffix_len == -2
3261
6.18k
          || (rela && spec[i].type == SHT_REL)))
3262
8.97k
    continue;
3263
72.0k
      }
3264
254k
  }
3265
2.50k
      else
3266
2.50k
  {
3267
2.50k
    if (len < prefix_len + suffix_len)
3268
1.35k
      continue;
3269
1.15k
    if (memcmp (name + len - suffix_len,
3270
1.15k
          spec[i].prefix + prefix_len,
3271
1.15k
          suffix_len) != 0)
3272
855
      continue;
3273
1.15k
  }
3274
169k
      return &spec[i];
3275
257k
    }
3276
3277
891k
  return NULL;
3278
1.06M
}
3279
3280
const struct bfd_elf_special_section *
3281
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3282
1.71M
{
3283
1.71M
  int i;
3284
1.71M
  const struct bfd_elf_special_section *spec;
3285
1.71M
  elf_backend_data *bed;
3286
3287
  /* See if this is one of the special sections.  */
3288
1.71M
  if (sec->name == NULL)
3289
0
    return NULL;
3290
3291
1.71M
  bed = get_elf_backend_data (abfd);
3292
1.71M
  spec = bed->special_sections;
3293
1.71M
  if (spec)
3294
783k
    {
3295
783k
      spec = _bfd_elf_get_special_section (sec->name,
3296
783k
             bed->special_sections,
3297
783k
             sec->use_rela_p);
3298
783k
      if (spec != NULL)
3299
2.63k
  return spec;
3300
783k
    }
3301
3302
1.71M
  if (sec->name[0] != '.')
3303
1.43M
    return NULL;
3304
3305
276k
  i = sec->name[1] - 'b';
3306
276k
  if (i < 0 || i > 'z' - 'b')
3307
13.1k
    return NULL;
3308
3309
263k
  spec = special_sections[i];
3310
3311
263k
  if (spec == NULL)
3312
15.0k
    return NULL;
3313
3314
248k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3315
263k
}
3316
3317
bool
3318
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3319
1.68M
{
3320
1.68M
  struct bfd_elf_section_data *sdata;
3321
1.68M
  elf_backend_data *bed;
3322
1.68M
  const struct bfd_elf_special_section *ssect;
3323
3324
1.68M
  sdata = sec->used_by_bfd;
3325
1.68M
  if (sdata == NULL)
3326
1.17M
    {
3327
1.17M
      sdata = bfd_zalloc (abfd, sizeof (*sdata));
3328
1.17M
      if (sdata == NULL)
3329
0
  return false;
3330
1.17M
      sec->used_by_bfd = sdata;
3331
1.17M
    }
3332
3333
  /* Indicate whether or not this section should use RELA relocations.  */
3334
1.68M
  bed = get_elf_backend_data (abfd);
3335
1.68M
  sec->use_rela_p = bed->default_use_rela_p;
3336
3337
  /* Set up ELF section type and flags for newly created sections, if
3338
     there is an ABI mandated section.  */
3339
1.68M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3340
1.68M
  if (ssect != NULL)
3341
169k
    {
3342
169k
      elf_section_type (sec) = ssect->type;
3343
169k
      elf_section_flags (sec) = ssect->attr;
3344
169k
    }
3345
3346
1.68M
  return _bfd_generic_new_section_hook (abfd, sec);
3347
1.68M
}
3348
3349
/* Create a new bfd section from an ELF program header.
3350
3351
   Since program segments have no names, we generate a synthetic name
3352
   of the form segment<NUM>, where NUM is generally the index in the
3353
   program header table.  For segments that are split (see below) we
3354
   generate the names segment<NUM>a and segment<NUM>b.
3355
3356
   Note that some program segments may have a file size that is different than
3357
   (less than) the memory size.  All this means is that at execution the
3358
   system must allocate the amount of memory specified by the memory size,
3359
   but only initialize it with the first "file size" bytes read from the
3360
   file.  This would occur for example, with program segments consisting
3361
   of combined data+bss.
3362
3363
   To handle the above situation, this routine generates TWO bfd sections
3364
   for the single program segment.  The first has the length specified by
3365
   the file size of the segment, and the second has the length specified
3366
   by the difference between the two sizes.  In effect, the segment is split
3367
   into its initialized and uninitialized parts.  */
3368
3369
bool
3370
_bfd_elf_make_section_from_phdr (bfd *abfd,
3371
         Elf_Internal_Phdr *hdr,
3372
         int hdr_index,
3373
         const char *type_name)
3374
866k
{
3375
866k
  asection *newsect;
3376
866k
  char *name;
3377
866k
  char namebuf[64];
3378
866k
  size_t len;
3379
866k
  int split;
3380
866k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3381
3382
866k
  split = ((hdr->p_memsz > 0)
3383
647k
      && (hdr->p_filesz > 0)
3384
578k
      && (hdr->p_memsz > hdr->p_filesz));
3385
3386
866k
  if (hdr->p_filesz > 0)
3387
736k
    {
3388
736k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3389
736k
      len = strlen (namebuf) + 1;
3390
736k
      name = bfd_alloc (abfd, len);
3391
736k
      if (!name)
3392
0
  return false;
3393
736k
      memcpy (name, namebuf, len);
3394
736k
      newsect = bfd_make_section (abfd, name);
3395
736k
      if (newsect == NULL)
3396
0
  return false;
3397
736k
      newsect->vma = hdr->p_vaddr / opb;
3398
736k
      newsect->lma = hdr->p_paddr / opb;
3399
736k
      newsect->size = hdr->p_filesz;
3400
736k
      newsect->filepos = hdr->p_offset;
3401
736k
      newsect->flags |= SEC_HAS_CONTENTS;
3402
736k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3403
736k
      if (hdr->p_type == PT_LOAD)
3404
9.23k
  {
3405
9.23k
    newsect->flags |= SEC_ALLOC;
3406
9.23k
    newsect->flags |= SEC_LOAD;
3407
9.23k
    if (hdr->p_flags & PF_X)
3408
3.69k
      {
3409
        /* FIXME: all we known is that it has execute PERMISSION,
3410
     may be data.  */
3411
3.69k
        newsect->flags |= SEC_CODE;
3412
3.69k
      }
3413
9.23k
  }
3414
736k
      if (!(hdr->p_flags & PF_W))
3415
541k
  {
3416
541k
    newsect->flags |= SEC_READONLY;
3417
541k
  }
3418
736k
    }
3419
3420
866k
  if (hdr->p_memsz > hdr->p_filesz)
3421
324k
    {
3422
324k
      bfd_vma align;
3423
3424
324k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3425
324k
      len = strlen (namebuf) + 1;
3426
324k
      name = bfd_alloc (abfd, len);
3427
324k
      if (!name)
3428
0
  return false;
3429
324k
      memcpy (name, namebuf, len);
3430
324k
      newsect = bfd_make_section (abfd, name);
3431
324k
      if (newsect == NULL)
3432
0
  return false;
3433
324k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3434
324k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3435
324k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3436
324k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3437
324k
      align = newsect->vma & -newsect->vma;
3438
324k
      if (align == 0 || align > hdr->p_align)
3439
75.1k
  align = hdr->p_align;
3440
324k
      newsect->alignment_power = bfd_log2 (align);
3441
324k
      if (hdr->p_type == PT_LOAD)
3442
8.47k
  {
3443
8.47k
    newsect->flags |= SEC_ALLOC;
3444
8.47k
    if (hdr->p_flags & PF_X)
3445
2.81k
      newsect->flags |= SEC_CODE;
3446
8.47k
  }
3447
324k
      if (!(hdr->p_flags & PF_W))
3448
243k
  newsect->flags |= SEC_READONLY;
3449
324k
    }
3450
3451
866k
  return true;
3452
866k
}
3453
3454
static bool
3455
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3456
12.2k
{
3457
  /* The return value is ignored.  Build-ids are considered optional.  */
3458
12.2k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3459
12.2k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3460
12.2k
      (templ, offset);
3461
0
  return false;
3462
12.2k
}
3463
3464
bool
3465
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3466
866k
{
3467
866k
  elf_backend_data *bed;
3468
3469
866k
  switch (hdr->p_type)
3470
866k
    {
3471
170k
    case PT_NULL:
3472
170k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3473
3474
12.2k
    case PT_LOAD:
3475
12.2k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3476
0
  return false;
3477
12.2k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3478
12.2k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3479
12.2k
      return true;
3480
3481
3.01k
    case PT_DYNAMIC:
3482
3.01k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3483
3484
2.46k
    case PT_INTERP:
3485
2.46k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3486
3487
1.67k
    case PT_NOTE:
3488
1.67k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3489
0
  return false;
3490
1.67k
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3491
1.67k
         hdr->p_align))
3492
845
  return false;
3493
825
      return true;
3494
3495
1.00k
    case PT_SHLIB:
3496
1.00k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3497
3498
14.1k
    case PT_PHDR:
3499
14.1k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3500
3501
57
    case PT_GNU_EH_FRAME:
3502
57
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3503
57
                "eh_frame_hdr");
3504
3505
53
    case PT_GNU_STACK:
3506
53
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3507
3508
247
    case PT_GNU_RELRO:
3509
247
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3510
3511
13
    case PT_GNU_SFRAME:
3512
13
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3513
13
                "sframe");
3514
3515
661k
    default:
3516
      /* Check for any processor-specific program segment types.  */
3517
661k
      bed = get_elf_backend_data (abfd);
3518
661k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3519
866k
    }
3520
866k
}
3521
3522
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3523
   REL or RELA.  */
3524
3525
Elf_Internal_Shdr *
3526
_bfd_elf_single_rel_hdr (asection *sec)
3527
19
{
3528
19
  if (elf_section_data (sec)->rel.hdr)
3529
0
    {
3530
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3531
0
      return elf_section_data (sec)->rel.hdr;
3532
0
    }
3533
19
  else
3534
19
    return elf_section_data (sec)->rela.hdr;
3535
19
}
3536
3537
static bool
3538
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3539
          Elf_Internal_Shdr *rel_hdr,
3540
          const char *sec_name,
3541
          bool use_rela_p)
3542
147
{
3543
147
  char *name = bfd_alloc (abfd, sizeof ".rela" + strlen (sec_name));
3544
147
  if (name == NULL)
3545
0
    return false;
3546
3547
147
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3548
147
  rel_hdr->sh_name =
3549
147
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3550
147
          false);
3551
147
  if (rel_hdr->sh_name == (unsigned int) -1)
3552
0
    return false;
3553
3554
147
  return true;
3555
147
}
3556
3557
/* Allocate and initialize a section-header for a new reloc section,
3558
   containing relocations against ASECT.  It is stored in RELDATA.  If
3559
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3560
   relocations.  */
3561
3562
static bool
3563
_bfd_elf_init_reloc_shdr (bfd *abfd,
3564
        struct bfd_elf_section_reloc_data *reldata,
3565
        const char *sec_name,
3566
        bool use_rela_p,
3567
        bool delay_sh_name_p)
3568
147
{
3569
147
  Elf_Internal_Shdr *rel_hdr;
3570
147
  elf_backend_data *bed = get_elf_backend_data (abfd);
3571
3572
147
  BFD_ASSERT (reldata->hdr == NULL);
3573
147
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3574
147
  if (rel_hdr == NULL)
3575
0
    return false;
3576
147
  reldata->hdr = rel_hdr;
3577
3578
147
  if (delay_sh_name_p)
3579
0
    rel_hdr->sh_name = (unsigned int) -1;
3580
147
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3581
147
          use_rela_p))
3582
0
    return false;
3583
147
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3584
147
  rel_hdr->sh_entsize = (use_rela_p
3585
147
       ? bed->s->sizeof_rela
3586
147
       : bed->s->sizeof_rel);
3587
147
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3588
147
  rel_hdr->sh_flags = 0;
3589
147
  rel_hdr->sh_addr = 0;
3590
147
  rel_hdr->sh_size = 0;
3591
147
  rel_hdr->sh_offset = 0;
3592
3593
147
  return true;
3594
147
}
3595
3596
/* Return the default section type based on the passed in section flags.  */
3597
3598
int
3599
bfd_elf_get_default_section_type (flagword flags)
3600
2.39k
{
3601
2.39k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3602
912
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3603
43
    return SHT_NOBITS;
3604
2.34k
  return SHT_PROGBITS;
3605
2.39k
}
3606
3607
struct fake_section_arg
3608
{
3609
  struct bfd_link_info *link_info;
3610
  bool failed;
3611
};
3612
3613
/* Set up an ELF internal section header for a section.  */
3614
3615
static void
3616
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3617
1.51k
{
3618
1.51k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3619
1.51k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3620
1.51k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3621
1.51k
  Elf_Internal_Shdr *this_hdr;
3622
1.51k
  unsigned int sh_type;
3623
1.51k
  const char *name = asect->name;
3624
1.51k
  bool delay_sh_name_p = false;
3625
1.51k
  bfd_vma mask;
3626
3627
1.51k
  if (arg->failed)
3628
0
    {
3629
      /* We already failed; just get out of the bfd_map_over_sections
3630
   loop.  */
3631
0
      return;
3632
0
    }
3633
3634
1.51k
  this_hdr = &esd->this_hdr;
3635
3636
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3637
1.51k
  if (arg->link_info
3638
0
      && (abfd->flags & BFD_COMPRESS) != 0
3639
0
      && (asect->flags & SEC_DEBUGGING) != 0
3640
0
      && (asect->flags & SEC_ALLOC) == 0
3641
0
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3642
0
      && name[1] == 'd'
3643
0
      && name[6] == '_')
3644
0
    {
3645
      /* If this section will be compressed, delay adding section
3646
   name to section name section after it is compressed in
3647
   _bfd_elf_assign_file_positions_for_non_load.  */
3648
0
      delay_sh_name_p = true;
3649
0
    }
3650
3651
1.51k
  if (delay_sh_name_p)
3652
0
    this_hdr->sh_name = (unsigned int) -1;
3653
1.51k
  else
3654
1.51k
    {
3655
1.51k
      this_hdr->sh_name
3656
1.51k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3657
1.51k
                name, false);
3658
1.51k
      if (this_hdr->sh_name == (unsigned int) -1)
3659
0
  {
3660
0
    arg->failed = true;
3661
0
    return;
3662
0
  }
3663
1.51k
    }
3664
3665
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3666
3667
1.51k
  if ((asect->flags & SEC_ALLOC) != 0
3668
622
      || asect->user_set_vma)
3669
1.51k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3670
0
  else
3671
0
    this_hdr->sh_addr = 0;
3672
3673
1.51k
  this_hdr->sh_offset = 0;
3674
1.51k
  this_hdr->sh_size = asect->size;
3675
1.51k
  this_hdr->sh_link = 0;
3676
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3677
1.51k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3678
0
    {
3679
0
      _bfd_error_handler
3680
  /* xgettext:c-format */
3681
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3682
0
   abfd, asect->alignment_power, asect);
3683
0
      arg->failed = true;
3684
0
      return;
3685
0
    }
3686
  /* Set sh_addralign to the highest power of two given by alignment
3687
     consistent with the section VMA.  Linker scripts can force VMA.  */
3688
1.51k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3689
1.51k
  this_hdr->sh_addralign = mask & -mask;
3690
  /* The sh_entsize and sh_info fields may have been set already by
3691
     copy_private_section_data.  */
3692
3693
1.51k
  this_hdr->bfd_section = asect;
3694
1.51k
  this_hdr->contents = NULL;
3695
3696
  /* If the section type is unspecified, we set it based on
3697
     asect->flags.  */
3698
1.51k
  if (asect->type != 0)
3699
0
    sh_type = asect->type;
3700
1.51k
  else if ((asect->flags & SEC_GROUP) != 0)
3701
7
    sh_type = SHT_GROUP;
3702
1.51k
  else
3703
1.51k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3704
3705
1.51k
  if (this_hdr->sh_type == SHT_NULL)
3706
96
    this_hdr->sh_type = sh_type;
3707
1.42k
  else if (this_hdr->sh_type == SHT_NOBITS
3708
36
     && sh_type == SHT_PROGBITS
3709
1
     && (asect->flags & SEC_ALLOC) != 0)
3710
0
    {
3711
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3712
   allow the link to proceed.  This can happen when users link
3713
   non-bss input sections to bss output sections, or emit data
3714
   to a bss output section via a linker script.  */
3715
0
      _bfd_error_handler
3716
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3717
0
      this_hdr->sh_type = sh_type;
3718
0
    }
3719
3720
1.51k
  switch (this_hdr->sh_type)
3721
1.51k
    {
3722
206
    default:
3723
206
      break;
3724
3725
206
    case SHT_STRTAB:
3726
138
    case SHT_NOTE:
3727
182
    case SHT_NOBITS:
3728
996
    case SHT_PROGBITS:
3729
996
      break;
3730
3731
33
    case SHT_INIT_ARRAY:
3732
62
    case SHT_FINI_ARRAY:
3733
64
    case SHT_PREINIT_ARRAY:
3734
64
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3735
64
      break;
3736
3737
13
    case SHT_HASH:
3738
13
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3739
13
      break;
3740
3741
23
    case SHT_DYNSYM:
3742
23
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3743
23
      break;
3744
3745
43
    case SHT_DYNAMIC:
3746
43
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3747
43
      break;
3748
3749
84
    case SHT_RELA:
3750
84
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3751
76
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3752
84
      break;
3753
3754
4
     case SHT_REL:
3755
4
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3756
4
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3757
4
      break;
3758
3759
15
     case SHT_GNU_versym:
3760
15
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3761
15
      break;
3762
3763
26
     case SHT_GNU_verdef:
3764
26
      this_hdr->sh_entsize = 0;
3765
      /* objcopy or strip will copy over sh_info, but may not set
3766
   cverdefs.  The linker will set cverdefs, but sh_info will be
3767
   zero.  */
3768
26
      if (this_hdr->sh_info == 0)
3769
1
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3770
25
      else
3771
25
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3772
26
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3773
26
      break;
3774
3775
27
    case SHT_GNU_verneed:
3776
27
      this_hdr->sh_entsize = 0;
3777
      /* objcopy or strip will copy over sh_info, but may not set
3778
   cverrefs.  The linker will set cverrefs, but sh_info will be
3779
   zero.  */
3780
27
      if (this_hdr->sh_info == 0)
3781
1
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3782
26
      else
3783
26
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3784
27
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3785
27
      break;
3786
3787
7
    case SHT_GROUP:
3788
7
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3789
7
      break;
3790
3791
9
    case SHT_GNU_HASH:
3792
9
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3793
9
      break;
3794
1.51k
    }
3795
3796
1.51k
  if ((asect->flags & SEC_ALLOC) != 0)
3797
895
    this_hdr->sh_flags |= SHF_ALLOC;
3798
1.51k
  if ((asect->flags & SEC_READONLY) == 0)
3799
354
    this_hdr->sh_flags |= SHF_WRITE;
3800
1.51k
  if ((asect->flags & SEC_CODE) != 0)
3801
274
    this_hdr->sh_flags |= SHF_EXECINSTR;
3802
1.51k
  if ((asect->flags & SEC_MERGE) != 0)
3803
116
    {
3804
116
      this_hdr->sh_flags |= SHF_MERGE;
3805
116
      this_hdr->sh_entsize = asect->entsize;
3806
116
    }
3807
1.51k
  if ((asect->flags & SEC_STRINGS) != 0)
3808
115
    {
3809
115
      this_hdr->sh_flags |= SHF_STRINGS;
3810
115
      this_hdr->sh_entsize = asect->entsize;
3811
115
    }
3812
1.51k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3813
18
    this_hdr->sh_flags |= SHF_GROUP;
3814
1.51k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3815
76
    {
3816
76
      this_hdr->sh_flags |= SHF_TLS;
3817
76
      if (asect->size == 0
3818
12
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3819
3
  {
3820
3
    struct bfd_link_order *o = asect->map_tail.link_order;
3821
3822
3
    this_hdr->sh_size = 0;
3823
3
    if (o != NULL)
3824
0
      {
3825
0
        this_hdr->sh_size = o->offset + o->size;
3826
0
        if (this_hdr->sh_size != 0)
3827
0
    this_hdr->sh_type = SHT_NOBITS;
3828
0
      }
3829
3
  }
3830
76
    }
3831
1.51k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3832
95
    this_hdr->sh_flags |= SHF_EXCLUDE;
3833
3834
1.51k
  if (this_hdr->sh_entsize == 0)
3835
1.06k
    this_hdr->sh_entsize = asect->entsize;
3836
3837
  /* If the section has relocs, set up a section header for the
3838
     SHT_REL[A] section.  If two relocation sections are required for
3839
     this section, it is up to the processor-specific back-end to
3840
     create the other.  */
3841
1.51k
  if ((asect->flags & SEC_RELOC) != 0)
3842
147
    {
3843
      /* When doing a relocatable link, create both REL and RELA sections if
3844
   needed.  */
3845
147
      if (arg->link_info
3846
    /* Do the normal setup if we wouldn't create any sections here.  */
3847
0
    && esd->rel.count + esd->rela.count > 0
3848
0
    && (bfd_link_relocatable (arg->link_info)
3849
0
        || arg->link_info->emitrelocations))
3850
0
  {
3851
0
    if (esd->rel.count && esd->rel.hdr == NULL
3852
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3853
0
              false, delay_sh_name_p))
3854
0
      {
3855
0
        arg->failed = true;
3856
0
        return;
3857
0
      }
3858
0
    if (esd->rela.count && esd->rela.hdr == NULL
3859
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3860
0
              true, delay_sh_name_p))
3861
0
      {
3862
0
        arg->failed = true;
3863
0
        return;
3864
0
      }
3865
0
  }
3866
147
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3867
147
            (asect->use_rela_p
3868
147
             ? &esd->rela : &esd->rel),
3869
147
            name,
3870
147
            asect->use_rela_p,
3871
147
            delay_sh_name_p))
3872
0
  {
3873
0
    arg->failed = true;
3874
0
    return;
3875
0
  }
3876
147
    }
3877
3878
  /* Check for processor-specific section types.  */
3879
1.51k
  sh_type = this_hdr->sh_type;
3880
1.51k
  if (bed->elf_backend_fake_sections
3881
1.28k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3882
0
    {
3883
0
      arg->failed = true;
3884
0
      return;
3885
0
    }
3886
3887
1.51k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3888
37
    {
3889
      /* Don't change the header type from NOBITS if we are being
3890
   called for objcopy --only-keep-debug.  */
3891
37
      this_hdr->sh_type = sh_type;
3892
37
    }
3893
1.51k
}
3894
3895
/* Fill in the contents of a SHT_GROUP section.  Called from
3896
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3897
   when ELF targets use the generic linker, ld.  Called for ld -r
3898
   from bfd_elf_final_link.  */
3899
3900
void
3901
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3902
1.51k
{
3903
1.51k
  bool *failedptr = (bool *) failedptrarg;
3904
1.51k
  asection *elt, *first;
3905
1.51k
  unsigned char *loc;
3906
1.51k
  bool gas;
3907
3908
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3909
     elfxx-ia64.c.  */
3910
1.51k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3911
7
      || sec->size == 0
3912
7
      || *failedptr)
3913
1.51k
    return;
3914
3915
6
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3916
6
    {
3917
6
      unsigned long symindx = 0;
3918
3919
      /* elf_group_id will have been set up by objcopy and the
3920
   generic linker.  */
3921
6
      if (elf_group_id (sec) != NULL)
3922
6
  symindx = elf_group_id (sec)->udata.i;
3923
3924
6
      if (symindx == 0)
3925
0
  {
3926
    /* If called from the assembler, swap_out_syms will have set up
3927
       elf_section_syms.
3928
       PR 25699: A corrupt input file could contain bogus group info.  */
3929
0
    if (sec->index >= elf_num_section_syms (abfd)
3930
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3931
0
      {
3932
0
        *failedptr = true;
3933
0
        return;
3934
0
      }
3935
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3936
0
  }
3937
6
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3938
6
    }
3939
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3940
0
    {
3941
      /* The ELF backend linker sets sh_info to -2 when the group
3942
   signature symbol is global, and thus the index can't be
3943
   set until all local symbols are output.  */
3944
0
      asection *igroup;
3945
0
      struct bfd_elf_section_data *sec_data;
3946
0
      unsigned long symndx;
3947
0
      unsigned long extsymoff;
3948
0
      struct elf_link_hash_entry *h;
3949
3950
      /* The point of this little dance to the first SHF_GROUP section
3951
   then back to the SHT_GROUP section is that this gets us to
3952
   the SHT_GROUP in the input object.  */
3953
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3954
0
      sec_data = elf_section_data (igroup);
3955
0
      symndx = sec_data->this_hdr.sh_info;
3956
0
      extsymoff = 0;
3957
0
      if (!elf_bad_symtab (igroup->owner))
3958
0
  {
3959
0
    Elf_Internal_Shdr *symtab_hdr;
3960
3961
0
    symtab_hdr = &elf_symtab_hdr (igroup->owner);
3962
0
    extsymoff = symtab_hdr->sh_info;
3963
0
  }
3964
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3965
0
      while (h->root.type == bfd_link_hash_indirect
3966
0
       || h->root.type == bfd_link_hash_warning)
3967
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3968
3969
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3970
0
    }
3971
3972
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3973
6
  gas = true;
3974
6
  if (sec->contents == NULL)
3975
6
    {
3976
6
      gas = false;
3977
6
      sec->contents = bfd_alloc (abfd, sec->size);
3978
3979
      /* Arrange for the section to be written out.  */
3980
6
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3981
6
      if (sec->contents == NULL)
3982
0
  {
3983
0
    *failedptr = true;
3984
0
    return;
3985
0
  }
3986
6
      sec->alloced = 1;
3987
6
    }
3988
3989
6
  loc = sec->contents + sec->size;
3990
3991
  /* Get the pointer to the first section in the group that gas
3992
     squirreled away here.  objcopy arranges for this to be set to the
3993
     start of the input section group.  */
3994
6
  first = elt = elf_next_in_group (sec);
3995
3996
  /* First element is a flag word.  Rest of section is elf section
3997
     indices for all the sections of the group.  Write them backwards
3998
     just to keep the group in the same order as given in .section
3999
     directives, not that it matters.  */
4000
16
  while (elt != NULL)
4001
16
    {
4002
16
      asection *s;
4003
4004
16
      s = elt;
4005
16
      if (!gas)
4006
16
  s = s->output_section;
4007
16
      if (s != NULL
4008
16
    && !bfd_is_abs_section (s))
4009
16
  {
4010
16
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4011
16
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4012
4013
16
    if (elf_sec->rel.hdr != NULL
4014
0
        && (gas
4015
0
      || (input_elf_sec->rel.hdr != NULL
4016
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4017
0
      {
4018
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4019
0
        loc -= 4;
4020
0
        if (loc == sec->contents)
4021
0
    break;
4022
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4023
0
      }
4024
16
    if (elf_sec->rela.hdr != NULL
4025
11
        && (gas
4026
11
      || (input_elf_sec->rela.hdr != NULL
4027
11
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4028
11
      {
4029
11
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4030
11
        loc -= 4;
4031
11
        if (loc == sec->contents)
4032
0
    break;
4033
11
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4034
11
      }
4035
16
    loc -= 4;
4036
16
    if (loc == sec->contents)
4037
0
      break;
4038
16
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4039
16
  }
4040
16
      elt = elf_next_in_group (elt);
4041
16
      if (elt == first)
4042
6
  break;
4043
16
    }
4044
4045
  /* We should always get here with loc == sec->contents + 4.  Return
4046
     an error for bogus SHT_GROUP sections.  */
4047
6
  loc -= 4;
4048
6
  if (loc != sec->contents)
4049
1
    {
4050
      /* xgettext:c-format */
4051
1
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
4052
1
        abfd, sec);
4053
1
      bfd_set_error (bfd_error_bad_value);
4054
1
      *failedptr = true;
4055
1
      return;
4056
1
    }
4057
4058
5
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4059
5
}
4060
4061
/* Given NAME, the name of a relocation section stripped of its
4062
   .rel/.rela prefix, return the section in ABFD to which the
4063
   relocations apply.  */
4064
4065
asection *
4066
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4067
41
{
4068
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4069
     section likely apply to .got.plt or .got section.  */
4070
41
  if (get_elf_backend_data (abfd)->want_got_plt
4071
15
      && streq (name, ".plt"))
4072
3
    {
4073
3
      asection *sec;
4074
4075
3
      name = ".got.plt";
4076
3
      sec = bfd_get_section_by_name (abfd, name);
4077
3
      if (sec != NULL)
4078
3
  return sec;
4079
0
      name = ".got";
4080
0
    }
4081
4082
38
  return bfd_get_section_by_name (abfd, name);
4083
41
}
4084
4085
/* Return the section to which RELOC_SEC applies.  */
4086
4087
static asection *
4088
elf_get_reloc_section (asection *reloc_sec)
4089
88
{
4090
88
  const char *name;
4091
88
  unsigned int type;
4092
88
  bfd *abfd;
4093
88
  elf_backend_data *bed;
4094
4095
88
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4096
88
  if (type != SHT_REL && type != SHT_RELA)
4097
0
    return NULL;
4098
4099
  /* We look up the section the relocs apply to by name.  */
4100
88
  name = reloc_sec->name;
4101
88
  if (!startswith (name, ".rel"))
4102
42
    return NULL;
4103
46
  name += 4;
4104
46
  if (type == SHT_RELA && *name++ != 'a')
4105
0
    return NULL;
4106
4107
46
  abfd = reloc_sec->owner;
4108
46
  bed = get_elf_backend_data (abfd);
4109
46
  return bed->get_reloc_section (abfd, name);
4110
46
}
4111
4112
/* Assign all ELF section numbers.  The dummy first section is handled here
4113
   too.  The link/info pointers for the standard section types are filled
4114
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4115
   here for gas, objcopy, and when using the generic ELF linker.  */
4116
4117
static bool
4118
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4119
398
{
4120
398
  struct elf_obj_tdata *t = elf_tdata (abfd);
4121
398
  asection *sec;
4122
398
  unsigned int section_number;
4123
398
  Elf_Internal_Shdr **i_shdrp;
4124
398
  struct bfd_elf_section_data *d;
4125
398
  bool need_symtab;
4126
4127
398
  section_number = 1;
4128
4129
398
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4130
4131
  /* SHT_GROUP sections are in relocatable files only.  */
4132
398
  if (link_info == NULL || !link_info->resolve_section_groups)
4133
398
    {
4134
398
      size_t reloc_count = 0;
4135
4136
      /* Put SHT_GROUP sections first.  */
4137
1.91k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4138
1.51k
  {
4139
1.51k
    d = elf_section_data (sec);
4140
4141
1.51k
    if (d->this_hdr.sh_type == SHT_GROUP)
4142
7
      {
4143
7
        if (sec->flags & SEC_LINKER_CREATED)
4144
0
    {
4145
      /* Remove the linker created SHT_GROUP sections.  */
4146
0
      bfd_section_list_remove (abfd, sec);
4147
0
      abfd->section_count--;
4148
0
    }
4149
7
        else
4150
7
    d->this_idx = section_number++;
4151
7
      }
4152
4153
    /* Count relocations.  */
4154
1.51k
    reloc_count += sec->reloc_count;
4155
1.51k
  }
4156
4157
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4158
398
      if (reloc_count == 0)
4159
354
  abfd->flags &= ~HAS_RELOC;
4160
44
      else
4161
44
  abfd->flags |= HAS_RELOC;
4162
398
    }
4163
4164
1.91k
  for (sec = abfd->sections; sec; sec = sec->next)
4165
1.51k
    {
4166
1.51k
      d = elf_section_data (sec);
4167
4168
1.51k
      if (d->this_hdr.sh_type != SHT_GROUP)
4169
1.51k
  d->this_idx = section_number++;
4170
1.51k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4171
1.51k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4172
1.51k
      if (d->rel.hdr)
4173
1
  {
4174
1
    d->rel.idx = section_number++;
4175
1
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4176
1
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4177
1
  }
4178
1.51k
      else
4179
1.51k
  d->rel.idx = 0;
4180
4181
1.51k
      if (d->rela.hdr)
4182
146
  {
4183
146
    d->rela.idx = section_number++;
4184
146
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4185
146
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4186
146
  }
4187
1.37k
      else
4188
1.37k
  d->rela.idx = 0;
4189
1.51k
    }
4190
4191
398
  need_symtab = (bfd_get_symcount (abfd) > 0
4192
307
     || (link_info == NULL
4193
307
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4194
307
       == HAS_RELOC)));
4195
398
  if (need_symtab)
4196
91
    {
4197
91
      elf_onesymtab (abfd) = section_number++;
4198
91
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4199
91
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4200
0
  {
4201
0
    elf_section_list *entry;
4202
4203
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4204
4205
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4206
0
    if (entry == NULL)
4207
0
      return false;
4208
0
    entry->ndx = section_number++;
4209
0
    elf_symtab_shndx_list (abfd) = entry;
4210
0
    entry->hdr.sh_name = _bfd_elf_strtab_add (elf_shstrtab (abfd),
4211
0
                ".symtab_shndx", false);
4212
0
    if (entry->hdr.sh_name == -1u)
4213
0
      return false;
4214
0
  }
4215
91
      elf_strtab_sec (abfd) = section_number++;
4216
91
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4217
91
    }
4218
4219
398
  elf_shstrtab_sec (abfd) = section_number++;
4220
398
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4221
398
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4222
4223
398
  if (section_number >= SHN_LORESERVE)
4224
0
    {
4225
      /* xgettext:c-format */
4226
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4227
0
        abfd, section_number);
4228
0
      return false;
4229
0
    }
4230
4231
398
  elf_numsections (abfd) = section_number;
4232
398
  elf_elfheader (abfd)->e_shnum = section_number;
4233
4234
  /* Set up the list of section header pointers, in agreement with the
4235
     indices.  */
4236
398
  i_shdrp = bfd_zalloc (abfd, section_number * sizeof (*i_shdrp));
4237
398
  if (i_shdrp == NULL)
4238
0
    return false;
4239
4240
398
  i_shdrp[0] = bfd_zalloc (abfd, sizeof (*i_shdrp[0]));
4241
398
  if (i_shdrp[0] == NULL)
4242
0
    {
4243
0
      bfd_release (abfd, i_shdrp);
4244
0
      return false;
4245
0
    }
4246
4247
398
  elf_elfsections (abfd) = i_shdrp;
4248
4249
398
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4250
398
  if (need_symtab)
4251
91
    {
4252
91
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4253
91
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4254
0
  {
4255
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4256
0
    BFD_ASSERT (entry != NULL);
4257
0
    i_shdrp[entry->ndx] = & entry->hdr;
4258
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4259
0
  }
4260
91
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4261
91
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4262
91
    }
4263
4264
1.91k
  for (sec = abfd->sections; sec; sec = sec->next)
4265
1.51k
    {
4266
1.51k
      asection *s;
4267
4268
1.51k
      d = elf_section_data (sec);
4269
4270
1.51k
      i_shdrp[d->this_idx] = &d->this_hdr;
4271
1.51k
      if (d->rel.idx != 0)
4272
1
  i_shdrp[d->rel.idx] = d->rel.hdr;
4273
1.51k
      if (d->rela.idx != 0)
4274
146
  i_shdrp[d->rela.idx] = d->rela.hdr;
4275
4276
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4277
4278
      /* sh_link of a reloc section is the section index of the symbol
4279
   table.  sh_info is the section index of the section to which
4280
   the relocation entries apply.  */
4281
1.51k
      if (d->rel.idx != 0)
4282
1
  {
4283
1
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4284
1
    d->rel.hdr->sh_info = d->this_idx;
4285
1
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4286
1
  }
4287
1.51k
      if (d->rela.idx != 0)
4288
146
  {
4289
146
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4290
146
    d->rela.hdr->sh_info = d->this_idx;
4291
146
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4292
146
  }
4293
4294
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4295
1.51k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4296
41
  {
4297
41
    s = elf_linked_to_section (sec);
4298
    /* We can now have a NULL linked section pointer.
4299
       This happens when the sh_link field is 0, which is done
4300
       when a linked to section is discarded but the linking
4301
       section has been retained for some reason.  */
4302
41
    if (s)
4303
8
      {
4304
        /* Check discarded linkonce section.  */
4305
8
        if (discarded_section (s))
4306
0
    {
4307
0
      asection *kept;
4308
0
      _bfd_error_handler
4309
        /* xgettext:c-format */
4310
0
        (_("%pB: sh_link of section `%pA' points to"
4311
0
           " discarded section `%pA' of `%pB'"),
4312
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4313
      /* Point to the kept section if it has the same
4314
         size as the discarded one.  */
4315
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4316
0
      if (kept == NULL)
4317
0
        {
4318
0
          bfd_set_error (bfd_error_bad_value);
4319
0
          return false;
4320
0
        }
4321
0
      s = kept;
4322
0
    }
4323
        /* Handle objcopy. */
4324
8
        else if (s->output_section == NULL)
4325
0
    {
4326
0
      _bfd_error_handler
4327
        /* xgettext:c-format */
4328
0
        (_("%pB: sh_link of section `%pA' points to"
4329
0
           " removed section `%pA' of `%pB'"),
4330
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4331
0
      bfd_set_error (bfd_error_bad_value);
4332
0
      return false;
4333
0
    }
4334
8
        s = s->output_section;
4335
8
        d->this_hdr.sh_link
4336
8
    = _bfd_elf_section_from_bfd_section (abfd, s);
4337
8
      }
4338
41
  }
4339
4340
1.51k
      switch (d->this_hdr.sh_type)
4341
1.51k
  {
4342
4
  case SHT_REL:
4343
88
  case SHT_RELA:
4344
    /* sh_link is the section index of the symbol table.
4345
       sh_info is the section index of the section to which the
4346
       relocation entries apply.  */
4347
88
    if (d->this_hdr.sh_link == 0)
4348
88
      {
4349
        /* FIXME maybe: If this is a reloc section which we are
4350
     treating as a normal section then we likely should
4351
     not be assuming its sh_link is .dynsym or .symtab.  */
4352
88
        if ((sec->flags & SEC_ALLOC) != 0)
4353
22
    {
4354
22
      s = bfd_get_section_by_name (abfd, ".dynsym");
4355
22
      if (s != NULL)
4356
7
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4357
22
    }
4358
66
        else
4359
66
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4360
88
      }
4361
4362
88
    s = elf_get_reloc_section (sec);
4363
88
    if (s != NULL)
4364
32
      {
4365
32
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4366
32
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4367
32
      }
4368
88
    break;
4369
4370
59
  case SHT_STRTAB:
4371
    /* We assume that a section named .stab*str is a stabs
4372
       string section.  We look for a section with the same name
4373
       but without the trailing ``str'', and set its sh_link
4374
       field to point to this section.  */
4375
59
    if (startswith (sec->name, ".stab")
4376
0
        && streq (sec->name + strlen (sec->name) - 3, "str"))
4377
0
      {
4378
0
        size_t len;
4379
0
        char *alc;
4380
4381
0
        len = strlen (sec->name);
4382
0
        alc = bfd_malloc (len - 2);
4383
0
        if (alc == NULL)
4384
0
    return false;
4385
0
        memcpy (alc, sec->name, len - 3);
4386
0
        alc[len - 3] = '\0';
4387
0
        s = bfd_get_section_by_name (abfd, alc);
4388
0
        free (alc);
4389
0
        if (s != NULL)
4390
0
    {
4391
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4392
4393
      /* This is a .stab section.  */
4394
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4395
0
    }
4396
0
      }
4397
59
    break;
4398
4399
59
  case SHT_DYNAMIC:
4400
66
  case SHT_DYNSYM:
4401
93
  case SHT_GNU_verneed:
4402
119
  case SHT_GNU_verdef:
4403
    /* sh_link is the section header index of the string table
4404
       used for the dynamic entries, or the symbol table, or the
4405
       version strings.  */
4406
119
    s = bfd_get_section_by_name (abfd, ".dynstr");
4407
119
    if (s != NULL)
4408
54
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4409
119
    break;
4410
4411
0
  case SHT_GNU_LIBLIST:
4412
    /* sh_link is the section header index of the prelink library
4413
       list used for the dynamic entries, or the symbol table, or
4414
       the version strings.  */
4415
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4416
0
                ? ".dynstr" : ".gnu.libstr"));
4417
0
    if (s != NULL)
4418
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4419
0
    break;
4420
4421
13
  case SHT_HASH:
4422
22
  case SHT_GNU_HASH:
4423
37
  case SHT_GNU_versym:
4424
    /* sh_link is the section header index of the symbol table
4425
       this hash table or version table is for.  */
4426
37
    s = bfd_get_section_by_name (abfd, ".dynsym");
4427
37
    if (s != NULL)
4428
15
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4429
37
    break;
4430
4431
7
  case SHT_GROUP:
4432
7
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4433
1.51k
  }
4434
1.51k
    }
4435
4436
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4437
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4438
     debug section name from .debug_* to .zdebug_* if needed.  */
4439
4440
398
  return true;
4441
398
}
4442
4443
static bool
4444
sym_is_global (bfd *abfd, asymbol *sym)
4445
3.02k
{
4446
  /* If the backend has a special mapping, use it.  */
4447
3.02k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4448
3.02k
  if (bed->elf_backend_sym_is_global)
4449
50
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4450
4451
2.97k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4452
2.38k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4453
1.39k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4454
3.02k
}
4455
4456
/* Filter global symbols of INFO->OUTPUT_BFD to include in the import
4457
   library.  SYMCOUNT symbols can be examined from their pointers in
4458
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4459
   the beginning of that array.
4460
4461
   Returns the number of symbols to keep.  */
4462
4463
size_t
4464
_bfd_elf_filter_implib_symbols (struct bfd_link_info *info,
4465
        asymbol **syms, size_t symcount)
4466
0
{
4467
0
  size_t src_count, dst_count = 0;
4468
4469
0
  for (src_count = 0; src_count < symcount; src_count++)
4470
0
    {
4471
0
      asymbol *sym = syms[src_count];
4472
0
      const char *name = bfd_asymbol_name (sym);
4473
0
      struct bfd_link_hash_entry *h;
4474
4475
0
      if (!sym_is_global (info->output_bfd, sym))
4476
0
  continue;
4477
4478
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4479
0
      if (h == NULL)
4480
0
  continue;
4481
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4482
0
  continue;
4483
0
      if (h->linker_def || h->ldscript_def)
4484
0
  continue;
4485
4486
0
      syms[dst_count++] = sym;
4487
0
    }
4488
4489
0
  syms[dst_count] = NULL;
4490
4491
0
  return dst_count;
4492
0
}
4493
4494
/* Don't output symbols for sections that are not going to be output,
4495
   that are duplicates or there is no BFD section.  */
4496
4497
static bool
4498
ignore_sym (asymbol *sym)
4499
4.57k
{
4500
4.57k
  if (sym == NULL)
4501
0
    return false;
4502
4503
4.57k
  if (sym->section == NULL)
4504
0
    return true;
4505
4506
4.57k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4507
1.85k
    {
4508
1.85k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4509
1.43k
  return true;
4510
      /* With ld -r on generic elf targets it is possible to have
4511
   multiple section symbols in the output for a given section.
4512
   We'd like to get rid of all but the first one.  This drops
4513
   them if the first input section is non-zero size, but fails
4514
   to do so if the first input section is zero sized.  */
4515
421
      if (sym->section->output_offset != 0)
4516
0
  return true;
4517
421
    }
4518
4519
3.13k
  return discarded_section (sym->section);
4520
4.57k
}
4521
4522
/* Map symbol from it's internal number to the external number, moving
4523
   all local symbols to be at the head of the list.  */
4524
4525
static bool
4526
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4527
91
{
4528
91
  unsigned int symcount = bfd_get_symcount (abfd);
4529
91
  asymbol **syms = bfd_get_outsymbols (abfd);
4530
91
  asymbol **sect_syms;
4531
91
  unsigned int num_locals = 0;
4532
91
  unsigned int num_globals = 0;
4533
91
  unsigned int max_index = 0;
4534
91
  unsigned int idx;
4535
91
  asection *asect;
4536
91
  asymbol **new_syms;
4537
4538
#ifdef DEBUG
4539
  fprintf (stderr, "elf_map_symbols\n");
4540
  fflush (stderr);
4541
#endif
4542
4543
848
  for (asect = abfd->sections; asect; asect = asect->next)
4544
757
    {
4545
757
      if (max_index < asect->index)
4546
666
  max_index = asect->index;
4547
757
    }
4548
4549
91
  max_index++;
4550
91
  sect_syms = bfd_zalloc (abfd, max_index * sizeof (*sect_syms));
4551
91
  if (sect_syms == NULL)
4552
0
    return false;
4553
91
  elf_section_syms (abfd) = sect_syms;
4554
91
  elf_num_section_syms (abfd) = max_index;
4555
4556
  /* Init sect_syms entries for any section symbols we have already
4557
     decided to output.  */
4558
1.57k
  for (idx = 0; idx < symcount; idx++)
4559
1.47k
    {
4560
1.47k
      asymbol *sym = syms[idx];
4561
4562
1.47k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4563
120
    && sym->value == 0
4564
99
    && !ignore_sym (sym)
4565
97
    && !bfd_is_abs_section (sym->section))
4566
92
  {
4567
92
    asection *sec = sym->section;
4568
4569
92
    if (sec->owner != abfd)
4570
92
      {
4571
92
        sec = sec->output_section;
4572
92
        if (sec == NULL)
4573
0
    return false;
4574
92
      }
4575
4576
92
    sect_syms[sec->index] = syms[idx];
4577
92
  }
4578
1.47k
    }
4579
4580
  /* Classify all of the symbols.  */
4581
1.57k
  for (idx = 0; idx < symcount; idx++)
4582
1.47k
    {
4583
1.47k
      if (ignore_sym (syms[idx]))
4584
7
  continue;
4585
1.47k
      if (sym_is_global (abfd, syms[idx]))
4586
820
  num_globals++;
4587
652
      else
4588
652
  num_locals++;
4589
1.47k
    }
4590
4591
  /* We will be adding a section symbol for each normal BFD section.  Most
4592
     sections will already have a section symbol in outsymbols, but
4593
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4594
     at least in that case.  */
4595
848
  for (asect = abfd->sections; asect; asect = asect->next)
4596
757
    {
4597
757
      asymbol *sym = asect->symbol;
4598
      /* Don't include ignored section symbols.  */
4599
757
      if (!ignore_sym (sym)
4600
49
    && sect_syms[asect->index] == NULL)
4601
39
  {
4602
39
    if (sym_is_global (abfd, asect->symbol))
4603
0
      num_globals++;
4604
39
    else
4605
39
      num_locals++;
4606
39
  }
4607
757
    }
4608
4609
  /* Now sort the symbols so the local symbols are first.  */
4610
91
  new_syms = bfd_alloc (abfd, (num_locals + num_globals) * sizeof (*new_syms));
4611
91
  if (new_syms == NULL)
4612
0
    return false;
4613
4614
91
  unsigned int num_globals2 = 0;
4615
91
  unsigned int num_locals2 = 0;
4616
1.57k
  for (idx = 0; idx < symcount; idx++)
4617
1.47k
    {
4618
1.47k
      asymbol *sym = syms[idx];
4619
1.47k
      unsigned int i;
4620
4621
1.47k
      if (ignore_sym (sym))
4622
7
  continue;
4623
4624
1.47k
      if (sym_is_global (abfd, sym))
4625
820
  i = num_locals + num_globals2++;
4626
652
      else
4627
652
  i = num_locals2++;
4628
1.47k
      new_syms[i] = sym;
4629
1.47k
      sym->udata.i = i + 1;
4630
1.47k
    }
4631
848
  for (asect = abfd->sections; asect; asect = asect->next)
4632
757
    {
4633
757
      asymbol *sym = asect->symbol;
4634
757
      if (!ignore_sym (sym)
4635
49
    && sect_syms[asect->index] == NULL)
4636
39
  {
4637
39
    unsigned int i;
4638
4639
39
    sect_syms[asect->index] = sym;
4640
39
    if (sym_is_global (abfd, sym))
4641
0
      i = num_locals + num_globals2++;
4642
39
    else
4643
39
      i = num_locals2++;
4644
39
    new_syms[i] = sym;
4645
39
    sym->udata.i = i + 1;
4646
39
  }
4647
757
    }
4648
4649
91
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4650
4651
91
  *pnum_locals = num_locals;
4652
91
  return true;
4653
91
}
4654
4655
/* Assign a file position to a section, optionally aligning to the
4656
   required section alignment.  */
4657
4658
file_ptr
4659
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4660
             file_ptr offset,
4661
             bool align,
4662
             unsigned char log_file_align)
4663
2.18k
{
4664
2.18k
  if (i_shdrp->sh_addralign > 1)
4665
871
    {
4666
871
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4667
4668
871
      if (align)
4669
135
  offset = BFD_ALIGN (offset, salign);
4670
736
      else if (log_file_align)
4671
736
  {
4672
    /* Heuristic: Cap alignment at log_file_align.  */
4673
736
    file_ptr falign = 1u << log_file_align;
4674
4675
736
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4676
736
  }
4677
871
    }
4678
2.18k
  i_shdrp->sh_offset = offset;
4679
2.18k
  if (i_shdrp->bfd_section != NULL)
4680
1.11k
    i_shdrp->bfd_section->filepos = offset;
4681
2.18k
  if (i_shdrp->sh_type != SHT_NOBITS)
4682
2.17k
    offset += i_shdrp->sh_size;
4683
2.18k
  return offset;
4684
2.18k
}
4685
4686
/* Compute the file positions we are going to put the sections at, and
4687
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4688
   is not NULL, this is being called by the ELF backend linker.  */
4689
4690
bool
4691
_bfd_elf_compute_section_file_positions (bfd *abfd,
4692
           struct bfd_link_info *link_info)
4693
398
{
4694
398
  elf_backend_data *bed = get_elf_backend_data (abfd);
4695
398
  struct fake_section_arg fsargs;
4696
398
  bool failed;
4697
398
  struct elf_strtab_hash *strtab = NULL;
4698
398
  Elf_Internal_Shdr *shstrtab_hdr;
4699
398
  bool need_symtab;
4700
4701
398
  if (abfd->output_has_begun)
4702
0
    return true;
4703
4704
  /* Do any elf backend specific processing first.  */
4705
398
  if (bed->elf_backend_begin_write_processing)
4706
72
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4707
4708
398
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4709
0
    return false;
4710
4711
398
  fsargs.failed = false;
4712
398
  fsargs.link_info = link_info;
4713
398
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4714
398
  if (fsargs.failed)
4715
0
    return false;
4716
4717
398
  if (!assign_section_numbers (abfd, link_info))
4718
0
    return false;
4719
4720
  /* The backend linker builds symbol table information itself.  */
4721
398
  need_symtab = (link_info == NULL
4722
398
     && (bfd_get_symcount (abfd) > 0
4723
307
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4724
307
       == HAS_RELOC)));
4725
398
  if (need_symtab)
4726
91
    {
4727
      /* Non-zero if doing a relocatable link.  */
4728
91
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4729
4730
91
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4731
0
  return false;
4732
91
    }
4733
4734
398
  failed = false;
4735
398
  if (link_info == NULL)
4736
398
    {
4737
398
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4738
398
      if (failed)
4739
1
  goto err_free_strtab;
4740
398
    }
4741
4742
397
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4743
  /* sh_name was set in init_file_header.  */
4744
397
  shstrtab_hdr->sh_type = SHT_STRTAB;
4745
  /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
4746
     when tdata is allocated.  */
4747
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4748
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4749
397
  shstrtab_hdr->sh_addralign = 1;
4750
4751
397
  if (!assign_file_positions_except_relocs (abfd, link_info))
4752
12
    goto err_free_strtab;
4753
4754
385
  if (strtab != NULL)
4755
85
    {
4756
85
      file_ptr off;
4757
85
      Elf_Internal_Shdr *hdr;
4758
4759
85
      off = elf_next_file_pos (abfd);
4760
4761
85
      hdr = & elf_symtab_hdr (abfd);
4762
85
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4763
4764
85
      if (elf_symtab_shndx_list (abfd) != NULL)
4765
0
  {
4766
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4767
0
    if (hdr->sh_size != 0)
4768
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4769
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4770
0
  }
4771
4772
85
      hdr = &elf_tdata (abfd)->strtab_hdr;
4773
85
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4774
4775
85
      elf_next_file_pos (abfd) = off;
4776
4777
      /* Now that we know where the .strtab section goes, write it
4778
   out.  */
4779
85
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4780
85
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4781
0
  goto err_free_strtab;
4782
85
      _bfd_elf_strtab_free (strtab);
4783
85
    }
4784
4785
385
  abfd->output_has_begun = true;
4786
385
  return true;
4787
4788
13
 err_free_strtab:
4789
13
  if (strtab != NULL)
4790
6
    _bfd_elf_strtab_free (strtab);
4791
13
  return false;
4792
385
}
4793
4794
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4795
   function effectively returns whether --eh-frame-hdr is given on the
4796
   command line.  After size_dynamic_sections the result reflects
4797
   whether .eh_frame_hdr will actually be output (sizing isn't done
4798
   until ldemul_after_allocation).  */
4799
4800
static asection *
4801
elf_eh_frame_hdr (const struct bfd_link_info *info)
4802
61
{
4803
61
  if (info != NULL && is_elf_hash_table (info->hash))
4804
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4805
61
  return NULL;
4806
61
}
4807
4808
/* Make an initial estimate of the size of the program header.  If we
4809
   get the number wrong here, we'll redo section placement.  */
4810
4811
static bfd_size_type
4812
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4813
32
{
4814
32
  size_t segs;
4815
32
  asection *s;
4816
32
  elf_backend_data *bed;
4817
4818
  /* Assume we will need exactly two PT_LOAD segments: one for text
4819
     and one for data.  */
4820
32
  segs = 2;
4821
4822
32
  s = bfd_get_section_by_name (abfd, ".interp");
4823
32
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4824
0
    {
4825
      /* If we have a loadable interpreter section, we need a
4826
   PT_INTERP segment.  In this case, assume we also need a
4827
   PT_PHDR segment, although that may not be true for all
4828
   targets.  */
4829
0
      segs += 2;
4830
0
    }
4831
4832
32
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4833
8
    {
4834
      /* We need a PT_DYNAMIC segment.  */
4835
8
      ++segs;
4836
8
    }
4837
4838
32
  if (info != NULL && info->relro)
4839
0
    {
4840
      /* We need a PT_GNU_RELRO segment.  */
4841
0
      ++segs;
4842
0
    }
4843
4844
32
  if (elf_eh_frame_hdr (info))
4845
0
    {
4846
      /* We need a PT_GNU_EH_FRAME segment.  */
4847
0
      ++segs;
4848
0
    }
4849
4850
32
  if (elf_stack_flags (abfd))
4851
0
    {
4852
      /* We need a PT_GNU_STACK segment.  */
4853
0
      ++segs;
4854
0
    }
4855
4856
32
  if (elf_sframe (abfd))
4857
0
    {
4858
      /* We need a PT_GNU_SFRAME segment.  */
4859
0
      ++segs;
4860
0
    }
4861
4862
32
  s = bfd_get_section_by_name (abfd,
4863
32
             NOTE_GNU_PROPERTY_SECTION_NAME);
4864
32
  if (s != NULL && s->size != 0)
4865
0
    {
4866
      /* We need a PT_GNU_PROPERTY segment.  */
4867
0
      ++segs;
4868
0
    }
4869
4870
230
  for (s = abfd->sections; s != NULL; s = s->next)
4871
198
    {
4872
198
      if ((s->flags & SEC_LOAD) != 0
4873
120
    && elf_section_type (s) == SHT_NOTE)
4874
14
  {
4875
14
    unsigned int alignment_power;
4876
    /* We need a PT_NOTE segment.  */
4877
14
    ++segs;
4878
    /* Try to create just one PT_NOTE segment for all adjacent
4879
       loadable SHT_NOTE sections.  gABI requires that within a
4880
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4881
       each note should have the same alignment.  So we check
4882
       whether the sections are correctly aligned.  */
4883
14
    alignment_power = s->alignment_power;
4884
14
    while (s->next != NULL
4885
14
     && s->next->alignment_power == alignment_power
4886
12
     && (s->next->flags & SEC_LOAD) != 0
4887
8
     && elf_section_type (s->next) == SHT_NOTE)
4888
0
      s = s->next;
4889
14
  }
4890
198
    }
4891
4892
120
  for (s = abfd->sections; s != NULL; s = s->next)
4893
106
    {
4894
106
      if (s->flags & SEC_THREAD_LOCAL)
4895
18
  {
4896
    /* We need a PT_TLS segment.  */
4897
18
    ++segs;
4898
18
    break;
4899
18
  }
4900
106
    }
4901
4902
32
  bed = get_elf_backend_data (abfd);
4903
4904
32
  if ((abfd->flags & D_PAGED) != 0
4905
32
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4906
0
    {
4907
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4908
0
      bfd_vma commonpagesize;
4909
0
      unsigned int page_align_power;
4910
4911
0
      if (info != NULL)
4912
0
  commonpagesize = info->commonpagesize;
4913
0
      else
4914
0
  commonpagesize = bed->commonpagesize;
4915
0
      page_align_power = bfd_log2 (commonpagesize);
4916
0
      for (s = abfd->sections; s != NULL; s = s->next)
4917
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4918
0
    {
4919
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4920
0
        {
4921
0
    _bfd_error_handler
4922
      /* xgettext:c-format */
4923
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4924
0
         "sh_info field: %d"),
4925
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4926
0
    continue;
4927
0
        }
4928
      /* Align mbind section to page size.  */
4929
0
      if (s->alignment_power < page_align_power)
4930
0
        s->alignment_power = page_align_power;
4931
0
      segs ++;
4932
0
    }
4933
0
    }
4934
4935
  /* Let the backend count up any program headers it might need.  */
4936
32
  if (bed->elf_backend_additional_program_headers)
4937
27
    {
4938
27
      int a;
4939
4940
27
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4941
27
      if (a == -1)
4942
0
  abort ();
4943
27
      segs += a;
4944
27
    }
4945
4946
32
  return segs * bed->s->sizeof_phdr;
4947
32
}
4948
4949
/* Find the segment that contains the output_section of section.  */
4950
4951
Elf_Internal_Phdr *
4952
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4953
0
{
4954
0
  struct elf_segment_map *m;
4955
0
  Elf_Internal_Phdr *p;
4956
4957
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4958
0
       m != NULL;
4959
0
       m = m->next, p++)
4960
0
    {
4961
0
      int i;
4962
4963
0
      for (i = m->count - 1; i >= 0; i--)
4964
0
  if (m->sections[i] == section)
4965
0
    return p;
4966
0
    }
4967
4968
0
  return NULL;
4969
0
}
4970
4971
/* Create a mapping from a set of sections to a program segment.  */
4972
4973
static struct elf_segment_map *
4974
make_mapping (bfd *abfd,
4975
        asection **sections,
4976
        unsigned int from,
4977
        unsigned int to,
4978
        bool phdr)
4979
105
{
4980
105
  struct elf_segment_map *m;
4981
105
  unsigned int i;
4982
105
  asection **hdrpp;
4983
105
  size_t amt;
4984
4985
105
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4986
105
  amt += (to - from) * sizeof (asection *);
4987
105
  m = bfd_zalloc (abfd, amt);
4988
105
  if (m == NULL)
4989
0
    return NULL;
4990
105
  m->next = NULL;
4991
105
  m->p_type = PT_LOAD;
4992
236
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4993
131
    m->sections[i - from] = *hdrpp;
4994
105
  m->count = to - from;
4995
4996
105
  if (from == 0 && phdr)
4997
3
    {
4998
      /* Include the headers in the first PT_LOAD segment.  */
4999
3
      m->includes_filehdr = 1;
5000
3
      m->includes_phdrs = 1;
5001
3
    }
5002
5003
105
  return m;
5004
105
}
5005
5006
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5007
   on failure.  */
5008
5009
struct elf_segment_map *
5010
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5011
6
{
5012
6
  struct elf_segment_map *m;
5013
5014
6
  m = bfd_zalloc (abfd, sizeof (*m));
5015
6
  if (m == NULL)
5016
0
    return NULL;
5017
6
  m->next = NULL;
5018
6
  m->p_type = PT_DYNAMIC;
5019
6
  m->count = 1;
5020
6
  m->sections[0] = dynsec;
5021
5022
6
  return m;
5023
6
}
5024
5025
/* Possibly add or remove segments from the segment map.  */
5026
5027
static bool
5028
elf_modify_segment_map (bfd *abfd,
5029
      struct bfd_link_info *info,
5030
      bool remove_empty_load)
5031
103
{
5032
103
  struct elf_segment_map **m;
5033
103
  elf_backend_data *bed;
5034
5035
  /* The placement algorithm assumes that non allocated sections are
5036
     not in PT_LOAD segments.  We ensure this here by removing such
5037
     sections from the segment map.  We also remove excluded
5038
     sections.  Finally, any PT_LOAD segment without sections is
5039
     removed.  */
5040
103
  m = &elf_seg_map (abfd);
5041
450
  while (*m)
5042
347
    {
5043
347
      unsigned int i, new_count;
5044
5045
1.30k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5046
959
  {
5047
959
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5048
913
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5049
363
      || (*m)->p_type != PT_LOAD))
5050
913
      {
5051
913
        (*m)->sections[new_count] = (*m)->sections[i];
5052
913
        new_count++;
5053
913
      }
5054
959
  }
5055
347
      (*m)->count = new_count;
5056
5057
347
      if (remove_empty_load
5058
347
    && (*m)->p_type == PT_LOAD
5059
124
    && (*m)->count == 0
5060
32
    && !(*m)->includes_phdrs)
5061
30
  *m = (*m)->next;
5062
317
      else
5063
317
  m = &(*m)->next;
5064
347
    }
5065
5066
103
  bed = get_elf_backend_data (abfd);
5067
103
  if (bed->elf_backend_modify_segment_map != NULL)
5068
36
    {
5069
36
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5070
0
  return false;
5071
36
    }
5072
5073
103
  return true;
5074
103
}
5075
5076
#define IS_TBSS(s) \
5077
162
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5078
5079
/* Set up a mapping from BFD sections to program segments.  Update
5080
   NEED_LAYOUT if the section layout is changed.  */
5081
5082
bool
5083
bfd_elf_map_sections_to_segments (bfd *abfd,
5084
          struct bfd_link_info *info,
5085
          bool *need_layout)
5086
106
{
5087
106
  unsigned int count;
5088
106
  struct elf_segment_map *m;
5089
106
  asection **sections = NULL;
5090
106
  elf_backend_data *bed = get_elf_backend_data (abfd);
5091
106
  bool no_user_phdrs;
5092
5093
106
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5094
5095
106
  if (info != NULL)
5096
0
    {
5097
0
      info->user_phdrs = !no_user_phdrs;
5098
5099
      /* Size the relative relocations if DT_RELR is enabled.  */
5100
0
      if (info->enable_dt_relr
5101
0
    && need_layout != NULL
5102
0
    && bed->size_relative_relocs
5103
0
    && !bed->size_relative_relocs (info, need_layout))
5104
0
  info->callbacks->fatal
5105
0
    (_("%P: failed to size relative relocations\n"));
5106
0
    }
5107
5108
106
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5109
32
    {
5110
32
      asection *s;
5111
32
      unsigned int i;
5112
32
      struct elf_segment_map *mfirst;
5113
32
      struct elf_segment_map **pm;
5114
32
      asection *last_hdr;
5115
32
      bfd_vma last_size;
5116
32
      unsigned int hdr_index;
5117
32
      bfd_vma maxpagesize;
5118
32
      asection **hdrpp;
5119
32
      bool phdr_in_segment;
5120
32
      bool writable;
5121
32
      bool executable;
5122
32
      unsigned int tls_count = 0;
5123
32
      asection *first_tls = NULL;
5124
32
      asection *first_mbind = NULL;
5125
32
      asection *dynsec, *eh_frame_hdr;
5126
32
      asection *sframe;
5127
32
      size_t amt;
5128
32
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5129
32
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5130
32
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5131
5132
      /* Select the allocated sections, and sort them.  */
5133
5134
32
      sections = bfd_malloc (bfd_count_sections (abfd) * sizeof (*sections));
5135
32
      if (sections == NULL)
5136
0
  goto error_return;
5137
5138
      /* Calculate top address, avoiding undefined behaviour of shift
5139
   left operator when shift count is equal to size of type
5140
   being shifted.  */
5141
32
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5142
32
      addr_mask = (addr_mask << 1) + 1;
5143
5144
32
      i = 0;
5145
230
      for (s = abfd->sections; s != NULL; s = s->next)
5146
198
  {
5147
198
    if ((s->flags & SEC_ALLOC) != 0)
5148
132
      {
5149
        /* target_index is unused until bfd_elf_final_link
5150
     starts output of section symbols.  Use it to make
5151
     qsort stable.  */
5152
132
        s->target_index = i;
5153
132
        sections[i] = s;
5154
132
        ++i;
5155
        /* A wrapping section potentially clashes with header.  */
5156
132
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5157
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5158
132
      }
5159
198
  }
5160
32
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5161
32
      count = i;
5162
5163
32
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5164
5165
32
      phdr_size = elf_program_header_size (abfd);
5166
32
      if (phdr_size == (bfd_size_type) -1)
5167
32
  phdr_size = get_program_header_size (abfd, info);
5168
32
      phdr_size += bed->s->sizeof_ehdr;
5169
      /* phdr_size is compared to LMA values which are in bytes.  */
5170
32
      phdr_size /= opb;
5171
32
      if (info != NULL)
5172
0
  maxpagesize = info->maxpagesize;
5173
32
      else
5174
32
  maxpagesize = bed->maxpagesize;
5175
32
      if (maxpagesize == 0)
5176
0
  maxpagesize = 1;
5177
32
      phdr_in_segment = info != NULL && info->load_phdrs;
5178
32
      if (count != 0
5179
30
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5180
30
        >= (phdr_size & (maxpagesize - 1))))
5181
  /* For compatibility with old scripts that may not be using
5182
     SIZEOF_HEADERS, add headers when it looks like space has
5183
     been left for them.  */
5184
3
  phdr_in_segment = true;
5185
5186
      /* Build the mapping.  */
5187
32
      mfirst = NULL;
5188
32
      pm = &mfirst;
5189
5190
      /* If we have a .interp section, then create a PT_PHDR segment for
5191
   the program headers and a PT_INTERP segment for the .interp
5192
   section.  */
5193
32
      s = bfd_get_section_by_name (abfd, ".interp");
5194
32
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5195
0
  {
5196
0
    m = bfd_zalloc (abfd, sizeof (*m));
5197
0
    if (m == NULL)
5198
0
      goto error_return;
5199
0
    m->next = NULL;
5200
0
    m->p_type = PT_PHDR;
5201
0
    m->p_flags = PF_R;
5202
0
    m->p_flags_valid = 1;
5203
0
    m->includes_phdrs = 1;
5204
0
    phdr_in_segment = true;
5205
0
    *pm = m;
5206
0
    pm = &m->next;
5207
5208
0
    m = bfd_zalloc (abfd, sizeof (*m));
5209
0
    if (m == NULL)
5210
0
      goto error_return;
5211
0
    m->next = NULL;
5212
0
    m->p_type = PT_INTERP;
5213
0
    m->count = 1;
5214
0
    m->sections[0] = s;
5215
5216
0
    *pm = m;
5217
0
    pm = &m->next;
5218
0
  }
5219
5220
      /* Look through the sections.  We put sections in the same program
5221
   segment when the start of the second section can be placed within
5222
   a few bytes of the end of the first section.  */
5223
32
      last_hdr = NULL;
5224
32
      last_size = 0;
5225
32
      hdr_index = 0;
5226
32
      writable = false;
5227
32
      executable = false;
5228
32
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5229
32
      if (dynsec != NULL
5230
8
    && (dynsec->flags & SEC_LOAD) == 0)
5231
2
  dynsec = NULL;
5232
5233
32
      if ((abfd->flags & D_PAGED) == 0)
5234
0
  phdr_in_segment = false;
5235
5236
      /* Deal with -Ttext or something similar such that the first section
5237
   is not adjacent to the program headers.  This is an
5238
   approximation, since at this point we don't know exactly how many
5239
   program headers we will need.  */
5240
32
      if (phdr_in_segment && count > 0)
5241
3
  {
5242
3
    bfd_vma phdr_lma;  /* Bytes.  */
5243
3
    bool separate_phdr = false;
5244
5245
3
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5246
3
    if (info != NULL
5247
0
        && info->separate_code
5248
0
        && (sections[0]->flags & SEC_CODE) != 0)
5249
0
      {
5250
        /* If data sections should be separate from code and
5251
     thus not executable, and the first section is
5252
     executable then put the file and program headers in
5253
     their own PT_LOAD.  */
5254
0
        if (!info->one_rosegment)
5255
0
    separate_phdr = true;
5256
5257
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5258
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5259
0
    {
5260
      /* The file and program headers are currently on the
5261
         same page as the first section.  Put them on the
5262
         previous page if we can.  */
5263
0
      if (phdr_lma >= maxpagesize)
5264
0
        phdr_lma -= maxpagesize;
5265
0
      else
5266
0
        separate_phdr = false;
5267
0
    }
5268
0
      }
5269
3
    if ((sections[0]->lma & addr_mask) < phdr_lma
5270
3
        || (sections[0]->lma & addr_mask) < phdr_size)
5271
      /* If file and program headers would be placed at the end
5272
         of memory then it's probably better to omit them.  */
5273
0
      phdr_in_segment = false;
5274
3
    else if (phdr_lma < wrap_to)
5275
      /* If a section wraps around to where we'll be placing
5276
         file and program headers, then the headers will be
5277
         overwritten.  */
5278
0
      phdr_in_segment = false;
5279
3
    else if (separate_phdr)
5280
0
      {
5281
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5282
0
        if (m == NULL)
5283
0
    goto error_return;
5284
0
        m->p_paddr = phdr_lma * opb;
5285
0
        m->p_vaddr_offset
5286
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5287
0
        m->p_paddr_valid = 1;
5288
0
        *pm = m;
5289
0
        pm = &m->next;
5290
0
        phdr_in_segment = false;
5291
0
      }
5292
3
  }
5293
5294
164
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5295
132
  {
5296
132
    asection *hdr;
5297
132
    bool new_segment;
5298
5299
132
    hdr = *hdrpp;
5300
5301
    /* See if this section and the last one will fit in the same
5302
       segment.  */
5303
5304
132
    if (last_hdr == NULL)
5305
30
      {
5306
        /* If we don't have a segment yet, then we don't need a new
5307
     one (we build the last one after this loop).  */
5308
30
        new_segment = false;
5309
30
      }
5310
102
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5311
2
      {
5312
        /* If this section has a different relation between the
5313
     virtual address and the load address, then we need a new
5314
     segment.  */
5315
2
        new_segment = true;
5316
2
      }
5317
100
    else if (hdr->lma < last_hdr->lma + last_size
5318
67
       || last_hdr->lma + last_size < last_hdr->lma)
5319
33
      {
5320
        /* If this section has a load address that makes it overlap
5321
     the previous section, then we need a new segment.  */
5322
33
        new_segment = true;
5323
33
      }
5324
67
    else if ((abfd->flags & D_PAGED) != 0
5325
67
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5326
67
           == (hdr->lma & -maxpagesize)))
5327
9
      {
5328
        /* If we are demand paged then we can't map two disk
5329
     pages onto the same memory page.  */
5330
9
        new_segment = false;
5331
9
      }
5332
    /* In the next test we have to be careful when last_hdr->lma is close
5333
       to the end of the address space.  If the aligned address wraps
5334
       around to the start of the address space, then there are no more
5335
       pages left in memory and it is OK to assume that the current
5336
       section can be included in the current segment.  */
5337
58
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5338
58
        + maxpagesize > last_hdr->lma)
5339
58
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5340
58
           + maxpagesize <= hdr->lma))
5341
38
      {
5342
        /* If putting this section in this segment would force us to
5343
     skip a page in the segment, then we need a new segment.  */
5344
38
        new_segment = true;
5345
38
      }
5346
20
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5347
2
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5348
1
      {
5349
        /* We don't want to put a loaded section after a
5350
     nonloaded (ie. bss style) section in the same segment
5351
     as that will force the non-loaded section to be loaded.
5352
     Consider .tbss sections as loaded for this purpose.  */
5353
1
        new_segment = true;
5354
1
      }
5355
19
    else if ((abfd->flags & D_PAGED) == 0)
5356
0
      {
5357
        /* If the file is not demand paged, which means that we
5358
     don't require the sections to be correctly aligned in the
5359
     file, then there is no other reason for a new segment.  */
5360
0
        new_segment = false;
5361
0
      }
5362
19
    else if (info != NULL
5363
0
       && info->separate_code
5364
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5365
0
      {
5366
0
        new_segment = true;
5367
0
      }
5368
19
    else if (! writable
5369
8
       && (hdr->flags & SEC_READONLY) == 0)
5370
2
      {
5371
        /* We don't want to put a writable section in a read only
5372
     segment.  */
5373
2
        new_segment = true;
5374
2
      }
5375
17
    else
5376
17
      {
5377
        /* Otherwise, we can use the same segment.  */
5378
17
        new_segment = false;
5379
17
      }
5380
5381
    /* Allow interested parties a chance to override our decision.  */
5382
132
    if (last_hdr != NULL
5383
102
        && info != NULL
5384
0
        && info->callbacks->override_segment_assignment != NULL)
5385
0
      new_segment
5386
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5387
0
                    last_hdr,
5388
0
                    new_segment);
5389
5390
132
    if (! new_segment)
5391
56
      {
5392
56
        if ((hdr->flags & SEC_READONLY) == 0)
5393
8
    writable = true;
5394
56
        if ((hdr->flags & SEC_CODE) != 0)
5395
23
    executable = true;
5396
56
        last_hdr = hdr;
5397
        /* .tbss sections effectively have zero size.  */
5398
56
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5399
56
        continue;
5400
56
      }
5401
5402
    /* We need a new program segment.  We must create a new program
5403
       header holding all the sections from hdr_index until hdr.  */
5404
5405
76
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5406
76
    if (m == NULL)
5407
0
      goto error_return;
5408
5409
76
    *pm = m;
5410
76
    pm = &m->next;
5411
5412
76
    if ((hdr->flags & SEC_READONLY) == 0)
5413
27
      writable = true;
5414
49
    else
5415
49
      writable = false;
5416
5417
76
    if ((hdr->flags & SEC_CODE) == 0)
5418
67
      executable = false;
5419
9
    else
5420
9
      executable = true;
5421
5422
76
    last_hdr = hdr;
5423
    /* .tbss sections effectively have zero size.  */
5424
76
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5425
76
    hdr_index = i;
5426
76
    phdr_in_segment = false;
5427
76
  }
5428
5429
      /* Create a final PT_LOAD program segment, but not if it's just
5430
   for .tbss.  */
5431
32
      if (last_hdr != NULL
5432
30
    && (i - hdr_index != 1
5433
28
        || !IS_TBSS (last_hdr)))
5434
29
  {
5435
29
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5436
29
    if (m == NULL)
5437
0
      goto error_return;
5438
5439
29
    *pm = m;
5440
29
    pm = &m->next;
5441
29
  }
5442
5443
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5444
32
      if (dynsec != NULL)
5445
6
  {
5446
6
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5447
6
    if (m == NULL)
5448
0
      goto error_return;
5449
6
    *pm = m;
5450
6
    pm = &m->next;
5451
6
  }
5452
5453
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5454
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5455
   because if we link together nonloadable .note sections and
5456
   loadable .note sections, we will generate two .note sections
5457
   in the output file.  */
5458
230
      for (s = abfd->sections; s != NULL; s = s->next)
5459
198
  {
5460
198
    if ((s->flags & SEC_LOAD) != 0
5461
120
        && elf_section_type (s) == SHT_NOTE)
5462
14
      {
5463
14
        asection *s2;
5464
14
        unsigned int alignment_power = s->alignment_power;
5465
5466
14
        count = 1;
5467
14
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5468
14
    {
5469
14
      if (s2->next->alignment_power == alignment_power
5470
12
          && (s2->next->flags & SEC_LOAD) != 0
5471
8
          && elf_section_type (s2->next) == SHT_NOTE
5472
0
          && align_power (s2->lma + s2->size / opb,
5473
0
              alignment_power)
5474
0
          == s2->next->lma)
5475
0
        count++;
5476
14
      else
5477
14
        break;
5478
14
    }
5479
14
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5480
14
        amt += count * sizeof (asection *);
5481
14
        m = bfd_zalloc (abfd, amt);
5482
14
        if (m == NULL)
5483
0
    goto error_return;
5484
14
        m->next = NULL;
5485
14
        m->p_type = PT_NOTE;
5486
14
        m->count = count;
5487
14
        while (count > 1)
5488
0
    {
5489
0
      m->sections[m->count - count--] = s;
5490
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5491
0
      s = s->next;
5492
0
    }
5493
14
        m->sections[m->count - 1] = s;
5494
14
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5495
14
        *pm = m;
5496
14
        pm = &m->next;
5497
14
      }
5498
198
    if (s->flags & SEC_THREAD_LOCAL)
5499
27
      {
5500
27
        if (! tls_count)
5501
18
    first_tls = s;
5502
27
        tls_count++;
5503
27
      }
5504
198
    if (first_mbind == NULL
5505
122
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5506
18
      first_mbind = s;
5507
198
  }
5508
5509
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5510
32
      if (tls_count > 0)
5511
18
  {
5512
18
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5513
18
    amt += tls_count * sizeof (asection *);
5514
18
    m = bfd_zalloc (abfd, amt);
5515
18
    if (m == NULL)
5516
0
      goto error_return;
5517
18
    m->next = NULL;
5518
18
    m->p_type = PT_TLS;
5519
18
    m->count = tls_count;
5520
    /* Mandated PF_R.  */
5521
18
    m->p_flags = PF_R;
5522
18
    m->p_flags_valid = 1;
5523
18
    s = first_tls;
5524
41
    for (i = 0; i < tls_count; ++i)
5525
26
      {
5526
26
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5527
3
    {
5528
3
      _bfd_error_handler
5529
3
        (_("%pB: TLS sections are not adjacent:"), abfd);
5530
3
      s = first_tls;
5531
3
      i = 0;
5532
21
      while (i < tls_count)
5533
18
        {
5534
18
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5535
7
      {
5536
7
        _bfd_error_handler (_("      TLS: %pA"), s);
5537
7
        i++;
5538
7
      }
5539
11
          else
5540
11
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5541
18
          s = s->next;
5542
18
        }
5543
3
      bfd_set_error (bfd_error_bad_value);
5544
3
      goto error_return;
5545
3
    }
5546
23
        m->sections[i] = s;
5547
23
        s = s->next;
5548
23
      }
5549
5550
15
    *pm = m;
5551
15
    pm = &m->next;
5552
15
  }
5553
5554
29
      if (first_mbind
5555
15
    && (abfd->flags & D_PAGED) != 0
5556
15
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5557
0
  for (s = first_mbind; s != NULL; s = s->next)
5558
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5559
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5560
0
      {
5561
        /* Mandated PF_R.  */
5562
0
        unsigned long p_flags = PF_R;
5563
0
        if ((s->flags & SEC_READONLY) == 0)
5564
0
    p_flags |= PF_W;
5565
0
        if ((s->flags & SEC_CODE) != 0)
5566
0
    p_flags |= PF_X;
5567
5568
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5569
0
        m = bfd_zalloc (abfd, amt);
5570
0
        if (m == NULL)
5571
0
    goto error_return;
5572
0
        m->next = NULL;
5573
0
        m->p_type = (PT_GNU_MBIND_LO
5574
0
         + elf_section_data (s)->this_hdr.sh_info);
5575
0
        m->count = 1;
5576
0
        m->p_flags_valid = 1;
5577
0
        m->sections[0] = s;
5578
0
        m->p_flags = p_flags;
5579
5580
0
        *pm = m;
5581
0
        pm = &m->next;
5582
0
      }
5583
5584
29
      s = bfd_get_section_by_name (abfd,
5585
29
           NOTE_GNU_PROPERTY_SECTION_NAME);
5586
29
      if (s != NULL && s->size != 0)
5587
0
  {
5588
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5589
0
    m = bfd_zalloc (abfd, amt);
5590
0
    if (m == NULL)
5591
0
      goto error_return;
5592
0
    m->next = NULL;
5593
0
    m->p_type = PT_GNU_PROPERTY;
5594
0
    m->count = 1;
5595
0
    m->p_flags_valid = 1;
5596
0
    m->sections[0] = s;
5597
0
    m->p_flags = PF_R;
5598
0
    *pm = m;
5599
0
    pm = &m->next;
5600
0
  }
5601
5602
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5603
   segment.  */
5604
29
      eh_frame_hdr = elf_eh_frame_hdr (info);
5605
29
      if (eh_frame_hdr != NULL
5606
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5607
0
  {
5608
0
    m = bfd_zalloc (abfd, sizeof (*m));
5609
0
    if (m == NULL)
5610
0
      goto error_return;
5611
0
    m->next = NULL;
5612
0
    m->p_type = PT_GNU_EH_FRAME;
5613
0
    m->count = 1;
5614
0
    m->sections[0] = eh_frame_hdr->output_section;
5615
5616
0
    *pm = m;
5617
0
    pm = &m->next;
5618
0
  }
5619
5620
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5621
   segment.  */
5622
29
      sframe = elf_sframe (abfd);
5623
29
      if (sframe != NULL
5624
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5625
0
    && sframe->size != 0)
5626
0
  {
5627
0
    m = bfd_zalloc (abfd, sizeof (*m));
5628
0
    if (m == NULL)
5629
0
      goto error_return;
5630
0
    m->next = NULL;
5631
0
    m->p_type = PT_GNU_SFRAME;
5632
0
    m->count = 1;
5633
0
    m->sections[0] = sframe->output_section;
5634
5635
0
    *pm = m;
5636
0
    pm = &m->next;
5637
0
  }
5638
5639
29
      if (elf_stack_flags (abfd))
5640
0
  {
5641
0
    m = bfd_zalloc (abfd, sizeof (*m));
5642
0
    if (m == NULL)
5643
0
      goto error_return;
5644
0
    m->next = NULL;
5645
0
    m->p_type = PT_GNU_STACK;
5646
0
    m->p_flags = elf_stack_flags (abfd);
5647
0
    m->p_align = bed->stack_align;
5648
0
    m->p_flags_valid = 1;
5649
0
    m->p_align_valid = m->p_align != 0;
5650
0
    if (info->stacksize > 0)
5651
0
      {
5652
0
        m->p_size = info->stacksize;
5653
0
        m->p_size_valid = 1;
5654
0
      }
5655
5656
0
    *pm = m;
5657
0
    pm = &m->next;
5658
0
  }
5659
5660
29
      if (info != NULL && info->relro)
5661
0
  {
5662
0
    for (m = mfirst; m != NULL; m = m->next)
5663
0
      {
5664
0
        if (m->p_type == PT_LOAD
5665
0
      && m->count != 0
5666
0
      && m->sections[0]->vma >= info->relro_start
5667
0
      && m->sections[0]->vma < info->relro_end)
5668
0
    {
5669
0
      i = m->count;
5670
0
      while (--i != (unsigned) -1)
5671
0
        {
5672
0
          if (m->sections[i]->size > 0
5673
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5674
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5675
0
      break;
5676
0
        }
5677
5678
0
      if (i != (unsigned) -1)
5679
0
        break;
5680
0
    }
5681
0
      }
5682
5683
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5684
0
    if (m != NULL)
5685
0
      {
5686
0
        m = bfd_zalloc (abfd, sizeof (*m));
5687
0
        if (m == NULL)
5688
0
    goto error_return;
5689
0
        m->next = NULL;
5690
0
        m->p_type = PT_GNU_RELRO;
5691
0
        *pm = m;
5692
0
        pm = &m->next;
5693
0
      }
5694
0
  }
5695
5696
29
      free (sections);
5697
29
      elf_seg_map (abfd) = mfirst;
5698
29
    }
5699
5700
103
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5701
0
    return false;
5702
5703
420
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5704
317
    ++count;
5705
103
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5706
5707
103
  return true;
5708
5709
3
 error_return:
5710
3
  free (sections);
5711
3
  return false;
5712
103
}
5713
5714
/* Sort sections by address.  */
5715
5716
static int
5717
elf_sort_sections (const void *arg1, const void *arg2)
5718
2.01k
{
5719
2.01k
  const asection *sec1 = *(const asection **) arg1;
5720
2.01k
  const asection *sec2 = *(const asection **) arg2;
5721
2.01k
  bfd_size_type size1, size2;
5722
5723
  /* Sort by LMA first, since this is the address used to
5724
     place the section into a segment.  */
5725
2.01k
  if (sec1->lma < sec2->lma)
5726
1.34k
    return -1;
5727
674
  else if (sec1->lma > sec2->lma)
5728
569
    return 1;
5729
5730
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5731
     the same, and this will do nothing.  */
5732
105
  if (sec1->vma < sec2->vma)
5733
0
    return -1;
5734
105
  else if (sec1->vma > sec2->vma)
5735
13
    return 1;
5736
5737
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5738
5739
184
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5740
184
      && (x)->size != 0)
5741
5742
92
  if (TOEND (sec1))
5743
29
    {
5744
29
      if (!TOEND (sec2))
5745
0
  return 1;
5746
29
    }
5747
63
  else if (TOEND (sec2))
5748
0
    return -1;
5749
5750
92
#undef TOEND
5751
5752
  /* Sort by size, to put zero sized sections
5753
     before others at the same address.  */
5754
5755
92
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5756
92
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5757
5758
92
  if (size1 < size2)
5759
36
    return -1;
5760
56
  if (size1 > size2)
5761
17
    return 1;
5762
5763
39
  return sec1->target_index - sec2->target_index;
5764
56
}
5765
5766
/* This qsort comparison functions sorts PT_LOAD segments first and
5767
   by p_paddr, for assign_file_positions_for_load_sections.  */
5768
5769
static int
5770
elf_sort_segments (const void *arg1, const void *arg2)
5771
551
{
5772
551
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5773
551
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5774
5775
551
  if (m1->p_type != m2->p_type)
5776
473
    {
5777
473
      if (m1->p_type == PT_NULL)
5778
9
  return 1;
5779
464
      if (m2->p_type == PT_NULL)
5780
18
  return -1;
5781
446
      return m1->p_type < m2->p_type ? -1 : 1;
5782
464
    }
5783
78
  if (m1->includes_filehdr != m2->includes_filehdr)
5784
7
    return m1->includes_filehdr ? -1 : 1;
5785
71
  if (m1->no_sort_lma != m2->no_sort_lma)
5786
0
    return m1->no_sort_lma ? -1 : 1;
5787
71
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5788
60
    {
5789
60
      bfd_vma lma1, lma2;  /* Octets.  */
5790
60
      lma1 = 0;
5791
60
      if (m1->p_paddr_valid)
5792
9
  lma1 = m1->p_paddr;
5793
51
      else if (m1->count != 0)
5794
51
  {
5795
51
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5796
51
              m1->sections[0]);
5797
51
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5798
51
  }
5799
60
      lma2 = 0;
5800
60
      if (m2->p_paddr_valid)
5801
9
  lma2 = m2->p_paddr;
5802
51
      else if (m2->count != 0)
5803
51
  {
5804
51
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5805
51
              m2->sections[0]);
5806
51
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5807
51
  }
5808
60
      if (lma1 != lma2)
5809
45
  return lma1 < lma2 ? -1 : 1;
5810
60
    }
5811
26
  if (m1->idx != m2->idx)
5812
26
    return m1->idx < m2->idx ? -1 : 1;
5813
0
  return 0;
5814
26
}
5815
5816
/* Ian Lance Taylor writes:
5817
5818
   We shouldn't be using % with a negative signed number.  That's just
5819
   not good.  We have to make sure either that the number is not
5820
   negative, or that the number has an unsigned type.  When the types
5821
   are all the same size they wind up as unsigned.  When file_ptr is a
5822
   larger signed type, the arithmetic winds up as signed long long,
5823
   which is wrong.
5824
5825
   What we're trying to say here is something like ``increase OFF by
5826
   the least amount that will cause it to be equal to the VMA modulo
5827
   the page size.''  */
5828
/* In other words, something like:
5829
5830
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5831
   off_offset = off % bed->maxpagesize;
5832
   if (vma_offset < off_offset)
5833
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5834
   else
5835
     adjustment = vma_offset - off_offset;
5836
5837
   which can be collapsed into the expression below.  */
5838
5839
static file_ptr
5840
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5841
183
{
5842
  /* PR binutils/16199: Handle an alignment of zero.  */
5843
183
  if (maxpagesize == 0)
5844
1
    maxpagesize = 1;
5845
183
  return ((vma - off) % maxpagesize);
5846
183
}
5847
5848
static void
5849
print_segment_map (const struct elf_segment_map *m)
5850
1
{
5851
1
  unsigned int j;
5852
1
  const char *pt = get_segment_type (m->p_type);
5853
1
  char buf[32];
5854
5855
1
  if (pt == NULL)
5856
0
    {
5857
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5858
0
  sprintf (buf, "LOPROC+%7.7x",
5859
0
     (unsigned int) (m->p_type - PT_LOPROC));
5860
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5861
0
  sprintf (buf, "LOOS+%7.7x",
5862
0
     (unsigned int) (m->p_type - PT_LOOS));
5863
0
      else
5864
0
  snprintf (buf, sizeof (buf), "%8.8x",
5865
0
      (unsigned int) m->p_type);
5866
0
      pt = buf;
5867
0
    }
5868
1
  fflush (stdout);
5869
1
  fprintf (stderr, "%s:", pt);
5870
6
  for (j = 0; j < m->count; j++)
5871
5
    fprintf (stderr, " %s", m->sections [j]->name);
5872
1
  putc ('\n',stderr);
5873
1
  fflush (stderr);
5874
1
}
5875
5876
/* Assign file positions to the sections based on the mapping from
5877
   sections to segments.  This function also sets up some fields in
5878
   the file header.  */
5879
5880
static bool
5881
assign_file_positions_for_load_sections (bfd *abfd,
5882
           struct bfd_link_info *link_info)
5883
106
{
5884
106
  elf_backend_data *bed = get_elf_backend_data (abfd);
5885
106
  struct elf_segment_map *m;
5886
106
  struct elf_segment_map *phdr_load_seg;
5887
106
  Elf_Internal_Phdr *phdrs;
5888
106
  Elf_Internal_Phdr *p;
5889
106
  ufile_ptr off;  /* Octets.  */
5890
106
  bfd_size_type maxpagesize;
5891
106
  unsigned int alloc, actual;
5892
106
  unsigned int i, j;
5893
106
  struct elf_segment_map **sorted_seg_map;
5894
106
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5895
5896
106
  if (link_info == NULL
5897
106
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5898
3
    return false;
5899
5900
103
  alloc = 0;
5901
420
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5902
317
    m->idx = alloc++;
5903
5904
103
  if (alloc)
5905
53
    {
5906
53
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5907
53
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5908
53
    }
5909
50
  else
5910
50
    {
5911
      /* PR binutils/12467.  */
5912
50
      elf_elfheader (abfd)->e_phoff = 0;
5913
50
      elf_elfheader (abfd)->e_phentsize = 0;
5914
50
    }
5915
5916
103
  elf_elfheader (abfd)->e_phnum = alloc;
5917
5918
103
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5919
0
    {
5920
0
      actual = alloc;
5921
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5922
0
    }
5923
103
  else
5924
103
    {
5925
103
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5926
103
      BFD_ASSERT (elf_program_header_size (abfd)
5927
103
      == actual * bed->s->sizeof_phdr);
5928
103
      BFD_ASSERT (actual >= alloc);
5929
103
    }
5930
5931
103
  if (alloc == 0)
5932
50
    {
5933
50
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5934
50
      return true;
5935
50
    }
5936
5937
  /* We're writing the size in elf_program_header_size (abfd),
5938
     see assign_file_positions_except_relocs, so make sure we have
5939
     that amount allocated, with trailing space cleared.
5940
     The variable alloc contains the computed need, while
5941
     elf_program_header_size (abfd) contains the size used for the
5942
     layout.
5943
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5944
     where the layout is forced to according to a larger size in the
5945
     last iterations for the testcase ld-elf/header.  */
5946
53
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5947
53
           + alloc * sizeof (*sorted_seg_map)));
5948
53
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5949
53
  elf_tdata (abfd)->phdr = phdrs;
5950
53
  if (phdrs == NULL)
5951
0
    return false;
5952
5953
370
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5954
317
    {
5955
317
      sorted_seg_map[j] = m;
5956
      /* If elf_segment_map is not from map_sections_to_segments, the
5957
   sections may not be correctly ordered.  NOTE: sorting should
5958
   not be done to the PT_NOTE section of a corefile, which may
5959
   contain several pseudo-sections artificially created by bfd.
5960
   Sorting these pseudo-sections breaks things badly.  */
5961
317
      if (m->count > 1
5962
93
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5963
26
         && m->p_type == PT_NOTE))
5964
93
  {
5965
899
    for (i = 0; i < m->count; i++)
5966
806
      m->sections[i]->target_index = i;
5967
93
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5968
93
     elf_sort_sections);
5969
93
  }
5970
317
    }
5971
53
  if (alloc > 1)
5972
47
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5973
47
     elf_sort_segments);
5974
5975
53
  maxpagesize = 1;
5976
53
  if ((abfd->flags & D_PAGED) != 0)
5977
49
    {
5978
49
      if (link_info != NULL)
5979
0
  maxpagesize = link_info->maxpagesize;
5980
49
      else
5981
49
  maxpagesize = bed->maxpagesize;
5982
49
    }
5983
5984
  /* Sections must map to file offsets past the ELF file header.  */
5985
53
  off = bed->s->sizeof_ehdr;
5986
  /* And if one of the PT_LOAD headers doesn't include the program
5987
     headers then we'll be mapping program headers in the usual
5988
     position after the ELF file header.  */
5989
53
  phdr_load_seg = NULL;
5990
126
  for (j = 0; j < alloc; j++)
5991
117
    {
5992
117
      m = sorted_seg_map[j];
5993
117
      if (m->p_type != PT_LOAD)
5994
27
  break;
5995
90
      if (m->includes_phdrs)
5996
17
  {
5997
17
    phdr_load_seg = m;
5998
17
    break;
5999
17
  }
6000
90
    }
6001
53
  if (phdr_load_seg == NULL)
6002
36
    off += actual * bed->s->sizeof_phdr;
6003
6004
343
  for (j = 0; j < alloc; j++)
6005
297
    {
6006
297
      asection **secpp;
6007
297
      bfd_vma off_adjust;  /* Octets.  */
6008
297
      bool no_contents;
6009
297
      bfd_size_type align_pagesize;
6010
6011
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6012
   number of sections with contents contributing to both p_filesz
6013
   and p_memsz, followed by a number of sections with no contents
6014
   that just contribute to p_memsz.  In this loop, OFF tracks next
6015
   available file offset for PT_LOAD and PT_NOTE segments.  */
6016
297
      m = sorted_seg_map[j];
6017
297
      p = phdrs + m->idx;
6018
297
      p->p_type = m->p_type;
6019
297
      p->p_flags = m->p_flags;
6020
6021
297
      if (m->count == 0)
6022
109
  p->p_vaddr = m->p_vaddr_offset * opb;
6023
188
      else
6024
188
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6025
6026
297
      if (m->p_paddr_valid)
6027
182
  p->p_paddr = m->p_paddr;
6028
115
      else if (m->count == 0)
6029
21
  p->p_paddr = 0;
6030
94
      else
6031
94
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6032
6033
297
      align_pagesize = 0;
6034
297
      if (p->p_type == PT_LOAD
6035
94
    && (abfd->flags & D_PAGED) != 0)
6036
86
  {
6037
    /* p_align in demand paged PT_LOAD segments effectively stores
6038
       the maximum page size.  When copying an executable with
6039
       objcopy, we set m->p_align from the input file.  Use this
6040
       value for maxpagesize rather than bed->maxpagesize, which
6041
       may be different.  Note that we use maxpagesize for PT_TLS
6042
       segment alignment later in this function, so we are relying
6043
       on at least one PT_LOAD segment appearing before a PT_TLS
6044
       segment.  */
6045
86
    if (m->p_align_valid)
6046
8
      maxpagesize = m->p_align;
6047
78
    else if (bed->p_align != 0
6048
22
       && (link_info == NULL
6049
0
           || !link_info->maxpagesize_is_set))
6050
      /* We will lay out this binary using maxpagesize but set
6051
         p->p_align later to the possibly smaller bed->p_align.
6052
         The run-time loader will then be able to load this
6053
         binary when the system page size is maxpagesize, but if
6054
         the system page size is smaller can use p->p_align.
6055
         In either case p->p_align will be increased if
6056
         necessary to match section alignment.  */
6057
22
      align_pagesize = bed->p_align;
6058
6059
86
    p->p_align = maxpagesize;
6060
86
  }
6061
211
      else if (m->p_align_valid)
6062
73
  p->p_align = m->p_align;
6063
138
      else if (m->count == 0)
6064
73
  p->p_align = 1 << bed->s->log_file_align;
6065
6066
297
      if (m == phdr_load_seg)
6067
17
  off += actual * bed->s->sizeof_phdr;
6068
6069
297
      no_contents = false;
6070
297
      off_adjust = 0;
6071
297
      if (p->p_type == PT_LOAD
6072
94
    && m->count > 0)
6073
92
  {
6074
92
    bfd_size_type align;  /* Bytes.  */
6075
92
    unsigned int align_power = 0;
6076
6077
92
    if (m->p_align_valid)
6078
6
      align = p->p_align;
6079
86
    else
6080
86
      {
6081
321
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6082
235
    {
6083
235
      unsigned int secalign;
6084
6085
235
      secalign = bfd_section_alignment (*secpp);
6086
235
      if (secalign > align_power)
6087
84
        align_power = secalign;
6088
235
    }
6089
86
        align = (bfd_size_type) 1 << align_power;
6090
        /* If a section requires alignment higher than the
6091
     minimum p_align value, don't reduce a maxpagesize
6092
     p->p_align set earlier in this function.  */
6093
86
        if (align > bed->p_align)
6094
65
    align_pagesize = 0;
6095
86
        if (align < maxpagesize)
6096
74
    align = maxpagesize;
6097
12
        else
6098
12
    {
6099
      /* If a section requires alignment higher than the
6100
         maximum page size, set p_align to the section
6101
         alignment.  */
6102
12
      if ((abfd->flags & D_PAGED) != 0)
6103
4
        p->p_align = align;
6104
12
    }
6105
86
      }
6106
6107
364
    for (i = 0; i < m->count; i++)
6108
272
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6109
        /* If we aren't making room for this section, then
6110
     it must be SHT_NOBITS regardless of what we've
6111
     set via struct bfd_elf_special_section.  */
6112
18
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6113
6114
    /* Find out whether this segment contains any loadable
6115
       sections.  */
6116
92
    no_contents = true;
6117
101
    for (i = 0; i < m->count; i++)
6118
99
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6119
90
        {
6120
90
    no_contents = false;
6121
90
    break;
6122
90
        }
6123
6124
92
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6125
6126
    /* Broken hardware and/or kernel require that files do not
6127
       map the same page with different permissions on some hppa
6128
       processors.  */
6129
92
    if (j != 0
6130
47
        && (abfd->flags & D_PAGED) != 0
6131
41
        && bed->no_page_alias
6132
0
        && (off & (maxpagesize - 1)) != 0
6133
0
        && ((off & -maxpagesize)
6134
0
      == ((off + off_adjust) & -maxpagesize)))
6135
0
      off_adjust += maxpagesize;
6136
92
    off += off_adjust;
6137
92
    if (no_contents)
6138
2
      {
6139
        /* We shouldn't need to align the segment on disk since
6140
     the segment doesn't need file space, but the gABI
6141
     arguably requires the alignment and glibc ld.so
6142
     checks it.  So to comply with the alignment
6143
     requirement but not waste file space, we adjust
6144
     p_offset for just this segment.  (OFF_ADJUST is
6145
     subtracted from OFF later.)  This may put p_offset
6146
     past the end of file, but that shouldn't matter.  */
6147
2
      }
6148
90
    else
6149
90
      off_adjust = 0;
6150
92
  }
6151
      /* Make sure the .dynamic section is the first section in the
6152
   PT_DYNAMIC segment.  */
6153
205
      else if (p->p_type == PT_DYNAMIC
6154
27
         && m->count > 1
6155
3
         && ! streq (m->sections[0]->name, ".dynamic"))
6156
1
  {
6157
1
    _bfd_error_handler
6158
1
      (_("%pB: The first section in the PT_DYNAMIC segment"
6159
1
         " is not the .dynamic section"),
6160
1
       abfd);
6161
1
    bfd_set_error (bfd_error_bad_value);
6162
1
    return false;
6163
1
  }
6164
      /* Set the note section type to SHT_NOTE.  */
6165
204
      else if (p->p_type == PT_NOTE)
6166
105
  for (i = 0; i < m->count; i++)
6167
72
    elf_section_type (m->sections[i]) = SHT_NOTE;
6168
6169
296
      if (m->includes_filehdr)
6170
23
  {
6171
23
    if (!m->p_flags_valid)
6172
3
      p->p_flags |= PF_R;
6173
23
    p->p_filesz = bed->s->sizeof_ehdr;
6174
23
    p->p_memsz = bed->s->sizeof_ehdr;
6175
23
    if (p->p_type == PT_LOAD)
6176
17
      {
6177
17
        if (m->count > 0)
6178
15
    {
6179
15
      if (p->p_vaddr < off
6180
15
          || (!m->p_paddr_valid
6181
3
        && p->p_paddr < off))
6182
1
        {
6183
1
          _bfd_error_handler
6184
1
      (_("%pB: not enough room for program headers,"
6185
1
         " try linking with -N"),
6186
1
       abfd);
6187
1
          bfd_set_error (bfd_error_bad_value);
6188
1
          return false;
6189
1
        }
6190
14
      p->p_vaddr -= off;
6191
14
      if (!m->p_paddr_valid)
6192
2
        p->p_paddr -= off;
6193
14
    }
6194
17
      }
6195
6
    else if (sorted_seg_map[0]->includes_filehdr)
6196
3
      {
6197
3
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6198
3
        p->p_vaddr = filehdr->p_vaddr;
6199
3
        if (!m->p_paddr_valid)
6200
0
    p->p_paddr = filehdr->p_paddr;
6201
3
      }
6202
23
  }
6203
6204
295
      if (m->includes_phdrs)
6205
30
  {
6206
30
    if (!m->p_flags_valid)
6207
2
      p->p_flags |= PF_R;
6208
30
    p->p_filesz += actual * bed->s->sizeof_phdr;
6209
30
    p->p_memsz += actual * bed->s->sizeof_phdr;
6210
30
    if (!m->includes_filehdr)
6211
11
      {
6212
11
        if (p->p_type == PT_LOAD)
6213
0
    {
6214
0
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6215
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6216
0
      if (m->count > 0)
6217
0
        {
6218
0
          p->p_vaddr -= off - p->p_offset;
6219
0
          if (!m->p_paddr_valid)
6220
0
      p->p_paddr -= off - p->p_offset;
6221
0
        }
6222
0
    }
6223
11
        else if (phdr_load_seg != NULL)
6224
8
    {
6225
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6226
         sorted segments so that phdr_load_seg will
6227
         already be set by the code immediately above.  */
6228
8
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6229
8
      bfd_vma phdr_off = 0;  /* Octets.  */
6230
8
      if (phdr_load_seg->includes_filehdr)
6231
8
        phdr_off = bed->s->sizeof_ehdr;
6232
8
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6233
8
      if (!m->p_paddr_valid)
6234
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6235
8
      p->p_offset = phdr->p_offset + phdr_off;
6236
8
    }
6237
3
        else
6238
3
    p->p_offset = bed->s->sizeof_ehdr;
6239
11
      }
6240
30
  }
6241
6242
295
      if (p->p_type == PT_LOAD
6243
202
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6244
93
  {
6245
93
    if (!m->includes_filehdr && !m->includes_phdrs)
6246
77
      {
6247
77
        p->p_offset = off;
6248
77
        if (no_contents)
6249
2
    {
6250
      /* Put meaningless p_offset for PT_LOAD segments
6251
         without file contents somewhere within the first
6252
         page, in an attempt to not point past EOF.  */
6253
2
      bfd_size_type align = maxpagesize;
6254
2
      if (align < p->p_align)
6255
0
        align = p->p_align;
6256
2
      if (align < 1)
6257
1
        align = 1;
6258
      /* Avoid p_offset of zero, which might be wrongly
6259
         interpreted as the segment being the first one,
6260
         containing the file header.  PR32763.  */
6261
2
      p->p_offset = (off + align - 1) % align + 1;
6262
2
    }
6263
77
      }
6264
16
    else
6265
16
      {
6266
16
        file_ptr adjust;  /* Octets.  */
6267
6268
16
        adjust = off - (p->p_offset + p->p_filesz);
6269
16
        if (!no_contents)
6270
16
    p->p_filesz += adjust;
6271
16
        p->p_memsz += adjust;
6272
16
      }
6273
93
  }
6274
6275
295
      if (align_pagesize)
6276
20
  p->p_align = align_pagesize;
6277
6278
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6279
   maps.  Set filepos for sections in PT_LOAD segments, and in
6280
   core files, for sections in PT_NOTE segments.
6281
   assign_file_positions_for_non_load_sections will set filepos
6282
   for other sections and update p_filesz for other segments.  */
6283
1.16k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6284
874
  {
6285
874
    asection *sec;
6286
874
    bfd_size_type align;
6287
874
    Elf_Internal_Shdr *this_hdr;
6288
6289
874
    sec = *secpp;
6290
874
    this_hdr = &elf_section_data (sec)->this_hdr;
6291
874
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6292
6293
874
    if ((p->p_type == PT_LOAD
6294
603
         || p->p_type == PT_DYNAMIC
6295
579
         || p->p_type == PT_TLS)
6296
302
        && (this_hdr->sh_type != SHT_NOBITS
6297
20
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6298
20
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6299
2
        || p->p_type == PT_TLS))))
6300
300
      {
6301
300
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6302
300
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6303
300
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6304
300
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6305
6306
300
        if (adjust != 0
6307
117
      && (s_start < p_end
6308
103
          || p_end < p_start))
6309
14
    {
6310
14
      _bfd_error_handler
6311
        /* xgettext:c-format */
6312
14
        (_("%pB: section %pA lma %#" PRIx64
6313
14
           " adjusted to %#" PRIx64),
6314
14
         abfd, sec, (uint64_t) s_start / opb,
6315
14
         (uint64_t) p_end / opb);
6316
14
      adjust = 0;
6317
14
      sec->lma = p_end / opb;
6318
14
    }
6319
300
        p->p_memsz += adjust;
6320
6321
300
        if (p->p_type == PT_LOAD)
6322
269
    {
6323
269
      if (this_hdr->sh_type != SHT_NOBITS)
6324
253
        {
6325
253
          off -= off_adjust;
6326
253
          off_adjust = 0;
6327
253
          if (p->p_filesz + adjust < p->p_memsz)
6328
4
      {
6329
        /* We have a PROGBITS section following NOBITS ones.
6330
           Allocate file space for the NOBITS section(s).
6331
           We don't need to write out the zeros, posix
6332
           fseek past the end of data already written
6333
           followed by a write at that location is
6334
           guaranteed to result in zeros being read
6335
           from the gap.  */
6336
4
        adjust = p->p_memsz - p->p_filesz;
6337
4
      }
6338
253
        }
6339
      /* We only adjust sh_offset in SHT_NOBITS sections
6340
         as would seem proper for their address when the
6341
         section is first in the segment.  sh_offset
6342
         doesn't really have any significance for
6343
         SHT_NOBITS anyway, apart from a notional position
6344
         relative to other sections.  Historically we
6345
         didn't bother with adjusting sh_offset and some
6346
         programs depend on it not being adjusted.  See
6347
         pr12921 and pr25662.  */
6348
269
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6349
259
        {
6350
259
          off += adjust;
6351
259
          if (this_hdr->sh_type == SHT_NOBITS)
6352
6
      off_adjust += adjust;
6353
259
        }
6354
269
    }
6355
300
        if (this_hdr->sh_type != SHT_NOBITS)
6356
282
    p->p_filesz += adjust;
6357
300
      }
6358
6359
874
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6360
0
      {
6361
        /* The section at i == 0 is the one that actually contains
6362
     everything.  */
6363
0
        if (i == 0)
6364
0
    {
6365
0
      this_hdr->sh_offset = sec->filepos = off;
6366
0
      off += this_hdr->sh_size;
6367
0
      p->p_filesz = this_hdr->sh_size;
6368
0
      p->p_memsz = 0;
6369
0
      p->p_align = 1;
6370
0
    }
6371
0
        else
6372
0
    {
6373
      /* The rest are fake sections that shouldn't be written.  */
6374
0
      sec->filepos = 0;
6375
0
      sec->size = 0;
6376
0
      sec->flags = 0;
6377
0
      continue;
6378
0
    }
6379
0
      }
6380
874
    else
6381
874
      {
6382
874
        if (this_hdr->sh_type == SHT_NOBITS
6383
55
      && (this_hdr->sh_flags & SHF_TLS) != 0
6384
2
      && this_hdr->sh_offset == 0)
6385
2
    {
6386
      /* Set sh_offset for .tbss sections to their nominal
6387
         offset after aligning.  They are not loaded from
6388
         disk so the value doesn't really matter, except
6389
         when the .tbss section is the first one in a
6390
         PT_TLS segment.  In that case it sets the
6391
         p_offset for the PT_TLS segment, which according
6392
         to the ELF gABI ought to satisfy
6393
         p_offset % p_align == p_vaddr % p_align.  */
6394
2
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6395
2
                off, align);
6396
2
      this_hdr->sh_offset = sec->filepos = off + adjust;
6397
2
    }
6398
872
        else if (p->p_type == PT_LOAD)
6399
269
    {
6400
269
      this_hdr->sh_offset = sec->filepos = off;
6401
269
      if (this_hdr->sh_type != SHT_NOBITS)
6402
253
        off += this_hdr->sh_size;
6403
269
    }
6404
6405
874
        if (this_hdr->sh_type != SHT_NOBITS)
6406
819
    {
6407
819
      p->p_filesz += this_hdr->sh_size;
6408
      /* A load section without SHF_ALLOC is something like
6409
         a note section in a PT_NOTE segment.  These take
6410
         file space but are not loaded into memory.  */
6411
819
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6412
461
        p->p_memsz += this_hdr->sh_size;
6413
819
    }
6414
55
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6415
55
    {
6416
55
      if (p->p_type == PT_TLS)
6417
0
        p->p_memsz += this_hdr->sh_size;
6418
6419
      /* .tbss is special.  It doesn't contribute to p_memsz of
6420
         normal segments.  */
6421
55
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6422
53
        p->p_memsz += this_hdr->sh_size;
6423
55
    }
6424
6425
874
        if (align > p->p_align
6426
364
      && !m->p_align_valid
6427
83
      && (p->p_type != PT_LOAD
6428
10
          || (abfd->flags & D_PAGED) == 0))
6429
83
    p->p_align = align;
6430
874
      }
6431
6432
874
    if (!m->p_flags_valid)
6433
93
      {
6434
93
        p->p_flags |= PF_R;
6435
93
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6436
21
    p->p_flags |= PF_X;
6437
93
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6438
29
    p->p_flags |= PF_W;
6439
93
      }
6440
874
  }
6441
6442
295
      off -= off_adjust;
6443
6444
      /* PR ld/20815 - Check that the program header segment, if
6445
   present, will be loaded into memory.  */
6446
295
      if (p->p_type == PT_PHDR
6447
18
    && phdr_load_seg == NULL
6448
5
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6449
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6450
5
  {
6451
    /* The fix for this error is usually to edit the linker script being
6452
       used and set up the program headers manually.  Either that or
6453
       leave room for the headers at the start of the SECTIONS.  */
6454
5
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6455
5
        " by LOAD segment"),
6456
5
            abfd);
6457
5
    if (link_info == NULL)
6458
5
      return false;
6459
    /* Arrange for the linker to exit with an error, deleting
6460
       the output file unless --noinhibit-exec is given.  */
6461
0
    link_info->callbacks->info ("%X");
6462
0
  }
6463
6464
      /* Check that all sections are in a PT_LOAD segment.
6465
   Don't check funky gdb generated core files.  */
6466
290
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6467
85
  {
6468
85
    bool check_vma = true;
6469
6470
257
    for (i = 1; i < m->count; i++)
6471
172
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6472
3
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6473
3
               ->this_hdr), p) != 0
6474
2
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6475
2
               ->this_hdr), p) != 0)
6476
0
        {
6477
    /* Looks like we have overlays packed into the segment.  */
6478
0
    check_vma = false;
6479
0
    break;
6480
0
        }
6481
6482
340
    for (i = 0; i < m->count; i++)
6483
255
      {
6484
255
        Elf_Internal_Shdr *this_hdr;
6485
255
        asection *sec;
6486
6487
255
        sec = m->sections[i];
6488
255
        this_hdr = &(elf_section_data(sec)->this_hdr);
6489
255
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6490
1
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6491
1
    {
6492
1
      _bfd_error_handler
6493
        /* xgettext:c-format */
6494
1
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6495
1
         abfd, sec, m->idx);
6496
1
      print_segment_map (m);
6497
1
    }
6498
255
      }
6499
85
  }
6500
290
    }
6501
6502
46
  elf_next_file_pos (abfd) = off;
6503
6504
46
  if (link_info != NULL
6505
0
      && phdr_load_seg != NULL
6506
0
      && phdr_load_seg->includes_filehdr)
6507
0
    {
6508
      /* There is a segment that contains both the file headers and the
6509
   program headers, so provide a symbol __ehdr_start pointing there.
6510
   A program can use this to examine itself robustly.  */
6511
6512
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6513
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6514
6515
      /* If the symbol was referenced and not defined, define it.  */
6516
0
      if (hash != NULL
6517
0
    && (hash->root.type == bfd_link_hash_new
6518
0
        || hash->root.type == bfd_link_hash_undefined
6519
0
        || hash->root.type == bfd_link_hash_undefweak
6520
0
        || hash->root.type == bfd_link_hash_common))
6521
0
  {
6522
0
    asection *s = NULL;
6523
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6524
6525
0
    if (phdr_load_seg->count != 0)
6526
      /* The segment contains sections, so use the first one.  */
6527
0
      s = phdr_load_seg->sections[0];
6528
0
    else
6529
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6530
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6531
0
        if (m->p_type == PT_LOAD && m->count != 0)
6532
0
    {
6533
0
      s = m->sections[0];
6534
0
      break;
6535
0
    }
6536
6537
0
    if (s != NULL)
6538
0
      {
6539
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6540
0
        hash->root.u.def.section = s;
6541
0
      }
6542
0
    else
6543
0
      {
6544
0
        hash->root.u.def.value = filehdr_vaddr;
6545
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6546
0
      }
6547
6548
0
    hash->root.type = bfd_link_hash_defined;
6549
0
    hash->def_regular = 1;
6550
0
    hash->non_elf = 0;
6551
0
  }
6552
0
    }
6553
6554
46
  return true;
6555
53
}
6556
6557
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6558
   is no defined method for detecting such files, so we have to
6559
   use heuristics instead.  */
6560
6561
bool
6562
is_debuginfo_file (bfd *abfd)
6563
87
{
6564
87
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6565
0
    return false;
6566
6567
87
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6568
87
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6569
87
  Elf_Internal_Shdr **headerp;
6570
6571
274
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6572
271
    {
6573
271
      Elf_Internal_Shdr *header = * headerp;
6574
6575
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6576
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6577
271
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6578
181
    && header->sh_type != SHT_NOBITS
6579
181
    && header->sh_type != SHT_NOTE)
6580
84
  return false;
6581
271
    }
6582
6583
3
  return true;
6584
87
}
6585
6586
/* Assign file positions for other sections, except for compressed debug
6587
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6588
6589
static bool
6590
assign_file_positions_for_non_load_sections (bfd *abfd,
6591
               struct bfd_link_info *link_info)
6592
96
{
6593
96
  elf_backend_data *bed = get_elf_backend_data (abfd);
6594
96
  Elf_Internal_Shdr **i_shdrpp;
6595
96
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6596
96
  Elf_Internal_Phdr *phdrs;
6597
96
  Elf_Internal_Phdr *p;
6598
96
  struct elf_segment_map *m;
6599
96
  ufile_ptr off;
6600
96
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6601
96
  bfd_vma maxpagesize;
6602
6603
96
  if (link_info != NULL)
6604
0
    maxpagesize = link_info->maxpagesize;
6605
96
  else
6606
96
    maxpagesize = bed->maxpagesize;
6607
96
  i_shdrpp = elf_elfsections (abfd);
6608
96
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6609
96
  off = elf_next_file_pos (abfd);
6610
747
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6611
651
    {
6612
651
      Elf_Internal_Shdr *hdr;
6613
651
      bfd_vma align;
6614
6615
651
      hdr = *hdrpp;
6616
651
      if (hdr->bfd_section != NULL
6617
525
    && (hdr->bfd_section->filepos != 0
6618
265
        || (hdr->sh_type == SHT_NOBITS
6619
5
      && hdr->contents == NULL)))
6620
265
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6621
386
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6622
89
  {
6623
89
    if (hdr->sh_size != 0
6624
        /* PR 24717 - debuginfo files are known to be not strictly
6625
     compliant with the ELF standard.  In particular they often
6626
     have .note.gnu.property sections that are outside of any
6627
     loadable segment.  This is not a problem for such files,
6628
     so do not warn about them.  */
6629
87
        && ! is_debuginfo_file (abfd))
6630
84
      _bfd_error_handler
6631
        /* xgettext:c-format */
6632
84
        (_("%pB: warning: allocated section `%s' not in segment"),
6633
84
         abfd,
6634
84
         (hdr->bfd_section == NULL
6635
84
    ? "*unknown*"
6636
84
    : hdr->bfd_section->name));
6637
    /* We don't need to page align empty sections.  */
6638
89
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6639
87
      align = maxpagesize;
6640
2
    else
6641
2
      align = hdr->sh_addralign & -hdr->sh_addralign;
6642
89
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6643
89
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6644
89
                 bed->s->log_file_align);
6645
89
  }
6646
297
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6647
3
    && hdr->bfd_section == NULL)
6648
         /* We don't know the offset of these sections yet:
6649
      their size has not been decided.  */
6650
295
         || (abfd->is_linker_output
6651
0
       && hdr->bfd_section != NULL
6652
0
       && (hdr->sh_name == -1u
6653
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6654
295
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6655
281
         || (elf_symtab_shndx_list (abfd) != NULL
6656
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6657
281
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6658
267
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6659
126
  hdr->sh_offset = -1;
6660
171
      else
6661
171
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6662
651
    }
6663
96
  elf_next_file_pos (abfd) = off;
6664
6665
  /* Now that we have set the section file positions, we can set up
6666
     the file positions for the non PT_LOAD segments.  */
6667
96
  phdrs = elf_tdata (abfd)->phdr;
6668
362
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6669
268
    {
6670
268
      if (p->p_type == PT_GNU_RELRO)
6671
4
  {
6672
4
    bfd_vma start, end;  /* Bytes.  */
6673
4
    bool ok;
6674
6675
4
    if (link_info != NULL)
6676
0
      {
6677
        /* During linking the range of the RELRO segment is passed
6678
     in link_info.  Note that there may be padding between
6679
     relro_start and the first RELRO section.  */
6680
0
        start = link_info->relro_start;
6681
0
        end = link_info->relro_end;
6682
0
      }
6683
4
    else if (m->count != 0)
6684
4
      {
6685
4
        if (!m->p_size_valid)
6686
0
    abort ();
6687
4
        start = m->sections[0]->vma;
6688
4
        end = start + m->p_size / opb;
6689
4
      }
6690
0
    else
6691
0
      {
6692
0
        start = 0;
6693
0
        end = 0;
6694
0
      }
6695
6696
4
    ok = false;
6697
4
    if (start < end)
6698
4
      {
6699
4
        struct elf_segment_map *lm;
6700
4
        const Elf_Internal_Phdr *lp;
6701
4
        unsigned int i;
6702
6703
        /* Find a LOAD segment containing a section in the RELRO
6704
     segment.  */
6705
4
        for (lm = elf_seg_map (abfd), lp = phdrs;
6706
27
       lm != NULL;
6707
23
       lm = lm->next, lp++)
6708
24
    {
6709
24
      if (lp->p_type == PT_LOAD
6710
4
          && lm->count != 0
6711
2
          && (lm->sections[lm->count - 1]->vma
6712
2
        + (!IS_TBSS (lm->sections[lm->count - 1])
6713
2
           ? lm->sections[lm->count - 1]->size / opb
6714
2
           : 0)) > start
6715
1
          && lm->sections[0]->vma < end)
6716
1
        break;
6717
24
    }
6718
6719
4
        if (lm != NULL)
6720
1
    {
6721
      /* Find the section starting the RELRO segment.  */
6722
15
      for (i = 0; i < lm->count; i++)
6723
14
        {
6724
14
          asection *s = lm->sections[i];
6725
14
          if (s->vma >= start
6726
1
        && s->vma < end
6727
0
        && s->size != 0)
6728
0
      break;
6729
14
        }
6730
6731
1
      if (i < lm->count)
6732
0
        {
6733
0
          p->p_vaddr = lm->sections[i]->vma * opb;
6734
0
          p->p_paddr = lm->sections[i]->lma * opb;
6735
0
          p->p_offset = lm->sections[i]->filepos;
6736
0
          p->p_memsz = end * opb - p->p_vaddr;
6737
0
          p->p_filesz = p->p_memsz;
6738
6739
          /* The RELRO segment typically ends a few bytes
6740
       into .got.plt but other layouts are possible.
6741
       In cases where the end does not match any
6742
       loaded section (for instance is in file
6743
       padding), trim p_filesz back to correspond to
6744
       the end of loaded section contents.  */
6745
0
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6746
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6747
6748
          /* Preserve the alignment and flags if they are
6749
       valid.  The gold linker generates RW/4 for
6750
       the PT_GNU_RELRO section.  It is better for
6751
       objcopy/strip to honor these attributes
6752
       otherwise gdb will choke when using separate
6753
       debug files.  */
6754
0
          if (!m->p_align_valid)
6755
0
      p->p_align = 1;
6756
0
          if (!m->p_flags_valid)
6757
0
      p->p_flags = PF_R;
6758
0
          ok = true;
6759
0
        }
6760
1
    }
6761
4
      }
6762
6763
4
    if (!ok)
6764
4
      {
6765
4
        if (link_info != NULL)
6766
0
    _bfd_error_handler
6767
0
      (_("%pB: warning: unable to allocate any sections"
6768
0
         " to PT_GNU_RELRO segment"),
6769
0
       abfd);
6770
4
        memset (p, 0, sizeof *p);
6771
4
      }
6772
4
  }
6773
264
      else if (p->p_type == PT_GNU_STACK)
6774
7
  {
6775
7
    if (m->p_size_valid)
6776
2
      p->p_memsz = m->p_size;
6777
7
  }
6778
257
      else if (m->count != 0)
6779
170
  {
6780
170
    unsigned int i;
6781
6782
170
    if (p->p_type != PT_LOAD
6783
84
        && (p->p_type != PT_NOTE
6784
14
      || bfd_get_format (abfd) != bfd_core))
6785
84
      {
6786
        /* A user specified segment layout may include a PHDR
6787
     segment that overlaps with a LOAD segment...  */
6788
84
        if (p->p_type == PT_PHDR)
6789
1
    {
6790
1
      m->count = 0;
6791
1
      continue;
6792
1
    }
6793
6794
83
        if (m->includes_filehdr || m->includes_phdrs)
6795
2
    {
6796
      /* PR 17512: file: 2195325e.  */
6797
2
      _bfd_error_handler
6798
2
        (_("%pB: error: non-load segment %d includes file header "
6799
2
           "and/or program header"),
6800
2
         abfd, (int) (p - phdrs));
6801
2
      return false;
6802
2
    }
6803
6804
81
        p->p_filesz = 0;
6805
81
        p->p_offset = m->sections[0]->filepos;
6806
92
        for (i = m->count; i-- != 0;)
6807
85
    {
6808
85
      asection *sect = m->sections[i];
6809
85
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6810
85
      if (hdr->sh_type != SHT_NOBITS)
6811
74
        {
6812
74
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6813
          /* NB: p_memsz of the loadable PT_NOTE segment
6814
       should be the same as p_filesz.  */
6815
74
          if (p->p_type == PT_NOTE
6816
14
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6817
12
      p->p_memsz = p->p_filesz;
6818
74
          break;
6819
74
        }
6820
85
    }
6821
81
      }
6822
170
  }
6823
268
    }
6824
6825
94
  return true;
6826
96
}
6827
6828
static elf_section_list *
6829
find_section_in_list (unsigned int i, elf_section_list * list)
6830
1.00k
{
6831
1.02k
  for (;list != NULL; list = list->next)
6832
20
    if (list->ndx == i)
6833
1
      break;
6834
1.00k
  return list;
6835
1.00k
}
6836
6837
/* Work out the file positions of all the sections.  This is called by
6838
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6839
   VMAs must be known before this is called.
6840
6841
   Reloc sections come in two flavours: Those processed specially as
6842
   "side-channel" data attached to a section to which they apply, and
6843
   those that bfd doesn't process as relocations.  The latter sort are
6844
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6845
   consider the former sort here, unless they form part of the loadable
6846
   image.  Reloc sections not assigned here (and compressed debugging
6847
   sections and CTF sections which nothing else in the file can rely
6848
   upon) will be handled later by assign_file_positions_for_relocs.
6849
6850
   We also don't set the positions of the .symtab and .strtab here.  */
6851
6852
static bool
6853
assign_file_positions_except_relocs (bfd *abfd,
6854
             struct bfd_link_info *link_info)
6855
397
{
6856
397
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6857
397
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6858
397
  elf_backend_data *bed = get_elf_backend_data (abfd);
6859
397
  unsigned int alloc;
6860
6861
397
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6862
302
      && bfd_get_format (abfd) != bfd_core)
6863
291
    {
6864
291
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6865
291
      unsigned int num_sec = elf_numsections (abfd);
6866
291
      Elf_Internal_Shdr **hdrpp;
6867
291
      unsigned int i;
6868
291
      ufile_ptr off;
6869
6870
      /* Start after the ELF header.  */
6871
291
      off = i_ehdrp->e_ehsize;
6872
6873
      /* We are not creating an executable, which means that we are
6874
   not creating a program header, and that the actual order of
6875
   the sections in the file is unimportant.  */
6876
1.71k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6877
1.42k
  {
6878
1.42k
    Elf_Internal_Shdr *hdr;
6879
6880
1.42k
    hdr = *hdrpp;
6881
1.42k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6882
192
         && hdr->bfd_section == NULL)
6883
        /* Do not assign offsets for these sections yet: we don't know
6884
     their sizes.  */
6885
1.29k
        || (abfd->is_linker_output
6886
0
      && hdr->bfd_section != NULL
6887
0
      && (hdr->sh_name == -1u
6888
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6889
1.29k
        || i == elf_onesymtab (abfd)
6890
1.22k
        || (elf_symtab_shndx_list (abfd) != NULL
6891
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6892
1.22k
        || i == elf_strtab_sec (abfd)
6893
1.14k
        || i == elf_shstrtab_sec (abfd))
6894
562
      {
6895
562
        hdr->sh_offset = -1;
6896
562
      }
6897
858
    else
6898
      /* There shouldn't be a need to effect "capped" file alignment here,
6899
         yet at least the Linux kernel's modpost utility was found to be
6900
         unhappy without.  While the issue was addressed there, let's be
6901
         kind for at least the foreseeable future ...  */
6902
858
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6903
858
                   bed->s->log_file_align);
6904
1.42k
  }
6905
6906
291
      elf_next_file_pos (abfd) = off;
6907
291
      elf_program_header_size (abfd) = 0;
6908
291
    }
6909
106
  else
6910
106
    {
6911
      /* Assign file positions for the loaded sections based on the
6912
   assignment of sections to segments.  */
6913
106
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6914
10
  return false;
6915
6916
      /* And for non-load sections.  */
6917
96
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6918
2
  return false;
6919
96
    }
6920
6921
385
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6922
0
    return false;
6923
6924
  /* Write out the program headers.  */
6925
385
  alloc = i_ehdrp->e_phnum;
6926
385
  if (alloc != 0)
6927
44
    {
6928
44
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6929
0
  {
6930
0
    bool warned_tls = false;
6931
0
    bool warned_rwx = false;
6932
6933
    /* Memory resident segments with non-zero size and RWX
6934
       permissions are a security risk, so we generate a warning
6935
       here if we are creating any.  */
6936
0
    unsigned int i;
6937
6938
0
    for (i = 0; i < alloc; i++)
6939
0
      {
6940
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6941
6942
0
        if (phdr->p_memsz == 0)
6943
0
    continue;
6944
6945
0
        if (! warned_tls
6946
0
      && phdr->p_type == PT_TLS
6947
0
      && (phdr->p_flags & PF_X))
6948
0
    {
6949
0
      if (link_info->warn_is_error_for_rwx_segments)
6950
0
        {
6951
0
          _bfd_error_handler (_("\
6952
0
error: %pB has a TLS segment with execute permission"),
6953
0
            abfd);
6954
0
          return false;
6955
0
        }
6956
6957
0
      _bfd_error_handler (_("\
6958
0
warning: %pB has a TLS segment with execute permission"),
6959
0
              abfd);
6960
0
      if (warned_rwx)
6961
0
        break;
6962
6963
0
      warned_tls = true;
6964
0
    }
6965
0
        else if (! warned_rwx
6966
0
           && phdr->p_type == PT_LOAD
6967
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6968
0
         == (PF_R | PF_W | PF_X)))
6969
0
    {
6970
0
      if (link_info->warn_is_error_for_rwx_segments)
6971
0
        {
6972
0
          _bfd_error_handler (_("\
6973
0
error: %pB has a LOAD segment with RWX permissions"),
6974
0
            abfd);
6975
0
          return false;
6976
0
        }
6977
6978
0
      _bfd_error_handler (_("\
6979
0
warning: %pB has a LOAD segment with RWX permissions"),
6980
0
              abfd);
6981
0
      if (warned_tls)
6982
0
        break;
6983
6984
0
      warned_rwx = true;
6985
0
    }
6986
0
      }
6987
0
  }
6988
6989
44
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6990
44
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6991
0
  return false;
6992
44
    }
6993
6994
385
  return true;
6995
385
}
6996
6997
bool
6998
_bfd_elf_init_file_header (bfd *abfd,
6999
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7000
398
{
7001
398
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7002
398
  struct elf_strtab_hash *shstrtab;
7003
398
  elf_backend_data *bed = get_elf_backend_data (abfd);
7004
7005
398
  i_ehdrp = elf_elfheader (abfd);
7006
7007
398
  shstrtab = _bfd_elf_strtab_init ();
7008
398
  if (shstrtab == NULL)
7009
0
    return false;
7010
7011
398
  elf_shstrtab (abfd) = shstrtab;
7012
7013
398
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7014
398
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7015
398
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7016
398
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7017
7018
398
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7019
398
  i_ehdrp->e_ident[EI_DATA] =
7020
398
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7021
398
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7022
7023
398
  if ((abfd->flags & DYNAMIC) != 0)
7024
46
    i_ehdrp->e_type = ET_DYN;
7025
352
  else if ((abfd->flags & EXEC_P) != 0)
7026
49
    i_ehdrp->e_type = ET_EXEC;
7027
303
  else if (bfd_get_format (abfd) == bfd_core)
7028
11
    i_ehdrp->e_type = ET_CORE;
7029
292
  else
7030
292
    i_ehdrp->e_type = ET_REL;
7031
7032
398
  switch (bfd_get_arch (abfd))
7033
398
    {
7034
0
    case bfd_arch_unknown:
7035
0
      i_ehdrp->e_machine = EM_NONE;
7036
0
      break;
7037
7038
      /* There used to be a long list of cases here, each one setting
7039
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7040
   in the corresponding bfd definition.  To avoid duplication,
7041
   the switch was removed.  Machines that need special handling
7042
   can generally do it in elf_backend_final_write_processing(),
7043
   unless they need the information earlier than the final write.
7044
   Such need can generally be supplied by replacing the tests for
7045
   e_machine with the conditions used to determine it.  */
7046
398
    default:
7047
398
      i_ehdrp->e_machine = bed->elf_machine_code;
7048
398
    }
7049
7050
398
  i_ehdrp->e_version = bed->s->ev_current;
7051
398
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7052
7053
  /* No program header, for now.  */
7054
398
  i_ehdrp->e_phoff = 0;
7055
398
  i_ehdrp->e_phentsize = 0;
7056
398
  i_ehdrp->e_phnum = 0;
7057
7058
  /* Each bfd section is section header entry.  */
7059
398
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7060
398
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7061
7062
398
  elf_tdata (abfd)->symtab_hdr.sh_name =
7063
398
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7064
398
  elf_tdata (abfd)->strtab_hdr.sh_name =
7065
398
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7066
398
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7067
398
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7068
398
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7069
398
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7070
398
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7071
0
    return false;
7072
7073
398
  return true;
7074
398
}
7075
7076
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7077
7078
   FIXME: We used to have code here to sort the PT_LOAD segments into
7079
   ascending order, as per the ELF spec.  But this breaks some programs,
7080
   including the Linux kernel.  But really either the spec should be
7081
   changed or the programs updated.  */
7082
7083
bool
7084
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7085
385
{
7086
385
  if (link_info != NULL && bfd_link_pie (link_info))
7087
0
    {
7088
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7089
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7090
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7091
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7092
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7093
7094
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7095
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7096
0
      for (; segment < end_segment; segment++)
7097
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7098
0
    p_vaddr = segment->p_vaddr;
7099
7100
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7101
   segments is non-zero.  */
7102
0
      if (p_vaddr)
7103
0
  i_ehdrp->e_type = ET_EXEC;
7104
0
    }
7105
385
  return true;
7106
385
}
7107
7108
/* Assign file positions for all the reloc sections which are not part
7109
   of the loadable file image, and the file position of section headers.  */
7110
7111
static bool
7112
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7113
385
{
7114
385
  ufile_ptr off;
7115
385
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7116
385
  Elf_Internal_Shdr *shdrp;
7117
385
  Elf_Internal_Ehdr *i_ehdrp;
7118
385
  elf_backend_data *bed = get_elf_backend_data (abfd);
7119
7120
  /* Skip non-load sections without section header.  */
7121
385
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7122
0
    return true;
7123
7124
385
  off = elf_next_file_pos (abfd);
7125
7126
385
  shdrpp = elf_elfsections (abfd);
7127
385
  end_shdrpp = shdrpp + elf_numsections (abfd);
7128
2.44k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7129
2.05k
    {
7130
2.05k
      shdrp = *shdrpp;
7131
2.05k
      if (shdrp->sh_offset == -1)
7132
516
  {
7133
516
    asection *sec = shdrp->bfd_section;
7134
516
    if (sec == NULL
7135
0
        || shdrp->sh_type == SHT_REL
7136
0
        || shdrp->sh_type == SHT_RELA)
7137
516
      ;
7138
0
    else if (bfd_section_is_ctf (sec))
7139
0
      {
7140
        /* Update section size and contents.  */
7141
0
        shdrp->sh_size = sec->size;
7142
0
        shdrp->contents = sec->contents;
7143
0
      }
7144
0
    else if (shdrp->sh_name == -1u)
7145
0
      {
7146
0
        const char *name = sec->name;
7147
0
        struct bfd_elf_section_data *d;
7148
7149
        /* Compress DWARF debug sections.  */
7150
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7151
0
    return false;
7152
7153
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7154
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7155
0
      && name[1] == 'd')
7156
0
    {
7157
      /* If section is compressed with zlib-gnu, convert
7158
         section name from .debug_* to .zdebug_*.  */
7159
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7160
0
      if (new_name == NULL)
7161
0
        return false;
7162
0
      name = new_name;
7163
0
    }
7164
        /* Add section name to section name section.  */
7165
0
        shdrp->sh_name
7166
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7167
0
                  name, false);
7168
0
        d = elf_section_data (sec);
7169
7170
        /* Add reloc section name to section name section.  */
7171
0
        if (d->rel.hdr
7172
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7173
0
              name, false))
7174
0
    return false;
7175
0
        if (d->rela.hdr
7176
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7177
0
              name, true))
7178
0
    return false;
7179
7180
        /* Update section size and contents.  */
7181
0
        shdrp->sh_size = sec->size;
7182
0
        shdrp->contents = sec->contents;
7183
0
        sec->contents = NULL;
7184
0
      }
7185
7186
516
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7187
516
      (abfd->flags & (EXEC_P | DYNAMIC))
7188
430
      || bfd_get_format (abfd) == bfd_core,
7189
516
      bed->s->log_file_align);
7190
516
  }
7191
2.05k
    }
7192
7193
  /* Place section name section after DWARF debug sections have been
7194
     compressed.  */
7195
385
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7196
385
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7197
385
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7198
385
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7199
7200
  /* Place the section headers.  */
7201
385
  i_ehdrp = elf_elfheader (abfd);
7202
385
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7203
385
  i_ehdrp->e_shoff = off;
7204
385
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7205
385
  elf_next_file_pos (abfd) = off;
7206
7207
385
  return true;
7208
385
}
7209
7210
bool
7211
_bfd_elf_write_object_contents (bfd *abfd)
7212
387
{
7213
387
  elf_backend_data *bed = get_elf_backend_data (abfd);
7214
387
  Elf_Internal_Shdr **i_shdrp;
7215
387
  bool failed;
7216
387
  unsigned int count, num_sec;
7217
387
  struct elf_obj_tdata *t;
7218
7219
387
  if (! abfd->output_has_begun
7220
243
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7221
2
    return false;
7222
  /* Do not rewrite ELF data when the BFD has been opened for update.
7223
     abfd->output_has_begun was set to TRUE on opening, so creation of
7224
     new sections, and modification of existing section sizes was
7225
     restricted.  This means the ELF header, program headers and
7226
     section headers can't have changed.  If the contents of any
7227
     sections has been modified, then those changes have already been
7228
     written to the BFD.  */
7229
385
  else if (abfd->direction == both_direction)
7230
0
    {
7231
0
      BFD_ASSERT (abfd->output_has_begun);
7232
0
      return true;
7233
0
    }
7234
7235
385
  i_shdrp = elf_elfsections (abfd);
7236
7237
385
  failed = false;
7238
385
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7239
385
  if (failed)
7240
0
    return false;
7241
7242
385
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7243
0
    return false;
7244
7245
  /* After writing the headers, we need to write the sections too...  */
7246
385
  num_sec = elf_numsections (abfd);
7247
2.44k
  for (count = 1; count < num_sec; count++)
7248
2.05k
    {
7249
      /* Don't set the sh_name field without section header.  */
7250
2.05k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7251
2.05k
  i_shdrp[count]->sh_name
7252
2.05k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7253
2.05k
            i_shdrp[count]->sh_name);
7254
2.05k
      if (bed->elf_backend_section_processing)
7255
383
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7256
0
    return false;
7257
2.05k
      if (i_shdrp[count]->contents)
7258
212
  {
7259
212
    bfd_size_type amt = i_shdrp[count]->sh_size;
7260
7261
212
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7262
212
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7263
0
      return false;
7264
212
  }
7265
2.05k
    }
7266
7267
  /* Write out the section header names.  */
7268
385
  t = elf_tdata (abfd);
7269
385
  if (elf_shstrtab (abfd) != NULL
7270
385
      && t->shstrtab_hdr.sh_offset != -1
7271
385
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7272
384
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7273
1
    return false;
7274
7275
384
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7276
0
    return false;
7277
7278
384
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7279
0
    return false;
7280
7281
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7282
384
  if (t->o->build_id.after_write_object_contents != NULL
7283
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7284
0
    return false;
7285
384
  if (t->o->package_metadata.after_write_object_contents != NULL
7286
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7287
0
    return false;
7288
7289
384
  return true;
7290
384
}
7291
7292
bool
7293
_bfd_elf_write_corefile_contents (bfd *abfd)
7294
11
{
7295
  /* Hopefully this can be done just like an object file.  */
7296
11
  return _bfd_elf_write_object_contents (abfd);
7297
11
}
7298
7299
/* Given a section, search the header to find them.  */
7300
7301
unsigned int
7302
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7303
997
{
7304
997
  elf_backend_data *bed;
7305
997
  unsigned int sec_index;
7306
7307
997
  if (elf_section_data (asect) != NULL
7308
456
      && elf_section_data (asect)->this_idx != 0)
7309
456
    return elf_section_data (asect)->this_idx;
7310
7311
541
  if (bfd_is_abs_section (asect))
7312
0
    sec_index = SHN_ABS;
7313
541
  else if (bfd_is_com_section (asect))
7314
10
    sec_index = SHN_COMMON;
7315
531
  else if (bfd_is_und_section (asect))
7316
529
    sec_index = SHN_UNDEF;
7317
2
  else
7318
2
    sec_index = SHN_BAD;
7319
7320
541
  bed = get_elf_backend_data (abfd);
7321
541
  if (bed->elf_backend_section_from_bfd_section)
7322
398
    {
7323
398
      int retval = sec_index;
7324
7325
398
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7326
12
  return retval;
7327
398
    }
7328
7329
529
  if (sec_index == SHN_BAD)
7330
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7331
7332
529
  return sec_index;
7333
541
}
7334
7335
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7336
   on error.  */
7337
7338
int
7339
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7340
475
{
7341
475
  asymbol *asym_ptr = *asym_ptr_ptr;
7342
475
  int idx;
7343
475
  flagword flags = asym_ptr->flags;
7344
7345
  /* When gas creates relocations against local labels, it creates its
7346
     own symbol for the section, but does put the symbol into the
7347
     symbol chain, so udata is 0.  When the linker is generating
7348
     relocatable output, this section symbol may be for one of the
7349
     input sections rather than the output section.  */
7350
475
  if (asym_ptr->udata.i == 0
7351
0
      && (flags & BSF_SECTION_SYM)
7352
0
      && asym_ptr->section)
7353
0
    {
7354
0
      asection *sec;
7355
7356
0
      sec = asym_ptr->section;
7357
0
      if (sec->owner != abfd && sec->output_section != NULL)
7358
0
  sec = sec->output_section;
7359
0
      if (sec->owner == abfd
7360
0
    && sec->index < elf_num_section_syms (abfd)
7361
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7362
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7363
0
    }
7364
7365
475
  idx = asym_ptr->udata.i;
7366
7367
475
  if (idx == 0)
7368
0
    {
7369
      /* This case can occur when using --strip-symbol on a symbol
7370
   which is used in a relocation entry.  */
7371
0
      _bfd_error_handler
7372
  /* xgettext:c-format */
7373
0
  (_("%pB: symbol `%s' required but not present"),
7374
0
   abfd, bfd_asymbol_name (asym_ptr));
7375
0
      bfd_set_error (bfd_error_no_symbols);
7376
0
      return -1;
7377
0
    }
7378
7379
#if DEBUG & 4
7380
  {
7381
    fprintf (stderr,
7382
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7383
       " flags = 0x%.8x\n",
7384
       (long) asym_ptr, asym_ptr->name, idx, flags);
7385
    fflush (stderr);
7386
  }
7387
#endif
7388
7389
475
  return idx;
7390
475
}
7391
7392
static inline bfd_vma
7393
segment_size (Elf_Internal_Phdr *segment)
7394
4.81k
{
7395
4.81k
  return (segment->p_memsz > segment->p_filesz
7396
4.81k
    ? segment->p_memsz : segment->p_filesz);
7397
4.81k
}
7398
7399
7400
/* Returns the end address of the segment + 1.  */
7401
static inline bfd_vma
7402
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7403
94
{
7404
94
  return start + segment_size (segment);
7405
94
}
7406
7407
static inline bfd_size_type
7408
section_size (asection *section, Elf_Internal_Phdr *segment)
7409
4.75k
{
7410
4.75k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7411
945
      || (section->flags & SEC_THREAD_LOCAL) == 0
7412
12
      || segment->p_type == PT_TLS)
7413
4.74k
    return section->size;
7414
12
  return 0;
7415
4.75k
}
7416
7417
/* Returns TRUE if the given section is contained within the given
7418
   segment.  LMA addresses are compared against PADDR when
7419
   USE_VADDR is false, VMA against VADDR when true.  */
7420
static bool
7421
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7422
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7423
     bool use_vaddr)
7424
3.73k
{
7425
3.73k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7426
3.73k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7427
3.73k
  bfd_vma octet;
7428
3.73k
  if (_bfd_mul_overflow (addr, opb, &octet))
7429
0
    return false;
7430
  /* The third and fourth lines below are testing that the section end
7431
     address is within the segment.  It's written this way to avoid
7432
     overflow.  Add seg_addr + section_size to both sides of the
7433
     inequality to make it obvious.  */
7434
3.73k
  return (octet >= seg_addr
7435
2.60k
    && segment_size (segment) >= section_size (section, segment)
7436
2.10k
    && (octet - seg_addr
7437
2.10k
        <= segment_size (segment) - section_size (section, segment)));
7438
3.73k
}
7439
7440
/* Handle PT_NOTE segment.  */
7441
static bool
7442
is_note (asection *s, Elf_Internal_Phdr *p)
7443
2.57k
{
7444
2.57k
  return (p->p_type == PT_NOTE
7445
159
    && elf_section_type (s) == SHT_NOTE
7446
26
    && (ufile_ptr) s->filepos >= p->p_offset
7447
18
    && p->p_filesz >= s->size
7448
18
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7449
2.57k
}
7450
7451
/* Rewrite program header information.  */
7452
7453
static bool
7454
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7455
15
{
7456
15
  Elf_Internal_Ehdr *iehdr;
7457
15
  struct elf_segment_map *map;
7458
15
  struct elf_segment_map *map_first;
7459
15
  struct elf_segment_map **pointer_to_map;
7460
15
  Elf_Internal_Phdr *segment;
7461
15
  asection *section;
7462
15
  unsigned int i;
7463
15
  unsigned int num_segments;
7464
15
  bool phdr_included = false;
7465
15
  bool p_paddr_valid;
7466
15
  struct elf_segment_map *phdr_adjust_seg = NULL;
7467
15
  unsigned int phdr_adjust_num = 0;
7468
15
  elf_backend_data *bed;
7469
15
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7470
7471
15
  bed = get_elf_backend_data (ibfd);
7472
15
  iehdr = elf_elfheader (ibfd);
7473
7474
15
  map_first = NULL;
7475
15
  pointer_to_map = &map_first;
7476
7477
15
  num_segments = elf_elfheader (ibfd)->e_phnum;
7478
7479
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7480
     linker, which generates a PT_INTERP section with p_vaddr and
7481
     p_memsz set to 0.  */
7482
15
#define IS_SOLARIS_PT_INTERP(p, s)          \
7483
373
  (p->p_vaddr == 0              \
7484
373
   && p->p_paddr == 0              \
7485
373
   && p->p_memsz == 0              \
7486
373
   && p->p_filesz > 0              \
7487
373
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7488
373
   && s->size > 0              \
7489
373
   && (bfd_vma) s->filepos >= p->p_offset        \
7490
373
   && ((bfd_vma) s->filepos + s->size         \
7491
2
       <= p->p_offset + p->p_filesz))
7492
7493
  /* Decide if the given section should be included in the given segment.
7494
     A section will be included if:
7495
       1. It is within the address space of the segment -- we use the LMA
7496
    if that is set for the segment and the VMA otherwise,
7497
       2. It is an allocated section or a NOTE section in a PT_NOTE
7498
    segment.
7499
       3. There is an output section associated with it,
7500
       4. The section has not already been allocated to a previous segment.
7501
       5. PT_GNU_STACK segments do not include any sections.
7502
       6. PT_TLS segment includes only SHF_TLS sections.
7503
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7504
       8. PT_DYNAMIC should not contain empty sections at the beginning
7505
    (with the possible exception of .dynamic).  */
7506
15
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7507
3.91k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7508
3.35k
          segment->p_vaddr, opb, !paddr_valid)    \
7509
3.35k
     && (section->flags & SEC_ALLOC) != 0)       \
7510
3.35k
    || is_note (section, segment))         \
7511
3.35k
   && segment->p_type != PT_GNU_STACK          \
7512
3.35k
   && (segment->p_type != PT_TLS          \
7513
785
       || (section->flags & SEC_THREAD_LOCAL))        \
7514
3.35k
   && (segment->p_type == PT_LOAD          \
7515
784
       || segment->p_type == PT_TLS          \
7516
784
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7517
3.35k
   && (segment->p_type != PT_DYNAMIC          \
7518
778
       || section_size (section, segment) > 0        \
7519
778
       || (segment->p_paddr            \
7520
2
     ? segment->p_paddr != section->lma * (opb)      \
7521
2
     : segment->p_vaddr != section->vma * (opb))      \
7522
778
       || (streq (bfd_section_name (section), ".dynamic")))    \
7523
3.91k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7524
7525
/* If the output section of a section in the input segment is NULL,
7526
   it is removed from the corresponding output segment.   */
7527
15
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7528
567
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7529
567
   && section->output_section != NULL)
7530
7531
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7532
15
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7533
126
  (seg1->field >= segment_end (seg2, seg2->field))
7534
7535
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7536
     their VMA address ranges and their LMA address ranges overlap.
7537
     It is possible to have overlapping VMA ranges without overlapping LMA
7538
     ranges.  RedBoot images for example can have both .data and .bss mapped
7539
     to the same VMA range, but with the .data section mapped to a different
7540
     LMA.  */
7541
15
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7542
32
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7543
32
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7544
32
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7545
14
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7546
7547
  /* Initialise the segment mark field, and discard stupid alignment.  */
7548
288
  for (section = ibfd->sections; section != NULL; section = section->next)
7549
273
    {
7550
273
      asection *o = section->output_section;
7551
273
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7552
0
  o->alignment_power = 0;
7553
273
      section->segment_mark = false;
7554
273
    }
7555
7556
  /* The Solaris linker creates program headers in which all the
7557
     p_paddr fields are zero.  When we try to objcopy or strip such a
7558
     file, we get confused.  Check for this case, and if we find it
7559
     don't set the p_paddr_valid fields.  */
7560
15
  p_paddr_valid = false;
7561
15
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7562
16
       i < num_segments;
7563
15
       i++, segment++)
7564
16
    if (segment->p_paddr != 0)
7565
15
      {
7566
15
  p_paddr_valid = true;
7567
15
  break;
7568
15
      }
7569
7570
  /* Scan through the segments specified in the program header
7571
     of the input BFD.  For this first scan we look for overlaps
7572
     in the loadable segments.  These can be created by weird
7573
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7574
15
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7575
207
       i < num_segments;
7576
192
       i++, segment++)
7577
192
    {
7578
192
      unsigned int j;
7579
192
      Elf_Internal_Phdr *segment2;
7580
7581
192
      if (segment->p_type == PT_INTERP)
7582
393
  for (section = ibfd->sections; section; section = section->next)
7583
373
    if (IS_SOLARIS_PT_INTERP (segment, section))
7584
1
      {
7585
        /* Mininal change so that the normal section to segment
7586
     assignment code will work.  */
7587
1
        segment->p_vaddr = section->vma * opb;
7588
1
        break;
7589
1
      }
7590
7591
192
      if (segment->p_type != PT_LOAD)
7592
150
  {
7593
    /* Remove PT_GNU_RELRO segment.  */
7594
150
    if (segment->p_type == PT_GNU_RELRO)
7595
9
      segment->p_type = PT_NULL;
7596
150
    continue;
7597
150
  }
7598
7599
      /* Determine if this segment overlaps any previous segments.  */
7600
231
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7601
195
  {
7602
195
    bfd_signed_vma extra_length;
7603
7604
195
    if (segment2->p_type != PT_LOAD
7605
32
        || !SEGMENT_OVERLAPS (segment, segment2))
7606
188
      continue;
7607
7608
    /* Merge the two segments together.  */
7609
7
    if (segment2->p_vaddr < segment->p_vaddr)
7610
6
      {
7611
        /* Extend SEGMENT2 to include SEGMENT and then delete
7612
     SEGMENT.  */
7613
6
        extra_length = (segment_end (segment, segment->p_vaddr)
7614
6
            - segment_end (segment2, segment2->p_vaddr));
7615
7616
6
        if (extra_length > 0)
7617
3
    {
7618
3
      segment2->p_memsz += extra_length;
7619
3
      segment2->p_filesz += extra_length;
7620
3
    }
7621
7622
6
        segment->p_type = PT_NULL;
7623
7624
        /* Since we have deleted P we must restart the outer loop.  */
7625
6
        i = 0;
7626
6
        segment = elf_tdata (ibfd)->phdr;
7627
6
        break;
7628
6
      }
7629
1
    else
7630
1
      {
7631
        /* Extend SEGMENT to include SEGMENT2 and then delete
7632
     SEGMENT2.  */
7633
1
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7634
1
            - segment_end (segment, segment->p_vaddr));
7635
7636
1
        if (extra_length > 0)
7637
0
    {
7638
0
      segment->p_memsz += extra_length;
7639
0
      segment->p_filesz += extra_length;
7640
0
    }
7641
7642
1
        segment2->p_type = PT_NULL;
7643
1
      }
7644
7
  }
7645
42
    }
7646
7647
  /* The second scan attempts to assign sections to segments.  */
7648
15
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7649
174
       i < num_segments;
7650
159
       i++, segment++)
7651
159
    {
7652
159
      unsigned int section_count;
7653
159
      asection **sections;
7654
159
      asection *output_section;
7655
159
      unsigned int isec;
7656
159
      asection *matching_lma;
7657
159
      asection *suggested_lma;
7658
159
      unsigned int j;
7659
159
      size_t amt;
7660
159
      asection *first_section;
7661
7662
159
      if (segment->p_type == PT_NULL)
7663
25
  continue;
7664
7665
134
      first_section = NULL;
7666
      /* Compute how many sections might be placed into this segment.  */
7667
134
      for (section = ibfd->sections, section_count = 0;
7668
2.91k
     section != NULL;
7669
2.78k
     section = section->next)
7670
2.78k
  {
7671
    /* Find the first section in the input segment, which may be
7672
       removed from the corresponding output segment.   */
7673
2.78k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7674
389
      {
7675
389
        if (first_section == NULL)
7676
72
    first_section = section;
7677
389
        if (section->output_section != NULL)
7678
385
    ++section_count;
7679
389
      }
7680
2.78k
  }
7681
7682
      /* Allocate a segment map big enough to contain
7683
   all of the sections we have selected.  */
7684
134
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7685
134
      amt += section_count * sizeof (asection *);
7686
134
      map = bfd_zalloc (obfd, amt);
7687
134
      if (map == NULL)
7688
0
  return false;
7689
7690
      /* Initialise the fields of the segment map.  Default to
7691
   using the physical address of the segment in the input BFD.  */
7692
134
      map->next = NULL;
7693
134
      map->p_type = segment->p_type;
7694
134
      map->p_flags = segment->p_flags;
7695
134
      map->p_flags_valid = 1;
7696
7697
134
      if (map->p_type == PT_LOAD
7698
27
    && (ibfd->flags & D_PAGED) != 0
7699
4
    && maxpagesize > 1
7700
4
    && segment->p_align > 1)
7701
3
  {
7702
3
    map->p_align = segment->p_align;
7703
3
    if (segment->p_align > maxpagesize)
7704
0
      map->p_align = maxpagesize;
7705
3
    map->p_align_valid = 1;
7706
3
  }
7707
7708
      /* If the first section in the input segment is removed, there is
7709
   no need to preserve segment physical address in the corresponding
7710
   output segment.  */
7711
134
      if (!first_section || first_section->output_section != NULL)
7712
134
  {
7713
134
    map->p_paddr = segment->p_paddr;
7714
134
    map->p_paddr_valid = p_paddr_valid;
7715
134
  }
7716
7717
      /* Determine if this segment contains the ELF file header
7718
   and if it contains the program headers themselves.  */
7719
134
      map->includes_filehdr = (segment->p_offset == 0
7720
43
             && segment->p_filesz >= iehdr->e_ehsize);
7721
134
      map->includes_phdrs = 0;
7722
7723
134
      if (!phdr_included || segment->p_type != PT_LOAD)
7724
128
  {
7725
128
    map->includes_phdrs =
7726
128
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7727
49
       && (segment->p_offset + segment->p_filesz
7728
49
     >= ((bfd_vma) iehdr->e_phoff
7729
49
         + iehdr->e_phnum * iehdr->e_phentsize)));
7730
7731
128
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7732
11
      phdr_included = true;
7733
128
  }
7734
7735
134
      if (section_count == 0)
7736
62
  {
7737
    /* Special segments, such as the PT_PHDR segment, may contain
7738
       no sections, but ordinary, loadable segments should contain
7739
       something.  They are allowed by the ELF spec however, so only
7740
       a warning is produced.
7741
       Don't warn if an empty PT_LOAD contains the program headers.
7742
       There is however the valid use case of embedded systems which
7743
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7744
       flash memory with zeros.  No warning is shown for that case.  */
7745
62
    if (segment->p_type == PT_LOAD
7746
3
        && !map->includes_phdrs
7747
2
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7748
      /* xgettext:c-format */
7749
2
      _bfd_error_handler
7750
2
        (_("%pB: warning: empty loadable segment detected"
7751
2
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7752
2
         ibfd, (uint64_t) segment->p_vaddr);
7753
7754
62
    map->p_vaddr_offset = segment->p_vaddr / opb;
7755
62
    map->count = 0;
7756
62
    *pointer_to_map = map;
7757
62
    pointer_to_map = &map->next;
7758
7759
62
    continue;
7760
62
  }
7761
7762
      /* Now scan the sections in the input BFD again and attempt
7763
   to add their corresponding output sections to the segment map.
7764
   The problem here is how to handle an output section which has
7765
   been moved (ie had its LMA changed).  There are four possibilities:
7766
7767
   1. None of the sections have been moved.
7768
      In this case we can continue to use the segment LMA from the
7769
      input BFD.
7770
7771
   2. All of the sections have been moved by the same amount.
7772
      In this case we can change the segment's LMA to match the LMA
7773
      of the first section.
7774
7775
   3. Some of the sections have been moved, others have not.
7776
      In this case those sections which have not been moved can be
7777
      placed in the current segment which will have to have its size,
7778
      and possibly its LMA changed, and a new segment or segments will
7779
      have to be created to contain the other sections.
7780
7781
   4. The sections have been moved, but not by the same amount.
7782
      In this case we can change the segment's LMA to match the LMA
7783
      of the first section and we will have to create a new segment
7784
      or segments to contain the other sections.
7785
7786
   In order to save time, we allocate an array to hold the section
7787
   pointers that we are interested in.  As these sections get assigned
7788
   to a segment, they are removed from this array.  */
7789
7790
72
      sections = bfd_malloc (section_count * sizeof (*sections));
7791
72
      if (sections == NULL)
7792
0
  return false;
7793
7794
      /* Step One: Scan for segment vs section LMA conflicts.
7795
   Also add the sections to the section array allocated above.
7796
   Also add the sections to the current segment.  In the common
7797
   case, where the sections have not been moved, this means that
7798
   we have completely filled the segment, and there is nothing
7799
   more to do.  */
7800
72
      isec = 0;
7801
72
      matching_lma = NULL;
7802
72
      suggested_lma = NULL;
7803
7804
72
      for (section = first_section, j = 0;
7805
567
     section != NULL;
7806
495
     section = section->next)
7807
567
  {
7808
567
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7809
385
      {
7810
385
        output_section = section->output_section;
7811
7812
385
        sections[j++] = section;
7813
7814
        /* The Solaris native linker always sets p_paddr to 0.
7815
     We try to catch that case here, and set it to the
7816
     correct value.  Note - some backends require that
7817
     p_paddr be left as zero.  */
7818
385
        if (!p_paddr_valid
7819
0
      && segment->p_vaddr != 0
7820
0
      && !bed->want_p_paddr_set_to_zero
7821
0
      && isec == 0
7822
0
      && output_section->lma != 0
7823
0
      && (align_power (segment->p_vaddr
7824
0
           + (map->includes_filehdr
7825
0
              ? iehdr->e_ehsize : 0)
7826
0
           + (map->includes_phdrs
7827
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7828
0
              : 0),
7829
0
           output_section->alignment_power * opb)
7830
0
          == (output_section->vma * opb)))
7831
0
    map->p_paddr = segment->p_vaddr;
7832
7833
        /* Match up the physical address of the segment with the
7834
     LMA address of the output section.  */
7835
385
        if (is_contained_by (output_section, segment, map->p_paddr,
7836
385
           0, opb, false)
7837
6
      || is_note (section, segment))
7838
385
    {
7839
385
      if (matching_lma == NULL
7840
313
          || output_section->lma < matching_lma->lma)
7841
88
        matching_lma = output_section;
7842
7843
      /* We assume that if the section fits within the segment
7844
         then it does not overlap any other section within that
7845
         segment.  */
7846
385
      map->sections[isec++] = output_section;
7847
385
    }
7848
0
        else if (suggested_lma == NULL)
7849
0
    suggested_lma = output_section;
7850
7851
385
        if (j == section_count)
7852
72
    break;
7853
385
      }
7854
567
  }
7855
7856
72
      BFD_ASSERT (j == section_count);
7857
7858
      /* Step Two: Adjust the physical address of the current segment,
7859
   if necessary.  */
7860
72
      if (isec == section_count)
7861
72
  {
7862
    /* All of the sections fitted within the segment as currently
7863
       specified.  This is the default case.  Add the segment to
7864
       the list of built segments and carry on to process the next
7865
       program header in the input BFD.  */
7866
72
    map->count = section_count;
7867
72
    *pointer_to_map = map;
7868
72
    pointer_to_map = &map->next;
7869
7870
72
    if (p_paddr_valid
7871
72
        && !bed->want_p_paddr_set_to_zero)
7872
72
      {
7873
72
        bfd_vma hdr_size = 0;
7874
72
        if (map->includes_filehdr)
7875
11
    hdr_size = iehdr->e_ehsize;
7876
72
        if (map->includes_phdrs)
7877
11
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7878
7879
        /* Account for padding before the first section in the
7880
     segment.  */
7881
72
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7882
72
             - matching_lma->lma);
7883
72
      }
7884
7885
72
    free (sections);
7886
72
    continue;
7887
72
  }
7888
0
      else
7889
0
  {
7890
    /* Change the current segment's physical address to match
7891
       the LMA of the first section that fitted, or if no
7892
       section fitted, the first section.  */
7893
0
    if (matching_lma == NULL)
7894
0
      matching_lma = suggested_lma;
7895
7896
0
    map->p_paddr = matching_lma->lma * opb;
7897
7898
    /* Offset the segment physical address from the lma
7899
       to allow for space taken up by elf headers.  */
7900
0
    if (map->includes_phdrs)
7901
0
      {
7902
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7903
7904
        /* iehdr->e_phnum is just an estimate of the number
7905
     of program headers that we will need.  Make a note
7906
     here of the number we used and the segment we chose
7907
     to hold these headers, so that we can adjust the
7908
     offset when we know the correct value.  */
7909
0
        phdr_adjust_num = iehdr->e_phnum;
7910
0
        phdr_adjust_seg = map;
7911
0
      }
7912
7913
0
    if (map->includes_filehdr)
7914
0
      {
7915
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7916
0
        map->p_paddr -= iehdr->e_ehsize;
7917
        /* We've subtracted off the size of headers from the
7918
     first section lma, but there may have been some
7919
     alignment padding before that section too.  Try to
7920
     account for that by adjusting the segment lma down to
7921
     the same alignment.  */
7922
0
        if (segment->p_align != 0 && segment->p_align < align)
7923
0
    align = segment->p_align;
7924
0
        map->p_paddr &= -(align * opb);
7925
0
      }
7926
0
  }
7927
7928
      /* Step Three: Loop over the sections again, this time assigning
7929
   those that fit to the current segment and removing them from the
7930
   sections array; but making sure not to leave large gaps.  Once all
7931
   possible sections have been assigned to the current segment it is
7932
   added to the list of built segments and if sections still remain
7933
   to be assigned, a new segment is constructed before repeating
7934
   the loop.  */
7935
0
      isec = 0;
7936
0
      do
7937
0
  {
7938
0
    map->count = 0;
7939
0
    suggested_lma = NULL;
7940
7941
    /* Fill the current segment with sections that fit.  */
7942
0
    for (j = 0; j < section_count; j++)
7943
0
      {
7944
0
        section = sections[j];
7945
7946
0
        if (section == NULL)
7947
0
    continue;
7948
7949
0
        output_section = section->output_section;
7950
7951
0
        BFD_ASSERT (output_section != NULL);
7952
7953
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7954
0
           0, opb, false)
7955
0
      || is_note (section, segment))
7956
0
    {
7957
0
      if (map->count == 0)
7958
0
        {
7959
          /* If the first section in a segment does not start at
7960
       the beginning of the segment, then something is
7961
       wrong.  */
7962
0
          if (align_power (map->p_paddr
7963
0
               + (map->includes_filehdr
7964
0
            ? iehdr->e_ehsize : 0)
7965
0
               + (map->includes_phdrs
7966
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7967
0
            : 0),
7968
0
               output_section->alignment_power * opb)
7969
0
        != output_section->lma * opb)
7970
0
      goto sorry;
7971
0
        }
7972
0
      else
7973
0
        {
7974
0
          asection *prev_sec;
7975
7976
0
          prev_sec = map->sections[map->count - 1];
7977
7978
          /* If the gap between the end of the previous section
7979
       and the start of this section is more than
7980
       maxpagesize then we need to start a new segment.  */
7981
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7982
0
              maxpagesize)
7983
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7984
0
        || (prev_sec->lma + prev_sec->size
7985
0
            > output_section->lma))
7986
0
      {
7987
0
        if (suggested_lma == NULL)
7988
0
          suggested_lma = output_section;
7989
7990
0
        continue;
7991
0
      }
7992
0
        }
7993
7994
0
      map->sections[map->count++] = output_section;
7995
0
      ++isec;
7996
0
      sections[j] = NULL;
7997
0
      if (segment->p_type == PT_LOAD)
7998
0
        section->segment_mark = true;
7999
0
    }
8000
0
        else if (suggested_lma == NULL)
8001
0
    suggested_lma = output_section;
8002
0
      }
8003
8004
    /* PR 23932.  A corrupt input file may contain sections that cannot
8005
       be assigned to any segment - because for example they have a
8006
       negative size - or segments that do not contain any sections.
8007
       But there are also valid reasons why a segment can be empty.
8008
       So allow a count of zero.  */
8009
8010
    /* Add the current segment to the list of built segments.  */
8011
0
    *pointer_to_map = map;
8012
0
    pointer_to_map = &map->next;
8013
8014
0
    if (isec < section_count)
8015
0
      {
8016
        /* We still have not allocated all of the sections to
8017
     segments.  Create a new segment here, initialise it
8018
     and carry on looping.  */
8019
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8020
0
        amt += section_count * sizeof (asection *);
8021
0
        map = bfd_zalloc (obfd, amt);
8022
0
        if (map == NULL)
8023
0
    {
8024
0
      free (sections);
8025
0
      return false;
8026
0
    }
8027
8028
        /* Initialise the fields of the segment map.  Set the physical
8029
     physical address to the LMA of the first section that has
8030
     not yet been assigned.  */
8031
0
        map->next = NULL;
8032
0
        map->p_type = segment->p_type;
8033
0
        map->p_flags = segment->p_flags;
8034
0
        map->p_flags_valid = 1;
8035
0
        map->p_paddr = suggested_lma->lma * opb;
8036
0
        map->p_paddr_valid = p_paddr_valid;
8037
0
        map->includes_filehdr = 0;
8038
0
        map->includes_phdrs = 0;
8039
0
      }
8040
8041
0
    continue;
8042
0
  sorry:
8043
0
    bfd_set_error (bfd_error_sorry);
8044
0
    free (sections);
8045
0
    return false;
8046
0
  }
8047
0
      while (isec < section_count);
8048
8049
0
      free (sections);
8050
0
    }
8051
8052
15
  elf_seg_map (obfd) = map_first;
8053
8054
  /* If we had to estimate the number of program headers that were
8055
     going to be needed, then check our estimate now and adjust
8056
     the offset if necessary.  */
8057
15
  if (phdr_adjust_seg != NULL)
8058
0
    {
8059
0
      unsigned int count;
8060
8061
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8062
0
  count++;
8063
8064
0
      if (count > phdr_adjust_num)
8065
0
  phdr_adjust_seg->p_paddr
8066
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8067
8068
0
      for (map = map_first; map != NULL; map = map->next)
8069
0
  if (map->p_type == PT_PHDR)
8070
0
    {
8071
0
      bfd_vma adjust
8072
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8073
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8074
0
      break;
8075
0
    }
8076
0
    }
8077
8078
15
#undef IS_SOLARIS_PT_INTERP
8079
15
#undef IS_SECTION_IN_INPUT_SEGMENT
8080
15
#undef INCLUDE_SECTION_IN_SEGMENT
8081
15
#undef SEGMENT_AFTER_SEGMENT
8082
15
#undef SEGMENT_OVERLAPS
8083
15
  return true;
8084
15
}
8085
8086
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8087
8088
static bool
8089
elf_is_p_align_valid (bfd *abfd)
8090
15
{
8091
15
  unsigned int i;
8092
15
  Elf_Internal_Phdr *segment;
8093
15
  unsigned int num_segments;
8094
15
  elf_backend_data *bed = get_elf_backend_data (abfd);
8095
15
  bfd_size_type maxpagesize = bed->maxpagesize;
8096
15
  bfd_size_type p_align = bed->p_align;
8097
8098
  /* Return true if the default p_align value isn't set or the maximum
8099
     page size is the same as the minimum page size.  */
8100
15
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8101
15
    return true;
8102
8103
  /* When the default p_align value is set, p_align may be set to the
8104
     default p_align value while segments are aligned to the maximum
8105
     page size.  In this case, the input p_align will be ignored and
8106
     the maximum page size will be used to align the output segments.  */
8107
0
  segment = elf_tdata (abfd)->phdr;
8108
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8109
0
  for (i = 0; i < num_segments; i++, segment++)
8110
0
    if (segment->p_type == PT_LOAD
8111
0
  && (segment->p_align != p_align
8112
0
      || vma_page_aligned_bias (segment->p_vaddr,
8113
0
              segment->p_offset,
8114
0
              maxpagesize) != 0))
8115
0
      return true;
8116
8117
0
  return false;
8118
0
}
8119
8120
/* Copy ELF program header information.  */
8121
8122
static bool
8123
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8124
15
{
8125
15
  Elf_Internal_Ehdr *iehdr;
8126
15
  struct elf_segment_map *map;
8127
15
  struct elf_segment_map *map_first;
8128
15
  struct elf_segment_map **pointer_to_map;
8129
15
  Elf_Internal_Phdr *segment;
8130
15
  unsigned int i;
8131
15
  unsigned int num_segments;
8132
15
  bool phdr_included = false;
8133
15
  bool p_paddr_valid;
8134
15
  bool p_palign_valid;
8135
15
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8136
8137
15
  iehdr = elf_elfheader (ibfd);
8138
8139
15
  map_first = NULL;
8140
15
  pointer_to_map = &map_first;
8141
8142
  /* If all the segment p_paddr fields are zero, don't set
8143
     map->p_paddr_valid.  */
8144
15
  p_paddr_valid = false;
8145
15
  num_segments = elf_elfheader (ibfd)->e_phnum;
8146
15
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8147
20
       i < num_segments;
8148
15
       i++, segment++)
8149
19
    if (segment->p_paddr != 0)
8150
14
      {
8151
14
  p_paddr_valid = true;
8152
14
  break;
8153
14
      }
8154
8155
15
  p_palign_valid = elf_is_p_align_valid (ibfd);
8156
8157
15
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8158
125
       i < num_segments;
8159
110
       i++, segment++)
8160
110
    {
8161
110
      asection *section;
8162
110
      unsigned int section_count;
8163
110
      size_t amt;
8164
110
      Elf_Internal_Shdr *this_hdr;
8165
110
      asection *first_section = NULL;
8166
110
      asection *lowest_section;
8167
8168
      /* Compute how many sections are in this segment.  */
8169
110
      for (section = ibfd->sections, section_count = 0;
8170
2.21k
     section != NULL;
8171
2.10k
     section = section->next)
8172
2.10k
  {
8173
2.10k
    this_hdr = &(elf_section_data(section)->this_hdr);
8174
2.10k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8175
472
      {
8176
472
        if (first_section == NULL)
8177
58
    first_section = section;
8178
472
        section_count++;
8179
472
      }
8180
2.10k
  }
8181
8182
      /* Allocate a segment map big enough to contain
8183
   all of the sections we have selected.  */
8184
110
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8185
110
      amt += section_count * sizeof (asection *);
8186
110
      map = bfd_zalloc (obfd, amt);
8187
110
      if (map == NULL)
8188
0
  return false;
8189
8190
      /* Initialize the fields of the output segment map with the
8191
   input segment.  */
8192
110
      map->next = NULL;
8193
110
      map->p_type = segment->p_type;
8194
110
      map->p_flags = segment->p_flags;
8195
110
      map->p_flags_valid = 1;
8196
110
      map->p_paddr = segment->p_paddr;
8197
110
      map->p_paddr_valid = p_paddr_valid;
8198
110
      map->p_align = segment->p_align;
8199
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8200
110
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8201
108
          || p_palign_valid);
8202
110
      map->p_vaddr_offset = 0;
8203
8204
110
      if (map->p_type == PT_GNU_RELRO
8205
104
    || map->p_type == PT_GNU_STACK)
8206
8
  {
8207
    /* The PT_GNU_RELRO segment may contain the first a few
8208
       bytes in the .got.plt section even if the whole .got.plt
8209
       section isn't in the PT_GNU_RELRO segment.  We won't
8210
       change the size of the PT_GNU_RELRO segment.
8211
       Similarly, PT_GNU_STACK size is significant on uclinux
8212
       systems.    */
8213
8
    map->p_size = segment->p_memsz;
8214
8
    map->p_size_valid = 1;
8215
8
  }
8216
8217
      /* Determine if this segment contains the ELF file header
8218
   and if it contains the program headers themselves.  */
8219
110
      map->includes_filehdr = (segment->p_offset == 0
8220
35
             && segment->p_filesz >= iehdr->e_ehsize);
8221
8222
110
      map->includes_phdrs = 0;
8223
110
      if (! phdr_included || segment->p_type != PT_LOAD)
8224
106
  {
8225
106
    map->includes_phdrs =
8226
106
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8227
42
       && (segment->p_offset + segment->p_filesz
8228
42
     >= ((bfd_vma) iehdr->e_phoff
8229
42
         + iehdr->e_phnum * iehdr->e_phentsize)));
8230
8231
106
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8232
4
      phdr_included = true;
8233
106
  }
8234
8235
110
      lowest_section = NULL;
8236
110
      if (section_count != 0)
8237
58
  {
8238
58
    unsigned int isec = 0;
8239
8240
58
    for (section = first_section;
8241
512
         section != NULL;
8242
454
         section = section->next)
8243
512
      {
8244
512
        this_hdr = &(elf_section_data(section)->this_hdr);
8245
512
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8246
472
    {
8247
472
      map->sections[isec++] = section->output_section;
8248
472
      if ((section->flags & SEC_ALLOC) != 0)
8249
109
        {
8250
109
          bfd_vma seg_off;
8251
8252
109
          if (lowest_section == NULL
8253
60
        || section->lma < lowest_section->lma)
8254
52
      lowest_section = section;
8255
8256
          /* Section lmas are set up from PT_LOAD header
8257
       p_paddr in _bfd_elf_make_section_from_shdr.
8258
       If this header has a p_paddr that disagrees
8259
       with the section lma, flag the p_paddr as
8260
       invalid.  */
8261
109
          if ((section->flags & SEC_LOAD) != 0)
8262
90
      seg_off = this_hdr->sh_offset - segment->p_offset;
8263
19
          else
8264
19
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8265
109
          if (section->lma * opb - segment->p_paddr != seg_off)
8266
22
      map->p_paddr_valid = false;
8267
109
        }
8268
472
      if (isec == section_count)
8269
58
        break;
8270
472
    }
8271
512
      }
8272
58
  }
8273
8274
110
      if (section_count == 0)
8275
52
  map->p_vaddr_offset = segment->p_vaddr / opb;
8276
58
      else if (map->p_paddr_valid)
8277
38
  {
8278
    /* Account for padding before the first section in the segment.  */
8279
38
    bfd_vma hdr_size = 0;
8280
38
    if (map->includes_filehdr)
8281
8
      hdr_size = iehdr->e_ehsize;
8282
38
    if (map->includes_phdrs)
8283
5
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8284
8285
38
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8286
38
         - (lowest_section ? lowest_section->lma : 0));
8287
38
  }
8288
8289
110
      map->count = section_count;
8290
110
      *pointer_to_map = map;
8291
110
      pointer_to_map = &map->next;
8292
110
    }
8293
8294
15
  elf_seg_map (obfd) = map_first;
8295
15
  return true;
8296
15
}
8297
8298
/* Copy private BFD data.  This copies or rewrites ELF program header
8299
   information.  */
8300
8301
static bool
8302
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8303
30
{
8304
30
  bfd_vma maxpagesize;
8305
8306
30
  if (elf_tdata (ibfd)->phdr == NULL)
8307
0
    return true;
8308
8309
30
  if (ibfd->xvec == obfd->xvec)
8310
30
    {
8311
      /* Check to see if any sections in the input BFD
8312
   covered by ELF program header have changed.  */
8313
30
      Elf_Internal_Phdr *segment;
8314
30
      asection * section;
8315
30
      asection * osec;
8316
30
      asection * prev;
8317
30
      unsigned int i, num_segments;
8318
30
      Elf_Internal_Shdr *this_hdr;
8319
30
      elf_backend_data *bed;
8320
8321
30
      bed = get_elf_backend_data (ibfd);
8322
8323
      /* Regenerate the segment map if p_paddr is set to 0.  */
8324
30
      if (bed->want_p_paddr_set_to_zero)
8325
0
  goto rewrite;
8326
8327
      /* Initialize the segment mark field.  */
8328
542
      for (section = obfd->sections; section != NULL;
8329
512
     section = section->next)
8330
512
  section->segment_mark = false;
8331
8332
30
      num_segments = elf_elfheader (ibfd)->e_phnum;
8333
30
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8334
166
     i < num_segments;
8335
136
     i++, segment++)
8336
151
  {
8337
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8338
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8339
       which severly confuses things, so always regenerate the segment
8340
       map in this case.  */
8341
151
    if (segment->p_paddr == 0
8342
36
        && segment->p_memsz == 0
8343
15
        && (segment->p_type == PT_INTERP
8344
14
      || segment->p_type == PT_DYNAMIC))
8345
1
      goto rewrite;
8346
8347
150
    for (section = ibfd->sections, prev = NULL;
8348
2.77k
         section != NULL; section = section->next)
8349
2.64k
      {
8350
        /* We mark the output section so that we know it comes
8351
     from the input BFD.  */
8352
2.64k
        osec = section->output_section;
8353
2.64k
        if (osec)
8354
2.63k
    osec->segment_mark = true;
8355
8356
        /* Check if this section is covered by the segment.  */
8357
2.64k
        this_hdr = &(elf_section_data(section)->this_hdr);
8358
2.64k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8359
547
    {
8360
      /* FIXME: Check if its output section is changed or
8361
         removed.  What else do we need to check?  */
8362
547
      if (osec == NULL
8363
546
          || section->flags != osec->flags
8364
546
          || section->lma != osec->lma
8365
546
          || section->vma != osec->vma
8366
546
          || section->size != osec->size
8367
546
          || section->rawsize != osec->rawsize
8368
546
          || section->alignment_power != osec->alignment_power)
8369
1
        goto rewrite;
8370
8371
      /* PR 31450: If this is an allocated section then make sure
8372
         that this section's vma to lma relationship is the same
8373
         as previous (allocated) section's.  */
8374
546
      if (prev != NULL
8375
206
          && section->flags & SEC_ALLOC
8376
100
          && section->lma - section->vma != prev->lma - prev->vma)
8377
13
        goto rewrite;
8378
8379
533
      if (section->flags & SEC_ALLOC)
8380
152
        prev = section;
8381
533
    }
8382
2.64k
      }
8383
150
  }
8384
8385
      /* Check to see if any output section do not come from the
8386
   input BFD.  */
8387
256
      for (section = obfd->sections; section != NULL;
8388
241
     section = section->next)
8389
241
  {
8390
241
    if (!section->segment_mark)
8391
0
      goto rewrite;
8392
241
    else
8393
241
      section->segment_mark = false;
8394
241
  }
8395
8396
15
      return copy_elf_program_header (ibfd, obfd);
8397
15
    }
8398
8399
15
 rewrite:
8400
15
  maxpagesize = 0;
8401
15
  if (ibfd->xvec == obfd->xvec)
8402
15
    {
8403
      /* When rewriting program header, set the output maxpagesize to
8404
   the maximum alignment of input PT_LOAD segments.  */
8405
15
      Elf_Internal_Phdr *segment;
8406
15
      unsigned int i;
8407
15
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8408
8409
15
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8410
174
     i < num_segments;
8411
159
     i++, segment++)
8412
159
  if (segment->p_type == PT_LOAD
8413
34
      && maxpagesize < segment->p_align)
8414
17
    {
8415
      /* PR 17512: file: f17299af.  */
8416
17
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8417
        /* xgettext:c-format */
8418
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8419
0
            PRIx64 " is too large"),
8420
0
          ibfd, (uint64_t) segment->p_align);
8421
17
      else
8422
17
        maxpagesize = segment->p_align;
8423
17
    }
8424
15
    }
8425
15
  if (maxpagesize == 0)
8426
1
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8427
8428
15
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8429
30
}
8430
8431
/* Copy private section information from input to output.  This function
8432
   is called both by objcopy where LINK_INFO is NULL, and ld where
8433
   LINK_INFO is non-NULL in the usual case but NULL for a special case
8434
   when dealing with LTO IR or cloning output sections.  */
8435
8436
bool
8437
_bfd_elf_copy_private_section_data (bfd *ibfd,
8438
            asection *isec,
8439
            bfd *obfd ATTRIBUTE_UNUSED,
8440
            asection *osec,
8441
            struct bfd_link_info *link_info)
8442
1.85k
{
8443
1.85k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8444
0
    return true;
8445
8446
1.85k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8447
1.85k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8448
1.85k
  if (link_info == NULL)
8449
1.85k
    {
8450
1.85k
      ohdr->sh_entsize = ihdr->sh_entsize;
8451
8452
1.85k
      if (ihdr->sh_type == SHT_SYMTAB
8453
1.85k
    || ihdr->sh_type == SHT_DYNSYM
8454
1.83k
    || ihdr->sh_type == SHT_GNU_verneed
8455
1.80k
    || ihdr->sh_type == SHT_GNU_verdef)
8456
78
  ohdr->sh_info = ihdr->sh_info;
8457
1.85k
    }
8458
8459
  /* If this is a known ABI section, ELF section type and flags may
8460
     have been set up when OSEC was created.  For normal sections we
8461
     allow the user to override the type and flags other than
8462
     SHF_MASKOS and SHF_MASKPROC.  */
8463
1.85k
  if (elf_section_type (osec) == SHT_PROGBITS
8464
1.50k
      || elf_section_type (osec) == SHT_NOTE
8465
1.46k
      || elf_section_type (osec) == SHT_NOBITS)
8466
416
    elf_section_type (osec) = SHT_NULL;
8467
8468
  /* For objcopy and relocatable link, copy the ELF section type from
8469
     the input file if the BFD section flags are the same.  (If they
8470
     are different the user may be doing something like
8471
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8472
     link allow some flags that the linker clears to differ.  */
8473
1.85k
  bool final_link = (link_info != NULL
8474
0
         && !bfd_link_relocatable (link_info));
8475
1.85k
  if (elf_section_type (osec) == SHT_NULL
8476
1.66k
      && (osec->flags == isec->flags
8477
0
    || (final_link
8478
0
        && ((osec->flags ^ isec->flags)
8479
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8480
1.66k
    elf_section_type (osec) = elf_section_type (isec);
8481
8482
1.85k
  elf_section_flags (osec) = elf_section_flags (isec);
8483
  /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
8484
     for relocatable link).  Same heuristic as there: If the BFD section flags
8485
     are different, assume --set-section-flags is in use for the section.
8486
8487
     FIXME: Is this correct for all OS/PROC specific flags?  */
8488
1.85k
  if (link_info != NULL || osec->flags != isec->flags)
8489
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8490
1.85k
  else
8491
1.85k
    {
8492
      /* Clear only flags which are set below or elsewhere.  */
8493
1.85k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8494
1.85k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8495
1.85k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8496
1.85k
            | SHF_COMPRESSED);
8497
1.85k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8498
163
  _bfd_error_handler
8499
163
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8500
163
     ibfd, isec,
8501
163
     (uint64_t) (elf_section_flags (osec)
8502
163
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8503
1.85k
    }
8504
8505
  /* Copy sh_info from input for mbind section.  */
8506
1.85k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8507
283
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8508
41
    elf_section_data (osec)->this_hdr.sh_info
8509
41
      = elf_section_data (isec)->this_hdr.sh_info;
8510
8511
  /* Set things up for objcopy and relocatable link.  The output
8512
     SHT_GROUP section will have its elf_next_in_group pointing back
8513
     to the input group members.  Ignore linker created group section.
8514
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8515
1.85k
  if ((link_info == NULL
8516
0
       || !link_info->resolve_section_groups)
8517
1.85k
      && (elf_sec_group (isec) == NULL
8518
31
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8519
1.85k
    {
8520
1.85k
      if (elf_section_flags (isec) & SHF_GROUP)
8521
102
  elf_section_flags (osec) |= SHF_GROUP;
8522
1.85k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8523
1.85k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8524
1.85k
    }
8525
8526
  /* If not decompress, preserve SHF_COMPRESSED.  */
8527
1.85k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8528
1.85k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8529
1.85k
         & SHF_COMPRESSED);
8530
8531
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8532
     don't use the output section of the linked-to section since it
8533
     may be NULL at this point.  */
8534
1.85k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8535
45
    {
8536
45
      ohdr->sh_flags |= SHF_LINK_ORDER;
8537
45
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8538
45
    }
8539
8540
1.85k
  osec->use_rela_p = isec->use_rela_p;
8541
8542
1.85k
  return true;
8543
1.85k
}
8544
8545
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8546
   necessary if we are removing either the SHT_GROUP section or any of
8547
   the group member sections.  DISCARDED is the value that a section's
8548
   output_section has if the section will be discarded, NULL when this
8549
   function is called from objcopy, bfd_abs_section_ptr when called
8550
   from the linker.  */
8551
8552
bool
8553
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8554
232
{
8555
232
  asection *isec;
8556
8557
2.04k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8558
1.80k
    if (elf_section_type (isec) == SHT_GROUP)
8559
14
      {
8560
14
  asection *first = elf_next_in_group (isec);
8561
14
  asection *s = first;
8562
14
  bfd_size_type removed = 0;
8563
8564
33
  while (s != NULL)
8565
31
    {
8566
      /* If this member section is being output but the
8567
         SHT_GROUP section is not, then clear the group info
8568
         set up by _bfd_elf_copy_private_section_data.  */
8569
31
      if (s->output_section != discarded
8570
31
    && isec->output_section == discarded)
8571
0
        {
8572
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8573
0
    elf_group_name (s->output_section) = NULL;
8574
0
        }
8575
31
      else
8576
31
        {
8577
31
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8578
31
    if (s->output_section == discarded
8579
0
        && isec->output_section != discarded)
8580
0
      {
8581
        /* Conversely, if the member section is not being
8582
           output but the SHT_GROUP section is, then adjust
8583
           its size.  */
8584
0
        removed += 4;
8585
0
        if (elf_sec->rel.hdr != NULL
8586
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8587
0
          removed += 4;
8588
0
        if (elf_sec->rela.hdr != NULL
8589
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8590
0
          removed += 4;
8591
0
      }
8592
31
    else
8593
31
      {
8594
        /* Also adjust for zero-sized relocation member
8595
           section.  */
8596
31
        if (elf_sec->rel.hdr != NULL
8597
0
      && elf_sec->rel.hdr->sh_size == 0)
8598
0
          removed += 4;
8599
31
        if (elf_sec->rela.hdr != NULL
8600
19
      && elf_sec->rela.hdr->sh_size == 0)
8601
0
          removed += 4;
8602
31
      }
8603
31
        }
8604
31
      s = elf_next_in_group (s);
8605
31
      if (s == first)
8606
12
        break;
8607
31
    }
8608
14
  if (removed != 0)
8609
0
    {
8610
0
      if (discarded != NULL)
8611
0
        {
8612
    /* If we've been called for ld -r, then we need to
8613
       adjust the input section size.  */
8614
0
    if (isec->rawsize == 0)
8615
0
      isec->rawsize = isec->size;
8616
0
    isec->size = isec->rawsize - removed;
8617
0
    if (isec->size <= 4)
8618
0
      {
8619
0
        isec->size = 0;
8620
0
        isec->flags |= SEC_EXCLUDE;
8621
0
      }
8622
0
        }
8623
0
      else if (isec->output_section != NULL)
8624
0
        {
8625
    /* Adjust the output section size when called from
8626
       objcopy. */
8627
0
    isec->output_section->size -= removed;
8628
0
    if (isec->output_section->size <= 4)
8629
0
      {
8630
0
        isec->output_section->size = 0;
8631
0
        isec->output_section->flags |= SEC_EXCLUDE;
8632
0
      }
8633
0
        }
8634
0
    }
8635
14
      }
8636
8637
232
  return true;
8638
232
}
8639
8640
/* Copy private header information.  */
8641
8642
bool
8643
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8644
232
{
8645
232
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8646
0
    return true;
8647
8648
  /* Copy over private BFD data if it has not already been copied.
8649
     This must be done here, rather than in the copy_private_bfd_data
8650
     entry point, because the latter is called after the section
8651
     contents have been set, which means that the program headers have
8652
     already been worked out.  */
8653
232
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8654
30
    {
8655
30
      if (! copy_private_bfd_data (ibfd, obfd))
8656
0
  return false;
8657
30
    }
8658
8659
232
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8660
232
}
8661
8662
/* Copy private symbol information.  If this symbol is in a section
8663
   which we did not map into a BFD section, try to map the section
8664
   index correctly.  We use special macro definitions for the mapped
8665
   section indices; these definitions are interpreted by the
8666
   swap_out_syms function.  */
8667
8668
6
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8669
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8670
26
#define MAP_STRTAB    (SHN_HIOS + 3)
8671
18
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8672
1
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8673
8674
bool
8675
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8676
           asymbol **isymarg,
8677
           bfd *obfd ATTRIBUTE_UNUSED,
8678
           asymbol **osymarg)
8679
12.5k
{
8680
12.5k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8681
0
    return true;
8682
8683
12.5k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8684
12.5k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8685
12.5k
  if (isym != NULL
8686
12.5k
      && isym->internal_elf_sym.st_shndx != 0
8687
1.48k
      && osym != NULL
8688
1.48k
      && bfd_is_abs_section (isym->symbol.section))
8689
1.05k
    {
8690
1.05k
      unsigned int shndx;
8691
8692
1.05k
      shndx = isym->internal_elf_sym.st_shndx;
8693
1.05k
      if (shndx == elf_onesymtab (ibfd))
8694
3
  shndx = MAP_ONESYMTAB;
8695
1.04k
      else if (shndx == elf_dynsymtab (ibfd))
8696
0
  shndx = MAP_DYNSYMTAB;
8697
1.04k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8698
22
  shndx = MAP_STRTAB;
8699
1.02k
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8700
18
  shndx = MAP_SHSTRTAB;
8701
1.00k
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8702
1
  shndx = MAP_SYM_SHNDX;
8703
1.05k
      osym->internal_elf_sym.st_shndx = shndx;
8704
1.05k
    }
8705
8706
12.5k
  return true;
8707
12.5k
}
8708
8709
/* Swap out the symbols.  */
8710
8711
static bool
8712
swap_out_syms (bfd *abfd,
8713
         struct elf_strtab_hash **sttp,
8714
         int relocatable_p,
8715
         struct bfd_link_info *info)
8716
91
{
8717
91
  elf_backend_data *bed;
8718
91
  unsigned int symcount;
8719
91
  asymbol **syms;
8720
91
  struct elf_strtab_hash *stt;
8721
91
  Elf_Internal_Shdr *symtab_hdr;
8722
91
  Elf_Internal_Shdr *symtab_shndx_hdr;
8723
91
  Elf_Internal_Shdr *symstrtab_hdr;
8724
91
  struct elf_sym_strtab *symstrtab;
8725
91
  bfd_byte *outbound_syms;
8726
91
  bfd_byte *outbound_shndx;
8727
91
  unsigned long outbound_syms_index;
8728
91
  unsigned int idx;
8729
91
  unsigned int num_locals;
8730
91
  size_t amt;
8731
91
  bool name_local_sections;
8732
8733
91
  if (!elf_map_symbols (abfd, &num_locals))
8734
0
    return false;
8735
8736
  /* Dump out the symtabs.  */
8737
91
  stt = _bfd_elf_strtab_init ();
8738
91
  if (stt == NULL)
8739
0
    return false;
8740
8741
91
  bed = get_elf_backend_data (abfd);
8742
91
  symcount = bfd_get_symcount (abfd);
8743
91
  symtab_hdr = &elf_symtab_hdr (abfd);
8744
91
  symtab_hdr->sh_type = SHT_SYMTAB;
8745
91
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8746
91
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8747
91
  symtab_hdr->sh_info = num_locals + 1;
8748
91
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8749
8750
91
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8751
91
  symstrtab_hdr->sh_type = SHT_STRTAB;
8752
8753
  /* Allocate buffer to swap out the .strtab section.  */
8754
91
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8755
91
      || (symstrtab = bfd_malloc (amt)) == NULL)
8756
0
    {
8757
0
      bfd_set_error (bfd_error_no_memory);
8758
0
      _bfd_elf_strtab_free (stt);
8759
0
      return false;
8760
0
    }
8761
8762
91
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8763
91
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8764
0
    {
8765
0
    error_no_mem:
8766
0
      bfd_set_error (bfd_error_no_memory);
8767
0
    error_return:
8768
0
      free (symstrtab);
8769
0
      _bfd_elf_strtab_free (stt);
8770
0
      return false;
8771
0
    }
8772
91
  symtab_hdr->contents = outbound_syms;
8773
91
  outbound_syms_index = 0;
8774
8775
91
  outbound_shndx = NULL;
8776
8777
91
  if (elf_symtab_shndx_list (abfd))
8778
0
    {
8779
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8780
0
      if (symtab_shndx_hdr->sh_name != 0)
8781
0
  {
8782
0
    if (_bfd_mul_overflow (symcount + 1,
8783
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8784
0
      goto error_no_mem;
8785
0
    outbound_shndx = bfd_zalloc (abfd, amt);
8786
0
    if (outbound_shndx == NULL)
8787
0
      goto error_return;
8788
8789
0
    symtab_shndx_hdr->contents = outbound_shndx;
8790
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8791
0
    symtab_shndx_hdr->sh_size = amt;
8792
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8793
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8794
0
  }
8795
      /* FIXME: What about any other headers in the list ?  */
8796
0
    }
8797
8798
  /* Now generate the data (for "contents").  */
8799
91
  {
8800
    /* Fill in zeroth symbol and swap it out.  */
8801
91
    Elf_Internal_Sym sym;
8802
91
    sym.st_name = 0;
8803
91
    sym.st_value = 0;
8804
91
    sym.st_size = 0;
8805
91
    sym.st_info = 0;
8806
91
    sym.st_other = 0;
8807
91
    sym.st_shndx = SHN_UNDEF;
8808
91
    sym.st_target_internal = 0;
8809
91
    symstrtab[outbound_syms_index].sym = sym;
8810
91
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8811
91
    outbound_syms_index++;
8812
91
  }
8813
8814
91
  name_local_sections
8815
91
    = (bed->elf_backend_name_local_section_symbols
8816
14
       && bed->elf_backend_name_local_section_symbols (abfd));
8817
8818
91
  syms = bfd_get_outsymbols (abfd);
8819
1.60k
  for (idx = 0; idx < symcount; idx++)
8820
1.51k
    {
8821
1.51k
      Elf_Internal_Sym sym;
8822
8823
1.51k
      flagword flags = syms[idx]->flags;
8824
1.51k
      if (!name_local_sections
8825
1.34k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8826
129
  {
8827
    /* Local section symbols have no name.  */
8828
129
    sym.st_name = 0;
8829
129
  }
8830
1.38k
      else
8831
1.38k
  {
8832
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8833
       to get the final offset for st_name.  */
8834
1.38k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8835
1.38k
    if (stridx == (size_t) -1)
8836
0
      goto error_return;
8837
1.38k
    sym.st_name = stridx;
8838
1.38k
  }
8839
8840
1.51k
      bfd_vma value = syms[idx]->value;
8841
1.51k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8842
1.51k
      asection *sec = syms[idx]->section;
8843
8844
1.51k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8845
10
  {
8846
    /* ELF common symbols put the alignment into the `value' field,
8847
       and the size into the `size' field.  This is backwards from
8848
       how BFD handles it, so reverse it here.  */
8849
10
    sym.st_size = value;
8850
10
    if (type_ptr == NULL
8851
10
        || type_ptr->internal_elf_sym.st_value == 0)
8852
8
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8853
2
    else
8854
2
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8855
10
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8856
10
  }
8857
1.50k
      else
8858
1.50k
  {
8859
1.50k
    unsigned int shndx;
8860
8861
1.50k
    if (sec->output_section)
8862
1.46k
      {
8863
1.46k
        value += sec->output_offset;
8864
1.46k
        sec = sec->output_section;
8865
1.46k
      }
8866
8867
    /* Don't add in the section vma for relocatable output.  */
8868
1.50k
    if (! relocatable_p)
8869
612
      value += sec->vma;
8870
1.50k
    sym.st_value = value;
8871
1.50k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8872
8873
1.50k
    if (bfd_is_abs_section (sec)
8874
683
        && type_ptr != NULL
8875
683
        && type_ptr->internal_elf_sym.st_shndx != 0)
8876
683
      {
8877
        /* This symbol is in a real ELF section which we did
8878
     not create as a BFD section.  Undo the mapping done
8879
     by copy_private_symbol_data.  */
8880
683
        shndx = type_ptr->internal_elf_sym.st_shndx;
8881
683
        switch (shndx)
8882
683
    {
8883
3
    case MAP_ONESYMTAB:
8884
3
      shndx = elf_onesymtab (abfd);
8885
3
      break;
8886
0
    case MAP_DYNSYMTAB:
8887
0
      shndx = elf_dynsymtab (abfd);
8888
0
      break;
8889
4
    case MAP_STRTAB:
8890
4
      shndx = elf_strtab_sec (abfd);
8891
4
      break;
8892
0
    case MAP_SHSTRTAB:
8893
0
      shndx = elf_shstrtab_sec (abfd);
8894
0
      break;
8895
0
    case MAP_SYM_SHNDX:
8896
0
      if (elf_symtab_shndx_list (abfd))
8897
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8898
0
      break;
8899
0
    case SHN_COMMON:
8900
10
    case SHN_ABS:
8901
10
      shndx = SHN_ABS;
8902
10
      break;
8903
666
    default:
8904
666
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8905
9
        {
8906
9
          if (bed->symbol_section_index)
8907
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8908
          /* Otherwise just leave the index alone.  */
8909
9
        }
8910
657
      else
8911
657
        {
8912
657
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8913
0
      _bfd_error_handler (_("%pB: \
8914
0
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8915
0
            abfd, shndx);
8916
657
          shndx = SHN_ABS;
8917
657
        }
8918
666
      break;
8919
683
    }
8920
683
      }
8921
818
    else
8922
818
      {
8923
818
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8924
8925
818
        if (shndx == SHN_BAD)
8926
0
    {
8927
0
      asection *sec2;
8928
8929
      /* Writing this would be a hell of a lot easier if
8930
         we had some decent documentation on bfd, and
8931
         knew what to expect of the library, and what to
8932
         demand of applications.  For example, it
8933
         appears that `objcopy' might not set the
8934
         section of a symbol to be a section that is
8935
         actually in the output file.  */
8936
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8937
0
      if (sec2 != NULL)
8938
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8939
0
      if (shndx == SHN_BAD)
8940
0
        {
8941
          /* xgettext:c-format */
8942
0
          _bfd_error_handler
8943
0
      (_("unable to find equivalent output section"
8944
0
         " for symbol '%s' from section '%s'"),
8945
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8946
0
       sec->name);
8947
0
          bfd_set_error (bfd_error_invalid_operation);
8948
0
          goto error_return;
8949
0
        }
8950
0
    }
8951
818
      }
8952
8953
1.50k
    sym.st_shndx = shndx;
8954
1.50k
  }
8955
8956
1.51k
      int type;
8957
1.51k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8958
85
  type = STT_TLS;
8959
1.42k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8960
114
  type = STT_GNU_IFUNC;
8961
1.31k
      else if ((flags & BSF_FUNCTION) != 0)
8962
131
  type = STT_FUNC;
8963
1.18k
      else if ((flags & BSF_OBJECT) != 0)
8964
204
  type = STT_OBJECT;
8965
977
      else if ((flags & BSF_RELC) != 0)
8966
28
  type = STT_RELC;
8967
949
      else if ((flags & BSF_SRELC) != 0)
8968
51
  type = STT_SRELC;
8969
898
      else
8970
898
  type = STT_NOTYPE;
8971
8972
1.51k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8973
3
  type = STT_TLS;
8974
8975
      /* Processor-specific types.  */
8976
1.51k
      if (type_ptr != NULL
8977
1.51k
    && bed->elf_backend_get_symbol_type)
8978
50
  type = ((*bed->elf_backend_get_symbol_type)
8979
50
    (&type_ptr->internal_elf_sym, type));
8980
8981
1.51k
      if (flags & BSF_SECTION_SYM)
8982
152
  {
8983
152
    if (flags & BSF_GLOBAL)
8984
14
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8985
138
    else
8986
138
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8987
152
  }
8988
1.35k
      else if (bfd_is_com_section (syms[idx]->section))
8989
10
  {
8990
10
    if (type != STT_TLS)
8991
8
      {
8992
8
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8993
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8994
0
      ? STT_COMMON : STT_OBJECT);
8995
8
        else
8996
8
    type = ((flags & BSF_ELF_COMMON) != 0
8997
8
      ? STT_COMMON : STT_OBJECT);
8998
8
      }
8999
10
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9000
10
  }
9001
1.34k
      else if (bfd_is_und_section (syms[idx]->section))
9002
520
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9003
1.34k
            ? STB_WEAK
9004
1.34k
            : STB_GLOBAL),
9005
1.34k
           type);
9006
829
      else if (flags & BSF_FILE)
9007
25
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9008
804
      else
9009
804
  {
9010
804
    int bind = STB_LOCAL;
9011
9012
804
    if (flags & BSF_LOCAL)
9013
454
      bind = STB_LOCAL;
9014
350
    else if (flags & BSF_GNU_UNIQUE)
9015
13
      bind = STB_GNU_UNIQUE;
9016
337
    else if (flags & BSF_WEAK)
9017
47
      bind = STB_WEAK;
9018
290
    else if (flags & BSF_GLOBAL)
9019
207
      bind = STB_GLOBAL;
9020
9021
804
    sym.st_info = ELF_ST_INFO (bind, type);
9022
804
  }
9023
9024
1.51k
      if (type_ptr != NULL)
9025
1.51k
  {
9026
1.51k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9027
1.51k
    sym.st_target_internal
9028
1.51k
      = type_ptr->internal_elf_sym.st_target_internal;
9029
1.51k
  }
9030
0
      else
9031
0
  {
9032
0
    sym.st_other = 0;
9033
0
    sym.st_target_internal = 0;
9034
0
  }
9035
9036
1.51k
      symstrtab[outbound_syms_index].sym = sym;
9037
1.51k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9038
1.51k
      outbound_syms_index++;
9039
1.51k
    }
9040
9041
  /* Finalize the .strtab section.  */
9042
91
  _bfd_elf_strtab_finalize (stt);
9043
9044
  /* Swap out the .strtab section.  */
9045
1.69k
  for (idx = 0; idx < outbound_syms_index; idx++)
9046
1.60k
    {
9047
1.60k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9048
1.60k
      if (elfsym->sym.st_name != 0)
9049
197
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9050
197
                  elfsym->sym.st_name);
9051
1.60k
      if (info && info->callbacks->ctf_new_symbol)
9052
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9053
0
           &elfsym->sym);
9054
9055
      /* Inform the linker of the addition of this symbol.  */
9056
9057
1.60k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9058
1.60k
             (outbound_syms
9059
1.60k
        + (elfsym->dest_index
9060
1.60k
           * bed->s->sizeof_sym)),
9061
1.60k
             NPTR_ADD (outbound_shndx,
9062
1.60k
           (elfsym->dest_index
9063
1.60k
            * sizeof (Elf_External_Sym_Shndx))));
9064
1.60k
    }
9065
91
  free (symstrtab);
9066
9067
91
  *sttp = stt;
9068
91
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9069
91
  symstrtab_hdr->sh_type = SHT_STRTAB;
9070
91
  symstrtab_hdr->sh_addralign = 1;
9071
9072
91
  return true;
9073
91
}
9074
9075
/* Return the number of bytes required to hold the symtab vector.
9076
9077
   Note that we base it on the count plus 1, since we will null terminate
9078
   the vector allocated based on this size.  However, the ELF symbol table
9079
   always has a dummy entry as symbol #0, so it ends up even.  */
9080
9081
long
9082
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9083
9.77k
{
9084
9.77k
  bfd_size_type symcount;
9085
9.77k
  long symtab_size;
9086
9.77k
  Elf_Internal_Shdr *hdr = &elf_symtab_hdr (abfd);
9087
9088
9.77k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9089
9.77k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9090
0
    {
9091
0
      bfd_set_error (bfd_error_file_too_big);
9092
0
      return -1;
9093
0
    }
9094
9.77k
  symtab_size = symcount * (sizeof (asymbol *));
9095
9.77k
  if (symcount == 0)
9096
559
    symtab_size = sizeof (asymbol *);
9097
9.21k
  else if (!bfd_write_p (abfd))
9098
9.21k
    {
9099
9.21k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9100
9101
9.21k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9102
246
  {
9103
246
    bfd_set_error (bfd_error_file_truncated);
9104
246
    return -1;
9105
246
  }
9106
9.21k
    }
9107
9108
9.52k
  return symtab_size;
9109
9.77k
}
9110
9111
long
9112
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9113
17.1k
{
9114
17.1k
  bfd_size_type symcount;
9115
17.1k
  long symtab_size;
9116
17.1k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9117
9118
17.1k
  if (elf_dynsymtab (abfd) == 0)
9119
16.6k
    {
9120
      /* Check if there is dynamic symbol table.  */
9121
16.6k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9122
16.6k
      if (symcount)
9123
10
  goto compute_symtab_size;
9124
9125
16.6k
      bfd_set_error (bfd_error_invalid_operation);
9126
16.6k
      return -1;
9127
16.6k
    }
9128
9129
458
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9130
458
  if (symcount > LONG_MAX / sizeof (asymbol *))
9131
0
    {
9132
0
      bfd_set_error (bfd_error_file_too_big);
9133
0
      return -1;
9134
0
    }
9135
9136
468
 compute_symtab_size:
9137
468
  symtab_size = symcount * (sizeof (asymbol *));
9138
468
  if (symcount == 0)
9139
7
    symtab_size = sizeof (asymbol *);
9140
461
  else if (!bfd_write_p (abfd))
9141
461
    {
9142
461
      ufile_ptr filesize = bfd_get_file_size (abfd);
9143
9144
461
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9145
19
  {
9146
19
    bfd_set_error (bfd_error_file_truncated);
9147
19
    return -1;
9148
19
  }
9149
461
    }
9150
9151
449
  return symtab_size;
9152
468
}
9153
9154
long
9155
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9156
23.4k
{
9157
23.4k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9158
21.0k
    {
9159
      /* Sanity check reloc section size.  */
9160
21.0k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9161
9162
21.0k
      if (filesize != 0)
9163
21.0k
  {
9164
21.0k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9165
21.0k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9166
21.0k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9167
9168
21.0k
    if (rel_size + rela_size > filesize
9169
20.1k
        || rel_size + rela_size < rel_size)
9170
849
      {
9171
849
        bfd_set_error (bfd_error_file_truncated);
9172
849
        return -1;
9173
849
      }
9174
21.0k
  }
9175
21.0k
    }
9176
9177
#if SIZEOF_LONG == SIZEOF_INT
9178
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9179
    {
9180
      bfd_set_error (bfd_error_file_too_big);
9181
      return -1;
9182
    }
9183
#endif
9184
22.6k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9185
23.4k
}
9186
9187
/* Canonicalize the relocs.  */
9188
9189
long
9190
_bfd_elf_canonicalize_reloc (bfd *abfd,
9191
           sec_ptr section,
9192
           arelent **relptr,
9193
           asymbol **symbols)
9194
22.5k
{
9195
22.5k
  arelent *tblptr;
9196
22.5k
  unsigned int i;
9197
22.5k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9198
9199
22.5k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9200
5.51k
    return -1;
9201
9202
16.9k
  tblptr = section->relocation;
9203
193k
  for (i = 0; i < section->reloc_count; i++)
9204
176k
    *relptr++ = tblptr++;
9205
9206
16.9k
  *relptr = NULL;
9207
9208
16.9k
  return section->reloc_count;
9209
22.5k
}
9210
9211
long
9212
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9213
9.52k
{
9214
9.52k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9215
9.52k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9216
9217
9.52k
  if (symcount >= 0)
9218
8.50k
    abfd->symcount = symcount;
9219
9.52k
  return symcount;
9220
9.52k
}
9221
9222
long
9223
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9224
              asymbol **allocation)
9225
298
{
9226
298
  elf_backend_data *bed = get_elf_backend_data (abfd);
9227
298
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9228
9229
298
  if (symcount >= 0)
9230
151
    abfd->dynsymcount = symcount;
9231
298
  return symcount;
9232
298
}
9233
9234
/* Return the size required for the dynamic reloc entries.  Any loadable
9235
   section that was actually installed in the BFD, and has type SHT_REL
9236
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9237
   dynamic reloc section.  */
9238
9239
long
9240
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9241
16.6k
{
9242
16.6k
  bfd_size_type count, ext_rel_size;
9243
16.6k
  asection *s;
9244
9245
16.6k
  if (elf_dynsymtab (abfd) == 0)
9246
16.4k
    {
9247
16.4k
      bfd_set_error (bfd_error_invalid_operation);
9248
16.4k
      return -1;
9249
16.4k
    }
9250
9251
201
  count = 1;
9252
201
  ext_rel_size = 0;
9253
4.55k
  for (s = abfd->sections; s != NULL; s = s->next)
9254
4.34k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9255
570
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9256
546
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9257
234
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9258
234
      {
9259
234
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9260
234
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9261
0
    {
9262
0
      bfd_set_error (bfd_error_file_truncated);
9263
0
      return -1;
9264
0
    }
9265
234
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9266
234
  if (count > LONG_MAX / sizeof (arelent *))
9267
0
    {
9268
0
      bfd_set_error (bfd_error_file_too_big);
9269
0
      return -1;
9270
0
    }
9271
234
      }
9272
201
  if (count > 1 && !bfd_write_p (abfd))
9273
143
    {
9274
      /* Sanity check reloc section sizes.  */
9275
143
      ufile_ptr filesize = bfd_get_file_size (abfd);
9276
143
      if (filesize != 0 && ext_rel_size > filesize)
9277
3
  {
9278
3
    bfd_set_error (bfd_error_file_truncated);
9279
3
    return -1;
9280
3
  }
9281
143
    }
9282
198
  return count * sizeof (arelent *);
9283
201
}
9284
9285
/* Canonicalize the dynamic relocation entries.  Note that we return the
9286
   dynamic relocations as a single block, although they are actually
9287
   associated with particular sections; the interface, which was
9288
   designed for SunOS style shared libraries, expects that there is only
9289
   one set of dynamic relocs.  Any loadable section that was actually
9290
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9291
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9292
9293
long
9294
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9295
             arelent **storage,
9296
             asymbol **syms)
9297
177
{
9298
177
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9299
177
  asection *s;
9300
177
  long ret;
9301
9302
177
  if (elf_dynsymtab (abfd) == 0)
9303
0
    {
9304
0
      bfd_set_error (bfd_error_invalid_operation);
9305
0
      return -1;
9306
0
    }
9307
9308
177
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9309
177
  ret = 0;
9310
3.50k
  for (s = abfd->sections; s != NULL; s = s->next)
9311
3.34k
    {
9312
3.34k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9313
474
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9314
453
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9315
195
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9316
195
  {
9317
195
    arelent *p;
9318
195
    long count, i;
9319
9320
195
    if (! (*slurp_relocs) (abfd, s, syms, true))
9321
23
      return -1;
9322
172
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9323
172
    p = s->relocation;
9324
1.47k
    for (i = 0; i < count; i++)
9325
1.30k
      *storage++ = p++;
9326
172
    ret += count;
9327
172
  }
9328
3.34k
    }
9329
9330
154
  *storage = NULL;
9331
9332
154
  return ret;
9333
177
}
9334

9335
/* Read in the version information.  */
9336
9337
bool
9338
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9339
328
{
9340
328
  bfd_byte *contents = NULL;
9341
328
  unsigned int freeidx = 0;
9342
328
  size_t amt;
9343
328
  void *contents_addr = NULL;
9344
328
  size_t contents_size = 0;
9345
9346
328
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9347
292
    {
9348
292
      Elf_Internal_Shdr *hdr;
9349
292
      Elf_External_Verneed *everneed;
9350
292
      Elf_Internal_Verneed *iverneed;
9351
292
      unsigned int i;
9352
292
      bfd_byte *contents_end;
9353
292
      size_t verneed_count;
9354
292
      size_t verneed_size;
9355
9356
292
      if (elf_tdata (abfd)->dt_verneed != NULL)
9357
2
  {
9358
2
    hdr = NULL;
9359
2
    contents = elf_tdata (abfd)->dt_verneed;
9360
2
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9361
2
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9362
2
  }
9363
290
      else
9364
290
  {
9365
290
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9366
9367
290
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9368
6
      {
9369
130
      error_return_bad_verref:
9370
130
        _bfd_error_handler
9371
130
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9372
130
        bfd_set_error (bfd_error_bad_value);
9373
159
      error_return_verref:
9374
159
        elf_tdata (abfd)->verref = NULL;
9375
159
        elf_tdata (abfd)->cverrefs = 0;
9376
159
        goto error_return;
9377
130
      }
9378
9379
284
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9380
5
      goto error_return_verref;
9381
279
    contents_size = hdr->sh_size;
9382
279
    contents = _bfd_mmap_temporary (abfd, contents_size,
9383
279
            &contents_addr, &contents_size);
9384
279
    if (contents == NULL)
9385
24
      goto error_return_verref;
9386
9387
255
    verneed_size = hdr->sh_size;
9388
255
    verneed_count = hdr->sh_info;
9389
255
  }
9390
9391
257
      if (_bfd_mul_overflow (verneed_count,
9392
257
           sizeof (Elf_Internal_Verneed), &amt))
9393
0
  {
9394
0
    bfd_set_error (bfd_error_file_too_big);
9395
0
    goto error_return_verref;
9396
0
  }
9397
257
      if (amt == 0)
9398
0
  goto error_return_verref;
9399
257
      elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
9400
257
      if (elf_tdata (abfd)->verref == NULL)
9401
0
  goto error_return_verref;
9402
9403
257
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9404
257
      == sizeof (Elf_External_Vernaux));
9405
257
      contents_end = (contents + verneed_size
9406
257
          - sizeof (Elf_External_Verneed));
9407
257
      everneed = (Elf_External_Verneed *) contents;
9408
257
      iverneed = elf_tdata (abfd)->verref;
9409
283
      for (i = 0; i < verneed_count; i++, iverneed++)
9410
258
  {
9411
258
    Elf_External_Vernaux *evernaux;
9412
258
    Elf_Internal_Vernaux *ivernaux;
9413
258
    unsigned int j;
9414
9415
258
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9416
9417
258
    iverneed->vn_bfd = abfd;
9418
9419
258
    if (elf_use_dt_symtab_p (abfd))
9420
2
      {
9421
2
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9422
1
    iverneed->vn_filename
9423
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9424
1
        else
9425
1
    iverneed->vn_filename = NULL;
9426
2
      }
9427
256
    else if (hdr == NULL)
9428
0
      goto error_return_bad_verref;
9429
256
    else
9430
256
      iverneed->vn_filename
9431
256
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9432
256
             iverneed->vn_file);
9433
258
    if (iverneed->vn_filename == NULL)
9434
83
      goto error_return_bad_verref;
9435
9436
175
    if (iverneed->vn_cnt == 0)
9437
65
      iverneed->vn_auxptr = NULL;
9438
110
    else
9439
110
      {
9440
110
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9441
110
             sizeof (Elf_Internal_Vernaux), &amt))
9442
0
    {
9443
0
      bfd_set_error (bfd_error_file_too_big);
9444
0
      goto error_return_verref;
9445
0
    }
9446
110
        iverneed->vn_auxptr = bfd_alloc (abfd, amt);
9447
110
        if (iverneed->vn_auxptr == NULL)
9448
0
    goto error_return_verref;
9449
110
      }
9450
9451
175
    if (iverneed->vn_aux
9452
175
        > (size_t) (contents_end - (bfd_byte *) everneed))
9453
20
      goto error_return_bad_verref;
9454
9455
155
    evernaux = ((Elf_External_Vernaux *)
9456
155
          ((bfd_byte *) everneed + iverneed->vn_aux));
9457
155
    ivernaux = iverneed->vn_auxptr;
9458
231
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9459
168
      {
9460
168
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9461
9462
168
        if (elf_use_dt_symtab_p (abfd))
9463
0
    {
9464
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9465
0
        ivernaux->vna_nodename
9466
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9467
0
      else
9468
0
        ivernaux->vna_nodename = NULL;
9469
0
    }
9470
168
        else if (hdr == NULL)
9471
0
    goto error_return_bad_verref;
9472
168
        else
9473
168
    ivernaux->vna_nodename
9474
168
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9475
168
                 ivernaux->vna_name);
9476
168
        if (ivernaux->vna_nodename == NULL)
9477
4
    goto error_return_bad_verref;
9478
9479
164
        if (ivernaux->vna_other > freeidx)
9480
83
    freeidx = ivernaux->vna_other;
9481
9482
164
        ivernaux->vna_nextptr = NULL;
9483
164
        if (ivernaux->vna_next == 0)
9484
79
    {
9485
79
      iverneed->vn_cnt = j + 1;
9486
79
      break;
9487
79
    }
9488
85
        if (j + 1 < iverneed->vn_cnt)
9489
72
    ivernaux->vna_nextptr = ivernaux + 1;
9490
9491
85
        if (ivernaux->vna_next
9492
85
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9493
9
    goto error_return_bad_verref;
9494
9495
76
        evernaux = ((Elf_External_Vernaux *)
9496
76
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9497
76
      }
9498
9499
142
    iverneed->vn_nextref = NULL;
9500
142
    if (iverneed->vn_next == 0)
9501
108
      break;
9502
34
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9503
2
      iverneed->vn_nextref = iverneed + 1;
9504
9505
34
    if (iverneed->vn_next
9506
34
        > (size_t) (contents_end - (bfd_byte *) everneed))
9507
8
      goto error_return_bad_verref;
9508
9509
26
    everneed = ((Elf_External_Verneed *)
9510
26
          ((bfd_byte *) everneed + iverneed->vn_next));
9511
26
  }
9512
133
      elf_tdata (abfd)->cverrefs = i;
9513
9514
133
      if (contents != elf_tdata (abfd)->dt_verneed)
9515
132
  _bfd_munmap_temporary (contents_addr, contents_size);
9516
133
      contents = NULL;
9517
133
      contents_addr = NULL;
9518
133
    }
9519
9520
169
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9521
68
    {
9522
68
      Elf_Internal_Shdr *hdr;
9523
68
      Elf_External_Verdef *everdef;
9524
68
      Elf_Internal_Verdef *iverdef;
9525
68
      Elf_Internal_Verdef *iverdefarr;
9526
68
      Elf_Internal_Verdef iverdefmem;
9527
68
      unsigned int i;
9528
68
      unsigned int maxidx;
9529
68
      bfd_byte *contents_end_def, *contents_end_aux;
9530
68
      size_t verdef_count;
9531
68
      size_t verdef_size;
9532
9533
68
      if (elf_tdata (abfd)->dt_verdef != NULL)
9534
0
  {
9535
0
    hdr = NULL;
9536
0
    contents = elf_tdata (abfd)->dt_verdef;
9537
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9538
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9539
0
  }
9540
68
      else
9541
68
  {
9542
68
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9543
9544
68
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9545
5
      {
9546
37
      error_return_bad_verdef:
9547
37
        _bfd_error_handler
9548
37
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9549
37
        bfd_set_error (bfd_error_bad_value);
9550
46
      error_return_verdef:
9551
46
        elf_tdata (abfd)->verdef = NULL;
9552
46
        elf_tdata (abfd)->cverdefs = 0;
9553
46
        goto error_return;
9554
37
      }
9555
9556
63
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9557
2
      goto error_return_verdef;
9558
61
    contents_size = hdr->sh_size;
9559
61
    contents = _bfd_mmap_temporary (abfd, contents_size,
9560
61
            &contents_addr, &contents_size);
9561
61
    if (contents == NULL)
9562
7
      goto error_return_verdef;
9563
9564
54
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9565
54
          >= sizeof (Elf_External_Verdaux));
9566
9567
54
    verdef_count = hdr->sh_info;
9568
54
    verdef_size = hdr->sh_size;
9569
54
  }
9570
9571
54
      contents_end_def = (contents + verdef_size
9572
54
        - sizeof (Elf_External_Verdef));
9573
54
      contents_end_aux = (contents + verdef_size
9574
54
        - sizeof (Elf_External_Verdaux));
9575
9576
      /* We know the number of entries in the section but not the maximum
9577
   index.  Therefore we have to run through all entries and find
9578
   the maximum.  */
9579
54
      everdef = (Elf_External_Verdef *) contents;
9580
54
      maxidx = 0;
9581
64
      for (i = 0; i < verdef_count; ++i)
9582
57
  {
9583
57
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9584
9585
57
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9586
8
      goto error_return_bad_verdef;
9587
49
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9588
48
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9589
9590
49
    if (iverdefmem.vd_next == 0)
9591
31
      break;
9592
9593
18
    if (iverdefmem.vd_next
9594
18
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9595
8
      goto error_return_bad_verdef;
9596
9597
10
    everdef = ((Elf_External_Verdef *)
9598
10
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9599
10
  }
9600
9601
38
      if (default_imported_symver)
9602
0
  {
9603
0
    if (freeidx > maxidx)
9604
0
      maxidx = ++freeidx;
9605
0
    else
9606
0
      freeidx = ++maxidx;
9607
0
  }
9608
38
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9609
0
  {
9610
0
    bfd_set_error (bfd_error_file_too_big);
9611
0
    goto error_return_verdef;
9612
0
  }
9613
9614
38
      if (amt == 0)
9615
0
  goto error_return_verdef;
9616
38
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9617
38
      if (elf_tdata (abfd)->verdef == NULL)
9618
0
  goto error_return_verdef;
9619
9620
38
      elf_tdata (abfd)->cverdefs = maxidx;
9621
9622
38
      everdef = (Elf_External_Verdef *) contents;
9623
38
      iverdefarr = elf_tdata (abfd)->verdef;
9624
42
      for (i = 0; i < verdef_count; ++i)
9625
40
  {
9626
40
    Elf_External_Verdaux *everdaux;
9627
40
    Elf_Internal_Verdaux *iverdaux;
9628
40
    unsigned int j;
9629
9630
40
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9631
9632
40
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9633
0
      goto error_return_bad_verdef;
9634
9635
40
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9636
40
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9637
9638
40
    iverdef->vd_bfd = abfd;
9639
9640
40
    if (iverdef->vd_cnt == 0)
9641
28
      iverdef->vd_auxptr = NULL;
9642
12
    else
9643
12
      {
9644
12
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9645
12
             sizeof (Elf_Internal_Verdaux), &amt))
9646
0
    {
9647
0
      bfd_set_error (bfd_error_file_too_big);
9648
0
      goto error_return_verdef;
9649
0
    }
9650
12
        iverdef->vd_auxptr = bfd_alloc (abfd, amt);
9651
12
        if (iverdef->vd_auxptr == NULL)
9652
0
    goto error_return_verdef;
9653
12
      }
9654
9655
40
    if (iverdef->vd_aux
9656
40
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9657
7
      goto error_return_bad_verdef;
9658
9659
33
    everdaux = ((Elf_External_Verdaux *)
9660
33
          ((bfd_byte *) everdef + iverdef->vd_aux));
9661
33
    iverdaux = iverdef->vd_auxptr;
9662
33
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9663
9
      {
9664
9
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9665
9666
9
        if (elf_use_dt_symtab_p (abfd))
9667
0
    {
9668
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9669
0
        iverdaux->vda_nodename
9670
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9671
0
      else
9672
0
        iverdaux->vda_nodename = NULL;
9673
0
    }
9674
9
        else
9675
9
    iverdaux->vda_nodename
9676
9
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9677
9
                 iverdaux->vda_name);
9678
9
        if (iverdaux->vda_nodename == NULL)
9679
6
    goto error_return_bad_verdef;
9680
9681
3
        iverdaux->vda_nextptr = NULL;
9682
3
        if (iverdaux->vda_next == 0)
9683
0
    {
9684
0
      iverdef->vd_cnt = j + 1;
9685
0
      break;
9686
0
    }
9687
3
        if (j + 1 < iverdef->vd_cnt)
9688
1
    iverdaux->vda_nextptr = iverdaux + 1;
9689
9690
3
        if (iverdaux->vda_next
9691
3
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9692
3
    goto error_return_bad_verdef;
9693
9694
0
        everdaux = ((Elf_External_Verdaux *)
9695
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9696
0
      }
9697
9698
24
    iverdef->vd_nodename = NULL;
9699
24
    if (iverdef->vd_cnt)
9700
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9701
9702
24
    iverdef->vd_nextdef = NULL;
9703
24
    if (iverdef->vd_next == 0)
9704
20
      break;
9705
4
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9706
1
      iverdef->vd_nextdef = iverdef + 1;
9707
9708
4
    everdef = ((Elf_External_Verdef *)
9709
4
         ((bfd_byte *) everdef + iverdef->vd_next));
9710
4
  }
9711
9712
22
      if (contents != elf_tdata (abfd)->dt_verdef)
9713
22
  _bfd_munmap_temporary (contents_addr, contents_size);
9714
22
      contents = NULL;
9715
22
      contents_addr = NULL;
9716
22
    }
9717
101
  else if (default_imported_symver)
9718
0
    {
9719
0
      if (freeidx < 3)
9720
0
  freeidx = 3;
9721
0
      else
9722
0
  freeidx++;
9723
9724
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9725
0
  {
9726
0
    bfd_set_error (bfd_error_file_too_big);
9727
0
    goto error_return;
9728
0
  }
9729
0
      if (amt == 0)
9730
0
  goto error_return;
9731
0
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9732
0
      if (elf_tdata (abfd)->verdef == NULL)
9733
0
  goto error_return;
9734
9735
0
      elf_tdata (abfd)->cverdefs = freeidx;
9736
0
    }
9737
9738
  /* Create a default version based on the soname.  */
9739
123
  if (default_imported_symver)
9740
0
    {
9741
0
      Elf_Internal_Verdef *iverdef;
9742
0
      Elf_Internal_Verdaux *iverdaux;
9743
9744
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9745
9746
0
      iverdef->vd_version = VER_DEF_CURRENT;
9747
0
      iverdef->vd_flags = 0;
9748
0
      iverdef->vd_ndx = freeidx;
9749
0
      iverdef->vd_cnt = 1;
9750
9751
0
      iverdef->vd_bfd = abfd;
9752
9753
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9754
0
      if (iverdef->vd_nodename == NULL)
9755
0
  goto error_return_verdef;
9756
0
      iverdef->vd_nextdef = NULL;
9757
0
      iverdef->vd_auxptr = bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux));
9758
0
      if (iverdef->vd_auxptr == NULL)
9759
0
  goto error_return_verdef;
9760
9761
0
      iverdaux = iverdef->vd_auxptr;
9762
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9763
0
    }
9764
9765
123
  return true;
9766
9767
205
 error_return:
9768
205
  if (contents != elf_tdata (abfd)->dt_verneed
9769
155
      && contents != elf_tdata (abfd)->dt_verdef)
9770
155
    _bfd_munmap_temporary (contents_addr, contents_size);
9771
205
  return false;
9772
123
}
9773

9774
asymbol *
9775
_bfd_elf_make_empty_symbol (bfd *abfd)
9776
1.74M
{
9777
1.74M
  elf_symbol_type *newsym;
9778
9779
1.74M
  newsym = bfd_zalloc (abfd, sizeof (*newsym));
9780
1.74M
  if (!newsym)
9781
0
    return NULL;
9782
1.74M
  newsym->symbol.the_bfd = abfd;
9783
1.74M
  return &newsym->symbol;
9784
1.74M
}
9785
9786
void
9787
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9788
        asymbol *symbol,
9789
        symbol_info *ret)
9790
40.2k
{
9791
40.2k
  bfd_symbol_info (symbol, ret);
9792
40.2k
}
9793
9794
/* Return whether a symbol name implies a local symbol.  Most targets
9795
   use this function for the is_local_label_name entry point, but some
9796
   override it.  */
9797
9798
bool
9799
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9800
            const char *name)
9801
28.5k
{
9802
  /* Normal local symbols start with ``.L''.  */
9803
28.5k
  if (name[0] == '.' && name[1] == 'L')
9804
8.28k
    return true;
9805
9806
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9807
     DWARF debugging symbols starting with ``..''.  */
9808
20.3k
  if (name[0] == '.' && name[1] == '.')
9809
529
    return true;
9810
9811
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9812
     emitting DWARF debugging output.  I suspect this is actually a
9813
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9814
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9815
     underscore to be emitted on some ELF targets).  For ease of use,
9816
     we treat such symbols as local.  */
9817
19.7k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9818
1
    return true;
9819
9820
  /* Treat assembler generated fake symbols, dollar local labels and
9821
     forward-backward labels (aka local labels) as locals.
9822
     These labels have the form:
9823
9824
       L0^A.*              (fake symbols)
9825
9826
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9827
9828
     Versions which start with .L will have already been matched above,
9829
     so we only need to match the rest.  */
9830
19.7k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9831
82
    {
9832
82
      bool ret = false;
9833
82
      const char * p;
9834
82
      char c;
9835
9836
96
      for (p = name + 2; (c = *p); p++)
9837
96
  {
9838
96
    if (c == 1 || c == 2)
9839
12
      {
9840
12
        if (c == 1 && p == name + 2)
9841
    /* A fake symbol.  */
9842
12
    return true;
9843
9844
        /* FIXME: We are being paranoid here and treating symbols like
9845
     L0^Bfoo as if there were non-local, on the grounds that the
9846
     assembler will never generate them.  But can any symbol
9847
     containing an ASCII value in the range 1-31 ever be anything
9848
     other than some kind of local ?  */
9849
0
        ret = true;
9850
0
      }
9851
9852
84
    if (! ISDIGIT (c))
9853
70
      {
9854
70
        ret = false;
9855
70
        break;
9856
70
      }
9857
84
  }
9858
70
      return ret;
9859
82
    }
9860
9861
19.6k
  return false;
9862
19.7k
}
9863
9864
alent *
9865
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9866
         asymbol *symbol ATTRIBUTE_UNUSED)
9867
0
{
9868
0
  abort ();
9869
0
  return NULL;
9870
0
}
9871
9872
bool
9873
_bfd_elf_set_arch_mach (bfd *abfd,
9874
      enum bfd_architecture arch,
9875
      unsigned long machine)
9876
792
{
9877
  /* If this isn't the right architecture for this backend, and this
9878
     isn't the generic backend, fail.  */
9879
792
  if (arch != get_elf_backend_data (abfd)->arch
9880
0
      && arch != bfd_arch_unknown
9881
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9882
0
    return false;
9883
9884
792
  return bfd_default_set_arch_mach (abfd, arch, machine);
9885
792
}
9886
9887
/* Find the nearest line to a particular section and offset,
9888
   for error reporting.  */
9889
9890
bool
9891
_bfd_elf_find_nearest_line (bfd *abfd,
9892
          asymbol **symbols,
9893
          asection *section,
9894
          bfd_vma offset,
9895
          const char **filename_ptr,
9896
          const char **functionname_ptr,
9897
          unsigned int *line_ptr,
9898
          unsigned int *discriminator_ptr)
9899
35.8k
{
9900
35.8k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9901
35.8k
                offset, filename_ptr,
9902
35.8k
                functionname_ptr, line_ptr,
9903
35.8k
                discriminator_ptr);
9904
35.8k
}
9905
9906
/* Find the nearest line to a particular section and offset,
9907
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9908
   can be optionally specified.  */
9909
9910
bool
9911
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9912
             const char *alt_filename,
9913
             asymbol **symbols,
9914
             asection *section,
9915
             bfd_vma offset,
9916
             const char **filename_ptr,
9917
             const char **functionname_ptr,
9918
             unsigned int *line_ptr,
9919
             unsigned int *discriminator_ptr)
9920
35.8k
{
9921
35.8k
  bool found;
9922
9923
35.8k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9924
35.8k
                section, offset, filename_ptr,
9925
35.8k
                functionname_ptr, line_ptr,
9926
35.8k
                discriminator_ptr,
9927
35.8k
                dwarf_debug_sections,
9928
35.8k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9929
10.0k
    return true;
9930
9931
25.7k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9932
25.7k
             filename_ptr, functionname_ptr, line_ptr))
9933
0
    {
9934
0
      if (!*functionname_ptr)
9935
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9936
0
        *filename_ptr ? NULL : filename_ptr,
9937
0
        functionname_ptr);
9938
0
      return true;
9939
0
    }
9940
9941
25.7k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9942
25.7k
               &found, filename_ptr,
9943
25.7k
               functionname_ptr, line_ptr,
9944
25.7k
               &elf_tdata (abfd)->line_info))
9945
0
    return false;
9946
25.7k
  if (found && (*functionname_ptr || *line_ptr))
9947
0
    return true;
9948
9949
25.7k
  if (symbols == NULL)
9950
7.29k
    return false;
9951
9952
18.4k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9953
18.4k
        filename_ptr, functionname_ptr))
9954
14.9k
    return false;
9955
9956
3.55k
  *line_ptr = 0;
9957
3.55k
  return true;
9958
18.4k
}
9959
9960
/* Find the line for a symbol.  */
9961
9962
bool
9963
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9964
        const char **filename_ptr, unsigned int *line_ptr)
9965
6.07k
{
9966
6.07k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9967
6.07k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9968
6.07k
          filename_ptr, NULL, line_ptr, NULL,
9969
6.07k
          dwarf_debug_sections,
9970
6.07k
          &tdata->dwarf2_find_line_info);
9971
6.07k
}
9972
9973
/* After a call to bfd_find_nearest_line, successive calls to
9974
   bfd_find_inliner_info can be used to get source information about
9975
   each level of function inlining that terminated at the address
9976
   passed to bfd_find_nearest_line.  Currently this is only supported
9977
   for DWARF2 with appropriate DWARF3 extensions. */
9978
9979
bool
9980
_bfd_elf_find_inliner_info (bfd *abfd,
9981
          const char **filename_ptr,
9982
          const char **functionname_ptr,
9983
          unsigned int *line_ptr)
9984
0
{
9985
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9986
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9987
0
          functionname_ptr, line_ptr,
9988
0
          &tdata->dwarf2_find_line_info);
9989
0
}
9990
9991
int
9992
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9993
0
{
9994
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9995
0
  int ret = bed->s->sizeof_ehdr;
9996
9997
0
  if (!bfd_link_relocatable (info))
9998
0
    {
9999
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10000
10001
0
      if (phdr_size == (bfd_size_type) -1)
10002
0
  {
10003
0
    struct elf_segment_map *m;
10004
10005
0
    phdr_size = 0;
10006
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10007
0
      phdr_size += bed->s->sizeof_phdr;
10008
10009
0
    if (phdr_size == 0)
10010
0
      phdr_size = get_program_header_size (abfd, info);
10011
0
  }
10012
10013
0
      elf_program_header_size (abfd) = phdr_size;
10014
0
      ret += phdr_size;
10015
0
    }
10016
10017
0
  return ret;
10018
0
}
10019
10020
bool
10021
_bfd_elf_set_section_contents (bfd *abfd,
10022
             sec_ptr section,
10023
             const void *location,
10024
             file_ptr offset,
10025
             bfd_size_type count)
10026
1.19k
{
10027
1.19k
  Elf_Internal_Shdr *hdr;
10028
10029
1.19k
  if (! abfd->output_has_begun
10030
155
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10031
11
    return false;
10032
10033
1.18k
  if (!count)
10034
0
    return true;
10035
10036
1.18k
  hdr = &elf_section_data (section)->this_hdr;
10037
1.18k
  if (hdr->sh_offset == (file_ptr) -1)
10038
0
    {
10039
0
      unsigned char *contents;
10040
10041
0
      if (bfd_section_is_ctf (section))
10042
  /* Nothing to do with this section: the contents are generated
10043
     later.  */
10044
0
  return true;
10045
10046
0
      if ((offset + count) > hdr->sh_size)
10047
0
  {
10048
0
    _bfd_error_handler
10049
0
      (_("%pB:%pA: error: attempting to write"
10050
0
         " over the end of the section"),
10051
0
       abfd, section);
10052
10053
0
    bfd_set_error (bfd_error_invalid_operation);
10054
0
    return false;
10055
0
  }
10056
10057
0
      contents = hdr->contents;
10058
0
      if (contents == NULL)
10059
0
  {
10060
0
    _bfd_error_handler
10061
0
      (_("%pB:%pA: error: attempting to write"
10062
0
         " section into an empty buffer"),
10063
0
       abfd, section);
10064
10065
0
    bfd_set_error (bfd_error_invalid_operation);
10066
0
    return false;
10067
0
  }
10068
10069
0
      memcpy (contents + offset, location, count);
10070
0
      return true;
10071
0
    }
10072
10073
1.18k
  return _bfd_generic_set_section_contents (abfd, section,
10074
1.18k
              location, offset, count);
10075
1.18k
}
10076
10077
bool
10078
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10079
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10080
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10081
0
{
10082
0
  abort ();
10083
0
  return false;
10084
0
}
10085
10086
/* Try to convert a non-ELF reloc into an ELF one.  */
10087
10088
bool
10089
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10090
0
{
10091
  /* Check whether we really have an ELF howto.  */
10092
10093
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10094
0
    {
10095
0
      bfd_reloc_code_real_type code;
10096
0
      reloc_howto_type *howto;
10097
10098
      /* Alien reloc: Try to determine its type to replace it with an
10099
   equivalent ELF reloc.  */
10100
10101
0
      if (areloc->howto->pc_relative)
10102
0
  {
10103
0
    switch (areloc->howto->bitsize)
10104
0
      {
10105
0
      case 8:
10106
0
        code = BFD_RELOC_8_PCREL;
10107
0
        break;
10108
0
      case 12:
10109
0
        code = BFD_RELOC_12_PCREL;
10110
0
        break;
10111
0
      case 16:
10112
0
        code = BFD_RELOC_16_PCREL;
10113
0
        break;
10114
0
      case 24:
10115
0
        code = BFD_RELOC_24_PCREL;
10116
0
        break;
10117
0
      case 32:
10118
0
        code = BFD_RELOC_32_PCREL;
10119
0
        break;
10120
0
      case 64:
10121
0
        code = BFD_RELOC_64_PCREL;
10122
0
        break;
10123
0
      default:
10124
0
        goto fail;
10125
0
      }
10126
10127
0
    howto = bfd_reloc_type_lookup (abfd, code);
10128
10129
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10130
0
      {
10131
0
        if (howto->pcrel_offset)
10132
0
    areloc->addend += areloc->address;
10133
0
        else
10134
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10135
0
      }
10136
0
  }
10137
0
      else
10138
0
  {
10139
0
    switch (areloc->howto->bitsize)
10140
0
      {
10141
0
      case 8:
10142
0
        code = BFD_RELOC_8;
10143
0
        break;
10144
0
      case 14:
10145
0
        code = BFD_RELOC_14;
10146
0
        break;
10147
0
      case 16:
10148
0
        code = BFD_RELOC_16;
10149
0
        break;
10150
0
      case 26:
10151
0
        code = BFD_RELOC_26;
10152
0
        break;
10153
0
      case 32:
10154
0
        code = BFD_RELOC_32;
10155
0
        break;
10156
0
      case 64:
10157
0
        code = BFD_RELOC_64;
10158
0
        break;
10159
0
      default:
10160
0
        goto fail;
10161
0
      }
10162
10163
0
    howto = bfd_reloc_type_lookup (abfd, code);
10164
0
  }
10165
10166
0
      if (howto)
10167
0
  areloc->howto = howto;
10168
0
      else
10169
0
  goto fail;
10170
0
    }
10171
10172
0
  return true;
10173
10174
0
 fail:
10175
  /* xgettext:c-format */
10176
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10177
0
          abfd, areloc->howto->name);
10178
0
  bfd_set_error (bfd_error_sorry);
10179
0
  return false;
10180
0
}
10181
10182
bool
10183
_bfd_elf_free_cached_info (bfd *abfd)
10184
8.03M
{
10185
8.03M
  struct elf_obj_tdata *tdata;
10186
10187
8.03M
  if ((bfd_get_format (abfd) == bfd_object
10188
8.00M
       || bfd_get_format (abfd) == bfd_core)
10189
44.1k
      && (tdata = elf_tdata (abfd)) != NULL)
10190
44.1k
    {
10191
44.1k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10192
398
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10193
44.1k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10194
44.1k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10195
44.1k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10196
44.1k
      _bfd_elf_cleanup_object_attributes (abfd);
10197
549k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10198
505k
  {
10199
505k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10200
505k
    if (!sec->alloced)
10201
504k
      {
10202
504k
        free (elf_section_data (sec)->this_hdr.contents);
10203
504k
        elf_section_data (sec)->this_hdr.contents = NULL;
10204
504k
      }
10205
505k
    free (elf_section_data (sec)->relocs);
10206
505k
    elf_section_data (sec)->relocs = NULL;
10207
505k
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10208
0
      {
10209
0
        struct eh_frame_sec_info *sec_info = sec->sec_info;
10210
0
        free (sec_info->cies);
10211
0
      }
10212
505k
    if (sec->sec_info_type == SEC_INFO_TYPE_SFRAME)
10213
0
      {
10214
0
        struct sframe_dec_info *sfd_info = sec->sec_info;
10215
0
        sframe_decoder_free (&sfd_info->sfd_ctx);
10216
0
      }
10217
505k
  }
10218
44.1k
      free (tdata->symtab_hdr.contents);
10219
44.1k
      tdata->symtab_hdr.contents = NULL;
10220
44.1k
    }
10221
10222
8.03M
  return _bfd_generic_bfd_free_cached_info (abfd);
10223
8.03M
}
10224
10225
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10226
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10227
   range-checking to interfere.  There is nothing else to do in processing
10228
   this reloc.  */
10229
10230
bfd_reloc_status_type
10231
_bfd_elf_rel_vtable_reloc_fn
10232
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10233
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10234
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10235
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10236
68
{
10237
68
  return bfd_reloc_ok;
10238
68
}
10239

10240
/* Elf core file support.  Much of this only works on native
10241
   toolchains, since we rely on knowing the
10242
   machine-dependent procfs structure in order to pick
10243
   out details about the corefile.  */
10244
10245
#ifdef HAVE_SYS_PROCFS_H
10246
# include <sys/procfs.h>
10247
#endif
10248
10249
/* Return a PID that identifies a "thread" for threaded cores, or the
10250
   PID of the main process for non-threaded cores.  */
10251
10252
static int
10253
elfcore_make_pid (bfd *abfd)
10254
230
{
10255
230
  int pid;
10256
10257
230
  pid = elf_tdata (abfd)->core->lwpid;
10258
230
  if (pid == 0)
10259
230
    pid = elf_tdata (abfd)->core->pid;
10260
10261
230
  return pid;
10262
230
}
10263
10264
/* If there isn't a section called NAME, make one, using data from
10265
   SECT.  Note, this function will generate a reference to NAME, so
10266
   you shouldn't deallocate or overwrite it.  */
10267
10268
static bool
10269
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10270
230
{
10271
230
  asection *sect2;
10272
10273
230
  if (bfd_get_section_by_name (abfd, name) != NULL)
10274
146
    return true;
10275
10276
84
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10277
84
  if (sect2 == NULL)
10278
0
    return false;
10279
10280
84
  sect2->size = sect->size;
10281
84
  sect2->filepos = sect->filepos;
10282
84
  sect2->alignment_power = sect->alignment_power;
10283
84
  return true;
10284
84
}
10285
10286
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10287
   actually creates up to two pseudosections:
10288
   - For the single-threaded case, a section named NAME, unless
10289
     such a section already exists.
10290
   - For the multi-threaded case, a section named "NAME/PID", where
10291
     PID is elfcore_make_pid (abfd).
10292
   Both pseudosections have identical contents.  */
10293
bool
10294
_bfd_elfcore_make_pseudosection (bfd *abfd,
10295
         char *name,
10296
         size_t size,
10297
         ufile_ptr filepos)
10298
230
{
10299
230
  char buf[100];
10300
230
  char *threaded_name;
10301
230
  size_t len;
10302
230
  asection *sect;
10303
10304
  /* Build the section name.  */
10305
10306
230
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10307
230
  len = strlen (buf) + 1;
10308
230
  threaded_name = bfd_alloc (abfd, len);
10309
230
  if (threaded_name == NULL)
10310
0
    return false;
10311
230
  memcpy (threaded_name, buf, len);
10312
10313
230
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10314
230
               SEC_HAS_CONTENTS);
10315
230
  if (sect == NULL)
10316
0
    return false;
10317
230
  sect->size = size;
10318
230
  sect->filepos = filepos;
10319
230
  sect->alignment_power = 2;
10320
10321
230
  return elfcore_maybe_make_sect (abfd, name, sect);
10322
230
}
10323
10324
static bool
10325
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10326
        size_t offs)
10327
58
{
10328
58
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10329
58
                   SEC_HAS_CONTENTS);
10330
10331
58
  if (sect == NULL)
10332
0
    return false;
10333
10334
58
  sect->size = note->descsz - offs;
10335
58
  sect->filepos = note->descpos + offs;
10336
58
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10337
10338
58
  return true;
10339
58
}
10340
10341
/* prstatus_t exists on:
10342
     solaris 2.5+
10343
     linux 2.[01] + glibc
10344
     unixware 4.2
10345
*/
10346
10347
#if defined (HAVE_PRSTATUS_T)
10348
10349
static bool
10350
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10351
417
{
10352
417
  size_t size;
10353
417
  int offset;
10354
10355
417
  if (note->descsz == sizeof (prstatus_t))
10356
0
    {
10357
0
      prstatus_t prstat;
10358
10359
0
      size = sizeof (prstat.pr_reg);
10360
0
      offset   = offsetof (prstatus_t, pr_reg);
10361
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10362
10363
      /* Do not overwrite the core signal if it
10364
   has already been set by another thread.  */
10365
0
      if (elf_tdata (abfd)->core->signal == 0)
10366
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10367
0
      if (elf_tdata (abfd)->core->pid == 0)
10368
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10369
10370
      /* pr_who exists on:
10371
   solaris 2.5+
10372
   unixware 4.2
10373
   pr_who doesn't exist on:
10374
   linux 2.[01]
10375
   */
10376
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10377
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10378
#else
10379
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10380
0
#endif
10381
0
    }
10382
417
#if defined (HAVE_PRSTATUS32_T)
10383
417
  else if (note->descsz == sizeof (prstatus32_t))
10384
0
    {
10385
      /* 64-bit host, 32-bit corefile */
10386
0
      prstatus32_t prstat;
10387
10388
0
      size = sizeof (prstat.pr_reg);
10389
0
      offset   = offsetof (prstatus32_t, pr_reg);
10390
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10391
10392
      /* Do not overwrite the core signal if it
10393
   has already been set by another thread.  */
10394
0
      if (elf_tdata (abfd)->core->signal == 0)
10395
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10396
0
      if (elf_tdata (abfd)->core->pid == 0)
10397
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10398
10399
      /* pr_who exists on:
10400
   solaris 2.5+
10401
   unixware 4.2
10402
   pr_who doesn't exist on:
10403
   linux 2.[01]
10404
   */
10405
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10406
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10407
#else
10408
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10409
0
#endif
10410
0
    }
10411
417
#endif /* HAVE_PRSTATUS32_T */
10412
417
  else
10413
417
    {
10414
      /* Fail - we don't know how to handle any other
10415
   note size (ie. data object type).  */
10416
417
      return true;
10417
417
    }
10418
10419
  /* Make a ".reg/999" section and a ".reg" section.  */
10420
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10421
0
            size, note->descpos + offset);
10422
417
}
10423
#endif /* defined (HAVE_PRSTATUS_T) */
10424
10425
/* Create a pseudosection containing the exact contents of NOTE.  */
10426
static bool
10427
elfcore_make_note_pseudosection (bfd *abfd,
10428
         char *name,
10429
         Elf_Internal_Note *note)
10430
230
{
10431
230
  return _bfd_elfcore_make_pseudosection (abfd, name,
10432
230
            note->descsz, note->descpos);
10433
230
}
10434
10435
/* There isn't a consistent prfpregset_t across platforms,
10436
   but it doesn't matter, because we don't have to pick this
10437
   data structure apart.  */
10438
10439
static bool
10440
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10441
230
{
10442
230
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10443
230
}
10444
10445
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10446
   type of NT_PRXFPREG.  Just include the whole note's contents
10447
   literally.  */
10448
10449
static bool
10450
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10451
0
{
10452
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
10453
0
}
10454
10455
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10456
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10457
   contents literally.  */
10458
10459
static bool
10460
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10461
0
{
10462
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
10463
0
}
10464
10465
static bool
10466
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10467
0
{
10468
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
10469
0
}
10470
10471
/* Linux dumps the XSAVE Layout description in a note named "LINUX"
10472
   with a note type of NT_X86_XSAVE_LAYOUT. */
10473
static bool
10474
elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
10475
0
{
10476
0
  return elfcore_make_note_pseudosection (abfd,
10477
0
            NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
10478
0
            note);
10479
0
}
10480
10481
static bool
10482
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10483
0
{
10484
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
10485
0
}
10486
10487
static bool
10488
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10489
0
{
10490
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
10491
0
}
10492
10493
static bool
10494
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10495
0
{
10496
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
10497
0
}
10498
10499
static bool
10500
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10501
0
{
10502
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
10503
0
}
10504
10505
static bool
10506
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10507
0
{
10508
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
10509
0
}
10510
10511
static bool
10512
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10513
0
{
10514
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
10515
0
}
10516
10517
static bool
10518
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10519
0
{
10520
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
10521
0
}
10522
10523
static bool
10524
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10525
0
{
10526
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
10527
0
}
10528
10529
static bool
10530
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10531
0
{
10532
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
10533
0
}
10534
10535
static bool
10536
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10537
0
{
10538
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
10539
0
}
10540
10541
static bool
10542
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10543
0
{
10544
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
10545
0
}
10546
10547
static bool
10548
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10549
0
{
10550
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
10551
0
}
10552
10553
static bool
10554
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10555
0
{
10556
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
10557
0
}
10558
10559
static bool
10560
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10561
0
{
10562
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
10563
0
}
10564
10565
static bool
10566
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10567
0
{
10568
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
10569
0
}
10570
10571
static bool
10572
elfcore_grok_ppc_dmr (bfd *abfd, Elf_Internal_Note *note)
10573
0
{
10574
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DMR, note);
10575
0
}
10576
10577
static bool
10578
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10579
0
{
10580
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
10581
0
}
10582
10583
static bool
10584
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10585
0
{
10586
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
10587
0
}
10588
10589
static bool
10590
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10591
0
{
10592
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
10593
0
}
10594
10595
static bool
10596
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10597
0
{
10598
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
10599
0
}
10600
10601
static bool
10602
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10603
0
{
10604
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
10605
0
}
10606
10607
static bool
10608
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10609
0
{
10610
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
10611
0
}
10612
10613
static bool
10614
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10615
0
{
10616
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
10617
0
}
10618
10619
static bool
10620
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10621
0
{
10622
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
10623
0
}
10624
10625
static bool
10626
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10627
0
{
10628
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
10629
0
}
10630
10631
static bool
10632
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10633
0
{
10634
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
10635
0
}
10636
10637
static bool
10638
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10639
0
{
10640
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
10641
0
}
10642
10643
static bool
10644
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10645
0
{
10646
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
10647
0
}
10648
10649
static bool
10650
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10651
0
{
10652
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
10653
0
}
10654
10655
static bool
10656
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10657
0
{
10658
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
10659
0
}
10660
10661
static bool
10662
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10663
0
{
10664
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
10665
0
}
10666
10667
static bool
10668
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10669
0
{
10670
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
10671
0
}
10672
10673
static bool
10674
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10675
0
{
10676
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
10677
0
}
10678
10679
static bool
10680
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10681
0
{
10682
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
10683
0
}
10684
10685
static bool
10686
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10687
0
{
10688
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
10689
0
}
10690
10691
static bool
10692
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10693
0
{
10694
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
10695
0
}
10696
10697
static bool
10698
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10699
0
{
10700
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
10701
0
}
10702
10703
static bool
10704
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10705
0
{
10706
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
10707
0
}
10708
10709
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10710
   successful, otherwise return FALSE.  */
10711
10712
static bool
10713
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10714
0
{
10715
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
10716
0
}
10717
10718
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10719
   successful, otherwise return FALSE.  */
10720
10721
static bool
10722
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10723
0
{
10724
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
10725
0
}
10726
10727
/* Convert NOTE into a bfd_section called ".reg-aarch-poe".  Return TRUE if
10728
   successful, otherwise return FALSE.  */
10729
10730
static bool
10731
elfcore_grok_aarch_poe (bfd *abfd, Elf_Internal_Note *note)
10732
0
{
10733
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_POE, note);
10734
0
}
10735
10736
/* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
10737
 * Return TRUE if successful, otherwise return FALSE.  */
10738
10739
static bool
10740
elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
10741
0
{
10742
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
10743
0
}
10744
10745
static bool
10746
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10747
0
{
10748
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
10749
0
}
10750
10751
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10752
   successful otherwise, return FALSE.  */
10753
10754
static bool
10755
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10756
0
{
10757
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
10758
0
}
10759
10760
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10761
   successful otherwise, return FALSE.  */
10762
10763
static bool
10764
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10765
0
{
10766
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10767
0
}
10768
10769
static bool
10770
elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
10771
0
{
10772
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
10773
0
            note);
10774
0
}
10775
10776
static bool
10777
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10778
0
{
10779
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
10780
0
}
10781
10782
static bool
10783
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10784
0
{
10785
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
10786
0
}
10787
10788
static bool
10789
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10790
0
{
10791
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
10792
0
}
10793
10794
static bool
10795
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10796
0
{
10797
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
10798
0
}
10799
10800
#if defined (HAVE_PRPSINFO_T)
10801
typedef prpsinfo_t   elfcore_psinfo_t;
10802
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10803
typedef prpsinfo32_t elfcore_psinfo32_t;
10804
#endif
10805
#endif
10806
10807
#if defined (HAVE_PSINFO_T)
10808
typedef psinfo_t   elfcore_psinfo_t;
10809
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10810
typedef psinfo32_t elfcore_psinfo32_t;
10811
#endif
10812
#endif
10813
10814
/* return a malloc'ed copy of a string at START which is at
10815
   most MAX bytes long, possibly without a terminating '\0'.
10816
   the copy will always have a terminating '\0'.  */
10817
10818
char *
10819
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10820
28
{
10821
28
  char *dups;
10822
28
  char *end = (char *) memchr (start, '\0', max);
10823
28
  size_t len;
10824
10825
28
  if (end == NULL)
10826
7
    len = max;
10827
21
  else
10828
21
    len = end - start;
10829
10830
28
  dups = bfd_alloc (abfd, len + 1);
10831
28
  if (dups == NULL)
10832
0
    return NULL;
10833
10834
28
  memcpy (dups, start, len);
10835
28
  dups[len] = '\0';
10836
10837
28
  return dups;
10838
28
}
10839
10840
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10841
static bool
10842
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10843
175
{
10844
175
  if (note->descsz == sizeof (elfcore_psinfo_t))
10845
0
    {
10846
0
      elfcore_psinfo_t psinfo;
10847
10848
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10849
10850
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10851
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10852
0
#endif
10853
0
      elf_tdata (abfd)->core->program
10854
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10855
0
        sizeof (psinfo.pr_fname));
10856
10857
0
      elf_tdata (abfd)->core->command
10858
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10859
0
        sizeof (psinfo.pr_psargs));
10860
0
    }
10861
175
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10862
175
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10863
14
    {
10864
      /* 64-bit host, 32-bit corefile */
10865
14
      elfcore_psinfo32_t psinfo;
10866
10867
14
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10868
10869
14
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10870
14
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10871
14
#endif
10872
14
      elf_tdata (abfd)->core->program
10873
14
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10874
14
        sizeof (psinfo.pr_fname));
10875
10876
14
      elf_tdata (abfd)->core->command
10877
14
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10878
14
        sizeof (psinfo.pr_psargs));
10879
14
    }
10880
161
#endif
10881
10882
161
  else
10883
161
    {
10884
      /* Fail - we don't know how to handle any other
10885
   note size (ie. data object type).  */
10886
161
      return true;
10887
161
    }
10888
10889
  /* Note that for some reason, a spurious space is tacked
10890
     onto the end of the args in some (at least one anyway)
10891
     implementations, so strip it off if it exists.  */
10892
10893
14
  {
10894
14
    char *command = elf_tdata (abfd)->core->command;
10895
14
    int n = strlen (command);
10896
10897
14
    if (0 < n && command[n - 1] == ' ')
10898
3
      command[n - 1] = '\0';
10899
14
  }
10900
10901
14
  return true;
10902
175
}
10903
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10904
10905
#if defined (HAVE_PSTATUS_T)
10906
static bool
10907
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10908
{
10909
  if (note->descsz == sizeof (pstatus_t)
10910
#if defined (HAVE_PXSTATUS_T)
10911
      || note->descsz == sizeof (pxstatus_t)
10912
#endif
10913
      )
10914
    {
10915
      pstatus_t pstat;
10916
10917
      memcpy (&pstat, note->descdata, sizeof (pstat));
10918
10919
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10920
    }
10921
#if defined (HAVE_PSTATUS32_T)
10922
  else if (note->descsz == sizeof (pstatus32_t))
10923
    {
10924
      /* 64-bit host, 32-bit corefile */
10925
      pstatus32_t pstat;
10926
10927
      memcpy (&pstat, note->descdata, sizeof (pstat));
10928
10929
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10930
    }
10931
#endif
10932
  /* Could grab some more details from the "representative"
10933
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10934
     NT_LWPSTATUS note, presumably.  */
10935
10936
  return true;
10937
}
10938
#endif /* defined (HAVE_PSTATUS_T) */
10939
10940
#if defined (HAVE_LWPSTATUS_T)
10941
static bool
10942
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10943
{
10944
  lwpstatus_t lwpstat;
10945
  char buf[100];
10946
  char *name;
10947
  size_t len;
10948
  asection *sect;
10949
10950
  if (note->descsz != sizeof (lwpstat)
10951
#if defined (HAVE_LWPXSTATUS_T)
10952
      && note->descsz != sizeof (lwpxstatus_t)
10953
#endif
10954
      )
10955
    return true;
10956
10957
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10958
10959
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10960
  /* Do not overwrite the core signal if it has already been set by
10961
     another thread.  */
10962
  if (elf_tdata (abfd)->core->signal == 0)
10963
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10964
10965
  /* Make a ".reg/999" section.  */
10966
10967
  sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
10968
  len = strlen (buf) + 1;
10969
  name = bfd_alloc (abfd, len);
10970
  if (name == NULL)
10971
    return false;
10972
  memcpy (name, buf, len);
10973
10974
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10975
  if (sect == NULL)
10976
    return false;
10977
10978
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10979
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10980
  sect->filepos = note->descpos
10981
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10982
#endif
10983
10984
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10985
  sect->size = sizeof (lwpstat.pr_reg);
10986
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10987
#endif
10988
10989
  sect->alignment_power = 2;
10990
10991
  if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
10992
    return false;
10993
10994
  /* Make a ".reg2/999" section */
10995
10996
  sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
10997
  len = strlen (buf) + 1;
10998
  name = bfd_alloc (abfd, len);
10999
  if (name == NULL)
11000
    return false;
11001
  memcpy (name, buf, len);
11002
11003
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11004
  if (sect == NULL)
11005
    return false;
11006
11007
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11008
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11009
  sect->filepos = note->descpos
11010
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11011
#endif
11012
11013
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11014
  sect->size = sizeof (lwpstat.pr_fpreg);
11015
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11016
#endif
11017
11018
  sect->alignment_power = 2;
11019
11020
  return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
11021
}
11022
#endif /* defined (HAVE_LWPSTATUS_T) */
11023
11024
/* These constants, and the structure offsets used below, are defined by
11025
   Cygwin's core_dump.h */
11026
0
#define NOTE_INFO_PROCESS  1
11027
0
#define NOTE_INFO_THREAD   2
11028
0
#define NOTE_INFO_MODULE   3
11029
0
#define NOTE_INFO_MODULE64 4
11030
11031
static bool
11032
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11033
47
{
11034
47
  char buf[30];
11035
47
  char *name;
11036
47
  size_t len;
11037
47
  unsigned int name_size;
11038
47
  asection *sect;
11039
47
  unsigned int type;
11040
47
  int is_active_thread;
11041
47
  bfd_vma base_addr;
11042
11043
47
  if (note->descsz < 4)
11044
5
    return true;
11045
11046
42
  if (! startswith (note->namedata, "win32"))
11047
42
    return true;
11048
11049
0
  type = bfd_get_32 (abfd, note->descdata);
11050
11051
0
  static const struct
11052
0
  {
11053
0
    const char *type_name;
11054
0
    unsigned long min_size;
11055
0
  } size_check[] =
11056
0
      {
11057
0
       { "NOTE_INFO_PROCESS", 12 },
11058
0
       { "NOTE_INFO_THREAD", 12 },
11059
0
       { "NOTE_INFO_MODULE", 12 },
11060
0
       { "NOTE_INFO_MODULE64", 16 },
11061
0
      };
11062
11063
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11064
0
      return true;
11065
11066
0
  if (note->descsz < size_check[type - 1].min_size)
11067
0
    {
11068
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11069
0
          " is too small"),
11070
0
        abfd, size_check[type - 1].type_name, note->descsz);
11071
0
      return true;
11072
0
    }
11073
11074
0
  switch (type)
11075
0
    {
11076
0
    case NOTE_INFO_PROCESS:
11077
      /* FIXME: need to add ->core->command.  */
11078
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11079
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11080
0
      break;
11081
11082
0
    case NOTE_INFO_THREAD:
11083
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11084
   structure. */
11085
      /* thread_info.tid */
11086
0
      sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11087
11088
0
      len = strlen (buf) + 1;
11089
0
      name = bfd_alloc (abfd, len);
11090
0
      if (name == NULL)
11091
0
  return false;
11092
11093
0
      memcpy (name, buf, len);
11094
11095
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11096
0
      if (sect == NULL)
11097
0
  return false;
11098
11099
      /* sizeof (thread_info.thread_context) */
11100
0
      sect->size = note->descsz - 12;
11101
      /* offsetof (thread_info.thread_context) */
11102
0
      sect->filepos = note->descpos + 12;
11103
0
      sect->alignment_power = 2;
11104
11105
      /* thread_info.is_active_thread */
11106
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11107
11108
0
      if (is_active_thread)
11109
0
  if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11110
0
    return false;
11111
0
      break;
11112
11113
0
    case NOTE_INFO_MODULE:
11114
0
    case NOTE_INFO_MODULE64:
11115
      /* Make a ".module/xxxxxxxx" section.  */
11116
0
      if (type == NOTE_INFO_MODULE)
11117
0
  {
11118
    /* module_info.base_address */
11119
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11120
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11121
    /* module_info.module_name_size */
11122
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11123
0
  }
11124
0
      else /* NOTE_INFO_MODULE64 */
11125
0
  {
11126
    /* module_info.base_address */
11127
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11128
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11129
    /* module_info.module_name_size */
11130
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11131
0
  }
11132
11133
0
      len = strlen (buf) + 1;
11134
0
      name = bfd_alloc (abfd, len);
11135
0
      if (name == NULL)
11136
0
  return false;
11137
11138
0
      memcpy (name, buf, len);
11139
11140
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11141
11142
0
      if (sect == NULL)
11143
0
  return false;
11144
11145
0
      if (note->descsz < 12 + name_size)
11146
0
  {
11147
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11148
0
        " is too small to contain a name of size %u"),
11149
0
            abfd, note->descsz, name_size);
11150
0
    return true;
11151
0
  }
11152
11153
0
      sect->size = note->descsz;
11154
0
      sect->filepos = note->descpos;
11155
0
      sect->alignment_power = 2;
11156
0
      break;
11157
11158
0
    default:
11159
0
      return true;
11160
0
    }
11161
11162
0
  return true;
11163
0
}
11164
11165
static bool
11166
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11167
3.93k
{
11168
3.93k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11169
11170
  /* Short cut for LINUX notes.  */
11171
3.93k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11172
88
      && streq (note->namedata, NOTE_NAME_LINUX))
11173
0
    {
11174
0
      switch (note->type)
11175
0
  {
11176
0
  case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note);
11177
0
  case NT_ARC_V2:   return elfcore_grok_arc_v2 (abfd, note);
11178
0
  case NT_ARM_FPMR: return elfcore_grok_aarch_fpmr (abfd, note);
11179
0
  case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note);
11180
0
  case NT_ARM_POE: return elfcore_grok_aarch_poe (abfd, note);
11181
0
  case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note);
11182
0
  case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note);
11183
0
  case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note);
11184
0
  case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note);
11185
0
  case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note);
11186
0
  case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
11187
0
  case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note);
11188
0
  case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note);
11189
0
  case NT_ARM_ZA:   return elfcore_grok_aarch_za (abfd, note);
11190
0
  case NT_ARM_ZT:   return elfcore_grok_aarch_zt (abfd, note);
11191
0
  case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note);
11192
0
  case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note);
11193
0
  case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note);
11194
0
  case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note);
11195
0
  case NT_PPC_DMR: return elfcore_grok_ppc_dmr (abfd, note);
11196
0
  case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note);
11197
0
  case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note);
11198
0
  case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note);
11199
0
  case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note);
11200
0
  case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note);
11201
0
  case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note);
11202
0
  case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note);
11203
0
  case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note);
11204
0
  case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note);
11205
0
  case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note);
11206
0
  case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note);
11207
0
  case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note);
11208
0
  case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note);
11209
0
  case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note);
11210
0
  case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note);
11211
0
  case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note);
11212
0
  case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note);
11213
0
  case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note);
11214
0
  case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note);
11215
0
  case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note);
11216
0
  case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
11217
0
  case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note);
11218
0
  case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
11219
0
  case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note);
11220
0
  case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note);
11221
0
  case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note);
11222
0
  case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note);
11223
0
  case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note);
11224
0
  case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note);
11225
0
  case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note);
11226
0
  case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note);
11227
0
  case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
11228
0
  default: break;
11229
0
  }
11230
0
    }
11231
  
11232
3.93k
  switch (note->type)
11233
3.93k
    {
11234
2.92k
    default:
11235
2.92k
      return true;
11236
11237
417
    case NT_PRSTATUS:
11238
417
      if (bed->elf_backend_grok_prstatus)
11239
273
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11240
0
    return true;
11241
417
#if defined (HAVE_PRSTATUS_T)
11242
417
      return elfcore_grok_prstatus (abfd, note);
11243
#else
11244
      return true;
11245
#endif
11246
11247
#if defined (HAVE_PSTATUS_T)
11248
    case NT_PSTATUS:
11249
      return elfcore_grok_pstatus (abfd, note);
11250
#endif
11251
11252
#if defined (HAVE_LWPSTATUS_T)
11253
    case NT_LWPSTATUS:
11254
      return elfcore_grok_lwpstatus (abfd, note);
11255
#endif
11256
11257
222
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11258
222
      return elfcore_grok_prfpreg (abfd, note);
11259
11260
47
    case NT_WIN32PSTATUS:
11261
47
      return elfcore_grok_win32pstatus (abfd, note);
11262
11263
19
    case NT_GDB_TDESC:
11264
19
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11265
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11266
19
      else
11267
19
  return true;
11268
11269
65
    case NT_RISCV_CSR:
11270
65
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11271
0
  return elfcore_grok_riscv_csr (abfd, note);
11272
65
      else
11273
65
  return true;
11274
11275
155
    case NT_PRPSINFO:
11276
175
    case NT_PSINFO:
11277
175
      if (bed->elf_backend_grok_psinfo)
11278
103
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11279
0
    return true;
11280
175
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11281
175
      return elfcore_grok_psinfo (abfd, note);
11282
#else
11283
      return true;
11284
#endif
11285
11286
58
    case NT_AUXV:
11287
58
      return elfcore_make_auxv_note_section (abfd, note, 0);
11288
11289
0
    case NT_FILE:
11290
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11291
0
                note);
11292
11293
0
    case NT_SIGINFO:
11294
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11295
0
                note);
11296
3.93k
    }
11297
3.93k
}
11298
11299
static bool
11300
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11301
447
{
11302
447
  struct bfd_build_id* build_id;
11303
11304
447
  if (note->descsz == 0)
11305
37
    return false;
11306
11307
410
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11308
410
  if (build_id == NULL)
11309
0
    return false;
11310
11311
410
  build_id->size = note->descsz;
11312
410
  memcpy (build_id->data, note->descdata, note->descsz);
11313
410
  abfd->build_id = build_id;
11314
11315
410
  return true;
11316
410
}
11317
11318
static bool
11319
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11320
1.80k
{
11321
1.80k
  switch (note->type)
11322
1.80k
    {
11323
468
    default:
11324
468
      return true;
11325
11326
887
    case NT_GNU_PROPERTY_TYPE_0:
11327
887
      return _bfd_elf_parse_gnu_properties (abfd, note);
11328
11329
447
    case NT_GNU_BUILD_ID:
11330
447
      return elfobj_grok_gnu_build_id (abfd, note);
11331
1.80k
    }
11332
1.80k
}
11333
11334
static bool
11335
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11336
0
{
11337
0
  struct sdt_note *cur = bfd_alloc (abfd, sizeof (*cur) + note->descsz);
11338
11339
0
  if (cur == NULL)
11340
0
    return false;
11341
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11342
0
  cur->size = note->descsz;
11343
0
  memcpy (cur->data, note->descdata, note->descsz);
11344
11345
0
  elf_tdata (abfd)->sdt_note_head = cur;
11346
11347
0
  return true;
11348
0
}
11349
11350
static bool
11351
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11352
0
{
11353
0
  switch (note->type)
11354
0
    {
11355
0
    case NT_STAPSDT:
11356
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11357
11358
0
    default:
11359
0
      return true;
11360
0
    }
11361
0
}
11362
11363
static bool
11364
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11365
2
{
11366
2
  size_t offset;
11367
11368
2
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11369
2
    {
11370
0
    case ELFCLASS32:
11371
0
      if (note->descsz < 108)
11372
0
  return false;
11373
0
      break;
11374
11375
2
    case ELFCLASS64:
11376
2
      if (note->descsz < 120)
11377
1
  return false;
11378
1
      break;
11379
11380
1
    default:
11381
0
      return false;
11382
2
    }
11383
11384
  /* Check for version 1 in pr_version.  */
11385
1
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11386
1
    return false;
11387
11388
0
  offset = 4;
11389
11390
  /* Skip over pr_psinfosz. */
11391
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11392
0
    offset += 4;
11393
0
  else
11394
0
    {
11395
0
      offset += 4;  /* Padding before pr_psinfosz. */
11396
0
      offset += 8;
11397
0
    }
11398
11399
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11400
0
  elf_tdata (abfd)->core->program
11401
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11402
0
  offset += 17;
11403
11404
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11405
0
  elf_tdata (abfd)->core->command
11406
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11407
0
  offset += 81;
11408
11409
  /* Padding before pr_pid.  */
11410
0
  offset += 2;
11411
11412
  /* The pr_pid field was added in version "1a".  */
11413
0
  if (note->descsz < offset + 4)
11414
0
    return true;
11415
11416
0
  elf_tdata (abfd)->core->pid
11417
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11418
11419
0
  return true;
11420
0
}
11421
11422
static bool
11423
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11424
5
{
11425
5
  size_t offset;
11426
5
  size_t size;
11427
5
  size_t min_size;
11428
11429
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11430
     Also compute minimum size of this note.  */
11431
5
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11432
5
    {
11433
0
    case ELFCLASS32:
11434
0
      offset = 4 + 4;
11435
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11436
0
      break;
11437
11438
5
    case ELFCLASS64:
11439
5
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11440
5
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11441
5
      break;
11442
11443
0
    default:
11444
0
      return false;
11445
5
    }
11446
11447
5
  if (note->descsz < min_size)
11448
5
    return false;
11449
11450
  /* Check for version 1 in pr_version.  */
11451
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11452
0
    return false;
11453
11454
  /* Extract size of pr_reg from pr_gregsetsz.  */
11455
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11456
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11457
0
    {
11458
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11459
0
      offset += 4 * 2;
11460
0
    }
11461
0
  else
11462
0
    {
11463
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11464
0
      offset += 8 * 2;
11465
0
    }
11466
11467
  /* Skip over pr_osreldate.  */
11468
0
  offset += 4;
11469
11470
  /* Read signal from pr_cursig.  */
11471
0
  if (elf_tdata (abfd)->core->signal == 0)
11472
0
    elf_tdata (abfd)->core->signal
11473
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11474
0
  offset += 4;
11475
11476
  /* Read TID from pr_pid.  */
11477
0
  elf_tdata (abfd)->core->lwpid
11478
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11479
0
  offset += 4;
11480
11481
  /* Padding before pr_reg.  */
11482
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11483
0
    offset += 4;
11484
11485
  /* Make sure that there is enough data remaining in the note.  */
11486
0
  if ((note->descsz - offset) < size)
11487
0
    return false;
11488
11489
  /* Make a ".reg/999" section and a ".reg" section.  */
11490
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
11491
0
            size, note->descpos + offset);
11492
0
}
11493
11494
static bool
11495
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11496
40
{
11497
40
  elf_backend_data *bed = get_elf_backend_data (abfd);
11498
11499
40
  switch (note->type)
11500
40
    {
11501
5
    case NT_PRSTATUS:
11502
5
      if (bed->elf_backend_grok_freebsd_prstatus)
11503
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11504
0
    return true;
11505
5
      return elfcore_grok_freebsd_prstatus (abfd, note);
11506
11507
8
    case NT_FPREGSET:
11508
8
      return elfcore_grok_prfpreg (abfd, note);
11509
11510
2
    case NT_PRPSINFO:
11511
2
      return elfcore_grok_freebsd_psinfo (abfd, note);
11512
11513
0
    case NT_FREEBSD_THRMISC:
11514
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11515
11516
0
    case NT_FREEBSD_PROCSTAT_PROC:
11517
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11518
0
                note);
11519
11520
0
    case NT_FREEBSD_PROCSTAT_FILES:
11521
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11522
0
                note);
11523
11524
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11525
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11526
0
                note);
11527
11528
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11529
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11530
11531
0
    case NT_FREEBSD_X86_SEGBASES:
11532
0
      return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
11533
11534
0
    case NT_X86_XSTATE:
11535
0
      return elfcore_grok_xstatereg (abfd, note);
11536
11537
0
    case NT_FREEBSD_PTLWPINFO:
11538
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11539
0
                note);
11540
11541
0
    case NT_ARM_TLS:
11542
0
      return elfcore_grok_aarch_tls (abfd, note);
11543
11544
0
    case NT_ARM_VFP:
11545
0
      return elfcore_grok_arm_vfp (abfd, note);
11546
11547
25
    default:
11548
25
      return true;
11549
40
    }
11550
40
}
11551
11552
static bool
11553
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11554
18
{
11555
18
  char *cp;
11556
11557
18
  cp = strchr (note->namedata, '@');
11558
18
  if (cp != NULL)
11559
0
    {
11560
0
      *lwpidp = atoi(cp + 1);
11561
0
      return true;
11562
0
    }
11563
18
  return false;
11564
18
}
11565
11566
static bool
11567
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11568
0
{
11569
0
  if (note->descsz <= 0x7c + 31)
11570
0
    return false;
11571
11572
  /* Signal number at offset 0x08. */
11573
0
  elf_tdata (abfd)->core->signal
11574
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11575
11576
  /* Process ID at offset 0x50. */
11577
0
  elf_tdata (abfd)->core->pid
11578
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11579
11580
  /* Command name at 0x7c (max 32 bytes, including nul). */
11581
0
  elf_tdata (abfd)->core->command
11582
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11583
11584
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11585
0
            note);
11586
0
}
11587
11588
static bool
11589
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11590
18
{
11591
18
  int lwp;
11592
11593
18
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11594
0
    elf_tdata (abfd)->core->lwpid = lwp;
11595
11596
18
  switch (note->type)
11597
18
    {
11598
0
    case NT_NETBSDCORE_PROCINFO:
11599
      /* NetBSD-specific core "procinfo".  Note that we expect to
11600
   find this note before any of the others, which is fine,
11601
   since the kernel writes this note out first when it
11602
   creates a core file.  */
11603
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11604
0
    case NT_NETBSDCORE_AUXV:
11605
      /* NetBSD-specific Elf Auxiliary Vector data. */
11606
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11607
0
    case NT_NETBSDCORE_LWPSTATUS:
11608
0
      return elfcore_make_note_pseudosection (abfd,
11609
0
                ".note.netbsdcore.lwpstatus",
11610
0
                note);
11611
18
    default:
11612
18
      break;
11613
18
    }
11614
11615
  /* As of March 2020 there are no other machine-independent notes
11616
     defined for NetBSD core files.  If the note type is less
11617
     than the start of the machine-dependent note types, we don't
11618
     understand it.  */
11619
11620
18
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11621
0
    return true;
11622
11623
11624
18
  switch (bfd_get_arch (abfd))
11625
18
    {
11626
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11627
   PT_GETFPREGS == mach+2.  */
11628
11629
0
    case bfd_arch_aarch64:
11630
0
    case bfd_arch_alpha:
11631
0
    case bfd_arch_sparc:
11632
0
      switch (note->type)
11633
0
  {
11634
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11635
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11636
11637
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11638
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11639
11640
0
  default:
11641
0
    return true;
11642
0
  }
11643
11644
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11645
   There's also old PT___GETREGS40 == mach + 1 for old reg
11646
   structure which lacks GBR.  */
11647
11648
0
    case bfd_arch_sh:
11649
0
      switch (note->type)
11650
0
  {
11651
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11652
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11653
11654
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11655
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11656
11657
0
  default:
11658
0
    return true;
11659
0
  }
11660
11661
      /* On all other arch's, PT_GETREGS == mach+1 and
11662
   PT_GETFPREGS == mach+3.  */
11663
11664
18
    default:
11665
18
      switch (note->type)
11666
18
  {
11667
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11668
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11669
11670
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11671
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11672
11673
18
  default:
11674
18
    return true;
11675
18
  }
11676
18
    }
11677
    /* NOTREACHED */
11678
18
}
11679
11680
static bool
11681
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11682
0
{
11683
0
  if (note->descsz <= 0x48 + 31)
11684
0
    return false;
11685
11686
  /* Signal number at offset 0x08. */
11687
0
  elf_tdata (abfd)->core->signal
11688
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11689
11690
  /* Process ID at offset 0x20. */
11691
0
  elf_tdata (abfd)->core->pid
11692
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11693
11694
  /* Command name at 0x48 (max 32 bytes, including nul). */
11695
0
  elf_tdata (abfd)->core->command
11696
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11697
11698
0
  return true;
11699
0
}
11700
11701
/* Processes Solaris's process status note.
11702
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11703
   pid_off ~ offsetof(prstatus_t, pr_pid)
11704
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11705
   gregset_size ~ sizeof(gregset_t)
11706
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11707
11708
static bool
11709
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11710
             int pid_off, int lwpid_off, size_t gregset_size,
11711
             size_t gregset_offset)
11712
0
{
11713
0
  asection *sect = NULL;
11714
0
  elf_tdata (abfd)->core->signal
11715
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11716
0
  elf_tdata (abfd)->core->pid
11717
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11718
0
  elf_tdata (abfd)->core->lwpid
11719
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11720
11721
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11722
0
  if (sect != NULL)
11723
0
    sect->size = gregset_size;
11724
11725
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11726
0
            note->descpos + gregset_offset);
11727
0
}
11728
11729
/* Gets program and arguments from a core.
11730
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11731
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11732
11733
static bool
11734
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11735
        int prog_off, int comm_off)
11736
0
{
11737
0
  elf_tdata (abfd)->core->program
11738
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11739
0
  elf_tdata (abfd)->core->command
11740
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11741
11742
0
  return true;
11743
0
}
11744
11745
/* Processes Solaris's LWP status note.
11746
   gregset_size ~ sizeof(gregset_t)
11747
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11748
   fpregset_size ~ sizeof(fpregset_t)
11749
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11750
11751
static bool
11752
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11753
        size_t gregset_size, int gregset_off,
11754
        size_t fpregset_size, int fpregset_off)
11755
0
{
11756
0
  asection *sect = NULL;
11757
0
  char reg2_section_name[16] = { 0 };
11758
11759
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
11760
0
       elf_tdata (abfd)->core->lwpid);
11761
11762
  /* offsetof(lwpstatus_t, pr_lwpid) */
11763
0
  elf_tdata (abfd)->core->lwpid
11764
0
    = bfd_get_32 (abfd, note->descdata + 4);
11765
  /* offsetof(lwpstatus_t, pr_cursig) */
11766
0
  elf_tdata (abfd)->core->signal
11767
0
    = bfd_get_16 (abfd, note->descdata + 12);
11768
11769
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11770
0
  if (sect != NULL)
11771
0
    sect->size = gregset_size;
11772
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11773
0
               note->descpos + gregset_off))
11774
0
    return false;
11775
11776
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11777
0
  if (sect != NULL)
11778
0
    {
11779
0
      sect->size = fpregset_size;
11780
0
      sect->filepos = note->descpos + fpregset_off;
11781
0
      sect->alignment_power = 2;
11782
0
    }
11783
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
11784
0
               note->descpos + fpregset_off))
11785
0
    return false;
11786
11787
0
  return true;
11788
0
}
11789
11790
static bool
11791
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11792
40
{
11793
40
  if (note == NULL)
11794
0
    return false;
11795
11796
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11797
     by the size of the descsz which matches the sizeof()
11798
     the type appropriate for that note type (e.g., prstatus_t for
11799
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11800
     on Solaris. The core file bitness may differ from the bitness of
11801
     gdb itself, so fixed values are used instead of sizeof().
11802
     Appropriate fixed offsets are also used to obtain data from
11803
     the note.  */
11804
11805
40
  switch ((int) note->type)
11806
40
    {
11807
23
    case SOLARIS_NT_PRSTATUS:
11808
23
      switch (note->descsz)
11809
23
  {
11810
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11811
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11812
0
                 136, 216, 308, 152, 356);
11813
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11814
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11815
0
                 264, 360, 520, 304, 600);
11816
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11817
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11818
0
                 136, 216, 308, 76, 356);
11819
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11820
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11821
0
                 264, 360, 520, 224, 600);
11822
23
  default:
11823
23
    return true;
11824
23
  }
11825
11826
0
    case SOLARIS_NT_PSINFO:
11827
3
    case SOLARIS_NT_PRPSINFO:
11828
3
      switch (note->descsz)
11829
3
  {
11830
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11831
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11832
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11833
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11834
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11835
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11836
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11837
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11838
3
  default:
11839
3
    return true;
11840
3
  }
11841
11842
0
    case SOLARIS_NT_LWPSTATUS:
11843
0
      switch (note->descsz)
11844
0
  {
11845
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11846
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11847
0
            152, 344, 400, 496);
11848
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11849
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11850
0
            304, 544, 544, 848);
11851
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11852
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11853
0
            76, 344, 380, 420);
11854
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11855
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11856
0
            224, 544, 528, 768);
11857
0
  default:
11858
0
    return true;
11859
0
  }
11860
11861
0
    case SOLARIS_NT_LWPSINFO:
11862
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11863
0
      if (note->descsz == 128 || note->descsz == 152)
11864
0
  elf_tdata (abfd)->core->lwpid =
11865
0
    bfd_get_32 (abfd, note->descdata + 4);
11866
0
      break;
11867
11868
14
    default:
11869
14
      break;
11870
40
    }
11871
11872
14
  return true;
11873
40
}
11874
11875
/* For name starting with "CORE" this may be either a Solaris
11876
   core file or a gdb-generated core file.  Do Solaris-specific
11877
   processing on selected note types first with
11878
   elfcore_grok_solaris_note(), then process the note
11879
   in elfcore_grok_note().  */
11880
11881
static bool
11882
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11883
40
{
11884
40
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11885
0
    return false;
11886
11887
40
  return elfcore_grok_note (abfd, note);
11888
40
}
11889
11890
static bool
11891
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11892
1
{
11893
1
  if (note->type == NT_OPENBSD_PROCINFO)
11894
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11895
11896
1
  if (note->type == NT_OPENBSD_REGS)
11897
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11898
11899
1
  if (note->type == NT_OPENBSD_FPREGS)
11900
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11901
11902
1
  if (note->type == NT_OPENBSD_XFPREGS)
11903
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11904
11905
1
  if (note->type == NT_OPENBSD_AUXV)
11906
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11907
11908
1
  if (note->type == NT_OPENBSD_WCOOKIE)
11909
0
    {
11910
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11911
0
                 SEC_HAS_CONTENTS);
11912
11913
0
      if (sect == NULL)
11914
0
  return false;
11915
0
      sect->size = note->descsz;
11916
0
      sect->filepos = note->descpos;
11917
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11918
11919
0
      return true;
11920
0
    }
11921
11922
1
  return true;
11923
1
}
11924
11925
static bool
11926
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11927
0
{
11928
0
  void *ddata = note->descdata;
11929
0
  char buf[100];
11930
0
  char *name;
11931
0
  asection *sect;
11932
0
  short sig;
11933
0
  unsigned flags;
11934
11935
0
  if (note->descsz < 16)
11936
0
    return false;
11937
11938
  /* nto_procfs_status 'pid' field is at offset 0.  */
11939
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11940
11941
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11942
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11943
11944
  /* nto_procfs_status 'flags' field is at offset 8.  */
11945
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11946
11947
  /* nto_procfs_status 'what' field is at offset 14.  */
11948
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11949
0
    {
11950
0
      elf_tdata (abfd)->core->signal = sig;
11951
0
      elf_tdata (abfd)->core->lwpid = *tid;
11952
0
    }
11953
11954
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11955
     do not come from signals so we make sure we set the current
11956
     thread just in case.  */
11957
0
  if (flags & 0x00000080)
11958
0
    elf_tdata (abfd)->core->lwpid = *tid;
11959
11960
  /* Make a ".qnx_core_status/%d" section.  */
11961
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11962
11963
0
  name = bfd_alloc (abfd, strlen (buf) + 1);
11964
0
  if (name == NULL)
11965
0
    return false;
11966
0
  strcpy (name, buf);
11967
11968
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11969
0
  if (sect == NULL)
11970
0
    return false;
11971
11972
0
  sect->size    = note->descsz;
11973
0
  sect->filepos   = note->descpos;
11974
0
  sect->alignment_power = 2;
11975
11976
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11977
0
}
11978
11979
static bool
11980
elfcore_grok_nto_regs (bfd *abfd,
11981
           Elf_Internal_Note *note,
11982
           long tid,
11983
           char *base)
11984
0
{
11985
0
  char buf[100];
11986
0
  char *name;
11987
0
  asection *sect;
11988
11989
  /* Make a "(base)/%d" section.  */
11990
0
  sprintf (buf, "%s/%ld", base, tid);
11991
11992
0
  name = bfd_alloc (abfd, strlen (buf) + 1);
11993
0
  if (name == NULL)
11994
0
    return false;
11995
0
  strcpy (name, buf);
11996
11997
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11998
0
  if (sect == NULL)
11999
0
    return false;
12000
12001
0
  sect->size    = note->descsz;
12002
0
  sect->filepos   = note->descpos;
12003
0
  sect->alignment_power = 2;
12004
12005
  /* This is the current thread.  */
12006
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12007
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12008
12009
0
  return true;
12010
0
}
12011
12012
static bool
12013
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12014
102
{
12015
  /* Every GREG section has a STATUS section before it.  Store the
12016
     tid from the previous call to pass down to the next gregs
12017
     function.  */
12018
102
  static long tid = 1;
12019
12020
102
  switch (note->type)
12021
102
    {
12022
0
    case QNT_CORE_INFO:
12023
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12024
0
    case QNT_CORE_STATUS:
12025
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12026
0
    case QNT_CORE_GREG:
12027
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
12028
0
    case QNT_CORE_FPREG:
12029
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
12030
102
    default:
12031
102
      return true;
12032
102
    }
12033
102
}
12034
12035
static bool
12036
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12037
18
{
12038
18
  char *name;
12039
18
  asection *sect;
12040
18
  size_t len;
12041
12042
  /* Use note name as section name.  */
12043
18
  len = note->namesz;
12044
18
  name = bfd_alloc (abfd, len);
12045
18
  if (name == NULL)
12046
0
    return false;
12047
18
  memcpy (name, note->namedata, len);
12048
18
  name[len - 1] = '\0';
12049
12050
18
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12051
18
  if (sect == NULL)
12052
0
    return false;
12053
12054
18
  sect->size    = note->descsz;
12055
18
  sect->filepos   = note->descpos;
12056
18
  sect->alignment_power = 1;
12057
12058
18
  return true;
12059
18
}
12060
12061
/* Function: elfcore_write_note
12062
12063
   Inputs:
12064
     buffer to hold note, and current size of buffer
12065
     name of note
12066
     type of note
12067
     data for note
12068
     size of data for note
12069
12070
   Writes note to end of buffer.  ELF64 notes are written exactly as
12071
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12072
   that they ought to have 8-byte namesz and descsz field, and have
12073
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12074
12075
   Return:
12076
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12077
12078
char *
12079
elfcore_write_note (bfd *abfd,
12080
        char *buf,
12081
        int *bufsiz,
12082
        const char *name,
12083
        int type,
12084
        const void *input,
12085
        int size)
12086
0
{
12087
0
  Elf_External_Note *xnp;
12088
0
  size_t namesz;
12089
0
  size_t newspace;
12090
0
  char *dest;
12091
12092
0
  namesz = 0;
12093
0
  if (name != NULL)
12094
0
    namesz = strlen (name) + 1;
12095
12096
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12097
12098
0
  buf = realloc (buf, *bufsiz + newspace);
12099
0
  if (buf == NULL)
12100
0
    return buf;
12101
0
  dest = buf + *bufsiz;
12102
0
  *bufsiz += newspace;
12103
0
  xnp = (Elf_External_Note *) dest;
12104
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12105
0
  H_PUT_32 (abfd, size, xnp->descsz);
12106
0
  H_PUT_32 (abfd, type, xnp->type);
12107
0
  dest = xnp->name;
12108
0
  if (name != NULL)
12109
0
    {
12110
0
      memcpy (dest, name, namesz);
12111
0
      dest += namesz;
12112
0
      while (namesz & 3)
12113
0
  {
12114
0
    *dest++ = '\0';
12115
0
    ++namesz;
12116
0
  }
12117
0
    }
12118
0
  memcpy (dest, input, size);
12119
0
  dest += size;
12120
0
  while (size & 3)
12121
0
    {
12122
0
      *dest++ = '\0';
12123
0
      ++size;
12124
0
    }
12125
0
  return buf;
12126
0
}
12127
12128
/* gcc-8 warns (*) on all the strncpy calls in this function about
12129
   possible string truncation.  The "truncation" is not a bug.  We
12130
   have an external representation of structs with fields that are not
12131
   necessarily NULL terminated and corresponding internal
12132
   representation fields that are one larger so that they can always
12133
   be NULL terminated.
12134
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12135
   diagnostics inside functions, giving a hard error if you try to use
12136
   the finer control available with later versions.
12137
   gcc prior to 4.2 warns about diagnostic push and pop.
12138
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12139
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12140
   (*) Depending on your system header files!  */
12141
#if GCC_VERSION >= 8000
12142
# pragma GCC diagnostic push
12143
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12144
#endif
12145
char *
12146
elfcore_write_prpsinfo (bfd  *abfd,
12147
      char *buf,
12148
      int  *bufsiz,
12149
      const char *fname,
12150
      const char *psargs)
12151
0
{
12152
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12153
12154
0
  if (bed->elf_backend_write_core_note != NULL)
12155
0
    {
12156
0
      char *ret;
12157
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12158
0
             NT_PRPSINFO, fname, psargs);
12159
0
      if (ret != NULL)
12160
0
  return ret;
12161
0
    }
12162
12163
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12164
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12165
0
  if (bed->s->elfclass == ELFCLASS32)
12166
0
    {
12167
#  if defined (HAVE_PSINFO32_T)
12168
      psinfo32_t data;
12169
      int note_type = NT_PSINFO;
12170
#  else
12171
0
      prpsinfo32_t data;
12172
0
      int note_type = NT_PRPSINFO;
12173
0
#  endif
12174
12175
0
      memset (&data, 0, sizeof (data));
12176
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12177
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12178
0
      return elfcore_write_note (abfd, buf, bufsiz,
12179
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12180
0
    }
12181
0
  else
12182
0
# endif
12183
0
    {
12184
# if defined (HAVE_PSINFO_T)
12185
      psinfo_t data;
12186
      int note_type = NT_PSINFO;
12187
# else
12188
0
      prpsinfo_t data;
12189
0
      int note_type = NT_PRPSINFO;
12190
0
# endif
12191
12192
0
      memset (&data, 0, sizeof (data));
12193
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12194
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12195
0
      return elfcore_write_note (abfd, buf, bufsiz,
12196
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12197
0
    }
12198
0
#endif  /* PSINFO_T or PRPSINFO_T */
12199
12200
0
  free (buf);
12201
0
  return NULL;
12202
0
}
12203
#if GCC_VERSION >= 8000
12204
# pragma GCC diagnostic pop
12205
#endif
12206
12207
char *
12208
elfcore_write_linux_prpsinfo32
12209
  (bfd *abfd, char *buf, int *bufsiz,
12210
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12211
0
{
12212
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12213
0
    {
12214
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12215
12216
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12217
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12218
0
         &data, sizeof (data));
12219
0
    }
12220
0
  else
12221
0
    {
12222
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12223
12224
0
      swap_linux_prpsinfo32_ugid32_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
}
12229
12230
char *
12231
elfcore_write_linux_prpsinfo64
12232
  (bfd *abfd, char *buf, int *bufsiz,
12233
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12234
0
{
12235
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12236
0
    {
12237
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12238
12239
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12240
0
      return elfcore_write_note (abfd, buf, bufsiz,
12241
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12242
0
    }
12243
0
  else
12244
0
    {
12245
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12246
12247
0
      swap_linux_prpsinfo64_ugid32_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
}
12252
12253
char *
12254
elfcore_write_prstatus (bfd *abfd,
12255
      char *buf,
12256
      int *bufsiz,
12257
      long pid,
12258
      int cursig,
12259
      const void *gregs)
12260
0
{
12261
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12262
12263
0
  if (bed->elf_backend_write_core_note != NULL)
12264
0
    {
12265
0
      char *ret;
12266
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12267
0
             NT_PRSTATUS,
12268
0
             pid, cursig, gregs);
12269
0
      if (ret != NULL)
12270
0
  return ret;
12271
0
    }
12272
12273
0
#if defined (HAVE_PRSTATUS_T)
12274
0
#if defined (HAVE_PRSTATUS32_T)
12275
0
  if (bed->s->elfclass == ELFCLASS32)
12276
0
    {
12277
0
      prstatus32_t prstat;
12278
12279
0
      memset (&prstat, 0, sizeof (prstat));
12280
0
      prstat.pr_pid = pid;
12281
0
      prstat.pr_cursig = cursig;
12282
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12283
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12284
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12285
0
    }
12286
0
  else
12287
0
#endif
12288
0
    {
12289
0
      prstatus_t prstat;
12290
12291
0
      memset (&prstat, 0, sizeof (prstat));
12292
0
      prstat.pr_pid = pid;
12293
0
      prstat.pr_cursig = cursig;
12294
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12295
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12296
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12297
0
    }
12298
0
#endif /* HAVE_PRSTATUS_T */
12299
12300
0
  free (buf);
12301
0
  return NULL;
12302
0
}
12303
12304
#if defined (HAVE_LWPSTATUS_T)
12305
char *
12306
elfcore_write_lwpstatus (bfd *abfd,
12307
       char *buf,
12308
       int *bufsiz,
12309
       long pid,
12310
       int cursig,
12311
       const void *gregs)
12312
{
12313
  lwpstatus_t lwpstat;
12314
12315
  memset (&lwpstat, 0, sizeof (lwpstat));
12316
  lwpstat.pr_lwpid  = pid >> 16;
12317
  lwpstat.pr_cursig = cursig;
12318
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12319
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12320
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12321
#if !defined(gregs)
12322
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12323
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12324
#else
12325
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12326
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12327
#endif
12328
#endif
12329
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12330
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12331
}
12332
#endif /* HAVE_LWPSTATUS_T */
12333
12334
#if defined (HAVE_PSTATUS_T)
12335
char *
12336
elfcore_write_pstatus (bfd *abfd,
12337
           char *buf,
12338
           int *bufsiz,
12339
           long pid,
12340
           int cursig ATTRIBUTE_UNUSED,
12341
           const void *gregs ATTRIBUTE_UNUSED)
12342
{
12343
#if defined (HAVE_PSTATUS32_T)
12344
  elf_backend_data *bed = get_elf_backend_data (abfd);
12345
12346
  if (bed->s->elfclass == ELFCLASS32)
12347
    {
12348
      pstatus32_t pstat;
12349
12350
      memset (&pstat, 0, sizeof (pstat));
12351
      pstat.pr_pid = pid & 0xffff;
12352
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12353
        NT_PSTATUS, &pstat, sizeof (pstat));
12354
      return buf;
12355
    }
12356
  else
12357
#endif
12358
    {
12359
      pstatus_t pstat;
12360
12361
      memset (&pstat, 0, sizeof (pstat));
12362
      pstat.pr_pid = pid & 0xffff;
12363
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12364
        NT_PSTATUS, &pstat, sizeof (pstat));
12365
      return buf;
12366
    }
12367
}
12368
#endif /* HAVE_PSTATUS_T */
12369
12370
char *
12371
elfcore_write_prfpreg (bfd *abfd,
12372
           char *buf,
12373
           int *bufsiz,
12374
           const void *fpregs,
12375
           int size)
12376
0
{
12377
0
  return elfcore_write_note (abfd, buf, bufsiz,
12378
0
           NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
12379
0
}
12380
12381
char *
12382
elfcore_write_prxfpreg (bfd *abfd,
12383
      char *buf,
12384
      int *bufsiz,
12385
      const void *xfpregs,
12386
      int size)
12387
0
{
12388
0
  return elfcore_write_note (abfd, buf, bufsiz,
12389
0
           NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
12390
0
}
12391
12392
char *
12393
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12394
       const void *xfpregs, int size)
12395
0
{
12396
0
  char *note_name;
12397
12398
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12399
0
    note_name = NOTE_NAME_FREEBSD;
12400
0
  else
12401
0
    note_name = NOTE_NAME_LINUX;
12402
12403
0
  return elfcore_write_note (abfd, buf, bufsiz,
12404
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12405
0
}
12406
12407
char *
12408
elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
12409
          const void *xsave_layout, int size)
12410
0
{
12411
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
12412
0
           NT_X86_XSAVE_LAYOUT, xsave_layout, size);
12413
0
}
12414
12415
static char *
12416
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12417
          const void *ssp, int size)
12418
0
{
12419
0
  return elfcore_write_note (abfd, buf, bufsiz,
12420
0
           NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
12421
0
}
12422
12423
char *
12424
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12425
          const void *regs, int size)
12426
0
{
12427
0
  return elfcore_write_note (abfd, buf, bufsiz,
12428
0
           NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
12429
0
           regs, size);
12430
0
}
12431
12432
char *
12433
elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
12434
          const void *regs, int size)
12435
0
{
12436
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
12437
0
           regs, size);
12438
0
}
12439
12440
char *
12441
elfcore_write_ppc_vmx (bfd *abfd,
12442
           char *buf,
12443
           int *bufsiz,
12444
           const void *ppc_vmx,
12445
           int size)
12446
0
{
12447
0
  return elfcore_write_note (abfd, buf, bufsiz,
12448
0
           NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
12449
0
}
12450
12451
char *
12452
elfcore_write_ppc_vsx (bfd *abfd,
12453
           char *buf,
12454
           int *bufsiz,
12455
           const void *ppc_vsx,
12456
           int size)
12457
0
{
12458
0
  return elfcore_write_note (abfd, buf, bufsiz,
12459
0
           NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
12460
0
}
12461
12462
char *
12463
elfcore_write_ppc_tar (bfd *abfd,
12464
           char *buf,
12465
           int *bufsiz,
12466
           const void *ppc_tar,
12467
           int size)
12468
0
{
12469
0
  return elfcore_write_note (abfd, buf, bufsiz,
12470
0
           NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
12471
0
}
12472
12473
char *
12474
elfcore_write_ppc_ppr (bfd *abfd,
12475
           char *buf,
12476
           int *bufsiz,
12477
           const void *ppc_ppr,
12478
           int size)
12479
0
{
12480
0
  return elfcore_write_note (abfd, buf, bufsiz,
12481
0
           NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
12482
0
}
12483
12484
char *
12485
elfcore_write_ppc_dscr (bfd *abfd,
12486
      char *buf,
12487
      int *bufsiz,
12488
      const void *ppc_dscr,
12489
      int size)
12490
0
{
12491
0
  return elfcore_write_note (abfd, buf, bufsiz,
12492
0
           NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
12493
0
}
12494
12495
char *
12496
elfcore_write_ppc_ebb (bfd *abfd,
12497
           char *buf,
12498
           int *bufsiz,
12499
           const void *ppc_ebb,
12500
           int size)
12501
0
{
12502
0
  return elfcore_write_note (abfd, buf, bufsiz,
12503
0
           NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
12504
0
}
12505
12506
char *
12507
elfcore_write_ppc_pmu (bfd *abfd,
12508
           char *buf,
12509
           int *bufsiz,
12510
           const void *ppc_pmu,
12511
           int size)
12512
0
{
12513
0
  return elfcore_write_note (abfd, buf, bufsiz,
12514
0
           NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
12515
0
}
12516
12517
char *
12518
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12519
         char *buf,
12520
         int *bufsiz,
12521
         const void *ppc_tm_cgpr,
12522
         int size)
12523
0
{
12524
0
  return elfcore_write_note (abfd, buf, bufsiz,
12525
0
           NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
12526
0
           ppc_tm_cgpr, size);
12527
0
}
12528
12529
char *
12530
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12531
         char *buf,
12532
         int *bufsiz,
12533
         const void *ppc_tm_cfpr,
12534
         int size)
12535
0
{
12536
0
  return elfcore_write_note (abfd, buf, bufsiz,
12537
0
           NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
12538
0
           ppc_tm_cfpr, size);
12539
0
}
12540
12541
char *
12542
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12543
         char *buf,
12544
         int *bufsiz,
12545
         const void *ppc_tm_cvmx,
12546
         int size)
12547
0
{
12548
0
  return elfcore_write_note (abfd, buf, bufsiz,
12549
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
12550
0
           ppc_tm_cvmx, size);
12551
0
}
12552
12553
char *
12554
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12555
         char *buf,
12556
         int *bufsiz,
12557
         const void *ppc_tm_cvsx,
12558
         int size)
12559
0
{
12560
0
  return elfcore_write_note (abfd, buf, bufsiz,
12561
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
12562
0
           ppc_tm_cvsx, size);
12563
0
}
12564
12565
char *
12566
elfcore_write_ppc_tm_spr (bfd *abfd,
12567
        char *buf,
12568
        int *bufsiz,
12569
        const void *ppc_tm_spr,
12570
        int size)
12571
0
{
12572
0
  return elfcore_write_note (abfd, buf, bufsiz,
12573
0
           NOTE_NAME_LINUX, NT_PPC_TM_SPR,
12574
0
           ppc_tm_spr, size);
12575
0
}
12576
12577
char *
12578
elfcore_write_ppc_tm_ctar (bfd *abfd,
12579
         char *buf,
12580
         int *bufsiz,
12581
         const void *ppc_tm_ctar,
12582
         int size)
12583
0
{
12584
0
  return elfcore_write_note (abfd, buf, bufsiz,
12585
0
           NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
12586
0
           ppc_tm_ctar, size);
12587
0
}
12588
12589
char *
12590
elfcore_write_ppc_tm_cppr (bfd *abfd,
12591
         char *buf,
12592
         int *bufsiz,
12593
         const void *ppc_tm_cppr,
12594
         int size)
12595
0
{
12596
0
  return elfcore_write_note (abfd, buf, bufsiz,
12597
0
           NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
12598
0
           ppc_tm_cppr, size);
12599
0
}
12600
12601
char *
12602
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12603
          char *buf,
12604
          int *bufsiz,
12605
          const void *ppc_tm_cdscr,
12606
          int size)
12607
0
{
12608
0
  return elfcore_write_note (abfd, buf, bufsiz,
12609
0
           NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
12610
0
           ppc_tm_cdscr, size);
12611
0
}
12612
12613
char *
12614
elfcore_write_ppc_dmr (bfd *abfd,
12615
           char *buf,
12616
           int *bufsiz,
12617
           const void *ppc_dmr,
12618
           int size)
12619
0
{
12620
0
  return elfcore_write_note (abfd, buf, bufsiz,
12621
0
           NOTE_NAME_LINUX, NT_PPC_DMR, ppc_dmr, size);
12622
0
}
12623
12624
static char *
12625
elfcore_write_s390_high_gprs (bfd *abfd,
12626
            char *buf,
12627
            int *bufsiz,
12628
            const void *s390_high_gprs,
12629
            int size)
12630
0
{
12631
0
  return elfcore_write_note (abfd, buf, bufsiz,
12632
0
           NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
12633
0
           s390_high_gprs, size);
12634
0
}
12635
12636
char *
12637
elfcore_write_s390_timer (bfd *abfd,
12638
        char *buf,
12639
        int *bufsiz,
12640
        const void *s390_timer,
12641
        int size)
12642
0
{
12643
0
  return elfcore_write_note (abfd, buf, bufsiz,
12644
0
           NOTE_NAME_LINUX, NT_S390_TIMER,
12645
0
           s390_timer, size);
12646
0
}
12647
12648
char *
12649
elfcore_write_s390_todcmp (bfd *abfd,
12650
         char *buf,
12651
         int *bufsiz,
12652
         const void *s390_todcmp,
12653
         int size)
12654
0
{
12655
0
  return elfcore_write_note (abfd, buf, bufsiz,
12656
0
           NOTE_NAME_LINUX, NT_S390_TODCMP,
12657
0
           s390_todcmp, size);
12658
0
}
12659
12660
char *
12661
elfcore_write_s390_todpreg (bfd *abfd,
12662
          char *buf,
12663
          int *bufsiz,
12664
          const void *s390_todpreg,
12665
          int size)
12666
0
{
12667
0
  return elfcore_write_note (abfd, buf, bufsiz,
12668
0
           NOTE_NAME_LINUX, NT_S390_TODPREG,
12669
0
           s390_todpreg, size);
12670
0
}
12671
12672
char *
12673
elfcore_write_s390_ctrs (bfd *abfd,
12674
       char *buf,
12675
       int *bufsiz,
12676
       const void *s390_ctrs,
12677
       int size)
12678
0
{
12679
0
  return elfcore_write_note (abfd, buf, bufsiz,
12680
0
           NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
12681
0
}
12682
12683
char *
12684
elfcore_write_s390_prefix (bfd *abfd,
12685
         char *buf,
12686
         int *bufsiz,
12687
         const void *s390_prefix,
12688
         int size)
12689
0
{
12690
0
  return elfcore_write_note (abfd, buf, bufsiz,
12691
0
           NOTE_NAME_LINUX, NT_S390_PREFIX,
12692
0
           s390_prefix, size);
12693
0
}
12694
12695
char *
12696
elfcore_write_s390_last_break (bfd *abfd,
12697
             char *buf,
12698
             int *bufsiz,
12699
             const void *s390_last_break,
12700
             int size)
12701
0
{
12702
0
  return elfcore_write_note (abfd, buf, bufsiz,
12703
0
           NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
12704
0
           s390_last_break, size);
12705
0
}
12706
12707
char *
12708
elfcore_write_s390_system_call (bfd *abfd,
12709
        char *buf,
12710
        int *bufsiz,
12711
        const void *s390_system_call,
12712
        int size)
12713
0
{
12714
0
  return elfcore_write_note (abfd, buf, bufsiz,
12715
0
           NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
12716
0
           s390_system_call, size);
12717
0
}
12718
12719
char *
12720
elfcore_write_s390_tdb (bfd *abfd,
12721
      char *buf,
12722
      int *bufsiz,
12723
      const void *s390_tdb,
12724
      int size)
12725
0
{
12726
0
  return elfcore_write_note (abfd, buf, bufsiz,
12727
0
           NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
12728
0
}
12729
12730
char *
12731
elfcore_write_s390_vxrs_low (bfd *abfd,
12732
           char *buf,
12733
           int *bufsiz,
12734
           const void *s390_vxrs_low,
12735
           int size)
12736
0
{
12737
0
  return elfcore_write_note (abfd, buf, bufsiz,
12738
0
           NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
12739
0
           s390_vxrs_low, size);
12740
0
}
12741
12742
char *
12743
elfcore_write_s390_vxrs_high (bfd *abfd,
12744
           char *buf,
12745
           int *bufsiz,
12746
           const void *s390_vxrs_high,
12747
           int size)
12748
0
{
12749
0
  return elfcore_write_note (abfd, buf, bufsiz,
12750
0
           NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
12751
0
           s390_vxrs_high, size);
12752
0
}
12753
12754
char *
12755
elfcore_write_s390_gs_cb (bfd *abfd,
12756
        char *buf,
12757
        int *bufsiz,
12758
        const void *s390_gs_cb,
12759
        int size)
12760
0
{
12761
0
  return elfcore_write_note (abfd, buf, bufsiz,
12762
0
           NOTE_NAME_LINUX, NT_S390_GS_CB,
12763
0
           s390_gs_cb, size);
12764
0
}
12765
12766
char *
12767
elfcore_write_s390_gs_bc (bfd *abfd,
12768
        char *buf,
12769
        int *bufsiz,
12770
        const void *s390_gs_bc,
12771
        int size)
12772
0
{
12773
0
  return elfcore_write_note (abfd, buf, bufsiz,
12774
0
           NOTE_NAME_LINUX, NT_S390_GS_BC,
12775
0
           s390_gs_bc, size);
12776
0
}
12777
12778
char *
12779
elfcore_write_arm_vfp (bfd *abfd,
12780
           char *buf,
12781
           int *bufsiz,
12782
           const void *arm_vfp,
12783
           int size)
12784
0
{
12785
0
  return elfcore_write_note (abfd, buf, bufsiz,
12786
0
           NOTE_NAME_LINUX, NT_ARM_VFP,
12787
0
           arm_vfp, size);
12788
0
}
12789
12790
char *
12791
elfcore_write_aarch_tls (bfd *abfd,
12792
           char *buf,
12793
           int *bufsiz,
12794
           const void *aarch_tls,
12795
           int size)
12796
0
{
12797
0
  return elfcore_write_note (abfd, buf, bufsiz,
12798
0
           NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
12799
0
}
12800
12801
char *
12802
elfcore_write_aarch_hw_break (bfd *abfd,
12803
          char *buf,
12804
          int *bufsiz,
12805
          const void *aarch_hw_break,
12806
          int size)
12807
0
{
12808
0
  return elfcore_write_note (abfd, buf, bufsiz,
12809
0
           NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
12810
0
           aarch_hw_break, size);
12811
0
}
12812
12813
char *
12814
elfcore_write_aarch_hw_watch (bfd *abfd,
12815
          char *buf,
12816
          int *bufsiz,
12817
          const void *aarch_hw_watch,
12818
          int size)
12819
0
{
12820
0
  return elfcore_write_note (abfd, buf, bufsiz,
12821
0
           NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
12822
0
           aarch_hw_watch, size);
12823
0
}
12824
12825
char *
12826
elfcore_write_aarch_sve (bfd *abfd,
12827
       char *buf,
12828
       int *bufsiz,
12829
       const void *aarch_sve,
12830
       int size)
12831
0
{
12832
0
  return elfcore_write_note (abfd, buf, bufsiz,
12833
0
           NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
12834
0
}
12835
12836
char *
12837
elfcore_write_aarch_pauth (bfd *abfd,
12838
         char *buf,
12839
         int *bufsiz,
12840
         const void *aarch_pauth,
12841
         int size)
12842
0
{
12843
0
  return elfcore_write_note (abfd, buf, bufsiz,
12844
0
           NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
12845
0
           aarch_pauth, size);
12846
0
}
12847
12848
char *
12849
elfcore_write_aarch_mte (bfd *abfd,
12850
       char *buf,
12851
       int *bufsiz,
12852
       const void *aarch_mte,
12853
       int size)
12854
0
{
12855
0
  return elfcore_write_note (abfd, buf, bufsiz,
12856
0
           NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
12857
0
           aarch_mte, size);
12858
0
}
12859
12860
char *
12861
elfcore_write_aarch_ssve (bfd *abfd,
12862
        char *buf,
12863
        int *bufsiz,
12864
        const void *aarch_ssve,
12865
        int size)
12866
0
{
12867
0
  return elfcore_write_note (abfd, buf, bufsiz,
12868
0
           NOTE_NAME_LINUX, NT_ARM_SSVE,
12869
0
           aarch_ssve, size);
12870
0
}
12871
12872
char *
12873
elfcore_write_aarch_za (bfd *abfd,
12874
      char *buf,
12875
      int *bufsiz,
12876
      const void *aarch_za,
12877
      int size)
12878
0
{
12879
0
  return elfcore_write_note (abfd, buf, bufsiz,
12880
0
           NOTE_NAME_LINUX, NT_ARM_ZA,
12881
0
           aarch_za, size);
12882
0
}
12883
12884
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
12885
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12886
   written into.  Return a pointer to the new start of the note buffer, to
12887
   replace BUF which may no longer be valid.  */
12888
12889
char *
12890
elfcore_write_aarch_zt (bfd *abfd,
12891
      char *buf,
12892
      int *bufsiz,
12893
      const void *aarch_zt,
12894
      int size)
12895
0
{
12896
0
  return elfcore_write_note (abfd, buf, bufsiz,
12897
0
           NOTE_NAME_LINUX, NT_ARM_ZT,
12898
0
           aarch_zt, size);
12899
0
}
12900
12901
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
12902
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12903
   written into.  Return a pointer to the new start of the note buffer, to
12904
   replace BUF which may no longer be valid.  */
12905
12906
static char *
12907
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
12908
       const void *aarch_gcs, int size)
12909
0
{
12910
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
12911
0
           aarch_gcs, size);
12912
0
}
12913
12914
/* Write the buffer of POE register values in AARCH_POE (length SIZE) into
12915
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12916
   written into.  Return a pointer to the new start of the note buffer, to
12917
   replace BUF which may no longer be valid.  */
12918
12919
static char *
12920
elfcore_write_aarch_poe (bfd *abfd, char *buf, int *bufsiz,
12921
       const void *aarch_poe, int size)
12922
0
{
12923
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_POE,
12924
0
           aarch_poe, size);
12925
0
}
12926
12927
/* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
12928
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12929
   written into.  Return a pointer to the new start of the note buffer, to
12930
   replace BUF which may no longer be valid.  */
12931
12932
char *
12933
elfcore_write_aarch_fpmr (bfd *abfd,
12934
      char *buf,
12935
      int *bufsiz,
12936
      const void *aarch_fpmr,
12937
      int size)
12938
0
{
12939
0
  return elfcore_write_note (abfd, buf, bufsiz,
12940
0
           NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
12941
0
}
12942
12943
char *
12944
elfcore_write_arc_v2 (bfd *abfd,
12945
          char *buf,
12946
          int *bufsiz,
12947
          const void *arc_v2,
12948
          int size)
12949
0
{
12950
0
  return elfcore_write_note (abfd, buf, bufsiz,
12951
0
           NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
12952
0
}
12953
12954
char *
12955
elfcore_write_loongarch_cpucfg (bfd *abfd,
12956
        char *buf,
12957
        int *bufsiz,
12958
        const void *loongarch_cpucfg,
12959
        int size)
12960
0
{
12961
0
  return elfcore_write_note (abfd, buf, bufsiz,
12962
0
           NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
12963
0
           loongarch_cpucfg, size);
12964
0
}
12965
12966
char *
12967
elfcore_write_loongarch_lbt (bfd *abfd,
12968
           char *buf,
12969
           int *bufsiz,
12970
           const void *loongarch_lbt,
12971
           int size)
12972
0
{
12973
0
  return elfcore_write_note (abfd, buf, bufsiz,
12974
0
           NOTE_NAME_LINUX, NT_LARCH_LBT,
12975
0
           loongarch_lbt, size);
12976
0
}
12977
12978
char *
12979
elfcore_write_loongarch_lsx (bfd *abfd,
12980
           char *buf,
12981
           int *bufsiz,
12982
           const void *loongarch_lsx,
12983
           int size)
12984
0
{
12985
0
  return elfcore_write_note (abfd, buf, bufsiz,
12986
0
           NOTE_NAME_LINUX, NT_LARCH_LSX,
12987
0
           loongarch_lsx, size);
12988
0
}
12989
12990
char *
12991
elfcore_write_loongarch_lasx (bfd *abfd,
12992
            char *buf,
12993
            int *bufsiz,
12994
            const void *loongarch_lasx,
12995
            int size)
12996
0
{
12997
0
  return elfcore_write_note (abfd, buf, bufsiz,
12998
0
           NOTE_NAME_LINUX, NT_LARCH_LASX,
12999
0
           loongarch_lasx, size);
13000
0
}
13001
13002
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13003
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13004
   written into.  Return a pointer to the new start of the note buffer, to
13005
   replace BUF which may no longer be valid.  */
13006
13007
char *
13008
elfcore_write_riscv_csr (bfd *abfd,
13009
       char *buf,
13010
       int *bufsiz,
13011
       const void *csrs,
13012
       int size)
13013
0
{
13014
0
  return elfcore_write_note (abfd, buf, bufsiz,
13015
0
           NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
13016
0
}
13017
13018
/* Write the target description (a string) pointed to by TDESC, length
13019
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13020
   note is being written into.  Return a pointer to the new start of the
13021
   note buffer, to replace BUF which may no longer be valid.  */
13022
13023
char *
13024
elfcore_write_gdb_tdesc (bfd *abfd,
13025
       char *buf,
13026
       int *bufsiz,
13027
       const void *tdesc,
13028
       int size)
13029
0
{
13030
0
  return elfcore_write_note (abfd, buf, bufsiz,
13031
0
           NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
13032
0
}
13033
13034
char *
13035
elfcore_write_register_note (bfd *abfd,
13036
           char *buf,
13037
           int *bufsiz,
13038
           const char *section,
13039
           const void *data,
13040
           int size)
13041
0
{
13042
0
  static const struct
13043
0
    {
13044
0
      const char * section_name;
13045
0
      char *       (*writer) (bfd *, char *, int *, const void *, int);
13046
0
    }
13047
0
  note_writers [] =
13048
0
    {
13049
0
      { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
13050
0
      { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
13051
0
      { NOTE_PSEUDO_SECTION_AARCH_POE,        elfcore_write_aarch_poe},
13052
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
13053
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
13054
0
      { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
13055
0
      { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
13056
0
      { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
13057
0
      { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
13058
0
      { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
13059
0
      { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
13060
0
      { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
13061
0
      { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
13062
0
      { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
13063
0
      { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
13064
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
13065
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
13066
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
13067
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
13068
0
      { NOTE_PSEUDO_SECTION_PPC_DMR,          elfcore_write_ppc_dmr},
13069
0
      { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
13070
0
      { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
13071
0
      { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
13072
0
      { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
13073
0
      { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
13074
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
13075
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
13076
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
13077
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
13078
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
13079
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
13080
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
13081
0
      { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
13082
0
      { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
13083
0
      { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
13084
0
      { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
13085
0
      { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
13086
0
      { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
13087
0
      { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
13088
0
      { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
13089
0
      { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
13090
0
      { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
13091
0
      { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
13092
0
      { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
13093
0
      { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
13094
0
      { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
13095
0
      { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
13096
0
      { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
13097
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
13098
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
13099
0
      { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
13100
0
      { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
13101
0
      { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
13102
0
      { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
13103
0
      { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
13104
0
      { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
13105
0
    };
13106
13107
0
  int i;
13108
13109
0
  for (i = ARRAY_SIZE (note_writers); i--;)
13110
0
    if (streq (section, note_writers[i].section_name))
13111
0
      return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
13112
13113
0
  return NULL;
13114
0
}
13115
13116
char *
13117
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13118
       const void *buf, int bufsiz)
13119
0
{
13120
0
  return elfcore_write_note (obfd, note_data, note_size,
13121
0
           NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
13122
0
}
13123
13124
static bool
13125
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13126
     size_t align)
13127
10.1k
{
13128
10.1k
  char *p;
13129
13130
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13131
     gABI specifies that PT_NOTE alignment should be aligned to 4
13132
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13133
     align is less than 4, we use 4 byte alignment.   */
13134
10.1k
  if (align < 4)
13135
3.53k
    align = 4;
13136
10.1k
  if (align != 4 && align != 8)
13137
2.59k
    return false;
13138
13139
7.59k
  p = buf;
13140
20.2k
  while (p < buf + size)
13141
19.2k
    {
13142
19.2k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13143
19.2k
      Elf_Internal_Note in;
13144
13145
19.2k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13146
946
  return false;
13147
13148
18.3k
      in.type = H_GET_32 (abfd, xnp->type);
13149
13150
18.3k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13151
18.3k
      in.namedata = xnp->name;
13152
18.3k
      if (in.namesz > buf - in.namedata + size)
13153
3.12k
  return false;
13154
13155
15.1k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13156
15.1k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13157
15.1k
      in.descpos = offset + (in.descdata - buf);
13158
15.1k
      if (in.descsz != 0
13159
6.93k
    && (in.descdata >= buf + size
13160
6.70k
        || in.descsz > buf - in.descdata + size))
13161
1.84k
  return false;
13162
13163
13.3k
      switch (bfd_get_format (abfd))
13164
13.3k
  {
13165
0
  default:
13166
0
    return true;
13167
13168
4.45k
  case bfd_core:
13169
4.45k
    {
13170
35.6k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13171
4.45k
      static const struct
13172
4.45k
      {
13173
4.45k
        const char * string;
13174
4.45k
        size_t len;
13175
4.45k
        bool (*func) (bfd *, Elf_Internal_Note *);
13176
4.45k
      }
13177
4.45k
      grokers[] =
13178
4.45k
      {
13179
4.45k
        GROKER_ELEMENT ("", elfcore_grok_note),
13180
4.45k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13181
4.45k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13182
4.45k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13183
4.45k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13184
4.45k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13185
4.45k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13186
4.45k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13187
4.45k
      };
13188
4.45k
#undef GROKER_ELEMENT
13189
4.45k
      int i;
13190
13191
32.6k
      for (i = ARRAY_SIZE (grokers); i--;)
13192
32.6k
        {
13193
32.6k
    if (in.namesz >= grokers[i].len
13194
13.7k
        && strncmp (in.namedata, grokers[i].string,
13195
13.7k
        grokers[i].len) == 0)
13196
4.45k
      {
13197
4.45k
        if (! grokers[i].func (abfd, & in))
13198
202
          return false;
13199
4.24k
        break;
13200
4.45k
      }
13201
32.6k
        }
13202
4.24k
      break;
13203
4.45k
    }
13204
13205
8.90k
  case bfd_object:
13206
8.90k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13207
1.46k
      {
13208
1.46k
        if (! elfobj_grok_gnu_note (abfd, &in))
13209
466
    return false;
13210
1.46k
      }
13211
7.44k
    else if (in.namesz == sizeof "stapsdt"
13212
459
       && strcmp (in.namedata, "stapsdt") == 0)
13213
0
      {
13214
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13215
0
    return false;
13216
0
      }
13217
8.43k
    break;
13218
13.3k
  }
13219
13220
12.6k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13221
12.6k
    }
13222
13223
1.00k
  return true;
13224
7.59k
}
13225
13226
bool
13227
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13228
         size_t align)
13229
5.00k
{
13230
5.00k
  char *buf;
13231
13232
5.00k
  if (size == 0 || (size + 1) == 0)
13233
843
    return true;
13234
13235
4.16k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13236
1.19k
    return false;
13237
13238
2.96k
  buf = _bfd_malloc_and_read (abfd, size + 1, size);
13239
2.96k
  if (buf == NULL)
13240
1.22k
    return false;
13241
13242
  /* PR 17512: file: ec08f814
13243
     0-termintate the buffer so that string searches will not overflow.  */
13244
1.74k
  buf[size] = 0;
13245
13246
1.74k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13247
1.64k
    {
13248
1.64k
      free (buf);
13249
1.64k
      return false;
13250
1.64k
    }
13251
13252
98
  free (buf);
13253
98
  return true;
13254
1.74k
}
13255

13256
/* Providing external access to the ELF program header table.  */
13257
13258
/* Return an upper bound on the number of bytes required to store a
13259
   copy of ABFD's program header table entries.  Return -1 if an error
13260
   occurs; bfd_get_error will return an appropriate code.  */
13261
13262
long
13263
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13264
0
{
13265
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13266
0
    {
13267
0
      bfd_set_error (bfd_error_wrong_format);
13268
0
      return -1;
13269
0
    }
13270
13271
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13272
0
}
13273
13274
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13275
   will be stored as an array of Elf_Internal_Phdr structures, as
13276
   defined in include/elf/internal.h.  To find out how large the
13277
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13278
13279
   Return the number of program header table entries read, or -1 if an
13280
   error occurs; bfd_get_error will return an appropriate code.  */
13281
13282
int
13283
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13284
0
{
13285
0
  int num_phdrs;
13286
13287
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13288
0
    {
13289
0
      bfd_set_error (bfd_error_wrong_format);
13290
0
      return -1;
13291
0
    }
13292
13293
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13294
0
  if (num_phdrs != 0)
13295
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13296
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13297
13298
0
  return num_phdrs;
13299
0
}
13300
13301
enum elf_reloc_type_class
13302
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13303
         const asection *rel_sec ATTRIBUTE_UNUSED,
13304
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13305
0
{
13306
0
  return reloc_class_normal;
13307
0
}
13308
13309
/* For RELA architectures, return the relocation value for a
13310
   relocation against a local symbol.  */
13311
13312
bfd_vma
13313
_bfd_elf_rela_local_sym (bfd *abfd,
13314
       Elf_Internal_Sym *sym,
13315
       asection **psec,
13316
       Elf_Internal_Rela *rel)
13317
0
{
13318
0
  asection *sec = *psec;
13319
0
  bfd_vma relocation;
13320
13321
0
  relocation = (sec->output_section->vma
13322
0
    + sec->output_offset
13323
0
    + sym->st_value);
13324
0
  if ((sec->flags & SEC_MERGE)
13325
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13326
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13327
0
    {
13328
0
      rel->r_addend =
13329
0
  _bfd_merged_section_offset (abfd, psec,
13330
0
            sym->st_value + rel->r_addend);
13331
0
      if (sec != *psec)
13332
0
  {
13333
    /* If we have changed the section, and our original section is
13334
       marked with SEC_EXCLUDE, it means that the original
13335
       SEC_MERGE section has been completely subsumed in some
13336
       other SEC_MERGE section.  In this case, we need to leave
13337
       some info around for --emit-relocs.  */
13338
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13339
0
      sec->kept_section = *psec;
13340
0
    sec = *psec;
13341
0
  }
13342
0
      rel->r_addend -= relocation;
13343
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13344
0
    }
13345
0
  return relocation;
13346
0
}
13347
13348
bfd_vma
13349
_bfd_elf_rel_local_sym (bfd *abfd,
13350
      Elf_Internal_Sym *sym,
13351
      asection **psec,
13352
      bfd_vma addend)
13353
0
{
13354
0
  asection *sec = *psec;
13355
13356
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13357
0
    return sym->st_value + addend;
13358
13359
0
  return _bfd_merged_section_offset (abfd, psec,
13360
0
             sym->st_value + addend);
13361
0
}
13362
13363
/* Adjust an address within a section.  Given OFFSET within SEC, return
13364
   the new offset within the section, based upon changes made to the
13365
   section.  Returns -1 if the offset is now invalid.
13366
   The offset (in abnd out) is in target sized bytes, however big a
13367
   byte may be.  */
13368
13369
bfd_vma
13370
_bfd_elf_section_offset (bfd *abfd,
13371
       struct bfd_link_info *info,
13372
       asection *sec,
13373
       bfd_vma offset)
13374
0
{
13375
0
  switch (sec->sec_info_type)
13376
0
    {
13377
0
    case SEC_INFO_TYPE_STABS:
13378
0
      return _bfd_stab_section_offset (sec, offset);
13379
13380
0
    case SEC_INFO_TYPE_EH_FRAME:
13381
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13382
13383
0
    case SEC_INFO_TYPE_SFRAME:
13384
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13385
13386
0
    default:
13387
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13388
0
  {
13389
    /* Reverse the offset.  */
13390
0
    elf_backend_data *bed = get_elf_backend_data (abfd);
13391
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13392
13393
    /* address_size and sec->size are in octets.  Convert
13394
       to bytes before subtracting the original offset.  */
13395
0
    offset = ((sec->size - address_size)
13396
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13397
0
  }
13398
0
      return offset;
13399
0
    }
13400
0
}
13401

13402
long
13403
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13404
             long symcount ATTRIBUTE_UNUSED,
13405
             asymbol **syms ATTRIBUTE_UNUSED,
13406
             long dynsymcount,
13407
             asymbol **dynsyms,
13408
             asymbol **ret)
13409
15.0k
{
13410
15.0k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13411
15.0k
  asection *relplt;
13412
15.0k
  asymbol *s;
13413
15.0k
  const char *relplt_name;
13414
15.0k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13415
15.0k
  arelent *p;
13416
15.0k
  long count, i, n;
13417
15.0k
  size_t size;
13418
15.0k
  Elf_Internal_Shdr *hdr;
13419
15.0k
  char *names;
13420
15.0k
  asection *plt;
13421
13422
15.0k
  *ret = NULL;
13423
13424
15.0k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13425
15.0k
    return 0;
13426
13427
61
  if (dynsymcount <= 0)
13428
55
    return 0;
13429
13430
6
  if (!bed->plt_sym_val)
13431
3
    return 0;
13432
13433
3
  relplt_name = bed->relplt_name;
13434
3
  if (relplt_name == NULL)
13435
3
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13436
3
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13437
3
  if (relplt == NULL)
13438
3
    return 0;
13439
13440
0
  hdr = &elf_section_data (relplt)->this_hdr;
13441
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13442
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13443
0
    return 0;
13444
13445
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13446
0
  if (plt == NULL)
13447
0
    return 0;
13448
13449
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13450
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13451
0
    return -1;
13452
13453
0
  count = NUM_SHDR_ENTRIES (hdr);
13454
0
  size = count * sizeof (asymbol);
13455
0
  p = relplt->relocation;
13456
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13457
0
    {
13458
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13459
0
      if (p->addend != 0)
13460
0
  {
13461
0
#ifdef BFD64
13462
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13463
#else
13464
    size += sizeof ("+0x") - 1 + 8;
13465
#endif
13466
0
  }
13467
0
    }
13468
13469
0
  s = *ret = bfd_malloc (size);
13470
0
  if (s == NULL)
13471
0
    return -1;
13472
13473
0
  names = (char *) (s + count);
13474
0
  p = relplt->relocation;
13475
0
  n = 0;
13476
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13477
0
    {
13478
0
      size_t len;
13479
0
      bfd_vma addr;
13480
13481
0
      addr = bed->plt_sym_val (i, plt, p);
13482
0
      if (addr == (bfd_vma) -1)
13483
0
  continue;
13484
13485
0
      *s = **p->sym_ptr_ptr;
13486
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13487
   we are defining a symbol, ensure one of them is set.  */
13488
0
      if ((s->flags & BSF_LOCAL) == 0)
13489
0
  s->flags |= BSF_GLOBAL;
13490
0
      s->flags |= BSF_SYNTHETIC;
13491
0
      s->section = plt;
13492
0
      s->value = addr - plt->vma;
13493
0
      s->name = names;
13494
0
      s->udata.p = NULL;
13495
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13496
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13497
0
      names += len;
13498
0
      if (p->addend != 0)
13499
0
  {
13500
0
    char buf[30], *a;
13501
13502
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13503
0
    names += sizeof ("+0x") - 1;
13504
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13505
0
    for (a = buf; *a == '0'; ++a)
13506
0
      ;
13507
0
    len = strlen (a);
13508
0
    memcpy (names, a, len);
13509
0
    names += len;
13510
0
  }
13511
0
      memcpy (names, "@plt", sizeof ("@plt"));
13512
0
      names += sizeof ("@plt");
13513
0
      ++s, ++n;
13514
0
    }
13515
13516
0
  return n;
13517
0
}
13518
13519
bool
13520
_bfd_elf_final_write_processing (bfd *abfd)
13521
384
{
13522
384
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13523
384
  elf_backend_data *bed = get_elf_backend_data (abfd);
13524
13525
384
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13526
0
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13527
13528
384
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13529
374
      || bed->target_os == is_solaris)
13530
10
    {
13531
10
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13532
10
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13533
10
    }
13534
13535
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13536
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13537
     or STB_GNU_UNIQUE binding.  */
13538
384
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13539
0
    {
13540
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13541
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13542
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13543
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13544
0
  {
13545
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13546
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13547
0
          "and FreeBSD targets"));
13548
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13549
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13550
0
          "only by GNU and FreeBSD targets"));
13551
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13552
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13553
0
          "only by GNU and FreeBSD targets"));
13554
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13555
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13556
0
          "only by GNU and FreeBSD targets"));
13557
0
    bfd_set_error (bfd_error_sorry);
13558
0
    return false;
13559
0
  }
13560
0
    }
13561
384
  return true;
13562
384
}
13563
13564
13565
/* Return TRUE for ELF symbol types that represent functions.
13566
   This is the default version of this function, which is sufficient for
13567
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13568
13569
bool
13570
_bfd_elf_is_function_type (unsigned int type)
13571
0
{
13572
0
  return (type == STT_FUNC
13573
0
    || type == STT_GNU_IFUNC);
13574
0
}
13575
13576
/* If the ELF symbol SYM might be a function in SEC, return the
13577
   function size and set *CODE_OFF to the function's entry point,
13578
   otherwise return zero.  */
13579
13580
bfd_size_type
13581
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13582
           bfd_vma *code_off)
13583
952k
{
13584
952k
  bfd_size_type size;
13585
952k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13586
13587
952k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13588
952k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13589
726k
      || sym->section != sec)
13590
936k
    return 0;
13591
13592
15.8k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13593
13594
  /* In theory we should check that the symbol's type satisfies
13595
     _bfd_elf_is_function_type(), but there are some function-like
13596
     symbols which would fail this test.  (eg _start).  Instead
13597
     we check for hidden, local, notype symbols with zero size.
13598
     This type of symbol is generated by the annobin plugin for gcc
13599
     and clang, and should not be considered to be a function symbol.  */
13600
15.8k
  if (size == 0
13601
3.21k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13602
1.90k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13603
1.68k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13604
115
    return 0;
13605
13606
15.7k
  *code_off = sym->value;
13607
  /* Do not return 0 for the function's size.  */
13608
15.7k
  return size ? size : 1;
13609
15.8k
}
13610
13611
/* Set to non-zero to enable some debug messages.  */
13612
#define DEBUG_SECONDARY_RELOCS   0
13613
13614
/* An internal-to-the-bfd-library only section type
13615
   used to indicate a cached secondary reloc section.  */
13616
5.66k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13617
13618
/* Create a BFD section to hold a secondary reloc section.  */
13619
13620
bool
13621
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13622
               Elf_Internal_Shdr *hdr,
13623
               const char * name,
13624
               unsigned int shindex)
13625
2.00k
{
13626
  /* We only support RELA secondary relocs.  */
13627
2.00k
  if (hdr->sh_type != SHT_RELA)
13628
262
    return false;
13629
13630
#if DEBUG_SECONDARY_RELOCS
13631
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13632
#endif
13633
1.74k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13634
1.74k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13635
2.00k
}
13636
13637
/* Read in any secondary relocs associated with SEC.  */
13638
13639
bool
13640
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13641
          asection *  sec,
13642
          asymbol **  symbols,
13643
          bool dynamic)
13644
11.3k
{
13645
11.3k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13646
11.3k
  asection * relsec;
13647
11.3k
  bool result = true;
13648
11.3k
  bfd_vma (*r_sym) (bfd_vma);
13649
11.3k
  ufile_ptr filesize;
13650
13651
11.3k
#if BFD_DEFAULT_TARGET_SIZE > 32
13652
11.3k
  if (bfd_arch_bits_per_address (abfd) != 32)
13653
5.43k
    r_sym = elf64_r_sym;
13654
5.91k
  else
13655
5.91k
#endif
13656
5.91k
    r_sym = elf32_r_sym;
13657
13658
11.3k
  if (!elf_section_data (sec)->has_secondary_relocs)
13659
11.2k
    return true;
13660
13661
  /* Discover if there are any secondary reloc sections
13662
     associated with SEC.  */
13663
93
  filesize = bfd_get_file_size (abfd);
13664
2.03k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13665
1.94k
    {
13666
1.94k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13667
13668
1.94k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13669
112
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13670
93
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13671
93
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13672
93
  {
13673
93
    bfd_byte * native_relocs;
13674
93
    bfd_byte * native_reloc;
13675
93
    arelent * internal_relocs;
13676
93
    arelent * internal_reloc;
13677
93
    size_t i;
13678
93
    unsigned int entsize;
13679
93
    unsigned int symcount;
13680
93
    bfd_size_type reloc_count;
13681
93
    size_t amt;
13682
13683
93
    if (ebd->elf_info_to_howto == NULL)
13684
0
      return false;
13685
13686
#if DEBUG_SECONDARY_RELOCS
13687
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13688
       sec->name, relsec->name);
13689
#endif
13690
93
    entsize = hdr->sh_entsize;
13691
13692
93
    if (filesize != 0
13693
93
        && ((ufile_ptr) hdr->sh_offset > filesize
13694
80
      || hdr->sh_size > filesize - hdr->sh_offset))
13695
22
      {
13696
22
        bfd_set_error (bfd_error_file_truncated);
13697
22
        result = false;
13698
22
        continue;
13699
22
      }
13700
13701
71
    native_relocs = bfd_malloc (hdr->sh_size);
13702
71
    if (native_relocs == NULL)
13703
0
      {
13704
0
        result = false;
13705
0
        continue;
13706
0
      }
13707
13708
71
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13709
71
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13710
0
      {
13711
0
        free (native_relocs);
13712
0
        bfd_set_error (bfd_error_file_too_big);
13713
0
        result = false;
13714
0
        continue;
13715
0
      }
13716
13717
71
    internal_relocs = bfd_alloc (abfd, amt);
13718
71
    if (internal_relocs == NULL)
13719
0
      {
13720
0
        free (native_relocs);
13721
0
        result = false;
13722
0
        continue;
13723
0
      }
13724
13725
71
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13726
71
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13727
0
      {
13728
0
        free (native_relocs);
13729
        /* The internal_relocs will be freed when
13730
     the memory for the bfd is released.  */
13731
0
        result = false;
13732
0
        continue;
13733
0
      }
13734
13735
71
    if (dynamic)
13736
0
      symcount = bfd_get_dynamic_symcount (abfd);
13737
71
    else
13738
71
      symcount = bfd_get_symcount (abfd);
13739
13740
71
    for (i = 0, internal_reloc = internal_relocs,
13741
71
     native_reloc = native_relocs;
13742
578
         i < reloc_count;
13743
507
         i++, internal_reloc++, native_reloc += entsize)
13744
507
      {
13745
507
        bool res;
13746
507
        Elf_Internal_Rela rela;
13747
13748
507
        if (entsize == ebd->s->sizeof_rel)
13749
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13750
507
        else /* entsize == ebd->s->sizeof_rela */
13751
507
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13752
13753
        /* The address of an ELF reloc is section relative for an object
13754
     file, and absolute for an executable file or shared library.
13755
     The address of a normal BFD reloc is always section relative,
13756
     and the address of a dynamic reloc is absolute..  */
13757
507
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13758
434
    internal_reloc->address = rela.r_offset;
13759
73
        else
13760
73
    internal_reloc->address = rela.r_offset - sec->vma;
13761
13762
507
        if (r_sym (rela.r_info) == STN_UNDEF)
13763
111
    {
13764
      /* FIXME: This and the error case below mean that we
13765
         have a symbol on relocs that is not elf_symbol_type.  */
13766
111
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13767
111
    }
13768
396
        else if (r_sym (rela.r_info) > symcount)
13769
0
    {
13770
0
      _bfd_error_handler
13771
        /* xgettext:c-format */
13772
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13773
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13774
0
      bfd_set_error (bfd_error_bad_value);
13775
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13776
0
      result = false;
13777
0
    }
13778
396
        else
13779
396
    {
13780
396
      asymbol **ps;
13781
13782
396
      ps = symbols + r_sym (rela.r_info) - 1;
13783
396
      internal_reloc->sym_ptr_ptr = ps;
13784
      /* Make sure that this symbol is not removed by strip.  */
13785
396
      (*ps)->flags |= BSF_KEEP;
13786
396
    }
13787
13788
507
        internal_reloc->addend = rela.r_addend;
13789
13790
507
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13791
507
        if (! res || internal_reloc->howto == NULL)
13792
37
    {
13793
#if DEBUG_SECONDARY_RELOCS
13794
      fprintf (stderr,
13795
         "there is no howto associated with reloc %lx\n",
13796
         rela.r_info);
13797
#endif
13798
37
      result = false;
13799
37
    }
13800
507
      }
13801
13802
71
    free (native_relocs);
13803
    /* Store the internal relocs.  */
13804
71
    relsec->sec_info = internal_relocs;
13805
71
  }
13806
1.94k
    }
13807
13808
93
  return result;
13809
93
}
13810
13811
/* Set the ELF section header fields of an output secondary reloc section.  */
13812
13813
bool
13814
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13815
              bfd *obfd ATTRIBUTE_UNUSED,
13816
              const Elf_Internal_Shdr *isection,
13817
              Elf_Internal_Shdr *osection)
13818
37
{
13819
37
  asection * isec;
13820
37
  asection * osec;
13821
37
  struct bfd_elf_section_data * esd;
13822
13823
37
  if (isection == NULL)
13824
0
    return false;
13825
13826
37
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13827
37
    return true;
13828
13829
0
  isec = isection->bfd_section;
13830
0
  if (isec == NULL)
13831
0
    return false;
13832
13833
0
  osec = osection->bfd_section;
13834
0
  if (osec == NULL)
13835
0
    return false;
13836
13837
0
  BFD_ASSERT (osec->sec_info == NULL);
13838
0
  osec->sec_info = isec->sec_info;
13839
0
  osection->sh_type = SHT_RELA;
13840
0
  osection->sh_link = elf_onesymtab (obfd);
13841
0
  if (osection->sh_link == 0)
13842
0
    {
13843
      /* There is no symbol table - we are hosed...  */
13844
0
      _bfd_error_handler
13845
  /* xgettext:c-format */
13846
0
  (_("%pB(%pA): link section cannot be set"
13847
0
     " because the output file does not have a symbol table"),
13848
0
  obfd, osec);
13849
0
      bfd_set_error (bfd_error_bad_value);
13850
0
      return false;
13851
0
    }
13852
13853
  /* Find the output section that corresponds to the isection's
13854
     sh_info link.  */
13855
0
  if (isection->sh_info == 0
13856
0
      || isection->sh_info >= elf_numsections (ibfd))
13857
0
    {
13858
0
      _bfd_error_handler
13859
  /* xgettext:c-format */
13860
0
  (_("%pB(%pA): info section index is invalid"),
13861
0
  obfd, osec);
13862
0
      bfd_set_error (bfd_error_bad_value);
13863
0
      return false;
13864
0
    }
13865
13866
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
13867
13868
0
  if (isection == NULL
13869
0
      || isection->bfd_section == NULL
13870
0
      || isection->bfd_section->output_section == NULL)
13871
0
    {
13872
0
      _bfd_error_handler
13873
  /* xgettext:c-format */
13874
0
  (_("%pB(%pA): info section index cannot be set"
13875
0
     " because the section is not in the output"),
13876
0
  obfd, osec);
13877
0
      bfd_set_error (bfd_error_bad_value);
13878
0
      return false;
13879
0
    }
13880
13881
0
  esd = elf_section_data (isection->bfd_section->output_section);
13882
0
  BFD_ASSERT (esd != NULL);
13883
0
  osection->sh_info = esd->this_idx;
13884
0
  esd->has_secondary_relocs = true;
13885
#if DEBUG_SECONDARY_RELOCS
13886
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13887
     osec->name, osection->sh_link, osection->sh_info);
13888
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13889
     bfd_section_name (isection->bfd_section->output_section));
13890
#endif
13891
13892
0
  return true;
13893
0
}
13894
13895
/* Write out a secondary reloc section.
13896
13897
   FIXME: Currently this function can result in a serious performance penalty
13898
   for files with secondary relocs and lots of sections.  The proper way to
13899
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13900
   relocs together and then to have this function just walk that chain.  */
13901
13902
bool
13903
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13904
0
{
13905
0
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13906
0
  bfd_vma addr_offset;
13907
0
  asection * relsec;
13908
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13909
0
  bool result = true;
13910
13911
0
  if (sec == NULL)
13912
0
    return false;
13913
13914
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13915
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13916
0
    r_info = elf64_r_info;
13917
0
  else
13918
0
#endif
13919
0
    r_info = elf32_r_info;
13920
13921
  /* The address of an ELF reloc is section relative for an object
13922
     file, and absolute for an executable file or shared library.
13923
     The address of a BFD reloc is always section relative.  */
13924
0
  addr_offset = 0;
13925
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13926
0
    addr_offset = sec->vma;
13927
13928
  /* Discover if there are any secondary reloc sections
13929
     associated with SEC.  */
13930
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13931
0
    {
13932
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13933
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13934
13935
0
      if (hdr->sh_type == SHT_RELA
13936
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13937
0
  {
13938
0
    asymbol *    last_sym;
13939
0
    int          last_sym_idx;
13940
0
    size_t       reloc_count;
13941
0
    size_t       idx;
13942
0
    bfd_size_type entsize;
13943
0
    arelent *    src_irel;
13944
0
    bfd_byte *   dst_rela;
13945
13946
0
    if (hdr->contents != NULL)
13947
0
      {
13948
0
        _bfd_error_handler
13949
    /* xgettext:c-format */
13950
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13951
0
     abfd, relsec);
13952
0
        bfd_set_error (bfd_error_bad_value);
13953
0
        result = false;
13954
0
        continue;
13955
0
      }
13956
13957
0
    entsize = hdr->sh_entsize;
13958
0
    if (entsize == 0)
13959
0
      {
13960
0
        _bfd_error_handler
13961
    /* xgettext:c-format */
13962
0
    (_("%pB(%pA): error: secondary reloc section"
13963
0
       " has zero sized entries"),
13964
0
     abfd, relsec);
13965
0
        bfd_set_error (bfd_error_bad_value);
13966
0
        result = false;
13967
0
        continue;
13968
0
      }
13969
0
    else if (entsize != ebd->s->sizeof_rel
13970
0
       && entsize != ebd->s->sizeof_rela)
13971
0
      {
13972
0
        _bfd_error_handler
13973
    /* xgettext:c-format */
13974
0
    (_("%pB(%pA): error: secondary reloc section"
13975
0
       " has non-standard sized entries"),
13976
0
     abfd, relsec);
13977
0
        bfd_set_error (bfd_error_bad_value);
13978
0
        result = false;
13979
0
        continue;
13980
0
      }
13981
13982
0
    reloc_count = hdr->sh_size / entsize;
13983
0
    hdr->sh_size = entsize * reloc_count;
13984
0
    if (reloc_count == 0)
13985
0
      {
13986
0
        _bfd_error_handler
13987
    /* xgettext:c-format */
13988
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13989
0
     abfd, relsec);
13990
0
        bfd_set_error (bfd_error_bad_value);
13991
0
        result = false;
13992
0
        continue;
13993
0
      }
13994
13995
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13996
0
    if (hdr->contents == NULL)
13997
0
      continue;
13998
0
    relsec->alloced = 1;
13999
14000
#if DEBUG_SECONDARY_RELOCS
14001
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14002
       reloc_count, sec->name, relsec->name);
14003
#endif
14004
0
    last_sym = NULL;
14005
0
    last_sym_idx = 0;
14006
0
    dst_rela = hdr->contents;
14007
0
    src_irel = sec->sec_info;
14008
0
    if (src_irel == NULL)
14009
0
      {
14010
0
        _bfd_error_handler
14011
    /* xgettext:c-format */
14012
0
    (_("%pB(%pA): error: internal relocs missing"
14013
0
       " for secondary reloc section"),
14014
0
     abfd, relsec);
14015
0
        bfd_set_error (bfd_error_bad_value);
14016
0
        result = false;
14017
0
        continue;
14018
0
      }
14019
14020
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14021
0
      {
14022
0
        Elf_Internal_Rela src_rela;
14023
0
        arelent *ptr;
14024
0
        asymbol *sym;
14025
0
        int n;
14026
14027
0
        ptr = src_irel + idx;
14028
0
        if (ptr == NULL)
14029
0
    {
14030
0
      _bfd_error_handler
14031
        /* xgettext:c-format */
14032
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14033
0
         abfd, relsec, idx);
14034
0
      bfd_set_error (bfd_error_bad_value);
14035
0
      result = false;
14036
0
      break;
14037
0
    }
14038
14039
0
        if (ptr->sym_ptr_ptr == NULL)
14040
0
    {
14041
      /* FIXME: Is this an error ? */
14042
0
      n = 0;
14043
0
    }
14044
0
        else
14045
0
    {
14046
0
      sym = *ptr->sym_ptr_ptr;
14047
14048
0
      if (sym == last_sym)
14049
0
        n = last_sym_idx;
14050
0
      else
14051
0
        {
14052
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14053
0
          if (n < 0)
14054
0
      {
14055
0
        _bfd_error_handler
14056
          /* xgettext:c-format */
14057
0
          (_("%pB(%pA): error: secondary reloc %zu"
14058
0
             " references a missing symbol"),
14059
0
           abfd, relsec, idx);
14060
0
        bfd_set_error (bfd_error_bad_value);
14061
0
        result = false;
14062
0
        n = 0;
14063
0
      }
14064
14065
0
          last_sym = sym;
14066
0
          last_sym_idx = n;
14067
0
        }
14068
14069
0
      if (sym->the_bfd != NULL
14070
0
          && sym->the_bfd->xvec != abfd->xvec
14071
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14072
0
        {
14073
0
          _bfd_error_handler
14074
      /* xgettext:c-format */
14075
0
      (_("%pB(%pA): error: secondary reloc %zu"
14076
0
         " references a deleted symbol"),
14077
0
       abfd, relsec, idx);
14078
0
          bfd_set_error (bfd_error_bad_value);
14079
0
          result = false;
14080
0
          n = 0;
14081
0
        }
14082
0
    }
14083
14084
0
        src_rela.r_offset = ptr->address + addr_offset;
14085
0
        if (ptr->howto == NULL)
14086
0
    {
14087
0
      _bfd_error_handler
14088
        /* xgettext:c-format */
14089
0
        (_("%pB(%pA): error: secondary reloc %zu"
14090
0
           " is of an unknown type"),
14091
0
         abfd, relsec, idx);
14092
0
      bfd_set_error (bfd_error_bad_value);
14093
0
      result = false;
14094
0
      src_rela.r_info = r_info (0, 0);
14095
0
    }
14096
0
        else
14097
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14098
0
        src_rela.r_addend = ptr->addend;
14099
14100
0
        if (entsize == ebd->s->sizeof_rel)
14101
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14102
0
        else /* entsize == ebd->s->sizeof_rela */
14103
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14104
0
      }
14105
0
  }
14106
0
    }
14107
14108
0
  return result;
14109
0
}
14110
14111
/* Mmap in section contents.  */
14112
14113
static bool
14114
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14115
10.6k
{
14116
10.6k
#ifdef USE_MMAP
14117
10.6k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14118
10.6k
  if (bed->use_mmap
14119
3.83k
      && sec->compress_status == COMPRESS_SECTION_NONE
14120
3.83k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14121
3.83k
    {
14122
      /* Use mmap only if section size >= the minimum mmap section
14123
   size.  */
14124
3.83k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14125
3.83k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14126
3.83k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14127
948
  {
14128
948
    if (sec->contents != NULL)
14129
0
      {
14130
0
        if (!sec->mmapped_p)
14131
0
    abort ();
14132
0
        *buf = sec->contents;
14133
0
        return true;
14134
0
      }
14135
948
    if (sec->mmapped_p)
14136
0
      abort ();
14137
948
    sec->mmapped_p = 1;
14138
14139
    /* We can't use the final link preallocated buffer for mmap.  */
14140
948
    *buf = NULL;
14141
948
  }
14142
3.83k
    }
14143
10.6k
#endif
14144
  /* FIXME: We should not get here if sec->alloced is set.  */
14145
10.6k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14146
10.6k
  if (ret && sec->mmapped_p)
14147
0
    *buf = sec->contents;
14148
10.6k
  return ret;
14149
10.6k
}
14150
14151
/* Mmap in section contents.  */
14152
14153
bool
14154
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14155
10.6k
{
14156
10.6k
  *buf = NULL;
14157
10.6k
  return elf_mmap_section_contents (abfd, sec, buf);
14158
10.6k
}
14159
14160
/* Mmap in the full section contents for the final link.  */
14161
14162
bool
14163
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14164
             bfd_byte **buf)
14165
0
{
14166
0
  return elf_mmap_section_contents (abfd, sec, buf);
14167
0
}
14168
14169
/* Munmap section contents.  */
14170
14171
void
14172
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14173
513k
{
14174
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14175
     CONTENTS may be NULL.  */
14176
513k
  if (contents == NULL)
14177
504k
    return;
14178
14179
9.23k
  if (sec->alloced
14180
      /* What a tangled web we weave with section contents.
14181
   FIXME: We shouldn't need to test anything but sec->alloced
14182
   here, but there are cases where a buffer is allocated for a
14183
   section but then another buffer is malloc'd anyway.  eg.
14184
   trace through ld-elf/eh4 testcase on x86_64.  */
14185
560
      && (sec->contents == contents
14186
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14187
560
    return;
14188
14189
  /* Don't leave pointers to data we are about to munmap or free.  */
14190
8.67k
  if (sec->contents == contents)
14191
0
    sec->contents = NULL;
14192
8.67k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14193
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14194
14195
8.67k
#ifdef USE_MMAP
14196
8.67k
  if (sec->mmapped_p)
14197
0
    {
14198
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14199
   malloced, CONTENTS_ADDR is set to NULL.  */
14200
0
      if (elf_section_data (sec)->contents_addr != NULL)
14201
0
  {
14202
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14203
0
    if (munmap (elf_section_data (sec)->contents_addr,
14204
0
          elf_section_data (sec)->contents_size) != 0)
14205
0
      abort ();
14206
0
    sec->mmapped_p = 0;
14207
0
    elf_section_data (sec)->contents_addr = NULL;
14208
0
    elf_section_data (sec)->contents_size = 0;
14209
0
    return;
14210
0
  }
14211
0
    }
14212
8.67k
#endif
14213
14214
8.67k
  free (contents);
14215
8.67k
}
14216
14217
/* Munmap the full section contents for the final link.  */
14218
14219
void
14220
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14221
0
{
14222
0
#ifdef USE_MMAP
14223
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14224
0
    {
14225
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14226
   malloced, CONTENTS_ADDR is set to NULL.  */
14227
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14228
0
      if (munmap (elf_section_data (sec)->contents_addr,
14229
0
      elf_section_data (sec)->contents_size) != 0)
14230
0
  abort ();
14231
0
      sec->mmapped_p = 0;
14232
0
      sec->contents = NULL;
14233
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14234
0
      elf_section_data (sec)->contents_addr = NULL;
14235
0
      elf_section_data (sec)->contents_size = 0;
14236
0
    }
14237
0
#endif
14238
0
}
14239
14240
#ifndef OBJ_MAYBE_ELF_ATTRIBUTES
14241
14242
bfd *_bfd_elf_link_setup_object_attributes
14243
  (struct bfd_link_info *info ATTRIBUTE_UNUSED)
14244
{ return NULL; }
14245
14246
#endif /* !OBJ_MAYBE_ELF_ATTRIBUTES */