Coverage Report

Created: 2026-03-10 08:46

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
54.9k
#define streq(A,B) (strcmp ((A), (B)) == 0)
56
#endif
57
58
/* Core note names.  */
59
0
#define NOTE_NAME_CORE     "CORE"
60
0
#define NOTE_NAME_FREEBSD  "FreeBSD"
61
0
#define NOTE_NAME_GDB      "GDB"
62
0
#define NOTE_NAME_LINUX    "LINUX"
63
64
/* Names of a pseudo-section which represent core notes.  */
65
0
#define NOTE_PSEUDO_SECTION_AARCH_FPMR    ".reg-aarch-fpmr"
66
0
#define NOTE_PSEUDO_SECTION_AARCH_GCS   ".reg-aarch-gcs"
67
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK  ".reg-aarch-hw-break"
68
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH  ".reg-aarch-hw-watch"
69
0
#define NOTE_PSEUDO_SECTION_AARCH_MTE   ".reg-aarch-mte"
70
0
#define NOTE_PSEUDO_SECTION_AARCH_PAUTH   ".reg-aarch-pauth"
71
0
#define NOTE_PSEUDO_SECTION_AARCH_SSVE    ".reg-aarch-ssve"
72
0
#define NOTE_PSEUDO_SECTION_AARCH_SVE   ".reg-aarch-sve"
73
0
#define NOTE_PSEUDO_SECTION_AARCH_TLS   ".reg-aarch-tls"
74
0
#define NOTE_PSEUDO_SECTION_AARCH_ZA    ".reg-aarch-za"
75
0
#define NOTE_PSEUDO_SECTION_AARCH_ZT    ".reg-aarch-zt"
76
0
#define NOTE_PSEUDO_SECTION_ARC_V2    ".reg-arc-v2"
77
0
#define NOTE_PSEUDO_SECTION_ARM_VFP   ".reg-arm-vfp"
78
0
#define NOTE_PSEUDO_SECTION_I386_TLS    ".reg-i386-tls"
79
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG  ".reg-loongarch-cpucfg"
80
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LASX  ".reg-loongarch-lasx"
81
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt"
82
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx"
83
0
#define NOTE_PSEUDO_SECTION_PPC_DSCR    ".reg-ppc-dscr"
84
0
#define NOTE_PSEUDO_SECTION_PPC_EBB   ".reg-ppc-ebb"
85
0
#define NOTE_PSEUDO_SECTION_PPC_PMU   ".reg-ppc-pmu"
86
0
#define NOTE_PSEUDO_SECTION_PPC_PPR   ".reg-ppc-ppr"
87
0
#define NOTE_PSEUDO_SECTION_PPC_TAR   ".reg-ppc-tar"
88
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR  ".reg-ppc-tm-cdscr"
89
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CFPR   ".reg-ppc-tm-cfpr"
90
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CGPR   ".reg-ppc-tm-cgpr"
91
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CPPR   ".reg-ppc-tm-cppr"
92
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CTAR   ".reg-ppc-tm-ctar"
93
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVMX   ".reg-ppc-tm-cvmx"
94
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVSX   ".reg-ppc-tm-cvsx"
95
0
#define NOTE_PSEUDO_SECTION_PPC_TM_SPR    ".reg-ppc-tm-spr"
96
0
#define NOTE_PSEUDO_SECTION_PPC_VSX   ".reg-ppc-vsx"
97
0
#define NOTE_PSEUDO_SECTION_PPC_VMX   ".reg-ppc-vmx"
98
9
#define NOTE_PSEUDO_SECTION_REG     ".reg"
99
63
#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
20
{
139
20
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
140
20
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
141
20
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
142
20
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
143
20
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
144
20
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
145
20
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
146
20
}
147
148
/* Swap out a Verdef structure.  */
149
150
void
151
_bfd_elf_swap_verdef_out (bfd *abfd,
152
        const Elf_Internal_Verdef *src,
153
        Elf_External_Verdef *dst)
154
0
{
155
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
156
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
157
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
158
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
159
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
160
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
161
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
162
0
}
163
164
/* Swap in a Verdaux structure.  */
165
166
void
167
_bfd_elf_swap_verdaux_in (bfd *abfd,
168
        const Elf_External_Verdaux *src,
169
        Elf_Internal_Verdaux *dst)
170
3
{
171
3
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
172
3
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
173
3
}
174
175
/* Swap out a Verdaux structure.  */
176
177
void
178
_bfd_elf_swap_verdaux_out (bfd *abfd,
179
         const Elf_Internal_Verdaux *src,
180
         Elf_External_Verdaux *dst)
181
0
{
182
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
183
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
184
0
}
185
186
/* Swap in a Verneed structure.  */
187
188
void
189
_bfd_elf_swap_verneed_in (bfd *abfd,
190
        const Elf_External_Verneed *src,
191
        Elf_Internal_Verneed *dst)
192
223
{
193
223
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
194
223
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
195
223
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
196
223
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
197
223
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
198
223
}
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
282
{
221
282
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
222
282
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
223
282
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
224
282
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
225
282
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
226
282
}
227
228
/* Swap out a Vernaux structure.  */
229
230
void
231
_bfd_elf_swap_vernaux_out (bfd *abfd,
232
         const Elf_Internal_Vernaux *src,
233
         Elf_External_Vernaux *dst)
234
0
{
235
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
236
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
237
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
238
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
239
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
240
0
}
241
242
/* Swap in a Versym structure.  */
243
244
void
245
_bfd_elf_swap_versym_in (bfd *abfd,
246
       const Elf_External_Versym *src,
247
       Elf_Internal_Versym *dst)
248
1.41k
{
249
1.41k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
250
1.41k
}
251
252
/* Swap out a Versym structure.  */
253
254
void
255
_bfd_elf_swap_versym_out (bfd *abfd,
256
        const Elf_Internal_Versym *src,
257
        Elf_External_Versym *dst)
258
0
{
259
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
260
0
}
261
262
/* Standard ELF hash function.  Do not change this function; you will
263
   cause invalid hash tables to be generated.  */
264
265
unsigned long
266
bfd_elf_hash (const char *namearg)
267
0
{
268
0
  uint32_t h = 0;
269
270
0
  for (const unsigned char *name = (const unsigned char *) namearg;
271
0
       *name; name++)
272
0
    {
273
0
      h = (h << 4) + *name;
274
0
      h ^= (h >> 24) & 0xf0;
275
0
    }
276
0
  return h & 0x0fffffff;
277
0
}
278
279
/* DT_GNU_HASH hash function.  Do not change this function; you will
280
   cause invalid hash tables to be generated.  */
281
282
unsigned long
283
bfd_elf_gnu_hash (const char *namearg)
284
0
{
285
0
  uint32_t h = 5381;
286
287
0
  for (const unsigned char *name = (const unsigned char *) namearg;
288
0
       *name; name++)
289
0
    h = (h << 5) + h + *name;
290
0
  return h;
291
0
}
292
293
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
294
   the object_id field of an elf_obj_tdata field set.  */
295
bool
296
bfd_elf_allocate_object (bfd *abfd,
297
       size_t object_size)
298
2.24M
{
299
2.24M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
300
2.24M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
301
2.24M
  if (abfd->tdata.any == NULL)
302
0
    return false;
303
304
2.24M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
305
2.24M
  if (abfd->direction != read_direction)
306
800
    {
307
800
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
308
800
      if (o == NULL)
309
0
  return false;
310
800
      elf_tdata (abfd)->o = o;
311
800
      elf_program_header_size (abfd) = (bfd_size_type) -1;
312
800
    }
313
2.24M
  return true;
314
2.24M
}
315
316
317
bool
318
bfd_elf_make_object (bfd *abfd)
319
1.07M
{
320
1.07M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
321
1.07M
}
322
323
bool
324
bfd_elf_mkcorefile (bfd *abfd)
325
644k
{
326
  /* I think this can be done just like an object file.  */
327
644k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
328
0
    return false;
329
644k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
330
644k
  return elf_tdata (abfd)->core != NULL;
331
644k
}
332
333
char *
334
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
335
45.5k
{
336
45.5k
  Elf_Internal_Shdr **i_shdrp;
337
45.5k
  bfd_byte *shstrtab = NULL;
338
45.5k
  file_ptr offset;
339
45.5k
  bfd_size_type shstrtabsize;
340
341
45.5k
  i_shdrp = elf_elfsections (abfd);
342
45.5k
  if (i_shdrp == 0
343
45.5k
      || shindex >= elf_numsections (abfd)
344
45.5k
      || i_shdrp[shindex] == 0)
345
0
    return NULL;
346
347
45.5k
  shstrtab = i_shdrp[shindex]->contents;
348
45.5k
  if (shstrtab == NULL)
349
45.5k
    {
350
      /* No cached one, attempt to read, and cache what we read.  */
351
45.5k
      offset = i_shdrp[shindex]->sh_offset;
352
45.5k
      shstrtabsize = i_shdrp[shindex]->sh_size;
353
354
45.5k
      if (shstrtabsize == 0
355
43.4k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
356
43.0k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
357
4.78k
  {
358
    /* Once we've failed to read it, make sure we don't keep
359
       trying.  Otherwise, we'll keep allocating space for
360
       the string table over and over.  */
361
4.78k
    i_shdrp[shindex]->sh_size = 0;
362
4.78k
  }
363
40.8k
      else if (shstrtab[shstrtabsize - 1] != 0)
364
9.28k
  {
365
    /* It is an error if a string table isn't terminated.  */
366
9.28k
    _bfd_error_handler
367
      /* xgettext:c-format */
368
9.28k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
369
9.28k
    shstrtab[shstrtabsize - 1] = 0;
370
9.28k
  }
371
45.5k
      i_shdrp[shindex]->contents = shstrtab;
372
45.5k
    }
373
45.5k
  return (char *) shstrtab;
374
45.5k
}
375
376
char *
377
bfd_elf_string_from_elf_section (bfd *abfd,
378
         unsigned int shindex,
379
         unsigned int strindex)
380
967k
{
381
967k
  Elf_Internal_Shdr *hdr;
382
383
967k
  if (strindex == 0)
384
188k
    return "";
385
386
778k
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
387
164
    return NULL;
388
389
778k
  hdr = elf_elfsections (abfd)[shindex];
390
391
778k
  if (hdr->contents == NULL)
392
61.9k
    {
393
61.9k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
394
16.4k
  {
395
    /* PR 17512: file: f057ec89.  */
396
    /* xgettext:c-format */
397
16.4k
    _bfd_error_handler (_("%pB: attempt to load strings from"
398
16.4k
        " a non-string section (number %d)"),
399
16.4k
            abfd, shindex);
400
16.4k
    return NULL;
401
16.4k
  }
402
403
45.5k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
404
4.78k
  return NULL;
405
45.5k
    }
406
716k
  else
407
716k
    {
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
716k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
414
0
  return NULL;
415
716k
    }
416
417
757k
  if (strindex >= hdr->sh_size)
418
13.7k
    {
419
13.7k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
420
13.7k
      _bfd_error_handler
421
  /* xgettext:c-format */
422
13.7k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
423
13.7k
   abfd, strindex, (uint64_t) hdr->sh_size,
424
13.7k
   (shindex == shstrndx && strindex == hdr->sh_name
425
13.7k
    ? ".shstrtab"
426
13.7k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
427
13.7k
      return NULL;
428
13.7k
    }
429
430
743k
  return ((char *) hdr->contents) + strindex;
431
757k
}
432
433
/* Read and convert symbols to internal format.
434
   SYMCOUNT specifies the number of symbols to read, starting from
435
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
436
   are non-NULL, they are used to store the internal symbols, external
437
   symbols, and symbol section index extensions, respectively.
438
   Returns a pointer to the internal symbol buffer (malloced if necessary)
439
   or NULL if there were no symbols or some kind of problem.  */
440
441
Elf_Internal_Sym *
442
bfd_elf_get_elf_syms (bfd *ibfd,
443
          Elf_Internal_Shdr *symtab_hdr,
444
          size_t symcount,
445
          size_t symoffset,
446
          Elf_Internal_Sym *intsym_buf,
447
          void *extsym_buf,
448
          Elf_External_Sym_Shndx *extshndx_buf)
449
12.5k
{
450
12.5k
  Elf_Internal_Shdr *shndx_hdr;
451
12.5k
  void *alloc_ext;
452
12.5k
  const bfd_byte *esym;
453
12.5k
  Elf_External_Sym_Shndx *alloc_extshndx;
454
12.5k
  Elf_External_Sym_Shndx *shndx;
455
12.5k
  Elf_Internal_Sym *alloc_intsym;
456
12.5k
  Elf_Internal_Sym *isym;
457
12.5k
  Elf_Internal_Sym *isymend;
458
12.5k
  elf_backend_data *bed;
459
12.5k
  size_t extsym_size;
460
12.5k
  size_t amt;
461
12.5k
  file_ptr pos;
462
463
12.5k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
464
0
    abort ();
465
466
12.5k
  if (symcount == 0)
467
0
    return intsym_buf;
468
469
12.5k
  if (elf_use_dt_symtab_p (ibfd))
470
3
    {
471
      /* Use dynamic symbol table.  */
472
3
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
473
0
  {
474
0
    bfd_set_error (bfd_error_invalid_operation);
475
0
    return NULL;
476
0
  }
477
3
      return elf_tdata (ibfd)->dt_symtab + symoffset;
478
3
    }
479
480
  /* Normal syms might have section extension entries.  */
481
12.5k
  shndx_hdr = NULL;
482
12.5k
  if (elf_symtab_shndx_list (ibfd) != NULL)
483
1.99k
    {
484
1.99k
      elf_section_list * entry;
485
1.99k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
486
487
      /* Find an index section that is linked to this symtab section.  */
488
3.40k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
489
2.14k
  {
490
    /* PR 20063.  */
491
2.14k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
492
102
      continue;
493
494
2.04k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
495
734
      {
496
734
        shndx_hdr = & entry->hdr;
497
734
        break;
498
1.31k
      };
499
1.31k
  }
500
501
1.99k
      if (shndx_hdr == NULL)
502
1.26k
  {
503
1.26k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
504
      /* Not really accurate, but this was how the old code used
505
         to work.  */
506
1.25k
      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.26k
  }
510
1.99k
    }
511
512
  /* Read the symbols.  */
513
12.5k
  alloc_ext = NULL;
514
12.5k
  alloc_extshndx = NULL;
515
12.5k
  alloc_intsym = NULL;
516
12.5k
  bed = get_elf_backend_data (ibfd);
517
12.5k
  extsym_size = bed->s->sizeof_sym;
518
12.5k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
519
0
    {
520
0
      bfd_set_error (bfd_error_file_too_big);
521
0
      return NULL;
522
0
    }
523
12.5k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
524
12.5k
  size_t alloc_ext_size = amt;
525
12.5k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
526
12.2k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
527
12.2k
            &alloc_ext, ibfd, false))
528
1.01k
    {
529
1.01k
      intsym_buf = NULL;
530
1.01k
      goto out2;
531
1.01k
    }
532
533
11.5k
  size_t alloc_extshndx_size = 0;
534
11.5k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
535
10.1k
    extshndx_buf = NULL;
536
1.38k
  else
537
1.38k
    {
538
1.38k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
539
0
  {
540
0
    bfd_set_error (bfd_error_file_too_big);
541
0
    intsym_buf = NULL;
542
0
    goto out1;
543
0
  }
544
1.38k
      alloc_extshndx_size = amt;
545
1.38k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
546
1.38k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
547
1.27k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
548
1.27k
          &alloc_extshndx_size,
549
1.27k
          (void **) &alloc_extshndx,
550
1.27k
          ibfd, false))
551
243
  {
552
243
    intsym_buf = NULL;
553
243
    goto out1;
554
243
  }
555
1.38k
    }
556
557
11.3k
  if (intsym_buf == NULL)
558
4.13k
    {
559
4.13k
      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.13k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
565
4.13k
      intsym_buf = alloc_intsym;
566
4.13k
      if (intsym_buf == NULL)
567
0
  goto out1;
568
4.13k
    }
569
570
  /* Convert the symbols to internal form.  */
571
11.3k
  isymend = intsym_buf + symcount;
572
11.3k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
573
11.3k
     shndx = extshndx_buf;
574
145k
       isym < isymend;
575
133k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
576
135k
    {
577
135k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
578
437
  {
579
437
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
580
    /* xgettext:c-format */
581
437
    _bfd_error_handler (_("%pB symbol number %lu references"
582
437
        " nonexistent SHT_SYMTAB_SHNDX section"),
583
437
            ibfd, (unsigned long) symoffset);
584
437
    free (alloc_intsym);
585
437
    intsym_buf = NULL;
586
437
    goto out1;
587
437
  }
588
589
      /* PR 33019: Do not accept unsupported binding values - they will
590
   likely cause problems later on.  */
591
134k
      int bind = ELF_ST_BIND (isym->st_info);
592
134k
      if (bind > STB_WEAK && bind < STB_LOOS)
593
833
  { 
594
    /* xgettext:c-format */
595
833
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
596
833
            ibfd, (unsigned long) (isym - intsym_buf), bind);
597
833
    free (alloc_intsym);
598
833
    intsym_buf = NULL;
599
833
    goto out1;
600
833
  }
601
602
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
603
133k
      int t = ELF_ST_TYPE (isym->st_info);
604
133k
      if (t == 7)
605
213
  {
606
    /* xgettext:c-format */
607
213
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
608
213
            ibfd, (unsigned long) (isym - intsym_buf), t);
609
213
    free (alloc_intsym);
610
213
    intsym_buf = NULL;
611
213
    goto out1;
612
213
  }
613
133k
    }
614
615
11.5k
 out1:
616
11.5k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
617
12.5k
 out2:
618
12.5k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
619
620
12.5k
  return intsym_buf;
621
11.5k
}
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
125k
{
629
125k
  unsigned int iname = isym->st_name;
630
125k
  unsigned int shindex = symtab_hdr->sh_link;
631
632
125k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
633
      /* Check for a bogus st_shndx to avoid crashing.  */
634
24.0k
      && isym->st_shndx < elf_numsections (abfd))
635
23.7k
    {
636
23.7k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
637
23.7k
      shindex = elf_elfheader (abfd)->e_shstrndx;
638
23.7k
    }
639
640
125k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
641
125k
}
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
118k
{
649
118k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
650
118k
  if (name == NULL)
651
25.6k
    name = bfd_symbol_error_name;
652
93.3k
  else if (sym_sec && *name == '\0')
653
0
    name = bfd_section_name (sym_sec);
654
655
118k
  return name;
656
118k
}
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
8.95k
{
664
8.95k
  Elf_Internal_Shdr *hdr;
665
8.95k
  unsigned char esym[sizeof (Elf64_External_Sym)];
666
8.95k
  Elf_External_Sym_Shndx eshndx;
667
8.95k
  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
8.95k
  if (ghdr->sh_link >= elf_numsections (abfd))
672
28
    return NULL;
673
8.92k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
674
8.92k
  if (hdr->sh_type != SHT_SYMTAB
675
8.25k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
676
723
    return NULL;
677
678
  /* Go read the symbol.  */
679
8.20k
  hdr = &elf_tdata (abfd)->symtab_hdr;
680
8.20k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
681
8.20k
          &isym, esym, &eshndx) == NULL)
682
1.98k
    return NULL;
683
684
6.21k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
685
8.20k
}
686
687
static bool
688
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
689
13.6k
{
690
13.6k
  return (shdr->sh_size >= minsize
691
13.3k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
692
12.3k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
693
12.1k
    && shdr->bfd_section != NULL);
694
13.6k
}
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.0k
{
703
12.0k
  unsigned char *contents;
704
705
  /* Read the raw contents.  */
706
12.0k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
707
762
    {
708
762
      _bfd_error_handler
709
  /* xgettext:c-format */
710
762
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
711
762
      return false;
712
762
    }
713
714
11.3k
  asection *last_elt = NULL;
715
11.3k
  const char *gname = NULL;
716
11.3k
  unsigned char *p = contents + ghdr->sh_size;
717
467k
  while (1)
718
467k
    {
719
467k
      unsigned int idx;
720
467k
      Elf_Internal_Shdr *shdr;
721
467k
      asection *elt;
722
723
467k
      p -= 4;
724
467k
      idx = H_GET_32 (abfd, p);
725
467k
      if (p == contents)
726
7.44k
  {
727
7.44k
    if ((idx & GRP_COMDAT) != 0)
728
2.68k
      ghdr->bfd_section->flags
729
2.68k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
730
7.44k
    break;
731
7.44k
  }
732
733
459k
      if (idx == 0
734
336k
    || idx >= elf_numsections (abfd)
735
63.5k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
736
59.4k
    || ((elt = shdr->bfd_section) != NULL
737
37.5k
        && elf_sec_group (elt) != NULL
738
18.9k
        && elf_sec_group (elt) != ghdr->bfd_section))
739
405k
  {
740
405k
    _bfd_error_handler
741
405k
      (_("%pB: invalid entry (%#x) in group [%u]"),
742
405k
       abfd, idx, gidx);
743
405k
    continue;
744
405k
  }
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
54.8k
      shdr->sh_flags |= SHF_GROUP;
750
751
54.8k
      if (elt == NULL)
752
21.8k
  {
753
21.8k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
754
9.22k
      {
755
9.22k
        const char *name = bfd_elf_string_from_elf_section
756
9.22k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
757
758
9.22k
        _bfd_error_handler
759
    /* xgettext:c-format */
760
9.22k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
761
9.22k
     abfd, shdr->sh_type, name, gidx);
762
9.22k
      }
763
21.8k
    continue;
764
21.8k
  }
765
766
      /* Don't try to add a section to elf_next_in_group list twice.  */
767
32.9k
      if (elf_sec_group (elt) != NULL)
768
14.3k
  continue;
769
770
18.5k
      if (last_elt == NULL)
771
8.95k
  {
772
    /* Start a circular list with one element.
773
       It will be in reverse order to match what gas does.  */
774
8.95k
    elf_next_in_group (elt) = elt;
775
    /* Point the group section to it.  */
776
8.95k
    elf_next_in_group (ghdr->bfd_section) = elt;
777
8.95k
    gname = group_signature (abfd, ghdr);
778
8.95k
    if (gname == NULL)
779
3.87k
      {
780
3.87k
        free (contents);
781
3.87k
        return false;
782
3.87k
      }
783
8.95k
  }
784
9.61k
      else
785
9.61k
  {
786
9.61k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
787
9.61k
    elf_next_in_group (last_elt) = elt;
788
9.61k
  }
789
14.6k
      last_elt = elt;
790
14.6k
      elf_group_name (elt) = gname;
791
14.6k
      elf_sec_group (elt) = ghdr->bfd_section;
792
14.6k
    }
793
794
7.44k
  free (contents);
795
7.44k
  return last_elt != NULL;
796
11.3k
}
797
798
bool
799
_bfd_elf_setup_sections (bfd *abfd)
800
33.0k
{
801
33.0k
  bool result = true;
802
803
  /* Process SHF_LINK_ORDER.  */
804
357k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
805
324k
    {
806
324k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
807
324k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
808
21.3k
  {
809
21.3k
    unsigned int elfsec = this_hdr->sh_link;
810
    /* An sh_link value of 0 is now allowed.  It indicates that linked
811
       to section has already been discarded, but that the current
812
       section has been retained for some other reason.  This linking
813
       section is still a candidate for later garbage collection
814
       however.  */
815
21.3k
    if (elfsec == 0)
816
16.1k
      {
817
16.1k
        elf_linked_to_section (s) = NULL;
818
16.1k
      }
819
5.17k
    else
820
5.17k
      {
821
5.17k
        asection *linksec = NULL;
822
823
5.17k
        if (elfsec < elf_numsections (abfd))
824
5.03k
    {
825
5.03k
      this_hdr = elf_elfsections (abfd)[elfsec];
826
5.03k
      linksec = this_hdr->bfd_section;
827
5.03k
    }
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.17k
        if (linksec == NULL)
833
1.84k
    {
834
1.84k
      _bfd_error_handler
835
        /* xgettext:c-format */
836
1.84k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
837
1.84k
         s->owner, elfsec, s);
838
1.84k
      result = false;
839
1.84k
    }
840
841
5.17k
        elf_linked_to_section (s) = linksec;
842
5.17k
      }
843
21.3k
  }
844
324k
    }
845
846
  /* Process section groups.  */
847
537k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
848
504k
    {
849
504k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
850
851
504k
      if (shdr && shdr->sh_type == SHT_GROUP)
852
13.6k
  {
853
13.6k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
854
12.1k
      {
855
12.1k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
856
12.0k
      && !process_sht_group_entries (abfd, shdr, i))
857
7.00k
    result = false;
858
12.1k
      }
859
1.42k
    else
860
1.42k
      {
861
        /* PR binutils/18758: Beware of corrupt binaries with
862
     invalid group data.  */
863
1.42k
        _bfd_error_handler
864
    /* xgettext:c-format */
865
1.42k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
866
1.42k
        result = false;
867
1.42k
      }
868
13.6k
  }
869
504k
    }
870
871
33.0k
  return result;
872
33.0k
}
873
874
bool
875
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
876
0
{
877
0
  return elf_next_in_group (sec) != NULL;
878
0
}
879
880
const char *
881
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
882
0
{
883
0
  if (elf_sec_group (sec) != NULL)
884
0
    return elf_group_name (sec);
885
0
  return NULL;
886
0
}
887
888
/* Make a BFD section from an ELF section.  We store a pointer to the
889
   BFD section in the bfd_section field of the header.  */
890
891
bool
892
_bfd_elf_make_section_from_shdr (bfd *abfd,
893
         Elf_Internal_Shdr *hdr,
894
         const char *name,
895
         int shindex)
896
490k
{
897
490k
  asection *newsect;
898
490k
  flagword flags;
899
490k
  elf_backend_data *bed;
900
490k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
901
902
490k
  if (hdr->bfd_section != NULL)
903
74.1k
    return true;
904
905
415k
  newsect = bfd_make_section_anyway (abfd, name);
906
415k
  if (newsect == NULL)
907
0
    return false;
908
909
415k
  hdr->bfd_section = newsect;
910
415k
  elf_section_data (newsect)->this_hdr = *hdr;
911
415k
  elf_section_data (newsect)->this_idx = shindex;
912
913
  /* Always use the real type/flags.  */
914
415k
  elf_section_type (newsect) = hdr->sh_type;
915
415k
  elf_section_flags (newsect) = hdr->sh_flags;
916
917
415k
  newsect->filepos = hdr->sh_offset;
918
919
415k
  flags = SEC_NO_FLAGS;
920
415k
  if (hdr->sh_type != SHT_NOBITS)
921
405k
    flags |= SEC_HAS_CONTENTS;
922
415k
  if (hdr->sh_type == SHT_GROUP)
923
18.1k
    flags |= SEC_GROUP;
924
415k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
925
184k
    {
926
184k
      flags |= SEC_ALLOC;
927
184k
      if (hdr->sh_type != SHT_NOBITS)
928
177k
  flags |= SEC_LOAD;
929
184k
    }
930
415k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
931
323k
    flags |= SEC_READONLY;
932
415k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
933
91.2k
    flags |= SEC_CODE;
934
324k
  else if ((flags & SEC_LOAD) != 0)
935
98.9k
    flags |= SEC_DATA;
936
415k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
937
66.2k
    flags |= SEC_MERGE;
938
415k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
939
75.3k
    flags |= SEC_STRINGS;
940
415k
  if ((hdr->sh_flags & SHF_TLS) != 0)
941
33.3k
    flags |= SEC_THREAD_LOCAL;
942
415k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
943
31.7k
    flags |= SEC_EXCLUDE;
944
945
415k
  newsect->entsize = hdr->sh_entsize;
946
947
415k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
948
415k
    {
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
171k
    case ELFOSABI_GNU:
953
257k
    case ELFOSABI_FREEBSD:
954
257k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
955
43.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
956
      /* Fall through */
957
354k
    case ELFOSABI_NONE:
958
354k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
959
24.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
960
354k
      break;
961
415k
    }
962
963
415k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
964
213k
    {
965
      /* The debugging sections appear to be recognized only by name,
966
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
967
213k
      if (name [0] == '.')
968
88.4k
  {
969
88.4k
    if (startswith (name, ".debug")
970
57.8k
        || startswith (name, ".gnu.debuglto_.debug_")
971
57.5k
        || startswith (name, ".gnu.linkonce.wi.")
972
57.1k
        || startswith (name, ".zdebug"))
973
32.0k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
974
56.3k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
975
56.1k
       || startswith (name, ".note.gnu"))
976
522
      {
977
522
        flags |= SEC_ELF_OCTETS;
978
522
        opb = 1;
979
522
      }
980
55.8k
    else if (startswith (name, ".line")
981
55.3k
       || startswith (name, ".stab")
982
54.2k
       || streq (name, ".gdb_index"))
983
2.19k
      flags |= SEC_DEBUGGING;
984
88.4k
  }
985
213k
    }
986
987
415k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
988
415k
      || !bfd_set_section_size (newsect, hdr->sh_size)
989
415k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
990
415k
              & -hdr->sh_addralign)))
991
261
    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
415k
  if (startswith (name, ".gnu.linkonce")
1000
1.72k
      && elf_next_in_group (newsect) == NULL)
1001
1.72k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1002
1003
415k
  if (!bfd_set_section_flags (newsect, flags))
1004
0
    return false;
1005
1006
415k
  bed = get_elf_backend_data (abfd);
1007
415k
  if (bed->elf_backend_section_flags)
1008
133k
    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
415k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1015
7.40k
    {
1016
7.40k
      bfd_byte *contents;
1017
1018
7.40k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1019
1.44k
  return false;
1020
1021
5.95k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1022
5.95k
           hdr->sh_offset, hdr->sh_addralign);
1023
5.95k
      _bfd_elf_munmap_section_contents (newsect, contents);
1024
5.95k
    }
1025
1026
414k
  if ((newsect->flags & SEC_ALLOC) != 0)
1027
184k
    {
1028
184k
      Elf_Internal_Phdr *phdr;
1029
184k
      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
184k
      phdr = elf_tdata (abfd)->phdr;
1036
205k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1037
73.2k
  if (phdr->p_paddr != 0)
1038
52.3k
    break;
1039
20.9k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1040
2.04k
    ++nload;
1041
184k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1042
128
  return true;
1043
1044
183k
      phdr = elf_tdata (abfd)->phdr;
1045
2.48M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1046
2.32M
  {
1047
2.32M
    if (((phdr->p_type == PT_LOAD
1048
89.0k
    && (hdr->sh_flags & SHF_TLS) == 0)
1049
2.23M
         || phdr->p_type == PT_TLS)
1050
90.1k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1051
18.5k
      {
1052
18.5k
        if ((newsect->flags & SEC_LOAD) == 0)
1053
1.07k
    newsect->lma = (phdr->p_paddr
1054
1.07k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1055
17.4k
        else
1056
    /* We used to use the same adjustment for SEC_LOAD
1057
       sections, but that doesn't work if the segment
1058
       is packed with code from multiple VMAs.
1059
       Instead we calculate the section LMA based on
1060
       the segment LMA.  It is assumed that the
1061
       segment will contain sections with contiguous
1062
       LMAs, even if the VMAs are not.  */
1063
17.4k
    newsect->lma = (phdr->p_paddr
1064
17.4k
        + hdr->sh_offset - phdr->p_offset) / opb;
1065
1066
        /* With contiguous segments, we can't tell from file
1067
     offsets whether a section with zero size should
1068
     be placed at the end of one segment or the
1069
     beginning of the next.  Decide based on vaddr.  */
1070
18.5k
        if (hdr->sh_addr >= phdr->p_vaddr
1071
18.5k
      && (hdr->sh_addr + hdr->sh_size
1072
18.5k
          <= phdr->p_vaddr + phdr->p_memsz))
1073
17.1k
    break;
1074
18.5k
      }
1075
2.32M
  }
1076
183k
    }
1077
1078
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1079
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1080
414k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1081
34.2k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1082
34.1k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1083
31.9k
    {
1084
31.9k
      enum { nothing, compress, decompress } action = nothing;
1085
31.9k
      int compression_header_size;
1086
31.9k
      bfd_size_type uncompressed_size;
1087
31.9k
      unsigned int uncompressed_align_power;
1088
31.9k
      enum compression_type ch_type = ch_none;
1089
31.9k
      bool compressed
1090
31.9k
  = bfd_is_section_compressed_info (abfd, newsect,
1091
31.9k
            &compression_header_size,
1092
31.9k
            &uncompressed_size,
1093
31.9k
            &uncompressed_align_power,
1094
31.9k
            &ch_type);
1095
1096
      /* Should we decompress?  */
1097
31.9k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1098
173
  action = decompress;
1099
1100
      /* Should we compress?  Or convert to a different compression?  */
1101
31.7k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1102
2.59k
         && newsect->size != 0
1103
2.53k
         && compression_header_size >= 0
1104
2.44k
         && uncompressed_size > 0)
1105
2.44k
  {
1106
2.44k
    if (!compressed)
1107
2.41k
      action = compress;
1108
31
    else
1109
31
      {
1110
31
        enum compression_type new_ch_type = ch_none;
1111
31
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1112
31
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1113
31
             ? ch_compress_zstd : ch_compress_zlib);
1114
31
        if (new_ch_type != ch_type)
1115
25
    action = compress;
1116
31
      }
1117
2.44k
  }
1118
1119
31.9k
      if (action == compress)
1120
2.43k
  {
1121
2.43k
    if (!bfd_init_section_compress_status (abfd, newsect))
1122
192
      {
1123
192
        _bfd_error_handler
1124
    /* xgettext:c-format */
1125
192
    (_("%pB: unable to compress section %s"), abfd, name);
1126
192
        return false;
1127
192
      }
1128
2.43k
  }
