Coverage Report

Created: 2026-05-11 07:54

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
62.1k
#define streq(A,B) (strcmp ((A), (B)) == 0)
56
#endif
57
58
/* Core note names.  */
59
0
#define NOTE_NAME_CORE     "CORE"
60
0
#define NOTE_NAME_FREEBSD  "FreeBSD"
61
0
#define NOTE_NAME_GDB      "GDB"
62
0
#define NOTE_NAME_LINUX    "LINUX"
63
64
/* Names of a pseudo-section which represent core notes.  */
65
0
#define NOTE_PSEUDO_SECTION_AARCH_FPMR    ".reg-aarch-fpmr"
66
0
#define NOTE_PSEUDO_SECTION_AARCH_GCS   ".reg-aarch-gcs"
67
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK  ".reg-aarch-hw-break"
68
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH  ".reg-aarch-hw-watch"
69
0
#define NOTE_PSEUDO_SECTION_AARCH_MTE   ".reg-aarch-mte"
70
0
#define NOTE_PSEUDO_SECTION_AARCH_PAUTH   ".reg-aarch-pauth"
71
0
#define NOTE_PSEUDO_SECTION_AARCH_SSVE    ".reg-aarch-ssve"
72
0
#define NOTE_PSEUDO_SECTION_AARCH_SVE   ".reg-aarch-sve"
73
0
#define NOTE_PSEUDO_SECTION_AARCH_TLS   ".reg-aarch-tls"
74
0
#define NOTE_PSEUDO_SECTION_AARCH_ZA    ".reg-aarch-za"
75
0
#define NOTE_PSEUDO_SECTION_AARCH_ZT    ".reg-aarch-zt"
76
0
#define NOTE_PSEUDO_SECTION_ARC_V2    ".reg-arc-v2"
77
0
#define NOTE_PSEUDO_SECTION_ARM_VFP   ".reg-arm-vfp"
78
0
#define NOTE_PSEUDO_SECTION_I386_TLS    ".reg-i386-tls"
79
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG  ".reg-loongarch-cpucfg"
80
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LASX  ".reg-loongarch-lasx"
81
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt"
82
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx"
83
0
#define NOTE_PSEUDO_SECTION_PPC_DSCR    ".reg-ppc-dscr"
84
0
#define NOTE_PSEUDO_SECTION_PPC_EBB   ".reg-ppc-ebb"
85
0
#define NOTE_PSEUDO_SECTION_PPC_PMU   ".reg-ppc-pmu"
86
0
#define NOTE_PSEUDO_SECTION_PPC_PPR   ".reg-ppc-ppr"
87
0
#define NOTE_PSEUDO_SECTION_PPC_TAR   ".reg-ppc-tar"
88
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR  ".reg-ppc-tm-cdscr"
89
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CFPR   ".reg-ppc-tm-cfpr"
90
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CGPR   ".reg-ppc-tm-cgpr"
91
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CPPR   ".reg-ppc-tm-cppr"
92
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CTAR   ".reg-ppc-tm-ctar"
93
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVMX   ".reg-ppc-tm-cvmx"
94
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVSX   ".reg-ppc-tm-cvsx"
95
0
#define NOTE_PSEUDO_SECTION_PPC_TM_SPR    ".reg-ppc-tm-spr"
96
0
#define NOTE_PSEUDO_SECTION_PPC_VSX   ".reg-ppc-vsx"
97
0
#define NOTE_PSEUDO_SECTION_PPC_VMX   ".reg-ppc-vmx"
98
1
#define NOTE_PSEUDO_SECTION_REG     ".reg"
99
50
#define NOTE_PSEUDO_SECTION_REG2    ".reg2"
100
0
#define NOTE_PSEUDO_SECTION_RISCV_CSR   ".reg-riscv-csr"
101
0
#define NOTE_PSEUDO_SECTION_S390_CTRS   ".reg-s390-ctrs"
102
0
#define NOTE_PSEUDO_SECTION_S390_GS_BC    ".reg-s390-gs-bc"
103
0
#define NOTE_PSEUDO_SECTION_S390_GS_CB    ".reg-s390-gs-cb"
104
0
#define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS  ".reg-s390-high-gprs"
105
0
#define NOTE_PSEUDO_SECTION_S390_LAST_BREAK ".reg-s390-last-break"
106
0
#define NOTE_PSEUDO_SECTION_S390_PREFIX   ".reg-s390-prefix"
107
0
#define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL  ".reg-s390-system-call"
108
0
#define NOTE_PSEUDO_SECTION_S390_TDB    ".reg-s390-tdb"
109
0
#define NOTE_PSEUDO_SECTION_S390_TIMER    ".reg-s390-timer"
110
0
#define NOTE_PSEUDO_SECTION_S390_TODCMP   ".reg-s390-todcmp"
111
0
#define NOTE_PSEUDO_SECTION_S390_TODPREG  ".reg-s390-todpreg"
112
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH  ".reg-s390-vxrs-high"
113
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_LOW ".reg-s390-vxrs-low"
114
0
#define NOTE_PSEUDO_SECTION_SSP     ".reg-ssp"
115
0
#define NOTE_PSEUDO_SECTION_TDESC   ".gdb-tdesc"
116
0
#define NOTE_PSEUDO_SECTION_X86_SEGBASES  ".reg-x86-segbases"
117
0
#define NOTE_PSEUDO_SECTION_XFP           ".reg-xfp"
118
0
#define NOTE_PSEUDO_SECTION_XSTATE        ".reg-xstate"
119
0
#define NOTE_PSEUDO_SECTION_XSAVE_LAYOUT  ".reg-xsave-layout"
120
121
static int elf_sort_sections (const void *, const void *);
122
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
123
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
124
         struct bfd_link_info *);
125
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
126
           file_ptr offset, size_t align);
127
128
/* Swap version information in and out.  The version information is
129
   currently size independent.  If that ever changes, this code will
130
   need to move into elfcode.h.  */
131
132
/* Swap in a Verdef structure.  */
133
134
void
135
_bfd_elf_swap_verdef_in (bfd *abfd,
136
       const Elf_External_Verdef *src,
137
       Elf_Internal_Verdef *dst)
138
21
{
139
21
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
140
21
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
141
21
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
142
21
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
143
21
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
144
21
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
145
21
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
146
21
}
147
148
/* Swap out a Verdef structure.  */
149
150
void
151
_bfd_elf_swap_verdef_out (bfd *abfd,
152
        const Elf_Internal_Verdef *src,
153
        Elf_External_Verdef *dst)
154
0
{
155
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
156
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
157
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
158
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
159
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
160
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
161
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
162
0
}
163
164
/* Swap in a Verdaux structure.  */
165
166
void
167
_bfd_elf_swap_verdaux_in (bfd *abfd,
168
        const Elf_External_Verdaux *src,
169
        Elf_Internal_Verdaux *dst)
170
5
{
171
5
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
172
5
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
173
5
}
174
175
/* Swap out a Verdaux structure.  */
176
177
void
178
_bfd_elf_swap_verdaux_out (bfd *abfd,
179
         const Elf_Internal_Verdaux *src,
180
         Elf_External_Verdaux *dst)
181
0
{
182
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
183
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
184
0
}
185
186
/* Swap in a Verneed structure.  */
187
188
void
189
_bfd_elf_swap_verneed_in (bfd *abfd,
190
        const Elf_External_Verneed *src,
191
        Elf_Internal_Verneed *dst)
192
188
{
193
188
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
194
188
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
195
188
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
196
188
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
197
188
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
198
188
}
199
200
/* Swap out a Verneed structure.  */
201
202
void
203
_bfd_elf_swap_verneed_out (bfd *abfd,
204
         const Elf_Internal_Verneed *src,
205
         Elf_External_Verneed *dst)
206
0
{
207
0
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
208
0
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
209
0
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
210
0
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
211
0
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
212
0
}
213
214
/* Swap in a Vernaux structure.  */
215
216
void
217
_bfd_elf_swap_vernaux_in (bfd *abfd,
218
        const Elf_External_Vernaux *src,
219
        Elf_Internal_Vernaux *dst)
220
208
{
221
208
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
222
208
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
223
208
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
224
208
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
225
208
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
226
208
}
227
228
/* Swap out a Vernaux structure.  */
229
230
void
231
_bfd_elf_swap_vernaux_out (bfd *abfd,
232
         const Elf_Internal_Vernaux *src,
233
         Elf_External_Vernaux *dst)
234
0
{
235
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
236
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
237
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
238
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
239
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
240
0
}
241
242
/* Swap in a Versym structure.  */
243
244
void
245
_bfd_elf_swap_versym_in (bfd *abfd,
246
       const Elf_External_Versym *src,
247
       Elf_Internal_Versym *dst)
248
1.01k
{
249
1.01k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
250
1.01k
}
251
252
/* Swap out a Versym structure.  */
253
254
void
255
_bfd_elf_swap_versym_out (bfd *abfd,
256
        const Elf_Internal_Versym *src,
257
        Elf_External_Versym *dst)
258
0
{
259
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
260
0
}
261
262
/* Standard ELF hash function.  Do not change this function; you will
263
   cause invalid hash tables to be generated.  */
264
265
unsigned long
266
bfd_elf_hash (const char *namearg)
267
0
{
268
0
  uint32_t h = 0;
269
270
0
  for (const unsigned char *name = (const unsigned char *) namearg;
271
0
       *name; name++)
272
0
    {
273
0
      h = (h << 4) + *name;
274
0
      h ^= (h >> 24) & 0xf0;
275
0
    }
276
0
  return h & 0x0fffffff;
277
0
}
278
279
/* DT_GNU_HASH hash function.  Do not change this function; you will
280
   cause invalid hash tables to be generated.  */
281
282
unsigned long
283
bfd_elf_gnu_hash (const char *namearg)
284
0
{
285
0
  uint32_t h = 5381;
286
287
0
  for (const unsigned char *name = (const unsigned char *) namearg;
288
0
       *name; name++)
289
0
    h = (h << 5) + h + *name;
290
0
  return h;
291
0
}
292
293
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
294
   the object_id field of an elf_obj_tdata field set.  */
295
bool
296
bfd_elf_allocate_object (bfd *abfd,
297
       size_t object_size)
298
2.18M
{
299
2.18M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
300
2.18M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
301
2.18M
  if (abfd->tdata.any == NULL)
302
0
    return false;
303
304
2.18M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
305
2.18M
  if (abfd->direction != read_direction)
306
657
    {
307
657
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
308
657
      if (o == NULL)
309
0
  return false;
310
657
      elf_tdata (abfd)->o = o;
311
657
      elf_program_header_size (abfd) = (bfd_size_type) -1;
312
657
    }
313
2.18M
  return true;
314
2.18M
}
315
316
317
bool
318
bfd_elf_make_object (bfd *abfd)
319
1.04M
{
320
1.04M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
321
1.04M
}
322
323
bool
324
bfd_elf_mkcorefile (bfd *abfd)
325
552k
{
326
  /* I think this can be done just like an object file.  */
327
552k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
328
0
    return false;
329
552k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
330
552k
  return elf_tdata (abfd)->core != NULL;
331
552k
}
332
333
char *
334
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
335
51.1k
{
336
51.1k
  Elf_Internal_Shdr **i_shdrp;
337
51.1k
  bfd_byte *shstrtab = NULL;
338
51.1k
  file_ptr offset;
339
51.1k
  bfd_size_type shstrtabsize;
340
341
51.1k
  i_shdrp = elf_elfsections (abfd);
342
51.1k
  if (i_shdrp == 0
343
51.1k
      || shindex >= elf_numsections (abfd)
344
51.1k
      || i_shdrp[shindex] == 0)
345
0
    return NULL;
346
347
51.1k
  shstrtab = i_shdrp[shindex]->contents;
348
51.1k
  if (shstrtab == NULL)
349
51.1k
    {
350
      /* No cached one, attempt to read, and cache what we read.  */
351
51.1k
      offset = i_shdrp[shindex]->sh_offset;
352
51.1k
      shstrtabsize = i_shdrp[shindex]->sh_size;
353
354
51.1k
      if (shstrtabsize == 0
355
46.4k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
356
46.0k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
357
7.51k
  {
358
    /* Once we've failed to read it, make sure we don't keep
359
       trying.  Otherwise, we'll keep allocating space for
360
       the string table over and over.  */
361
7.51k
    i_shdrp[shindex]->sh_size = 0;
362
7.51k
  }
363
43.6k
      else if (shstrtab[shstrtabsize - 1] != 0)
364
10.5k
  {
365
    /* It is an error if a string table isn't terminated.  */
366
10.5k
    _bfd_error_handler
367
      /* xgettext:c-format */
368
10.5k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
369
10.5k
    shstrtab[shstrtabsize - 1] = 0;
370
10.5k
  }
371
51.1k
      i_shdrp[shindex]->contents = shstrtab;
372
51.1k
    }
373
51.1k
  return (char *) shstrtab;
374
51.1k
}
375
376
char *
377
bfd_elf_string_from_elf_section (bfd *abfd,
378
         unsigned int shindex,
379
         unsigned int strindex)
380
1.05M
{
381
1.05M
  Elf_Internal_Shdr *hdr;
382
383
1.05M
  if (strindex == 0)
384
196k
    return "";
385
386
859k
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
387
258
    return NULL;
388
389
859k
  hdr = elf_elfsections (abfd)[shindex];
390
391
859k
  if (hdr->contents == NULL)
392
72.5k
    {
393
72.5k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
394
21.4k
  {
395
    /* PR 17512: file: f057ec89.  */
396
    /* xgettext:c-format */
397
21.4k
    _bfd_error_handler (_("%pB: attempt to load strings from"
398
21.4k
        " a non-string section (number %d)"),
399
21.4k
            abfd, shindex);
400
21.4k
    return NULL;
401
21.4k
  }
402
403
51.1k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
404
7.51k
  return NULL;
405
51.1k
    }
406
786k
  else
407
786k
    {
408
      /* PR 24273: The string section's contents may have already
409
   been loaded elsewhere, eg because a corrupt file has the
410
   string section index in the ELF header pointing at a group
411
   section.  So be paranoid, and test that the last byte of
412
   the section is zero.  */
413
786k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
414
0
  return NULL;
415
786k
    }
416
417
830k
  if (strindex >= hdr->sh_size)
418
16.7k
    {
419
16.7k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
420
16.7k
      _bfd_error_handler
421
  /* xgettext:c-format */
422
16.7k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
423
16.7k
   abfd, strindex, (uint64_t) hdr->sh_size,
424
16.7k
   (shindex == shstrndx && strindex == hdr->sh_name
425
16.7k
    ? ".shstrtab"
426
16.7k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
427
16.7k
      return NULL;
428
16.7k
    }
429
430
813k
  return ((char *) hdr->contents) + strindex;
431
830k
}
432
433
/* Read and convert symbols to internal format.
434
   SYMCOUNT specifies the number of symbols to read, starting from
435
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
436
   are non-NULL, they are used to store the internal symbols, external
437
   symbols, and symbol section index extensions, respectively.
438
   Returns a pointer to the internal symbol buffer (malloced if necessary)
439
   or NULL if there were no symbols or some kind of problem.  */
440
441
Elf_Internal_Sym *
442
bfd_elf_get_elf_syms (bfd *ibfd,
443
          Elf_Internal_Shdr *symtab_hdr,
444
          size_t symcount,
445
          size_t symoffset,
446
          Elf_Internal_Sym *intsym_buf,
447
          void *extsym_buf,
448
          Elf_External_Sym_Shndx *extshndx_buf)
449
14.0k
{
450
14.0k
  Elf_Internal_Shdr *shndx_hdr;
451
14.0k
  void *alloc_ext;
452
14.0k
  const bfd_byte *esym;
453
14.0k
  Elf_External_Sym_Shndx *alloc_extshndx;
454
14.0k
  Elf_External_Sym_Shndx *shndx;
455
14.0k
  Elf_Internal_Sym *alloc_intsym;
456
14.0k
  Elf_Internal_Sym *isym;
457
14.0k
  Elf_Internal_Sym *isymend;
458
14.0k
  elf_backend_data *bed;
459
14.0k
  size_t extsym_size;
460
14.0k
  size_t amt;
461
14.0k
  file_ptr pos;
462
463
14.0k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
464
0
    abort ();
465
466
14.0k
  if (symcount == 0)
467
0
    return intsym_buf;
468
469
14.0k
  if (elf_use_dt_symtab_p (ibfd))
470
3
    {
471
      /* Use dynamic symbol table.  */
472
3
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
473
0
  {
474
0
    bfd_set_error (bfd_error_invalid_operation);
475
0
    return NULL;
476
0
  }
477
3
      return elf_tdata (ibfd)->dt_symtab + symoffset;
478
3
    }
479
480
  /* Normal syms might have section extension entries.  */
481
14.0k
  shndx_hdr = NULL;
482
14.0k
  if (elf_symtab_shndx_list (ibfd) != NULL)
483
2.41k
    {
484
2.41k
      elf_section_list * entry;
485
2.41k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
486
487
      /* Find an index section that is linked to this symtab section.  */
488
4.13k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
489
2.67k
  {
490
    /* PR 20063.  */
491
2.67k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
492
87
      continue;
493
494
2.59k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
495
965
      {
496
965
        shndx_hdr = & entry->hdr;
497
965
        break;
498
1.62k
      };
499
1.62k
  }
500
501
2.41k
      if (shndx_hdr == NULL)
502
1.45k
  {
503
1.45k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
504
      /* Not really accurate, but this was how the old code used
505
         to work.  */
506
1.44k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
507
    /* Otherwise we do nothing.  The assumption is that
508
       the index table will not be needed.  */
509
1.45k
  }
510
2.41k
    }
511
512
  /* Read the symbols.  */
513
14.0k
  alloc_ext = NULL;
514
14.0k
  alloc_extshndx = NULL;
515
14.0k
  alloc_intsym = NULL;
516
14.0k
  bed = get_elf_backend_data (ibfd);
517
14.0k
  extsym_size = bed->s->sizeof_sym;
518
14.0k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
519
0
    {
520
0
      bfd_set_error (bfd_error_file_too_big);
521
0
      return NULL;
522
0
    }
523
14.0k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
524
14.0k
  size_t alloc_ext_size = amt;
525
14.0k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
526
13.7k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
527
13.7k
            &alloc_ext, ibfd, false))
528
1.14k
    {
529
1.14k
      intsym_buf = NULL;
530
1.14k
      goto out2;
531
1.14k
    }
532
533
12.9k
  size_t alloc_extshndx_size = 0;
534
12.9k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
535
11.2k
    extshndx_buf = NULL;
536
1.61k
  else
537
1.61k
    {
538
1.61k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
539
0
  {
540
0
    bfd_set_error (bfd_error_file_too_big);
541
0
    intsym_buf = NULL;
542
0
    goto out1;
543
0
  }
544
1.61k
      alloc_extshndx_size = amt;
545
1.61k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
546
1.61k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
547
1.52k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
548
1.52k
          &alloc_extshndx_size,
549
1.52k
          (void **) &alloc_extshndx,
550
1.52k
          ibfd, false))
551
206
  {
552
206
    intsym_buf = NULL;
553
206
    goto out1;
554
206
  }
555
1.61k
    }
556
557
12.7k
  if (intsym_buf == NULL)
558
4.85k
    {
559
4.85k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
560
0
  {
561
0
    bfd_set_error (bfd_error_file_too_big);
562
0
    goto out1;
563
0
  }
564
4.85k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
565
4.85k
      intsym_buf = alloc_intsym;
566
4.85k
      if (intsym_buf == NULL)
567
0
  goto out1;
568
4.85k
    }
569
570
  /* Convert the symbols to internal form.  */
571
12.7k
  isymend = intsym_buf + symcount;
572
12.7k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
573
12.7k
     shndx = extshndx_buf;
574
170k
       isym < isymend;
575
158k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
576
159k
    {
577
159k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
578
472
  {
579
472
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
580
    /* xgettext:c-format */
581
472
    _bfd_error_handler (_("%pB symbol number %lu references"
582
472
        " nonexistent SHT_SYMTAB_SHNDX section"),
583
472
            ibfd, (unsigned long) symoffset);
584
472
    free (alloc_intsym);
585
472
    intsym_buf = NULL;
586
472
    goto out1;
587
472
  }
588
589
      /* PR 33019: Do not accept unsupported binding values - they will
590
   likely cause problems later on.  */
591
159k
      int bind = ELF_ST_BIND (isym->st_info);
592
159k
      if (bind > STB_WEAK && bind < STB_LOOS)
593
875
  { 
594
    /* xgettext:c-format */
595
875
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
596
875
            ibfd, (unsigned long) (isym - intsym_buf), bind);
597
875
    free (alloc_intsym);
598
875
    intsym_buf = NULL;
599
875
    goto out1;
600
875
  }
601
602
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
603
158k
      int t = ELF_ST_TYPE (isym->st_info);
604
158k
      if (t == 7)
605
224
  {
606
    /* xgettext:c-format */
607
224
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
608
224
            ibfd, (unsigned long) (isym - intsym_buf), t);
609
224
    free (alloc_intsym);
610
224
    intsym_buf = NULL;
611
224
    goto out1;
612
224
  }
613
158k
    }
614
615
12.9k
 out1:
616
12.9k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
617
14.0k
 out2:
618
14.0k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
619
620
14.0k
  return intsym_buf;
621
12.9k
}
622
623
/* Look up a symbol name.  */
624
static const char *
625
bfd_elf_sym_name_raw (bfd *abfd,
626
          Elf_Internal_Shdr *symtab_hdr,
627
          Elf_Internal_Sym *isym)
628
148k
{
629
148k
  unsigned int iname = isym->st_name;
630
148k
  unsigned int shindex = symtab_hdr->sh_link;
631
632
148k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
633
      /* Check for a bogus st_shndx to avoid crashing.  */
634
25.3k
      && isym->st_shndx < elf_numsections (abfd))
635
24.9k
    {
636
24.9k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
637
24.9k
      shindex = elf_elfheader (abfd)->e_shstrndx;
638
24.9k
    }
639
640
148k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
641
148k
}
642
643
const char *
644
bfd_elf_sym_name (bfd *abfd,
645
      Elf_Internal_Shdr *symtab_hdr,
646
      Elf_Internal_Sym *isym,
647
      asection *sym_sec)
648
142k
{
649
142k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
650
142k
  if (name == NULL)
651
36.9k
    name = bfd_symbol_error_name;
652
105k
  else if (sym_sec && *name == '\0')
653
0
    name = bfd_section_name (sym_sec);
654
655
142k
  return name;
656
142k
}
657
658
/* Return the name of the group signature symbol.  Why isn't the
659
   signature just a string?  */
660
661
static const char *
662
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
663
9.58k
{
664
9.58k
  Elf_Internal_Shdr *hdr;
665
9.58k
  unsigned char esym[sizeof (Elf64_External_Sym)];
666
9.58k
  Elf_External_Sym_Shndx eshndx;
667
9.58k
  Elf_Internal_Sym isym;
668
669
  /* First we need to ensure the symbol table is available.  Make sure
670
     that it is a symbol table section.  */
671
9.58k
  if (ghdr->sh_link >= elf_numsections (abfd))
672
28
    return NULL;
673
9.55k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
674
9.55k
  if (hdr->sh_type != SHT_SYMTAB
675
8.86k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
676
734
    return NULL;
677
678
  /* Go read the symbol.  */
679
8.82k
  hdr = &elf_symtab_hdr (abfd);
680
8.82k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
681
8.82k
          &isym, esym, &eshndx) == NULL)
682
2.09k
    return NULL;
683
684
6.72k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
685
8.82k
}
686
687
static bool
688
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
689
14.5k
{
690
14.5k
  return (shdr->sh_size >= minsize
691
14.2k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
692
12.8k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
693
12.7k
    && shdr->bfd_section != NULL);
694
14.5k
}
695
696
697
/* Set next_in_group, sec_group list pointers, and group names.  */
698
699
static bool
700
process_sht_group_entries (bfd *abfd,
701
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
702
12.6k
{
703
12.6k
  unsigned char *contents;
704
705
  /* Read the raw contents.  */
706
12.6k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
707
696
    {
708
696
      _bfd_error_handler
709
  /* xgettext:c-format */
710
696
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
711
696
      return false;
712
696
    }
713
714
11.9k
  asection *last_elt = NULL;
715
11.9k
  const char *gname = NULL;
716
11.9k
  unsigned char *p = contents + ghdr->sh_size;
717
504k
  while (1)
718
504k
    {
719
504k
      unsigned int idx;
720
504k
      Elf_Internal_Shdr *shdr;
721
504k
      asection *elt;
722
723
504k
      p -= 4;
724
504k
      idx = H_GET_32 (abfd, p);
725
504k
      if (p == contents)
726
7.94k
  {
727
7.94k
    if ((idx & GRP_COMDAT) != 0)
728
2.96k
      ghdr->bfd_section->flags
729
2.96k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
730
7.94k
    break;
731
7.94k
  }
732
733
496k
      if (idx == 0
734
361k
    || idx >= elf_numsections (abfd)
735
61.2k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
736
55.3k
    || ((elt = shdr->bfd_section) != NULL
737
35.9k
        && elf_sec_group (elt) != NULL
738
16.1k
        && elf_sec_group (elt) != ghdr->bfd_section))
739
444k
  {
740
444k
    _bfd_error_handler
741
444k
      (_("%pB: invalid entry (%#x) in group [%u]"),
742
444k
       abfd, idx, gidx);
743
444k
    continue;
744
444k
  }
745
746
      /* PR binutils/23199: According to the ELF gABI all sections in
747
   a group must be marked with SHF_GROUP, but some tools
748
   generate broken objects.  Fix them up here.  */
749
52.3k
      shdr->sh_flags |= SHF_GROUP;
750
751
52.3k
      if (elt == NULL)
752
19.3k
  {
753
19.3k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
754
7.21k
      {
755
7.21k
        const char *name = bfd_elf_string_from_elf_section
756
7.21k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
757
758
7.21k
        _bfd_error_handler
759
    /* xgettext:c-format */
760
7.21k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
761
7.21k
     abfd, shdr->sh_type, name, gidx);
762
7.21k
      }
763
19.3k
    continue;
764
19.3k
  }
765
766
      /* Don't try to add a section to elf_next_in_group list twice.  */
767
33.0k
      if (elf_sec_group (elt) != NULL)
768
13.2k
  continue;
769
770
19.8k
      if (last_elt == NULL)
771
9.58k
  {
772
    /* Start a circular list with one element.
773
       It will be in reverse order to match what gas does.  */
774
9.58k
    elf_next_in_group (elt) = elt;
775
    /* Point the group section to it.  */
776
9.58k
    elf_next_in_group (ghdr->bfd_section) = elt;
777
9.58k
    gname = group_signature (abfd, ghdr);
778
9.58k
    if (gname == NULL)
779
3.99k
      {
780
3.99k
        free (contents);
781
3.99k
        return false;
782
3.99k
      }
783
9.58k
  }
784
10.2k
      else
785
10.2k
  {
786
10.2k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
787
10.2k
    elf_next_in_group (last_elt) = elt;
788
10.2k
  }
789
15.8k
      last_elt = elt;
790
15.8k
      elf_group_name (elt) = gname;
791
15.8k
      elf_sec_group (elt) = ghdr->bfd_section;
792
15.8k
    }
793
794
7.94k
  free (contents);
795
7.94k
  return last_elt != NULL;
796
11.9k
}
797
798
bool
799
_bfd_elf_setup_sections (bfd *abfd)
800
36.4k
{
801
36.4k
  bool result = true;
802
803
  /* Process SHF_LINK_ORDER.  */
804
407k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
805
370k
    {
806
370k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
807
370k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
808
25.1k
  {
809
25.1k
    unsigned int elfsec = this_hdr->sh_link;
810
    /* An sh_link value of 0 is now allowed.  It indicates that linked
811
       to section has already been discarded, but that the current
812
       section has been retained for some other reason.  This linking
813
       section is still a candidate for later garbage collection
814
       however.  */
815
25.1k
    if (elfsec == 0)
816
20.1k
      {
817
20.1k
        elf_linked_to_section (s) = NULL;
818
20.1k
      }
819
5.08k
    else
820
5.08k
      {
821
5.08k
        asection *linksec = NULL;
822
823
5.08k
        if (elfsec < elf_numsections (abfd))
824
4.97k
    {
825
4.97k
      this_hdr = elf_elfsections (abfd)[elfsec];
826
4.97k
      linksec = this_hdr->bfd_section;
827
4.97k
    }
828
829
        /* PR 1991, 2008:
830
     Some strip/objcopy may leave an incorrect value in
831
     sh_link.  We don't want to proceed.  */
832
5.08k
        if (linksec == NULL)
833
1.77k
    {
834
1.77k
      _bfd_error_handler
835
        /* xgettext:c-format */
836
1.77k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
837
1.77k
         s->owner, elfsec, s);
838
1.77k
      result = false;
839
1.77k
    }
840
841
5.08k
        elf_linked_to_section (s) = linksec;
842
5.08k
      }
843
25.1k
  }
844
370k
    }
845
846
  /* Process section groups.  */
847
601k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
848
565k
    {
849
565k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
850
851
565k
      if (shdr && shdr->sh_type == SHT_GROUP)
852
14.5k
  {
853
14.5k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
854
12.7k
      {
855
12.7k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
856
12.6k
      && !process_sht_group_entries (abfd, shdr, i))
857
7.04k
    result = false;
858
12.7k
      }
859
1.80k
    else
860
1.80k
      {
861
        /* PR binutils/18758: Beware of corrupt binaries with
862
     invalid group data.  */
863
1.80k
        _bfd_error_handler
864
    /* xgettext:c-format */
865
1.80k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
866
1.80k
        result = false;
867
1.80k
      }
868
14.5k
  }
869
565k
    }
870
871
36.4k
  return result;
872
36.4k
}
873
874
bool
875
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
876
0
{
877
0
  return elf_next_in_group (sec) != NULL;
878
0
}
879
880
const char *
881
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
882
0
{
883
0
  if (elf_sec_group (sec) != NULL)
884
0
    return elf_group_name (sec);
885
0
  return NULL;
886
0
}
887
888
/* Make a BFD section from an ELF section.  We store a pointer to the
889
   BFD section in the bfd_section field of the header.  */
890
891
bool
892
_bfd_elf_make_section_from_shdr (bfd *abfd,
893
         Elf_Internal_Shdr *hdr,
894
         const char *name,
895
         int shindex)
896
538k
{
897
538k
  asection *newsect;
898
538k
  flagword flags;
899
538k
  elf_backend_data *bed;
900
538k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
901
902
538k
  if (hdr->bfd_section != NULL)
903
79.0k
    return true;
904
905
459k
  newsect = bfd_make_section_anyway (abfd, name);
906
459k
  if (newsect == NULL)
907
0
    return false;
908
909
459k
  hdr->bfd_section = newsect;
910
459k
  elf_section_data (newsect)->this_hdr = *hdr;
911
459k
  elf_section_data (newsect)->this_idx = shindex;
912
913
  /* Always use the real type/flags.  */
914
459k
  elf_section_type (newsect) = hdr->sh_type;
915
459k
  elf_section_flags (newsect) = hdr->sh_flags;
916
917
459k
  newsect->filepos = hdr->sh_offset;
918
919
459k
  flags = SEC_NO_FLAGS;
920
459k
  if (hdr->sh_type != SHT_NOBITS)
921
446k
    flags |= SEC_HAS_CONTENTS;
922
459k
  if (hdr->sh_type == SHT_GROUP)
923
19.5k
    flags |= SEC_GROUP;
924
459k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
925
196k
    {
926
196k
      flags |= SEC_ALLOC;
927
196k
      if (hdr->sh_type != SHT_NOBITS)
928
187k
  flags |= SEC_LOAD;
929
196k
    }
930
459k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
931
357k
    flags |= SEC_READONLY;
932
459k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
933
98.5k
    flags |= SEC_CODE;
934
360k
  else if ((flags & SEC_LOAD) != 0)
935
102k
    flags |= SEC_DATA;
936
459k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
937
73.9k
    flags |= SEC_MERGE;
938
459k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
939
93.9k
    flags |= SEC_STRINGS;
940
459k
  if ((hdr->sh_flags & SHF_TLS) != 0)
941
36.7k
    flags |= SEC_THREAD_LOCAL;
942
459k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
943
33.2k
    flags |= SEC_EXCLUDE;
944
945
459k
  newsect->entsize = hdr->sh_entsize;
946
947
459k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
948
459k
    {
949
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
950
   but binutils as of 2019-07-23 did not set the EI_OSABI header
951
   byte.  */
952
169k
    case ELFOSABI_GNU:
953
253k
    case ELFOSABI_FREEBSD:
954
253k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
955
43.2k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
956
      /* Fall through */
957
383k
    case ELFOSABI_NONE:
958
383k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
959
28.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
960
383k
      break;
961
459k
    }
962
963
459k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
964
243k
    {
965
      /* The debugging sections appear to be recognized only by name,
966
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
967
243k
      if (name [0] == '.')
968
99.4k
  {
969
99.4k
    if (startswith (name, ".debug")
970
65.4k
        || startswith (name, ".gnu.debuglto_.debug_")
971
65.1k
        || startswith (name, ".gnu.linkonce.wi.")
972
64.4k
        || startswith (name, ".zdebug"))
973
35.9k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
974
63.5k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
975
63.4k
       || startswith (name, ".note.gnu"))
976
354
      {
977
354
        flags |= SEC_ELF_OCTETS;
978
354
        opb = 1;
979
354
      }
980
63.2k
    else if (startswith (name, ".line")
981
62.7k
       || startswith (name, ".stab")
982
61.3k
       || streq (name, ".gdb_index"))
983
2.39k
      flags |= SEC_DEBUGGING;
984
99.4k
  }
985
243k
    }
986
987
459k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
988
459k
      || !bfd_set_section_size (newsect, hdr->sh_size)
989
459k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
990
459k
              & -hdr->sh_addralign)))
991
250
    return false;
992
993
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
994
     only link a single copy of the section.  This is used to support
995
     g++.  g++ will emit each template expansion in its own section.
996
     The symbols will be defined as weak, so that multiple definitions
997
     are permitted.  The GNU linker extension is to actually discard
998
     all but one of the sections.  */
999
459k
  if (startswith (name, ".gnu.linkonce")
1000
2.19k
      && elf_next_in_group (newsect) == NULL)
1001
2.19k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1002
1003
459k
  if (!bfd_set_section_flags (newsect, flags))
1004
0
    return false;
1005
1006
459k
  bed = get_elf_backend_data (abfd);
1007
459k
  if (bed->elf_backend_section_flags)
1008
128k
    if (!bed->elf_backend_section_flags (hdr))
1009
0
      return false;
1010
1011
  /* We do not parse the PT_NOTE segments as we are interested even in the
1012
     separate debug info files which may have the segments offsets corrupted.
1013
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1014
459k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1015
7.49k
    {
1016
7.49k
      bfd_byte *contents;
1017
1018
7.49k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1019
1.46k
  return false;
1020
1021
6.03k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1022
6.03k
           hdr->sh_offset, hdr->sh_addralign);
1023
6.03k
      _bfd_elf_munmap_section_contents (newsect, contents);
1024
6.03k
    }
1025
1026
457k
  if ((newsect->flags & SEC_ALLOC) != 0)
1027
196k
    {
1028
196k
      Elf_Internal_Phdr *phdr;
1029
196k
      unsigned int i, nload;
1030
1031
      /* Some ELF linkers produce binaries with all the program header
1032
   p_paddr fields zero.  If we have such a binary with more than
1033
   one PT_LOAD header, then leave the section lma equal to vma
1034
   so that we don't create sections with overlapping lma.  */
1035
196k
      phdr = elf_tdata (abfd)->phdr;
1036
217k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1037
77.0k
  if (phdr->p_paddr != 0)
1038
55.8k
    break;
1039
21.1k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1040
2.68k
    ++nload;
1041
196k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1042
126
  return true;
1043
1044
196k
      phdr = elf_tdata (abfd)->phdr;
1045
2.88M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1046
2.70M
  {
1047
2.70M
    if (((phdr->p_type == PT_LOAD
1048
100k
    && (hdr->sh_flags & SHF_TLS) == 0)
1049
2.61M
         || phdr->p_type == PT_TLS)
1050
102k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1051
16.4k
      {
1052
16.4k
        if ((newsect->flags & SEC_LOAD) == 0)
1053
900
    newsect->lma = (phdr->p_paddr
1054
900
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1055
15.5k
        else
1056
    /* We used to use the same adjustment for SEC_LOAD
1057
       sections, but that doesn't work if the segment
1058
       is packed with code from multiple VMAs.
1059
       Instead we calculate the section LMA based on
1060
       the segment LMA.  It is assumed that the
1061
       segment will contain sections with contiguous
1062
       LMAs, even if the VMAs are not.  */
1063
15.5k
    newsect->lma = (phdr->p_paddr
1064
15.5k
        + hdr->sh_offset - phdr->p_offset) / opb;
1065
1066
        /* With contiguous segments, we can't tell from file
1067
     offsets whether a section with zero size should
1068
     be placed at the end of one segment or the
1069
     beginning of the next.  Decide based on vaddr.  */
1070
16.4k
        if (hdr->sh_addr >= phdr->p_vaddr
1071
16.4k
      && (hdr->sh_addr + hdr->sh_size
1072
16.4k
          <= phdr->p_vaddr + phdr->p_memsz))
1073
15.2k
    break;
1074
16.4k
      }
1075
2.70M
  }
1076
196k
    }
1077
1078
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1079
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1080
457k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1081
38.2k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1082
38.2k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1083
35.8k
    {
1084
35.8k
      enum { nothing, compress, decompress } action = nothing;
1085
35.8k
      int compression_header_size;
1086
35.8k
      bfd_size_type uncompressed_size;
1087
35.8k
      unsigned int uncompressed_align_power;
1088
35.8k
      enum compression_type ch_type = ch_none;
1089
35.8k
      bool compressed
1090
35.8k
  = bfd_is_section_compressed_info (abfd, newsect,
1091
35.8k
            &compression_header_size,
1092
35.8k
            &uncompressed_size,
1093
35.8k
            &uncompressed_align_power,
1094
35.8k
            &ch_type);
1095
1096
      /* Should we decompress?  */
1097
35.8k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1098
213
  action = decompress;
1099
1100
      /* Should we compress?  Or convert to a different compression?  */
1101
35.6k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1102
2.20k
         && newsect->size != 0
1103
2.15k
         && compression_header_size >= 0
1104
2.08k
         && uncompressed_size > 0)
1105
2.08k
  {
1106
2.08k
    if (!compressed)
1107
2.05k
      action = compress;
1108
23
    else
1109
23
      {
1110
23
        enum compression_type new_ch_type = ch_none;
1111
23
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1112
23
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1113
23
             ? ch_compress_zstd : ch_compress_zlib);
1114
23
        if (new_ch_type != ch_type)
1115
11
    action = compress;
1116
23
      }
1117
2.08k
  }
1118
1119
35.8k
      if (action == compress)
1120
2.07k
  {
1121
2.07k
    if (!bfd_init_section_compress_status (abfd, newsect))
1122
150
      {
1123
150
        _bfd_error_handler
1124
    /* xgettext:c-format */
1125
150
    (_("%pB: unable to compress section %s"), abfd, name);
1126
150
        return false;
1127
150
      }
1128
2.07k
  }
1129
33.7k
      else if (action == decompress)
1130
213
  {
1131
213
    if (!bfd_init_section_decompress_status (abfd, newsect))
1132
166
      {
1133
166
        _bfd_error_handler
1134
    /* xgettext:c-format */
1135
166
    (_("%pB: unable to decompress section %s"), abfd, name);
1136
166
        return false;
1137
166
      }
1138
47
#ifndef HAVE_ZSTD
1139
47
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1140
9
      {
1141
9
        _bfd_error_handler
1142
      /* xgettext:c-format */
1143
9
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1144
9
          "is not built with zstd support"),
1145
9
       abfd, name);
1146
9
        newsect->compress_status = COMPRESS_SECTION_NONE;
1147
9
        return false;
1148
9
      }
1149
38
#endif
1150
38
    if (abfd->is_linker_input
1151
0
        && name[1] == 'z')
1152
0
      {
1153
        /* Rename section from .zdebug_* to .debug_* so that ld
1154
     scripts will see this section as a debug section.  */
1155
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1156
0
        if (new_name == NULL)
1157
0
    return false;
1158
0
        bfd_rename_section (newsect, new_name);
1159
0
      }
1160
38
  }
1161
35.8k
    }
1162
1163
457k
  return true;
1164
457k
}
1165
1166
const char *const bfd_elf_section_type_names[] =
1167
{
1168
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1169
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1170
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1171
};
1172
1173
/* ELF relocs are against symbols.  If we are producing relocatable
1174
   output, and the reloc is against an external symbol, and nothing
1175
   has given us any additional addend, the resulting reloc will also
1176
   be against the same symbol.  In such a case, we don't want to
1177
   change anything about the way the reloc is handled, since it will
1178
   all be done at final link time.  Rather than put special case code
1179
   into bfd_perform_relocation, all the reloc types use this howto
1180
   function, or should call this function for relocatable output.  */
1181
1182
bfd_reloc_status_type
1183
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1184
           arelent *reloc_entry,
1185
           asymbol *symbol,
1186
           void *data ATTRIBUTE_UNUSED,
1187
           asection *input_section,
1188
           bfd *output_bfd,
1189
           char **error_message ATTRIBUTE_UNUSED)
1190
12.0k
{
1191
12.0k
  if (output_bfd != NULL
1192
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1193
0
      && (! reloc_entry->howto->partial_inplace
1194
0
    || reloc_entry->addend == 0))
1195
0
    {
1196
0
      reloc_entry->address += input_section->output_offset;
1197
0
      return bfd_reloc_ok;
1198
0
    }
1199
1200
  /* In some cases the relocation should be treated as output section
1201
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1202
     targets lack section relative relocations and instead use
1203
     ordinary absolute relocations for references between DWARF
1204
     sections.  That is arguably a bug in those targets but it happens
1205
     to work for the usual case of linking to non-loaded ELF debug
1206
     sections with VMAs forced to zero.  PE COFF on the other hand
1207
     doesn't allow a section VMA of zero.  */
1208
12.0k
  if (output_bfd == NULL
1209
12.0k
      && !reloc_entry->howto->pc_relative
1210
11.4k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1211
4.16k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1212
3.97k
    reloc_entry->addend -= symbol->section->output_section->vma;
1213
1214
12.0k
  return bfd_reloc_continue;
1215
12.0k
}
1216

1217
/* Returns TRUE if section A matches section B.
1218
   Names, addresses and links may be different, but everything else
1219
   should be the same.  */
1220
1221
static bool
1222
section_match (const Elf_Internal_Shdr * a,
1223
         const Elf_Internal_Shdr * b)
1224
56
{
1225
56
  if (a->sh_type != b->sh_type
1226
1
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1227
1
      || a->sh_addralign != b->sh_addralign
1228
1
      || a->sh_entsize != b->sh_entsize)
1229
55
    return false;
1230
1
  if (a->sh_type == SHT_SYMTAB
1231
0
      || a->sh_type == SHT_STRTAB)
1232
1
    return true;
1233
0
  return a->sh_size == b->sh_size;
1234
1
}
1235
1236
/* Find a section in OBFD that has the same characteristics
1237
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1238
   none can be found.  Check's section HINT first, as this is likely
1239
   to be the correct section.  */
1240
1241
static unsigned int
1242
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1243
     const unsigned int hint)
1244
5
{
1245
5
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1246
5
  unsigned int i;
1247
1248
5
  BFD_ASSERT (iheader != NULL);
1249
1250
  /* See PR 20922 for a reproducer of the NULL test.  */
1251
5
  if (hint < elf_numsections (obfd)
1252
4
      && oheaders[hint] != NULL
1253
4
      && section_match (oheaders[hint], iheader))
1254
0
    return hint;
1255
1256
56
  for (i = 1; i < elf_numsections (obfd); i++)
1257
52
    {
1258
52
      Elf_Internal_Shdr * oheader = oheaders[i];
1259
1260
52
      if (oheader == NULL)
1261
0
  continue;
1262
52
      if (section_match (oheader, iheader))
1263
  /* FIXME: Do we care if there is a potential for
1264
     multiple matches ?  */
1265
1
  return i;
1266
52
    }
1267
1268
4
  return SHN_UNDEF;
1269
5
}
1270
1271
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1272
   Processor specific section, based upon a matching input section.
1273
   Returns TRUE upon success, FALSE otherwise.  */
1274
1275
static bool
1276
copy_special_section_fields (const bfd *ibfd,
1277
           bfd *obfd,
1278
           const Elf_Internal_Shdr *iheader,
1279
           Elf_Internal_Shdr *oheader,
1280
           const unsigned int secnum)
1281
99
{
1282
99
  elf_backend_data *bed = get_elf_backend_data (obfd);
1283
99
  const Elf_Internal_Shdr **iheaders
1284
99
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1285
99
  bool changed = false;
1286
99
  unsigned int sh_link;
1287
1288
99
  if (oheader->sh_type == SHT_NOBITS)
1289
20
    {
1290
      /* This is a feature for objcopy --only-keep-debug:
1291
   When a section's type is changed to NOBITS, we preserve
1292
   the sh_link and sh_info fields so that they can be
1293
   matched up with the original.
1294
1295
   Note: Strictly speaking these assignments are wrong.
1296
   The sh_link and sh_info fields should point to the
1297
   relevent sections in the output BFD, which may not be in
1298
   the same location as they were in the input BFD.  But
1299
   the whole point of this action is to preserve the
1300
   original values of the sh_link and sh_info fields, so
1301
   that they can be matched up with the section headers in
1302
   the original file.  So strictly speaking we may be
1303
   creating an invalid ELF file, but it is only for a file
1304
   that just contains debug info and only for sections
1305
   without any contents.  */
1306
20
      if (oheader->sh_link == 0)
1307
20
  oheader->sh_link = iheader->sh_link;
1308
20
      if (oheader->sh_info == 0)
1309
20
  oheader->sh_info = iheader->sh_info;
1310
20
      return true;
1311
20
    }
1312
1313
  /* Allow the target a chance to decide how these fields should be set.  */
1314
79
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1315
79
                iheader, oheader))
1316
72
    return true;
1317
1318
  /* We have an iheader which might match oheader, and which has non-zero
1319
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1320
     the section in the output bfd which corresponds to the linked section
1321
     in the input bfd.  */
1322
7
  if (iheader->sh_link != SHN_UNDEF)
1323
5
    {
1324
      /* See PR 20931 for a reproducer.  */
1325
5
      if (iheader->sh_link >= elf_numsections (ibfd))
1326
0
  {
1327
0
    _bfd_error_handler
1328
      /* xgettext:c-format */
1329
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1330
0
       ibfd, iheader->sh_link, secnum);
1331
0
    return false;
1332
0
  }
1333
1334
5
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1335
5
      if (sh_link != SHN_UNDEF)
1336
1
  {
1337
1
    oheader->sh_link = sh_link;
1338
1
    changed = true;
1339
1
  }
1340
4
      else
1341
  /* FIXME: Should we install iheader->sh_link
1342
     if we could not find a match ?  */
1343
4
  _bfd_error_handler
1344
    /* xgettext:c-format */
1345
4
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1346
5
    }
1347
1348
7
  if (iheader->sh_info)
1349
0
    {
1350
      /* The sh_info field can hold arbitrary information, but if the
1351
   SHF_LINK_INFO flag is set then it should be interpreted as a
1352
   section index.  */
1353
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1354
0
  {
1355
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1356
0
             iheader->sh_info);
1357
0
    if (sh_link != SHN_UNDEF)
1358
0
      oheader->sh_flags |= SHF_INFO_LINK;
1359
0
  }
1360
0
      else
1361
  /* No idea what it means - just copy it.  */
1362
0
  sh_link = iheader->sh_info;
1363
1364
0
      if (sh_link != SHN_UNDEF)
1365
0
  {
1366
0
    oheader->sh_info = sh_link;
1367
0
    changed = true;
1368
0
  }
1369
0
      else
1370
0
  _bfd_error_handler
1371
    /* xgettext:c-format */
1372
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1373
0
    }
1374
1375
7
  return changed;
1376
7
}
1377
1378
/* Copy the program header and other data from one object module to
1379
   another.  */
1380
1381
bool
1382
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1383
131
{
1384
131
  const Elf_Internal_Shdr **iheaders;
1385
131
  Elf_Internal_Shdr **oheaders;
1386
131
  elf_backend_data *bed;
1387
131
  unsigned int i;
1388
1389
131
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1390
0
    return true;
1391
1392
131
  if (!elf_flags_init (obfd))
1393
131
    {
1394
131
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1395
131
      elf_flags_init (obfd) = true;
1396
131
    }
1397
1398
131
  elf_gp (obfd) = elf_gp (ibfd);
1399
1400
  /* Also copy the EI_OSABI field.  */
1401
131
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1402
131
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1403
1404
  /* If set, copy the EI_ABIVERSION field.  */
1405
131
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1406
15
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1407
15
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1408
1409
  /* Copy object attributes.  */
1410
131
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1411
1412
131
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1413
131
  oheaders = elf_elfsections (obfd);
1414
131
  if (iheaders == NULL || oheaders == NULL)
1415
81
    return true;
1416
1417
50
  bed = get_elf_backend_data (obfd);
1418
1419
  /* Possibly copy other fields in the section header.  */
1420
1.25k
  for (i = 1; i < elf_numsections (obfd); i++)
1421
1.20k
    {
1422
1.20k
      unsigned int j;
1423
1.20k
      Elf_Internal_Shdr * oheader = oheaders[i];
1424
1425
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1426
   because of a special case need for generating separate debug info
1427
   files.  See below for more details.  */
1428
1.20k
      if (oheader == NULL
1429
1.20k
    || (oheader->sh_type != SHT_NOBITS
1430
1.18k
        && oheader->sh_type < SHT_LOOS))
1431
1.09k
  continue;
1432
1433
      /* Ignore empty sections, and sections whose
1434
   fields have already been initialised.  */
1435
110
      if (oheader->sh_size == 0
1436
99
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1437
13
  continue;
1438
1439
      /* Scan for the matching section in the input bfd.
1440
   First we try for a direct mapping between the input and
1441
   output sections.  */
1442
1.87k
      for (j = 1; j < elf_numsections (ibfd); j++)
1443
1.87k
  {
1444
1.87k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1445
1446
1.87k
    if (iheader == NULL)
1447
0
      continue;
1448
1449
1.87k
    if (oheader->bfd_section != NULL
1450
1.87k
        && iheader->bfd_section != NULL
1451
1.35k
        && iheader->bfd_section->output_section != NULL
1452
1.35k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1453
97
      {
1454
        /* We have found a connection from the input section to
1455
     the output section.  Attempt to copy the header fields.
1456
     If this fails then do not try any further sections -
1457
     there should only be a one-to-one mapping between
1458
     input and output.  */
1459
97
        if (!copy_special_section_fields (ibfd, obfd,
1460
97
            iheader, oheader, i))
1461
4
    j = elf_numsections (ibfd);
1462
97
        break;
1463
97
      }
1464
1.87k
  }
1465
1466
97
      if (j < elf_numsections (ibfd))
1467
93
  continue;
1468
1469
      /* That failed.  So try to deduce the corresponding input section.
1470
   Unfortunately we cannot compare names as the output string table
1471
   is empty, so instead we check size, address and type.  */
1472
96
      for (j = 1; j < elf_numsections (ibfd); j++)
1473
92
  {
1474
92
    const Elf_Internal_Shdr * iheader = iheaders[j];
1475
1476
92
    if (iheader == NULL)
1477
0
      continue;
1478
1479
    /* Try matching fields in the input section's header.
1480
       Since --only-keep-debug turns all non-debug sections into
1481
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1482
       input type.  */
1483
92
    if ((oheader->sh_type == SHT_NOBITS
1484
92
         || iheader->sh_type == oheader->sh_type)
1485
5
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1486
5
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1487
4
        && iheader->sh_addralign == oheader->sh_addralign
1488
4
        && iheader->sh_entsize == oheader->sh_entsize
1489
4
        && iheader->sh_size == oheader->sh_size
1490
4
        && iheader->sh_addr == oheader->sh_addr
1491
4
        && (iheader->sh_info != oheader->sh_info
1492
4
      || iheader->sh_link != oheader->sh_link))
1493
2
      {
1494
2
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1495
0
    break;
1496
2
      }
1497
92
  }
1498
1499
4
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1500
4
  {
1501
    /* Final attempt.  Call the backend copy function
1502
       with a NULL input section.  */
1503
4
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1504
4
                     NULL, oheader);
1505
4
  }
1506
4
    }
1507
1508
50
  return true;
1509
131
}
1510
1511
static const char *
1512
get_segment_type (unsigned int p_type)
1513
165k
{
1514
165k
  const char *pt;
1515
165k
  switch (p_type)
1516
165k
    {
1517
32.0k
    case PT_NULL: pt = "NULL"; break;
1518
2.20k
    case PT_LOAD: pt = "LOAD"; break;
1519
794
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1520
609
    case PT_INTERP: pt = "INTERP"; break;
1521
301
    case PT_NOTE: pt = "NOTE"; break;
1522
146
    case PT_SHLIB: pt = "SHLIB"; break;
1523
2.49k
    case PT_PHDR: pt = "PHDR"; break;
1524
66
    case PT_TLS: pt = "TLS"; break;
1525
225
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1526
181
    case PT_GNU_STACK: pt = "STACK"; break;
1527
163
    case PT_GNU_RELRO: pt = "RELRO"; break;
1528
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1529
125k
    default: pt = NULL; break;
1530
165k
    }
1531
165k
  return pt;
1532
165k
}
1533
1534
/* Print out the program headers.  */
1535
1536
bool
1537
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1538
8.43k
{
1539
8.43k
  FILE *f = (FILE *) farg;
1540
8.43k
  Elf_Internal_Phdr *p;
1541
8.43k
  asection *s;
1542
8.43k
  bfd_byte *dynbuf = NULL;
1543
1544
8.43k
  p = elf_tdata (abfd)->phdr;
1545
8.43k
  if (p != NULL)
1546
5.90k
    {
1547
5.90k
      unsigned int i, c;
1548
1549
5.90k
      fprintf (f, _("\nProgram Header:\n"));
1550
5.90k
      c = elf_elfheader (abfd)->e_phnum;
1551
171k
      for (i = 0; i < c; i++, p++)
1552
165k
  {
1553
165k
    const char *pt = get_segment_type (p->p_type);
1554
165k
    char buf[20];
1555
1556
165k
    if (pt == NULL)
1557
125k
      {
1558
125k
        sprintf (buf, "0x%lx", p->p_type);
1559
125k
        pt = buf;
1560
125k
      }
1561
165k
    fprintf (f, "%8s off    0x", pt);
1562
165k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1563
165k
    fprintf (f, " vaddr 0x");
1564
165k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1565
165k
    fprintf (f, " paddr 0x");
1566
165k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1567
165k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1568
165k
    fprintf (f, "         filesz 0x");
1569
165k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1570
165k
    fprintf (f, " memsz 0x");
1571
165k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1572
165k
    fprintf (f, " flags %c%c%c",
1573
165k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1574
165k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1575
165k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1576
165k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1577
116k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1578
165k
    fprintf (f, "\n");
1579
165k
  }
1580
5.90k
    }
1581
1582
8.43k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1583
8.43k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1584
201
    {
1585
201
      unsigned int elfsec;
1586
201
      unsigned long shlink;
1587
201
      bfd_byte *extdyn, *extdynend;
1588
201
      size_t extdynsize;
1589
201
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1590
1591
201
      fprintf (f, _("\nDynamic Section:\n"));
1592
1593
201
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1594
13
  goto error_return;
1595
1596
188
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1597
188
      if (elfsec == SHN_BAD)
1598
0
  goto error_return;
1599
188
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1600
1601
188
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1602
188
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1603
1604
188
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1605
3.48k
     (size_t) (extdynend - extdyn) >= extdynsize;
1606
3.29k
     extdyn += extdynsize)
1607
3.46k
  {
1608
3.46k
    Elf_Internal_Dyn dyn;
1609
3.46k
    const char *name = "";
1610
3.46k
    char ab[20];
1611
3.46k
    bool stringp;
1612
3.46k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1613
1614
3.46k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1615
1616
3.46k
    if (dyn.d_tag == DT_NULL)
1617
163
      break;
1618
1619
3.30k
    stringp = false;
1620
3.30k
    switch (dyn.d_tag)
1621
3.30k
      {
1622
786
      default:
1623
786
        if (bed->elf_backend_get_target_dtag)
1624
81
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1625
1626
786
        if (streq (name, ""))
1627
786
    {
1628
786
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1629
786
      name = ab;
1630
786
    }
1631
786
        break;
1632
1633
140
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1634
53
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1635
110
      case DT_PLTGOT: name = "PLTGOT"; break;
1636
65
      case DT_HASH: name = "HASH"; break;
1637
120
      case DT_STRTAB: name = "STRTAB"; break;
1638
123
      case DT_SYMTAB: name = "SYMTAB"; break;
1639
109
      case DT_RELA: name = "RELA"; break;
1640
119
      case DT_RELASZ: name = "RELASZ"; break;
1641
113
      case DT_RELAENT: name = "RELAENT"; break;
1642
113
      case DT_STRSZ: name = "STRSZ"; break;
1643
110
      case DT_SYMENT: name = "SYMENT"; break;
1644
114
      case DT_INIT: name = "INIT"; break;
1645
114
      case DT_FINI: name = "FINI"; break;
1646
2
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1647
22
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1648
2
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1649
4
      case DT_REL: name = "REL"; break;
1650
4
      case DT_RELSZ: name = "RELSZ"; break;
1651
3
      case DT_RELENT: name = "RELENT"; break;
1652
3
      case DT_RELR: name = "RELR"; break;
1653
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1654
0
      case DT_RELRENT: name = "RELRENT"; break;
1655
56
      case DT_PLTREL: name = "PLTREL"; break;
1656
109
      case DT_DEBUG: name = "DEBUG"; break;
1657
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1658
52
      case DT_JMPREL: name = "JMPREL"; break;
1659
2
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1660
67
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1661
68
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1662
65
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1663
68
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1664
1
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1665
60
      case DT_FLAGS: name = "FLAGS"; break;
1666
3
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1667
0
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1668
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1669
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1670
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1671
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1672
0
      case DT_FEATURE: name = "FEATURE"; break;
1673
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1674
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1675
2
      case DT_SYMINENT: name = "SYMINENT"; break;
1676
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1677
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1678
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1679
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1680
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1681
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1682
75
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1683
2
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1684
78
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1685
94
      case DT_VERSYM: name = "VERSYM"; break;
1686
0
      case DT_VERDEF: name = "VERDEF"; break;
1687
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1688
94
      case DT_VERNEED: name = "VERNEED"; break;
1689
88
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1690
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1691
0
      case DT_USED: name = "USED"; break;
1692
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1693
87
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1694
3.30k
      }
1695
1696
3.30k
    fprintf (f, "  %-20s ", name);
1697
3.30k
    if (! stringp)
1698
3.13k
      {
1699
3.13k
        fprintf (f, "0x");
1700
3.13k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1701
3.13k
      }
1702
165
    else
1703
165
      {
1704
165
        const char *string;
1705
165
        unsigned int tagv = dyn.d_un.d_val;
1706
1707
165
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1708
165
        if (string == NULL)
1709
8
    goto error_return;
1710
157
        fprintf (f, "%s", string);
1711
157
      }
1712
3.29k
    fprintf (f, "\n");
1713
3.29k
  }
1714
1715
180
      _bfd_elf_munmap_section_contents (s, dynbuf);
1716
180
      dynbuf = NULL;
1717
180
    }
1718
1719
8.40k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1720
8.40k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1721
163
    {
1722
163
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1723
56
  return false;
1724
163
    }
1725
1726
8.35k
  if (elf_dynverdef (abfd) != 0)
1727
0
    {
1728
0
      Elf_Internal_Verdef *t;
1729
1730
0
      fprintf (f, _("\nVersion definitions:\n"));
1731
0
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1732
0
  {
1733
0
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1734
0
       t->vd_flags, t->vd_hash,
1735
0
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1736
0
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1737
0
      {
1738
0
        Elf_Internal_Verdaux *a;
1739
1740
0
        fprintf (f, "\t");
1741
0
        for (a = t->vd_auxptr->vda_nextptr;
1742
0
       a != NULL;
1743
0
       a = a->vda_nextptr)
1744
0
    fprintf (f, "%s ",
1745
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1746
0
        fprintf (f, "\n");
1747
0
      }
1748
0
  }
1749
0
    }
1750
1751
8.35k
  if (elf_dynverref (abfd) != 0)
1752
107
    {
1753
107
      Elf_Internal_Verneed *t;
1754
1755
107
      fprintf (f, _("\nVersion References:\n"));
1756
214
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1757
107
  {
1758
107
    Elf_Internal_Vernaux *a;
1759
1760
107
    fprintf (f, _("  required from %s:\n"),
1761
107
       t->vn_filename ? t->vn_filename : "<corrupt>");
1762
267
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1763
160
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1764
160
         a->vna_flags, a->vna_other,
1765
160
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1766
107
  }
1767
107
    }
1768
1769
8.35k
  return true;
1770
1771
21
 error_return:
1772
21
  _bfd_elf_munmap_section_contents (s, dynbuf);
1773
21
  return false;
1774
8.40k
}
1775
1776
/* Find the file offset corresponding to VMA by using the program
1777
   headers.  */
1778
1779
static file_ptr
1780
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1781
     size_t size, size_t *max_size_p)
1782
2.44k
{
1783
2.44k
  Elf_Internal_Phdr *seg;
1784
2.44k
  size_t i;
1785
1786
8.83k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1787
8.59k
    if (seg->p_type == PT_LOAD
1788
3.69k
  && vma >= (seg->p_vaddr & -seg->p_align)
1789
2.87k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1790
2.19k
      {
1791
2.19k
  if (max_size_p)
1792
32
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1793
2.19k
  return vma - seg->p_vaddr + seg->p_offset;
1794
2.19k
      }
1795
1796
245
  if (max_size_p)
1797
26
    *max_size_p = 0;
1798
245
  bfd_set_error (bfd_error_invalid_operation);
1799
245
  return (file_ptr) -1;
1800
2.44k
}
1801
1802
/* Convert hash table to internal form.  */
1803
1804
static bfd_vma *
1805
get_hash_table_data (bfd *abfd, bfd_size_type number,
1806
         unsigned int ent_size, bfd_size_type filesize)
1807
347
{
1808
347
  unsigned char *e_data = NULL;
1809
347
  bfd_vma *i_data = NULL;
1810
347
  bfd_size_type size;
1811
347
  void *e_data_addr;
1812
347
  size_t e_data_size ATTRIBUTE_UNUSED;
1813
1814
347
  if (ent_size != 4 && ent_size != 8)
1815
0
    return NULL;
1816
1817
347
  if ((size_t) number != number)
1818
0
    {
1819
0
      bfd_set_error (bfd_error_file_too_big);
1820
0
      return NULL;
1821
0
    }
1822
1823
347
  size = ent_size * number;
1824
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1825
     attempting to allocate memory when the read is bound to fail.  */
1826
347
  if (size > filesize
1827
318
      || number >= ~(size_t) 0 / ent_size
1828
318
      || number >= ~(size_t) 0 / sizeof (*i_data))
1829
29
    {
1830
29
      bfd_set_error (bfd_error_file_too_big);
1831
29
      return NULL;
1832
29
    }
1833
1834
318
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1835
318
  if (e_data == NULL)
1836
20
    return NULL;
1837
1838
298
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1839
298
  if (i_data == NULL)
1840
0
    {
1841
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1842
0
      return NULL;
1843
0
    }
1844
1845
298
  if (ent_size == 4)
1846
280k
    while (number--)
1847
280k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1848
0
  else
1849
0
    while (number--)
1850
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1851
1852
298
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1853
298
  return i_data;
1854
298
}
1855
1856
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1857
   support DT_MIPS_XHASH?  */
1858
106k
#define DT_MIPS_XHASH        0x70000036
1859
1860
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1861
1862
bool
1863
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1864
            Elf_Internal_Phdr *phdrs, size_t phnum,
1865
            bfd_size_type filesize)
1866
3.04k
{
1867
3.04k
  bfd_byte *extdyn, *extdynend;
1868
3.04k
  size_t extdynsize;
1869
3.04k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1870
3.04k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1871
3.04k
        Elf_Internal_Sym *);
1872
3.04k
  Elf_Internal_Dyn dyn;
1873
3.04k
  bfd_vma dt_hash = 0;
1874
3.04k
  bfd_vma dt_gnu_hash = 0;
1875
3.04k
  bfd_vma dt_mips_xhash = 0;
1876
3.04k
  bfd_vma dt_strtab = 0;
1877
3.04k
  bfd_vma dt_symtab = 0;
1878
3.04k
  size_t dt_strsz = 0;
1879
3.04k
  bfd_vma dt_versym = 0;
1880
3.04k
  bfd_vma dt_verdef = 0;
1881
3.04k
  bfd_vma dt_verneed = 0;
1882
3.04k
  bfd_byte *dynbuf = NULL;
1883
3.04k
  char *strbuf = NULL;
1884
3.04k
  bfd_vma *gnubuckets = NULL;
1885
3.04k
  bfd_vma *gnuchains = NULL;
1886
3.04k
  bfd_vma *mipsxlat = NULL;
1887
3.04k
  file_ptr saved_filepos, filepos;
1888
3.04k
  bool res = false;
1889
3.04k
  size_t amt;
1890
3.04k
  bfd_byte *esymbuf = NULL, *esym;
1891
3.04k
  bfd_size_type symcount;
1892
3.04k
  Elf_Internal_Sym *isymbuf = NULL;
1893
3.04k
  Elf_Internal_Sym *isym, *isymend;
1894
3.04k
  bfd_byte *versym = NULL;
1895
3.04k
  bfd_byte *verdef = NULL;
1896
3.04k
  bfd_byte *verneed = NULL;
1897
3.04k
  size_t verdef_size = 0;
1898
3.04k
  size_t verneed_size = 0;
1899
3.04k
  size_t extsym_size;
1900
3.04k
  elf_backend_data *bed;
1901
3.04k
  void *dynbuf_addr = NULL;
1902
3.04k
  void *esymbuf_addr = NULL;
1903
3.04k
  size_t dynbuf_size = 0;
1904
3.04k
  size_t esymbuf_size = 0;
1905
1906
  /* Return TRUE if symbol table is bad.  */
1907
3.04k
  if (elf_bad_symtab (abfd))
1908
0
    return true;
1909
1910
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1911
3.04k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1912
0
    return true;
1913
1914
3.04k
  bed = get_elf_backend_data (abfd);
1915
1916
  /* Save file position for elf_object_p.  */
1917
3.04k
  saved_filepos = bfd_tell (abfd);
1918
1919
3.04k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1920
0
    goto error_return;
1921
1922
3.04k
  dynbuf_size = phdr->p_filesz;
1923
3.04k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1924
3.04k
  if (dynbuf == NULL)
1925
51
    goto error_return;
1926
1927
2.99k
  extsym_size = bed->s->sizeof_sym;
1928
2.99k
  extdynsize = bed->s->sizeof_dyn;
1929
2.99k
  swap_dyn_in = bed->s->swap_dyn_in;
1930
1931
2.99k
  extdyn = dynbuf;
1932
2.99k
  if (phdr->p_filesz < extdynsize)
1933
45
    goto error_return;
1934
2.95k
  extdynend = extdyn + phdr->p_filesz;
1935
63.2k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1936
61.7k
    {
1937
61.7k
      swap_dyn_in (abfd, extdyn, &dyn);
1938
1939
61.7k
      if (dyn.d_tag == DT_NULL)
1940
1.29k
  break;
1941
1942
60.4k
      switch (dyn.d_tag)
1943
60.4k
  {
1944
1.16k
  case DT_HASH:
1945
1.16k
    dt_hash = dyn.d_un.d_val;
1946
1.16k
    break;
1947
943
  case DT_GNU_HASH:
1948
943
    if (bed->elf_machine_code != EM_MIPS
1949
777
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1950
777
      dt_gnu_hash = dyn.d_un.d_val;
1951
943
    break;
1952
1.31k
  case DT_STRTAB:
1953
1.31k
    dt_strtab = dyn.d_un.d_val;
1954
1.31k
    break;
1955
1.08k
  case DT_SYMTAB:
1956
1.08k
    dt_symtab = dyn.d_un.d_val;
1957
1.08k
    break;
1958
1.13k
  case DT_STRSZ:
1959
1.13k
    dt_strsz = dyn.d_un.d_val;
1960
1.13k
    break;
1961
510
  case DT_SYMENT:
1962
510
    if (dyn.d_un.d_val != extsym_size)
1963
127
      goto error_return;
1964
383
    break;
1965
519
  case DT_VERSYM:
1966
519
    dt_versym = dyn.d_un.d_val;
1967
519
    break;
1968
277
  case DT_VERDEF:
1969
277
    dt_verdef = dyn.d_un.d_val;
1970
277
    break;
1971
298
  case DT_VERNEED:
1972
298
    dt_verneed = dyn.d_un.d_val;
1973
298
    break;
1974
53.2k
  default:
1975
53.2k
    if (dyn.d_tag == DT_MIPS_XHASH
1976
547
        && (bed->elf_machine_code == EM_MIPS
1977
257
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1978
290
      {
1979
290
        dt_gnu_hash = dyn.d_un.d_val;
1980
290
        dt_mips_xhash = dyn.d_un.d_val;
1981
290
      }
1982
53.2k
    break;
1983
60.4k
  }
1984
60.4k
    }
1985
1986
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1987
     segment.  */
1988
2.82k
  if ((!dt_hash && !dt_gnu_hash)
1989
1.70k
      || !dt_strtab
1990
1.16k
      || !dt_symtab
1991
899
      || !dt_strsz)
1992
2.04k
    goto error_return;
1993
1994
  /* Get dynamic string table.  */
1995
776
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1996
776
  if (filepos == (file_ptr) -1
1997
695
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1998
113
    goto error_return;
1999
2000
  /* Dynamic string table must be valid until ABFD is closed.  */
2001
663
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2002
663
  if (strbuf == NULL)
2003
16
    goto error_return;
2004
647
  if (strbuf[dt_strsz - 1] != 0)
2005
169
    {
2006
      /* It is an error if a string table is't terminated.  */
2007
169
      _bfd_error_handler
2008
  /* xgettext:c-format */
2009
169
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2010
169
      strbuf[dt_strsz - 1] = 0;
2011
169
    }
2012
2013
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2014
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2015
647
  if (dt_hash)
2016
254
    {
2017
254
      unsigned char nb[16];
2018
254
      unsigned int hash_ent_size;
2019
2020
254
      switch (bed->elf_machine_code)
2021
254
  {
2022
0
  case EM_ALPHA:
2023
0
  case EM_S390:
2024
0
  case EM_S390_OLD:
2025
0
    if (bed->s->elfclass == ELFCLASS64)
2026
0
      {
2027
0
        hash_ent_size = 8;
2028
0
        break;
2029
0
      }
2030
    /* FALLTHROUGH */
2031
254
  default:
2032
254
    hash_ent_size = 4;
2033
254
    break;
2034
254
  }
2035
2036
254
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2037
254
         NULL);
2038
254
      if (filepos == (file_ptr) -1
2039
241
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2040
232
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2041
41
  goto error_return;
2042
2043
      /* The number of dynamic symbol table entries equals the number
2044
   of chains.  */
2045
213
      if (hash_ent_size == 8)
2046
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2047
213
      else
2048
213
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2049
213
    }
2050
393
  else
2051
393
    {
2052
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2053
   bindings are in hash table.  Since in dynamic symbol table,
2054
   all symbols with STB_LOCAL binding are placed before symbols
2055
   with other bindings and all undefined symbols are placed
2056
   before defined ones, the highest symbol index in DT_GNU_HASH
2057
   is the highest dynamic symbol table index.  */
2058
393
      unsigned char nb[16];
2059
393
      bfd_vma ngnubuckets;
2060
393
      bfd_vma gnusymidx;
2061
393
      size_t i, ngnuchains;
2062
393
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2063
393
      bfd_vma buckets_vma;
2064
2065
393
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2066
393
         sizeof (nb), NULL);
2067
393
      if (filepos == (file_ptr) -1
2068
377
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2069
360
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2070
36
  goto error_return;
2071
2072
357
      ngnubuckets = bfd_get_32 (abfd, nb);
2073
357
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2074
357
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2075
357
      buckets_vma = dt_gnu_hash + 16;
2076
357
      if (bed->s->elfclass == ELFCLASS32)
2077
0
  buckets_vma += bitmaskwords * 4;
2078
357
      else
2079
357
  buckets_vma += bitmaskwords * 8;
2080
357
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2081
357
      if (filepos == (file_ptr) -1
2082
342
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2083
48
  goto error_return;
2084
2085
309
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2086
309
      if (gnubuckets == NULL)
2087
49
  goto error_return;
2088
2089
265k
      for (i = 0; i < ngnubuckets; i++)
2090
265k
  if (gnubuckets[i] != 0)
2091
209k
    {
2092
209k
      if (gnubuckets[i] < gnusymidx)
2093
22
        goto error_return;
2094
2095
209k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2096
19.9k
        maxchain = gnubuckets[i];
2097
209k
    }
2098
2099
238
      if (maxchain == 0xffffffff)
2100
23
  {
2101
23
    symcount = 0;
2102
23
    goto empty_gnu_hash;
2103
23
  }
2104
2105
215
      maxchain -= gnusymidx;
2106
215
      filepos = offset_from_vma (phdrs, phnum,
2107
215
         buckets_vma + 4 * (ngnubuckets + maxchain),
2108
215
         4, NULL);
2109
215
      if (filepos == (file_ptr) -1
2110
164
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2111
168
  goto error_return;
2112
2113
47
      do
2114
749
  {
2115
749
    if (bfd_read (nb, 4, abfd) != 4)
2116
9
      goto error_return;
2117
740
    ++maxchain;
2118
740
    if (maxchain == 0)
2119
0
      goto error_return;
2120
740
  }
2121
740
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2122
2123
38
      filepos = offset_from_vma (phdrs, phnum,
2124
38
         buckets_vma + 4 * ngnubuckets,
2125
38
         4, NULL);
2126
38
      if (filepos == (file_ptr) -1
2127
38
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2128
0
  goto error_return;
2129
2130
38
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2131
38
      if (gnuchains == NULL)
2132
0
  goto error_return;
2133
38
      ngnuchains = maxchain;
2134
2135
38
      if (dt_mips_xhash)
2136
0
  {
2137
0
    filepos = offset_from_vma (phdrs, phnum,
2138
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2139
0
             4, NULL);
2140
0
    if (filepos == (file_ptr) -1
2141
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2142
0
      goto error_return;
2143
2144
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2145
0
    if (mipsxlat == NULL)
2146
0
      goto error_return;
2147
0
  }
2148
2149
38
      symcount = 0;
2150
380
      for (i = 0; i < ngnubuckets; ++i)
2151
342
  if (gnubuckets[i] != 0)
2152
91
    {
2153
91
      bfd_vma si = gnubuckets[i];
2154
91
      bfd_vma off = si - gnusymidx;
2155
91
      do
2156
974
        {
2157
974
    if (mipsxlat)
2158
0
      {
2159
0
        if (mipsxlat[off] >= symcount)
2160
0
          symcount = mipsxlat[off] + 1;
2161
0
      }
2162
974
    else
2163
974
      {
2164
974
        if (si >= symcount)
2165
733
          symcount = si + 1;
2166
974
      }
2167
974
    si++;
2168
974
        }
2169
974
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2170
91
    }
2171
38
    }
2172
2173
  /* Swap in dynamic symbol table.  */
2174
251
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2175
0
    {
2176
0
      bfd_set_error (bfd_error_file_too_big);
2177
0
      goto error_return;
2178
0
    }
2179
2180
251
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2181
251
  if (filepos == (file_ptr) -1
2182
225
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2183
53
    goto error_return;
2184
198
  esymbuf_size = amt;
2185
198
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2186
198
         &esymbuf_addr, &esymbuf_size);
2187
198
  if (esymbuf == NULL)
2188
23
    goto error_return;
2189
2190
175
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2191
0
    {
2192
0
      bfd_set_error (bfd_error_file_too_big);
2193
0
      goto error_return;
2194
0
    }
2195
2196
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2197
175
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2198
175
  if (isymbuf == NULL)
2199
0
    goto error_return;
2200
2201
175
  swap_symbol_in = bed->s->swap_symbol_in;
2202
2203
  /* Convert the symbols to internal form.  */
2204
175
  isymend = isymbuf + symcount;
2205
175
  for (esym = esymbuf, isym = isymbuf;
2206
516
       isym < isymend;
2207
341
       esym += extsym_size, isym++)
2208
375
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2209
363
  || isym->st_name >= dt_strsz)
2210
34
      {
2211
34
  bfd_set_error (bfd_error_invalid_operation);
2212
34
  goto error_return;
2213
34
      }
2214
2215
141
  if (dt_versym)
2216
100
    {
2217
      /* Swap in DT_VERSYM.  */
2218
100
      if (_bfd_mul_overflow (symcount, 2, &amt))
2219
0
  {
2220
0
    bfd_set_error (bfd_error_file_too_big);
2221
0
    goto error_return;
2222
0
  }
2223
2224
100
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2225
100
      if (filepos == (file_ptr) -1
2226
83
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2227
32
  goto error_return;
2228
2229
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2230
68
      versym = _bfd_mmap_persistent (abfd, amt);
2231
2232
68
      if (dt_verdef)
2233
27
  {
2234
    /* Read in DT_VERDEF.  */
2235
27
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2236
27
             0, &verdef_size);
2237
27
    if (filepos == (file_ptr) -1
2238
11
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2239
23
      goto error_return;
2240
2241
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2242
4
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2243
4
  }
2244
2245
45
      if (dt_verneed)
2246
31
  {
2247
    /* Read in DT_VERNEED.  */
2248
31
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2249
31
             0, &verneed_size);
2250
31
    if (filepos == (file_ptr) -1
2251
21
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2252
15
      goto error_return;
2253
2254
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2255
16
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2256
16
  }
2257
45
    }
2258
2259
94
 empty_gnu_hash:
2260
94
  elf_tdata (abfd)->dt_strtab = strbuf;
2261
94
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2262
94
  elf_tdata (abfd)->dt_symtab = isymbuf;
2263
94
  elf_tdata (abfd)->dt_symtab_count = symcount;
2264
94
  elf_tdata (abfd)->dt_versym = versym;
2265
94
  elf_tdata (abfd)->dt_verdef = verdef;
2266
94
  elf_tdata (abfd)->dt_verneed = verneed;
2267
94
  elf_tdata (abfd)->dt_verdef_count
2268
94
    = verdef_size / sizeof (Elf_External_Verdef);
2269
94
  elf_tdata (abfd)->dt_verneed_count
2270
94
    = verneed_size / sizeof (Elf_External_Verneed);
2271
2272
94
  res = true;
2273
2274
3.04k
 error_return:
2275
  /* Restore file position for elf_object_p.  */
2276
3.04k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2277
0
    res = false;
2278
3.04k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2279
3.04k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2280
3.04k
  free (gnubuckets);
2281
3.04k
  free (gnuchains);
2282
3.04k
  free (mipsxlat);
2283
3.04k
  return res;
2284
94
}
2285
2286
/* Reconstruct section from dynamic symbol.  */
2287
2288
asection *
2289
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2290
            Elf_Internal_Sym *isym)
2291
1
{
2292
1
  asection *sec;
2293
1
  flagword flags;
2294
2295
1
  if (!elf_use_dt_symtab_p (abfd))
2296
0
    return NULL;
2297
2298
1
  flags = SEC_ALLOC | SEC_LOAD;
2299
1
  switch (ELF_ST_TYPE (isym->st_info))
2300
1
    {
2301
0
    case STT_FUNC:
2302
0
    case STT_GNU_IFUNC:
2303
0
      sec = bfd_get_section_by_name (abfd, ".text");
2304
0
      if (sec == NULL)
2305
0
  sec = bfd_make_section_with_flags (abfd,
2306
0
             ".text",
2307
0
             flags | SEC_CODE);
2308
0
      break;
2309
0
    case STT_COMMON:
2310
0
      sec = bfd_com_section_ptr;
2311
0
      break;
2312
0
    case STT_OBJECT:
2313
0
      sec = bfd_get_section_by_name (abfd, ".data");
2314
0
      if (sec == NULL)
2315
0
  sec = bfd_make_section_with_flags (abfd,
2316
0
             ".data",
2317
0
             flags | SEC_DATA);
2318
0
      break;
2319
0
    case STT_TLS:
2320
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2321
0
      if (sec == NULL)
2322
0
  sec = bfd_make_section_with_flags (abfd,
2323
0
             ".tdata",
2324
0
             (flags
2325
0
              | SEC_DATA
2326
0
              | SEC_THREAD_LOCAL));
2327
0
      break;
2328
1
    default:
2329
1
      sec = bfd_abs_section_ptr;
2330
1
      break;
2331
1
    }
2332
2333
1
  return sec;
2334
1
}
2335
2336
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2337
   and return symbol version for symbol version itself.   */
2338
2339
const char *
2340
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2341
            bool base_p,
2342
            bool *hidden)
2343
42.1k
{
2344
42.1k
  const char *version_string = NULL;
2345
42.1k
  if ((elf_dynversym (abfd) != 0
2346
4.99k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2347
37.1k
      || (elf_tdata (abfd)->dt_versym != NULL
2348
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2349
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2350
4.98k
    {
2351
4.98k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2352
2353
4.98k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2354
4.98k
      vernum &= VERSYM_VERSION;
2355
2356
4.98k
      if (vernum == 0)
2357
1.53k
  version_string = "";
2358
3.44k
      else if (vernum == 1
2359
2.41k
         && (vernum > elf_tdata (abfd)->cverdefs
2360
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2361
0
           == VER_FLG_BASE)))
2362
2.41k
  version_string = base_p ? "Base" : "";
2363
1.03k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2364
0
  {
2365
0
    const char *nodename
2366
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2367
0
    version_string = "";
2368
0
    if (base_p
2369
0
        || nodename == NULL
2370
0
        || symbol->name == NULL
2371
0
        || ! streq (symbol->name, nodename))
2372
0
      version_string = nodename;
2373
0
  }
2374
1.03k
      else
2375
1.03k
  {
2376
1.03k
    Elf_Internal_Verneed *t;
2377
2378
1.03k
    version_string = _("<corrupt>");
2379
1.03k
    for (t = elf_tdata (abfd)->verref;
2380
2.06k
         t != NULL;
2381
1.03k
         t = t->vn_nextref)
2382
1.03k
      {
2383
1.03k
        Elf_Internal_Vernaux *a;
2384
2385
2.18k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2386
1.86k
    {
2387
1.86k
      if (a->vna_other == vernum)
2388
706
        {
2389
706
          *hidden = true;
2390
706
          version_string = a->vna_nodename;
2391
706
          break;
2392
706
        }
2393
1.86k
    }
2394
1.03k
      }
2395
1.03k
  }
2396
4.98k
    }
2397
42.1k
  return version_string;
2398
42.1k
}
2399
2400
/* Display ELF-specific fields of a symbol.  */
2401
2402
void
2403
_bfd_elf_print_symbol (bfd *abfd,
2404
           void *filep,
2405
           asymbol *symbol,
2406
           bfd_print_symbol_type how)
2407
0
{
2408
0
  FILE *file = (FILE *) filep;
2409
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2410
0
       ? symbol->name : _("<corrupt>"));
2411
2412
0
  switch (how)
2413
0
    {
2414
0
    case bfd_print_symbol_name:
2415
0
      fprintf (file, "%s", symname);
2416
0
      break;
2417
0
    case bfd_print_symbol_more:
2418
0
      fprintf (file, "elf ");
2419
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2420
0
      fprintf (file, " %x", symbol->flags);
2421
0
      break;
2422
0
    case bfd_print_symbol_all:
2423
0
      {
2424
0
  const char *section_name;
2425
0
  const char *name = NULL;
2426
0
  elf_backend_data *bed;
2427
0
  unsigned char st_other;
2428
0
  bfd_vma val;
2429
0
  const char *version_string;
2430
0
  bool hidden;
2431
2432
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2433
2434
0
  bed = get_elf_backend_data (abfd);
2435
0
  if (bed->elf_backend_print_symbol_all)
2436
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2437
2438
0
  if (name != NULL)
2439
0
    symname = name;
2440
0
  else
2441
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2442
2443
0
  fprintf (file, " %s\t", section_name);
2444
  /* Print the "other" value for a symbol.  For common symbols,
2445
     we've already printed the size; now print the alignment.
2446
     For other symbols, we have no specified alignment, and
2447
     we've printed the address; now print the size.  */
2448
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2449
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2450
0
  else
2451
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2452
0
  bfd_fprintf_vma (abfd, file, val);
2453
2454
  /* If we have version information, print it.  */
2455
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2456
0
                   symbol,
2457
0
                   true,
2458
0
                   &hidden);
2459
0
  if (version_string)
2460
0
    {
2461
0
      if (!hidden)
2462
0
        fprintf (file, "  %-11s", version_string);
2463
0
      else
2464
0
        {
2465
0
    int i;
2466
2467
0
    fprintf (file, " (%s)", version_string);
2468
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2469
0
      putc (' ', file);
2470
0
        }
2471
0
    }
2472
2473
  /* If the st_other field is not zero, print it.  */
2474
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2475
2476
0
  switch (st_other)
2477
0
    {
2478
0
    case 0: break;
2479
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2480
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2481
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2482
0
    default:
2483
      /* Some other non-defined flags are also present, so print
2484
         everything hex.  */
2485
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2486
0
    }
2487
2488
0
  fprintf (file, " %s", symname);
2489
0
      }
2490
0
      break;
2491
0
    }
2492
0
}
2493

2494
/* ELF .o/exec file reading */
2495
2496
/* Create a new bfd section from an ELF section header.  */
2497
2498
bool
2499
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2500
883k
{
2501
883k
  Elf_Internal_Shdr *hdr;
2502
883k
  Elf_Internal_Ehdr *ehdr;
2503
883k
  elf_backend_data *bed;
2504
883k
  const char *name;
2505
883k
  bool ret = true;
2506
2507
883k
  if (shindex >= elf_numsections (abfd))
2508
0
    return false;
2509
2510
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2511
     sh_link or sh_info.  Detect this here, by refusing to load a
2512
     section that we are already in the process of loading.  */
2513
883k
  if (elf_tdata (abfd)->being_created[shindex])
2514
16
    {
2515
16
      _bfd_error_handler
2516
16
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2517
16
      return false;
2518
16
    }
2519
883k
  elf_tdata (abfd)->being_created[shindex] = true;
2520
2521
883k
  hdr = elf_elfsections (abfd)[shindex];
2522
883k
  ehdr = elf_elfheader (abfd);
2523
883k
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2524
883k
            hdr->sh_name);
2525
883k
  if (name == NULL)
2526
7.42k
    goto fail;
2527
2528
876k
  bed = get_elf_backend_data (abfd);
2529
876k
  switch (hdr->sh_type)
2530
876k
    {
2531
84.7k
    case SHT_NULL:
2532
      /* Inactive section. Throw it away.  */
2533
84.7k
      goto success;
2534
2535
259k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2536
272k
    case SHT_NOBITS:   /* .bss section.  */
2537
273k
    case SHT_HASH:   /* .hash section.  */
2538
282k
    case SHT_NOTE:   /* .note section.  */
2539
300k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2540
309k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2541
312k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2542
312k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2543
313k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2544
313k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2545
313k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2546
313k
      goto success;
2547
2548
2.31k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2549
2.31k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2550
57
  goto fail;
2551
2552
2.26k
      if (hdr->sh_link > elf_numsections (abfd))
2553
174
  {
2554
    /* PR 10478: Accept Solaris binaries with a sh_link field
2555
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2556
174
    switch (bfd_get_arch (abfd))
2557
174
      {
2558
133
      case bfd_arch_i386:
2559
174
      case bfd_arch_sparc:
2560
174
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2561
37
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2562
174
    break;
2563
        /* Otherwise fall through.  */
2564
0
      default:
2565
0
        goto fail;
2566
174
      }
2567
174
  }
2568
2.08k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2569
0
  goto fail;
2570
2.08k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2571
1.25k
  {
2572
1.25k
    Elf_Internal_Shdr *dynsymhdr;
2573
2574
    /* The shared libraries distributed with hpux11 have a bogus
2575
       sh_link field for the ".dynamic" section.  Find the
2576
       string table for the ".dynsym" section instead.  */
2577
1.25k
    if (elf_dynsymtab (abfd) != 0)
2578
253
      {
2579
253
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2580
253
        hdr->sh_link = dynsymhdr->sh_link;
2581
253
      }
2582
1.00k
    else
2583
1.00k
      {
2584
1.00k
        unsigned int i, num_sec;
2585
2586
1.00k
        num_sec = elf_numsections (abfd);
2587
19.2k
        for (i = 1; i < num_sec; i++)
2588
18.4k
    {
2589
18.4k
      dynsymhdr = elf_elfsections (abfd)[i];
2590
18.4k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2591
158
        {
2592
158
          hdr->sh_link = dynsymhdr->sh_link;
2593
158
          break;
2594
158
        }
2595
18.4k
    }
2596
1.00k
      }
2597
1.25k
  }
2598
2.26k
      goto success;
2599
2600
108k
    case SHT_SYMTAB:   /* A symbol table.  */
2601
108k
      if (elf_onesymtab (abfd) == shindex)
2602
85.7k
  goto success;
2603
2604
22.3k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2605
544
  goto fail;
2606
2607
21.8k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2608
1.28k
  {
2609
1.28k
    if (hdr->sh_size != 0)
2610
160
      goto fail;
2611
    /* Some assemblers erroneously set sh_info to one with a
2612
       zero sh_size.  ld sees this as a global symbol count
2613
       of (unsigned) -1.  Fix it here.  */
2614
1.12k
    hdr->sh_info = 0;
2615
1.12k
    goto success;
2616
1.28k
  }
2617
2618
      /* PR 18854: A binary might contain more than one symbol table.
2619
   Unusual, but possible.  Warn, but continue.  */
2620
20.5k
      if (elf_onesymtab (abfd) != 0)
2621
3.01k
  {
2622
3.01k
    _bfd_error_handler
2623
      /* xgettext:c-format */
2624
3.01k
      (_("%pB: warning: multiple symbol tables detected"
2625
3.01k
         " - ignoring the table in section %u"),
2626
3.01k
       abfd, shindex);
2627
3.01k
    goto success;
2628
3.01k
  }
2629
17.5k
      elf_onesymtab (abfd) = shindex;
2630
17.5k
      elf_symtab_hdr (abfd) = *hdr;
2631
17.5k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2632
17.5k
      abfd->flags |= HAS_SYMS;
2633
2634
      /* Sometimes a shared object will map in the symbol table.  If
2635
   SHF_ALLOC is set, and this is a shared object, then we also
2636
   treat this section as a BFD section.  We can not base the
2637
   decision purely on SHF_ALLOC, because that flag is sometimes
2638
   set in a relocatable object file, which would confuse the
2639
   linker.  */
2640
17.5k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2641
843
    && (abfd->flags & DYNAMIC) != 0
2642
203
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2643
203
            shindex))
2644
7
  goto fail;
2645
2646
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2647
   can't read symbols without that section loaded as well.  It
2648
   is most likely specified by the next section header.  */
2649
17.5k
      {
2650
17.5k
  elf_section_list * entry;
2651
17.5k
  unsigned int i, num_sec;
2652
2653
19.6k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2654
4.34k
    if (entry->hdr.sh_link == shindex)
2655
2.19k
      goto success;
2656
2657
15.3k
  num_sec = elf_numsections (abfd);
2658
44.9k
  for (i = shindex + 1; i < num_sec; i++)
2659
29.7k
    {
2660
29.7k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2661
2662
29.7k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2663
386
    && hdr2->sh_link == shindex)
2664
67
        break;
2665
29.7k
    }
2666
2667
15.3k
  if (i == num_sec)
2668
376k
    for (i = 1; i < shindex; i++)
2669
361k
      {
2670
361k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2671
2672
361k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2673
2.96k
      && hdr2->sh_link == shindex)
2674
246
    break;
2675
361k
      }
2676
2677
15.3k
  if (i != shindex)
2678
313
    ret = bfd_section_from_shdr (abfd, i);
2679
  /* else FIXME: we have failed to find the symbol table.
2680
     Should we issue an error?  */
2681
15.3k
  goto success;
2682
17.5k
      }
2683
2684
3.54k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2685
3.54k
      if (elf_dynsymtab (abfd) == shindex)
2686
1.37k
  goto success;
2687
2688
2.16k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2689
451
  goto fail;
2690
2691
1.71k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2692
283
  {
2693
283
    if (hdr->sh_size != 0)
2694
133
      goto fail;
2695
2696
    /* Some linkers erroneously set sh_info to one with a
2697
       zero sh_size.  ld sees this as a global symbol count
2698
       of (unsigned) -1.  Fix it here.  */
2699
150
    hdr->sh_info = 0;
2700
150
    goto success;
2701
283
  }
2702
2703
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2704
   Unusual, but possible.  Warn, but continue.  */
2705
1.43k
      if (elf_dynsymtab (abfd) != 0)
2706
105
  {
2707
105
    _bfd_error_handler
2708
      /* xgettext:c-format */
2709
105
      (_("%pB: warning: multiple dynamic symbol tables detected"
2710
105
         " - ignoring the table in section %u"),
2711
105
       abfd, shindex);
2712
105
    goto success;
2713
105
  }
2714
1.32k
      elf_dynsymtab (abfd) = shindex;
2715
1.32k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2716
1.32k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2717
1.32k
      abfd->flags |= HAS_SYMS;
2718
2719
      /* Besides being a symbol table, we also treat this as a regular
2720
   section, so that objcopy can handle it.  */
2721
1.32k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2722
1.32k
      goto success;
2723
2724
8.31k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2725
8.31k
      {
2726
8.31k
  elf_section_list * entry;
2727
2728
10.2k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2729
2.25k
    if (entry->ndx == shindex)
2730
361
      goto success;
2731
2732
7.95k
  entry = bfd_alloc (abfd, sizeof (*entry));
2733
7.95k
  if (entry == NULL)
2734
0
    goto fail;
2735
7.95k
  entry->ndx = shindex;
2736
7.95k
  entry->hdr = * hdr;
2737
7.95k
  entry->next = elf_symtab_shndx_list (abfd);
2738
7.95k
  elf_symtab_shndx_list (abfd) = entry;
2739
7.95k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2740
7.95k
  goto success;
2741
7.95k
      }
2742
2743
55.7k
    case SHT_STRTAB:   /* A string table.  */
2744
55.7k
      if (hdr->bfd_section != NULL)
2745
373
  goto success;
2746
2747
55.3k
      if (ehdr->e_shstrndx == shindex)
2748
47.7k
  {
2749
47.7k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2750
47.7k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2751
47.7k
    goto success;
2752
47.7k
  }
2753
2754
7.60k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2755
2.33k
  {
2756
2.56k
  symtab_strtab:
2757
2.56k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2758
2.56k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2759
2.56k
    goto success;
2760
2.33k
  }
2761
2762
5.27k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2763
786
  {
2764
844
  dynsymtab_strtab:
2765
844
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2766
844
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2767
844
    elf_elfsections (abfd)[shindex] = hdr;
2768
    /* We also treat this as a regular section, so that objcopy
2769
       can handle it.  */
2770
844
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2771
844
             shindex);
2772
844
    goto success;
2773
786
  }
2774
2775
      /* If the string table isn't one of the above, then treat it as a
2776
   regular section.  We need to scan all the headers to be sure,
2777
   just in case this strtab section appeared before the above.  */
2778
4.48k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2779
4.37k
  {
2780
4.37k
    unsigned int i, num_sec;
2781
2782
4.37k
    num_sec = elf_numsections (abfd);
2783
87.6k
    for (i = 1; i < num_sec; i++)
2784
83.8k
      {
2785
83.8k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2786
83.8k
        if (hdr2->sh_link == shindex)
2787
2.98k
    {
2788
      /* Prevent endless recursion on broken objects.  */
2789
2.98k
      if (i == shindex)
2790
60
        goto fail;
2791
2.92k
      if (! bfd_section_from_shdr (abfd, i))
2792
155
        goto fail;
2793
2.76k
      if (elf_onesymtab (abfd) == i)
2794
231
        goto symtab_strtab;
2795
2.53k
      if (elf_dynsymtab (abfd) == i)
2796
58
        goto dynsymtab_strtab;
2797
2.53k
    }
2798
83.8k
      }
2799
4.37k
  }
2800
3.98k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2801
3.98k
      goto success;
2802
2803
19.8k
    case SHT_REL:
2804
110k
    case SHT_RELA:
2805
110k
    case SHT_RELR:
2806
      /* *These* do a lot of work -- but build no sections!  */
2807
110k
      {
2808
110k
  asection *target_sect;
2809
110k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2810
110k
  unsigned int num_sec = elf_numsections (abfd);
2811
110k
  struct bfd_elf_section_data *esdt;
2812
110k
  bfd_size_type size;
2813
2814
110k
  if (hdr->sh_type == SHT_REL)
2815
19.8k
    size = bed->s->sizeof_rel;
2816
90.6k
  else if (hdr->sh_type == SHT_RELA)
2817
90.3k
    size = bed->s->sizeof_rela;
2818
347
  else
2819
347
    size = bed->s->arch_size / 8;
2820
110k
  if (hdr->sh_entsize != size)
2821
1.10k
    goto fail;
2822
2823
  /* Check for a bogus link to avoid crashing.  */
2824
109k
  if (hdr->sh_link >= num_sec)
2825
92
    {
2826
92
      _bfd_error_handler
2827
        /* xgettext:c-format */
2828
92
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2829
92
         abfd, hdr->sh_link, name, shindex);
2830
92
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2831
92
      goto success;
2832
92
    }
2833
2834
  /* Get the symbol table.  */
2835
109k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2836
26.7k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2837
84.0k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2838
429
    goto fail;
2839
2840
  /* If this is an alloc section in an executable or shared
2841
     library, or the reloc section does not use the main symbol
2842
     table we don't treat it as a reloc section.  BFD can't
2843
     adequately represent such a section, so at least for now,
2844
     we don't try.  We just present it as a normal section.  We
2845
     also can't use it as a reloc section if it points to the
2846
     null section, an invalid section, another reloc section, or
2847
     its sh_link points to the null section.  */
2848
108k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2849
5.08k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2850
107k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2851
104k
      || hdr->sh_type == SHT_RELR
2852
104k
      || hdr->sh_link == SHN_UNDEF
2853
101k
      || hdr->sh_link != elf_onesymtab (abfd)
2854
78.9k
      || hdr->sh_info == SHN_UNDEF
2855
78.3k
      || hdr->sh_info >= num_sec
2856
78.3k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2857
78.0k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2858
31.2k
    {
2859
31.2k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2860
31.2k
      goto success;
2861
31.2k
    }
2862
2863
77.5k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2864
104
    goto fail;
2865
2866
77.4k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2867
77.4k
  if (target_sect == NULL)
2868
149
    goto fail;
2869
2870
77.3k
  esdt = elf_section_data (target_sect);
2871
77.3k
  if (hdr->sh_type == SHT_RELA)
2872
64.6k
    p_hdr = &esdt->rela.hdr;
2873
12.7k
  else
2874
12.7k
    p_hdr = &esdt->rel.hdr;
2875
2876
  /* PR 17512: file: 0b4f81b7.
2877
     Also see PR 24456, for a file which deliberately has two reloc
2878
     sections.  */
2879
77.3k
  if (*p_hdr != NULL)
2880
1.33k
    {
2881
1.33k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2882
128
        {
2883
128
    _bfd_error_handler
2884
      /* xgettext:c-format */
2885
128
      (_("%pB: warning: secondary relocation section '%s' "
2886
128
         "for section %pA found - ignoring"),
2887
128
       abfd, name, target_sect);
2888
128
        }
2889
1.20k
      else
2890
1.20k
        esdt->has_secondary_relocs = true;
2891
1.33k
      goto success;
2892
1.33k
    }
2893
2894
76.0k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2895
76.0k
  if (hdr2 == NULL)
2896
0
    goto fail;
2897
76.0k
  *hdr2 = *hdr;
2898
76.0k
  *p_hdr = hdr2;
2899
76.0k
  elf_elfsections (abfd)[shindex] = hdr2;
2900
76.0k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2901
76.0k
             * bed->s->int_rels_per_ext_rel);
2902
76.0k
  target_sect->flags |= SEC_RELOC;
2903
76.0k
  target_sect->relocation = NULL;
2904
76.0k
  target_sect->rel_filepos = hdr->sh_offset;
2905
  /* In the section to which the relocations apply, mark whether
2906
     its relocations are of the REL or RELA variety.  */
2907
76.0k
  if (hdr->sh_size != 0)
2908
75.3k
    {
2909
75.3k
      if (hdr->sh_type == SHT_RELA)
2910
62.8k
        target_sect->use_rela_p = 1;
2911
75.3k
    }
2912
76.0k
  abfd->flags |= HAS_RELOC;
2913
76.0k
  goto success;
2914
76.0k
      }
2915
2916
307
    case SHT_GNU_verdef:
2917
307
      if (hdr->sh_info != 0)
2918
143
  elf_dynverdef (abfd) = shindex;
2919
307
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2920
307
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2921
307
      goto success;
2922
2923
763
    case SHT_GNU_versym:
2924
763
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2925
96
  goto fail;
2926
2927
667
      elf_dynversym (abfd) = shindex;
2928
667
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2929
667
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2930
667
      goto success;
2931
2932
887
    case SHT_GNU_verneed:
2933
887
      if (hdr->sh_info != 0)
2934
761
  elf_dynverref (abfd) = shindex;
2935
887
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2936
887
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2937
887
      goto success;
2938
2939
1.11k
    case SHT_SHLIB:
2940
1.11k
      goto success;
2941
2942
20.3k
    case SHT_GROUP:
2943
20.3k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2944
33
  goto fail;
2945
2946
20.3k
      goto success;
2947
2948
166k
    default:
2949
      /* Possibly an attributes section.  */
2950
166k
      if (get_elf_backend_data (abfd)->target_os != is_solaris
2951
    /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
2952
161k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2953
157k
        || hdr->sh_type == bed->obj_attrs_section_type))
2954
4.35k
  {
2955
4.35k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2956
30
      goto fail;
2957
4.32k
    _bfd_elf_parse_attributes (abfd, hdr);
2958
4.32k
    goto success;
2959
4.35k
  }
2960
2961
      /* Check for any processor-specific section types.  */
2962
161k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2963
147k
  goto success;
2964
2965
14.6k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2966
6.25k
  {
2967
6.25k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2968
      /* FIXME: How to properly handle allocated section reserved
2969
         for applications?  */
2970
585
      _bfd_error_handler
2971
        /* xgettext:c-format */
2972
585
        (_("%pB: unknown type [%#x] section `%s'"),
2973
585
         abfd, hdr->sh_type, name);
2974
5.67k
    else
2975
5.67k
      {
2976
        /* Allow sections reserved for applications.  */
2977
5.67k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2978
5.67k
        goto success;
2979
5.67k
      }
2980
6.25k
  }
2981
8.43k
      else if (hdr->sh_type >= SHT_LOPROC
2982
917
         && hdr->sh_type <= SHT_HIPROC)
2983
  /* FIXME: We should handle this section.  */
2984
917
  _bfd_error_handler
2985
    /* xgettext:c-format */
2986
917
    (_("%pB: unknown type [%#x] section `%s'"),
2987
917
     abfd, hdr->sh_type, name);
2988
7.51k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2989
4.22k
  {
2990
    /* Unrecognised OS-specific sections.  */
2991
4.22k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2992
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2993
         required to correctly process the section and the file should
2994
         be rejected with an error message.  */
2995
292
      _bfd_error_handler
2996
        /* xgettext:c-format */
2997
292
        (_("%pB: unknown type [%#x] section `%s'"),
2998
292
         abfd, hdr->sh_type, name);
2999
3.93k
    else
3000
3.93k
      {
3001
        /* Otherwise it should be processed.  */
3002
3.93k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3003
3.93k
        goto success;
3004
3.93k
      }
3005
4.22k
  }
3006
3.29k
      else
3007
  /* FIXME: We should handle this section.  */
3008
3.29k
  _bfd_error_handler
3009
    /* xgettext:c-format */
3010
3.29k
    (_("%pB: unknown type [%#x] section `%s'"),
3011
3.29k
     abfd, hdr->sh_type, name);
3012
3013
5.08k
      goto fail;
3014
876k
    }
3015
3016
16.0k
 fail:
3017
16.0k
  ret = false;
3018
883k
 success:
3019
883k
  elf_tdata (abfd)->being_created[shindex] = false;
3020
883k
  return ret;
3021
16.0k
}
3022
3023
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3024
3025
Elf_Internal_Sym *
3026
bfd_sym_from_r_symndx (struct sym_cache *cache,
3027
           bfd *abfd,
3028
           unsigned long r_symndx)
3029
0
{
3030
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3031
3032
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3033
0
    {
3034
0
      Elf_Internal_Shdr *symtab_hdr;
3035
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3036
0
      Elf_External_Sym_Shndx eshndx;
3037
3038
0
      symtab_hdr = &elf_symtab_hdr (abfd);
3039
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3040
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3041
0
  return NULL;
3042
3043
0
      if (cache->abfd != abfd)
3044
0
  {
3045
0
    memset (cache->indx, -1, sizeof (cache->indx));
3046
0
    cache->abfd = abfd;
3047
0
  }
3048
0
      cache->indx[ent] = r_symndx;
3049
0
    }
3050
3051
0
  return &cache->sym[ent];
3052
0
}
3053
3054
/* Given an ELF section number, retrieve the corresponding BFD
3055
   section.  */
3056
3057
asection *
3058
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3059
161k
{
3060
161k
  if (sec_index >= elf_numsections (abfd))
3061
23.4k
    return NULL;
3062
137k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3063
161k
}
3064
3065
static const struct bfd_elf_special_section special_sections_b[] =
3066
{
3067
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3068
  { NULL,       0,  0, 0,    0 }
3069
};
3070
3071
static const struct bfd_elf_special_section special_sections_c[] =
3072
{
3073
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3074
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3075
  { NULL,     0, 0, 0,      0 }
3076
};
3077
3078
static const struct bfd_elf_special_section special_sections_d[] =
3079
{
3080
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3081
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082
  /* There are more DWARF sections than these, but they needn't be added here
3083
     unless you have to cope with broken compilers that don't emit section
3084
     attributes or you want to help the user writing assembler.  */
3085
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3086
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3087
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3088
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3089
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3090
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3091
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3092
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3093
  { NULL,          0,  0, 0,      0 }
3094
};
3095
3096
static const struct bfd_elf_special_section special_sections_f[] =
3097
{
3098
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3099
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3100
  { NULL,        0 , 0, 0,      0 }
3101
};
3102
3103
static const struct bfd_elf_special_section special_sections_g[] =
3104
{
3105
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3106
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3107
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3108
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3109
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3110
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3111
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3112
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3113
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3114
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3115
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3116
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3117
  { NULL,      0,    0, 0,         0 }
3118
};
3119
3120
static const struct bfd_elf_special_section special_sections_h[] =
3121
{
3122
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3123
  { NULL,        0, 0, 0,    0 }
3124
};
3125
3126
static const struct bfd_elf_special_section special_sections_i[] =
3127
{
3128
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3129
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3130
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3131
  { NULL,          0,      0, 0,      0 }
3132
};
3133
3134
static const struct bfd_elf_special_section special_sections_l[] =
3135
{
3136
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3137
  { NULL,        0, 0, 0,    0 }
3138
};
3139
3140
static const struct bfd_elf_special_section special_sections_n[] =
3141
{
3142
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3143
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_NOTE,     0 },
3144
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3145
  { NULL,        0,     0, 0,      0 }
3146
};
3147
3148
static const struct bfd_elf_special_section special_sections_p[] =
3149
{
3150
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3151
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3152
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3153
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3154
  { NULL,       0,      0, 0,     0 }
3155
};
3156
3157
static const struct bfd_elf_special_section special_sections_r[] =
3158
{
3159
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3160
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3161
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3162
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3163
  /* .relro_padding is generated by lld.  It should not be confused with a
3164
     reloc containing section, because otherwise elf_fake_sections() will
3165
     set the entsize to 8, which may not be an actual multiple of the
3166
     section's size.
3167
     Note - this entry must appear before the ".rel" entry below.  */
3168
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3169
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3170
  { NULL,       0,     0, 0,      0 }
3171
};
3172
3173
static const struct bfd_elf_special_section special_sections_s[] =
3174
{
3175
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3176
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3177
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3178
  /* See struct bfd_elf_special_section declaration for the semantics of
3179
     this special case where .prefix_length != strlen (.prefix).  */
3180
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3181
  { NULL,     0,  0, 0,    0 }
3182
};
3183
3184
static const struct bfd_elf_special_section special_sections_t[] =
3185
{
3186
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3187
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3188
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3189
  { NULL,         0,  0, 0,      0 }
3190
};
3191
3192
static const struct bfd_elf_special_section special_sections_z[] =
3193
{
3194
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3195
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3196
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3197
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3198
  { NULL,         0,  0, 0,      0 }
3199
};
3200
3201
static const struct bfd_elf_special_section * const special_sections[] =
3202
{
3203
  special_sections_b,   /* 'b' */
3204
  special_sections_c,   /* 'c' */
3205
  special_sections_d,   /* 'd' */
3206
  NULL,       /* 'e' */
3207
  special_sections_f,   /* 'f' */
3208
  special_sections_g,   /* 'g' */
3209
  special_sections_h,   /* 'h' */
3210
  special_sections_i,   /* 'i' */
3211
  NULL,       /* 'j' */
3212
  NULL,       /* 'k' */
3213
  special_sections_l,   /* 'l' */
3214
  NULL,       /* 'm' */
3215
  special_sections_n,   /* 'n' */
3216
  NULL,       /* 'o' */
3217
  special_sections_p,   /* 'p' */
3218
  NULL,       /* 'q' */
3219
  special_sections_r,   /* 'r' */
3220
  special_sections_s,   /* 's' */
3221
  special_sections_t,   /* 't' */
3222
  NULL,       /* 'u' */
3223
  NULL,       /* 'v' */
3224
  NULL,       /* 'w' */
3225
  NULL,       /* 'x' */
3226
  NULL,       /* 'y' */
3227
  special_sections_z    /* 'z' */
3228
};
3229
3230
const struct bfd_elf_special_section *
3231
_bfd_elf_get_special_section (const char *name,
3232
            const struct bfd_elf_special_section *spec,
3233
            unsigned int rela)
3234
786k
{
3235
786k
  int i;
3236
786k
  int len;
3237
3238
786k
  len = strlen (name);
3239
3240
4.42M
  for (i = 0; spec[i].prefix != NULL; i++)
3241
3.77M
    {
3242
3.77M
      int suffix_len;
3243
3.77M
      int prefix_len = spec[i].prefix_length;
3244
3245
3.77M
      if (len < prefix_len)
3246
1.92M
  continue;
3247
1.85M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3248
1.65M
  continue;
3249
3250
206k
      suffix_len = spec[i].suffix_length;
3251
206k
      if (suffix_len <= 0)
3252
204k
  {
3253
204k
    if (name[prefix_len] != 0)
3254
121k
      {
3255
121k
        if (suffix_len == 0)
3256
54.3k
    continue;
3257
67.3k
        if (name[prefix_len] != '.'
3258
11.2k
      && (suffix_len == -2
3259
4.44k
          || (rela && spec[i].type == SHT_REL)))
3260
8.28k
    continue;
3261
67.3k
      }
3262
204k
  }
3263
1.68k
      else
3264
1.68k
  {
3265
1.68k
    if (len < prefix_len + suffix_len)
3266
942
      continue;
3267
745
    if (memcmp (name + len - suffix_len,
3268
745
          spec[i].prefix + prefix_len,
3269
745
          suffix_len) != 0)
3270
592
      continue;
3271
745
  }
3272
142k
      return &spec[i];
3273
206k
    }
3274
3275
644k
  return NULL;
3276
786k
}
3277
3278
const struct bfd_elf_special_section *
3279
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3280
1.18M
{
3281
1.18M
  int i;
3282
1.18M
  const struct bfd_elf_special_section *spec;
3283
1.18M
  elf_backend_data *bed;
3284
3285
  /* See if this is one of the special sections.  */
3286
1.18M
  if (sec->name == NULL)
3287
0
    return NULL;
3288
3289
1.18M
  bed = get_elf_backend_data (abfd);
3290
1.18M
  spec = bed->special_sections;
3291
1.18M
  if (spec)
3292
572k
    {
3293
572k
      spec = _bfd_elf_get_special_section (sec->name,
3294
572k
             bed->special_sections,
3295
572k
             sec->use_rela_p);
3296
572k
      if (spec != NULL)
3297
2.70k
  return spec;
3298
572k
    }
3299
3300
1.18M
  if (sec->name[0] != '.')
3301
967k
    return NULL;
3302
3303
218k
  i = sec->name[1] - 'b';
3304
218k
  if (i < 0 || i > 'z' - 'b')
3305
8.36k
    return NULL;
3306
3307
210k
  spec = special_sections[i];
3308
3309
210k
  if (spec == NULL)
3310
13.3k
    return NULL;
3311
3312
197k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3313
210k
}
3314
3315
bool
3316
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3317
1.18M
{
3318
1.18M
  struct bfd_elf_section_data *sdata;
3319
1.18M
  elf_backend_data *bed;
3320
1.18M
  const struct bfd_elf_special_section *ssect;
3321
3322
1.18M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3323
1.18M
  if (sdata == NULL)
3324
839k
    {
3325
839k
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3326
839k
                sizeof (*sdata));
3327
839k
      if (sdata == NULL)
3328
0
  return false;
3329
839k
      sec->used_by_bfd = sdata;
3330
839k
    }
3331
3332
  /* Indicate whether or not this section should use RELA relocations.  */
3333
1.18M
  bed = get_elf_backend_data (abfd);
3334
1.18M
  sec->use_rela_p = bed->default_use_rela_p;
3335
3336
  /* Set up ELF section type and flags for newly created sections, if
3337
     there is an ABI mandated section.  */
3338
1.18M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3339
1.18M
  if (ssect != NULL)
3340
142k
    {
3341
142k
      elf_section_type (sec) = ssect->type;
3342
142k
      elf_section_flags (sec) = ssect->attr;
3343
142k
    }
3344
3345
1.18M
  return _bfd_generic_new_section_hook (abfd, sec);
3346
1.18M
}
3347
3348
/* Create a new bfd section from an ELF program header.
3349
3350
   Since program segments have no names, we generate a synthetic name
3351
   of the form segment<NUM>, where NUM is generally the index in the
3352
   program header table.  For segments that are split (see below) we
3353
   generate the names segment<NUM>a and segment<NUM>b.
3354
3355
   Note that some program segments may have a file size that is different than
3356
   (less than) the memory size.  All this means is that at execution the
3357
   system must allocate the amount of memory specified by the memory size,
3358
   but only initialize it with the first "file size" bytes read from the
3359
   file.  This would occur for example, with program segments consisting
3360
   of combined data+bss.
3361
3362
   To handle the above situation, this routine generates TWO bfd sections
3363
   for the single program segment.  The first has the length specified by
3364
   the file size of the segment, and the second has the length specified
3365
   by the difference between the two sizes.  In effect, the segment is split
3366
   into its initialized and uninitialized parts.  */
3367
3368
bool
3369
_bfd_elf_make_section_from_phdr (bfd *abfd,
3370
         Elf_Internal_Phdr *hdr,
3371
         int hdr_index,
3372
         const char *type_name)
3373
587k
{
3374
587k
  asection *newsect;
3375
587k
  char *name;
3376
587k
  char namebuf[64];
3377
587k
  size_t len;
3378
587k
  int split;
3379
587k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3380
3381
587k
  split = ((hdr->p_memsz > 0)
3382
449k
      && (hdr->p_filesz > 0)
3383
401k
      && (hdr->p_memsz > hdr->p_filesz));
3384
3385
587k
  if (hdr->p_filesz > 0)
3386
495k
    {
3387
495k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3388
495k
      len = strlen (namebuf) + 1;
3389
495k
      name = (char *) bfd_alloc (abfd, len);
3390
495k
      if (!name)
3391
0
  return false;
3392
495k
      memcpy (name, namebuf, len);
3393
495k
      newsect = bfd_make_section (abfd, name);
3394
495k
      if (newsect == NULL)
3395
0
  return false;
3396
495k
      newsect->vma = hdr->p_vaddr / opb;
3397
495k
      newsect->lma = hdr->p_paddr / opb;
3398
495k
      newsect->size = hdr->p_filesz;
3399
495k
      newsect->filepos = hdr->p_offset;
3400
495k
      newsect->flags |= SEC_HAS_CONTENTS;
3401
495k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3402
495k
      if (hdr->p_type == PT_LOAD)
3403
6.38k
  {
3404
6.38k
    newsect->flags |= SEC_ALLOC;
3405
6.38k
    newsect->flags |= SEC_LOAD;
3406
6.38k
    if (hdr->p_flags & PF_X)
3407
2.38k
      {
3408
        /* FIXME: all we known is that it has execute PERMISSION,
3409
     may be data.  */
3410
2.38k
        newsect->flags |= SEC_CODE;
3411
2.38k
      }
3412
6.38k
  }
3413
495k
      if (!(hdr->p_flags & PF_W))
3414
359k
  {
3415
359k
    newsect->flags |= SEC_READONLY;
3416
359k
  }
3417
495k
    }
3418
3419
587k
  if (hdr->p_memsz > hdr->p_filesz)
3420
222k
    {
3421
222k
      bfd_vma align;
3422
3423
222k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3424
222k
      len = strlen (namebuf) + 1;
3425
222k
      name = (char *) bfd_alloc (abfd, len);
3426
222k
      if (!name)
3427
0
  return false;
3428
222k
      memcpy (name, namebuf, len);
3429
222k
      newsect = bfd_make_section (abfd, name);
3430
222k
      if (newsect == NULL)
3431
0
  return false;
3432
222k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3433
222k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3434
222k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3435
222k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3436
222k
      align = newsect->vma & -newsect->vma;
3437
222k
      if (align == 0 || align > hdr->p_align)
3438
50.2k
  align = hdr->p_align;
3439
222k
      newsect->alignment_power = bfd_log2 (align);
3440
222k
      if (hdr->p_type == PT_LOAD)
3441
5.40k
  {
3442
5.40k
    newsect->flags |= SEC_ALLOC;
3443
5.40k
    if (hdr->p_flags & PF_X)
3444
1.60k
      newsect->flags |= SEC_CODE;
3445
5.40k
  }
3446
222k
      if (!(hdr->p_flags & PF_W))
3447
165k
  newsect->flags |= SEC_READONLY;
3448
222k
    }
3449
3450
587k
  return true;
3451
587k
}
3452
3453
static bool
3454
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3455
8.72k
{
3456
  /* The return value is ignored.  Build-ids are considered optional.  */
3457
8.72k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3458
8.72k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3459
8.72k
      (templ, offset);
3460
0
  return false;
3461
8.72k
}
3462
3463
bool
3464
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3465
587k
{
3466
587k
  elf_backend_data *bed;
3467
3468
587k
  switch (hdr->p_type)
3469
587k
    {
3470
117k
    case PT_NULL:
3471
117k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3472
3473
8.72k
    case PT_LOAD:
3474
8.72k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3475
0
  return false;
3476
8.72k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3477
8.72k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3478
8.72k
      return true;
3479
3480
2.30k
    case PT_DYNAMIC:
3481
2.30k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3482
3483
1.29k
    case PT_INTERP:
3484
1.29k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3485
3486
819
    case PT_NOTE:
3487
819
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3488
0
  return false;
3489
819
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3490
819
         hdr->p_align))
3491
320
  return false;
3492
499
      return true;
3493
3494
667
    case PT_SHLIB:
3495
667
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3496
3497
6.89k
    case PT_PHDR:
3498
6.89k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3499
3500
47
    case PT_GNU_EH_FRAME:
3501
47
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3502
47
                "eh_frame_hdr");
3503
3504
32
    case PT_GNU_STACK:
3505
32
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3506
3507
194
    case PT_GNU_RELRO:
3508
194
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3509
3510
4
    case PT_GNU_SFRAME:
3511
4
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3512
4
                "sframe");
3513
3514
448k
    default:
3515
      /* Check for any processor-specific program segment types.  */
3516
448k
      bed = get_elf_backend_data (abfd);
3517
448k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3518
587k
    }
3519
587k
}
3520
3521
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3522
   REL or RELA.  */
3523
3524
Elf_Internal_Shdr *
3525
_bfd_elf_single_rel_hdr (asection *sec)
3526
8
{
3527
8
  if (elf_section_data (sec)->rel.hdr)
3528
0
    {
3529
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3530
0
      return elf_section_data (sec)->rel.hdr;
3531
0
    }
3532
8
  else
3533
8
    return elf_section_data (sec)->rela.hdr;
3534
8
}
3535
3536
static bool
3537
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3538
          Elf_Internal_Shdr *rel_hdr,
3539
          const char *sec_name,
3540
          bool use_rela_p)
3541
167
{
3542
167
  char *name = (char *) bfd_alloc (abfd,
3543
167
           sizeof ".rela" + strlen (sec_name));
3544
167
  if (name == NULL)
3545
0
    return false;
3546
3547
167
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3548
167
  rel_hdr->sh_name =
3549
167
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3550
167
          false);
3551
167
  if (rel_hdr->sh_name == (unsigned int) -1)
3552
0
    return false;
3553
3554
167
  return true;
3555
167
}
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
167
{
3569
167
  Elf_Internal_Shdr *rel_hdr;
3570
167
  elf_backend_data *bed = get_elf_backend_data (abfd);
3571
3572
167
  BFD_ASSERT (reldata->hdr == NULL);
3573
167
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3574
167
  if (rel_hdr == NULL)
3575
0
    return false;
3576
167
  reldata->hdr = rel_hdr;
3577
3578
167
  if (delay_sh_name_p)
3579
0
    rel_hdr->sh_name = (unsigned int) -1;
3580
167
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3581
167
          use_rela_p))
3582
0
    return false;
3583
167
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3584
167
  rel_hdr->sh_entsize = (use_rela_p
3585
167
       ? bed->s->sizeof_rela
3586
167
       : bed->s->sizeof_rel);
3587
167
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3588
167
  rel_hdr->sh_flags = 0;
3589
167
  rel_hdr->sh_addr = 0;
3590
167
  rel_hdr->sh_size = 0;
3591
167
  rel_hdr->sh_offset = 0;
3592
3593
167
  return true;
3594
167
}
3595
3596
/* Return the default section type based on the passed in section flags.  */
3597
3598
int
3599
bfd_elf_get_default_section_type (flagword flags)
3600
1.19k
{
3601
1.19k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3602
820
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3603
31
    return SHT_NOBITS;
3604
1.15k
  return SHT_PROGBITS;
3605
1.19k
}
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.11k
{
3618
1.11k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3619
1.11k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3620
1.11k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3621
1.11k
  Elf_Internal_Shdr *this_hdr;
3622
1.11k
  unsigned int sh_type;
3623
1.11k
  const char *name = asect->name;
3624
1.11k
  bool delay_sh_name_p = false;
3625
1.11k
  bfd_vma mask;
3626
3627
1.11k
  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.11k
  this_hdr = &esd->this_hdr;
3635
3636
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3637
1.11k
  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.11k
  if (delay_sh_name_p)
3652
0
    this_hdr->sh_name = (unsigned int) -1;
3653
1.11k
  else
3654
1.11k
    {
3655
1.11k
      this_hdr->sh_name
3656
1.11k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3657
1.11k
                name, false);
3658
1.11k
      if (this_hdr->sh_name == (unsigned int) -1)
3659
0
  {
3660
0
    arg->failed = true;
3661
0
    return;
3662
0
  }
3663
1.11k
    }
3664
3665
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3666
3667
1.11k
  if ((asect->flags & SEC_ALLOC) != 0
3668
320
      || asect->user_set_vma)
3669
1.11k
    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.11k
  this_hdr->sh_offset = 0;
3674
1.11k
  this_hdr->sh_size = asect->size;
3675
1.11k
  this_hdr->sh_link = 0;
3676
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3677
1.11k
  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.11k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3689
1.11k
  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.11k
  this_hdr->bfd_section = asect;
3694
1.11k
  this_hdr->contents = NULL;
3695
3696
  /* If the section type is unspecified, we set it based on
3697
     asect->flags.  */
3698
1.11k
  if (asect->type != 0)
3699
0
    sh_type = asect->type;
3700
1.11k
  else if ((asect->flags & SEC_GROUP) != 0)
3701
13
    sh_type = SHT_GROUP;
3702
1.09k
  else
3703
1.09k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3704
3705
1.11k
  if (this_hdr->sh_type == SHT_NULL)
3706
11
    this_hdr->sh_type = sh_type;
3707
1.09k
  else if (this_hdr->sh_type == SHT_NOBITS
3708
31
     && 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.11k
  switch (this_hdr->sh_type)
3721
1.11k
    {
3722
82
    default:
3723
82
      break;
3724
3725
82
    case SHT_STRTAB:
3726
112
    case SHT_NOTE:
3727
144
    case SHT_NOBITS:
3728
797
    case SHT_PROGBITS:
3729
797
      break;
3730
3731
34
    case SHT_INIT_ARRAY:
3732
61
    case SHT_FINI_ARRAY:
3733
62
    case SHT_PREINIT_ARRAY:
3734
62
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3735
62
      break;
3736
3737
20
    case SHT_HASH:
3738
20
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3739
20
      break;
3740
3741
20
    case SHT_DYNSYM:
3742
20
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3743
20
      break;
3744
3745
20
    case SHT_DYNAMIC:
3746
20
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3747
20
      break;
3748
3749
72
    case SHT_RELA:
3750
72
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3751
72
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3752
72
      break;
3753
3754
0
     case SHT_REL:
3755
0
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3756
0
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3757
0
      break;
3758
3759
6
     case SHT_GNU_versym:
3760
6
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3761
6
      break;
3762
3763
0
     case SHT_GNU_verdef:
3764
0
      this_hdr->sh_entsize = 0;
3765
      /* objcopy or strip will copy over sh_info, but may not set
3766
   cverdefs.  The linker will set cverdefs, but sh_info will be
3767
   zero.  */
3768
0
      if (this_hdr->sh_info == 0)
3769
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3770
0
      else
3771
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3772
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3773
0
      break;
3774
3775
6
    case SHT_GNU_verneed:
3776
6
      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
6
      if (this_hdr->sh_info == 0)
3781
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3782
6
      else
3783
6
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3784
6
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3785
6
      break;
3786
3787
13
    case SHT_GROUP:
3788
13
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3789
13
      break;
3790
3791
12
    case SHT_GNU_HASH:
3792
12
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3793
12
      break;
3794
1.11k
    }
3795
3796
1.11k
  if ((asect->flags & SEC_ALLOC) != 0)
3797
790
    this_hdr->sh_flags |= SHF_ALLOC;
3798
1.11k
  if ((asect->flags & SEC_READONLY) == 0)
3799
268
    this_hdr->sh_flags |= SHF_WRITE;
3800
1.11k
  if ((asect->flags & SEC_CODE) != 0)
3801
212
    this_hdr->sh_flags |= SHF_EXECINSTR;
3802
1.11k
  if ((asect->flags & SEC_MERGE) != 0)
3803
113
    {
3804
113
      this_hdr->sh_flags |= SHF_MERGE;
3805
113
      this_hdr->sh_entsize = asect->entsize;
3806
113
    }
3807
1.11k
  if ((asect->flags & SEC_STRINGS) != 0)
3808
111
    {
3809
111
      this_hdr->sh_flags |= SHF_STRINGS;
3810
111
      this_hdr->sh_entsize = asect->entsize;
3811
111
    }
3812
1.11k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3813
35
    this_hdr->sh_flags |= SHF_GROUP;
3814
1.11k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3815
23
    {
3816
23
      this_hdr->sh_flags |= SHF_TLS;
3817
23
      if (asect->size == 0
3818
11
    && (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
23
    }
3831
1.11k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3832
43
    this_hdr->sh_flags |= SHF_EXCLUDE;
3833
3834
1.11k
  if (this_hdr->sh_entsize == 0)
3835
726
    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.11k
  if ((asect->flags & SEC_RELOC) != 0)
3842
167
    {
3843
      /* When doing a relocatable link, create both REL and RELA sections if
3844
   needed.  */
3845
167
      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
167
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3867
167
            (asect->use_rela_p
3868
167
             ? &esd->rela : &esd->rel),
3869
167
            name,
3870
167
            asect->use_rela_p,
3871
167
            delay_sh_name_p))
3872
0
  {
3873
0
    arg->failed = true;
3874
0
    return;
3875
0
  }
3876
167
    }
3877
3878
  /* Check for processor-specific section types.  */
3879
1.11k
  sh_type = this_hdr->sh_type;
3880
1.11k
  if (bed->elf_backend_fake_sections
3881
1.09k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3882
0
    {
3883
0
      arg->failed = true;
3884
0
      return;
3885
0
    }
3886
3887
1.11k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3888
25
    {
3889
      /* Don't change the header type from NOBITS if we are being
3890
   called for objcopy --only-keep-debug.  */
3891
25
      this_hdr->sh_type = sh_type;
3892
25
    }
3893
1.11k
}
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.11k
{
3903
1.11k
  bool *failedptr = (bool *) failedptrarg;
3904
1.11k
  asection *elt, *first;
3905
1.11k
  unsigned char *loc;
3906
1.11k
  bool gas;
3907
3908
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3909
     elfxx-ia64.c.  */
3910
1.11k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3911
13
      || sec->size == 0
3912
13
      || *failedptr)
3913
1.09k
    return;
3914
3915
13
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3916
13
    {
3917
13
      unsigned long symindx = 0;
3918
3919
      /* elf_group_id will have been set up by objcopy and the
3920
   generic linker.  */
3921
13
      if (elf_group_id (sec) != NULL)
3922
13
  symindx = elf_group_id (sec)->udata.i;
3923
3924
13
      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
13
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3938
13
    }
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
13
  gas = true;
3974
13
  if (sec->contents == NULL)
3975
13
    {
3976
13
      gas = false;
3977
13
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3978
3979
      /* Arrange for the section to be written out.  */
3980
13
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3981
13
      if (sec->contents == NULL)
3982
0
  {
3983
0
    *failedptr = true;
3984
0
    return;
3985
0
  }
3986
13
      sec->alloced = 1;
3987
13
    }
3988
3989
13
  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
13
  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
35
  while (elt != NULL)
4001
35
    {
4002
35
      asection *s;
4003
4004
35
      s = elt;
4005
35
      if (!gas)
4006
35
  s = s->output_section;
4007
35
      if (s != NULL
4008
35
    && !bfd_is_abs_section (s))
4009
35
  {
4010
35
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4011
35
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4012
4013
35
    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
35
    if (elf_sec->rela.hdr != NULL
4025
25
        && (gas
4026
25
      || (input_elf_sec->rela.hdr != NULL
4027
25
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4028
25
      {
4029
25
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4030
25
        loc -= 4;
4031
25
        if (loc == sec->contents)
4032
0
    break;
4033
25
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4034
25
      }
4035
35
    loc -= 4;
4036
35
    if (loc == sec->contents)
4037
0
      break;
4038
35
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4039
35
  }
4040
35
      elt = elf_next_in_group (elt);
4041
35
      if (elt == first)
4042
13
  break;
4043
35
    }
4044
4045
  /* We should always get here with loc == sec->contents + 4.  Return
4046
     an error for bogus SHT_GROUP sections.  */
4047
13
  loc -= 4;
4048
13
  if (loc != sec->contents)
4049
0
    {
4050
      /* xgettext:c-format */
4051
0
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
4052
0
        abfd, sec);
4053
0
      bfd_set_error (bfd_error_bad_value);
4054
0
      *failedptr = true;
4055
0
      return;
4056
0
    }
4057
4058
13
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4059
13
}
4060
4061
/* Given NAME, the name of a relocation section stripped of its
4062
   .rel/.rela prefix, return the section in ABFD to which the
4063
   relocations apply.  */
4064
4065
asection *
4066
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4067
47
{
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
47
  if (get_elf_backend_data (abfd)->want_got_plt
4071
27
      && streq (name, ".plt"))
4072
9
    {
4073
9
      asection *sec;
4074
4075
9
      name = ".got.plt";
4076
9
      sec = bfd_get_section_by_name (abfd, name);
4077
9
      if (sec != NULL)
4078
8
  return sec;
4079
1
      name = ".got";
4080
1
    }
4081
4082
39
  return bfd_get_section_by_name (abfd, name);
4083
47
}
4084
4085
/* Return the section to which RELOC_SEC applies.  */
4086
4087
static asection *
4088
elf_get_reloc_section (asection *reloc_sec)
4089
72
{
4090
72
  const char *name;
4091
72
  unsigned int type;
4092
72
  bfd *abfd;
4093
72
  elf_backend_data *bed;
4094
4095
72
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4096
72
  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
72
  name = reloc_sec->name;
4101
72
  if (!startswith (name, ".rel"))
4102
18
    return NULL;
4103
54
  name += 4;
4104
54
  if (type == SHT_RELA && *name++ != 'a')
4105
0
    return NULL;
4106
4107
54
  abfd = reloc_sec->owner;
4108
54
  bed = get_elf_backend_data (abfd);
4109
54
  return bed->get_reloc_section (abfd, name);
4110
54
}
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
139
{
4120
139
  struct elf_obj_tdata *t = elf_tdata (abfd);
4121
139
  asection *sec;
4122
139
  unsigned int section_number;
4123
139
  Elf_Internal_Shdr **i_shdrp;
4124
139
  struct bfd_elf_section_data *d;
4125
139
  bool need_symtab;
4126
139
  size_t amt;
4127
4128
139
  section_number = 1;
4129
4130
139
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4131
4132
  /* SHT_GROUP sections are in relocatable files only.  */
4133
139
  if (link_info == NULL || !link_info->resolve_section_groups)
4134
139
    {
4135
139
      size_t reloc_count = 0;
4136
4137
      /* Put SHT_GROUP sections first.  */
4138
1.24k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4139
1.11k
  {
4140
1.11k
    d = elf_section_data (sec);
4141
4142
1.11k
    if (d->this_hdr.sh_type == SHT_GROUP)
4143
13
      {
4144
13
        if (sec->flags & SEC_LINKER_CREATED)
4145
0
    {
4146
      /* Remove the linker created SHT_GROUP sections.  */
4147
0
      bfd_section_list_remove (abfd, sec);
4148
0
      abfd->section_count--;
4149
0
    }
4150
13
        else
4151
13
    d->this_idx = section_number++;
4152
13
      }
4153
4154
    /* Count relocations.  */
4155
1.11k
    reloc_count += sec->reloc_count;
4156
1.11k
  }
4157
4158
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4159
139
      if (reloc_count == 0)
4160
115
  abfd->flags &= ~HAS_RELOC;
4161
24
      else
4162
24
  abfd->flags |= HAS_RELOC;
4163
139
    }
4164
4165
1.24k
  for (sec = abfd->sections; sec; sec = sec->next)
4166
1.11k
    {
4167
1.11k
      d = elf_section_data (sec);
4168
4169
1.11k
      if (d->this_hdr.sh_type != SHT_GROUP)
4170
1.09k
  d->this_idx = section_number++;
4171
1.11k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4172
1.11k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4173
1.11k
      if (d->rel.hdr)
4174
0
  {
4175
0
    d->rel.idx = section_number++;
4176
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4177
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4178
0
  }
4179
1.11k
      else
4180
1.11k
  d->rel.idx = 0;
4181
4182
1.11k
      if (d->rela.hdr)
4183
167
  {
4184
167
    d->rela.idx = section_number++;
4185
167
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4186
167
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4187
167
  }
4188
943
      else
4189
943
  d->rela.idx = 0;
4190
1.11k
    }
4191
4192
139
  need_symtab = (bfd_get_symcount (abfd) > 0
4193
98
     || (link_info == NULL
4194
98
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4195
98
       == HAS_RELOC)));
4196
139
  if (need_symtab)
4197
41
    {
4198
41
      elf_onesymtab (abfd) = section_number++;
4199
41
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4200
41
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4201
0
  {
4202
0
    elf_section_list *entry;
4203
4204
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4205
4206
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4207
0
    entry->ndx = section_number++;
4208
0
    elf_symtab_shndx_list (abfd) = entry;
4209
0
    entry->hdr.sh_name
4210
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4211
0
              ".symtab_shndx", false);
4212
0
    if (entry->hdr.sh_name == (unsigned int) -1)
4213
0
      return false;
4214
0
  }
4215
41
      elf_strtab_sec (abfd) = section_number++;
4216
41
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4217
41
    }
4218
4219
139
  elf_shstrtab_sec (abfd) = section_number++;
4220
139
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4221
139
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4222
4223
139
  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
139
  elf_numsections (abfd) = section_number;
4232
139
  elf_elfheader (abfd)->e_shnum = section_number;
4233
4234
  /* Set up the list of section header pointers, in agreement with the
4235
     indices.  */
4236
139
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4237
139
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4238
139
  if (i_shdrp == NULL)
4239
0
    return false;
4240
4241
139
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4242
139
             sizeof (Elf_Internal_Shdr));
4243
139
  if (i_shdrp[0] == NULL)
4244
0
    {
4245
0
      bfd_release (abfd, i_shdrp);
4246
0
      return false;
4247
0
    }
4248
4249
139
  elf_elfsections (abfd) = i_shdrp;
4250
4251
139
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4252
139
  if (need_symtab)
4253
41
    {
4254
41
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4255
41
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4256
0
  {
4257
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4258
0
    BFD_ASSERT (entry != NULL);
4259
0
    i_shdrp[entry->ndx] = & entry->hdr;
4260
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4261
0
  }
4262
41
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4263
41
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4264
41
    }
4265
4266
1.24k
  for (sec = abfd->sections; sec; sec = sec->next)
4267
1.11k
    {
4268
1.11k
      asection *s;
4269
4270
1.11k
      d = elf_section_data (sec);
4271
4272
1.11k
      i_shdrp[d->this_idx] = &d->this_hdr;
4273
1.11k
      if (d->rel.idx != 0)
4274
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4275
1.11k
      if (d->rela.idx != 0)
4276
167
  i_shdrp[d->rela.idx] = d->rela.hdr;
4277
4278
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4279
4280
      /* sh_link of a reloc section is the section index of the symbol
4281
   table.  sh_info is the section index of the section to which
4282
   the relocation entries apply.  */
4283
1.11k
      if (d->rel.idx != 0)
4284
0
  {
4285
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4286
0
    d->rel.hdr->sh_info = d->this_idx;
4287
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4288
0
  }
4289
1.11k
      if (d->rela.idx != 0)
4290
167
  {
4291
167
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4292
167
    d->rela.hdr->sh_info = d->this_idx;
4293
167
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4294
167
  }
4295
4296
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4297
1.11k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4298
15
  {
4299
15
    s = elf_linked_to_section (sec);
4300
    /* We can now have a NULL linked section pointer.
4301
       This happens when the sh_link field is 0, which is done
4302
       when a linked to section is discarded but the linking
4303
       section has been retained for some reason.  */
4304
15
    if (s)
4305
0
      {
4306
        /* Check discarded linkonce section.  */
4307
0
        if (discarded_section (s))
4308
0
    {
4309
0
      asection *kept;
4310
0
      _bfd_error_handler
4311
        /* xgettext:c-format */
4312
0
        (_("%pB: sh_link of section `%pA' points to"
4313
0
           " discarded section `%pA' of `%pB'"),
4314
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4315
      /* Point to the kept section if it has the same
4316
         size as the discarded one.  */
4317
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4318
0
      if (kept == NULL)
4319
0
        {
4320
0
          bfd_set_error (bfd_error_bad_value);
4321
0
          return false;
4322
0
        }
4323
0
      s = kept;
4324
0
    }
4325
        /* Handle objcopy. */
4326
0
        else if (s->output_section == NULL)
4327
0
    {
4328
0
      _bfd_error_handler
4329
        /* xgettext:c-format */
4330
0
        (_("%pB: sh_link of section `%pA' points to"
4331
0
           " removed section `%pA' of `%pB'"),
4332
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4333
0
      bfd_set_error (bfd_error_bad_value);
4334
0
      return false;
4335
0
    }
4336
0
        s = s->output_section;
4337
0
        d->this_hdr.sh_link
4338
0
    = _bfd_elf_section_from_bfd_section (abfd, s);
4339
0
      }
4340
15
  }
4341
4342
1.11k
      switch (d->this_hdr.sh_type)
4343
1.11k
  {
4344
0
  case SHT_REL:
4345
72
  case SHT_RELA:
4346
    /* sh_link is the section index of the symbol table.
4347
       sh_info is the section index of the section to which the
4348
       relocation entries apply.  */
4349
72
    if (d->this_hdr.sh_link == 0)
4350
72
      {
4351
        /* FIXME maybe: If this is a reloc section which we are
4352
     treating as a normal section then we likely should
4353
     not be assuming its sh_link is .dynsym or .symtab.  */
4354
72
        if ((sec->flags & SEC_ALLOC) != 0)
4355
37
    {
4356
37
      s = bfd_get_section_by_name (abfd, ".dynsym");
4357
37
      if (s != NULL)
4358
22
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4359
37
    }
4360
35
        else
4361
35
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4362
72
      }
4363
4364
72
    s = elf_get_reloc_section (sec);
4365
72
    if (s != NULL)
4366
39
      {
4367
39
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4368
39
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4369
39
      }
4370
72
    break;
4371
4372
29
  case SHT_STRTAB:
4373
    /* We assume that a section named .stab*str is a stabs
4374
       string section.  We look for a section with the same name
4375
       but without the trailing ``str'', and set its sh_link
4376
       field to point to this section.  */
4377
29
    if (startswith (sec->name, ".stab")
4378
0
        && streq (sec->name + strlen (sec->name) - 3, "str"))
4379
0
      {
4380
0
        size_t len;
4381
0
        char *alc;
4382
4383
0
        len = strlen (sec->name);
4384
0
        alc = (char *) bfd_malloc (len - 2);
4385
0
        if (alc == NULL)
4386
0
    return false;
4387
0
        memcpy (alc, sec->name, len - 3);
4388
0
        alc[len - 3] = '\0';
4389
0
        s = bfd_get_section_by_name (abfd, alc);
4390
0
        free (alc);
4391
0
        if (s != NULL)
4392
0
    {
4393
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4394
4395
      /* This is a .stab section.  */
4396
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4397
0
    }
4398
0
      }
4399
29
    break;
4400
4401
29
  case SHT_DYNAMIC:
4402
40
  case SHT_DYNSYM:
4403
46
  case SHT_GNU_verneed:
4404
46
  case SHT_GNU_verdef:
4405
    /* sh_link is the section header index of the string table
4406
       used for the dynamic entries, or the symbol table, or the
4407
       version strings.  */
4408
46
    s = bfd_get_section_by_name (abfd, ".dynstr");
4409
46
    if (s != NULL)
4410
28
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4411
46
    break;
4412
4413
0
  case SHT_GNU_LIBLIST:
4414
    /* sh_link is the section header index of the prelink library
4415
       list used for the dynamic entries, or the symbol table, or
4416
       the version strings.  */
4417
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4418
0
                ? ".dynstr" : ".gnu.libstr"));
4419
0
    if (s != NULL)
4420
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4421
0
    break;
4422
4423
20
  case SHT_HASH:
4424
32
  case SHT_GNU_HASH:
4425
38
  case SHT_GNU_versym:
4426
    /* sh_link is the section header index of the symbol table
4427
       this hash table or version table is for.  */
4428
38
    s = bfd_get_section_by_name (abfd, ".dynsym");
4429
38
    if (s != NULL)
4430
17
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4431
38
    break;
4432
4433
13
  case SHT_GROUP:
4434
13
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4435
1.11k
  }
4436
1.11k
    }
4437
4438
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4439
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4440
     debug section name from .debug_* to .zdebug_* if needed.  */
4441
4442
139
  return true;
4443
139
}
4444
4445
static bool
4446
sym_is_global (bfd *abfd, asymbol *sym)
4447
2.72k
{
4448
  /* If the backend has a special mapping, use it.  */
4449
2.72k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4450
2.72k
  if (bed->elf_backend_sym_is_global)
4451
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4452
4453
2.72k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4454
2.26k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4455
1.14k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4456
2.72k
}
4457
4458
/* Filter global symbols of ABFD to include in the import library.  All
4459
   SYMCOUNT symbols of ABFD can be examined from their pointers in
4460
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4461
   the beginning of that array.
4462
4463
   Returns the number of symbols to keep.  */
4464
4465
unsigned int
4466
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4467
        asymbol **syms, long symcount)
4468
0
{
4469
0
  long src_count, dst_count = 0;
4470
4471
0
  for (src_count = 0; src_count < symcount; src_count++)
4472
0
    {
4473
0
      asymbol *sym = syms[src_count];
4474
0
      char *name = (char *) bfd_asymbol_name (sym);
4475
0
      struct bfd_link_hash_entry *h;
4476
4477
0
      if (!sym_is_global (abfd, sym))
4478
0
  continue;
4479
4480
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4481
0
      if (h == NULL)
4482
0
  continue;
4483
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4484
0
  continue;
4485
0
      if (h->linker_def || h->ldscript_def)
4486
0
  continue;
4487
4488
0
      syms[dst_count++] = sym;
4489
0
    }
4490
4491
0
  syms[dst_count] = NULL;
4492
4493
0
  return dst_count;
4494
0
}
4495
4496
/* Don't output symbols for sections that are not going to be output,
4497
   that are duplicates or there is no BFD section.  */
4498
4499
static bool
4500
ignore_sym (asymbol *sym)
4501
4.76k
{
4502
4.76k
  if (sym == NULL)
4503
0
    return false;
4504
4505
4.76k
  if (sym->section == NULL)
4506
0
    return true;
4507
4508
4.76k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4509
2.26k
    {
4510
2.26k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4511
1.93k
  return true;
4512
      /* With ld -r on generic elf targets it is possible to have
4513
   multiple section symbols in the output for a given section.
4514
   We'd like to get rid of all but the first one.  This drops
4515
   them if the first input section is non-zero size, but fails
4516
   to do so if the first input section is zero sized.  */
4517
336
      if (sym->section->output_offset != 0)
4518
0
  return true;
4519
336
    }
4520
4521
2.83k
  return discarded_section (sym->section);
4522
4.76k
}
4523
4524
/* Map symbol from it's internal number to the external number, moving
4525
   all local symbols to be at the head of the list.  */
4526
4527
static bool
4528
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4529
41
{
4530
41
  unsigned int symcount = bfd_get_symcount (abfd);
4531
41
  asymbol **syms = bfd_get_outsymbols (abfd);
4532
41
  asymbol **sect_syms;
4533
41
  unsigned int num_locals = 0;
4534
41
  unsigned int num_globals = 0;
4535
41
  unsigned int max_index = 0;
4536
41
  unsigned int idx;
4537
41
  asection *asect;
4538
41
  asymbol **new_syms;
4539
41
  size_t amt;
4540
4541
#ifdef DEBUG
4542
  fprintf (stderr, "elf_map_symbols\n");
4543
  fflush (stderr);
4544
#endif
4545
4546
870
  for (asect = abfd->sections; asect; asect = asect->next)
4547
829
    {
4548
829
      if (max_index < asect->index)
4549
788
  max_index = asect->index;
4550
829
    }
4551
4552
41
  max_index++;
4553
41
  amt = max_index * sizeof (asymbol *);
4554
41
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4555
41
  if (sect_syms == NULL)
4556
0
    return false;
4557
41
  elf_section_syms (abfd) = sect_syms;
4558
41
  elf_num_section_syms (abfd) = max_index;
4559
4560
  /* Init sect_syms entries for any section symbols we have already
4561
     decided to output.  */
4562
1.50k
  for (idx = 0; idx < symcount; idx++)
4563
1.46k
    {
4564
1.46k
      asymbol *sym = syms[idx];
4565
4566
1.46k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4567
210
    && sym->value == 0
4568
191
    && !ignore_sym (sym)
4569
108
    && !bfd_is_abs_section (sym->section))
4570
102
  {
4571
102
    asection *sec = sym->section;
4572
4573
102
    if (sec->owner != abfd)
4574
102
      {
4575
102
        sec = sec->output_section;
4576
102
        if (sec == NULL)
4577
0
    return false;
4578
102
      }
4579
4580
102
    sect_syms[sec->index] = syms[idx];
4581
102
  }
4582
1.46k
    }
4583
4584
  /* Classify all of the symbols.  */
4585
1.50k
  for (idx = 0; idx < symcount; idx++)
4586
1.46k
    {
4587
1.46k
      if (ignore_sym (syms[idx]))
4588
96
  continue;
4589
1.36k
      if (sym_is_global (abfd, syms[idx]))
4590
798
  num_globals++;
4591
566
      else
4592
566
  num_locals++;
4593
1.36k
    }
4594
4595
  /* We will be adding a section symbol for each normal BFD section.  Most
4596
     sections will already have a section symbol in outsymbols, but
4597
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4598
     at least in that case.  */
4599
870
  for (asect = abfd->sections; asect; asect = asect->next)
4600
829
    {
4601
829
      asymbol *sym = asect->symbol;
4602
      /* Don't include ignored section symbols.  */
4603
829
      if (!ignore_sym (sym)
4604
0
    && sect_syms[asect->index] == NULL)
4605
0
  {
4606
0
    if (sym_is_global (abfd, asect->symbol))
4607
0
      num_globals++;
4608
0
    else
4609
0
      num_locals++;
4610
0
  }
4611
829
    }
4612
4613
  /* Now sort the symbols so the local symbols are first.  */
4614
41
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4615
41
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4616
41
  if (new_syms == NULL)
4617
0
    return false;
4618
4619
41
  unsigned int num_globals2 = 0;
4620
41
  unsigned int num_locals2 = 0;
4621
1.50k
  for (idx = 0; idx < symcount; idx++)
4622
1.46k
    {
4623
1.46k
      asymbol *sym = syms[idx];
4624
1.46k
      unsigned int i;
4625
4626
1.46k
      if (ignore_sym (sym))
4627
96
  continue;
4628
4629
1.36k
      if (sym_is_global (abfd, sym))
4630
798
  i = num_locals + num_globals2++;
4631
566
      else
4632
566
  i = num_locals2++;
4633
1.36k
      new_syms[i] = sym;
4634
1.36k
      sym->udata.i = i + 1;
4635
1.36k
    }
4636
870
  for (asect = abfd->sections; asect; asect = asect->next)
4637
829
    {
4638
829
      asymbol *sym = asect->symbol;
4639
829
      if (!ignore_sym (sym)
4640
0
    && sect_syms[asect->index] == NULL)
4641
0
  {
4642
0
    unsigned int i;
4643
4644
0
    sect_syms[asect->index] = sym;
4645
0
    if (sym_is_global (abfd, sym))
4646
0
      i = num_locals + num_globals2++;
4647
0
    else
4648
0
      i = num_locals2++;
4649
0
    new_syms[i] = sym;
4650
0
    sym->udata.i = i + 1;
4651
0
  }
4652
829
    }
4653
4654
41
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4655
4656
41
  *pnum_locals = num_locals;
4657
41
  return true;
4658
41
}
4659
4660
/* Assign a file position to a section, optionally aligning to the
4661
   required section alignment.  */
4662
4663
file_ptr
4664
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4665
             file_ptr offset,
4666
             bool align,
4667
             unsigned char log_file_align)
4668
1.20k
{
4669
1.20k
  if (i_shdrp->sh_addralign > 1)
4670
647
    {
4671
647
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4672
4673
647
      if (align)
4674
77
  offset = BFD_ALIGN (offset, salign);
4675
570
      else if (log_file_align)
4676
570
  {
4677
    /* Heuristic: Cap alignment at log_file_align.  */
4678
570
    file_ptr falign = 1u << log_file_align;
4679
4680
570
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4681
570
  }
4682
647
    }
4683
1.20k
  i_shdrp->sh_offset = offset;
4684
1.20k
  if (i_shdrp->bfd_section != NULL)
4685
706
    i_shdrp->bfd_section->filepos = offset;
4686
1.20k
  if (i_shdrp->sh_type != SHT_NOBITS)
4687
1.19k
    offset += i_shdrp->sh_size;
4688
1.20k
  return offset;
4689
1.20k
}
4690
4691
/* Compute the file positions we are going to put the sections at, and
4692
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4693
   is not NULL, this is being called by the ELF backend linker.  */
4694
4695
bool
4696
_bfd_elf_compute_section_file_positions (bfd *abfd,
4697
           struct bfd_link_info *link_info)
4698
139
{
4699
139
  elf_backend_data *bed = get_elf_backend_data (abfd);
4700
139
  struct fake_section_arg fsargs;
4701
139
  bool failed;
4702
139
  struct elf_strtab_hash *strtab = NULL;
4703
139
  Elf_Internal_Shdr *shstrtab_hdr;
4704
139
  bool need_symtab;
4705
4706
139
  if (abfd->output_has_begun)
4707
0
    return true;
4708
4709
  /* Do any elf backend specific processing first.  */
4710
139
  if (bed->elf_backend_begin_write_processing)
4711
0
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4712
4713
139
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4714
0
    return false;
4715
4716
139
  fsargs.failed = false;
4717
139
  fsargs.link_info = link_info;
4718
139
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4719
139
  if (fsargs.failed)
4720
0
    return false;
4721
4722
139
  if (!assign_section_numbers (abfd, link_info))
4723
0
    return false;
4724
4725
  /* The backend linker builds symbol table information itself.  */
4726
139
  need_symtab = (link_info == NULL
4727
139
     && (bfd_get_symcount (abfd) > 0
4728
98
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4729
98
       == HAS_RELOC)));
4730
139
  if (need_symtab)
4731
41
    {
4732
      /* Non-zero if doing a relocatable link.  */
4733
41
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4734
4735
41
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4736
0
  return false;
4737
41
    }
4738
4739
139
  failed = false;
4740
139
  if (link_info == NULL)
4741
139
    {
4742
139
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4743
139
      if (failed)
4744
0
  goto err_free_strtab;
4745
139
    }
4746
4747
139
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4748
  /* sh_name was set in init_file_header.  */
4749
139
  shstrtab_hdr->sh_type = SHT_STRTAB;
4750
  /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
4751
     when tdata is allocated.  */
4752
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4753
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4754
139
  shstrtab_hdr->sh_addralign = 1;
4755
4756
139
  if (!assign_file_positions_except_relocs (abfd, link_info))
4757
8
    goto err_free_strtab;
4758
4759
131
  if (strtab != NULL)
4760
37
    {
4761
37
      file_ptr off;
4762
37
      Elf_Internal_Shdr *hdr;
4763
4764
37
      off = elf_next_file_pos (abfd);
4765
4766
37
      hdr = & elf_symtab_hdr (abfd);
4767
37
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4768
4769
37
      if (elf_symtab_shndx_list (abfd) != NULL)
4770
0
  {
4771
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4772
0
    if (hdr->sh_size != 0)
4773
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4774
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4775
0
  }
4776
4777
37
      hdr = &elf_tdata (abfd)->strtab_hdr;
4778
37
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4779
4780
37
      elf_next_file_pos (abfd) = off;
4781
4782
      /* Now that we know where the .strtab section goes, write it
4783
   out.  */
4784
37
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4785
37
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4786
0
  goto err_free_strtab;
4787
37
      _bfd_elf_strtab_free (strtab);
4788
37
    }
4789
4790
131
  abfd->output_has_begun = true;
4791
131
  return true;
4792
4793
8
 err_free_strtab:
4794
8
  if (strtab != NULL)
4795
4
    _bfd_elf_strtab_free (strtab);
4796
8
  return false;
4797
131
}
4798
4799
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4800
   function effectively returns whether --eh-frame-hdr is given on the
4801
   command line.  After size_dynamic_sections the result reflects
4802
   whether .eh_frame_hdr will actually be output (sizing isn't done
4803
   until ldemul_after_allocation).  */
4804
4805
static asection *
4806
elf_eh_frame_hdr (const struct bfd_link_info *info)
4807
27
{
4808
27
  if (info != NULL && is_elf_hash_table (info->hash))
4809
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4810
27
  return NULL;
4811
27
}
4812
4813
/* Make an initial estimate of the size of the program header.  If we
4814
   get the number wrong here, we'll redo section placement.  */
4815
4816
static bfd_size_type
4817
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4818
14
{
4819
14
  size_t segs;
4820
14
  asection *s;
4821
14
  elf_backend_data *bed;
4822
4823
  /* Assume we will need exactly two PT_LOAD segments: one for text
4824
     and one for data.  */
4825
14
  segs = 2;
4826
4827
14
  s = bfd_get_section_by_name (abfd, ".interp");
4828
14
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4829
0
    {
4830
      /* If we have a loadable interpreter section, we need a
4831
   PT_INTERP segment.  In this case, assume we also need a
4832
   PT_PHDR segment, although that may not be true for all
4833
   targets.  */
4834
0
      segs += 2;
4835
0
    }
4836
4837
14
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4838
0
    {
4839
      /* We need a PT_DYNAMIC segment.  */
4840
0
      ++segs;
4841
0
    }
4842
4843
14
  if (info != NULL && info->relro)
4844
0
    {
4845
      /* We need a PT_GNU_RELRO segment.  */
4846
0
      ++segs;
4847
0
    }
4848
4849
14
  if (elf_eh_frame_hdr (info))
4850
0
    {
4851
      /* We need a PT_GNU_EH_FRAME segment.  */
4852
0
      ++segs;
4853
0
    }
4854
4855
14
  if (elf_stack_flags (abfd))
4856
0
    {
4857
      /* We need a PT_GNU_STACK segment.  */
4858
0
      ++segs;
4859
0
    }
4860
4861
14
  if (elf_sframe (abfd))
4862
0
    {
4863
      /* We need a PT_GNU_SFRAME segment.  */
4864
0
      ++segs;
4865
0
    }
4866
4867
14
  s = bfd_get_section_by_name (abfd,
4868
14
             NOTE_GNU_PROPERTY_SECTION_NAME);
4869
14
  if (s != NULL && s->size != 0)
4870
0
    {
4871
      /* We need a PT_GNU_PROPERTY segment.  */
4872
0
      ++segs;
4873
0
    }
4874
4875
76
  for (s = abfd->sections; s != NULL; s = s->next)
4876
62
    {
4877
62
      if ((s->flags & SEC_LOAD) != 0
4878
19
    && elf_section_type (s) == SHT_NOTE)
4879
0
  {
4880
0
    unsigned int alignment_power;
4881
    /* We need a PT_NOTE segment.  */
4882
0
    ++segs;
4883
    /* Try to create just one PT_NOTE segment for all adjacent
4884
       loadable SHT_NOTE sections.  gABI requires that within a
4885
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4886
       each note should have the same alignment.  So we check
4887
       whether the sections are correctly aligned.  */
4888
0
    alignment_power = s->alignment_power;
4889
0
    while (s->next != NULL
4890
0
     && s->next->alignment_power == alignment_power
4891
0
     && (s->next->flags & SEC_LOAD) != 0
4892
0
     && elf_section_type (s->next) == SHT_NOTE)
4893
0
      s = s->next;
4894
0
  }
4895
62
    }
4896
4897
37
  for (s = abfd->sections; s != NULL; s = s->next)
4898
30
    {
4899
30
      if (s->flags & SEC_THREAD_LOCAL)
4900
7
  {
4901
    /* We need a PT_TLS segment.  */
4902
7
    ++segs;
4903
7
    break;
4904
7
  }
4905
30
    }
4906
4907
14
  bed = get_elf_backend_data (abfd);
4908
4909
14
  if ((abfd->flags & D_PAGED) != 0
4910
14
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4911
0
    {
4912
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4913
0
      bfd_vma commonpagesize;
4914
0
      unsigned int page_align_power;
4915
4916
0
      if (info != NULL)
4917
0
  commonpagesize = info->commonpagesize;
4918
0
      else
4919
0
  commonpagesize = bed->commonpagesize;
4920
0
      page_align_power = bfd_log2 (commonpagesize);
4921
0
      for (s = abfd->sections; s != NULL; s = s->next)
4922
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4923
0
    {
4924
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4925
0
        {
4926
0
    _bfd_error_handler
4927
      /* xgettext:c-format */
4928
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4929
0
         "sh_info field: %d"),
4930
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4931
0
    continue;
4932
0
        }
4933
      /* Align mbind section to page size.  */
4934
0
      if (s->alignment_power < page_align_power)
4935
0
        s->alignment_power = page_align_power;
4936
0
      segs ++;
4937
0
    }
4938
0
    }
4939
4940
  /* Let the backend count up any program headers it might need.  */
4941
14
  if (bed->elf_backend_additional_program_headers)
4942
14
    {
4943
14
      int a;
4944
4945
14
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4946
14
      if (a == -1)
4947
0
  abort ();
4948
14
      segs += a;
4949
14
    }
4950
4951
14
  return segs * bed->s->sizeof_phdr;
4952
14
}
4953
4954
/* Find the segment that contains the output_section of section.  */
4955
4956
Elf_Internal_Phdr *
4957
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4958
0
{
4959
0
  struct elf_segment_map *m;
4960
0
  Elf_Internal_Phdr *p;
4961
4962
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4963
0
       m != NULL;
4964
0
       m = m->next, p++)
4965
0
    {
4966
0
      int i;
4967
4968
0
      for (i = m->count - 1; i >= 0; i--)
4969
0
  if (m->sections[i] == section)
4970
0
    return p;
4971
0
    }
4972
4973
0
  return NULL;
4974
0
}
4975
4976
/* Create a mapping from a set of sections to a program segment.  */
4977
4978
static struct elf_segment_map *
4979
make_mapping (bfd *abfd,
4980
        asection **sections,
4981
        unsigned int from,
4982
        unsigned int to,
4983
        bool phdr)
4984
26
{
4985
26
  struct elf_segment_map *m;
4986
26
  unsigned int i;
4987
26
  asection **hdrpp;
4988
26
  size_t amt;
4989
4990
26
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4991
26
  amt += (to - from) * sizeof (asection *);
4992
26
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4993
26
  if (m == NULL)
4994
0
    return NULL;
4995
26
  m->next = NULL;
4996
26
  m->p_type = PT_LOAD;
4997
62
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4998
36
    m->sections[i - from] = *hdrpp;
4999
26
  m->count = to - from;
5000
5001
26
  if (from == 0 && phdr)
5002
4
    {
5003
      /* Include the headers in the first PT_LOAD segment.  */
5004
4
      m->includes_filehdr = 1;
5005
4
      m->includes_phdrs = 1;
5006
4
    }
5007
5008
26
  return m;
5009
26
}
5010
5011
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5012
   on failure.  */
5013
5014
struct elf_segment_map *
5015
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5016
0
{
5017
0
  struct elf_segment_map *m;
5018
5019
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
5020
0
               sizeof (struct elf_segment_map));
5021
0
  if (m == NULL)
5022
0
    return NULL;
5023
0
  m->next = NULL;
5024
0
  m->p_type = PT_DYNAMIC;
5025
0
  m->count = 1;
5026
0
  m->sections[0] = dynsec;
5027
5028
0
  return m;
5029
0
}
5030
5031
/* Possibly add or remove segments from the segment map.  */
5032
5033
static bool
5034
elf_modify_segment_map (bfd *abfd,
5035
      struct bfd_link_info *info,
5036
      bool remove_empty_load)
5037
64
{
5038
64
  struct elf_segment_map **m;
5039
64
  elf_backend_data *bed;
5040
5041
  /* The placement algorithm assumes that non allocated sections are
5042
     not in PT_LOAD segments.  We ensure this here by removing such
5043
     sections from the segment map.  We also remove excluded
5044
     sections.  Finally, any PT_LOAD segment without sections is
5045
     removed.  */
5046
64
  m = &elf_seg_map (abfd);
5047
311
  while (*m)
5048
247
    {
5049
247
      unsigned int i, new_count;
5050
5051
938
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5052
691
  {
5053
691
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5054
688
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5055
41
      || (*m)->p_type != PT_LOAD))
5056
688
      {
5057
688
        (*m)->sections[new_count] = (*m)->sections[i];
5058
688
        new_count++;
5059
688
      }
5060
691
  }
5061
247
      (*m)->count = new_count;
5062
5063
247
      if (remove_empty_load
5064
247
    && (*m)->p_type == PT_LOAD
5065
68
    && (*m)->count == 0
5066
11
    && !(*m)->includes_phdrs)
5067
6
  *m = (*m)->next;
5068
241
      else
5069
241
  m = &(*m)->next;
5070
247
    }
5071
5072
64
  bed = get_elf_backend_data (abfd);
5073
64
  if (bed->elf_backend_modify_segment_map != NULL)
5074
3
    {
5075
3
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5076
0
  return false;
5077
3
    }
5078
5079
64
  return true;
5080
64
}
5081
5082
#define IS_TBSS(s) \
5083
62
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5084
5085
/* Set up a mapping from BFD sections to program segments.  Update
5086
   NEED_LAYOUT if the section layout is changed.  */
5087
5088
bool
5089
bfd_elf_map_sections_to_segments (bfd *abfd,
5090
          struct bfd_link_info *info,
5091
          bool *need_layout)
5092
65
{
5093
65
  unsigned int count;
5094
65
  struct elf_segment_map *m;
5095
65
  asection **sections = NULL;
5096
65
  elf_backend_data *bed = get_elf_backend_data (abfd);
5097
65
  bool no_user_phdrs;
5098
5099
65
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5100
5101
65
  if (info != NULL)
5102
0
    {
5103
0
      info->user_phdrs = !no_user_phdrs;
5104
5105
      /* Size the relative relocations if DT_RELR is enabled.  */
5106
0
      if (info->enable_dt_relr
5107
0
    && need_layout != NULL
5108
0
    && bed->size_relative_relocs
5109
0
    && !bed->size_relative_relocs (info, need_layout))
5110
0
  info->callbacks->fatal
5111
0
    (_("%P: failed to size relative relocations\n"));
5112
0
    }
5113
5114
65
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5115
14
    {
5116
14
      asection *s;
5117
14
      unsigned int i;
5118
14
      struct elf_segment_map *mfirst;
5119
14
      struct elf_segment_map **pm;
5120
14
      asection *last_hdr;
5121
14
      bfd_vma last_size;
5122
14
      unsigned int hdr_index;
5123
14
      bfd_vma maxpagesize;
5124
14
      asection **hdrpp;
5125
14
      bool phdr_in_segment;
5126
14
      bool writable;
5127
14
      bool executable;
5128
14
      unsigned int tls_count = 0;
5129
14
      asection *first_tls = NULL;
5130
14
      asection *first_mbind = NULL;
5131
14
      asection *dynsec, *eh_frame_hdr;
5132
14
      asection *sframe;
5133
14
      size_t amt;
5134
14
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5135
14
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5136
14
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5137
5138
      /* Select the allocated sections, and sort them.  */
5139
5140
14
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5141
14
      sections = (asection **) bfd_malloc (amt);
5142
14
      if (sections == NULL)
5143
0
  goto error_return;
5144
5145
      /* Calculate top address, avoiding undefined behaviour of shift
5146
   left operator when shift count is equal to size of type
5147
   being shifted.  */
5148
14
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5149
14
      addr_mask = (addr_mask << 1) + 1;
5150
5151
14
      i = 0;
5152
76
      for (s = abfd->sections; s != NULL; s = s->next)
5153
62
  {
5154
62
    if ((s->flags & SEC_ALLOC) != 0)
5155
38
      {
5156
        /* target_index is unused until bfd_elf_final_link
5157
     starts output of section symbols.  Use it to make
5158
     qsort stable.  */
5159
38
        s->target_index = i;
5160
38
        sections[i] = s;
5161
38
        ++i;
5162
        /* A wrapping section potentially clashes with header.  */
5163
38
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5164
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5165
38
      }
5166
62
  }
5167
14
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5168
14
      count = i;
5169
5170
14
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5171
5172
14
      phdr_size = elf_program_header_size (abfd);
5173
14
      if (phdr_size == (bfd_size_type) -1)
5174
14
  phdr_size = get_program_header_size (abfd, info);
5175
14
      phdr_size += bed->s->sizeof_ehdr;
5176
      /* phdr_size is compared to LMA values which are in bytes.  */
5177
14
      phdr_size /= opb;
5178
14
      if (info != NULL)
5179
0
  maxpagesize = info->maxpagesize;
5180
14
      else
5181
14
  maxpagesize = bed->maxpagesize;
5182
14
      if (maxpagesize == 0)
5183
0
  maxpagesize = 1;
5184
14
      phdr_in_segment = info != NULL && info->load_phdrs;
5185
14
      if (count != 0
5186
14
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5187
14
        >= (phdr_size & (maxpagesize - 1))))
5188
  /* For compatibility with old scripts that may not be using
5189
     SIZEOF_HEADERS, add headers when it looks like space has
5190
     been left for them.  */
5191
4
  phdr_in_segment = true;
5192
5193
      /* Build the mapping.  */
5194
14
      mfirst = NULL;
5195
14
      pm = &mfirst;
5196
5197
      /* If we have a .interp section, then create a PT_PHDR segment for
5198
   the program headers and a PT_INTERP segment for the .interp
5199
   section.  */
5200
14
      s = bfd_get_section_by_name (abfd, ".interp");
5201
14
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5202
0
  {
5203
0
    amt = sizeof (struct elf_segment_map);
5204
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5205
0
    if (m == NULL)
5206
0
      goto error_return;
5207
0
    m->next = NULL;
5208
0
    m->p_type = PT_PHDR;
5209
0
    m->p_flags = PF_R;
5210
0
    m->p_flags_valid = 1;
5211
0
    m->includes_phdrs = 1;
5212
0
    phdr_in_segment = true;
5213
0
    *pm = m;
5214
0
    pm = &m->next;
5215
5216
0
    amt = sizeof (struct elf_segment_map);
5217
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5218
0
    if (m == NULL)
5219
0
      goto error_return;
5220
0
    m->next = NULL;
5221
0
    m->p_type = PT_INTERP;
5222
0
    m->count = 1;
5223
0
    m->sections[0] = s;
5224
5225
0
    *pm = m;
5226
0
    pm = &m->next;
5227
0
  }
5228
5229
      /* Look through the sections.  We put sections in the same program
5230
   segment when the start of the second section can be placed within
5231
   a few bytes of the end of the first section.  */
5232
14
      last_hdr = NULL;
5233
14
      last_size = 0;
5234
14
      hdr_index = 0;
5235
14
      writable = false;
5236
14
      executable = false;
5237
14
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5238
14
      if (dynsec != NULL
5239
0
    && (dynsec->flags & SEC_LOAD) == 0)
5240
0
  dynsec = NULL;
5241
5242
14
      if ((abfd->flags & D_PAGED) == 0)
5243
0
  phdr_in_segment = false;
5244
5245
      /* Deal with -Ttext or something similar such that the first section
5246
   is not adjacent to the program headers.  This is an
5247
   approximation, since at this point we don't know exactly how many
5248
   program headers we will need.  */
5249
14
      if (phdr_in_segment && count > 0)
5250
4
  {
5251
4
    bfd_vma phdr_lma;  /* Bytes.  */
5252
4
    bool separate_phdr = false;
5253
5254
4
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5255
4
    if (info != NULL
5256
0
        && info->separate_code
5257
0
        && (sections[0]->flags & SEC_CODE) != 0)
5258
0
      {
5259
        /* If data sections should be separate from code and
5260
     thus not executable, and the first section is
5261
     executable then put the file and program headers in
5262
     their own PT_LOAD.  */
5263
0
        if (!info->one_rosegment)
5264
0
    separate_phdr = true;
5265
5266
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5267
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5268
0
    {
5269
      /* The file and program headers are currently on the
5270
         same page as the first section.  Put them on the
5271
         previous page if we can.  */
5272
0
      if (phdr_lma >= maxpagesize)
5273
0
        phdr_lma -= maxpagesize;
5274
0
      else
5275
0
        separate_phdr = false;
5276
0
    }
5277
0
      }
5278
4
    if ((sections[0]->lma & addr_mask) < phdr_lma
5279
4
        || (sections[0]->lma & addr_mask) < phdr_size)
5280
      /* If file and program headers would be placed at the end
5281
         of memory then it's probably better to omit them.  */
5282
0
      phdr_in_segment = false;
5283
4
    else if (phdr_lma < wrap_to)
5284
      /* If a section wraps around to where we'll be placing
5285
         file and program headers, then the headers will be
5286
         overwritten.  */
5287
0
      phdr_in_segment = false;
5288
4
    else if (separate_phdr)
5289
0
      {
5290
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5291
0
        if (m == NULL)
5292
0
    goto error_return;
5293
0
        m->p_paddr = phdr_lma * opb;
5294
0
        m->p_vaddr_offset
5295
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5296
0
        m->p_paddr_valid = 1;
5297
0
        *pm = m;
5298
0
        pm = &m->next;
5299
0
        phdr_in_segment = false;
5300
0
      }
5301
4
  }
5302
5303
52
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5304
38
  {
5305
38
    asection *hdr;
5306
38
    bool new_segment;
5307
5308
38
    hdr = *hdrpp;
5309
5310
    /* See if this section and the last one will fit in the same
5311
       segment.  */
5312
5313
38
    if (last_hdr == NULL)
5314
14
      {
5315
        /* If we don't have a segment yet, then we don't need a new
5316
     one (we build the last one after this loop).  */
5317
14
        new_segment = false;
5318
14
      }
5319
24
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5320
2
      {
5321
        /* If this section has a different relation between the
5322
     virtual address and the load address, then we need a new
5323
     segment.  */
5324
2
        new_segment = true;
5325
2
      }
5326
22
    else if (hdr->lma < last_hdr->lma + last_size
5327
14
       || last_hdr->lma + last_size < last_hdr->lma)
5328
8
      {
5329
        /* If this section has a load address that makes it overlap
5330
     the previous section, then we need a new segment.  */
5331
8
        new_segment = true;
5332
8
      }
5333
14
    else if ((abfd->flags & D_PAGED) != 0
5334
14
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5335
14
           == (hdr->lma & -maxpagesize)))
5336
4
      {
5337
        /* If we are demand paged then we can't map two disk
5338
     pages onto the same memory page.  */
5339
4
        new_segment = false;
5340
4
      }
5341
    /* In the next test we have to be careful when last_hdr->lma is close
5342
       to the end of the address space.  If the aligned address wraps
5343
       around to the start of the address space, then there are no more
5344
       pages left in memory and it is OK to assume that the current
5345
       section can be included in the current segment.  */
5346
10
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5347
10
        + maxpagesize > last_hdr->lma)
5348
10
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5349
10
           + maxpagesize <= hdr->lma))
5350
3
      {
5351
        /* If putting this section in this segment would force us to
5352
     skip a page in the segment, then we need a new segment.  */
5353
3
        new_segment = true;
5354
3
      }
5355
7
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5356
3
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5357
0
      {
5358
        /* We don't want to put a loaded section after a
5359
     nonloaded (ie. bss style) section in the same segment
5360
     as that will force the non-loaded section to be loaded.
5361
     Consider .tbss sections as loaded for this purpose.  */
5362
0
        new_segment = true;
5363
0
      }
5364
7
    else if ((abfd->flags & D_PAGED) == 0)
5365
0
      {
5366
        /* If the file is not demand paged, which means that we
5367
     don't require the sections to be correctly aligned in the
5368
     file, then there is no other reason for a new segment.  */
5369
0
        new_segment = false;
5370
0
      }
5371
7
    else if (info != NULL
5372
0
       && info->separate_code
5373
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5374
0
      {
5375
0
        new_segment = true;
5376
0
      }
5377
7
    else if (! writable
5378
7
       && (hdr->flags & SEC_READONLY) == 0)
5379
1
      {
5380
        /* We don't want to put a writable section in a read only
5381
     segment.  */
5382
1
        new_segment = true;
5383
1
      }
5384
6
    else
5385
6
      {
5386
        /* Otherwise, we can use the same segment.  */
5387
6
        new_segment = false;
5388
6
      }
5389
5390
    /* Allow interested parties a chance to override our decision.  */
5391
38
    if (last_hdr != NULL
5392
24
        && info != NULL
5393
0
        && info->callbacks->override_segment_assignment != NULL)
5394
0
      new_segment
5395
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5396
0
                    last_hdr,
5397
0
                    new_segment);
5398
5399
38
    if (! new_segment)
5400
24
      {
5401
24
        if ((hdr->flags & SEC_READONLY) == 0)
5402
3
    writable = true;
5403
24
        if ((hdr->flags & SEC_CODE) != 0)
5404
8
    executable = true;
5405
24
        last_hdr = hdr;
5406
        /* .tbss sections effectively have zero size.  */
5407
24
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5408
24
        continue;
5409
24
      }
5410
5411
    /* We need a new program segment.  We must create a new program
5412
       header holding all the sections from hdr_index until hdr.  */
5413
5414
14
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5415
14
    if (m == NULL)
5416
0
      goto error_return;
5417
5418
14
    *pm = m;
5419
14
    pm = &m->next;
5420
5421
14
    if ((hdr->flags & SEC_READONLY) == 0)
5422
5
      writable = true;
5423
9
    else
5424
9
      writable = false;
5425
5426
14
    if ((hdr->flags & SEC_CODE) == 0)
5427
11
      executable = false;
5428
3
    else
5429
3
      executable = true;
5430
5431
14
    last_hdr = hdr;
5432
    /* .tbss sections effectively have zero size.  */
5433
14
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5434
14
    hdr_index = i;
5435
14
    phdr_in_segment = false;
5436
14
  }
5437
5438
      /* Create a final PT_LOAD program segment, but not if it's just
5439
   for .tbss.  */
5440
14
      if (last_hdr != NULL
5441
14
    && (i - hdr_index != 1
5442
11
        || !IS_TBSS (last_hdr)))
5443
12
  {
5444
12
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5445
12
    if (m == NULL)
5446
0
      goto error_return;
5447
5448
12
    *pm = m;
5449
12
    pm = &m->next;
5450
12
  }
5451
5452
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5453
14
      if (dynsec != NULL)
5454
0
  {
5455
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5456
0
    if (m == NULL)
5457
0
      goto error_return;
5458
0
    *pm = m;
5459
0
    pm = &m->next;
5460
0
  }
5461
5462
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5463
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5464
   because if we link together nonloadable .note sections and
5465
   loadable .note sections, we will generate two .note sections
5466
   in the output file.  */
5467
76
      for (s = abfd->sections; s != NULL; s = s->next)
5468
62
  {
5469
62
    if ((s->flags & SEC_LOAD) != 0
5470
19
        && elf_section_type (s) == SHT_NOTE)
5471
0
      {
5472
0
        asection *s2;
5473
0
        unsigned int alignment_power = s->alignment_power;
5474
5475
0
        count = 1;
5476
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5477
0
    {
5478
0
      if (s2->next->alignment_power == alignment_power
5479
0
          && (s2->next->flags & SEC_LOAD) != 0
5480
0
          && elf_section_type (s2->next) == SHT_NOTE
5481
0
          && align_power (s2->lma + s2->size / opb,
5482
0
              alignment_power)
5483
0
          == s2->next->lma)
5484
0
        count++;
5485
0
      else
5486
0
        break;
5487
0
    }
5488
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5489
0
        amt += count * sizeof (asection *);
5490
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5491
0
        if (m == NULL)
5492
0
    goto error_return;
5493
0
        m->next = NULL;
5494
0
        m->p_type = PT_NOTE;
5495
0
        m->count = count;
5496
0
        while (count > 1)
5497
0
    {
5498
0
      m->sections[m->count - count--] = s;
5499
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5500
0
      s = s->next;
5501
0
    }
5502
0
        m->sections[m->count - 1] = s;
5503
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5504
0
        *pm = m;
5505
0
        pm = &m->next;
5506
0
      }
5507
62
    if (s->flags & SEC_THREAD_LOCAL)
5508
8
      {
5509
8
        if (! tls_count)
5510
7
    first_tls = s;
5511
8
        tls_count++;
5512
8
      }
5513
62
    if (first_mbind == NULL
5514
49
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5515
6
      first_mbind = s;
5516
62
  }
5517
5518
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5519
14
      if (tls_count > 0)
5520
7
  {
5521
7
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5522
7
    amt += tls_count * sizeof (asection *);
5523
7
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5524
7
    if (m == NULL)
5525
0
      goto error_return;
5526
7
    m->next = NULL;
5527
7
    m->p_type = PT_TLS;
5528
7
    m->count = tls_count;
5529
    /* Mandated PF_R.  */
5530
7
    m->p_flags = PF_R;
5531
7
    m->p_flags_valid = 1;
5532
7
    s = first_tls;
5533
14
    for (i = 0; i < tls_count; ++i)
5534
8
      {
5535
8
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5536
1
    {
5537
1
      _bfd_error_handler
5538
1
        (_("%pB: TLS sections are not adjacent:"), abfd);
5539
1
      s = first_tls;
5540
1
      i = 0;
5541
4
      while (i < tls_count)
5542
3
        {
5543
3
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5544
2
      {
5545
2
        _bfd_error_handler (_("      TLS: %pA"), s);
5546
2
        i++;
5547
2
      }
5548
1
          else
5549
1
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5550
3
          s = s->next;
5551
3
        }
5552
1
      bfd_set_error (bfd_error_bad_value);
5553
1
      goto error_return;
5554
1
    }
5555
7
        m->sections[i] = s;
5556
7
        s = s->next;
5557
7
      }
5558
5559
6
    *pm = m;
5560
6
    pm = &m->next;
5561
6
  }
5562
5563
13
      if (first_mbind
5564
5
    && (abfd->flags & D_PAGED) != 0
5565
5
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5566
0
  for (s = first_mbind; s != NULL; s = s->next)
5567
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5568
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5569
0
      {
5570
        /* Mandated PF_R.  */
5571
0
        unsigned long p_flags = PF_R;
5572
0
        if ((s->flags & SEC_READONLY) == 0)
5573
0
    p_flags |= PF_W;
5574
0
        if ((s->flags & SEC_CODE) != 0)
5575
0
    p_flags |= PF_X;
5576
5577
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5578
0
        m = bfd_zalloc (abfd, amt);
5579
0
        if (m == NULL)
5580
0
    goto error_return;
5581
0
        m->next = NULL;
5582
0
        m->p_type = (PT_GNU_MBIND_LO
5583
0
         + elf_section_data (s)->this_hdr.sh_info);
5584
0
        m->count = 1;
5585
0
        m->p_flags_valid = 1;
5586
0
        m->sections[0] = s;
5587
0
        m->p_flags = p_flags;
5588
5589
0
        *pm = m;
5590
0
        pm = &m->next;
5591
0
      }
5592
5593
13
      s = bfd_get_section_by_name (abfd,
5594
13
           NOTE_GNU_PROPERTY_SECTION_NAME);
5595
13
      if (s != NULL && s->size != 0)
5596
0
  {
5597
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5598
0
    m = bfd_zalloc (abfd, amt);
5599
0
    if (m == NULL)
5600
0
      goto error_return;
5601
0
    m->next = NULL;
5602
0
    m->p_type = PT_GNU_PROPERTY;
5603
0
    m->count = 1;
5604
0
    m->p_flags_valid = 1;
5605
0
    m->sections[0] = s;
5606
0
    m->p_flags = PF_R;
5607
0
    *pm = m;
5608
0
    pm = &m->next;
5609
0
  }
5610
5611
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5612
   segment.  */
5613
13
      eh_frame_hdr = elf_eh_frame_hdr (info);
5614
13
      if (eh_frame_hdr != NULL
5615
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5616
0
  {
5617
0
    amt = sizeof (struct elf_segment_map);
5618
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5619
0
    if (m == NULL)
5620
0
      goto error_return;
5621
0
    m->next = NULL;
5622
0
    m->p_type = PT_GNU_EH_FRAME;
5623
0
    m->count = 1;
5624
0
    m->sections[0] = eh_frame_hdr->output_section;
5625
5626
0
    *pm = m;
5627
0
    pm = &m->next;
5628
0
  }
5629
5630
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5631
   segment.  */
5632
13
      sframe = elf_sframe (abfd);
5633
13
      if (sframe != NULL
5634
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5635
0
    && sframe->size != 0)
5636
0
  {
5637
0
    amt = sizeof (struct elf_segment_map);
5638
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5639
0
    if (m == NULL)
5640
0
      goto error_return;
5641
0
    m->next = NULL;
5642
0
    m->p_type = PT_GNU_SFRAME;
5643
0
    m->count = 1;
5644
0
    m->sections[0] = sframe->output_section;
5645
5646
0
    *pm = m;
5647
0
    pm = &m->next;
5648
0
  }
5649
5650
13
      if (elf_stack_flags (abfd))
5651
0
  {
5652
0
    amt = sizeof (struct elf_segment_map);
5653
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5654
0
    if (m == NULL)
5655
0
      goto error_return;
5656
0
    m->next = NULL;
5657
0
    m->p_type = PT_GNU_STACK;
5658
0
    m->p_flags = elf_stack_flags (abfd);
5659
0
    m->p_align = bed->stack_align;
5660
0
    m->p_flags_valid = 1;
5661
0
    m->p_align_valid = m->p_align != 0;
5662
0
    if (info->stacksize > 0)
5663
0
      {
5664
0
        m->p_size = info->stacksize;
5665
0
        m->p_size_valid = 1;
5666
0
      }
5667
5668
0
    *pm = m;
5669
0
    pm = &m->next;
5670
0
  }
5671
5672
13
      if (info != NULL && info->relro)
5673
0
  {
5674
0
    for (m = mfirst; m != NULL; m = m->next)
5675
0
      {
5676
0
        if (m->p_type == PT_LOAD
5677
0
      && m->count != 0
5678
0
      && m->sections[0]->vma >= info->relro_start
5679
0
      && m->sections[0]->vma < info->relro_end)
5680
0
    {
5681
0
      i = m->count;
5682
0
      while (--i != (unsigned) -1)
5683
0
        {
5684
0
          if (m->sections[i]->size > 0
5685
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5686
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5687
0
      break;
5688
0
        }
5689
5690
0
      if (i != (unsigned) -1)
5691
0
        break;
5692
0
    }
5693
0
      }
5694
5695
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5696
0
    if (m != NULL)
5697
0
      {
5698
0
        amt = sizeof (struct elf_segment_map);
5699
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5700
0
        if (m == NULL)
5701
0
    goto error_return;
5702
0
        m->next = NULL;
5703
0
        m->p_type = PT_GNU_RELRO;
5704
0
        *pm = m;
5705
0
        pm = &m->next;
5706
0
      }
5707
0
  }
5708
5709
13
      free (sections);
5710
13
      elf_seg_map (abfd) = mfirst;
5711
13
    }
5712
5713
64
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5714
0
    return false;
5715
5716
305
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5717
241
    ++count;
5718
64
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5719
5720
64
  return true;
5721
5722
1
 error_return:
5723
1
  free (sections);
5724
1
  return false;
5725
64
}
5726
5727
/* Sort sections by address.  */
5728
5729
static int
5730
elf_sort_sections (const void *arg1, const void *arg2)
5731
1.07k
{
5732
1.07k
  const asection *sec1 = *(const asection **) arg1;
5733
1.07k
  const asection *sec2 = *(const asection **) arg2;
5734
1.07k
  bfd_size_type size1, size2;
5735
5736
  /* Sort by LMA first, since this is the address used to
5737
     place the section into a segment.  */
5738
1.07k
  if (sec1->lma < sec2->lma)
5739
927
    return -1;
5740
147
  else if (sec1->lma > sec2->lma)
5741
108
    return 1;
5742
5743
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5744
     the same, and this will do nothing.  */
5745
39
  if (sec1->vma < sec2->vma)
5746
0
    return -1;
5747
39
  else if (sec1->vma > sec2->vma)
5748
0
    return 1;
5749
5750
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5751
5752
78
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5753
78
      && (x)->size != 0)
5754
5755
39
  if (TOEND (sec1))
5756
1
    {
5757
1
      if (!TOEND (sec2))
5758
0
  return 1;
5759
1
    }
5760
38
  else if (TOEND (sec2))
5761
0
    return -1;
5762
5763
39
#undef TOEND
5764
5765
  /* Sort by size, to put zero sized sections
5766
     before others at the same address.  */
5767
5768
39
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5769
39
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5770
5771
39
  if (size1 < size2)
5772
16
    return -1;
5773
23
  if (size1 > size2)
5774
7
    return 1;
5775
5776
16
  return sec1->target_index - sec2->target_index;
5777
23
}
5778
5779
/* This qsort comparison functions sorts PT_LOAD segments first and
5780
   by p_paddr, for assign_file_positions_for_load_sections.  */
5781
5782
static int
5783
elf_sort_segments (const void *arg1, const void *arg2)
5784
394
{
5785
394
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5786
394
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5787
5788
394
  if (m1->p_type != m2->p_type)
5789
363
    {
5790
363
      if (m1->p_type == PT_NULL)
5791
0
  return 1;
5792
363
      if (m2->p_type == PT_NULL)
5793
10
  return -1;
5794
353
      return m1->p_type < m2->p_type ? -1 : 1;
5795
363
    }
5796
31
  if (m1->includes_filehdr != m2->includes_filehdr)
5797
19
    return m1->includes_filehdr ? -1 : 1;
5798
12
  if (m1->no_sort_lma != m2->no_sort_lma)
5799
0
    return m1->no_sort_lma ? -1 : 1;
5800
12
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5801
6
    {
5802
6
      bfd_vma lma1, lma2;  /* Octets.  */
5803
6
      lma1 = 0;
5804
6
      if (m1->p_paddr_valid)
5805
0
  lma1 = m1->p_paddr;
5806
6
      else if (m1->count != 0)
5807
6
  {
5808
6
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5809
6
              m1->sections[0]);
5810
6
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5811
6
  }
5812
6
      lma2 = 0;
5813
6
      if (m2->p_paddr_valid)
5814
0
  lma2 = m2->p_paddr;
5815
6
      else if (m2->count != 0)
5816
6
  {
5817
6
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5818
6
              m2->sections[0]);
5819
6
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5820
6
  }
5821
6
      if (lma1 != lma2)
5822
1
  return lma1 < lma2 ? -1 : 1;
5823
6
    }
5824
11
  if (m1->idx != m2->idx)
5825
11
    return m1->idx < m2->idx ? -1 : 1;
5826
0
  return 0;
5827
11
}
5828
5829
/* Ian Lance Taylor writes:
5830
5831
   We shouldn't be using % with a negative signed number.  That's just
5832
   not good.  We have to make sure either that the number is not
5833
   negative, or that the number has an unsigned type.  When the types
5834
   are all the same size they wind up as unsigned.  When file_ptr is a
5835
   larger signed type, the arithmetic winds up as signed long long,
5836
   which is wrong.
5837
5838
   What we're trying to say here is something like ``increase OFF by
5839
   the least amount that will cause it to be equal to the VMA modulo
5840
   the page size.''  */
5841
/* In other words, something like:
5842
5843
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5844
   off_offset = off % bed->maxpagesize;
5845
   if (vma_offset < off_offset)
5846
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5847
   else
5848
     adjustment = vma_offset - off_offset;
5849
5850
   which can be collapsed into the expression below.  */
5851
5852
static file_ptr
5853
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5854
166
{
5855
  /* PR binutils/16199: Handle an alignment of zero.  */
5856
166
  if (maxpagesize == 0)
5857
0
    maxpagesize = 1;
5858
166
  return ((vma - off) % maxpagesize);
5859
166
}
5860
5861
static void
5862
print_segment_map (const struct elf_segment_map *m)
5863
10
{
5864
10
  unsigned int j;
5865
10
  const char *pt = get_segment_type (m->p_type);
5866
10
  char buf[32];
5867
5868
10
  if (pt == NULL)
5869
0
    {
5870
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5871
0
  sprintf (buf, "LOPROC+%7.7x",
5872
0
     (unsigned int) (m->p_type - PT_LOPROC));
5873
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5874
0
  sprintf (buf, "LOOS+%7.7x",
5875
0
     (unsigned int) (m->p_type - PT_LOOS));
5876
0
      else
5877
0
  snprintf (buf, sizeof (buf), "%8.8x",
5878
0
      (unsigned int) m->p_type);
5879
0
      pt = buf;
5880
0
    }
5881
10
  fflush (stdout);
5882
10
  fprintf (stderr, "%s:", pt);
5883
109
  for (j = 0; j < m->count; j++)
5884
99
    fprintf (stderr, " %s", m->sections [j]->name);
5885
10
  putc ('\n',stderr);
5886
10
  fflush (stderr);
5887
10
}
5888
5889
/* Assign file positions to the sections based on the mapping from
5890
   sections to segments.  This function also sets up some fields in
5891
   the file header.  */
5892
5893
static bool
5894
assign_file_positions_for_load_sections (bfd *abfd,
5895
           struct bfd_link_info *link_info)
5896
65
{
5897
65
  elf_backend_data *bed = get_elf_backend_data (abfd);
5898
65
  struct elf_segment_map *m;
5899
65
  struct elf_segment_map *phdr_load_seg;
5900
65
  Elf_Internal_Phdr *phdrs;
5901
65
  Elf_Internal_Phdr *p;
5902
65
  file_ptr off;  /* Octets.  */
5903
65
  bfd_size_type maxpagesize;
5904
65
  unsigned int alloc, actual;
5905
65
  unsigned int i, j;
5906
65
  struct elf_segment_map **sorted_seg_map;
5907
65
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5908
5909
65
  if (link_info == NULL
5910
65
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5911
1
    return false;
5912
5913
64
  alloc = 0;
5914
305
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5915
241
    m->idx = alloc++;
5916
5917
64
  if (alloc)
5918
41
    {
5919
41
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5920
41
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5921
41
    }
5922
23
  else
5923
23
    {
5924
      /* PR binutils/12467.  */
5925
23
      elf_elfheader (abfd)->e_phoff = 0;
5926
23
      elf_elfheader (abfd)->e_phentsize = 0;
5927
23
    }
5928
5929
64
  elf_elfheader (abfd)->e_phnum = alloc;
5930
5931
64
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5932
0
    {
5933
0
      actual = alloc;
5934
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5935
0
    }
5936
64
  else
5937
64
    {
5938
64
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5939
64
      BFD_ASSERT (elf_program_header_size (abfd)
5940
64
      == actual * bed->s->sizeof_phdr);
5941
64
      BFD_ASSERT (actual >= alloc);
5942
64
    }
5943
5944
64
  if (alloc == 0)
5945
23
    {
5946
23
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5947
23
      return true;
5948
23
    }
5949
5950
  /* We're writing the size in elf_program_header_size (abfd),
5951
     see assign_file_positions_except_relocs, so make sure we have
5952
     that amount allocated, with trailing space cleared.
5953
     The variable alloc contains the computed need, while
5954
     elf_program_header_size (abfd) contains the size used for the
5955
     layout.
5956
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5957
     where the layout is forced to according to a larger size in the
5958
     last iterations for the testcase ld-elf/header.  */
5959
41
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5960
41
           + alloc * sizeof (*sorted_seg_map)));
5961
41
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5962
41
  elf_tdata (abfd)->phdr = phdrs;
5963
41
  if (phdrs == NULL)
5964
0
    return false;
5965
5966
282
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5967
241
    {
5968
241
      sorted_seg_map[j] = m;
5969
      /* If elf_segment_map is not from map_sections_to_segments, the
5970
   sections may not be correctly ordered.  NOTE: sorting should
5971
   not be done to the PT_NOTE section of a corefile, which may
5972
   contain several pseudo-sections artificially created by bfd.
5973
   Sorting these pseudo-sections breaks things badly.  */
5974
241
      if (m->count > 1
5975
80
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5976
1
         && m->p_type == PT_NOTE))
5977
80
  {
5978
694
    for (i = 0; i < m->count; i++)
5979
614
      m->sections[i]->target_index = i;
5980
80
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5981
80
     elf_sort_sections);
5982
80
  }
5983
241
    }
5984
41
  if (alloc > 1)
5985
33
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5986
33
     elf_sort_segments);
5987
5988
41
  maxpagesize = 1;
5989
41
  if ((abfd->flags & D_PAGED) != 0)
5990
40
    {
5991
40
      if (link_info != NULL)
5992
0
  maxpagesize = link_info->maxpagesize;
5993
40
      else
5994
40
  maxpagesize = bed->maxpagesize;
5995
40
    }
5996
5997
  /* Sections must map to file offsets past the ELF file header.  */
5998
41
  off = bed->s->sizeof_ehdr;
5999
  /* And if one of the PT_LOAD headers doesn't include the program
6000
     headers then we'll be mapping program headers in the usual
6001
     position after the ELF file header.  */
6002
41
  phdr_load_seg = NULL;
6003
53
  for (j = 0; j < alloc; j++)
6004
49
    {
6005
49
      m = sorted_seg_map[j];
6006
49
      if (m->p_type != PT_LOAD)
6007
6
  break;
6008
43
      if (m->includes_phdrs)
6009
31
  {
6010
31
    phdr_load_seg = m;
6011
31
    break;
6012
31
  }
6013
43
    }
6014
41
  if (phdr_load_seg == NULL)
6015
10
    off += actual * bed->s->sizeof_phdr;
6016
6017
263
  for (j = 0; j < alloc; j++)
6018
225
    {
6019
225
      asection **secpp;
6020
225
      bfd_vma off_adjust;  /* Octets.  */
6021
225
      bool no_contents;
6022
225
      bfd_size_type align_pagesize;
6023
6024
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6025
   number of sections with contents contributing to both p_filesz
6026
   and p_memsz, followed by a number of sections with no contents
6027
   that just contribute to p_memsz.  In this loop, OFF tracks next
6028
   available file offset for PT_LOAD and PT_NOTE segments.  */
6029
225
      m = sorted_seg_map[j];
6030
225
      p = phdrs + m->idx;
6031
225
      p->p_type = m->p_type;
6032
225
      p->p_flags = m->p_flags;
6033
6034
225
      if (m->count == 0)
6035
78
  p->p_vaddr = m->p_vaddr_offset * opb;
6036
147
      else
6037
147
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6038
6039
225
      if (m->p_paddr_valid)
6040
198
  p->p_paddr = m->p_paddr;
6041
27
      else if (m->count == 0)
6042
2
  p->p_paddr = 0;
6043
25
      else
6044
25
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6045
6046
225
      align_pagesize = 0;
6047
225
      if (p->p_type == PT_LOAD
6048
62
    && (abfd->flags & D_PAGED) != 0)
6049
61
  {
6050
    /* p_align in demand paged PT_LOAD segments effectively stores
6051
       the maximum page size.  When copying an executable with
6052
       objcopy, we set m->p_align from the input file.  Use this
6053
       value for maxpagesize rather than bed->maxpagesize, which
6054
       may be different.  Note that we use maxpagesize for PT_TLS
6055
       segment alignment later in this function, so we are relying
6056
       on at least one PT_LOAD segment appearing before a PT_TLS
6057
       segment.  */
6058
61
    if (m->p_align_valid)
6059
23
      maxpagesize = m->p_align;
6060
38
    else if (bed->p_align != 0
6061
38
       && (link_info == NULL
6062
0
           || !link_info->maxpagesize_is_set))
6063
      /* We will lay out this binary using maxpagesize but set
6064
         p->p_align later to the possibly smaller bed->p_align.
6065
         The run-time loader will then be able to load this
6066
         binary when the system page size is maxpagesize, but if
6067
         the system page size is smaller can use p->p_align.
6068
         In either case p->p_align will be increased if
6069
         necessary to match section alignment.  */
6070
38
      align_pagesize = bed->p_align;
6071
6072
61
    p->p_align = maxpagesize;
6073
61
  }
6074
164
      else if (m->p_align_valid)
6075
90
  p->p_align = m->p_align;
6076
74
      else if (m->count == 0)
6077
33
  p->p_align = 1 << bed->s->log_file_align;
6078
6079
225
      if (m == phdr_load_seg)
6080
31
  off += actual * bed->s->sizeof_phdr;
6081
6082
225
      no_contents = false;
6083
225
      off_adjust = 0;
6084
225
      if (p->p_type == PT_LOAD
6085
62
    && m->count > 0)
6086
57
  {
6087
57
    bfd_size_type align;  /* Bytes.  */
6088
57
    unsigned int align_power = 0;
6089
6090
57
    if (m->p_align_valid)
6091
19
      align = p->p_align;
6092
38
    else
6093
38
      {
6094
243
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6095
205
    {
6096
205
      unsigned int secalign;
6097
6098
205
      secalign = bfd_section_alignment (*secpp);
6099
205
      if (secalign > align_power)
6100
49
        align_power = secalign;
6101
205
    }
6102
38
        align = (bfd_size_type) 1 << align_power;
6103
        /* If a section requires alignment higher than the
6104
     minimum p_align value, don't reduce a maxpagesize
6105
     p->p_align set earlier in this function.  */
6106
38
        if (align > bed->p_align)
6107
2
    align_pagesize = 0;
6108
38
        if (align < maxpagesize)
6109
29
    align = maxpagesize;
6110
9
        else
6111
9
    {
6112
      /* If a section requires alignment higher than the
6113
         maximum page size, set p_align to the section
6114
         alignment.  */
6115
9
      if ((abfd->flags & D_PAGED) != 0)
6116
9
        p->p_align = align;
6117
9
    }
6118
38
      }
6119
6120
424
    for (i = 0; i < m->count; i++)
6121
367
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6122
        /* If we aren't making room for this section, then
6123
     it must be SHT_NOBITS regardless of what we've
6124
     set via struct bfd_elf_special_section.  */
6125
18
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6126
6127
    /* Find out whether this segment contains any loadable
6128
       sections.  */
6129
57
    no_contents = true;
6130
62
    for (i = 0; i < m->count; i++)
6131
62
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6132
57
        {
6133
57
    no_contents = false;
6134
57
    break;
6135
57
        }
6136
6137
57
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6138
6139
    /* Broken hardware and/or kernel require that files do not
6140
       map the same page with different permissions on some hppa
6141
       processors.  */
6142
57
    if (j != 0
6143
24
        && (abfd->flags & D_PAGED) != 0
6144
24
        && bed->no_page_alias
6145
0
        && (off & (maxpagesize - 1)) != 0
6146
0
        && ((off & -maxpagesize)
6147
0
      == ((off + off_adjust) & -maxpagesize)))
6148
0
      off_adjust += maxpagesize;
6149
57
    off += off_adjust;
6150
57
    if (no_contents)
6151
0
      {
6152
        /* We shouldn't need to align the segment on disk since
6153
     the segment doesn't need file space, but the gABI
6154
     arguably requires the alignment and glibc ld.so
6155
     checks it.  So to comply with the alignment
6156
     requirement but not waste file space, we adjust
6157
     p_offset for just this segment.  (OFF_ADJUST is
6158
     subtracted from OFF later.)  This may put p_offset
6159
     past the end of file, but that shouldn't matter.  */
6160
0
      }
6161
57
    else
6162
57
      off_adjust = 0;
6163
57
  }
6164
      /* Make sure the .dynamic section is the first section in the
6165
   PT_DYNAMIC segment.  */
6166
168
      else if (p->p_type == PT_DYNAMIC
6167
27
         && m->count > 1
6168
1
         && ! streq (m->sections[0]->name, ".dynamic"))
6169
1
  {
6170
1
    _bfd_error_handler
6171
1
      (_("%pB: The first section in the PT_DYNAMIC segment"
6172
1
         " is not the .dynamic section"),
6173
1
       abfd);
6174
1
    bfd_set_error (bfd_error_bad_value);
6175
1
    return false;
6176
1
  }
6177
      /* Set the note section type to SHT_NOTE.  */
6178
167
      else if (p->p_type == PT_NOTE)
6179
56
  for (i = 0; i < m->count; i++)
6180
31
    elf_section_type (m->sections[i]) = SHT_NOTE;
6181
6182
224
      if (m->includes_filehdr)
6183
39
  {
6184
39
    if (!m->p_flags_valid)
6185
4
      p->p_flags |= PF_R;
6186
39
    p->p_filesz = bed->s->sizeof_ehdr;
6187
39
    p->p_memsz = bed->s->sizeof_ehdr;
6188
39
    if (p->p_type == PT_LOAD)
6189
30
      {
6190
30
        if (m->count > 0)
6191
25
    {
6192
25
      if (p->p_vaddr < (bfd_vma) off
6193
24
          || (!m->p_paddr_valid
6194
4
        && p->p_paddr < (bfd_vma) off))
6195
1
        {
6196
1
          _bfd_error_handler
6197
1
      (_("%pB: not enough room for program headers,"
6198
1
         " try linking with -N"),
6199
1
       abfd);
6200
1
          bfd_set_error (bfd_error_bad_value);
6201
1
          return false;
6202
1
        }
6203
24
      p->p_vaddr -= off;
6204
24
      if (!m->p_paddr_valid)
6205
4
        p->p_paddr -= off;
6206
24
    }
6207
30
      }
6208
9
    else if (sorted_seg_map[0]->includes_filehdr)
6209
9
      {
6210
9
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6211
9
        p->p_vaddr = filehdr->p_vaddr;
6212
9
        if (!m->p_paddr_valid)
6213
1
    p->p_paddr = filehdr->p_paddr;
6214
9
      }
6215
39
  }
6216
6217
223
      if (m->includes_phdrs)
6218
53
  {
6219
53
    if (!m->p_flags_valid)
6220
4
      p->p_flags |= PF_R;
6221
53
    p->p_filesz += actual * bed->s->sizeof_phdr;
6222
53
    p->p_memsz += actual * bed->s->sizeof_phdr;
6223
53
    if (!m->includes_filehdr)
6224
15
      {
6225
15
        if (p->p_type == PT_LOAD)
6226
1
    {
6227
1
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6228
1
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6229
1
      if (m->count > 0)
6230
1
        {
6231
1
          p->p_vaddr -= off - p->p_offset;
6232
1
          if (!m->p_paddr_valid)
6233
0
      p->p_paddr -= off - p->p_offset;
6234
1
        }
6235
1
    }
6236
14
        else if (phdr_load_seg != NULL)
6237
14
    {
6238
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6239
         sorted segments so that phdr_load_seg will
6240
         already be set by the code immediately above.  */
6241
14
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6242
14
      bfd_vma phdr_off = 0;  /* Octets.  */
6243
14
      if (phdr_load_seg->includes_filehdr)
6244
13
        phdr_off = bed->s->sizeof_ehdr;
6245
14
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6246
14
      if (!m->p_paddr_valid)
6247
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6248
14
      p->p_offset = phdr->p_offset + phdr_off;
6249
14
    }
6250
0
        else
6251
0
    p->p_offset = bed->s->sizeof_ehdr;
6252
15
      }
6253
53
  }
6254
6255
223
      if (p->p_type == PT_LOAD
6256
162
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6257
61
  {
6258
61
    if (!m->includes_filehdr && !m->includes_phdrs)
6259
31
      {
6260
31
        p->p_offset = off;
6261
31
        if (no_contents)
6262
0
    {
6263
      /* Put meaningless p_offset for PT_LOAD segments
6264
         without file contents somewhere within the first
6265
         page, in an attempt to not point past EOF.  */
6266
0
      bfd_size_type align = maxpagesize;
6267
0
      if (align < p->p_align)
6268
0
        align = p->p_align;
6269
0
      if (align < 1)
6270
0
        align = 1;
6271
      /* Avoid p_offset of zero, which might be wrongly
6272
         interpreted as the segment being the first one,
6273
         containing the file header.  PR32763.  */
6274
0
      p->p_offset = (off + align - 1) % align + 1;
6275
0
    }
6276
31
      }
6277
30
    else
6278
30
      {
6279
30
        file_ptr adjust;  /* Octets.  */
6280
6281
30
        adjust = off - (p->p_offset + p->p_filesz);
6282
30
        if (!no_contents)
6283
30
    p->p_filesz += adjust;
6284
30
        p->p_memsz += adjust;
6285
30
      }
6286
61
  }
6287
6288
223
      if (align_pagesize)
6289
36
  p->p_align = align_pagesize;
6290
6291
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6292
   maps.  Set filepos for sections in PT_LOAD segments, and in
6293
   core files, for sections in PT_NOTE segments.
6294
   assign_file_positions_for_non_load_sections will set filepos
6295
   for other sections and update p_filesz for other segments.  */
6296
872
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6297
649
  {
6298
649
    asection *sec;
6299
649
    bfd_size_type align;
6300
649
    Elf_Internal_Shdr *this_hdr;
6301
6302
649
    sec = *secpp;
6303
649
    this_hdr = &elf_section_data (sec)->this_hdr;
6304
649
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6305
6306
649
    if ((p->p_type == PT_LOAD
6307
286
         || p->p_type == PT_DYNAMIC
6308
271
         || p->p_type == PT_TLS)
6309
382
        && (this_hdr->sh_type != SHT_NOBITS
6310
18
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6311
18
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6312
2
        || p->p_type == PT_TLS))))
6313
380
      {
6314
380
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6315
380
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6316
380
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6317
380
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6318
6319
380
        if (adjust != 0
6320
196
      && (s_start < p_end
6321
117
          || p_end < p_start))
6322
79
    {
6323
79
      _bfd_error_handler
6324
        /* xgettext:c-format */
6325
79
        (_("%pB: section %pA lma %#" PRIx64
6326
79
           " adjusted to %#" PRIx64),
6327
79
         abfd, sec, (uint64_t) s_start / opb,
6328
79
         (uint64_t) p_end / opb);
6329
79
      adjust = 0;
6330
79
      sec->lma = p_end / opb;
6331
79
    }
6332
380
        p->p_memsz += adjust;
6333
6334
380
        if (p->p_type == PT_LOAD)
6335
361
    {
6336
361
      if (this_hdr->sh_type != SHT_NOBITS)
6337
345
        {
6338
345
          off_adjust = 0;
6339
345
          if (p->p_filesz + adjust < p->p_memsz)
6340
1
      {
6341
        /* We have a PROGBITS section following NOBITS ones.
6342
           Allocate file space for the NOBITS section(s).
6343
           We don't need to write out the zeros, posix
6344
           fseek past the end of data already written
6345
           followed by a write at that location is
6346
           guaranteed to result in zeros being read
6347
           from the gap.  */
6348
1
        adjust = p->p_memsz - p->p_filesz;
6349
1
      }
6350
345
        }
6351
      /* We only adjust sh_offset in SHT_NOBITS sections
6352
         as would seem proper for their address when the
6353
         section is first in the segment.  sh_offset
6354
         doesn't really have any significance for
6355
         SHT_NOBITS anyway, apart from a notional position
6356
         relative to other sections.  Historically we
6357
         didn't bother with adjusting sh_offset and some
6358
         programs depend on it not being adjusted.  See
6359
         pr12921 and pr25662.  */
6360
361
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6361
348
        {
6362
348
          off += adjust;
6363
348
          if (this_hdr->sh_type == SHT_NOBITS)
6364
3
      off_adjust += adjust;
6365
348
        }
6366
361
    }
6367
380
        if (this_hdr->sh_type != SHT_NOBITS)
6368
364
    p->p_filesz += adjust;
6369
380
      }
6370
6371
649
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6372
0
      {
6373
        /* The section at i == 0 is the one that actually contains
6374
     everything.  */
6375
0
        if (i == 0)
6376
0
    {
6377
0
      this_hdr->sh_offset = sec->filepos = off;
6378
0
      off += this_hdr->sh_size;
6379
0
      p->p_filesz = this_hdr->sh_size;
6380
0
      p->p_memsz = 0;
6381
0
      p->p_align = 1;
6382
0
    }
6383
0
        else
6384
0
    {
6385
      /* The rest are fake sections that shouldn't be written.  */
6386
0
      sec->filepos = 0;
6387
0
      sec->size = 0;
6388
0
      sec->flags = 0;
6389
0
      continue;
6390
0
    }
6391
0
      }
6392
649
    else
6393
649
      {
6394
649
        if (this_hdr->sh_type == SHT_NOBITS
6395
27
      && (this_hdr->sh_flags & SHF_TLS) != 0
6396
2
      && this_hdr->sh_offset == 0)
6397
2
    {
6398
      /* Set sh_offset for .tbss sections to their nominal
6399
         offset after aligning.  They are not loaded from
6400
         disk so the value doesn't really matter, except
6401
         when the .tbss section is the first one in a
6402
         PT_TLS segment.  In that case it sets the
6403
         p_offset for the PT_TLS segment, which according
6404
         to the ELF gABI ought to satisfy
6405
         p_offset % p_align == p_vaddr % p_align.  */
6406
2
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6407
2
                off, align);
6408
2
      this_hdr->sh_offset = sec->filepos = off + adjust;
6409
2
    }
6410
647
        else if (p->p_type == PT_LOAD)
6411
361
    {
6412
361
      this_hdr->sh_offset = sec->filepos = off;
6413
361
      if (this_hdr->sh_type != SHT_NOBITS)
6414
345
        off += this_hdr->sh_size;
6415
361
    }
6416
6417
649
        if (this_hdr->sh_type != SHT_NOBITS)
6418
622
    {
6419
622
      p->p_filesz += this_hdr->sh_size;
6420
      /* A load section without SHF_ALLOC is something like
6421
         a note section in a PT_NOTE segment.  These take
6422
         file space but are not loaded into memory.  */
6423
622
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6424
593
        p->p_memsz += this_hdr->sh_size;
6425
622
    }
6426
27
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6427
27
    {
6428
27
      if (p->p_type == PT_TLS)
6429
0
        p->p_memsz += this_hdr->sh_size;
6430
6431
      /* .tbss is special.  It doesn't contribute to p_memsz of
6432
         normal segments.  */
6433
27
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6434
25
        p->p_memsz += this_hdr->sh_size;
6435
27
    }
6436
6437
649
        if (align > p->p_align
6438
114
      && !m->p_align_valid
6439
50
      && (p->p_type != PT_LOAD
6440
0
          || (abfd->flags & D_PAGED) == 0))
6441
50
    p->p_align = align;
6442
649
      }
6443
6444
649
    if (!m->p_flags_valid)
6445
28
      {
6446
28
        p->p_flags |= PF_R;
6447
28
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6448
6
    p->p_flags |= PF_X;
6449
28
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6450
2
    p->p_flags |= PF_W;
6451
28
      }
6452
649
  }
6453
6454
223
      off -= off_adjust;
6455
6456
      /* PR ld/20815 - Check that the program header segment, if
6457
   present, will be loaded into memory.  */
6458
223
      if (p->p_type == PT_PHDR
6459
24
    && phdr_load_seg == NULL
6460
1
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6461
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6462
1
  {
6463
    /* The fix for this error is usually to edit the linker script being
6464
       used and set up the program headers manually.  Either that or
6465
       leave room for the headers at the start of the SECTIONS.  */
6466
1
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6467
1
        " by LOAD segment"),
6468
1
            abfd);
6469
1
    if (link_info == NULL)
6470
1
      return false;
6471
    /* Arrange for the linker to exit with an error, deleting
6472
       the output file unless --noinhibit-exec is given.  */
6473
0
    link_info->callbacks->info ("%X");
6474
0
  }
6475
6476
      /* Check that all sections are in a PT_LOAD segment.
6477
   Don't check funky gdb generated core files.  */
6478
222
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6479
60
  {
6480
60
    bool check_vma = true;
6481
6482
367
    for (i = 1; i < m->count; i++)
6483
307
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6484
4
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6485
4
               ->this_hdr), p) != 0
6486
3
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6487
3
               ->this_hdr), p) != 0)
6488
0
        {
6489
    /* Looks like we have overlays packed into the segment.  */
6490
0
    check_vma = false;
6491
0
    break;
6492
0
        }
6493
6494
423
    for (i = 0; i < m->count; i++)
6495
363
      {
6496
363
        Elf_Internal_Shdr *this_hdr;
6497
363
        asection *sec;
6498
6499
363
        sec = m->sections[i];
6500
363
        this_hdr = &(elf_section_data(sec)->this_hdr);
6501
363
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6502
10
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6503
10
    {
6504
10
      _bfd_error_handler
6505
        /* xgettext:c-format */
6506
10
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6507
10
         abfd, sec, m->idx);
6508
10
      print_segment_map (m);
6509
10
    }
6510
363
      }
6511
60
  }
6512
222
    }
6513
6514
38
  elf_next_file_pos (abfd) = off;
6515
6516
38
  if (link_info != NULL
6517
0
      && phdr_load_seg != NULL
6518
0
      && phdr_load_seg->includes_filehdr)
6519
0
    {
6520
      /* There is a segment that contains both the file headers and the
6521
   program headers, so provide a symbol __ehdr_start pointing there.
6522
   A program can use this to examine itself robustly.  */
6523
6524
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6525
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6526
6527
      /* If the symbol was referenced and not defined, define it.  */
6528
0
      if (hash != NULL
6529
0
    && (hash->root.type == bfd_link_hash_new
6530
0
        || hash->root.type == bfd_link_hash_undefined
6531
0
        || hash->root.type == bfd_link_hash_undefweak
6532
0
        || hash->root.type == bfd_link_hash_common))
6533
0
  {
6534
0
    asection *s = NULL;
6535
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6536
6537
0
    if (phdr_load_seg->count != 0)
6538
      /* The segment contains sections, so use the first one.  */
6539
0
      s = phdr_load_seg->sections[0];
6540
0
    else
6541
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6542
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6543
0
        if (m->p_type == PT_LOAD && m->count != 0)
6544
0
    {
6545
0
      s = m->sections[0];
6546
0
      break;
6547
0
    }
6548
6549
0
    if (s != NULL)
6550
0
      {
6551
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6552
0
        hash->root.u.def.section = s;
6553
0
      }
6554
0
    else
6555
0
      {
6556
0
        hash->root.u.def.value = filehdr_vaddr;
6557
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6558
0
      }
6559
6560
0
    hash->root.type = bfd_link_hash_defined;
6561
0
    hash->def_regular = 1;
6562
0
    hash->non_elf = 0;
6563
0
  }
6564
0
    }
6565
6566
38
  return true;
6567
41
}
6568
6569
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6570
   is no defined method for detecting such files, so we have to
6571
   use heuristics instead.  */
6572
6573
bool
6574
is_debuginfo_file (bfd *abfd)
6575
105
{
6576
105
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6577
0
    return false;
6578
6579
105
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6580
105
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6581
105
  Elf_Internal_Shdr **headerp;
6582
6583
213
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6584
213
    {
6585
213
      Elf_Internal_Shdr *header = * headerp;
6586
6587
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6588
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6589
213
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6590
105
    && header->sh_type != SHT_NOBITS
6591
105
    && header->sh_type != SHT_NOTE)
6592
105
  return false;
6593
213
    }
6594
6595
0
  return true;
6596
105
}
6597
6598
/* Assign file positions for other sections, except for compressed debug
6599
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6600
6601
static bool
6602
assign_file_positions_for_non_load_sections (bfd *abfd,
6603
               struct bfd_link_info *link_info)
6604
61
{
6605
61
  elf_backend_data *bed = get_elf_backend_data (abfd);
6606
61
  Elf_Internal_Shdr **i_shdrpp;
6607
61
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6608
61
  Elf_Internal_Phdr *phdrs;
6609
61
  Elf_Internal_Phdr *p;
6610
61
  struct elf_segment_map *m;
6611
61
  file_ptr off;
6612
61
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6613
61
  bfd_vma maxpagesize;
6614
6615
61
  if (link_info != NULL)
6616
0
    maxpagesize = link_info->maxpagesize;
6617
61
  else
6618
61
    maxpagesize = bed->maxpagesize;
6619
61
  i_shdrpp = elf_elfsections (abfd);
6620
61
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6621
61
  off = elf_next_file_pos (abfd);
6622
738
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6623
677
    {
6624
677
      Elf_Internal_Shdr *hdr;
6625
677
      bfd_vma align;
6626
6627
677
      hdr = *hdrpp;
6628
677
      if (hdr->bfd_section != NULL
6629
580
    && (hdr->bfd_section->filepos != 0
6630
235
        || (hdr->sh_type == SHT_NOBITS
6631
7
      && hdr->contents == NULL)))
6632
352
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6633
325
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6634
107
  {
6635
107
    if (hdr->sh_size != 0
6636
        /* PR 24717 - debuginfo files are known to be not strictly
6637
     compliant with the ELF standard.  In particular they often
6638
     have .note.gnu.property sections that are outside of any
6639
     loadable segment.  This is not a problem for such files,
6640
     so do not warn about them.  */
6641
105
        && ! is_debuginfo_file (abfd))
6642
105
      _bfd_error_handler
6643
        /* xgettext:c-format */
6644
105
        (_("%pB: warning: allocated section `%s' not in segment"),
6645
105
         abfd,
6646
105
         (hdr->bfd_section == NULL
6647
105
    ? "*unknown*"
6648
105
    : hdr->bfd_section->name));
6649
    /* We don't need to page align empty sections.  */
6650
107
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6651
104
      align = maxpagesize;
6652
3
    else
6653
3
      align = hdr->sh_addralign & -hdr->sh_addralign;
6654
107
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6655
107
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6656
107
                 bed->s->log_file_align);
6657
107
  }
6658
218
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6659
6
    && hdr->bfd_section == NULL)
6660
         /* We don't know the offset of these sections yet:
6661
      their size has not been decided.  */
6662
214
         || (abfd->is_linker_output
6663
0
       && hdr->bfd_section != NULL
6664
0
       && (hdr->sh_name == -1u
6665
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6666
214
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6667
198
         || (elf_symtab_shndx_list (abfd) != NULL
6668
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6669
198
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6670
182
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6671
97
  hdr->sh_offset = -1;
6672
121
      else
6673
121
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6674
677
    }
6675
61
  elf_next_file_pos (abfd) = off;
6676
6677
  /* Now that we have set the section file positions, we can set up
6678
     the file positions for the non PT_LOAD segments.  */
6679
61
  phdrs = elf_tdata (abfd)->phdr;
6680
257
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6681
200
    {
6682
200
      if (p->p_type == PT_GNU_RELRO)
6683
10
  {
6684
10
    bfd_vma start, end;  /* Bytes.  */
6685
10
    bool ok;
6686
6687
10
    if (link_info != NULL)
6688
0
      {
6689
        /* During linking the range of the RELRO segment is passed
6690
     in link_info.  Note that there may be padding between
6691
     relro_start and the first RELRO section.  */
6692
0
        start = link_info->relro_start;
6693
0
        end = link_info->relro_end;
6694
0
      }
6695
10
    else if (m->count != 0)
6696
10
      {
6697
10
        if (!m->p_size_valid)
6698
0
    abort ();
6699
10
        start = m->sections[0]->vma;
6700
10
        end = start + m->p_size / opb;
6701
10
      }
6702
0
    else
6703
0
      {
6704
0
        start = 0;
6705
0
        end = 0;
6706
0
      }
6707
6708
10
    ok = false;
6709
10
    if (start < end)
6710
10
      {
6711
10
        struct elf_segment_map *lm;
6712
10
        const Elf_Internal_Phdr *lp;
6713
10
        unsigned int i;
6714
6715
        /* Find a LOAD segment containing a section in the RELRO
6716
     segment.  */
6717
10
        for (lm = elf_seg_map (abfd), lp = phdrs;
6718
54
       lm != NULL;
6719
44
       lm = lm->next, lp++)
6720
51
    {
6721
51
      if (lp->p_type == PT_LOAD
6722
17
          && lm->count != 0
6723
13
          && (lm->sections[lm->count - 1]->vma
6724
13
        + (!IS_TBSS (lm->sections[lm->count - 1])
6725
13
           ? lm->sections[lm->count - 1]->size / opb
6726
13
           : 0)) > start
6727
7
          && lm->sections[0]->vma < end)
6728
7
        break;
6729
51
    }
6730
6731
10
        if (lm != NULL)
6732
7
    {
6733
      /* Find the section starting the RELRO segment.  */
6734
7
      for (i = 0; i < lm->count; i++)
6735
7
        {
6736
7
          asection *s = lm->sections[i];
6737
7
          if (s->vma >= start
6738
7
        && s->vma < end
6739
7
        && s->size != 0)
6740
7
      break;
6741
7
        }
6742
6743
7
      if (i < lm->count)
6744
7
        {
6745
7
          p->p_vaddr = lm->sections[i]->vma * opb;
6746
7
          p->p_paddr = lm->sections[i]->lma * opb;
6747
7
          p->p_offset = lm->sections[i]->filepos;
6748
7
          p->p_memsz = end * opb - p->p_vaddr;
6749
7
          p->p_filesz = p->p_memsz;
6750
6751
          /* The RELRO segment typically ends a few bytes
6752
       into .got.plt but other layouts are possible.
6753
       In cases where the end does not match any
6754
       loaded section (for instance is in file
6755
       padding), trim p_filesz back to correspond to
6756
       the end of loaded section contents.  */
6757
7
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6758
2
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6759
6760
          /* Preserve the alignment and flags if they are
6761
       valid.  The gold linker generates RW/4 for
6762
       the PT_GNU_RELRO section.  It is better for
6763
       objcopy/strip to honor these attributes
6764
       otherwise gdb will choke when using separate
6765
       debug files.  */
6766
7
          if (!m->p_align_valid)
6767
0
      p->p_align = 1;
6768
7
          if (!m->p_flags_valid)
6769
0
      p->p_flags = PF_R;
6770
7
          ok = true;
6771
7
        }
6772
7
    }
6773
10
      }
6774
6775
10
    if (!ok)
6776
3
      {
6777
3
        if (link_info != NULL)
6778
0
    _bfd_error_handler
6779
0
      (_("%pB: warning: unable to allocate any sections"
6780
0
         " to PT_GNU_RELRO segment"),
6781
0
       abfd);
6782
3
        memset (p, 0, sizeof *p);
6783
3
      }
6784
10
  }
6785
190
      else if (p->p_type == PT_GNU_STACK)
6786
9
  {
6787
9
    if (m->p_size_valid)
6788
3
      p->p_memsz = m->p_size;
6789
9
  }
6790
181
      else if (m->count != 0)
6791
118
  {
6792
118
    unsigned int i;
6793
6794
118
    if (p->p_type != PT_LOAD
6795
69
        && (p->p_type != PT_NOTE
6796
15
      || bfd_get_format (abfd) != bfd_core))
6797
69
      {
6798
        /* A user specified segment layout may include a PHDR
6799
     segment that overlaps with a LOAD segment...  */
6800
69
        if (p->p_type == PT_PHDR)
6801
2
    {
6802
2
      m->count = 0;
6803
2
      continue;
6804
2
    }
6805
6806
67
        if (m->includes_filehdr || m->includes_phdrs)
6807
4
    {
6808
      /* PR 17512: file: 2195325e.  */
6809
4
      _bfd_error_handler
6810
4
        (_("%pB: error: non-load segment %d includes file header "
6811
4
           "and/or program header"),
6812
4
         abfd, (int) (p - phdrs));
6813
4
      return false;
6814
4
    }
6815
6816
63
        p->p_filesz = 0;
6817
63
        p->p_offset = m->sections[0]->filepos;
6818
63
        for (i = m->count; i-- != 0;)
6819
63
    {
6820
63
      asection *sect = m->sections[i];
6821
63
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6822
63
      if (hdr->sh_type != SHT_NOBITS)
6823
63
        {
6824
63
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6825
          /* NB: p_memsz of the loadable PT_NOTE segment
6826
       should be the same as p_filesz.  */
6827
63
          if (p->p_type == PT_NOTE
6828
15
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6829
15
      p->p_memsz = p->p_filesz;
6830
63
          break;
6831
63
        }
6832
63
    }
6833
63
      }
6834
118
  }
6835
200
    }
6836
6837
57
  return true;
6838
61
}
6839
6840
static elf_section_list *
6841
find_section_in_list (unsigned int i, elf_section_list * list)
6842
463
{
6843
465
  for (;list != NULL; list = list->next)
6844
3
    if (list->ndx == i)
6845
1
      break;
6846
463
  return list;
6847
463
}
6848
6849
/* Work out the file positions of all the sections.  This is called by
6850
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6851
   VMAs must be known before this is called.
6852
6853
   Reloc sections come in two flavours: Those processed specially as
6854
   "side-channel" data attached to a section to which they apply, and
6855
   those that bfd doesn't process as relocations.  The latter sort are
6856
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6857
   consider the former sort here, unless they form part of the loadable
6858
   image.  Reloc sections not assigned here (and compressed debugging
6859
   sections and CTF sections which nothing else in the file can rely
6860
   upon) will be handled later by assign_file_positions_for_relocs.
6861
6862
   We also don't set the positions of the .symtab and .strtab here.  */
6863
6864
static bool
6865
assign_file_positions_except_relocs (bfd *abfd,
6866
             struct bfd_link_info *link_info)
6867
139
{
6868
139
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6869
139
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6870
139
  elf_backend_data *bed = get_elf_backend_data (abfd);
6871
139
  unsigned int alloc;
6872
6873
139
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6874
75
      && bfd_get_format (abfd) != bfd_core)
6875
74
    {
6876
74
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6877
74
      unsigned int num_sec = elf_numsections (abfd);
6878
74
      Elf_Internal_Shdr **hdrpp;
6879
74
      unsigned int i;
6880
74
      file_ptr off;
6881
6882
      /* Start after the ELF header.  */
6883
74
      off = i_ehdrp->e_ehsize;
6884
6885
      /* We are not creating an executable, which means that we are
6886
   not creating a program header, and that the actual order of
6887
   the sections in the file is unimportant.  */
6888
833
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6889
759
  {
6890
759
    Elf_Internal_Shdr *hdr;
6891
6892
759
    hdr = *hdrpp;
6893
759
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6894
197
         && hdr->bfd_section == NULL)
6895
        /* Do not assign offsets for these sections yet: we don't know
6896
     their sizes.  */
6897
600
        || (abfd->is_linker_output
6898
0
      && hdr->bfd_section != NULL
6899
0
      && (hdr->sh_name == -1u
6900
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6901
600
        || i == elf_onesymtab (abfd)
6902
576
        || (elf_symtab_shndx_list (abfd) != NULL
6903
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6904
576
        || i == elf_strtab_sec (abfd)
6905
552
        || i == elf_shstrtab_sec (abfd))
6906
281
      {
6907
281
        hdr->sh_offset = -1;
6908
281
      }
6909
478
    else
6910
      /* There shouldn't be a need to effect "capped" file alignment here,
6911
         yet at least the Linux kernel's modpost utility was found to be
6912
         unhappy without.  While the issue was addressed there, let's be
6913
         kind for at least the foreseeable future ...  */
6914
478
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6915
478
                   bed->s->log_file_align);
6916
759
  }
6917
6918
74
      elf_next_file_pos (abfd) = off;
6919
74
      elf_program_header_size (abfd) = 0;
6920
74
    }
6921
65
  else
6922
65
    {
6923
      /* Assign file positions for the loaded sections based on the
6924
   assignment of sections to segments.  */
6925
65
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6926
4
  return false;
6927
6928
      /* And for non-load sections.  */
6929
61
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6930
4
  return false;
6931
61
    }
6932
6933
131
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6934
0
    return false;
6935
6936
  /* Write out the program headers.  */
6937
131
  alloc = i_ehdrp->e_phnum;
6938
131
  if (alloc != 0)
6939
34
    {
6940
34
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6941
0
  {
6942
0
    bool warned_tls = false;
6943
0
    bool warned_rwx = false;
6944
6945
    /* Memory resident segments with non-zero size and RWX
6946
       permissions are a security risk, so we generate a warning
6947
       here if we are creating any.  */
6948
0
    unsigned int i;
6949
6950
0
    for (i = 0; i < alloc; i++)
6951
0
      {
6952
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6953
6954
0
        if (phdr->p_memsz == 0)
6955
0
    continue;
6956
6957
0
        if (! warned_tls
6958
0
      && phdr->p_type == PT_TLS
6959
0
      && (phdr->p_flags & PF_X))
6960
0
    {
6961
0
      if (link_info->warn_is_error_for_rwx_segments)
6962
0
        {
6963
0
          _bfd_error_handler (_("\
6964
0
error: %pB has a TLS segment with execute permission"),
6965
0
            abfd);
6966
0
          return false;
6967
0
        }
6968
6969
0
      _bfd_error_handler (_("\
6970
0
warning: %pB has a TLS segment with execute permission"),
6971
0
              abfd);
6972
0
      if (warned_rwx)
6973
0
        break;
6974
6975
0
      warned_tls = true;
6976
0
    }
6977
0
        else if (! warned_rwx
6978
0
           && phdr->p_type == PT_LOAD
6979
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6980
0
         == (PF_R | PF_W | PF_X)))
6981
0
    {
6982
0
      if (link_info->warn_is_error_for_rwx_segments)
6983
0
        {
6984
0
          _bfd_error_handler (_("\
6985
0
error: %pB has a LOAD segment with RWX permissions"),
6986
0
            abfd);
6987
0
          return false;
6988
0
        }
6989
6990
0
      _bfd_error_handler (_("\
6991
0
warning: %pB has a LOAD segment with RWX permissions"),
6992
0
              abfd);
6993
0
      if (warned_tls)
6994
0
        break;
6995
6996
0
      warned_rwx = true;
6997
0
    }
6998
0
      }
6999
0
  }
7000
7001
34
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7002
34
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7003
0
  return false;
7004
34
    }
7005
7006
131
  return true;
7007
131
}
7008
7009
bool
7010
_bfd_elf_init_file_header (bfd *abfd,
7011
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7012
139
{
7013
139
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7014
139
  struct elf_strtab_hash *shstrtab;
7015
139
  elf_backend_data *bed = get_elf_backend_data (abfd);
7016
7017
139
  i_ehdrp = elf_elfheader (abfd);
7018
7019
139
  shstrtab = _bfd_elf_strtab_init ();
7020
139
  if (shstrtab == NULL)
7021
0
    return false;
7022
7023
139
  elf_shstrtab (abfd) = shstrtab;
7024
7025
139
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7026
139
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7027
139
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7028
139
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7029
7030
139
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7031
139
  i_ehdrp->e_ident[EI_DATA] =
7032
139
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7033
139
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7034
7035
139
  if ((abfd->flags & DYNAMIC) != 0)
7036
55
    i_ehdrp->e_type = ET_DYN;
7037
84
  else if ((abfd->flags & EXEC_P) != 0)
7038
9
    i_ehdrp->e_type = ET_EXEC;
7039
75
  else if (bfd_get_format (abfd) == bfd_core)
7040
1
    i_ehdrp->e_type = ET_CORE;
7041
74
  else
7042
74
    i_ehdrp->e_type = ET_REL;
7043
7044
139
  switch (bfd_get_arch (abfd))
7045
139
    {
7046
0
    case bfd_arch_unknown:
7047
0
      i_ehdrp->e_machine = EM_NONE;
7048
0
      break;
7049
7050
      /* There used to be a long list of cases here, each one setting
7051
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7052
   in the corresponding bfd definition.  To avoid duplication,
7053
   the switch was removed.  Machines that need special handling
7054
   can generally do it in elf_backend_final_write_processing(),
7055
   unless they need the information earlier than the final write.
7056
   Such need can generally be supplied by replacing the tests for
7057
   e_machine with the conditions used to determine it.  */
7058
139
    default:
7059
139
      i_ehdrp->e_machine = bed->elf_machine_code;
7060
139
    }
7061
7062
139
  i_ehdrp->e_version = bed->s->ev_current;
7063
139
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7064
7065
  /* No program header, for now.  */
7066
139
  i_ehdrp->e_phoff = 0;
7067
139
  i_ehdrp->e_phentsize = 0;
7068
139
  i_ehdrp->e_phnum = 0;
7069
7070
  /* Each bfd section is section header entry.  */
7071
139
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7072
139
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7073
7074
139
  elf_tdata (abfd)->symtab_hdr.sh_name =
7075
139
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7076
139
  elf_tdata (abfd)->strtab_hdr.sh_name =
7077
139
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7078
139
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7079
139
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7080
139
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7081
139
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7082
139
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7083
0
    return false;
7084
7085
139
  return true;
7086
139
}
7087
7088
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7089
7090
   FIXME: We used to have code here to sort the PT_LOAD segments into
7091
   ascending order, as per the ELF spec.  But this breaks some programs,
7092
   including the Linux kernel.  But really either the spec should be
7093
   changed or the programs updated.  */
7094
7095
bool
7096
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7097
131
{
7098
131
  if (link_info != NULL && bfd_link_pie (link_info))
7099
0
    {
7100
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7101
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7102
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7103
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7104
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7105
7106
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7107
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7108
0
      for (; segment < end_segment; segment++)
7109
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7110
0
    p_vaddr = segment->p_vaddr;
7111
7112
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7113
   segments is non-zero.  */
7114
0
      if (p_vaddr)
7115
0
  i_ehdrp->e_type = ET_EXEC;
7116
0
    }
7117
131
  return true;
7118
131
}
7119
7120
/* Assign file positions for all the reloc sections which are not part
7121
   of the loadable file image, and the file position of section headers.  */
7122
7123
static bool
7124
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7125
131
{
7126
131
  file_ptr off;
7127
131
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7128
131
  Elf_Internal_Shdr *shdrp;
7129
131
  Elf_Internal_Ehdr *i_ehdrp;
7130
131
  elf_backend_data *bed = get_elf_backend_data (abfd);
7131
7132
  /* Skip non-load sections without section header.  */
7133
131
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7134
0
    return true;
7135
7136
131
  off = elf_next_file_pos (abfd);
7137
7138
131
  shdrpp = elf_elfsections (abfd);
7139
131
  end_shdrpp = shdrpp + elf_numsections (abfd);
7140
1.45k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7141
1.32k
    {
7142
1.32k
      shdrp = *shdrpp;
7143
1.32k
      if (shdrp->sh_offset == -1)
7144
294
  {
7145
294
    asection *sec = shdrp->bfd_section;
7146
294
    if (sec == NULL
7147
0
        || shdrp->sh_type == SHT_REL
7148
0
        || shdrp->sh_type == SHT_RELA)
7149
294
      ;
7150
0
    else if (bfd_section_is_ctf (sec))
7151
0
      {
7152
        /* Update section size and contents.  */
7153
0
        shdrp->sh_size = sec->size;
7154
0
        shdrp->contents = sec->contents;
7155
0
      }
7156
0
    else if (shdrp->sh_name == -1u)
7157
0
      {
7158
0
        const char *name = sec->name;
7159
0
        struct bfd_elf_section_data *d;
7160
7161
        /* Compress DWARF debug sections.  */
7162
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7163
0
    return false;
7164
7165
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7166
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7167
0
      && name[1] == 'd')
7168
0
    {
7169
      /* If section is compressed with zlib-gnu, convert
7170
         section name from .debug_* to .zdebug_*.  */
7171
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7172
0
      if (new_name == NULL)
7173
0
        return false;
7174
0
      name = new_name;
7175
0
    }
7176
        /* Add section name to section name section.  */
7177
0
        shdrp->sh_name
7178
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7179
0
                  name, false);
7180
0
        d = elf_section_data (sec);
7181
7182
        /* Add reloc section name to section name section.  */
7183
0
        if (d->rel.hdr
7184
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7185
0
              name, false))
7186
0
    return false;
7187
0
        if (d->rela.hdr
7188
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7189
0
              name, true))
7190
0
    return false;
7191
7192
        /* Update section size and contents.  */
7193
0
        shdrp->sh_size = sec->size;
7194
0
        shdrp->contents = sec->contents;
7195
0
        sec->contents = NULL;
7196
0
      }
7197
7198
294
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7199
294
      (abfd->flags & (EXEC_P | DYNAMIC))
7200
233
      || bfd_get_format (abfd) == bfd_core,
7201
294
      bed->s->log_file_align);
7202
294
  }
7203
1.32k
    }
7204
7205
  /* Place section name section after DWARF debug sections have been
7206
     compressed.  */
7207
131
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7208
131
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7209
131
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7210
131
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7211
7212
  /* Place the section headers.  */
7213
131
  i_ehdrp = elf_elfheader (abfd);
7214
131
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7215
131
  i_ehdrp->e_shoff = off;
7216
131
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7217
131
  elf_next_file_pos (abfd) = off;
7218
7219
131
  return true;
7220
131
}
7221
7222
bool
7223
_bfd_elf_write_object_contents (bfd *abfd)
7224
131
{
7225
131
  elf_backend_data *bed = get_elf_backend_data (abfd);
7226
131
  Elf_Internal_Shdr **i_shdrp;
7227
131
  bool failed;
7228
131
  unsigned int count, num_sec;
7229
131
  struct elf_obj_tdata *t;
7230
7231
131
  if (! abfd->output_has_begun
7232
81
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7233
0
    return false;
7234
  /* Do not rewrite ELF data when the BFD has been opened for update.
7235
     abfd->output_has_begun was set to TRUE on opening, so creation of
7236
     new sections, and modification of existing section sizes was
7237
     restricted.  This means the ELF header, program headers and
7238
     section headers can't have changed.  If the contents of any
7239
     sections has been modified, then those changes have already been
7240
     written to the BFD.  */
7241
131
  else if (abfd->direction == both_direction)
7242
0
    {
7243
0
      BFD_ASSERT (abfd->output_has_begun);
7244
0
      return true;
7245
0
    }
7246
7247
131
  i_shdrp = elf_elfsections (abfd);
7248
7249
131
  failed = false;
7250
131
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7251
131
  if (failed)
7252
0
    return false;
7253
7254
131
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7255
0
    return false;
7256
7257
  /* After writing the headers, we need to write the sections too...  */
7258
131
  num_sec = elf_numsections (abfd);
7259
1.45k
  for (count = 1; count < num_sec; count++)
7260
1.32k
    {
7261
      /* Don't set the sh_name field without section header.  */
7262
1.32k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7263
1.32k
  i_shdrp[count]->sh_name
7264
1.32k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7265
1.32k
            i_shdrp[count]->sh_name);
7266
1.32k
      if (bed->elf_backend_section_processing)
7267
176
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7268
0
    return false;
7269
1.32k
      if (i_shdrp[count]->contents)
7270
212
  {
7271
212
    bfd_size_type amt = i_shdrp[count]->sh_size;
7272
7273
212
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7274
212
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7275
0
      return false;
7276
212
  }
7277
1.32k
    }
7278
7279
  /* Write out the section header names.  */
7280
131
  t = elf_tdata (abfd);
7281
131
  if (elf_shstrtab (abfd) != NULL
7282
131
      && t->shstrtab_hdr.sh_offset != -1
7283
131
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7284
131
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7285
0
    return false;
7286
7287
131
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7288
0
    return false;
7289
7290
131
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7291
0
    return false;
7292
7293
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7294
131
  if (t->o->build_id.after_write_object_contents != NULL
7295
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7296
0
    return false;
7297
131
  if (t->o->package_metadata.after_write_object_contents != NULL
7298
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7299
0
    return false;
7300
7301
131
  return true;
7302
131
}
7303
7304
bool
7305
_bfd_elf_write_corefile_contents (bfd *abfd)
7306
0
{
7307
  /* Hopefully this can be done just like an object file.  */
7308
0
  return _bfd_elf_write_object_contents (abfd);
7309
0
}
7310
7311
/* Given a section, search the header to find them.  */
7312
7313
unsigned int
7314
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7315
1.11k
{
7316
1.11k
  elf_backend_data *bed;
7317
1.11k
  unsigned int sec_index;
7318
7319
1.11k
  if (elf_section_data (asect) != NULL
7320
496
      && elf_section_data (asect)->this_idx != 0)
7321
496
    return elf_section_data (asect)->this_idx;
7322
7323
622
  if (bfd_is_abs_section (asect))
7324
0
    sec_index = SHN_ABS;
7325
622
  else if (bfd_is_com_section (asect))
7326
5
    sec_index = SHN_COMMON;
7327
617
  else if (bfd_is_und_section (asect))
7328
617
    sec_index = SHN_UNDEF;
7329
0
  else
7330
0
    sec_index = SHN_BAD;
7331
7332
622
  bed = get_elf_backend_data (abfd);
7333
622
  if (bed->elf_backend_section_from_bfd_section)
7334
622
    {
7335
622
      int retval = sec_index;
7336
7337
622
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7338
3
  return retval;
7339
622
    }
7340
7341
619
  if (sec_index == SHN_BAD)
7342
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7343
7344
619
  return sec_index;
7345
622
}
7346
7347
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7348
   on error.  */
7349
7350
int
7351
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7352
541
{
7353
541
  asymbol *asym_ptr = *asym_ptr_ptr;
7354
541
  int idx;
7355
541
  flagword flags = asym_ptr->flags;
7356
7357
  /* When gas creates relocations against local labels, it creates its
7358
     own symbol for the section, but does put the symbol into the
7359
     symbol chain, so udata is 0.  When the linker is generating
7360
     relocatable output, this section symbol may be for one of the
7361
     input sections rather than the output section.  */
7362
541
  if (asym_ptr->udata.i == 0
7363
0
      && (flags & BSF_SECTION_SYM)
7364
0
      && asym_ptr->section)
7365
0
    {
7366
0
      asection *sec;
7367
7368
0
      sec = asym_ptr->section;
7369
0
      if (sec->owner != abfd && sec->output_section != NULL)
7370
0
  sec = sec->output_section;
7371
0
      if (sec->owner == abfd
7372
0
    && sec->index < elf_num_section_syms (abfd)
7373
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7374
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7375
0
    }
7376
7377
541
  idx = asym_ptr->udata.i;
7378
7379
541
  if (idx == 0)
7380
0
    {
7381
      /* This case can occur when using --strip-symbol on a symbol
7382
   which is used in a relocation entry.  */
7383
0
      _bfd_error_handler
7384
  /* xgettext:c-format */
7385
0
  (_("%pB: symbol `%s' required but not present"),
7386
0
   abfd, bfd_asymbol_name (asym_ptr));
7387
0
      bfd_set_error (bfd_error_no_symbols);
7388
0
      return -1;
7389
0
    }
7390
7391
#if DEBUG & 4
7392
  {
7393
    fprintf (stderr,
7394
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7395
       " flags = 0x%.8x\n",
7396
       (long) asym_ptr, asym_ptr->name, idx, flags);
7397
    fflush (stderr);
7398
  }
7399
#endif
7400
7401
541
  return idx;
7402
541
}
7403
7404
static inline bfd_vma
7405
segment_size (Elf_Internal_Phdr *segment)
7406
3.96k
{
7407
3.96k
  return (segment->p_memsz > segment->p_filesz
7408
3.96k
    ? segment->p_memsz : segment->p_filesz);
7409
3.96k
}
7410
7411
7412
/* Returns the end address of the segment + 1.  */
7413
static inline bfd_vma
7414
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7415
23
{
7416
23
  return start + segment_size (segment);
7417
23
}
7418
7419
static inline bfd_size_type
7420
section_size (asection *section, Elf_Internal_Phdr *segment)
7421
3.96k
{
7422
3.96k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7423
224
      || (section->flags & SEC_THREAD_LOCAL) == 0
7424
0
      || segment->p_type == PT_TLS)
7425
3.96k
    return section->size;
7426
0
  return 0;
7427
3.96k
}
7428
7429
/* Returns TRUE if the given section is contained within the given
7430
   segment.  LMA addresses are compared against PADDR when
7431
   USE_VADDR is false, VMA against VADDR when true.  */
7432
static bool
7433
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7434
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7435
     bool use_vaddr)
7436
2.94k
{
7437
2.94k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7438
2.94k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7439
2.94k
  bfd_vma octet;
7440
2.94k
  if (_bfd_mul_overflow (addr, opb, &octet))
7441
0
    return false;
7442
  /* The third and fourth lines below are testing that the section end
7443
     address is within the segment.  It's written this way to avoid
7444
     overflow.  Add seg_addr + section_size to both sides of the
7445
     inequality to make it obvious.  */
7446
2.94k
  return (octet >= seg_addr
7447
2.11k
    && segment_size (segment) >= section_size (section, segment)
7448
1.82k
    && (octet - seg_addr
7449
1.82k
        <= segment_size (segment) - section_size (section, segment)));
7450
2.94k
}
7451
7452
/* Handle PT_NOTE segment.  */
7453
static bool
7454
is_note (asection *s, Elf_Internal_Phdr *p)
7455
1.66k
{
7456
1.66k
  return (p->p_type == PT_NOTE
7457
282
    && elf_section_type (s) == SHT_NOTE
7458
24
    && (ufile_ptr) s->filepos >= p->p_offset
7459
20
    && p->p_filesz >= s->size
7460
20
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7461
1.66k
}
7462
7463
/* Rewrite program header information.  */
7464
7465
static bool
7466
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7467
14
{
7468
14
  Elf_Internal_Ehdr *iehdr;
7469
14
  struct elf_segment_map *map;
7470
14
  struct elf_segment_map *map_first;
7471
14
  struct elf_segment_map **pointer_to_map;
7472
14
  Elf_Internal_Phdr *segment;
7473
14
  asection *section;
7474
14
  unsigned int i;
7475
14
  unsigned int num_segments;
7476
14
  bool phdr_included = false;
7477
14
  bool p_paddr_valid;
7478
14
  struct elf_segment_map *phdr_adjust_seg = NULL;
7479
14
  unsigned int phdr_adjust_num = 0;
7480
14
  elf_backend_data *bed;
7481
14
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7482
7483
14
  bed = get_elf_backend_data (ibfd);
7484
14
  iehdr = elf_elfheader (ibfd);
7485
7486
14
  map_first = NULL;
7487
14
  pointer_to_map = &map_first;
7488
7489
14
  num_segments = elf_elfheader (ibfd)->e_phnum;
7490
7491
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7492
     linker, which generates a PT_INTERP section with p_vaddr and
7493
     p_memsz set to 0.  */
7494
14
#define IS_SOLARIS_PT_INTERP(p, s)          \
7495
335
  (p->p_vaddr == 0              \
7496
335
   && p->p_paddr == 0              \
7497
335
   && p->p_memsz == 0              \
7498
335
   && p->p_filesz > 0              \
7499
335
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7500
335
   && s->size > 0              \
7501
335
   && (bfd_vma) s->filepos >= p->p_offset        \
7502
335
   && ((bfd_vma) s->filepos + s->size         \
7503
0
       <= p->p_offset + p->p_filesz))
7504
7505
  /* Decide if the given section should be included in the given segment.
7506
     A section will be included if:
7507
       1. It is within the address space of the segment -- we use the LMA
7508
    if that is set for the segment and the VMA otherwise,
7509
       2. It is an allocated section or a NOTE section in a PT_NOTE
7510
    segment.
7511
       3. There is an output section associated with it,
7512
       4. The section has not already been allocated to a previous segment.
7513
       5. PT_GNU_STACK segments do not include any sections.
7514
       6. PT_TLS segment includes only SHF_TLS sections.
7515
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7516
       8. PT_DYNAMIC should not contain empty sections at the beginning
7517
    (with the possible exception of .dynamic).  */
7518
14
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7519
2.98k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7520
2.52k
          segment->p_vaddr, opb, !paddr_valid)    \
7521
2.52k
     && (section->flags & SEC_ALLOC) != 0)       \
7522
2.52k
    || is_note (section, segment))         \
7523
2.52k
   && segment->p_type != PT_GNU_STACK          \
7524
2.52k
   && (segment->p_type != PT_TLS          \
7525
850
       || (section->flags & SEC_THREAD_LOCAL))        \
7526
2.52k
   && (segment->p_type == PT_LOAD          \
7527
850
       || segment->p_type == PT_TLS          \
7528
850
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7529
2.52k
   && (segment->p_type != PT_DYNAMIC          \
7530
848
       || section_size (section, segment) > 0        \
7531
848
       || (segment->p_paddr            \
7532
0
     ? segment->p_paddr != section->lma * (opb)      \
7533
0
     : segment->p_vaddr != section->vma * (opb))      \
7534
848
       || (streq (bfd_section_name (section), ".dynamic")))    \
7535
2.98k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7536
7537
/* If the output section of a section in the input segment is NULL,
7538
   it is removed from the corresponding output segment.   */
7539
14
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7540
458
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7541
458
   && section->output_section != NULL)
7542
7543
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7544
14
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7545
34
  (seg1->field >= segment_end (seg2, seg2->field))
7546
7547
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7548
     their VMA address ranges and their LMA address ranges overlap.
7549
     It is possible to have overlapping VMA ranges without overlapping LMA
7550
     ranges.  RedBoot images for example can have both .data and .bss mapped
7551
     to the same VMA range, but with the .data section mapped to a different
7552
     LMA.  */
7553
14
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7554
14
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7555
13
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7556
13
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7557
2
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7558
7559
  /* Initialise the segment mark field, and discard stupid alignment.  */
7560
303
  for (section = ibfd->sections; section != NULL; section = section->next)
7561
289
    {
7562
289
      asection *o = section->output_section;
7563
289
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7564
0
  o->alignment_power = 0;
7565
289
      section->segment_mark = false;
7566
289
    }
7567
7568
  /* The Solaris linker creates program headers in which all the
7569
     p_paddr fields are zero.  When we try to objcopy or strip such a
7570
     file, we get confused.  Check for this case, and if we find it
7571
     don't set the p_paddr_valid fields.  */
7572
14
  p_paddr_valid = false;
7573
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7574
14
       i < num_segments;
7575
14
       i++, segment++)
7576
14
    if (segment->p_paddr != 0)
7577
14
      {
7578
14
  p_paddr_valid = true;
7579
14
  break;
7580
14
      }
7581
7582
  /* Scan through the segments specified in the program header
7583
     of the input BFD.  For this first scan we look for overlaps
7584
     in the loadable segments.  These can be created by weird
7585
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7586
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7587
138
       i < num_segments;
7588
124
       i++, segment++)
7589
124
    {
7590
124
      unsigned int j;
7591
124
      Elf_Internal_Phdr *segment2;
7592
7593
124
      if (segment->p_type == PT_INTERP)
7594
351
  for (section = ibfd->sections; section; section = section->next)
7595
335
    if (IS_SOLARIS_PT_INTERP (segment, section))
7596
0
      {
7597
        /* Mininal change so that the normal section to segment
7598
     assignment code will work.  */
7599
0
        segment->p_vaddr = section->vma * opb;
7600
0
        break;
7601
0
      }
7602
7603
124
      if (segment->p_type != PT_LOAD)
7604
95
  {
7605
    /* Remove PT_GNU_RELRO segment.  */
7606
95
    if (segment->p_type == PT_GNU_RELRO)
7607
13
      segment->p_type = PT_NULL;
7608
95
    continue;
7609
95
  }
7610
7611
      /* Determine if this segment overlaps any previous segments.  */
7612
98
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7613
71
  {
7614
71
    bfd_signed_vma extra_length;
7615
7616
71
    if (segment2->p_type != PT_LOAD
7617
13
        || !SEGMENT_OVERLAPS (segment, segment2))
7618
69
      continue;
7619
7620
    /* Merge the two segments together.  */
7621
2
    if (segment2->p_vaddr < segment->p_vaddr)
7622
2
      {
7623
        /* Extend SEGMENT2 to include SEGMENT and then delete
7624
     SEGMENT.  */
7625
2
        extra_length = (segment_end (segment, segment->p_vaddr)
7626
2
            - segment_end (segment2, segment2->p_vaddr));
7627
7628
2
        if (extra_length > 0)
7629
0
    {
7630
0
      segment2->p_memsz += extra_length;
7631
0
      segment2->p_filesz += extra_length;
7632
0
    }
7633
7634
2
        segment->p_type = PT_NULL;
7635
7636
        /* Since we have deleted P we must restart the outer loop.  */
7637
2
        i = 0;
7638
2
        segment = elf_tdata (ibfd)->phdr;
7639
2
        break;
7640
2
      }
7641
0
    else
7642
0
      {
7643
        /* Extend SEGMENT to include SEGMENT2 and then delete
7644
     SEGMENT2.  */
7645
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7646
0
            - segment_end (segment, segment->p_vaddr));
7647
7648
0
        if (extra_length > 0)
7649
0
    {
7650
0
      segment->p_memsz += extra_length;
7651
0
      segment->p_filesz += extra_length;
7652
0
    }
7653
7654
0
        segment2->p_type = PT_NULL;
7655
0
      }
7656
2
  }
7657
29
    }
7658
7659
  /* The second scan attempts to assign sections to segments.  */
7660
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7661
132
       i < num_segments;
7662
118
       i++, segment++)
7663
118
    {
7664
118
      unsigned int section_count;
7665
118
      asection **sections;
7666
118
      asection *output_section;
7667
118
      unsigned int isec;
7668
118
      asection *matching_lma;
7669
118
      asection *suggested_lma;
7670
118
      unsigned int j;
7671
118
      size_t amt;
7672
118
      asection *first_section;
7673
7674
118
      if (segment->p_type == PT_NULL)
7675
15
  continue;
7676
7677
103
      first_section = NULL;
7678
      /* Compute how many sections might be placed into this segment.  */
7679
103
      for (section = ibfd->sections, section_count = 0;
7680
2.16k
     section != NULL;
7681
2.06k
     section = section->next)
7682
2.06k
  {
7683
    /* Find the first section in the input segment, which may be
7684
       removed from the corresponding output segment.   */
7685
2.06k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7686
424
      {
7687
424
        if (first_section == NULL)
7688
67
    first_section = section;
7689
424
        if (section->output_section != NULL)
7690
424
    ++section_count;
7691
424
      }
7692
2.06k
  }
7693
7694
      /* Allocate a segment map big enough to contain
7695
   all of the sections we have selected.  */
7696
103
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7697
103
      amt += section_count * sizeof (asection *);
7698
103
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7699
103
      if (map == NULL)
7700
0
  return false;
7701
7702
      /* Initialise the fields of the segment map.  Default to
7703
   using the physical address of the segment in the input BFD.  */
7704
103
      map->next = NULL;
7705
103
      map->p_type = segment->p_type;
7706
103
      map->p_flags = segment->p_flags;
7707
103
      map->p_flags_valid = 1;
7708
7709
103
      if (map->p_type == PT_LOAD
7710
25
    && (ibfd->flags & D_PAGED) != 0
7711
3
    && maxpagesize > 1
7712
3
    && segment->p_align > 1)
7713
3
  {
7714
3
    map->p_align = segment->p_align;
7715
3
    if (segment->p_align > maxpagesize)
7716
0
      map->p_align = maxpagesize;
7717
3
    map->p_align_valid = 1;
7718
3
  }
7719
7720
      /* If the first section in the input segment is removed, there is
7721
   no need to preserve segment physical address in the corresponding
7722
   output segment.  */
7723
103
      if (!first_section || first_section->output_section != NULL)
7724
103
  {
7725
103
    map->p_paddr = segment->p_paddr;
7726
103
    map->p_paddr_valid = p_paddr_valid;
7727
103
  }
7728
7729
      /* Determine if this segment contains the ELF file header
7730
   and if it contains the program headers themselves.  */
7731
103
      map->includes_filehdr = (segment->p_offset == 0
7732
27
             && segment->p_filesz >= iehdr->e_ehsize);
7733
103
      map->includes_phdrs = 0;
7734
7735
103
      if (!phdr_included || segment->p_type != PT_LOAD)
7736
92
  {
7737
92
    map->includes_phdrs =
7738
92
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7739
34
       && (segment->p_offset + segment->p_filesz
7740
34
     >= ((bfd_vma) iehdr->e_phoff
7741
34
         + iehdr->e_phnum * iehdr->e_phentsize)));
7742
7743
92
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7744
14
      phdr_included = true;
7745
92
  }
7746
7747
103
      if (section_count == 0)
7748
36
  {
7749
    /* Special segments, such as the PT_PHDR segment, may contain
7750
       no sections, but ordinary, loadable segments should contain
7751
       something.  They are allowed by the ELF spec however, so only
7752
       a warning is produced.
7753
       Don't warn if an empty PT_LOAD contains the program headers.
7754
       There is however the valid use case of embedded systems which
7755
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7756
       flash memory with zeros.  No warning is shown for that case.  */
7757
36
    if (segment->p_type == PT_LOAD
7758
1
        && !map->includes_phdrs
7759
1
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7760
      /* xgettext:c-format */
7761
1
      _bfd_error_handler
7762
1
        (_("%pB: warning: empty loadable segment detected"
7763
1
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7764
1
         ibfd, (uint64_t) segment->p_vaddr);
7765
7766
36
    map->p_vaddr_offset = segment->p_vaddr / opb;
7767
36
    map->count = 0;
7768
36
    *pointer_to_map = map;
7769
36
    pointer_to_map = &map->next;
7770
7771
36
    continue;
7772
36
  }
7773
7774
      /* Now scan the sections in the input BFD again and attempt
7775
   to add their corresponding output sections to the segment map.
7776
   The problem here is how to handle an output section which has
7777
   been moved (ie had its LMA changed).  There are four possibilities:
7778
7779
   1. None of the sections have been moved.
7780
      In this case we can continue to use the segment LMA from the
7781
      input BFD.
7782
7783
   2. All of the sections have been moved by the same amount.
7784
      In this case we can change the segment's LMA to match the LMA
7785
      of the first section.
7786
7787
   3. Some of the sections have been moved, others have not.
7788
      In this case those sections which have not been moved can be
7789
      placed in the current segment which will have to have its size,
7790
      and possibly its LMA changed, and a new segment or segments will
7791
      have to be created to contain the other sections.
7792
7793
   4. The sections have been moved, but not by the same amount.
7794
      In this case we can change the segment's LMA to match the LMA
7795
      of the first section and we will have to create a new segment
7796
      or segments to contain the other sections.
7797
7798
   In order to save time, we allocate an array to hold the section
7799
   pointers that we are interested in.  As these sections get assigned
7800
   to a segment, they are removed from this array.  */
7801
7802
67
      amt = section_count * sizeof (asection *);
7803
67
      sections = (asection **) bfd_malloc (amt);
7804
67
      if (sections == NULL)
7805
0
  return false;
7806
7807
      /* Step One: Scan for segment vs section LMA conflicts.
7808
   Also add the sections to the section array allocated above.
7809
   Also add the sections to the current segment.  In the common
7810
   case, where the sections have not been moved, this means that
7811
   we have completely filled the segment, and there is nothing
7812
   more to do.  */
7813
67
      isec = 0;
7814
67
      matching_lma = NULL;
7815
67
      suggested_lma = NULL;
7816
7817
67
      for (section = first_section, j = 0;
7818
458
     section != NULL;
7819
391
     section = section->next)
7820
458
  {
7821
458
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7822
424
      {
7823
424
        output_section = section->output_section;
7824
7825
424
        sections[j++] = section;
7826
7827
        /* The Solaris native linker always sets p_paddr to 0.
7828
     We try to catch that case here, and set it to the
7829
     correct value.  Note - some backends require that
7830
     p_paddr be left as zero.  */
7831
424
        if (!p_paddr_valid
7832
0
      && segment->p_vaddr != 0
7833
0
      && !bed->want_p_paddr_set_to_zero
7834
0
      && isec == 0
7835
0
      && output_section->lma != 0
7836
0
      && (align_power (segment->p_vaddr
7837
0
           + (map->includes_filehdr
7838
0
              ? iehdr->e_ehsize : 0)
7839
0
           + (map->includes_phdrs
7840
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7841
0
              : 0),
7842
0
           output_section->alignment_power * opb)
7843
0
          == (output_section->vma * opb)))
7844
0
    map->p_paddr = segment->p_vaddr;
7845
7846
        /* Match up the physical address of the segment with the
7847
     LMA address of the output section.  */
7848
424
        if (is_contained_by (output_section, segment, map->p_paddr,
7849
424
           0, opb, false)
7850
6
      || is_note (section, segment))
7851
424
    {
7852
424
      if (matching_lma == NULL
7853
357
          || output_section->lma < matching_lma->lma)
7854
75
        matching_lma = output_section;
7855
7856
      /* We assume that if the section fits within the segment
7857
         then it does not overlap any other section within that
7858
         segment.  */
7859
424
      map->sections[isec++] = output_section;
7860
424
    }
7861
0
        else if (suggested_lma == NULL)
7862
0
    suggested_lma = output_section;
7863
7864
424
        if (j == section_count)
7865
67
    break;
7866
424
      }
7867
458
  }
7868
7869
67
      BFD_ASSERT (j == section_count);
7870
7871
      /* Step Two: Adjust the physical address of the current segment,
7872
   if necessary.  */
7873
67
      if (isec == section_count)
7874
67
  {
7875
    /* All of the sections fitted within the segment as currently
7876
       specified.  This is the default case.  Add the segment to
7877
       the list of built segments and carry on to process the next
7878
       program header in the input BFD.  */
7879
67
    map->count = section_count;
7880
67
    *pointer_to_map = map;
7881
67
    pointer_to_map = &map->next;
7882
7883
67
    if (p_paddr_valid
7884
67
        && !bed->want_p_paddr_set_to_zero)
7885
67
      {
7886
67
        bfd_vma hdr_size = 0;
7887
67
        if (map->includes_filehdr)
7888
17
    hdr_size = iehdr->e_ehsize;
7889
67
        if (map->includes_phdrs)
7890
21
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7891
7892
        /* Account for padding before the first section in the
7893
     segment.  */
7894
67
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7895
67
             - matching_lma->lma);
7896
67
      }
7897
7898
67
    free (sections);
7899
67
    continue;
7900
67
  }
7901
0
      else
7902
0
  {
7903
    /* Change the current segment's physical address to match
7904
       the LMA of the first section that fitted, or if no
7905
       section fitted, the first section.  */
7906
0
    if (matching_lma == NULL)
7907
0
      matching_lma = suggested_lma;
7908
7909
0
    map->p_paddr = matching_lma->lma * opb;
7910
7911
    /* Offset the segment physical address from the lma
7912
       to allow for space taken up by elf headers.  */
7913
0
    if (map->includes_phdrs)
7914
0
      {
7915
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7916
7917
        /* iehdr->e_phnum is just an estimate of the number
7918
     of program headers that we will need.  Make a note
7919
     here of the number we used and the segment we chose
7920
     to hold these headers, so that we can adjust the
7921
     offset when we know the correct value.  */
7922
0
        phdr_adjust_num = iehdr->e_phnum;
7923
0
        phdr_adjust_seg = map;
7924
0
      }
7925
7926
0
    if (map->includes_filehdr)
7927
0
      {
7928
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7929
0
        map->p_paddr -= iehdr->e_ehsize;
7930
        /* We've subtracted off the size of headers from the
7931
     first section lma, but there may have been some
7932
     alignment padding before that section too.  Try to
7933
     account for that by adjusting the segment lma down to
7934
     the same alignment.  */
7935
0
        if (segment->p_align != 0 && segment->p_align < align)
7936
0
    align = segment->p_align;
7937
0
        map->p_paddr &= -(align * opb);
7938
0
      }
7939
0
  }
7940
7941
      /* Step Three: Loop over the sections again, this time assigning
7942
   those that fit to the current segment and removing them from the
7943
   sections array; but making sure not to leave large gaps.  Once all
7944
   possible sections have been assigned to the current segment it is
7945
   added to the list of built segments and if sections still remain
7946
   to be assigned, a new segment is constructed before repeating
7947
   the loop.  */
7948
0
      isec = 0;
7949
0
      do
7950
0
  {
7951
0
    map->count = 0;
7952
0
    suggested_lma = NULL;
7953
7954
    /* Fill the current segment with sections that fit.  */
7955
0
    for (j = 0; j < section_count; j++)
7956
0
      {
7957
0
        section = sections[j];
7958
7959
0
        if (section == NULL)
7960
0
    continue;
7961
7962
0
        output_section = section->output_section;
7963
7964
0
        BFD_ASSERT (output_section != NULL);
7965
7966
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7967
0
           0, opb, false)
7968
0
      || is_note (section, segment))
7969
0
    {
7970
0
      if (map->count == 0)
7971
0
        {
7972
          /* If the first section in a segment does not start at
7973
       the beginning of the segment, then something is
7974
       wrong.  */
7975
0
          if (align_power (map->p_paddr
7976
0
               + (map->includes_filehdr
7977
0
            ? iehdr->e_ehsize : 0)
7978
0
               + (map->includes_phdrs
7979
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7980
0
            : 0),
7981
0
               output_section->alignment_power * opb)
7982
0
        != output_section->lma * opb)
7983
0
      goto sorry;
7984
0
        }
7985
0
      else
7986
0
        {
7987
0
          asection *prev_sec;
7988
7989
0
          prev_sec = map->sections[map->count - 1];
7990
7991
          /* If the gap between the end of the previous section
7992
       and the start of this section is more than
7993
       maxpagesize then we need to start a new segment.  */
7994
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7995
0
              maxpagesize)
7996
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7997
0
        || (prev_sec->lma + prev_sec->size
7998
0
            > output_section->lma))
7999
0
      {
8000
0
        if (suggested_lma == NULL)
8001
0
          suggested_lma = output_section;
8002
8003
0
        continue;
8004
0
      }
8005
0
        }
8006
8007
0
      map->sections[map->count++] = output_section;
8008
0
      ++isec;
8009
0
      sections[j] = NULL;
8010
0
      if (segment->p_type == PT_LOAD)
8011
0
        section->segment_mark = true;
8012
0
    }
8013
0
        else if (suggested_lma == NULL)
8014
0
    suggested_lma = output_section;
8015
0
      }
8016
8017
    /* PR 23932.  A corrupt input file may contain sections that cannot
8018
       be assigned to any segment - because for example they have a
8019
       negative size - or segments that do not contain any sections.
8020
       But there are also valid reasons why a segment can be empty.
8021
       So allow a count of zero.  */
8022
8023
    /* Add the current segment to the list of built segments.  */
8024
0
    *pointer_to_map = map;
8025
0
    pointer_to_map = &map->next;
8026
8027
0
    if (isec < section_count)
8028
0
      {
8029
        /* We still have not allocated all of the sections to
8030
     segments.  Create a new segment here, initialise it
8031
     and carry on looping.  */
8032
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8033
0
        amt += section_count * sizeof (asection *);
8034
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8035
0
        if (map == NULL)
8036
0
    {
8037
0
      free (sections);
8038
0
      return false;
8039
0
    }
8040
8041
        /* Initialise the fields of the segment map.  Set the physical
8042
     physical address to the LMA of the first section that has
8043
     not yet been assigned.  */
8044
0
        map->next = NULL;
8045
0
        map->p_type = segment->p_type;
8046
0
        map->p_flags = segment->p_flags;
8047
0
        map->p_flags_valid = 1;
8048
0
        map->p_paddr = suggested_lma->lma * opb;
8049
0
        map->p_paddr_valid = p_paddr_valid;
8050
0
        map->includes_filehdr = 0;
8051
0
        map->includes_phdrs = 0;
8052
0
      }
8053
8054
0
    continue;
8055
0
  sorry:
8056
0
    bfd_set_error (bfd_error_sorry);
8057
0
    free (sections);
8058
0
    return false;
8059
0
  }
8060
0
      while (isec < section_count);
8061
8062
0
      free (sections);
8063
0
    }
8064
8065
14
  elf_seg_map (obfd) = map_first;
8066
8067
  /* If we had to estimate the number of program headers that were
8068
     going to be needed, then check our estimate now and adjust
8069
     the offset if necessary.  */
8070
14
  if (phdr_adjust_seg != NULL)
8071
0
    {
8072
0
      unsigned int count;
8073
8074
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8075
0
  count++;
8076
8077
0
      if (count > phdr_adjust_num)
8078
0
  phdr_adjust_seg->p_paddr
8079
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8080
8081
0
      for (map = map_first; map != NULL; map = map->next)
8082
0
  if (map->p_type == PT_PHDR)
8083
0
    {
8084
0
      bfd_vma adjust
8085
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8086
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8087
0
      break;
8088
0
    }
8089
0
    }
8090
8091
14
#undef IS_SOLARIS_PT_INTERP
8092
14
#undef IS_SECTION_IN_INPUT_SEGMENT
8093
14
#undef INCLUDE_SECTION_IN_SEGMENT
8094
14
#undef SEGMENT_AFTER_SEGMENT
8095
14
#undef SEGMENT_OVERLAPS
8096
14
  return true;
8097
14
}
8098
8099
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8100
8101
static bool
8102
elf_is_p_align_valid (bfd *abfd)
8103
17
{
8104
17
  unsigned int i;
8105
17
  Elf_Internal_Phdr *segment;
8106
17
  unsigned int num_segments;
8107
17
  elf_backend_data *bed = get_elf_backend_data (abfd);
8108
17
  bfd_size_type maxpagesize = bed->maxpagesize;
8109
17
  bfd_size_type p_align = bed->p_align;
8110
8111
  /* Return true if the default p_align value isn't set or the maximum
8112
     page size is the same as the minimum page size.  */
8113
17
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8114
17
    return true;
8115
8116
  /* When the default p_align value is set, p_align may be set to the
8117
     default p_align value while segments are aligned to the maximum
8118
     page size.  In this case, the input p_align will be ignored and
8119
     the maximum page size will be used to align the output segments.  */
8120
0
  segment = elf_tdata (abfd)->phdr;
8121
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8122
0
  for (i = 0; i < num_segments; i++, segment++)
8123
0
    if (segment->p_type == PT_LOAD
8124
0
  && (segment->p_align != p_align
8125
0
      || vma_page_aligned_bias (segment->p_vaddr,
8126
0
              segment->p_offset,
8127
0
              maxpagesize) != 0))
8128
0
      return true;
8129
8130
0
  return false;
8131
0
}
8132
8133
/* Copy ELF program header information.  */
8134
8135
static bool
8136
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8137
17
{
8138
17
  Elf_Internal_Ehdr *iehdr;
8139
17
  struct elf_segment_map *map;
8140
17
  struct elf_segment_map *map_first;
8141
17
  struct elf_segment_map **pointer_to_map;
8142
17
  Elf_Internal_Phdr *segment;
8143
17
  unsigned int i;
8144
17
  unsigned int num_segments;
8145
17
  bool phdr_included = false;
8146
17
  bool p_paddr_valid;
8147
17
  bool p_palign_valid;
8148
17
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8149
8150
17
  iehdr = elf_elfheader (ibfd);
8151
8152
17
  map_first = NULL;
8153
17
  pointer_to_map = &map_first;
8154
8155
  /* If all the segment p_paddr fields are zero, don't set
8156
     map->p_paddr_valid.  */
8157
17
  p_paddr_valid = false;
8158
17
  num_segments = elf_elfheader (ibfd)->e_phnum;
8159
17
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8160
18
       i < num_segments;
8161
17
       i++, segment++)
8162
17
    if (segment->p_paddr != 0)
8163
16
      {
8164
16
  p_paddr_valid = true;
8165
16
  break;
8166
16
      }
8167
8168
17
  p_palign_valid = elf_is_p_align_valid (ibfd);
8169
8170
17
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8171
153
       i < num_segments;
8172
136
       i++, segment++)
8173
136
    {
8174
136
      asection *section;
8175
136
      unsigned int section_count;
8176
136
      size_t amt;
8177
136
      Elf_Internal_Shdr *this_hdr;
8178
136
      asection *first_section = NULL;
8179
136
      asection *lowest_section;
8180
8181
      /* Compute how many sections are in this segment.  */
8182
136
      for (section = ibfd->sections, section_count = 0;
8183
2.96k
     section != NULL;
8184
2.83k
     section = section->next)
8185
2.83k
  {
8186
2.83k
    this_hdr = &(elf_section_data(section)->this_hdr);
8187
2.83k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8188
280
      {
8189
280
        if (first_section == NULL)
8190
75
    first_section = section;
8191
280
        section_count++;
8192
280
      }
8193
2.83k
  }
8194
8195
      /* Allocate a segment map big enough to contain
8196
   all of the sections we have selected.  */
8197
136
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8198
136
      amt += section_count * sizeof (asection *);
8199
136
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8200
136
      if (map == NULL)
8201
0
  return false;
8202
8203
      /* Initialize the fields of the output segment map with the
8204
   input segment.  */
8205
136
      map->next = NULL;
8206
136
      map->p_type = segment->p_type;
8207
136
      map->p_flags = segment->p_flags;
8208
136
      map->p_flags_valid = 1;
8209
136
      map->p_paddr = segment->p_paddr;
8210
136
      map->p_paddr_valid = p_paddr_valid;
8211
136
      map->p_align = segment->p_align;
8212
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8213
136
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8214
133
          || p_palign_valid);
8215
136
      map->p_vaddr_offset = 0;
8216
8217
136
      if (map->p_type == PT_GNU_RELRO
8218
124
    || map->p_type == PT_GNU_STACK)
8219
15
  {
8220
    /* The PT_GNU_RELRO segment may contain the first a few
8221
       bytes in the .got.plt section even if the whole .got.plt
8222
       section isn't in the PT_GNU_RELRO segment.  We won't
8223
       change the size of the PT_GNU_RELRO segment.
8224
       Similarly, PT_GNU_STACK size is significant on uclinux
8225
       systems.    */
8226
15
    map->p_size = segment->p_memsz;
8227
15
    map->p_size_valid = 1;
8228
15
  }
8229
8230
      /* Determine if this segment contains the ELF file header
8231
   and if it contains the program headers themselves.  */
8232
136
      map->includes_filehdr = (segment->p_offset == 0
8233
28
             && segment->p_filesz >= iehdr->e_ehsize);
8234
8235
136
      map->includes_phdrs = 0;
8236
136
      if (! phdr_included || segment->p_type != PT_LOAD)
8237
123
  {
8238
123
    map->includes_phdrs =
8239
123
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8240
39
       && (segment->p_offset + segment->p_filesz
8241
39
     >= ((bfd_vma) iehdr->e_phoff
8242
39
         + iehdr->e_phnum * iehdr->e_phentsize)));
8243
8244
123
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8245
15
      phdr_included = true;
8246
123
  }
8247
8248
136
      lowest_section = NULL;
8249
136
      if (section_count != 0)
8250
75
  {
8251
75
    unsigned int isec = 0;
8252
8253
75
    for (section = first_section;
8254
323
         section != NULL;
8255
248
         section = section->next)
8256
323
      {
8257
323
        this_hdr = &(elf_section_data(section)->this_hdr);
8258
323
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8259
280
    {
8260
280
      map->sections[isec++] = section->output_section;
8261
280
      if ((section->flags & SEC_ALLOC) != 0)
8262
242
        {
8263
242
          bfd_vma seg_off;
8264
8265
242
          if (lowest_section == NULL
8266
174
        || section->lma < lowest_section->lma)
8267
75
      lowest_section = section;
8268
8269
          /* Section lmas are set up from PT_LOAD header
8270
       p_paddr in _bfd_elf_make_section_from_shdr.
8271
       If this header has a p_paddr that disagrees
8272
       with the section lma, flag the p_paddr as
8273
       invalid.  */
8274
242
          if ((section->flags & SEC_LOAD) != 0)
8275
231
      seg_off = this_hdr->sh_offset - segment->p_offset;
8276
11
          else
8277
11
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8278
242
          if (section->lma * opb - segment->p_paddr != seg_off)
8279
8
      map->p_paddr_valid = false;
8280
242
        }
8281
280
      if (isec == section_count)
8282
75
        break;
8283
280
    }
8284
323
      }
8285
75
  }
8286
8287
136
      if (section_count == 0)
8288
61
  map->p_vaddr_offset = segment->p_vaddr / opb;
8289
75
      else if (map->p_paddr_valid)
8290
71
  {
8291
    /* Account for padding before the first section in the segment.  */
8292
71
    bfd_vma hdr_size = 0;
8293
71
    if (map->includes_filehdr)
8294
8
      hdr_size = iehdr->e_ehsize;
8295
71
    if (map->includes_phdrs)
8296
8
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8297
8298
71
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8299
71
         - (lowest_section ? lowest_section->lma : 0));
8300
71
  }
8301
8302
136
      map->count = section_count;
8303
136
      *pointer_to_map = map;
8304
136
      pointer_to_map = &map->next;
8305
136
    }
8306
8307
17
  elf_seg_map (obfd) = map_first;
8308
17
  return true;
8309
17
}
8310
8311
/* Copy private BFD data.  This copies or rewrites ELF program header
8312
   information.  */
8313
8314
static bool
8315
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8316
31
{
8317
31
  bfd_vma maxpagesize;
8318
8319
31
  if (elf_tdata (ibfd)->phdr == NULL)
8320
0
    return true;
8321
8322
31
  if (ibfd->xvec == obfd->xvec)
8323
31
    {
8324
      /* Check to see if any sections in the input BFD
8325
   covered by ELF program header have changed.  */
8326
31
      Elf_Internal_Phdr *segment;
8327
31
      asection * section;
8328
31
      asection * osec;
8329
31
      asection * prev;
8330
31
      unsigned int i, num_segments;
8331
31
      Elf_Internal_Shdr *this_hdr;
8332
31
      elf_backend_data *bed;
8333
8334
31
      bed = get_elf_backend_data (ibfd);
8335
8336
      /* Regenerate the segment map if p_paddr is set to 0.  */
8337
31
      if (bed->want_p_paddr_set_to_zero)
8338
0
  goto rewrite;
8339
8340
      /* Initialize the segment mark field.  */
8341
674
      for (section = obfd->sections; section != NULL;
8342
643
     section = section->next)
8343
643
  section->segment_mark = false;
8344
8345
31
      num_segments = elf_elfheader (ibfd)->e_phnum;
8346
31
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8347
196
     i < num_segments;
8348
165
     i++, segment++)
8349
179
  {
8350
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8351
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8352
       which severly confuses things, so always regenerate the segment
8353
       map in this case.  */
8354
179
    if (segment->p_paddr == 0
8355
40
        && segment->p_memsz == 0
8356
12
        && (segment->p_type == PT_INTERP
8357
12
      || segment->p_type == PT_DYNAMIC))
8358
0
      goto rewrite;
8359
8360
179
    for (section = ibfd->sections, prev = NULL;
8361
3.68k
         section != NULL; section = section->next)
8362
3.51k
      {
8363
        /* We mark the output section so that we know it comes
8364
     from the input BFD.  */
8365
3.51k
        osec = section->output_section;
8366
3.51k
        if (osec)
8367
3.51k
    osec->segment_mark = true;
8368
8369
        /* Check if this section is covered by the segment.  */
8370
3.51k
        this_hdr = &(elf_section_data(section)->this_hdr);
8371
3.51k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8372
389
    {
8373
      /* FIXME: Check if its output section is changed or
8374
         removed.  What else do we need to check?  */
8375
389
      if (osec == NULL
8376
389
          || section->flags != osec->flags
8377
389
          || section->lma != osec->lma
8378
389
          || section->vma != osec->vma
8379
389
          || section->size != osec->size
8380
389
          || section->rawsize != osec->rawsize
8381
389
          || section->alignment_power != osec->alignment_power)
8382
0
        goto rewrite;
8383
8384
      /* PR 31450: If this is an allocated section then make sure
8385
         that this section's vma to lma relationship is the same
8386
         as previous (allocated) section's.  */
8387
389
      if (prev != NULL
8388
263
          && section->flags & SEC_ALLOC
8389
235
          && section->lma - section->vma != prev->lma - prev->vma)
8390
14
        goto rewrite;
8391
8392
375
      if (section->flags & SEC_ALLOC)
8393
313
        prev = section;
8394
375
    }
8395
3.51k
      }
8396
179
  }
8397
8398
      /* Check to see if any output section do not come from the
8399
   input BFD.  */
8400
371
      for (section = obfd->sections; section != NULL;
8401
354
     section = section->next)
8402
354
  {
8403
354
    if (!section->segment_mark)
8404
0
      goto rewrite;
8405
354
    else
8406
354
      section->segment_mark = false;
8407
354
  }
8408
8409
17
      return copy_elf_program_header (ibfd, obfd);
8410
17
    }
8411
8412
14
 rewrite:
8413
14
  maxpagesize = 0;
8414
14
  if (ibfd->xvec == obfd->xvec)
8415
14
    {
8416
      /* When rewriting program header, set the output maxpagesize to
8417
   the maximum alignment of input PT_LOAD segments.  */
8418
14
      Elf_Internal_Phdr *segment;
8419
14
      unsigned int i;
8420
14
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8421
8422
14
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8423
132
     i < num_segments;
8424
118
     i++, segment++)
8425
118
  if (segment->p_type == PT_LOAD
8426
27
      && maxpagesize < segment->p_align)
8427
14
    {
8428
      /* PR 17512: file: f17299af.  */
8429
14
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8430
        /* xgettext:c-format */
8431
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8432
0
            PRIx64 " is too large"),
8433
0
          ibfd, (uint64_t) segment->p_align);
8434
14
      else
8435
14
        maxpagesize = segment->p_align;
8436
14
    }
8437
14
    }
8438
14
  if (maxpagesize == 0)
8439
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8440
8441
14
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8442
31
}
8443
8444
/* Copy private section information from input to output.  This function
8445
   is called both by objcopy where LINK_INFO is NULL, and ld where
8446
   LINK_INFO is non-NULL in the usual case but NULL for a special case
8447
   when dealing with LTO IR or cloning output sections.  */
8448
8449
bool
8450
_bfd_elf_copy_private_section_data (bfd *ibfd,
8451
            asection *isec,
8452
            bfd *obfd ATTRIBUTE_UNUSED,
8453
            asection *osec,
8454
            struct bfd_link_info *link_info)
8455
1.41k
{
8456
1.41k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8457
0
    return true;
8458
8459
1.41k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8460
1.41k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8461
1.41k
  if (link_info == NULL)
8462
1.41k
    {
8463
1.41k
      ohdr->sh_entsize = ihdr->sh_entsize;
8464
8465
1.41k
      if (ihdr->sh_type == SHT_SYMTAB
8466
1.41k
    || ihdr->sh_type == SHT_DYNSYM
8467
1.39k
    || ihdr->sh_type == SHT_GNU_verneed
8468
1.39k
    || ihdr->sh_type == SHT_GNU_verdef)
8469
26
  ohdr->sh_info = ihdr->sh_info;
8470
1.41k
    }
8471
8472
  /* If this is a known ABI section, ELF section type and flags may
8473
     have been set up when OSEC was created.  For normal sections we
8474
     allow the user to override the type and flags other than
8475
     SHF_MASKOS and SHF_MASKPROC.  */
8476
1.41k
  if (elf_section_type (osec) == SHT_PROGBITS
8477
1.08k
      || elf_section_type (osec) == SHT_NOTE
8478
1.00k
      || elf_section_type (osec) == SHT_NOBITS)
8479
436
    elf_section_type (osec) = SHT_NULL;
8480
8481
  /* For objcopy and relocatable link, copy the ELF section type from
8482
     the input file if the BFD section flags are the same.  (If they
8483
     are different the user may be doing something like
8484
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8485
     link allow some flags that the linker clears to differ.  */
8486
1.41k
  bool final_link = (link_info != NULL
8487
0
         && !bfd_link_relocatable (link_info));
8488
1.41k
  if (elf_section_type (osec) == SHT_NULL
8489
1.21k
      && (osec->flags == isec->flags
8490
0
    || (final_link
8491
0
        && ((osec->flags ^ isec->flags)
8492
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8493
1.21k
    elf_section_type (osec) = elf_section_type (isec);
8494
8495
1.41k
  elf_section_flags (osec) = elf_section_flags (isec);
8496
  /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
8497
     for relocatable link).  Same heuristic as there: If the BFD section flags
8498
     are different, assume --set-section-flags is in use for the section.
8499
8500
     FIXME: Is this correct for all OS/PROC specific flags?  */
8501
1.41k
  if (link_info != NULL || osec->flags != isec->flags)
8502
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8503
1.41k
  else
8504
1.41k
    {
8505
      /* Clear only flags which are set below or elsewhere.  */
8506
1.41k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8507
1.41k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8508
1.41k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8509
1.41k
            | SHF_COMPRESSED);
8510
1.41k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8511
55
  _bfd_error_handler
8512
55
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8513
55
     ibfd, isec,
8514
55
     (uint64_t) (elf_section_flags (osec)
8515
55
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8516
1.41k
    }
8517
8518
  /* Copy sh_info from input for mbind section.  */
8519
1.41k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8520
169
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8521
13
    elf_section_data (osec)->this_hdr.sh_info
8522
13
      = elf_section_data (isec)->this_hdr.sh_info;
8523
8524
  /* Set things up for objcopy and relocatable link.  The output
8525
     SHT_GROUP section will have its elf_next_in_group pointing back
8526
     to the input group members.  Ignore linker created group section.
8527
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8528
1.41k
  if ((link_info == NULL
8529
0
       || !link_info->resolve_section_groups)
8530
1.41k
      && (elf_sec_group (isec) == NULL
8531
75
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8532
1.41k
    {
8533
1.41k
      if (elf_section_flags (isec) & SHF_GROUP)
8534
97
  elf_section_flags (osec) |= SHF_GROUP;
8535
1.41k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8536
1.41k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8537
1.41k
    }
8538
8539
  /* If not decompress, preserve SHF_COMPRESSED.  */
8540
1.41k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8541
1.41k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8542
1.41k
         & SHF_COMPRESSED);
8543
8544
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8545
     don't use the output section of the linked-to section since it
8546
     may be NULL at this point.  */
8547
1.41k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8548
19
    {
8549
19
      ohdr->sh_flags |= SHF_LINK_ORDER;
8550
19
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8551
19
    }
8552
8553
1.41k
  osec->use_rela_p = isec->use_rela_p;
8554
8555
1.41k
  return true;
8556
1.41k
}
8557
8558
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8559
   necessary if we are removing either the SHT_GROUP section or any of
8560
   the group member sections.  DISCARDED is the value that a section's
8561
   output_section has if the section will be discarded, NULL when this
8562
   function is called from objcopy, bfd_abs_section_ptr when called
8563
   from the linker.  */
8564
8565
bool
8566
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8567
75
{
8568
75
  asection *isec;
8569
8570
1.46k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8571
1.38k
    if (elf_section_type (isec) == SHT_GROUP)
8572
28
      {
8573
28
  asection *first = elf_next_in_group (isec);
8574
28
  asection *s = first;
8575
28
  bfd_size_type removed = 0;
8576
8577
75
  while (s != NULL)
8578
75
    {
8579
      /* If this member section is being output but the
8580
         SHT_GROUP section is not, then clear the group info
8581
         set up by _bfd_elf_copy_private_section_data.  */
8582
75
      if (s->output_section != discarded
8583
75
    && isec->output_section == discarded)
8584
0
        {
8585
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8586
0
    elf_group_name (s->output_section) = NULL;
8587
0
        }
8588
75
      else
8589
75
        {
8590
75
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8591
75
    if (s->output_section == discarded
8592
0
        && isec->output_section != discarded)
8593
0
      {
8594
        /* Conversely, if the member section is not being
8595
           output but the SHT_GROUP section is, then adjust
8596
           its size.  */
8597
0
        removed += 4;
8598
0
        if (elf_sec->rel.hdr != NULL
8599
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8600
0
          removed += 4;
8601
0
        if (elf_sec->rela.hdr != NULL
8602
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8603
0
          removed += 4;
8604
0
      }
8605
75
    else
8606
75
      {
8607
        /* Also adjust for zero-sized relocation member
8608
           section.  */
8609
75
        if (elf_sec->rel.hdr != NULL
8610
0
      && elf_sec->rel.hdr->sh_size == 0)
8611
0
          removed += 4;
8612
75
        if (elf_sec->rela.hdr != NULL
8613
55
      && elf_sec->rela.hdr->sh_size == 0)
8614
0
          removed += 4;
8615
75
      }
8616
75
        }
8617
75
      s = elf_next_in_group (s);
8618
75
      if (s == first)
8619
28
        break;
8620
75
    }
8621
28
  if (removed != 0)
8622
0
    {
8623
0
      if (discarded != NULL)
8624
0
        {
8625
    /* If we've been called for ld -r, then we need to
8626
       adjust the input section size.  */
8627
0
    if (isec->rawsize == 0)
8628
0
      isec->rawsize = isec->size;
8629
0
    isec->size = isec->rawsize - removed;
8630
0
    if (isec->size <= 4)
8631
0
      {
8632
0
        isec->size = 0;
8633
0
        isec->flags |= SEC_EXCLUDE;
8634
0
      }
8635
0
        }
8636
0
      else if (isec->output_section != NULL)
8637
0
        {
8638
    /* Adjust the output section size when called from
8639
       objcopy. */
8640
0
    isec->output_section->size -= removed;
8641
0
    if (isec->output_section->size <= 4)
8642
0
      {
8643
0
        isec->output_section->size = 0;
8644
0
        isec->output_section->flags |= SEC_EXCLUDE;
8645
0
      }
8646
0
        }
8647
0
    }
8648
28
      }
8649
8650
75
  return true;
8651
75
}
8652
8653
/* Copy private header information.  */
8654
8655
bool
8656
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8657
75
{
8658
75
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8659
0
    return true;
8660
8661
  /* Copy over private BFD data if it has not already been copied.
8662
     This must be done here, rather than in the copy_private_bfd_data
8663
     entry point, because the latter is called after the section
8664
     contents have been set, which means that the program headers have
8665
     already been worked out.  */
8666
75
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8667
31
    {
8668
31
      if (! copy_private_bfd_data (ibfd, obfd))
8669
0
  return false;
8670
31
    }
8671
8672
75
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8673
75
}
8674
8675
/* Copy private symbol information.  If this symbol is in a section
8676
   which we did not map into a BFD section, try to map the section
8677
   index correctly.  We use special macro definitions for the mapped
8678
   section indices; these definitions are interpreted by the
8679
   swap_out_syms function.  */
8680
8681
6
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8682
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8683
46
#define MAP_STRTAB    (SHN_HIOS + 3)
8684
14
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8685
1
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8686
8687
bool
8688
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8689
           asymbol **isymarg,
8690
           bfd *obfd ATTRIBUTE_UNUSED,
8691
           asymbol **osymarg)
8692
21.1k
{
8693
21.1k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8694
0
    return true;
8695
8696
21.1k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8697
21.1k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8698
21.1k
  if (isym != NULL
8699
21.1k
      && isym->internal_elf_sym.st_shndx != 0
8700
1.06k
      && osym != NULL
8701
1.06k
      && bfd_is_abs_section (isym->symbol.section))
8702
501
    {
8703
501
      unsigned int shndx;
8704
8705
501
      shndx = isym->internal_elf_sym.st_shndx;
8706
501
      if (shndx == elf_onesymtab (ibfd))
8707
3
  shndx = MAP_ONESYMTAB;
8708
498
      else if (shndx == elf_dynsymtab (ibfd))
8709
0
  shndx = MAP_DYNSYMTAB;
8710
498
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8711
23
  shndx = MAP_STRTAB;
8712
475
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8713
12
  shndx = MAP_SHSTRTAB;
8714
463
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8715
1
  shndx = MAP_SYM_SHNDX;
8716
501
      osym->internal_elf_sym.st_shndx = shndx;
8717
501
    }
8718
8719
21.1k
  return true;
8720
21.1k
}
8721
8722
/* Swap out the symbols.  */
8723
8724
static bool
8725
swap_out_syms (bfd *abfd,
8726
         struct elf_strtab_hash **sttp,
8727
         int relocatable_p,
8728
         struct bfd_link_info *info)
8729
41
{
8730
41
  elf_backend_data *bed;
8731
41
  unsigned int symcount;
8732
41
  asymbol **syms;
8733
41
  struct elf_strtab_hash *stt;
8734
41
  Elf_Internal_Shdr *symtab_hdr;
8735
41
  Elf_Internal_Shdr *symtab_shndx_hdr;
8736
41
  Elf_Internal_Shdr *symstrtab_hdr;
8737
41
  struct elf_sym_strtab *symstrtab;
8738
41
  bfd_byte *outbound_syms;
8739
41
  bfd_byte *outbound_shndx;
8740
41
  unsigned long outbound_syms_index;
8741
41
  unsigned int idx;
8742
41
  unsigned int num_locals;
8743
41
  size_t amt;
8744
41
  bool name_local_sections;
8745
8746
41
  if (!elf_map_symbols (abfd, &num_locals))
8747
0
    return false;
8748
8749
  /* Dump out the symtabs.  */
8750
41
  stt = _bfd_elf_strtab_init ();
8751
41
  if (stt == NULL)
8752
0
    return false;
8753
8754
41
  bed = get_elf_backend_data (abfd);
8755
41
  symcount = bfd_get_symcount (abfd);
8756
41
  symtab_hdr = &elf_symtab_hdr (abfd);
8757
41
  symtab_hdr->sh_type = SHT_SYMTAB;
8758
41
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8759
41
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8760
41
  symtab_hdr->sh_info = num_locals + 1;
8761
41
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8762
8763
41
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8764
41
  symstrtab_hdr->sh_type = SHT_STRTAB;
8765
8766
  /* Allocate buffer to swap out the .strtab section.  */
8767
41
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8768
41
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8769
0
    {
8770
0
      bfd_set_error (bfd_error_no_memory);
8771
0
      _bfd_elf_strtab_free (stt);
8772
0
      return false;
8773
0
    }
8774
8775
41
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8776
41
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8777
0
    {
8778
0
    error_no_mem:
8779
0
      bfd_set_error (bfd_error_no_memory);
8780
0
    error_return:
8781
0
      free (symstrtab);
8782
0
      _bfd_elf_strtab_free (stt);
8783
0
      return false;
8784
0
    }
8785
41
  symtab_hdr->contents = outbound_syms;
8786
41
  outbound_syms_index = 0;
8787
8788
41
  outbound_shndx = NULL;
8789
8790
41
  if (elf_symtab_shndx_list (abfd))
8791
0
    {
8792
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8793
0
      if (symtab_shndx_hdr->sh_name != 0)
8794
0
  {
8795
0
    if (_bfd_mul_overflow (symcount + 1,
8796
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8797
0
      goto error_no_mem;
8798
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8799
0
    if (outbound_shndx == NULL)
8800
0
      goto error_return;
8801
8802
0
    symtab_shndx_hdr->contents = outbound_shndx;
8803
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8804
0
    symtab_shndx_hdr->sh_size = amt;
8805
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8806
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8807
0
  }
8808
      /* FIXME: What about any other headers in the list ?  */
8809
0
    }
8810
8811
  /* Now generate the data (for "contents").  */
8812
41
  {
8813
    /* Fill in zeroth symbol and swap it out.  */
8814
41
    Elf_Internal_Sym sym;
8815
41
    sym.st_name = 0;
8816
41
    sym.st_value = 0;
8817
41
    sym.st_size = 0;
8818
41
    sym.st_info = 0;
8819
41
    sym.st_other = 0;
8820
41
    sym.st_shndx = SHN_UNDEF;
8821
41
    sym.st_target_internal = 0;
8822
41
    symstrtab[outbound_syms_index].sym = sym;
8823
41
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8824
41
    outbound_syms_index++;
8825
41
  }
8826
8827
41
  name_local_sections
8828
41
    = (bed->elf_backend_name_local_section_symbols
8829
5
       && bed->elf_backend_name_local_section_symbols (abfd));
8830
8831
41
  syms = bfd_get_outsymbols (abfd);
8832
1.40k
  for (idx = 0; idx < symcount; idx++)
8833
1.36k
    {
8834
1.36k
      Elf_Internal_Sym sym;
8835
8836
1.36k
      flagword flags = syms[idx]->flags;
8837
1.36k
      if (!name_local_sections
8838
1.26k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8839
109
  {
8840
    /* Local section symbols have no name.  */
8841
109
    sym.st_name = 0;
8842
109
  }
8843
1.25k
      else
8844
1.25k
  {
8845
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8846
       to get the final offset for st_name.  */
8847
1.25k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8848
1.25k
    if (stridx == (size_t) -1)
8849
0
      goto error_return;
8850
1.25k
    sym.st_name = stridx;
8851
1.25k
  }
8852
8853
1.36k
      bfd_vma value = syms[idx]->value;
8854
1.36k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8855
1.36k
      asection *sec = syms[idx]->section;
8856
8857
1.36k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8858
3
  {
8859
    /* ELF common symbols put the alignment into the `value' field,
8860
       and the size into the `size' field.  This is backwards from
8861
       how BFD handles it, so reverse it here.  */
8862
3
    sym.st_size = value;
8863
3
    if (type_ptr == NULL
8864
3
        || type_ptr->internal_elf_sym.st_value == 0)
8865
2
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8866
1
    else
8867
1
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8868
3
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8869
3
  }
8870
1.36k
      else
8871
1.36k
  {
8872
1.36k
    unsigned int shndx;
8873
8874
1.36k
    if (sec->output_section)
8875
1.36k
      {
8876
1.36k
        value += sec->output_offset;
8877
1.36k
        sec = sec->output_section;
8878
1.36k
      }
8879
8880
    /* Don't add in the section vma for relocatable output.  */
8881
1.36k
    if (! relocatable_p)
8882
737
      value += sec->vma;
8883
1.36k
    sym.st_value = value;
8884
1.36k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8885
8886
1.36k
    if (bfd_is_abs_section (sec)
8887
434
        && type_ptr != NULL
8888
434
        && type_ptr->internal_elf_sym.st_shndx != 0)
8889
434
      {
8890
        /* This symbol is in a real ELF section which we did
8891
     not create as a BFD section.  Undo the mapping done
8892
     by copy_private_symbol_data.  */
8893
434
        shndx = type_ptr->internal_elf_sym.st_shndx;
8894
434
        switch (shndx)
8895
434
    {
8896
3
    case MAP_ONESYMTAB:
8897
3
      shndx = elf_onesymtab (abfd);
8898
3
      break;
8899
0
    case MAP_DYNSYMTAB:
8900
0
      shndx = elf_dynsymtab (abfd);
8901
0
      break;
8902
23
    case MAP_STRTAB:
8903
23
      shndx = elf_strtab_sec (abfd);
8904
23
      break;
8905
2
    case MAP_SHSTRTAB:
8906
2
      shndx = elf_shstrtab_sec (abfd);
8907
2
      break;
8908
0
    case MAP_SYM_SHNDX:
8909
0
      if (elf_symtab_shndx_list (abfd))
8910
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8911
0
      break;
8912
0
    case SHN_COMMON:
8913
13
    case SHN_ABS:
8914
13
      shndx = SHN_ABS;
8915
13
      break;
8916
393
    default:
8917
393
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8918
1
        {
8919
1
          if (bed->symbol_section_index)
8920
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8921
          /* Otherwise just leave the index alone.  */
8922
1
        }
8923
392
      else
8924
392
        {
8925
392
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8926
2
      _bfd_error_handler (_("%pB: \
8927
2
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8928
2
            abfd, shndx);
8929
392
          shndx = SHN_ABS;
8930
392
        }
8931
393
      break;
8932
434
    }
8933
434
      }
8934
927
    else
8935
927
      {
8936
927
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8937
8938
927
        if (shndx == SHN_BAD)
8939
0
    {
8940
0
      asection *sec2;
8941
8942
      /* Writing this would be a hell of a lot easier if
8943
         we had some decent documentation on bfd, and
8944
         knew what to expect of the library, and what to
8945
         demand of applications.  For example, it
8946
         appears that `objcopy' might not set the
8947
         section of a symbol to be a section that is
8948
         actually in the output file.  */
8949
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8950
0
      if (sec2 != NULL)
8951
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8952
0
      if (shndx == SHN_BAD)
8953
0
        {
8954
          /* xgettext:c-format */
8955
0
          _bfd_error_handler
8956
0
      (_("unable to find equivalent output section"
8957
0
         " for symbol '%s' from section '%s'"),
8958
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8959
0
       sec->name);
8960
0
          bfd_set_error (bfd_error_invalid_operation);
8961
0
          goto error_return;
8962
0
        }
8963
0
    }
8964
927
      }
8965
8966
1.36k
    sym.st_shndx = shndx;
8967
1.36k
  }
8968
8969
1.36k
      int type;
8970
1.36k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8971
3
  type = STT_TLS;
8972
1.36k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8973
6
  type = STT_GNU_IFUNC;
8974
1.35k
      else if ((flags & BSF_FUNCTION) != 0)
8975
118
  type = STT_FUNC;
8976
1.23k
      else if ((flags & BSF_OBJECT) != 0)
8977
173
  type = STT_OBJECT;
8978
1.06k
      else if ((flags & BSF_RELC) != 0)
8979
3
  type = STT_RELC;
8980
1.06k
      else if ((flags & BSF_SRELC) != 0)
8981
127
  type = STT_SRELC;
8982
934
      else
8983
934
  type = STT_NOTYPE;
8984
8985
1.36k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8986
0
  type = STT_TLS;
8987
8988
      /* Processor-specific types.  */
8989
1.36k
      if (type_ptr != NULL
8990
1.36k
    && bed->elf_backend_get_symbol_type)
8991
0
  type = ((*bed->elf_backend_get_symbol_type)
8992
0
    (&type_ptr->internal_elf_sym, type));
8993
8994
1.36k
      if (flags & BSF_SECTION_SYM)
8995
114
  {
8996
114
    if (flags & BSF_GLOBAL)
8997
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8998
114
    else
8999
114
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9000
114
  }
9001
1.25k
      else if (bfd_is_com_section (syms[idx]->section))
9002
3
  {
9003
3
    if (type != STT_TLS)
9004
3
      {
9005
3
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9006
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9007
0
      ? STT_COMMON : STT_OBJECT);
9008
3
        else
9009
3
    type = ((flags & BSF_ELF_COMMON) != 0
9010
3
      ? STT_COMMON : STT_OBJECT);
9011
3
      }
9012
3
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9013
3
  }
9014
1.24k
      else if (bfd_is_und_section (syms[idx]->section))
9015
617
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9016
1.24k
            ? STB_WEAK
9017
1.24k
            : STB_GLOBAL),
9018
1.24k
           type);
9019
630
      else if (flags & BSF_FILE)
9020
13
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9021
617
      else
9022
617
  {
9023
617
    int bind = STB_LOCAL;
9024
9025
617
    if (flags & BSF_LOCAL)
9026
414
      bind = STB_LOCAL;
9027
203
    else if (flags & BSF_GNU_UNIQUE)
9028
5
      bind = STB_GNU_UNIQUE;
9029
198
    else if (flags & BSF_WEAK)
9030
8
      bind = STB_WEAK;
9031
190
    else if (flags & BSF_GLOBAL)
9032
163
      bind = STB_GLOBAL;
9033
9034
617
    sym.st_info = ELF_ST_INFO (bind, type);
9035
617
  }
9036
9037
1.36k
      if (type_ptr != NULL)
9038
1.36k
  {
9039
1.36k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9040
1.36k
    sym.st_target_internal
9041
1.36k
      = type_ptr->internal_elf_sym.st_target_internal;
9042
1.36k
  }
9043
0
      else
9044
0
  {
9045
0
    sym.st_other = 0;
9046
0
    sym.st_target_internal = 0;
9047
0
  }
9048
9049
1.36k
      symstrtab[outbound_syms_index].sym = sym;
9050
1.36k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9051
1.36k
      outbound_syms_index++;
9052
1.36k
    }
9053
9054
  /* Finalize the .strtab section.  */
9055
41
  _bfd_elf_strtab_finalize (stt);
9056
9057
  /* Swap out the .strtab section.  */
9058
1.44k
  for (idx = 0; idx < outbound_syms_index; idx++)
9059
1.40k
    {
9060
1.40k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9061
1.40k
      if (elfsym->sym.st_name != 0)
9062
378
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9063
378
                  elfsym->sym.st_name);
9064
1.40k
      if (info && info->callbacks->ctf_new_symbol)
9065
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9066
0
           &elfsym->sym);
9067
9068
      /* Inform the linker of the addition of this symbol.  */
9069
9070
1.40k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9071
1.40k
             (outbound_syms
9072
1.40k
        + (elfsym->dest_index
9073
1.40k
           * bed->s->sizeof_sym)),
9074
1.40k
             NPTR_ADD (outbound_shndx,
9075
1.40k
           (elfsym->dest_index
9076
1.40k
            * sizeof (Elf_External_Sym_Shndx))));
9077
1.40k
    }
9078
41
  free (symstrtab);
9079
9080
41
  *sttp = stt;
9081
41
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9082
41
  symstrtab_hdr->sh_type = SHT_STRTAB;
9083
41
  symstrtab_hdr->sh_addralign = 1;
9084
9085
41
  return true;
9086
41
}
9087
9088
/* Return the number of bytes required to hold the symtab vector.
9089
9090
   Note that we base it on the count plus 1, since we will null terminate
9091
   the vector allocated based on this size.  However, the ELF symbol table
9092
   always has a dummy entry as symbol #0, so it ends up even.  */
9093
9094
long
9095
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9096
5.62k
{
9097
5.62k
  bfd_size_type symcount;
9098
5.62k
  long symtab_size;
9099
5.62k
  Elf_Internal_Shdr *hdr = &elf_symtab_hdr (abfd);
9100
9101
5.62k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9102
5.62k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9103
0
    {
9104
0
      bfd_set_error (bfd_error_file_too_big);
9105
0
      return -1;
9106
0
    }
9107
5.62k
  symtab_size = symcount * (sizeof (asymbol *));
9108
5.62k
  if (symcount == 0)
9109
348
    symtab_size = sizeof (asymbol *);
9110
5.27k
  else if (!bfd_write_p (abfd))
9111
5.27k
    {
9112
5.27k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9113
9114
5.27k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9115
176
  {
9116
176
    bfd_set_error (bfd_error_file_truncated);
9117
176
    return -1;
9118
176
  }
9119
5.27k
    }
9120
9121
5.44k
  return symtab_size;
9122
5.62k
}
9123
9124
long
9125
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9126
8.23k
{
9127
8.23k
  bfd_size_type symcount;
9128
8.23k
  long symtab_size;
9129
8.23k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9130
9131
8.23k
  if (elf_dynsymtab (abfd) == 0)
9132
7.91k
    {
9133
      /* Check if there is dynamic symbol table.  */
9134
7.91k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9135
7.91k
      if (symcount)
9136
8
  goto compute_symtab_size;
9137
9138
7.91k
      bfd_set_error (bfd_error_invalid_operation);
9139
7.91k
      return -1;
9140
7.91k
    }
9141
9142
320
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9143
320
  if (symcount > LONG_MAX / sizeof (asymbol *))
9144
0
    {
9145
0
      bfd_set_error (bfd_error_file_too_big);
9146
0
      return -1;
9147
0
    }
9148
9149
328
 compute_symtab_size:
9150
328
  symtab_size = symcount * (sizeof (asymbol *));
9151
328
  if (symcount == 0)
9152
5
    symtab_size = sizeof (asymbol *);
9153
323
  else if (!bfd_write_p (abfd))
9154
323
    {
9155
323
      ufile_ptr filesize = bfd_get_file_size (abfd);
9156
9157
323
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9158
22
  {
9159
22
    bfd_set_error (bfd_error_file_truncated);
9160
22
    return -1;
9161
22
  }
9162
323
    }
9163
9164
306
  return symtab_size;
9165
328
}
9166
9167
long
9168
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9169
16.5k
{
9170
16.5k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9171
15.2k
    {
9172
      /* Sanity check reloc section size.  */
9173
15.2k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9174
9175
15.2k
      if (filesize != 0)
9176
15.2k
  {
9177
15.2k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9178
15.2k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9179
15.2k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9180
9181
15.2k
    if (rel_size + rela_size > filesize
9182
14.8k
        || rel_size + rela_size < rel_size)
9183
410
      {
9184
410
        bfd_set_error (bfd_error_file_truncated);
9185
410
        return -1;
9186
410
      }
9187
15.2k
  }
9188
15.2k
    }
9189
9190
#if SIZEOF_LONG == SIZEOF_INT
9191
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9192
    {
9193
      bfd_set_error (bfd_error_file_too_big);
9194
      return -1;
9195
    }
9196
#endif
9197
16.1k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9198
16.5k
}
9199
9200
/* Canonicalize the relocs.  */
9201
9202
long
9203
_bfd_elf_canonicalize_reloc (bfd *abfd,
9204
           sec_ptr section,
9205
           arelent **relptr,
9206
           asymbol **symbols)
9207
16.0k
{
9208
16.0k
  arelent *tblptr;
9209
16.0k
  unsigned int i;
9210
16.0k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9211
9212
16.0k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9213
2.51k
    return -1;
9214
9215
13.5k
  tblptr = section->relocation;
9216
133k
  for (i = 0; i < section->reloc_count; i++)
9217
119k
    *relptr++ = tblptr++;
9218
9219
13.5k
  *relptr = NULL;
9220
9221
13.5k
  return section->reloc_count;
9222
16.0k
}
9223
9224
long
9225
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9226
5.44k
{
9227
5.44k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9228
5.44k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9229
9230
5.44k
  if (symcount >= 0)
9231
4.62k
    abfd->symcount = symcount;
9232
5.44k
  return symcount;
9233
5.44k
}
9234
9235
long
9236
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9237
              asymbol **allocation)
9238
181
{
9239
181
  elf_backend_data *bed = get_elf_backend_data (abfd);
9240
181
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9241
9242
181
  if (symcount >= 0)
9243
124
    abfd->dynsymcount = symcount;
9244
181
  return symcount;
9245
181
}
9246
9247
/* Return the size required for the dynamic reloc entries.  Any loadable
9248
   section that was actually installed in the BFD, and has type SHT_REL
9249
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9250
   dynamic reloc section.  */
9251
9252
long
9253
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9254
8.01k
{
9255
8.01k
  bfd_size_type count, ext_rel_size;
9256
8.01k
  asection *s;
9257
9258
8.01k
  if (elf_dynsymtab (abfd) == 0)
9259
7.80k
    {
9260
7.80k
      bfd_set_error (bfd_error_invalid_operation);
9261
7.80k
      return -1;
9262
7.80k
    }
9263
9264
205
  count = 1;
9265
205
  ext_rel_size = 0;
9266
5.45k
  for (s = abfd->sections; s != NULL; s = s->next)
9267
5.24k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9268
722
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9269
700
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9270
296
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9271
296
      {
9272
296
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9273
296
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9274
0
    {
9275
0
      bfd_set_error (bfd_error_file_truncated);
9276
0
      return -1;
9277
0
    }
9278
296
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9279
296
  if (count > LONG_MAX / sizeof (arelent *))
9280
0
    {
9281
0
      bfd_set_error (bfd_error_file_too_big);
9282
0
      return -1;
9283
0
    }
9284
296
      }
9285
205
  if (count > 1 && !bfd_write_p (abfd))
9286
198
    {
9287
      /* Sanity check reloc section sizes.  */
9288
198
      ufile_ptr filesize = bfd_get_file_size (abfd);
9289
198
      if (filesize != 0 && ext_rel_size > filesize)
9290
3
  {
9291
3
    bfd_set_error (bfd_error_file_truncated);
9292
3
    return -1;
9293
3
  }
9294
198
    }
9295
202
  return count * sizeof (arelent *);
9296
205
}
9297
9298
/* Canonicalize the dynamic relocation entries.  Note that we return the
9299
   dynamic relocations as a single block, although they are actually
9300
   associated with particular sections; the interface, which was
9301
   designed for SunOS style shared libraries, expects that there is only
9302
   one set of dynamic relocs.  Any loadable section that was actually
9303
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9304
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9305
9306
long
9307
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9308
             arelent **storage,
9309
             asymbol **syms)
9310
180
{
9311
180
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9312
180
  asection *s;
9313
180
  long ret;
9314
9315
180
  if (elf_dynsymtab (abfd) == 0)
9316
0
    {
9317
0
      bfd_set_error (bfd_error_invalid_operation);
9318
0
      return -1;
9319
0
    }
9320
9321
180
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9322
180
  ret = 0;
9323
4.52k
  for (s = abfd->sections; s != NULL; s = s->next)
9324
4.36k
    {
9325
4.36k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9326
629
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9327
611
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9328
259
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9329
259
  {
9330
259
    arelent *p;
9331
259
    long count, i;
9332
9333
259
    if (! (*slurp_relocs) (abfd, s, syms, true))
9334
14
      return -1;
9335
245
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9336
245
    p = s->relocation;
9337
2.21k
    for (i = 0; i < count; i++)
9338
1.97k
      *storage++ = p++;
9339
245
    ret += count;
9340
245
  }
9341
4.36k
    }
9342
9343
166
  *storage = NULL;
9344
9345
166
  return ret;
9346
180
}
9347

9348
/* Read in the version information.  */
9349
9350
bool
9351
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9352
226
{
9353
226
  bfd_byte *contents = NULL;
9354
226
  unsigned int freeidx = 0;
9355
226
  size_t amt;
9356
226
  void *contents_addr = NULL;
9357
226
  size_t contents_size = 0;
9358
9359
226
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9360
211
    {
9361
211
      Elf_Internal_Shdr *hdr;
9362
211
      Elf_External_Verneed *everneed;
9363
211
      Elf_Internal_Verneed *iverneed;
9364
211
      unsigned int i;
9365
211
      bfd_byte *contents_end;
9366
211
      size_t verneed_count;
9367
211
      size_t verneed_size;
9368
9369
211
      if (elf_tdata (abfd)->dt_verneed != NULL)
9370
2
  {
9371
2
    hdr = NULL;
9372
2
    contents = elf_tdata (abfd)->dt_verneed;
9373
2
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9374
2
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9375
2
  }
9376
209
      else
9377
209
  {
9378
209
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9379
9380
209
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9381
6
      {
9382
68
      error_return_bad_verref:
9383
68
        _bfd_error_handler
9384
68
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9385
68
        bfd_set_error (bfd_error_bad_value);
9386
85
      error_return_verref:
9387
85
        elf_tdata (abfd)->verref = NULL;
9388
85
        elf_tdata (abfd)->cverrefs = 0;
9389
85
        goto error_return;
9390
68
      }
9391
9392
203
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9393
2
      goto error_return_verref;
9394
201
    contents_size = hdr->sh_size;
9395
201
    contents = _bfd_mmap_temporary (abfd, contents_size,
9396
201
            &contents_addr, &contents_size);
9397
201
    if (contents == NULL)
9398
15
      goto error_return_verref;
9399
9400
186
    verneed_size = hdr->sh_size;
9401
186
    verneed_count = hdr->sh_info;
9402
186
  }
9403
9404
188
      if (_bfd_mul_overflow (verneed_count,
9405
188
           sizeof (Elf_Internal_Verneed), &amt))
9406
0
  {
9407
0
    bfd_set_error (bfd_error_file_too_big);
9408
0
    goto error_return_verref;
9409
0
  }
9410
188
      if (amt == 0)
9411
0
  goto error_return_verref;
9412
188
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9413
188
      if (elf_tdata (abfd)->verref == NULL)
9414
0
  goto error_return_verref;
9415
9416
188
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9417
188
      == sizeof (Elf_External_Vernaux));
9418
188
      contents_end = (contents + verneed_size
9419
188
          - sizeof (Elf_External_Verneed));
9420
188
      everneed = (Elf_External_Verneed *) contents;
9421
188
      iverneed = elf_tdata (abfd)->verref;
9422
191
      for (i = 0; i < verneed_count; i++, iverneed++)
9423
188
  {
9424
188
    Elf_External_Vernaux *evernaux;
9425
188
    Elf_Internal_Vernaux *ivernaux;
9426
188
    unsigned int j;
9427
9428
188
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9429
9430
188
    iverneed->vn_bfd = abfd;
9431
9432
188
    if (elf_use_dt_symtab_p (abfd))
9433
2
      {
9434
2
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9435
1
    iverneed->vn_filename
9436
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9437
1
        else
9438
1
    iverneed->vn_filename = NULL;
9439
2
      }
9440
186
    else if (hdr == NULL)
9441
0
      goto error_return_bad_verref;
9442
186
    else
9443
186
      iverneed->vn_filename
9444
186
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9445
186
             iverneed->vn_file);
9446
188
    if (iverneed->vn_filename == NULL)
9447
34
      goto error_return_bad_verref;
9448
9449
154
    if (iverneed->vn_cnt == 0)
9450
18
      iverneed->vn_auxptr = NULL;
9451
136
    else
9452
136
      {
9453
136
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9454
136
             sizeof (Elf_Internal_Vernaux), &amt))
9455
0
    {
9456
0
      bfd_set_error (bfd_error_file_too_big);
9457
0
      goto error_return_verref;
9458
0
    }
9459
136
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9460
136
    bfd_alloc (abfd, amt);
9461
136
        if (iverneed->vn_auxptr == NULL)
9462
0
    goto error_return_verref;
9463
136
      }
9464
9465
154
    if (iverneed->vn_aux
9466
154
        > (size_t) (contents_end - (bfd_byte *) everneed))
9467
8
      goto error_return_bad_verref;
9468
9469
146
    evernaux = ((Elf_External_Vernaux *)
9470
146
          ((bfd_byte *) everneed + iverneed->vn_aux));
9471
146
    ivernaux = iverneed->vn_auxptr;
9472
221
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9473
208
      {
9474
208
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9475
9476
208
        if (elf_use_dt_symtab_p (abfd))
9477
0
    {
9478
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9479
0
        ivernaux->vna_nodename
9480
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9481
0
      else
9482
0
        ivernaux->vna_nodename = NULL;
9483
0
    }
9484
208
        else if (hdr == NULL)
9485
0
    goto error_return_bad_verref;
9486
208
        else
9487
208
    ivernaux->vna_nodename
9488
208
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9489
208
                 ivernaux->vna_name);
9490
208
        if (ivernaux->vna_nodename == NULL)
9491
5
    goto error_return_bad_verref;
9492
9493
203
        if (ivernaux->vna_other > freeidx)
9494
123
    freeidx = ivernaux->vna_other;
9495
9496
203
        ivernaux->vna_nextptr = NULL;
9497
203
        if (ivernaux->vna_next == 0)
9498
116
    {
9499
116
      iverneed->vn_cnt = j + 1;
9500
116
      break;
9501
116
    }
9502
87
        if (j + 1 < iverneed->vn_cnt)
9503
83
    ivernaux->vna_nextptr = ivernaux + 1;
9504
9505
87
        if (ivernaux->vna_next
9506
87
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9507
12
    goto error_return_bad_verref;
9508
9509
75
        evernaux = ((Elf_External_Vernaux *)
9510
75
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9511
75
      }
9512
9513
129
    iverneed->vn_nextref = NULL;
9514
129
    if (iverneed->vn_next == 0)
9515
123
      break;
9516
6
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9517
0
      iverneed->vn_nextref = iverneed + 1;
9518
9519
6
    if (iverneed->vn_next
9520
6
        > (size_t) (contents_end - (bfd_byte *) everneed))
9521
3
      goto error_return_bad_verref;
9522
9523
3
    everneed = ((Elf_External_Verneed *)
9524
3
          ((bfd_byte *) everneed + iverneed->vn_next));
9525
3
  }
9526
126
      elf_tdata (abfd)->cverrefs = i;
9527
9528
126
      if (contents != elf_tdata (abfd)->dt_verneed)
9529
125
  _bfd_munmap_temporary (contents_addr, contents_size);
9530
126
      contents = NULL;
9531
126
      contents_addr = NULL;
9532
126
    }
9533
9534
141
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9535
15
    {
9536
15
      Elf_Internal_Shdr *hdr;
9537
15
      Elf_External_Verdef *everdef;
9538
15
      Elf_Internal_Verdef *iverdef;
9539
15
      Elf_Internal_Verdef *iverdefarr;
9540
15
      Elf_Internal_Verdef iverdefmem;
9541
15
      unsigned int i;
9542
15
      unsigned int maxidx;
9543
15
      bfd_byte *contents_end_def, *contents_end_aux;
9544
15
      size_t verdef_count;
9545
15
      size_t verdef_size;
9546
9547
15
      if (elf_tdata (abfd)->dt_verdef != NULL)
9548
0
  {
9549
0
    hdr = NULL;
9550
0
    contents = elf_tdata (abfd)->dt_verdef;
9551
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9552
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9553
0
  }
9554
15
      else
9555
15
  {
9556
15
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9557
9558
15
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9559
1
      {
9560
9
      error_return_bad_verdef:
9561
9
        _bfd_error_handler
9562
9
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9563
9
        bfd_set_error (bfd_error_bad_value);
9564
13
      error_return_verdef:
9565
13
        elf_tdata (abfd)->verdef = NULL;
9566
13
        elf_tdata (abfd)->cverdefs = 0;
9567
13
        goto error_return;
9568
9
      }
9569
9570
14
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9571
1
      goto error_return_verdef;
9572
13
    contents_size = hdr->sh_size;
9573
13
    contents = _bfd_mmap_temporary (abfd, contents_size,
9574
13
            &contents_addr, &contents_size);
9575
13
    if (contents == NULL)
9576
3
      goto error_return_verdef;
9577
9578
10
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9579
10
          >= sizeof (Elf_External_Verdaux));
9580
9581
10
    verdef_count = hdr->sh_info;
9582
10
    verdef_size = hdr->sh_size;
9583
10
  }
9584
9585
10
      contents_end_def = (contents + verdef_size
9586
10
        - sizeof (Elf_External_Verdef));
9587
10
      contents_end_aux = (contents + verdef_size
9588
10
        - sizeof (Elf_External_Verdaux));
9589
9590
      /* We know the number of entries in the section but not the maximum
9591
   index.  Therefore we have to run through all entries and find
9592
   the maximum.  */
9593
10
      everdef = (Elf_External_Verdef *) contents;
9594
10
      maxidx = 0;
9595
15
      for (i = 0; i < verdef_count; ++i)
9596
12
  {
9597
12
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9598
9599
12
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9600
1
      goto error_return_bad_verdef;
9601
11
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9602
10
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9603
9604
11
    if (iverdefmem.vd_next == 0)
9605
5
      break;
9606
9607
6
    if (iverdefmem.vd_next
9608
6
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9609
1
      goto error_return_bad_verdef;
9610
9611
5
    everdef = ((Elf_External_Verdef *)
9612
5
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9613
5
  }
9614
9615
8
      if (default_imported_symver)
9616
0
  {
9617
0
    if (freeidx > maxidx)
9618
0
      maxidx = ++freeidx;
9619
0
    else
9620
0
      freeidx = ++maxidx;
9621
0
  }
9622
8
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9623
0
  {
9624
0
    bfd_set_error (bfd_error_file_too_big);
9625
0
    goto error_return_verdef;
9626
0
  }
9627
9628
8
      if (amt == 0)
9629
0
  goto error_return_verdef;
9630
8
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9631
8
      if (elf_tdata (abfd)->verdef == NULL)
9632
0
  goto error_return_verdef;
9633
9634
8
      elf_tdata (abfd)->cverdefs = maxidx;
9635
9636
8
      everdef = (Elf_External_Verdef *) contents;
9637
8
      iverdefarr = elf_tdata (abfd)->verdef;
9638
10
      for (i = 0; i < verdef_count; ++i)
9639
9
  {
9640
9
    Elf_External_Verdaux *everdaux;
9641
9
    Elf_Internal_Verdaux *iverdaux;
9642
9
    unsigned int j;
9643
9644
9
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9645
9646
9
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9647
0
      goto error_return_bad_verdef;
9648
9649
9
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9650
9
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9651
9652
9
    iverdef->vd_bfd = abfd;
9653
9654
9
    if (iverdef->vd_cnt == 0)
9655
4
      iverdef->vd_auxptr = NULL;
9656
5
    else
9657
5
      {
9658
5
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9659
5
             sizeof (Elf_Internal_Verdaux), &amt))
9660
0
    {
9661
0
      bfd_set_error (bfd_error_file_too_big);
9662
0
      goto error_return_verdef;
9663
0
    }
9664
5
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9665
5
    bfd_alloc (abfd, amt);
9666
5
        if (iverdef->vd_auxptr == NULL)
9667
0
    goto error_return_verdef;
9668
5
      }
9669
9670
9
    if (iverdef->vd_aux
9671
9
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9672
1
      goto error_return_bad_verdef;
9673
9674
8
    everdaux = ((Elf_External_Verdaux *)
9675
8
          ((bfd_byte *) everdef + iverdef->vd_aux));
9676
8
    iverdaux = iverdef->vd_auxptr;
9677
8
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9678
5
      {
9679
5
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9680
9681
5
        if (elf_use_dt_symtab_p (abfd))
9682
0
    {
9683
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9684
0
        iverdaux->vda_nodename
9685
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9686
0
      else
9687
0
        iverdaux->vda_nodename = NULL;
9688
0
    }
9689
5
        else
9690
5
    iverdaux->vda_nodename
9691
5
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9692
5
                 iverdaux->vda_name);
9693
5
        if (iverdaux->vda_nodename == NULL)
9694
3
    goto error_return_bad_verdef;
9695
9696
2
        iverdaux->vda_nextptr = NULL;
9697
2
        if (iverdaux->vda_next == 0)
9698
0
    {
9699
0
      iverdef->vd_cnt = j + 1;
9700
0
      break;
9701
0
    }
9702
2
        if (j + 1 < iverdef->vd_cnt)
9703
1
    iverdaux->vda_nextptr = iverdaux + 1;
9704
9705
2
        if (iverdaux->vda_next
9706
2
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9707
2
    goto error_return_bad_verdef;
9708
9709
0
        everdaux = ((Elf_External_Verdaux *)
9710
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9711
0
      }
9712
9713
3
    iverdef->vd_nodename = NULL;
9714
3
    if (iverdef->vd_cnt)
9715
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9716
9717
3
    iverdef->vd_nextdef = NULL;
9718
3
    if (iverdef->vd_next == 0)
9719
1
      break;
9720
2
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9721
1
      iverdef->vd_nextdef = iverdef + 1;
9722
9723
2
    everdef = ((Elf_External_Verdef *)
9724
2
         ((bfd_byte *) everdef + iverdef->vd_next));
9725
2
  }
9726
9727
2
      if (contents != elf_tdata (abfd)->dt_verdef)
9728
2
  _bfd_munmap_temporary (contents_addr, contents_size);
9729
2
      contents = NULL;
9730
2
      contents_addr = NULL;
9731
2
    }
9732
126
  else if (default_imported_symver)
9733
0
    {
9734
0
      if (freeidx < 3)
9735
0
  freeidx = 3;
9736
0
      else
9737
0
  freeidx++;
9738
9739
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9740
0
  {
9741
0
    bfd_set_error (bfd_error_file_too_big);
9742
0
    goto error_return;
9743
0
  }
9744
0
      if (amt == 0)
9745
0
  goto error_return;
9746
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9747
0
      if (elf_tdata (abfd)->verdef == NULL)
9748
0
  goto error_return;
9749
9750
0
      elf_tdata (abfd)->cverdefs = freeidx;
9751
0
    }
9752
9753
  /* Create a default version based on the soname.  */
9754
128
  if (default_imported_symver)
9755
0
    {
9756
0
      Elf_Internal_Verdef *iverdef;
9757
0
      Elf_Internal_Verdaux *iverdaux;
9758
9759
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9760
9761
0
      iverdef->vd_version = VER_DEF_CURRENT;
9762
0
      iverdef->vd_flags = 0;
9763
0
      iverdef->vd_ndx = freeidx;
9764
0
      iverdef->vd_cnt = 1;
9765
9766
0
      iverdef->vd_bfd = abfd;
9767
9768
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9769
0
      if (iverdef->vd_nodename == NULL)
9770
0
  goto error_return_verdef;
9771
0
      iverdef->vd_nextdef = NULL;
9772
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9773
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9774
0
      if (iverdef->vd_auxptr == NULL)
9775
0
  goto error_return_verdef;
9776
9777
0
      iverdaux = iverdef->vd_auxptr;
9778
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9779
0
    }
9780
9781
128
  return true;
9782
9783
98
 error_return:
9784
98
  if (contents != elf_tdata (abfd)->dt_verneed
9785
69
      && contents != elf_tdata (abfd)->dt_verdef)
9786
69
    _bfd_munmap_temporary (contents_addr, contents_size);
9787
98
  return false;
9788
128
}
9789

9790
asymbol *
9791
_bfd_elf_make_empty_symbol (bfd *abfd)
9792
1.26M
{
9793
1.26M
  elf_symbol_type *newsym;
9794
9795
1.26M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9796
1.26M
  if (!newsym)
9797
0
    return NULL;
9798
1.26M
  newsym->symbol.the_bfd = abfd;
9799
1.26M
  return &newsym->symbol;
9800
1.26M
}
9801
9802
void
9803
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9804
        asymbol *symbol,
9805
        symbol_info *ret)
9806
26.2k
{
9807
26.2k
  bfd_symbol_info (symbol, ret);
9808
26.2k
}
9809
9810
/* Return whether a symbol name implies a local symbol.  Most targets
9811
   use this function for the is_local_label_name entry point, but some
9812
   override it.  */
9813
9814
bool
9815
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9816
            const char *name)
9817
21.6k
{
9818
  /* Normal local symbols start with ``.L''.  */
9819
21.6k
  if (name[0] == '.' && name[1] == 'L')
9820
1.36k
    return true;
9821
9822
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9823
     DWARF debugging symbols starting with ``..''.  */
9824
20.2k
  if (name[0] == '.' && name[1] == '.')
9825
801
    return true;
9826
9827
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9828
     emitting DWARF debugging output.  I suspect this is actually a
9829
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9830
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9831
     underscore to be emitted on some ELF targets).  For ease of use,
9832
     we treat such symbols as local.  */
9833
19.4k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9834
0
    return true;
9835
9836
  /* Treat assembler generated fake symbols, dollar local labels and
9837
     forward-backward labels (aka local labels) as locals.
9838
     These labels have the form:
9839
9840
       L0^A.*              (fake symbols)
9841
9842
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9843
9844
     Versions which start with .L will have already been matched above,
9845
     so we only need to match the rest.  */
9846
19.4k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9847
0
    {
9848
0
      bool ret = false;
9849
0
      const char * p;
9850
0
      char c;
9851
9852
0
      for (p = name + 2; (c = *p); p++)
9853
0
  {
9854
0
    if (c == 1 || c == 2)
9855
0
      {
9856
0
        if (c == 1 && p == name + 2)
9857
    /* A fake symbol.  */
9858
0
    return true;
9859
9860
        /* FIXME: We are being paranoid here and treating symbols like
9861
     L0^Bfoo as if there were non-local, on the grounds that the
9862
     assembler will never generate them.  But can any symbol
9863
     containing an ASCII value in the range 1-31 ever be anything
9864
     other than some kind of local ?  */
9865
0
        ret = true;
9866
0
      }
9867
9868
0
    if (! ISDIGIT (c))
9869
0
      {
9870
0
        ret = false;
9871
0
        break;
9872
0
      }
9873
0
  }
9874
0
      return ret;
9875
0
    }
9876
9877
19.4k
  return false;
9878
19.4k
}
9879
9880
alent *
9881
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9882
         asymbol *symbol ATTRIBUTE_UNUSED)
9883
0
{
9884
0
  abort ();
9885
0
  return NULL;
9886
0
}
9887
9888
bool
9889
_bfd_elf_set_arch_mach (bfd *abfd,
9890
      enum bfd_architecture arch,
9891
      unsigned long machine)
9892
639
{
9893
  /* If this isn't the right architecture for this backend, and this
9894
     isn't the generic backend, fail.  */
9895
639
  if (arch != get_elf_backend_data (abfd)->arch
9896
0
      && arch != bfd_arch_unknown
9897
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9898
0
    return false;
9899
9900
639
  return bfd_default_set_arch_mach (abfd, arch, machine);
9901
639
}
9902
9903
/* Find the nearest line to a particular section and offset,
9904
   for error reporting.  */
9905
9906
bool
9907
_bfd_elf_find_nearest_line (bfd *abfd,
9908
          asymbol **symbols,
9909
          asection *section,
9910
          bfd_vma offset,
9911
          const char **filename_ptr,
9912
          const char **functionname_ptr,
9913
          unsigned int *line_ptr,
9914
          unsigned int *discriminator_ptr)
9915
30.1k
{
9916
30.1k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9917
30.1k
                offset, filename_ptr,
9918
30.1k
                functionname_ptr, line_ptr,
9919
30.1k
                discriminator_ptr);
9920
30.1k
}
9921
9922
/* Find the nearest line to a particular section and offset,
9923
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9924
   can be optionally specified.  */
9925
9926
bool
9927
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9928
             const char *alt_filename,
9929
             asymbol **symbols,
9930
             asection *section,
9931
             bfd_vma offset,
9932
             const char **filename_ptr,
9933
             const char **functionname_ptr,
9934
             unsigned int *line_ptr,
9935
             unsigned int *discriminator_ptr)
9936
30.1k
{
9937
30.1k
  bool found;
9938
9939
30.1k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9940
30.1k
                section, offset, filename_ptr,
9941
30.1k
                functionname_ptr, line_ptr,
9942
30.1k
                discriminator_ptr,
9943
30.1k
                dwarf_debug_sections,
9944
30.1k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9945
9.08k
    return true;
9946
9947
21.0k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9948
21.0k
             filename_ptr, functionname_ptr, line_ptr))
9949
0
    {
9950
0
      if (!*functionname_ptr)
9951
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9952
0
        *filename_ptr ? NULL : filename_ptr,
9953
0
        functionname_ptr);
9954
0
      return true;
9955
0
    }
9956
9957
21.0k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9958
21.0k
               &found, filename_ptr,
9959
21.0k
               functionname_ptr, line_ptr,
9960
21.0k
               &elf_tdata (abfd)->line_info))
9961
0
    return false;
9962
21.0k
  if (found && (*functionname_ptr || *line_ptr))
9963
0
    return true;
9964
9965
21.0k
  if (symbols == NULL)
9966
7.53k
    return false;
9967
9968
13.4k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9969
13.4k
        filename_ptr, functionname_ptr))
9970
10.9k
    return false;
9971
9972
2.52k
  *line_ptr = 0;
9973
2.52k
  return true;
9974
13.4k
}
9975
9976
/* Find the line for a symbol.  */
9977
9978
bool
9979
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9980
        const char **filename_ptr, unsigned int *line_ptr)
9981
4.63k
{
9982
4.63k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9983
4.63k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9984
4.63k
          filename_ptr, NULL, line_ptr, NULL,
9985
4.63k
          dwarf_debug_sections,
9986
4.63k
          &tdata->dwarf2_find_line_info);
9987
4.63k
}
9988
9989
/* After a call to bfd_find_nearest_line, successive calls to
9990
   bfd_find_inliner_info can be used to get source information about
9991
   each level of function inlining that terminated at the address
9992
   passed to bfd_find_nearest_line.  Currently this is only supported
9993
   for DWARF2 with appropriate DWARF3 extensions. */
9994
9995
bool
9996
_bfd_elf_find_inliner_info (bfd *abfd,
9997
          const char **filename_ptr,
9998
          const char **functionname_ptr,
9999
          unsigned int *line_ptr)
10000
0
{
10001
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
10002
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10003
0
          functionname_ptr, line_ptr,
10004
0
          &tdata->dwarf2_find_line_info);
10005
0
}
10006
10007
int
10008
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10009
0
{
10010
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
10011
0
  int ret = bed->s->sizeof_ehdr;
10012
10013
0
  if (!bfd_link_relocatable (info))
10014
0
    {
10015
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10016
10017
0
      if (phdr_size == (bfd_size_type) -1)
10018
0
  {
10019
0
    struct elf_segment_map *m;
10020
10021
0
    phdr_size = 0;
10022
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10023
0
      phdr_size += bed->s->sizeof_phdr;
10024
10025
0
    if (phdr_size == 0)
10026
0
      phdr_size = get_program_header_size (abfd, info);
10027
0
  }
10028
10029
0
      elf_program_header_size (abfd) = phdr_size;
10030
0
      ret += phdr_size;
10031
0
    }
10032
10033
0
  return ret;
10034
0
}
10035
10036
bool
10037
_bfd_elf_set_section_contents (bfd *abfd,
10038
             sec_ptr section,
10039
             const void *location,
10040
             file_ptr offset,
10041
             bfd_size_type count)
10042
817
{
10043
817
  Elf_Internal_Shdr *hdr;
10044
10045
817
  if (! abfd->output_has_begun
10046
58
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10047
8
    return false;
10048
10049
809
  if (!count)
10050
0
    return true;
10051
10052
809
  hdr = &elf_section_data (section)->this_hdr;
10053
809
  if (hdr->sh_offset == (file_ptr) -1)
10054
0
    {
10055
0
      unsigned char *contents;
10056
10057
0
      if (bfd_section_is_ctf (section))
10058
  /* Nothing to do with this section: the contents are generated
10059
     later.  */
10060
0
  return true;
10061
10062
0
      if ((offset + count) > hdr->sh_size)
10063
0
  {
10064
0
    _bfd_error_handler
10065
0
      (_("%pB:%pA: error: attempting to write"
10066
0
         " over the end of the section"),
10067
0
       abfd, section);
10068
10069
0
    bfd_set_error (bfd_error_invalid_operation);
10070
0
    return false;
10071
0
  }
10072
10073
0
      contents = hdr->contents;
10074
0
      if (contents == NULL)
10075
0
  {
10076
0
    _bfd_error_handler
10077
0
      (_("%pB:%pA: error: attempting to write"
10078
0
         " section into an empty buffer"),
10079
0
       abfd, section);
10080
10081
0
    bfd_set_error (bfd_error_invalid_operation);
10082
0
    return false;
10083
0
  }
10084
10085
0
      memcpy (contents + offset, location, count);
10086
0
      return true;
10087
0
    }
10088
10089
809
  return _bfd_generic_set_section_contents (abfd, section,
10090
809
              location, offset, count);
10091
809
}
10092
10093
bool
10094
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10095
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10096
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10097
0
{
10098
0
  abort ();
10099
0
  return false;
10100
0
}
10101
10102
/* Try to convert a non-ELF reloc into an ELF one.  */
10103
10104
bool
10105
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10106
0
{
10107
  /* Check whether we really have an ELF howto.  */
10108
10109
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10110
0
    {
10111
0
      bfd_reloc_code_real_type code;
10112
0
      reloc_howto_type *howto;
10113
10114
      /* Alien reloc: Try to determine its type to replace it with an
10115
   equivalent ELF reloc.  */
10116
10117
0
      if (areloc->howto->pc_relative)
10118
0
  {
10119
0
    switch (areloc->howto->bitsize)
10120
0
      {
10121
0
      case 8:
10122
0
        code = BFD_RELOC_8_PCREL;
10123
0
        break;
10124
0
      case 12:
10125
0
        code = BFD_RELOC_12_PCREL;
10126
0
        break;
10127
0
      case 16:
10128
0
        code = BFD_RELOC_16_PCREL;
10129
0
        break;
10130
0
      case 24:
10131
0
        code = BFD_RELOC_24_PCREL;
10132
0
        break;
10133
0
      case 32:
10134
0
        code = BFD_RELOC_32_PCREL;
10135
0
        break;
10136
0
      case 64:
10137
0
        code = BFD_RELOC_64_PCREL;
10138
0
        break;
10139
0
      default:
10140
0
        goto fail;
10141
0
      }
10142
10143
0
    howto = bfd_reloc_type_lookup (abfd, code);
10144
10145
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10146
0
      {
10147
0
        if (howto->pcrel_offset)
10148
0
    areloc->addend += areloc->address;
10149
0
        else
10150
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10151
0
      }
10152
0
  }
10153
0
      else
10154
0
  {
10155
0
    switch (areloc->howto->bitsize)
10156
0
      {
10157
0
      case 8:
10158
0
        code = BFD_RELOC_8;
10159
0
        break;
10160
0
      case 14:
10161
0
        code = BFD_RELOC_14;
10162
0
        break;
10163
0
      case 16:
10164
0
        code = BFD_RELOC_16;
10165
0
        break;
10166
0
      case 26:
10167
0
        code = BFD_RELOC_26;
10168
0
        break;
10169
0
      case 32:
10170
0
        code = BFD_RELOC_32;
10171
0
        break;
10172
0
      case 64:
10173
0
        code = BFD_RELOC_64;
10174
0
        break;
10175
0
      default:
10176
0
        goto fail;
10177
0
      }
10178
10179
0
    howto = bfd_reloc_type_lookup (abfd, code);
10180
0
  }
10181
10182
0
      if (howto)
10183
0
  areloc->howto = howto;
10184
0
      else
10185
0
  goto fail;
10186
0
    }
10187
10188
0
  return true;
10189
10190
0
 fail:
10191
  /* xgettext:c-format */
10192
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10193
0
          abfd, areloc->howto->name);
10194
0
  bfd_set_error (bfd_error_sorry);
10195
0
  return false;
10196
0
}
10197
10198
bool
10199
_bfd_elf_free_cached_info (bfd *abfd)
10200
6.92M
{
10201
6.92M
  struct elf_obj_tdata *tdata;
10202
10203
6.92M
  if ((bfd_get_format (abfd) == bfd_object
10204
6.90M
       || bfd_get_format (abfd) == bfd_core)
10205
28.1k
      && (tdata = elf_tdata (abfd)) != NULL)
10206
28.1k
    {
10207
28.1k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10208
139
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10209
28.1k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10210
28.1k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10211
28.1k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10212
28.1k
      _bfd_elf_cleanup_object_attributes (abfd);
10213
384k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10214
356k
  {
10215
356k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10216
356k
    if (!sec->alloced)
10217
355k
      {
10218
355k
        free (elf_section_data (sec)->this_hdr.contents);
10219
355k
        elf_section_data (sec)->this_hdr.contents = NULL;
10220
355k
      }
10221
356k
    free (elf_section_data (sec)->relocs);
10222
356k
    elf_section_data (sec)->relocs = NULL;
10223
356k
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10224
0
      {
10225
0
        struct eh_frame_sec_info *sec_info = sec->sec_info;
10226
0
        free (sec_info->cies);
10227
0
      }
10228
356k
    if (sec->sec_info_type == SEC_INFO_TYPE_SFRAME)
10229
0
      {
10230
0
        struct sframe_dec_info *sfd_info = sec->sec_info;
10231
0
        sframe_decoder_free (&sfd_info->sfd_ctx);
10232
0
      }
10233
356k
  }
10234
28.1k
      free (tdata->symtab_hdr.contents);
10235
28.1k
      tdata->symtab_hdr.contents = NULL;
10236
28.1k
    }
10237
10238
6.92M
  return _bfd_generic_bfd_free_cached_info (abfd);
10239
6.92M
}
10240
10241
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10242
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10243
   range-checking to interfere.  There is nothing else to do in processing
10244
   this reloc.  */
10245
10246
bfd_reloc_status_type
10247
_bfd_elf_rel_vtable_reloc_fn
10248
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10249
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10250
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10251
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10252
52
{
10253
52
  return bfd_reloc_ok;
10254
52
}
10255

10256
/* Elf core file support.  Much of this only works on native
10257
   toolchains, since we rely on knowing the
10258
   machine-dependent procfs structure in order to pick
10259
   out details about the corefile.  */
10260
10261
#ifdef HAVE_SYS_PROCFS_H
10262
# include <sys/procfs.h>
10263
#endif
10264
10265
/* Return a PID that identifies a "thread" for threaded cores, or the
10266
   PID of the main process for non-threaded cores.  */
10267
10268
static int
10269
elfcore_make_pid (bfd *abfd)
10270
51
{
10271
51
  int pid;
10272
10273
51
  pid = elf_tdata (abfd)->core->lwpid;
10274
51
  if (pid == 0)
10275
50
    pid = elf_tdata (abfd)->core->pid;
10276
10277
51
  return pid;
10278
51
}
10279
10280
/* If there isn't a section called NAME, make one, using data from
10281
   SECT.  Note, this function will generate a reference to NAME, so
10282
   you shouldn't deallocate or overwrite it.  */
10283
10284
static bool
10285
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10286
51
{
10287
51
  asection *sect2;
10288
10289
51
  if (bfd_get_section_by_name (abfd, name) != NULL)
10290
27
    return true;
10291
10292
24
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10293
24
  if (sect2 == NULL)
10294
0
    return false;
10295
10296
24
  sect2->size = sect->size;
10297
24
  sect2->filepos = sect->filepos;
10298
24
  sect2->alignment_power = sect->alignment_power;
10299
24
  return true;
10300
24
}
10301
10302
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10303
   actually creates up to two pseudosections:
10304
   - For the single-threaded case, a section named NAME, unless
10305
     such a section already exists.
10306
   - For the multi-threaded case, a section named "NAME/PID", where
10307
     PID is elfcore_make_pid (abfd).
10308
   Both pseudosections have identical contents.  */
10309
bool
10310
_bfd_elfcore_make_pseudosection (bfd *abfd,
10311
         char *name,
10312
         size_t size,
10313
         ufile_ptr filepos)
10314
51
{
10315
51
  char buf[100];
10316
51
  char *threaded_name;
10317
51
  size_t len;
10318
51
  asection *sect;
10319
10320
  /* Build the section name.  */
10321
10322
51
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10323
51
  len = strlen (buf) + 1;
10324
51
  threaded_name = (char *) bfd_alloc (abfd, len);
10325
51
  if (threaded_name == NULL)
10326
0
    return false;
10327
51
  memcpy (threaded_name, buf, len);
10328
10329
51
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10330
51
               SEC_HAS_CONTENTS);
10331
51
  if (sect == NULL)
10332
0
    return false;
10333
51
  sect->size = size;
10334
51
  sect->filepos = filepos;
10335
51
  sect->alignment_power = 2;
10336
10337
51
  return elfcore_maybe_make_sect (abfd, name, sect);
10338
51
}
10339
10340
static bool
10341
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10342
        size_t offs)
10343
0
{
10344
0
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10345
0
                   SEC_HAS_CONTENTS);
10346
10347
0
  if (sect == NULL)
10348
0
    return false;
10349
10350
0
  sect->size = note->descsz - offs;
10351
0
  sect->filepos = note->descpos + offs;
10352
0
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10353
10354
0
  return true;
10355
0
}
10356
10357
/* prstatus_t exists on:
10358
     solaris 2.5+
10359
     linux 2.[01] + glibc
10360
     unixware 4.2
10361
*/
10362
10363
#if defined (HAVE_PRSTATUS_T)
10364
10365
static bool
10366
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10367
81
{
10368
81
  size_t size;
10369
81
  int offset;
10370
10371
81
  if (note->descsz == sizeof (prstatus_t))
10372
0
    {
10373
0
      prstatus_t prstat;
10374
10375
0
      size = sizeof (prstat.pr_reg);
10376
0
      offset   = offsetof (prstatus_t, pr_reg);
10377
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10378
10379
      /* Do not overwrite the core signal if it
10380
   has already been set by another thread.  */
10381
0
      if (elf_tdata (abfd)->core->signal == 0)
10382
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10383
0
      if (elf_tdata (abfd)->core->pid == 0)
10384
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10385
10386
      /* pr_who exists on:
10387
   solaris 2.5+
10388
   unixware 4.2
10389
   pr_who doesn't exist on:
10390
   linux 2.[01]
10391
   */
10392
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10393
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10394
#else
10395
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10396
0
#endif
10397
0
    }
10398
81
#if defined (HAVE_PRSTATUS32_T)
10399
81
  else if (note->descsz == sizeof (prstatus32_t))
10400
1
    {
10401
      /* 64-bit host, 32-bit corefile */
10402
1
      prstatus32_t prstat;
10403
10404
1
      size = sizeof (prstat.pr_reg);
10405
1
      offset   = offsetof (prstatus32_t, pr_reg);
10406
1
      memcpy (&prstat, note->descdata, sizeof (prstat));
10407
10408
      /* Do not overwrite the core signal if it
10409
   has already been set by another thread.  */
10410
1
      if (elf_tdata (abfd)->core->signal == 0)
10411
1
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10412
1
      if (elf_tdata (abfd)->core->pid == 0)
10413
1
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10414
10415
      /* pr_who exists on:
10416
   solaris 2.5+
10417
   unixware 4.2
10418
   pr_who doesn't exist on:
10419
   linux 2.[01]
10420
   */
10421
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10422
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10423
#else
10424
1
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10425
1
#endif
10426
1
    }
10427
80
#endif /* HAVE_PRSTATUS32_T */
10428
80
  else
10429
80
    {
10430
      /* Fail - we don't know how to handle any other
10431
   note size (ie. data object type).  */
10432
80
      return true;
10433
80
    }
10434
10435
  /* Make a ".reg/999" section and a ".reg" section.  */
10436
1
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10437
1
            size, note->descpos + offset);
10438
81
}
10439
#endif /* defined (HAVE_PRSTATUS_T) */
10440
10441
/* Create a pseudosection containing the exact contents of NOTE.  */
10442
static bool
10443
elfcore_make_note_pseudosection (bfd *abfd,
10444
         char *name,
10445
         Elf_Internal_Note *note)
10446
50
{
10447
50
  return _bfd_elfcore_make_pseudosection (abfd, name,
10448
50
            note->descsz, note->descpos);
10449
50
}
10450
10451
/* There isn't a consistent prfpregset_t across platforms,
10452
   but it doesn't matter, because we don't have to pick this
10453
   data structure apart.  */
10454
10455
static bool
10456
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10457
50
{
10458
50
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10459
50
}
10460
10461
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10462
   type of NT_PRXFPREG.  Just include the whole note's contents
10463
   literally.  */
10464
10465
static bool
10466
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10467
0
{
10468
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
10469
0
}
10470
10471
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10472
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10473
   contents literally.  */
10474
10475
static bool
10476
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10477
0
{
10478
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
10479
0
}
10480
10481
static bool
10482
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10483
0
{
10484
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
10485
0
}
10486
10487
/* Linux dumps the XSAVE Layout description in a note named "LINUX"
10488
   with a note type of NT_X86_XSAVE_LAYOUT. */
10489
static bool
10490
elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
10491
0
{
10492
0
  return elfcore_make_note_pseudosection (abfd,
10493
0
            NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
10494
0
            note);
10495
0
}
10496
10497
static bool
10498
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10499
0
{
10500
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
10501
0
}
10502
10503
static bool
10504
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10505
0
{
10506
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
10507
0
}
10508
10509
static bool
10510
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10511
0
{
10512
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
10513
0
}
10514
10515
static bool
10516
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10517
0
{
10518
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
10519
0
}
10520
10521
static bool
10522
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10523
0
{
10524
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
10525
0
}
10526
10527
static bool
10528
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10529
0
{
10530
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
10531
0
}
10532
10533
static bool
10534
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10535
0
{
10536
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
10537
0
}
10538
10539
static bool
10540
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10541
0
{
10542
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
10543
0
}
10544
10545
static bool
10546
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10547
0
{
10548
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
10549
0
}
10550
10551
static bool
10552
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10553
0
{
10554
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
10555
0
}
10556
10557
static bool
10558
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10559
0
{
10560
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
10561
0
}
10562
10563
static bool
10564
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10565
0
{
10566
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
10567
0
}
10568
10569
static bool
10570
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10571
0
{
10572
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
10573
0
}
10574
10575
static bool
10576
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10577
0
{
10578
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
10579
0
}
10580
10581
static bool
10582
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10583
0
{
10584
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
10585
0
}
10586
10587
static bool
10588
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10589
0
{
10590
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
10591
0
}
10592
10593
static bool
10594
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10595
0
{
10596
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
10597
0
}
10598
10599
static bool
10600
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10601
0
{
10602
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
10603
0
}
10604
10605
static bool
10606
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10607
0
{
10608
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
10609
0
}
10610
10611
static bool
10612
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10613
0
{
10614
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
10615
0
}
10616
10617
static bool
10618
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10619
0
{
10620
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
10621
0
}
10622
10623
static bool
10624
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10625
0
{
10626
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
10627
0
}
10628
10629
static bool
10630
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10631
0
{
10632
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
10633
0
}
10634
10635
static bool
10636
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10637
0
{
10638
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
10639
0
}
10640
10641
static bool
10642
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10643
0
{
10644
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
10645
0
}
10646
10647
static bool
10648
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10649
0
{
10650
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
10651
0
}
10652
10653
static bool
10654
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10655
0
{
10656
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
10657
0
}
10658
10659
static bool
10660
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10661
0
{
10662
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
10663
0
}
10664
10665
static bool
10666
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10667
0
{
10668
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
10669
0
}
10670
10671
static bool
10672
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10673
0
{
10674
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
10675
0
}
10676
10677
static bool
10678
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10679
0
{
10680
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
10681
0
}
10682
10683
static bool
10684
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10685
0
{
10686
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
10687
0
}
10688
10689
static bool
10690
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10691
0
{
10692
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
10693
0
}
10694
10695
static bool
10696
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10697
0
{
10698
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
10699
0
}
10700
10701
static bool
10702
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10703
0
{
10704
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
10705
0
}
10706
10707
static bool
10708
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10709
0
{
10710
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
10711
0
}
10712
10713
static bool
10714
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10715
0
{
10716
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
10717
0
}
10718
10719
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10720
   successful, otherwise return FALSE.  */
10721
10722
static bool
10723
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10724
0
{
10725
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
10726
0
}
10727
10728
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10729
   successful, otherwise return FALSE.  */
10730
10731
static bool
10732
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10733
0
{
10734
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
10735
0
}
10736
10737
/* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
10738
 * Return TRUE if successful, otherwise return FALSE.  */
10739
10740
static bool
10741
elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
10742
0
{
10743
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
10744
0
}
10745
10746
static bool
10747
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10748
0
{
10749
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
10750
0
}
10751
10752
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10753
   successful otherwise, return FALSE.  */
10754
10755
static bool
10756
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10757
0
{
10758
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
10759
0
}
10760
10761
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10762
   successful otherwise, return FALSE.  */
10763
10764
static bool
10765
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10766
0
{
10767
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10768
0
}
10769
10770
static bool
10771
elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
10772
0
{
10773
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
10774
0
            note);
10775
0
}
10776
10777
static bool
10778
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10779
0
{
10780
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
10781
0
}
10782
10783
static bool
10784
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10785
0
{
10786
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
10787
0
}
10788
10789
static bool
10790
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10791
0
{
10792
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
10793
0
}
10794
10795
static bool
10796
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10797
0
{
10798
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
10799
0
}
10800
10801
#if defined (HAVE_PRPSINFO_T)
10802
typedef prpsinfo_t   elfcore_psinfo_t;
10803
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10804
typedef prpsinfo32_t elfcore_psinfo32_t;
10805
#endif
10806
#endif
10807
10808
#if defined (HAVE_PSINFO_T)
10809
typedef psinfo_t   elfcore_psinfo_t;
10810
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10811
typedef psinfo32_t elfcore_psinfo32_t;
10812
#endif
10813
#endif
10814
10815
/* return a malloc'ed copy of a string at START which is at
10816
   most MAX bytes long, possibly without a terminating '\0'.
10817
   the copy will always have a terminating '\0'.  */
10818
10819
char *
10820
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10821
0
{
10822
0
  char *dups;
10823
0
  char *end = (char *) memchr (start, '\0', max);
10824
0
  size_t len;
10825
10826
0
  if (end == NULL)
10827
0
    len = max;
10828
0
  else
10829
0
    len = end - start;
10830
10831
0
  dups = (char *) bfd_alloc (abfd, len + 1);
10832
0
  if (dups == NULL)
10833
0
    return NULL;
10834
10835
0
  memcpy (dups, start, len);
10836
0
  dups[len] = '\0';
10837
10838
0
  return dups;
10839
0
}
10840
10841
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10842
static bool
10843
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10844
22
{
10845
22
  if (note->descsz == sizeof (elfcore_psinfo_t))
10846
0
    {
10847
0
      elfcore_psinfo_t psinfo;
10848
10849
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10850
10851
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10852
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10853
0
#endif
10854
0
      elf_tdata (abfd)->core->program
10855
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10856
0
        sizeof (psinfo.pr_fname));
10857
10858
0
      elf_tdata (abfd)->core->command
10859
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10860
0
        sizeof (psinfo.pr_psargs));
10861
0
    }
10862
22
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10863
22
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10864
0
    {
10865
      /* 64-bit host, 32-bit corefile */
10866
0
      elfcore_psinfo32_t psinfo;
10867
10868
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10869
10870
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10871
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10872
0
#endif
10873
0
      elf_tdata (abfd)->core->program
10874
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10875
0
        sizeof (psinfo.pr_fname));
10876
10877
0
      elf_tdata (abfd)->core->command
10878
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10879
0
        sizeof (psinfo.pr_psargs));
10880
0
    }
10881
22
#endif
10882
10883
22
  else
10884
22
    {
10885
      /* Fail - we don't know how to handle any other
10886
   note size (ie. data object type).  */
10887
22
      return true;
10888
22
    }
10889
10890
  /* Note that for some reason, a spurious space is tacked
10891
     onto the end of the args in some (at least one anyway)
10892
     implementations, so strip it off if it exists.  */
10893
10894
0
  {
10895
0
    char *command = elf_tdata (abfd)->core->command;
10896
0
    int n = strlen (command);
10897
10898
0
    if (0 < n && command[n - 1] == ' ')
10899
0
      command[n - 1] = '\0';
10900
0
  }
10901
10902
0
  return true;
10903
22
}
10904
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10905
10906
#if defined (HAVE_PSTATUS_T)
10907
static bool
10908
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10909
{
10910
  if (note->descsz == sizeof (pstatus_t)
10911
#if defined (HAVE_PXSTATUS_T)
10912
      || note->descsz == sizeof (pxstatus_t)
10913
#endif
10914
      )
10915
    {
10916
      pstatus_t pstat;
10917
10918
      memcpy (&pstat, note->descdata, sizeof (pstat));
10919
10920
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10921
    }
10922
#if defined (HAVE_PSTATUS32_T)
10923
  else if (note->descsz == sizeof (pstatus32_t))
10924
    {
10925
      /* 64-bit host, 32-bit corefile */
10926
      pstatus32_t pstat;
10927
10928
      memcpy (&pstat, note->descdata, sizeof (pstat));
10929
10930
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10931
    }
10932
#endif
10933
  /* Could grab some more details from the "representative"
10934
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10935
     NT_LWPSTATUS note, presumably.  */
10936
10937
  return true;
10938
}
10939
#endif /* defined (HAVE_PSTATUS_T) */
10940
10941
#if defined (HAVE_LWPSTATUS_T)
10942
static bool
10943
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10944
{
10945
  lwpstatus_t lwpstat;
10946
  char buf[100];
10947
  char *name;
10948
  size_t len;
10949
  asection *sect;
10950
10951
  if (note->descsz != sizeof (lwpstat)
10952
#if defined (HAVE_LWPXSTATUS_T)
10953
      && note->descsz != sizeof (lwpxstatus_t)
10954
#endif
10955
      )
10956
    return true;
10957
10958
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10959
10960
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10961
  /* Do not overwrite the core signal if it has already been set by
10962
     another thread.  */
10963
  if (elf_tdata (abfd)->core->signal == 0)
10964
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10965
10966
  /* Make a ".reg/999" section.  */
10967
10968
  sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
10969
  len = strlen (buf) + 1;
10970
  name = bfd_alloc (abfd, len);
10971
  if (name == NULL)
10972
    return false;
10973
  memcpy (name, buf, len);
10974
10975
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10976
  if (sect == NULL)
10977
    return false;
10978
10979
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10980
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10981
  sect->filepos = note->descpos
10982
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10983
#endif
10984
10985
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10986
  sect->size = sizeof (lwpstat.pr_reg);
10987
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10988
#endif
10989
10990
  sect->alignment_power = 2;
10991
10992
  if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
10993
    return false;
10994
10995
  /* Make a ".reg2/999" section */
10996
10997
  sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
10998
  len = strlen (buf) + 1;
10999
  name = bfd_alloc (abfd, len);
11000
  if (name == NULL)
11001
    return false;
11002
  memcpy (name, buf, len);
11003
11004
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11005
  if (sect == NULL)
11006
    return false;
11007
11008
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11009
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11010
  sect->filepos = note->descpos
11011
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11012
#endif
11013
11014
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11015
  sect->size = sizeof (lwpstat.pr_fpreg);
11016
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11017
#endif
11018
11019
  sect->alignment_power = 2;
11020
11021
  return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
11022
}
11023
#endif /* defined (HAVE_LWPSTATUS_T) */
11024
11025
/* These constants, and the structure offsets used below, are defined by
11026
   Cygwin's core_dump.h */
11027
0
#define NOTE_INFO_PROCESS  1
11028
0
#define NOTE_INFO_THREAD   2
11029
0
#define NOTE_INFO_MODULE   3
11030
0
#define NOTE_INFO_MODULE64 4
11031
11032
static bool
11033
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11034
21
{
11035
21
  char buf[30];
11036
21
  char *name;
11037
21
  size_t len;
11038
21
  unsigned int name_size;
11039
21
  asection *sect;
11040
21
  unsigned int type;
11041
21
  int is_active_thread;
11042
21
  bfd_vma base_addr;
11043
11044
21
  if (note->descsz < 4)
11045
3
    return true;
11046
11047
18
  if (! startswith (note->namedata, "win32"))
11048
18
    return true;
11049
11050
0
  type = bfd_get_32 (abfd, note->descdata);
11051
11052
0
  static const struct
11053
0
  {
11054
0
    const char *type_name;
11055
0
    unsigned long min_size;
11056
0
  } size_check[] =
11057
0
      {
11058
0
       { "NOTE_INFO_PROCESS", 12 },
11059
0
       { "NOTE_INFO_THREAD", 12 },
11060
0
       { "NOTE_INFO_MODULE", 12 },
11061
0
       { "NOTE_INFO_MODULE64", 16 },
11062
0
      };
11063
11064
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11065
0
      return true;
11066
11067
0
  if (note->descsz < size_check[type - 1].min_size)
11068
0
    {
11069
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11070
0
          " is too small"),
11071
0
        abfd, size_check[type - 1].type_name, note->descsz);
11072
0
      return true;
11073
0
    }
11074
11075
0
  switch (type)
11076
0
    {
11077
0
    case NOTE_INFO_PROCESS:
11078
      /* FIXME: need to add ->core->command.  */
11079
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11080
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11081
0
      break;
11082
11083
0
    case NOTE_INFO_THREAD:
11084
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11085
   structure. */
11086
      /* thread_info.tid */
11087
0
      sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11088
11089
0
      len = strlen (buf) + 1;
11090
0
      name = (char *) bfd_alloc (abfd, len);
11091
0
      if (name == NULL)
11092
0
  return false;
11093
11094
0
      memcpy (name, buf, len);
11095
11096
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11097
0
      if (sect == NULL)
11098
0
  return false;
11099
11100
      /* sizeof (thread_info.thread_context) */
11101
0
      sect->size = note->descsz - 12;
11102
      /* offsetof (thread_info.thread_context) */
11103
0
      sect->filepos = note->descpos + 12;
11104
0
      sect->alignment_power = 2;
11105
11106
      /* thread_info.is_active_thread */
11107
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11108
11109
0
      if (is_active_thread)
11110
0
  if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11111
0
    return false;
11112
0
      break;
11113
11114
0
    case NOTE_INFO_MODULE:
11115
0
    case NOTE_INFO_MODULE64:
11116
      /* Make a ".module/xxxxxxxx" section.  */
11117
0
      if (type == NOTE_INFO_MODULE)
11118
0
  {
11119
    /* module_info.base_address */
11120
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11121
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11122
    /* module_info.module_name_size */
11123
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11124
0
  }
11125
0
      else /* NOTE_INFO_MODULE64 */
11126
0
  {
11127
    /* module_info.base_address */
11128
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11129
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11130
    /* module_info.module_name_size */
11131
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11132
0
  }
11133
11134
0
      len = strlen (buf) + 1;
11135
0
      name = (char *) bfd_alloc (abfd, len);
11136
0
      if (name == NULL)
11137
0
  return false;
11138
11139
0
      memcpy (name, buf, len);
11140
11141
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11142
11143
0
      if (sect == NULL)
11144
0
  return false;
11145
11146
0
      if (note->descsz < 12 + name_size)
11147
0
  {
11148
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11149
0
        " is too small to contain a name of size %u"),
11150
0
            abfd, note->descsz, name_size);
11151
0
    return true;
11152
0
  }
11153
11154
0
      sect->size = note->descsz;
11155
0
      sect->filepos = note->descpos;
11156
0
      sect->alignment_power = 2;
11157
0
      break;
11158
11159
0
    default:
11160
0
      return true;
11161
0
    }
11162
11163
0
  return true;
11164
0
}
11165
11166
static bool
11167
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11168
1.03k
{
11169
1.03k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11170
11171
  /* Short cut for LINUX notes.  */
11172
1.03k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11173
6
      && streq (note->namedata, NOTE_NAME_LINUX))
11174
0
    {
11175
0
      switch (note->type)
11176
0
  {
11177
0
  case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note);
11178
0
  case NT_ARC_V2:   return elfcore_grok_arc_v2 (abfd, note);
11179
0
  case NT_ARM_FPMR: return elfcore_grok_aarch_fpmr (abfd, note);
11180
0
  case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note);
11181
0
  case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note);
11182
0
  case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note);
11183
0
  case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note);
11184
0
  case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note);
11185
0
  case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note);
11186
0
  case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
11187
0
  case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note);
11188
0
  case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note);
11189
0
  case NT_ARM_ZA:   return elfcore_grok_aarch_za (abfd, note);
11190
0
  case NT_ARM_ZT:   return elfcore_grok_aarch_zt (abfd, note);
11191
0
  case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note);
11192
0
  case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note);
11193
0
  case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note);
11194
0
  case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note);
11195
0
  case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note);
11196
0
  case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note);
11197
0
  case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note);
11198
0
  case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note);
11199
0
  case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note);
11200
0
  case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note);
11201
0
  case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note);
11202
0
  case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note);
11203
0
  case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note);
11204
0
  case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note);
11205
0
  case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note);
11206
0
  case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note);
11207
0
  case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note);
11208
0
  case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note);
11209
0
  case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note);
11210
0
  case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note);
11211
0
  case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note);
11212
0
  case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note);
11213
0
  case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note);
11214
0
  case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note);
11215
0
  case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
11216
0
  case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note);
11217
0
  case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
11218
0
  case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note);
11219
0
  case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note);
11220
0
  case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note);
11221
0
  case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note);
11222
0
  case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note);
11223
0
  case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note);
11224
0
  case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note);
11225
0
  case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note);
11226
0
  case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
11227
0
  default: break;
11228
0
  }
11229
0
    }
11230
  
11231
1.03k
  switch (note->type)
11232
1.03k
    {
11233
849
    default:
11234
849
      return true;
11235
11236
81
    case NT_PRSTATUS:
11237
81
      if (bed->elf_backend_grok_prstatus)
11238
56
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11239
0
    return true;
11240
81
#if defined (HAVE_PRSTATUS_T)
11241
81
      return elfcore_grok_prstatus (abfd, note);
11242
#else
11243
      return true;
11244
#endif
11245
11246
#if defined (HAVE_PSTATUS_T)
11247
    case NT_PSTATUS:
11248
      return elfcore_grok_pstatus (abfd, note);
11249
#endif
11250
11251
#if defined (HAVE_LWPSTATUS_T)
11252
    case NT_LWPSTATUS:
11253
      return elfcore_grok_lwpstatus (abfd, note);
11254
#endif
11255
11256
50
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11257
50
      return elfcore_grok_prfpreg (abfd, note);
11258
11259
21
    case NT_WIN32PSTATUS:
11260
21
      return elfcore_grok_win32pstatus (abfd, note);
11261
11262
0
    case NT_GDB_TDESC:
11263
0
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11264
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11265
0
      else
11266
0
  return true;
11267
11268
7
    case NT_RISCV_CSR:
11269
7
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11270
0
  return elfcore_grok_riscv_csr (abfd, note);
11271
7
      else
11272
7
  return true;
11273
11274
22
    case NT_PRPSINFO:
11275
22
    case NT_PSINFO:
11276
22
      if (bed->elf_backend_grok_psinfo)
11277
16
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11278
0
    return true;
11279
22
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11280
22
      return elfcore_grok_psinfo (abfd, note);
11281
#else
11282
      return true;
11283
#endif
11284
11285
0
    case NT_AUXV:
11286
0
      return elfcore_make_auxv_note_section (abfd, note, 0);
11287
11288
0
    case NT_FILE:
11289
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11290
0
                note);
11291
11292
0
    case NT_SIGINFO:
11293
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11294
0
                note);
11295
1.03k
    }
11296
1.03k
}
11297
11298
static bool
11299
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11300
299
{
11301
299
  struct bfd_build_id* build_id;
11302
11303
299
  if (note->descsz == 0)
11304
39
    return false;
11305
11306
260
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11307
260
  if (build_id == NULL)
11308
0
    return false;
11309
11310
260
  build_id->size = note->descsz;
11311
260
  memcpy (build_id->data, note->descdata, note->descsz);
11312
260
  abfd->build_id = build_id;
11313
11314
260
  return true;
11315
260
}
11316
11317
static bool
11318
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11319
979
{
11320
979
  switch (note->type)
11321
979
    {
11322
318
    default:
11323
318
      return true;
11324
11325
362
    case NT_GNU_PROPERTY_TYPE_0:
11326
362
      return _bfd_elf_parse_gnu_properties (abfd, note);
11327
11328
299
    case NT_GNU_BUILD_ID:
11329
299
      return elfobj_grok_gnu_build_id (abfd, note);
11330
979
    }
11331
979
}
11332
11333
static bool
11334
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11335
0
{
11336
0
  struct sdt_note *cur =
11337
0
    (struct sdt_note *) bfd_alloc (abfd,
11338
0
           sizeof (struct sdt_note) + note->descsz);
11339
11340
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11341
0
  cur->size = (bfd_size_type) note->descsz;
11342
0
  memcpy (cur->data, note->descdata, note->descsz);
11343
11344
0
  elf_tdata (abfd)->sdt_note_head = cur;
11345
11346
0
  return true;
11347
0
}
11348
11349
static bool
11350
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11351
0
{
11352
0
  switch (note->type)
11353
0
    {
11354
0
    case NT_STAPSDT:
11355
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11356
11357
0
    default:
11358
0
      return true;
11359
0
    }
11360
0
}
11361
11362
static bool
11363
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11364
0
{
11365
0
  size_t offset;
11366
11367
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11368
0
    {
11369
0
    case ELFCLASS32:
11370
0
      if (note->descsz < 108)
11371
0
  return false;
11372
0
      break;
11373
11374
0
    case ELFCLASS64:
11375
0
      if (note->descsz < 120)
11376
0
  return false;
11377
0
      break;
11378
11379
0
    default:
11380
0
      return false;
11381
0
    }
11382
11383
  /* Check for version 1 in pr_version.  */
11384
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11385
0
    return false;
11386
11387
0
  offset = 4;
11388
11389
  /* Skip over pr_psinfosz. */
11390
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11391
0
    offset += 4;
11392
0
  else
11393
0
    {
11394
0
      offset += 4;  /* Padding before pr_psinfosz. */
11395
0
      offset += 8;
11396
0
    }
11397
11398
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11399
0
  elf_tdata (abfd)->core->program
11400
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11401
0
  offset += 17;
11402
11403
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11404
0
  elf_tdata (abfd)->core->command
11405
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11406
0
  offset += 81;
11407
11408
  /* Padding before pr_pid.  */
11409
0
  offset += 2;
11410
11411
  /* The pr_pid field was added in version "1a".  */
11412
0
  if (note->descsz < offset + 4)
11413
0
    return true;
11414
11415
0
  elf_tdata (abfd)->core->pid
11416
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11417
11418
0
  return true;
11419
0
}
11420
11421
static bool
11422
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11423
0
{
11424
0
  size_t offset;
11425
0
  size_t size;
11426
0
  size_t min_size;
11427
11428
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11429
     Also compute minimum size of this note.  */
11430
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11431
0
    {
11432
0
    case ELFCLASS32:
11433
0
      offset = 4 + 4;
11434
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11435
0
      break;
11436
11437
0
    case ELFCLASS64:
11438
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11439
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11440
0
      break;
11441
11442
0
    default:
11443
0
      return false;
11444
0
    }
11445
11446
0
  if (note->descsz < min_size)
11447
0
    return false;
11448
11449
  /* Check for version 1 in pr_version.  */
11450
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11451
0
    return false;
11452
11453
  /* Extract size of pr_reg from pr_gregsetsz.  */
11454
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11455
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11456
0
    {
11457
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11458
0
      offset += 4 * 2;
11459
0
    }
11460
0
  else
11461
0
    {
11462
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11463
0
      offset += 8 * 2;
11464
0
    }
11465
11466
  /* Skip over pr_osreldate.  */
11467
0
  offset += 4;
11468
11469
  /* Read signal from pr_cursig.  */
11470
0
  if (elf_tdata (abfd)->core->signal == 0)
11471
0
    elf_tdata (abfd)->core->signal
11472
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11473
0
  offset += 4;
11474
11475
  /* Read TID from pr_pid.  */
11476
0
  elf_tdata (abfd)->core->lwpid
11477
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11478
0
  offset += 4;
11479
11480
  /* Padding before pr_reg.  */
11481
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11482
0
    offset += 4;
11483
11484
  /* Make sure that there is enough data remaining in the note.  */
11485
0
  if ((note->descsz - offset) < size)
11486
0
    return false;
11487
11488
  /* Make a ".reg/999" section and a ".reg" section.  */
11489
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
11490
0
            size, note->descpos + offset);
11491
0
}
11492
11493
static bool
11494
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11495
0
{
11496
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
11497
11498
0
  switch (note->type)
11499
0
    {
11500
0
    case NT_PRSTATUS:
11501
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11502
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11503
0
    return true;
11504
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11505
11506
0
    case NT_FPREGSET:
11507
0
      return elfcore_grok_prfpreg (abfd, note);
11508
11509
0
    case NT_PRPSINFO:
11510
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11511
11512
0
    case NT_FREEBSD_THRMISC:
11513
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11514
11515
0
    case NT_FREEBSD_PROCSTAT_PROC:
11516
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11517
0
                note);
11518
11519
0
    case NT_FREEBSD_PROCSTAT_FILES:
11520
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11521
0
                note);
11522
11523
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11524
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11525
0
                note);
11526
11527
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11528
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11529
11530
0
    case NT_FREEBSD_X86_SEGBASES:
11531
0
      return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
11532
11533
0
    case NT_X86_XSTATE:
11534
0
      return elfcore_grok_xstatereg (abfd, note);
11535
11536
0
    case NT_FREEBSD_PTLWPINFO:
11537
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11538
0
                note);
11539
11540
0
    case NT_ARM_TLS:
11541
0
      return elfcore_grok_aarch_tls (abfd, note);
11542
11543
0
    case NT_ARM_VFP:
11544
0
      return elfcore_grok_arm_vfp (abfd, note);
11545
11546
0
    default:
11547
0
      return true;
11548
0
    }
11549
0
}
11550
11551
static bool
11552
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11553
0
{
11554
0
  char *cp;
11555
11556
0
  cp = strchr (note->namedata, '@');
11557
0
  if (cp != NULL)
11558
0
    {
11559
0
      *lwpidp = atoi(cp + 1);
11560
0
      return true;
11561
0
    }
11562
0
  return false;
11563
0
}
11564
11565
static bool
11566
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11567
0
{
11568
0
  if (note->descsz <= 0x7c + 31)
11569
0
    return false;
11570
11571
  /* Signal number at offset 0x08. */
11572
0
  elf_tdata (abfd)->core->signal
11573
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11574
11575
  /* Process ID at offset 0x50. */
11576
0
  elf_tdata (abfd)->core->pid
11577
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11578
11579
  /* Command name at 0x7c (max 32 bytes, including nul). */
11580
0
  elf_tdata (abfd)->core->command
11581
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11582
11583
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11584
0
            note);
11585
0
}
11586
11587
static bool
11588
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11589
0
{
11590
0
  int lwp;
11591
11592
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11593
0
    elf_tdata (abfd)->core->lwpid = lwp;
11594
11595
0
  switch (note->type)
11596
0
    {
11597
0
    case NT_NETBSDCORE_PROCINFO:
11598
      /* NetBSD-specific core "procinfo".  Note that we expect to
11599
   find this note before any of the others, which is fine,
11600
   since the kernel writes this note out first when it
11601
   creates a core file.  */
11602
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11603
0
    case NT_NETBSDCORE_AUXV:
11604
      /* NetBSD-specific Elf Auxiliary Vector data. */
11605
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11606
0
    case NT_NETBSDCORE_LWPSTATUS:
11607
0
      return elfcore_make_note_pseudosection (abfd,
11608
0
                ".note.netbsdcore.lwpstatus",
11609
0
                note);
11610
0
    default:
11611
0
      break;
11612
0
    }
11613
11614
  /* As of March 2020 there are no other machine-independent notes
11615
     defined for NetBSD core files.  If the note type is less
11616
     than the start of the machine-dependent note types, we don't
11617
     understand it.  */
11618
11619
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11620
0
    return true;
11621
11622
11623
0
  switch (bfd_get_arch (abfd))
11624
0
    {
11625
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11626
   PT_GETFPREGS == mach+2.  */
11627
11628
0
    case bfd_arch_aarch64:
11629
0
    case bfd_arch_alpha:
11630
0
    case bfd_arch_sparc:
11631
0
      switch (note->type)
11632
0
  {
11633
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11634
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11635
11636
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11637
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11638
11639
0
  default:
11640
0
    return true;
11641
0
  }
11642
11643
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11644
   There's also old PT___GETREGS40 == mach + 1 for old reg
11645
   structure which lacks GBR.  */
11646
11647
0
    case bfd_arch_sh:
11648
0
      switch (note->type)
11649
0
  {
11650
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11651
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11652
11653
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11654
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11655
11656
0
  default:
11657
0
    return true;
11658
0
  }
11659
11660
      /* On all other arch's, PT_GETREGS == mach+1 and
11661
   PT_GETFPREGS == mach+3.  */
11662
11663
0
    default:
11664
0
      switch (note->type)
11665
0
  {
11666
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11667
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11668
11669
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11670
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11671
11672
0
  default:
11673
0
    return true;
11674
0
  }
11675
0
    }
11676
    /* NOTREACHED */
11677
0
}
11678
11679
static bool
11680
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11681
0
{
11682
0
  if (note->descsz <= 0x48 + 31)
11683
0
    return false;
11684
11685
  /* Signal number at offset 0x08. */
11686
0
  elf_tdata (abfd)->core->signal
11687
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11688
11689
  /* Process ID at offset 0x20. */
11690
0
  elf_tdata (abfd)->core->pid
11691
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11692
11693
  /* Command name at 0x48 (max 32 bytes, including nul). */
11694
0
  elf_tdata (abfd)->core->command
11695
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11696
11697
0
  return true;
11698
0
}
11699
11700
/* Processes Solaris's process status note.
11701
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11702
   pid_off ~ offsetof(prstatus_t, pr_pid)
11703
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11704
   gregset_size ~ sizeof(gregset_t)
11705
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11706
11707
static bool
11708
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11709
             int pid_off, int lwpid_off, size_t gregset_size,
11710
             size_t gregset_offset)
11711
0
{
11712
0
  asection *sect = NULL;
11713
0
  elf_tdata (abfd)->core->signal
11714
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11715
0
  elf_tdata (abfd)->core->pid
11716
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11717
0
  elf_tdata (abfd)->core->lwpid
11718
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11719
11720
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11721
0
  if (sect != NULL)
11722
0
    sect->size = gregset_size;
11723
11724
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11725
0
            note->descpos + gregset_offset);
11726
0
}
11727
11728
/* Gets program and arguments from a core.
11729
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11730
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11731
11732
static bool
11733
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11734
        int prog_off, int comm_off)
11735
0
{
11736
0
  elf_tdata (abfd)->core->program
11737
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11738
0
  elf_tdata (abfd)->core->command
11739
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11740
11741
0
  return true;
11742
0
}
11743
11744
/* Processes Solaris's LWP status note.
11745
   gregset_size ~ sizeof(gregset_t)
11746
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11747
   fpregset_size ~ sizeof(fpregset_t)
11748
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11749
11750
static bool
11751
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11752
        size_t gregset_size, int gregset_off,
11753
        size_t fpregset_size, int fpregset_off)
11754
0
{
11755
0
  asection *sect = NULL;
11756
0
  char reg2_section_name[16] = { 0 };
11757
11758
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
11759
0
       elf_tdata (abfd)->core->lwpid);
11760
11761
  /* offsetof(lwpstatus_t, pr_lwpid) */
11762
0
  elf_tdata (abfd)->core->lwpid
11763
0
    = bfd_get_32 (abfd, note->descdata + 4);
11764
  /* offsetof(lwpstatus_t, pr_cursig) */
11765
0
  elf_tdata (abfd)->core->signal
11766
0
    = bfd_get_16 (abfd, note->descdata + 12);
11767
11768
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11769
0
  if (sect != NULL)
11770
0
    sect->size = gregset_size;
11771
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11772
0
               note->descpos + gregset_off))
11773
0
    return false;
11774
11775
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11776
0
  if (sect != NULL)
11777
0
    {
11778
0
      sect->size = fpregset_size;
11779
0
      sect->filepos = note->descpos + fpregset_off;
11780
0
      sect->alignment_power = 2;
11781
0
    }
11782
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
11783
0
               note->descpos + fpregset_off))
11784
0
    return false;
11785
11786
0
  return true;
11787
0
}
11788
11789
static bool
11790
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11791
0
{
11792
0
  if (note == NULL)
11793
0
    return false;
11794
11795
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11796
     by the size of the descsz which matches the sizeof()
11797
     the type appropriate for that note type (e.g., prstatus_t for
11798
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11799
     on Solaris. The core file bitness may differ from the bitness of
11800
     gdb itself, so fixed values are used instead of sizeof().
11801
     Appropriate fixed offsets are also used to obtain data from
11802
     the note.  */
11803
11804
0
  switch ((int) note->type)
11805
0
    {
11806
0
    case SOLARIS_NT_PRSTATUS:
11807
0
      switch (note->descsz)
11808
0
  {
11809
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11810
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11811
0
                 136, 216, 308, 152, 356);
11812
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11813
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11814
0
                 264, 360, 520, 304, 600);
11815
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11816
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11817
0
                 136, 216, 308, 76, 356);
11818
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11819
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11820
0
                 264, 360, 520, 224, 600);
11821
0
  default:
11822
0
    return true;
11823
0
  }
11824
11825
0
    case SOLARIS_NT_PSINFO:
11826
0
    case SOLARIS_NT_PRPSINFO:
11827
0
      switch (note->descsz)
11828
0
  {
11829
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11830
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11831
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11832
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11833
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11834
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11835
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11836
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11837
0
  default:
11838
0
    return true;
11839
0
  }
11840
11841
0
    case SOLARIS_NT_LWPSTATUS:
11842
0
      switch (note->descsz)
11843
0
  {
11844
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11845
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11846
0
            152, 344, 400, 496);
11847
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11848
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11849
0
            304, 544, 544, 848);
11850
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11851
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11852
0
            76, 344, 380, 420);
11853
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11854
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11855
0
            224, 544, 528, 768);
11856
0
  default:
11857
0
    return true;
11858
0
  }
11859
11860
0
    case SOLARIS_NT_LWPSINFO:
11861
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11862
0
      if (note->descsz == 128 || note->descsz == 152)
11863
0
  elf_tdata (abfd)->core->lwpid =
11864
0
    bfd_get_32 (abfd, note->descdata + 4);
11865
0
      break;
11866
11867
0
    default:
11868
0
      break;
11869
0
    }
11870
11871
0
  return true;
11872
0
}
11873
11874
/* For name starting with "CORE" this may be either a Solaris
11875
   core file or a gdb-generated core file.  Do Solaris-specific
11876
   processing on selected note types first with
11877
   elfcore_grok_solaris_note(), then process the note
11878
   in elfcore_grok_note().  */
11879
11880
static bool
11881
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11882
0
{
11883
0
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11884
0
    return false;
11885
11886
0
  return elfcore_grok_note (abfd, note);
11887
0
}
11888
11889
static bool
11890
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11891
0
{
11892
0
  if (note->type == NT_OPENBSD_PROCINFO)
11893
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11894
11895
0
  if (note->type == NT_OPENBSD_REGS)
11896
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11897
11898
0
  if (note->type == NT_OPENBSD_FPREGS)
11899
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11900
11901
0
  if (note->type == NT_OPENBSD_XFPREGS)
11902
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11903
11904
0
  if (note->type == NT_OPENBSD_AUXV)
11905
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11906
11907
0
  if (note->type == NT_OPENBSD_WCOOKIE)
11908
0
    {
11909
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11910
0
                 SEC_HAS_CONTENTS);
11911
11912
0
      if (sect == NULL)
11913
0
  return false;
11914
0
      sect->size = note->descsz;
11915
0
      sect->filepos = note->descpos;
11916
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11917
11918
0
      return true;
11919
0
    }
11920
11921
0
  return true;
11922
0
}
11923
11924
static bool
11925
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11926
0
{
11927
0
  void *ddata = note->descdata;
11928
0
  char buf[100];
11929
0
  char *name;
11930
0
  asection *sect;
11931
0
  short sig;
11932
0
  unsigned flags;
11933
11934
0
  if (note->descsz < 16)
11935
0
    return false;
11936
11937
  /* nto_procfs_status 'pid' field is at offset 0.  */
11938
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11939
11940
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11941
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11942
11943
  /* nto_procfs_status 'flags' field is at offset 8.  */
11944
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11945
11946
  /* nto_procfs_status 'what' field is at offset 14.  */
11947
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11948
0
    {
11949
0
      elf_tdata (abfd)->core->signal = sig;
11950
0
      elf_tdata (abfd)->core->lwpid = *tid;
11951
0
    }
11952
11953
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11954
     do not come from signals so we make sure we set the current
11955
     thread just in case.  */
11956
0
  if (flags & 0x00000080)
11957
0
    elf_tdata (abfd)->core->lwpid = *tid;
11958
11959
  /* Make a ".qnx_core_status/%d" section.  */
11960
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11961
11962
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11963
0
  if (name == NULL)
11964
0
    return false;
11965
0
  strcpy (name, buf);
11966
11967
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11968
0
  if (sect == NULL)
11969
0
    return false;
11970
11971
0
  sect->size    = note->descsz;
11972
0
  sect->filepos   = note->descpos;
11973
0
  sect->alignment_power = 2;
11974
11975
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11976
0
}
11977
11978
static bool
11979
elfcore_grok_nto_regs (bfd *abfd,
11980
           Elf_Internal_Note *note,
11981
           long tid,
11982
           char *base)
11983
0
{
11984
0
  char buf[100];
11985
0
  char *name;
11986
0
  asection *sect;
11987
11988
  /* Make a "(base)/%d" section.  */
11989
0
  sprintf (buf, "%s/%ld", base, tid);
11990
11991
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11992
0
  if (name == NULL)
11993
0
    return false;
11994
0
  strcpy (name, buf);
11995
11996
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11997
0
  if (sect == NULL)
11998
0
    return false;
11999
12000
0
  sect->size    = note->descsz;
12001
0
  sect->filepos   = note->descpos;
12002
0
  sect->alignment_power = 2;
12003
12004
  /* This is the current thread.  */
12005
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12006
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12007
12008
0
  return true;
12009
0
}
12010
12011
static bool
12012
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12013
32
{
12014
  /* Every GREG section has a STATUS section before it.  Store the
12015
     tid from the previous call to pass down to the next gregs
12016
     function.  */
12017
32
  static long tid = 1;
12018
12019
32
  switch (note->type)
12020
32
    {
12021
0
    case QNT_CORE_INFO:
12022
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12023
0
    case QNT_CORE_STATUS:
12024
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12025
0
    case QNT_CORE_GREG:
12026
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
12027
0
    case QNT_CORE_FPREG:
12028
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
12029
32
    default:
12030
32
      return true;
12031
32
    }
12032
32
}
12033
12034
static bool
12035
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12036
0
{
12037
0
  char *name;
12038
0
  asection *sect;
12039
0
  size_t len;
12040
12041
  /* Use note name as section name.  */
12042
0
  len = note->namesz;
12043
0
  name = (char *) bfd_alloc (abfd, len);
12044
0
  if (name == NULL)
12045
0
    return false;
12046
0
  memcpy (name, note->namedata, len);
12047
0
  name[len - 1] = '\0';
12048
12049
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12050
0
  if (sect == NULL)
12051
0
    return false;
12052
12053
0
  sect->size    = note->descsz;
12054
0
  sect->filepos   = note->descpos;
12055
0
  sect->alignment_power = 1;
12056
12057
0
  return true;
12058
0
}
12059
12060
/* Function: elfcore_write_note
12061
12062
   Inputs:
12063
     buffer to hold note, and current size of buffer
12064
     name of note
12065
     type of note
12066
     data for note
12067
     size of data for note
12068
12069
   Writes note to end of buffer.  ELF64 notes are written exactly as
12070
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12071
   that they ought to have 8-byte namesz and descsz field, and have
12072
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12073
12074
   Return:
12075
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12076
12077
char *
12078
elfcore_write_note (bfd *abfd,
12079
        char *buf,
12080
        int *bufsiz,
12081
        const char *name,
12082
        int type,
12083
        const void *input,
12084
        int size)
12085
0
{
12086
0
  Elf_External_Note *xnp;
12087
0
  size_t namesz;
12088
0
  size_t newspace;
12089
0
  char *dest;
12090
12091
0
  namesz = 0;
12092
0
  if (name != NULL)
12093
0
    namesz = strlen (name) + 1;
12094
12095
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12096
12097
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12098
0
  if (buf == NULL)
12099
0
    return buf;
12100
0
  dest = buf + *bufsiz;
12101
0
  *bufsiz += newspace;
12102
0
  xnp = (Elf_External_Note *) dest;
12103
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12104
0
  H_PUT_32 (abfd, size, xnp->descsz);
12105
0
  H_PUT_32 (abfd, type, xnp->type);
12106
0
  dest = xnp->name;
12107
0
  if (name != NULL)
12108
0
    {
12109
0
      memcpy (dest, name, namesz);
12110
0
      dest += namesz;
12111
0
      while (namesz & 3)
12112
0
  {
12113
0
    *dest++ = '\0';
12114
0
    ++namesz;
12115
0
  }
12116
0
    }
12117
0
  memcpy (dest, input, size);
12118
0
  dest += size;
12119
0
  while (size & 3)
12120
0
    {
12121
0
      *dest++ = '\0';
12122
0
      ++size;
12123
0
    }
12124
0
  return buf;
12125
0
}
12126
12127
/* gcc-8 warns (*) on all the strncpy calls in this function about
12128
   possible string truncation.  The "truncation" is not a bug.  We
12129
   have an external representation of structs with fields that are not
12130
   necessarily NULL terminated and corresponding internal
12131
   representation fields that are one larger so that they can always
12132
   be NULL terminated.
12133
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12134
   diagnostics inside functions, giving a hard error if you try to use
12135
   the finer control available with later versions.
12136
   gcc prior to 4.2 warns about diagnostic push and pop.
12137
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12138
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12139
   (*) Depending on your system header files!  */
12140
#if GCC_VERSION >= 8000
12141
# pragma GCC diagnostic push
12142
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12143
#endif
12144
char *
12145
elfcore_write_prpsinfo (bfd  *abfd,
12146
      char *buf,
12147
      int  *bufsiz,
12148
      const char *fname,
12149
      const char *psargs)
12150
0
{
12151
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12152
12153
0
  if (bed->elf_backend_write_core_note != NULL)
12154
0
    {
12155
0
      char *ret;
12156
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12157
0
             NT_PRPSINFO, fname, psargs);
12158
0
      if (ret != NULL)
12159
0
  return ret;
12160
0
    }
12161
12162
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12163
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12164
0
  if (bed->s->elfclass == ELFCLASS32)
12165
0
    {
12166
#  if defined (HAVE_PSINFO32_T)
12167
      psinfo32_t data;
12168
      int note_type = NT_PSINFO;
12169
#  else
12170
0
      prpsinfo32_t data;
12171
0
      int note_type = NT_PRPSINFO;
12172
0
#  endif
12173
12174
0
      memset (&data, 0, sizeof (data));
12175
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12176
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12177
0
      return elfcore_write_note (abfd, buf, bufsiz,
12178
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12179
0
    }
12180
0
  else
12181
0
# endif
12182
0
    {
12183
# if defined (HAVE_PSINFO_T)
12184
      psinfo_t data;
12185
      int note_type = NT_PSINFO;
12186
# else
12187
0
      prpsinfo_t data;
12188
0
      int note_type = NT_PRPSINFO;
12189
0
# endif
12190
12191
0
      memset (&data, 0, sizeof (data));
12192
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12193
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12194
0
      return elfcore_write_note (abfd, buf, bufsiz,
12195
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12196
0
    }
12197
0
#endif  /* PSINFO_T or PRPSINFO_T */
12198
12199
0
  free (buf);
12200
0
  return NULL;
12201
0
}
12202
#if GCC_VERSION >= 8000
12203
# pragma GCC diagnostic pop
12204
#endif
12205
12206
char *
12207
elfcore_write_linux_prpsinfo32
12208
  (bfd *abfd, char *buf, int *bufsiz,
12209
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12210
0
{
12211
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12212
0
    {
12213
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12214
12215
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12216
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12217
0
         &data, sizeof (data));
12218
0
    }
12219
0
  else
12220
0
    {
12221
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12222
12223
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12224
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12225
0
         &data, sizeof (data));
12226
0
    }
12227
0
}
12228
12229
char *
12230
elfcore_write_linux_prpsinfo64
12231
  (bfd *abfd, char *buf, int *bufsiz,
12232
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12233
0
{
12234
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12235
0
    {
12236
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12237
12238
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12239
0
      return elfcore_write_note (abfd, buf, bufsiz,
12240
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12241
0
    }
12242
0
  else
12243
0
    {
12244
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12245
12246
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12247
0
      return elfcore_write_note (abfd, buf, bufsiz,
12248
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12249
0
    }
12250
0
}
12251
12252
char *
12253
elfcore_write_prstatus (bfd *abfd,
12254
      char *buf,
12255
      int *bufsiz,
12256
      long pid,
12257
      int cursig,
12258
      const void *gregs)
12259
0
{
12260
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12261
12262
0
  if (bed->elf_backend_write_core_note != NULL)
12263
0
    {
12264
0
      char *ret;
12265
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12266
0
             NT_PRSTATUS,
12267
0
             pid, cursig, gregs);
12268
0
      if (ret != NULL)
12269
0
  return ret;
12270
0
    }
12271
12272
0
#if defined (HAVE_PRSTATUS_T)
12273
0
#if defined (HAVE_PRSTATUS32_T)
12274
0
  if (bed->s->elfclass == ELFCLASS32)
12275
0
    {
12276
0
      prstatus32_t prstat;
12277
12278
0
      memset (&prstat, 0, sizeof (prstat));
12279
0
      prstat.pr_pid = pid;
12280
0
      prstat.pr_cursig = cursig;
12281
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12282
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12283
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12284
0
    }
12285
0
  else
12286
0
#endif
12287
0
    {
12288
0
      prstatus_t prstat;
12289
12290
0
      memset (&prstat, 0, sizeof (prstat));
12291
0
      prstat.pr_pid = pid;
12292
0
      prstat.pr_cursig = cursig;
12293
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12294
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12295
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12296
0
    }
12297
0
#endif /* HAVE_PRSTATUS_T */
12298
12299
0
  free (buf);
12300
0
  return NULL;
12301
0
}
12302
12303
#if defined (HAVE_LWPSTATUS_T)
12304
char *
12305
elfcore_write_lwpstatus (bfd *abfd,
12306
       char *buf,
12307
       int *bufsiz,
12308
       long pid,
12309
       int cursig,
12310
       const void *gregs)
12311
{
12312
  lwpstatus_t lwpstat;
12313
12314
  memset (&lwpstat, 0, sizeof (lwpstat));
12315
  lwpstat.pr_lwpid  = pid >> 16;
12316
  lwpstat.pr_cursig = cursig;
12317
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12318
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12319
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12320
#if !defined(gregs)
12321
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12322
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12323
#else
12324
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12325
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12326
#endif
12327
#endif
12328
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12329
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12330
}
12331
#endif /* HAVE_LWPSTATUS_T */
12332
12333
#if defined (HAVE_PSTATUS_T)
12334
char *
12335
elfcore_write_pstatus (bfd *abfd,
12336
           char *buf,
12337
           int *bufsiz,
12338
           long pid,
12339
           int cursig ATTRIBUTE_UNUSED,
12340
           const void *gregs ATTRIBUTE_UNUSED)
12341
{
12342
#if defined (HAVE_PSTATUS32_T)
12343
  elf_backend_data *bed = get_elf_backend_data (abfd);
12344
12345
  if (bed->s->elfclass == ELFCLASS32)
12346
    {
12347
      pstatus32_t pstat;
12348
12349
      memset (&pstat, 0, sizeof (pstat));
12350
      pstat.pr_pid = pid & 0xffff;
12351
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12352
        NT_PSTATUS, &pstat, sizeof (pstat));
12353
      return buf;
12354
    }
12355
  else
12356
#endif
12357
    {
12358
      pstatus_t pstat;
12359
12360
      memset (&pstat, 0, sizeof (pstat));
12361
      pstat.pr_pid = pid & 0xffff;
12362
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12363
        NT_PSTATUS, &pstat, sizeof (pstat));
12364
      return buf;
12365
    }
12366
}
12367
#endif /* HAVE_PSTATUS_T */
12368
12369
char *
12370
elfcore_write_prfpreg (bfd *abfd,
12371
           char *buf,
12372
           int *bufsiz,
12373
           const void *fpregs,
12374
           int size)
12375
0
{
12376
0
  return elfcore_write_note (abfd, buf, bufsiz,
12377
0
           NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
12378
0
}
12379
12380
char *
12381
elfcore_write_prxfpreg (bfd *abfd,
12382
      char *buf,
12383
      int *bufsiz,
12384
      const void *xfpregs,
12385
      int size)
12386
0
{
12387
0
  return elfcore_write_note (abfd, buf, bufsiz,
12388
0
           NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
12389
0
}
12390
12391
char *
12392
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12393
       const void *xfpregs, int size)
12394
0
{
12395
0
  char *note_name;
12396
12397
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12398
0
    note_name = NOTE_NAME_FREEBSD;
12399
0
  else
12400
0
    note_name = NOTE_NAME_LINUX;
12401
12402
0
  return elfcore_write_note (abfd, buf, bufsiz,
12403
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12404
0
}
12405
12406
char *
12407
elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
12408
          const void *xsave_layout, int size)
12409
0
{
12410
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
12411
0
           NT_X86_XSAVE_LAYOUT, xsave_layout, size);
12412
0
}
12413
12414
static char *
12415
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12416
          const void *ssp, int size)
12417
0
{
12418
0
  return elfcore_write_note (abfd, buf, bufsiz,
12419
0
           NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
12420
0
}
12421
12422
char *
12423
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12424
          const void *regs, int size)
12425
0
{
12426
0
  return elfcore_write_note (abfd, buf, bufsiz,
12427
0
           NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
12428
0
           regs, size);
12429
0
}
12430
12431
char *
12432
elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
12433
          const void *regs, int size)
12434
0
{
12435
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
12436
0
           regs, size);
12437
0
}
12438
12439
char *
12440
elfcore_write_ppc_vmx (bfd *abfd,
12441
           char *buf,
12442
           int *bufsiz,
12443
           const void *ppc_vmx,
12444
           int size)
12445
0
{
12446
0
  return elfcore_write_note (abfd, buf, bufsiz,
12447
0
           NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
12448
0
}
12449
12450
char *
12451
elfcore_write_ppc_vsx (bfd *abfd,
12452
           char *buf,
12453
           int *bufsiz,
12454
           const void *ppc_vsx,
12455
           int size)
12456
0
{
12457
0
  return elfcore_write_note (abfd, buf, bufsiz,
12458
0
           NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
12459
0
}
12460
12461
char *
12462
elfcore_write_ppc_tar (bfd *abfd,
12463
           char *buf,
12464
           int *bufsiz,
12465
           const void *ppc_tar,
12466
           int size)
12467
0
{
12468
0
  return elfcore_write_note (abfd, buf, bufsiz,
12469
0
           NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
12470
0
}
12471
12472
char *
12473
elfcore_write_ppc_ppr (bfd *abfd,
12474
           char *buf,
12475
           int *bufsiz,
12476
           const void *ppc_ppr,
12477
           int size)
12478
0
{
12479
0
  return elfcore_write_note (abfd, buf, bufsiz,
12480
0
           NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
12481
0
}
12482
12483
char *
12484
elfcore_write_ppc_dscr (bfd *abfd,
12485
      char *buf,
12486
      int *bufsiz,
12487
      const void *ppc_dscr,
12488
      int size)
12489
0
{
12490
0
  return elfcore_write_note (abfd, buf, bufsiz,
12491
0
           NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
12492
0
}
12493
12494
char *
12495
elfcore_write_ppc_ebb (bfd *abfd,
12496
           char *buf,
12497
           int *bufsiz,
12498
           const void *ppc_ebb,
12499
           int size)
12500
0
{
12501
0
  return elfcore_write_note (abfd, buf, bufsiz,
12502
0
           NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
12503
0
}
12504
12505
char *
12506
elfcore_write_ppc_pmu (bfd *abfd,
12507
           char *buf,
12508
           int *bufsiz,
12509
           const void *ppc_pmu,
12510
           int size)
12511
0
{
12512
0
  return elfcore_write_note (abfd, buf, bufsiz,
12513
0
           NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
12514
0
}
12515
12516
char *
12517
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12518
         char *buf,
12519
         int *bufsiz,
12520
         const void *ppc_tm_cgpr,
12521
         int size)
12522
0
{
12523
0
  return elfcore_write_note (abfd, buf, bufsiz,
12524
0
           NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
12525
0
           ppc_tm_cgpr, size);
12526
0
}
12527
12528
char *
12529
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12530
         char *buf,
12531
         int *bufsiz,
12532
         const void *ppc_tm_cfpr,
12533
         int size)
12534
0
{
12535
0
  return elfcore_write_note (abfd, buf, bufsiz,
12536
0
           NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
12537
0
           ppc_tm_cfpr, size);
12538
0
}
12539
12540
char *
12541
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12542
         char *buf,
12543
         int *bufsiz,
12544
         const void *ppc_tm_cvmx,
12545
         int size)
12546
0
{
12547
0
  return elfcore_write_note (abfd, buf, bufsiz,
12548
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
12549
0
           ppc_tm_cvmx, size);
12550
0
}
12551
12552
char *
12553
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12554
         char *buf,
12555
         int *bufsiz,
12556
         const void *ppc_tm_cvsx,
12557
         int size)
12558
0
{
12559
0
  return elfcore_write_note (abfd, buf, bufsiz,
12560
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
12561
0
           ppc_tm_cvsx, size);
12562
0
}
12563
12564
char *
12565
elfcore_write_ppc_tm_spr (bfd *abfd,
12566
        char *buf,
12567
        int *bufsiz,
12568
        const void *ppc_tm_spr,
12569
        int size)
12570
0
{
12571
0
  return elfcore_write_note (abfd, buf, bufsiz,
12572
0
           NOTE_NAME_LINUX, NT_PPC_TM_SPR,
12573
0
           ppc_tm_spr, size);
12574
0
}
12575
12576
char *
12577
elfcore_write_ppc_tm_ctar (bfd *abfd,
12578
         char *buf,
12579
         int *bufsiz,
12580
         const void *ppc_tm_ctar,
12581
         int size)
12582
0
{
12583
0
  return elfcore_write_note (abfd, buf, bufsiz,
12584
0
           NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
12585
0
           ppc_tm_ctar, size);
12586
0
}
12587
12588
char *
12589
elfcore_write_ppc_tm_cppr (bfd *abfd,
12590
         char *buf,
12591
         int *bufsiz,
12592
         const void *ppc_tm_cppr,
12593
         int size)
12594
0
{
12595
0
  return elfcore_write_note (abfd, buf, bufsiz,
12596
0
           NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
12597
0
           ppc_tm_cppr, size);
12598
0
}
12599
12600
char *
12601
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12602
          char *buf,
12603
          int *bufsiz,
12604
          const void *ppc_tm_cdscr,
12605
          int size)
12606
0
{
12607
0
  return elfcore_write_note (abfd, buf, bufsiz,
12608
0
           NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
12609
0
           ppc_tm_cdscr, size);
12610
0
}
12611
12612
static char *
12613
elfcore_write_s390_high_gprs (bfd *abfd,
12614
            char *buf,
12615
            int *bufsiz,
12616
            const void *s390_high_gprs,
12617
            int size)
12618
0
{
12619
0
  return elfcore_write_note (abfd, buf, bufsiz,
12620
0
           NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
12621
0
           s390_high_gprs, size);
12622
0
}
12623
12624
char *
12625
elfcore_write_s390_timer (bfd *abfd,
12626
        char *buf,
12627
        int *bufsiz,
12628
        const void *s390_timer,
12629
        int size)
12630
0
{
12631
0
  return elfcore_write_note (abfd, buf, bufsiz,
12632
0
           NOTE_NAME_LINUX, NT_S390_TIMER,
12633
0
           s390_timer, size);
12634
0
}
12635
12636
char *
12637
elfcore_write_s390_todcmp (bfd *abfd,
12638
         char *buf,
12639
         int *bufsiz,
12640
         const void *s390_todcmp,
12641
         int size)
12642
0
{
12643
0
  return elfcore_write_note (abfd, buf, bufsiz,
12644
0
           NOTE_NAME_LINUX, NT_S390_TODCMP,
12645
0
           s390_todcmp, size);
12646
0
}
12647
12648
char *
12649
elfcore_write_s390_todpreg (bfd *abfd,
12650
          char *buf,
12651
          int *bufsiz,
12652
          const void *s390_todpreg,
12653
          int size)
12654
0
{
12655
0
  return elfcore_write_note (abfd, buf, bufsiz,
12656
0
           NOTE_NAME_LINUX, NT_S390_TODPREG,
12657
0
           s390_todpreg, size);
12658
0
}
12659
12660
char *
12661
elfcore_write_s390_ctrs (bfd *abfd,
12662
       char *buf,
12663
       int *bufsiz,
12664
       const void *s390_ctrs,
12665
       int size)
12666
0
{
12667
0
  return elfcore_write_note (abfd, buf, bufsiz,
12668
0
           NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
12669
0
}
12670
12671
char *
12672
elfcore_write_s390_prefix (bfd *abfd,
12673
         char *buf,
12674
         int *bufsiz,
12675
         const void *s390_prefix,
12676
         int size)
12677
0
{
12678
0
  return elfcore_write_note (abfd, buf, bufsiz,
12679
0
           NOTE_NAME_LINUX, NT_S390_PREFIX,
12680
0
           s390_prefix, size);
12681
0
}
12682
12683
char *
12684
elfcore_write_s390_last_break (bfd *abfd,
12685
             char *buf,
12686
             int *bufsiz,
12687
             const void *s390_last_break,
12688
             int size)
12689
0
{
12690
0
  return elfcore_write_note (abfd, buf, bufsiz,
12691
0
           NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
12692
0
           s390_last_break, size);
12693
0
}
12694
12695
char *
12696
elfcore_write_s390_system_call (bfd *abfd,
12697
        char *buf,
12698
        int *bufsiz,
12699
        const void *s390_system_call,
12700
        int size)
12701
0
{
12702
0
  return elfcore_write_note (abfd, buf, bufsiz,
12703
0
           NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
12704
0
           s390_system_call, size);
12705
0
}
12706
12707
char *
12708
elfcore_write_s390_tdb (bfd *abfd,
12709
      char *buf,
12710
      int *bufsiz,
12711
      const void *s390_tdb,
12712
      int size)
12713
0
{
12714
0
  return elfcore_write_note (abfd, buf, bufsiz,
12715
0
           NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
12716
0
}
12717
12718
char *
12719
elfcore_write_s390_vxrs_low (bfd *abfd,
12720
           char *buf,
12721
           int *bufsiz,
12722
           const void *s390_vxrs_low,
12723
           int size)
12724
0
{
12725
0
  return elfcore_write_note (abfd, buf, bufsiz,
12726
0
           NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
12727
0
           s390_vxrs_low, size);
12728
0
}
12729
12730
char *
12731
elfcore_write_s390_vxrs_high (bfd *abfd,
12732
           char *buf,
12733
           int *bufsiz,
12734
           const void *s390_vxrs_high,
12735
           int size)
12736
0
{
12737
0
  return elfcore_write_note (abfd, buf, bufsiz,
12738
0
           NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
12739
0
           s390_vxrs_high, size);
12740
0
}
12741
12742
char *
12743
elfcore_write_s390_gs_cb (bfd *abfd,
12744
        char *buf,
12745
        int *bufsiz,
12746
        const void *s390_gs_cb,
12747
        int size)
12748
0
{
12749
0
  return elfcore_write_note (abfd, buf, bufsiz,
12750
0
           NOTE_NAME_LINUX, NT_S390_GS_CB,
12751
0
           s390_gs_cb, size);
12752
0
}
12753
12754
char *
12755
elfcore_write_s390_gs_bc (bfd *abfd,
12756
        char *buf,
12757
        int *bufsiz,
12758
        const void *s390_gs_bc,
12759
        int size)
12760
0
{
12761
0
  return elfcore_write_note (abfd, buf, bufsiz,
12762
0
           NOTE_NAME_LINUX, NT_S390_GS_BC,
12763
0
           s390_gs_bc, size);
12764
0
}
12765
12766
char *
12767
elfcore_write_arm_vfp (bfd *abfd,
12768
           char *buf,
12769
           int *bufsiz,
12770
           const void *arm_vfp,
12771
           int size)
12772
0
{
12773
0
  return elfcore_write_note (abfd, buf, bufsiz,
12774
0
           NOTE_NAME_LINUX, NT_ARM_VFP,
12775
0
           arm_vfp, size);
12776
0
}
12777
12778
char *
12779
elfcore_write_aarch_tls (bfd *abfd,
12780
           char *buf,
12781
           int *bufsiz,
12782
           const void *aarch_tls,
12783
           int size)
12784
0
{
12785
0
  return elfcore_write_note (abfd, buf, bufsiz,
12786
0
           NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
12787
0
}
12788
12789
char *
12790
elfcore_write_aarch_hw_break (bfd *abfd,
12791
          char *buf,
12792
          int *bufsiz,
12793
          const void *aarch_hw_break,
12794
          int size)
12795
0
{
12796
0
  return elfcore_write_note (abfd, buf, bufsiz,
12797
0
           NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
12798
0
           aarch_hw_break, size);
12799
0
}
12800
12801
char *
12802
elfcore_write_aarch_hw_watch (bfd *abfd,
12803
          char *buf,
12804
          int *bufsiz,
12805
          const void *aarch_hw_watch,
12806
          int size)
12807
0
{
12808
0
  return elfcore_write_note (abfd, buf, bufsiz,
12809
0
           NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
12810
0
           aarch_hw_watch, size);
12811
0
}
12812
12813
char *
12814
elfcore_write_aarch_sve (bfd *abfd,
12815
       char *buf,
12816
       int *bufsiz,
12817
       const void *aarch_sve,
12818
       int size)
12819
0
{
12820
0
  return elfcore_write_note (abfd, buf, bufsiz,
12821
0
           NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
12822
0
}
12823
12824
char *
12825
elfcore_write_aarch_pauth (bfd *abfd,
12826
         char *buf,
12827
         int *bufsiz,
12828
         const void *aarch_pauth,
12829
         int size)
12830
0
{
12831
0
  return elfcore_write_note (abfd, buf, bufsiz,
12832
0
           NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
12833
0
           aarch_pauth, size);
12834
0
}
12835
12836
char *
12837
elfcore_write_aarch_mte (bfd *abfd,
12838
       char *buf,
12839
       int *bufsiz,
12840
       const void *aarch_mte,
12841
       int size)
12842
0
{
12843
0
  return elfcore_write_note (abfd, buf, bufsiz,
12844
0
           NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
12845
0
           aarch_mte, size);
12846
0
}
12847
12848
char *
12849
elfcore_write_aarch_ssve (bfd *abfd,
12850
        char *buf,
12851
        int *bufsiz,
12852
        const void *aarch_ssve,
12853
        int size)
12854
0
{
12855
0
  return elfcore_write_note (abfd, buf, bufsiz,
12856
0
           NOTE_NAME_LINUX, NT_ARM_SSVE,
12857
0
           aarch_ssve, size);
12858
0
}
12859
12860
char *
12861
elfcore_write_aarch_za (bfd *abfd,
12862
      char *buf,
12863
      int *bufsiz,
12864
      const void *aarch_za,
12865
      int size)
12866
0
{
12867
0
  return elfcore_write_note (abfd, buf, bufsiz,
12868
0
           NOTE_NAME_LINUX, NT_ARM_ZA,
12869
0
           aarch_za, size);
12870
0
}
12871
12872
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
12873
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12874
   written into.  Return a pointer to the new start of the note buffer, to
12875
   replace BUF which may no longer be valid.  */
12876
12877
char *
12878
elfcore_write_aarch_zt (bfd *abfd,
12879
      char *buf,
12880
      int *bufsiz,
12881
      const void *aarch_zt,
12882
      int size)
12883
0
{
12884
0
  return elfcore_write_note (abfd, buf, bufsiz,
12885
0
           NOTE_NAME_LINUX, NT_ARM_ZT,
12886
0
           aarch_zt, size);
12887
0
}
12888
12889
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
12890
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12891
   written into.  Return a pointer to the new start of the note buffer, to
12892
   replace BUF which may no longer be valid.  */
12893
12894
static char *
12895
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
12896
       const void *aarch_gcs, int size)
12897
0
{
12898
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
12899
0
           aarch_gcs, size);
12900
0
}
12901
12902
/* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
12903
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12904
   written into.  Return a pointer to the new start of the note buffer, to
12905
   replace BUF which may no longer be valid.  */
12906
12907
char *
12908
elfcore_write_aarch_fpmr (bfd *abfd,
12909
      char *buf,
12910
      int *bufsiz,
12911
      const void *aarch_fpmr,
12912
      int size)
12913
0
{
12914
0
  return elfcore_write_note (abfd, buf, bufsiz,
12915
0
           NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
12916
0
}
12917
12918
char *
12919
elfcore_write_arc_v2 (bfd *abfd,
12920
          char *buf,
12921
          int *bufsiz,
12922
          const void *arc_v2,
12923
          int size)
12924
0
{
12925
0
  return elfcore_write_note (abfd, buf, bufsiz,
12926
0
           NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
12927
0
}
12928
12929
char *
12930
elfcore_write_loongarch_cpucfg (bfd *abfd,
12931
        char *buf,
12932
        int *bufsiz,
12933
        const void *loongarch_cpucfg,
12934
        int size)
12935
0
{
12936
0
  return elfcore_write_note (abfd, buf, bufsiz,
12937
0
           NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
12938
0
           loongarch_cpucfg, size);
12939
0
}
12940
12941
char *
12942
elfcore_write_loongarch_lbt (bfd *abfd,
12943
           char *buf,
12944
           int *bufsiz,
12945
           const void *loongarch_lbt,
12946
           int size)
12947
0
{
12948
0
  return elfcore_write_note (abfd, buf, bufsiz,
12949
0
           NOTE_NAME_LINUX, NT_LARCH_LBT,
12950
0
           loongarch_lbt, size);
12951
0
}
12952
12953
char *
12954
elfcore_write_loongarch_lsx (bfd *abfd,
12955
           char *buf,
12956
           int *bufsiz,
12957
           const void *loongarch_lsx,
12958
           int size)
12959
0
{
12960
0
  return elfcore_write_note (abfd, buf, bufsiz,
12961
0
           NOTE_NAME_LINUX, NT_LARCH_LSX,
12962
0
           loongarch_lsx, size);
12963
0
}
12964
12965
char *
12966
elfcore_write_loongarch_lasx (bfd *abfd,
12967
            char *buf,
12968
            int *bufsiz,
12969
            const void *loongarch_lasx,
12970
            int size)
12971
0
{
12972
0
  return elfcore_write_note (abfd, buf, bufsiz,
12973
0
           NOTE_NAME_LINUX, NT_LARCH_LASX,
12974
0
           loongarch_lasx, size);
12975
0
}
12976
12977
/* Write the buffer of csr values in CSRS (length SIZE) into the note
12978
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12979
   written into.  Return a pointer to the new start of the note buffer, to
12980
   replace BUF which may no longer be valid.  */
12981
12982
char *
12983
elfcore_write_riscv_csr (bfd *abfd,
12984
       char *buf,
12985
       int *bufsiz,
12986
       const void *csrs,
12987
       int size)
12988
0
{
12989
0
  return elfcore_write_note (abfd, buf, bufsiz,
12990
0
           NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
12991
0
}
12992
12993
/* Write the target description (a string) pointed to by TDESC, length
12994
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
12995
   note is being written into.  Return a pointer to the new start of the
12996
   note buffer, to replace BUF which may no longer be valid.  */
12997
12998
char *
12999
elfcore_write_gdb_tdesc (bfd *abfd,
13000
       char *buf,
13001
       int *bufsiz,
13002
       const void *tdesc,
13003
       int size)
13004
0
{
13005
0
  return elfcore_write_note (abfd, buf, bufsiz,
13006
0
           NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
13007
0
}
13008
13009
char *
13010
elfcore_write_register_note (bfd *abfd,
13011
           char *buf,
13012
           int *bufsiz,
13013
           const char *section,
13014
           const void *data,
13015
           int size)
13016
0
{
13017
0
  static const struct
13018
0
    {
13019
0
      const char * section_name;
13020
0
      char *       (*writer) (bfd *, char *, int *, const void *, int);
13021
0
    }
13022
0
  note_writers [] =
13023
0
    {
13024
0
      { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
13025
0
      { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
13026
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
13027
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
13028
0
      { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
13029
0
      { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
13030
0
      { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
13031
0
      { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
13032
0
      { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
13033
0
      { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
13034
0
      { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
13035
0
      { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
13036
0
      { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
13037
0
      { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
13038
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
13039
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
13040
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
13041
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
13042
0
      { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
13043
0
      { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
13044
0
      { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
13045
0
      { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
13046
0
      { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
13047
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
13048
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
13049
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
13050
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
13051
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
13052
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
13053
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
13054
0
      { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
13055
0
      { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
13056
0
      { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
13057
0
      { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
13058
0
      { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
13059
0
      { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
13060
0
      { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
13061
0
      { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
13062
0
      { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
13063
0
      { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
13064
0
      { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
13065
0
      { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
13066
0
      { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
13067
0
      { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
13068
0
      { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
13069
0
      { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
13070
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
13071
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
13072
0
      { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
13073
0
      { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
13074
0
      { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
13075
0
      { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
13076
0
      { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
13077
0
      { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
13078
0
    };
13079
13080
0
  int i;
13081
13082
0
  for (i = ARRAY_SIZE (note_writers); i--;)
13083
0
    if (streq (section, note_writers[i].section_name))
13084
0
      return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
13085
13086
0
  return NULL;
13087
0
}
13088
13089
char *
13090
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13091
       const void *buf, int bufsiz)
13092
0
{
13093
0
  return elfcore_write_note (obfd, note_data, note_size,
13094
0
           NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
13095
0
}
13096
13097
static bool
13098
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13099
     size_t align)
13100
6.85k
{
13101
6.85k
  char *p;
13102
13103
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13104
     gABI specifies that PT_NOTE alignment should be aligned to 4
13105
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13106
     align is less than 4, we use 4 byte alignment.   */
13107
6.85k
  if (align < 4)
13108
2.58k
    align = 4;
13109
6.85k
  if (align != 4 && align != 8)
13110
1.93k
    return false;
13111
13112
4.91k
  p = buf;
13113
12.8k
  while (p < buf + size)
13114
11.7k
    {
13115
11.7k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13116
11.7k
      Elf_Internal_Note in;
13117
13118
11.7k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13119
600
  return false;
13120
13121
11.1k
      in.type = H_GET_32 (abfd, xnp->type);
13122
13123
11.1k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13124
11.1k
      in.namedata = xnp->name;
13125
11.1k
      if (in.namesz > buf - in.namedata + size)
13126
1.76k
  return false;
13127
13128
9.41k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13129
9.41k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13130
9.41k
      in.descpos = offset + (in.descdata - buf);
13131
9.41k
      if (in.descsz != 0
13132
4.49k
    && (in.descdata >= buf + size
13133
4.33k
        || in.descsz > buf - in.descdata + size))
13134
1.17k
  return false;
13135
13136
8.23k
      switch (bfd_get_format (abfd))
13137
8.23k
  {
13138
0
  default:
13139
0
    return true;
13140
13141
1.06k
  case bfd_core:
13142
1.06k
    {
13143
8.50k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13144
1.06k
      static const struct
13145
1.06k
      {
13146
1.06k
        const char * string;
13147
1.06k
        size_t len;
13148
1.06k
        bool (*func) (bfd *, Elf_Internal_Note *);
13149
1.06k
      }
13150
1.06k
      grokers[] =
13151
1.06k
      {
13152
1.06k
        GROKER_ELEMENT ("", elfcore_grok_note),
13153
1.06k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13154
1.06k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13155
1.06k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13156
1.06k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13157
1.06k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13158
1.06k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13159
1.06k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13160
1.06k
      };
13161
1.06k
#undef GROKER_ELEMENT
13162
1.06k
      int i;
13163
13164
8.37k
      for (i = ARRAY_SIZE (grokers); i--;)
13165
8.37k
        {
13166
8.37k
    if (in.namesz >= grokers[i].len
13167
3.52k
        && strncmp (in.namedata, grokers[i].string,
13168
3.52k
        grokers[i].len) == 0)
13169
1.06k
      {
13170
1.06k
        if (! grokers[i].func (abfd, & in))
13171
0
          return false;
13172
1.06k
        break;
13173
1.06k
      }
13174
8.37k
        }
13175
1.06k
      break;
13176
1.06k
    }
13177
13178
7.17k
  case bfd_object:
13179
7.17k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13180
978
      {
13181
978
        if (! elfobj_grok_gnu_note (abfd, &in))
13182
308
    return false;
13183
978
      }
13184
6.19k
    else if (in.namesz == sizeof "stapsdt"
13185
409
       && strcmp (in.namedata, "stapsdt") == 0)
13186
0
      {
13187
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13188
0
    return false;
13189
0
      }
13190
6.86k
    break;
13191
8.23k
  }
13192
13193
7.92k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13194
7.92k
    }
13195
13196
1.06k
  return true;
13197
4.91k
}
13198
13199
bool
13200
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13201
         size_t align)
13202
3.38k
{
13203
3.38k
  char *buf;
13204
13205
3.38k
  if (size == 0 || (size + 1) == 0)
13206
560
    return true;
13207
13208
2.82k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13209
1.13k
    return false;
13210
13211
1.68k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13212
1.68k
  if (buf == NULL)
13213
865
    return false;
13214
13215
  /* PR 17512: file: ec08f814
13216
     0-termintate the buffer so that string searches will not overflow.  */
13217
824
  buf[size] = 0;
13218
13219
824
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13220
817
    {
13221
817
      free (buf);
13222
817
      return false;
13223
817
    }
13224
13225
7
  free (buf);
13226
7
  return true;
13227
824
}
13228

13229
/* Providing external access to the ELF program header table.  */
13230
13231
/* Return an upper bound on the number of bytes required to store a
13232
   copy of ABFD's program header table entries.  Return -1 if an error
13233
   occurs; bfd_get_error will return an appropriate code.  */
13234
13235
long
13236
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13237
0
{
13238
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13239
0
    {
13240
0
      bfd_set_error (bfd_error_wrong_format);
13241
0
      return -1;
13242
0
    }
13243
13244
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13245
0
}
13246
13247
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13248
   will be stored as an array of Elf_Internal_Phdr structures, as
13249
   defined in include/elf/internal.h.  To find out how large the
13250
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13251
13252
   Return the number of program header table entries read, or -1 if an
13253
   error occurs; bfd_get_error will return an appropriate code.  */
13254
13255
int
13256
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13257
0
{
13258
0
  int num_phdrs;
13259
13260
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13261
0
    {
13262
0
      bfd_set_error (bfd_error_wrong_format);
13263
0
      return -1;
13264
0
    }
13265
13266
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13267
0
  if (num_phdrs != 0)
13268
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13269
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13270
13271
0
  return num_phdrs;
13272
0
}
13273
13274
enum elf_reloc_type_class
13275
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13276
         const asection *rel_sec ATTRIBUTE_UNUSED,
13277
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13278
0
{
13279
0
  return reloc_class_normal;
13280
0
}
13281
13282
/* For RELA architectures, return the relocation value for a
13283
   relocation against a local symbol.  */
13284
13285
bfd_vma
13286
_bfd_elf_rela_local_sym (bfd *abfd,
13287
       Elf_Internal_Sym *sym,
13288
       asection **psec,
13289
       Elf_Internal_Rela *rel)
13290
0
{
13291
0
  asection *sec = *psec;
13292
0
  bfd_vma relocation;
13293
13294
0
  relocation = (sec->output_section->vma
13295
0
    + sec->output_offset
13296
0
    + sym->st_value);
13297
0
  if ((sec->flags & SEC_MERGE)
13298
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13299
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13300
0
    {
13301
0
      rel->r_addend =
13302
0
  _bfd_merged_section_offset (abfd, psec,
13303
0
            sym->st_value + rel->r_addend);
13304
0
      if (sec != *psec)
13305
0
  {
13306
    /* If we have changed the section, and our original section is
13307
       marked with SEC_EXCLUDE, it means that the original
13308
       SEC_MERGE section has been completely subsumed in some
13309
       other SEC_MERGE section.  In this case, we need to leave
13310
       some info around for --emit-relocs.  */
13311
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13312
0
      sec->kept_section = *psec;
13313
0
    sec = *psec;
13314
0
  }
13315
0
      rel->r_addend -= relocation;
13316
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13317
0
    }
13318
0
  return relocation;
13319
0
}
13320
13321
bfd_vma
13322
_bfd_elf_rel_local_sym (bfd *abfd,
13323
      Elf_Internal_Sym *sym,
13324
      asection **psec,
13325
      bfd_vma addend)
13326
0
{
13327
0
  asection *sec = *psec;
13328
13329
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13330
0
    return sym->st_value + addend;
13331
13332
0
  return _bfd_merged_section_offset (abfd, psec,
13333
0
             sym->st_value + addend);
13334
0
}
13335
13336
/* Adjust an address within a section.  Given OFFSET within SEC, return
13337
   the new offset within the section, based upon changes made to the
13338
   section.  Returns -1 if the offset is now invalid.
13339
   The offset (in abnd out) is in target sized bytes, however big a
13340
   byte may be.  */
13341
13342
bfd_vma
13343
_bfd_elf_section_offset (bfd *abfd,
13344
       struct bfd_link_info *info,
13345
       asection *sec,
13346
       bfd_vma offset)
13347
0
{
13348
0
  switch (sec->sec_info_type)
13349
0
    {
13350
0
    case SEC_INFO_TYPE_STABS:
13351
0
      return _bfd_stab_section_offset (sec, offset);
13352
13353
0
    case SEC_INFO_TYPE_EH_FRAME:
13354
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13355
13356
0
    case SEC_INFO_TYPE_SFRAME:
13357
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13358
13359
0
    default:
13360
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13361
0
  {
13362
    /* Reverse the offset.  */
13363
0
    elf_backend_data *bed = get_elf_backend_data (abfd);
13364
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13365
13366
    /* address_size and sec->size are in octets.  Convert
13367
       to bytes before subtracting the original offset.  */
13368
0
    offset = ((sec->size - address_size)
13369
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13370
0
  }
13371
0
      return offset;
13372
0
    }
13373
0
}
13374

13375
long
13376
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13377
             long symcount ATTRIBUTE_UNUSED,
13378
             asymbol **syms ATTRIBUTE_UNUSED,
13379
             long dynsymcount,
13380
             asymbol **dynsyms,
13381
             asymbol **ret)
13382
7.02k
{
13383
7.02k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13384
7.02k
  asection *relplt;
13385
7.02k
  asymbol *s;
13386
7.02k
  const char *relplt_name;
13387
7.02k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13388
7.02k
  arelent *p;
13389
7.02k
  long count, i, n;
13390
7.02k
  size_t size;
13391
7.02k
  Elf_Internal_Shdr *hdr;
13392
7.02k
  char *names;
13393
7.02k
  asection *plt;
13394
13395
7.02k
  *ret = NULL;
13396
13397
7.02k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13398
6.98k
    return 0;
13399
13400
38
  if (dynsymcount <= 0)
13401
30
    return 0;
13402
13403
8
  if (!bed->plt_sym_val)
13404
8
    return 0;
13405
13406
0
  relplt_name = bed->relplt_name;
13407
0
  if (relplt_name == NULL)
13408
0
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13409
0
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13410
0
  if (relplt == NULL)
13411
0
    return 0;
13412
13413
0
  hdr = &elf_section_data (relplt)->this_hdr;
13414
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13415
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13416
0
    return 0;
13417
13418
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13419
0
  if (plt == NULL)
13420
0
    return 0;
13421
13422
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13423
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13424
0
    return -1;
13425
13426
0
  count = NUM_SHDR_ENTRIES (hdr);
13427
0
  size = count * sizeof (asymbol);
13428
0
  p = relplt->relocation;
13429
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13430
0
    {
13431
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13432
0
      if (p->addend != 0)
13433
0
  {
13434
0
#ifdef BFD64
13435
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13436
#else
13437
    size += sizeof ("+0x") - 1 + 8;
13438
#endif
13439
0
  }
13440
0
    }
13441
13442
0
  s = *ret = (asymbol *) bfd_malloc (size);
13443
0
  if (s == NULL)
13444
0
    return -1;
13445
13446
0
  names = (char *) (s + count);
13447
0
  p = relplt->relocation;
13448
0
  n = 0;
13449
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13450
0
    {
13451
0
      size_t len;
13452
0
      bfd_vma addr;
13453
13454
0
      addr = bed->plt_sym_val (i, plt, p);
13455
0
      if (addr == (bfd_vma) -1)
13456
0
  continue;
13457
13458
0
      *s = **p->sym_ptr_ptr;
13459
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13460
   we are defining a symbol, ensure one of them is set.  */
13461
0
      if ((s->flags & BSF_LOCAL) == 0)
13462
0
  s->flags |= BSF_GLOBAL;
13463
0
      s->flags |= BSF_SYNTHETIC;
13464
0
      s->section = plt;
13465
0
      s->value = addr - plt->vma;
13466
0
      s->name = names;
13467
0
      s->udata.p = NULL;
13468
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13469
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13470
0
      names += len;
13471
0
      if (p->addend != 0)
13472
0
  {
13473
0
    char buf[30], *a;
13474
13475
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13476
0
    names += sizeof ("+0x") - 1;
13477
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13478
0
    for (a = buf; *a == '0'; ++a)
13479
0
      ;
13480
0
    len = strlen (a);
13481
0
    memcpy (names, a, len);
13482
0
    names += len;
13483
0
  }
13484
0
      memcpy (names, "@plt", sizeof ("@plt"));
13485
0
      names += sizeof ("@plt");
13486
0
      ++s, ++n;
13487
0
    }
13488
13489
0
  return n;
13490
0
}
13491
13492
bool
13493
_bfd_elf_final_write_processing (bfd *abfd)
13494
131
{
13495
131
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13496
131
  elf_backend_data *bed = get_elf_backend_data (abfd);
13497
13498
131
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13499
0
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13500
13501
131
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13502
124
      || bed->target_os == is_solaris)
13503
7
    {
13504
7
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13505
7
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13506
7
    }
13507
13508
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13509
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13510
     or STB_GNU_UNIQUE binding.  */
13511
131
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13512
0
    {
13513
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13514
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13515
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13516
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13517
0
  {
13518
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13519
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13520
0
          "and FreeBSD targets"));
13521
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13522
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13523
0
          "only by GNU and FreeBSD targets"));
13524
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13525
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13526
0
          "only by GNU and FreeBSD targets"));
13527
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13528
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13529
0
          "only by GNU and FreeBSD targets"));
13530
0
    bfd_set_error (bfd_error_sorry);
13531
0
    return false;
13532
0
  }
13533
0
    }
13534
131
  return true;
13535
131
}
13536
13537
13538
/* Return TRUE for ELF symbol types that represent functions.
13539
   This is the default version of this function, which is sufficient for
13540
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13541
13542
bool
13543
_bfd_elf_is_function_type (unsigned int type)
13544
0
{
13545
0
  return (type == STT_FUNC
13546
0
    || type == STT_GNU_IFUNC);
13547
0
}
13548
13549
/* If the ELF symbol SYM might be a function in SEC, return the
13550
   function size and set *CODE_OFF to the function's entry point,
13551
   otherwise return zero.  */
13552
13553
bfd_size_type
13554
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13555
           bfd_vma *code_off)
13556
842k
{
13557
842k
  bfd_size_type size;
13558
842k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13559
13560
842k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13561
842k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13562
601k
      || sym->section != sec)
13563
830k
    return 0;
13564
13565
12.5k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13566
13567
  /* In theory we should check that the symbol's type satisfies
13568
     _bfd_elf_is_function_type(), but there are some function-like
13569
     symbols which would fail this test.  (eg _start).  Instead
13570
     we check for hidden, local, notype symbols with zero size.
13571
     This type of symbol is generated by the annobin plugin for gcc
13572
     and clang, and should not be considered to be a function symbol.  */
13573
12.5k
  if (size == 0
13574
2.17k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13575
1.26k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13576
1.15k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13577
90
    return 0;
13578
13579
12.4k
  *code_off = sym->value;
13580
  /* Do not return 0 for the function's size.  */
13581
12.4k
  return size ? size : 1;
13582
12.5k
}
13583
13584
/* Set to non-zero to enable some debug messages.  */
13585
#define DEBUG_SECONDARY_RELOCS   0
13586
13587
/* An internal-to-the-bfd-library only section type
13588
   used to indicate a cached secondary reloc section.  */
13589
4.08k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13590
13591
/* Create a BFD section to hold a secondary reloc section.  */
13592
13593
bool
13594
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13595
               Elf_Internal_Shdr *hdr,
13596
               const char * name,
13597
               unsigned int shindex)
13598
1.33k
{
13599
  /* We only support RELA secondary relocs.  */
13600
1.33k
  if (hdr->sh_type != SHT_RELA)
13601
128
    return false;
13602
13603
#if DEBUG_SECONDARY_RELOCS
13604
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13605
#endif
13606
1.20k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13607
1.20k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13608
1.33k
}
13609
13610
/* Read in any secondary relocs associated with SEC.  */
13611
13612
bool
13613
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13614
          asection *  sec,
13615
          asymbol **  symbols,
13616
          bool dynamic)
13617
9.86k
{
13618
9.86k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13619
9.86k
  asection * relsec;
13620
9.86k
  bool result = true;
13621
9.86k
  bfd_vma (*r_sym) (bfd_vma);
13622
9.86k
  ufile_ptr filesize;
13623
13624
9.86k
#if BFD_DEFAULT_TARGET_SIZE > 32
13625
9.86k
  if (bfd_arch_bits_per_address (abfd) != 32)
13626
6.33k
    r_sym = elf64_r_sym;
13627
3.52k
  else
13628
3.52k
#endif
13629
3.52k
    r_sym = elf32_r_sym;
13630
13631
9.86k
  if (!elf_section_data (sec)->has_secondary_relocs)
13632
9.79k
    return true;
13633
13634
  /* Discover if there are any secondary reloc sections
13635
     associated with SEC.  */
13636
65
  filesize = bfd_get_file_size (abfd);
13637
1.50k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13638
1.43k
    {
13639
1.43k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13640
13641
1.43k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13642
67
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13643
65
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13644
65
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13645
65
  {
13646
65
    bfd_byte * native_relocs;
13647
65
    bfd_byte * native_reloc;
13648
65
    arelent * internal_relocs;
13649
65
    arelent * internal_reloc;
13650
65
    size_t i;
13651
65
    unsigned int entsize;
13652
65
    unsigned int symcount;
13653
65
    bfd_size_type reloc_count;
13654
65
    size_t amt;
13655
13656
65
    if (ebd->elf_info_to_howto == NULL)
13657
0
      return false;
13658
13659
#if DEBUG_SECONDARY_RELOCS
13660
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13661
       sec->name, relsec->name);
13662
#endif
13663
65
    entsize = hdr->sh_entsize;
13664
13665
65
    if (filesize != 0
13666
65
        && ((ufile_ptr) hdr->sh_offset > filesize
13667
60
      || hdr->sh_size > filesize - hdr->sh_offset))
13668
19
      {
13669
19
        bfd_set_error (bfd_error_file_truncated);
13670
19
        result = false;
13671
19
        continue;
13672
19
      }
13673
13674
46
    native_relocs = bfd_malloc (hdr->sh_size);
13675
46
    if (native_relocs == NULL)
13676
0
      {
13677
0
        result = false;
13678
0
        continue;
13679
0
      }
13680
13681
46
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13682
46
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13683
0
      {
13684
0
        free (native_relocs);
13685
0
        bfd_set_error (bfd_error_file_too_big);
13686
0
        result = false;
13687
0
        continue;
13688
0
      }
13689
13690
46
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13691
46
    if (internal_relocs == NULL)
13692
0
      {
13693
0
        free (native_relocs);
13694
0
        result = false;
13695
0
        continue;
13696
0
      }
13697
13698
46
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13699
46
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13700
0
      {
13701
0
        free (native_relocs);
13702
        /* The internal_relocs will be freed when
13703
     the memory for the bfd is released.  */
13704
0
        result = false;
13705
0
        continue;
13706
0
      }
13707
13708
46
    if (dynamic)
13709
0
      symcount = bfd_get_dynamic_symcount (abfd);
13710
46
    else
13711
46
      symcount = bfd_get_symcount (abfd);
13712
13713
46
    for (i = 0, internal_reloc = internal_relocs,
13714
46
     native_reloc = native_relocs;
13715
506
         i < reloc_count;
13716
460
         i++, internal_reloc++, native_reloc += entsize)
13717
460
      {
13718
460
        bool res;
13719
460
        Elf_Internal_Rela rela;
13720
13721
460
        if (entsize == ebd->s->sizeof_rel)
13722
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13723
460
        else /* entsize == ebd->s->sizeof_rela */
13724
460
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13725
13726
        /* The address of an ELF reloc is section relative for an object
13727
     file, and absolute for an executable file or shared library.
13728
     The address of a normal BFD reloc is always section relative,
13729
     and the address of a dynamic reloc is absolute..  */
13730
460
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13731
426
    internal_reloc->address = rela.r_offset;
13732
34
        else
13733
34
    internal_reloc->address = rela.r_offset - sec->vma;
13734
13735
460
        if (r_sym (rela.r_info) == STN_UNDEF)
13736
52
    {
13737
      /* FIXME: This and the error case below mean that we
13738
         have a symbol on relocs that is not elf_symbol_type.  */
13739
52
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13740
52
    }
13741
408
        else if (r_sym (rela.r_info) > symcount)
13742
0
    {
13743
0
      _bfd_error_handler
13744
        /* xgettext:c-format */
13745
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13746
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13747
0
      bfd_set_error (bfd_error_bad_value);
13748
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13749
0
      result = false;
13750
0
    }
13751
408
        else
13752
408
    {
13753
408
      asymbol **ps;
13754
13755
408
      ps = symbols + r_sym (rela.r_info) - 1;
13756
408
      internal_reloc->sym_ptr_ptr = ps;
13757
      /* Make sure that this symbol is not removed by strip.  */
13758
408
      (*ps)->flags |= BSF_KEEP;
13759
408
    }
13760
13761
460
        internal_reloc->addend = rela.r_addend;
13762
13763
460
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13764
460
        if (! res || internal_reloc->howto == NULL)
13765
31
    {
13766
#if DEBUG_SECONDARY_RELOCS
13767
      fprintf (stderr,
13768
         "there is no howto associated with reloc %lx\n",
13769
         rela.r_info);
13770
#endif
13771
31
      result = false;
13772
31
    }
13773
460
      }
13774
13775
46
    free (native_relocs);
13776
    /* Store the internal relocs.  */
13777
46
    relsec->sec_info = internal_relocs;
13778
46
  }
13779
1.43k
    }
13780
13781
65
  return result;
13782
65
}
13783
13784
/* Set the ELF section header fields of an output secondary reloc section.  */
13785
13786
bool
13787
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13788
              bfd *obfd ATTRIBUTE_UNUSED,
13789
              const Elf_Internal_Shdr *isection,
13790
              Elf_Internal_Shdr *osection)
13791
14
{
13792
14
  asection * isec;
13793
14
  asection * osec;
13794
14
  struct bfd_elf_section_data * esd;
13795
13796
14
  if (isection == NULL)
13797
0
    return false;
13798
13799
14
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13800
14
    return true;
13801
13802
0
  isec = isection->bfd_section;
13803
0
  if (isec == NULL)
13804
0
    return false;
13805
13806
0
  osec = osection->bfd_section;
13807
0
  if (osec == NULL)
13808
0
    return false;
13809
13810
0
  BFD_ASSERT (osec->sec_info == NULL);
13811
0
  osec->sec_info = isec->sec_info;
13812
0
  osection->sh_type = SHT_RELA;
13813
0
  osection->sh_link = elf_onesymtab (obfd);
13814
0
  if (osection->sh_link == 0)
13815
0
    {
13816
      /* There is no symbol table - we are hosed...  */
13817
0
      _bfd_error_handler
13818
  /* xgettext:c-format */
13819
0
  (_("%pB(%pA): link section cannot be set"
13820
0
     " because the output file does not have a symbol table"),
13821
0
  obfd, osec);
13822
0
      bfd_set_error (bfd_error_bad_value);
13823
0
      return false;
13824
0
    }
13825
13826
  /* Find the output section that corresponds to the isection's
13827
     sh_info link.  */
13828
0
  if (isection->sh_info == 0
13829
0
      || isection->sh_info >= elf_numsections (ibfd))
13830
0
    {
13831
0
      _bfd_error_handler
13832
  /* xgettext:c-format */
13833
0
  (_("%pB(%pA): info section index is invalid"),
13834
0
  obfd, osec);
13835
0
      bfd_set_error (bfd_error_bad_value);
13836
0
      return false;
13837
0
    }
13838
13839
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
13840
13841
0
  if (isection == NULL
13842
0
      || isection->bfd_section == NULL
13843
0
      || isection->bfd_section->output_section == NULL)
13844
0
    {
13845
0
      _bfd_error_handler
13846
  /* xgettext:c-format */
13847
0
  (_("%pB(%pA): info section index cannot be set"
13848
0
     " because the section is not in the output"),
13849
0
  obfd, osec);
13850
0
      bfd_set_error (bfd_error_bad_value);
13851
0
      return false;
13852
0
    }
13853
13854
0
  esd = elf_section_data (isection->bfd_section->output_section);
13855
0
  BFD_ASSERT (esd != NULL);
13856
0
  osection->sh_info = esd->this_idx;
13857
0
  esd->has_secondary_relocs = true;
13858
#if DEBUG_SECONDARY_RELOCS
13859
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13860
     osec->name, osection->sh_link, osection->sh_info);
13861
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13862
     bfd_section_name (isection->bfd_section->output_section));
13863
#endif
13864
13865
0
  return true;
13866
0
}
13867
13868
/* Write out a secondary reloc section.
13869
13870
   FIXME: Currently this function can result in a serious performance penalty
13871
   for files with secondary relocs and lots of sections.  The proper way to
13872
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13873
   relocs together and then to have this function just walk that chain.  */
13874
13875
bool
13876
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13877
0
{
13878
0
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13879
0
  bfd_vma addr_offset;
13880
0
  asection * relsec;
13881
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13882
0
  bool result = true;
13883
13884
0
  if (sec == NULL)
13885
0
    return false;
13886
13887
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13888
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13889
0
    r_info = elf64_r_info;
13890
0
  else
13891
0
#endif
13892
0
    r_info = elf32_r_info;
13893
13894
  /* The address of an ELF reloc is section relative for an object
13895
     file, and absolute for an executable file or shared library.
13896
     The address of a BFD reloc is always section relative.  */
13897
0
  addr_offset = 0;
13898
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13899
0
    addr_offset = sec->vma;
13900
13901
  /* Discover if there are any secondary reloc sections
13902
     associated with SEC.  */
13903
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13904
0
    {
13905
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13906
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13907
13908
0
      if (hdr->sh_type == SHT_RELA
13909
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13910
0
  {
13911
0
    asymbol *    last_sym;
13912
0
    int          last_sym_idx;
13913
0
    size_t       reloc_count;
13914
0
    size_t       idx;
13915
0
    bfd_size_type entsize;
13916
0
    arelent *    src_irel;
13917
0
    bfd_byte *   dst_rela;
13918
13919
0
    if (hdr->contents != NULL)
13920
0
      {
13921
0
        _bfd_error_handler
13922
    /* xgettext:c-format */
13923
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13924
0
     abfd, relsec);
13925
0
        bfd_set_error (bfd_error_bad_value);
13926
0
        result = false;
13927
0
        continue;
13928
0
      }
13929
13930
0
    entsize = hdr->sh_entsize;
13931
0
    if (entsize == 0)
13932
0
      {
13933
0
        _bfd_error_handler
13934
    /* xgettext:c-format */
13935
0
    (_("%pB(%pA): error: secondary reloc section"
13936
0
       " has zero sized entries"),
13937
0
     abfd, relsec);
13938
0
        bfd_set_error (bfd_error_bad_value);
13939
0
        result = false;
13940
0
        continue;
13941
0
      }
13942
0
    else if (entsize != ebd->s->sizeof_rel
13943
0
       && entsize != ebd->s->sizeof_rela)
13944
0
      {
13945
0
        _bfd_error_handler
13946
    /* xgettext:c-format */
13947
0
    (_("%pB(%pA): error: secondary reloc section"
13948
0
       " has non-standard sized entries"),
13949
0
     abfd, relsec);
13950
0
        bfd_set_error (bfd_error_bad_value);
13951
0
        result = false;
13952
0
        continue;
13953
0
      }
13954
13955
0
    reloc_count = hdr->sh_size / entsize;
13956
0
    hdr->sh_size = entsize * reloc_count;
13957
0
    if (reloc_count == 0)
13958
0
      {
13959
0
        _bfd_error_handler
13960
    /* xgettext:c-format */
13961
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13962
0
     abfd, relsec);
13963
0
        bfd_set_error (bfd_error_bad_value);
13964
0
        result = false;
13965
0
        continue;
13966
0
      }
13967
13968
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13969
0
    if (hdr->contents == NULL)
13970
0
      continue;
13971
0
    relsec->alloced = 1;
13972
13973
#if DEBUG_SECONDARY_RELOCS
13974
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13975
       reloc_count, sec->name, relsec->name);
13976
#endif
13977
0
    last_sym = NULL;
13978
0
    last_sym_idx = 0;
13979
0
    dst_rela = hdr->contents;
13980
0
    src_irel = sec->sec_info;
13981
0
    if (src_irel == NULL)
13982
0
      {
13983
0
        _bfd_error_handler
13984
    /* xgettext:c-format */
13985
0
    (_("%pB(%pA): error: internal relocs missing"
13986
0
       " for secondary reloc section"),
13987
0
     abfd, relsec);
13988
0
        bfd_set_error (bfd_error_bad_value);
13989
0
        result = false;
13990
0
        continue;
13991
0
      }
13992
13993
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13994
0
      {
13995
0
        Elf_Internal_Rela src_rela;
13996
0
        arelent *ptr;
13997
0
        asymbol *sym;
13998
0
        int n;
13999
14000
0
        ptr = src_irel + idx;
14001
0
        if (ptr == NULL)
14002
0
    {
14003
0
      _bfd_error_handler
14004
        /* xgettext:c-format */
14005
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14006
0
         abfd, relsec, idx);
14007
0
      bfd_set_error (bfd_error_bad_value);
14008
0
      result = false;
14009
0
      break;
14010
0
    }
14011
14012
0
        if (ptr->sym_ptr_ptr == NULL)
14013
0
    {
14014
      /* FIXME: Is this an error ? */
14015
0
      n = 0;
14016
0
    }
14017
0
        else
14018
0
    {
14019
0
      sym = *ptr->sym_ptr_ptr;
14020
14021
0
      if (sym == last_sym)
14022
0
        n = last_sym_idx;
14023
0
      else
14024
0
        {
14025
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14026
0
          if (n < 0)
14027
0
      {
14028
0
        _bfd_error_handler
14029
          /* xgettext:c-format */
14030
0
          (_("%pB(%pA): error: secondary reloc %zu"
14031
0
             " references a missing symbol"),
14032
0
           abfd, relsec, idx);
14033
0
        bfd_set_error (bfd_error_bad_value);
14034
0
        result = false;
14035
0
        n = 0;
14036
0
      }
14037
14038
0
          last_sym = sym;
14039
0
          last_sym_idx = n;
14040
0
        }
14041
14042
0
      if (sym->the_bfd != NULL
14043
0
          && sym->the_bfd->xvec != abfd->xvec
14044
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14045
0
        {
14046
0
          _bfd_error_handler
14047
      /* xgettext:c-format */
14048
0
      (_("%pB(%pA): error: secondary reloc %zu"
14049
0
         " references a deleted symbol"),
14050
0
       abfd, relsec, idx);
14051
0
          bfd_set_error (bfd_error_bad_value);
14052
0
          result = false;
14053
0
          n = 0;
14054
0
        }
14055
0
    }
14056
14057
0
        src_rela.r_offset = ptr->address + addr_offset;
14058
0
        if (ptr->howto == NULL)
14059
0
    {
14060
0
      _bfd_error_handler
14061
        /* xgettext:c-format */
14062
0
        (_("%pB(%pA): error: secondary reloc %zu"
14063
0
           " is of an unknown type"),
14064
0
         abfd, relsec, idx);
14065
0
      bfd_set_error (bfd_error_bad_value);
14066
0
      result = false;
14067
0
      src_rela.r_info = r_info (0, 0);
14068
0
    }
14069
0
        else
14070
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14071
0
        src_rela.r_addend = ptr->addend;
14072
14073
0
        if (entsize == ebd->s->sizeof_rel)
14074
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14075
0
        else /* entsize == ebd->s->sizeof_rela */
14076
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14077
0
      }
14078
0
  }
14079
0
    }
14080
14081
0
  return result;
14082
0
}
14083
14084
/* Mmap in section contents.  */
14085
14086
static bool
14087
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14088
7.81k
{
14089
7.81k
#ifdef USE_MMAP
14090
7.81k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14091
7.81k
  if (bed->use_mmap
14092
3.46k
      && sec->compress_status == COMPRESS_SECTION_NONE
14093
3.46k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14094
3.46k
    {
14095
      /* Use mmap only if section size >= the minimum mmap section
14096
   size.  */
14097
3.46k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14098
3.46k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14099
3.46k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14100
631
  {
14101
631
    if (sec->contents != NULL)
14102
0
      {
14103
0
        if (!sec->mmapped_p)
14104
0
    abort ();
14105
0
        *buf = sec->contents;
14106
0
        return true;
14107
0
      }
14108
631
    if (sec->mmapped_p)
14109
0
      abort ();
14110
631
    sec->mmapped_p = 1;
14111
14112
    /* We can't use the final link preallocated buffer for mmap.  */
14113
631
    *buf = NULL;
14114
631
  }
14115
3.46k
    }
14116
7.81k
#endif
14117
  /* FIXME: We should not get here if sec->alloced is set.  */
14118
7.81k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14119
7.81k
  if (ret && sec->mmapped_p)
14120
0
    *buf = sec->contents;
14121
7.81k
  return ret;
14122
7.81k
}
14123
14124
/* Mmap in section contents.  */
14125
14126
bool
14127
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14128
7.81k
{
14129
7.81k
  *buf = NULL;
14130
7.81k
  return elf_mmap_section_contents (abfd, sec, buf);
14131
7.81k
}
14132
14133
/* Mmap in the full section contents for the final link.  */
14134
14135
bool
14136
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14137
             bfd_byte **buf)
14138
0
{
14139
0
  return elf_mmap_section_contents (abfd, sec, buf);
14140
0
}
14141
14142
/* Munmap section contents.  */
14143
14144
void
14145
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14146
362k
{
14147
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14148
     CONTENTS may be NULL.  */
14149
362k
  if (contents == NULL)
14150
356k
    return;
14151
14152
6.69k
  if (sec->alloced
14153
      /* What a tangled web we weave with section contents.
14154
   FIXME: We shouldn't need to test anything but sec->alloced
14155
   here, but there are cases where a buffer is allocated for a
14156
   section but then another buffer is malloc'd anyway.  eg.
14157
   trace through ld-elf/eh4 testcase on x86_64.  */
14158
357
      && (sec->contents == contents
14159
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14160
357
    return;
14161
14162
  /* Don't leave pointers to data we are about to munmap or free.  */
14163
6.33k
  if (sec->contents == contents)
14164
0
    sec->contents = NULL;
14165
6.33k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14166
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14167
14168
6.33k
#ifdef USE_MMAP
14169
6.33k
  if (sec->mmapped_p)
14170
0
    {
14171
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14172
   malloced, CONTENTS_ADDR is set to NULL.  */
14173
0
      if (elf_section_data (sec)->contents_addr != NULL)
14174
0
  {
14175
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14176
0
    if (munmap (elf_section_data (sec)->contents_addr,
14177
0
          elf_section_data (sec)->contents_size) != 0)
14178
0
      abort ();
14179
0
    sec->mmapped_p = 0;
14180
0
    elf_section_data (sec)->contents_addr = NULL;
14181
0
    elf_section_data (sec)->contents_size = 0;
14182
0
    return;
14183
0
  }
14184
0
    }
14185
6.33k
#endif
14186
14187
6.33k
  free (contents);
14188
6.33k
}
14189
14190
/* Munmap the full section contents for the final link.  */
14191
14192
void
14193
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14194
0
{
14195
0
#ifdef USE_MMAP
14196
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14197
0
    {
14198
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14199
   malloced, CONTENTS_ADDR is set to NULL.  */
14200
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14201
0
      if (munmap (elf_section_data (sec)->contents_addr,
14202
0
      elf_section_data (sec)->contents_size) != 0)
14203
0
  abort ();
14204
0
      sec->mmapped_p = 0;
14205
0
      sec->contents = NULL;
14206
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14207
0
      elf_section_data (sec)->contents_addr = NULL;
14208
0
      elf_section_data (sec)->contents_size = 0;
14209
0
    }
14210
0
#endif
14211
0
}
14212
14213
#ifndef OBJ_MAYBE_ELF_ATTRIBUTES
14214
14215
bfd *_bfd_elf_link_setup_object_attributes
14216
  (struct bfd_link_info *info ATTRIBUTE_UNUSED)
14217
{ return NULL; }
14218
14219
#endif /* !OBJ_MAYBE_ELF_ATTRIBUTES */