1129
29.5k
      else if (action == decompress)
1130
173
  {
1131
173
    if (!bfd_init_section_decompress_status (abfd, newsect))
1132
128
      {
1133
128
        _bfd_error_handler
1134
    /* xgettext:c-format */
1135
128
    (_("%pB: unable to decompress section %s"), abfd, name);
1136
128
        return false;
1137
128
      }
1138
45
#ifndef HAVE_ZSTD
1139
45
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1140
12
      {
1141
12
        _bfd_error_handler
1142
      /* xgettext:c-format */
1143
12
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1144
12
          "is not built with zstd support"),
1145
12
       abfd, name);
1146
12
        newsect->compress_status = COMPRESS_SECTION_NONE;
1147
12
        return false;
1148
12
      }
1149
33
#endif
1150
33
    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
33
  }
1161
31.9k
    }
1162
1163
413k
  return true;
1164
414k
}
1165
1166
const char *const bfd_elf_section_type_names[] =
1167
{
1168
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1169
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1170
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1171
};
1172
1173
/* ELF relocs are against symbols.  If we are producing relocatable
1174
   output, and the reloc is against an external symbol, and nothing
1175
   has given us any additional addend, the resulting reloc will also
1176
   be against the same symbol.  In such a case, we don't want to
1177
   change anything about the way the reloc is handled, since it will
1178
   all be done at final link time.  Rather than put special case code
1179
   into bfd_perform_relocation, all the reloc types use this howto
1180
   function, or should call this function for relocatable output.  */
1181
1182
bfd_reloc_status_type
1183
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1184
           arelent *reloc_entry,
1185
           asymbol *symbol,
1186
           void *data ATTRIBUTE_UNUSED,
1187
           asection *input_section,
1188
           bfd *output_bfd,
1189
           char **error_message ATTRIBUTE_UNUSED)
1190
13.7k
{
1191
13.7k
  if (output_bfd != NULL
1192
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1193
0
      && (! reloc_entry->howto->partial_inplace
1194
0
    || reloc_entry->addend == 0))
1195
0
    {
1196
0
      reloc_entry->address += input_section->output_offset;
1197
0
      return bfd_reloc_ok;
1198
0
    }
1199
1200
  /* In some cases the relocation should be treated as output section
1201
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1202
     targets lack section relative relocations and instead use
1203
     ordinary absolute relocations for references between DWARF
1204
     sections.  That is arguably a bug in those targets but it happens
1205
     to work for the usual case of linking to non-loaded ELF debug
1206
     sections with VMAs forced to zero.  PE COFF on the other hand
1207
     doesn't allow a section VMA of zero.  */
1208
13.7k
  if (output_bfd == NULL
1209
13.7k
      && !reloc_entry->howto->pc_relative
1210
13.0k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1211
5.10k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1212
4.87k
    reloc_entry->addend -= symbol->section->output_section->vma;
1213
1214
13.7k
  return bfd_reloc_continue;
1215
13.7k
}
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
42
{
1225
42
  if (a->sh_type != b->sh_type
1226
2
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1227
2
      || a->sh_addralign != b->sh_addralign
1228
2
      || a->sh_entsize != b->sh_entsize)
1229
40
    return false;
1230
2
  if (a->sh_type == SHT_SYMTAB
1231
0
      || a->sh_type == SHT_STRTAB)
1232
2
    return true;
1233
0
  return a->sh_size == b->sh_size;
1234
2
}
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
2
{
1245
2
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1246
2
  unsigned int i;
1247
1248
2
  BFD_ASSERT (iheader != NULL);
1249
1250
  /* See PR 20922 for a reproducer of the NULL test.  */
1251
2
  if (hint < elf_numsections (obfd)
1252
2
      && oheaders[hint] != NULL
1253
2
      && section_match (oheaders[hint], iheader))
1254
0
    return hint;
1255
1256
40
  for (i = 1; i < elf_numsections (obfd); i++)
1257
40
    {
1258
40
      Elf_Internal_Shdr * oheader = oheaders[i];
1259
1260
40
      if (oheader == NULL)
1261
0
  continue;
1262
40
      if (section_match (oheader, iheader))
1263
  /* FIXME: Do we care if there is a potential for
1264
     multiple matches ?  */
1265
2
  return i;
1266
40
    }
1267
1268
0
  return SHN_UNDEF;
1269
2
}
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
88
{
1282
88
  elf_backend_data *bed = get_elf_backend_data (obfd);
1283
88
  const Elf_Internal_Shdr **iheaders
1284
88
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1285
88
  bool changed = false;
1286
88
  unsigned int sh_link;
1287
1288
88
  if (oheader->sh_type == SHT_NOBITS)
1289
22
    {
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
22
      if (oheader->sh_link == 0)
1307
22
  oheader->sh_link = iheader->sh_link;
1308
22
      if (oheader->sh_info == 0)
1309
22
  oheader->sh_info = iheader->sh_info;
1310
22
      return true;
1311
22
    }
1312
1313
  /* Allow the target a chance to decide how these fields should be set.  */
1314
66
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1315
66
                iheader, oheader))
1316
61
    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
5
  if (iheader->sh_link != SHN_UNDEF)
1323
2
    {
1324
      /* See PR 20931 for a reproducer.  */
1325
2
      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
2
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1335
2
      if (sh_link != SHN_UNDEF)
1336
2
  {
1337
2
    oheader->sh_link = sh_link;
1338
2
    changed = true;
1339
2
  }
1340
0
      else
1341
  /* FIXME: Should we install iheader->sh_link
1342
     if we could not find a match ?  */
1343
0
  _bfd_error_handler
1344
    /* xgettext:c-format */
1345
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1346
2
    }
1347
1348
5
  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
5
  return changed;
1376
5
}
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
313
{
1384
313
  const Elf_Internal_Shdr **iheaders;
1385
313
  Elf_Internal_Shdr **oheaders;
1386
313
  elf_backend_data *bed;
1387
313
  unsigned int i;
1388
1389
313
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1390
0
    return true;
1391
1392
313
  if (!elf_flags_init (obfd))
1393
308
    {
1394
308
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1395
308
      elf_flags_init (obfd) = true;
1396
308
    }
1397
1398
313
  elf_gp (obfd) = elf_gp (ibfd);
1399
1400
  /* Also copy the EI_OSABI field.  */
1401
313
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1402
313
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1403
1404
  /* If set, copy the EI_ABIVERSION field.  */
1405
313
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1406
122
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1407
122
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1408
1409
  /* Copy object attributes.  */
1410
313
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1411
1412
313
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1413
313
  oheaders = elf_elfsections (obfd);
1414
313
  if (iheaders == NULL || oheaders == NULL)
1415
265
    return true;
1416
1417
48
  bed = get_elf_backend_data (obfd);
1418
1419
  /* Possibly copy other fields in the section header.  */
1420
1.22k
  for (i = 1; i < elf_numsections (obfd); i++)
1421
1.17k
    {
1422
1.17k
      unsigned int j;
1423
1.17k
      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.17k
      if (oheader == NULL
1429
1.17k
    || (oheader->sh_type != SHT_NOBITS
1430
1.14k
        && oheader->sh_type < SHT_LOOS))
1431
1.07k
  continue;
1432
1433
      /* Ignore empty sections, and sections whose
1434
   fields have already been initialised.  */
1435
96
      if (oheader->sh_size == 0
1436
91
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1437
8
  continue;
1438
1439
      /* Scan for the matching section in the input bfd.
1440
   First we try for a direct mapping between the input and
1441
   output sections.  */
1442
1.71k
      for (j = 1; j < elf_numsections (ibfd); j++)
1443
1.71k
  {
1444
1.71k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1445
1446
1.71k
    if (iheader == NULL)
1447
0
      continue;
1448
1449
1.71k
    if (oheader->bfd_section != NULL
1450
1.71k
        && iheader->bfd_section != NULL
1451
1.28k
        && iheader->bfd_section->output_section != NULL
1452
1.28k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1453
88
      {
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
88
        if (!copy_special_section_fields (ibfd, obfd,
1460
88
            iheader, oheader, i))
1461
3
    j = elf_numsections (ibfd);
1462
88
        break;
1463
88
      }
1464
1.71k
  }
1465
1466
88
      if (j < elf_numsections (ibfd))
1467
85
  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
66
      for (j = 1; j < elf_numsections (ibfd); j++)
1473
63
  {
1474
63
    const Elf_Internal_Shdr * iheader = iheaders[j];
1475
1476
63
    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
63
    if ((oheader->sh_type == SHT_NOBITS
1484
63
         || iheader->sh_type == oheader->sh_type)
1485
3
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1486
3
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1487
3
        && iheader->sh_addralign == oheader->sh_addralign
1488
3
        && iheader->sh_entsize == oheader->sh_entsize
1489
3
        && iheader->sh_size == oheader->sh_size
1490
3
        && iheader->sh_addr == oheader->sh_addr
1491
3
        && (iheader->sh_info != oheader->sh_info
1492
3
      || iheader->sh_link != oheader->sh_link))
1493
0
      {
1494
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1495
0
    break;
1496
0
      }
1497
63
  }
1498
1499
3
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1500
3
  {
1501
    /* Final attempt.  Call the backend copy function
1502
       with a NULL input section.  */
1503
3
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1504
3
                     NULL, oheader);
1505
3
  }
1506
3
    }
1507
1508
48
  return true;
1509
313
}
1510
1511
static const char *
1512
get_segment_type (unsigned int p_type)
1513
164k
{
1514
164k
  const char *pt;
1515
164k
  switch (p_type)
1516
164k
    {
1517
31.9k
    case PT_NULL: pt = "NULL"; break;
1518
2.31k
    case PT_LOAD: pt = "LOAD"; break;
1519
989
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1520
685
    case PT_INTERP: pt = "INTERP"; break;
1521
341
    case PT_NOTE: pt = "NOTE"; break;
1522
119
    case PT_SHLIB: pt = "SHLIB"; break;
1523
2.77k
    case PT_PHDR: pt = "PHDR"; break;
1524
63
    case PT_TLS: pt = "TLS"; break;
1525
303
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1526
232
    case PT_GNU_STACK: pt = "STACK"; break;
1527
212
    case PT_GNU_RELRO: pt = "RELRO"; break;
1528
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1529
124k
    default: pt = NULL; break;
1530
164k
    }
1531
164k
  return pt;
1532
164k
}
1533
1534
/* Print out the program headers.  */
1535
1536
bool
1537
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1538
8.80k
{
1539
8.80k
  FILE *f = (FILE *) farg;
1540
8.80k
  Elf_Internal_Phdr *p;
1541
8.80k
  asection *s;
1542
8.80k
  bfd_byte *dynbuf = NULL;
1543
1544
8.80k
  p = elf_tdata (abfd)->phdr;
1545
8.80k
  if (p != NULL)
1546
6.38k
    {
1547
6.38k
      unsigned int i, c;
1548
1549
6.38k
      fprintf (f, _("\nProgram Header:\n"));
1550
6.38k
      c = elf_elfheader (abfd)->e_phnum;
1551
171k
      for (i = 0; i < c; i++, p++)
1552
164k
  {
1553
164k
    const char *pt = get_segment_type (p->p_type);
1554
164k
    char buf[20];
1555
1556
164k
    if (pt == NULL)
1557
124k
      {
1558
124k
        sprintf (buf, "0x%lx", p->p_type);
1559
124k
        pt = buf;
1560
124k
      }
1561
164k
    fprintf (f, "%8s off    0x", pt);
1562
164k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1563
164k
    fprintf (f, " vaddr 0x");
1564
164k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1565
164k
    fprintf (f, " paddr 0x");
1566
164k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1567
164k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1568
164k
    fprintf (f, "         filesz 0x");
1569
164k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1570
164k
    fprintf (f, " memsz 0x");
1571
164k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1572
164k
    fprintf (f, " flags %c%c%c",
1573
164k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1574
164k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1575
164k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1576
164k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1577
114k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1578
164k
    fprintf (f, "\n");
1579
164k
  }
1580
6.38k
    }
1581
1582
8.80k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1583
8.80k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1584
248
    {
1585
248
      unsigned int elfsec;
1586
248
      unsigned long shlink;
1587
248
      bfd_byte *extdyn, *extdynend;
1588
248
      size_t extdynsize;
1589
248
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1590
1591
248
      fprintf (f, _("\nDynamic Section:\n"));
1592
1593
248
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1594
13
  goto error_return;
1595
1596
235
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1597
235
      if (elfsec == SHN_BAD)
1598
0
  goto error_return;
1599
235
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1600
1601
235
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1602
235
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1603
1604
235
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1605
4.51k
     (size_t) (extdynend - extdyn) >= extdynsize;
1606
4.27k
     extdyn += extdynsize)
1607
4.49k
  {
1608
4.49k
    Elf_Internal_Dyn dyn;
1609
4.49k
    const char *name = "";
1610
4.49k
    char ab[20];
1611
4.49k
    bool stringp;
1612
4.49k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1613
1614
4.49k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1615
1616
4.49k
    if (dyn.d_tag == DT_NULL)
1617
209
      break;
1618
1619
4.29k
    stringp = false;
1620
4.29k
    switch (dyn.d_tag)
1621
4.29k
      {
1622
594
      default:
1623
594
        if (bed->elf_backend_get_target_dtag)
1624
45
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1625
1626
594
        if (streq (name, ""))
1627
594
    {
1628
594
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1629
594
      name = ab;
1630
594
    }
1631
594
        break;
1632
1633
218
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1634
77
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1635
160
      case DT_PLTGOT: name = "PLTGOT"; break;
1636
88
      case DT_HASH: name = "HASH"; break;
1637
177
      case DT_STRTAB: name = "STRTAB"; break;
1638
174
      case DT_SYMTAB: name = "SYMTAB"; break;
1639
162
      case DT_RELA: name = "RELA"; break;
1640
169
      case DT_RELASZ: name = "RELASZ"; break;
1641
163
      case DT_RELAENT: name = "RELAENT"; break;
1642
168
      case DT_STRSZ: name = "STRSZ"; break;
1643
167
      case DT_SYMENT: name = "SYMENT"; break;
1644
172
      case DT_INIT: name = "INIT"; break;
1645
172
      case DT_FINI: name = "FINI"; break;
1646
4
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1647
39
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1648
5
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1649
5
      case DT_REL: name = "REL"; break;
1650
5
      case DT_RELSZ: name = "RELSZ"; break;
1651
4
      case DT_RELENT: name = "RELENT"; break;
1652
2
      case DT_RELR: name = "RELR"; break;
1653
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1654
0
      case DT_RELRENT: name = "RELRENT"; break;
1655
80
      case DT_PLTREL: name = "PLTREL"; break;
1656
164
      case DT_DEBUG: name = "DEBUG"; break;
1657
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1658
77
      case DT_JMPREL: name = "JMPREL"; break;
1659
2
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1660
102
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1661
100
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1662
97
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1663
96
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1664
1
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1665
84
      case DT_FLAGS: name = "FLAGS"; break;
1666
4
      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
111
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1683
2
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1684
116
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1685
134
      case DT_VERSYM: name = "VERSYM"; break;
1686
0
      case DT_VERDEF: name = "VERDEF"; break;
1687
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1688
135
      case DT_VERNEED: name = "VERNEED"; break;
1689
129
      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
125
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1694
4.29k
      }
1695
1696
4.29k
    fprintf (f, "  %-20s ", name);
1697
4.29k
    if (! stringp)
1698
4.02k
      {
1699
4.02k
        fprintf (f, "0x");
1700
4.02k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1701
4.02k
      }
1702
262
    else
1703
262
      {
1704
262
        const char *string;
1705
262
        unsigned int tagv = dyn.d_un.d_val;
1706
1707
262
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1708
262
        if (string == NULL)
1709
11
    goto error_return;
1710
251
        fprintf (f, "%s", string);
1711
251
      }
1712
4.27k
    fprintf (f, "\n");
1713
4.27k
  }
1714
1715
224
      _bfd_elf_munmap_section_contents (s, dynbuf);
1716
224
      dynbuf = NULL;
1717
224
    }
1718
1719
8.77k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1720
8.77k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1721
191
    {
1722
191
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1723
44
  return false;
1724
191
    }
1725
1726
8.73k
  if (elf_dynverdef (abfd) != 0)
1727
1
    {
1728
1
      Elf_Internal_Verdef *t;
1729
1730
1
      fprintf (f, _("\nVersion definitions:\n"));
1731
2
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1732
1
  {
1733
1
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1734
1
       t->vd_flags, t->vd_hash,
1735
1
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1736
1
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1737
0
      {
1738
0
        Elf_Internal_Verdaux *a;
1739
1740
0
        fprintf (f, "\t");
1741
0
        for (a = t->vd_auxptr->vda_nextptr;
1742
0
       a != NULL;
1743
0
       a = a->vda_nextptr)
1744
0
    fprintf (f, "%s ",
1745
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1746
0
        fprintf (f, "\n");
1747
0
      }
1748
1
  }
1749
1
    }
1750
1751
8.73k
  if (elf_dynverref (abfd) != 0)
1752
146
    {
1753
146
      Elf_Internal_Verneed *t;
1754
1755
146
      fprintf (f, _("\nVersion References:\n"));
1756
292
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1757
146
  {
1758
146
    Elf_Internal_Vernaux *a;
1759
1760
146
    fprintf (f, _("  required from %s:\n"),
1761
146
       t->vn_filename ? t->vn_filename : "<corrupt>");
1762
376
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1763
230
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1764
230
         a->vna_flags, a->vna_other,
1765
230
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1766
146
  }
1767
146
    }
1768
1769
8.73k
  return true;
1770
1771
24
 error_return:
1772
24
  _bfd_elf_munmap_section_contents (s, dynbuf);
1773
24
  return false;
1774
8.77k
}
1775
1776
/* Find the file offset corresponding to VMA by using the program
1777
   headers.  */
1778
1779
static file_ptr
1780
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1781
     size_t size, size_t *max_size_p)
1782
3.44k
{
1783
3.44k
  Elf_Internal_Phdr *seg;
1784
3.44k
  size_t i;
1785
1786
12.5k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1787
12.2k
    if (seg->p_type == PT_LOAD
1788
5.32k
  && vma >= (seg->p_vaddr & -seg->p_align)
1789
4.22k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1790
3.16k
      {
1791
3.16k
  if (max_size_p)
1792
47
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1793
3.16k
  return vma - seg->p_vaddr + seg->p_offset;
1794
3.16k
      }
1795
1796
282
  if (max_size_p)
1797
41
    *max_size_p = 0;
1798
282
  bfd_set_error (bfd_error_invalid_operation);
1799
282
  return (file_ptr) -1;
1800
3.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
486
{
1808
486
  unsigned char *e_data = NULL;
1809
486
  bfd_vma *i_data = NULL;
1810
486
  bfd_size_type size;
1811
486
  void *e_data_addr;
1812
486
  size_t e_data_size ATTRIBUTE_UNUSED;
1813
1814
486
  if (ent_size != 4 && ent_size != 8)
1815
0
    return NULL;
1816
1817
486
  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
486
  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
486
  if (size > filesize
1827
433
      || number >= ~(size_t) 0 / ent_size
1828
433
      || number >= ~(size_t) 0 / sizeof (*i_data))
1829
53
    {
1830
53
      bfd_set_error (bfd_error_file_too_big);
1831
53
      return NULL;
1832
53
    }
1833
1834
433
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1835
433
  if (e_data == NULL)
1836
14
    return NULL;
1837
1838
419
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1839
419
  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
419
  if (ent_size == 4)
1846
345k
    while (number--)
1847
345k
      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
419
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1853
419
  return i_data;
1854
419
}
1855
1856
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1857
   support DT_MIPS_XHASH?  */
1858
109k
#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.11k
{
1867
3.11k
  bfd_byte *extdyn, *extdynend;
1868
3.11k
  size_t extdynsize;
1869
3.11k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1870
3.11k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1871
3.11k
        Elf_Internal_Sym *);
1872
3.11k
  Elf_Internal_Dyn dyn;
1873
3.11k
  bfd_vma dt_hash = 0;
1874
3.11k
  bfd_vma dt_gnu_hash = 0;
1875
3.11k
  bfd_vma dt_mips_xhash = 0;
1876
3.11k
  bfd_vma dt_strtab = 0;
1877
3.11k
  bfd_vma dt_symtab = 0;
1878
3.11k
  size_t dt_strsz = 0;
1879
3.11k
  bfd_vma dt_versym = 0;
1880
3.11k
  bfd_vma dt_verdef = 0;
1881
3.11k
  bfd_vma dt_verneed = 0;
1882
3.11k
  bfd_byte *dynbuf = NULL;
1883
3.11k
  char *strbuf = NULL;
1884
3.11k
  bfd_vma *gnubuckets = NULL;
1885
3.11k
  bfd_vma *gnuchains = NULL;
1886
3.11k
  bfd_vma *mipsxlat = NULL;
1887
3.11k
  file_ptr saved_filepos, filepos;
1888
3.11k
  bool res = false;
1889
3.11k
  size_t amt;
1890
3.11k
  bfd_byte *esymbuf = NULL, *esym;
1891
3.11k
  bfd_size_type symcount;
1892
3.11k
  Elf_Internal_Sym *isymbuf = NULL;
1893
3.11k
  Elf_Internal_Sym *isym, *isymend;
1894
3.11k
  bfd_byte *versym = NULL;
1895
3.11k
  bfd_byte *verdef = NULL;
1896
3.11k
  bfd_byte *verneed = NULL;
1897
3.11k
  size_t verdef_size = 0;
1898
3.11k
  size_t verneed_size = 0;
1899
3.11k
  size_t extsym_size;
1900
3.11k
  elf_backend_data *bed;
1901
3.11k
  void *dynbuf_addr = NULL;
1902
3.11k
  void *esymbuf_addr = NULL;
1903
3.11k
  size_t dynbuf_size = 0;
1904
3.11k
  size_t esymbuf_size = 0;
1905
1906
  /* Return TRUE if symbol table is bad.  */
1907
3.11k
  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.11k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1912
0
    return true;
1913
1914
3.11k
  bed = get_elf_backend_data (abfd);
1915
1916
  /* Save file position for elf_object_p.  */
1917
3.11k
  saved_filepos = bfd_tell (abfd);
1918
1919
3.11k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1920
0
    goto error_return;
1921
1922
3.11k
  dynbuf_size = phdr->p_filesz;
1923
3.11k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1924
3.11k
  if (dynbuf == NULL)
1925
82
    goto error_return;
1926
1927
3.03k
  extsym_size = bed->s->sizeof_sym;
1928
3.03k
  extdynsize = bed->s->sizeof_dyn;
1929
3.03k
  swap_dyn_in = bed->s->swap_dyn_in;
1930
1931
3.03k
  extdyn = dynbuf;
1932
3.03k
  if (phdr->p_filesz < extdynsize)
1933
45
    goto error_return;
1934
2.98k
  extdynend = extdyn + phdr->p_filesz;
1935
65.5k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1936
64.2k
    {
1937
64.2k
      swap_dyn_in (abfd, extdyn, &dyn);
1938
1939
64.2k
      if (dyn.d_tag == DT_NULL)
1940
1.53k
  break;
1941
1942
62.7k
      switch (dyn.d_tag)
1943
62.7k
  {
1944
1.00k
  case DT_HASH:
1945
1.00k
    dt_hash = dyn.d_un.d_val;
1946
1.00k
    break;
1947
1.08k
  case DT_GNU_HASH:
1948
1.08k
    if (bed->elf_machine_code != EM_MIPS
1949
922
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1950
922
      dt_gnu_hash = dyn.d_un.d_val;
1951
1.08k
    break;
1952
1.43k
  case DT_STRTAB:
1953
1.43k
    dt_strtab = dyn.d_un.d_val;
1954
1.43k
    break;
1955
1.30k
  case DT_SYMTAB:
1956
1.30k
    dt_symtab = dyn.d_un.d_val;
1957
1.30k
    break;
1958
1.36k
  case DT_STRSZ:
1959
1.36k
    dt_strsz = dyn.d_un.d_val;
1960
1.36k
    break;
1961
623
  case DT_SYMENT:
1962
623
    if (dyn.d_un.d_val != extsym_size)
1963
110
      goto error_return;
1964
513
    break;
1965
599
  case DT_VERSYM:
1966
599
    dt_versym = dyn.d_un.d_val;
1967
599
    break;
1968
390
  case DT_VERDEF:
1969
390
    dt_verdef = dyn.d_un.d_val;
1970
390
    break;
1971
320
  case DT_VERNEED:
1972
320
    dt_verneed = dyn.d_un.d_val;
1973
320
    break;
1974
54.5k
  default:
1975
54.5k
    if (dyn.d_tag == DT_MIPS_XHASH
1976
541
        && (bed->elf_machine_code == EM_MIPS
1977
261
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1978
280
      {
1979
280
        dt_gnu_hash = dyn.d_un.d_val;
1980
280
        dt_mips_xhash = dyn.d_un.d_val;
1981
280
      }
1982
54.5k
    break;
1983
62.7k
  }
1984
62.7k
    }
1985
1986
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1987
     segment.  */
1988
2.87k
  if ((!dt_hash && !dt_gnu_hash)
1989
1.74k
      || !dt_strtab
1990
1.27k
      || !dt_symtab
1991
1.13k
      || !dt_strsz)
1992
1.80k
    goto error_return;
1993
1994
  /* Get dynamic string table.  */
1995
1.07k
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1996
1.07k
  if (filepos == (file_ptr) -1
1997
1.00k
      || 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
957
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2002
957
  if (strbuf == NULL)
2003
37
    goto error_return;
2004
920
  if (strbuf[dt_strsz - 1] != 0)
2005
242
    {
2006
      /* It is an error if a string table is't terminated.  */
2007
242
      _bfd_error_handler
2008
  /* xgettext:c-format */
2009
242
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2010
242
      strbuf[dt_strsz - 1] = 0;
2011
242
    }
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
920
  if (dt_hash)
2016
386
    {
2017
386
      unsigned char nb[16];
2018
386
      unsigned int hash_ent_size;
2019
2020
386
      switch (bed->elf_machine_code)
2021
386
  {
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
386
  default:
2032
386
    hash_ent_size = 4;
2033
386
    break;
2034
386
  }
2035
2036
386
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2037
386
         NULL);
2038
386
      if (filepos == (file_ptr) -1
2039
357
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2040
343
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2041
60
  goto error_return;
2042
2043
      /* The number of dynamic symbol table entries equals the number
2044
   of chains.  */
2045
326
      if (hash_ent_size == 8)
2046
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2047
326
      else
2048
326
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2049
326
    }
2050
534
  else
2051
534
    {
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
534
      unsigned char nb[16];
2059
534
      bfd_vma ngnubuckets;
2060
534
      bfd_vma gnusymidx;
2061
534
      size_t i, ngnuchains;
2062
534
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2063
534
      bfd_vma buckets_vma;
2064
2065
534
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2066
534
         sizeof (nb), NULL);
2067
534
      if (filepos == (file_ptr) -1
2068
508
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2069
480
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2070
63
  goto error_return;
2071
2072
471
      ngnubuckets = bfd_get_32 (abfd, nb);
2073
471
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2074
471
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2075
471
      buckets_vma = dt_gnu_hash + 16;
2076
471
      if (bed->s->elfclass == ELFCLASS32)
2077
0
  buckets_vma += bitmaskwords * 4;
2078
471
      else
2079
471
  buckets_vma += bitmaskwords * 8;
2080
471
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2081
471
      if (filepos == (file_ptr) -1
2082
450
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2083
57
  goto error_return;
2084
2085
414
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2086
414
      if (gnubuckets == NULL)
2087
64
  goto error_return;
2088
2089
329k
      for (i = 0; i < ngnubuckets; i++)
2090
329k
  if (gnubuckets[i] != 0)
2091
264k
    {
2092
264k
      if (gnubuckets[i] < gnusymidx)
2093
29
        goto error_return;
2094
2095
264k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2096
18.3k
        maxchain = gnubuckets[i];
2097
264k
    }
2098
2099
321
      if (maxchain == 0xffffffff)
2100
30
  {
2101
30
    symcount = 0;
2102
30
    goto empty_gnu_hash;
2103
30
  }
2104
2105
291
      maxchain -= gnusymidx;
2106
291
      filepos = offset_from_vma (phdrs, phnum,
2107
291
         buckets_vma + 4 * (ngnubuckets + maxchain),
2108
291
         4, NULL);
2109
291
      if (filepos == (file_ptr) -1
2110
249
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2111
213
  goto error_return;
2112
2113
78
      do
2114
1.15k
  {
2115
1.15k
    if (bfd_read (nb, 4, abfd) != 4)
2116
6
      goto error_return;
2117
1.14k
    ++maxchain;
2118
1.14k
    if (maxchain == 0)
2119
0
      goto error_return;
2120
1.14k
  }
2121
1.14k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2122
2123
72
      filepos = offset_from_vma (phdrs, phnum,
2124
72
         buckets_vma + 4 * ngnubuckets,
2125
72
         4, NULL);
2126
72
      if (filepos == (file_ptr) -1
2127
72
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2128
0
  goto error_return;
2129
2130
72
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2131
72
      if (gnuchains == NULL)
2132
3
  goto error_return;
2133
69
      ngnuchains = maxchain;
2134
2135
69
      if (dt_mips_xhash)
2136
0
  {
2137
0
    filepos = offset_from_vma (phdrs, phnum,
2138
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2139
0
             4, NULL);
2140
0
    if (filepos == (file_ptr) -1
2141
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2142
0
      goto error_return;
2143
2144
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2145
0
    if (mipsxlat == NULL)
2146
0
      goto error_return;
2147
0
  }
2148
2149
69
      symcount = 0;
2150
976
      for (i = 0; i < ngnubuckets; ++i)
2151
907
  if (gnubuckets[i] != 0)
2152
234
    {
2153
234
      bfd_vma si = gnubuckets[i];
2154
234
      bfd_vma off = si - gnusymidx;
2155
234
      do
2156
1.86k
        {
2157
1.86k
    if (mipsxlat)
2158
0
      {
2159
0
        if (mipsxlat[off] >= symcount)
2160
0
          symcount = mipsxlat[off] + 1;
2161
0
      }
2162
1.86k
    else
2163
1.86k
      {
2164
1.86k
        if (si >= symcount)
2165
1.21k
          symcount = si + 1;
2166
1.86k
      }
2167
1.86k
    si++;
2168
1.86k
        }
2169
1.86k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2170
234
    }
2171
69
    }
2172
2173
  /* Swap in dynamic symbol table.  */
2174
395
  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
395
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2181
395
  if (filepos == (file_ptr) -1
2182
350
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2183
90
    goto error_return;
2184
305
  esymbuf_size = amt;
2185
305
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2186
305
         &esymbuf_addr, &esymbuf_size);
2187
305
  if (esymbuf == NULL)
2188
50
    goto error_return;
2189
2190
255
  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
255
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2198
255
  if (isymbuf == NULL)
2199
0
    goto error_return;
2200
2201
255
  swap_symbol_in = bed->s->swap_symbol_in;
2202
2203
  /* Convert the symbols to internal form.  */
2204
255
  isymend = isymbuf + symcount;
2205
255
  for (esym = esymbuf, isym = isymbuf;
2206
860
       isym < isymend;
2207
605
       esym += extsym_size, isym++)
2208
672
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2209
650
  || isym->st_name >= dt_strsz)
2210
67
      {
2211
67
  bfd_set_error (bfd_error_invalid_operation);
2212
67
  goto error_return;
2213
67
      }
2214
2215
188
  if (dt_versym)
2216
141
    {
2217
      /* Swap in DT_VERSYM.  */
2218
141
      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
141
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2225
141
      if (filepos == (file_ptr) -1
2226
124
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2227
39
  goto error_return;
2228
2229
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2230
102
      versym = _bfd_mmap_persistent (abfd, amt);
2231
2232
102
      if (dt_verdef)
2233
46
  {
2234
    /* Read in DT_VERDEF.  */
2235
46
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2236
46
             0, &verdef_size);
2237
46
    if (filepos == (file_ptr) -1
2238
19
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2239
36
      goto error_return;
2240
2241
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2242
10
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2243
10
  }
2244
2245
66
      if (dt_verneed)
2246
42
  {
2247
    /* Read in DT_VERNEED.  */
2248
42
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2249
42
             0, &verneed_size);
2250
42
    if (filepos == (file_ptr) -1
2251
28
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2252
22
      goto error_return;
2253
2254
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2255
20
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2256
20
  }
2257
66
    }
2258
2259
121
 empty_gnu_hash:
2260
121
  elf_tdata (abfd)->dt_strtab = strbuf;
2261
121
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2262
121
  elf_tdata (abfd)->dt_symtab = isymbuf;
2263
121
  elf_tdata (abfd)->dt_symtab_count = symcount;
2264
121
  elf_tdata (abfd)->dt_versym = versym;
2265
121
  elf_tdata (abfd)->dt_verdef = verdef;
2266
121
  elf_tdata (abfd)->dt_verneed = verneed;
2267
121
  elf_tdata (abfd)->dt_verdef_count
2268
121
    = verdef_size / sizeof (Elf_External_Verdef);
2269
121
  elf_tdata (abfd)->dt_verneed_count
2270
121
    = verneed_size / sizeof (Elf_External_Verneed);
2271
2272
121
  res = true;
2273
2274
3.11k
 error_return:
2275
  /* Restore file position for elf_object_p.  */
2276
3.11k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2277
0
    res = false;
2278
3.11k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2279
3.11k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2280
3.11k
  free (gnubuckets);
2281
3.11k
  free (gnuchains);
2282
3.11k
  free (mipsxlat);
2283
3.11k
  return res;
2284
121
}
2285
2286
/* Reconstruct section from dynamic symbol.  */
2287
2288
asection *
2289
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2290
            Elf_Internal_Sym *isym)
2291
1
{
2292
1
  asection *sec;
2293
1
  flagword flags;
2294
2295
1
  if (!elf_use_dt_symtab_p (abfd))
2296
0
    return NULL;
2297
2298
1
  flags = SEC_ALLOC | SEC_LOAD;
2299
1
  switch (ELF_ST_TYPE (isym->st_info))
2300
1
    {
2301
0
    case STT_FUNC:
2302
0
    case STT_GNU_IFUNC:
2303
0
      sec = bfd_get_section_by_name (abfd, ".text");
2304
0
      if (sec == NULL)
2305
0
  sec = bfd_make_section_with_flags (abfd,
2306
0
             ".text",
2307
0
             flags | SEC_CODE);
2308
0
      break;
2309
0
    case STT_COMMON:
2310
0
      sec = bfd_com_section_ptr;
2311
0
      break;
2312
0
    case STT_OBJECT:
2313
0
      sec = bfd_get_section_by_name (abfd, ".data");
2314
0
      if (sec == NULL)
2315
0
  sec = bfd_make_section_with_flags (abfd,
2316
0
             ".data",
2317
0
             flags | SEC_DATA);
2318
0
      break;
2319
0
    case STT_TLS:
2320
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2321
0
      if (sec == NULL)
2322
0
  sec = bfd_make_section_with_flags (abfd,
2323
0
             ".tdata",
2324
0
             (flags
2325
0
              | SEC_DATA
2326
0
              | SEC_THREAD_LOCAL));
2327
0
      break;
2328
1
    default:
2329
1
      sec = bfd_abs_section_ptr;
2330
1
      break;
2331
1
    }
2332
2333
1
  return sec;
2334
1
}
2335
2336
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2337
   and return symbol version for symbol version itself.   */
2338
2339
const char *
2340
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2341
            bool base_p,
2342
            bool *hidden)
2343
47.4k
{
2344
47.4k
  const char *version_string = NULL;
2345
47.4k
  if ((elf_dynversym (abfd) != 0
2346
7.50k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2347
39.9k
      || (elf_tdata (abfd)->dt_versym != NULL
2348
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2349
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2350
7.46k
    {
2351
7.46k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2352
2353
7.46k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2354
7.46k
      vernum &= VERSYM_VERSION;
2355
2356
7.46k
      if (vernum == 0)
2357
2.48k
  version_string = "";
2358
4.97k
      else if (vernum == 1
2359
3.79k
         && (vernum > elf_tdata (abfd)->cverdefs
2360
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2361
0
           == VER_FLG_BASE)))
2362
3.79k
  version_string = base_p ? "Base" : "";
2363
1.18k
      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.18k
      else
2375
1.18k
  {
2376
1.18k
    Elf_Internal_Verneed *t;
2377
2378
1.18k
    version_string = _("<corrupt>");
2379
1.18k
    for (t = elf_tdata (abfd)->verref;
2380
2.37k
         t != NULL;
2381
1.18k
         t = t->vn_nextref)
2382
1.18k
      {
2383
1.18k
        Elf_Internal_Vernaux *a;
2384
2385
2.64k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2386
2.31k
    {
2387
2.31k
      if (a->vna_other == vernum)
2388
865
        {
2389
865
          *hidden = true;
2390
865
          version_string = a->vna_nodename;
2391
865
          break;
2392
865
        }
2393
2.31k
    }
2394
1.18k
      }
2395
1.18k
  }
2396
7.46k
    }
2397
47.4k
  return version_string;
2398
47.4k
}
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
819k
{
2501
819k
  Elf_Internal_Shdr *hdr;
2502
819k
  Elf_Internal_Ehdr *ehdr;
2503
819k
  elf_backend_data *bed;
2504
819k
  const char *name;
2505
819k
  bool ret = true;
2506
2507
819k
  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
819k
  if (elf_tdata (abfd)->being_created[shindex])
2514
25
    {
2515
25
      _bfd_error_handler
2516
25
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2517
25
      return false;
2518
25
    }
2519
819k
  elf_tdata (abfd)->being_created[shindex] = true;
2520
2521
819k
  hdr = elf_elfsections (abfd)[shindex];
2522
819k
  ehdr = elf_elfheader (abfd);
2523
819k
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2524
819k
            hdr->sh_name);
2525
819k
  if (name == NULL)
2526
7.72k
    goto fail;
2527
2528
811k
  bed = get_elf_backend_data (abfd);
2529
811k
  switch (hdr->sh_type)
2530
811k
    {
2531
85.2k
    case SHT_NULL:
2532
      /* Inactive section. Throw it away.  */
2533
85.2k
      goto success;
2534
2535
236k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2536
246k
    case SHT_NOBITS:   /* .bss section.  */
2537
247k
    case SHT_HASH:   /* .hash section.  */
2538
257k
    case SHT_NOTE:   /* .note section.  */
2539
275k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2540
285k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2541
288k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2542
288k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2543
289k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2544
289k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2545
289k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2546
289k
      goto success;
2547
2548
2.62k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2549
2.62k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2550
62
  goto fail;
2551
2552
2.56k
      if (hdr->sh_link > elf_numsections (abfd))
2553
241
  {
2554
    /* PR 10478: Accept Solaris binaries with a sh_link field
2555
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2556
241
    switch (bfd_get_arch (abfd))
2557
241
      {
2558
202
      case bfd_arch_i386:
2559
241
      case bfd_arch_sparc:
2560
241
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2561
41
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2562
241
    break;
2563
        /* Otherwise fall through.  */
2564
0
      default:
2565
0
        goto fail;
2566
241
      }
2567
241
  }
2568
2.31k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2569
0
  goto fail;
2570
2.31k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2571
1.41k
  {
2572
1.41k
    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.41k
    if (elf_dynsymtab (abfd) != 0)
2578
261
      {
2579
261
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2580
261
        hdr->sh_link = dynsymhdr->sh_link;
2581
261
      }
2582
1.15k
    else
2583
1.15k
      {
2584
1.15k
        unsigned int i, num_sec;
2585
2586
1.15k
        num_sec = elf_numsections (abfd);
2587
20.7k
        for (i = 1; i < num_sec; i++)
2588
19.8k
    {
2589
19.8k
      dynsymhdr = elf_elfsections (abfd)[i];
2590
19.8k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2591
201
        {
2592
201
          hdr->sh_link = dynsymhdr->sh_link;
2593
201
          break;
2594
201
        }
2595
19.8k
    }
2596
1.15k
      }
2597
1.41k
  }
2598
2.56k
      goto success;
2599
2600
99.6k
    case SHT_SYMTAB:   /* A symbol table.  */
2601
99.6k
      if (elf_onesymtab (abfd) == shindex)
2602
80.3k
  goto success;
2603
2604
19.3k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2605
613
  goto fail;
2606
2607
18.6k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2608
1.23k
  {
2609
1.23k
    if (hdr->sh_size != 0)
2610
151
      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.08k
    hdr->sh_info = 0;
2615
1.08k
    goto success;
2616
1.23k
  }
2617
2618
      /* PR 18854: A binary might contain more than one symbol table.
2619
   Unusual, but possible.  Warn, but continue.  */
2620
17.4k
      if (elf_onesymtab (abfd) != 0)
2621
2.90k
  {
2622
2.90k
    _bfd_error_handler
2623
      /* xgettext:c-format */
2624
2.90k
      (_("%pB: warning: multiple symbol tables detected"
2625
2.90k
         " - ignoring the table in section %u"),
2626
2.90k
       abfd, shindex);
2627
2.90k
    goto success;
2628
2.90k
  }
2629
14.5k
      elf_onesymtab (abfd) = shindex;
2630
14.5k
      elf_symtab_hdr (abfd) = *hdr;
2631
14.5k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2632
14.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
14.5k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2641
757
    && (abfd->flags & DYNAMIC) != 0
2642
206
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2643
206
            shindex))
2644
10
  goto fail;
2645
2646
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2647
   can't read symbols without that section loaded as well.  It
2648
   is most likely specified by the next section header.  */
2649
14.5k
      {
2650
14.5k
  elf_section_list * entry;
2651
14.5k
  unsigned int i, num_sec;
2652
2653
16.3k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2654
3.28k
    if (entry->hdr.sh_link == shindex)
2655
1.44k
      goto success;
2656
2657
13.0k
  num_sec = elf_numsections (abfd);
2658
41.0k
  for (i = shindex + 1; i < num_sec; i++)
2659
28.0k
    {
2660
28.0k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2661
2662
28.0k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2663
461
    && hdr2->sh_link == shindex)
2664
64
        break;
2665
28.0k
    }
2666
2667
13.0k
  if (i == num_sec)
2668
327k
    for (i = 1; i < shindex; i++)
2669
314k
      {
2670
314k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2671
2672
314k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2673
2.85k
      && hdr2->sh_link == shindex)
2674
361
    break;
2675
314k
      }
2676
2677
13.0k
  if (i != shindex)
2678
425
    ret = bfd_section_from_shdr (abfd, i);
2679
  /* else FIXME: we have failed to find the symbol table.
2680
     Should we issue an error?  */
2681
13.0k
  goto success;
2682
14.5k
      }
2683
2684
3.61k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2685
3.61k
      if (elf_dynsymtab (abfd) == shindex)
2686
1.48k
  goto success;
2687
2688
2.13k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2689
321
  goto fail;
2690
2691
1.81k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2692
263
  {
2693
263
    if (hdr->sh_size != 0)
2694
107
      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
156
    hdr->sh_info = 0;
2700
156
    goto success;
2701
263
  }
2702
2703
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2704
   Unusual, but possible.  Warn, but continue.  */
2705
1.54k
      if (elf_dynsymtab (abfd) != 0)
2706
183
  {
2707
183
    _bfd_error_handler
2708
      /* xgettext:c-format */
2709
183
      (_("%pB: warning: multiple dynamic symbol tables detected"
2710
183
         " - ignoring the table in section %u"),
2711
183
       abfd, shindex);
2712
183
    goto success;
2713
183
  }
2714
1.36k
      elf_dynsymtab (abfd) = shindex;
2715
1.36k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2716
1.36k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2717
1.36k
      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.36k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2722
1.36k
      goto success;
2723
2724
7.70k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2725
7.70k
      {
2726
7.70k
  elf_section_list * entry;
2727
2728
9.82k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2729
2.50k
    if (entry->ndx == shindex)
2730
387
      goto success;
2731
2732
7.32k
  entry = bfd_alloc (abfd, sizeof (*entry));
2733
7.32k
  if (entry == NULL)
2734
0
    goto fail;
2735
7.32k
  entry->ndx = shindex;
2736
7.32k
  entry->hdr = * hdr;
2737
7.32k
  entry->next = elf_symtab_shndx_list (abfd);
2738
7.32k
  elf_symtab_shndx_list (abfd) = entry;
2739
7.32k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2740
7.32k
  goto success;
2741
7.32k
      }
2742
2743
52.4k
    case SHT_STRTAB:   /* A string table.  */
2744
52.4k
      if (hdr->bfd_section != NULL)
2745
400
  goto success;
2746
2747
52.0k
      if (ehdr->e_shstrndx == shindex)
2748
45.0k
  {
2749
45.0k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2750
45.0k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2751
45.0k
    goto success;
2752
45.0k
  }
2753
2754
7.03k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2755
1.80k
  {
2756
2.03k
  symtab_strtab:
2757
2.03k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2758
2.03k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2759
2.03k
    goto success;
2760
1.80k
  }
2761
2762
5.22k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2763
836
  {
2764
919
  dynsymtab_strtab:
2765
919
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2766
919
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2767
919
    elf_elfsections (abfd)[shindex] = hdr;
2768
    /* We also treat this as a regular section, so that objcopy
2769
       can handle it.  */
2770
919
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2771
919
             shindex);
2772
919
    goto success;
2773
836
  }
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.38k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2779
4.21k
  {
2780
4.21k
    unsigned int i, num_sec;
2781
2782
4.21k
    num_sec = elf_numsections (abfd);
2783
81.8k
    for (i = 1; i < num_sec; i++)
2784
78.1k
      {
2785
78.1k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2786
78.1k
        if (hdr2->sh_link == shindex)
2787
3.13k
    {
2788
      /* Prevent endless recursion on broken objects.  */
2789
3.13k
      if (i == shindex)
2790
61
        goto fail;
2791
3.06k
      if (! bfd_section_from_shdr (abfd, i))
2792
130
        goto fail;
2793
2.93k
      if (elf_onesymtab (abfd) == i)
2794
229
        goto symtab_strtab;
2795
2.71k
      if (elf_dynsymtab (abfd) == i)
2796
83
        goto dynsymtab_strtab;
2797
2.71k
    }
2798
78.1k
      }
2799
4.21k
  }
2800
3.88k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2801
3.88k
      goto success;
2802
2803
14.7k
    case SHT_REL:
2804
104k
    case SHT_RELA:
2805
105k
    case SHT_RELR:
2806
      /* *These* do a lot of work -- but build no sections!  */
2807
105k
      {
2808
105k
  asection *target_sect;
2809
105k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2810
105k
  unsigned int num_sec = elf_numsections (abfd);
2811
105k
  struct bfd_elf_section_data *esdt;
2812
105k
  bfd_size_type size;
2813
2814
105k
  if (hdr->sh_type == SHT_REL)
2815
14.7k
    size = bed->s->sizeof_rel;
2816
90.3k
  else if (hdr->sh_type == SHT_RELA)
2817
90.0k
    size = bed->s->sizeof_rela;
2818
332
  else
2819
332
    size = bed->s->arch_size / 8;
2820
105k
  if (hdr->sh_entsize != size)
2821
1.05k
    goto fail;
2822
2823
  /* Check for a bogus link to avoid crashing.  */
2824
104k
  if (hdr->sh_link >= num_sec)
2825
121
    {
2826
121
      _bfd_error_handler
2827
        /* xgettext:c-format */
2828
121
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2829
121
         abfd, hdr->sh_link, name, shindex);
2830
121
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2831
121
      goto success;
2832
121
    }
2833
2834
  /* Get the symbol table.  */
2835
103k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2836
26.1k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2837
79.3k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2838
482
    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
103k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2849
6.48k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2850
101k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2851
98.8k
      || hdr->sh_type == SHT_RELR
2852
98.7k
      || hdr->sh_link == SHN_UNDEF
2853
96.0k
      || hdr->sh_link != elf_onesymtab (abfd)
2854
73.8k
      || hdr->sh_info == SHN_UNDEF
2855
73.3k
      || hdr->sh_info >= num_sec
2856
73.3k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2857
73.0k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2858
30.7k
    {
2859
30.7k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2860
30.7k
      goto success;
2861
30.7k
    }
2862
2863
72.6k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2864
112
    goto fail;
2865
2866
72.5k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2867
72.5k
  if (target_sect == NULL)
2868
130
    goto fail;
2869
2870
72.4k
  esdt = elf_section_data (target_sect);
2871
72.4k
  if (hdr->sh_type == SHT_RELA)
2872
63.6k
    p_hdr = &esdt->rela.hdr;
2873
8.75k
  else
2874
8.75k
    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
72.4k
  if (*p_hdr != NULL)
2880
1.23k
    {
2881
1.23k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2882
151
        {
2883
151
    _bfd_error_handler
2884
      /* xgettext:c-format */
2885
151
      (_("%pB: warning: secondary relocation section '%s' "
2886
151
         "for section %pA found - ignoring"),
2887
151
       abfd, name, target_sect);
2888
151
        }
2889
1.08k
      else
2890
1.08k
        esdt->has_secondary_relocs = true;
2891
1.23k
      goto success;
2892
1.23k
    }
2893
2894
71.1k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2895
71.1k
  if (hdr2 == NULL)
2896
0
    goto fail;
2897
71.1k
  *hdr2 = *hdr;
2898
71.1k
  *p_hdr = hdr2;
2899
71.1k
  elf_elfsections (abfd)[shindex] = hdr2;
2900
71.1k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2901
71.1k
             * bed->s->int_rels_per_ext_rel);
2902
71.1k
  target_sect->flags |= SEC_RELOC;
2903
71.1k
  target_sect->relocation = NULL;
2904
71.1k
  target_sect->rel_filepos = hdr->sh_offset;
2905
  /* In the section to which the relocations apply, mark whether
2906
     its relocations are of the REL or RELA variety.  */
2907
71.1k
  if (hdr->sh_size != 0)
2908
70.5k
    {
2909
70.5k
      if (hdr->sh_type == SHT_RELA)
2910
62.0k
        target_sect->use_rela_p = 1;
2911
70.5k
    }
2912
71.1k
  abfd->flags |= HAS_RELOC;
2913
71.1k
  goto success;
2914
71.1k
      }
2915
2916
290
    case SHT_GNU_verdef:
2917
290
      if (hdr->sh_info != 0)
2918
139
  elf_dynverdef (abfd) = shindex;
2919
290
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2920
290
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2921
290
      goto success;
2922
2923
778
    case SHT_GNU_versym:
2924
778
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2925
85
  goto fail;
2926
2927
693
      elf_dynversym (abfd) = shindex;
2928
693
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2929
693
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2930
693
      goto success;
2931
2932
826
    case SHT_GNU_verneed:
2933
826
      if (hdr->sh_info != 0)
2934
706
  elf_dynverref (abfd) = shindex;
2935
826
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2936
826
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2937
826
      goto success;
2938
2939
1.19k
    case SHT_SHLIB:
2940
1.19k
      goto success;
2941
2942
18.5k
    case SHT_GROUP:
2943
18.5k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2944
35
  goto fail;
2945
2946
18.5k
      goto success;
2947
2948
144k
    default:
2949
      /* Possibly an attributes section.  */
2950
144k
      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
139k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2953
134k
        || hdr->sh_type == bed->obj_attrs_section_type))
2954
4.81k
  {
2955
4.81k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2956
28
      goto fail;
2957
4.78k
    _bfd_elf_parse_attributes (abfd, hdr);
2958
4.78k
    goto success;
2959
4.81k
  }
2960
2961
      /* Check for any processor-specific section types.  */
2962
139k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2963
123k
  goto success;
2964
2965
15.7k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2966
6.50k
  {
2967
6.50k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2968
      /* FIXME: How to properly handle allocated section reserved
2969
         for applications?  */
2970
457
      _bfd_error_handler
2971
        /* xgettext:c-format */
2972
457
        (_("%pB: unknown type [%#x] section `%s'"),
2973
457
         abfd, hdr->sh_type, name);
2974
6.05k
    else
2975
6.05k
      {
2976
        /* Allow sections reserved for applications.  */
2977
6.05k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2978
6.05k
        goto success;
2979
6.05k
      }
2980
6.50k
  }
2981
9.25k
      else if (hdr->sh_type >= SHT_LOPROC
2982
1.12k
         && hdr->sh_type <= SHT_HIPROC)
2983
  /* FIXME: We should handle this section.  */
2984
1.12k
  _bfd_error_handler
2985
    /* xgettext:c-format */
2986
1.12k
    (_("%pB: unknown type [%#x] section `%s'"),
2987
1.12k
     abfd, hdr->sh_type, name);
2988
8.13k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2989
4.70k
  {
2990
    /* Unrecognised OS-specific sections.  */
2991
4.70k
    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
243
      _bfd_error_handler
2996
        /* xgettext:c-format */
2997
243
        (_("%pB: unknown type [%#x] section `%s'"),
2998
243
         abfd, hdr->sh_type, name);
2999
4.46k
    else
3000
4.46k
      {
3001
        /* Otherwise it should be processed.  */
3002
4.46k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3003
4.46k
        goto success;
3004
4.46k
      }
3005
4.70k
  }
3006
3.42k
      else
3007
  /* FIXME: We should handle this section.  */
3008
3.42k
  _bfd_error_handler
3009
    /* xgettext:c-format */
3010
3.42k
    (_("%pB: unknown type [%#x] section `%s'"),
3011
3.42k
     abfd, hdr->sh_type, name);
3012
3013
5.24k
      goto fail;
3014
811k
    }
3015
3016
16.3k
 fail:
3017
16.3k
  ret = false;
3018
819k
 success:
3019
819k
  elf_tdata (abfd)->being_created[shindex] = false;
3020
819k
  return ret;
3021
16.3k
}
3022
3023
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3024
3025
Elf_Internal_Sym *
3026
bfd_sym_from_r_symndx (struct sym_cache *cache,
3027
           bfd *abfd,
3028
           unsigned long r_symndx)
3029
0
{
3030
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3031
3032
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3033
0
    {
3034
0
      Elf_Internal_Shdr *symtab_hdr;
3035
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3036
0
      Elf_External_Sym_Shndx eshndx;
3037
3038
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3039
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3040
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3041
0
  return NULL;
3042
3043
0
      if (cache->abfd != abfd)
3044
0
  {
3045
0
    memset (cache->indx, -1, sizeof (cache->indx));
3046
0
    cache->abfd = abfd;
3047
0
  }
3048
0
      cache->indx[ent] = r_symndx;
3049
0
    }
3050
3051
0
  return &cache->sym[ent];
3052
0
}
3053
3054
/* Given an ELF section number, retrieve the corresponding BFD
3055
   section.  */
3056
3057
asection *
3058
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3059
144k
{
3060
144k
  if (sec_index >= elf_numsections (abfd))
3061
16.0k
    return NULL;
3062
128k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3063
144k
}
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
773k
{
3235
773k
  int i;
3236
773k
  int len;
3237
3238
773k
  len = strlen (name);
3239
3240
4.35M
  for (i = 0; spec[i].prefix != NULL; i++)
3241
3.71M
    {
3242
3.71M
      int suffix_len;
3243
3.71M
      int prefix_len = spec[i].prefix_length;
3244
3245
3.71M
      if (len < prefix_len)
3246
1.83M
  continue;
3247
1.88M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3248
1.69M
  continue;
3249
3250
192k
      suffix_len = spec[i].suffix_length;
3251
192k
      if (suffix_len <= 0)
3252
191k
  {
3253
191k
    if (name[prefix_len] != 0)
3254
116k
      {
3255
116k
        if (suffix_len == 0)
3256
48.7k
    continue;
3257
67.2k
        if (name[prefix_len] != '.'
3258
9.96k
      && (suffix_len == -2
3259
3.45k
          || (rela && spec[i].type == SHT_REL)))
3260
7.40k
    continue;
3261
67.2k
      }
3262
191k
  }
3263
1.29k
      else
3264
1.29k
  {
3265
1.29k
    if (len < prefix_len + suffix_len)
3266
594
      continue;
3267
703
    if (memcmp (name + len - suffix_len,
3268
703
          spec[i].prefix + prefix_len,
3269
703
          suffix_len) != 0)
3270
406
      continue;
3271
703
  }
3272
135k
      return &spec[i];
3273
192k
    }
3274
3275
638k
  return NULL;
3276
773k
}
3277
3278
const struct bfd_elf_special_section *
3279
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3280
1.17M
{
3281
1.17M
  int i;
3282
1.17M
  const struct bfd_elf_special_section *spec;
3283
1.17M
  elf_backend_data *bed;
3284
3285
  /* See if this is one of the special sections.  */
3286
1.17M
  if (sec->name == NULL)
3287
0
    return NULL;
3288
3289
1.17M
  bed = get_elf_backend_data (abfd);
3290
1.17M
  spec = bed->special_sections;
3291
1.17M
  if (spec)
3292
570k
    {
3293
570k
      spec = _bfd_elf_get_special_section (sec->name,
3294
570k
             bed->special_sections,
3295
570k
             sec->use_rela_p);
3296
570k
      if (spec != NULL)
3297
2.36k
  return spec;
3298
570k
    }
3299
3300
1.17M
  if (sec->name[0] != '.')
3301
968k
    return NULL;
3302
3303
203k
  i = sec->name[1] - 'b';
3304
203k
  if (i < 0 || i > 'z' - 'b')
3305
7.10k
    return NULL;
3306
3307
196k
  spec = special_sections[i];
3308
3309
196k
  if (spec == NULL)
3310
10.9k
    return NULL;
3311
3312
185k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3313
196k
}
3314
3315
bool
3316
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3317
1.17M
{
3318
1.17M
  struct bfd_elf_section_data *sdata;
3319
1.17M
  elf_backend_data *bed;
3320
1.17M
  const struct bfd_elf_special_section *ssect;
3321
3322
1.17M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3323
1.17M
  if (sdata == NULL)
3324
841k
    {
3325
841k
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3326
841k
                sizeof (*sdata));
3327
841k
      if (sdata == NULL)
3328
0
  return false;
3329
841k
      sec->used_by_bfd = sdata;
3330
841k
    }
3331
3332
  /* Indicate whether or not this section should use RELA relocations.  */
3333
1.17M
  bed = get_elf_backend_data (abfd);
3334
1.17M
  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.17M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3339
1.17M
  if (ssect != NULL)
3340
135k
    {
3341
135k
      elf_section_type (sec) = ssect->type;
3342
135k
      elf_section_flags (sec) = ssect->attr;
3343
135k
    }
3344
3345
1.17M
  return _bfd_generic_new_section_hook (abfd, sec);
3346
1.17M
}
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
611k
{
3374
611k
  asection *newsect;
3375
611k
  char *name;
3376
611k
  char namebuf[64];
3377
611k
  size_t len;
3378
611k
  int split;
3379
611k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3380
3381
611k
  split = ((hdr->p_memsz > 0)
3382
468k
      && (hdr->p_filesz > 0)
3383
419k
      && (hdr->p_memsz > hdr->p_filesz));
3384
3385
611k
  if (hdr->p_filesz > 0)
3386
516k
    {
3387
516k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3388
516k
      len = strlen (namebuf) + 1;
3389
516k
      name = (char *) bfd_alloc (abfd, len);
3390
516k
      if (!name)
3391
0
  return false;
3392
516k
      memcpy (name, namebuf, len);
3393
516k
      newsect = bfd_make_section (abfd, name);
3394
516k
      if (newsect == NULL)
3395
0
  return false;
3396
516k
      newsect->vma = hdr->p_vaddr / opb;
3397
516k
      newsect->lma = hdr->p_paddr / opb;
3398
516k
      newsect->size = hdr->p_filesz;
3399
516k
      newsect->filepos = hdr->p_offset;
3400
516k
      newsect->flags |= SEC_HAS_CONTENTS;
3401
516k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3402
516k
      if (hdr->p_type == PT_LOAD)
3403
8.42k
  {
3404
8.42k
    newsect->flags |= SEC_ALLOC;
3405
8.42k
    newsect->flags |= SEC_LOAD;
3406
8.42k
    if (hdr->p_flags & PF_X)
3407
3.61k
      {
3408
        /* FIXME: all we known is that it has execute PERMISSION,
3409
     may be data.  */
3410
3.61k
        newsect->flags |= SEC_CODE;
3411
3.61k
      }
3412
8.42k
  }
3413
516k
      if (!(hdr->p_flags & PF_W))
3414
376k
  {
3415
376k
    newsect->flags |= SEC_READONLY;
3416
376k
  }
3417
516k
    }
3418
3419
611k
  if (hdr->p_memsz > hdr->p_filesz)
3420
234k
    {
3421
234k
      bfd_vma align;
3422
3423
234k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3424
234k
      len = strlen (namebuf) + 1;
3425
234k
      name = (char *) bfd_alloc (abfd, len);
3426
234k
      if (!name)
3427
0
  return false;
3428
234k
      memcpy (name, namebuf, len);
3429
234k
      newsect = bfd_make_section (abfd, name);
3430
234k
      if (newsect == NULL)
3431
0
  return false;
3432
234k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3433
234k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3434
234k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3435
234k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3436
234k
      align = newsect->vma & -newsect->vma;
3437
234k
      if (align == 0 || align > hdr->p_align)
3438
52.0k
  align = hdr->p_align;
3439
234k
      newsect->alignment_power = bfd_log2 (align);
3440
234k
      if (hdr->p_type == PT_LOAD)
3441
6.58k
  {
3442
6.58k
    newsect->flags |= SEC_ALLOC;
3443
6.58k
    if (hdr->p_flags & PF_X)
3444
2.18k
      newsect->flags |= SEC_CODE;
3445
6.58k
  }
3446
234k
      if (!(hdr->p_flags & PF_W))
3447
175k
  newsect->flags |= SEC_READONLY;
3448
234k
    }
3449
3450
611k
  return true;
3451
611k
}
3452
3453
static bool
3454
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3455
10.9k
{
3456
  /* The return value is ignored.  Build-ids are considered optional.  */
3457
10.9k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3458
10.9k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3459
10.9k
      (templ, offset);
3460
0
  return false;
3461
10.9k
}
3462
3463
bool
3464
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3465
611k
{
3466
611k
  elf_backend_data *bed;
3467
3468
611k
  switch (hdr->p_type)
3469
611k
    {
3470
121k
    case PT_NULL:
3471
121k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3472
3473
10.9k
    case PT_LOAD:
3474
10.9k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3475
0
  return false;
3476
10.9k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3477
10.9k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3478
10.9k
      return true;
3479
3480
2.95k
    case PT_DYNAMIC:
3481
2.95k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3482
3483
1.71k
    case PT_INTERP:
3484
1.71k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3485
3486
1.80k
    case PT_NOTE:
3487
1.80k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3488
0
  return false;
3489
1.80k
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3490
1.80k
         hdr->p_align))
3491
1.22k
  return false;
3492
579
      return true;
3493
3494
596
    case PT_SHLIB:
3495
596
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3496
3497
8.04k
    case PT_PHDR:
3498
8.04k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3499
3500
111
    case PT_GNU_EH_FRAME:
3501
111
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3502
111
                "eh_frame_hdr");
3503
3504
69
    case PT_GNU_STACK:
3505
69
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3506
3507
207
    case PT_GNU_RELRO:
3508
207
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3509
3510
7
    case PT_GNU_SFRAME:
3511
7
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3512
7
                "sframe");
3513
3514
462k
    default:
3515
      /* Check for any processor-specific program segment types.  */
3516
462k
      bed = get_elf_backend_data (abfd);
3517
462k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3518
611k
    }
3519
611k
}
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
22
{
3527
22
  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
22
  else
3533
22
    return elf_section_data (sec)->rela.hdr;
3534
22
}
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
173
{
3542
173
  char *name = (char *) bfd_alloc (abfd,
3543
173
           sizeof ".rela" + strlen (sec_name));
3544
173
  if (name == NULL)
3545
0
    return false;
3546
3547
173
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3548
173
  rel_hdr->sh_name =
3549
173
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3550
173
          false);
3551
173
  if (rel_hdr->sh_name == (unsigned int) -1)
3552
0
    return false;
3553
3554
173
  return true;
3555
173
}
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
173
{
3569
173
  Elf_Internal_Shdr *rel_hdr;
3570
173
  elf_backend_data *bed = get_elf_backend_data (abfd);
3571
3572
173
  BFD_ASSERT (reldata->hdr == NULL);
3573
173
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3574
173
  if (rel_hdr == NULL)
3575
0
    return false;
3576
173
  reldata->hdr = rel_hdr;
3577
3578
173
  if (delay_sh_name_p)
3579
0
    rel_hdr->sh_name = (unsigned int) -1;
3580
173
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3581
173
          use_rela_p))
3582
0
    return false;
3583
173
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3584
173
  rel_hdr->sh_entsize = (use_rela_p
3585
173
       ? bed->s->sizeof_rela
3586
173
       : bed->s->sizeof_rel);
3587
173
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3588
173
  rel_hdr->sh_flags = 0;
3589
173
  rel_hdr->sh_addr = 0;
3590
173
  rel_hdr->sh_size = 0;
3591
173
  rel_hdr->sh_offset = 0;
3592
3593
173
  return true;
3594
173
}
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.39k
{
3601
1.39k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3602
823
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3603
55
    return SHT_NOBITS;
3604
1.33k
  return SHT_PROGBITS;
3605
1.39k
}
3606
3607
struct fake_section_arg
3608
{
3609
  struct bfd_link_info *link_info;
3610
  bool failed;
3611
};
3612
3613
/* Set up an ELF internal section header for a section.  */
3614
3615
static void
3616
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3617
1.38k
{
3618
1.38k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3619
1.38k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3620
1.38k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3621
1.38k
  Elf_Internal_Shdr *this_hdr;
3622
1.38k
  unsigned int sh_type;
3623
1.38k
  const char *name = asect->name;
3624
1.38k
  bool delay_sh_name_p = false;
3625
1.38k
  bfd_vma mask;
3626
3627
1.38k
  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.38k
  this_hdr = &esd->this_hdr;
3635
3636
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3637
1.38k
  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.38k
  if (delay_sh_name_p)
3652
0
    this_hdr->sh_name = (unsigned int) -1;
3653
1.38k
  else
3654
1.38k
    {
3655
1.38k
      this_hdr->sh_name
3656
1.38k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3657
1.38k
                name, false);
3658
1.38k
      if (this_hdr->sh_name == (unsigned int) -1)
3659
0
  {
3660
0
    arg->failed = true;
3661
0
    return;
3662
0
  }
3663
1.38k
    }
3664
3665
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3666
3667
1.38k
  if ((asect->flags & SEC_ALLOC) != 0
3668
566
      || asect->user_set_vma)
3669
1.38k
    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.38k
  this_hdr->sh_offset = 0;
3674
1.38k
  this_hdr->sh_size = asect->size;
3675
1.38k
  this_hdr->sh_link = 0;
3676
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3677
1.38k
  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.38k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3689
1.38k
  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.38k
  this_hdr->bfd_section = asect;
3694
1.38k
  this_hdr->contents = NULL;
3695
3696
  /* If the section type is unspecified, we set it based on
3697
     asect->flags.  */
3698
1.38k
  if (asect->type != 0)
3699
0
    sh_type = asect->type;
3700
1.38k
  else if ((asect->flags & SEC_GROUP) != 0)
3701
11
    sh_type = SHT_GROUP;
3702
1.37k
  else
3703
1.37k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3704
3705
1.38k
  if (this_hdr->sh_type == SHT_NULL)
3706
344
    this_hdr->sh_type = sh_type;
3707
1.04k
  else if (this_hdr->sh_type == SHT_NOBITS
3708
30
     && sh_type == SHT_PROGBITS
3709
0
     && (asect->flags & SEC_ALLOC) != 0)
3710
0
    {
3711
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3712
   allow the link to proceed.  This can happen when users link
3713
   non-bss input sections to bss output sections, or emit data
3714
   to a bss output section via a linker script.  */
3715
0
      _bfd_error_handler
3716
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3717
0
      this_hdr->sh_type = sh_type;
3718
0
    }
3719
3720
1.38k
  switch (this_hdr->sh_type)
3721
1.38k
    {
3722
78
    default:
3723
78
      break;
3724
3725
78
    case SHT_STRTAB:
3726
88
    case SHT_NOTE:
3727
143
    case SHT_NOBITS:
3728
1.09k
    case SHT_PROGBITS:
3729
1.09k
      break;
3730
3731
31
    case SHT_INIT_ARRAY:
3732
57
    case SHT_FINI_ARRAY:
3733
59
    case SHT_PREINIT_ARRAY:
3734
59
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3735
59
      break;
3736
3737
22
    case SHT_HASH:
3738
22
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3739
22
      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
60
    case SHT_RELA:
3750
60
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3751
60
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3752
60
      break;
3753
3754
0
     case SHT_REL:
3755
0
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3756
0
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3757
0
      break;
3758
3759
4
     case SHT_GNU_versym:
3760
4
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3761
4
      break;
3762
3763
0
     case SHT_GNU_verdef:
3764
0
      this_hdr->sh_entsize = 0;
3765
      /* objcopy or strip will copy over sh_info, but may not set
3766
   cverdefs.  The linker will set cverdefs, but sh_info will be
3767
   zero.  */
3768
0
      if (this_hdr->sh_info == 0)
3769
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3770
0
      else
3771
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3772
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3773
0
      break;
3774
3775
4
    case SHT_GNU_verneed:
3776
4
      this_hdr->sh_entsize = 0;
3777
      /* objcopy or strip will copy over sh_info, but may not set
3778
   cverrefs.  The linker will set cverrefs, but sh_info will be
3779
   zero.  */
3780
4
      if (this_hdr->sh_info == 0)
3781
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3782
4
      else
3783
4
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3784
4
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3785
4
      break;
3786
3787
11
    case SHT_GROUP:
3788
11
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3789
11
      break;
3790
3791
10
    case SHT_GNU_HASH:
3792
10
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3793
10
      break;
3794
1.38k
    }
3795
3796
1.38k
  if ((asect->flags & SEC_ALLOC) != 0)
3797
819
    this_hdr->sh_flags |= SHF_ALLOC;
3798
1.38k
  if ((asect->flags & SEC_READONLY) == 0)
3799
372
    this_hdr->sh_flags |= SHF_WRITE;
3800
1.38k
  if ((asect->flags & SEC_CODE) != 0)
3801
223
    this_hdr->sh_flags |= SHF_EXECINSTR;
3802
1.38k
  if ((asect->flags & SEC_MERGE) != 0)
3803
111
    {
3804
111
      this_hdr->sh_flags |= SHF_MERGE;
3805
111
      this_hdr->sh_entsize = asect->entsize;
3806
111
    }
3807
1.38k
  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.38k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3813
28
    this_hdr->sh_flags |= SHF_GROUP;
3814
1.38k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3815
17
    {
3816
17
      this_hdr->sh_flags |= SHF_TLS;
3817
17
      if (asect->size == 0
3818
6
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3819
0
  {
3820
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3821
3822
0
    this_hdr->sh_size = 0;
3823
0
    if (o != NULL)
3824
0
      {
3825
0
        this_hdr->sh_size = o->offset + o->size;
3826
0
        if (this_hdr->sh_size != 0)
3827
0
    this_hdr->sh_type = SHT_NOBITS;
3828
0
      }
3829
0
  }
3830
17
    }
3831
1.38k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3832
39
    this_hdr->sh_flags |= SHF_EXCLUDE;
3833
3834
1.38k
  if (this_hdr->sh_entsize == 0)
3835
1.02k
    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.38k
  if ((asect->flags & SEC_RELOC) != 0)
3842
173
    {
3843
      /* When doing a relocatable link, create both REL and RELA sections if
3844
   needed.  */
3845
173
      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
173
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3867
173
            (asect->use_rela_p
3868
173
             ? &esd->rela : &esd->rel),
3869
173
            name,
3870
173
            asect->use_rela_p,
3871
173
            delay_sh_name_p))
3872
0
  {
3873
0
    arg->failed = true;
3874
0
    return;
3875
0
  }
3876
173
    }
3877
3878
  /* Check for processor-specific section types.  */
3879
1.38k
  sh_type = this_hdr->sh_type;
3880
1.38k
  if (bed->elf_backend_fake_sections
3881
1.16k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3882
0
    {
3883
0
      arg->failed = true;
3884
0
      return;
3885
0
    }
3886
3887
1.38k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3888
50
    {
3889
      /* Don't change the header type from NOBITS if we are being
3890
   called for objcopy --only-keep-debug.  */
3891
50
      this_hdr->sh_type = sh_type;
3892
50
    }
3893
1.38k
}
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.38k
{
3903
1.38k
  bool *failedptr = (bool *) failedptrarg;
3904
1.38k
  asection *elt, *first;
3905
1.38k
  unsigned char *loc;
3906
1.38k
  bool gas;
3907
3908
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3909
     elfxx-ia64.c.  */
3910
1.38k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3911
11
      || sec->size == 0
3912
11
      || *failedptr)
3913
1.37k
    return;
3914
3915
11
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3916
11
    {
3917
11
      unsigned long symindx = 0;
3918
3919
      /* elf_group_id will have been set up by objcopy and the
3920
   generic linker.  */
3921
11
      if (elf_group_id (sec) != NULL)
3922
11
  symindx = elf_group_id (sec)->udata.i;
3923
3924
11
      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
11
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3938
11
    }
3939
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3940
0
    {
3941
      /* The ELF backend linker sets sh_info to -2 when the group
3942
   signature symbol is global, and thus the index can't be
3943
   set until all local symbols are output.  */
3944
0
      asection *igroup;
3945
0
      struct bfd_elf_section_data *sec_data;
3946
0
      unsigned long symndx;
3947
0
      unsigned long extsymoff;
3948
0
      struct elf_link_hash_entry *h;
3949
3950
      /* The point of this little dance to the first SHF_GROUP section
3951
   then back to the SHT_GROUP section is that this gets us to
3952
   the SHT_GROUP in the input object.  */
3953
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3954
0
      sec_data = elf_section_data (igroup);
3955
0
      symndx = sec_data->this_hdr.sh_info;
3956
0
      extsymoff = 0;
3957
0
      if (!elf_bad_symtab (igroup->owner))
3958
0
  {
3959
0
    Elf_Internal_Shdr *symtab_hdr;
3960
3961
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3962
0
    extsymoff = symtab_hdr->sh_info;
3963
0
  }
3964
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3965
0
      while (h->root.type == bfd_link_hash_indirect
3966
0
       || h->root.type == bfd_link_hash_warning)
3967
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3968
3969
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3970
0
    }
3971
3972
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3973
11
  gas = true;
3974
11
  if (sec->contents == NULL)
3975
11
    {
3976
11
      gas = false;
3977
11
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3978
3979
      /* Arrange for the section to be written out.  */
3980
11
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3981
11
      if (sec->contents == NULL)
3982
0
  {
3983
0
    *failedptr = true;
3984
0
    return;
3985
0
  }
3986
11
      sec->alloced = 1;
3987
11
    }
3988
3989
11
  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
11
  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
28
  while (elt != NULL)
4001
28
    {
4002
28
      asection *s;
4003
4004
28
      s = elt;
4005
28
      if (!gas)
4006
28
  s = s->output_section;
4007
28
      if (s != NULL
4008
28
    && !bfd_is_abs_section (s))
4009
28
  {
4010
28
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4011
28
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4012
4013
28
    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
28
    if (elf_sec->rela.hdr != NULL
4025
22
        && (gas
4026
22
      || (input_elf_sec->rela.hdr != NULL
4027
22
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4028
22
      {
4029
22
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4030
22
        loc -= 4;
4031
22
        if (loc == sec->contents)
4032
0
    break;
4033
22
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4034
22
      }
4035
28
    loc -= 4;
4036
28
    if (loc == sec->contents)
4037
0
      break;
4038
28
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4039
28
  }
4040
28
      elt = elf_next_in_group (elt);
4041
28
      if (elt == first)
4042
11
  break;
4043
28
    }
4044
4045
  /* We should always get here with loc == sec->contents + 4.  Return
4046
     an error for bogus SHT_GROUP sections.  */
4047
11
  loc -= 4;
4048
11
  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
11
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4059
11
}
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
42
{
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
42
  if (get_elf_backend_data (abfd)->want_got_plt
4071
36
      && streq (name, ".plt"))
4072
15
    {
4073
15
      asection *sec;
4074
4075
15
      name = ".got.plt";
4076
15
      sec = bfd_get_section_by_name (abfd, name);
4077
15
      if (sec != NULL)
4078
15
  return sec;
4079
0
      name = ".got";
4080
0
    }
4081
4082
27
  return bfd_get_section_by_name (abfd, name);
4083
42
}
4084
4085
/* Return the section to which RELOC_SEC applies.  */
4086
4087
static asection *
4088
elf_get_reloc_section (asection *reloc_sec)
4089
60
{
4090
60
  const char *name;
4091
60
  unsigned int type;
4092
60
  bfd *abfd;
4093
60
  elf_backend_data *bed;
4094
4095
60
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4096
60
  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
60
  name = reloc_sec->name;
4101
60
  if (!startswith (name, ".rel"))
4102
17
    return NULL;
4103
43
  name += 4;
4104
43
  if (type == SHT_RELA && *name++ != 'a')
4105
1
    return NULL;
4106
4107
42
  abfd = reloc_sec->owner;
4108
42
  bed = get_elf_backend_data (abfd);
4109
42
  return bed->get_reloc_section (abfd, name);
4110
43
}
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
329
{
4120
329
  struct elf_obj_tdata *t = elf_tdata (abfd);
4121
329
  asection *sec;
4122
329
  unsigned int section_number;
4123
329
  Elf_Internal_Shdr **i_shdrp;
4124
329
  struct bfd_elf_section_data *d;
4125
329
  bool need_symtab;
4126
329
  size_t amt;
4127
4128
329
  section_number = 1;
4129
4130
329
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4131
4132
  /* SHT_GROUP sections are in relocatable files only.  */
4133
329
  if (link_info == NULL || !link_info->resolve_section_groups)
4134
329
    {
4135
329
      size_t reloc_count = 0;
4136
4137
      /* Put SHT_GROUP sections first.  */
4138
1.71k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4139
1.38k
  {
4140
1.38k
    d = elf_section_data (sec);
4141
4142
1.38k
    if (d->this_hdr.sh_type == SHT_GROUP)
4143
11
      {
4144
11
        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
11
        else
4151
11
    d->this_idx = section_number++;
4152
11
      }
4153
4154
    /* Count relocations.  */
4155
1.38k
    reloc_count += sec->reloc_count;
4156
1.38k
  }
4157
4158
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4159
329
      if (reloc_count == 0)
4160
306
  abfd->flags &= ~HAS_RELOC;
4161
23
      else
4162
23
  abfd->flags |= HAS_RELOC;
4163
329
    }
4164
4165
1.71k
  for (sec = abfd->sections; sec; sec = sec->next)
4166
1.38k
    {
4167
1.38k
      d = elf_section_data (sec);
4168
4169
1.38k
      if (d->this_hdr.sh_type != SHT_GROUP)
4170
1.37k
  d->this_idx = section_number++;
4171
1.38k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4172
1.38k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4173
1.38k
      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.38k
      else
4180
1.38k
  d->rel.idx = 0;
4181
4182
1.38k
      if (d->rela.hdr)
4183
173
  {
4184
173
    d->rela.idx = section_number++;
4185
173
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4186
173
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4187
173
  }
4188
1.21k
      else
4189
1.21k
  d->rela.idx = 0;
4190
1.38k
    }
4191
4192
329
  need_symtab = (bfd_get_symcount (abfd) > 0
4193
290
     || (link_info == NULL
4194
290
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4195
290
       == HAS_RELOC)));
4196
329
  if (need_symtab)
4197
39
    {
4198
39
      elf_onesymtab (abfd) = section_number++;
4199
39
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4200
39
      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
39
      elf_strtab_sec (abfd) = section_number++;
4216
39
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4217
39
    }
4218
4219
329
  elf_shstrtab_sec (abfd) = section_number++;
4220
329
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4221
329
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4222
4223
329
  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
329
  elf_numsections (abfd) = section_number;
4232
329
  elf_elfheader (abfd)->e_shnum = section_number;
4233
4234
  /* Set up the list of section header pointers, in agreement with the
4235
     indices.  */
4236
329
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4237
329
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4238
329
  if (i_shdrp == NULL)
4239
0
    return false;
4240
4241
329
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4242
329
             sizeof (Elf_Internal_Shdr));
4243
329
  if (i_shdrp[0] == NULL)
4244
0
    {
4245
0
      bfd_release (abfd, i_shdrp);
4246
0
      return false;
4247
0
    }
4248
4249
329
  elf_elfsections (abfd) = i_shdrp;
4250
4251
329
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4252
329
  if (need_symtab)
4253
39
    {
4254
39
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4255
39
      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
39
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4263
39
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4264
39
    }
4265
4266
1.71k
  for (sec = abfd->sections; sec; sec = sec->next)
4267
1.38k
    {
4268
1.38k
      asection *s;
4269
4270
1.38k
      d = elf_section_data (sec);
4271
4272
1.38k
      i_shdrp[d->this_idx] = &d->this_hdr;
4273
1.38k
      if (d->rel.idx != 0)
4274
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4275
1.38k
      if (d->rela.idx != 0)
4276
173
  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.38k
      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.38k
      if (d->rela.idx != 0)
4290
173
  {
4291
173
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4292
173
    d->rela.hdr->sh_info = d->this_idx;
4293
173
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4294
173
  }
4295
4296
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4297
1.38k
      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.38k
      switch (d->this_hdr.sh_type)
4343
1.38k
  {
4344
0
  case SHT_REL:
4345
60
  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
60
    if (d->this_hdr.sh_link == 0)
4350
60
      {
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
60
        if ((sec->flags & SEC_ALLOC) != 0)
4355
38
    {
4356
38
      s = bfd_get_section_by_name (abfd, ".dynsym");
4357
38
      if (s != NULL)
4358
26
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4359
38
    }
4360
22
        else
4361
22
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4362
60
      }
4363
4364
60
    s = elf_get_reloc_section (sec);
4365
60
    if (s != NULL)
4366
28
      {
4367
28
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4368
28
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4369
28
      }
4370
60
    break;
4371
4372
23
  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
23
    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
23
    break;
4400
4401
23
  case SHT_DYNAMIC:
4402
40
  case SHT_DYNSYM:
4403
44
  case SHT_GNU_verneed:
4404
44
  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
44
    s = bfd_get_section_by_name (abfd, ".dynstr");
4409
44
    if (s != NULL)
4410
31
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4411
44
    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
22
  case SHT_HASH:
4424
32
  case SHT_GNU_HASH:
4425
36
  case SHT_GNU_versym:
4426
    /* sh_link is the section header index of the symbol table
4427
       this hash table or version table is for.  */
4428
36
    s = bfd_get_section_by_name (abfd, ".dynsym");
4429
36
    if (s != NULL)
4430
20
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4431
36
    break;
4432
4433
11
  case SHT_GROUP:
4434
11
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4435
1.38k
  }
4436
1.38k
    }
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
329
  return true;
4443
329
}
4444
4445
static bool
4446
sym_is_global (bfd *abfd, asymbol *sym)
4447
2.50k
{
4448
  /* If the backend has a special mapping, use it.  */
4449
2.50k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4450
2.50k
  if (bed->elf_backend_sym_is_global)
4451
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4452
4453
2.50k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4454
2.00k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4455
1.04k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4456
2.50k
}
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.66k
{
4502
4.66k
  if (sym == NULL)
4503
0
    return false;
4504
4505
4.66k
  if (sym->section == NULL)
4506
0
    return true;
4507
4508
4.66k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4509
2.43k
    {
4510
2.43k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4511
1.98k
  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
450
      if (sym->section->output_offset != 0)
4518
0
  return true;
4519
450
    }
4520
4521
2.68k
  return discarded_section (sym->section);
4522
4.66k
}
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
39
{
4530
39
  unsigned int symcount = bfd_get_symcount (abfd);
4531
39
  asymbol **syms = bfd_get_outsymbols (abfd);
4532
39
  asymbol **sect_syms;
4533
39
  unsigned int num_locals = 0;
4534
39
  unsigned int num_globals = 0;
4535
39
  unsigned int max_index = 0;
4536
39
  unsigned int idx;
4537
39
  asection *asect;
4538
39
  asymbol **new_syms;
4539
39
  size_t amt;
4540
4541
#ifdef DEBUG
4542
  fprintf (stderr, "elf_map_symbols\n");
4543
  fflush (stderr);
4544
#endif
4545
4546
863
  for (asect = abfd->sections; asect; asect = asect->next)
4547
824
    {
4548
824
      if (max_index < asect->index)
4549
785
  max_index = asect->index;
4550
824
    }
4551
4552
39
  max_index++;
4553
39
  amt = max_index * sizeof (asymbol *);
4554
39
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4555
39
  if (sect_syms == NULL)
4556
0
    return false;
4557
39
  elf_section_syms (abfd) = sect_syms;
4558
39
  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.42k
  for (idx = 0; idx < symcount; idx++)
4563
1.38k
    {
4564
1.38k
      asymbol *sym = syms[idx];
4565
4566
1.38k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4567
266
    && sym->value == 0
4568
252
    && !ignore_sym (sym)
4569
128
    && !bfd_is_abs_section (sym->section))
4570
124
  {
4571
124
    asection *sec = sym->section;
4572
4573
124
    if (sec->owner != abfd)
4574
124
      {
4575
124
        sec = sec->output_section;
4576
124
        if (sec == NULL)
4577
0
    return false;
4578
124
      }
4579
4580
124
    sect_syms[sec->index] = syms[idx];
4581
124
  }
4582
1.38k
    }
4583
4584
  /* Classify all of the symbols.  */
4585
1.42k
  for (idx = 0; idx < symcount; idx++)
4586
1.38k
    {
4587
1.38k
      if (ignore_sym (syms[idx]))
4588
134
  continue;
4589
1.24k
      if (sym_is_global (abfd, syms[idx]))
4590
729
  num_globals++;
4591
518
      else
4592
518
  num_locals++;
4593
1.24k
    }
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
863
  for (asect = abfd->sections; asect; asect = asect->next)
4600
824
    {
4601
824
      asymbol *sym = asect->symbol;
4602
      /* Don't include ignored section symbols.  */
4603
824
      if (!ignore_sym (sym)
4604
29
    && sect_syms[asect->index] == NULL)
4605
3
  {
4606
3
    if (sym_is_global (abfd, asect->symbol))
4607
0
      num_globals++;
4608
3
    else
4609
3
      num_locals++;
4610
3
  }
4611
824
    }
4612
4613
  /* Now sort the symbols so the local symbols are first.  */
4614
39
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4615
39
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4616
39
  if (new_syms == NULL)
4617
0
    return false;
4618
4619
39
  unsigned int num_globals2 = 0;
4620
39
  unsigned int num_locals2 = 0;
4621
1.42k
  for (idx = 0; idx < symcount; idx++)
4622
1.38k
    {
4623
1.38k
      asymbol *sym = syms[idx];
4624
1.38k
      unsigned int i;
4625
4626
1.38k
      if (ignore_sym (sym))
4627
134
  continue;
4628
4629
1.24k
      if (sym_is_global (abfd, sym))
4630
729
  i = num_locals + num_globals2++;
4631
518
      else
4632
518
  i = num_locals2++;
4633
1.24k
      new_syms[i] = sym;
4634
1.24k
      sym->udata.i = i + 1;
4635
1.24k
    }
4636
863
  for (asect = abfd->sections; asect; asect = asect->next)
4637
824
    {
4638
824
      asymbol *sym = asect->symbol;
4639
824
      if (!ignore_sym (sym)
4640
29
    && sect_syms[asect->index] == NULL)
4641
3
  {
4642
3
    unsigned int i;
4643
4644
3
    sect_syms[asect->index] = sym;
4645
3
    if (sym_is_global (abfd, sym))
4646
0
      i = num_locals + num_globals2++;
4647
3
    else
4648
3
      i = num_locals2++;
4649
3
    new_syms[i] = sym;
4650
3
    sym->udata.i = i + 1;
4651
3
  }
4652
824
    }
4653
4654
39
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4655
4656
39
  *pnum_locals = num_locals;
4657
39
  return true;
4658
39
}
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.69k
{
4669
1.69k
  if (i_shdrp->sh_addralign > 1)
4670
672
    {
4671
672
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4672
4673
672
      if (align)
4674
212
  offset = BFD_ALIGN (offset, salign);
4675
460
      else if (log_file_align)
4676
460
  {
4677
    /* Heuristic: Cap alignment at log_file_align.  */
4678
460
    file_ptr falign = 1u << log_file_align;
4679
4680
460
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4681
460
  }
4682
672
    }
4683
1.69k
  i_shdrp->sh_offset = offset;
4684
1.69k
  if (i_shdrp->bfd_section != NULL)
4685
841
    i_shdrp->bfd_section->filepos = offset;
4686
1.69k
  if (i_shdrp->sh_type != SHT_NOBITS)
4687
1.68k
    offset += i_shdrp->sh_size;
4688
1.69k
  return offset;
4689
1.69k
}
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
329
{
4699
329
  elf_backend_data *bed = get_elf_backend_data (abfd);
4700
329
  struct fake_section_arg fsargs;
4701
329
  bool failed;
4702
329
  struct elf_strtab_hash *strtab = NULL;
4703
329
  Elf_Internal_Shdr *shstrtab_hdr;
4704
329
  bool need_symtab;
4705
4706
329
  if (abfd->output_has_begun)
4707
0
    return true;
4708
4709
  /* Do any elf backend specific processing first.  */
4710
329
  if (bed->elf_backend_begin_write_processing)
4711
14
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4712
4713
329
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4714
0
    return false;
4715
4716
329
  fsargs.failed = false;
4717
329
  fsargs.link_info = link_info;
4718
329
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4719
329
  if (fsargs.failed)
4720
0
    return false;
4721
4722
329
  if (!assign_section_numbers (abfd, link_info))
4723
0
    return false;
4724
4725
  /* The backend linker builds symbol table information itself.  */
4726
329
  need_symtab = (link_info == NULL
4727
329
     && (bfd_get_symcount (abfd) > 0
4728
290
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4729
290
       == HAS_RELOC)));
4730
329
  if (need_symtab)
4731
39
    {
4732
      /* Non-zero if doing a relocatable link.  */
4733
39
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4734
4735
39
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4736
0
  return false;
4737
39
    }
4738
4739
329
  failed = false;
4740
329
  if (link_info == NULL)
4741
329
    {
4742
329
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4743
329
      if (failed)
4744
0
  goto err_free_strtab;
4745
329
    }
4746
4747
329
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4748
  /* sh_name was set in init_file_header.  */
4749
329
  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
329
  shstrtab_hdr->sh_addralign = 1;
4755
4756
329
  if (!assign_file_positions_except_relocs (abfd, link_info))
4757
18
    goto err_free_strtab;
4758
4759
311
  if (strtab != NULL)
4760
34
    {
4761
34
      file_ptr off;
4762
34
      Elf_Internal_Shdr *hdr;
4763
4764
34
      off = elf_next_file_pos (abfd);
4765
4766
34
      hdr = & elf_symtab_hdr (abfd);
4767
34
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4768
4769
34
      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
34
      hdr = &elf_tdata (abfd)->strtab_hdr;
4778
34
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4779
4780
34
      elf_next_file_pos (abfd) = off;
4781
4782
      /* Now that we know where the .strtab section goes, write it
4783
   out.  */
4784
34
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4785
34
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4786
0
  goto err_free_strtab;
4787
34
      _bfd_elf_strtab_free (strtab);
4788
34
    }
4789
4790
311
  abfd->output_has_begun = true;
4791
311
  return true;
4792
4793
18
 err_free_strtab:
4794
18
  if (strtab != NULL)
4795
5
    _bfd_elf_strtab_free (strtab);
4796
18
  return false;
4797
311
}
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
18
{
4808
18
  if (info != NULL && is_elf_hash_table (info->hash))
4809
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4810
18
  return NULL;
4811
18
}
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
10
{
4819
10
  size_t segs;
4820
10
  asection *s;
4821
10
  elf_backend_data *bed;
4822
4823
  /* Assume we will need exactly two PT_LOAD segments: one for text
4824
     and one for data.  */
4825
10
  segs = 2;
4826
4827
10
  s = bfd_get_section_by_name (abfd, ".interp");
4828
10
  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
10
  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
10
  if (info != NULL && info->relro)
4844
0
    {
4845
      /* We need a PT_GNU_RELRO segment.  */
4846
0
      ++segs;
4847
0
    }
4848
4849
10
  if (elf_eh_frame_hdr (info))
4850
0
    {
4851
      /* We need a PT_GNU_EH_FRAME segment.  */
4852
0
      ++segs;
4853
0
    }
4854
4855
10
  if (elf_stack_flags (abfd))
4856
0
    {
4857
      /* We need a PT_GNU_STACK segment.  */
4858
0
      ++segs;
4859
0
    }
4860
4861
10
  if (elf_sframe (abfd))
4862
0
    {
4863
      /* We need a PT_GNU_SFRAME segment.  */
4864
0
      ++segs;
4865
0
    }
4866
4867
10
  s = bfd_get_section_by_name (abfd,
4868
10
             NOTE_GNU_PROPERTY_SECTION_NAME);
4869
10
  if (s != NULL && s->size != 0)
4870
0
    {
4871
      /* We need a PT_GNU_PROPERTY segment.  */
4872
0
      ++segs;
4873
0
    }
4874
4875
95
  for (s = abfd->sections; s != NULL; s = s->next)
4876
85
    {
4877
85
      if ((s->flags & SEC_LOAD) != 0
4878
41
    && 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
85
    }
4896
4897
64
  for (s = abfd->sections; s != NULL; s = s->next)
4898
58
    {
4899
58
      if (s->flags & SEC_THREAD_LOCAL)
4900
4
  {
4901
    /* We need a PT_TLS segment.  */
4902
4
    ++segs;
4903
4
    break;
4904
4
  }
4905
58
    }
4906
4907
10
  bed = get_elf_backend_data (abfd);
4908
4909
10
  if ((abfd->flags & D_PAGED) != 0
4910
10
      && (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
10
  if (bed->elf_backend_additional_program_headers)
4942
10
    {
4943
10
      int a;
4944
4945
10
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4946
10
      if (a == -1)
4947
0
  abort ();
4948
10
      segs += a;
4949
10
    }
4950
4951
10
  return segs * bed->s->sizeof_phdr;
4952
10
}
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
45
{
4985
45
  struct elf_segment_map *m;
4986
45
  unsigned int i;
4987
45
  asection **hdrpp;
4988
45
  size_t amt;
4989
4990
45
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4991
45
  amt += (to - from) * sizeof (asection *);
4992
45
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4993
45
  if (m == NULL)
4994
0
    return NULL;
4995
45
  m->next = NULL;
4996
45
  m->p_type = PT_LOAD;
4997
92
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4998
47
    m->sections[i - from] = *hdrpp;
4999
45
  m->count = to - from;
5000
5001
45
  if (from == 0 && phdr)
5002
2
    {
5003
      /* Include the headers in the first PT_LOAD segment.  */
5004
2
      m->includes_filehdr = 1;
5005
2
      m->includes_phdrs = 1;
5006
2
    }
5007
5008
45
  return m;
5009
45
}
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
256
{
5038
256
  struct elf_segment_map **m;
5039
256
  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
256
  m = &elf_seg_map (abfd);
5047
819
  while (*m)
5048
563
    {
5049
563
      unsigned int i, new_count;
5050
5051
1.54k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5052
982
  {
5053
982
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5054
980
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5055
203
      || (*m)->p_type != PT_LOAD))
5056
980
      {
5057
980
        (*m)->sections[new_count] = (*m)->sections[i];
5058
980
        new_count++;
5059
980
      }
5060
982
  }
5061
563
      (*m)->count = new_count;
5062
5063
563
      if (remove_empty_load
5064
563
    && (*m)->p_type == PT_LOAD
5065
127
    && (*m)->count == 0
5066
31
    && !(*m)->includes_phdrs)
5067
26
  *m = (*m)->next;
5068
537
      else
5069
537
  m = &(*m)->next;
5070
563
    }
5071
5072
256
  bed = get_elf_backend_data (abfd);
5073
256
  if (bed->elf_backend_modify_segment_map != NULL)
5074
75
    {
5075
75
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5076
0
  return false;
5077
75
    }
5078
5079
256
  return true;
5080
256
}
5081
5082
#define IS_TBSS(s) \
5083
72
  ((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
258
{
5093
258
  unsigned int count;
5094
258
  struct elf_segment_map *m;
5095
258
  asection **sections = NULL;
5096
258
  elf_backend_data *bed = get_elf_backend_data (abfd);
5097
258
  bool no_user_phdrs;
5098
5099
258
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5100
5101
258
  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
258
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5115
10
    {
5116
10
      asection *s;
5117
10
      unsigned int i;
5118
10
      struct elf_segment_map *mfirst;
5119
10
      struct elf_segment_map **pm;
5120
10
      asection *last_hdr;
5121
10
      bfd_vma last_size;
5122
10
      unsigned int hdr_index;
5123
10
      bfd_vma maxpagesize;
5124
10
      asection **hdrpp;
5125
10
      bool phdr_in_segment;
5126
10
      bool writable;
5127
10
      bool executable;
5128
10
      unsigned int tls_count = 0;
5129
10
      asection *first_tls = NULL;
5130
10
      asection *first_mbind = NULL;
5131
10
      asection *dynsec, *eh_frame_hdr;
5132
10
      asection *sframe;
5133
10
      size_t amt;
5134
10
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5135
10
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5136
10
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5137
5138
      /* Select the allocated sections, and sort them.  */
5139
5140
10
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5141
10
      sections = (asection **) bfd_malloc (amt);
5142
10
      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
10
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5149
10
      addr_mask = (addr_mask << 1) + 1;
5150
5151
10
      i = 0;
5152
95
      for (s = abfd->sections; s != NULL; s = s->next)
5153
85
  {
5154
85
    if ((s->flags & SEC_ALLOC) != 0)
5155
48
      {
5156
        /* target_index is unused until bfd_elf_final_link
5157
     starts output of section symbols.  Use it to make
5158
     qsort stable.  */
5159
48
        s->target_index = i;
5160
48
        sections[i] = s;
5161
48
        ++i;
5162
        /* A wrapping section potentially clashes with header.  */
5163
48
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5164
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5165
48
      }
5166
85
  }
5167
10
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5168
10
      count = i;
5169
5170
10
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5171
5172
10
      phdr_size = elf_program_header_size (abfd);
5173
10
      if (phdr_size == (bfd_size_type) -1)
5174
10
  phdr_size = get_program_header_size (abfd, info);
5175
10
      phdr_size += bed->s->sizeof_ehdr;
5176
      /* phdr_size is compared to LMA values which are in bytes.  */
5177
10
      phdr_size /= opb;
5178
10
      if (info != NULL)
5179
0
  maxpagesize = info->maxpagesize;
5180
10
      else
5181
10
  maxpagesize = bed->maxpagesize;
5182
10
      if (maxpagesize == 0)
5183
0
  maxpagesize = 1;
5184
10
      phdr_in_segment = info != NULL && info->load_phdrs;
5185
10
      if (count != 0
5186
9
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5187
9
        >= (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
2
  phdr_in_segment = true;
5192
5193
      /* Build the mapping.  */
5194
10
      mfirst = NULL;
5195
10
      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
10
      s = bfd_get_section_by_name (abfd, ".interp");
5201
10
      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
10
      last_hdr = NULL;
5233
10
      last_size = 0;
5234
10
      hdr_index = 0;
5235
10
      writable = false;
5236
10
      executable = false;
5237
10
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5238
10
      if (dynsec != NULL
5239
0
    && (dynsec->flags & SEC_LOAD) == 0)
5240
0
  dynsec = NULL;
5241
5242
10
      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
10
      if (phdr_in_segment && count > 0)
5250
2
  {
5251
2
    bfd_vma phdr_lma;  /* Bytes.  */
5252
2
    bool separate_phdr = false;
5253
5254
2
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5255
2
    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
2
    if ((sections[0]->lma & addr_mask) < phdr_lma
5279
2
        || (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
2
    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
2
    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
2
  }
5302
5303
58
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5304
48
  {
5305
48
    asection *hdr;
5306
48
    bool new_segment;
5307
5308
48
    hdr = *hdrpp;
5309
5310
    /* See if this section and the last one will fit in the same
5311
       segment.  */
5312
5313
48
    if (last_hdr == NULL)
5314
9
      {
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
9
        new_segment = false;
5318
9
      }
5319
39
    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
37
    else if (hdr->lma < last_hdr->lma + last_size
5327
14
       || last_hdr->lma + last_size < last_hdr->lma)
5328
23
      {
5329
        /* If this section has a load address that makes it overlap
5330
     the previous section, then we need a new segment.  */
5331
23
        new_segment = true;
5332
23
      }
5333
14
    else if ((abfd->flags & D_PAGED) != 0
5334
14
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5335
14
           == (hdr->lma & -maxpagesize)))
5336
0
      {
5337
        /* If we are demand paged then we can't map two disk
5338
     pages onto the same memory page.  */
5339
0
        new_segment = false;
5340
0
      }
5341
    /* In the next test we have to be careful when last_hdr->lma is close
5342
       to the end of the address space.  If the aligned address wraps
5343
       around to the start of the address space, then there are no more
5344
       pages left in memory and it is OK to assume that the current
5345
       section can be included in the current segment.  */
5346
14
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5347
14
        + maxpagesize > last_hdr->lma)
5348
14
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5349
14
           + maxpagesize <= hdr->lma))
5350
9
      {
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
9
        new_segment = true;
5354
9
      }
5355
5
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5356
1
       && (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
5
    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
5
    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
5
    else if (! writable
5378
5
       && (hdr->flags & SEC_READONLY) == 0)
5379
3
      {
5380
        /* We don't want to put a writable section in a read only
5381
     segment.  */
5382
3
        new_segment = true;
5383
3
      }
5384
2
    else
5385
2
      {
5386
        /* Otherwise, we can use the same segment.  */
5387
2
        new_segment = false;
5388
2
      }
5389
5390
    /* Allow interested parties a chance to override our decision.  */
5391
48
    if (last_hdr != NULL
5392
39
        && 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
48
    if (! new_segment)
5400
11
      {
5401
11
        if ((hdr->flags & SEC_READONLY) == 0)
5402
3
    writable = true;
5403
11
        if ((hdr->flags & SEC_CODE) != 0)
5404
6
    executable = true;
5405
11
        last_hdr = hdr;
5406
        /* .tbss sections effectively have zero size.  */
5407
11
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5408
11
        continue;
5409
11
      }
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
37
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5415
37
    if (m == NULL)
5416
0
      goto error_return;
5417
5418
37
    *pm = m;
5419
37
    pm = &m->next;
5420
5421
37
    if ((hdr->flags & SEC_READONLY) == 0)
5422
16
      writable = true;
5423
21
    else
5424
21
      writable = false;
5425
5426
37
    if ((hdr->flags & SEC_CODE) == 0)
5427
27
      executable = false;
5428
10
    else
5429
10
      executable = true;
5430
5431
37
    last_hdr = hdr;
5432
    /* .tbss sections effectively have zero size.  */
5433
37
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5434
37
    hdr_index = i;
5435
37
    phdr_in_segment = false;
5436
37
  }
5437
5438
      /* Create a final PT_LOAD program segment, but not if it's just
5439
   for .tbss.  */
5440
10
      if (last_hdr != NULL
5441
9
    && (i - hdr_index != 1
5442
9
        || !IS_TBSS (last_hdr)))
5443
8
  {
5444
8
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5445
8
    if (m == NULL)
5446
0
      goto error_return;
5447
5448
8
    *pm = m;
5449
8
    pm = &m->next;
5450
8
  }
5451
5452
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5453
10
      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
95
      for (s = abfd->sections; s != NULL; s = s->next)
5468
85
  {
5469
85
    if ((s->flags & SEC_LOAD) != 0
5470
41
        && 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
85
    if (s->flags & SEC_THREAD_LOCAL)
5508
6
      {
5509
6
        if (! tls_count)
5510
4
    first_tls = s;
5511
6
        tls_count++;
5512
6
      }
5513
85
    if (first_mbind == NULL
5514
81
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5515
5
      first_mbind = s;
5516
85
  }
5517
5518
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5519
10
      if (tls_count > 0)
5520
4
  {
5521
4
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5522
4
    amt += tls_count * sizeof (asection *);
5523
4
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5524
4
    if (m == NULL)
5525
0
      goto error_return;
5526
4
    m->next = NULL;
5527
4
    m->p_type = PT_TLS;
5528
4
    m->count = tls_count;
5529
    /* Mandated PF_R.  */
5530
4
    m->p_flags = PF_R;
5531
4
    m->p_flags_valid = 1;
5532
4
    s = first_tls;
5533
8
    for (i = 0; i < tls_count; ++i)
5534
6
      {
5535
6
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5536
2
    {
5537
2
      _bfd_error_handler
5538
2
        (_("%pB: TLS sections are not adjacent:"), abfd);
5539
2
      s = first_tls;
5540
2
      i = 0;
5541
8
      while (i < tls_count)
5542
6
        {
5543
6
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5544
4
      {
5545
4
        _bfd_error_handler (_("      TLS: %pA"), s);
5546
4
        i++;
5547
4
      }
5548
2
          else
5549
2
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5550
6
          s = s->next;
5551
6
        }
5552
2
      bfd_set_error (bfd_error_bad_value);
5553
2
      goto error_return;
5554
2
    }
5555
4
        m->sections[i] = s;
5556
4
        s = s->next;
5557
4
      }
5558
5559
2
    *pm = m;
5560
2
    pm = &m->next;
5561
2
  }
5562
5563
8
      if (first_mbind
5564
3
    && (abfd->flags & D_PAGED) != 0
5565
3
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5566
0
  for (s = first_mbind; s != NULL; s = s->next)
5567
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5568
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5569
0
      {
5570
        /* Mandated PF_R.  */
5571
0
        unsigned long p_flags = PF_R;
5572
0
        if ((s->flags & SEC_READONLY) == 0)
5573
0
    p_flags |= PF_W;
5574
0
        if ((s->flags & SEC_CODE) != 0)
5575
0
    p_flags |= PF_X;
5576
5577
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5578
0
        m = bfd_zalloc (abfd, amt);
5579
0
        if (m == NULL)
5580
0
    goto error_return;
5581
0
        m->next = NULL;
5582
0
        m->p_type = (PT_GNU_MBIND_LO
5583
0
         + elf_section_data (s)->this_hdr.sh_info);
5584
0
        m->count = 1;
5585
0
        m->p_flags_valid = 1;
5586
0
        m->sections[0] = s;
5587
0
        m->p_flags = p_flags;
5588
5589
0
        *pm = m;
5590
0
        pm = &m->next;
5591
0
      }
5592
5593
8
      s = bfd_get_section_by_name (abfd,
5594
8
           NOTE_GNU_PROPERTY_SECTION_NAME);
5595
8
      if (s != NULL && s->size != 0)
5596
0
  {
5597
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5598
0
    m = bfd_zalloc (abfd, amt);
5599
0
    if (m == NULL)
5600
0
      goto error_return;
5601
0
    m->next = NULL;
5602
0
    m->p_type = PT_GNU_PROPERTY;
5603
0
    m->count = 1;
5604
0
    m->p_flags_valid = 1;
5605
0
    m->sections[0] = s;
5606
0
    m->p_flags = PF_R;
5607
0
    *pm = m;
5608
0
    pm = &m->next;
5609
0
  }
5610
5611
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5612
   segment.  */
5613
8
      eh_frame_hdr = elf_eh_frame_hdr (info);
5614
8
      if (eh_frame_hdr != NULL
5615
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5616
0
  {
5617
0
    amt = sizeof (struct elf_segment_map);
5618
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5619
0
    if (m == NULL)
5620
0
      goto error_return;
5621
0
    m->next = NULL;
5622
0
    m->p_type = PT_GNU_EH_FRAME;
5623
0
    m->count = 1;
5624
0
    m->sections[0] = eh_frame_hdr->output_section;
5625
5626
0
    *pm = m;
5627
0
    pm = &m->next;
5628
0
  }
5629
5630
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5631
   segment.  */
5632
8
      sframe = elf_sframe (abfd);
5633
8
      if (sframe != NULL
5634
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5635
0
    && sframe->size != 0)
5636
0
  {
5637
0
    amt = sizeof (struct elf_segment_map);
5638
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5639
0
    if (m == NULL)
5640
0
      goto error_return;
5641
0
    m->next = NULL;
5642
0
    m->p_type = PT_GNU_SFRAME;
5643
0
    m->count = 1;
5644
0
    m->sections[0] = sframe->output_section;
5645
5646
0
    *pm = m;
5647
0
    pm = &m->next;
5648
0
  }
5649
5650
8
      if (elf_stack_flags (abfd))
5651
0
  {
5652
0
    amt = sizeof (struct elf_segment_map);
5653
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5654
0
    if (m == NULL)
5655
0
      goto error_return;
5656
0
    m->next = NULL;
5657
0
    m->p_type = PT_GNU_STACK;
5658
0
    m->p_flags = elf_stack_flags (abfd);
5659
0
    m->p_align = bed->stack_align;
5660
0
    m->p_flags_valid = 1;
5661
0
    m->p_align_valid = m->p_align != 0;
5662
0
    if (info->stacksize > 0)
5663
0
      {
5664
0
        m->p_size = info->stacksize;
5665
0
        m->p_size_valid = 1;
5666
0
      }
5667
5668
0
    *pm = m;
5669
0
    pm = &m->next;
5670
0
  }
5671
5672
8
      if (info != NULL && info->relro)
5673
0
  {
5674
0
    for (m = mfirst; m != NULL; m = m->next)
5675
0
      {
5676
0
        if (m->p_type == PT_LOAD
5677
0
      && m->count != 0
5678
0
      && m->sections[0]->vma >= info->relro_start
5679
0
      && m->sections[0]->vma < info->relro_end)
5680
0
    {
5681
0
      i = m->count;
5682
0
      while (--i != (unsigned) -1)
5683
0
        {
5684
0
          if (m->sections[i]->size > 0
5685
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5686
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5687
0
      break;
5688
0
        }
5689
5690
0
      if (i != (unsigned) -1)
5691
0
        break;
5692
0
    }
5693
0
      }
5694
5695
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5696
0
    if (m != NULL)
5697
0
      {
5698
0
        amt = sizeof (struct elf_segment_map);
5699
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5700
0
        if (m == NULL)
5701
0
    goto error_return;
5702
0
        m->next = NULL;
5703
0
        m->p_type = PT_GNU_RELRO;
5704
0
        *pm = m;
5705
0
        pm = &m->next;
5706
0
      }
5707
0
  }
5708
5709
8
      free (sections);
5710
8
      elf_seg_map (abfd) = mfirst;
5711
8
    }
5712
5713
256
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5714
0
    return false;
5715
5716
793
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5717
537
    ++count;
5718
256
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5719
5720
256
  return true;
5721
5722
2
 error_return:
5723
2
  free (sections);
5724
2
  return false;
5725
256
}
5726
5727
/* Sort sections by address.  */
5728
5729
static int
5730
elf_sort_sections (const void *arg1, const void *arg2)
5731
1.30k
{
5732
1.30k
  const asection *sec1 = *(const asection **) arg1;
5733
1.30k
  const asection *sec2 = *(const asection **) arg2;
5734
1.30k
  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.30k
  if (sec1->lma < sec2->lma)
5739
993
    return -1;
5740
309
  else if (sec1->lma > sec2->lma)
5741
198
    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
111
  if (sec1->vma < sec2->vma)
5746
13
    return -1;
5747
98
  else if (sec1->vma > sec2->vma)
5748
15
    return 1;
5749
5750
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5751
5752
166
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5753
166
      && (x)->size != 0)
5754
5755
83
  if (TOEND (sec1))
5756
19
    {
5757
19
      if (!TOEND (sec2))
5758
0
  return 1;
5759
19
    }
5760
64
  else if (TOEND (sec2))
5761
3
    return -1;
5762
5763
80
#undef TOEND
5764
5765
  /* Sort by size, to put zero sized sections
5766
     before others at the same address.  */
5767
5768
80
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5769
80
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5770
5771
80
  if (size1 < size2)
5772
35
    return -1;
5773
45
  if (size1 > size2)
5774
24
    return 1;
5775
5776
21
  return sec1->target_index - sec2->target_index;
5777
45
}
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
857
{
5785
857
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5786
857
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5787
5788
857
  if (m1->p_type != m2->p_type)
5789
727
    {
5790
727
      if (m1->p_type == PT_NULL)
5791
20
  return 1;
5792
707
      if (m2->p_type == PT_NULL)
5793
24
  return -1;
5794
683
      return m1->p_type < m2->p_type ? -1 : 1;
5795
707
    }
5796
130
  if (m1->includes_filehdr != m2->includes_filehdr)
5797
24
    return m1->includes_filehdr ? -1 : 1;
5798
106
  if (m1->no_sort_lma != m2->no_sort_lma)
5799
0
    return m1->no_sort_lma ? -1 : 1;
5800
106
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5801
64
    {
5802
64
      bfd_vma lma1, lma2;  /* Octets.  */
5803
64
      lma1 = 0;
5804
64
      if (m1->p_paddr_valid)
5805
29
  lma1 = m1->p_paddr;
5806
35
      else if (m1->count != 0)
5807
35
  {
5808
35
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5809
35
              m1->sections[0]);
5810
35
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5811
35
  }
5812
64
      lma2 = 0;
5813
64
      if (m2->p_paddr_valid)
5814
29
  lma2 = m2->p_paddr;
5815
35
      else if (m2->count != 0)
5816
35
  {
5817
35
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5818
35
              m2->sections[0]);
5819
35
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5820
35
  }
5821
64
      if (lma1 != lma2)
5822
37
  return lma1 < lma2 ? -1 : 1;
5823
64
    }
5824
69
  if (m1->idx != m2->idx)
5825
69
    return m1->idx < m2->idx ? -1 : 1;
5826
0
  return 0;
5827
69
}
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
185
{
5855
  /* PR binutils/16199: Handle an alignment of zero.  */
5856
185
  if (maxpagesize == 0)
5857
0
    maxpagesize = 1;
5858
185
  return ((vma - off) % maxpagesize);
5859
185
}
5860
5861
static void
5862
print_segment_map (const struct elf_segment_map *m)
5863
3
{
5864
3
  unsigned int j;
5865
3
  const char *pt = get_segment_type (m->p_type);
5866
3
  char buf[32];
5867
5868
3
  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
3
  fflush (stdout);
5882
3
  fprintf (stderr, "%s:", pt);
5883
14
  for (j = 0; j < m->count; j++)
5884
11
    fprintf (stderr, " %s", m->sections [j]->name);
5885
3
  putc ('\n',stderr);
5886
3
  fflush (stderr);
5887
3
}
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
258
{
5897
258
  elf_backend_data *bed = get_elf_backend_data (abfd);
5898
258
  struct elf_segment_map *m;
5899
258
  struct elf_segment_map *phdr_load_seg;
5900
258
  Elf_Internal_Phdr *phdrs;
5901
258
  Elf_Internal_Phdr *p;
5902
258
  file_ptr off;  /* Octets.  */
5903
258
  bfd_size_type maxpagesize;
5904
258
  unsigned int alloc, actual;
5905
258
  unsigned int i, j;
5906
258
  struct elf_segment_map **sorted_seg_map;
5907
258
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5908
5909
258
  if (link_info == NULL
5910
258
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5911
2
    return false;
5912
5913
256
  alloc = 0;
5914
793
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5915
537
    m->idx = alloc++;
5916
5917
256
  if (alloc)
5918
142
    {
5919
142
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5920
142
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5921
142
    }
5922
114
  else
5923
114
    {
5924
      /* PR binutils/12467.  */
5925
114
      elf_elfheader (abfd)->e_phoff = 0;
5926
114
      elf_elfheader (abfd)->e_phentsize = 0;
5927
114
    }
5928
5929
256
  elf_elfheader (abfd)->e_phnum = alloc;
5930
5931
256
  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
256
  else
5937
256
    {
5938
256
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5939
256
      BFD_ASSERT (elf_program_header_size (abfd)
5940
256
      == actual * bed->s->sizeof_phdr);
5941
256
      BFD_ASSERT (actual >= alloc);
5942
256
    }
5943
5944
256
  if (alloc == 0)
5945
114
    {
5946
114
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5947
114
      return true;
5948
114
    }
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
142
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5960
142
           + alloc * sizeof (*sorted_seg_map)));
5961
142
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5962
142
  elf_tdata (abfd)->phdr = phdrs;
5963
142
  if (phdrs == NULL)
5964
0
    return false;
5965
5966
679
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5967
537
    {
5968
537
      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
537
      if (m->count > 1
5975
156
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5976
84
         && m->p_type == PT_NOTE))
5977
155
  {
5978
988
    for (i = 0; i < m->count; i++)
5979
833
      m->sections[i]->target_index = i;
5980
155
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5981
155
     elf_sort_sections);
5982
155
  }
5983
537
    }
5984
142
  if (alloc > 1)
5985
71
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5986
71
     elf_sort_segments);
5987
5988
142
  maxpagesize = 1;
5989
142
  if ((abfd->flags & D_PAGED) != 0)
5990
37
    {
5991
37
      if (link_info != NULL)
5992
0
  maxpagesize = link_info->maxpagesize;
5993
37
      else
5994
37
  maxpagesize = bed->maxpagesize;
5995
37
    }
5996
5997
  /* Sections must map to file offsets past the ELF file header.  */
5998
142
  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
142
  phdr_load_seg = NULL;
6003
194
  for (j = 0; j < alloc; j++)
6004
190
    {
6005
190
      m = sorted_seg_map[j];
6006
190
      if (m->p_type != PT_LOAD)
6007
107
  break;
6008
83
      if (m->includes_phdrs)
6009
31
  {
6010
31
    phdr_load_seg = m;
6011
31
    break;
6012
31
  }
6013
83
    }
6014
142
  if (phdr_load_seg == NULL)
6015
111
    off += actual * bed->s->sizeof_phdr;
6016
6017
648
  for (j = 0; j < alloc; j++)
6018
512
    {
6019
512
      asection **secpp;
6020
512
      bfd_vma off_adjust;  /* Octets.  */
6021
512
      bool no_contents;
6022
512
      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
512
      m = sorted_seg_map[j];
6030
512
      p = phdrs + m->idx;
6031
512
      p->p_type = m->p_type;
6032
512
      p->p_flags = m->p_flags;
6033
6034
512
      if (m->count == 0)
6035
230
  p->p_vaddr = m->p_vaddr_offset * opb;
6036
282
      else
6037
282
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6038
6039
512
      if (m->p_paddr_valid)
6040
447
  p->p_paddr = m->p_paddr;
6041
65
      else if (m->count == 0)
6042
13
  p->p_paddr = 0;
6043
52
      else
6044
52
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6045
6046
512
      align_pagesize = 0;
6047
512
      if (p->p_type == PT_LOAD
6048
101
    && (abfd->flags & D_PAGED) != 0)
6049
74
  {
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
74
    if (m->p_align_valid)
6059
31
      maxpagesize = m->p_align;
6060
43
    else if (bed->p_align != 0
6061
43
       && (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
43
      align_pagesize = bed->p_align;
6071
6072
74
    p->p_align = maxpagesize;
6073
74
  }
6074
438
      else if (m->p_align_valid)
6075
292
  p->p_align = m->p_align;
6076
146
      else if (m->count == 0)
6077
71
  p->p_align = 1 << bed->s->log_file_align;
6078
6079
512
      if (m == phdr_load_seg)
6080
31
  off += actual * bed->s->sizeof_phdr;
6081
6082
512
      no_contents = false;
6083
512
      off_adjust = 0;
6084
512
      if (p->p_type == PT_LOAD
6085
101
    && m->count > 0)
6086
96
  {
6087
96
    bfd_size_type align;  /* Bytes.  */
6088
96
    unsigned int align_power = 0;
6089
6090
96
    if (m->p_align_valid)
6091
29
      align = p->p_align;
6092
67
    else
6093
67
      {
6094
274
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6095
207
    {
6096
207
      unsigned int secalign;
6097
6098
207
      secalign = bfd_section_alignment (*secpp);
6099
207
      if (secalign > align_power)
6100
74
        align_power = secalign;
6101
207
    }
6102
67
        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
67
        if (align > bed->p_align)
6107
25
    align_pagesize = 0;
6108
67
        if (align < maxpagesize)
6109
40
    align = maxpagesize;
6110
27
        else
6111
27
    {
6112
      /* If a section requires alignment higher than the
6113
         maximum page size, set p_align to the section
6114
         alignment.  */
6115
27
      if ((abfd->flags & D_PAGED) != 0)
6116
3
        p->p_align = align;
6117
27
    }
6118
67
      }
6119
6120
564
    for (i = 0; i < m->count; i++)
6121
468
      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
33
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6126
6127
    /* Find out whether this segment contains any loadable
6128
       sections.  */
6129
96
    no_contents = true;
6130
109
    for (i = 0; i < m->count; i++)
6131
103
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6132
90
        {
6133
90
    no_contents = false;
6134
90
    break;
6135
90
        }
6136
6137
96
    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
96
    if (j != 0
6143
57
        && (abfd->flags & D_PAGED) != 0
6144
40
        && 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
96
    off += off_adjust;
6150
96
    if (no_contents)
6151
6
      {
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
6
      }
6161
90
    else
6162
90
      off_adjust = 0;
6163
96
  }
6164
      /* Make sure the .dynamic section is the first section in the
6165
   PT_DYNAMIC segment.  */
6166
416
      else if (p->p_type == PT_DYNAMIC
6167
29
         && m->count > 1
6168
1
         && ! streq (m->sections[0]->name, ".dynamic"))
6169
0
  {
6170
0
    _bfd_error_handler
6171
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6172
0
         " is not the .dynamic section"),
6173
0
       abfd);
6174
0
    bfd_set_error (bfd_error_bad_value);
6175
0
    return false;
6176
0
  }
6177
      /* Set the note section type to SHT_NOTE.  */
6178
416
      else if (p->p_type == PT_NOTE)
6179
75
  for (i = 0; i < m->count; i++)
6180
44
    elf_section_type (m->sections[i]) = SHT_NOTE;
6181
6182
512
      if (m->includes_filehdr)
6183
51
  {
6184
51
    if (!m->p_flags_valid)
6185
2
      p->p_flags |= PF_R;
6186
51
    p->p_filesz = bed->s->sizeof_ehdr;
6187
51
    p->p_memsz = bed->s->sizeof_ehdr;
6188
51
    if (p->p_type == PT_LOAD)
6189
28
      {
6190
28
        if (m->count > 0)
6191
25
    {
6192
25
      if (p->p_vaddr < (bfd_vma) off
6193
24
          || (!m->p_paddr_valid
6194
2
        && 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
2
        p->p_paddr -= off;
6206
24
    }
6207
28
      }
6208
23
    else if (sorted_seg_map[0]->includes_filehdr)
6209
13
      {
6210
13
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6211
13
        p->p_vaddr = filehdr->p_vaddr;
6212
13
        if (!m->p_paddr_valid)
6213
6
    p->p_paddr = filehdr->p_paddr;
6214
13
      }
6215
51
  }
6216
6217
511
      if (m->includes_phdrs)
6218
57
  {
6219
57
    if (!m->p_flags_valid)
6220
2
      p->p_flags |= PF_R;
6221
57
    p->p_filesz += actual * bed->s->sizeof_phdr;
6222
57
    p->p_memsz += actual * bed->s->sizeof_phdr;
6223
57
    if (!m->includes_filehdr)
6224
25
      {
6225
25
        if (p->p_type == PT_LOAD)
6226
3
    {
6227
3
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6228
3
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6229
3
      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
3
    }
6236
22
        else if (phdr_load_seg != NULL)
6237
17
    {
6238
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6239
         sorted segments so that phdr_load_seg will
6240
         already be set by the code immediately above.  */
6241
17
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6242
17
      bfd_vma phdr_off = 0;  /* Octets.  */
6243
17
      if (phdr_load_seg->includes_filehdr)
6244
15
        phdr_off = bed->s->sizeof_ehdr;
6245
17
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6246
17
      if (!m->p_paddr_valid)
6247
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6248
17
      p->p_offset = phdr->p_offset + phdr_off;
6249
17
    }
6250
5
        else
6251
5
    p->p_offset = bed->s->sizeof_ehdr;
6252
25
      }
6253
57
  }
6254
6255
511
      if (p->p_type == PT_LOAD
6256
411
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6257
105
  {
6258
105
    if (!m->includes_filehdr && !m->includes_phdrs)
6259
75
      {
6260
75
        p->p_offset = off;
6261
75
        if (no_contents)
6262
6
    {
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
6
      bfd_size_type align = maxpagesize;
6267
6
      if (align < p->p_align)
6268
0
        align = p->p_align;
6269
6
      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
6
      p->p_offset = (off + align - 1) % align + 1;
6275
6
    }
6276
75
      }
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
105
  }
6287
6288
511
      if (align_pagesize)
6289
42
  p->p_align = align_pagesize;
6290
6291
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6292
   maps.  Set filepos for sections in PT_LOAD segments, and in
6293
   core files, for sections in PT_NOTE segments.
6294
   assign_file_positions_for_non_load_sections will set filepos
6295
   for other sections and update p_filesz for other segments.  */
6296
1.45k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6297
944
  {
6298
944
    asection *sec;
6299
944
    bfd_size_type align;
6300
944
    Elf_Internal_Shdr *this_hdr;
6301
6302
944
    sec = *secpp;
6303
944
    this_hdr = &elf_section_data (sec)->this_hdr;
6304
944
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6305
6306
944
    if ((p->p_type == PT_LOAD
6307
480
         || p->p_type == PT_DYNAMIC
6308
460
         || p->p_type == PT_TLS)
6309
485
        && (this_hdr->sh_type != SHT_NOBITS
6310
33
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6311
33
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6312
0
        || p->p_type == PT_TLS))))
6313
485
      {
6314
485
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6315
485
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6316
485
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6317
485
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6318
6319
485
        if (adjust != 0
6320
239
      && (s_start < p_end
6321
172
          || p_end < p_start))
6322
67
    {
6323
67
      _bfd_error_handler
6324
        /* xgettext:c-format */
6325
67
        (_("%pB: section %pA lma %#" PRIx64
6326
67
           " adjusted to %#" PRIx64),
6327
67
         abfd, sec, (uint64_t) s_start / opb,
6328
67
         (uint64_t) p_end / opb);
6329
67
      adjust = 0;
6330
67
      sec->lma = p_end / opb;
6331
67
    }
6332
485
        p->p_memsz += adjust;
6333
6334
485
        if (p->p_type == PT_LOAD)
6335
464
    {
6336
464
      if (this_hdr->sh_type != SHT_NOBITS)
6337
431
        {
6338
431
          off_adjust = 0;
6339
431
          if (p->p_filesz + adjust < p->p_memsz)
6340
7
      {
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
7
        adjust = p->p_memsz - p->p_filesz;
6349
7
      }
6350
431
        }
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
464
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6361
442
        {
6362
442
          off += adjust;
6363
442
          if (this_hdr->sh_type == SHT_NOBITS)
6364
11
      off_adjust += adjust;
6365
442
        }
6366
464
    }
6367
485
        if (this_hdr->sh_type != SHT_NOBITS)
6368
452
    p->p_filesz += adjust;
6369
485
      }
6370
6371
944
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6372
4
      {
6373
        /* The section at i == 0 is the one that actually contains
6374
     everything.  */
6375
4
        if (i == 0)
6376
1
    {
6377
1
      this_hdr->sh_offset = sec->filepos = off;
6378
1
      off += this_hdr->sh_size;
6379
1
      p->p_filesz = this_hdr->sh_size;
6380
1
      p->p_memsz = 0;
6381
1
      p->p_align = 1;
6382
1
    }
6383
3
        else
6384
3
    {
6385
      /* The rest are fake sections that shouldn't be written.  */
6386
3
      sec->filepos = 0;
6387
3
      sec->size = 0;
6388
3
      sec->flags = 0;
6389
3
      continue;
6390
3
    }
6391
4
      }
6392
940
    else
6393
940
      {
6394
940
        if (this_hdr->sh_type == SHT_NOBITS
6395
63
      && (this_hdr->sh_flags & SHF_TLS) != 0
6396
0
      && this_hdr->sh_offset == 0)
6397
0
    {
6398
      /* Set sh_offset for .tbss sections to their nominal
6399
         offset after aligning.  They are not loaded from
6400
         disk so the value doesn't really matter, except
6401
         when the .tbss section is the first one in a
6402
         PT_TLS segment.  In that case it sets the
6403
         p_offset for the PT_TLS segment, which according
6404
         to the ELF gABI ought to satisfy
6405
         p_offset % p_align == p_vaddr % p_align.  */
6406
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6407
0
                off, align);
6408
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
6409
0
    }
6410
940
        else if (p->p_type == PT_LOAD)
6411
464
    {
6412
464
      this_hdr->sh_offset = sec->filepos = off;
6413
464
      if (this_hdr->sh_type != SHT_NOBITS)
6414
431
        off += this_hdr->sh_size;
6415
464
    }
6416
6417
940
        if (this_hdr->sh_type != SHT_NOBITS)
6418
877
    {
6419
877
      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
877
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6424
700
        p->p_memsz += this_hdr->sh_size;
6425
877
    }
6426
63
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6427
63
    {
6428
63
      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
63
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6434
63
        p->p_memsz += this_hdr->sh_size;
6435
63
    }
6436
6437
940
        if (align > p->p_align
6438
253
      && !m->p_align_valid
6439
93
      && (p->p_type != PT_LOAD
6440
28
          || (abfd->flags & D_PAGED) == 0))
6441
93
    p->p_align = align;
6442
940
      }
6443
6444
941
    if (!m->p_flags_valid)
6445
31
      {
6446
31
        p->p_flags |= PF_R;
6447
31
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6448
9
    p->p_flags |= PF_X;
6449
31
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6450
10
    p->p_flags |= PF_W;
6451
31
      }
6452
941
  }
6453
6454
511
      off -= off_adjust;
6455
6456
      /* PR ld/20815 - Check that the program header segment, if
6457
   present, will be loaded into memory.  */
6458
511
      if (p->p_type == PT_PHDR
6459
28
    && phdr_load_seg == NULL
6460
5
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6461
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6462
5
  {
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
5
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6467
5
        " by LOAD segment"),
6468
5
            abfd);
6469
5
    if (link_info == NULL)
6470
5
      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
506
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6479
73
  {
6480
73
    bool check_vma = true;
6481
6482
429
    for (i = 1; i < m->count; i++)
6483
356
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6484
2
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6485
2
               ->this_hdr), p) != 0
6486
1
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6487
1
               ->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
500
    for (i = 0; i < m->count; i++)
6495
427
      {
6496
427
        Elf_Internal_Shdr *this_hdr;
6497
427
        asection *sec;
6498
6499
427
        sec = m->sections[i];
6500
427
        this_hdr = &(elf_section_data(sec)->this_hdr);
6501
427
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6502
3
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6503
3
    {
6504
3
      _bfd_error_handler
6505
        /* xgettext:c-format */
6506
3
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6507
3
         abfd, sec, m->idx);
6508
3
      print_segment_map (m);
6509
3
    }
6510
427
      }
6511
73
  }
6512
506
    }
6513
6514
136
  elf_next_file_pos (abfd) = off;
6515
6516
136
  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
136
  return true;
6567
142
}
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
87
{
6576
87
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6577
0
    return false;
6578
6579
87
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6580
87
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6581
87
  Elf_Internal_Shdr **headerp;
6582
6583
195
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6584
195
    {
6585
195
      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
195
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6590
87
    && header->sh_type != SHT_NOBITS
6591
87
    && header->sh_type != SHT_NOTE)
6592
87
  return false;
6593
195
    }
6594
6595
0
  return true;
6596
87
}
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
250
{
6605
250
  elf_backend_data *bed = get_elf_backend_data (abfd);
6606
250
  Elf_Internal_Shdr **i_shdrpp;
6607
250
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6608
250
  Elf_Internal_Phdr *phdrs;
6609
250
  Elf_Internal_Phdr *p;
6610
250
  struct elf_segment_map *m;
6611
250
  file_ptr off;
6612
250
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6613
250
  bfd_vma maxpagesize;
6614
6615
250
  if (link_info != NULL)
6616
0
    maxpagesize = link_info->maxpagesize;
6617
250
  else
6618
250
    maxpagesize = bed->maxpagesize;
6619
250
  i_shdrpp = elf_elfsections (abfd);
6620
250
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6621
250
  off = elf_next_file_pos (abfd);
6622
1.48k
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6623
1.23k
    {
6624
1.23k
      Elf_Internal_Shdr *hdr;
6625
1.23k
      bfd_vma align;
6626
6627
1.23k
      hdr = *hdrpp;
6628
1.23k
      if (hdr->bfd_section != NULL
6629
931
    && (hdr->bfd_section->filepos != 0
6630
503
        || (hdr->sh_type == SHT_NOBITS
6631
16
      && hdr->contents == NULL)))
6632
444
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6633
787
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6634
89
  {
6635
89
    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
87
        && ! is_debuginfo_file (abfd))
6642
87
      _bfd_error_handler
6643
        /* xgettext:c-format */
6644
87
        (_("%pB: warning: allocated section `%s' not in segment"),
6645
87
         abfd,
6646
87
         (hdr->bfd_section == NULL
6647
87
    ? "*unknown*"
6648
87
    : hdr->bfd_section->name));
6649
    /* We don't need to page align empty sections.  */
6650
89
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6651
77
      align = maxpagesize;
6652
12
    else
6653
12
      align = hdr->sh_addralign & -hdr->sh_addralign;
6654
89
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6655
89
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6656
89
                 bed->s->log_file_align);
6657
89
  }
6658
698
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6659
21
    && hdr->bfd_section == NULL)
6660
         /* We don't know the offset of these sections yet:
6661
      their size has not been decided.  */
6662
680
         || (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
680
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6667
664
         || (elf_symtab_shndx_list (abfd) != NULL
6668
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6669
664
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6670
648
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6671
300
  hdr->sh_offset = -1;
6672
398
      else
6673
398
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6674
1.23k
    }
6675
250
  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
250
  phdrs = elf_tdata (abfd)->phdr;
6680
708
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6681
468
    {
6682
468
      if (p->p_type == PT_GNU_RELRO)
6683
14
  {
6684
14
    bfd_vma start, end;  /* Bytes.  */
6685
14
    bool ok;
6686
6687
14
    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
14
    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
4
    else
6703
4
      {
6704
4
        start = 0;
6705
4
        end = 0;
6706
4
      }
6707
6708
14
    ok = false;
6709
14
    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
56
       lm != NULL;
6719
46
       lm = lm->next, lp++)
6720
52
    {
6721
52
      if (lp->p_type == PT_LOAD
6722
16
          && lm->count != 0
6723
15
          && (lm->sections[lm->count - 1]->vma
6724
15
        + (!IS_TBSS (lm->sections[lm->count - 1])
6725
15
           ? lm->sections[lm->count - 1]->size / opb
6726
15
           : 0)) > start
6727
6
          && lm->sections[0]->vma < end)
6728
6
        break;
6729
52
    }
6730
6731
10
        if (lm != NULL)
6732
6
    {
6733
      /* Find the section starting the RELRO segment.  */
6734
6
      for (i = 0; i < lm->count; i++)
6735
6
        {
6736
6
          asection *s = lm->sections[i];
6737
6
          if (s->vma >= start
6738
6
        && s->vma < end
6739
6
        && s->size != 0)
6740
6
      break;
6741
6
        }
6742
6743
6
      if (i < lm->count)
6744
6
        {
6745
6
          p->p_vaddr = lm->sections[i]->vma * opb;
6746
6
          p->p_paddr = lm->sections[i]->lma * opb;
6747
6
          p->p_offset = lm->sections[i]->filepos;
6748
6
          p->p_memsz = end * opb - p->p_vaddr;
6749
6
          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
6
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6758
1
      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
6
          if (!m->p_align_valid)
6767
0
      p->p_align = 1;
6768
6
          if (!m->p_flags_valid)
6769
0
      p->p_flags = PF_R;
6770
6
          ok = true;
6771
6
        }
6772
6
    }
6773
10
      }
6774
6775
14
    if (!ok)
6776
8
      {
6777
8
        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
8
        memset (p, 0, sizeof *p);
6783
8
      }
6784
14
  }
6785
454
      else if (p->p_type == PT_GNU_STACK)
6786
9
  {
6787
9
    if (m->p_size_valid)
6788
7
      p->p_memsz = m->p_size;
6789
9
  }
6790
445
      else if (m->count != 0)
6791
240
  {
6792
240
    unsigned int i;
6793
6794
240
    if (p->p_type != PT_LOAD
6795
150
        && (p->p_type != PT_NOTE
6796
22
      || bfd_get_format (abfd) != bfd_core))
6797
149
      {
6798
        /* A user specified segment layout may include a PHDR
6799
     segment that overlaps with a LOAD segment...  */
6800
149
        if (p->p_type == PT_PHDR)
6801
1
    {
6802
1
      m->count = 0;
6803
1
      continue;
6804
1
    }
6805
6806
148
        if (m->includes_filehdr || m->includes_phdrs)
6807
10
    {
6808
      /* PR 17512: file: 2195325e.  */
6809
10
      _bfd_error_handler
6810
10
        (_("%pB: error: non-load segment %d includes file header "
6811
10
           "and/or program header"),
6812
10
         abfd, (int) (p - phdrs));
6813
10
      return false;
6814
10
    }
6815
6816
138
        p->p_filesz = 0;
6817
138
        p->p_offset = m->sections[0]->filepos;
6818
142
        for (i = m->count; i-- != 0;)
6819
138
    {
6820
138
      asection *sect = m->sections[i];
6821
138
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6822
138
      if (hdr->sh_type != SHT_NOBITS)
6823
134
        {
6824
134
          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
134
          if (p->p_type == PT_NOTE
6828
21
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6829
20
      p->p_memsz = p->p_filesz;
6830
134
          break;
6831
134
        }
6832
138
    }
6833
138
      }
6834
240
  }
6835
468
    }
6836
6837
240
  return true;
6838
250
}
6839
6840
static elf_section_list *
6841
find_section_in_list (unsigned int i, elf_section_list * list)
6842
356
{
6843
357
  for (;list != NULL; list = list->next)
6844
3
    if (list->ndx == i)
6845
2
      break;
6846
356
  return list;
6847
356
}
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
329
{
6868
329
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6869
329
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6870
329
  elf_backend_data *bed = get_elf_backend_data (abfd);
6871
329
  unsigned int alloc;
6872
6873
329
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6874
267
      && bfd_get_format (abfd) != bfd_core)
6875
71
    {
6876
71
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6877
71
      unsigned int num_sec = elf_numsections (abfd);
6878
71
      Elf_Internal_Shdr **hdrpp;
6879
71
      unsigned int i;
6880
71
      file_ptr off;
6881
6882
      /* Start after the ELF header.  */
6883
71
      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
681
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6889
610
  {
6890
610
    Elf_Internal_Shdr *hdr;
6891
6892
610
    hdr = *hdrpp;
6893
610
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6894
164
         && hdr->bfd_section == NULL)
6895
        /* Do not assign offsets for these sections yet: we don't know
6896
     their sizes.  */
6897
463
        || (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
463
        || i == elf_onesymtab (abfd)
6902
444
        || (elf_symtab_shndx_list (abfd) != NULL
6903
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6904
444
        || i == elf_strtab_sec (abfd)
6905
425
        || i == elf_shstrtab_sec (abfd))
6906
256
      {
6907
256
        hdr->sh_offset = -1;
6908
256
      }
6909
354
    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
354
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6915
354
                   bed->s->log_file_align);
6916
610
  }
6917
6918
71
      elf_next_file_pos (abfd) = off;
6919
71
      elf_program_header_size (abfd) = 0;
6920
71
    }
6921
258
  else
6922
258
    {
6923
      /* Assign file positions for the loaded sections based on the
6924
   assignment of sections to segments.  */
6925
258
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6926
8
  return false;
6927
6928
      /* And for non-load sections.  */
6929
250
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6930
10
  return false;
6931
250
    }
6932
6933
311
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6934
0
    return false;
6935
6936
  /* Write out the program headers.  */
6937
311
  alloc = i_ehdrp->e_phnum;
6938
311
  if (alloc != 0)
6939
126
    {
6940
126
      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
126
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7002
126
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7003
0
  return false;
7004
126
    }
7005
7006
311
  return true;
7007
311
}
7008
7009
bool
7010
_bfd_elf_init_file_header (bfd *abfd,
7011
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7012
329
{
7013
329
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7014
329
  struct elf_strtab_hash *shstrtab;
7015
329
  elf_backend_data *bed = get_elf_backend_data (abfd);
7016
7017
329
  i_ehdrp = elf_elfheader (abfd);
7018
7019
329
  shstrtab = _bfd_elf_strtab_init ();
7020
329
  if (shstrtab == NULL)
7021
0
    return false;
7022
7023
329
  elf_shstrtab (abfd) = shstrtab;
7024
7025
329
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7026
329
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7027
329
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7028
329
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7029
7030
329
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7031
329
  i_ehdrp->e_ident[EI_DATA] =
7032
329
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7033
329
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7034
7035
329
  if ((abfd->flags & DYNAMIC) != 0)
7036
48
    i_ehdrp->e_type = ET_DYN;
7037
281
  else if ((abfd->flags & EXEC_P) != 0)
7038
14
    i_ehdrp->e_type = ET_EXEC;
7039
267
  else if (bfd_get_format (abfd) == bfd_core)
7040
196
    i_ehdrp->e_type = ET_CORE;
7041
71
  else
7042
71
    i_ehdrp->e_type = ET_REL;
7043
7044
329
  switch (bfd_get_arch (abfd))
7045
329
    {
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
329
    default:
7059
329
      i_ehdrp->e_machine = bed->elf_machine_code;
7060
329
    }
7061
7062
329
  i_ehdrp->e_version = bed->s->ev_current;
7063
329
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7064
7065
  /* No program header, for now.  */
7066
329
  i_ehdrp->e_phoff = 0;
7067
329
  i_ehdrp->e_phentsize = 0;
7068
329
  i_ehdrp->e_phnum = 0;
7069
7070
  /* Each bfd section is section header entry.  */
7071
329
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7072
329
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7073
7074
329
  elf_tdata (abfd)->symtab_hdr.sh_name =
7075
329
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7076
329
  elf_tdata (abfd)->strtab_hdr.sh_name =
7077
329
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7078
329
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7079
329
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7080
329
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7081
329
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7082
329
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7083
0
    return false;
7084
7085
329
  return true;
7086
329
}
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
311
{
7098
311
  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
311
  return true;
7118
311
}
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
308
{
7126
308
  file_ptr off;
7127
308
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7128
308
  Elf_Internal_Shdr *shdrp;
7129
308
  Elf_Internal_Ehdr *i_ehdrp;
7130
308
  elf_backend_data *bed = get_elf_backend_data (abfd);
7131
7132
  /* Skip non-load sections without section header.  */
7133
308
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7134
0
    return true;
7135
7136
308
  off = elf_next_file_pos (abfd);
7137
7138
308
  shdrpp = elf_elfsections (abfd);
7139
308
  end_shdrpp = shdrpp + elf_numsections (abfd);
7140
2.06k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7141
1.75k
    {
7142
1.75k
      shdrp = *shdrpp;
7143
1.75k
      if (shdrp->sh_offset == -1)
7144
473
  {
7145
473
    asection *sec = shdrp->bfd_section;
7146
473
    if (sec == NULL
7147
0
        || shdrp->sh_type == SHT_REL
7148
0
        || shdrp->sh_type == SHT_RELA)
7149
473
      ;
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
473
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7199
473
      (abfd->flags & (EXEC_P | DYNAMIC))
7200
400
      || bfd_get_format (abfd) == bfd_core,
7201
473
      bed->s->log_file_align);
7202
473
  }
7203
1.75k
    }
7204
7205
  /* Place section name section after DWARF debug sections have been
7206
     compressed.  */
7207
308
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7208
308
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7209
308
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7210
308
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7211
7212
  /* Place the section headers.  */
7213
308
  i_ehdrp = elf_elfheader (abfd);
7214
308
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7215
308
  i_ehdrp->e_shoff = off;
7216
308
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7217
308
  elf_next_file_pos (abfd) = off;
7218
7219
308
  return true;
7220
308
}
7221
7222
bool
7223
_bfd_elf_write_object_contents (bfd *abfd)
7224
313
{
7225
313
  elf_backend_data *bed = get_elf_backend_data (abfd);
7226
313
  Elf_Internal_Shdr **i_shdrp;
7227
313
  bool failed;
7228
313
  unsigned int count, num_sec;
7229
313
  struct elf_obj_tdata *t;
7230
7231
313
  if (! abfd->output_has_begun
7232
240
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7233
5
    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
308
  else if (abfd->direction == both_direction)
7242
0
    {
7243
0
      BFD_ASSERT (abfd->output_has_begun);
7244
0
      return true;
7245
0
    }
7246
7247
308
  i_shdrp = elf_elfsections (abfd);
7248
7249
308
  failed = false;
7250
308
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7251
308
  if (failed)
7252
0
    return false;
7253
7254
308
  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
308
  num_sec = elf_numsections (abfd);
7259
2.06k
  for (count = 1; count < num_sec; count++)
7260
1.75k
    {
7261
      /* Don't set the sh_name field without section header.  */
7262
1.75k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7263
1.75k
  i_shdrp[count]->sh_name
7264
1.75k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7265
1.75k
            i_shdrp[count]->sh_name);
7266
1.75k
      if (bed->elf_backend_section_processing)
7267
236
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7268
0
    return false;
7269
1.75k
      if (i_shdrp[count]->contents)
7270
210
  {
7271
210
    bfd_size_type amt = i_shdrp[count]->sh_size;
7272
7273
210
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7274
210
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7275
0
      return false;
7276
210
  }
7277
1.75k
    }
7278
7279
  /* Write out the section header names.  */
7280
308
  t = elf_tdata (abfd);
7281
308
  if (elf_shstrtab (abfd) != NULL
7282
308
      && t->shstrtab_hdr.sh_offset != -1
7283
308
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7284
287
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7285
21
    return false;
7286
7287
287
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7288
0
    return false;
7289
7290
287
  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
287
  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
287
  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
287
  return true;
7302
287
}
7303
7304
bool
7305
_bfd_elf_write_corefile_contents (bfd *abfd)
7306
187
{
7307
  /* Hopefully this can be done just like an object file.  */
7308
187
  return _bfd_elf_write_object_contents (abfd);
7309
187
}
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.15k
{
7316
1.15k
  elf_backend_data *bed;
7317
1.15k
  unsigned int sec_index;
7318
7319
1.15k
  if (elf_section_data (asect) != NULL
7320
617
      && elf_section_data (asect)->this_idx != 0)
7321
617
    return elf_section_data (asect)->this_idx;
7322
7323
536
  if (bfd_is_abs_section (asect))
7324
0
    sec_index = SHN_ABS;
7325
536
  else if (bfd_is_com_section (asect))
7326
3
    sec_index = SHN_COMMON;
7327
533
  else if (bfd_is_und_section (asect))
7328
532
    sec_index = SHN_UNDEF;
7329
1
  else
7330
1
    sec_index = SHN_BAD;
7331
7332
536
  bed = get_elf_backend_data (abfd);
7333
536
  if (bed->elf_backend_section_from_bfd_section)
7334
521
    {
7335
521
      int retval = sec_index;
7336
7337
521
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7338
3
  return retval;
7339
521
    }
7340
7341
533
  if (sec_index == SHN_BAD)
7342
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7343
7344
533
  return sec_index;
7345
536
}
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
522
{
7353
522
  asymbol *asym_ptr = *asym_ptr_ptr;
7354
522
  int idx;
7355
522
  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
522
  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
522
  idx = asym_ptr->udata.i;
7378
7379
522
  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
522
  return idx;
7402
522
}
7403
7404
static inline bfd_vma
7405
segment_size (Elf_Internal_Phdr *segment)
7406
5.87k
{
7407
5.87k
  return (segment->p_memsz > segment->p_filesz
7408
5.87k
    ? segment->p_memsz : segment->p_filesz);
7409
5.87k
}
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
126
{
7416
126
  return start + segment_size (segment);
7417
126
}
7418
7419
static inline bfd_size_type
7420
section_size (asection *section, Elf_Internal_Phdr *segment)
7421
5.76k
{
7422
5.76k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7423
1.54k
      || (section->flags & SEC_THREAD_LOCAL) == 0
7424
0
      || segment->p_type == PT_TLS)
7425
5.76k
    return section->size;
7426
0
  return 0;
7427
5.76k
}
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
5.23k
{
7437
5.23k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7438
5.23k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7439
5.23k
  bfd_vma octet;
7440
5.23k
  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
5.23k
  return (octet >= seg_addr
7447
3.35k
    && segment_size (segment) >= section_size (section, segment)
7448
2.39k
    && (octet - seg_addr
7449
2.39k
        <= segment_size (segment) - section_size (section, segment)));
7450
5.23k
}
7451
7452
/* Handle PT_NOTE segment.  */
7453
static bool
7454
is_note (asection *s, Elf_Internal_Phdr *p)
7455
4.19k
{
7456
4.19k
  return (p->p_type == PT_NOTE
7457
242
    && elf_section_type (s) == SHT_NOTE
7458
5
    && (ufile_ptr) s->filepos >= p->p_offset
7459
4
    && p->p_filesz >= s->size
7460
4
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7461
4.19k
}
7462
7463
/* Rewrite program header information.  */
7464
7465
static bool
7466
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7467
19
{
7468
19
  Elf_Internal_Ehdr *iehdr;
7469
19
  struct elf_segment_map *map;
7470
19
  struct elf_segment_map *map_first;
7471
19
  struct elf_segment_map **pointer_to_map;
7472
19
  Elf_Internal_Phdr *segment;
7473
19
  asection *section;
7474
19
  unsigned int i;
7475
19
  unsigned int num_segments;
7476
19
  bool phdr_included = false;
7477
19
  bool p_paddr_valid;
7478
19
  struct elf_segment_map *phdr_adjust_seg = NULL;
7479
19
  unsigned int phdr_adjust_num = 0;
7480
19
  elf_backend_data *bed;
7481
19
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7482
7483
19
  bed = get_elf_backend_data (ibfd);
7484
19
  iehdr = elf_elfheader (ibfd);
7485
7486
19
  map_first = NULL;
7487
19
  pointer_to_map = &map_first;
7488
7489
19
  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
19
#define IS_SOLARIS_PT_INTERP(p, s)          \
7495
336
  (p->p_vaddr == 0              \
7496
336
   && p->p_paddr == 0              \
7497
336
   && p->p_memsz == 0              \
7498
336
   && p->p_filesz > 0              \
7499
336
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7500
336
   && s->size > 0              \
7501
336
   && (bfd_vma) s->filepos >= p->p_offset        \
7502
336
   && ((bfd_vma) s->filepos + s->size         \
7503
6
       <= 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
19
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7519
5.56k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7520
4.89k
          segment->p_vaddr, opb, !paddr_valid)    \
7521
4.89k
     && (section->flags & SEC_ALLOC) != 0)       \
7522
4.89k
    || is_note (section, segment))         \
7523
4.89k
   && segment->p_type != PT_GNU_STACK          \
7524
4.89k
   && (segment->p_type != PT_TLS          \
7525
694
       || (section->flags & SEC_THREAD_LOCAL))        \
7526
4.89k
   && (segment->p_type == PT_LOAD          \
7527
694
       || segment->p_type == PT_TLS          \
7528
694
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7529
4.89k
   && (segment->p_type != PT_DYNAMIC          \
7530
690
       || section_size (section, segment) > 0        \
7531
690
       || (segment->p_paddr            \
7532
0
     ? segment->p_paddr != section->lma * (opb)      \
7533
0
     : segment->p_vaddr != section->vma * (opb))      \
7534
690
       || (streq (bfd_section_name (section), ".dynamic")))    \
7535
5.56k
   && (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
19
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7540
677
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7541
677
   && section->output_section != NULL)
7542
7543
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7544
19
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7545
190
  (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
19
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7554
54
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7555
54
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7556
54
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7557
16
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7558
7559
  /* Initialise the segment mark field, and discard stupid alignment.  */
7560
361
  for (section = ibfd->sections; section != NULL; section = section->next)
7561
342
    {
7562
342
      asection *o = section->output_section;
7563
342
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7564
0
  o->alignment_power = 0;
7565
342
      section->segment_mark = false;
7566
342
    }
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
19
  p_paddr_valid = false;
7573
19
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7574
31
       i < num_segments;
7575
19
       i++, segment++)
7576
28
    if (segment->p_paddr != 0)
7577
16
      {
7578
16
  p_paddr_valid = true;
7579
16
  break;
7580
16
      }
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
19
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7587
237
       i < num_segments;
7588
218
       i++, segment++)
7589
218
    {
7590
218
      unsigned int j;
7591
218
      Elf_Internal_Phdr *segment2;
7592
7593
218
      if (segment->p_type == PT_INTERP)
7594
348
  for (section = ibfd->sections; section; section = section->next)
7595
336
    if (IS_SOLARIS_PT_INTERP (segment, section))
7596
3
      {
7597
        /* Mininal change so that the normal section to segment
7598
     assignment code will work.  */
7599
3
        segment->p_vaddr = section->vma * opb;
7600
3
        break;
7601
3
      }
7602
7603
218
      if (segment->p_type != PT_LOAD)
7604
169
  {
7605
    /* Remove PT_GNU_RELRO segment.  */
7606
169
    if (segment->p_type == PT_GNU_RELRO)
7607
7
      segment->p_type = PT_NULL;
7608
169
    continue;
7609
169
  }
7610
7611
      /* Determine if this segment overlaps any previous segments.  */
7612
394
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7613
346
  {
7614
346
    bfd_signed_vma extra_length;
7615
7616
346
    if (segment2->p_type != PT_LOAD
7617
54
        || !SEGMENT_OVERLAPS (segment, segment2))
7618
343
      continue;
7619
7620
    /* Merge the two segments together.  */
7621
3
    if (segment2->p_vaddr < segment->p_vaddr)
7622
1
      {
7623
        /* Extend SEGMENT2 to include SEGMENT and then delete
7624
     SEGMENT.  */
7625
1
        extra_length = (segment_end (segment, segment->p_vaddr)
7626
1
            - segment_end (segment2, segment2->p_vaddr));
7627
7628
1
        if (extra_length > 0)
7629
0
    {
7630
0
      segment2->p_memsz += extra_length;
7631
0
      segment2->p_filesz += extra_length;
7632
0
    }
7633
7634
1
        segment->p_type = PT_NULL;
7635
7636
        /* Since we have deleted P we must restart the outer loop.  */
7637
1
        i = 0;
7638
1
        segment = elf_tdata (ibfd)->phdr;
7639
1
        break;
7640
1
      }
7641
2
    else
7642
2
      {
7643
        /* Extend SEGMENT to include SEGMENT2 and then delete
7644
     SEGMENT2.  */
7645
2
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7646
2
            - segment_end (segment, segment->p_vaddr));
7647
7648
2
        if (extra_length > 0)
7649
2
    {
7650
2
      segment->p_memsz += extra_length;
7651
2
      segment->p_filesz += extra_length;
7652
2
    }
7653
7654
2
        segment2->p_type = PT_NULL;
7655
2
      }
7656
3
  }
7657
49
    }
7658
7659
  /* The second scan attempts to assign sections to segments.  */
7660
19
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7661
218
       i < num_segments;
7662
199
       i++, segment++)
7663
199
    {
7664
199
      unsigned int section_count;
7665
199
      asection **sections;
7666
199
      asection *output_section;
7667
199
      unsigned int isec;
7668
199
      asection *matching_lma;
7669
199
      asection *suggested_lma;
7670
199
      unsigned int j;
7671
199
      size_t amt;
7672
199
      asection *first_section;
7673
7674
199
      if (segment->p_type == PT_NULL)
7675
34
  continue;
7676
7677
165
      first_section = NULL;
7678
      /* Compute how many sections might be placed into this segment.  */
7679
165
      for (section = ibfd->sections, section_count = 0;
7680
4.37k
     section != NULL;
7681
4.21k
     section = section->next)
7682
4.21k
  {
7683
    /* Find the first section in the input segment, which may be
7684
       removed from the corresponding output segment.   */
7685
4.21k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7686
345
      {
7687
345
        if (first_section == NULL)
7688
90
    first_section = section;
7689
345
        if (section->output_section != NULL)
7690
345
    ++section_count;
7691
345
      }
7692
4.21k
  }
7693
7694
      /* Allocate a segment map big enough to contain
7695
   all of the sections we have selected.  */
7696
165
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7697
165
      amt += section_count * sizeof (asection *);
7698
165
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7699
165
      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
165
      map->next = NULL;
7705
165
      map->p_type = segment->p_type;
7706
165
      map->p_flags = segment->p_flags;
7707
165
      map->p_flags_valid = 1;
7708
7709
165
      if (map->p_type == PT_LOAD
7710
42
    && (ibfd->flags & D_PAGED) != 0
7711
4
    && maxpagesize > 1
7712
4
    && 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
165
      if (!first_section || first_section->output_section != NULL)
7724
165
  {
7725
165
    map->p_paddr = segment->p_paddr;
7726
165
    map->p_paddr_valid = p_paddr_valid;
7727
165
  }
7728
7729
      /* Determine if this segment contains the ELF file header
7730
   and if it contains the program headers themselves.  */
7731
165
      map->includes_filehdr = (segment->p_offset == 0
7732
74
             && segment->p_filesz >= iehdr->e_ehsize);
7733
165
      map->includes_phdrs = 0;
7734
7735
165
      if (!phdr_included || segment->p_type != PT_LOAD)
7736
158
  {
7737
158
    map->includes_phdrs =
7738
158
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7739
82
       && (segment->p_offset + segment->p_filesz
7740
82
     >= ((bfd_vma) iehdr->e_phoff
7741
82
         + iehdr->e_phnum * iehdr->e_phentsize)));
7742
7743
158
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7744
7
      phdr_included = true;
7745
158
  }
7746
7747
165
      if (section_count == 0)
7748
75
  {
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
75
    if (segment->p_type == PT_LOAD
7758
2
        && !map->includes_phdrs
7759
2
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7760
      /* xgettext:c-format */
7761
2
      _bfd_error_handler
7762
2
        (_("%pB: warning: empty loadable segment detected"
7763
2
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7764
2
         ibfd, (uint64_t) segment->p_vaddr);
7765
7766
75
    map->p_vaddr_offset = segment->p_vaddr / opb;
7767
75
    map->count = 0;
7768
75
    *pointer_to_map = map;
7769
75
    pointer_to_map = &map->next;
7770
7771
75
    continue;
7772
75
  }
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
90
      amt = section_count * sizeof (asection *);
7803
90
      sections = (asection **) bfd_malloc (amt);
7804
90
      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
90
      isec = 0;
7814
90
      matching_lma = NULL;
7815
90
      suggested_lma = NULL;
7816
7817
90
      for (section = first_section, j = 0;
7818
677
     section != NULL;
7819
587
     section = section->next)
7820
677
  {
7821
677
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7822
345
      {
7823
345
        output_section = section->output_section;
7824
7825
345
        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
345
        if (!p_paddr_valid
7832
1
      && segment->p_vaddr != 0
7833
1
      && !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
345
        if (is_contained_by (output_section, segment, map->p_paddr,
7849
345
           0, opb, false)
7850
1
      || is_note (section, segment))
7851
345
    {
7852
345
      if (matching_lma == NULL
7853
255
          || output_section->lma < matching_lma->lma)
7854
118
        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
345
      map->sections[isec++] = output_section;
7860
345
    }
7861
0
        else if (suggested_lma == NULL)
7862
0
    suggested_lma = output_section;
7863
7864
345
        if (j == section_count)
7865
90
    break;
7866
345
      }
7867
677
  }
7868
7869
90
      BFD_ASSERT (j == section_count);
7870
7871
      /* Step Two: Adjust the physical address of the current segment,
7872
   if necessary.  */
7873
90
      if (isec == section_count)
7874
90
  {
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
90
    map->count = section_count;
7880
90
    *pointer_to_map = map;
7881
90
    pointer_to_map = &map->next;
7882
7883
90
    if (p_paddr_valid
7884
89
        && !bed->want_p_paddr_set_to_zero)
7885
89
      {
7886
89
        bfd_vma hdr_size = 0;
7887
89
        if (map->includes_filehdr)
7888
7
    hdr_size = iehdr->e_ehsize;
7889
89
        if (map->includes_phdrs)
7890
9
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7891
7892
        /* Account for padding before the first section in the
7893
     segment.  */
7894
89
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7895
89
             - matching_lma->lma);
7896
89
      }
7897
7898
90
    free (sections);
7899
90
    continue;
7900
90
  }
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
19
  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
19
  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
19
#undef IS_SOLARIS_PT_INTERP
8092
19
#undef IS_SECTION_IN_INPUT_SEGMENT
8093
19
#undef INCLUDE_SECTION_IN_SEGMENT
8094
19
#undef SEGMENT_AFTER_SEGMENT
8095
19
#undef SEGMENT_OVERLAPS
8096
19
  return true;
8097
19
}
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
123
{
8104
123
  unsigned int i;
8105
123
  Elf_Internal_Phdr *segment;
8106
123
  unsigned int num_segments;
8107
123
  elf_backend_data *bed = get_elf_backend_data (abfd);
8108
123
  bfd_size_type maxpagesize = bed->maxpagesize;
8109
123
  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
123
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8114
123
    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
123
{
8138
123
  Elf_Internal_Ehdr *iehdr;
8139
123
  struct elf_segment_map *map;
8140
123
  struct elf_segment_map *map_first;
8141
123
  struct elf_segment_map **pointer_to_map;
8142
123
  Elf_Internal_Phdr *segment;
8143
123
  unsigned int i;
8144
123
  unsigned int num_segments;
8145
123
  bool phdr_included = false;
8146
123
  bool p_paddr_valid;
8147
123
  bool p_palign_valid;
8148
123
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8149
8150
123
  iehdr = elf_elfheader (ibfd);
8151
8152
123
  map_first = NULL;
8153
123
  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
123
  p_paddr_valid = false;
8158
123
  num_segments = elf_elfheader (ibfd)->e_phnum;
8159
123
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8160
176
       i < num_segments;
8161
123
       i++, segment++)
8162
162
    if (segment->p_paddr != 0)
8163
109
      {
8164
109
  p_paddr_valid = true;
8165
109
  break;
8166
109
      }
8167
8168
123
  p_palign_valid = elf_is_p_align_valid (ibfd);
8169
8170
123
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8171
505
       i < num_segments;
8172
382
       i++, segment++)
8173
382
    {
8174
382
      asection *section;
8175
382
      unsigned int section_count;
8176
382
      size_t amt;
8177
382
      Elf_Internal_Shdr *this_hdr;
8178
382
      asection *first_section = NULL;
8179
382
      asection *lowest_section;
8180
8181
      /* Compute how many sections are in this segment.  */
8182
382
      for (section = ibfd->sections, section_count = 0;
8183
4.46k
     section != NULL;
8184
4.07k
     section = section->next)
8185
4.07k
  {
8186
4.07k
    this_hdr = &(elf_section_data(section)->this_hdr);
8187
4.07k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8188
611
      {
8189
611
        if (first_section == NULL)
8190
187
    first_section = section;
8191
611
        section_count++;
8192
611
      }
8193
4.07k
  }
8194
8195
      /* Allocate a segment map big enough to contain
8196
   all of the sections we have selected.  */
8197
382
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8198
382
      amt += section_count * sizeof (asection *);
8199
382
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8200
382
      if (map == NULL)
8201
0
  return false;
8202
8203
      /* Initialize the fields of the output segment map with the
8204
   input segment.  */
8205
382
      map->next = NULL;
8206
382
      map->p_type = segment->p_type;
8207
382
      map->p_flags = segment->p_flags;
8208
382
      map->p_flags_valid = 1;
8209
382
      map->p_paddr = segment->p_paddr;
8210
382
      map->p_paddr_valid = p_paddr_valid;
8211
382
      map->p_align = segment->p_align;
8212
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8213
382
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8214
374
          || p_palign_valid);
8215
382
      map->p_vaddr_offset = 0;
8216
8217
382
      if (map->p_type == PT_GNU_RELRO
8218
364
    || map->p_type == PT_GNU_STACK)
8219
26
  {
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
26
    map->p_size = segment->p_memsz;
8227
26
    map->p_size_valid = 1;
8228
26
  }
8229
8230
      /* Determine if this segment contains the ELF file header
8231
   and if it contains the program headers themselves.  */
8232
382
      map->includes_filehdr = (segment->p_offset == 0
8233
121
             && segment->p_filesz >= iehdr->e_ehsize);
8234
8235
382
      map->includes_phdrs = 0;
8236
382
      if (! phdr_included || segment->p_type != PT_LOAD)
8237
365
  {
8238
365
    map->includes_phdrs =
8239
365
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8240
149
       && (segment->p_offset + segment->p_filesz
8241
149
     >= ((bfd_vma) iehdr->e_phoff
8242
149
         + iehdr->e_phnum * iehdr->e_phentsize)));
8243
8244
365
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8245
23
      phdr_included = true;
8246
365
  }
8247
8248
382
      lowest_section = NULL;
8249
382
      if (section_count != 0)
8250
187
  {
8251
187
    unsigned int isec = 0;
8252
8253
187
    for (section = first_section;
8254
646
         section != NULL;
8255
459
         section = section->next)
8256
646
      {
8257
646
        this_hdr = &(elf_section_data(section)->this_hdr);
8258
646
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8259
611
    {
8260
611
      map->sections[isec++] = section->output_section;
8261
611
      if ((section->flags & SEC_ALLOC) != 0)
8262
408
        {
8263
408
          bfd_vma seg_off;
8264
8265
408
          if (lowest_section == NULL
8266
287
        || section->lma < lowest_section->lma)
8267
127
      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
408
          if ((section->flags & SEC_LOAD) != 0)
8275
390
      seg_off = this_hdr->sh_offset - segment->p_offset;
8276
18
          else
8277
18
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8278
408
          if (section->lma * opb - segment->p_paddr != seg_off)
8279
12
      map->p_paddr_valid = false;
8280
408
        }
8281
611
      if (isec == section_count)
8282
187
        break;
8283
611
    }
8284
646
      }
8285
187
  }
8286
8287
382
      if (section_count == 0)
8288
195
  map->p_vaddr_offset = segment->p_vaddr / opb;
8289
187
      else if (map->p_paddr_valid)
8290
166
  {
8291
    /* Account for padding before the first section in the segment.  */
8292
166
    bfd_vma hdr_size = 0;
8293
166
    if (map->includes_filehdr)
8294
30
      hdr_size = iehdr->e_ehsize;
8295
166
    if (map->includes_phdrs)
8296
18
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8297
8298
166
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8299
166
         - (lowest_section ? lowest_section->lma : 0));
8300
166
  }
8301
8302
382
      map->count = section_count;
8303
382
      *pointer_to_map = map;
8304
382
      pointer_to_map = &map->next;
8305
382
    }
8306
8307
123
  elf_seg_map (obfd) = map_first;
8308
123
  return true;
8309
123
}
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
142
{
8317
142
  bfd_vma maxpagesize;
8318
8319
142
  if (elf_tdata (ibfd)->phdr == NULL)
8320
0
    return true;
8321
8322
142
  if (ibfd->xvec == obfd->xvec)
8323
139
    {
8324
      /* Check to see if any sections in the input BFD
8325
   covered by ELF program header have changed.  */
8326
139
      Elf_Internal_Phdr *segment;
8327
139
      asection * section;
8328
139
      asection * osec;
8329
139
      asection * prev;
8330
139
      unsigned int i, num_segments;
8331
139
      Elf_Internal_Shdr *this_hdr;
8332
139
      elf_backend_data *bed;
8333
8334
139
      bed = get_elf_backend_data (ibfd);
8335
8336
      /* Regenerate the segment map if p_paddr is set to 0.  */
8337
139
      if (bed->want_p_paddr_set_to_zero)
8338
1
  goto rewrite;
8339
8340
      /* Initialize the segment mark field.  */
8341
1.09k
      for (section = obfd->sections; section != NULL;
8342
952
     section = section->next)
8343
952
  section->segment_mark = false;
8344
8345
138
      num_segments = elf_elfheader (ibfd)->e_phnum;
8346
138
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8347
547
     i < num_segments;
8348
409
     i++, segment++)
8349
424
  {
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
424
    if (segment->p_paddr == 0
8355
141
        && segment->p_memsz == 0
8356
60
        && (segment->p_type == PT_INTERP
8357
60
      || segment->p_type == PT_DYNAMIC))
8358
0
      goto rewrite;
8359
8360
424
    for (section = ibfd->sections, prev = NULL;
8361
5.04k
         section != NULL; section = section->next)
8362
4.63k
      {
8363
        /* We mark the output section so that we know it comes
8364
     from the input BFD.  */
8365
4.63k
        osec = section->output_section;
8366
4.63k
        if (osec)
8367
4.63k
    osec->segment_mark = true;
8368
8369
        /* Check if this section is covered by the segment.  */
8370
4.63k
        this_hdr = &(elf_section_data(section)->this_hdr);
8371
4.63k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8372
721
    {
8373
      /* FIXME: Check if its output section is changed or
8374
         removed.  What else do we need to check?  */
8375
721
      if (osec == NULL
8376
721
          || section->flags != osec->flags
8377
721
          || section->lma != osec->lma
8378
721
          || section->vma != osec->vma
8379
721
          || section->size != osec->size
8380
721
          || section->rawsize != osec->rawsize
8381
721
          || 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
721
      if (prev != NULL
8388
392
          && section->flags & SEC_ALLOC
8389
333
          && section->lma - section->vma != prev->lma - prev->vma)
8390
15
        goto rewrite;
8391
8392
706
      if (section->flags & SEC_ALLOC)
8393
459
        prev = section;
8394
706
    }
8395
4.63k
      }
8396
424
  }
8397
8398
      /* Check to see if any output section do not come from the
8399
   input BFD.  */
8400
738
      for (section = obfd->sections; section != NULL;
8401
615
     section = section->next)
8402
615
  {
8403
615
    if (!section->segment_mark)
8404
0
      goto rewrite;
8405
615
    else
8406
615
      section->segment_mark = false;
8407
615
  }
8408
8409
123
      return copy_elf_program_header (ibfd, obfd);
8410
123
    }
8411
8412
19
 rewrite:
8413
19
  maxpagesize = 0;
8414
19
  if (ibfd->xvec == obfd->xvec)
8415
16
    {
8416
      /* When rewriting program header, set the output maxpagesize to
8417
   the maximum alignment of input PT_LOAD segments.  */
8418
16
      Elf_Internal_Phdr *segment;
8419
16
      unsigned int i;
8420
16
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8421
8422
16
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8423
212
     i < num_segments;
8424
196
     i++, segment++)
8425
196
  if (segment->p_type == PT_LOAD
8426
45
      && maxpagesize < segment->p_align)
8427
22
    {
8428
      /* PR 17512: file: f17299af.  */
8429
22
      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
22
      else
8435
22
        maxpagesize = segment->p_align;
8436
22
    }
8437
16
    }
8438
19
  if (maxpagesize == 0)
8439
3
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8440
8441
19
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8442
142
}
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.59k
{
8456
1.59k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8457
0
    return true;
8458
8459
1.59k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8460
1.59k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8461
1.59k
  if (link_info == NULL)
8462
1.59k
    {
8463
1.59k
      ohdr->sh_entsize = ihdr->sh_entsize;
8464
8465
1.59k
      if (ihdr->sh_type == SHT_SYMTAB
8466
1.58k
    || ihdr->sh_type == SHT_DYNSYM
8467
1.56k
    || ihdr->sh_type == SHT_GNU_verneed
8468
1.56k
    || ihdr->sh_type == SHT_GNU_verdef)
8469
25
  ohdr->sh_info = ihdr->sh_info;
8470
1.59k
    }
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.59k
  if (elf_section_type (osec) == SHT_PROGBITS
8477
1.18k
      || elf_section_type (osec) == SHT_NOTE
8478
1.11k
      || elf_section_type (osec) == SHT_NOBITS)
8479
510
    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.59k
  bool final_link = (link_info != NULL
8487
0
         && !bfd_link_relocatable (link_info));
8488
1.59k
  if (elf_section_type (osec) == SHT_NULL
8489
1.40k
      && (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.40k
    elf_section_type (osec) = elf_section_type (isec);
8494
8495
1.59k
  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.59k
  if (link_info != NULL || osec->flags != isec->flags)
8502
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8503
1.59k
  else
8504
1.59k
    {
8505
      /* Clear only flags which are set below or elsewhere.  */
8506
1.59k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8507
1.59k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8508
1.59k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8509
1.59k
            | SHF_COMPRESSED);
8510
1.59k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8511
31
  _bfd_error_handler
8512
31
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8513
31
     ibfd, isec,
8514
31
     (uint64_t) (elf_section_flags (osec)
8515
31
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8516
1.59k
    }
8517
8518
  /* Copy sh_info from input for mbind section.  */
8519
1.59k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8520
168
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8521
14
    elf_section_data (osec)->this_hdr.sh_info
8522
14
      = 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.59k
  if ((link_info == NULL
8529
0
       || !link_info->resolve_section_groups)
8530
1.59k
      && (elf_sec_group (isec) == NULL
8531
39
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8532
1.59k
    {
8533
1.59k
      if (elf_section_flags (isec) & SHF_GROUP)
8534
56
  elf_section_flags (osec) |= SHF_GROUP;
8535
1.59k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8536
1.59k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8537
1.59k
    }
8538
8539
  /* If not decompress, preserve SHF_COMPRESSED.  */
8540
1.59k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8541
1.59k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8542
1.59k
         & 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.59k
  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.59k
  osec->use_rela_p = isec->use_rela_p;
8554
8555
1.59k
  return true;
8556
1.59k
}
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
183
{
8568
183
  asection *isec;
8569
8570
1.76k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8571
1.58k
    if (elf_section_type (isec) == SHT_GROUP)
8572
16
      {
8573
16
  asection *first = elf_next_in_group (isec);
8574
16
  asection *s = first;
8575
16
  bfd_size_type removed = 0;
8576
8577
39
  while (s != NULL)
8578
39
    {
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
39
      if (s->output_section != discarded
8583
39
    && 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
39
      else
8589
39
        {
8590
39
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8591
39
    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
39
    else
8606
39
      {
8607
        /* Also adjust for zero-sized relocation member
8608
           section.  */
8609
39
        if (elf_sec->rel.hdr != NULL
8610
0
      && elf_sec->rel.hdr->sh_size == 0)
8611
0
          removed += 4;
8612
39
        if (elf_sec->rela.hdr != NULL
8613
30
      && elf_sec->rela.hdr->sh_size == 0)
8614
0
          removed += 4;
8615
39
      }
8616
39
        }
8617
39
      s = elf_next_in_group (s);
8618
39
      if (s == first)
8619
16
        break;
8620
39
    }
8621
16
  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
16
      }
8649
8650
183
  return true;
8651
183
}
8652
8653
/* Copy private header information.  */
8654
8655
bool
8656
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8657
183
{
8658
183
  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
183
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8667
142
    {
8668
142
      if (! copy_private_bfd_data (ibfd, obfd))
8669
0
  return false;
8670
142
    }
8671
8672
183
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8673
183
}
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
4
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8682
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8683
7
#define MAP_STRTAB    (SHN_HIOS + 3)
8684
8
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8685
3
#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
3.03k
{
8693
3.03k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8694
0
    return true;
8695
8696
3.03k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8697
3.03k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8698
3.03k
  if (isym != NULL
8699
3.03k
      && isym->internal_elf_sym.st_shndx != 0
8700
970
      && osym != NULL
8701
970
      && bfd_is_abs_section (isym->symbol.section))
8702
366
    {
8703
366
      unsigned int shndx;
8704
8705
366
      shndx = isym->internal_elf_sym.st_shndx;
8706
366
      if (shndx == elf_onesymtab (ibfd))
8707
2
  shndx = MAP_ONESYMTAB;
8708
364
      else if (shndx == elf_dynsymtab (ibfd))
8709
0
  shndx = MAP_DYNSYMTAB;
8710
364
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8711
4
  shndx = MAP_STRTAB;
8712
360
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8713
4
  shndx = MAP_SHSTRTAB;
8714
356
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8715
2
  shndx = MAP_SYM_SHNDX;
8716
366
      osym->internal_elf_sym.st_shndx = shndx;
8717
366
    }
8718
8719
3.03k
  return true;
8720
3.03k
}
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
39
{
8730
39
  elf_backend_data *bed;
8731
39
  unsigned int symcount;
8732
39
  asymbol **syms;
8733
39
  struct elf_strtab_hash *stt;
8734
39
  Elf_Internal_Shdr *symtab_hdr;
8735
39
  Elf_Internal_Shdr *symtab_shndx_hdr;
8736
39
  Elf_Internal_Shdr *symstrtab_hdr;
8737
39
  struct elf_sym_strtab *symstrtab;
8738
39
  bfd_byte *outbound_syms;
8739
39
  bfd_byte *outbound_shndx;
8740
39
  unsigned long outbound_syms_index;
8741
39
  unsigned int idx;
8742
39
  unsigned int num_locals;
8743
39
  size_t amt;
8744
39
  bool name_local_sections;
8745
8746
39
  if (!elf_map_symbols (abfd, &num_locals))
8747
0
    return false;
8748
8749
  /* Dump out the symtabs.  */
8750
39
  stt = _bfd_elf_strtab_init ();
8751
39
  if (stt == NULL)
8752
0
    return false;
8753
8754
39
  bed = get_elf_backend_data (abfd);
8755
39
  symcount = bfd_get_symcount (abfd);
8756
39
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8757
39
  symtab_hdr->sh_type = SHT_SYMTAB;
8758
39
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8759
39
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8760
39
  symtab_hdr->sh_info = num_locals + 1;
8761
39
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8762
8763
39
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8764
39
  symstrtab_hdr->sh_type = SHT_STRTAB;
8765
8766
  /* Allocate buffer to swap out the .strtab section.  */
8767
39
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8768
39
      || (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
39
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8776
39
      || (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
39
  symtab_hdr->contents = outbound_syms;
8786
39
  outbound_syms_index = 0;
8787
8788
39
  outbound_shndx = NULL;
8789
8790
39
  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
39
  {
8813
    /* Fill in zeroth symbol and swap it out.  */
8814
39
    Elf_Internal_Sym sym;
8815
39
    sym.st_name = 0;
8816
39
    sym.st_value = 0;
8817
39
    sym.st_size = 0;
8818
39
    sym.st_info = 0;
8819
39
    sym.st_other = 0;
8820
39
    sym.st_shndx = SHN_UNDEF;
8821
39
    sym.st_target_internal = 0;
8822
39
    symstrtab[outbound_syms_index].sym = sym;
8823
39
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8824
39
    outbound_syms_index++;
8825
39
  }
8826
8827
39
  name_local_sections
8828
39
    = (bed->elf_backend_name_local_section_symbols
8829
5
       && bed->elf_backend_name_local_section_symbols (abfd));
8830
8831
39
  syms = bfd_get_outsymbols (abfd);
8832
1.28k
  for (idx = 0; idx < symcount; idx++)
8833
1.25k
    {
8834
1.25k
      Elf_Internal_Sym sym;
8835
8836
1.25k
      flagword flags = syms[idx]->flags;
8837
1.25k
      if (!name_local_sections
8838
1.17k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8839
122
  {
8840
    /* Local section symbols have no name.  */
8841
122
    sym.st_name = 0;
8842
122
  }
8843
1.12k
      else
8844
1.12k
  {
8845
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8846
       to get the final offset for st_name.  */
8847
1.12k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8848
1.12k
    if (stridx == (size_t) -1)
8849
0
      goto error_return;
8850
1.12k
    sym.st_name = stridx;
8851
1.12k
  }
8852
8853
1.25k
      bfd_vma value = syms[idx]->value;
8854
1.25k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8855
1.25k
      asection *sec = syms[idx]->section;
8856
8857
1.25k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8858
2
  {
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
2
    sym.st_size = value;
8863
2
    if (type_ptr == NULL
8864
2
        || type_ptr->internal_elf_sym.st_value == 0)
8865
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8866
2
    else
8867
2
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8868
2
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8869
2
  }
8870
1.24k
      else
8871
1.24k
  {
8872
1.24k
    unsigned int shndx;
8873
8874
1.24k
    if (sec->output_section)
8875
1.24k
      {
8876
1.24k
        value += sec->output_offset;
8877
1.24k
        sec = sec->output_section;
8878
1.24k
      }
8879
8880
    /* Don't add in the section vma for relocatable output.  */
8881
1.24k
    if (! relocatable_p)
8882
795
      value += sec->vma;
8883
1.24k
    sym.st_value = value;
8884
1.24k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8885
8886
1.24k
    if (bfd_is_abs_section (sec)
8887
332
        && type_ptr != NULL
8888
332
        && type_ptr->internal_elf_sym.st_shndx != 0)
8889
332
      {
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
332
        shndx = type_ptr->internal_elf_sym.st_shndx;
8894
332
        switch (shndx)
8895
332
    {
8896
2
    case MAP_ONESYMTAB:
8897
2
      shndx = elf_onesymtab (abfd);
8898
2
      break;
8899
0
    case MAP_DYNSYMTAB:
8900
0
      shndx = elf_dynsymtab (abfd);
8901
0
      break;
8902
3
    case MAP_STRTAB:
8903
3
      shndx = elf_strtab_sec (abfd);
8904
3
      break;
8905
4
    case MAP_SHSTRTAB:
8906
4
      shndx = elf_shstrtab_sec (abfd);
8907
4
      break;
8908
1
    case MAP_SYM_SHNDX:
8909
1
      if (elf_symtab_shndx_list (abfd))
8910
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8911
1
      break;
8912
0
    case SHN_COMMON:
8913
13
    case SHN_ABS:
8914
13
      shndx = SHN_ABS;
8915
13
      break;
8916
309
    default:
8917
309
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8918
2
        {
8919
2
          if (bed->symbol_section_index)
8920
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8921
          /* Otherwise just leave the index alone.  */
8922
2
        }
8923
307
      else
8924
307
        {
8925
307
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8926
0
      _bfd_error_handler (_("%pB: \
8927
0
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8928
0
            abfd, shndx);
8929
307
          shndx = SHN_ABS;
8930
307
        }
8931
309
      break;
8932
332
    }
8933
332
      }
8934
916
    else
8935
916
      {
8936
916
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8937
8938
916
        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
916
      }
8965
8966
1.24k
    sym.st_shndx = shndx;
8967
1.24k
  }
8968
8969
1.25k
      int type;
8970
1.25k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8971
34
  type = STT_TLS;
8972
1.21k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8973
4
  type = STT_GNU_IFUNC;
8974
1.21k
      else if ((flags & BSF_FUNCTION) != 0)
8975
212
  type = STT_FUNC;
8976
1.00k
      else if ((flags & BSF_OBJECT) != 0)
8977
163
  type = STT_OBJECT;
8978
837
      else if ((flags & BSF_RELC) != 0)
8979
9
  type = STT_RELC;
8980
828
      else if ((flags & BSF_SRELC) != 0)
8981
54
  type = STT_SRELC;
8982
774
      else
8983
774
  type = STT_NOTYPE;
8984
8985
1.25k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8986
0
  type = STT_TLS;
8987
8988
      /* Processor-specific types.  */
8989
1.25k
      if (type_ptr != NULL
8990
1.25k
    && 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.25k
      if (flags & BSF_SECTION_SYM)
8995
135
  {
8996
135
    if (flags & BSF_GLOBAL)
8997
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8998
135
    else
8999
135
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9000
135
  }
9001
1.11k
      else if (bfd_is_com_section (syms[idx]->section))
9002
2
  {
9003
2
    if (type != STT_TLS)
9004
2
      {
9005
2
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9006
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9007
0
      ? STT_COMMON : STT_OBJECT);
9008
2
        else
9009
2
    type = ((flags & BSF_ELF_COMMON) != 0
9010
2
      ? STT_COMMON : STT_OBJECT);
9011
2
      }
9012
2
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9013
2
  }
9014
1.11k
      else if (bfd_is_und_section (syms[idx]->section))
9015
532
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9016
1.11k
            ? STB_WEAK
9017
1.11k
            : STB_GLOBAL),
9018
1.11k
           type);
9019
581
      else if (flags & BSF_FILE)
9020
14
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9021
567
      else
9022
567
  {
9023
567
    int bind = STB_LOCAL;
9024
9025
567
    if (flags & BSF_LOCAL)
9026
359
      bind = STB_LOCAL;
9027
208
    else if (flags & BSF_GNU_UNIQUE)
9028
0
      bind = STB_GNU_UNIQUE;
9029
208
    else if (flags & BSF_WEAK)
9030
4
      bind = STB_WEAK;
9031
204
    else if (flags & BSF_GLOBAL)
9032
190
      bind = STB_GLOBAL;
9033
9034
567
    sym.st_info = ELF_ST_INFO (bind, type);
9035
567
  }
9036
9037
1.25k
      if (type_ptr != NULL)
9038
1.25k
  {
9039
1.25k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9040
1.25k
    sym.st_target_internal
9041
1.25k
      = type_ptr->internal_elf_sym.st_target_internal;
9042
1.25k
  }
9043
0
      else
9044
0
  {
9045
0
    sym.st_other = 0;
9046
0
    sym.st_target_internal = 0;
9047
0
  }
9048
9049
1.25k
      symstrtab[outbound_syms_index].sym = sym;
9050
1.25k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9051
1.25k
      outbound_syms_index++;
9052
1.25k
    }
9053
9054
  /* Finalize the .strtab section.  */
9055
39
  _bfd_elf_strtab_finalize (stt);
9056
9057
  /* Swap out the .strtab section.  */
9058
1.32k
  for (idx = 0; idx < outbound_syms_index; idx++)
9059
1.28k
    {
9060
1.28k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9061
1.28k
      if (elfsym->sym.st_name != 0)
9062
498
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9063
498
                  elfsym->sym.st_name);
9064
1.28k
      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.28k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9071
1.28k
             (outbound_syms
9072
1.28k
        + (elfsym->dest_index
9073
1.28k
           * bed->s->sizeof_sym)),
9074
1.28k
             NPTR_ADD (outbound_shndx,
9075
1.28k
           (elfsym->dest_index
9076
1.28k
            * sizeof (Elf_External_Sym_Shndx))));
9077
1.28k
    }
9078
39
  free (symstrtab);
9079
9080
39
  *sttp = stt;
9081
39
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9082
39
  symstrtab_hdr->sh_type = SHT_STRTAB;
9083
39
  symstrtab_hdr->sh_addralign = 1;
9084
9085
39
  return true;
9086
39
}
9087
9088
/* Return the number of bytes required to hold the symtab vector.
9089
9090
   Note that we base it on the count plus 1, since we will null terminate
9091
   the vector allocated based on this size.  However, the ELF symbol table
9092
   always has a dummy entry as symbol #0, so it ends up even.  */
9093
9094
long
9095
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9096
4.92k
{
9097
4.92k
  bfd_size_type symcount;
9098
4.92k
  long symtab_size;
9099
4.92k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9100
9101
4.92k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9102
4.92k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9103
0
    {
9104
0
      bfd_set_error (bfd_error_file_too_big);
9105
0
      return -1;
9106
0
    }
9107
4.92k
  symtab_size = symcount * (sizeof (asymbol *));
9108
4.92k
  if (symcount == 0)
9109
567
    symtab_size = sizeof (asymbol *);
9110
4.36k
  else if (!bfd_write_p (abfd))
9111
4.36k
    {
9112
4.36k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9113
9114
4.36k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9115
155
  {
9116
155
    bfd_set_error (bfd_error_file_truncated);
9117
155
    return -1;
9118
155
  }
9119
4.36k
    }
9120
9121
4.77k
  return symtab_size;
9122
4.92k
}
9123
9124
long
9125
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9126
8.66k
{
9127
8.66k
  bfd_size_type symcount;
9128
8.66k
  long symtab_size;
9129
8.66k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9130
9131
8.66k
  if (elf_dynsymtab (abfd) == 0)
9132
8.25k
    {
9133
      /* Check if there is dynamic symbol table.  */
9134
8.25k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9135
8.25k
      if (symcount)
9136
6
  goto compute_symtab_size;
9137
9138
8.24k
      bfd_set_error (bfd_error_invalid_operation);
9139
8.24k
      return -1;
9140
8.25k
    }
9141
9142
414
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9143
414
  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
420
 compute_symtab_size:
9150
420
  symtab_size = symcount * (sizeof (asymbol *));
9151
420
  if (symcount == 0)
9152
9
    symtab_size = sizeof (asymbol *);
9153
411
  else if (!bfd_write_p (abfd))
9154
411
    {
9155
411
      ufile_ptr filesize = bfd_get_file_size (abfd);
9156
9157
411
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9158
19
  {
9159
19
    bfd_set_error (bfd_error_file_truncated);
9160
19
    return -1;
9161
19
  }
9162
411
    }
9163
9164
401
  return symtab_size;
9165
420
}
9166
9167
long
9168
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9169
15.8k
{
9170
15.8k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9171
14.6k
    {
9172
      /* Sanity check reloc section size.  */
9173
14.6k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9174
9175
14.6k
      if (filesize != 0)
9176
14.6k
  {
9177
14.6k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9178
14.6k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9179
14.6k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9180
9181
14.6k
    if (rel_size + rela_size > filesize
9182
14.3k
        || rel_size + rela_size < rel_size)
9183
374
      {
9184
374
        bfd_set_error (bfd_error_file_truncated);
9185
374
        return -1;
9186
374
      }
9187
14.6k
  }
9188
14.6k
    }
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
15.5k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9198
15.8k
}
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
15.5k
{
9208
15.5k
  arelent *tblptr;
9209
15.5k
  unsigned int i;
9210
15.5k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9211
9212
15.5k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9213
2.09k
    return -1;
9214
9215
13.4k
  tblptr = section->relocation;
9216
117k
  for (i = 0; i < section->reloc_count; i++)
9217
103k
    *relptr++ = tblptr++;
9218
9219
13.4k
  *relptr = NULL;
9220
9221
13.4k
  return section->reloc_count;
9222
15.5k
}
9223
9224
long
9225
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9226
4.77k
{
9227
4.77k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9228
4.77k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9229
9230
4.77k
  if (symcount >= 0)
9231
4.03k
    abfd->symcount = symcount;
9232
4.77k
  return symcount;
9233
4.77k
}
9234
9235
long
9236
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9237
              asymbol **allocation)
9238
225
{
9239
225
  elf_backend_data *bed = get_elf_backend_data (abfd);
9240
225
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9241
9242
225
  if (symcount >= 0)
9243
168
    abfd->dynsymcount = symcount;
9244
225
  return symcount;
9245
225
}
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.41k
{
9255
8.41k
  bfd_size_type count, ext_rel_size;
9256
8.41k
  asection *s;
9257
9258
8.41k
  if (elf_dynsymtab (abfd) == 0)
9259
8.13k
    {
9260
8.13k
      bfd_set_error (bfd_error_invalid_operation);
9261
8.13k
      return -1;
9262
8.13k
    }
9263
9264
284
  count = 1;
9265
284
  ext_rel_size = 0;
9266
7.54k
  for (s = abfd->sections; s != NULL; s = s->next)
9267
7.26k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9268
1.01k
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9269
990
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9270
412
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9271
410
      {
9272
410
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9273
410
  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
410
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9279
410
  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
410
      }
9285
284
  if (count > 1 && !bfd_write_p (abfd))
9286
277
    {
9287
      /* Sanity check reloc section sizes.  */
9288
277
      ufile_ptr filesize = bfd_get_file_size (abfd);
9289
277
      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
277
    }
9295
281
  return count * sizeof (arelent *);
9296
284
}
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
252
{
9311
252
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9312
252
  asection *s;
9313
252
  long ret;
9314
9315
252
  if (elf_dynsymtab (abfd) == 0)
9316
0
    {
9317
0
      bfd_set_error (bfd_error_invalid_operation);
9318
0
      return -1;
9319
0
    }
9320
9321
252
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9322
252
  ret = 0;
9323
6.37k
  for (s = abfd->sections; s != NULL; s = s->next)
9324
6.14k
    {
9325
6.14k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9326
894
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9327
872
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9328
366
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9329
364
  {
9330
364
    arelent *p;
9331
364
    long count, i;
9332
9333
364
    if (! (*slurp_relocs) (abfd, s, syms, true))
9334
18
      return -1;
9335
346
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9336
346
    p = s->relocation;
9337
3.16k
    for (i = 0; i < count; i++)
9338
2.81k
      *storage++ = p++;
9339
346
    ret += count;
9340
346
  }
9341
6.14k
    }
9342
9343
234
  *storage = NULL;
9344
9345
234
  return ret;
9346
252
}
9347

9348
/* Read in the version information.  */
9349
9350
bool
9351
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9352
257
{
9353
257
  bfd_byte *contents = NULL;
9354
257
  unsigned int freeidx = 0;
9355
257
  size_t amt;
9356
257
  void *contents_addr = NULL;
9357
257
  size_t contents_size = 0;
9358
9359
257
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9360
242
    {
9361
242
      Elf_Internal_Shdr *hdr;
9362
242
      Elf_External_Verneed *everneed;
9363
242
      Elf_Internal_Verneed *iverneed;
9364
242
      unsigned int i;
9365
242
      bfd_byte *contents_end;
9366
242
      size_t verneed_count;
9367
242
      size_t verneed_size;
9368
9369
242
      if (elf_tdata (abfd)->dt_verneed != NULL)
9370
1
  {
9371
1
    hdr = NULL;
9372
1
    contents = elf_tdata (abfd)->dt_verneed;
9373
1
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9374
1
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9375
1
  }
9376
241
      else
9377
241
  {
9378
241
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9379
9380
241
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9381
4
      {
9382
58
      error_return_bad_verref:
9383
58
        _bfd_error_handler
9384
58
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9385
58
        bfd_set_error (bfd_error_bad_value);
9386
73
      error_return_verref:
9387
73
        elf_tdata (abfd)->verref = NULL;
9388
73
        elf_tdata (abfd)->cverrefs = 0;
9389
73
        goto error_return;
9390
58
      }
9391
9392
237
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9393
1
      goto error_return_verref;
9394
236
    contents_size = hdr->sh_size;
9395
236
    contents = _bfd_mmap_temporary (abfd, contents_size,
9396
236
            &contents_addr, &contents_size);
9397
236
    if (contents == NULL)
9398
13
      goto error_return_verref;
9399
9400
223
    verneed_size = hdr->sh_size;
9401
223
    verneed_count = hdr->sh_info;
9402
223
  }
9403
9404
224
      if (_bfd_mul_overflow (verneed_count,
9405
224
           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
224
      if (amt == 0)
9411
1
  goto error_return_verref;
9412
223
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9413
223
      if (elf_tdata (abfd)->verref == NULL)
9414
0
  goto error_return_verref;
9415
9416
223
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9417
223
      == sizeof (Elf_External_Vernaux));
9418
223
      contents_end = (contents + verneed_size
9419
223
          - sizeof (Elf_External_Verneed));
9420
223
      everneed = (Elf_External_Verneed *) contents;
9421
223
      iverneed = elf_tdata (abfd)->verref;
9422
226
      for (i = 0; i < verneed_count; i++, iverneed++)
9423
223
  {
9424
223
    Elf_External_Vernaux *evernaux;
9425
223
    Elf_Internal_Vernaux *ivernaux;
9426
223
    unsigned int j;
9427
9428
223
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9429
9430
223
    iverneed->vn_bfd = abfd;
9431
9432
223
    if (elf_use_dt_symtab_p (abfd))
9433
1
      {
9434
1
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9435
1
    iverneed->vn_filename
9436
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9437
0
        else
9438
0
    iverneed->vn_filename = NULL;
9439
1
      }
9440
222
    else if (hdr == NULL)
9441
0
      goto error_return_bad_verref;
9442
222
    else
9443
222
      iverneed->vn_filename
9444
222
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9445
222
             iverneed->vn_file);
9446
223
    if (iverneed->vn_filename == NULL)
9447
20
      goto error_return_bad_verref;
9448
9449
203
    if (iverneed->vn_cnt == 0)
9450
20
      iverneed->vn_auxptr = NULL;
9451
183
    else
9452
183
      {
9453
183
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9454
183
             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
183
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9460
183
    bfd_alloc (abfd, amt);
9461
183
        if (iverneed->vn_auxptr == NULL)
9462
0
    goto error_return_verref;
9463
183
      }
9464
9465
203
    if (iverneed->vn_aux
9466
203
        > (size_t) (contents_end - (bfd_byte *) everneed))
9467
8
      goto error_return_bad_verref;
9468
9469
195
    evernaux = ((Elf_External_Vernaux *)
9470
195
          ((bfd_byte *) everneed + iverneed->vn_aux));
9471
195
    ivernaux = iverneed->vn_auxptr;
9472
297
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9473
282
      {
9474
282
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9475
9476
282
        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
282
        else if (hdr == NULL)
9485
0
    goto error_return_bad_verref;
9486
282
        else
9487
282
    ivernaux->vna_nodename
9488
282
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9489
282
                 ivernaux->vna_name);
9490
282
        if (ivernaux->vna_nodename == NULL)
9491
5
    goto error_return_bad_verref;
9492
9493
277
        if (ivernaux->vna_other > freeidx)
9494
173
    freeidx = ivernaux->vna_other;
9495
9496
277
        ivernaux->vna_nextptr = NULL;
9497
277
        if (ivernaux->vna_next == 0)
9498
162
    {
9499
162
      iverneed->vn_cnt = j + 1;
9500
162
      break;
9501
162
    }
9502
115
        if (j + 1 < iverneed->vn_cnt)
9503
109
    ivernaux->vna_nextptr = ivernaux + 1;
9504
9505
115
        if (ivernaux->vna_next
9506
115
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9507
13
    goto error_return_bad_verref;
9508
9509
102
        evernaux = ((Elf_External_Vernaux *)
9510
102
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9511
102
      }
9512
9513
177
    iverneed->vn_nextref = NULL;
9514
177
    if (iverneed->vn_next == 0)
9515
166
      break;
9516
11
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9517
0
      iverneed->vn_nextref = iverneed + 1;
9518
9519
11
    if (iverneed->vn_next
9520
11
        > (size_t) (contents_end - (bfd_byte *) everneed))
9521
8
      goto error_return_bad_verref;
9522
9523
3
    everneed = ((Elf_External_Verneed *)
9524
3
          ((bfd_byte *) everneed + iverneed->vn_next));
9525
3
  }
9526
169
      elf_tdata (abfd)->cverrefs = i;
9527
9528
169
      if (contents != elf_tdata (abfd)->dt_verneed)
9529
168
  _bfd_munmap_temporary (contents_addr, contents_size);
9530
169
      contents = NULL;
9531
169
      contents_addr = NULL;
9532
169
    }
9533
9534
184
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9535
15
    {
9536
15
      Elf_Internal_Shdr *hdr;
9537
15
      Elf_External_Verdef *everdef;
9538
15
      Elf_Internal_Verdef *iverdef;
9539
15
      Elf_Internal_Verdef *iverdefarr;
9540
15
      Elf_Internal_Verdef iverdefmem;
9541
15
      unsigned int i;
9542
15
      unsigned int maxidx;
9543
15
      bfd_byte *contents_end_def, *contents_end_aux;
9544
15
      size_t verdef_count;
9545
15
      size_t verdef_size;
9546
9547
15
      if (elf_tdata (abfd)->dt_verdef != NULL)
9548
0
  {
9549
0
    hdr = NULL;
9550
0
    contents = elf_tdata (abfd)->dt_verdef;
9551
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9552
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9553
0
  }
9554
15
      else
9555
15
  {
9556
15
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9557
9558
15
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9559
1
      {
9560
8
      error_return_bad_verdef:
9561
8
        _bfd_error_handler
9562
8
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9563
8
        bfd_set_error (bfd_error_bad_value);
9564
12
      error_return_verdef:
9565
12
        elf_tdata (abfd)->verdef = NULL;
9566
12
        elf_tdata (abfd)->cverdefs = 0;
9567
12
        goto error_return;
9568
8
      }
9569
9570
14
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9571
1
      goto error_return_verdef;
9572
13
    contents_size = hdr->sh_size;
9573
13
    contents = _bfd_mmap_temporary (abfd, contents_size,
9574
13
            &contents_addr, &contents_size);
9575
13
    if (contents == NULL)
9576
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
2
      goto error_return_bad_verdef;
9601
10
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9602
9
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9603
9604
10
    if (iverdefmem.vd_next == 0)
9605
4
      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
7
      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
7
      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
7
      if (amt == 0)
9629
0
  goto error_return_verdef;
9630
7
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9631
7
      if (elf_tdata (abfd)->verdef == NULL)
9632
0
  goto error_return_verdef;
9633
9634
7
      elf_tdata (abfd)->cverdefs = maxidx;
9635
9636
7
      everdef = (Elf_External_Verdef *) contents;
9637
7
      iverdefarr = elf_tdata (abfd)->verdef;
9638
9
      for (i = 0; i < verdef_count; ++i)
9639
8
  {
9640
8
    Elf_External_Verdaux *everdaux;
9641
8
    Elf_Internal_Verdaux *iverdaux;
9642
8
    unsigned int j;
9643
9644
8
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9645
9646
8
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9647
0
      goto error_return_bad_verdef;
9648
9649
8
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9650
8
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9651
9652
8
    iverdef->vd_bfd = abfd;
9653
9654
8
    if (iverdef->vd_cnt == 0)
9655
5
      iverdef->vd_auxptr = NULL;
9656
3
    else
9657
3
      {
9658
3
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9659
3
             sizeof (Elf_Internal_Verdaux), &amt))
9660
0
    {
9661
0
      bfd_set_error (bfd_error_file_too_big);
9662
0
      goto error_return_verdef;
9663
0
    }
9664
3
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9665
3
    bfd_alloc (abfd, amt);
9666
3
        if (iverdef->vd_auxptr == NULL)
9667
0
    goto error_return_verdef;
9668
3
      }
9669
9670
8
    if (iverdef->vd_aux
9671
8
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9672
1
      goto error_return_bad_verdef;
9673
9674
7
    everdaux = ((Elf_External_Verdaux *)
9675
7
          ((bfd_byte *) everdef + iverdef->vd_aux));
9676
7
    iverdaux = iverdef->vd_auxptr;
9677
7
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9678
3
      {
9679
3
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9680
9681
3
        if (elf_use_dt_symtab_p (abfd))
9682
0
    {
9683
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9684
0
        iverdaux->vda_nodename
9685
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9686
0
      else
9687
0
        iverdaux->vda_nodename = NULL;
9688
0
    }
9689
3
        else
9690
3
    iverdaux->vda_nodename
9691
3
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9692
3
                 iverdaux->vda_name);
9693
3
        if (iverdaux->vda_nodename == NULL)
9694
2
    goto error_return_bad_verdef;
9695
9696
1
        iverdaux->vda_nextptr = NULL;
9697
1
        if (iverdaux->vda_next == 0)
9698
0
    {
9699
0
      iverdef->vd_cnt = j + 1;
9700
0
      break;
9701
0
    }
9702
1
        if (j + 1 < iverdef->vd_cnt)
9703
1
    iverdaux->vda_nextptr = iverdaux + 1;
9704
9705
1
        if (iverdaux->vda_next
9706
1
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9707
1
    goto error_return_bad_verdef;
9708
9709
0
        everdaux = ((Elf_External_Verdaux *)
9710
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9711
0
      }
9712
9713
4
    iverdef->vd_nodename = NULL;
9714
4
    if (iverdef->vd_cnt)
9715
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9716
9717
4
    iverdef->vd_nextdef = NULL;
9718
4
    if (iverdef->vd_next == 0)
9719
2
      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
3
      if (contents != elf_tdata (abfd)->dt_verdef)
9728
3
  _bfd_munmap_temporary (contents_addr, contents_size);
9729
3
      contents = NULL;
9730
3
      contents_addr = NULL;
9731
3
    }
9732
169
  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
172
  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
172
  return true;
9782
9783
85
 error_return:
9784
85
  if (contents != elf_tdata (abfd)->dt_verneed
9785
62
      && contents != elf_tdata (abfd)->dt_verdef)
9786
62
    _bfd_munmap_temporary (contents_addr, contents_size);
9787
85
  return false;
9788
172
}
9789

9790
asymbol *
9791
_bfd_elf_make_empty_symbol (bfd *abfd)
9792
1.19M
{
9793
1.19M
  elf_symbol_type *newsym;
9794
9795
1.19M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9796
1.19M
  if (!newsym)
9797
0
    return NULL;
9798
1.19M
  newsym->symbol.the_bfd = abfd;
9799
1.19M
  return &newsym->symbol;
9800
1.19M
}
9801
9802
void
9803
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9804
        asymbol *symbol,
9805
        symbol_info *ret)
9806
22.9k
{
9807
22.9k
  bfd_symbol_info (symbol, ret);
9808
22.9k
}
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
15.2k
{
9818
  /* Normal local symbols start with ``.L''.  */
9819
15.2k
  if (name[0] == '.' && name[1] == 'L')
9820
1.41k
    return true;
9821
9822
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9823
     DWARF debugging symbols starting with ``..''.  */
9824
13.8k
  if (name[0] == '.' && name[1] == '.')
9825
480
    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
13.3k
  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
13.3k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9847
3
    {
9848
3
      bool ret = false;
9849
3
      const char * p;
9850
3
      char c;
9851
9852
6
      for (p = name + 2; (c = *p); p++)
9853
3
  {
9854
3
    if (c == 1 || c == 2)
9855
0
      {
9856
0
        if (c == 1 && p == name + 2)
9857
    /* A fake symbol.  */
9858
0
    return true;
9859
9860
        /* FIXME: We are being paranoid here and treating symbols like
9861
     L0^Bfoo as if there were non-local, on the grounds that the
9862
     assembler will never generate them.  But can any symbol
9863
     containing an ASCII value in the range 1-31 ever be anything
9864
     other than some kind of local ?  */
9865
0
        ret = true;
9866
0
      }
9867
9868
3
    if (! ISDIGIT (c))
9869
0
      {
9870
0
        ret = false;
9871
0
        break;
9872
0
      }
9873
3
  }
9874
3
      return ret;
9875
3
    }
9876
9877
13.3k
  return false;
9878
13.3k
}
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
563
{
9893
  /* If this isn't the right architecture for this backend, and this
9894
     isn't the generic backend, fail.  */
9895
563
  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
563
  return bfd_default_set_arch_mach (abfd, arch, machine);
9901
563
}
9902
9903
/* Find the nearest line to a particular section and offset,
9904
   for error reporting.  */
9905
9906
bool
9907
_bfd_elf_find_nearest_line (bfd *abfd,
9908
          asymbol **symbols,
9909
          asection *section,
9910
          bfd_vma offset,
9911
          const char **filename_ptr,
9912
          const char **functionname_ptr,
9913
          unsigned int *line_ptr,
9914
          unsigned int *discriminator_ptr)
9915
24.1k
{
9916
24.1k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9917
24.1k
                offset, filename_ptr,
9918
24.1k
                functionname_ptr, line_ptr,
9919
24.1k
                discriminator_ptr);
9920
24.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
24.1k
{
9937
24.1k
  bool found;
9938
9939
24.1k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9940
24.1k
                section, offset, filename_ptr,
9941
24.1k
                functionname_ptr, line_ptr,
9942
24.1k
                discriminator_ptr,
9943
24.1k
                dwarf_debug_sections,
9944
24.1k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9945
8.17k
    return true;
9946
9947
15.9k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9948
15.9k
             filename_ptr, functionname_ptr, line_ptr))
9949
0
    {
9950
0
      if (!*functionname_ptr)
9951
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9952
0
        *filename_ptr ? NULL : filename_ptr,
9953
0
        functionname_ptr);
9954
0
      return true;
9955
0
    }
9956
9957
15.9k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9958
15.9k
               &found, filename_ptr,
9959
15.9k
               functionname_ptr, line_ptr,
9960
15.9k
               &elf_tdata (abfd)->line_info))
9961
0
    return false;
9962
15.9k
  if (found && (*functionname_ptr || *line_ptr))
9963
0
    return true;
9964
9965
15.9k
  if (symbols == NULL)
9966
5.31k
    return false;
9967
9968
10.6k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9969
10.6k
        filename_ptr, functionname_ptr))
9970
8.02k
    return false;
9971
9972
2.63k
  *line_ptr = 0;
9973
2.63k
  return true;
9974
10.6k
}
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.41k
{
9982
4.41k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9983
4.41k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9984
4.41k
          filename_ptr, NULL, line_ptr, NULL,
9985
4.41k
          dwarf_debug_sections,
9986
4.41k
          &tdata->dwarf2_find_line_info);
9987
4.41k
}
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
925
{
10043
925
  Elf_Internal_Shdr *hdr;
10044
10045
925
  if (! abfd->output_has_begun
10046
89
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10047
13
    return false;
10048
10049
912
  if (!count)
10050
0
    return true;
10051
10052
912
  hdr = &elf_section_data (section)->this_hdr;
10053
912
  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
912
  return _bfd_generic_set_section_contents (abfd, section,
10090
912
              location, offset, count);
10091
912
}
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.71M
{
10201
6.71M
  struct elf_obj_tdata *tdata;
10202
10203
6.71M
  if ((bfd_get_format (abfd) == bfd_object
10204
6.68M
       || bfd_get_format (abfd) == bfd_core)
10205
28.3k
      && (tdata = elf_tdata (abfd)) != NULL)
10206
28.3k
    {
10207
28.3k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10208
329
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10209
28.3k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10210
28.3k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10211
28.3k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10212
28.3k
      _bfd_elf_cleanup_object_attributes (abfd);
10213
376k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10214
348k
  {
10215
348k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10216
348k
    if (!sec->alloced)
10217
347k
      {
10218
347k
        free (elf_section_data (sec)->this_hdr.contents);
10219
347k
        elf_section_data (sec)->this_hdr.contents = NULL;
10220
347k
      }
10221
348k
    free (elf_section_data (sec)->relocs);
10222
348k
    elf_section_data (sec)->relocs = NULL;
10223
348k
    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
348k
    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
348k
  }
10234
28.3k
      free (tdata->symtab_hdr.contents);
10235
28.3k
      tdata->symtab_hdr.contents = NULL;
10236
28.3k
    }
10237
10238
6.71M
  return _bfd_generic_bfd_free_cached_info (abfd);
10239
6.71M
}
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
14
{
10253
14
  return bfd_reloc_ok;
10254
14
}
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
73
{
10271
73
  int pid;
10272
10273
73
  pid = elf_tdata (abfd)->core->lwpid;
10274
73
  if (pid == 0)
10275
60
    pid = elf_tdata (abfd)->core->pid;
10276
10277
73
  return pid;
10278
73
}
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
73
{
10287
73
  asection *sect2;
10288
10289
73
  if (bfd_get_section_by_name (abfd, name) != NULL)
10290
28
    return true;
10291
10292
45
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10293
45
  if (sect2 == NULL)
10294
0
    return false;
10295
10296
45
  sect2->size = sect->size;
10297
45
  sect2->filepos = sect->filepos;
10298
45
  sect2->alignment_power = sect->alignment_power;
10299
45
  return true;
10300
45
}
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
73
{
10315
73
  char buf[100];
10316
73
  char *threaded_name;
10317
73
  size_t len;
10318
73
  asection *sect;
10319
10320
  /* Build the section name.  */
10321
10322
73
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10323
73
  len = strlen (buf) + 1;
10324
73
  threaded_name = (char *) bfd_alloc (abfd, len);
10325
73
  if (threaded_name == NULL)
10326
0
    return false;
10327
73
  memcpy (threaded_name, buf, len);
10328
10329
73
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10330
73
               SEC_HAS_CONTENTS);
10331
73
  if (sect == NULL)
10332
0
    return false;
10333
73
  sect->size = size;
10334
73
  sect->filepos = filepos;
10335
73
  sect->alignment_power = 2;
10336
10337
73
  return elfcore_maybe_make_sect (abfd, name, sect);
10338
73
}
10339
10340
static bool
10341
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10342
        size_t offs)
10343
42
{
10344
42
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10345
42
                   SEC_HAS_CONTENTS);
10346
10347
42
  if (sect == NULL)
10348
0
    return false;
10349
10350
42
  sect->size = note->descsz - offs;
10351
42
  sect->filepos = note->descpos + offs;
10352
42
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10353
10354
42
  return true;
10355
42
}
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
204
{
10368
204
  size_t size;
10369
204
  int offset;
10370
10371
204
  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
204
#if defined (HAVE_PRSTATUS32_T)
10399
204
  else if (note->descsz == sizeof (prstatus32_t))
10400
9
    {
10401
      /* 64-bit host, 32-bit corefile */
10402
9
      prstatus32_t prstat;
10403
10404
9
      size = sizeof (prstat.pr_reg);
10405
9
      offset   = offsetof (prstatus32_t, pr_reg);
10406
9
      memcpy (&prstat, note->descdata, sizeof (prstat));
10407
10408
      /* Do not overwrite the core signal if it
10409
   has already been set by another thread.  */
10410
9
      if (elf_tdata (abfd)->core->signal == 0)
10411
5
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10412
9
      if (elf_tdata (abfd)->core->pid == 0)
10413
5
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10414
10415
      /* pr_who exists on:
10416
   solaris 2.5+
10417
   unixware 4.2
10418
   pr_who doesn't exist on:
10419
   linux 2.[01]
10420
   */
10421
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10422
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10423
#else
10424
9
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10425
9
#endif
10426
9
    }
10427
195
#endif /* HAVE_PRSTATUS32_T */
10428
195
  else
10429
195
    {
10430
      /* Fail - we don't know how to handle any other
10431
   note size (ie. data object type).  */
10432
195
      return true;
10433
195
    }
10434
10435
  /* Make a ".reg/999" section and a ".reg" section.  */
10436
9
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10437
9
            size, note->descpos + offset);
10438
204
}
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
64
{
10447
64
  return _bfd_elfcore_make_pseudosection (abfd, name,
10448
64
            note->descsz, note->descpos);
10449
64
}
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
63
{
10458
63
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10459
63
}
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
22
{
10822
22
  char *dups;
10823
22
  char *end = (char *) memchr (start, '\0', max);
10824
22
  size_t len;
10825
10826
22
  if (end == NULL)
10827
6
    len = max;
10828
16
  else
10829
16
    len = end - start;
10830
10831
22
  dups = (char *) bfd_alloc (abfd, len + 1);
10832
22
  if (dups == NULL)
10833
0
    return NULL;
10834
10835
22
  memcpy (dups, start, len);
10836
22
  dups[len] = '\0';
10837
10838
22
  return dups;
10839
22
}
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
180
{
10845
180
  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
180
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10863
180
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10864
11
    {
10865
      /* 64-bit host, 32-bit corefile */
10866
11
      elfcore_psinfo32_t psinfo;
10867
10868
11
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10869
10870
11
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10871
11
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10872
11
#endif
10873
11
      elf_tdata (abfd)->core->program
10874
11
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10875
11
        sizeof (psinfo.pr_fname));
10876
10877
11
      elf_tdata (abfd)->core->command
10878
11
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10879
11
        sizeof (psinfo.pr_psargs));
10880
11
    }
10881
169
#endif
10882
10883
169
  else
10884
169
    {
10885
      /* Fail - we don't know how to handle any other
10886
   note size (ie. data object type).  */
10887
169
      return true;
10888
169
    }
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
11
  {
10895
11
    char *command = elf_tdata (abfd)->core->command;
10896
11
    int n = strlen (command);
10897
10898
11
    if (0 < n && command[n - 1] == ' ')
10899
1
      command[n - 1] = '\0';
10900
11
  }
10901
10902
11
  return true;
10903
180
}
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
67
{
11035
67
  char buf[30];
11036
67
  char *name;
11037
67
  size_t len;
11038
67
  unsigned int name_size;
11039
67
  asection *sect;
11040
67
  unsigned int type;
11041
67
  int is_active_thread;
11042
67
  bfd_vma base_addr;
11043
11044
67
  if (note->descsz < 4)
11045
41
    return true;
11046
11047
26
  if (! startswith (note->namedata, "win32"))
11048
26
    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
2.27k
{
11169
2.27k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11170
11171
  /* Short cut for LINUX notes.  */
11172
2.27k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11173
64
      && 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
2.27k
  switch (note->type)
11232
2.27k
    {
11233
1.67k
    default:
11234
1.67k
      return true;
11235
11236
204
    case NT_PRSTATUS:
11237
204
      if (bed->elf_backend_grok_prstatus)
11238
120
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11239
0
    return true;
11240
204
#if defined (HAVE_PRSTATUS_T)
11241
204
      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
56
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11257
56
      return elfcore_grok_prfpreg (abfd, note);
11258
11259
67
    case NT_WIN32PSTATUS:
11260
67
      return elfcore_grok_win32pstatus (abfd, note);
11261
11262
34
    case NT_GDB_TDESC:
11263
34
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11264
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11265
34
      else
11266
34
  return true;
11267
11268
23
    case NT_RISCV_CSR:
11269
23
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11270
0
  return elfcore_grok_riscv_csr (abfd, note);
11271
23
      else
11272
23
  return true;
11273
11274
156
    case NT_PRPSINFO:
11275
180
    case NT_PSINFO:
11276
180
      if (bed->elf_backend_grok_psinfo)
11277
112
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11278
0
    return true;
11279
180
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11280
180
      return elfcore_grok_psinfo (abfd, note);
11281
#else
11282
      return true;
11283
#endif
11284
11285
42
    case NT_AUXV:
11286
42
      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
2.27k
    }
11296
2.27k
}
11297
11298
static bool
11299
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11300
419
{
11301
419
  struct bfd_build_id* build_id;
11302
11303
419
  if (note->descsz == 0)
11304
70
    return false;
11305
11306
349
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11307
349
  if (build_id == NULL)
11308
0
    return false;
11309
11310
349
  build_id->size = note->descsz;
11311
349
  memcpy (build_id->data, note->descdata, note->descsz);
11312
349
  abfd->build_id = build_id;
11313
11314
349
  return true;
11315
349
}
11316
11317
static bool
11318
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11319
1.34k
{
11320
1.34k
  switch (note->type)
11321
1.34k
    {
11322
371
    default:
11323
371
      return true;
11324
11325
558
    case NT_GNU_PROPERTY_TYPE_0:
11326
558
      return _bfd_elf_parse_gnu_properties (abfd, note);
11327
11328
419
    case NT_GNU_BUILD_ID:
11329
419
      return elfobj_grok_gnu_build_id (abfd, note);
11330
1.34k
    }
11331
1.34k
}
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
1
{
11365
1
  size_t offset;
11366
11367
1
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11368
1
    {
11369
0
    case ELFCLASS32:
11370
0
      if (note->descsz < 108)
11371
0
  return false;
11372
0
      break;
11373
11374
1
    case ELFCLASS64:
11375
1
      if (note->descsz < 120)
11376
0
  return false;
11377
1
      break;
11378
11379
1
    default:
11380
0
      return false;
11381
1
    }
11382
11383
  /* Check for version 1 in pr_version.  */
11384
1
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11385
1
    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
4
{
11424
4
  size_t offset;
11425
4
  size_t size;
11426
4
  size_t min_size;
11427
11428
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11429
     Also compute minimum size of this note.  */
11430
4
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11431
4
    {
11432
0
    case ELFCLASS32:
11433
0
      offset = 4 + 4;
11434
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11435
0
      break;
11436
11437
4
    case ELFCLASS64:
11438
4
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11439
4
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11440
4
      break;
11441
11442
0
    default:
11443
0
      return false;
11444
4
    }
11445
11446
4
  if (note->descsz < min_size)
11447
4
    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
47
{
11496
47
  elf_backend_data *bed = get_elf_backend_data (abfd);
11497
11498
47
  switch (note->type)
11499
47
    {
11500
4
    case NT_PRSTATUS:
11501
4
      if (bed->elf_backend_grok_freebsd_prstatus)
11502
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11503
0
    return true;
11504
4
      return elfcore_grok_freebsd_prstatus (abfd, note);
11505
11506
7
    case NT_FPREGSET:
11507
7
      return elfcore_grok_prfpreg (abfd, note);
11508
11509
1
    case NT_PRPSINFO:
11510
1
      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
1
    case NT_FREEBSD_PROCSTAT_FILES:
11520
1
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11521
1
                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
34
    default:
11547
34
      return true;
11548
47
    }
11549
47
}
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
7
{
11792
7
  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
7
  switch ((int) note->type)
11805
7
    {
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
7
    default:
11868
7
      break;
11869
7
    }
11870
11871
7
  return true;
11872
7
}
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
7
{
11883
7
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11884
0
    return false;
11885
11886
7
  return elfcore_grok_note (abfd, note);
11887
7
}
11888
11889
static bool
11890
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11891
1
{
11892
1
  if (note->type == NT_OPENBSD_PROCINFO)
11893
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11894
11895
1
  if (note->type == NT_OPENBSD_REGS)
11896
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11897
11898
1
  if (note->type == NT_OPENBSD_FPREGS)
11899
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11900
11901
1
  if (note->type == NT_OPENBSD_XFPREGS)
11902
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11903
11904
1
  if (note->type == NT_OPENBSD_AUXV)
11905
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11906
11907
1
  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
1
  return true;
11922
1
}
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
27
{
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
27
  static long tid = 1;
12018
12019
27
  switch (note->type)
12020
27
    {
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
27
    default:
12030
27
      return true;
12031
27
    }
12032
27
}
12033
12034
static bool
12035
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12036
1
{
12037
1
  char *name;
12038
1
  asection *sect;
12039
1
  size_t len;
12040
12041
  /* Use note name as section name.  */
12042
1
  len = note->namesz;
12043
1
  name = (char *) bfd_alloc (abfd, len);
12044
1
  if (name == NULL)
12045
0
    return false;
12046
1
  memcpy (name, note->namedata, len);
12047
1
  name[len - 1] = '\0';
12048
12049
1
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12050
1
  if (sect == NULL)
12051
0
    return false;
12052
12053
1
  sect->size    = note->descsz;
12054
1
  sect->filepos   = note->descpos;
12055
1
  sect->alignment_power = 1;
12056
12057
1
  return true;
12058
1
}
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
7.75k
{
13101
7.75k
  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
7.75k
  if (align < 4)
13108
3.04k
    align = 4;
13109
7.75k
  if (align != 4 && align != 8)
13110
2.01k
    return false;
13111
13112
5.73k
  p = buf;
13113
14.1k
  while (p < buf + size)
13114
12.7k
    {
13115
12.7k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13116
12.7k
      Elf_Internal_Note in;
13117
13118
12.7k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13119
764
  return false;
13120
13121
11.9k
      in.type = H_GET_32 (abfd, xnp->type);
13122
13123
11.9k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13124
11.9k
      in.namedata = xnp->name;
13125
11.9k
      if (in.namesz > buf - in.namedata + size)
13126
1.88k
  return false;
13127
13128
10.1k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13129
10.1k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13130
10.1k
      in.descpos = offset + (in.descdata - buf);
13131
10.1k
      if (in.descsz != 0
13132
4.95k
    && (in.descdata >= buf + size
13133
4.79k
        || in.descsz > buf - in.descdata + size))
13134
1.19k
  return false;
13135
13136
8.90k
      switch (bfd_get_format (abfd))
13137
8.90k
  {
13138
0
  default:
13139
0
    return true;
13140
13141
2.62k
  case bfd_core:
13142
2.62k
    {
13143
21.0k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13144
2.62k
      static const struct
13145
2.62k
      {
13146
2.62k
        const char * string;
13147
2.62k
        size_t len;
13148
2.62k
        bool (*func) (bfd *, Elf_Internal_Note *);
13149
2.62k
      }
13150
2.62k
      grokers[] =
13151
2.62k
      {
13152
2.62k
        GROKER_ELEMENT ("", elfcore_grok_note),
13153
2.62k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13154
2.62k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13155
2.62k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13156
2.62k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13157
2.62k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13158
2.62k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13159
2.62k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13160
2.62k
      };
13161
2.62k
#undef GROKER_ELEMENT
13162
2.62k
      int i;
13163
13164
19.1k
      for (i = ARRAY_SIZE (grokers); i--;)
13165
19.1k
        {
13166
19.1k
    if (in.namesz >= grokers[i].len
13167
7.04k
        && strncmp (in.namedata, grokers[i].string,
13168
7.04k
        grokers[i].len) == 0)
13169
2.62k
      {
13170
2.62k
        if (! grokers[i].func (abfd, & in))
13171
158
          return false;
13172
2.47k
        break;
13173
2.62k
      }
13174
19.1k
        }
13175
2.47k
      break;
13176
2.62k
    }
13177
13178
6.27k
  case bfd_object:
13179
6.27k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13180
1.07k
      {
13181
1.07k
        if (! elfobj_grok_gnu_note (abfd, &in))
13182
332
    return false;
13183
1.07k
      }
13184
5.20k
    else if (in.namesz == sizeof "stapsdt"
13185
426
       && strcmp (in.namedata, "stapsdt") == 0)
13186
0
      {
13187
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13188
0
    return false;
13189
0
      }
13190
5.94k
    break;
13191
8.90k
  }
13192
13193
8.41k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13194
8.41k
    }
13195
13196
1.39k
  return true;
13197
5.73k
}
13198
13199
bool
13200
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13201
         size_t align)
13202
5.29k
{
13203
5.29k
  char *buf;
13204
13205
5.29k
  if (size == 0 || (size + 1) == 0)
13206
569
    return true;
13207
13208
4.72k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13209
1.67k
    return false;
13210
13211
3.05k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13212
3.05k
  if (buf == NULL)
13213
1.26k
    return false;
13214
13215
  /* PR 17512: file: ec08f814
13216
     0-termintate the buffer so that string searches will not overflow.  */
13217
1.79k
  buf[size] = 0;
13218
13219
1.79k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13220
1.66k
    {
13221
1.66k
      free (buf);
13222
1.66k
      return false;
13223
1.66k
    }
13224
13225
137
  free (buf);
13226
137
  return true;
13227
1.79k
}
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.28k
{
13383
7.28k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13384
7.28k
  asection *relplt;
13385
7.28k
  asymbol *s;
13386
7.28k
  const char *relplt_name;
13387
7.28k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13388
7.28k
  arelent *p;
13389
7.28k
  long count, i, n;
13390
7.28k
  size_t size;
13391
7.28k
  Elf_Internal_Shdr *hdr;
13392
7.28k
  char *names;
13393
7.28k
  asection *plt;
13394
13395
7.28k
  *ret = NULL;
13396
13397
7.28k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13398
7.23k
    return 0;
13399
13400
54
  if (dynsymcount <= 0)
13401
41
    return 0;
13402
13403
13
  if (!bed->plt_sym_val)
13404
13
    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
287
{
13495
287
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13496
287
  elf_backend_data *bed = get_elf_backend_data (abfd);
13497
13498
287
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13499
7
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13500
13501
287
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13502
284
      || bed->target_os == is_solaris)
13503
3
    {
13504
3
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13505
3
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13506
3
    }
13507
13508
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13509
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13510
     or STB_GNU_UNIQUE binding.  */
13511
287
  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
287
  return true;
13535
287
}
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
696k
{
13557
696k
  bfd_size_type size;
13558
696k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13559
13560
696k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13561
696k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13562
487k
      || sym->section != sec)
13563
684k
    return 0;
13564
13565
11.3k
  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
11.3k
  if (size == 0
13574
2.83k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13575
1.81k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13576
1.74k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13577
116
    return 0;
13578
13579
11.2k
  *code_off = sym->value;
13580
  /* Do not return 0 for the function's size.  */
13581
11.2k
  return size ? size : 1;
13582
11.3k
}
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.23k
#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.23k
{
13599
  /* We only support RELA secondary relocs.  */
13600
1.23k
  if (hdr->sh_type != SHT_RELA)
13601
151
    return false;
13602
13603
#if DEBUG_SECONDARY_RELOCS
13604
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13605
#endif
13606
1.08k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13607
1.08k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13608
1.23k
}
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.79k
{
13618
9.79k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13619
9.79k
  asection * relsec;
13620
9.79k
  bool result = true;
13621
9.79k
  bfd_vma (*r_sym) (bfd_vma);
13622
9.79k
  ufile_ptr filesize;
13623
13624
9.79k
#if BFD_DEFAULT_TARGET_SIZE > 32
13625
9.79k
  if (bfd_arch_bits_per_address (abfd) != 32)
13626
7.03k
    r_sym = elf64_r_sym;
13627
2.75k
  else
13628
2.75k
#endif
13629
2.75k
    r_sym = elf32_r_sym;
13630
13631
9.79k
  if (!elf_section_data (sec)->has_secondary_relocs)
13632
9.72k
    return true;
13633
13634
  /* Discover if there are any secondary reloc sections
13635
     associated with SEC.  */
13636
69
  filesize = bfd_get_file_size (abfd);
13637
1.63k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13638
1.56k
    {
13639
1.56k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13640
13641
1.56k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13642
74
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13643
69
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13644
69
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13645
69
  {
13646
69
    bfd_byte * native_relocs;
13647
69
    bfd_byte * native_reloc;
13648
69
    arelent * internal_relocs;
13649
69
    arelent * internal_reloc;
13650
69
    size_t i;
13651
69
    unsigned int entsize;
13652
69
    unsigned int symcount;
13653
69
    bfd_size_type reloc_count;
13654
69
    size_t amt;
13655
13656
69
    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
69
    entsize = hdr->sh_entsize;
13664
13665
69
    if (filesize != 0
13666
69
        && ((ufile_ptr) hdr->sh_offset > filesize
13667
62
      || hdr->sh_size > filesize - hdr->sh_offset))
13668
17
      {
13669
17
        bfd_set_error (bfd_error_file_truncated);
13670
17
        result = false;
13671
17
        continue;
13672
17
      }
13673
13674
52
    native_relocs = bfd_malloc (hdr->sh_size);
13675
52
    if (native_relocs == NULL)
13676
0
      {
13677
0
        result = false;
13678
0
        continue;
13679
0
      }
13680
13681
52
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13682
52
    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
52
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13691
52
    if (internal_relocs == NULL)
13692
0
      {
13693
0
        free (native_relocs);
13694
0
        result = false;
13695
0
        continue;
13696
0
      }
13697
13698
52
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13699
52
        || 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
52
    if (dynamic)
13709
0
      symcount = bfd_get_dynamic_symcount (abfd);
13710
52
    else
13711
52
      symcount = bfd_get_symcount (abfd);
13712
13713
52
    for (i = 0, internal_reloc = internal_relocs,
13714
52
     native_reloc = native_relocs;
13715
659
         i < reloc_count;
13716
607
         i++, internal_reloc++, native_reloc += entsize)
13717
607
      {
13718
607
        bool res;
13719
607
        Elf_Internal_Rela rela;
13720
13721
607
        if (entsize == ebd->s->sizeof_rel)
13722
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13723
607
        else /* entsize == ebd->s->sizeof_rela */
13724
607
    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
607
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13731
579
    internal_reloc->address = rela.r_offset;
13732
28
        else
13733
28
    internal_reloc->address = rela.r_offset - sec->vma;
13734
13735
607
        if (r_sym (rela.r_info) == STN_UNDEF)
13736
37
    {
13737
      /* FIXME: This and the error case below mean that we
13738
         have a symbol on relocs that is not elf_symbol_type.  */
13739
37
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13740
37
    }
13741
570
        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
570
        else
13752
570
    {
13753
570
      asymbol **ps;
13754
13755
570
      ps = symbols + r_sym (rela.r_info) - 1;
13756
570
      internal_reloc->sym_ptr_ptr = ps;
13757
      /* Make sure that this symbol is not removed by strip.  */
13758
570
      (*ps)->flags |= BSF_KEEP;
13759
570
    }
13760
13761
607
        internal_reloc->addend = rela.r_addend;
13762
13763
607
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13764
607
        if (! res || internal_reloc->howto == NULL)
13765
46
    {
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
46
      result = false;
13772
46
    }
13773
607
      }
13774
13775
52
    free (native_relocs);
13776
    /* Store the internal relocs.  */
13777
52
    relsec->sec_info = internal_relocs;
13778
52
  }
13779
1.56k
    }
13780
13781
69
  return result;
13782
69
}
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
13
{
13792
13
  asection * isec;
13793
13
  asection * osec;
13794
13
  struct bfd_elf_section_data * esd;
13795
13796
13
  if (isection == NULL)
13797
0
    return false;
13798
13799
13
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13800
13
    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.82k
{
14089
7.82k
#ifdef USE_MMAP
14090
7.82k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14091
7.82k
  if (bed->use_mmap
14092
3.32k
      && sec->compress_status == COMPRESS_SECTION_NONE
14093
3.32k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14094
3.32k
    {
14095
      /* Use mmap only if section size >= the minimum mmap section
14096
   size.  */
14097
3.32k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14098
3.32k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14099
3.32k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14100
554
  {
14101
554
    if (sec->contents != NULL)
14102
0
      {
14103
0
        if (!sec->mmapped_p)
14104
0
    abort ();
14105
0
        *buf = sec->contents;
14106
0
        return true;
14107
0
      }
14108
554
    if (sec->mmapped_p)
14109
0
      abort ();
14110
554
    sec->mmapped_p = 1;
14111
14112
    /* We can't use the final link preallocated buffer for mmap.  */
14113
554
    *buf = NULL;
14114
554
  }
14115
3.32k
    }
14116
7.82k
#endif
14117
  /* FIXME: We should not get here if sec->alloced is set.  */
14118
7.82k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14119
7.82k
  if (ret && sec->mmapped_p)
14120
0
    *buf = sec->contents;
14121
7.82k
  return ret;
14122
7.82k
}
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.82k
{
14129
7.82k
  *buf = NULL;
14130
7.82k
  return elf_mmap_section_contents (abfd, sec, buf);
14131
7.82k
}
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
354k
{
14147
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14148
     CONTENTS may be NULL.  */
14149
354k
  if (contents == NULL)
14150
348k
    return;
14151
14152
6.66k
  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
293
      && (sec->contents == contents
14159
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14160
293
    return;
14161
14162
  /* Don't leave pointers to data we are about to munmap or free.  */
14163
6.36k
  if (sec->contents == contents)
14164
0
    sec->contents = NULL;
14165
6.36k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14166
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14167
14168
6.36k
#ifdef USE_MMAP
14169
6.36k
  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.36k
#endif
14186
14187
6.36k
  free (contents);
14188
6.36k
}
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
}