Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/elf.c
Line
Count
Source (jump to first uncovered line)
1
/* ELF executable support for BFD.
2
3
   Copyright (C) 1993-2025 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 "libiberty.h"
45
#include "safe-ctype.h"
46
#include "elf-linux-core.h"
47
48
#ifdef CORE_HEADER
49
#include CORE_HEADER
50
#endif
51
52
static int elf_sort_sections (const void *, const void *);
53
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55
         struct bfd_link_info *);
56
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57
           file_ptr offset, size_t align);
58
59
/* Swap version information in and out.  The version information is
60
   currently size independent.  If that ever changes, this code will
61
   need to move into elfcode.h.  */
62
63
/* Swap in a Verdef structure.  */
64
65
void
66
_bfd_elf_swap_verdef_in (bfd *abfd,
67
       const Elf_External_Verdef *src,
68
       Elf_Internal_Verdef *dst)
69
19
{
70
19
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
19
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
19
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
19
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
19
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
19
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
19
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
19
}
78
79
/* Swap out a Verdef structure.  */
80
81
void
82
_bfd_elf_swap_verdef_out (bfd *abfd,
83
        const Elf_Internal_Verdef *src,
84
        Elf_External_Verdef *dst)
85
0
{
86
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93
0
}
94
95
/* Swap in a Verdaux structure.  */
96
97
void
98
_bfd_elf_swap_verdaux_in (bfd *abfd,
99
        const Elf_External_Verdaux *src,
100
        Elf_Internal_Verdaux *dst)
101
3
{
102
3
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
103
3
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
104
3
}
105
106
/* Swap out a Verdaux structure.  */
107
108
void
109
_bfd_elf_swap_verdaux_out (bfd *abfd,
110
         const Elf_Internal_Verdaux *src,
111
         Elf_External_Verdaux *dst)
112
0
{
113
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115
0
}
116
117
/* Swap in a Verneed structure.  */
118
119
void
120
_bfd_elf_swap_verneed_in (bfd *abfd,
121
        const Elf_External_Verneed *src,
122
        Elf_Internal_Verneed *dst)
123
870
{
124
870
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
870
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
870
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
870
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
870
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
870
}
130
131
/* Swap out a Verneed structure.  */
132
133
void
134
_bfd_elf_swap_verneed_out (bfd *abfd,
135
         const Elf_Internal_Verneed *src,
136
         Elf_External_Verneed *dst)
137
0
{
138
0
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139
0
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140
0
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141
0
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142
0
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143
0
}
144
145
/* Swap in a Vernaux structure.  */
146
147
void
148
_bfd_elf_swap_vernaux_in (bfd *abfd,
149
        const Elf_External_Vernaux *src,
150
        Elf_Internal_Vernaux *dst)
151
1.24k
{
152
1.24k
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
1.24k
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
1.24k
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
1.24k
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
1.24k
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
1.24k
}
158
159
/* Swap out a Vernaux structure.  */
160
161
void
162
_bfd_elf_swap_vernaux_out (bfd *abfd,
163
         const Elf_Internal_Vernaux *src,
164
         Elf_External_Vernaux *dst)
165
0
{
166
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171
0
}
172
173
/* Swap in a Versym structure.  */
174
175
void
176
_bfd_elf_swap_versym_in (bfd *abfd,
177
       const Elf_External_Versym *src,
178
       Elf_Internal_Versym *dst)
179
77.0k
{
180
77.0k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
77.0k
}
182
183
/* Swap out a Versym structure.  */
184
185
void
186
_bfd_elf_swap_versym_out (bfd *abfd,
187
        const Elf_Internal_Versym *src,
188
        Elf_External_Versym *dst)
189
0
{
190
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191
0
}
192
193
/* Standard ELF hash function.  Do not change this function; you will
194
   cause invalid hash tables to be generated.  */
195
196
unsigned long
197
bfd_elf_hash (const char *namearg)
198
0
{
199
0
  uint32_t h = 0;
200
201
0
  for (const unsigned char *name = (const unsigned char *) namearg;
202
0
       *name; name++)
203
0
    {
204
0
      h = (h << 4) + *name;
205
0
      h ^= (h >> 24) & 0xf0;
206
0
    }
207
0
  return h & 0x0fffffff;
208
0
}
209
210
/* DT_GNU_HASH hash function.  Do not change this function; you will
211
   cause invalid hash tables to be generated.  */
212
213
unsigned long
214
bfd_elf_gnu_hash (const char *namearg)
215
0
{
216
0
  uint32_t h = 5381;
217
218
0
  for (const unsigned char *name = (const unsigned char *) namearg;
219
0
       *name; name++)
220
0
    h = (h << 5) + h + *name;
221
0
  return h;
222
0
}
223
224
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225
   the object_id field of an elf_obj_tdata field set.  */
226
bool
227
bfd_elf_allocate_object (bfd *abfd,
228
       size_t object_size)
229
27.4M
{
230
27.4M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
231
27.4M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
232
27.4M
  if (abfd->tdata.any == NULL)
233
0
    return false;
234
235
27.4M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
236
27.4M
  if (abfd->direction != read_direction)
237
172
    {
238
172
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
239
172
      if (o == NULL)
240
0
  return false;
241
172
      elf_tdata (abfd)->o = o;
242
172
      elf_program_header_size (abfd) = (bfd_size_type) -1;
243
172
    }
244
27.4M
  return true;
245
27.4M
}
246
247
248
bool
249
bfd_elf_make_object (bfd *abfd)
250
11.9M
{
251
11.9M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
252
11.9M
}
253
254
bool
255
bfd_elf_mkcorefile (bfd *abfd)
256
494k
{
257
  /* I think this can be done just like an object file.  */
258
494k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
259
0
    return false;
260
494k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
261
494k
  return elf_tdata (abfd)->core != NULL;
262
494k
}
263
264
char *
265
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
266
574k
{
267
574k
  Elf_Internal_Shdr **i_shdrp;
268
574k
  bfd_byte *shstrtab = NULL;
269
574k
  file_ptr offset;
270
574k
  bfd_size_type shstrtabsize;
271
272
574k
  i_shdrp = elf_elfsections (abfd);
273
574k
  if (i_shdrp == 0
274
574k
      || shindex >= elf_numsections (abfd)
275
574k
      || i_shdrp[shindex] == 0)
276
0
    return NULL;
277
278
574k
  shstrtab = i_shdrp[shindex]->contents;
279
574k
  if (shstrtab == NULL)
280
574k
    {
281
      /* No cached one, attempt to read, and cache what we read.  */
282
574k
      offset = i_shdrp[shindex]->sh_offset;
283
574k
      shstrtabsize = i_shdrp[shindex]->sh_size;
284
285
574k
      if (shstrtabsize == 0
286
574k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
287
574k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
288
16.6k
  {
289
    /* Once we've failed to read it, make sure we don't keep
290
       trying.  Otherwise, we'll keep allocating space for
291
       the string table over and over.  */
292
16.6k
    i_shdrp[shindex]->sh_size = 0;
293
16.6k
  }
294
557k
      else if (shstrtab[shstrtabsize - 1] != 0)
295
83.7k
  {
296
    /* It is an error if a string table isn't terminated.  */
297
83.7k
    _bfd_error_handler
298
      /* xgettext:c-format */
299
83.7k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
300
83.7k
    shstrtab[shstrtabsize - 1] = 0;
301
83.7k
  }
302
574k
      i_shdrp[shindex]->contents = shstrtab;
303
574k
    }
304
574k
  return (char *) shstrtab;
305
574k
}
306
307
char *
308
bfd_elf_string_from_elf_section (bfd *abfd,
309
         unsigned int shindex,
310
         unsigned int strindex)
311
10.7M
{
312
10.7M
  Elf_Internal_Shdr *hdr;
313
314
10.7M
  if (strindex == 0)
315
4.11M
    return "";
316
317
6.61M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318
106
    return NULL;
319
320
6.61M
  hdr = elf_elfsections (abfd)[shindex];
321
322
6.61M
  if (hdr->contents == NULL)
323
591k
    {
324
591k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325
16.5k
  {
326
    /* PR 17512: file: f057ec89.  */
327
    /* xgettext:c-format */
328
16.5k
    _bfd_error_handler (_("%pB: attempt to load strings from"
329
16.5k
        " a non-string section (number %d)"),
330
16.5k
            abfd, shindex);
331
16.5k
    return NULL;
332
16.5k
  }
333
334
574k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335
16.6k
  return NULL;
336
574k
    }
337
6.02M
  else
338
6.02M
    {
339
      /* PR 24273: The string section's contents may have already
340
   been loaded elsewhere, eg because a corrupt file has the
341
   string section index in the ELF header pointing at a group
342
   section.  So be paranoid, and test that the last byte of
343
   the section is zero.  */
344
6.02M
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345
0
  return NULL;
346
6.02M
    }
347
348
6.58M
  if (strindex >= hdr->sh_size)
349
45.5k
    {
350
45.5k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351
45.5k
      _bfd_error_handler
352
  /* xgettext:c-format */
353
45.5k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354
45.5k
   abfd, strindex, (uint64_t) hdr->sh_size,
355
45.5k
   (shindex == shstrndx && strindex == hdr->sh_name
356
45.5k
    ? ".shstrtab"
357
45.5k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
45.5k
      return NULL;
359
45.5k
    }
360
361
6.53M
  return ((char *) hdr->contents) + strindex;
362
6.58M
}
363
364
/* Read and convert symbols to internal format.
365
   SYMCOUNT specifies the number of symbols to read, starting from
366
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367
   are non-NULL, they are used to store the internal symbols, external
368
   symbols, and symbol section index extensions, respectively.
369
   Returns a pointer to the internal symbol buffer (malloced if necessary)
370
   or NULL if there were no symbols or some kind of problem.  */
371
372
Elf_Internal_Sym *
373
bfd_elf_get_elf_syms (bfd *ibfd,
374
          Elf_Internal_Shdr *symtab_hdr,
375
          size_t symcount,
376
          size_t symoffset,
377
          Elf_Internal_Sym *intsym_buf,
378
          void *extsym_buf,
379
          Elf_External_Sym_Shndx *extshndx_buf)
380
78.9k
{
381
78.9k
  Elf_Internal_Shdr *shndx_hdr;
382
78.9k
  void *alloc_ext;
383
78.9k
  const bfd_byte *esym;
384
78.9k
  Elf_External_Sym_Shndx *alloc_extshndx;
385
78.9k
  Elf_External_Sym_Shndx *shndx;
386
78.9k
  Elf_Internal_Sym *alloc_intsym;
387
78.9k
  Elf_Internal_Sym *isym;
388
78.9k
  Elf_Internal_Sym *isymend;
389
78.9k
  const struct elf_backend_data *bed;
390
78.9k
  size_t extsym_size;
391
78.9k
  size_t amt;
392
78.9k
  file_ptr pos;
393
394
78.9k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
0
    abort ();
396
397
78.9k
  if (symcount == 0)
398
0
    return intsym_buf;
399
400
78.9k
  if (elf_use_dt_symtab_p (ibfd))
401
3
    {
402
      /* Use dynamic symbol table.  */
403
3
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
404
0
  {
405
0
    bfd_set_error (bfd_error_invalid_operation);
406
0
    return NULL;
407
0
  }
408
3
      return elf_tdata (ibfd)->dt_symtab + symoffset;
409
3
    }
410
411
  /* Normal syms might have section extension entries.  */
412
78.9k
  shndx_hdr = NULL;
413
78.9k
  if (elf_symtab_shndx_list (ibfd) != NULL)
414
15.7k
    {
415
15.7k
      elf_section_list * entry;
416
15.7k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418
      /* Find an index section that is linked to this symtab section.  */
419
28.4k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420
16.1k
  {
421
    /* PR 20063.  */
422
16.1k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
423
15
      continue;
424
425
16.1k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
426
3.54k
      {
427
3.54k
        shndx_hdr = & entry->hdr;
428
3.54k
        break;
429
12.6k
      };
430
12.6k
  }
431
432
15.7k
      if (shndx_hdr == NULL)
433
12.2k
  {
434
12.2k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
435
      /* Not really accurate, but this was how the old code used
436
         to work.  */
437
12.2k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438
    /* Otherwise we do nothing.  The assumption is that
439
       the index table will not be needed.  */
440
12.2k
  }
441
15.7k
    }
442
443
  /* Read the symbols.  */
444
78.9k
  alloc_ext = NULL;
445
78.9k
  alloc_extshndx = NULL;
446
78.9k
  alloc_intsym = NULL;
447
78.9k
  bed = get_elf_backend_data (ibfd);
448
78.9k
  extsym_size = bed->s->sizeof_sym;
449
78.9k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
450
0
    {
451
0
      bfd_set_error (bfd_error_file_too_big);
452
0
      return NULL;
453
0
    }
454
78.9k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455
78.9k
  size_t alloc_ext_size = amt;
456
78.9k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
457
78.9k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
458
78.3k
            &alloc_ext, ibfd, false))
459
2.80k
    {
460
2.80k
      intsym_buf = NULL;
461
2.80k
      goto out2;
462
2.80k
    }
463
464
76.1k
  size_t alloc_extshndx_size = 0;
465
76.1k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466
64.0k
    extshndx_buf = NULL;
467
12.0k
  else
468
12.0k
    {
469
12.0k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470
0
  {
471
0
    bfd_set_error (bfd_error_file_too_big);
472
0
    intsym_buf = NULL;
473
0
    goto out1;
474
0
  }
475
12.0k
      alloc_extshndx_size = amt;
476
12.0k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477
12.0k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
478
12.0k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
479
11.3k
          &alloc_extshndx_size,
480
11.3k
          (void **) &alloc_extshndx,
481
11.3k
          ibfd, false))
482
1.04k
  {
483
1.04k
    intsym_buf = NULL;
484
1.04k
    goto out1;
485
1.04k
  }
486
12.0k
    }
487
488
75.0k
  if (intsym_buf == NULL)
489
4.82k
    {
490
4.82k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
491
0
  {
492
0
    bfd_set_error (bfd_error_file_too_big);
493
0
    goto out1;
494
0
  }
495
4.82k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
496
4.82k
      intsym_buf = alloc_intsym;
497
4.82k
      if (intsym_buf == NULL)
498
0
  goto out1;
499
4.82k
    }
500
501
  /* Convert the symbols to internal form.  */
502
75.0k
  isymend = intsym_buf + symcount;
503
75.0k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
504
75.0k
     shndx = extshndx_buf;
505
791k
       isym < isymend;
506
716k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
507
721k
    {
508
721k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
509
1.75k
  {
510
1.75k
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
511
    /* xgettext:c-format */
512
1.75k
    _bfd_error_handler (_("%pB symbol number %lu references"
513
1.75k
        " nonexistent SHT_SYMTAB_SHNDX section"),
514
1.75k
            ibfd, (unsigned long) symoffset);
515
1.75k
    free (alloc_intsym);
516
1.75k
    intsym_buf = NULL;
517
1.75k
    goto out1;
518
1.75k
  }
519
520
      /* PR 33019: Do not accept unsupported binding values - they will
521
   likely cause problems later on.  */
522
719k
      int bind = ELF_ST_BIND (isym->st_info);
523
719k
      if (bind > STB_WEAK && bind < STB_LOOS)
524
2.51k
  { 
525
    /* xgettext:c-format */
526
2.51k
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
527
2.51k
            ibfd, (unsigned long) (isym - intsym_buf), bind);
528
2.51k
    free (alloc_intsym);
529
2.51k
    intsym_buf = NULL;
530
2.51k
    goto out1;
531
2.51k
  }
532
533
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
534
717k
      int t = ELF_ST_TYPE (isym->st_info);
535
717k
      if (t == 7)
536
693
  {
537
    /* xgettext:c-format */
538
693
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
539
693
            ibfd, (unsigned long) (isym - intsym_buf), t);
540
693
    free (alloc_intsym);
541
693
    intsym_buf = NULL;
542
693
    goto out1;
543
693
  }
544
717k
    }
545
546
76.1k
 out1:
547
76.1k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
548
78.9k
 out2:
549
78.9k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
550
551
78.9k
  return intsym_buf;
552
76.1k
}
553
554
/* Look up a symbol name.  */
555
static const char *
556
bfd_elf_sym_name_raw (bfd *abfd,
557
          Elf_Internal_Shdr *symtab_hdr,
558
          Elf_Internal_Sym *isym)
559
680k
{
560
680k
  unsigned int iname = isym->st_name;
561
680k
  unsigned int shindex = symtab_hdr->sh_link;
562
563
680k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
564
      /* Check for a bogus st_shndx to avoid crashing.  */
565
680k
      && isym->st_shndx < elf_numsections (abfd))
566
75.6k
    {
567
75.6k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
568
75.6k
      shindex = elf_elfheader (abfd)->e_shstrndx;
569
75.6k
    }
570
571
680k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
572
680k
}
573
574
const char *
575
bfd_elf_sym_name (bfd *abfd,
576
      Elf_Internal_Shdr *symtab_hdr,
577
      Elf_Internal_Sym *isym,
578
      asection *sym_sec)
579
614k
{
580
614k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
581
614k
  if (name == NULL)
582
43.8k
    name = bfd_symbol_error_name;
583
570k
  else if (sym_sec && *name == '\0')
584
0
    name = bfd_section_name (sym_sec);
585
586
614k
  return name;
587
614k
}
588
589
/* Return the name of the group signature symbol.  Why isn't the
590
   signature just a string?  */
591
592
static const char *
593
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
594
79.3k
{
595
79.3k
  Elf_Internal_Shdr *hdr;
596
79.3k
  unsigned char esym[sizeof (Elf64_External_Sym)];
597
79.3k
  Elf_External_Sym_Shndx eshndx;
598
79.3k
  Elf_Internal_Sym isym;
599
600
  /* First we need to ensure the symbol table is available.  Make sure
601
     that it is a symbol table section.  */
602
79.3k
  if (ghdr->sh_link >= elf_numsections (abfd))
603
3
    return NULL;
604
79.3k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
605
79.3k
  if (hdr->sh_type != SHT_SYMTAB
606
79.3k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
607
5.60k
    return NULL;
608
609
  /* Go read the symbol.  */
610
73.7k
  hdr = &elf_tdata (abfd)->symtab_hdr;
611
73.7k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
612
73.7k
          &isym, esym, &eshndx) == NULL)
613
7.31k
    return NULL;
614
615
66.4k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
616
73.7k
}
617
618
static bool
619
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
620
148k
{
621
148k
  return (shdr->sh_size >= minsize
622
148k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
623
148k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
624
148k
    && shdr->bfd_section != NULL);
625
148k
}
626
627
628
/* Set next_in_group, sec_group list pointers, and group names.  */
629
630
static bool
631
process_sht_group_entries (bfd *abfd,
632
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
633
137k
{
634
137k
  unsigned char *contents;
635
636
  /* Read the raw contents.  */
637
137k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
638
2.11k
    {
639
2.11k
      _bfd_error_handler
640
  /* xgettext:c-format */
641
2.11k
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
642
2.11k
      return false;
643
2.11k
    }
644
645
135k
  asection *last_elt = NULL;
646
135k
  const char *gname = NULL;
647
135k
  unsigned char *p = contents + ghdr->sh_size;
648
11.3M
  while (1)
649
11.3M
    {
650
11.3M
      unsigned int idx;
651
11.3M
      Elf_Internal_Shdr *shdr;
652
11.3M
      asection *elt;
653
654
11.3M
      p -= 4;
655
11.3M
      idx = H_GET_32 (abfd, p);
656
11.3M
      if (p == contents)
657
119k
  {
658
119k
    if ((idx & GRP_COMDAT) != 0)
659
47.1k
      ghdr->bfd_section->flags
660
47.1k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
661
119k
    break;
662
119k
  }
663
664
11.2M
      if (idx == 0
665
11.2M
    || idx >= elf_numsections (abfd)
666
11.2M
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
667
11.2M
    || ((elt = shdr->bfd_section) != NULL
668
641k
        && elf_sec_group (elt) != NULL
669
641k
        && elf_sec_group (elt) != ghdr->bfd_section))
670
10.6M
  {
671
10.6M
    _bfd_error_handler
672
10.6M
      (_("%pB: invalid entry (%#x) in group [%u]"),
673
10.6M
       abfd, idx, gidx);
674
10.6M
    continue;
675
10.6M
  }
676
677
      /* PR binutils/23199: According to the ELF gABI all sections in
678
   a group must be marked with SHF_GROUP, but some tools
679
   generate broken objects.  Fix them up here.  */
680
598k
      shdr->sh_flags |= SHF_GROUP;
681
682
598k
      if (elt == NULL)
683
230k
  {
684
230k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
685
122k
      {
686
122k
        const char *name = bfd_elf_string_from_elf_section
687
122k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
688
689
122k
        _bfd_error_handler
690
    /* xgettext:c-format */
691
122k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
692
122k
     abfd, shdr->sh_type, name, gidx);
693
122k
      }
694
230k
    continue;
695
230k
  }
696
697
      /* Don't try to add a section to elf_next_in_group list twice.  */
698
367k
      if (elf_sec_group (elt) != NULL)
699
152k
  continue;
700
701
214k
      if (last_elt == NULL)
702
79.3k
  {
703
    /* Start a circular list with one element.
704
       It will be in reverse order to match what gas does.  */
705
79.3k
    elf_next_in_group (elt) = elt;
706
    /* Point the group section to it.  */
707
79.3k
    elf_next_in_group (ghdr->bfd_section) = elt;
708
79.3k
    gname = group_signature (abfd, ghdr);
709
79.3k
    if (gname == NULL)
710
16.1k
      {
711
16.1k
        free (contents);
712
16.1k
        return false;
713
16.1k
      }
714
79.3k
  }
715
135k
      else
716
135k
  {
717
135k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
718
135k
    elf_next_in_group (last_elt) = elt;
719
135k
  }
720
198k
      last_elt = elt;
721
198k
      elf_group_name (elt) = gname;
722
198k
      elf_sec_group (elt) = ghdr->bfd_section;
723
198k
    }
724
725
119k
  free (contents);
726
119k
  return true;
727
135k
}
728
729
bool
730
_bfd_elf_setup_sections (bfd *abfd)
731
573k
{
732
573k
  bool result = true;
733
734
  /* Process SHF_LINK_ORDER.  */
735
4.74M
  for (asection *s = abfd->sections; s != NULL; s = s->next)
736
4.16M
    {
737
4.16M
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
738
4.16M
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
739
472k
  {
740
472k
    unsigned int elfsec = this_hdr->sh_link;
741
    /* An sh_link value of 0 is now allowed.  It indicates that linked
742
       to section has already been discarded, but that the current
743
       section has been retained for some other reason.  This linking
744
       section is still a candidate for later garbage collection
745
       however.  */
746
472k
    if (elfsec == 0)
747
345k
      {
748
345k
        elf_linked_to_section (s) = NULL;
749
345k
      }
750
127k
    else
751
127k
      {
752
127k
        asection *linksec = NULL;
753
754
127k
        if (elfsec < elf_numsections (abfd))
755
127k
    {
756
127k
      this_hdr = elf_elfsections (abfd)[elfsec];
757
127k
      linksec = this_hdr->bfd_section;
758
127k
    }
759
760
        /* PR 1991, 2008:
761
     Some strip/objcopy may leave an incorrect value in
762
     sh_link.  We don't want to proceed.  */
763
127k
        if (linksec == NULL)
764
5.03k
    {
765
5.03k
      _bfd_error_handler
766
        /* xgettext:c-format */
767
5.03k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
768
5.03k
         s->owner, elfsec, s);
769
5.03k
      result = false;
770
5.03k
    }
771
772
127k
        elf_linked_to_section (s) = linksec;
773
127k
      }
774
472k
  }
775
4.16M
    }
776
777
  /* Process section groups.  */
778
7.76M
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
779
7.18M
    {
780
7.18M
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
781
782
7.18M
      if (shdr && shdr->sh_type == SHT_GROUP)
783
148k
  {
784
148k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
785
140k
      {
786
140k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
787
140k
      && !process_sht_group_entries (abfd, shdr, i))
788
18.2k
    result = false;
789
140k
      }
790
8.12k
    else
791
8.12k
      {
792
        /* PR binutils/18758: Beware of corrupt binaries with
793
     invalid group data.  */
794
8.12k
        _bfd_error_handler
795
    /* xgettext:c-format */
796
8.12k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
797
8.12k
        result = false;
798
8.12k
      }
799
148k
  }
800
7.18M
    }
801
802
573k
  return result;
803
573k
}
804
805
bool
806
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
807
0
{
808
0
  return elf_next_in_group (sec) != NULL;
809
0
}
810
811
const char *
812
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
813
0
{
814
0
  if (elf_sec_group (sec) != NULL)
815
0
    return elf_group_name (sec);
816
0
  return NULL;
817
0
}
818
819
/* Make a BFD section from an ELF section.  We store a pointer to the
820
   BFD section in the bfd_section field of the header.  */
821
822
bool
823
_bfd_elf_make_section_from_shdr (bfd *abfd,
824
         Elf_Internal_Shdr *hdr,
825
         const char *name,
826
         int shindex)
827
5.55M
{
828
5.55M
  asection *newsect;
829
5.55M
  flagword flags;
830
5.55M
  const struct elf_backend_data *bed;
831
5.55M
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
832
833
5.55M
  if (hdr->bfd_section != NULL)
834
407k
    return true;
835
836
5.14M
  newsect = bfd_make_section_anyway (abfd, name);
837
5.14M
  if (newsect == NULL)
838
0
    return false;
839
840
5.14M
  hdr->bfd_section = newsect;
841
5.14M
  elf_section_data (newsect)->this_hdr = *hdr;
842
5.14M
  elf_section_data (newsect)->this_idx = shindex;
843
844
  /* Always use the real type/flags.  */
845
5.14M
  elf_section_type (newsect) = hdr->sh_type;
846
5.14M
  elf_section_flags (newsect) = hdr->sh_flags;
847
848
5.14M
  newsect->filepos = hdr->sh_offset;
849
850
5.14M
  flags = SEC_NO_FLAGS;
851
5.14M
  if (hdr->sh_type != SHT_NOBITS)
852
5.02M
    flags |= SEC_HAS_CONTENTS;
853
5.14M
  if (hdr->sh_type == SHT_GROUP)
854
216k
    flags |= SEC_GROUP;
855
5.14M
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
856
1.69M
    {
857
1.69M
      flags |= SEC_ALLOC;
858
1.69M
      if (hdr->sh_type != SHT_NOBITS)
859
1.61M
  flags |= SEC_LOAD;
860
1.69M
    }
861
5.14M
  if ((hdr->sh_flags & SHF_WRITE) == 0)
862
4.08M
    flags |= SEC_READONLY;
863
5.14M
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
864
997k
    flags |= SEC_CODE;
865
4.14M
  else if ((flags & SEC_LOAD) != 0)
866
869k
    flags |= SEC_DATA;
867
5.14M
  if ((hdr->sh_flags & SHF_MERGE) != 0)
868
808k
    {
869
808k
      flags |= SEC_MERGE;
870
808k
      newsect->entsize = hdr->sh_entsize;
871
808k
    }
872
5.14M
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
873
863k
    {
874
863k
      flags |= SEC_STRINGS;
875
863k
      newsect->entsize = hdr->sh_entsize;
876
863k
    }
877
5.14M
  if ((hdr->sh_flags & SHF_TLS) != 0)
878
484k
    flags |= SEC_THREAD_LOCAL;
879
5.14M
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
880
464k
    flags |= SEC_EXCLUDE;
881
882
5.14M
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
883
5.14M
    {
884
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
885
   but binutils as of 2019-07-23 did not set the EI_OSABI header
886
   byte.  */
887
1.37M
    case ELFOSABI_GNU:
888
2.15M
    case ELFOSABI_FREEBSD:
889
2.15M
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
890
306k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
891
      /* Fall through */
892
3.84M
    case ELFOSABI_NONE:
893
3.84M
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
894
264k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
895
3.84M
      break;
896
5.14M
    }
897
898
5.14M
  if ((flags & SEC_ALLOC) == 0)
899
3.44M
    {
900
      /* The debugging sections appear to be recognized only by name,
901
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
902
3.44M
      if (name [0] == '.')
903
934k
  {
904
934k
    if (startswith (name, ".debug")
905
934k
        || startswith (name, ".gnu.debuglto_.debug_")
906
934k
        || startswith (name, ".gnu.linkonce.wi.")
907
934k
        || startswith (name, ".zdebug"))
908
213k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
909
721k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
910
721k
       || startswith (name, ".note.gnu"))
911
21.5k
      {
912
21.5k
        flags |= SEC_ELF_OCTETS;
913
21.5k
        opb = 1;
914
21.5k
      }
915
700k
    else if (startswith (name, ".line")
916
700k
       || startswith (name, ".stab")
917
700k
       || strcmp (name, ".gdb_index") == 0)
918
35.0k
      flags |= SEC_DEBUGGING;
919
934k
  }
920
3.44M
    }
921
922
5.14M
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
923
5.14M
      || !bfd_set_section_size (newsect, hdr->sh_size)
924
5.14M
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
925
5.14M
              & -hdr->sh_addralign)))
926
3.24k
    return false;
927
928
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
929
     only link a single copy of the section.  This is used to support
930
     g++.  g++ will emit each template expansion in its own section.
931
     The symbols will be defined as weak, so that multiple definitions
932
     are permitted.  The GNU linker extension is to actually discard
933
     all but one of the sections.  */
934
5.14M
  if (startswith (name, ".gnu.linkonce")
935
5.14M
      && elf_next_in_group (newsect) == NULL)
936
19.8k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
937
938
5.14M
  if (!bfd_set_section_flags (newsect, flags))
939
0
    return false;
940
941
5.14M
  bed = get_elf_backend_data (abfd);
942
5.14M
  if (bed->elf_backend_section_flags)
943
1.16M
    if (!bed->elf_backend_section_flags (hdr))
944
0
      return false;
945
946
  /* We do not parse the PT_NOTE segments as we are interested even in the
947
     separate debug info files which may have the segments offsets corrupted.
948
     PT_NOTEs from the core files are currently not parsed using BFD.  */
949
5.14M
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
950
67.4k
    {
951
67.4k
      bfd_byte *contents;
952
953
67.4k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
954
7.29k
  return false;
955
956
60.1k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
957
60.1k
           hdr->sh_offset, hdr->sh_addralign);
958
60.1k
      _bfd_elf_munmap_section_contents (newsect, contents);
959
60.1k
    }
960
961
5.13M
  if ((newsect->flags & SEC_ALLOC) != 0)
962
1.69M
    {
963
1.69M
      Elf_Internal_Phdr *phdr;
964
1.69M
      unsigned int i, nload;
965
966
      /* Some ELF linkers produce binaries with all the program header
967
   p_paddr fields zero.  If we have such a binary with more than
968
   one PT_LOAD header, then leave the section lma equal to vma
969
   so that we don't create sections with overlapping lma.  */
970
1.69M
      phdr = elf_tdata (abfd)->phdr;
971
2.03M
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
972
701k
  if (phdr->p_paddr != 0)
973
360k
    break;
974
341k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
975
68.5k
    ++nload;
976
1.69M
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
977
25.7k
  return true;
978
979
1.66M
      phdr = elf_tdata (abfd)->phdr;
980
22.1M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
981
20.5M
  {
982
20.5M
    if (((phdr->p_type == PT_LOAD
983
20.5M
    && (hdr->sh_flags & SHF_TLS) == 0)
984
20.5M
         || phdr->p_type == PT_TLS)
985
20.5M
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
986
71.9k
      {
987
71.9k
        if ((newsect->flags & SEC_LOAD) == 0)
988
5.15k
    newsect->lma = (phdr->p_paddr
989
5.15k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
990
66.8k
        else
991
    /* We used to use the same adjustment for SEC_LOAD
992
       sections, but that doesn't work if the segment
993
       is packed with code from multiple VMAs.
994
       Instead we calculate the section LMA based on
995
       the segment LMA.  It is assumed that the
996
       segment will contain sections with contiguous
997
       LMAs, even if the VMAs are not.  */
998
66.8k
    newsect->lma = (phdr->p_paddr
999
66.8k
        + hdr->sh_offset - phdr->p_offset) / opb;
1000
1001
        /* With contiguous segments, we can't tell from file
1002
     offsets whether a section with zero size should
1003
     be placed at the end of one segment or the
1004
     beginning of the next.  Decide based on vaddr.  */
1005
71.9k
        if (hdr->sh_addr >= phdr->p_vaddr
1006
71.9k
      && (hdr->sh_addr + hdr->sh_size
1007
71.9k
          <= phdr->p_vaddr + phdr->p_memsz))
1008
69.2k
    break;
1009
71.9k
      }
1010
20.5M
  }
1011
1.66M
    }
1012
1013
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1014
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1015
5.10M
  if ((newsect->flags & SEC_DEBUGGING) != 0
1016
5.10M
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1017
5.10M
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1018
211k
    {
1019
211k
      enum { nothing, compress, decompress } action = nothing;
1020
211k
      int compression_header_size;
1021
211k
      bfd_size_type uncompressed_size;
1022
211k
      unsigned int uncompressed_align_power;
1023
211k
      enum compression_type ch_type = ch_none;
1024
211k
      bool compressed
1025
211k
  = bfd_is_section_compressed_info (abfd, newsect,
1026
211k
            &compression_header_size,
1027
211k
            &uncompressed_size,
1028
211k
            &uncompressed_align_power,
1029
211k
            &ch_type);
1030
1031
      /* Should we decompress?  */
1032
211k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1033
847
  action = decompress;
1034
1035
      /* Should we compress?  Or convert to a different compression?  */
1036
210k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1037
210k
         && newsect->size != 0
1038
210k
         && compression_header_size >= 0
1039
210k
         && uncompressed_size > 0)
1040
12.7k
  {
1041
12.7k
    if (!compressed)
1042
12.7k
      action = compress;
1043
0
    else
1044
0
      {
1045
0
        enum compression_type new_ch_type = ch_none;
1046
0
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1047
0
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1048
0
             ? ch_compress_zstd : ch_compress_zlib);
1049
0
        if (new_ch_type != ch_type)
1050
0
    action = compress;
1051
0
      }
1052
12.7k
  }
1053
1054
211k
      if (action == compress)
1055
12.7k
  {
1056
12.7k
    if (!bfd_init_section_compress_status (abfd, newsect))
1057
20
      {
1058
20
        _bfd_error_handler
1059
    /* xgettext:c-format */
1060
20
    (_("%pB: unable to compress section %s"), abfd, name);
1061
20
        return false;
1062
20
      }
1063
12.7k
  }
1064
198k
      else if (action == decompress)
1065
847
  {
1066
847
    if (!bfd_init_section_decompress_status (abfd, newsect))
1067
270
      {
1068
270
        _bfd_error_handler
1069
    /* xgettext:c-format */
1070
270
    (_("%pB: unable to decompress section %s"), abfd, name);
1071
270
        return false;
1072
270
      }
1073
577
#ifndef HAVE_ZSTD
1074
577
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1075
1
      {
1076
1
        _bfd_error_handler
1077
      /* xgettext:c-format */
1078
1
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1079
1
          "is not built with zstd support"),
1080
1
       abfd, name);
1081
1
        newsect->compress_status = COMPRESS_SECTION_NONE;
1082
1
        return false;
1083
1
      }
1084
576
#endif
1085
576
    if (abfd->is_linker_input
1086
576
        && name[1] == 'z')
1087
0
      {
1088
        /* Rename section from .zdebug_* to .debug_* so that ld
1089
     scripts will see this section as a debug section.  */
1090
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1091
0
        if (new_name == NULL)
1092
0
    return false;
1093
0
        bfd_rename_section (newsect, new_name);
1094
0
      }
1095
576
  }
1096
211k
    }
1097
1098
5.10M
  return true;
1099
5.10M
}
1100
1101
const char *const bfd_elf_section_type_names[] =
1102
{
1103
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1104
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1105
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1106
};
1107
1108
/* ELF relocs are against symbols.  If we are producing relocatable
1109
   output, and the reloc is against an external symbol, and nothing
1110
   has given us any additional addend, the resulting reloc will also
1111
   be against the same symbol.  In such a case, we don't want to
1112
   change anything about the way the reloc is handled, since it will
1113
   all be done at final link time.  Rather than put special case code
1114
   into bfd_perform_relocation, all the reloc types use this howto
1115
   function, or should call this function for relocatable output.  */
1116
1117
bfd_reloc_status_type
1118
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1119
           arelent *reloc_entry,
1120
           asymbol *symbol,
1121
           void *data ATTRIBUTE_UNUSED,
1122
           asection *input_section,
1123
           bfd *output_bfd,
1124
           char **error_message ATTRIBUTE_UNUSED)
1125
300k
{
1126
300k
  if (output_bfd != NULL
1127
300k
      && (symbol->flags & BSF_SECTION_SYM) == 0
1128
300k
      && (! reloc_entry->howto->partial_inplace
1129
0
    || reloc_entry->addend == 0))
1130
0
    {
1131
0
      reloc_entry->address += input_section->output_offset;
1132
0
      return bfd_reloc_ok;
1133
0
    }
1134
1135
  /* In some cases the relocation should be treated as output section
1136
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1137
     targets lack section relative relocations and instead use
1138
     ordinary absolute relocations for references between DWARF
1139
     sections.  That is arguably a bug in those targets but it happens
1140
     to work for the usual case of linking to non-loaded ELF debug
1141
     sections with VMAs forced to zero.  PE COFF on the other hand
1142
     doesn't allow a section VMA of zero.  */
1143
300k
  if (output_bfd == NULL
1144
300k
      && !reloc_entry->howto->pc_relative
1145
300k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1146
300k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1147
43.9k
    reloc_entry->addend -= symbol->section->output_section->vma;
1148
1149
300k
  return bfd_reloc_continue;
1150
300k
}
1151

1152
/* Returns TRUE if section A matches section B.
1153
   Names, addresses and links may be different, but everything else
1154
   should be the same.  */
1155
1156
static bool
1157
section_match (const Elf_Internal_Shdr * a,
1158
         const Elf_Internal_Shdr * b)
1159
3
{
1160
3
  if (a->sh_type != b->sh_type
1161
3
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1162
3
      || a->sh_addralign != b->sh_addralign
1163
3
      || a->sh_entsize != b->sh_entsize)
1164
0
    return false;
1165
3
  if (a->sh_type == SHT_SYMTAB
1166
3
      || a->sh_type == SHT_STRTAB)
1167
1
    return true;
1168
2
  return a->sh_size == b->sh_size;
1169
3
}
1170
1171
/* Find a section in OBFD that has the same characteristics
1172
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1173
   none can be found.  Check's section HINT first, as this is likely
1174
   to be the correct section.  */
1175
1176
static unsigned int
1177
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1178
     const unsigned int hint)
1179
3
{
1180
3
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1181
3
  unsigned int i;
1182
1183
3
  BFD_ASSERT (iheader != NULL);
1184
1185
  /* See PR 20922 for a reproducer of the NULL test.  */
1186
3
  if (hint < elf_numsections (obfd)
1187
3
      && oheaders[hint] != NULL
1188
3
      && section_match (oheaders[hint], iheader))
1189
3
    return hint;
1190
1191
0
  for (i = 1; i < elf_numsections (obfd); i++)
1192
0
    {
1193
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1194
1195
0
      if (oheader == NULL)
1196
0
  continue;
1197
0
      if (section_match (oheader, iheader))
1198
  /* FIXME: Do we care if there is a potential for
1199
     multiple matches ?  */
1200
0
  return i;
1201
0
    }
1202
1203
0
  return SHN_UNDEF;
1204
0
}
1205
1206
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1207
   Processor specific section, based upon a matching input section.
1208
   Returns TRUE upon success, FALSE otherwise.  */
1209
1210
static bool
1211
copy_special_section_fields (const bfd *ibfd,
1212
           bfd *obfd,
1213
           const Elf_Internal_Shdr *iheader,
1214
           Elf_Internal_Shdr *oheader,
1215
           const unsigned int secnum)
1216
125
{
1217
125
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1218
125
  const Elf_Internal_Shdr **iheaders
1219
125
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1220
125
  bool changed = false;
1221
125
  unsigned int sh_link;
1222
1223
125
  if (oheader->sh_type == SHT_NOBITS)
1224
38
    {
1225
      /* This is a feature for objcopy --only-keep-debug:
1226
   When a section's type is changed to NOBITS, we preserve
1227
   the sh_link and sh_info fields so that they can be
1228
   matched up with the original.
1229
1230
   Note: Strictly speaking these assignments are wrong.
1231
   The sh_link and sh_info fields should point to the
1232
   relevent sections in the output BFD, which may not be in
1233
   the same location as they were in the input BFD.  But
1234
   the whole point of this action is to preserve the
1235
   original values of the sh_link and sh_info fields, so
1236
   that they can be matched up with the section headers in
1237
   the original file.  So strictly speaking we may be
1238
   creating an invalid ELF file, but it is only for a file
1239
   that just contains debug info and only for sections
1240
   without any contents.  */
1241
38
      if (oheader->sh_link == 0)
1242
38
  oheader->sh_link = iheader->sh_link;
1243
38
      if (oheader->sh_info == 0)
1244
38
  oheader->sh_info = iheader->sh_info;
1245
38
      return true;
1246
38
    }
1247
1248
  /* Allow the target a chance to decide how these fields should be set.  */
1249
87
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1250
87
                iheader, oheader))
1251
83
    return true;
1252
1253
  /* We have an iheader which might match oheader, and which has non-zero
1254
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1255
     the section in the output bfd which corresponds to the linked section
1256
     in the input bfd.  */
1257
4
  if (iheader->sh_link != SHN_UNDEF)
1258
3
    {
1259
      /* See PR 20931 for a reproducer.  */
1260
3
      if (iheader->sh_link >= elf_numsections (ibfd))
1261
0
  {
1262
0
    _bfd_error_handler
1263
      /* xgettext:c-format */
1264
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1265
0
       ibfd, iheader->sh_link, secnum);
1266
0
    return false;
1267
0
  }
1268
1269
3
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1270
3
      if (sh_link != SHN_UNDEF)
1271
3
  {
1272
3
    oheader->sh_link = sh_link;
1273
3
    changed = true;
1274
3
  }
1275
0
      else
1276
  /* FIXME: Should we install iheader->sh_link
1277
     if we could not find a match ?  */
1278
0
  _bfd_error_handler
1279
    /* xgettext:c-format */
1280
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1281
3
    }
1282
1283
4
  if (iheader->sh_info)
1284
1
    {
1285
      /* The sh_info field can hold arbitrary information, but if the
1286
   SHF_LINK_INFO flag is set then it should be interpreted as a
1287
   section index.  */
1288
1
      if (iheader->sh_flags & SHF_INFO_LINK)
1289
0
  {
1290
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1291
0
             iheader->sh_info);
1292
0
    if (sh_link != SHN_UNDEF)
1293
0
      oheader->sh_flags |= SHF_INFO_LINK;
1294
0
  }
1295
1
      else
1296
  /* No idea what it means - just copy it.  */
1297
1
  sh_link = iheader->sh_info;
1298
1299
1
      if (sh_link != SHN_UNDEF)
1300
1
  {
1301
1
    oheader->sh_info = sh_link;
1302
1
    changed = true;
1303
1
  }
1304
0
      else
1305
0
  _bfd_error_handler
1306
    /* xgettext:c-format */
1307
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1308
1
    }
1309
1310
4
  return changed;
1311
4
}
1312
1313
/* Copy the program header and other data from one object module to
1314
   another.  */
1315
1316
bool
1317
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1318
95
{
1319
95
  const Elf_Internal_Shdr **iheaders
1320
95
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1321
95
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1322
95
  const struct elf_backend_data *bed;
1323
95
  unsigned int i;
1324
1325
95
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1326
95
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1327
0
    return true;
1328
1329
95
  if (!elf_flags_init (obfd))
1330
93
    {
1331
93
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1332
93
      elf_flags_init (obfd) = true;
1333
93
    }
1334
1335
95
  elf_gp (obfd) = elf_gp (ibfd);
1336
1337
  /* Also copy the EI_OSABI field.  */
1338
95
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1339
95
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1340
1341
  /* If set, copy the EI_ABIVERSION field.  */
1342
95
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1343
0
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1344
0
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1345
1346
  /* Copy object attributes.  */
1347
95
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1348
1349
95
  if (iheaders == NULL || oheaders == NULL)
1350
49
    return true;
1351
1352
46
  bed = get_elf_backend_data (obfd);
1353
1354
  /* Possibly copy other fields in the section header.  */
1355
4.14k
  for (i = 1; i < elf_numsections (obfd); i++)
1356
4.09k
    {
1357
4.09k
      unsigned int j;
1358
4.09k
      Elf_Internal_Shdr * oheader = oheaders[i];
1359
1360
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1361
   because of a special case need for generating separate debug info
1362
   files.  See below for more details.  */
1363
4.09k
      if (oheader == NULL
1364
4.09k
    || (oheader->sh_type != SHT_NOBITS
1365
4.09k
        && oheader->sh_type < SHT_LOOS))
1366
3.96k
  continue;
1367
1368
      /* Ignore empty sections, and sections whose
1369
   fields have already been initialised.  */
1370
138
      if (oheader->sh_size == 0
1371
138
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1372
13
  continue;
1373
1374
      /* Scan for the matching section in the input bfd.
1375
   First we try for a direct mapping between the input and
1376
   output sections.  */
1377
8.87k
      for (j = 1; j < elf_numsections (ibfd); j++)
1378
8.87k
  {
1379
8.87k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1380
1381
8.87k
    if (iheader == NULL)
1382
0
      continue;
1383
1384
8.87k
    if (oheader->bfd_section != NULL
1385
8.87k
        && iheader->bfd_section != NULL
1386
8.87k
        && iheader->bfd_section->output_section != NULL
1387
8.87k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1388
125
      {
1389
        /* We have found a connection from the input section to
1390
     the output section.  Attempt to copy the header fields.
1391
     If this fails then do not try any further sections -
1392
     there should only be a one-to-one mapping between
1393
     input and output.  */
1394
125
        if (!copy_special_section_fields (ibfd, obfd,
1395
125
            iheader, oheader, i))
1396
1
    j = elf_numsections (ibfd);
1397
125
        break;
1398
125
      }
1399
8.87k
  }
1400
1401
125
      if (j < elf_numsections (ibfd))
1402
124
  continue;
1403
1404
      /* That failed.  So try to deduce the corresponding input section.
1405
   Unfortunately we cannot compare names as the output string table
1406
   is empty, so instead we check size, address and type.  */
1407
28
      for (j = 1; j < elf_numsections (ibfd); j++)
1408
27
  {
1409
27
    const Elf_Internal_Shdr * iheader = iheaders[j];
1410
1411
27
    if (iheader == NULL)
1412
0
      continue;
1413
1414
    /* Try matching fields in the input section's header.
1415
       Since --only-keep-debug turns all non-debug sections into
1416
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1417
       input type.  */
1418
27
    if ((oheader->sh_type == SHT_NOBITS
1419
27
         || iheader->sh_type == oheader->sh_type)
1420
27
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1421
1
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1422
27
        && iheader->sh_addralign == oheader->sh_addralign
1423
27
        && iheader->sh_entsize == oheader->sh_entsize
1424
27
        && iheader->sh_size == oheader->sh_size
1425
27
        && iheader->sh_addr == oheader->sh_addr
1426
27
        && (iheader->sh_info != oheader->sh_info
1427
1
      || iheader->sh_link != oheader->sh_link))
1428
0
      {
1429
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1430
0
    break;
1431
0
      }
1432
27
  }
1433
1434
1
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1435
1
  {
1436
    /* Final attempt.  Call the backend copy function
1437
       with a NULL input section.  */
1438
1
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1439
1
                     NULL, oheader);
1440
1
  }
1441
1
    }
1442
1443
46
  return true;
1444
95
}
1445
1446
static const char *
1447
get_segment_type (unsigned int p_type)
1448
339k
{
1449
339k
  const char *pt;
1450
339k
  switch (p_type)
1451
339k
    {
1452
58.7k
    case PT_NULL: pt = "NULL"; break;
1453
5.11k
    case PT_LOAD: pt = "LOAD"; break;
1454
2.02k
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1455
972
    case PT_INTERP: pt = "INTERP"; break;
1456
647
    case PT_NOTE: pt = "NOTE"; break;
1457
373
    case PT_SHLIB: pt = "SHLIB"; break;
1458
2.59k
    case PT_PHDR: pt = "PHDR"; break;
1459
145
    case PT_TLS: pt = "TLS"; break;
1460
338
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1461
307
    case PT_GNU_STACK: pt = "STACK"; break;
1462
280
    case PT_GNU_RELRO: pt = "RELRO"; break;
1463
1
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1464
268k
    default: pt = NULL; break;
1465
339k
    }
1466
339k
  return pt;
1467
339k
}
1468
1469
/* Print out the program headers.  */
1470
1471
bool
1472
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1473
7.53k
{
1474
7.53k
  FILE *f = (FILE *) farg;
1475
7.53k
  Elf_Internal_Phdr *p;
1476
7.53k
  asection *s;
1477
7.53k
  bfd_byte *dynbuf = NULL;
1478
1479
7.53k
  p = elf_tdata (abfd)->phdr;
1480
7.53k
  if (p != NULL)
1481
6.43k
    {
1482
6.43k
      unsigned int i, c;
1483
1484
6.43k
      fprintf (f, _("\nProgram Header:\n"));
1485
6.43k
      c = elf_elfheader (abfd)->e_phnum;
1486
346k
      for (i = 0; i < c; i++, p++)
1487
339k
  {
1488
339k
    const char *pt = get_segment_type (p->p_type);
1489
339k
    char buf[20];
1490
1491
339k
    if (pt == NULL)
1492
268k
      {
1493
268k
        sprintf (buf, "0x%lx", p->p_type);
1494
268k
        pt = buf;
1495
268k
      }
1496
339k
    fprintf (f, "%8s off    0x", pt);
1497
339k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1498
339k
    fprintf (f, " vaddr 0x");
1499
339k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1500
339k
    fprintf (f, " paddr 0x");
1501
339k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1502
339k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1503
339k
    fprintf (f, "         filesz 0x");
1504
339k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1505
339k
    fprintf (f, " memsz 0x");
1506
339k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1507
339k
    fprintf (f, " flags %c%c%c",
1508
339k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1509
339k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1510
339k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1511
339k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1512
261k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1513
339k
    fprintf (f, "\n");
1514
339k
  }
1515
6.43k
    }
1516
1517
7.53k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1518
7.53k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1519
506
    {
1520
506
      unsigned int elfsec;
1521
506
      unsigned long shlink;
1522
506
      bfd_byte *extdyn, *extdynend;
1523
506
      size_t extdynsize;
1524
506
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1525
1526
506
      fprintf (f, _("\nDynamic Section:\n"));
1527
1528
506
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1529
8
  goto error_return;
1530
1531
498
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1532
498
      if (elfsec == SHN_BAD)
1533
0
  goto error_return;
1534
498
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1535
1536
498
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1537
498
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1538
1539
498
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1540
10.3k
     (size_t) (extdynend - extdyn) >= extdynsize;
1541
9.88k
     extdyn += extdynsize)
1542
10.2k
  {
1543
10.2k
    Elf_Internal_Dyn dyn;
1544
10.2k
    const char *name = "";
1545
10.2k
    char ab[20];
1546
10.2k
    bool stringp;
1547
10.2k
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1548
1549
10.2k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1550
1551
10.2k
    if (dyn.d_tag == DT_NULL)
1552
384
      break;
1553
1554
9.89k
    stringp = false;
1555
9.89k
    switch (dyn.d_tag)
1556
9.89k
      {
1557
3.06k
      default:
1558
3.06k
        if (bed->elf_backend_get_target_dtag)
1559
32
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1560
1561
3.06k
        if (!strcmp (name, ""))
1562
3.03k
    {
1563
3.03k
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1564
3.03k
      name = ab;
1565
3.03k
    }
1566
3.06k
        break;
1567
1568
637
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1569
246
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1570
295
      case DT_PLTGOT: name = "PLTGOT"; break;
1571
175
      case DT_HASH: name = "HASH"; break;
1572
287
      case DT_STRTAB: name = "STRTAB"; break;
1573
289
      case DT_SYMTAB: name = "SYMTAB"; break;
1574
237
      case DT_RELA: name = "RELA"; break;
1575
255
      case DT_RELASZ: name = "RELASZ"; break;
1576
229
      case DT_RELAENT: name = "RELAENT"; break;
1577
285
      case DT_STRSZ: name = "STRSZ"; break;
1578
277
      case DT_SYMENT: name = "SYMENT"; break;
1579
281
      case DT_INIT: name = "INIT"; break;
1580
283
      case DT_FINI: name = "FINI"; break;
1581
5
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1582
79
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1583
10
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1584
62
      case DT_REL: name = "REL"; break;
1585
74
      case DT_RELSZ: name = "RELSZ"; break;
1586
48
      case DT_RELENT: name = "RELENT"; break;
1587
3
      case DT_RELR: name = "RELR"; break;
1588
4
      case DT_RELRSZ: name = "RELRSZ"; break;
1589
10
      case DT_RELRENT: name = "RELRENT"; break;
1590
229
      case DT_PLTREL: name = "PLTREL"; break;
1591
273
      case DT_DEBUG: name = "DEBUG"; break;
1592
4
      case DT_TEXTREL: name = "TEXTREL"; break;
1593
229
      case DT_JMPREL: name = "JMPREL"; break;
1594
13
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1595
134
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1596
133
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1597
135
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1598
138
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1599
61
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1600
124
      case DT_FLAGS: name = "FLAGS"; break;
1601
7
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1602
3
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1603
60
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1604
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1605
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1606
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1607
60
      case DT_FEATURE: name = "FEATURE"; break;
1608
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1609
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1610
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1611
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1612
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1613
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1614
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1615
1
      case DT_MOVETAB: name = "MOVETAB"; break;
1616
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1617
110
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1618
14
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1619
158
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1620
198
      case DT_VERSYM: name = "VERSYM"; break;
1621
0
      case DT_VERDEF: name = "VERDEF"; break;
1622
0
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1623
257
      case DT_VERNEED: name = "VERNEED"; break;
1624
259
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1625
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1626
0
      case DT_USED: name = "USED"; break;
1627
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1628
160
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1629
9.89k
      }
1630
1631
9.89k
    fprintf (f, "  %-20s ", name);
1632
9.89k
    if (! stringp)
1633
9.11k
      {
1634
9.11k
        fprintf (f, "0x");
1635
9.11k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1636
9.11k
      }
1637
782
    else
1638
782
      {
1639
782
        const char *string;
1640
782
        unsigned int tagv = dyn.d_un.d_val;
1641
1642
782
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1643
782
        if (string == NULL)
1644
12
    goto error_return;
1645
770
        fprintf (f, "%s", string);
1646
770
      }
1647
9.88k
    fprintf (f, "\n");
1648
9.88k
  }
1649
1650
486
      _bfd_elf_munmap_section_contents (s, dynbuf);
1651
486
      dynbuf = NULL;
1652
486
    }
1653
1654
7.51k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1655
7.51k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1656
492
    {
1657
492
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1658
170
  return false;
1659
492
    }
1660
1661
7.34k
  if (elf_dynverdef (abfd) != 0)
1662
0
    {
1663
0
      Elf_Internal_Verdef *t;
1664
1665
0
      fprintf (f, _("\nVersion definitions:\n"));
1666
0
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1667
0
  {
1668
0
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1669
0
       t->vd_flags, t->vd_hash,
1670
0
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1671
0
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1672
0
      {
1673
0
        Elf_Internal_Verdaux *a;
1674
1675
0
        fprintf (f, "\t");
1676
0
        for (a = t->vd_auxptr->vda_nextptr;
1677
0
       a != NULL;
1678
0
       a = a->vda_nextptr)
1679
0
    fprintf (f, "%s ",
1680
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1681
0
        fprintf (f, "\n");
1682
0
      }
1683
0
  }
1684
0
    }
1685
1686
7.34k
  if (elf_dynverref (abfd) != 0)
1687
322
    {
1688
322
      Elf_Internal_Verneed *t;
1689
1690
322
      fprintf (f, _("\nVersion References:\n"));
1691
869
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1692
547
  {
1693
547
    Elf_Internal_Vernaux *a;
1694
1695
547
    fprintf (f, _("  required from %s:\n"),
1696
547
       t->vn_filename ? t->vn_filename : "<corrupt>");
1697
1.52k
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1698
975
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1699
975
         a->vna_flags, a->vna_other,
1700
975
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1701
547
  }
1702
322
    }
1703
1704
7.34k
  return true;
1705
1706
20
 error_return:
1707
20
  _bfd_elf_munmap_section_contents (s, dynbuf);
1708
20
  return false;
1709
7.51k
}
1710
1711
/* Find the file offset corresponding to VMA by using the program
1712
   headers.  */
1713
1714
static file_ptr
1715
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1716
     size_t size, size_t *max_size_p)
1717
4.51k
{
1718
4.51k
  Elf_Internal_Phdr *seg;
1719
4.51k
  size_t i;
1720
1721
16.8k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1722
16.3k
    if (seg->p_type == PT_LOAD
1723
16.3k
  && vma >= (seg->p_vaddr & -seg->p_align)
1724
16.3k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1725
4.05k
      {
1726
4.05k
  if (max_size_p)
1727
71
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1728
4.05k
  return vma - seg->p_vaddr + seg->p_offset;
1729
4.05k
      }
1730
1731
458
  if (max_size_p)
1732
40
    *max_size_p = 0;
1733
458
  bfd_set_error (bfd_error_invalid_operation);
1734
458
  return (file_ptr) -1;
1735
4.51k
}
1736
1737
/* Convert hash table to internal form.  */
1738
1739
static bfd_vma *
1740
get_hash_table_data (bfd *abfd, bfd_size_type number,
1741
         unsigned int ent_size, bfd_size_type filesize)
1742
679
{
1743
679
  unsigned char *e_data = NULL;
1744
679
  bfd_vma *i_data = NULL;
1745
679
  bfd_size_type size;
1746
679
  void *e_data_addr;
1747
679
  size_t e_data_size ATTRIBUTE_UNUSED;
1748
1749
679
  if (ent_size != 4 && ent_size != 8)
1750
0
    return NULL;
1751
1752
679
  if ((size_t) number != number)
1753
0
    {
1754
0
      bfd_set_error (bfd_error_file_too_big);
1755
0
      return NULL;
1756
0
    }
1757
1758
679
  size = ent_size * number;
1759
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1760
     attempting to allocate memory when the read is bound to fail.  */
1761
679
  if (size > filesize
1762
679
      || number >= ~(size_t) 0 / ent_size
1763
679
      || number >= ~(size_t) 0 / sizeof (*i_data))
1764
58
    {
1765
58
      bfd_set_error (bfd_error_file_too_big);
1766
58
      return NULL;
1767
58
    }
1768
1769
621
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1770
621
  if (e_data == NULL)
1771
43
    return NULL;
1772
1773
578
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1774
578
  if (i_data == NULL)
1775
0
    {
1776
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1777
0
      return NULL;
1778
0
    }
1779
1780
578
  if (ent_size == 4)
1781
793k
    while (number--)
1782
792k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1783
0
  else
1784
0
    while (number--)
1785
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1786
1787
578
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1788
578
  return i_data;
1789
578
}
1790
1791
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1792
   support DT_MIPS_XHASH?  */
1793
584k
#define DT_MIPS_XHASH        0x70000036
1794
1795
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1796
1797
bool
1798
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1799
            Elf_Internal_Phdr *phdrs, size_t phnum,
1800
            bfd_size_type filesize)
1801
16.0k
{
1802
16.0k
  bfd_byte *extdyn, *extdynend;
1803
16.0k
  size_t extdynsize;
1804
16.0k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1805
16.0k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1806
16.0k
        Elf_Internal_Sym *);
1807
16.0k
  Elf_Internal_Dyn dyn;
1808
16.0k
  bfd_vma dt_hash = 0;
1809
16.0k
  bfd_vma dt_gnu_hash = 0;
1810
16.0k
  bfd_vma dt_mips_xhash = 0;
1811
16.0k
  bfd_vma dt_strtab = 0;
1812
16.0k
  bfd_vma dt_symtab = 0;
1813
16.0k
  size_t dt_strsz = 0;
1814
16.0k
  bfd_vma dt_versym = 0;
1815
16.0k
  bfd_vma dt_verdef = 0;
1816
16.0k
  bfd_vma dt_verneed = 0;
1817
16.0k
  bfd_byte *dynbuf = NULL;
1818
16.0k
  char *strbuf = NULL;
1819
16.0k
  bfd_vma *gnubuckets = NULL;
1820
16.0k
  bfd_vma *gnuchains = NULL;
1821
16.0k
  bfd_vma *mipsxlat = NULL;
1822
16.0k
  file_ptr saved_filepos, filepos;
1823
16.0k
  bool res = false;
1824
16.0k
  size_t amt;
1825
16.0k
  bfd_byte *esymbuf = NULL, *esym;
1826
16.0k
  bfd_size_type symcount;
1827
16.0k
  Elf_Internal_Sym *isymbuf = NULL;
1828
16.0k
  Elf_Internal_Sym *isym, *isymend;
1829
16.0k
  bfd_byte *versym = NULL;
1830
16.0k
  bfd_byte *verdef = NULL;
1831
16.0k
  bfd_byte *verneed = NULL;
1832
16.0k
  size_t verdef_size = 0;
1833
16.0k
  size_t verneed_size = 0;
1834
16.0k
  size_t extsym_size;
1835
16.0k
  const struct elf_backend_data *bed;
1836
16.0k
  void *dynbuf_addr = NULL;
1837
16.0k
  void *esymbuf_addr = NULL;
1838
16.0k
  size_t dynbuf_size = 0;
1839
16.0k
  size_t esymbuf_size = 0;
1840
1841
  /* Return TRUE if symbol table is bad.  */
1842
16.0k
  if (elf_bad_symtab (abfd))
1843
0
    return true;
1844
1845
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1846
16.0k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1847
0
    return true;
1848
1849
16.0k
  bed = get_elf_backend_data (abfd);
1850
1851
  /* Save file position for elf_object_p.  */
1852
16.0k
  saved_filepos = bfd_tell (abfd);
1853
1854
16.0k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1855
0
    goto error_return;
1856
1857
16.0k
  dynbuf_size = phdr->p_filesz;
1858
16.0k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1859
16.0k
  if (dynbuf == NULL)
1860
1.49k
    goto error_return;
1861
1862
14.5k
  extsym_size = bed->s->sizeof_sym;
1863
14.5k
  extdynsize = bed->s->sizeof_dyn;
1864
14.5k
  swap_dyn_in = bed->s->swap_dyn_in;
1865
1866
14.5k
  extdyn = dynbuf;
1867
14.5k
  if (phdr->p_filesz < extdynsize)
1868
1.17k
    goto error_return;
1869
13.3k
  extdynend = extdyn + phdr->p_filesz;
1870
321k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1871
314k
    {
1872
314k
      swap_dyn_in (abfd, extdyn, &dyn);
1873
1874
314k
      if (dyn.d_tag == DT_NULL)
1875
5.02k
  break;
1876
1877
309k
      switch (dyn.d_tag)
1878
309k
  {
1879
2.64k
  case DT_HASH:
1880
2.64k
    dt_hash = dyn.d_un.d_val;
1881
2.64k
    break;
1882
1.36k
  case DT_GNU_HASH:
1883
1.36k
    if (bed->elf_machine_code != EM_MIPS
1884
1.36k
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1885
1.14k
      dt_gnu_hash = dyn.d_un.d_val;
1886
1.36k
    break;
1887
3.55k
  case DT_STRTAB:
1888
3.55k
    dt_strtab = dyn.d_un.d_val;
1889
3.55k
    break;
1890
3.66k
  case DT_SYMTAB:
1891
3.66k
    dt_symtab = dyn.d_un.d_val;
1892
3.66k
    break;
1893
2.62k
  case DT_STRSZ:
1894
2.62k
    dt_strsz = dyn.d_un.d_val;
1895
2.62k
    break;
1896
1.92k
  case DT_SYMENT:
1897
1.92k
    if (dyn.d_un.d_val != extsym_size)
1898
1.42k
      goto error_return;
1899
503
    break;
1900
596
  case DT_VERSYM:
1901
596
    dt_versym = dyn.d_un.d_val;
1902
596
    break;
1903
377
  case DT_VERDEF:
1904
377
    dt_verdef = dyn.d_un.d_val;
1905
377
    break;
1906
821
  case DT_VERNEED:
1907
821
    dt_verneed = dyn.d_un.d_val;
1908
821
    break;
1909
292k
  default:
1910
292k
    if (dyn.d_tag == DT_MIPS_XHASH
1911
292k
        && (bed->elf_machine_code == EM_MIPS
1912
60
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1913
7
      {
1914
7
        dt_gnu_hash = dyn.d_un.d_val;
1915
7
        dt_mips_xhash = dyn.d_un.d_val;
1916
7
      }
1917
292k
    break;
1918
309k
  }
1919
309k
    }
1920
1921
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1922
     segment.  */
1923
11.9k
  if ((!dt_hash && !dt_gnu_hash)
1924
11.9k
      || !dt_strtab
1925
11.9k
      || !dt_symtab
1926
11.9k
      || !dt_strsz)
1927
10.5k
    goto error_return;
1928
1929
  /* Get dynamic string table.  */
1930
1.41k
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1931
1.41k
  if (filepos == (file_ptr) -1
1932
1.41k
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1933
223
    goto error_return;
1934
1935
  /* Dynamic string table must be valid until ABFD is closed.  */
1936
1.19k
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1937
1.19k
  if (strbuf == NULL)
1938
38
    goto error_return;
1939
1.15k
  if (strbuf[dt_strsz - 1] != 0)
1940
372
    {
1941
      /* It is an error if a string table is't terminated.  */
1942
372
      _bfd_error_handler
1943
  /* xgettext:c-format */
1944
372
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
1945
372
      strbuf[dt_strsz - 1] = 0;
1946
372
    }
1947
1948
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
1949
     DT_HASH since it is simpler than DT_GNU_HASH.  */
1950
1.15k
  if (dt_hash)
1951
507
    {
1952
507
      unsigned char nb[16];
1953
507
      unsigned int hash_ent_size;
1954
1955
507
      switch (bed->elf_machine_code)
1956
507
  {
1957
0
  case EM_ALPHA:
1958
0
  case EM_S390:
1959
0
  case EM_S390_OLD:
1960
0
    if (bed->s->elfclass == ELFCLASS64)
1961
0
      {
1962
0
        hash_ent_size = 8;
1963
0
        break;
1964
0
      }
1965
    /* FALLTHROUGH */
1966
507
  default:
1967
507
    hash_ent_size = 4;
1968
507
    break;
1969
507
  }
1970
1971
507
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
1972
507
         NULL);
1973
507
      if (filepos == (file_ptr) -1
1974
507
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
1975
507
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1976
99
  goto error_return;
1977
1978
      /* The number of dynamic symbol table entries equals the number
1979
   of chains.  */
1980
408
      if (hash_ent_size == 8)
1981
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1982
408
      else
1983
408
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1984
408
    }
1985
646
  else
1986
646
    {
1987
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1988
   bindings are in hash table.  Since in dynamic symbol table,
1989
   all symbols with STB_LOCAL binding are placed before symbols
1990
   with other bindings and all undefined symbols are placed
1991
   before defined ones, the highest symbol index in DT_GNU_HASH
1992
   is the highest dynamic symbol table index.  */
1993
646
      unsigned char nb[16];
1994
646
      bfd_vma ngnubuckets;
1995
646
      bfd_vma gnusymidx;
1996
646
      size_t i, ngnuchains;
1997
646
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
1998
646
      bfd_vma buckets_vma;
1999
2000
646
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2001
646
         sizeof (nb), NULL);
2002
646
      if (filepos == (file_ptr) -1
2003
646
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2004
646
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2005
76
  goto error_return;
2006
2007
570
      ngnubuckets = bfd_get_32 (abfd, nb);
2008
570
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2009
570
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2010
570
      buckets_vma = dt_gnu_hash + 16;
2011
570
      if (bed->s->elfclass == ELFCLASS32)
2012
0
  buckets_vma += bitmaskwords * 4;
2013
570
      else
2014
570
  buckets_vma += bitmaskwords * 8;
2015
570
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2016
570
      if (filepos == (file_ptr) -1
2017
570
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2018
68
  goto error_return;
2019
2020
502
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2021
502
      if (gnubuckets == NULL)
2022
82
  goto error_return;
2023
2024
466k
      for (i = 0; i < ngnubuckets; i++)
2025
466k
  if (gnubuckets[i] != 0)
2026
342k
    {
2027
342k
      if (gnubuckets[i] < gnusymidx)
2028
33
        goto error_return;
2029
2030
342k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2031
33.6k
        maxchain = gnubuckets[i];
2032
342k
    }
2033
2034
387
      if (maxchain == 0xffffffff)
2035
46
  {
2036
46
    symcount = 0;
2037
46
    goto empty_gnu_hash;
2038
46
  }
2039
2040
341
      maxchain -= gnusymidx;
2041
341
      filepos = offset_from_vma (phdrs, phnum,
2042
341
         buckets_vma + 4 * (ngnubuckets + maxchain),
2043
341
         4, NULL);
2044
341
      if (filepos == (file_ptr) -1
2045
341
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2046
134
  goto error_return;
2047
2048
207
      do
2049
4.24k
  {
2050
4.24k
    if (bfd_read (nb, 4, abfd) != 4)
2051
30
      goto error_return;
2052
4.21k
    ++maxchain;
2053
4.21k
    if (maxchain == 0)
2054
0
      goto error_return;
2055
4.21k
  }
2056
4.21k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2057
2058
177
      filepos = offset_from_vma (phdrs, phnum,
2059
177
         buckets_vma + 4 * ngnubuckets,
2060
177
         4, NULL);
2061
177
      if (filepos == (file_ptr) -1
2062
177
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2063
0
  goto error_return;
2064
2065
177
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2066
177
      if (gnuchains == NULL)
2067
19
  goto error_return;
2068
158
      ngnuchains = maxchain;
2069
2070
158
      if (dt_mips_xhash)
2071
0
  {
2072
0
    filepos = offset_from_vma (phdrs, phnum,
2073
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2074
0
             4, NULL);
2075
0
    if (filepos == (file_ptr) -1
2076
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2077
0
      goto error_return;
2078
2079
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2080
0
    if (mipsxlat == NULL)
2081
0
      goto error_return;
2082
0
  }
2083
2084
158
      symcount = 0;
2085
80.8k
      for (i = 0; i < ngnubuckets; ++i)
2086
80.6k
  if (gnubuckets[i] != 0)
2087
46.9k
    {
2088
46.9k
      bfd_vma si = gnubuckets[i];
2089
46.9k
      bfd_vma off = si - gnusymidx;
2090
46.9k
      do
2091
933k
        {
2092
933k
    if (mipsxlat)
2093
0
      {
2094
0
        if (mipsxlat[off] >= symcount)
2095
0
          symcount = mipsxlat[off] + 1;
2096
0
      }
2097
933k
    else
2098
933k
      {
2099
933k
        if (si >= symcount)
2100
4.02k
          symcount = si + 1;
2101
933k
      }
2102
933k
    si++;
2103
933k
        }
2104
933k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2105
46.9k
    }
2106
158
    }
2107
2108
  /* Swap in dynamic symbol table.  */
2109
566
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2110
0
    {
2111
0
      bfd_set_error (bfd_error_file_too_big);
2112
0
      goto error_return;
2113
0
    }
2114
2115
566
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2116
566
  if (filepos == (file_ptr) -1
2117
566
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2118
106
    goto error_return;
2119
460
  esymbuf_size = amt;
2120
460
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2121
460
         &esymbuf_addr, &esymbuf_size);
2122
460
  if (esymbuf == NULL)
2123
142
    goto error_return;
2124
2125
318
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2126
0
    {
2127
0
      bfd_set_error (bfd_error_file_too_big);
2128
0
      goto error_return;
2129
0
    }
2130
2131
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2132
318
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2133
318
  if (isymbuf == NULL)
2134
0
    goto error_return;
2135
2136
318
  swap_symbol_in = bed->s->swap_symbol_in;
2137
2138
  /* Convert the symbols to internal form.  */
2139
318
  isymend = isymbuf + symcount;
2140
318
  for (esym = esymbuf, isym = isymbuf;
2141
1.65k
       isym < isymend;
2142
1.33k
       esym += extsym_size, isym++)
2143
1.42k
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2144
1.42k
  || isym->st_name >= dt_strsz)
2145
91
      {
2146
91
  bfd_set_error (bfd_error_invalid_operation);
2147
91
  goto error_return;
2148
91
      }
2149
2150
227
  if (dt_versym)
2151
185
    {
2152
      /* Swap in DT_VERSYM.  */
2153
185
      if (_bfd_mul_overflow (symcount, 2, &amt))
2154
0
  {
2155
0
    bfd_set_error (bfd_error_file_too_big);
2156
0
    goto error_return;
2157
0
  }
2158
2159
185
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2160
185
      if (filepos == (file_ptr) -1
2161
185
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2162
58
  goto error_return;
2163
2164
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2165
127
      versym = _bfd_mmap_persistent (abfd, amt);
2166
2167
127
      if (dt_verdef)
2168
70
  {
2169
    /* Read in DT_VERDEF.  */
2170
70
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2171
70
             0, &verdef_size);
2172
70
    if (filepos == (file_ptr) -1
2173
70
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2174
55
      goto error_return;
2175
2176
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2177
15
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2178
15
  }
2179
2180
72
      if (dt_verneed)
2181
41
  {
2182
    /* Read in DT_VERNEED.  */
2183
41
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2184
41
             0, &verneed_size);
2185
41
    if (filepos == (file_ptr) -1
2186
41
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2187
20
      goto error_return;
2188
2189
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2190
21
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2191
21
  }
2192
72
    }
2193
2194
140
 empty_gnu_hash:
2195
140
  elf_tdata (abfd)->dt_strtab = strbuf;
2196
140
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2197
140
  elf_tdata (abfd)->dt_symtab = isymbuf;
2198
140
  elf_tdata (abfd)->dt_symtab_count = symcount;
2199
140
  elf_tdata (abfd)->dt_versym = versym;
2200
140
  elf_tdata (abfd)->dt_verdef = verdef;
2201
140
  elf_tdata (abfd)->dt_verneed = verneed;
2202
140
  elf_tdata (abfd)->dt_verdef_count
2203
140
    = verdef_size / sizeof (Elf_External_Verdef);
2204
140
  elf_tdata (abfd)->dt_verneed_count
2205
140
    = verneed_size / sizeof (Elf_External_Verneed);
2206
2207
140
  res = true;
2208
2209
16.0k
 error_return:
2210
  /* Restore file position for elf_object_p.  */
2211
16.0k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2212
0
    res = false;
2213
16.0k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2214
16.0k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2215
16.0k
  free (gnubuckets);
2216
16.0k
  free (gnuchains);
2217
16.0k
  free (mipsxlat);
2218
16.0k
  return res;
2219
140
}
2220
2221
/* Reconstruct section from dynamic symbol.  */
2222
2223
asection *
2224
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2225
            Elf_Internal_Sym *isym)
2226
1
{
2227
1
  asection *sec;
2228
1
  flagword flags;
2229
2230
1
  if (!elf_use_dt_symtab_p (abfd))
2231
0
    return NULL;
2232
2233
1
  flags = SEC_ALLOC | SEC_LOAD;
2234
1
  switch (ELF_ST_TYPE (isym->st_info))
2235
1
    {
2236
0
    case STT_FUNC:
2237
0
    case STT_GNU_IFUNC:
2238
0
      sec = bfd_get_section_by_name (abfd, ".text");
2239
0
      if (sec == NULL)
2240
0
  sec = bfd_make_section_with_flags (abfd,
2241
0
             ".text",
2242
0
             flags | SEC_CODE);
2243
0
      break;
2244
0
    case STT_COMMON:
2245
0
      sec = bfd_com_section_ptr;
2246
0
      break;
2247
0
    case STT_OBJECT:
2248
0
      sec = bfd_get_section_by_name (abfd, ".data");
2249
0
      if (sec == NULL)
2250
0
  sec = bfd_make_section_with_flags (abfd,
2251
0
             ".data",
2252
0
             flags | SEC_DATA);
2253
0
      break;
2254
0
    case STT_TLS:
2255
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2256
0
      if (sec == NULL)
2257
0
  sec = bfd_make_section_with_flags (abfd,
2258
0
             ".tdata",
2259
0
             (flags
2260
0
              | SEC_DATA
2261
0
              | SEC_THREAD_LOCAL));
2262
0
      break;
2263
1
    default:
2264
1
      sec = bfd_abs_section_ptr;
2265
1
      break;
2266
1
    }
2267
2268
1
  return sec;
2269
1
}
2270
2271
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2272
   and return symbol version for symbol version itself.   */
2273
2274
const char *
2275
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2276
            bool base_p,
2277
            bool *hidden)
2278
1.70M
{
2279
1.70M
  const char *version_string = NULL;
2280
1.70M
  if ((elf_dynversym (abfd) != 0
2281
1.70M
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2282
1.70M
      || (elf_tdata (abfd)->dt_versym != NULL
2283
466k
    && (elf_tdata (abfd)->dt_verdef != NULL
2284
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2285
1.23M
    {
2286
1.23M
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2287
2288
1.23M
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2289
1.23M
      vernum &= VERSYM_VERSION;
2290
2291
1.23M
      if (vernum == 0)
2292
440k
  version_string = "";
2293
799k
      else if (vernum == 1
2294
799k
         && (vernum > elf_tdata (abfd)->cverdefs
2295
785k
       || (elf_tdata (abfd)->verdef[0].vd_flags
2296
0
           == VER_FLG_BASE)))
2297
785k
  version_string = base_p ? "Base" : "";
2298
13.6k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2299
0
  {
2300
0
    const char *nodename
2301
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2302
0
    version_string = "";
2303
0
    if (base_p
2304
0
        || nodename == NULL
2305
0
        || symbol->name == NULL
2306
0
        || strcmp (symbol->name, nodename) != 0)
2307
0
      version_string = nodename;
2308
0
  }
2309
13.6k
      else
2310
13.6k
  {
2311
13.6k
    Elf_Internal_Verneed *t;
2312
2313
13.6k
    version_string = _("<corrupt>");
2314
13.6k
    for (t = elf_tdata (abfd)->verref;
2315
29.6k
         t != NULL;
2316
16.0k
         t = t->vn_nextref)
2317
16.0k
      {
2318
16.0k
        Elf_Internal_Vernaux *a;
2319
2320
46.0k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2321
43.4k
    {
2322
43.4k
      if (a->vna_other == vernum)
2323
13.4k
        {
2324
13.4k
          *hidden = true;
2325
13.4k
          version_string = a->vna_nodename;
2326
13.4k
          break;
2327
13.4k
        }
2328
43.4k
    }
2329
16.0k
      }
2330
13.6k
  }
2331
1.23M
    }
2332
1.70M
  return version_string;
2333
1.70M
}
2334
2335
/* Display ELF-specific fields of a symbol.  */
2336
2337
void
2338
bfd_elf_print_symbol (bfd *abfd,
2339
          void *filep,
2340
          asymbol *symbol,
2341
          bfd_print_symbol_type how)
2342
0
{
2343
0
  FILE *file = (FILE *) filep;
2344
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2345
0
       ? symbol->name : _("<corrupt>"));
2346
2347
0
  switch (how)
2348
0
    {
2349
0
    case bfd_print_symbol_name:
2350
0
      fprintf (file, "%s", symname);
2351
0
      break;
2352
0
    case bfd_print_symbol_more:
2353
0
      fprintf (file, "elf ");
2354
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2355
0
      fprintf (file, " %x", symbol->flags);
2356
0
      break;
2357
0
    case bfd_print_symbol_all:
2358
0
      {
2359
0
  const char *section_name;
2360
0
  const char *name = NULL;
2361
0
  const struct elf_backend_data *bed;
2362
0
  unsigned char st_other;
2363
0
  bfd_vma val;
2364
0
  const char *version_string;
2365
0
  bool hidden;
2366
2367
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2368
2369
0
  bed = get_elf_backend_data (abfd);
2370
0
  if (bed->elf_backend_print_symbol_all)
2371
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2372
2373
0
  if (name != NULL)
2374
0
    symname = name;
2375
0
  else
2376
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2377
2378
0
  fprintf (file, " %s\t", section_name);
2379
  /* Print the "other" value for a symbol.  For common symbols,
2380
     we've already printed the size; now print the alignment.
2381
     For other symbols, we have no specified alignment, and
2382
     we've printed the address; now print the size.  */
2383
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2384
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2385
0
  else
2386
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2387
0
  bfd_fprintf_vma (abfd, file, val);
2388
2389
  /* If we have version information, print it.  */
2390
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2391
0
                   symbol,
2392
0
                   true,
2393
0
                   &hidden);
2394
0
  if (version_string)
2395
0
    {
2396
0
      if (!hidden)
2397
0
        fprintf (file, "  %-11s", version_string);
2398
0
      else
2399
0
        {
2400
0
    int i;
2401
2402
0
    fprintf (file, " (%s)", version_string);
2403
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2404
0
      putc (' ', file);
2405
0
        }
2406
0
    }
2407
2408
  /* If the st_other field is not zero, print it.  */
2409
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2410
2411
0
  switch (st_other)
2412
0
    {
2413
0
    case 0: break;
2414
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2415
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2416
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2417
0
    default:
2418
      /* Some other non-defined flags are also present, so print
2419
         everything hex.  */
2420
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2421
0
    }
2422
2423
0
  fprintf (file, " %s", symname);
2424
0
      }
2425
0
      break;
2426
0
    }
2427
0
}
2428

2429
/* ELF .o/exec file reading */
2430
2431
/* Create a new bfd section from an ELF section header.  */
2432
2433
bool
2434
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2435
9.88M
{
2436
9.88M
  Elf_Internal_Shdr *hdr;
2437
9.88M
  Elf_Internal_Ehdr *ehdr;
2438
9.88M
  const struct elf_backend_data *bed;
2439
9.88M
  const char *name;
2440
9.88M
  bool ret = true;
2441
2442
9.88M
  if (shindex >= elf_numsections (abfd))
2443
0
    return false;
2444
2445
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2446
     sh_link or sh_info.  Detect this here, by refusing to load a
2447
     section that we are already in the process of loading.  */
2448
9.88M
  if (elf_tdata (abfd)->being_created[shindex])
2449
414
    {
2450
414
      _bfd_error_handler
2451
414
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2452
414
      return false;
2453
414
    }
2454
9.88M
  elf_tdata (abfd)->being_created[shindex] = true;
2455
2456
9.88M
  hdr = elf_elfsections (abfd)[shindex];
2457
9.88M
  ehdr = elf_elfheader (abfd);
2458
9.88M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2459
9.88M
            hdr->sh_name);
2460
9.88M
  if (name == NULL)
2461
28.9k
    goto fail;
2462
2463
9.85M
  bed = get_elf_backend_data (abfd);
2464
9.85M
  switch (hdr->sh_type)
2465
9.85M
    {
2466
2.19M
    case SHT_NULL:
2467
      /* Inactive section. Throw it away.  */
2468
2.19M
      goto success;
2469
2470
1.92M
    case SHT_PROGBITS:   /* Normal section with contents.  */
2471
2.04M
    case SHT_NOBITS:   /* .bss section.  */
2472
2.08M
    case SHT_HASH:   /* .hash section.  */
2473
2.19M
    case SHT_NOTE:   /* .note section.  */
2474
2.29M
    case SHT_INIT_ARRAY: /* .init_array section.  */
2475
2.34M
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2476
2.41M
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2477
2.41M
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2478
2.41M
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2479
2.41M
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2480
2.41M
      goto success;
2481
2482
38.3k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2483
38.3k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2484
42
  goto fail;
2485
2486
38.3k
      if (hdr->sh_link > elf_numsections (abfd))
2487
205
  {
2488
    /* PR 10478: Accept Solaris binaries with a sh_link field
2489
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2490
205
    switch (bfd_get_arch (abfd))
2491
205
      {
2492
204
      case bfd_arch_i386:
2493
204
      case bfd_arch_sparc:
2494
204
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2495
204
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2496
204
    break;
2497
        /* Otherwise fall through.  */
2498
1
      default:
2499
1
        goto fail;
2500
205
      }
2501
205
  }
2502
38.1k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2503
0
  goto fail;
2504
38.1k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2505
20.2k
  {
2506
20.2k
    Elf_Internal_Shdr *dynsymhdr;
2507
2508
    /* The shared libraries distributed with hpux11 have a bogus
2509
       sh_link field for the ".dynamic" section.  Find the
2510
       string table for the ".dynsym" section instead.  */
2511
20.2k
    if (elf_dynsymtab (abfd) != 0)
2512
888
      {
2513
888
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2514
888
        hdr->sh_link = dynsymhdr->sh_link;
2515
888
      }
2516
19.3k
    else
2517
19.3k
      {
2518
19.3k
        unsigned int i, num_sec;
2519
2520
19.3k
        num_sec = elf_numsections (abfd);
2521
336k
        for (i = 1; i < num_sec; i++)
2522
317k
    {
2523
317k
      dynsymhdr = elf_elfsections (abfd)[i];
2524
317k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2525
754
        {
2526
754
          hdr->sh_link = dynsymhdr->sh_link;
2527
754
          break;
2528
754
        }
2529
317k
    }
2530
19.3k
      }
2531
20.2k
  }
2532
38.3k
      goto success;
2533
2534
649k
    case SHT_SYMTAB:   /* A symbol table.  */
2535
649k
      if (elf_onesymtab (abfd) == shindex)
2536
485k
  goto success;
2537
2538
163k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2539
2.83k
  goto fail;
2540
2541
161k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2542
24.4k
  {
2543
24.4k
    if (hdr->sh_size != 0)
2544
335
      goto fail;
2545
    /* Some assemblers erroneously set sh_info to one with a
2546
       zero sh_size.  ld sees this as a global symbol count
2547
       of (unsigned) -1.  Fix it here.  */
2548
24.1k
    hdr->sh_info = 0;
2549
24.1k
    goto success;
2550
24.4k
  }
2551
2552
      /* PR 18854: A binary might contain more than one symbol table.
2553
   Unusual, but possible.  Warn, but continue.  */
2554
136k
      if (elf_onesymtab (abfd) != 0)
2555
2.83k
  {
2556
2.83k
    _bfd_error_handler
2557
      /* xgettext:c-format */
2558
2.83k
      (_("%pB: warning: multiple symbol tables detected"
2559
2.83k
         " - ignoring the table in section %u"),
2560
2.83k
       abfd, shindex);
2561
2.83k
    goto success;
2562
2.83k
  }
2563
133k
      elf_onesymtab (abfd) = shindex;
2564
133k
      elf_symtab_hdr (abfd) = *hdr;
2565
133k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2566
133k
      abfd->flags |= HAS_SYMS;
2567
2568
      /* Sometimes a shared object will map in the symbol table.  If
2569
   SHF_ALLOC is set, and this is a shared object, then we also
2570
   treat this section as a BFD section.  We can not base the
2571
   decision purely on SHF_ALLOC, because that flag is sometimes
2572
   set in a relocatable object file, which would confuse the
2573
   linker.  */
2574
133k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2575
133k
    && (abfd->flags & DYNAMIC) != 0
2576
133k
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2577
8.57k
            shindex))
2578
0
  goto fail;
2579
2580
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2581
   can't read symbols without that section loaded as well.  It
2582
   is most likely specified by the next section header.  */
2583
133k
      {
2584
133k
  elf_section_list * entry;
2585
133k
  unsigned int i, num_sec;
2586
2587
147k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2588
19.6k
    if (entry->hdr.sh_link == shindex)
2589
5.43k
      goto success;
2590
2591
128k
  num_sec = elf_numsections (abfd);
2592
415k
  for (i = shindex + 1; i < num_sec; i++)
2593
288k
    {
2594
288k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2595
2596
288k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2597
288k
    && hdr2->sh_link == shindex)
2598
968
        break;
2599
288k
    }
2600
2601
128k
  if (i == num_sec)
2602
3.00M
    for (i = 1; i < shindex; i++)
2603
2.88M
      {
2604
2.88M
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2605
2606
2.88M
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2607
2.88M
      && hdr2->sh_link == shindex)
2608
8.40k
    break;
2609
2.88M
      }
2610
2611
128k
  if (i != shindex)
2612
9.37k
    ret = bfd_section_from_shdr (abfd, i);
2613
  /* else FIXME: we have failed to find the symbol table.
2614
     Should we issue an error?  */
2615
128k
  goto success;
2616
133k
      }
2617
2618
23.4k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2619
23.4k
      if (elf_dynsymtab (abfd) == shindex)
2620
10.1k
  goto success;
2621
2622
13.2k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2623
1.29k
  goto fail;
2624
2625
11.9k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2626
2.06k
  {
2627
2.06k
    if (hdr->sh_size != 0)
2628
77
      goto fail;
2629
2630
    /* Some linkers erroneously set sh_info to one with a
2631
       zero sh_size.  ld sees this as a global symbol count
2632
       of (unsigned) -1.  Fix it here.  */
2633
1.98k
    hdr->sh_info = 0;
2634
1.98k
    goto success;
2635
2.06k
  }
2636
2637
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2638
   Unusual, but possible.  Warn, but continue.  */
2639
9.91k
      if (elf_dynsymtab (abfd) != 0)
2640
552
  {
2641
552
    _bfd_error_handler
2642
      /* xgettext:c-format */
2643
552
      (_("%pB: warning: multiple dynamic symbol tables detected"
2644
552
         " - ignoring the table in section %u"),
2645
552
       abfd, shindex);
2646
552
    goto success;
2647
552
  }
2648
9.36k
      elf_dynsymtab (abfd) = shindex;
2649
9.36k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2650
9.36k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2651
9.36k
      abfd->flags |= HAS_SYMS;
2652
2653
      /* Besides being a symbol table, we also treat this as a regular
2654
   section, so that objcopy can handle it.  */
2655
9.36k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2656
9.36k
      goto success;
2657
2658
98.7k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2659
98.7k
      {
2660
98.7k
  elf_section_list * entry;
2661
2662
137k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2663
46.9k
    if (entry->ndx == shindex)
2664
7.71k
      goto success;
2665
2666
91.0k
  entry = bfd_alloc (abfd, sizeof (*entry));
2667
91.0k
  if (entry == NULL)
2668
0
    goto fail;
2669
91.0k
  entry->ndx = shindex;
2670
91.0k
  entry->hdr = * hdr;
2671
91.0k
  entry->next = elf_symtab_shndx_list (abfd);
2672
91.0k
  elf_symtab_shndx_list (abfd) = entry;
2673
91.0k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2674
91.0k
  goto success;
2675
91.0k
      }
2676
2677
832k
    case SHT_STRTAB:   /* A string table.  */
2678
832k
      if (hdr->bfd_section != NULL)
2679
6.41k
  goto success;
2680
2681
825k
      if (ehdr->e_shstrndx == shindex)
2682
715k
  {
2683
715k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2684
715k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2685
715k
    goto success;
2686
715k
  }
2687
2688
109k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2689
41.2k
  {
2690
42.0k
  symtab_strtab:
2691
42.0k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2692
42.0k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2693
42.0k
    goto success;
2694
41.2k
  }
2695
2696
68.6k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2697
3.12k
  {
2698
3.19k
  dynsymtab_strtab:
2699
3.19k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2700
3.19k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2701
3.19k
    elf_elfsections (abfd)[shindex] = hdr;
2702
    /* We also treat this as a regular section, so that objcopy
2703
       can handle it.  */
2704
3.19k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2705
3.19k
             shindex);
2706
3.19k
    goto success;
2707
3.12k
  }
2708
2709
      /* If the string table isn't one of the above, then treat it as a
2710
   regular section.  We need to scan all the headers to be sure,
2711
   just in case this strtab section appeared before the above.  */
2712
65.5k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2713
64.9k
  {
2714
64.9k
    unsigned int i, num_sec;
2715
2716
64.9k
    num_sec = elf_numsections (abfd);
2717
1.27M
    for (i = 1; i < num_sec; i++)
2718
1.21M
      {
2719
1.21M
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2720
1.21M
        if (hdr2->sh_link == shindex)
2721
30.0k
    {
2722
      /* Prevent endless recursion on broken objects.  */
2723
30.0k
      if (i == shindex)
2724
355
        goto fail;
2725
29.7k
      if (! bfd_section_from_shdr (abfd, i))
2726
920
        goto fail;
2727
28.7k
      if (elf_onesymtab (abfd) == i)
2728
756
        goto symtab_strtab;
2729
28.0k
      if (elf_dynsymtab (abfd) == i)
2730
69
        goto dynsymtab_strtab;
2731
28.0k
    }
2732
1.21M
      }
2733
64.9k
  }
2734
63.4k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2735
63.4k
      goto success;
2736
2737
317k
    case SHT_REL:
2738
864k
    case SHT_RELA:
2739
865k
    case SHT_RELR:
2740
      /* *These* do a lot of work -- but build no sections!  */
2741
865k
      {
2742
865k
  asection *target_sect;
2743
865k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2744
865k
  unsigned int num_sec = elf_numsections (abfd);
2745
865k
  struct bfd_elf_section_data *esdt;
2746
865k
  bfd_size_type size;
2747
2748
865k
  if (hdr->sh_type == SHT_REL)
2749
317k
    size = bed->s->sizeof_rel;
2750
548k
  else if (hdr->sh_type == SHT_RELA)
2751
547k
    size = bed->s->sizeof_rela;
2752
1.57k
  else
2753
1.57k
    size = bed->s->arch_size / 8;
2754
865k
  if (hdr->sh_entsize != size)
2755
3.69k
    goto fail;
2756
2757
  /* Check for a bogus link to avoid crashing.  */
2758
862k
  if (hdr->sh_link >= num_sec)
2759
370
    {
2760
370
      _bfd_error_handler
2761
        /* xgettext:c-format */
2762
370
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2763
370
         abfd, hdr->sh_link, name, shindex);
2764
370
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2765
370
      goto success;
2766
370
    }
2767
2768
  /* Get the symbol table.  */
2769
861k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2770
861k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2771
861k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2772
554
    goto fail;
2773
2774
  /* If this is an alloc section in an executable or shared
2775
     library, or the reloc section does not use the main symbol
2776
     table we don't treat it as a reloc section.  BFD can't
2777
     adequately represent such a section, so at least for now,
2778
     we don't try.  We just present it as a normal section.  We
2779
     also can't use it as a reloc section if it points to the
2780
     null section, an invalid section, another reloc section, or
2781
     its sh_link points to the null section.  */
2782
861k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2783
861k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2784
861k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2785
861k
      || hdr->sh_type == SHT_RELR
2786
861k
      || hdr->sh_link == SHN_UNDEF
2787
861k
      || hdr->sh_link != elf_onesymtab (abfd)
2788
861k
      || hdr->sh_info == SHN_UNDEF
2789
861k
      || hdr->sh_info >= num_sec
2790
861k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2791
861k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2792
473k
    {
2793
473k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2794
473k
      goto success;
2795
473k
    }
2796
2797
387k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2798
416
    goto fail;
2799
2800
387k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2801
387k
  if (target_sect == NULL)
2802
411
    goto fail;
2803
2804
387k
  esdt = elf_section_data (target_sect);
2805
387k
  if (hdr->sh_type == SHT_RELA)
2806
300k
    p_hdr = &esdt->rela.hdr;
2807
86.6k
  else
2808
86.6k
    p_hdr = &esdt->rel.hdr;
2809
2810
  /* PR 17512: file: 0b4f81b7.
2811
     Also see PR 24456, for a file which deliberately has two reloc
2812
     sections.  */
2813
387k
  if (*p_hdr != NULL)
2814
8.56k
    {
2815
8.56k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2816
3.87k
        {
2817
3.87k
    _bfd_error_handler
2818
      /* xgettext:c-format */
2819
3.87k
      (_("%pB: warning: secondary relocation section '%s' "
2820
3.87k
         "for section %pA found - ignoring"),
2821
3.87k
       abfd, name, target_sect);
2822
3.87k
        }
2823
4.69k
      else
2824
4.69k
        esdt->has_secondary_relocs = true;
2825
8.56k
      goto success;
2826
8.56k
    }
2827
2828
378k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2829
378k
  if (hdr2 == NULL)
2830
0
    goto fail;
2831
378k
  *hdr2 = *hdr;
2832
378k
  *p_hdr = hdr2;
2833
378k
  elf_elfsections (abfd)[shindex] = hdr2;
2834
378k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2835
378k
             * bed->s->int_rels_per_ext_rel);
2836
378k
  target_sect->flags |= SEC_RELOC;
2837
378k
  target_sect->relocation = NULL;
2838
378k
  target_sect->rel_filepos = hdr->sh_offset;
2839
  /* In the section to which the relocations apply, mark whether
2840
     its relocations are of the REL or RELA variety.  */
2841
378k
  if (hdr->sh_size != 0)
2842
366k
    {
2843
366k
      if (hdr->sh_type == SHT_RELA)
2844
292k
        target_sect->use_rela_p = 1;
2845
366k
    }
2846
378k
  abfd->flags |= HAS_RELOC;
2847
378k
  goto success;
2848
378k
      }
2849
2850
6.94k
    case SHT_GNU_verdef:
2851
6.94k
      if (hdr->sh_info != 0)
2852
3.38k
  elf_dynverdef (abfd) = shindex;
2853
6.94k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2854
6.94k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2855
6.94k
      goto success;
2856
2857
2.56k
    case SHT_GNU_versym:
2858
2.56k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2859
187
  goto fail;
2860
2861
2.37k
      elf_dynversym (abfd) = shindex;
2862
2.37k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2863
2.37k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2864
2.37k
      goto success;
2865
2866
7.33k
    case SHT_GNU_verneed:
2867
7.33k
      if (hdr->sh_info != 0)
2868
5.30k
  elf_dynverref (abfd) = shindex;
2869
7.33k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2870
7.33k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2871
7.33k
      goto success;
2872
2873
70.5k
    case SHT_SHLIB:
2874
70.5k
      goto success;
2875
2876
216k
    case SHT_GROUP:
2877
216k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2878
29
  goto fail;
2879
2880
216k
      goto success;
2881
2882
2.43M
    default:
2883
      /* Possibly an attributes section.  */
2884
2.43M
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2885
2.43M
    || hdr->sh_type == bed->obj_attrs_section_type)
2886
136k
  {
2887
136k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2888
12
      goto fail;
2889
136k
    _bfd_elf_parse_attributes (abfd, hdr);
2890
136k
    goto success;
2891
136k
  }
2892
2893
      /* Check for any processor-specific section types.  */
2894
2.29M
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2895
1.98M
  goto success;
2896
2897
312k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2898
156k
  {
2899
156k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2900
      /* FIXME: How to properly handle allocated section reserved
2901
         for applications?  */
2902
16.9k
      _bfd_error_handler
2903
        /* xgettext:c-format */
2904
16.9k
        (_("%pB: unknown type [%#x] section `%s'"),
2905
16.9k
         abfd, hdr->sh_type, name);
2906
139k
    else
2907
139k
      {
2908
        /* Allow sections reserved for applications.  */
2909
139k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2910
139k
        goto success;
2911
139k
      }
2912
156k
  }
2913
155k
      else if (hdr->sh_type >= SHT_LOPROC
2914
155k
         && hdr->sh_type <= SHT_HIPROC)
2915
  /* FIXME: We should handle this section.  */
2916
29.8k
  _bfd_error_handler
2917
    /* xgettext:c-format */
2918
29.8k
    (_("%pB: unknown type [%#x] section `%s'"),
2919
29.8k
     abfd, hdr->sh_type, name);
2920
125k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2921
40.6k
  {
2922
    /* Unrecognised OS-specific sections.  */
2923
40.6k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2924
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2925
         required to correctly process the section and the file should
2926
         be rejected with an error message.  */
2927
3.33k
      _bfd_error_handler
2928
        /* xgettext:c-format */
2929
3.33k
        (_("%pB: unknown type [%#x] section `%s'"),
2930
3.33k
         abfd, hdr->sh_type, name);
2931
37.2k
    else
2932
37.2k
      {
2933
        /* Otherwise it should be processed.  */
2934
37.2k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2935
37.2k
        goto success;
2936
37.2k
      }
2937
40.6k
  }
2938
85.3k
      else
2939
  /* FIXME: We should handle this section.  */
2940
85.3k
  _bfd_error_handler
2941
    /* xgettext:c-format */
2942
85.3k
    (_("%pB: unknown type [%#x] section `%s'"),
2943
85.3k
     abfd, hdr->sh_type, name);
2944
2945
135k
      goto fail;
2946
9.85M
    }
2947
2948
175k
 fail:
2949
175k
  ret = false;
2950
9.88M
 success:
2951
9.88M
  elf_tdata (abfd)->being_created[shindex] = false;
2952
9.88M
  return ret;
2953
175k
}
2954
2955
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
2956
2957
Elf_Internal_Sym *
2958
bfd_sym_from_r_symndx (struct sym_cache *cache,
2959
           bfd *abfd,
2960
           unsigned long r_symndx)
2961
0
{
2962
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2963
2964
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2965
0
    {
2966
0
      Elf_Internal_Shdr *symtab_hdr;
2967
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
2968
0
      Elf_External_Sym_Shndx eshndx;
2969
2970
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2971
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2972
0
        &cache->sym[ent], esym, &eshndx) == NULL)
2973
0
  return NULL;
2974
2975
0
      if (cache->abfd != abfd)
2976
0
  {
2977
0
    memset (cache->indx, -1, sizeof (cache->indx));
2978
0
    cache->abfd = abfd;
2979
0
  }
2980
0
      cache->indx[ent] = r_symndx;
2981
0
    }
2982
2983
0
  return &cache->sym[ent];
2984
0
}
2985
2986
/* Given an ELF section number, retrieve the corresponding BFD
2987
   section.  */
2988
2989
asection *
2990
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2991
1.04M
{
2992
1.04M
  if (sec_index >= elf_numsections (abfd))
2993
27.3k
    return NULL;
2994
1.01M
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2995
1.04M
}
2996
2997
static const struct bfd_elf_special_section special_sections_b[] =
2998
{
2999
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3000
  { NULL,       0,  0, 0,    0 }
3001
};
3002
3003
static const struct bfd_elf_special_section special_sections_c[] =
3004
{
3005
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3006
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3007
  { NULL,     0, 0, 0,      0 }
3008
};
3009
3010
static const struct bfd_elf_special_section special_sections_d[] =
3011
{
3012
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3013
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3014
  /* There are more DWARF sections than these, but they needn't be added here
3015
     unless you have to cope with broken compilers that don't emit section
3016
     attributes or you want to help the user writing assembler.  */
3017
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3018
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3019
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3020
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3021
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3022
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3023
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3024
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3025
  { NULL,          0,  0, 0,      0 }
3026
};
3027
3028
static const struct bfd_elf_special_section special_sections_f[] =
3029
{
3030
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3031
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3032
  { NULL,        0 , 0, 0,      0 }
3033
};
3034
3035
static const struct bfd_elf_special_section special_sections_g[] =
3036
{
3037
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3038
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3039
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3040
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3041
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3042
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3043
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3044
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3045
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3046
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3047
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3048
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3049
  { NULL,      0,    0, 0,         0 }
3050
};
3051
3052
static const struct bfd_elf_special_section special_sections_h[] =
3053
{
3054
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3055
  { NULL,        0, 0, 0,    0 }
3056
};
3057
3058
static const struct bfd_elf_special_section special_sections_i[] =
3059
{
3060
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3061
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3062
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3063
  { NULL,          0,      0, 0,      0 }
3064
};
3065
3066
static const struct bfd_elf_special_section special_sections_l[] =
3067
{
3068
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3069
  { NULL,        0, 0, 0,    0 }
3070
};
3071
3072
static const struct bfd_elf_special_section special_sections_n[] =
3073
{
3074
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3075
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3076
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3077
  { NULL,        0,     0, 0,      0 }
3078
};
3079
3080
static const struct bfd_elf_special_section special_sections_p[] =
3081
{
3082
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3083
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3084
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3085
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3086
  { NULL,       0,      0, 0,     0 }
3087
};
3088
3089
static const struct bfd_elf_special_section special_sections_r[] =
3090
{
3091
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3092
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3093
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3094
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3095
  /* .relro_padding is generated by lld.  It should not be confused with a
3096
     reloc containing section, because otherwise elf_fake_sections() will
3097
     set the entsize to 8, which may not be an actual multiple of the
3098
     section's size.
3099
     Note - this entry must appear before the ".rel" entry below.  */
3100
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3101
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3102
  { NULL,       0,     0, 0,      0 }
3103
};
3104
3105
static const struct bfd_elf_special_section special_sections_s[] =
3106
{
3107
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3108
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3109
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3110
  /* See struct bfd_elf_special_section declaration for the semantics of
3111
     this special case where .prefix_length != strlen (.prefix).  */
3112
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3113
  { NULL,     0,  0, 0,    0 }
3114
};
3115
3116
static const struct bfd_elf_special_section special_sections_t[] =
3117
{
3118
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3119
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3120
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3121
  { NULL,         0,  0, 0,      0 }
3122
};
3123
3124
static const struct bfd_elf_special_section special_sections_z[] =
3125
{
3126
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3127
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3128
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3129
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3130
  { NULL,         0,  0, 0,      0 }
3131
};
3132
3133
static const struct bfd_elf_special_section * const special_sections[] =
3134
{
3135
  special_sections_b,   /* 'b' */
3136
  special_sections_c,   /* 'c' */
3137
  special_sections_d,   /* 'd' */
3138
  NULL,       /* 'e' */
3139
  special_sections_f,   /* 'f' */
3140
  special_sections_g,   /* 'g' */
3141
  special_sections_h,   /* 'h' */
3142
  special_sections_i,   /* 'i' */
3143
  NULL,       /* 'j' */
3144
  NULL,       /* 'k' */
3145
  special_sections_l,   /* 'l' */
3146
  NULL,       /* 'm' */
3147
  special_sections_n,   /* 'n' */
3148
  NULL,       /* 'o' */
3149
  special_sections_p,   /* 'p' */
3150
  NULL,       /* 'q' */
3151
  special_sections_r,   /* 'r' */
3152
  special_sections_s,   /* 's' */
3153
  special_sections_t,   /* 't' */
3154
  NULL,       /* 'u' */
3155
  NULL,       /* 'v' */
3156
  NULL,       /* 'w' */
3157
  NULL,       /* 'x' */
3158
  NULL,       /* 'y' */
3159
  special_sections_z    /* 'z' */
3160
};
3161
3162
const struct bfd_elf_special_section *
3163
_bfd_elf_get_special_section (const char *name,
3164
            const struct bfd_elf_special_section *spec,
3165
            unsigned int rela)
3166
5.08M
{
3167
5.08M
  int i;
3168
5.08M
  int len;
3169
3170
5.08M
  len = strlen (name);
3171
3172
30.9M
  for (i = 0; spec[i].prefix != NULL; i++)
3173
26.7M
    {
3174
26.7M
      int suffix_len;
3175
26.7M
      int prefix_len = spec[i].prefix_length;
3176
3177
26.7M
      if (len < prefix_len)
3178
15.2M
  continue;
3179
11.5M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3180
10.1M
  continue;
3181
3182
1.41M
      suffix_len = spec[i].suffix_length;
3183
1.41M
      if (suffix_len <= 0)
3184
1.38M
  {
3185
1.38M
    if (name[prefix_len] != 0)
3186
960k
      {
3187
960k
        if (suffix_len == 0)
3188
285k
    continue;
3189
675k
        if (name[prefix_len] != '.'
3190
675k
      && (suffix_len == -2
3191
236k
          || (rela && spec[i].type == SHT_REL)))
3192
203k
    continue;
3193
675k
      }
3194
1.38M
  }
3195
31.2k
      else
3196
31.2k
  {
3197
31.2k
    if (len < prefix_len + suffix_len)
3198
9.88k
      continue;
3199
21.3k
    if (memcmp (name + len - suffix_len,
3200
21.3k
          spec[i].prefix + prefix_len,
3201
21.3k
          suffix_len) != 0)
3202
10.0k
      continue;
3203
21.3k
  }
3204
904k
      return &spec[i];
3205
1.41M
    }
3206
3207
4.17M
  return NULL;
3208
5.08M
}
3209
3210
const struct bfd_elf_special_section *
3211
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3212
6.68M
{
3213
6.68M
  int i;
3214
6.68M
  const struct bfd_elf_special_section *spec;
3215
6.68M
  const struct elf_backend_data *bed;
3216
3217
  /* See if this is one of the special sections.  */
3218
6.68M
  if (sec->name == NULL)
3219
0
    return NULL;
3220
3221
6.68M
  bed = get_elf_backend_data (abfd);
3222
6.68M
  spec = bed->special_sections;
3223
6.68M
  if (spec)
3224
3.36M
    {
3225
3.36M
      spec = _bfd_elf_get_special_section (sec->name,
3226
3.36M
             bed->special_sections,
3227
3.36M
             sec->use_rela_p);
3228
3.36M
      if (spec != NULL)
3229
12.4k
  return spec;
3230
3.36M
    }
3231
3232
6.67M
  if (sec->name[0] != '.')
3233
4.96M
    return NULL;
3234
3235
1.70M
  i = sec->name[1] - 'b';
3236
1.70M
  if (i < 0 || i > 'z' - 'b')
3237
65.0k
    return NULL;
3238
3239
1.63M
  spec = special_sections[i];
3240
3241
1.63M
  if (spec == NULL)
3242
73.2k
    return NULL;
3243
3244
1.56M
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3245
1.63M
}
3246
3247
bool
3248
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3249
6.68M
{
3250
6.68M
  struct bfd_elf_section_data *sdata;
3251
6.68M
  const struct elf_backend_data *bed;
3252
6.68M
  const struct bfd_elf_special_section *ssect;
3253
3254
6.68M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3255
6.68M
  if (sdata == NULL)
3256
5.15M
    {
3257
5.15M
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3258
5.15M
                sizeof (*sdata));
3259
5.15M
      if (sdata == NULL)
3260
0
  return false;
3261
5.15M
      sec->used_by_bfd = sdata;
3262
5.15M
    }
3263
3264
  /* Indicate whether or not this section should use RELA relocations.  */
3265
6.68M
  bed = get_elf_backend_data (abfd);
3266
6.68M
  sec->use_rela_p = bed->default_use_rela_p;
3267
3268
  /* Set up ELF section type and flags for newly created sections, if
3269
     there is an ABI mandated section.  */
3270
6.68M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3271
6.68M
  if (ssect != NULL)
3272
904k
    {
3273
904k
      elf_section_type (sec) = ssect->type;
3274
904k
      elf_section_flags (sec) = ssect->attr;
3275
904k
    }
3276
3277
6.68M
  return _bfd_generic_new_section_hook (abfd, sec);
3278
6.68M
}
3279
3280
/* Create a new bfd section from an ELF program header.
3281
3282
   Since program segments have no names, we generate a synthetic name
3283
   of the form segment<NUM>, where NUM is generally the index in the
3284
   program header table.  For segments that are split (see below) we
3285
   generate the names segment<NUM>a and segment<NUM>b.
3286
3287
   Note that some program segments may have a file size that is different than
3288
   (less than) the memory size.  All this means is that at execution the
3289
   system must allocate the amount of memory specified by the memory size,
3290
   but only initialize it with the first "file size" bytes read from the
3291
   file.  This would occur for example, with program segments consisting
3292
   of combined data+bss.
3293
3294
   To handle the above situation, this routine generates TWO bfd sections
3295
   for the single program segment.  The first has the length specified by
3296
   the file size of the segment, and the second has the length specified
3297
   by the difference between the two sizes.  In effect, the segment is split
3298
   into its initialized and uninitialized parts.  */
3299
3300
bool
3301
_bfd_elf_make_section_from_phdr (bfd *abfd,
3302
         Elf_Internal_Phdr *hdr,
3303
         int hdr_index,
3304
         const char *type_name)
3305
1.04M
{
3306
1.04M
  asection *newsect;
3307
1.04M
  char *name;
3308
1.04M
  char namebuf[64];
3309
1.04M
  size_t len;
3310
1.04M
  int split;
3311
1.04M
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3312
3313
1.04M
  split = ((hdr->p_memsz > 0)
3314
1.04M
      && (hdr->p_filesz > 0)
3315
1.04M
      && (hdr->p_memsz > hdr->p_filesz));
3316
3317
1.04M
  if (hdr->p_filesz > 0)
3318
924k
    {
3319
924k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3320
924k
      len = strlen (namebuf) + 1;
3321
924k
      name = (char *) bfd_alloc (abfd, len);
3322
924k
      if (!name)
3323
0
  return false;
3324
924k
      memcpy (name, namebuf, len);
3325
924k
      newsect = bfd_make_section (abfd, name);
3326
924k
      if (newsect == NULL)
3327
0
  return false;
3328
924k
      newsect->vma = hdr->p_vaddr / opb;
3329
924k
      newsect->lma = hdr->p_paddr / opb;
3330
924k
      newsect->size = hdr->p_filesz;
3331
924k
      newsect->filepos = hdr->p_offset;
3332
924k
      newsect->flags |= SEC_HAS_CONTENTS;
3333
924k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3334
924k
      if (hdr->p_type == PT_LOAD)
3335
11.9k
  {
3336
11.9k
    newsect->flags |= SEC_ALLOC;
3337
11.9k
    newsect->flags |= SEC_LOAD;
3338
11.9k
    if (hdr->p_flags & PF_X)
3339
4.20k
      {
3340
        /* FIXME: all we known is that it has execute PERMISSION,
3341
     may be data.  */
3342
4.20k
        newsect->flags |= SEC_CODE;
3343
4.20k
      }
3344
11.9k
  }
3345
924k
      if (!(hdr->p_flags & PF_W))
3346
643k
  {
3347
643k
    newsect->flags |= SEC_READONLY;
3348
643k
  }
3349
924k
    }
3350
3351
1.04M
  if (hdr->p_memsz > hdr->p_filesz)
3352
430k
    {
3353
430k
      bfd_vma align;
3354
3355
430k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3356
430k
      len = strlen (namebuf) + 1;
3357
430k
      name = (char *) bfd_alloc (abfd, len);
3358
430k
      if (!name)
3359
0
  return false;
3360
430k
      memcpy (name, namebuf, len);
3361
430k
      newsect = bfd_make_section (abfd, name);
3362
430k
      if (newsect == NULL)
3363
0
  return false;
3364
430k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3365
430k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3366
430k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3367
430k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3368
430k
      align = newsect->vma & -newsect->vma;
3369
430k
      if (align == 0 || align > hdr->p_align)
3370
66.9k
  align = hdr->p_align;
3371
430k
      newsect->alignment_power = bfd_log2 (align);
3372
430k
      if (hdr->p_type == PT_LOAD)
3373
5.81k
  {
3374
5.81k
    newsect->flags |= SEC_ALLOC;
3375
5.81k
    if (hdr->p_flags & PF_X)
3376
2.56k
      newsect->flags |= SEC_CODE;
3377
5.81k
  }
3378
430k
      if (!(hdr->p_flags & PF_W))
3379
302k
  newsect->flags |= SEC_READONLY;
3380
430k
    }
3381
3382
1.04M
  return true;
3383
1.04M
}
3384
3385
static bool
3386
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3387
13.1k
{
3388
  /* The return value is ignored.  Build-ids are considered optional.  */
3389
13.1k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3390
13.1k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3391
13.1k
      (templ, offset);
3392
0
  return false;
3393
13.1k
}
3394
3395
bool
3396
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3397
1.04M
{
3398
1.04M
  const struct elf_backend_data *bed;
3399
3400
1.04M
  switch (hdr->p_type)
3401
1.04M
    {
3402
188k
    case PT_NULL:
3403
188k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3404
3405
13.1k
    case PT_LOAD:
3406
13.1k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3407
0
  return false;
3408
13.1k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3409
13.1k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3410
13.1k
      return true;
3411
3412
4.59k
    case PT_DYNAMIC:
3413
4.59k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3414
3415
1.57k
    case PT_INTERP:
3416
1.57k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3417
3418
1.83k
    case PT_NOTE:
3419
1.83k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3420
0
  return false;
3421
1.83k
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3422
1.83k
          hdr->p_align))
3423
1.29k
  return false;
3424
541
      return true;
3425
3426
1.24k
    case PT_SHLIB:
3427
1.24k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3428
3429
6.50k
    case PT_PHDR:
3430
6.50k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3431
3432
18
    case PT_GNU_EH_FRAME:
3433
18
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3434
18
                "eh_frame_hdr");
3435
3436
29
    case PT_GNU_STACK:
3437
29
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3438
3439
22
    case PT_GNU_RELRO:
3440
22
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3441
3442
3
    case PT_GNU_SFRAME:
3443
3
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3444
3
                "sframe");
3445
3446
832k
    default:
3447
      /* Check for any processor-specific program segment types.  */
3448
832k
      bed = get_elf_backend_data (abfd);
3449
832k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3450
1.04M
    }
3451
1.04M
}
3452
3453
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3454
   REL or RELA.  */
3455
3456
Elf_Internal_Shdr *
3457
_bfd_elf_single_rel_hdr (asection *sec)
3458
0
{
3459
0
  if (elf_section_data (sec)->rel.hdr)
3460
0
    {
3461
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3462
0
      return elf_section_data (sec)->rel.hdr;
3463
0
    }
3464
0
  else
3465
0
    return elf_section_data (sec)->rela.hdr;
3466
0
}
3467
3468
static bool
3469
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3470
          Elf_Internal_Shdr *rel_hdr,
3471
          const char *sec_name,
3472
          bool use_rela_p)
3473
1.32k
{
3474
1.32k
  char *name = (char *) bfd_alloc (abfd,
3475
1.32k
           sizeof ".rela" + strlen (sec_name));
3476
1.32k
  if (name == NULL)
3477
0
    return false;
3478
3479
1.32k
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3480
1.32k
  rel_hdr->sh_name =
3481
1.32k
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3482
1.32k
          false);
3483
1.32k
  if (rel_hdr->sh_name == (unsigned int) -1)
3484
0
    return false;
3485
3486
1.32k
  return true;
3487
1.32k
}
3488
3489
/* Allocate and initialize a section-header for a new reloc section,
3490
   containing relocations against ASECT.  It is stored in RELDATA.  If
3491
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3492
   relocations.  */
3493
3494
static bool
3495
_bfd_elf_init_reloc_shdr (bfd *abfd,
3496
        struct bfd_elf_section_reloc_data *reldata,
3497
        const char *sec_name,
3498
        bool use_rela_p,
3499
        bool delay_sh_name_p)
3500
1.32k
{
3501
1.32k
  Elf_Internal_Shdr *rel_hdr;
3502
1.32k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3503
3504
1.32k
  BFD_ASSERT (reldata->hdr == NULL);
3505
1.32k
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3506
1.32k
  if (rel_hdr == NULL)
3507
0
    return false;
3508
1.32k
  reldata->hdr = rel_hdr;
3509
3510
1.32k
  if (delay_sh_name_p)
3511
0
    rel_hdr->sh_name = (unsigned int) -1;
3512
1.32k
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3513
1.32k
          use_rela_p))
3514
0
    return false;
3515
1.32k
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3516
1.32k
  rel_hdr->sh_entsize = (use_rela_p
3517
1.32k
       ? bed->s->sizeof_rela
3518
1.32k
       : bed->s->sizeof_rel);
3519
1.32k
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3520
1.32k
  rel_hdr->sh_flags = 0;
3521
1.32k
  rel_hdr->sh_addr = 0;
3522
1.32k
  rel_hdr->sh_size = 0;
3523
1.32k
  rel_hdr->sh_offset = 0;
3524
3525
1.32k
  return true;
3526
1.32k
}
3527
3528
/* Return the default section type based on the passed in section flags.  */
3529
3530
int
3531
bfd_elf_get_default_section_type (flagword flags)
3532
2.45k
{
3533
2.45k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3534
2.45k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3535
39
    return SHT_NOBITS;
3536
2.41k
  return SHT_PROGBITS;
3537
2.45k
}
3538
3539
struct fake_section_arg
3540
{
3541
  struct bfd_link_info *link_info;
3542
  bool failed;
3543
};
3544
3545
/* Set up an ELF internal section header for a section.  */
3546
3547
static void
3548
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3549
2.86k
{
3550
2.86k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3551
2.86k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3552
2.86k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3553
2.86k
  Elf_Internal_Shdr *this_hdr;
3554
2.86k
  unsigned int sh_type;
3555
2.86k
  const char *name = asect->name;
3556
2.86k
  bool delay_sh_name_p = false;
3557
2.86k
  bfd_vma mask;
3558
3559
2.86k
  if (arg->failed)
3560
0
    {
3561
      /* We already failed; just get out of the bfd_map_over_sections
3562
   loop.  */
3563
0
      return;
3564
0
    }
3565
3566
2.86k
  this_hdr = &esd->this_hdr;
3567
3568
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3569
2.86k
  if (arg->link_info
3570
2.86k
      && (abfd->flags & BFD_COMPRESS) != 0
3571
2.86k
      && (asect->flags & SEC_DEBUGGING) != 0
3572
2.86k
      && (asect->flags & SEC_ALLOC) == 0
3573
2.86k
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3574
2.86k
      && name[1] == 'd'
3575
2.86k
      && name[6] == '_')
3576
0
    {
3577
      /* If this section will be compressed, delay adding section
3578
   name to section name section after it is compressed in
3579
   _bfd_elf_assign_file_positions_for_non_load.  */
3580
0
      delay_sh_name_p = true;
3581
0
    }
3582
3583
2.86k
  if (delay_sh_name_p)
3584
0
    this_hdr->sh_name = (unsigned int) -1;
3585
2.86k
  else
3586
2.86k
    {
3587
2.86k
      this_hdr->sh_name
3588
2.86k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3589
2.86k
                name, false);
3590
2.86k
      if (this_hdr->sh_name == (unsigned int) -1)
3591
0
  {
3592
0
    arg->failed = true;
3593
0
    return;
3594
0
  }
3595
2.86k
    }
3596
3597
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3598
3599
2.86k
  if ((asect->flags & SEC_ALLOC) != 0
3600
2.86k
      || asect->user_set_vma)
3601
2.86k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3602
0
  else
3603
0
    this_hdr->sh_addr = 0;
3604
3605
2.86k
  this_hdr->sh_offset = 0;
3606
2.86k
  this_hdr->sh_size = asect->size;
3607
2.86k
  this_hdr->sh_link = 0;
3608
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3609
2.86k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3610
0
    {
3611
0
      _bfd_error_handler
3612
  /* xgettext:c-format */
3613
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3614
0
   abfd, asect->alignment_power, asect);
3615
0
      arg->failed = true;
3616
0
      return;
3617
0
    }
3618
  /* Set sh_addralign to the highest power of two given by alignment
3619
     consistent with the section VMA.  Linker scripts can force VMA.  */
3620
2.86k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3621
2.86k
  this_hdr->sh_addralign = mask & -mask;
3622
  /* The sh_entsize and sh_info fields may have been set already by
3623
     copy_private_section_data.  */
3624
3625
2.86k
  this_hdr->bfd_section = asect;
3626
2.86k
  this_hdr->contents = NULL;
3627
3628
  /* If the section type is unspecified, we set it based on
3629
     asect->flags.  */
3630
2.86k
  if (asect->type != 0)
3631
0
    sh_type = asect->type;
3632
2.86k
  else if ((asect->flags & SEC_GROUP) != 0)
3633
455
    sh_type = SHT_GROUP;
3634
2.41k
  else
3635
2.41k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3636
3637
2.86k
  if (this_hdr->sh_type == SHT_NULL)
3638
0
    this_hdr->sh_type = sh_type;
3639
2.86k
  else if (this_hdr->sh_type == SHT_NOBITS
3640
2.86k
     && sh_type == SHT_PROGBITS
3641
2.86k
     && (asect->flags & SEC_ALLOC) != 0)
3642
0
    {
3643
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3644
   allow the link to proceed.  This can happen when users link
3645
   non-bss input sections to bss output sections, or emit data
3646
   to a bss output section via a linker script.  */
3647
0
      _bfd_error_handler
3648
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3649
0
      this_hdr->sh_type = sh_type;
3650
0
    }
3651
3652
2.86k
  switch (this_hdr->sh_type)
3653
2.86k
    {
3654
68
    default:
3655
68
      break;
3656
3657
68
    case SHT_STRTAB:
3658
25
    case SHT_NOTE:
3659
64
    case SHT_NOBITS:
3660
2.14k
    case SHT_PROGBITS:
3661
2.14k
      break;
3662
3663
59
    case SHT_INIT_ARRAY:
3664
96
    case SHT_FINI_ARRAY:
3665
96
    case SHT_PREINIT_ARRAY:
3666
96
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3667
96
      break;
3668
3669
12
    case SHT_HASH:
3670
12
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3671
12
      break;
3672
3673
15
    case SHT_DYNSYM:
3674
15
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3675
15
      break;
3676
3677
15
    case SHT_DYNAMIC:
3678
15
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3679
15
      break;
3680
3681
16
    case SHT_RELA:
3682
16
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3683
16
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3684
16
      break;
3685
3686
16
     case SHT_REL:
3687
16
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3688
16
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3689
16
      break;
3690
3691
13
     case SHT_GNU_versym:
3692
13
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3693
13
      break;
3694
3695
0
     case SHT_GNU_verdef:
3696
0
      this_hdr->sh_entsize = 0;
3697
      /* objcopy or strip will copy over sh_info, but may not set
3698
   cverdefs.  The linker will set cverdefs, but sh_info will be
3699
   zero.  */
3700
0
      if (this_hdr->sh_info == 0)
3701
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3702
0
      else
3703
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3704
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3705
0
      break;
3706
3707
14
    case SHT_GNU_verneed:
3708
14
      this_hdr->sh_entsize = 0;
3709
      /* objcopy or strip will copy over sh_info, but may not set
3710
   cverrefs.  The linker will set cverrefs, but sh_info will be
3711
   zero.  */
3712
14
      if (this_hdr->sh_info == 0)
3713
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3714
14
      else
3715
14
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3716
14
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3717
14
      break;
3718
3719
455
    case SHT_GROUP:
3720
455
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3721
455
      break;
3722
3723
7
    case SHT_GNU_HASH:
3724
7
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3725
7
      break;
3726
2.86k
    }
3727
3728
2.86k
  if ((asect->flags & SEC_ALLOC) != 0)
3729
2.17k
    this_hdr->sh_flags |= SHF_ALLOC;
3730
2.86k
  if ((asect->flags & SEC_READONLY) == 0)
3731
734
    this_hdr->sh_flags |= SHF_WRITE;
3732
2.86k
  if ((asect->flags & SEC_CODE) != 0)
3733
651
    this_hdr->sh_flags |= SHF_EXECINSTR;
3734
2.86k
  if ((asect->flags & SEC_MERGE) != 0)
3735
99
    {
3736
99
      this_hdr->sh_flags |= SHF_MERGE;
3737
99
      this_hdr->sh_entsize = asect->entsize;
3738
99
    }
3739
2.86k
  if ((asect->flags & SEC_STRINGS) != 0)
3740
92
    {
3741
92
      this_hdr->sh_flags |= SHF_STRINGS;
3742
92
      this_hdr->sh_entsize = asect->entsize;
3743
92
    }
3744
2.86k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3745
1.38k
    this_hdr->sh_flags |= SHF_GROUP;
3746
2.86k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3747
0
    {
3748
0
      this_hdr->sh_flags |= SHF_TLS;
3749
0
      if (asect->size == 0
3750
0
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3751
0
  {
3752
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3753
3754
0
    this_hdr->sh_size = 0;
3755
0
    if (o != NULL)
3756
0
      {
3757
0
        this_hdr->sh_size = o->offset + o->size;
3758
0
        if (this_hdr->sh_size != 0)
3759
0
    this_hdr->sh_type = SHT_NOBITS;
3760
0
      }
3761
0
  }
3762
0
    }
3763
2.86k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3764
33
    this_hdr->sh_flags |= SHF_EXCLUDE;
3765
3766
  /* If the section has relocs, set up a section header for the
3767
     SHT_REL[A] section.  If two relocation sections are required for
3768
     this section, it is up to the processor-specific back-end to
3769
     create the other.  */
3770
2.86k
  if ((asect->flags & SEC_RELOC) != 0)
3771
1.32k
    {
3772
      /* When doing a relocatable link, create both REL and RELA sections if
3773
   needed.  */
3774
1.32k
      if (arg->link_info
3775
    /* Do the normal setup if we wouldn't create any sections here.  */
3776
1.32k
    && esd->rel.count + esd->rela.count > 0
3777
1.32k
    && (bfd_link_relocatable (arg->link_info)
3778
0
        || arg->link_info->emitrelocations))
3779
0
  {
3780
0
    if (esd->rel.count && esd->rel.hdr == NULL
3781
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3782
0
              false, delay_sh_name_p))
3783
0
      {
3784
0
        arg->failed = true;
3785
0
        return;
3786
0
      }
3787
0
    if (esd->rela.count && esd->rela.hdr == NULL
3788
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3789
0
              true, delay_sh_name_p))
3790
0
      {
3791
0
        arg->failed = true;
3792
0
        return;
3793
0
      }
3794
0
  }
3795
1.32k
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3796
1.32k
            (asect->use_rela_p
3797
1.32k
             ? &esd->rela : &esd->rel),
3798
1.32k
            name,
3799
1.32k
            asect->use_rela_p,
3800
1.32k
            delay_sh_name_p))
3801
0
  {
3802
0
    arg->failed = true;
3803
0
    return;
3804
0
  }
3805
1.32k
    }
3806
3807
  /* Check for processor-specific section types.  */
3808
2.86k
  sh_type = this_hdr->sh_type;
3809
2.86k
  if (bed->elf_backend_fake_sections
3810
2.86k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3811
0
    {
3812
0
      arg->failed = true;
3813
0
      return;
3814
0
    }
3815
3816
2.86k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3817
39
    {
3818
      /* Don't change the header type from NOBITS if we are being
3819
   called for objcopy --only-keep-debug.  */
3820
39
      this_hdr->sh_type = sh_type;
3821
39
    }
3822
2.86k
}
3823
3824
/* Fill in the contents of a SHT_GROUP section.  Called from
3825
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3826
   when ELF targets use the generic linker, ld.  Called for ld -r
3827
   from bfd_elf_final_link.  */
3828
3829
void
3830
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3831
2.86k
{
3832
2.86k
  bool *failedptr = (bool *) failedptrarg;
3833
2.86k
  asection *elt, *first;
3834
2.86k
  unsigned char *loc;
3835
2.86k
  bool gas;
3836
3837
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3838
     elfxx-ia64.c.  */
3839
2.86k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3840
2.86k
      || sec->size == 0
3841
2.86k
      || *failedptr)
3842
2.41k
    return;
3843
3844
455
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3845
455
    {
3846
455
      unsigned long symindx = 0;
3847
3848
      /* elf_group_id will have been set up by objcopy and the
3849
   generic linker.  */
3850
455
      if (elf_group_id (sec) != NULL)
3851
455
  symindx = elf_group_id (sec)->udata.i;
3852
3853
455
      if (symindx == 0)
3854
0
  {
3855
    /* If called from the assembler, swap_out_syms will have set up
3856
       elf_section_syms.
3857
       PR 25699: A corrupt input file could contain bogus group info.  */
3858
0
    if (sec->index >= elf_num_section_syms (abfd)
3859
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3860
0
      {
3861
0
        *failedptr = true;
3862
0
        return;
3863
0
      }
3864
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3865
0
  }
3866
455
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3867
455
    }
3868
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3869
0
    {
3870
      /* The ELF backend linker sets sh_info to -2 when the group
3871
   signature symbol is global, and thus the index can't be
3872
   set until all local symbols are output.  */
3873
0
      asection *igroup;
3874
0
      struct bfd_elf_section_data *sec_data;
3875
0
      unsigned long symndx;
3876
0
      unsigned long extsymoff;
3877
0
      struct elf_link_hash_entry *h;
3878
3879
      /* The point of this little dance to the first SHF_GROUP section
3880
   then back to the SHT_GROUP section is that this gets us to
3881
   the SHT_GROUP in the input object.  */
3882
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3883
0
      sec_data = elf_section_data (igroup);
3884
0
      symndx = sec_data->this_hdr.sh_info;
3885
0
      extsymoff = 0;
3886
0
      if (!elf_bad_symtab (igroup->owner))
3887
0
  {
3888
0
    Elf_Internal_Shdr *symtab_hdr;
3889
3890
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3891
0
    extsymoff = symtab_hdr->sh_info;
3892
0
  }
3893
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3894
0
      while (h->root.type == bfd_link_hash_indirect
3895
0
       || h->root.type == bfd_link_hash_warning)
3896
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3897
3898
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3899
0
    }
3900
3901
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3902
455
  gas = true;
3903
455
  if (sec->contents == NULL)
3904
455
    {
3905
455
      gas = false;
3906
455
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3907
3908
      /* Arrange for the section to be written out.  */
3909
455
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3910
455
      if (sec->contents == NULL)
3911
0
  {
3912
0
    *failedptr = true;
3913
0
    return;
3914
0
  }
3915
455
      sec->alloced = 1;
3916
455
    }
3917
3918
455
  loc = sec->contents + sec->size;
3919
3920
  /* Get the pointer to the first section in the group that gas
3921
     squirreled away here.  objcopy arranges for this to be set to the
3922
     start of the input section group.  */
3923
455
  first = elt = elf_next_in_group (sec);
3924
3925
  /* First element is a flag word.  Rest of section is elf section
3926
     indices for all the sections of the group.  Write them backwards
3927
     just to keep the group in the same order as given in .section
3928
     directives, not that it matters.  */
3929
1.38k
  while (elt != NULL)
3930
1.38k
    {
3931
1.38k
      asection *s;
3932
3933
1.38k
      s = elt;
3934
1.38k
      if (!gas)
3935
1.38k
  s = s->output_section;
3936
1.38k
      if (s != NULL
3937
1.38k
    && !bfd_is_abs_section (s))
3938
1.38k
  {
3939
1.38k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3940
1.38k
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3941
3942
1.38k
    if (elf_sec->rel.hdr != NULL
3943
1.38k
        && (gas
3944
0
      || (input_elf_sec->rel.hdr != NULL
3945
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3946
0
      {
3947
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3948
0
        loc -= 4;
3949
0
        if (loc == sec->contents)
3950
0
    break;
3951
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3952
0
      }
3953
1.38k
    if (elf_sec->rela.hdr != NULL
3954
1.38k
        && (gas
3955
954
      || (input_elf_sec->rela.hdr != NULL
3956
954
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3957
954
      {
3958
954
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3959
954
        loc -= 4;
3960
954
        if (loc == sec->contents)
3961
0
    break;
3962
954
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3963
954
      }
3964
1.38k
    loc -= 4;
3965
1.38k
    if (loc == sec->contents)
3966
0
      break;
3967
1.38k
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
3968
1.38k
  }
3969
1.38k
      elt = elf_next_in_group (elt);
3970
1.38k
      if (elt == first)
3971
455
  break;
3972
1.38k
    }
3973
3974
  /* We should always get here with loc == sec->contents + 4.  Return
3975
     an error for bogus SHT_GROUP sections.  */
3976
455
  loc -= 4;
3977
455
  if (loc != sec->contents)
3978
1
    {
3979
      /* xgettext:c-format */
3980
1
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
3981
1
        abfd, sec);
3982
1
      bfd_set_error (bfd_error_bad_value);
3983
1
      *failedptr = true;
3984
1
      return;
3985
1
    }
3986
3987
454
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3988
454
}
3989
3990
/* Given NAME, the name of a relocation section stripped of its
3991
   .rel/.rela prefix, return the section in ABFD to which the
3992
   relocations apply.  */
3993
3994
asection *
3995
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3996
32
{
3997
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3998
     section likely apply to .got.plt or .got section.  */
3999
32
  if (get_elf_backend_data (abfd)->want_got_plt
4000
32
      && strcmp (name, ".plt") == 0)
4001
12
    {
4002
12
      asection *sec;
4003
4004
12
      name = ".got.plt";
4005
12
      sec = bfd_get_section_by_name (abfd, name);
4006
12
      if (sec != NULL)
4007
2
  return sec;
4008
10
      name = ".got";
4009
10
    }
4010
4011
30
  return bfd_get_section_by_name (abfd, name);
4012
32
}
4013
4014
/* Return the section to which RELOC_SEC applies.  */
4015
4016
static asection *
4017
elf_get_reloc_section (asection *reloc_sec)
4018
32
{
4019
32
  const char *name;
4020
32
  unsigned int type;
4021
32
  bfd *abfd;
4022
32
  const struct elf_backend_data *bed;
4023
4024
32
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4025
32
  if (type != SHT_REL && type != SHT_RELA)
4026
0
    return NULL;
4027
4028
  /* We look up the section the relocs apply to by name.  */
4029
32
  name = reloc_sec->name;
4030
32
  if (!startswith (name, ".rel"))
4031
0
    return NULL;
4032
32
  name += 4;
4033
32
  if (type == SHT_RELA && *name++ != 'a')
4034
0
    return NULL;
4035
4036
32
  abfd = reloc_sec->owner;
4037
32
  bed = get_elf_backend_data (abfd);
4038
32
  return bed->get_reloc_section (abfd, name);
4039
32
}
4040
4041
/* Assign all ELF section numbers.  The dummy first section is handled here
4042
   too.  The link/info pointers for the standard section types are filled
4043
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4044
   here for gas, objcopy, and when using the generic ELF linker.  */
4045
4046
static bool
4047
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4048
96
{
4049
96
  struct elf_obj_tdata *t = elf_tdata (abfd);
4050
96
  asection *sec;
4051
96
  unsigned int section_number;
4052
96
  Elf_Internal_Shdr **i_shdrp;
4053
96
  struct bfd_elf_section_data *d;
4054
96
  bool need_symtab;
4055
96
  size_t amt;
4056
4057
96
  section_number = 1;
4058
4059
96
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4060
4061
  /* SHT_GROUP sections are in relocatable files only.  */
4062
96
  if (link_info == NULL || !link_info->resolve_section_groups)
4063
96
    {
4064
96
      size_t reloc_count = 0;
4065
4066
      /* Put SHT_GROUP sections first.  */
4067
2.96k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4068
2.86k
  {
4069
2.86k
    d = elf_section_data (sec);
4070
4071
2.86k
    if (d->this_hdr.sh_type == SHT_GROUP)
4072
455
      {
4073
455
        if (sec->flags & SEC_LINKER_CREATED)
4074
0
    {
4075
      /* Remove the linker created SHT_GROUP sections.  */
4076
0
      bfd_section_list_remove (abfd, sec);
4077
0
      abfd->section_count--;
4078
0
    }
4079
455
        else
4080
455
    d->this_idx = section_number++;
4081
455
      }
4082
4083
    /* Count relocations.  */
4084
2.86k
    reloc_count += sec->reloc_count;
4085
2.86k
  }
4086
4087
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4088
96
      if (reloc_count == 0)
4089
64
  abfd->flags &= ~HAS_RELOC;
4090
32
      else
4091
32
  abfd->flags |= HAS_RELOC;
4092
96
    }
4093
4094
2.96k
  for (sec = abfd->sections; sec; sec = sec->next)
4095
2.86k
    {
4096
2.86k
      d = elf_section_data (sec);
4097
4098
2.86k
      if (d->this_hdr.sh_type != SHT_GROUP)
4099
2.41k
  d->this_idx = section_number++;
4100
2.86k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4101
2.86k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4102
2.86k
      if (d->rel.hdr)
4103
0
  {
4104
0
    d->rel.idx = section_number++;
4105
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4106
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4107
0
  }
4108
2.86k
      else
4109
2.86k
  d->rel.idx = 0;
4110
4111
2.86k
      if (d->rela.hdr)
4112
1.32k
  {
4113
1.32k
    d->rela.idx = section_number++;
4114
1.32k
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4115
1.32k
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4116
1.32k
  }
4117
1.54k
      else
4118
1.54k
  d->rela.idx = 0;
4119
2.86k
    }
4120
4121
96
  need_symtab = (bfd_get_symcount (abfd) > 0
4122
96
     || (link_info == NULL
4123
56
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4124
56
       == HAS_RELOC)));
4125
96
  if (need_symtab)
4126
40
    {
4127
40
      elf_onesymtab (abfd) = section_number++;
4128
40
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4129
40
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4130
0
  {
4131
0
    elf_section_list *entry;
4132
4133
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4134
4135
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4136
0
    entry->ndx = section_number++;
4137
0
    elf_symtab_shndx_list (abfd) = entry;
4138
0
    entry->hdr.sh_name
4139
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4140
0
              ".symtab_shndx", false);
4141
0
    if (entry->hdr.sh_name == (unsigned int) -1)
4142
0
      return false;
4143
0
  }
4144
40
      elf_strtab_sec (abfd) = section_number++;
4145
40
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4146
40
    }
4147
4148
96
  elf_shstrtab_sec (abfd) = section_number++;
4149
96
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4150
96
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4151
4152
96
  if (section_number >= SHN_LORESERVE)
4153
0
    {
4154
      /* xgettext:c-format */
4155
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4156
0
        abfd, section_number);
4157
0
      return false;
4158
0
    }
4159
4160
96
  elf_numsections (abfd) = section_number;
4161
96
  elf_elfheader (abfd)->e_shnum = section_number;
4162
4163
  /* Set up the list of section header pointers, in agreement with the
4164
     indices.  */
4165
96
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4166
96
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4167
96
  if (i_shdrp == NULL)
4168
0
    return false;
4169
4170
96
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4171
96
             sizeof (Elf_Internal_Shdr));
4172
96
  if (i_shdrp[0] == NULL)
4173
0
    {
4174
0
      bfd_release (abfd, i_shdrp);
4175
0
      return false;
4176
0
    }
4177
4178
96
  elf_elfsections (abfd) = i_shdrp;
4179
4180
96
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4181
96
  if (need_symtab)
4182
40
    {
4183
40
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4184
40
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4185
0
  {
4186
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4187
0
    BFD_ASSERT (entry != NULL);
4188
0
    i_shdrp[entry->ndx] = & entry->hdr;
4189
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4190
0
  }
4191
40
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4192
40
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4193
40
    }
4194
4195
2.96k
  for (sec = abfd->sections; sec; sec = sec->next)
4196
2.86k
    {
4197
2.86k
      asection *s;
4198
4199
2.86k
      d = elf_section_data (sec);
4200
4201
2.86k
      i_shdrp[d->this_idx] = &d->this_hdr;
4202
2.86k
      if (d->rel.idx != 0)
4203
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4204
2.86k
      if (d->rela.idx != 0)
4205
1.32k
  i_shdrp[d->rela.idx] = d->rela.hdr;
4206
4207
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4208
4209
      /* sh_link of a reloc section is the section index of the symbol
4210
   table.  sh_info is the section index of the section to which
4211
   the relocation entries apply.  */
4212
2.86k
      if (d->rel.idx != 0)
4213
0
  {
4214
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4215
0
    d->rel.hdr->sh_info = d->this_idx;
4216
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4217
0
  }
4218
2.86k
      if (d->rela.idx != 0)
4219
1.32k
  {
4220
1.32k
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4221
1.32k
    d->rela.hdr->sh_info = d->this_idx;
4222
1.32k
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4223
1.32k
  }
4224
4225
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4226
2.86k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4227
1
  {
4228
1
    s = elf_linked_to_section (sec);
4229
    /* We can now have a NULL linked section pointer.
4230
       This happens when the sh_link field is 0, which is done
4231
       when a linked to section is discarded but the linking
4232
       section has been retained for some reason.  */
4233
1
    if (s)
4234
1
      {
4235
        /* Check discarded linkonce section.  */
4236
1
        if (discarded_section (s))
4237
0
    {
4238
0
      asection *kept;
4239
0
      _bfd_error_handler
4240
        /* xgettext:c-format */
4241
0
        (_("%pB: sh_link of section `%pA' points to"
4242
0
           " discarded section `%pA' of `%pB'"),
4243
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4244
      /* Point to the kept section if it has the same
4245
         size as the discarded one.  */
4246
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4247
0
      if (kept == NULL)
4248
0
        {
4249
0
          bfd_set_error (bfd_error_bad_value);
4250
0
          return false;
4251
0
        }
4252
0
      s = kept;
4253
0
    }
4254
        /* Handle objcopy. */
4255
1
        else if (s->output_section == NULL)
4256
0
    {
4257
0
      _bfd_error_handler
4258
        /* xgettext:c-format */
4259
0
        (_("%pB: sh_link of section `%pA' points to"
4260
0
           " removed section `%pA' of `%pB'"),
4261
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4262
0
      bfd_set_error (bfd_error_bad_value);
4263
0
      return false;
4264
0
    }
4265
1
        s = s->output_section;
4266
1
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4267
1
      }
4268
1
  }
4269
4270
2.86k
      switch (d->this_hdr.sh_type)
4271
2.86k
  {
4272
16
  case SHT_REL:
4273
32
  case SHT_RELA:
4274
    /* sh_link is the section index of the symbol table.
4275
       sh_info is the section index of the section to which the
4276
       relocation entries apply.  */
4277
32
    if (d->this_hdr.sh_link == 0)
4278
32
      {
4279
        /* FIXME maybe: If this is a reloc section which we are
4280
     treating as a normal section then we likely should
4281
     not be assuming its sh_link is .dynsym or .symtab.  */
4282
32
        if ((sec->flags & SEC_ALLOC) != 0)
4283
31
    {
4284
31
      s = bfd_get_section_by_name (abfd, ".dynsym");
4285
31
      if (s != NULL)
4286
29
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4287
31
    }
4288
1
        else
4289
1
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4290
32
      }
4291
4292
32
    s = elf_get_reloc_section (sec);
4293
32
    if (s != NULL)
4294
18
      {
4295
18
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4296
18
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4297
18
      }
4298
32
    break;
4299
4300
15
  case SHT_STRTAB:
4301
    /* We assume that a section named .stab*str is a stabs
4302
       string section.  We look for a section with the same name
4303
       but without the trailing ``str'', and set its sh_link
4304
       field to point to this section.  */
4305
15
    if (startswith (sec->name, ".stab")
4306
15
        && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4307
0
      {
4308
0
        size_t len;
4309
0
        char *alc;
4310
4311
0
        len = strlen (sec->name);
4312
0
        alc = (char *) bfd_malloc (len - 2);
4313
0
        if (alc == NULL)
4314
0
    return false;
4315
0
        memcpy (alc, sec->name, len - 3);
4316
0
        alc[len - 3] = '\0';
4317
0
        s = bfd_get_section_by_name (abfd, alc);
4318
0
        free (alc);
4319
0
        if (s != NULL)
4320
0
    {
4321
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4322
4323
      /* This is a .stab section.  */
4324
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4325
0
    }
4326
0
      }
4327
15
    break;
4328
4329
15
  case SHT_DYNAMIC:
4330
30
  case SHT_DYNSYM:
4331
44
  case SHT_GNU_verneed:
4332
44
  case SHT_GNU_verdef:
4333
    /* sh_link is the section header index of the string table
4334
       used for the dynamic entries, or the symbol table, or the
4335
       version strings.  */
4336
44
    s = bfd_get_section_by_name (abfd, ".dynstr");
4337
44
    if (s != NULL)
4338
41
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4339
44
    break;
4340
4341
0
  case SHT_GNU_LIBLIST:
4342
    /* sh_link is the section header index of the prelink library
4343
       list used for the dynamic entries, or the symbol table, or
4344
       the version strings.  */
4345
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4346
0
                ? ".dynstr" : ".gnu.libstr"));
4347
0
    if (s != NULL)
4348
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4349
0
    break;
4350
4351
12
  case SHT_HASH:
4352
19
  case SHT_GNU_HASH:
4353
32
  case SHT_GNU_versym:
4354
    /* sh_link is the section header index of the symbol table
4355
       this hash table or version table is for.  */
4356
32
    s = bfd_get_section_by_name (abfd, ".dynsym");
4357
32
    if (s != NULL)
4358
30
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4359
32
    break;
4360
4361
455
  case SHT_GROUP:
4362
455
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4363
2.86k
  }
4364
2.86k
    }
4365
4366
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4367
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4368
     debug section name from .debug_* to .zdebug_* if needed.  */
4369
4370
96
  return true;
4371
96
}
4372
4373
static bool
4374
sym_is_global (bfd *abfd, asymbol *sym)
4375
29.2k
{
4376
  /* If the backend has a special mapping, use it.  */
4377
29.2k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4378
29.2k
  if (bed->elf_backend_sym_is_global)
4379
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4380
4381
29.2k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4382
29.2k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4383
29.2k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4384
29.2k
}
4385
4386
/* Filter global symbols of ABFD to include in the import library.  All
4387
   SYMCOUNT symbols of ABFD can be examined from their pointers in
4388
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4389
   the beginning of that array.
4390
4391
   Returns the number of symbols to keep.  */
4392
4393
unsigned int
4394
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4395
        asymbol **syms, long symcount)
4396
0
{
4397
0
  long src_count, dst_count = 0;
4398
4399
0
  for (src_count = 0; src_count < symcount; src_count++)
4400
0
    {
4401
0
      asymbol *sym = syms[src_count];
4402
0
      char *name = (char *) bfd_asymbol_name (sym);
4403
0
      struct bfd_link_hash_entry *h;
4404
4405
0
      if (!sym_is_global (abfd, sym))
4406
0
  continue;
4407
4408
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4409
0
      if (h == NULL)
4410
0
  continue;
4411
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4412
0
  continue;
4413
0
      if (h->linker_def || h->ldscript_def)
4414
0
  continue;
4415
4416
0
      syms[dst_count++] = sym;
4417
0
    }
4418
4419
0
  syms[dst_count] = NULL;
4420
4421
0
  return dst_count;
4422
0
}
4423
4424
/* Don't output symbols for sections that are not going to be output,
4425
   that are duplicates or there is no BFD section.  */
4426
4427
static bool
4428
ignore_sym (asymbol *sym)
4429
36.7k
{
4430
36.7k
  if (sym == NULL)
4431
0
    return false;
4432
4433
36.7k
  if (sym->section == NULL)
4434
0
    return true;
4435
4436
36.7k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4437
10.7k
    {
4438
10.7k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4439
5.76k
  return true;
4440
      /* With ld -r on generic elf targets it is possible to have
4441
   multiple section symbols in the output for a given section.
4442
   We'd like to get rid of all but the first one.  This drops
4443
   them if the first input section is non-zero size, but fails
4444
   to do so if the first input section is zero sized.  */
4445
5.00k
      if (sym->section->output_offset != 0)
4446
0
  return true;
4447
5.00k
    }
4448
4449
30.9k
  return discarded_section (sym->section);
4450
36.7k
}
4451
4452
/* Map symbol from it's internal number to the external number, moving
4453
   all local symbols to be at the head of the list.  */
4454
4455
static bool
4456
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4457
40
{
4458
40
  unsigned int symcount = bfd_get_symcount (abfd);
4459
40
  asymbol **syms = bfd_get_outsymbols (abfd);
4460
40
  asymbol **sect_syms;
4461
40
  unsigned int num_locals = 0;
4462
40
  unsigned int num_globals = 0;
4463
40
  unsigned int max_index = 0;
4464
40
  unsigned int idx;
4465
40
  asection *asect;
4466
40
  asymbol **new_syms;
4467
40
  size_t amt;
4468
4469
#ifdef DEBUG
4470
  fprintf (stderr, "elf_map_symbols\n");
4471
  fflush (stderr);
4472
#endif
4473
4474
2.73k
  for (asect = abfd->sections; asect; asect = asect->next)
4475
2.69k
    {
4476
2.69k
      if (max_index < asect->index)
4477
2.65k
  max_index = asect->index;
4478
2.69k
    }
4479
4480
40
  max_index++;
4481
40
  amt = max_index * sizeof (asymbol *);
4482
40
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4483
40
  if (sect_syms == NULL)
4484
0
    return false;
4485
40
  elf_section_syms (abfd) = sect_syms;
4486
40
  elf_num_section_syms (abfd) = max_index;
4487
4488
  /* Init sect_syms entries for any section symbols we have already
4489
     decided to output.  */
4490
14.8k
  for (idx = 0; idx < symcount; idx++)
4491
14.7k
    {
4492
14.7k
      asymbol *sym = syms[idx];
4493
4494
14.7k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4495
14.7k
    && sym->value == 0
4496
14.7k
    && !ignore_sym (sym)
4497
14.7k
    && !bfd_is_abs_section (sym->section))
4498
1.64k
  {
4499
1.64k
    asection *sec = sym->section;
4500
4501
1.64k
    if (sec->owner != abfd)
4502
1.64k
      {
4503
1.64k
        sec = sec->output_section;
4504
1.64k
        if (sec == NULL)
4505
0
    return false;
4506
1.64k
      }
4507
4508
1.64k
    sect_syms[sec->index] = syms[idx];
4509
1.64k
  }
4510
14.7k
    }
4511
4512
  /* Classify all of the symbols.  */
4513
14.8k
  for (idx = 0; idx < symcount; idx++)
4514
14.7k
    {
4515
14.7k
      if (ignore_sym (syms[idx]))
4516
156
  continue;
4517
14.6k
      if (sym_is_global (abfd, syms[idx]))
4518
6.12k
  num_globals++;
4519
8.49k
      else
4520
8.49k
  num_locals++;
4521
14.6k
    }
4522
4523
  /* We will be adding a section symbol for each normal BFD section.  Most
4524
     sections will already have a section symbol in outsymbols, but
4525
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4526
     at least in that case.  */
4527
2.73k
  for (asect = abfd->sections; asect; asect = asect->next)
4528
2.69k
    {
4529
2.69k
      asymbol *sym = asect->symbol;
4530
      /* Don't include ignored section symbols.  */
4531
2.69k
      if (!ignore_sym (sym)
4532
2.69k
    && sect_syms[asect->index] == NULL)
4533
0
  {
4534
0
    if (sym_is_global (abfd, asect->symbol))
4535
0
      num_globals++;
4536
0
    else
4537
0
      num_locals++;
4538
0
  }
4539
2.69k
    }
4540
4541
  /* Now sort the symbols so the local symbols are first.  */
4542
40
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4543
40
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4544
40
  if (new_syms == NULL)
4545
0
    return false;
4546
4547
40
  unsigned int num_globals2 = 0;
4548
40
  unsigned int num_locals2 = 0;
4549
14.8k
  for (idx = 0; idx < symcount; idx++)
4550
14.7k
    {
4551
14.7k
      asymbol *sym = syms[idx];
4552
14.7k
      unsigned int i;
4553
4554
14.7k
      if (ignore_sym (sym))
4555
156
  continue;
4556
4557
14.6k
      if (sym_is_global (abfd, sym))
4558
6.12k
  i = num_locals + num_globals2++;
4559
8.49k
      else
4560
8.49k
  i = num_locals2++;
4561
14.6k
      new_syms[i] = sym;
4562
14.6k
      sym->udata.i = i + 1;
4563
14.6k
    }
4564
2.73k
  for (asect = abfd->sections; asect; asect = asect->next)
4565
2.69k
    {
4566
2.69k
      asymbol *sym = asect->symbol;
4567
2.69k
      if (!ignore_sym (sym)
4568
2.69k
    && sect_syms[asect->index] == NULL)
4569
0
  {
4570
0
    unsigned int i;
4571
4572
0
    sect_syms[asect->index] = sym;
4573
0
    if (sym_is_global (abfd, sym))
4574
0
      i = num_locals + num_globals2++;
4575
0
    else
4576
0
      i = num_locals2++;
4577
0
    new_syms[i] = sym;
4578
0
    sym->udata.i = i + 1;
4579
0
  }
4580
2.69k
    }
4581
4582
40
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4583
4584
40
  *pnum_locals = num_locals;
4585
40
  return true;
4586
40
}
4587
4588
/* Assign a file position to a section, optionally aligning to the
4589
   required section alignment.  */
4590
4591
file_ptr
4592
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4593
             file_ptr offset,
4594
             bool align,
4595
             unsigned char log_file_align)
4596
3.90k
{
4597
3.90k
  if (i_shdrp->sh_addralign > 1)
4598
3.08k
    {
4599
3.08k
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4600
4601
3.08k
      if (align)
4602
56
  offset = BFD_ALIGN (offset, salign);
4603
3.02k
      else if (log_file_align)
4604
3.02k
  {
4605
    /* Heuristic: Cap alignment at log_file_align.  */
4606
3.02k
    file_ptr falign = 1u << log_file_align;
4607
4608
3.02k
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4609
3.02k
  }
4610
3.08k
    }
4611
3.90k
  i_shdrp->sh_offset = offset;
4612
3.90k
  if (i_shdrp->bfd_section != NULL)
4613
2.39k
    i_shdrp->bfd_section->filepos = offset;
4614
3.90k
  if (i_shdrp->sh_type != SHT_NOBITS)
4615
3.88k
    offset += i_shdrp->sh_size;
4616
3.90k
  return offset;
4617
3.90k
}
4618
4619
/* Compute the file positions we are going to put the sections at, and
4620
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4621
   is not NULL, this is being called by the ELF backend linker.  */
4622
4623
bool
4624
_bfd_elf_compute_section_file_positions (bfd *abfd,
4625
           struct bfd_link_info *link_info)
4626
96
{
4627
96
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4628
96
  struct fake_section_arg fsargs;
4629
96
  bool failed;
4630
96
  struct elf_strtab_hash *strtab = NULL;
4631
96
  Elf_Internal_Shdr *shstrtab_hdr;
4632
96
  bool need_symtab;
4633
4634
96
  if (abfd->output_has_begun)
4635
0
    return true;
4636
4637
  /* Do any elf backend specific processing first.  */
4638
96
  if (bed->elf_backend_begin_write_processing)
4639
3
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4640
4641
96
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4642
0
    return false;
4643
4644
96
  fsargs.failed = false;
4645
96
  fsargs.link_info = link_info;
4646
96
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4647
96
  if (fsargs.failed)
4648
0
    return false;
4649
4650
96
  if (!assign_section_numbers (abfd, link_info))
4651
0
    return false;
4652
4653
  /* The backend linker builds symbol table information itself.  */
4654
96
  need_symtab = (link_info == NULL
4655
96
     && (bfd_get_symcount (abfd) > 0
4656
96
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4657
56
       == HAS_RELOC)));
4658
96
  if (need_symtab)
4659
40
    {
4660
      /* Non-zero if doing a relocatable link.  */
4661
40
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4662
4663
40
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4664
0
  return false;
4665
40
    }
4666
4667
96
  failed = false;
4668
96
  if (link_info == NULL)
4669
96
    {
4670
96
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4671
96
      if (failed)
4672
1
  goto err_free_strtab;
4673
96
    }
4674
4675
95
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4676
  /* sh_name was set in init_file_header.  */
4677
95
  shstrtab_hdr->sh_type = SHT_STRTAB;
4678
95
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4679
95
  shstrtab_hdr->sh_addr = 0;
4680
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4681
95
  shstrtab_hdr->sh_entsize = 0;
4682
95
  shstrtab_hdr->sh_link = 0;
4683
95
  shstrtab_hdr->sh_info = 0;
4684
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4685
95
  shstrtab_hdr->sh_addralign = 1;
4686
4687
95
  if (!assign_file_positions_except_relocs (abfd, link_info))
4688
0
    goto err_free_strtab;
4689
4690
95
  if (strtab != NULL)
4691
39
    {
4692
39
      file_ptr off;
4693
39
      Elf_Internal_Shdr *hdr;
4694
4695
39
      off = elf_next_file_pos (abfd);
4696
4697
39
      hdr = & elf_symtab_hdr (abfd);
4698
39
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4699
4700
39
      if (elf_symtab_shndx_list (abfd) != NULL)
4701
0
  {
4702
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4703
0
    if (hdr->sh_size != 0)
4704
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4705
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4706
0
  }
4707
4708
39
      hdr = &elf_tdata (abfd)->strtab_hdr;
4709
39
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4710
4711
39
      elf_next_file_pos (abfd) = off;
4712
4713
      /* Now that we know where the .strtab section goes, write it
4714
   out.  */
4715
39
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4716
39
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4717
0
  goto err_free_strtab;
4718
39
      _bfd_elf_strtab_free (strtab);
4719
39
    }
4720
4721
95
  abfd->output_has_begun = true;
4722
95
  return true;
4723
4724
1
 err_free_strtab:
4725
1
  if (strtab != NULL)
4726
1
    _bfd_elf_strtab_free (strtab);
4727
1
  return false;
4728
95
}
4729
4730
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4731
   function effectively returns whether --eh-frame-hdr is given on the
4732
   command line.  After size_dynamic_sections the result reflects
4733
   whether .eh_frame_hdr will actually be output (sizing isn't done
4734
   until ldemul_after_allocation).  */
4735
4736
static asection *
4737
elf_eh_frame_hdr (const struct bfd_link_info *info)
4738
2
{
4739
2
  if (info != NULL && is_elf_hash_table (info->hash))
4740
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4741
2
  return NULL;
4742
2
}
4743
4744
/* Make an initial estimate of the size of the program header.  If we
4745
   get the number wrong here, we'll redo section placement.  */
4746
4747
static bfd_size_type
4748
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4749
1
{
4750
1
  size_t segs;
4751
1
  asection *s;
4752
1
  const struct elf_backend_data *bed;
4753
4754
  /* Assume we will need exactly two PT_LOAD segments: one for text
4755
     and one for data.  */
4756
1
  segs = 2;
4757
4758
1
  s = bfd_get_section_by_name (abfd, ".interp");
4759
1
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4760
0
    {
4761
      /* If we have a loadable interpreter section, we need a
4762
   PT_INTERP segment.  In this case, assume we also need a
4763
   PT_PHDR segment, although that may not be true for all
4764
   targets.  */
4765
0
      segs += 2;
4766
0
    }
4767
4768
1
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4769
0
    {
4770
      /* We need a PT_DYNAMIC segment.  */
4771
0
      ++segs;
4772
0
    }
4773
4774
1
  if (info != NULL && info->relro)
4775
0
    {
4776
      /* We need a PT_GNU_RELRO segment.  */
4777
0
      ++segs;
4778
0
    }
4779
4780
1
  if (elf_eh_frame_hdr (info))
4781
0
    {
4782
      /* We need a PT_GNU_EH_FRAME segment.  */
4783
0
      ++segs;
4784
0
    }
4785
4786
1
  if (elf_stack_flags (abfd))
4787
0
    {
4788
      /* We need a PT_GNU_STACK segment.  */
4789
0
      ++segs;
4790
0
    }
4791
4792
1
  if (elf_sframe (abfd))
4793
0
    {
4794
      /* We need a PT_GNU_SFRAME segment.  */
4795
0
      ++segs;
4796
0
    }
4797
4798
1
  s = bfd_get_section_by_name (abfd,
4799
1
             NOTE_GNU_PROPERTY_SECTION_NAME);
4800
1
  if (s != NULL && s->size != 0)
4801
0
    {
4802
      /* We need a PT_GNU_PROPERTY segment.  */
4803
0
      ++segs;
4804
0
    }
4805
4806
2
  for (s = abfd->sections; s != NULL; s = s->next)
4807
1
    {
4808
1
      if ((s->flags & SEC_LOAD) != 0
4809
1
    && elf_section_type (s) == SHT_NOTE)
4810
0
  {
4811
0
    unsigned int alignment_power;
4812
    /* We need a PT_NOTE segment.  */
4813
0
    ++segs;
4814
    /* Try to create just one PT_NOTE segment for all adjacent
4815
       loadable SHT_NOTE sections.  gABI requires that within a
4816
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4817
       each note should have the same alignment.  So we check
4818
       whether the sections are correctly aligned.  */
4819
0
    alignment_power = s->alignment_power;
4820
0
    while (s->next != NULL
4821
0
     && s->next->alignment_power == alignment_power
4822
0
     && (s->next->flags & SEC_LOAD) != 0
4823
0
     && elf_section_type (s->next) == SHT_NOTE)
4824
0
      s = s->next;
4825
0
  }
4826
1
    }
4827
4828
2
  for (s = abfd->sections; s != NULL; s = s->next)
4829
1
    {
4830
1
      if (s->flags & SEC_THREAD_LOCAL)
4831
0
  {
4832
    /* We need a PT_TLS segment.  */
4833
0
    ++segs;
4834
0
    break;
4835
0
  }
4836
1
    }
4837
4838
1
  bed = get_elf_backend_data (abfd);
4839
4840
1
  if ((abfd->flags & D_PAGED) != 0
4841
1
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4842
0
    {
4843
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4844
0
      bfd_vma commonpagesize;
4845
0
      unsigned int page_align_power;
4846
4847
0
      if (info != NULL)
4848
0
  commonpagesize = info->commonpagesize;
4849
0
      else
4850
0
  commonpagesize = bed->commonpagesize;
4851
0
      page_align_power = bfd_log2 (commonpagesize);
4852
0
      for (s = abfd->sections; s != NULL; s = s->next)
4853
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4854
0
    {
4855
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4856
0
        {
4857
0
    _bfd_error_handler
4858
      /* xgettext:c-format */
4859
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4860
0
         "sh_info field: %d"),
4861
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4862
0
    continue;
4863
0
        }
4864
      /* Align mbind section to page size.  */
4865
0
      if (s->alignment_power < page_align_power)
4866
0
        s->alignment_power = page_align_power;
4867
0
      segs ++;
4868
0
    }
4869
0
    }
4870
4871
  /* Let the backend count up any program headers it might need.  */
4872
1
  if (bed->elf_backend_additional_program_headers)
4873
1
    {
4874
1
      int a;
4875
4876
1
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4877
1
      if (a == -1)
4878
0
  abort ();
4879
1
      segs += a;
4880
1
    }
4881
4882
1
  return segs * bed->s->sizeof_phdr;
4883
1
}
4884
4885
/* Find the segment that contains the output_section of section.  */
4886
4887
Elf_Internal_Phdr *
4888
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4889
0
{
4890
0
  struct elf_segment_map *m;
4891
0
  Elf_Internal_Phdr *p;
4892
4893
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4894
0
       m != NULL;
4895
0
       m = m->next, p++)
4896
0
    {
4897
0
      int i;
4898
4899
0
      for (i = m->count - 1; i >= 0; i--)
4900
0
  if (m->sections[i] == section)
4901
0
    return p;
4902
0
    }
4903
4904
0
  return NULL;
4905
0
}
4906
4907
/* Create a mapping from a set of sections to a program segment.  */
4908
4909
static struct elf_segment_map *
4910
make_mapping (bfd *abfd,
4911
        asection **sections,
4912
        unsigned int from,
4913
        unsigned int to,
4914
        bool phdr)
4915
1
{
4916
1
  struct elf_segment_map *m;
4917
1
  unsigned int i;
4918
1
  asection **hdrpp;
4919
1
  size_t amt;
4920
4921
1
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4922
1
  amt += (to - from) * sizeof (asection *);
4923
1
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4924
1
  if (m == NULL)
4925
0
    return NULL;
4926
1
  m->next = NULL;
4927
1
  m->p_type = PT_LOAD;
4928
2
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4929
1
    m->sections[i - from] = *hdrpp;
4930
1
  m->count = to - from;
4931
4932
1
  if (from == 0 && phdr)
4933
1
    {
4934
      /* Include the headers in the first PT_LOAD segment.  */
4935
1
      m->includes_filehdr = 1;
4936
1
      m->includes_phdrs = 1;
4937
1
    }
4938
4939
1
  return m;
4940
1
}
4941
4942
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
4943
   on failure.  */
4944
4945
struct elf_segment_map *
4946
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4947
0
{
4948
0
  struct elf_segment_map *m;
4949
4950
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
4951
0
               sizeof (struct elf_segment_map));
4952
0
  if (m == NULL)
4953
0
    return NULL;
4954
0
  m->next = NULL;
4955
0
  m->p_type = PT_DYNAMIC;
4956
0
  m->count = 1;
4957
0
  m->sections[0] = dynsec;
4958
4959
0
  return m;
4960
0
}
4961
4962
/* Possibly add or remove segments from the segment map.  */
4963
4964
static bool
4965
elf_modify_segment_map (bfd *abfd,
4966
      struct bfd_link_info *info,
4967
      bool remove_empty_load)
4968
28
{
4969
28
  struct elf_segment_map **m;
4970
28
  const struct elf_backend_data *bed;
4971
4972
  /* The placement algorithm assumes that non allocated sections are
4973
     not in PT_LOAD segments.  We ensure this here by removing such
4974
     sections from the segment map.  We also remove excluded
4975
     sections.  Finally, any PT_LOAD segment without sections is
4976
     removed.  */
4977
28
  m = &elf_seg_map (abfd);
4978
130
  while (*m)
4979
102
    {
4980
102
      unsigned int i, new_count;
4981
4982
520
      for (new_count = 0, i = 0; i < (*m)->count; i++)
4983
418
  {
4984
418
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4985
418
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4986
418
      || (*m)->p_type != PT_LOAD))
4987
418
      {
4988
418
        (*m)->sections[new_count] = (*m)->sections[i];
4989
418
        new_count++;
4990
418
      }
4991
418
  }
4992
102
      (*m)->count = new_count;
4993
4994
102
      if (remove_empty_load
4995
102
    && (*m)->p_type == PT_LOAD
4996
102
    && (*m)->count == 0
4997
102
    && !(*m)->includes_phdrs)
4998
0
  *m = (*m)->next;
4999
102
      else
5000
102
  m = &(*m)->next;
5001
102
    }
5002
5003
28
  bed = get_elf_backend_data (abfd);
5004
28
  if (bed->elf_backend_modify_segment_map != NULL)
5005
6
    {
5006
6
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5007
0
  return false;
5008
6
    }
5009
5010
28
  return true;
5011
28
}
5012
5013
#define IS_TBSS(s) \
5014
15
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5015
5016
/* Set up a mapping from BFD sections to program segments.  Update
5017
   NEED_LAYOUT if the section layout is changed.  */
5018
5019
bool
5020
_bfd_elf_map_sections_to_segments (bfd *abfd,
5021
           struct bfd_link_info *info,
5022
           bool *need_layout)
5023
28
{
5024
28
  unsigned int count;
5025
28
  struct elf_segment_map *m;
5026
28
  asection **sections = NULL;
5027
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5028
28
  bool no_user_phdrs;
5029
5030
28
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5031
5032
28
  if (info != NULL)
5033
0
    {
5034
0
      info->user_phdrs = !no_user_phdrs;
5035
5036
      /* Size the relative relocations if DT_RELR is enabled.  */
5037
0
      if (info->enable_dt_relr
5038
0
    && need_layout != NULL
5039
0
    && bed->size_relative_relocs
5040
0
    && !bed->size_relative_relocs (info, need_layout))
5041
0
  info->callbacks->fatal
5042
0
    (_("%P: failed to size relative relocations\n"));
5043
0
    }
5044
5045
28
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5046
1
    {
5047
1
      asection *s;
5048
1
      unsigned int i;
5049
1
      struct elf_segment_map *mfirst;
5050
1
      struct elf_segment_map **pm;
5051
1
      asection *last_hdr;
5052
1
      bfd_vma last_size;
5053
1
      unsigned int hdr_index;
5054
1
      bfd_vma maxpagesize;
5055
1
      asection **hdrpp;
5056
1
      bool phdr_in_segment;
5057
1
      bool writable;
5058
1
      bool executable;
5059
1
      unsigned int tls_count = 0;
5060
1
      asection *first_tls = NULL;
5061
1
      asection *first_mbind = NULL;
5062
1
      asection *dynsec, *eh_frame_hdr;
5063
1
      asection *sframe;
5064
1
      size_t amt;
5065
1
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5066
1
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5067
1
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5068
5069
      /* Select the allocated sections, and sort them.  */
5070
5071
1
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5072
1
      sections = (asection **) bfd_malloc (amt);
5073
1
      if (sections == NULL)
5074
0
  goto error_return;
5075
5076
      /* Calculate top address, avoiding undefined behaviour of shift
5077
   left operator when shift count is equal to size of type
5078
   being shifted.  */
5079
1
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5080
1
      addr_mask = (addr_mask << 1) + 1;
5081
5082
1
      i = 0;
5083
2
      for (s = abfd->sections; s != NULL; s = s->next)
5084
1
  {
5085
1
    if ((s->flags & SEC_ALLOC) != 0)
5086
1
      {
5087
        /* target_index is unused until bfd_elf_final_link
5088
     starts output of section symbols.  Use it to make
5089
     qsort stable.  */
5090
1
        s->target_index = i;
5091
1
        sections[i] = s;
5092
1
        ++i;
5093
        /* A wrapping section potentially clashes with header.  */
5094
1
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5095
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5096
1
      }
5097
1
  }
5098
1
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5099
1
      count = i;
5100
5101
1
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5102
5103
1
      phdr_size = elf_program_header_size (abfd);
5104
1
      if (phdr_size == (bfd_size_type) -1)
5105
1
  phdr_size = get_program_header_size (abfd, info);
5106
1
      phdr_size += bed->s->sizeof_ehdr;
5107
      /* phdr_size is compared to LMA values which are in bytes.  */
5108
1
      phdr_size /= opb;
5109
1
      if (info != NULL)
5110
0
  maxpagesize = info->maxpagesize;
5111
1
      else
5112
1
  maxpagesize = bed->maxpagesize;
5113
1
      if (maxpagesize == 0)
5114
0
  maxpagesize = 1;
5115
1
      phdr_in_segment = info != NULL && info->load_phdrs;
5116
1
      if (count != 0
5117
1
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5118
1
        >= (phdr_size & (maxpagesize - 1))))
5119
  /* For compatibility with old scripts that may not be using
5120
     SIZEOF_HEADERS, add headers when it looks like space has
5121
     been left for them.  */
5122
1
  phdr_in_segment = true;
5123
5124
      /* Build the mapping.  */
5125
1
      mfirst = NULL;
5126
1
      pm = &mfirst;
5127
5128
      /* If we have a .interp section, then create a PT_PHDR segment for
5129
   the program headers and a PT_INTERP segment for the .interp
5130
   section.  */
5131
1
      s = bfd_get_section_by_name (abfd, ".interp");
5132
1
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5133
0
  {
5134
0
    amt = sizeof (struct elf_segment_map);
5135
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5136
0
    if (m == NULL)
5137
0
      goto error_return;
5138
0
    m->next = NULL;
5139
0
    m->p_type = PT_PHDR;
5140
0
    m->p_flags = PF_R;
5141
0
    m->p_flags_valid = 1;
5142
0
    m->includes_phdrs = 1;
5143
0
    phdr_in_segment = true;
5144
0
    *pm = m;
5145
0
    pm = &m->next;
5146
5147
0
    amt = sizeof (struct elf_segment_map);
5148
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5149
0
    if (m == NULL)
5150
0
      goto error_return;
5151
0
    m->next = NULL;
5152
0
    m->p_type = PT_INTERP;
5153
0
    m->count = 1;
5154
0
    m->sections[0] = s;
5155
5156
0
    *pm = m;
5157
0
    pm = &m->next;
5158
0
  }
5159
5160
      /* Look through the sections.  We put sections in the same program
5161
   segment when the start of the second section can be placed within
5162
   a few bytes of the end of the first section.  */
5163
1
      last_hdr = NULL;
5164
1
      last_size = 0;
5165
1
      hdr_index = 0;
5166
1
      writable = false;
5167
1
      executable = false;
5168
1
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5169
1
      if (dynsec != NULL
5170
1
    && (dynsec->flags & SEC_LOAD) == 0)
5171
0
  dynsec = NULL;
5172
5173
1
      if ((abfd->flags & D_PAGED) == 0)
5174
0
  phdr_in_segment = false;
5175
5176
      /* Deal with -Ttext or something similar such that the first section
5177
   is not adjacent to the program headers.  This is an
5178
   approximation, since at this point we don't know exactly how many
5179
   program headers we will need.  */
5180
1
      if (phdr_in_segment && count > 0)
5181
1
  {
5182
1
    bfd_vma phdr_lma;  /* Bytes.  */
5183
1
    bool separate_phdr = false;
5184
5185
1
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5186
1
    if (info != NULL
5187
1
        && info->separate_code
5188
1
        && (sections[0]->flags & SEC_CODE) != 0)
5189
0
      {
5190
        /* If data sections should be separate from code and
5191
     thus not executable, and the first section is
5192
     executable then put the file and program headers in
5193
     their own PT_LOAD.  */
5194
0
        if (!info->one_rosegment)
5195
0
    separate_phdr = true;
5196
5197
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5198
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5199
0
    {
5200
      /* The file and program headers are currently on the
5201
         same page as the first section.  Put them on the
5202
         previous page if we can.  */
5203
0
      if (phdr_lma >= maxpagesize)
5204
0
        phdr_lma -= maxpagesize;
5205
0
      else
5206
0
        separate_phdr = false;
5207
0
    }
5208
0
      }
5209
1
    if ((sections[0]->lma & addr_mask) < phdr_lma
5210
1
        || (sections[0]->lma & addr_mask) < phdr_size)
5211
      /* If file and program headers would be placed at the end
5212
         of memory then it's probably better to omit them.  */
5213
0
      phdr_in_segment = false;
5214
1
    else if (phdr_lma < wrap_to)
5215
      /* If a section wraps around to where we'll be placing
5216
         file and program headers, then the headers will be
5217
         overwritten.  */
5218
0
      phdr_in_segment = false;
5219
1
    else if (separate_phdr)
5220
0
      {
5221
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5222
0
        if (m == NULL)
5223
0
    goto error_return;
5224
0
        m->p_paddr = phdr_lma * opb;
5225
0
        m->p_vaddr_offset
5226
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5227
0
        m->p_paddr_valid = 1;
5228
0
        *pm = m;
5229
0
        pm = &m->next;
5230
0
        phdr_in_segment = false;
5231
0
      }
5232
1
  }
5233
5234
2
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5235
1
  {
5236
1
    asection *hdr;
5237
1
    bool new_segment;
5238
5239
1
    hdr = *hdrpp;
5240
5241
    /* See if this section and the last one will fit in the same
5242
       segment.  */
5243
5244
1
    if (last_hdr == NULL)
5245
1
      {
5246
        /* If we don't have a segment yet, then we don't need a new
5247
     one (we build the last one after this loop).  */
5248
1
        new_segment = false;
5249
1
      }
5250
0
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5251
0
      {
5252
        /* If this section has a different relation between the
5253
     virtual address and the load address, then we need a new
5254
     segment.  */
5255
0
        new_segment = true;
5256
0
      }
5257
0
    else if (hdr->lma < last_hdr->lma + last_size
5258
0
       || last_hdr->lma + last_size < last_hdr->lma)
5259
0
      {
5260
        /* If this section has a load address that makes it overlap
5261
     the previous section, then we need a new segment.  */
5262
0
        new_segment = true;
5263
0
      }
5264
0
    else if ((abfd->flags & D_PAGED) != 0
5265
0
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5266
0
           == (hdr->lma & -maxpagesize)))
5267
0
      {
5268
        /* If we are demand paged then we can't map two disk
5269
     pages onto the same memory page.  */
5270
0
        new_segment = false;
5271
0
      }
5272
    /* In the next test we have to be careful when last_hdr->lma is close
5273
       to the end of the address space.  If the aligned address wraps
5274
       around to the start of the address space, then there are no more
5275
       pages left in memory and it is OK to assume that the current
5276
       section can be included in the current segment.  */
5277
0
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5278
0
        + maxpagesize > last_hdr->lma)
5279
0
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5280
0
           + maxpagesize <= hdr->lma))
5281
0
      {
5282
        /* If putting this section in this segment would force us to
5283
     skip a page in the segment, then we need a new segment.  */
5284
0
        new_segment = true;
5285
0
      }
5286
0
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5287
0
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5288
0
      {
5289
        /* We don't want to put a loaded section after a
5290
     nonloaded (ie. bss style) section in the same segment
5291
     as that will force the non-loaded section to be loaded.
5292
     Consider .tbss sections as loaded for this purpose.  */
5293
0
        new_segment = true;
5294
0
      }
5295
0
    else if ((abfd->flags & D_PAGED) == 0)
5296
0
      {
5297
        /* If the file is not demand paged, which means that we
5298
     don't require the sections to be correctly aligned in the
5299
     file, then there is no other reason for a new segment.  */
5300
0
        new_segment = false;
5301
0
      }
5302
0
    else if (info != NULL
5303
0
       && info->separate_code
5304
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5305
0
      {
5306
0
        new_segment = true;
5307
0
      }
5308
0
    else if (! writable
5309
0
       && (hdr->flags & SEC_READONLY) == 0)
5310
0
      {
5311
        /* We don't want to put a writable section in a read only
5312
     segment.  */
5313
0
        new_segment = true;
5314
0
      }
5315
0
    else
5316
0
      {
5317
        /* Otherwise, we can use the same segment.  */
5318
0
        new_segment = false;
5319
0
      }
5320
5321
    /* Allow interested parties a chance to override our decision.  */
5322
1
    if (last_hdr != NULL
5323
1
        && info != NULL
5324
1
        && info->callbacks->override_segment_assignment != NULL)
5325
0
      new_segment
5326
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5327
0
                    last_hdr,
5328
0
                    new_segment);
5329
5330
1
    if (! new_segment)
5331
1
      {
5332
1
        if ((hdr->flags & SEC_READONLY) == 0)
5333
1
    writable = true;
5334
1
        if ((hdr->flags & SEC_CODE) != 0)
5335
0
    executable = true;
5336
1
        last_hdr = hdr;
5337
        /* .tbss sections effectively have zero size.  */
5338
1
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5339
1
        continue;
5340
1
      }
5341
5342
    /* We need a new program segment.  We must create a new program
5343
       header holding all the sections from hdr_index until hdr.  */
5344
5345
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5346
0
    if (m == NULL)
5347
0
      goto error_return;
5348
5349
0
    *pm = m;
5350
0
    pm = &m->next;
5351
5352
0
    if ((hdr->flags & SEC_READONLY) == 0)
5353
0
      writable = true;
5354
0
    else
5355
0
      writable = false;
5356
5357
0
    if ((hdr->flags & SEC_CODE) == 0)
5358
0
      executable = false;
5359
0
    else
5360
0
      executable = true;
5361
5362
0
    last_hdr = hdr;
5363
    /* .tbss sections effectively have zero size.  */
5364
0
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5365
0
    hdr_index = i;
5366
0
    phdr_in_segment = false;
5367
0
  }
5368
5369
      /* Create a final PT_LOAD program segment, but not if it's just
5370
   for .tbss.  */
5371
1
      if (last_hdr != NULL
5372
1
    && (i - hdr_index != 1
5373
1
        || !IS_TBSS (last_hdr)))
5374
1
  {
5375
1
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5376
1
    if (m == NULL)
5377
0
      goto error_return;
5378
5379
1
    *pm = m;
5380
1
    pm = &m->next;
5381
1
  }
5382
5383
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5384
1
      if (dynsec != NULL)
5385
0
  {
5386
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5387
0
    if (m == NULL)
5388
0
      goto error_return;
5389
0
    *pm = m;
5390
0
    pm = &m->next;
5391
0
  }
5392
5393
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5394
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5395
   because if we link together nonloadable .note sections and
5396
   loadable .note sections, we will generate two .note sections
5397
   in the output file.  */
5398
2
      for (s = abfd->sections; s != NULL; s = s->next)
5399
1
  {
5400
1
    if ((s->flags & SEC_LOAD) != 0
5401
1
        && elf_section_type (s) == SHT_NOTE)
5402
0
      {
5403
0
        asection *s2;
5404
0
        unsigned int alignment_power = s->alignment_power;
5405
5406
0
        count = 1;
5407
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5408
0
    {
5409
0
      if (s2->next->alignment_power == alignment_power
5410
0
          && (s2->next->flags & SEC_LOAD) != 0
5411
0
          && elf_section_type (s2->next) == SHT_NOTE
5412
0
          && align_power (s2->lma + s2->size / opb,
5413
0
              alignment_power)
5414
0
          == s2->next->lma)
5415
0
        count++;
5416
0
      else
5417
0
        break;
5418
0
    }
5419
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5420
0
        amt += count * sizeof (asection *);
5421
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5422
0
        if (m == NULL)
5423
0
    goto error_return;
5424
0
        m->next = NULL;
5425
0
        m->p_type = PT_NOTE;
5426
0
        m->count = count;
5427
0
        while (count > 1)
5428
0
    {
5429
0
      m->sections[m->count - count--] = s;
5430
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5431
0
      s = s->next;
5432
0
    }
5433
0
        m->sections[m->count - 1] = s;
5434
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5435
0
        *pm = m;
5436
0
        pm = &m->next;
5437
0
      }
5438
1
    if (s->flags & SEC_THREAD_LOCAL)
5439
0
      {
5440
0
        if (! tls_count)
5441
0
    first_tls = s;
5442
0
        tls_count++;
5443
0
      }
5444
1
    if (first_mbind == NULL
5445
1
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5446
0
      first_mbind = s;
5447
1
  }
5448
5449
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5450
1
      if (tls_count > 0)
5451
0
  {
5452
0
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5453
0
    amt += tls_count * sizeof (asection *);
5454
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5455
0
    if (m == NULL)
5456
0
      goto error_return;
5457
0
    m->next = NULL;
5458
0
    m->p_type = PT_TLS;
5459
0
    m->count = tls_count;
5460
    /* Mandated PF_R.  */
5461
0
    m->p_flags = PF_R;
5462
0
    m->p_flags_valid = 1;
5463
0
    s = first_tls;
5464
0
    for (i = 0; i < tls_count; ++i)
5465
0
      {
5466
0
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5467
0
    {
5468
0
      _bfd_error_handler
5469
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5470
0
      s = first_tls;
5471
0
      i = 0;
5472
0
      while (i < tls_count)
5473
0
        {
5474
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5475
0
      {
5476
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5477
0
        i++;
5478
0
      }
5479
0
          else
5480
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5481
0
          s = s->next;
5482
0
        }
5483
0
      bfd_set_error (bfd_error_bad_value);
5484
0
      goto error_return;
5485
0
    }
5486
0
        m->sections[i] = s;
5487
0
        s = s->next;
5488
0
      }
5489
5490
0
    *pm = m;
5491
0
    pm = &m->next;
5492
0
  }
5493
5494
1
      if (first_mbind
5495
1
    && (abfd->flags & D_PAGED) != 0
5496
1
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5497
0
  for (s = first_mbind; s != NULL; s = s->next)
5498
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5499
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5500
0
      {
5501
        /* Mandated PF_R.  */
5502
0
        unsigned long p_flags = PF_R;
5503
0
        if ((s->flags & SEC_READONLY) == 0)
5504
0
    p_flags |= PF_W;
5505
0
        if ((s->flags & SEC_CODE) != 0)
5506
0
    p_flags |= PF_X;
5507
5508
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5509
0
        m = bfd_zalloc (abfd, amt);
5510
0
        if (m == NULL)
5511
0
    goto error_return;
5512
0
        m->next = NULL;
5513
0
        m->p_type = (PT_GNU_MBIND_LO
5514
0
         + elf_section_data (s)->this_hdr.sh_info);
5515
0
        m->count = 1;
5516
0
        m->p_flags_valid = 1;
5517
0
        m->sections[0] = s;
5518
0
        m->p_flags = p_flags;
5519
5520
0
        *pm = m;
5521
0
        pm = &m->next;
5522
0
      }
5523
5524
1
      s = bfd_get_section_by_name (abfd,
5525
1
           NOTE_GNU_PROPERTY_SECTION_NAME);
5526
1
      if (s != NULL && s->size != 0)
5527
0
  {
5528
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5529
0
    m = bfd_zalloc (abfd, amt);
5530
0
    if (m == NULL)
5531
0
      goto error_return;
5532
0
    m->next = NULL;
5533
0
    m->p_type = PT_GNU_PROPERTY;
5534
0
    m->count = 1;
5535
0
    m->p_flags_valid = 1;
5536
0
    m->sections[0] = s;
5537
0
    m->p_flags = PF_R;
5538
0
    *pm = m;
5539
0
    pm = &m->next;
5540
0
  }
5541
5542
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5543
   segment.  */
5544
1
      eh_frame_hdr = elf_eh_frame_hdr (info);
5545
1
      if (eh_frame_hdr != NULL
5546
1
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5547
0
  {
5548
0
    amt = sizeof (struct elf_segment_map);
5549
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5550
0
    if (m == NULL)
5551
0
      goto error_return;
5552
0
    m->next = NULL;
5553
0
    m->p_type = PT_GNU_EH_FRAME;
5554
0
    m->count = 1;
5555
0
    m->sections[0] = eh_frame_hdr->output_section;
5556
5557
0
    *pm = m;
5558
0
    pm = &m->next;
5559
0
  }
5560
5561
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5562
   segment.  */
5563
1
      sframe = elf_sframe (abfd);
5564
1
      if (sframe != NULL
5565
1
    && (sframe->output_section->flags & SEC_LOAD) != 0
5566
1
    && sframe->size != 0)
5567
0
  {
5568
0
    amt = sizeof (struct elf_segment_map);
5569
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5570
0
    if (m == NULL)
5571
0
      goto error_return;
5572
0
    m->next = NULL;
5573
0
    m->p_type = PT_GNU_SFRAME;
5574
0
    m->count = 1;
5575
0
    m->sections[0] = sframe->output_section;
5576
5577
0
    *pm = m;
5578
0
    pm = &m->next;
5579
0
  }
5580
5581
1
      if (elf_stack_flags (abfd))
5582
0
  {
5583
0
    amt = sizeof (struct elf_segment_map);
5584
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5585
0
    if (m == NULL)
5586
0
      goto error_return;
5587
0
    m->next = NULL;
5588
0
    m->p_type = PT_GNU_STACK;
5589
0
    m->p_flags = elf_stack_flags (abfd);
5590
0
    m->p_align = bed->stack_align;
5591
0
    m->p_flags_valid = 1;
5592
0
    m->p_align_valid = m->p_align != 0;
5593
0
    if (info->stacksize > 0)
5594
0
      {
5595
0
        m->p_size = info->stacksize;
5596
0
        m->p_size_valid = 1;
5597
0
      }
5598
5599
0
    *pm = m;
5600
0
    pm = &m->next;
5601
0
  }
5602
5603
1
      if (info != NULL && info->relro)
5604
0
  {
5605
0
    for (m = mfirst; m != NULL; m = m->next)
5606
0
      {
5607
0
        if (m->p_type == PT_LOAD
5608
0
      && m->count != 0
5609
0
      && m->sections[0]->vma >= info->relro_start
5610
0
      && m->sections[0]->vma < info->relro_end)
5611
0
    {
5612
0
      i = m->count;
5613
0
      while (--i != (unsigned) -1)
5614
0
        {
5615
0
          if (m->sections[i]->size > 0
5616
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5617
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5618
0
      break;
5619
0
        }
5620
5621
0
      if (i != (unsigned) -1)
5622
0
        break;
5623
0
    }
5624
0
      }
5625
5626
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5627
0
    if (m != NULL)
5628
0
      {
5629
0
        amt = sizeof (struct elf_segment_map);
5630
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5631
0
        if (m == NULL)
5632
0
    goto error_return;
5633
0
        m->next = NULL;
5634
0
        m->p_type = PT_GNU_RELRO;
5635
0
        *pm = m;
5636
0
        pm = &m->next;
5637
0
      }
5638
0
  }
5639
5640
1
      free (sections);
5641
1
      elf_seg_map (abfd) = mfirst;
5642
1
    }
5643
5644
28
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5645
0
    return false;
5646
5647
130
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5648
102
    ++count;
5649
28
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5650
5651
28
  return true;
5652
5653
0
 error_return:
5654
0
  free (sections);
5655
0
  return false;
5656
28
}
5657
5658
/* Sort sections by address.  */
5659
5660
static int
5661
elf_sort_sections (const void *arg1, const void *arg2)
5662
606
{
5663
606
  const asection *sec1 = *(const asection **) arg1;
5664
606
  const asection *sec2 = *(const asection **) arg2;
5665
606
  bfd_size_type size1, size2;
5666
5667
  /* Sort by LMA first, since this is the address used to
5668
     place the section into a segment.  */
5669
606
  if (sec1->lma < sec2->lma)
5670
606
    return -1;
5671
0
  else if (sec1->lma > sec2->lma)
5672
0
    return 1;
5673
5674
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5675
     the same, and this will do nothing.  */
5676
0
  if (sec1->vma < sec2->vma)
5677
0
    return -1;
5678
0
  else if (sec1->vma > sec2->vma)
5679
0
    return 1;
5680
5681
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5682
5683
0
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5684
0
      && (x)->size != 0)
5685
5686
0
  if (TOEND (sec1))
5687
0
    {
5688
0
      if (!TOEND (sec2))
5689
0
  return 1;
5690
0
    }
5691
0
  else if (TOEND (sec2))
5692
0
    return -1;
5693
5694
0
#undef TOEND
5695
5696
  /* Sort by size, to put zero sized sections
5697
     before others at the same address.  */
5698
5699
0
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5700
0
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5701
5702
0
  if (size1 < size2)
5703
0
    return -1;
5704
0
  if (size1 > size2)
5705
0
    return 1;
5706
5707
0
  return sec1->target_index - sec2->target_index;
5708
0
}
5709
5710
/* This qsort comparison functions sorts PT_LOAD segments first and
5711
   by p_paddr, for assign_file_positions_for_load_sections.  */
5712
5713
static int
5714
elf_sort_segments (const void *arg1, const void *arg2)
5715
154
{
5716
154
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5717
154
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5718
5719
154
  if (m1->p_type != m2->p_type)
5720
138
    {
5721
138
      if (m1->p_type == PT_NULL)
5722
0
  return 1;
5723
138
      if (m2->p_type == PT_NULL)
5724
0
  return -1;
5725
138
      return m1->p_type < m2->p_type ? -1 : 1;
5726
138
    }
5727
16
  if (m1->includes_filehdr != m2->includes_filehdr)
5728
15
    return m1->includes_filehdr ? -1 : 1;
5729
1
  if (m1->no_sort_lma != m2->no_sort_lma)
5730
0
    return m1->no_sort_lma ? -1 : 1;
5731
1
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5732
1
    {
5733
1
      bfd_vma lma1, lma2;  /* Octets.  */
5734
1
      lma1 = 0;
5735
1
      if (m1->p_paddr_valid)
5736
1
  lma1 = m1->p_paddr;
5737
0
      else if (m1->count != 0)
5738
0
  {
5739
0
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5740
0
              m1->sections[0]);
5741
0
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5742
0
  }
5743
1
      lma2 = 0;
5744
1
      if (m2->p_paddr_valid)
5745
1
  lma2 = m2->p_paddr;
5746
0
      else if (m2->count != 0)
5747
0
  {
5748
0
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5749
0
              m2->sections[0]);
5750
0
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5751
0
  }
5752
1
      if (lma1 != lma2)
5753
1
  return lma1 < lma2 ? -1 : 1;
5754
1
    }
5755
0
  if (m1->idx != m2->idx)
5756
0
    return m1->idx < m2->idx ? -1 : 1;
5757
0
  return 0;
5758
0
}
5759
5760
/* Ian Lance Taylor writes:
5761
5762
   We shouldn't be using % with a negative signed number.  That's just
5763
   not good.  We have to make sure either that the number is not
5764
   negative, or that the number has an unsigned type.  When the types
5765
   are all the same size they wind up as unsigned.  When file_ptr is a
5766
   larger signed type, the arithmetic winds up as signed long long,
5767
   which is wrong.
5768
5769
   What we're trying to say here is something like ``increase OFF by
5770
   the least amount that will cause it to be equal to the VMA modulo
5771
   the page size.''  */
5772
/* In other words, something like:
5773
5774
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5775
   off_offset = off % bed->maxpagesize;
5776
   if (vma_offset < off_offset)
5777
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5778
   else
5779
     adjustment = vma_offset - off_offset;
5780
5781
   which can be collapsed into the expression below.  */
5782
5783
static file_ptr
5784
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5785
32
{
5786
  /* PR binutils/16199: Handle an alignment of zero.  */
5787
32
  if (maxpagesize == 0)
5788
0
    maxpagesize = 1;
5789
32
  return ((vma - off) % maxpagesize);
5790
32
}
5791
5792
static void
5793
print_segment_map (const struct elf_segment_map *m)
5794
0
{
5795
0
  unsigned int j;
5796
0
  const char *pt = get_segment_type (m->p_type);
5797
0
  char buf[32];
5798
5799
0
  if (pt == NULL)
5800
0
    {
5801
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5802
0
  sprintf (buf, "LOPROC+%7.7x",
5803
0
     (unsigned int) (m->p_type - PT_LOPROC));
5804
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5805
0
  sprintf (buf, "LOOS+%7.7x",
5806
0
     (unsigned int) (m->p_type - PT_LOOS));
5807
0
      else
5808
0
  snprintf (buf, sizeof (buf), "%8.8x",
5809
0
      (unsigned int) m->p_type);
5810
0
      pt = buf;
5811
0
    }
5812
0
  fflush (stdout);
5813
0
  fprintf (stderr, "%s:", pt);
5814
0
  for (j = 0; j < m->count; j++)
5815
0
    fprintf (stderr, " %s", m->sections [j]->name);
5816
0
  putc ('\n',stderr);
5817
0
  fflush (stderr);
5818
0
}
5819
5820
/* Assign file positions to the sections based on the mapping from
5821
   sections to segments.  This function also sets up some fields in
5822
   the file header.  */
5823
5824
static bool
5825
assign_file_positions_for_load_sections (bfd *abfd,
5826
           struct bfd_link_info *link_info)
5827
28
{
5828
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5829
28
  struct elf_segment_map *m;
5830
28
  struct elf_segment_map *phdr_load_seg;
5831
28
  Elf_Internal_Phdr *phdrs;
5832
28
  Elf_Internal_Phdr *p;
5833
28
  file_ptr off;  /* Octets.  */
5834
28
  bfd_size_type maxpagesize;
5835
28
  unsigned int alloc, actual;
5836
28
  unsigned int i, j;
5837
28
  struct elf_segment_map **sorted_seg_map;
5838
28
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5839
5840
28
  if (link_info == NULL
5841
28
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5842
0
    return false;
5843
5844
28
  alloc = 0;
5845
130
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5846
102
    m->idx = alloc++;
5847
5848
28
  if (alloc)
5849
16
    {
5850
16
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5851
16
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5852
16
    }
5853
12
  else
5854
12
    {
5855
      /* PR binutils/12467.  */
5856
12
      elf_elfheader (abfd)->e_phoff = 0;
5857
12
      elf_elfheader (abfd)->e_phentsize = 0;
5858
12
    }
5859
5860
28
  elf_elfheader (abfd)->e_phnum = alloc;
5861
5862
28
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5863
0
    {
5864
0
      actual = alloc;
5865
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5866
0
    }
5867
28
  else
5868
28
    {
5869
28
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5870
28
      BFD_ASSERT (elf_program_header_size (abfd)
5871
28
      == actual * bed->s->sizeof_phdr);
5872
28
      BFD_ASSERT (actual >= alloc);
5873
28
    }
5874
5875
28
  if (alloc == 0)
5876
12
    {
5877
12
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5878
12
      return true;
5879
12
    }
5880
5881
  /* We're writing the size in elf_program_header_size (abfd),
5882
     see assign_file_positions_except_relocs, so make sure we have
5883
     that amount allocated, with trailing space cleared.
5884
     The variable alloc contains the computed need, while
5885
     elf_program_header_size (abfd) contains the size used for the
5886
     layout.
5887
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5888
     where the layout is forced to according to a larger size in the
5889
     last iterations for the testcase ld-elf/header.  */
5890
16
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5891
16
           + alloc * sizeof (*sorted_seg_map)));
5892
16
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5893
16
  elf_tdata (abfd)->phdr = phdrs;
5894
16
  if (phdrs == NULL)
5895
0
    return false;
5896
5897
118
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5898
102
    {
5899
102
      sorted_seg_map[j] = m;
5900
      /* If elf_segment_map is not from map_sections_to_segments, the
5901
   sections may not be correctly ordered.  NOTE: sorting should
5902
   not be done to the PT_NOTE section of a corefile, which may
5903
   contain several pseudo-sections artificially created by bfd.
5904
   Sorting these pseudo-sections breaks things badly.  */
5905
102
      if (m->count > 1
5906
102
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5907
40
         && m->p_type == PT_NOTE))
5908
40
  {
5909
418
    for (i = 0; i < m->count; i++)
5910
378
      m->sections[i]->target_index = i;
5911
40
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5912
40
     elf_sort_sections);
5913
40
  }
5914
102
    }
5915
16
  if (alloc > 1)
5916
15
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5917
15
     elf_sort_segments);
5918
5919
16
  maxpagesize = 1;
5920
16
  if ((abfd->flags & D_PAGED) != 0)
5921
16
    {
5922
16
      if (link_info != NULL)
5923
0
  maxpagesize = link_info->maxpagesize;
5924
16
      else
5925
16
  maxpagesize = bed->maxpagesize;
5926
16
    }
5927
5928
  /* Sections must map to file offsets past the ELF file header.  */
5929
16
  off = bed->s->sizeof_ehdr;
5930
  /* And if one of the PT_LOAD headers doesn't include the program
5931
     headers then we'll be mapping program headers in the usual
5932
     position after the ELF file header.  */
5933
16
  phdr_load_seg = NULL;
5934
16
  for (j = 0; j < alloc; j++)
5935
16
    {
5936
16
      m = sorted_seg_map[j];
5937
16
      if (m->p_type != PT_LOAD)
5938
0
  break;
5939
16
      if (m->includes_phdrs)
5940
16
  {
5941
16
    phdr_load_seg = m;
5942
16
    break;
5943
16
  }
5944
16
    }
5945
16
  if (phdr_load_seg == NULL)
5946
0
    off += actual * bed->s->sizeof_phdr;
5947
5948
118
  for (j = 0; j < alloc; j++)
5949
102
    {
5950
102
      asection **secpp;
5951
102
      bfd_vma off_adjust;  /* Octets.  */
5952
102
      bool no_contents;
5953
102
      bfd_size_type align_pagesize;
5954
5955
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5956
   number of sections with contents contributing to both p_filesz
5957
   and p_memsz, followed by a number of sections with no contents
5958
   that just contribute to p_memsz.  In this loop, OFF tracks next
5959
   available file offset for PT_LOAD and PT_NOTE segments.  */
5960
102
      m = sorted_seg_map[j];
5961
102
      p = phdrs + m->idx;
5962
102
      p->p_type = m->p_type;
5963
102
      p->p_flags = m->p_flags;
5964
5965
102
      if (m->count == 0)
5966
22
  p->p_vaddr = m->p_vaddr_offset * opb;
5967
80
      else
5968
80
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5969
5970
102
      if (m->p_paddr_valid)
5971
96
  p->p_paddr = m->p_paddr;
5972
6
      else if (m->count == 0)
5973
1
  p->p_paddr = 0;
5974
5
      else
5975
5
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5976
5977
102
      align_pagesize = 0;
5978
102
      if (p->p_type == PT_LOAD
5979
102
    && (abfd->flags & D_PAGED) != 0)
5980
32
  {
5981
    /* p_align in demand paged PT_LOAD segments effectively stores
5982
       the maximum page size.  When copying an executable with
5983
       objcopy, we set m->p_align from the input file.  Use this
5984
       value for maxpagesize rather than bed->maxpagesize, which
5985
       may be different.  Note that we use maxpagesize for PT_TLS
5986
       segment alignment later in this function, so we are relying
5987
       on at least one PT_LOAD segment appearing before a PT_TLS
5988
       segment.  */
5989
32
    if (m->p_align_valid)
5990
31
      maxpagesize = m->p_align;
5991
1
    else if (bed->p_align != 0
5992
1
       && (link_info == NULL
5993
1
           || !link_info->maxpagesize_is_set))
5994
      /* We will lay out this binary using maxpagesize but set
5995
         p->p_align later to the possibly smaller bed->p_align.
5996
         The run-time loader will then be able to load this
5997
         binary when the system page size is maxpagesize, but if
5998
         the system page size is smaller can use p->p_align.
5999
         In either case p->p_align will be increased if
6000
         necessary to match section alignment.  */
6001
1
      align_pagesize = bed->p_align;
6002
6003
32
    p->p_align = maxpagesize;
6004
32
  }
6005
70
      else if (m->p_align_valid)
6006
67
  p->p_align = m->p_align;
6007
3
      else if (m->count == 0)
6008
1
  p->p_align = 1 << bed->s->log_file_align;
6009
6010
102
      if (m == phdr_load_seg)
6011
16
  off += actual * bed->s->sizeof_phdr;
6012
6013
102
      no_contents = false;
6014
102
      off_adjust = 0;
6015
102
      if (p->p_type == PT_LOAD
6016
102
    && m->count > 0)
6017
32
  {
6018
32
    bfd_size_type align;  /* Bytes.  */
6019
32
    unsigned int align_power = 0;
6020
6021
32
    if (m->p_align_valid)
6022
31
      align = p->p_align;
6023
1
    else
6024
1
      {
6025
2
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6026
1
    {
6027
1
      unsigned int secalign;
6028
6029
1
      secalign = bfd_section_alignment (*secpp);
6030
1
      if (secalign > align_power)
6031
1
        align_power = secalign;
6032
1
    }
6033
1
        align = (bfd_size_type) 1 << align_power;
6034
        /* If a section requires alignment higher than the
6035
     minimum p_align value, don't reduce a maxpagesize
6036
     p->p_align set earlier in this function.  */
6037
1
        if (align > bed->p_align)
6038
0
    align_pagesize = 0;
6039
1
        if (align < maxpagesize)
6040
1
    align = maxpagesize;
6041
0
        else
6042
0
    {
6043
      /* If a section requires alignment higher than the
6044
         maximum page size, set p_align to the section
6045
         alignment.  */
6046
0
      if ((abfd->flags & D_PAGED) != 0)
6047
0
        p->p_align = align;
6048
0
    }
6049
1
      }
6050
6051
374
    for (i = 0; i < m->count; i++)
6052
342
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6053
        /* If we aren't making room for this section, then
6054
     it must be SHT_NOBITS regardless of what we've
6055
     set via struct bfd_elf_special_section.  */
6056
17
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6057
6058
    /* Find out whether this segment contains any loadable
6059
       sections.  */
6060
32
    no_contents = true;
6061
32
    for (i = 0; i < m->count; i++)
6062
32
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6063
32
        {
6064
32
    no_contents = false;
6065
32
    break;
6066
32
        }
6067
6068
32
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6069
6070
    /* Broken hardware and/or kernel require that files do not
6071
       map the same page with different permissions on some hppa
6072
       processors.  */
6073
32
    if (j != 0
6074
32
        && (abfd->flags & D_PAGED) != 0
6075
32
        && bed->no_page_alias
6076
32
        && (off & (maxpagesize - 1)) != 0
6077
32
        && ((off & -maxpagesize)
6078
0
      == ((off + off_adjust) & -maxpagesize)))
6079
0
      off_adjust += maxpagesize;
6080
32
    off += off_adjust;
6081
32
    if (no_contents)
6082
0
      {
6083
        /* We shouldn't need to align the segment on disk since
6084
     the segment doesn't need file space, but the gABI
6085
     arguably requires the alignment and glibc ld.so
6086
     checks it.  So to comply with the alignment
6087
     requirement but not waste file space, we adjust
6088
     p_offset for just this segment.  (OFF_ADJUST is
6089
     subtracted from OFF later.)  This may put p_offset
6090
     past the end of file, but that shouldn't matter.  */
6091
0
      }
6092
32
    else
6093
32
      off_adjust = 0;
6094
32
  }
6095
      /* Make sure the .dynamic section is the first section in the
6096
   PT_DYNAMIC segment.  */
6097
70
      else if (p->p_type == PT_DYNAMIC
6098
70
         && m->count > 1
6099
70
         && strcmp (m->sections[0]->name, ".dynamic") != 0)
6100
0
  {
6101
0
    _bfd_error_handler
6102
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6103
0
         " is not the .dynamic section"),
6104
0
       abfd);
6105
0
    bfd_set_error (bfd_error_bad_value);
6106
0
    return false;
6107
0
  }
6108
      /* Set the note section type to SHT_NOTE.  */
6109
70
      else if (p->p_type == PT_NOTE)
6110
17
  for (i = 0; i < m->count; i++)
6111
10
    elf_section_type (m->sections[i]) = SHT_NOTE;
6112
6113
102
      if (m->includes_filehdr)
6114
16
  {
6115
16
    if (!m->p_flags_valid)
6116
1
      p->p_flags |= PF_R;
6117
16
    p->p_filesz = bed->s->sizeof_ehdr;
6118
16
    p->p_memsz = bed->s->sizeof_ehdr;
6119
16
    if (p->p_type == PT_LOAD)
6120
16
      {
6121
16
        if (m->count > 0)
6122
16
    {
6123
16
      if (p->p_vaddr < (bfd_vma) off
6124
16
          || (!m->p_paddr_valid
6125
16
        && p->p_paddr < (bfd_vma) off))
6126
0
        {
6127
0
          _bfd_error_handler
6128
0
      (_("%pB: not enough room for program headers,"
6129
0
         " try linking with -N"),
6130
0
       abfd);
6131
0
          bfd_set_error (bfd_error_bad_value);
6132
0
          return false;
6133
0
        }
6134
16
      p->p_vaddr -= off;
6135
16
      if (!m->p_paddr_valid)
6136
2
        p->p_paddr -= off;
6137
16
    }
6138
16
      }
6139
0
    else if (sorted_seg_map[0]->includes_filehdr)
6140
0
      {
6141
0
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6142
0
        p->p_vaddr = filehdr->p_vaddr;
6143
0
        if (!m->p_paddr_valid)
6144
0
    p->p_paddr = filehdr->p_paddr;
6145
0
      }
6146
16
  }
6147
6148
102
      if (m->includes_phdrs)
6149
30
  {
6150
30
    if (!m->p_flags_valid)
6151
1
      p->p_flags |= PF_R;
6152
30
    p->p_filesz += actual * bed->s->sizeof_phdr;
6153
30
    p->p_memsz += actual * bed->s->sizeof_phdr;
6154
30
    if (!m->includes_filehdr)
6155
14
      {
6156
14
        if (p->p_type == PT_LOAD)
6157
0
    {
6158
0
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6159
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6160
0
      if (m->count > 0)
6161
0
        {
6162
0
          p->p_vaddr -= off - p->p_offset;
6163
0
          if (!m->p_paddr_valid)
6164
0
      p->p_paddr -= off - p->p_offset;
6165
0
        }
6166
0
    }
6167
14
        else if (phdr_load_seg != NULL)
6168
14
    {
6169
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6170
         sorted segments so that phdr_load_seg will
6171
         already be set by the code immediately above.  */
6172
14
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6173
14
      bfd_vma phdr_off = 0;  /* Octets.  */
6174
14
      if (phdr_load_seg->includes_filehdr)
6175
14
        phdr_off = bed->s->sizeof_ehdr;
6176
14
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6177
14
      if (!m->p_paddr_valid)
6178
1
        p->p_paddr = phdr->p_paddr + phdr_off;
6179
14
      p->p_offset = phdr->p_offset + phdr_off;
6180
14
    }
6181
0
        else
6182
0
    p->p_offset = bed->s->sizeof_ehdr;
6183
14
      }
6184
30
  }
6185
6186
102
      if (p->p_type == PT_LOAD
6187
102
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6188
32
  {
6189
32
    if (!m->includes_filehdr && !m->includes_phdrs)
6190
16
      {
6191
16
        p->p_offset = off;
6192
16
        if (no_contents)
6193
0
    {
6194
      /* Put meaningless p_offset for PT_LOAD segments
6195
         without file contents somewhere within the first
6196
         page, in an attempt to not point past EOF.  */
6197
0
      bfd_size_type align = maxpagesize;
6198
0
      if (align < p->p_align)
6199
0
        align = p->p_align;
6200
0
      if (align < 1)
6201
0
        align = 1;
6202
      /* Avoid p_offset of zero, which might be wrongly
6203
         interpreted as the segment being the first one,
6204
         containing the file header.  PR32763.  */
6205
0
      p->p_offset = (off + align - 1) % align + 1;
6206
0
    }
6207
16
      }
6208
16
    else
6209
16
      {
6210
16
        file_ptr adjust;  /* Octets.  */
6211
6212
16
        adjust = off - (p->p_offset + p->p_filesz);
6213
16
        if (!no_contents)
6214
16
    p->p_filesz += adjust;
6215
16
        p->p_memsz += adjust;
6216
16
      }
6217
32
  }
6218
6219
102
      if (align_pagesize)
6220
1
  p->p_align = align_pagesize;
6221
6222
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6223
   maps.  Set filepos for sections in PT_LOAD segments, and in
6224
   core files, for sections in PT_NOTE segments.
6225
   assign_file_positions_for_non_load_sections will set filepos
6226
   for other sections and update p_filesz for other segments.  */
6227
520
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6228
418
  {
6229
418
    asection *sec;
6230
418
    bfd_size_type align;
6231
418
    Elf_Internal_Shdr *this_hdr;
6232
6233
418
    sec = *secpp;
6234
418
    this_hdr = &elf_section_data (sec)->this_hdr;
6235
418
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6236
6237
418
    if ((p->p_type == PT_LOAD
6238
418
         || p->p_type == PT_TLS)
6239
418
        && (this_hdr->sh_type != SHT_NOBITS
6240
342
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6241
17
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6242
17
        || p->p_type == PT_TLS))))
6243
342
      {
6244
342
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6245
342
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6246
342
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6247
342
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6248
6249
342
        if (adjust != 0
6250
342
      && (s_start < p_end
6251
64
          || p_end < p_start))
6252
0
    {
6253
0
      _bfd_error_handler
6254
        /* xgettext:c-format */
6255
0
        (_("%pB: section %pA lma %#" PRIx64
6256
0
           " adjusted to %#" PRIx64),
6257
0
         abfd, sec, (uint64_t) s_start / opb,
6258
0
         (uint64_t) p_end / opb);
6259
0
      adjust = 0;
6260
0
      sec->lma = p_end / opb;
6261
0
    }
6262
342
        p->p_memsz += adjust;
6263
6264
342
        if (p->p_type == PT_LOAD)
6265
342
    {
6266
342
      if (this_hdr->sh_type != SHT_NOBITS)
6267
325
        {
6268
325
          off_adjust = 0;
6269
325
          if (p->p_filesz + adjust < p->p_memsz)
6270
0
      {
6271
        /* We have a PROGBITS section following NOBITS ones.
6272
           Allocate file space for the NOBITS section(s).
6273
           We don't need to write out the zeros, posix
6274
           fseek past the end of data already written
6275
           followed by a write at that location is
6276
           guaranteed to result in zeros being read
6277
           from the gap.  */
6278
0
        adjust = p->p_memsz - p->p_filesz;
6279
0
      }
6280
325
        }
6281
      /* We only adjust sh_offset in SHT_NOBITS sections
6282
         as would seem proper for their address when the
6283
         section is first in the segment.  sh_offset
6284
         doesn't really have any significance for
6285
         SHT_NOBITS anyway, apart from a notional position
6286
         relative to other sections.  Historically we
6287
         didn't bother with adjusting sh_offset and some
6288
         programs depend on it not being adjusted.  See
6289
         pr12921 and pr25662.  */
6290
342
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6291
325
        {
6292
325
          off += adjust;
6293
325
          if (this_hdr->sh_type == SHT_NOBITS)
6294
0
      off_adjust += adjust;
6295
325
        }
6296
342
    }
6297
342
        if (this_hdr->sh_type != SHT_NOBITS)
6298
325
    p->p_filesz += adjust;
6299
342
      }
6300
6301
418
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6302
0
      {
6303
        /* The section at i == 0 is the one that actually contains
6304
     everything.  */
6305
0
        if (i == 0)
6306
0
    {
6307
0
      this_hdr->sh_offset = sec->filepos = off;
6308
0
      off += this_hdr->sh_size;
6309
0
      p->p_filesz = this_hdr->sh_size;
6310
0
      p->p_memsz = 0;
6311
0
      p->p_align = 1;
6312
0
    }
6313
0
        else
6314
0
    {
6315
      /* The rest are fake sections that shouldn't be written.  */
6316
0
      sec->filepos = 0;
6317
0
      sec->size = 0;
6318
0
      sec->flags = 0;
6319
0
      continue;
6320
0
    }
6321
0
      }
6322
418
    else
6323
418
      {
6324
418
        if (this_hdr->sh_type == SHT_NOBITS
6325
418
      && (this_hdr->sh_flags & SHF_TLS) != 0
6326
418
      && this_hdr->sh_offset == 0)
6327
0
    {
6328
      /* Set sh_offset for .tbss sections to their nominal
6329
         offset after aligning.  They are not loaded from
6330
         disk so the value doesn't really matter, except
6331
         when the .tbss section is the first one in a
6332
         PT_TLS segment.  In that case it sets the
6333
         p_offset for the PT_TLS segment, which according
6334
         to the ELF gABI ought to satisfy
6335
         p_offset % p_align == p_vaddr % p_align.  */
6336
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6337
0
                off, align);
6338
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
6339
0
    }
6340
418
        else if (p->p_type == PT_LOAD)
6341
342
    {
6342
342
      this_hdr->sh_offset = sec->filepos = off;
6343
342
      if (this_hdr->sh_type != SHT_NOBITS)
6344
325
        off += this_hdr->sh_size;
6345
342
    }
6346
6347
418
        if (this_hdr->sh_type != SHT_NOBITS)
6348
401
    {
6349
401
      p->p_filesz += this_hdr->sh_size;
6350
      /* A load section without SHF_ALLOC is something like
6351
         a note section in a PT_NOTE segment.  These take
6352
         file space but are not loaded into memory.  */
6353
401
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6354
401
        p->p_memsz += this_hdr->sh_size;
6355
401
    }
6356
17
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6357
17
    {
6358
17
      if (p->p_type == PT_TLS)
6359
0
        p->p_memsz += this_hdr->sh_size;
6360
6361
      /* .tbss is special.  It doesn't contribute to p_memsz of
6362
         normal segments.  */
6363
17
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6364
17
        p->p_memsz += this_hdr->sh_size;
6365
17
    }
6366
6367
418
        if (align > p->p_align
6368
418
      && !m->p_align_valid
6369
418
      && (p->p_type != PT_LOAD
6370
2
          || (abfd->flags & D_PAGED) == 0))
6371
2
    p->p_align = align;
6372
418
      }
6373
6374
418
    if (!m->p_flags_valid)
6375
1
      {
6376
1
        p->p_flags |= PF_R;
6377
1
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6378
0
    p->p_flags |= PF_X;
6379
1
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6380
1
    p->p_flags |= PF_W;
6381
1
      }
6382
418
  }
6383
6384
102
      off -= off_adjust;
6385
6386
      /* PR ld/20815 - Check that the program header segment, if
6387
   present, will be loaded into memory.  */
6388
102
      if (p->p_type == PT_PHDR
6389
102
    && phdr_load_seg == NULL
6390
102
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6391
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6392
0
  {
6393
    /* The fix for this error is usually to edit the linker script being
6394
       used and set up the program headers manually.  Either that or
6395
       leave room for the headers at the start of the SECTIONS.  */
6396
0
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6397
0
        " by LOAD segment"),
6398
0
            abfd);
6399
0
    if (link_info == NULL)
6400
0
      return false;
6401
    /* Arrange for the linker to exit with an error, deleting
6402
       the output file unless --noinhibit-exec is given.  */
6403
0
    link_info->callbacks->info ("%X");
6404
0
  }
6405
6406
      /* Check that all sections are in a PT_LOAD segment.
6407
   Don't check funky gdb generated core files.  */
6408
102
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6409
32
  {
6410
32
    bool check_vma = true;
6411
6412
342
    for (i = 1; i < m->count; i++)
6413
310
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6414
310
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6415
0
               ->this_hdr), p) != 0
6416
310
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6417
0
               ->this_hdr), p) != 0)
6418
0
        {
6419
    /* Looks like we have overlays packed into the segment.  */
6420
0
    check_vma = false;
6421
0
    break;
6422
0
        }
6423
6424
374
    for (i = 0; i < m->count; i++)
6425
342
      {
6426
342
        Elf_Internal_Shdr *this_hdr;
6427
342
        asection *sec;
6428
6429
342
        sec = m->sections[i];
6430
342
        this_hdr = &(elf_section_data(sec)->this_hdr);
6431
342
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6432
342
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6433
0
    {
6434
0
      _bfd_error_handler
6435
        /* xgettext:c-format */
6436
0
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6437
0
         abfd, sec, m->idx);
6438
0
      print_segment_map (m);
6439
0
    }
6440
342
      }
6441
32
  }
6442
102
    }
6443
6444
16
  elf_next_file_pos (abfd) = off;
6445
6446
16
  if (link_info != NULL
6447
16
      && phdr_load_seg != NULL
6448
16
      && phdr_load_seg->includes_filehdr)
6449
0
    {
6450
      /* There is a segment that contains both the file headers and the
6451
   program headers, so provide a symbol __ehdr_start pointing there.
6452
   A program can use this to examine itself robustly.  */
6453
6454
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6455
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6456
6457
      /* If the symbol was referenced and not defined, define it.  */
6458
0
      if (hash != NULL
6459
0
    && (hash->root.type == bfd_link_hash_new
6460
0
        || hash->root.type == bfd_link_hash_undefined
6461
0
        || hash->root.type == bfd_link_hash_undefweak
6462
0
        || hash->root.type == bfd_link_hash_common))
6463
0
  {
6464
0
    asection *s = NULL;
6465
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6466
6467
0
    if (phdr_load_seg->count != 0)
6468
      /* The segment contains sections, so use the first one.  */
6469
0
      s = phdr_load_seg->sections[0];
6470
0
    else
6471
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6472
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6473
0
        if (m->p_type == PT_LOAD && m->count != 0)
6474
0
    {
6475
0
      s = m->sections[0];
6476
0
      break;
6477
0
    }
6478
6479
0
    if (s != NULL)
6480
0
      {
6481
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6482
0
        hash->root.u.def.section = s;
6483
0
      }
6484
0
    else
6485
0
      {
6486
0
        hash->root.u.def.value = filehdr_vaddr;
6487
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6488
0
      }
6489
6490
0
    hash->root.type = bfd_link_hash_defined;
6491
0
    hash->def_regular = 1;
6492
0
    hash->non_elf = 0;
6493
0
  }
6494
0
    }
6495
6496
16
  return true;
6497
16
}
6498
6499
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6500
   is no defined method for detecting such files, so we have to
6501
   use heuristics instead.  */
6502
6503
bool
6504
is_debuginfo_file (bfd *abfd)
6505
0
{
6506
0
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6507
0
    return false;
6508
6509
0
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6510
0
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6511
0
  Elf_Internal_Shdr **headerp;
6512
6513
0
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6514
0
    {
6515
0
      Elf_Internal_Shdr *header = * headerp;
6516
6517
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6518
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6519
0
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6520
0
    && header->sh_type != SHT_NOBITS
6521
0
    && header->sh_type != SHT_NOTE)
6522
0
  return false;
6523
0
    }
6524
6525
0
  return true;
6526
0
}
6527
6528
/* Assign file positions for other sections, except for compressed debug
6529
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6530
6531
static bool
6532
assign_file_positions_for_non_load_sections (bfd *abfd,
6533
               struct bfd_link_info *link_info)
6534
28
{
6535
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6536
28
  Elf_Internal_Shdr **i_shdrpp;
6537
28
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6538
28
  Elf_Internal_Phdr *phdrs;
6539
28
  Elf_Internal_Phdr *p;
6540
28
  struct elf_segment_map *m;
6541
28
  file_ptr off;
6542
28
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6543
28
  bfd_vma maxpagesize;
6544
6545
28
  if (link_info != NULL)
6546
0
    maxpagesize = link_info->maxpagesize;
6547
28
  else
6548
28
    maxpagesize = bed->maxpagesize;
6549
28
  i_shdrpp = elf_elfsections (abfd);
6550
28
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6551
28
  off = elf_next_file_pos (abfd);
6552
450
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6553
422
    {
6554
422
      Elf_Internal_Shdr *hdr;
6555
422
      bfd_vma align;
6556
6557
422
      hdr = *hdrpp;
6558
422
      if (hdr->bfd_section != NULL
6559
422
    && (hdr->bfd_section->filepos != 0
6560
378
        || (hdr->sh_type == SHT_NOBITS
6561
36
      && hdr->contents == NULL)))
6562
342
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6563
80
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6564
0
  {
6565
0
    if (hdr->sh_size != 0
6566
        /* PR 24717 - debuginfo files are known to be not strictly
6567
     compliant with the ELF standard.  In particular they often
6568
     have .note.gnu.property sections that are outside of any
6569
     loadable segment.  This is not a problem for such files,
6570
     so do not warn about them.  */
6571
0
        && ! is_debuginfo_file (abfd))
6572
0
      _bfd_error_handler
6573
        /* xgettext:c-format */
6574
0
        (_("%pB: warning: allocated section `%s' not in segment"),
6575
0
         abfd,
6576
0
         (hdr->bfd_section == NULL
6577
0
    ? "*unknown*"
6578
0
    : hdr->bfd_section->name));
6579
    /* We don't need to page align empty sections.  */
6580
0
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6581
0
      align = maxpagesize;
6582
0
    else
6583
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6584
0
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6585
0
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6586
0
                 bed->s->log_file_align);
6587
0
  }
6588
80
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6589
80
    && hdr->bfd_section == NULL)
6590
         /* We don't know the offset of these sections yet:
6591
      their size has not been decided.  */
6592
80
         || (abfd->is_linker_output
6593
80
       && hdr->bfd_section != NULL
6594
80
       && (hdr->sh_name == -1u
6595
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6596
80
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6597
80
         || (elf_symtab_shndx_list (abfd) != NULL
6598
72
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6599
80
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6600
80
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6601
44
  hdr->sh_offset = -1;
6602
36
      else
6603
36
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6604
422
    }
6605
28
  elf_next_file_pos (abfd) = off;
6606
6607
  /* Now that we have set the section file positions, we can set up
6608
     the file positions for the non PT_LOAD segments.  */
6609
28
  phdrs = elf_tdata (abfd)->phdr;
6610
130
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6611
102
    {
6612
102
      if (p->p_type == PT_GNU_RELRO)
6613
6
  {
6614
6
    bfd_vma start, end;  /* Bytes.  */
6615
6
    bool ok;
6616
6617
6
    if (link_info != NULL)
6618
0
      {
6619
        /* During linking the range of the RELRO segment is passed
6620
     in link_info.  Note that there may be padding between
6621
     relro_start and the first RELRO section.  */
6622
0
        start = link_info->relro_start;
6623
0
        end = link_info->relro_end;
6624
0
      }
6625
6
    else if (m->count != 0)
6626
6
      {
6627
6
        if (!m->p_size_valid)
6628
0
    abort ();
6629
6
        start = m->sections[0]->vma;
6630
6
        end = start + m->p_size / opb;
6631
6
      }
6632
0
    else
6633
0
      {
6634
0
        start = 0;
6635
0
        end = 0;
6636
0
      }
6637
6638
6
    ok = false;
6639
6
    if (start < end)
6640
6
      {
6641
6
        struct elf_segment_map *lm;
6642
6
        const Elf_Internal_Phdr *lp;
6643
6
        unsigned int i;
6644
6645
        /* Find a LOAD segment containing a section in the RELRO
6646
     segment.  */
6647
6
        for (lm = elf_seg_map (abfd), lp = phdrs;
6648
24
       lm != NULL;
6649
18
       lm = lm->next, lp++)
6650
24
    {
6651
24
      if (lp->p_type == PT_LOAD
6652
24
          && lm->count != 0
6653
24
          && (lm->sections[lm->count - 1]->vma
6654
13
        + (!IS_TBSS (lm->sections[lm->count - 1])
6655
13
           ? lm->sections[lm->count - 1]->size / opb
6656
13
           : 0)) > start
6657
24
          && lm->sections[0]->vma < end)
6658
6
        break;
6659
24
    }
6660
6661
6
        if (lm != NULL)
6662
6
    {
6663
      /* Find the section starting the RELRO segment.  */
6664
6
      for (i = 0; i < lm->count; i++)
6665
6
        {
6666
6
          asection *s = lm->sections[i];
6667
6
          if (s->vma >= start
6668
6
        && s->vma < end
6669
6
        && s->size != 0)
6670
6
      break;
6671
6
        }
6672
6673
6
      if (i < lm->count)
6674
6
        {
6675
6
          p->p_vaddr = lm->sections[i]->vma * opb;
6676
6
          p->p_paddr = lm->sections[i]->lma * opb;
6677
6
          p->p_offset = lm->sections[i]->filepos;
6678
6
          p->p_memsz = end * opb - p->p_vaddr;
6679
6
          p->p_filesz = p->p_memsz;
6680
6681
          /* The RELRO segment typically ends a few bytes
6682
       into .got.plt but other layouts are possible.
6683
       In cases where the end does not match any
6684
       loaded section (for instance is in file
6685
       padding), trim p_filesz back to correspond to
6686
       the end of loaded section contents.  */
6687
6
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6688
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6689
6690
          /* Preserve the alignment and flags if they are
6691
       valid.  The gold linker generates RW/4 for
6692
       the PT_GNU_RELRO section.  It is better for
6693
       objcopy/strip to honor these attributes
6694
       otherwise gdb will choke when using separate
6695
       debug files.  */
6696
6
          if (!m->p_align_valid)
6697
0
      p->p_align = 1;
6698
6
          if (!m->p_flags_valid)
6699
0
      p->p_flags = PF_R;
6700
6
          ok = true;
6701
6
        }
6702
6
    }
6703
6
      }
6704
6705
6
    if (!ok)
6706
0
      {
6707
0
        if (link_info != NULL)
6708
0
    _bfd_error_handler
6709
0
      (_("%pB: warning: unable to allocate any sections"
6710
0
         " to PT_GNU_RELRO segment"),
6711
0
       abfd);
6712
0
        memset (p, 0, sizeof *p);
6713
0
      }
6714
6
  }
6715
96
      else if (p->p_type == PT_GNU_STACK)
6716
5
  {
6717
5
    if (m->p_size_valid)
6718
5
      p->p_memsz = m->p_size;
6719
5
  }
6720
91
      else if (m->count != 0)
6721
74
  {
6722
74
    unsigned int i;
6723
6724
74
    if (p->p_type != PT_LOAD
6725
74
        && (p->p_type != PT_NOTE
6726
42
      || bfd_get_format (abfd) != bfd_core))
6727
42
      {
6728
        /* A user specified segment layout may include a PHDR
6729
     segment that overlaps with a LOAD segment...  */
6730
42
        if (p->p_type == PT_PHDR)
6731
0
    {
6732
0
      m->count = 0;
6733
0
      continue;
6734
0
    }
6735
6736
42
        if (m->includes_filehdr || m->includes_phdrs)
6737
0
    {
6738
      /* PR 17512: file: 2195325e.  */
6739
0
      _bfd_error_handler
6740
0
        (_("%pB: error: non-load segment %d includes file header "
6741
0
           "and/or program header"),
6742
0
         abfd, (int) (p - phdrs));
6743
0
      return false;
6744
0
    }
6745
6746
42
        p->p_filesz = 0;
6747
42
        p->p_offset = m->sections[0]->filepos;
6748
42
        for (i = m->count; i-- != 0;)
6749
42
    {
6750
42
      asection *sect = m->sections[i];
6751
42
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6752
42
      if (hdr->sh_type != SHT_NOBITS)
6753
42
        {
6754
42
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6755
          /* NB: p_memsz of the loadable PT_NOTE segment
6756
       should be the same as p_filesz.  */
6757
42
          if (p->p_type == PT_NOTE
6758
42
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6759
7
      p->p_memsz = p->p_filesz;
6760
42
          break;
6761
42
        }
6762
42
    }
6763
42
      }
6764
74
  }
6765
102
    }
6766
6767
28
  return true;
6768
28
}
6769
6770
static elf_section_list *
6771
find_section_in_list (unsigned int i, elf_section_list * list)
6772
1.60k
{
6773
1.61k
  for (;list != NULL; list = list->next)
6774
1
    if (list->ndx == i)
6775
0
      break;
6776
1.60k
  return list;
6777
1.60k
}
6778
6779
/* Work out the file positions of all the sections.  This is called by
6780
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6781
   VMAs must be known before this is called.
6782
6783
   Reloc sections come in two flavours: Those processed specially as
6784
   "side-channel" data attached to a section to which they apply, and
6785
   those that bfd doesn't process as relocations.  The latter sort are
6786
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6787
   consider the former sort here, unless they form part of the loadable
6788
   image.  Reloc sections not assigned here (and compressed debugging
6789
   sections and CTF sections which nothing else in the file can rely
6790
   upon) will be handled later by assign_file_positions_for_relocs.
6791
6792
   We also don't set the positions of the .symtab and .strtab here.  */
6793
6794
static bool
6795
assign_file_positions_except_relocs (bfd *abfd,
6796
             struct bfd_link_info *link_info)
6797
95
{
6798
95
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6799
95
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6800
95
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6801
95
  unsigned int alloc;
6802
6803
95
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6804
95
      && bfd_get_format (abfd) != bfd_core)
6805
67
    {
6806
67
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6807
67
      unsigned int num_sec = elf_numsections (abfd);
6808
67
      Elf_Internal_Shdr **hdrpp;
6809
67
      unsigned int i;
6810
67
      file_ptr off;
6811
6812
      /* Start after the ELF header.  */
6813
67
      off = i_ehdrp->e_ehsize;
6814
6815
      /* We are not creating an executable, which means that we are
6816
   not creating a program header, and that the actual order of
6817
   the sections in the file is unimportant.  */
6818
3.79k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6819
3.72k
  {
6820
3.72k
    Elf_Internal_Shdr *hdr;
6821
6822
3.72k
    hdr = *hdrpp;
6823
3.72k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6824
3.72k
         && hdr->bfd_section == NULL)
6825
        /* Do not assign offsets for these sections yet: we don't know
6826
     their sizes.  */
6827
3.72k
        || (abfd->is_linker_output
6828
2.48k
      && hdr->bfd_section != NULL
6829
2.48k
      && (hdr->sh_name == -1u
6830
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6831
3.72k
        || i == elf_onesymtab (abfd)
6832
3.72k
        || (elf_symtab_shndx_list (abfd) != NULL
6833
2.45k
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6834
3.72k
        || i == elf_strtab_sec (abfd)
6835
3.72k
        || i == elf_shstrtab_sec (abfd))
6836
1.36k
      {
6837
1.36k
        hdr->sh_offset = -1;
6838
1.36k
      }
6839
2.35k
    else
6840
      /* There shouldn't be a need to effect "capped" file alignment here,
6841
         yet at least the Linux kernel's modpost utility was found to be
6842
         unhappy without.  While the issue was addressed there, let's be
6843
         kind for at least the foreseeable future ...  */
6844
2.35k
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6845
2.35k
                   bed->s->log_file_align);
6846
3.72k
  }
6847
6848
67
      elf_next_file_pos (abfd) = off;
6849
67
      elf_program_header_size (abfd) = 0;
6850
67
    }
6851
28
  else
6852
28
    {
6853
      /* Assign file positions for the loaded sections based on the
6854
   assignment of sections to segments.  */
6855
28
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6856
0
  return false;
6857
6858
      /* And for non-load sections.  */
6859
28
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6860
0
  return false;
6861
28
    }
6862
6863
95
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6864
0
    return false;
6865
6866
  /* Write out the program headers.  */
6867
95
  alloc = i_ehdrp->e_phnum;
6868
95
  if (alloc != 0)
6869
16
    {
6870
16
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6871
0
  {
6872
0
    bool warned_tls = false;
6873
0
    bool warned_rwx = false;
6874
6875
    /* Memory resident segments with non-zero size and RWX
6876
       permissions are a security risk, so we generate a warning
6877
       here if we are creating any.  */
6878
0
    unsigned int i;
6879
6880
0
    for (i = 0; i < alloc; i++)
6881
0
      {
6882
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6883
6884
0
        if (phdr->p_memsz == 0)
6885
0
    continue;
6886
6887
0
        if (! warned_tls
6888
0
      && phdr->p_type == PT_TLS
6889
0
      && (phdr->p_flags & PF_X))
6890
0
    {
6891
0
      if (link_info->warn_is_error_for_rwx_segments)
6892
0
        {
6893
0
          _bfd_error_handler (_("\
6894
0
error: %pB has a TLS segment with execute permission"),
6895
0
            abfd);
6896
0
          return false;
6897
0
        }
6898
6899
0
      _bfd_error_handler (_("\
6900
0
warning: %pB has a TLS segment with execute permission"),
6901
0
              abfd);
6902
0
      if (warned_rwx)
6903
0
        break;
6904
6905
0
      warned_tls = true;
6906
0
    }
6907
0
        else if (! warned_rwx
6908
0
           && phdr->p_type == PT_LOAD
6909
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6910
0
         == (PF_R | PF_W | PF_X)))
6911
0
    {
6912
0
      if (link_info->warn_is_error_for_rwx_segments)
6913
0
        {
6914
0
          _bfd_error_handler (_("\
6915
0
error: %pB has a LOAD segment with RWX permissions"),
6916
0
            abfd);
6917
0
          return false;
6918
0
        }
6919
6920
0
      _bfd_error_handler (_("\
6921
0
warning: %pB has a LOAD segment with RWX permissions"),
6922
0
              abfd);
6923
0
      if (warned_tls)
6924
0
        break;
6925
6926
0
      warned_rwx = true;
6927
0
    }
6928
0
      }
6929
0
  }
6930
6931
16
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6932
16
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6933
0
  return false;
6934
16
    }
6935
6936
95
  return true;
6937
95
}
6938
6939
bool
6940
_bfd_elf_init_file_header (bfd *abfd,
6941
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6942
96
{
6943
96
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
6944
96
  struct elf_strtab_hash *shstrtab;
6945
96
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6946
6947
96
  i_ehdrp = elf_elfheader (abfd);
6948
6949
96
  shstrtab = _bfd_elf_strtab_init ();
6950
96
  if (shstrtab == NULL)
6951
0
    return false;
6952
6953
96
  elf_shstrtab (abfd) = shstrtab;
6954
6955
96
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6956
96
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6957
96
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6958
96
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6959
6960
96
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6961
96
  i_ehdrp->e_ident[EI_DATA] =
6962
96
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6963
96
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6964
6965
96
  if ((abfd->flags & DYNAMIC) != 0)
6966
15
    i_ehdrp->e_type = ET_DYN;
6967
81
  else if ((abfd->flags & EXEC_P) != 0)
6968
13
    i_ehdrp->e_type = ET_EXEC;
6969
68
  else if (bfd_get_format (abfd) == bfd_core)
6970
0
    i_ehdrp->e_type = ET_CORE;
6971
68
  else
6972
68
    i_ehdrp->e_type = ET_REL;
6973
6974
96
  switch (bfd_get_arch (abfd))
6975
96
    {
6976
0
    case bfd_arch_unknown:
6977
0
      i_ehdrp->e_machine = EM_NONE;
6978
0
      break;
6979
6980
      /* There used to be a long list of cases here, each one setting
6981
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6982
   in the corresponding bfd definition.  To avoid duplication,
6983
   the switch was removed.  Machines that need special handling
6984
   can generally do it in elf_backend_final_write_processing(),
6985
   unless they need the information earlier than the final write.
6986
   Such need can generally be supplied by replacing the tests for
6987
   e_machine with the conditions used to determine it.  */
6988
96
    default:
6989
96
      i_ehdrp->e_machine = bed->elf_machine_code;
6990
96
    }
6991
6992
96
  i_ehdrp->e_version = bed->s->ev_current;
6993
96
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6994
6995
  /* No program header, for now.  */
6996
96
  i_ehdrp->e_phoff = 0;
6997
96
  i_ehdrp->e_phentsize = 0;
6998
96
  i_ehdrp->e_phnum = 0;
6999
7000
  /* Each bfd section is section header entry.  */
7001
96
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7002
96
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7003
7004
96
  elf_tdata (abfd)->symtab_hdr.sh_name =
7005
96
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7006
96
  elf_tdata (abfd)->strtab_hdr.sh_name =
7007
96
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7008
96
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7009
96
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7010
96
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7011
96
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7012
96
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7013
0
    return false;
7014
7015
96
  return true;
7016
96
}
7017
7018
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7019
7020
   FIXME: We used to have code here to sort the PT_LOAD segments into
7021
   ascending order, as per the ELF spec.  But this breaks some programs,
7022
   including the Linux kernel.  But really either the spec should be
7023
   changed or the programs updated.  */
7024
7025
bool
7026
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7027
95
{
7028
95
  if (link_info != NULL && bfd_link_pie (link_info))
7029
0
    {
7030
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7031
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7032
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7033
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7034
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7035
7036
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7037
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7038
0
      for (; segment < end_segment; segment++)
7039
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7040
0
    p_vaddr = segment->p_vaddr;
7041
7042
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7043
   segments is non-zero.  */
7044
0
      if (p_vaddr)
7045
0
  i_ehdrp->e_type = ET_EXEC;
7046
0
    }
7047
95
  return true;
7048
95
}
7049
7050
/* Assign file positions for all the reloc sections which are not part
7051
   of the loadable file image, and the file position of section headers.  */
7052
7053
static bool
7054
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7055
95
{
7056
95
  file_ptr off;
7057
95
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7058
95
  Elf_Internal_Shdr *shdrp;
7059
95
  Elf_Internal_Ehdr *i_ehdrp;
7060
95
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7061
7062
  /* Skip non-load sections without section header.  */
7063
95
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7064
0
    return true;
7065
7066
95
  off = elf_next_file_pos (abfd);
7067
7068
95
  shdrpp = elf_elfsections (abfd);
7069
95
  end_shdrpp = shdrpp + elf_numsections (abfd);
7070
4.24k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7071
4.14k
    {
7072
4.14k
      shdrp = *shdrpp;
7073
4.14k
      if (shdrp->sh_offset == -1)
7074
1.33k
  {
7075
1.33k
    asection *sec = shdrp->bfd_section;
7076
1.33k
    if (sec == NULL
7077
1.33k
        || shdrp->sh_type == SHT_REL
7078
1.33k
        || shdrp->sh_type == SHT_RELA)
7079
1.33k
      ;
7080
0
    else if (bfd_section_is_ctf (sec))
7081
0
      {
7082
        /* Update section size and contents.  */
7083
0
        shdrp->sh_size = sec->size;
7084
0
        shdrp->contents = sec->contents;
7085
0
      }
7086
0
    else if (shdrp->sh_name == -1u)
7087
0
      {
7088
0
        const char *name = sec->name;
7089
0
        struct bfd_elf_section_data *d;
7090
7091
        /* Compress DWARF debug sections.  */
7092
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7093
0
    return false;
7094
7095
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7096
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7097
0
      && name[1] == 'd')
7098
0
    {
7099
      /* If section is compressed with zlib-gnu, convert
7100
         section name from .debug_* to .zdebug_*.  */
7101
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7102
0
      if (new_name == NULL)
7103
0
        return false;
7104
0
      name = new_name;
7105
0
    }
7106
        /* Add section name to section name section.  */
7107
0
        shdrp->sh_name
7108
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7109
0
                  name, false);
7110
0
        d = elf_section_data (sec);
7111
7112
        /* Add reloc section name to section name section.  */
7113
0
        if (d->rel.hdr
7114
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7115
0
              name, false))
7116
0
    return false;
7117
0
        if (d->rela.hdr
7118
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7119
0
              name, true))
7120
0
    return false;
7121
7122
        /* Update section size and contents.  */
7123
0
        shdrp->sh_size = sec->size;
7124
0
        shdrp->contents = sec->contents;
7125
0
        sec->contents = NULL;
7126
0
      }
7127
7128
1.33k
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7129
1.33k
      (abfd->flags & (EXEC_P | DYNAMIC))
7130
1.33k
      || bfd_get_format (abfd) == bfd_core,
7131
1.33k
      bed->s->log_file_align);
7132
1.33k
  }
7133
4.14k
    }
7134
7135
  /* Place section name section after DWARF debug sections have been
7136
     compressed.  */
7137
95
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7138
95
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7139
95
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7140
95
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7141
7142
  /* Place the section headers.  */
7143
95
  i_ehdrp = elf_elfheader (abfd);
7144
95
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7145
95
  i_ehdrp->e_shoff = off;
7146
95
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7147
95
  elf_next_file_pos (abfd) = off;
7148
7149
95
  return true;
7150
95
}
7151
7152
bool
7153
_bfd_elf_write_object_contents (bfd *abfd)
7154
95
{
7155
95
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7156
95
  Elf_Internal_Shdr **i_shdrp;
7157
95
  bool failed;
7158
95
  unsigned int count, num_sec;
7159
95
  struct elf_obj_tdata *t;
7160
7161
95
  if (! abfd->output_has_begun
7162
95
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7163
0
    return false;
7164
  /* Do not rewrite ELF data when the BFD has been opened for update.
7165
     abfd->output_has_begun was set to TRUE on opening, so creation of
7166
     new sections, and modification of existing section sizes was
7167
     restricted.  This means the ELF header, program headers and
7168
     section headers can't have changed.  If the contents of any
7169
     sections has been modified, then those changes have already been
7170
     written to the BFD.  */
7171
95
  else if (abfd->direction == both_direction)
7172
0
    {
7173
0
      BFD_ASSERT (abfd->output_has_begun);
7174
0
      return true;
7175
0
    }
7176
7177
95
  i_shdrp = elf_elfsections (abfd);
7178
7179
95
  failed = false;
7180
95
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7181
95
  if (failed)
7182
0
    return false;
7183
7184
95
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7185
0
    return false;
7186
7187
  /* After writing the headers, we need to write the sections too...  */
7188
95
  num_sec = elf_numsections (abfd);
7189
4.24k
  for (count = 1; count < num_sec; count++)
7190
4.14k
    {
7191
      /* Don't set the sh_name field without section header.  */
7192
4.14k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7193
4.14k
  i_shdrp[count]->sh_name
7194
4.14k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7195
4.14k
            i_shdrp[count]->sh_name);
7196
4.14k
      if (bed->elf_backend_section_processing)
7197
2
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7198
0
    return false;
7199
4.14k
      if (i_shdrp[count]->contents)
7200
1.73k
  {
7201
1.73k
    bfd_size_type amt = i_shdrp[count]->sh_size;
7202
7203
1.73k
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7204
1.73k
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7205
0
      return false;
7206
1.73k
  }
7207
4.14k
    }
7208
7209
  /* Write out the section header names.  */
7210
95
  t = elf_tdata (abfd);
7211
95
  if (elf_shstrtab (abfd) != NULL
7212
95
      && t->shstrtab_hdr.sh_offset != -1
7213
95
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7214
95
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7215
0
    return false;
7216
7217
95
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7218
0
    return false;
7219
7220
95
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7221
0
    return false;
7222
7223
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7224
95
  if (t->o->build_id.after_write_object_contents != NULL
7225
95
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7226
0
    return false;
7227
95
  if (t->o->package_metadata.after_write_object_contents != NULL
7228
95
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7229
0
    return false;
7230
7231
95
  return true;
7232
95
}
7233
7234
bool
7235
_bfd_elf_write_corefile_contents (bfd *abfd)
7236
0
{
7237
  /* Hopefully this can be done just like an object file.  */
7238
0
  return _bfd_elf_write_object_contents (abfd);
7239
0
}
7240
7241
/* Given a section, search the header to find them.  */
7242
7243
unsigned int
7244
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7245
13.7k
{
7246
13.7k
  const struct elf_backend_data *bed;
7247
13.7k
  unsigned int sec_index;
7248
7249
13.7k
  if (elf_section_data (asect) != NULL
7250
13.7k
      && elf_section_data (asect)->this_idx != 0)
7251
10.0k
    return elf_section_data (asect)->this_idx;
7252
7253
3.64k
  if (bfd_is_abs_section (asect))
7254
0
    sec_index = SHN_ABS;
7255
3.64k
  else if (bfd_is_com_section (asect))
7256
0
    sec_index = SHN_COMMON;
7257
3.64k
  else if (bfd_is_und_section (asect))
7258
3.64k
    sec_index = SHN_UNDEF;
7259
0
  else
7260
0
    sec_index = SHN_BAD;
7261
7262
3.64k
  bed = get_elf_backend_data (abfd);
7263
3.64k
  if (bed->elf_backend_section_from_bfd_section)
7264
1.65k
    {
7265
1.65k
      int retval = sec_index;
7266
7267
1.65k
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7268
0
  return retval;
7269
1.65k
    }
7270
7271
3.64k
  if (sec_index == SHN_BAD)
7272
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7273
7274
3.64k
  return sec_index;
7275
3.64k
}
7276
7277
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7278
   on error.  */
7279
7280
int
7281
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7282
35.1k
{
7283
35.1k
  asymbol *asym_ptr = *asym_ptr_ptr;
7284
35.1k
  int idx;
7285
35.1k
  flagword flags = asym_ptr->flags;
7286
7287
  /* When gas creates relocations against local labels, it creates its
7288
     own symbol for the section, but does put the symbol into the
7289
     symbol chain, so udata is 0.  When the linker is generating
7290
     relocatable output, this section symbol may be for one of the
7291
     input sections rather than the output section.  */
7292
35.1k
  if (asym_ptr->udata.i == 0
7293
35.1k
      && (flags & BSF_SECTION_SYM)
7294
35.1k
      && asym_ptr->section)
7295
0
    {
7296
0
      asection *sec;
7297
7298
0
      sec = asym_ptr->section;
7299
0
      if (sec->owner != abfd && sec->output_section != NULL)
7300
0
  sec = sec->output_section;
7301
0
      if (sec->owner == abfd
7302
0
    && sec->index < elf_num_section_syms (abfd)
7303
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7304
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7305
0
    }
7306
7307
35.1k
  idx = asym_ptr->udata.i;
7308
7309
35.1k
  if (idx == 0)
7310
0
    {
7311
      /* This case can occur when using --strip-symbol on a symbol
7312
   which is used in a relocation entry.  */
7313
0
      _bfd_error_handler
7314
  /* xgettext:c-format */
7315
0
  (_("%pB: symbol `%s' required but not present"),
7316
0
   abfd, bfd_asymbol_name (asym_ptr));
7317
0
      bfd_set_error (bfd_error_no_symbols);
7318
0
      return -1;
7319
0
    }
7320
7321
#if DEBUG & 4
7322
  {
7323
    fprintf (stderr,
7324
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7325
       " flags = 0x%.8x\n",
7326
       (long) asym_ptr, asym_ptr->name, idx, flags);
7327
    fflush (stderr);
7328
  }
7329
#endif
7330
7331
35.1k
  return idx;
7332
35.1k
}
7333
7334
static inline bfd_vma
7335
segment_size (Elf_Internal_Phdr *segment)
7336
261
{
7337
261
  return (segment->p_memsz > segment->p_filesz
7338
261
    ? segment->p_memsz : segment->p_filesz);
7339
261
}
7340
7341
7342
/* Returns the end address of the segment + 1.  */
7343
static inline bfd_vma
7344
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7345
1
{
7346
1
  return start + segment_size (segment);
7347
1
}
7348
7349
static inline bfd_size_type
7350
section_size (asection *section, Elf_Internal_Phdr *segment)
7351
262
{
7352
262
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7353
262
      || (section->flags & SEC_THREAD_LOCAL) == 0
7354
262
      || segment->p_type == PT_TLS)
7355
262
    return section->size;
7356
0
  return 0;
7357
262
}
7358
7359
/* Returns TRUE if the given section is contained within the given
7360
   segment.  LMA addresses are compared against PADDR when
7361
   USE_VADDR is false, VMA against VADDR when true.  */
7362
static bool
7363
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7364
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7365
     bool use_vaddr)
7366
224
{
7367
224
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7368
224
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7369
224
  bfd_vma octet;
7370
224
  if (_bfd_mul_overflow (addr, opb, &octet))
7371
0
    return false;
7372
  /* The third and fourth lines below are testing that the section end
7373
     address is within the segment.  It's written this way to avoid
7374
     overflow.  Add seg_addr + section_size to both sides of the
7375
     inequality to make it obvious.  */
7376
224
  return (octet >= seg_addr
7377
224
    && segment_size (segment) >= section_size (section, segment)
7378
224
    && (octet - seg_addr
7379
113
        <= segment_size (segment) - section_size (section, segment)));
7380
224
}
7381
7382
/* Handle PT_NOTE segment.  */
7383
static bool
7384
is_note (asection *s, Elf_Internal_Phdr *p)
7385
143
{
7386
143
  return (p->p_type == PT_NOTE
7387
143
    && elf_section_type (s) == SHT_NOTE
7388
143
    && (ufile_ptr) s->filepos >= p->p_offset
7389
143
    && p->p_filesz >= s->size
7390
143
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7391
143
}
7392
7393
/* Rewrite program header information.  */
7394
7395
static bool
7396
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7397
1
{
7398
1
  Elf_Internal_Ehdr *iehdr;
7399
1
  struct elf_segment_map *map;
7400
1
  struct elf_segment_map *map_first;
7401
1
  struct elf_segment_map **pointer_to_map;
7402
1
  Elf_Internal_Phdr *segment;
7403
1
  asection *section;
7404
1
  unsigned int i;
7405
1
  unsigned int num_segments;
7406
1
  bool phdr_included = false;
7407
1
  bool p_paddr_valid;
7408
1
  struct elf_segment_map *phdr_adjust_seg = NULL;
7409
1
  unsigned int phdr_adjust_num = 0;
7410
1
  const struct elf_backend_data *bed;
7411
1
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7412
7413
1
  bed = get_elf_backend_data (ibfd);
7414
1
  iehdr = elf_elfheader (ibfd);
7415
7416
1
  map_first = NULL;
7417
1
  pointer_to_map = &map_first;
7418
7419
1
  num_segments = elf_elfheader (ibfd)->e_phnum;
7420
7421
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7422
     linker, which generates a PT_INTERP section with p_vaddr and
7423
     p_memsz set to 0.  */
7424
1
#define IS_SOLARIS_PT_INTERP(p, s)          \
7425
1
  (p->p_vaddr == 0              \
7426
1
   && p->p_paddr == 0              \
7427
1
   && p->p_memsz == 0              \
7428
1
   && p->p_filesz > 0              \
7429
1
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7430
1
   && s->size > 0              \
7431
1
   && (bfd_vma) s->filepos >= p->p_offset        \
7432
1
   && ((bfd_vma) s->filepos + s->size         \
7433
1
       <= p->p_offset + p->p_filesz))
7434
7435
  /* Decide if the given section should be included in the given segment.
7436
     A section will be included if:
7437
       1. It is within the address space of the segment -- we use the LMA
7438
    if that is set for the segment and the VMA otherwise,
7439
       2. It is an allocated section or a NOTE section in a PT_NOTE
7440
    segment.
7441
       3. There is an output section associated with it,
7442
       4. The section has not already been allocated to a previous segment.
7443
       5. PT_GNU_STACK segments do not include any sections.
7444
       6. PT_TLS segment includes only SHF_TLS sections.
7445
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7446
       8. PT_DYNAMIC should not contain empty sections at the beginning
7447
    (with the possible exception of .dynamic).  */
7448
1
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7449
224
  (((is_contained_by (section, segment, segment->p_paddr,    \
7450
197
          segment->p_vaddr, opb, !paddr_valid)    \
7451
197
     && (section->flags & SEC_ALLOC) != 0)       \
7452
197
    || is_note (section, segment))         \
7453
197
   && segment->p_type != PT_GNU_STACK          \
7454
197
   && (segment->p_type != PT_TLS          \
7455
54
       || (section->flags & SEC_THREAD_LOCAL))        \
7456
197
   && (segment->p_type == PT_LOAD          \
7457
54
       || segment->p_type == PT_TLS          \
7458
54
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7459
197
   && (segment->p_type != PT_DYNAMIC          \
7460
54
       || section_size (section, segment) > 0        \
7461
54
       || (segment->p_paddr            \
7462
0
     ? segment->p_paddr != section->lma * (opb)      \
7463
0
     : segment->p_vaddr != section->vma * (opb))      \
7464
54
       || (strcmp (bfd_section_name (section), ".dynamic") == 0))  \
7465
224
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7466
7467
/* If the output section of a section in the input segment is NULL,
7468
   it is removed from the corresponding output segment.   */
7469
1
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7470
27
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7471
27
   && section->output_section != NULL)
7472
7473
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7474
1
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7475
2
  (seg1->field >= segment_end (seg2, seg2->field))
7476
7477
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7478
     their VMA address ranges and their LMA address ranges overlap.
7479
     It is possible to have overlapping VMA ranges without overlapping LMA
7480
     ranges.  RedBoot images for example can have both .data and .bss mapped
7481
     to the same VMA range, but with the .data section mapped to a different
7482
     LMA.  */
7483
1
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7484
1
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7485
1
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7486
1
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7487
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7488
7489
  /* Initialise the segment mark field, and discard stupid alignment.  */
7490
35
  for (section = ibfd->sections; section != NULL; section = section->next)
7491
34
    {
7492
34
      asection *o = section->output_section;
7493
34
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7494
0
  o->alignment_power = 0;
7495
34
      section->segment_mark = false;
7496
34
    }
7497
7498
  /* The Solaris linker creates program headers in which all the
7499
     p_paddr fields are zero.  When we try to objcopy or strip such a
7500
     file, we get confused.  Check for this case, and if we find it
7501
     don't set the p_paddr_valid fields.  */
7502
1
  p_paddr_valid = false;
7503
1
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7504
6
       i < num_segments;
7505
5
       i++, segment++)
7506
5
    if (segment->p_paddr != 0)
7507
0
      {
7508
0
  p_paddr_valid = true;
7509
0
  break;
7510
0
      }
7511
7512
  /* Scan through the segments specified in the program header
7513
     of the input BFD.  For this first scan we look for overlaps
7514
     in the loadable segments.  These can be created by weird
7515
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7516
1
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7517
6
       i < num_segments;
7518
5
       i++, segment++)
7519
5
    {
7520
5
      unsigned int j;
7521
5
      Elf_Internal_Phdr *segment2;
7522
7523
5
      if (segment->p_type == PT_INTERP)
7524
1
  for (section = ibfd->sections; section; section = section->next)
7525
1
    if (IS_SOLARIS_PT_INTERP (segment, section))
7526
1
      {
7527
        /* Mininal change so that the normal section to segment
7528
     assignment code will work.  */
7529
1
        segment->p_vaddr = section->vma * opb;
7530
1
        break;
7531
1
      }
7532
7533
5
      if (segment->p_type != PT_LOAD)
7534
3
  {
7535
    /* Remove PT_GNU_RELRO segment.  */
7536
3
    if (segment->p_type == PT_GNU_RELRO)
7537
0
      segment->p_type = PT_NULL;
7538
3
    continue;
7539
3
  }
7540
7541
      /* Determine if this segment overlaps any previous segments.  */
7542
7
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7543
5
  {
7544
5
    bfd_signed_vma extra_length;
7545
7546
5
    if (segment2->p_type != PT_LOAD
7547
5
        || !SEGMENT_OVERLAPS (segment, segment2))
7548
5
      continue;
7549
7550
    /* Merge the two segments together.  */
7551
0
    if (segment2->p_vaddr < segment->p_vaddr)
7552
0
      {
7553
        /* Extend SEGMENT2 to include SEGMENT and then delete
7554
     SEGMENT.  */
7555
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7556
0
            - segment_end (segment2, segment2->p_vaddr));
7557
7558
0
        if (extra_length > 0)
7559
0
    {
7560
0
      segment2->p_memsz += extra_length;
7561
0
      segment2->p_filesz += extra_length;
7562
0
    }
7563
7564
0
        segment->p_type = PT_NULL;
7565
7566
        /* Since we have deleted P we must restart the outer loop.  */
7567
0
        i = 0;
7568
0
        segment = elf_tdata (ibfd)->phdr;
7569
0
        break;
7570
0
      }
7571
0
    else
7572
0
      {
7573
        /* Extend SEGMENT to include SEGMENT2 and then delete
7574
     SEGMENT2.  */
7575
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7576
0
            - segment_end (segment, segment->p_vaddr));
7577
7578
0
        if (extra_length > 0)
7579
0
    {
7580
0
      segment->p_memsz += extra_length;
7581
0
      segment->p_filesz += extra_length;
7582
0
    }
7583
7584
0
        segment2->p_type = PT_NULL;
7585
0
      }
7586
0
  }
7587
2
    }
7588
7589
  /* The second scan attempts to assign sections to segments.  */
7590
1
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7591
6
       i < num_segments;
7592
5
       i++, segment++)
7593
5
    {
7594
5
      unsigned int section_count;
7595
5
      asection **sections;
7596
5
      asection *output_section;
7597
5
      unsigned int isec;
7598
5
      asection *matching_lma;
7599
5
      asection *suggested_lma;
7600
5
      unsigned int j;
7601
5
      size_t amt;
7602
5
      asection *first_section;
7603
7604
5
      if (segment->p_type == PT_NULL)
7605
0
  continue;
7606
7607
5
      first_section = NULL;
7608
      /* Compute how many sections might be placed into this segment.  */
7609
5
      for (section = ibfd->sections, section_count = 0;
7610
175
     section != NULL;
7611
170
     section = section->next)
7612
170
  {
7613
    /* Find the first section in the input segment, which may be
7614
       removed from the corresponding output segment.   */
7615
170
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7616
27
      {
7617
27
        if (first_section == NULL)
7618
4
    first_section = section;
7619
27
        if (section->output_section != NULL)
7620
27
    ++section_count;
7621
27
      }
7622
170
  }
7623
7624
      /* Allocate a segment map big enough to contain
7625
   all of the sections we have selected.  */
7626
5
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7627
5
      amt += section_count * sizeof (asection *);
7628
5
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7629
5
      if (map == NULL)
7630
0
  return false;
7631
7632
      /* Initialise the fields of the segment map.  Default to
7633
   using the physical address of the segment in the input BFD.  */
7634
5
      map->next = NULL;
7635
5
      map->p_type = segment->p_type;
7636
5
      map->p_flags = segment->p_flags;
7637
5
      map->p_flags_valid = 1;
7638
7639
5
      if (map->p_type == PT_LOAD
7640
5
    && (ibfd->flags & D_PAGED) != 0
7641
5
    && maxpagesize > 1
7642
5
    && segment->p_align > 1)
7643
2
  {
7644
2
    map->p_align = segment->p_align;
7645
2
    if (segment->p_align > maxpagesize)
7646
0
      map->p_align = maxpagesize;
7647
2
    map->p_align_valid = 1;
7648
2
  }
7649
7650
      /* If the first section in the input segment is removed, there is
7651
   no need to preserve segment physical address in the corresponding
7652
   output segment.  */
7653
5
      if (!first_section || first_section->output_section != NULL)
7654
5
  {
7655
5
    map->p_paddr = segment->p_paddr;
7656
5
    map->p_paddr_valid = p_paddr_valid;
7657
5
  }
7658
7659
      /* Determine if this segment contains the ELF file header
7660
   and if it contains the program headers themselves.  */
7661
5
      map->includes_filehdr = (segment->p_offset == 0
7662
5
             && segment->p_filesz >= iehdr->e_ehsize);
7663
5
      map->includes_phdrs = 0;
7664
7665
5
      if (!phdr_included || segment->p_type != PT_LOAD)
7666
4
  {
7667
4
    map->includes_phdrs =
7668
4
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7669
4
       && (segment->p_offset + segment->p_filesz
7670
2
     >= ((bfd_vma) iehdr->e_phoff
7671
2
         + iehdr->e_phnum * iehdr->e_phentsize)));
7672
7673
4
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7674
1
      phdr_included = true;
7675
4
  }
7676
7677
5
      if (section_count == 0)
7678
1
  {
7679
    /* Special segments, such as the PT_PHDR segment, may contain
7680
       no sections, but ordinary, loadable segments should contain
7681
       something.  They are allowed by the ELF spec however, so only
7682
       a warning is produced.
7683
       Don't warn if an empty PT_LOAD contains the program headers.
7684
       There is however the valid use case of embedded systems which
7685
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7686
       flash memory with zeros.  No warning is shown for that case.  */
7687
1
    if (segment->p_type == PT_LOAD
7688
1
        && !map->includes_phdrs
7689
1
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7690
      /* xgettext:c-format */
7691
0
      _bfd_error_handler
7692
0
        (_("%pB: warning: empty loadable segment detected"
7693
0
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7694
0
         ibfd, (uint64_t) segment->p_vaddr);
7695
7696
1
    map->p_vaddr_offset = segment->p_vaddr / opb;
7697
1
    map->count = 0;
7698
1
    *pointer_to_map = map;
7699
1
    pointer_to_map = &map->next;
7700
7701
1
    continue;
7702
1
  }
7703
7704
      /* Now scan the sections in the input BFD again and attempt
7705
   to add their corresponding output sections to the segment map.
7706
   The problem here is how to handle an output section which has
7707
   been moved (ie had its LMA changed).  There are four possibilities:
7708
7709
   1. None of the sections have been moved.
7710
      In this case we can continue to use the segment LMA from the
7711
      input BFD.
7712
7713
   2. All of the sections have been moved by the same amount.
7714
      In this case we can change the segment's LMA to match the LMA
7715
      of the first section.
7716
7717
   3. Some of the sections have been moved, others have not.
7718
      In this case those sections which have not been moved can be
7719
      placed in the current segment which will have to have its size,
7720
      and possibly its LMA changed, and a new segment or segments will
7721
      have to be created to contain the other sections.
7722
7723
   4. The sections have been moved, but not by the same amount.
7724
      In this case we can change the segment's LMA to match the LMA
7725
      of the first section and we will have to create a new segment
7726
      or segments to contain the other sections.
7727
7728
   In order to save time, we allocate an array to hold the section
7729
   pointers that we are interested in.  As these sections get assigned
7730
   to a segment, they are removed from this array.  */
7731
7732
4
      amt = section_count * sizeof (asection *);
7733
4
      sections = (asection **) bfd_malloc (amt);
7734
4
      if (sections == NULL)
7735
0
  return false;
7736
7737
      /* Step One: Scan for segment vs section LMA conflicts.
7738
   Also add the sections to the section array allocated above.
7739
   Also add the sections to the current segment.  In the common
7740
   case, where the sections have not been moved, this means that
7741
   we have completely filled the segment, and there is nothing
7742
   more to do.  */
7743
4
      isec = 0;
7744
4
      matching_lma = NULL;
7745
4
      suggested_lma = NULL;
7746
7747
4
      for (section = first_section, j = 0;
7748
27
     section != NULL;
7749
23
     section = section->next)
7750
27
  {
7751
27
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7752
27
      {
7753
27
        output_section = section->output_section;
7754
7755
27
        sections[j++] = section;
7756
7757
        /* The Solaris native linker always sets p_paddr to 0.
7758
     We try to catch that case here, and set it to the
7759
     correct value.  Note - some backends require that
7760
     p_paddr be left as zero.  */
7761
27
        if (!p_paddr_valid
7762
27
      && segment->p_vaddr != 0
7763
27
      && !bed->want_p_paddr_set_to_zero
7764
27
      && isec == 0
7765
27
      && output_section->lma != 0
7766
27
      && (align_power (segment->p_vaddr
7767
4
           + (map->includes_filehdr
7768
4
              ? iehdr->e_ehsize : 0)
7769
4
           + (map->includes_phdrs
7770
4
              ? iehdr->e_phnum * iehdr->e_phentsize
7771
4
              : 0),
7772
4
           output_section->alignment_power * opb)
7773
4
          == (output_section->vma * opb)))
7774
4
    map->p_paddr = segment->p_vaddr;
7775
7776
        /* Match up the physical address of the segment with the
7777
     LMA address of the output section.  */
7778
27
        if (is_contained_by (output_section, segment, map->p_paddr,
7779
27
           0, opb, false)
7780
27
      || is_note (section, segment))
7781
27
    {
7782
27
      if (matching_lma == NULL
7783
27
          || output_section->lma < matching_lma->lma)
7784
4
        matching_lma = output_section;
7785
7786
      /* We assume that if the section fits within the segment
7787
         then it does not overlap any other section within that
7788
         segment.  */
7789
27
      map->sections[isec++] = output_section;
7790
27
    }
7791
0
        else if (suggested_lma == NULL)
7792
0
    suggested_lma = output_section;
7793
7794
27
        if (j == section_count)
7795
4
    break;
7796
27
      }
7797
27
  }
7798
7799
4
      BFD_ASSERT (j == section_count);
7800
7801
      /* Step Two: Adjust the physical address of the current segment,
7802
   if necessary.  */
7803
4
      if (isec == section_count)
7804
4
  {
7805
    /* All of the sections fitted within the segment as currently
7806
       specified.  This is the default case.  Add the segment to
7807
       the list of built segments and carry on to process the next
7808
       program header in the input BFD.  */
7809
4
    map->count = section_count;
7810
4
    *pointer_to_map = map;
7811
4
    pointer_to_map = &map->next;
7812
7813
4
    if (p_paddr_valid
7814
4
        && !bed->want_p_paddr_set_to_zero)
7815
0
      {
7816
0
        bfd_vma hdr_size = 0;
7817
0
        if (map->includes_filehdr)
7818
0
    hdr_size = iehdr->e_ehsize;
7819
0
        if (map->includes_phdrs)
7820
0
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7821
7822
        /* Account for padding before the first section in the
7823
     segment.  */
7824
0
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7825
0
             - matching_lma->lma);
7826
0
      }
7827
7828
4
    free (sections);
7829
4
    continue;
7830
4
  }
7831
0
      else
7832
0
  {
7833
    /* Change the current segment's physical address to match
7834
       the LMA of the first section that fitted, or if no
7835
       section fitted, the first section.  */
7836
0
    if (matching_lma == NULL)
7837
0
      matching_lma = suggested_lma;
7838
7839
0
    map->p_paddr = matching_lma->lma * opb;
7840
7841
    /* Offset the segment physical address from the lma
7842
       to allow for space taken up by elf headers.  */
7843
0
    if (map->includes_phdrs)
7844
0
      {
7845
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7846
7847
        /* iehdr->e_phnum is just an estimate of the number
7848
     of program headers that we will need.  Make a note
7849
     here of the number we used and the segment we chose
7850
     to hold these headers, so that we can adjust the
7851
     offset when we know the correct value.  */
7852
0
        phdr_adjust_num = iehdr->e_phnum;
7853
0
        phdr_adjust_seg = map;
7854
0
      }
7855
7856
0
    if (map->includes_filehdr)
7857
0
      {
7858
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7859
0
        map->p_paddr -= iehdr->e_ehsize;
7860
        /* We've subtracted off the size of headers from the
7861
     first section lma, but there may have been some
7862
     alignment padding before that section too.  Try to
7863
     account for that by adjusting the segment lma down to
7864
     the same alignment.  */
7865
0
        if (segment->p_align != 0 && segment->p_align < align)
7866
0
    align = segment->p_align;
7867
0
        map->p_paddr &= -(align * opb);
7868
0
      }
7869
0
  }
7870
7871
      /* Step Three: Loop over the sections again, this time assigning
7872
   those that fit to the current segment and removing them from the
7873
   sections array; but making sure not to leave large gaps.  Once all
7874
   possible sections have been assigned to the current segment it is
7875
   added to the list of built segments and if sections still remain
7876
   to be assigned, a new segment is constructed before repeating
7877
   the loop.  */
7878
0
      isec = 0;
7879
0
      do
7880
0
  {
7881
0
    map->count = 0;
7882
0
    suggested_lma = NULL;
7883
7884
    /* Fill the current segment with sections that fit.  */
7885
0
    for (j = 0; j < section_count; j++)
7886
0
      {
7887
0
        section = sections[j];
7888
7889
0
        if (section == NULL)
7890
0
    continue;
7891
7892
0
        output_section = section->output_section;
7893
7894
0
        BFD_ASSERT (output_section != NULL);
7895
7896
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7897
0
           0, opb, false)
7898
0
      || is_note (section, segment))
7899
0
    {
7900
0
      if (map->count == 0)
7901
0
        {
7902
          /* If the first section in a segment does not start at
7903
       the beginning of the segment, then something is
7904
       wrong.  */
7905
0
          if (align_power (map->p_paddr
7906
0
               + (map->includes_filehdr
7907
0
            ? iehdr->e_ehsize : 0)
7908
0
               + (map->includes_phdrs
7909
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7910
0
            : 0),
7911
0
               output_section->alignment_power * opb)
7912
0
        != output_section->lma * opb)
7913
0
      goto sorry;
7914
0
        }
7915
0
      else
7916
0
        {
7917
0
          asection *prev_sec;
7918
7919
0
          prev_sec = map->sections[map->count - 1];
7920
7921
          /* If the gap between the end of the previous section
7922
       and the start of this section is more than
7923
       maxpagesize then we need to start a new segment.  */
7924
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7925
0
              maxpagesize)
7926
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7927
0
        || (prev_sec->lma + prev_sec->size
7928
0
            > output_section->lma))
7929
0
      {
7930
0
        if (suggested_lma == NULL)
7931
0
          suggested_lma = output_section;
7932
7933
0
        continue;
7934
0
      }
7935
0
        }
7936
7937
0
      map->sections[map->count++] = output_section;
7938
0
      ++isec;
7939
0
      sections[j] = NULL;
7940
0
      if (segment->p_type == PT_LOAD)
7941
0
        section->segment_mark = true;
7942
0
    }
7943
0
        else if (suggested_lma == NULL)
7944
0
    suggested_lma = output_section;
7945
0
      }
7946
7947
    /* PR 23932.  A corrupt input file may contain sections that cannot
7948
       be assigned to any segment - because for example they have a
7949
       negative size - or segments that do not contain any sections.
7950
       But there are also valid reasons why a segment can be empty.
7951
       So allow a count of zero.  */
7952
7953
    /* Add the current segment to the list of built segments.  */
7954
0
    *pointer_to_map = map;
7955
0
    pointer_to_map = &map->next;
7956
7957
0
    if (isec < section_count)
7958
0
      {
7959
        /* We still have not allocated all of the sections to
7960
     segments.  Create a new segment here, initialise it
7961
     and carry on looping.  */
7962
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7963
0
        amt += section_count * sizeof (asection *);
7964
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7965
0
        if (map == NULL)
7966
0
    {
7967
0
      free (sections);
7968
0
      return false;
7969
0
    }
7970
7971
        /* Initialise the fields of the segment map.  Set the physical
7972
     physical address to the LMA of the first section that has
7973
     not yet been assigned.  */
7974
0
        map->next = NULL;
7975
0
        map->p_type = segment->p_type;
7976
0
        map->p_flags = segment->p_flags;
7977
0
        map->p_flags_valid = 1;
7978
0
        map->p_paddr = suggested_lma->lma * opb;
7979
0
        map->p_paddr_valid = p_paddr_valid;
7980
0
        map->includes_filehdr = 0;
7981
0
        map->includes_phdrs = 0;
7982
0
      }
7983
7984
0
    continue;
7985
0
  sorry:
7986
0
    bfd_set_error (bfd_error_sorry);
7987
0
    free (sections);
7988
0
    return false;
7989
0
  }
7990
0
      while (isec < section_count);
7991
7992
0
      free (sections);
7993
0
    }
7994
7995
1
  elf_seg_map (obfd) = map_first;
7996
7997
  /* If we had to estimate the number of program headers that were
7998
     going to be needed, then check our estimate now and adjust
7999
     the offset if necessary.  */
8000
1
  if (phdr_adjust_seg != NULL)
8001
0
    {
8002
0
      unsigned int count;
8003
8004
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8005
0
  count++;
8006
8007
0
      if (count > phdr_adjust_num)
8008
0
  phdr_adjust_seg->p_paddr
8009
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8010
8011
0
      for (map = map_first; map != NULL; map = map->next)
8012
0
  if (map->p_type == PT_PHDR)
8013
0
    {
8014
0
      bfd_vma adjust
8015
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8016
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8017
0
      break;
8018
0
    }
8019
0
    }
8020
8021
1
#undef IS_SOLARIS_PT_INTERP
8022
1
#undef IS_SECTION_IN_INPUT_SEGMENT
8023
1
#undef INCLUDE_SECTION_IN_SEGMENT
8024
1
#undef SEGMENT_AFTER_SEGMENT
8025
1
#undef SEGMENT_OVERLAPS
8026
1
  return true;
8027
1
}
8028
8029
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8030
8031
static bool
8032
elf_is_p_align_valid (bfd *abfd)
8033
14
{
8034
14
  unsigned int i;
8035
14
  Elf_Internal_Phdr *segment;
8036
14
  unsigned int num_segments;
8037
14
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8038
14
  bfd_size_type maxpagesize = bed->maxpagesize;
8039
14
  bfd_size_type p_align = bed->p_align;
8040
8041
  /* Return true if the default p_align value isn't set or the maximum
8042
     page size is the same as the minimum page size.  */
8043
14
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8044
14
    return true;
8045
8046
  /* When the default p_align value is set, p_align may be set to the
8047
     default p_align value while segments are aligned to the maximum
8048
     page size.  In this case, the input p_align will be ignored and
8049
     the maximum page size will be used to align the output segments.  */
8050
0
  segment = elf_tdata (abfd)->phdr;
8051
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8052
0
  for (i = 0; i < num_segments; i++, segment++)
8053
0
    if (segment->p_type == PT_LOAD
8054
0
  && (segment->p_align != p_align
8055
0
      || vma_page_aligned_bias (segment->p_vaddr,
8056
0
              segment->p_offset,
8057
0
              maxpagesize) != 0))
8058
0
      return true;
8059
8060
0
  return false;
8061
0
}
8062
8063
/* Copy ELF program header information.  */
8064
8065
static bool
8066
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8067
14
{
8068
14
  Elf_Internal_Ehdr *iehdr;
8069
14
  struct elf_segment_map *map;
8070
14
  struct elf_segment_map *map_first;
8071
14
  struct elf_segment_map **pointer_to_map;
8072
14
  Elf_Internal_Phdr *segment;
8073
14
  unsigned int i;
8074
14
  unsigned int num_segments;
8075
14
  bool phdr_included = false;
8076
14
  bool p_paddr_valid;
8077
14
  bool p_palign_valid;
8078
14
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8079
8080
14
  iehdr = elf_elfheader (ibfd);
8081
8082
14
  map_first = NULL;
8083
14
  pointer_to_map = &map_first;
8084
8085
  /* If all the segment p_paddr fields are zero, don't set
8086
     map->p_paddr_valid.  */
8087
14
  p_paddr_valid = false;
8088
14
  num_segments = elf_elfheader (ibfd)->e_phnum;
8089
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8090
15
       i < num_segments;
8091
14
       i++, segment++)
8092
15
    if (segment->p_paddr != 0)
8093
14
      {
8094
14
  p_paddr_valid = true;
8095
14
  break;
8096
14
      }
8097
8098
14
  p_palign_valid = elf_is_p_align_valid (ibfd);
8099
8100
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8101
110
       i < num_segments;
8102
96
       i++, segment++)
8103
96
    {
8104
96
      asection *section;
8105
96
      unsigned int section_count;
8106
96
      size_t amt;
8107
96
      Elf_Internal_Shdr *this_hdr;
8108
96
      asection *first_section = NULL;
8109
96
      asection *lowest_section;
8110
8111
      /* Compute how many sections are in this segment.  */
8112
96
      for (section = ibfd->sections, section_count = 0;
8113
2.47k
     section != NULL;
8114
2.37k
     section = section->next)
8115
2.37k
  {
8116
2.37k
    this_hdr = &(elf_section_data(section)->this_hdr);
8117
2.37k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8118
390
      {
8119
390
        if (first_section == NULL)
8120
75
    first_section = section;
8121
390
        section_count++;
8122
390
      }
8123
2.37k
  }
8124
8125
      /* Allocate a segment map big enough to contain
8126
   all of the sections we have selected.  */
8127
96
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8128
96
      amt += section_count * sizeof (asection *);
8129
96
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8130
96
      if (map == NULL)
8131
0
  return false;
8132
8133
      /* Initialize the fields of the output segment map with the
8134
   input segment.  */
8135
96
      map->next = NULL;
8136
96
      map->p_type = segment->p_type;
8137
96
      map->p_flags = segment->p_flags;
8138
96
      map->p_flags_valid = 1;
8139
96
      map->p_paddr = segment->p_paddr;
8140
96
      map->p_paddr_valid = p_paddr_valid;
8141
96
      map->p_align = segment->p_align;
8142
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8143
96
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8144
96
          || p_palign_valid);
8145
96
      map->p_vaddr_offset = 0;
8146
8147
96
      if (map->p_type == PT_GNU_RELRO
8148
96
    || map->p_type == PT_GNU_STACK)
8149
11
  {
8150
    /* The PT_GNU_RELRO segment may contain the first a few
8151
       bytes in the .got.plt section even if the whole .got.plt
8152
       section isn't in the PT_GNU_RELRO segment.  We won't
8153
       change the size of the PT_GNU_RELRO segment.
8154
       Similarly, PT_GNU_STACK size is significant on uclinux
8155
       systems.    */
8156
11
    map->p_size = segment->p_memsz;
8157
11
    map->p_size_valid = 1;
8158
11
  }
8159
8160
      /* Determine if this segment contains the ELF file header
8161
   and if it contains the program headers themselves.  */
8162
96
      map->includes_filehdr = (segment->p_offset == 0
8163
96
             && segment->p_filesz >= iehdr->e_ehsize);
8164
8165
96
      map->includes_phdrs = 0;
8166
96
      if (! phdr_included || segment->p_type != PT_LOAD)
8167
81
  {
8168
81
    map->includes_phdrs =
8169
81
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8170
81
       && (segment->p_offset + segment->p_filesz
8171
35
     >= ((bfd_vma) iehdr->e_phoff
8172
35
         + iehdr->e_phnum * iehdr->e_phentsize)));
8173
8174
81
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8175
14
      phdr_included = true;
8176
81
  }
8177
8178
96
      lowest_section = NULL;
8179
96
      if (section_count != 0)
8180
75
  {
8181
75
    unsigned int isec = 0;
8182
8183
75
    for (section = first_section;
8184
390
         section != NULL;
8185
315
         section = section->next)
8186
390
      {
8187
390
        this_hdr = &(elf_section_data(section)->this_hdr);
8188
390
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8189
390
    {
8190
390
      map->sections[isec++] = section->output_section;
8191
390
      if ((section->flags & SEC_ALLOC) != 0)
8192
390
        {
8193
390
          bfd_vma seg_off;
8194
8195
390
          if (lowest_section == NULL
8196
390
        || section->lma < lowest_section->lma)
8197
75
      lowest_section = section;
8198
8199
          /* Section lmas are set up from PT_LOAD header
8200
       p_paddr in _bfd_elf_make_section_from_shdr.
8201
       If this header has a p_paddr that disagrees
8202
       with the section lma, flag the p_paddr as
8203
       invalid.  */
8204
390
          if ((section->flags & SEC_LOAD) != 0)
8205
374
      seg_off = this_hdr->sh_offset - segment->p_offset;
8206
16
          else
8207
16
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8208
390
          if (section->lma * opb - segment->p_paddr != seg_off)
8209
0
      map->p_paddr_valid = false;
8210
390
        }
8211
390
      if (isec == section_count)
8212
75
        break;
8213
390
    }
8214
390
      }
8215
75
  }
8216
8217
96
      if (section_count == 0)
8218
21
  map->p_vaddr_offset = segment->p_vaddr / opb;
8219
75
      else if (map->p_paddr_valid)
8220
75
  {
8221
    /* Account for padding before the first section in the segment.  */
8222
75
    bfd_vma hdr_size = 0;
8223
75
    if (map->includes_filehdr)
8224
14
      hdr_size = iehdr->e_ehsize;
8225
75
    if (map->includes_phdrs)
8226
14
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8227
8228
75
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8229
75
         - (lowest_section ? lowest_section->lma : 0));
8230
75
  }
8231
8232
96
      map->count = section_count;
8233
96
      *pointer_to_map = map;
8234
96
      pointer_to_map = &map->next;
8235
96
    }
8236
8237
14
  elf_seg_map (obfd) = map_first;
8238
14
  return true;
8239
14
}
8240
8241
/* Copy private BFD data.  This copies or rewrites ELF program header
8242
   information.  */
8243
8244
static bool
8245
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8246
15
{
8247
15
  bfd_vma maxpagesize;
8248
8249
15
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8250
15
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8251
0
    return true;
8252
8253
15
  if (elf_tdata (ibfd)->phdr == NULL)
8254
0
    return true;
8255
8256
15
  if (ibfd->xvec == obfd->xvec)
8257
15
    {
8258
      /* Check to see if any sections in the input BFD
8259
   covered by ELF program header have changed.  */
8260
15
      Elf_Internal_Phdr *segment;
8261
15
      asection * section;
8262
15
      asection * osec;
8263
15
      asection * prev;
8264
15
      unsigned int i, num_segments;
8265
15
      Elf_Internal_Shdr *this_hdr;
8266
15
      const struct elf_backend_data *bed;
8267
8268
15
      bed = get_elf_backend_data (ibfd);
8269
8270
      /* Regenerate the segment map if p_paddr is set to 0.  */
8271
15
      if (bed->want_p_paddr_set_to_zero)
8272
0
  goto rewrite;
8273
8274
      /* Initialize the segment mark field.  */
8275
392
      for (section = obfd->sections; section != NULL;
8276
377
     section = section->next)
8277
377
  section->segment_mark = false;
8278
8279
15
      num_segments = elf_elfheader (ibfd)->e_phnum;
8280
15
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8281
112
     i < num_segments;
8282
97
     i++, segment++)
8283
98
  {
8284
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8285
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8286
       which severly confuses things, so always regenerate the segment
8287
       map in this case.  */
8288
98
    if (segment->p_paddr == 0
8289
98
        && segment->p_memsz == 0
8290
98
        && (segment->p_type == PT_INTERP
8291
9
      || segment->p_type == PT_DYNAMIC))
8292
1
      goto rewrite;
8293
8294
97
    for (section = ibfd->sections, prev = NULL;
8295
2.50k
         section != NULL; section = section->next)
8296
2.41k
      {
8297
        /* We mark the output section so that we know it comes
8298
     from the input BFD.  */
8299
2.41k
        osec = section->output_section;
8300
2.41k
        if (osec)
8301
2.41k
    osec->segment_mark = true;
8302
8303
        /* Check if this section is covered by the segment.  */
8304
2.41k
        this_hdr = &(elf_section_data(section)->this_hdr);
8305
2.41k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8306
390
    {
8307
      /* FIXME: Check if its output section is changed or
8308
         removed.  What else do we need to check?  */
8309
390
      if (osec == NULL
8310
390
          || section->flags != osec->flags
8311
390
          || section->lma != osec->lma
8312
390
          || section->vma != osec->vma
8313
390
          || section->size != osec->size
8314
390
          || section->rawsize != osec->rawsize
8315
390
          || section->alignment_power != osec->alignment_power)
8316
0
        goto rewrite;
8317
8318
      /* PR 31450: If this is an allocated section then make sure
8319
         that this section's vma to lma relationship is the same
8320
         as previous (allocated) section's.  */
8321
390
      if (prev != NULL
8322
390
          && section->flags & SEC_ALLOC
8323
390
          && section->lma - section->vma != prev->lma - prev->vma)
8324
0
        goto rewrite;
8325
8326
390
      if (section->flags & SEC_ALLOC)
8327
390
        prev = section;
8328
390
    }
8329
2.41k
      }
8330
97
  }
8331
8332
      /* Check to see if any output section do not come from the
8333
   input BFD.  */
8334
357
      for (section = obfd->sections; section != NULL;
8335
343
     section = section->next)
8336
343
  {
8337
343
    if (!section->segment_mark)
8338
0
      goto rewrite;
8339
343
    else
8340
343
      section->segment_mark = false;
8341
343
  }
8342
8343
14
      return copy_elf_program_header (ibfd, obfd);
8344
14
    }
8345
8346
1
 rewrite:
8347
1
  maxpagesize = 0;
8348
1
  if (ibfd->xvec == obfd->xvec)
8349
1
    {
8350
      /* When rewriting program header, set the output maxpagesize to
8351
   the maximum alignment of input PT_LOAD segments.  */
8352
1
      Elf_Internal_Phdr *segment;
8353
1
      unsigned int i;
8354
1
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8355
8356
1
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8357
6
     i < num_segments;
8358
5
     i++, segment++)
8359
5
  if (segment->p_type == PT_LOAD
8360
5
      && maxpagesize < segment->p_align)
8361
1
    {
8362
      /* PR 17512: file: f17299af.  */
8363
1
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8364
        /* xgettext:c-format */
8365
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8366
0
            PRIx64 " is too large"),
8367
0
          ibfd, (uint64_t) segment->p_align);
8368
1
      else
8369
1
        maxpagesize = segment->p_align;
8370
1
    }
8371
1
    }
8372
1
  if (maxpagesize == 0)
8373
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8374
8375
1
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8376
15
}
8377
8378
/* Initialize private output section information from input section.  */
8379
8380
bool
8381
_bfd_elf_init_private_section_data (bfd *ibfd,
8382
            asection *isec,
8383
            bfd *obfd,
8384
            asection *osec,
8385
            struct bfd_link_info *link_info)
8386
8387
3.13k
{
8388
3.13k
  Elf_Internal_Shdr *ihdr, *ohdr;
8389
3.13k
  bool final_link = (link_info != NULL
8390
3.13k
         && !bfd_link_relocatable (link_info));
8391
8392
3.13k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8393
3.13k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8394
0
    return true;
8395
8396
3.13k
  BFD_ASSERT (elf_section_data (osec) != NULL);
8397
8398
  /* If this is a known ABI section, ELF section type and flags may
8399
     have been set up when OSEC was created.  For normal sections we
8400
     allow the user to override the type and flags other than
8401
     SHF_MASKOS and SHF_MASKPROC.  */
8402
3.13k
  if (elf_section_type (osec) == SHT_PROGBITS
8403
3.13k
      || elf_section_type (osec) == SHT_NOTE
8404
3.13k
      || elf_section_type (osec) == SHT_NOBITS)
8405
1.17k
    elf_section_type (osec) = SHT_NULL;
8406
  /* For objcopy and relocatable link, copy the ELF section type from
8407
     the input file if the BFD section flags are the same.  (If they
8408
     are different the user may be doing something like
8409
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8410
     link allow some flags that the linker clears to differ.  */
8411
3.13k
  if (elf_section_type (osec) == SHT_NULL
8412
3.13k
      && (osec->flags == isec->flags
8413
2.91k
    || (final_link
8414
0
        && ((osec->flags ^ isec->flags)
8415
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8416
2.91k
    elf_section_type (osec) = elf_section_type (isec);
8417
8418
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
8419
3.13k
  elf_section_flags (osec) = (elf_section_flags (isec)
8420
3.13k
            & (SHF_MASKOS | SHF_MASKPROC));
8421
8422
  /* Copy sh_info from input for mbind section.  */
8423
3.13k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8424
3.13k
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8425
0
    elf_section_data (osec)->this_hdr.sh_info
8426
0
      = elf_section_data (isec)->this_hdr.sh_info;
8427
8428
  /* Set things up for objcopy and relocatable link.  The output
8429
     SHT_GROUP section will have its elf_next_in_group pointing back
8430
     to the input group members.  Ignore linker created group section.
8431
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8432
3.13k
  if ((link_info == NULL
8433
3.13k
       || !link_info->resolve_section_groups)
8434
3.13k
      && (elf_sec_group (isec) == NULL
8435
3.13k
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8436
3.13k
    {
8437
3.13k
      if (elf_section_flags (isec) & SHF_GROUP)
8438
1.74k
  elf_section_flags (osec) |= SHF_GROUP;
8439
3.13k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8440
3.13k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8441
3.13k
    }
8442
8443
  /* If not decompress, preserve SHF_COMPRESSED.  */
8444
3.13k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8445
3.13k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8446
3.13k
         & SHF_COMPRESSED);
8447
8448
3.13k
  ihdr = &elf_section_data (isec)->this_hdr;
8449
8450
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8451
     don't use the output section of the linked-to section since it
8452
     may be NULL at this point.  */
8453
3.13k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8454
1
    {
8455
1
      ohdr = &elf_section_data (osec)->this_hdr;
8456
1
      ohdr->sh_flags |= SHF_LINK_ORDER;
8457
1
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8458
1
    }
8459
8460
3.13k
  osec->use_rela_p = isec->use_rela_p;
8461
8462
3.13k
  return true;
8463
3.13k
}
8464
8465
/* Copy private section information.  This copies over the entsize
8466
   field, and sometimes the info field.  */
8467
8468
bool
8469
_bfd_elf_copy_private_section_data (bfd *ibfd,
8470
            asection *isec,
8471
            bfd *obfd,
8472
            asection *osec)
8473
3.13k
{
8474
3.13k
  Elf_Internal_Shdr *ihdr, *ohdr;
8475
8476
3.13k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8477
3.13k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8478
0
    return true;
8479
8480
3.13k
  ihdr = &elf_section_data (isec)->this_hdr;
8481
3.13k
  ohdr = &elf_section_data (osec)->this_hdr;
8482
8483
3.13k
  ohdr->sh_entsize = ihdr->sh_entsize;
8484
8485
3.13k
  if (ihdr->sh_type == SHT_SYMTAB
8486
3.13k
      || ihdr->sh_type == SHT_DYNSYM
8487
3.13k
      || ihdr->sh_type == SHT_GNU_verneed
8488
3.13k
      || ihdr->sh_type == SHT_GNU_verdef)
8489
29
    ohdr->sh_info = ihdr->sh_info;
8490
8491
3.13k
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8492
3.13k
               NULL);
8493
3.13k
}
8494
8495
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8496
   necessary if we are removing either the SHT_GROUP section or any of
8497
   the group member sections.  DISCARDED is the value that a section's
8498
   output_section has if the section will be discarded, NULL when this
8499
   function is called from objcopy, bfd_abs_section_ptr when called
8500
   from the linker.  */
8501
8502
bool
8503
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8504
49
{
8505
49
  asection *isec;
8506
8507
3.29k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8508
3.24k
    if (elf_section_type (isec) == SHT_GROUP)
8509
569
      {
8510
569
  asection *first = elf_next_in_group (isec);
8511
569
  asection *s = first;
8512
569
  bfd_size_type removed = 0;
8513
8514
1.50k
  while (s != NULL)
8515
1.38k
    {
8516
      /* If this member section is being output but the
8517
         SHT_GROUP section is not, then clear the group info
8518
         set up by _bfd_elf_copy_private_section_data.  */
8519
1.38k
      if (s->output_section != discarded
8520
1.38k
    && isec->output_section == discarded)
8521
0
        {
8522
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8523
0
    elf_group_name (s->output_section) = NULL;
8524
0
        }
8525
1.38k
      else
8526
1.38k
        {
8527
1.38k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8528
1.38k
    if (s->output_section == discarded
8529
1.38k
        && isec->output_section != discarded)
8530
0
      {
8531
        /* Conversely, if the member section is not being
8532
           output but the SHT_GROUP section is, then adjust
8533
           its size.  */
8534
0
        removed += 4;
8535
0
        if (elf_sec->rel.hdr != NULL
8536
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8537
0
          removed += 4;
8538
0
        if (elf_sec->rela.hdr != NULL
8539
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8540
0
          removed += 4;
8541
0
      }
8542
1.38k
    else
8543
1.38k
      {
8544
        /* Also adjust for zero-sized relocation member
8545
           section.  */
8546
1.38k
        if (elf_sec->rel.hdr != NULL
8547
1.38k
      && elf_sec->rel.hdr->sh_size == 0)
8548
0
          removed += 4;
8549
1.38k
        if (elf_sec->rela.hdr != NULL
8550
1.38k
      && elf_sec->rela.hdr->sh_size == 0)
8551
0
          removed += 4;
8552
1.38k
      }
8553
1.38k
        }
8554
1.38k
      s = elf_next_in_group (s);
8555
1.38k
      if (s == first)
8556
455
        break;
8557
1.38k
    }
8558
569
  if (removed != 0)
8559
0
    {
8560
0
      if (discarded != NULL)
8561
0
        {
8562
    /* If we've been called for ld -r, then we need to
8563
       adjust the input section size.  */
8564
0
    if (isec->rawsize == 0)
8565
0
      isec->rawsize = isec->size;
8566
0
    isec->size = isec->rawsize - removed;
8567
0
    if (isec->size <= 4)
8568
0
      {
8569
0
        isec->size = 0;
8570
0
        isec->flags |= SEC_EXCLUDE;
8571
0
      }
8572
0
        }
8573
0
      else if (isec->output_section != NULL)
8574
0
        {
8575
    /* Adjust the output section size when called from
8576
       objcopy. */
8577
0
    isec->output_section->size -= removed;
8578
0
    if (isec->output_section->size <= 4)
8579
0
      {
8580
0
        isec->output_section->size = 0;
8581
0
        isec->output_section->flags |= SEC_EXCLUDE;
8582
0
      }
8583
0
        }
8584
0
    }
8585
569
      }
8586
8587
49
  return true;
8588
49
}
8589
8590
/* Copy private header information.  */
8591
8592
bool
8593
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8594
49
{
8595
49
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8596
49
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8597
0
    return true;
8598
8599
  /* Copy over private BFD data if it has not already been copied.
8600
     This must be done here, rather than in the copy_private_bfd_data
8601
     entry point, because the latter is called after the section
8602
     contents have been set, which means that the program headers have
8603
     already been worked out.  */
8604
49
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8605
15
    {
8606
15
      if (! copy_private_bfd_data (ibfd, obfd))
8607
0
  return false;
8608
15
    }
8609
8610
49
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8611
49
}
8612
8613
/* Copy private symbol information.  If this symbol is in a section
8614
   which we did not map into a BFD section, try to map the section
8615
   index correctly.  We use special macro definitions for the mapped
8616
   section indices; these definitions are interpreted by the
8617
   swap_out_syms function.  */
8618
8619
2
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8620
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8621
79
#define MAP_STRTAB    (SHN_HIOS + 3)
8622
2
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8623
0
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8624
8625
bool
8626
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8627
           asymbol *isymarg,
8628
           bfd *obfd,
8629
           asymbol *osymarg)
8630
38.7k
{
8631
38.7k
  elf_symbol_type *isym, *osym;
8632
8633
38.7k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8634
38.7k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8635
0
    return true;
8636
8637
38.7k
  isym = elf_symbol_from (isymarg);
8638
38.7k
  osym = elf_symbol_from (osymarg);
8639
8640
38.7k
  if (isym != NULL
8641
38.7k
      && isym->internal_elf_sym.st_shndx != 0
8642
38.7k
      && osym != NULL
8643
38.7k
      && bfd_is_abs_section (isym->symbol.section))
8644
1.68k
    {
8645
1.68k
      unsigned int shndx;
8646
8647
1.68k
      shndx = isym->internal_elf_sym.st_shndx;
8648
1.68k
      if (shndx == elf_onesymtab (ibfd))
8649
1
  shndx = MAP_ONESYMTAB;
8650
1.68k
      else if (shndx == elf_dynsymtab (ibfd))
8651
0
  shndx = MAP_DYNSYMTAB;
8652
1.68k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8653
74
  shndx = MAP_STRTAB;
8654
1.61k
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8655
1
  shndx = MAP_SHSTRTAB;
8656
1.60k
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8657
0
  shndx = MAP_SYM_SHNDX;
8658
1.68k
      osym->internal_elf_sym.st_shndx = shndx;
8659
1.68k
    }
8660
8661
38.7k
  return true;
8662
38.7k
}
8663
8664
/* Swap out the symbols.  */
8665
8666
static bool
8667
swap_out_syms (bfd *abfd,
8668
         struct elf_strtab_hash **sttp,
8669
         int relocatable_p,
8670
         struct bfd_link_info *info)
8671
40
{
8672
40
  const struct elf_backend_data *bed;
8673
40
  unsigned int symcount;
8674
40
  asymbol **syms;
8675
40
  struct elf_strtab_hash *stt;
8676
40
  Elf_Internal_Shdr *symtab_hdr;
8677
40
  Elf_Internal_Shdr *symtab_shndx_hdr;
8678
40
  Elf_Internal_Shdr *symstrtab_hdr;
8679
40
  struct elf_sym_strtab *symstrtab;
8680
40
  bfd_byte *outbound_syms;
8681
40
  bfd_byte *outbound_shndx;
8682
40
  unsigned long outbound_syms_index;
8683
40
  unsigned int idx;
8684
40
  unsigned int num_locals;
8685
40
  size_t amt;
8686
40
  bool name_local_sections;
8687
8688
40
  if (!elf_map_symbols (abfd, &num_locals))
8689
0
    return false;
8690
8691
  /* Dump out the symtabs.  */
8692
40
  stt = _bfd_elf_strtab_init ();
8693
40
  if (stt == NULL)
8694
0
    return false;
8695
8696
40
  bed = get_elf_backend_data (abfd);
8697
40
  symcount = bfd_get_symcount (abfd);
8698
40
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8699
40
  symtab_hdr->sh_type = SHT_SYMTAB;
8700
40
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8701
40
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8702
40
  symtab_hdr->sh_info = num_locals + 1;
8703
40
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8704
8705
40
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8706
40
  symstrtab_hdr->sh_type = SHT_STRTAB;
8707
8708
  /* Allocate buffer to swap out the .strtab section.  */
8709
40
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8710
40
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8711
0
    {
8712
0
      bfd_set_error (bfd_error_no_memory);
8713
0
      _bfd_elf_strtab_free (stt);
8714
0
      return false;
8715
0
    }
8716
8717
40
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8718
40
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8719
0
    {
8720
0
    error_no_mem:
8721
0
      bfd_set_error (bfd_error_no_memory);
8722
0
    error_return:
8723
0
      free (symstrtab);
8724
0
      _bfd_elf_strtab_free (stt);
8725
0
      return false;
8726
0
    }
8727
40
  symtab_hdr->contents = outbound_syms;
8728
40
  outbound_syms_index = 0;
8729
8730
40
  outbound_shndx = NULL;
8731
8732
40
  if (elf_symtab_shndx_list (abfd))
8733
0
    {
8734
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8735
0
      if (symtab_shndx_hdr->sh_name != 0)
8736
0
  {
8737
0
    if (_bfd_mul_overflow (symcount + 1,
8738
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8739
0
      goto error_no_mem;
8740
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8741
0
    if (outbound_shndx == NULL)
8742
0
      goto error_return;
8743
8744
0
    symtab_shndx_hdr->contents = outbound_shndx;
8745
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8746
0
    symtab_shndx_hdr->sh_size = amt;
8747
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8748
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8749
0
  }
8750
      /* FIXME: What about any other headers in the list ?  */
8751
0
    }
8752
8753
  /* Now generate the data (for "contents").  */
8754
40
  {
8755
    /* Fill in zeroth symbol and swap it out.  */
8756
40
    Elf_Internal_Sym sym;
8757
40
    sym.st_name = 0;
8758
40
    sym.st_value = 0;
8759
40
    sym.st_size = 0;
8760
40
    sym.st_info = 0;
8761
40
    sym.st_other = 0;
8762
40
    sym.st_shndx = SHN_UNDEF;
8763
40
    sym.st_target_internal = 0;
8764
40
    symstrtab[outbound_syms_index].sym = sym;
8765
40
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8766
40
    outbound_syms_index++;
8767
40
  }
8768
8769
40
  name_local_sections
8770
40
    = (bed->elf_backend_name_local_section_symbols
8771
40
       && bed->elf_backend_name_local_section_symbols (abfd));
8772
8773
40
  syms = bfd_get_outsymbols (abfd);
8774
14.6k
  for (idx = 0; idx < symcount; idx++)
8775
14.6k
    {
8776
14.6k
      Elf_Internal_Sym sym;
8777
8778
14.6k
      flagword flags = syms[idx]->flags;
8779
14.6k
      if (!name_local_sections
8780
14.6k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8781
1.64k
  {
8782
    /* Local section symbols have no name.  */
8783
1.64k
    sym.st_name = 0;
8784
1.64k
  }
8785
12.9k
      else
8786
12.9k
  {
8787
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8788
       to get the final offset for st_name.  */
8789
12.9k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8790
12.9k
    if (stridx == (size_t) -1)
8791
0
      goto error_return;
8792
12.9k
    sym.st_name = stridx;
8793
12.9k
  }
8794
8795
14.6k
      bfd_vma value = syms[idx]->value;
8796
14.6k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8797
14.6k
      asection *sec = syms[idx]->section;
8798
8799
14.6k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8800
0
  {
8801
    /* ELF common symbols put the alignment into the `value' field,
8802
       and the size into the `size' field.  This is backwards from
8803
       how BFD handles it, so reverse it here.  */
8804
0
    sym.st_size = value;
8805
0
    if (type_ptr == NULL
8806
0
        || type_ptr->internal_elf_sym.st_value == 0)
8807
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8808
0
    else
8809
0
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8810
0
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8811
0
  }
8812
14.6k
      else
8813
14.6k
  {
8814
14.6k
    unsigned int shndx;
8815
8816
14.6k
    if (sec->output_section)
8817
14.3k
      {
8818
14.3k
        value += sec->output_offset;
8819
14.3k
        sec = sec->output_section;
8820
14.3k
      }
8821
8822
    /* Don't add in the section vma for relocatable output.  */
8823
14.6k
    if (! relocatable_p)
8824
7.51k
      value += sec->vma;
8825
14.6k
    sym.st_value = value;
8826
14.6k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8827
8828
14.6k
    if (bfd_is_abs_section (sec)
8829
14.6k
        && type_ptr != NULL
8830
14.6k
        && type_ptr->internal_elf_sym.st_shndx != 0)
8831
1.39k
      {
8832
        /* This symbol is in a real ELF section which we did
8833
     not create as a BFD section.  Undo the mapping done
8834
     by copy_private_symbol_data.  */
8835
1.39k
        shndx = type_ptr->internal_elf_sym.st_shndx;
8836
1.39k
        switch (shndx)
8837
1.39k
    {
8838
1
    case MAP_ONESYMTAB:
8839
1
      shndx = elf_onesymtab (abfd);
8840
1
      break;
8841
0
    case MAP_DYNSYMTAB:
8842
0
      shndx = elf_dynsymtab (abfd);
8843
0
      break;
8844
5
    case MAP_STRTAB:
8845
5
      shndx = elf_strtab_sec (abfd);
8846
5
      break;
8847
1
    case MAP_SHSTRTAB:
8848
1
      shndx = elf_shstrtab_sec (abfd);
8849
1
      break;
8850
0
    case MAP_SYM_SHNDX:
8851
0
      if (elf_symtab_shndx_list (abfd))
8852
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8853
0
      break;
8854
0
    case SHN_COMMON:
8855
221
    case SHN_ABS:
8856
221
      shndx = SHN_ABS;
8857
221
      break;
8858
1.17k
    default:
8859
1.17k
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8860
1
        {
8861
1
          if (bed->symbol_section_index)
8862
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8863
          /* Otherwise just leave the index alone.  */
8864
1
        }
8865
1.17k
      else
8866
1.17k
        {
8867
1.17k
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8868
1
      _bfd_error_handler (_("%pB: \
8869
1
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8870
1
            abfd, shndx);
8871
1.17k
          shndx = SHN_ABS;
8872
1.17k
        }
8873
1.17k
      break;
8874
1.39k
    }
8875
1.39k
      }
8876
13.2k
    else
8877
13.2k
      {
8878
13.2k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8879
8880
13.2k
        if (shndx == SHN_BAD)
8881
0
    {
8882
0
      asection *sec2;
8883
8884
      /* Writing this would be a hell of a lot easier if
8885
         we had some decent documentation on bfd, and
8886
         knew what to expect of the library, and what to
8887
         demand of applications.  For example, it
8888
         appears that `objcopy' might not set the
8889
         section of a symbol to be a section that is
8890
         actually in the output file.  */
8891
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8892
0
      if (sec2 != NULL)
8893
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8894
0
      if (shndx == SHN_BAD)
8895
0
        {
8896
          /* xgettext:c-format */
8897
0
          _bfd_error_handler
8898
0
      (_("unable to find equivalent output section"
8899
0
         " for symbol '%s' from section '%s'"),
8900
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8901
0
       sec->name);
8902
0
          bfd_set_error (bfd_error_invalid_operation);
8903
0
          goto error_return;
8904
0
        }
8905
0
    }
8906
13.2k
      }
8907
8908
14.6k
    sym.st_shndx = shndx;
8909
14.6k
  }
8910
8911
14.6k
      int type;
8912
14.6k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8913
20
  type = STT_TLS;
8914
14.6k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8915
94
  type = STT_GNU_IFUNC;
8916
14.5k
      else if ((flags & BSF_FUNCTION) != 0)
8917
5.08k
  type = STT_FUNC;
8918
9.42k
      else if ((flags & BSF_OBJECT) != 0)
8919
3.86k
  type = STT_OBJECT;
8920
5.55k
      else if ((flags & BSF_RELC) != 0)
8921
1
  type = STT_RELC;
8922
5.55k
      else if ((flags & BSF_SRELC) != 0)
8923
2
  type = STT_SRELC;
8924
5.55k
      else
8925
5.55k
  type = STT_NOTYPE;
8926
8927
14.6k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8928
0
  type = STT_TLS;
8929
8930
      /* Processor-specific types.  */
8931
14.6k
      if (type_ptr != NULL
8932
14.6k
    && bed->elf_backend_get_symbol_type)
8933
0
  type = ((*bed->elf_backend_get_symbol_type)
8934
0
    (&type_ptr->internal_elf_sym, type));
8935
8936
14.6k
      if (flags & BSF_SECTION_SYM)
8937
1.64k
  {
8938
1.64k
    if (flags & BSF_GLOBAL)
8939
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8940
1.64k
    else
8941
1.64k
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8942
1.64k
  }
8943
12.9k
      else if (bfd_is_com_section (syms[idx]->section))
8944
0
  {
8945
0
    if (type != STT_TLS)
8946
0
      {
8947
0
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8948
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8949
0
      ? STT_COMMON : STT_OBJECT);
8950
0
        else
8951
0
    type = ((flags & BSF_ELF_COMMON) != 0
8952
0
      ? STT_COMMON : STT_OBJECT);
8953
0
      }
8954
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8955
0
  }
8956
12.9k
      else if (bfd_is_und_section (syms[idx]->section))
8957
3.64k
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8958
12.9k
            ? STB_WEAK
8959
12.9k
            : STB_GLOBAL),
8960
12.9k
           type);
8961
9.32k
      else if (flags & BSF_FILE)
8962
206
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8963
9.12k
      else
8964
9.12k
  {
8965
9.12k
    int bind = STB_LOCAL;
8966
8967
9.12k
    if (flags & BSF_LOCAL)
8968
6.63k
      bind = STB_LOCAL;
8969
2.48k
    else if (flags & BSF_GNU_UNIQUE)
8970
0
      bind = STB_GNU_UNIQUE;
8971
2.48k
    else if (flags & BSF_WEAK)
8972
455
      bind = STB_WEAK;
8973
2.02k
    else if (flags & BSF_GLOBAL)
8974
2.02k
      bind = STB_GLOBAL;
8975
8976
9.12k
    sym.st_info = ELF_ST_INFO (bind, type);
8977
9.12k
  }
8978
8979
14.6k
      if (type_ptr != NULL)
8980
14.6k
  {
8981
14.6k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
8982
14.6k
    sym.st_target_internal
8983
14.6k
      = type_ptr->internal_elf_sym.st_target_internal;
8984
14.6k
  }
8985
0
      else
8986
0
  {
8987
0
    sym.st_other = 0;
8988
0
    sym.st_target_internal = 0;
8989
0
  }
8990
8991
14.6k
      symstrtab[outbound_syms_index].sym = sym;
8992
14.6k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8993
14.6k
      outbound_syms_index++;
8994
14.6k
    }
8995
8996
  /* Finalize the .strtab section.  */
8997
40
  _bfd_elf_strtab_finalize (stt);
8998
8999
  /* Swap out the .strtab section.  */
9000
14.7k
  for (idx = 0; idx < outbound_syms_index; idx++)
9001
14.6k
    {
9002
14.6k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9003
14.6k
      if (elfsym->sym.st_name != 0)
9004
10.5k
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9005
10.5k
                  elfsym->sym.st_name);
9006
14.6k
      if (info && info->callbacks->ctf_new_symbol)
9007
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9008
0
           &elfsym->sym);
9009
9010
      /* Inform the linker of the addition of this symbol.  */
9011
9012
14.6k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9013
14.6k
             (outbound_syms
9014
14.6k
        + (elfsym->dest_index
9015
14.6k
           * bed->s->sizeof_sym)),
9016
14.6k
             NPTR_ADD (outbound_shndx,
9017
14.6k
           (elfsym->dest_index
9018
14.6k
            * sizeof (Elf_External_Sym_Shndx))));
9019
14.6k
    }
9020
40
  free (symstrtab);
9021
9022
40
  *sttp = stt;
9023
40
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9024
40
  symstrtab_hdr->sh_type = SHT_STRTAB;
9025
40
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9026
40
  symstrtab_hdr->sh_addr = 0;
9027
40
  symstrtab_hdr->sh_entsize = 0;
9028
40
  symstrtab_hdr->sh_link = 0;
9029
40
  symstrtab_hdr->sh_info = 0;
9030
40
  symstrtab_hdr->sh_addralign = 1;
9031
9032
40
  return true;
9033
40
}
9034
9035
/* Return the number of bytes required to hold the symtab vector.
9036
9037
   Note that we base it on the count plus 1, since we will null terminate
9038
   the vector allocated based on this size.  However, the ELF symbol table
9039
   always has a dummy entry as symbol #0, so it ends up even.  */
9040
9041
long
9042
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9043
5.56k
{
9044
5.56k
  bfd_size_type symcount;
9045
5.56k
  long symtab_size;
9046
5.56k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9047
9048
5.56k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9049
5.56k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9050
0
    {
9051
0
      bfd_set_error (bfd_error_file_too_big);
9052
0
      return -1;
9053
0
    }
9054
5.56k
  symtab_size = symcount * (sizeof (asymbol *));
9055
5.56k
  if (symcount == 0)
9056
433
    symtab_size = sizeof (asymbol *);
9057
5.13k
  else if (!bfd_write_p (abfd))
9058
5.13k
    {
9059
5.13k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9060
9061
5.13k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9062
221
  {
9063
221
    bfd_set_error (bfd_error_file_truncated);
9064
221
    return -1;
9065
221
  }
9066
5.13k
    }
9067
9068
5.34k
  return symtab_size;
9069
5.56k
}
9070
9071
long
9072
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9073
6.07k
{
9074
6.07k
  bfd_size_type symcount;
9075
6.07k
  long symtab_size;
9076
6.07k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9077
9078
6.07k
  if (elf_dynsymtab (abfd) == 0)
9079
5.39k
    {
9080
      /* Check if there is dynamic symbol table.  */
9081
5.39k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9082
5.39k
      if (symcount)
9083
6
  goto compute_symtab_size;
9084
9085
5.38k
      bfd_set_error (bfd_error_invalid_operation);
9086
5.38k
      return -1;
9087
5.39k
    }
9088
9089
678
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9090
678
  if (symcount > LONG_MAX / sizeof (asymbol *))
9091
0
    {
9092
0
      bfd_set_error (bfd_error_file_too_big);
9093
0
      return -1;
9094
0
    }
9095
9096
684
 compute_symtab_size:
9097
684
  symtab_size = symcount * (sizeof (asymbol *));
9098
684
  if (symcount == 0)
9099
11
    symtab_size = sizeof (asymbol *);
9100
673
  else if (!bfd_write_p (abfd))
9101
673
    {
9102
673
      ufile_ptr filesize = bfd_get_file_size (abfd);
9103
9104
673
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9105
18
  {
9106
18
    bfd_set_error (bfd_error_file_truncated);
9107
18
    return -1;
9108
18
  }
9109
673
    }
9110
9111
666
  return symtab_size;
9112
684
}
9113
9114
long
9115
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9116
22.9k
{
9117
22.9k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9118
21.4k
    {
9119
      /* Sanity check reloc section size.  */
9120
21.4k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9121
9122
21.4k
      if (filesize != 0)
9123
21.4k
  {
9124
21.4k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9125
21.4k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9126
21.4k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9127
9128
21.4k
    if (rel_size + rela_size > filesize
9129
21.4k
        || rel_size + rela_size < rel_size)
9130
331
      {
9131
331
        bfd_set_error (bfd_error_file_truncated);
9132
331
        return -1;
9133
331
      }
9134
21.4k
  }
9135
21.4k
    }
9136
9137
#if SIZEOF_LONG == SIZEOF_INT
9138
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9139
    {
9140
      bfd_set_error (bfd_error_file_too_big);
9141
      return -1;
9142
    }
9143
#endif
9144
22.6k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9145
22.9k
}
9146
9147
/* Canonicalize the relocs.  */
9148
9149
long
9150
_bfd_elf_canonicalize_reloc (bfd *abfd,
9151
           sec_ptr section,
9152
           arelent **relptr,
9153
           asymbol **symbols)
9154
22.5k
{
9155
22.5k
  arelent *tblptr;
9156
22.5k
  unsigned int i;
9157
22.5k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9158
9159
22.5k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9160
1.92k
    return -1;
9161
9162
20.6k
  tblptr = section->relocation;
9163
1.20M
  for (i = 0; i < section->reloc_count; i++)
9164
1.18M
    *relptr++ = tblptr++;
9165
9166
20.6k
  *relptr = NULL;
9167
9168
20.6k
  return section->reloc_count;
9169
22.5k
}
9170
9171
long
9172
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9173
5.34k
{
9174
5.34k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9175
5.34k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9176
9177
5.34k
  if (symcount >= 0)
9178
3.88k
    abfd->symcount = symcount;
9179
5.34k
  return symcount;
9180
5.34k
}
9181
9182
long
9183
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9184
              asymbol **allocation)
9185
393
{
9186
393
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9187
393
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9188
9189
393
  if (symcount >= 0)
9190
235
    abfd->dynsymcount = symcount;
9191
393
  return symcount;
9192
393
}
9193
9194
/* Return the size required for the dynamic reloc entries.  Any loadable
9195
   section that was actually installed in the BFD, and has type SHT_REL
9196
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9197
   dynamic reloc section.  */
9198
9199
long
9200
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9201
5.62k
{
9202
5.62k
  bfd_size_type count, ext_rel_size;
9203
5.62k
  asection *s;
9204
9205
5.62k
  if (elf_dynsymtab (abfd) == 0)
9206
5.27k
    {
9207
5.27k
      bfd_set_error (bfd_error_invalid_operation);
9208
5.27k
      return -1;
9209
5.27k
    }
9210
9211
354
  count = 1;
9212
354
  ext_rel_size = 0;
9213
10.7k
  for (s = abfd->sections; s != NULL; s = s->next)
9214
10.4k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9215
10.4k
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9216
1.32k
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9217
10.4k
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9218
596
      {
9219
596
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9220
596
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9221
0
    {
9222
0
      bfd_set_error (bfd_error_file_truncated);
9223
0
      return -1;
9224
0
    }
9225
596
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9226
596
  if (count > LONG_MAX / sizeof (arelent *))
9227
0
    {
9228
0
      bfd_set_error (bfd_error_file_too_big);
9229
0
      return -1;
9230
0
    }
9231
596
      }
9232
354
  if (count > 1 && !bfd_write_p (abfd))
9233
348
    {
9234
      /* Sanity check reloc section sizes.  */
9235
348
      ufile_ptr filesize = bfd_get_file_size (abfd);
9236
348
      if (filesize != 0 && ext_rel_size > filesize)
9237
2
  {
9238
2
    bfd_set_error (bfd_error_file_truncated);
9239
2
    return -1;
9240
2
  }
9241
348
    }
9242
352
  return count * sizeof (arelent *);
9243
354
}
9244
9245
/* Canonicalize the dynamic relocation entries.  Note that we return the
9246
   dynamic relocations as a single block, although they are actually
9247
   associated with particular sections; the interface, which was
9248
   designed for SunOS style shared libraries, expects that there is only
9249
   one set of dynamic relocs.  Any loadable section that was actually
9250
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9251
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9252
9253
long
9254
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9255
             arelent **storage,
9256
             asymbol **syms)
9257
308
{
9258
308
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9259
308
  asection *s;
9260
308
  long ret;
9261
9262
308
  if (elf_dynsymtab (abfd) == 0)
9263
0
    {
9264
0
      bfd_set_error (bfd_error_invalid_operation);
9265
0
      return -1;
9266
0
    }
9267
9268
308
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9269
308
  ret = 0;
9270
8.49k
  for (s = abfd->sections; s != NULL; s = s->next)
9271
8.22k
    {
9272
8.22k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9273
8.22k
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9274
1.12k
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9275
8.22k
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9276
498
  {
9277
498
    arelent *p;
9278
498
    long count, i;
9279
9280
498
    if (! (*slurp_relocs) (abfd, s, syms, true))
9281
34
      return -1;
9282
464
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9283
464
    p = s->relocation;
9284
43.8k
    for (i = 0; i < count; i++)
9285
43.4k
      *storage++ = p++;
9286
464
    ret += count;
9287
464
  }
9288
8.22k
    }
9289
9290
274
  *storage = NULL;
9291
9292
274
  return ret;
9293
308
}
9294

9295
/* Read in the version information.  */
9296
9297
bool
9298
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9299
659
{
9300
659
  bfd_byte *contents = NULL;
9301
659
  unsigned int freeidx = 0;
9302
659
  size_t amt;
9303
659
  void *contents_addr = NULL;
9304
659
  size_t contents_size = 0;
9305
9306
659
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9307
635
    {
9308
635
      Elf_Internal_Shdr *hdr;
9309
635
      Elf_External_Verneed *everneed;
9310
635
      Elf_Internal_Verneed *iverneed;
9311
635
      unsigned int i;
9312
635
      bfd_byte *contents_end;
9313
635
      size_t verneed_count;
9314
635
      size_t verneed_size;
9315
9316
635
      if (elf_tdata (abfd)->dt_verneed != NULL)
9317
1
  {
9318
1
    hdr = NULL;
9319
1
    contents = elf_tdata (abfd)->dt_verneed;
9320
1
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9321
1
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9322
1
  }
9323
634
      else
9324
634
  {
9325
634
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9326
9327
634
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9328
15
      {
9329
226
      error_return_bad_verref:
9330
226
        _bfd_error_handler
9331
226
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9332
226
        bfd_set_error (bfd_error_bad_value);
9333
264
      error_return_verref:
9334
264
        elf_tdata (abfd)->verref = NULL;
9335
264
        elf_tdata (abfd)->cverrefs = 0;
9336
264
        goto error_return;
9337
226
      }
9338
9339
619
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9340
6
      goto error_return_verref;
9341
613
    contents_size = hdr->sh_size;
9342
613
    contents = _bfd_mmap_temporary (abfd, contents_size,
9343
613
            &contents_addr, &contents_size);
9344
613
    if (contents == NULL)
9345
31
      goto error_return_verref;
9346
9347
582
    verneed_size = hdr->sh_size;
9348
582
    verneed_count = hdr->sh_info;
9349
582
  }
9350
9351
583
      if (_bfd_mul_overflow (verneed_count,
9352
583
           sizeof (Elf_Internal_Verneed), &amt))
9353
0
  {
9354
0
    bfd_set_error (bfd_error_file_too_big);
9355
0
    goto error_return_verref;
9356
0
  }
9357
583
      if (amt == 0)
9358
1
  goto error_return_verref;
9359
582
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9360
582
      if (elf_tdata (abfd)->verref == NULL)
9361
0
  goto error_return_verref;
9362
9363
582
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9364
582
      == sizeof (Elf_External_Vernaux));
9365
582
      contents_end = (contents + verneed_size
9366
582
          - sizeof (Elf_External_Verneed));
9367
582
      everneed = (Elf_External_Verneed *) contents;
9368
582
      iverneed = elf_tdata (abfd)->verref;
9369
874
      for (i = 0; i < verneed_count; i++, iverneed++)
9370
870
  {
9371
870
    Elf_External_Vernaux *evernaux;
9372
870
    Elf_Internal_Vernaux *ivernaux;
9373
870
    unsigned int j;
9374
9375
870
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9376
9377
870
    iverneed->vn_bfd = abfd;
9378
9379
870
    if (elf_use_dt_symtab_p (abfd))
9380
1
      {
9381
1
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9382
1
    iverneed->vn_filename
9383
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9384
0
        else
9385
0
    iverneed->vn_filename = NULL;
9386
1
      }
9387
869
    else if (hdr == NULL)
9388
0
      goto error_return_bad_verref;
9389
869
    else
9390
869
      iverneed->vn_filename
9391
869
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9392
869
             iverneed->vn_file);
9393
870
    if (iverneed->vn_filename == NULL)
9394
130
      goto error_return_bad_verref;
9395
9396
740
    if (iverneed->vn_cnt == 0)
9397
27
      iverneed->vn_auxptr = NULL;
9398
713
    else
9399
713
      {
9400
713
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9401
713
             sizeof (Elf_Internal_Vernaux), &amt))
9402
0
    {
9403
0
      bfd_set_error (bfd_error_file_too_big);
9404
0
      goto error_return_verref;
9405
0
    }
9406
713
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9407
713
    bfd_alloc (abfd, amt);
9408
713
        if (iverneed->vn_auxptr == NULL)
9409
0
    goto error_return_verref;
9410
713
      }
9411
9412
740
    if (iverneed->vn_aux
9413
740
        > (size_t) (contents_end - (bfd_byte *) everneed))
9414
13
      goto error_return_bad_verref;
9415
9416
727
    evernaux = ((Elf_External_Vernaux *)
9417
727
          ((bfd_byte *) everneed + iverneed->vn_aux));
9418
727
    ivernaux = iverneed->vn_auxptr;
9419
1.26k
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9420
1.24k
      {
9421
1.24k
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9422
9423
1.24k
        if (elf_use_dt_symtab_p (abfd))
9424
0
    {
9425
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9426
0
        ivernaux->vna_nodename
9427
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9428
0
      else
9429
0
        ivernaux->vna_nodename = NULL;
9430
0
    }
9431
1.24k
        else if (hdr == NULL)
9432
0
    goto error_return_bad_verref;
9433
1.24k
        else
9434
1.24k
    ivernaux->vna_nodename
9435
1.24k
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9436
1.24k
                 ivernaux->vna_name);
9437
1.24k
        if (ivernaux->vna_nodename == NULL)
9438
9
    goto error_return_bad_verref;
9439
9440
1.23k
        if (ivernaux->vna_other > freeidx)
9441
338
    freeidx = ivernaux->vna_other;
9442
9443
1.23k
        ivernaux->vna_nextptr = NULL;
9444
1.23k
        if (ivernaux->vna_next == 0)
9445
663
    {
9446
663
      iverneed->vn_cnt = j + 1;
9447
663
      break;
9448
663
    }
9449
571
        if (j + 1 < iverneed->vn_cnt)
9450
547
    ivernaux->vna_nextptr = ivernaux + 1;
9451
9452
571
        if (ivernaux->vna_next
9453
571
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9454
30
    goto error_return_bad_verref;
9455
9456
541
        evernaux = ((Elf_External_Vernaux *)
9457
541
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9458
541
      }
9459
9460
688
    iverneed->vn_nextref = NULL;
9461
688
    if (iverneed->vn_next == 0)
9462
367
      break;
9463
321
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9464
288
      iverneed->vn_nextref = iverneed + 1;
9465
9466
321
    if (iverneed->vn_next
9467
321
        > (size_t) (contents_end - (bfd_byte *) everneed))
9468
29
      goto error_return_bad_verref;
9469
9470
292
    everneed = ((Elf_External_Verneed *)
9471
292
          ((bfd_byte *) everneed + iverneed->vn_next));
9472
292
  }
9473
371
      elf_tdata (abfd)->cverrefs = i;
9474
9475
371
      if (contents != elf_tdata (abfd)->dt_verneed)
9476
370
  _bfd_munmap_temporary (contents_addr, contents_size);
9477
371
      contents = NULL;
9478
371
      contents_addr = NULL;
9479
371
    }
9480
9481
395
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9482
24
    {
9483
24
      Elf_Internal_Shdr *hdr;
9484
24
      Elf_External_Verdef *everdef;
9485
24
      Elf_Internal_Verdef *iverdef;
9486
24
      Elf_Internal_Verdef *iverdefarr;
9487
24
      Elf_Internal_Verdef iverdefmem;
9488
24
      unsigned int i;
9489
24
      unsigned int maxidx;
9490
24
      bfd_byte *contents_end_def, *contents_end_aux;
9491
24
      size_t verdef_count;
9492
24
      size_t verdef_size;
9493
9494
24
      if (elf_tdata (abfd)->dt_verdef != NULL)
9495
0
  {
9496
0
    hdr = NULL;
9497
0
    contents = elf_tdata (abfd)->dt_verdef;
9498
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9499
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9500
0
  }
9501
24
      else
9502
24
  {
9503
24
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9504
9505
24
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9506
2
      {
9507
10
      error_return_bad_verdef:
9508
10
        _bfd_error_handler
9509
10
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9510
10
        bfd_set_error (bfd_error_bad_value);
9511
22
      error_return_verdef:
9512
22
        elf_tdata (abfd)->verdef = NULL;
9513
22
        elf_tdata (abfd)->cverdefs = 0;
9514
22
        goto error_return;
9515
10
      }
9516
9517
22
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9518
2
      goto error_return_verdef;
9519
20
    contents_size = hdr->sh_size;
9520
20
    contents = _bfd_mmap_temporary (abfd, contents_size,
9521
20
            &contents_addr, &contents_size);
9522
20
    if (contents == NULL)
9523
10
      goto error_return_verdef;
9524
9525
10
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9526
10
          >= sizeof (Elf_External_Verdaux));
9527
9528
10
    verdef_count = hdr->sh_info;
9529
10
    verdef_size = hdr->sh_size;
9530
10
  }
9531
9532
10
      contents_end_def = (contents + verdef_size
9533
10
        - sizeof (Elf_External_Verdef));
9534
10
      contents_end_aux = (contents + verdef_size
9535
10
        - sizeof (Elf_External_Verdaux));
9536
9537
      /* We know the number of entries in the section but not the maximum
9538
   index.  Therefore we have to run through all entries and find
9539
   the maximum.  */
9540
10
      everdef = (Elf_External_Verdef *) contents;
9541
10
      maxidx = 0;
9542
15
      for (i = 0; i < verdef_count; ++i)
9543
12
  {
9544
12
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9545
9546
12
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9547
2
      goto error_return_bad_verdef;
9548
10
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9549
9
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9550
9551
10
    if (iverdefmem.vd_next == 0)
9552
3
      break;
9553
9554
7
    if (iverdefmem.vd_next
9555
7
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9556
2
      goto error_return_bad_verdef;
9557
9558
5
    everdef = ((Elf_External_Verdef *)
9559
5
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9560
5
  }
9561
9562
6
      if (default_imported_symver)
9563
0
  {
9564
0
    if (freeidx > maxidx)
9565
0
      maxidx = ++freeidx;
9566
0
    else
9567
0
      freeidx = ++maxidx;
9568
0
  }
9569
6
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9570
0
  {
9571
0
    bfd_set_error (bfd_error_file_too_big);
9572
0
    goto error_return_verdef;
9573
0
  }
9574
9575
6
      if (amt == 0)
9576
0
  goto error_return_verdef;
9577
6
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9578
6
      if (elf_tdata (abfd)->verdef == NULL)
9579
0
  goto error_return_verdef;
9580
9581
6
      elf_tdata (abfd)->cverdefs = maxidx;
9582
9583
6
      everdef = (Elf_External_Verdef *) contents;
9584
6
      iverdefarr = elf_tdata (abfd)->verdef;
9585
8
      for (i = 0; i < verdef_count; ++i)
9586
7
  {
9587
7
    Elf_External_Verdaux *everdaux;
9588
7
    Elf_Internal_Verdaux *iverdaux;
9589
7
    unsigned int j;
9590
9591
7
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9592
9593
7
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9594
0
      goto error_return_bad_verdef;
9595
9596
7
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9597
7
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9598
9599
7
    iverdef->vd_bfd = abfd;
9600
9601
7
    if (iverdef->vd_cnt == 0)
9602
4
      iverdef->vd_auxptr = NULL;
9603
3
    else
9604
3
      {
9605
3
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9606
3
             sizeof (Elf_Internal_Verdaux), &amt))
9607
0
    {
9608
0
      bfd_set_error (bfd_error_file_too_big);
9609
0
      goto error_return_verdef;
9610
0
    }
9611
3
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9612
3
    bfd_alloc (abfd, amt);
9613
3
        if (iverdef->vd_auxptr == NULL)
9614
0
    goto error_return_verdef;
9615
3
      }
9616
9617
7
    if (iverdef->vd_aux
9618
7
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9619
1
      goto error_return_bad_verdef;
9620
9621
6
    everdaux = ((Elf_External_Verdaux *)
9622
6
          ((bfd_byte *) everdef + iverdef->vd_aux));
9623
6
    iverdaux = iverdef->vd_auxptr;
9624
6
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9625
3
      {
9626
3
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9627
9628
3
        if (elf_use_dt_symtab_p (abfd))
9629
0
    {
9630
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9631
0
        iverdaux->vda_nodename
9632
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9633
0
      else
9634
0
        iverdaux->vda_nodename = NULL;
9635
0
    }
9636
3
        else
9637
3
    iverdaux->vda_nodename
9638
3
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9639
3
                 iverdaux->vda_name);
9640
3
        if (iverdaux->vda_nodename == NULL)
9641
2
    goto error_return_bad_verdef;
9642
9643
1
        iverdaux->vda_nextptr = NULL;
9644
1
        if (iverdaux->vda_next == 0)
9645
0
    {
9646
0
      iverdef->vd_cnt = j + 1;
9647
0
      break;
9648
0
    }
9649
1
        if (j + 1 < iverdef->vd_cnt)
9650
1
    iverdaux->vda_nextptr = iverdaux + 1;
9651
9652
1
        if (iverdaux->vda_next
9653
1
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9654
1
    goto error_return_bad_verdef;
9655
9656
0
        everdaux = ((Elf_External_Verdaux *)
9657
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9658
0
      }
9659
9660
3
    iverdef->vd_nodename = NULL;
9661
3
    if (iverdef->vd_cnt)
9662
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9663
9664
3
    iverdef->vd_nextdef = NULL;
9665
3
    if (iverdef->vd_next == 0)
9666
1
      break;
9667
2
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9668
1
      iverdef->vd_nextdef = iverdef + 1;
9669
9670
2
    everdef = ((Elf_External_Verdef *)
9671
2
         ((bfd_byte *) everdef + iverdef->vd_next));
9672
2
  }
9673
9674
2
      if (contents != elf_tdata (abfd)->dt_verdef)
9675
2
  _bfd_munmap_temporary (contents_addr, contents_size);
9676
2
      contents = NULL;
9677
2
      contents_addr = NULL;
9678
2
    }
9679
371
  else if (default_imported_symver)
9680
0
    {
9681
0
      if (freeidx < 3)
9682
0
  freeidx = 3;
9683
0
      else
9684
0
  freeidx++;
9685
9686
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9687
0
  {
9688
0
    bfd_set_error (bfd_error_file_too_big);
9689
0
    goto error_return;
9690
0
  }
9691
0
      if (amt == 0)
9692
0
  goto error_return;
9693
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9694
0
      if (elf_tdata (abfd)->verdef == NULL)
9695
0
  goto error_return;
9696
9697
0
      elf_tdata (abfd)->cverdefs = freeidx;
9698
0
    }
9699
9700
  /* Create a default version based on the soname.  */
9701
373
  if (default_imported_symver)
9702
0
    {
9703
0
      Elf_Internal_Verdef *iverdef;
9704
0
      Elf_Internal_Verdaux *iverdaux;
9705
9706
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9707
9708
0
      iverdef->vd_version = VER_DEF_CURRENT;
9709
0
      iverdef->vd_flags = 0;
9710
0
      iverdef->vd_ndx = freeidx;
9711
0
      iverdef->vd_cnt = 1;
9712
9713
0
      iverdef->vd_bfd = abfd;
9714
9715
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9716
0
      if (iverdef->vd_nodename == NULL)
9717
0
  goto error_return_verdef;
9718
0
      iverdef->vd_nextdef = NULL;
9719
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9720
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9721
0
      if (iverdef->vd_auxptr == NULL)
9722
0
  goto error_return_verdef;
9723
9724
0
      iverdaux = iverdef->vd_auxptr;
9725
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9726
0
    }
9727
9728
373
  return true;
9729
9730
286
 error_return:
9731
286
  if (contents != elf_tdata (abfd)->dt_verneed
9732
286
      && contents != elf_tdata (abfd)->dt_verdef)
9733
220
    _bfd_munmap_temporary (contents_addr, contents_size);
9734
286
  return false;
9735
373
}
9736

9737
asymbol *
9738
_bfd_elf_make_empty_symbol (bfd *abfd)
9739
6.76M
{
9740
6.76M
  elf_symbol_type *newsym;
9741
9742
6.76M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9743
6.76M
  if (!newsym)
9744
0
    return NULL;
9745
6.76M
  newsym->symbol.the_bfd = abfd;
9746
6.76M
  return &newsym->symbol;
9747
6.76M
}
9748
9749
void
9750
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9751
        asymbol *symbol,
9752
        symbol_info *ret)
9753
94.8k
{
9754
94.8k
  bfd_symbol_info (symbol, ret);
9755
94.8k
}
9756
9757
/* Return whether a symbol name implies a local symbol.  Most targets
9758
   use this function for the is_local_label_name entry point, but some
9759
   override it.  */
9760
9761
bool
9762
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9763
            const char *name)
9764
37.8k
{
9765
  /* Normal local symbols start with ``.L''.  */
9766
37.8k
  if (name[0] == '.' && name[1] == 'L')
9767
13.6k
    return true;
9768
9769
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9770
     DWARF debugging symbols starting with ``..''.  */
9771
24.2k
  if (name[0] == '.' && name[1] == '.')
9772
261
    return true;
9773
9774
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9775
     emitting DWARF debugging output.  I suspect this is actually a
9776
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9777
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9778
     underscore to be emitted on some ELF targets).  For ease of use,
9779
     we treat such symbols as local.  */
9780
23.9k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9781
0
    return true;
9782
9783
  /* Treat assembler generated fake symbols, dollar local labels and
9784
     forward-backward labels (aka local labels) as locals.
9785
     These labels have the form:
9786
9787
       L0^A.*              (fake symbols)
9788
9789
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9790
9791
     Versions which start with .L will have already been matched above,
9792
     so we only need to match the rest.  */
9793
23.9k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9794
124
    {
9795
124
      bool ret = false;
9796
124
      const char * p;
9797
124
      char c;
9798
9799
383
      for (p = name + 2; (c = *p); p++)
9800
315
  {
9801
315
    if (c == 1 || c == 2)
9802
46
      {
9803
46
        if (c == 1 && p == name + 2)
9804
    /* A fake symbol.  */
9805
0
    return true;
9806
9807
        /* FIXME: We are being paranoid here and treating symbols like
9808
     L0^Bfoo as if there were non-local, on the grounds that the
9809
     assembler will never generate them.  But can any symbol
9810
     containing an ASCII value in the range 1-31 ever be anything
9811
     other than some kind of local ?  */
9812
46
        ret = true;
9813
46
      }
9814
9815
315
    if (! ISDIGIT (c))
9816
56
      {
9817
56
        ret = false;
9818
56
        break;
9819
56
      }
9820
315
  }
9821
124
      return ret;
9822
124
    }
9823
9824
23.8k
  return false;
9825
23.9k
}
9826
9827
alent *
9828
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9829
         asymbol *symbol ATTRIBUTE_UNUSED)
9830
0
{
9831
0
  abort ();
9832
0
  return NULL;
9833
0
}
9834
9835
bool
9836
_bfd_elf_set_arch_mach (bfd *abfd,
9837
      enum bfd_architecture arch,
9838
      unsigned long machine)
9839
172
{
9840
  /* If this isn't the right architecture for this backend, and this
9841
     isn't the generic backend, fail.  */
9842
172
  if (arch != get_elf_backend_data (abfd)->arch
9843
172
      && arch != bfd_arch_unknown
9844
172
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9845
0
    return false;
9846
9847
172
  return bfd_default_set_arch_mach (abfd, arch, machine);
9848
172
}
9849
9850
/* Find the nearest line to a particular section and offset,
9851
   for error reporting.  */
9852
9853
bool
9854
_bfd_elf_find_nearest_line (bfd *abfd,
9855
          asymbol **symbols,
9856
          asection *section,
9857
          bfd_vma offset,
9858
          const char **filename_ptr,
9859
          const char **functionname_ptr,
9860
          unsigned int *line_ptr,
9861
          unsigned int *discriminator_ptr)
9862
87.8k
{
9863
87.8k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9864
87.8k
                offset, filename_ptr,
9865
87.8k
                functionname_ptr, line_ptr,
9866
87.8k
                discriminator_ptr);
9867
87.8k
}
9868
9869
/* Find the nearest line to a particular section and offset,
9870
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9871
   can be optionally specified.  */
9872
9873
bool
9874
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9875
             const char *alt_filename,
9876
             asymbol **symbols,
9877
             asection *section,
9878
             bfd_vma offset,
9879
             const char **filename_ptr,
9880
             const char **functionname_ptr,
9881
             unsigned int *line_ptr,
9882
             unsigned int *discriminator_ptr)
9883
87.8k
{
9884
87.8k
  bool found;
9885
9886
87.8k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9887
87.8k
                section, offset, filename_ptr,
9888
87.8k
                functionname_ptr, line_ptr,
9889
87.8k
                discriminator_ptr,
9890
87.8k
                dwarf_debug_sections,
9891
87.8k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9892
44.3k
    return true;
9893
9894
43.5k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9895
43.5k
             filename_ptr, functionname_ptr, line_ptr))
9896
0
    {
9897
0
      if (!*functionname_ptr)
9898
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9899
0
        *filename_ptr ? NULL : filename_ptr,
9900
0
        functionname_ptr);
9901
0
      return true;
9902
0
    }
9903
9904
43.5k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9905
43.5k
               &found, filename_ptr,
9906
43.5k
               functionname_ptr, line_ptr,
9907
43.5k
               &elf_tdata (abfd)->line_info))
9908
0
    return false;
9909
43.5k
  if (found && (*functionname_ptr || *line_ptr))
9910
0
    return true;
9911
9912
43.5k
  if (symbols == NULL)
9913
8.58k
    return false;
9914
9915
34.9k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9916
34.9k
        filename_ptr, functionname_ptr))
9917
31.6k
    return false;
9918
9919
3.32k
  *line_ptr = 0;
9920
3.32k
  return true;
9921
34.9k
}
9922
9923
/* Find the line for a symbol.  */
9924
9925
bool
9926
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9927
        const char **filename_ptr, unsigned int *line_ptr)
9928
61.1k
{
9929
61.1k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9930
61.1k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9931
61.1k
          filename_ptr, NULL, line_ptr, NULL,
9932
61.1k
          dwarf_debug_sections,
9933
61.1k
          &tdata->dwarf2_find_line_info);
9934
61.1k
}
9935
9936
/* After a call to bfd_find_nearest_line, successive calls to
9937
   bfd_find_inliner_info can be used to get source information about
9938
   each level of function inlining that terminated at the address
9939
   passed to bfd_find_nearest_line.  Currently this is only supported
9940
   for DWARF2 with appropriate DWARF3 extensions. */
9941
9942
bool
9943
_bfd_elf_find_inliner_info (bfd *abfd,
9944
          const char **filename_ptr,
9945
          const char **functionname_ptr,
9946
          unsigned int *line_ptr)
9947
0
{
9948
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9949
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9950
0
          functionname_ptr, line_ptr,
9951
0
          &tdata->dwarf2_find_line_info);
9952
0
}
9953
9954
int
9955
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9956
0
{
9957
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9958
0
  int ret = bed->s->sizeof_ehdr;
9959
9960
0
  if (!bfd_link_relocatable (info))
9961
0
    {
9962
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
9963
9964
0
      if (phdr_size == (bfd_size_type) -1)
9965
0
  {
9966
0
    struct elf_segment_map *m;
9967
9968
0
    phdr_size = 0;
9969
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9970
0
      phdr_size += bed->s->sizeof_phdr;
9971
9972
0
    if (phdr_size == 0)
9973
0
      phdr_size = get_program_header_size (abfd, info);
9974
0
  }
9975
9976
0
      elf_program_header_size (abfd) = phdr_size;
9977
0
      ret += phdr_size;
9978
0
    }
9979
9980
0
  return ret;
9981
0
}
9982
9983
bool
9984
_bfd_elf_set_section_contents (bfd *abfd,
9985
             sec_ptr section,
9986
             const void *location,
9987
             file_ptr offset,
9988
             bfd_size_type count)
9989
2.18k
{
9990
2.18k
  Elf_Internal_Shdr *hdr;
9991
9992
2.18k
  if (! abfd->output_has_begun
9993
2.18k
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9994
1
    return false;
9995
9996
2.18k
  if (!count)
9997
0
    return true;
9998
9999
2.18k
  hdr = &elf_section_data (section)->this_hdr;
10000
2.18k
  if (hdr->sh_offset == (file_ptr) -1)
10001
0
    {
10002
0
      unsigned char *contents;
10003
10004
0
      if (bfd_section_is_ctf (section))
10005
  /* Nothing to do with this section: the contents are generated
10006
     later.  */
10007
0
  return true;
10008
10009
0
      if ((offset + count) > hdr->sh_size)
10010
0
  {
10011
0
    _bfd_error_handler
10012
0
      (_("%pB:%pA: error: attempting to write"
10013
0
         " over the end of the section"),
10014
0
       abfd, section);
10015
10016
0
    bfd_set_error (bfd_error_invalid_operation);
10017
0
    return false;
10018
0
  }
10019
10020
0
      contents = hdr->contents;
10021
0
      if (contents == NULL)
10022
0
  {
10023
0
    _bfd_error_handler
10024
0
      (_("%pB:%pA: error: attempting to write"
10025
0
         " section into an empty buffer"),
10026
0
       abfd, section);
10027
10028
0
    bfd_set_error (bfd_error_invalid_operation);
10029
0
    return false;
10030
0
  }
10031
10032
0
      memcpy (contents + offset, location, count);
10033
0
      return true;
10034
0
    }
10035
10036
2.18k
  return _bfd_generic_set_section_contents (abfd, section,
10037
2.18k
              location, offset, count);
10038
2.18k
}
10039
10040
bool
10041
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10042
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10043
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10044
0
{
10045
0
  abort ();
10046
0
  return false;
10047
0
}
10048
10049
/* Try to convert a non-ELF reloc into an ELF one.  */
10050
10051
bool
10052
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10053
0
{
10054
  /* Check whether we really have an ELF howto.  */
10055
10056
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10057
0
    {
10058
0
      bfd_reloc_code_real_type code;
10059
0
      reloc_howto_type *howto;
10060
10061
      /* Alien reloc: Try to determine its type to replace it with an
10062
   equivalent ELF reloc.  */
10063
10064
0
      if (areloc->howto->pc_relative)
10065
0
  {
10066
0
    switch (areloc->howto->bitsize)
10067
0
      {
10068
0
      case 8:
10069
0
        code = BFD_RELOC_8_PCREL;
10070
0
        break;
10071
0
      case 12:
10072
0
        code = BFD_RELOC_12_PCREL;
10073
0
        break;
10074
0
      case 16:
10075
0
        code = BFD_RELOC_16_PCREL;
10076
0
        break;
10077
0
      case 24:
10078
0
        code = BFD_RELOC_24_PCREL;
10079
0
        break;
10080
0
      case 32:
10081
0
        code = BFD_RELOC_32_PCREL;
10082
0
        break;
10083
0
      case 64:
10084
0
        code = BFD_RELOC_64_PCREL;
10085
0
        break;
10086
0
      default:
10087
0
        goto fail;
10088
0
      }
10089
10090
0
    howto = bfd_reloc_type_lookup (abfd, code);
10091
10092
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10093
0
      {
10094
0
        if (howto->pcrel_offset)
10095
0
    areloc->addend += areloc->address;
10096
0
        else
10097
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10098
0
      }
10099
0
  }
10100
0
      else
10101
0
  {
10102
0
    switch (areloc->howto->bitsize)
10103
0
      {
10104
0
      case 8:
10105
0
        code = BFD_RELOC_8;
10106
0
        break;
10107
0
      case 14:
10108
0
        code = BFD_RELOC_14;
10109
0
        break;
10110
0
      case 16:
10111
0
        code = BFD_RELOC_16;
10112
0
        break;
10113
0
      case 26:
10114
0
        code = BFD_RELOC_26;
10115
0
        break;
10116
0
      case 32:
10117
0
        code = BFD_RELOC_32;
10118
0
        break;
10119
0
      case 64:
10120
0
        code = BFD_RELOC_64;
10121
0
        break;
10122
0
      default:
10123
0
        goto fail;
10124
0
      }
10125
10126
0
    howto = bfd_reloc_type_lookup (abfd, code);
10127
0
  }
10128
10129
0
      if (howto)
10130
0
  areloc->howto = howto;
10131
0
      else
10132
0
  goto fail;
10133
0
    }
10134
10135
0
  return true;
10136
10137
0
 fail:
10138
  /* xgettext:c-format */
10139
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10140
0
          abfd, areloc->howto->name);
10141
0
  bfd_set_error (bfd_error_sorry);
10142
0
  return false;
10143
0
}
10144
10145
bool
10146
_bfd_elf_free_cached_info (bfd *abfd)
10147
1.19M
{
10148
1.19M
  struct elf_obj_tdata *tdata;
10149
10150
1.19M
  if ((bfd_get_format (abfd) == bfd_object
10151
1.19M
       || bfd_get_format (abfd) == bfd_core)
10152
1.19M
      && (tdata = elf_tdata (abfd)) != NULL)
10153
490k
    {
10154
490k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10155
96
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10156
490k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10157
490k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10158
490k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10159
2.58M
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10160
2.09M
  {
10161
2.09M
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10162
2.09M
    if (!sec->alloced)
10163
2.08M
      {
10164
2.08M
        free (elf_section_data (sec)->this_hdr.contents);
10165
2.08M
        elf_section_data (sec)->this_hdr.contents = NULL;
10166
2.08M
      }
10167
2.09M
    free (elf_section_data (sec)->relocs);
10168
2.09M
    elf_section_data (sec)->relocs = NULL;
10169
2.09M
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10170
0
      {
10171
0
        struct eh_frame_sec_info *sec_info
10172
0
    = elf_section_data (sec)->sec_info;
10173
0
        free (sec_info->cies);
10174
0
      }
10175
2.09M
  }
10176
490k
      free (tdata->symtab_hdr.contents);
10177
490k
      tdata->symtab_hdr.contents = NULL;
10178
490k
    }
10179
10180
1.19M
  return _bfd_generic_bfd_free_cached_info (abfd);
10181
1.19M
}
10182
10183
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10184
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10185
   range-checking to interfere.  There is nothing else to do in processing
10186
   this reloc.  */
10187
10188
bfd_reloc_status_type
10189
_bfd_elf_rel_vtable_reloc_fn
10190
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10191
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10192
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10193
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10194
262
{
10195
262
  return bfd_reloc_ok;
10196
262
}
10197

10198
/* Elf core file support.  Much of this only works on native
10199
   toolchains, since we rely on knowing the
10200
   machine-dependent procfs structure in order to pick
10201
   out details about the corefile.  */
10202
10203
#ifdef HAVE_SYS_PROCFS_H
10204
# include <sys/procfs.h>
10205
#endif
10206
10207
/* Return a PID that identifies a "thread" for threaded cores, or the
10208
   PID of the main process for non-threaded cores.  */
10209
10210
static int
10211
elfcore_make_pid (bfd *abfd)
10212
349
{
10213
349
  int pid;
10214
10215
349
  pid = elf_tdata (abfd)->core->lwpid;
10216
349
  if (pid == 0)
10217
291
    pid = elf_tdata (abfd)->core->pid;
10218
10219
349
  return pid;
10220
349
}
10221
10222
/* If there isn't a section called NAME, make one, using data from
10223
   SECT.  Note, this function will generate a reference to NAME, so
10224
   you shouldn't deallocate or overwrite it.  */
10225
10226
static bool
10227
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10228
349
{
10229
349
  asection *sect2;
10230
10231
349
  if (bfd_get_section_by_name (abfd, name) != NULL)
10232
224
    return true;
10233
10234
125
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10235
125
  if (sect2 == NULL)
10236
0
    return false;
10237
10238
125
  sect2->size = sect->size;
10239
125
  sect2->filepos = sect->filepos;
10240
125
  sect2->alignment_power = sect->alignment_power;
10241
125
  return true;
10242
125
}
10243
10244
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10245
   actually creates up to two pseudosections:
10246
   - For the single-threaded case, a section named NAME, unless
10247
     such a section already exists.
10248
   - For the multi-threaded case, a section named "NAME/PID", where
10249
     PID is elfcore_make_pid (abfd).
10250
   Both pseudosections have identical contents.  */
10251
bool
10252
_bfd_elfcore_make_pseudosection (bfd *abfd,
10253
         char *name,
10254
         size_t size,
10255
         ufile_ptr filepos)
10256
349
{
10257
349
  char buf[100];
10258
349
  char *threaded_name;
10259
349
  size_t len;
10260
349
  asection *sect;
10261
10262
  /* Build the section name.  */
10263
10264
349
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10265
349
  len = strlen (buf) + 1;
10266
349
  threaded_name = (char *) bfd_alloc (abfd, len);
10267
349
  if (threaded_name == NULL)
10268
0
    return false;
10269
349
  memcpy (threaded_name, buf, len);
10270
10271
349
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10272
349
               SEC_HAS_CONTENTS);
10273
349
  if (sect == NULL)
10274
0
    return false;
10275
349
  sect->size = size;
10276
349
  sect->filepos = filepos;
10277
349
  sect->alignment_power = 2;
10278
10279
349
  return elfcore_maybe_make_sect (abfd, name, sect);
10280
349
}
10281
10282
static bool
10283
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10284
        size_t offs)
10285
66
{
10286
66
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10287
66
                   SEC_HAS_CONTENTS);
10288
10289
66
  if (sect == NULL)
10290
0
    return false;
10291
10292
66
  sect->size = note->descsz - offs;
10293
66
  sect->filepos = note->descpos + offs;
10294
66
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10295
10296
66
  return true;
10297
66
}
10298
10299
/* prstatus_t exists on:
10300
     solaris 2.5+
10301
     linux 2.[01] + glibc
10302
     unixware 4.2
10303
*/
10304
10305
#if defined (HAVE_PRSTATUS_T)
10306
10307
static bool
10308
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10309
1.20k
{
10310
1.20k
  size_t size;
10311
1.20k
  int offset;
10312
10313
1.20k
  if (note->descsz == sizeof (prstatus_t))
10314
12
    {
10315
12
      prstatus_t prstat;
10316
10317
12
      size = sizeof (prstat.pr_reg);
10318
12
      offset   = offsetof (prstatus_t, pr_reg);
10319
12
      memcpy (&prstat, note->descdata, sizeof (prstat));
10320
10321
      /* Do not overwrite the core signal if it
10322
   has already been set by another thread.  */
10323
12
      if (elf_tdata (abfd)->core->signal == 0)
10324
7
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10325
12
      if (elf_tdata (abfd)->core->pid == 0)
10326
7
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10327
10328
      /* pr_who exists on:
10329
   solaris 2.5+
10330
   unixware 4.2
10331
   pr_who doesn't exist on:
10332
   linux 2.[01]
10333
   */
10334
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10335
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10336
#else
10337
12
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10338
12
#endif
10339
12
    }
10340
1.19k
#if defined (HAVE_PRSTATUS32_T)
10341
1.19k
  else if (note->descsz == sizeof (prstatus32_t))
10342
0
    {
10343
      /* 64-bit host, 32-bit corefile */
10344
0
      prstatus32_t prstat;
10345
10346
0
      size = sizeof (prstat.pr_reg);
10347
0
      offset   = offsetof (prstatus32_t, pr_reg);
10348
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10349
10350
      /* Do not overwrite the core signal if it
10351
   has already been set by another thread.  */
10352
0
      if (elf_tdata (abfd)->core->signal == 0)
10353
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10354
0
      if (elf_tdata (abfd)->core->pid == 0)
10355
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10356
10357
      /* pr_who exists on:
10358
   solaris 2.5+
10359
   unixware 4.2
10360
   pr_who doesn't exist on:
10361
   linux 2.[01]
10362
   */
10363
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10364
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10365
#else
10366
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10367
0
#endif
10368
0
    }
10369
1.19k
#endif /* HAVE_PRSTATUS32_T */
10370
1.19k
  else
10371
1.19k
    {
10372
      /* Fail - we don't know how to handle any other
10373
   note size (ie. data object type).  */
10374
1.19k
      return true;
10375
1.19k
    }
10376
10377
  /* Make a ".reg/999" section and a ".reg" section.  */
10378
12
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10379
12
            size, note->descpos + offset);
10380
1.20k
}
10381
#endif /* defined (HAVE_PRSTATUS_T) */
10382
10383
/* Create a pseudosection containing the exact contents of NOTE.  */
10384
static bool
10385
elfcore_make_note_pseudosection (bfd *abfd,
10386
         char *name,
10387
         Elf_Internal_Note *note)
10388
302
{
10389
302
  return _bfd_elfcore_make_pseudosection (abfd, name,
10390
302
            note->descsz, note->descpos);
10391
302
}
10392
10393
/* There isn't a consistent prfpregset_t across platforms,
10394
   but it doesn't matter, because we don't have to pick this
10395
   data structure apart.  */
10396
10397
static bool
10398
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10399
278
{
10400
278
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10401
278
}
10402
10403
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10404
   type of NT_PRXFPREG.  Just include the whole note's contents
10405
   literally.  */
10406
10407
static bool
10408
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10409
0
{
10410
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10411
0
}
10412
10413
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10414
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10415
   contents literally.  */
10416
10417
static bool
10418
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10419
0
{
10420
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10421
0
}
10422
10423
static bool
10424
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10425
0
{
10426
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note);
10427
0
}
10428
10429
static bool
10430
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10431
0
{
10432
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10433
0
}
10434
10435
static bool
10436
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10437
0
{
10438
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10439
0
}
10440
10441
static bool
10442
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10443
0
{
10444
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10445
0
}
10446
10447
static bool
10448
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10449
0
{
10450
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10451
0
}
10452
10453
static bool
10454
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10455
0
{
10456
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10457
0
}
10458
10459
static bool
10460
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10461
0
{
10462
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10463
0
}
10464
10465
static bool
10466
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10467
0
{
10468
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10469
0
}
10470
10471
static bool
10472
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10473
0
{
10474
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10475
0
}
10476
10477
static bool
10478
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10479
0
{
10480
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10481
0
}
10482
10483
static bool
10484
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10485
0
{
10486
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10487
0
}
10488
10489
static bool
10490
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10491
0
{
10492
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10493
0
}
10494
10495
static bool
10496
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10497
0
{
10498
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10499
0
}
10500
10501
static bool
10502
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10503
0
{
10504
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10505
0
}
10506
10507
static bool
10508
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10509
0
{
10510
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10511
0
}
10512
10513
static bool
10514
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10515
0
{
10516
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10517
0
}
10518
10519
static bool
10520
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10521
0
{
10522
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10523
0
}
10524
10525
static bool
10526
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10527
0
{
10528
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10529
0
}
10530
10531
static bool
10532
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10533
0
{
10534
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10535
0
}
10536
10537
static bool
10538
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10539
0
{
10540
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10541
0
}
10542
10543
static bool
10544
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10545
0
{
10546
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10547
0
}
10548
10549
static bool
10550
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10551
0
{
10552
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10553
0
}
10554
10555
static bool
10556
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10557
0
{
10558
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10559
0
}
10560
10561
static bool
10562
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10563
0
{
10564
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10565
0
}
10566
10567
static bool
10568
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10569
0
{
10570
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10571
0
}
10572
10573
static bool
10574
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10575
0
{
10576
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10577
0
}
10578
10579
static bool
10580
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10581
0
{
10582
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10583
0
}
10584
10585
static bool
10586
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10587
0
{
10588
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10589
0
}
10590
10591
static bool
10592
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10593
0
{
10594
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10595
0
}
10596
10597
static bool
10598
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10599
0
{
10600
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10601
0
}
10602
10603
static bool
10604
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10605
0
{
10606
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10607
0
}
10608
10609
static bool
10610
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10611
0
{
10612
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10613
0
}
10614
10615
static bool
10616
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10617
0
{
10618
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10619
0
}
10620
10621
static bool
10622
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10623
0
{
10624
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10625
0
}
10626
10627
static bool
10628
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10629
0
{
10630
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10631
0
}
10632
10633
static bool
10634
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10635
0
{
10636
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10637
0
            note);
10638
0
}
10639
10640
static bool
10641
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10642
0
{
10643
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10644
0
}
10645
10646
static bool
10647
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10648
0
{
10649
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10650
0
}
10651
10652
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10653
   successful, otherwise return FALSE.  */
10654
10655
static bool
10656
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10657
0
{
10658
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10659
0
}
10660
10661
static bool
10662
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10663
0
{
10664
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10665
0
}
10666
10667
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10668
   successful otherwise, return FALSE.  */
10669
10670
static bool
10671
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10672
0
{
10673
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10674
0
}
10675
10676
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10677
   successful otherwise, return FALSE.  */
10678
10679
static bool
10680
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10681
0
{
10682
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10683
0
}
10684
10685
static bool
10686
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10687
0
{
10688
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10689
0
}
10690
10691
static bool
10692
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10693
0
{
10694
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10695
0
}
10696
10697
static bool
10698
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10699
0
{
10700
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10701
0
}
10702
10703
static bool
10704
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10705
0
{
10706
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10707
0
}
10708
10709
#if defined (HAVE_PRPSINFO_T)
10710
typedef prpsinfo_t   elfcore_psinfo_t;
10711
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10712
typedef prpsinfo32_t elfcore_psinfo32_t;
10713
#endif
10714
#endif
10715
10716
#if defined (HAVE_PSINFO_T)
10717
typedef psinfo_t   elfcore_psinfo_t;
10718
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10719
typedef psinfo32_t elfcore_psinfo32_t;
10720
#endif
10721
#endif
10722
10723
/* return a malloc'ed copy of a string at START which is at
10724
   most MAX bytes long, possibly without a terminating '\0'.
10725
   the copy will always have a terminating '\0'.  */
10726
10727
char *
10728
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10729
40
{
10730
40
  char *dups;
10731
40
  char *end = (char *) memchr (start, '\0', max);
10732
40
  size_t len;
10733
10734
40
  if (end == NULL)
10735
8
    len = max;
10736
32
  else
10737
32
    len = end - start;
10738
10739
40
  dups = (char *) bfd_alloc (abfd, len + 1);
10740
40
  if (dups == NULL)
10741
0
    return NULL;
10742
10743
40
  memcpy (dups, start, len);
10744
40
  dups[len] = '\0';
10745
10746
40
  return dups;
10747
40
}
10748
10749
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10750
static bool
10751
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10752
796
{
10753
796
  if (note->descsz == sizeof (elfcore_psinfo_t))
10754
3
    {
10755
3
      elfcore_psinfo_t psinfo;
10756
10757
3
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10758
10759
3
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10760
3
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10761
3
#endif
10762
3
      elf_tdata (abfd)->core->program
10763
3
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10764
3
        sizeof (psinfo.pr_fname));
10765
10766
3
      elf_tdata (abfd)->core->command
10767
3
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10768
3
        sizeof (psinfo.pr_psargs));
10769
3
    }
10770
793
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10771
793
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10772
14
    {
10773
      /* 64-bit host, 32-bit corefile */
10774
14
      elfcore_psinfo32_t psinfo;
10775
10776
14
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10777
10778
14
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10779
14
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10780
14
#endif
10781
14
      elf_tdata (abfd)->core->program
10782
14
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10783
14
        sizeof (psinfo.pr_fname));
10784
10785
14
      elf_tdata (abfd)->core->command
10786
14
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10787
14
        sizeof (psinfo.pr_psargs));
10788
14
    }
10789
779
#endif
10790
10791
779
  else
10792
779
    {
10793
      /* Fail - we don't know how to handle any other
10794
   note size (ie. data object type).  */
10795
779
      return true;
10796
779
    }
10797
10798
  /* Note that for some reason, a spurious space is tacked
10799
     onto the end of the args in some (at least one anyway)
10800
     implementations, so strip it off if it exists.  */
10801
10802
17
  {
10803
17
    char *command = elf_tdata (abfd)->core->command;
10804
17
    int n = strlen (command);
10805
10806
17
    if (0 < n && command[n - 1] == ' ')
10807
6
      command[n - 1] = '\0';
10808
17
  }
10809
10810
17
  return true;
10811
796
}
10812
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10813
10814
#if defined (HAVE_PSTATUS_T)
10815
static bool
10816
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10817
{
10818
  if (note->descsz == sizeof (pstatus_t)
10819
#if defined (HAVE_PXSTATUS_T)
10820
      || note->descsz == sizeof (pxstatus_t)
10821
#endif
10822
      )
10823
    {
10824
      pstatus_t pstat;
10825
10826
      memcpy (&pstat, note->descdata, sizeof (pstat));
10827
10828
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10829
    }
10830
#if defined (HAVE_PSTATUS32_T)
10831
  else if (note->descsz == sizeof (pstatus32_t))
10832
    {
10833
      /* 64-bit host, 32-bit corefile */
10834
      pstatus32_t pstat;
10835
10836
      memcpy (&pstat, note->descdata, sizeof (pstat));
10837
10838
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10839
    }
10840
#endif
10841
  /* Could grab some more details from the "representative"
10842
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10843
     NT_LWPSTATUS note, presumably.  */
10844
10845
  return true;
10846
}
10847
#endif /* defined (HAVE_PSTATUS_T) */
10848
10849
#if defined (HAVE_LWPSTATUS_T)
10850
static bool
10851
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10852
{
10853
  lwpstatus_t lwpstat;
10854
  char buf[100];
10855
  char *name;
10856
  size_t len;
10857
  asection *sect;
10858
10859
  if (note->descsz != sizeof (lwpstat)
10860
#if defined (HAVE_LWPXSTATUS_T)
10861
      && note->descsz != sizeof (lwpxstatus_t)
10862
#endif
10863
      )
10864
    return true;
10865
10866
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10867
10868
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10869
  /* Do not overwrite the core signal if it has already been set by
10870
     another thread.  */
10871
  if (elf_tdata (abfd)->core->signal == 0)
10872
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10873
10874
  /* Make a ".reg/999" section.  */
10875
10876
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10877
  len = strlen (buf) + 1;
10878
  name = bfd_alloc (abfd, len);
10879
  if (name == NULL)
10880
    return false;
10881
  memcpy (name, buf, len);
10882
10883
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10884
  if (sect == NULL)
10885
    return false;
10886
10887
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10888
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10889
  sect->filepos = note->descpos
10890
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10891
#endif
10892
10893
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10894
  sect->size = sizeof (lwpstat.pr_reg);
10895
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10896
#endif
10897
10898
  sect->alignment_power = 2;
10899
10900
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10901
    return false;
10902
10903
  /* Make a ".reg2/999" section */
10904
10905
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10906
  len = strlen (buf) + 1;
10907
  name = bfd_alloc (abfd, len);
10908
  if (name == NULL)
10909
    return false;
10910
  memcpy (name, buf, len);
10911
10912
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10913
  if (sect == NULL)
10914
    return false;
10915
10916
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10917
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10918
  sect->filepos = note->descpos
10919
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10920
#endif
10921
10922
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10923
  sect->size = sizeof (lwpstat.pr_fpreg);
10924
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10925
#endif
10926
10927
  sect->alignment_power = 2;
10928
10929
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10930
}
10931
#endif /* defined (HAVE_LWPSTATUS_T) */
10932
10933
/* These constants, and the structure offsets used below, are defined by
10934
   Cygwin's core_dump.h */
10935
0
#define NOTE_INFO_PROCESS  1
10936
0
#define NOTE_INFO_THREAD   2
10937
0
#define NOTE_INFO_MODULE   3
10938
0
#define NOTE_INFO_MODULE64 4
10939
10940
static bool
10941
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10942
20
{
10943
20
  char buf[30];
10944
20
  char *name;
10945
20
  size_t len;
10946
20
  unsigned int name_size;
10947
20
  asection *sect;
10948
20
  unsigned int type;
10949
20
  int is_active_thread;
10950
20
  bfd_vma base_addr;
10951
10952
20
  if (note->descsz < 4)
10953
19
    return true;
10954
10955
1
  if (! startswith (note->namedata, "win32"))
10956
1
    return true;
10957
10958
0
  type = bfd_get_32 (abfd, note->descdata);
10959
10960
0
  struct
10961
0
  {
10962
0
    const char *type_name;
10963
0
    unsigned long min_size;
10964
0
  } size_check[] =
10965
0
      {
10966
0
       { "NOTE_INFO_PROCESS", 12 },
10967
0
       { "NOTE_INFO_THREAD", 12 },
10968
0
       { "NOTE_INFO_MODULE", 12 },
10969
0
       { "NOTE_INFO_MODULE64", 16 },
10970
0
      };
10971
10972
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10973
0
      return true;
10974
10975
0
  if (note->descsz < size_check[type - 1].min_size)
10976
0
    {
10977
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10978
0
          " is too small"),
10979
0
        abfd, size_check[type - 1].type_name, note->descsz);
10980
0
      return true;
10981
0
    }
10982
10983
0
  switch (type)
10984
0
    {
10985
0
    case NOTE_INFO_PROCESS:
10986
      /* FIXME: need to add ->core->command.  */
10987
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10988
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10989
0
      break;
10990
10991
0
    case NOTE_INFO_THREAD:
10992
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10993
   structure. */
10994
      /* thread_info.tid */
10995
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10996
10997
0
      len = strlen (buf) + 1;
10998
0
      name = (char *) bfd_alloc (abfd, len);
10999
0
      if (name == NULL)
11000
0
  return false;
11001
11002
0
      memcpy (name, buf, len);
11003
11004
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11005
0
      if (sect == NULL)
11006
0
  return false;
11007
11008
      /* sizeof (thread_info.thread_context) */
11009
0
      sect->size = note->descsz - 12;
11010
      /* offsetof (thread_info.thread_context) */
11011
0
      sect->filepos = note->descpos + 12;
11012
0
      sect->alignment_power = 2;
11013
11014
      /* thread_info.is_active_thread */
11015
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11016
11017
0
      if (is_active_thread)
11018
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11019
0
    return false;
11020
0
      break;
11021
11022
0
    case NOTE_INFO_MODULE:
11023
0
    case NOTE_INFO_MODULE64:
11024
      /* Make a ".module/xxxxxxxx" section.  */
11025
0
      if (type == NOTE_INFO_MODULE)
11026
0
  {
11027
    /* module_info.base_address */
11028
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11029
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11030
    /* module_info.module_name_size */
11031
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11032
0
  }
11033
0
      else /* NOTE_INFO_MODULE64 */
11034
0
  {
11035
    /* module_info.base_address */
11036
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11037
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11038
    /* module_info.module_name_size */
11039
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11040
0
  }
11041
11042
0
      len = strlen (buf) + 1;
11043
0
      name = (char *) bfd_alloc (abfd, len);
11044
0
      if (name == NULL)
11045
0
  return false;
11046
11047
0
      memcpy (name, buf, len);
11048
11049
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11050
11051
0
      if (sect == NULL)
11052
0
  return false;
11053
11054
0
      if (note->descsz < 12 + name_size)
11055
0
  {
11056
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11057
0
        " is too small to contain a name of size %u"),
11058
0
            abfd, note->descsz, name_size);
11059
0
    return true;
11060
0
  }
11061
11062
0
      sect->size = note->descsz;
11063
0
      sect->filepos = note->descpos;
11064
0
      sect->alignment_power = 2;
11065
0
      break;
11066
11067
0
    default:
11068
0
      return true;
11069
0
    }
11070
11071
0
  return true;
11072
0
}
11073
11074
static bool
11075
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11076
17.3k
{
11077
17.3k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11078
11079
17.3k
  switch (note->type)
11080
17.3k
    {
11081
13.8k
    default:
11082
13.8k
      return true;
11083
11084
1.23k
    case NT_PRSTATUS:
11085
1.23k
      if (bed->elf_backend_grok_prstatus)
11086
720
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11087
35
    return true;
11088
1.20k
#if defined (HAVE_PRSTATUS_T)
11089
1.20k
      return elfcore_grok_prstatus (abfd, note);
11090
#else
11091
      return true;
11092
#endif
11093
11094
#if defined (HAVE_PSTATUS_T)
11095
    case NT_PSTATUS:
11096
      return elfcore_grok_pstatus (abfd, note);
11097
#endif
11098
11099
#if defined (HAVE_LWPSTATUS_T)
11100
    case NT_LWPSTATUS:
11101
      return elfcore_grok_lwpstatus (abfd, note);
11102
#endif
11103
11104
277
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
11105
277
      return elfcore_grok_prfpreg (abfd, note);
11106
11107
20
    case NT_WIN32PSTATUS:
11108
20
      return elfcore_grok_win32pstatus (abfd, note);
11109
11110
1
    case NT_PRXFPREG:   /* Linux SSE extension */
11111
1
      if (note->namesz == 6
11112
1
    && strcmp (note->namedata, "LINUX") == 0)
11113
0
  return elfcore_grok_prxfpreg (abfd, note);
11114
1
      else
11115
1
  return true;
11116
11117
9
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
11118
9
      if (note->namesz == 6
11119
9
    && strcmp (note->namedata, "LINUX") == 0)
11120
0
  return elfcore_grok_xstatereg (abfd, note);
11121
9
      else
11122
9
  return true;
11123
11124
0
    case NT_X86_SHSTK:   /* Linux CET extension.  */
11125
0
      if (note->namesz == 6
11126
0
    && strcmp (note->namedata, "LINUX") == 0)
11127
0
  return elfcore_grok_sspreg (abfd, note);
11128
0
      else
11129
0
  return true;
11130
11131
191
    case NT_PPC_VMX:
11132
191
      if (note->namesz == 6
11133
191
    && strcmp (note->namedata, "LINUX") == 0)
11134
0
  return elfcore_grok_ppc_vmx (abfd, note);
11135
191
      else
11136
191
  return true;
11137
11138
25
    case NT_PPC_VSX:
11139
25
      if (note->namesz == 6
11140
25
    && strcmp (note->namedata, "LINUX") == 0)
11141
0
  return elfcore_grok_ppc_vsx (abfd, note);
11142
25
      else
11143
25
  return true;
11144
11145
20
    case NT_PPC_TAR:
11146
20
      if (note->namesz == 6
11147
20
    && strcmp (note->namedata, "LINUX") == 0)
11148
0
  return elfcore_grok_ppc_tar (abfd, note);
11149
20
      else
11150
20
  return true;
11151
11152
14
    case NT_PPC_PPR:
11153
14
      if (note->namesz == 6
11154
14
    && strcmp (note->namedata, "LINUX") == 0)
11155
0
  return elfcore_grok_ppc_ppr (abfd, note);
11156
14
      else
11157
14
  return true;
11158
11159
8
    case NT_PPC_DSCR:
11160
8
      if (note->namesz == 6
11161
8
    && strcmp (note->namedata, "LINUX") == 0)
11162
0
  return elfcore_grok_ppc_dscr (abfd, note);
11163
8
      else
11164
8
  return true;
11165
11166
29
    case NT_PPC_EBB:
11167
29
      if (note->namesz == 6
11168
29
    && strcmp (note->namedata, "LINUX") == 0)
11169
0
  return elfcore_grok_ppc_ebb (abfd, note);
11170
29
      else
11171
29
  return true;
11172
11173
26
    case NT_PPC_PMU:
11174
26
      if (note->namesz == 6
11175
26
    && strcmp (note->namedata, "LINUX") == 0)
11176
0
  return elfcore_grok_ppc_pmu (abfd, note);
11177
26
      else
11178
26
  return true;
11179
11180
17
    case NT_PPC_TM_CGPR:
11181
17
      if (note->namesz == 6
11182
17
    && strcmp (note->namedata, "LINUX") == 0)
11183
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
11184
17
      else
11185
17
  return true;
11186
11187
3
    case NT_PPC_TM_CFPR:
11188
3
      if (note->namesz == 6
11189
3
    && strcmp (note->namedata, "LINUX") == 0)
11190
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
11191
3
      else
11192
3
  return true;
11193
11194
19
    case NT_PPC_TM_CVMX:
11195
19
      if (note->namesz == 6
11196
19
    && strcmp (note->namedata, "LINUX") == 0)
11197
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
11198
19
      else
11199
19
  return true;
11200
11201
12
    case NT_PPC_TM_CVSX:
11202
12
      if (note->namesz == 6
11203
12
    && strcmp (note->namedata, "LINUX") == 0)
11204
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
11205
12
      else
11206
12
  return true;
11207
11208
72
    case NT_PPC_TM_SPR:
11209
72
      if (note->namesz == 6
11210
72
    && strcmp (note->namedata, "LINUX") == 0)
11211
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
11212
72
      else
11213
72
  return true;
11214
11215
5
    case NT_PPC_TM_CTAR:
11216
5
      if (note->namesz == 6
11217
5
    && strcmp (note->namedata, "LINUX") == 0)
11218
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
11219
5
      else
11220
5
  return true;
11221
11222
43
    case NT_PPC_TM_CPPR:
11223
43
      if (note->namesz == 6
11224
43
    && strcmp (note->namedata, "LINUX") == 0)
11225
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
11226
43
      else
11227
43
  return true;
11228
11229
9
    case NT_PPC_TM_CDSCR:
11230
9
      if (note->namesz == 6
11231
9
    && strcmp (note->namedata, "LINUX") == 0)
11232
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
11233
9
      else
11234
9
  return true;
11235
11236
64
    case NT_S390_HIGH_GPRS:
11237
64
      if (note->namesz == 6
11238
64
    && strcmp (note->namedata, "LINUX") == 0)
11239
0
  return elfcore_grok_s390_high_gprs (abfd, note);
11240
64
      else
11241
64
  return true;
11242
11243
14
    case NT_S390_TIMER:
11244
14
      if (note->namesz == 6
11245
14
    && strcmp (note->namedata, "LINUX") == 0)
11246
0
  return elfcore_grok_s390_timer (abfd, note);
11247
14
      else
11248
14
  return true;
11249
11250
20
    case NT_S390_TODCMP:
11251
20
      if (note->namesz == 6
11252
20
    && strcmp (note->namedata, "LINUX") == 0)
11253
0
  return elfcore_grok_s390_todcmp (abfd, note);
11254
20
      else
11255
20
  return true;
11256
11257
23
    case NT_S390_TODPREG:
11258
23
      if (note->namesz == 6
11259
23
    && strcmp (note->namedata, "LINUX") == 0)
11260
0
  return elfcore_grok_s390_todpreg (abfd, note);
11261
23
      else
11262
23
  return true;
11263
11264
9
    case NT_S390_CTRS:
11265
9
      if (note->namesz == 6
11266
9
    && strcmp (note->namedata, "LINUX") == 0)
11267
0
  return elfcore_grok_s390_ctrs (abfd, note);
11268
9
      else
11269
9
  return true;
11270
11271
1
    case NT_S390_PREFIX:
11272
1
      if (note->namesz == 6
11273
1
    && strcmp (note->namedata, "LINUX") == 0)
11274
0
  return elfcore_grok_s390_prefix (abfd, note);
11275
1
      else
11276
1
  return true;
11277
11278
10
    case NT_S390_LAST_BREAK:
11279
10
      if (note->namesz == 6
11280
10
    && strcmp (note->namedata, "LINUX") == 0)
11281
0
  return elfcore_grok_s390_last_break (abfd, note);
11282
10
      else
11283
10
  return true;
11284
11285
16
    case NT_S390_SYSTEM_CALL:
11286
16
      if (note->namesz == 6
11287
16
    && strcmp (note->namedata, "LINUX") == 0)
11288
0
  return elfcore_grok_s390_system_call (abfd, note);
11289
16
      else
11290
16
  return true;
11291
11292
18
    case NT_S390_TDB:
11293
18
      if (note->namesz == 6
11294
18
    && strcmp (note->namedata, "LINUX") == 0)
11295
0
  return elfcore_grok_s390_tdb (abfd, note);
11296
18
      else
11297
18
  return true;
11298
11299
12
    case NT_S390_VXRS_LOW:
11300
12
      if (note->namesz == 6
11301
12
    && strcmp (note->namedata, "LINUX") == 0)
11302
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
11303
12
      else
11304
12
  return true;
11305
11306
5
    case NT_S390_VXRS_HIGH:
11307
5
      if (note->namesz == 6
11308
5
    && strcmp (note->namedata, "LINUX") == 0)
11309
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
11310
5
      else
11311
5
  return true;
11312
11313
6
    case NT_S390_GS_CB:
11314
6
      if (note->namesz == 6
11315
6
    && strcmp (note->namedata, "LINUX") == 0)
11316
0
  return elfcore_grok_s390_gs_cb (abfd, note);
11317
6
      else
11318
6
  return true;
11319
11320
12
    case NT_S390_GS_BC:
11321
12
      if (note->namesz == 6
11322
12
    && strcmp (note->namedata, "LINUX") == 0)
11323
0
  return elfcore_grok_s390_gs_bc (abfd, note);
11324
12
      else
11325
12
  return true;
11326
11327
28
    case NT_ARC_V2:
11328
28
      if (note->namesz == 6
11329
28
    && strcmp (note->namedata, "LINUX") == 0)
11330
0
  return elfcore_grok_arc_v2 (abfd, note);
11331
28
      else
11332
28
  return true;
11333
11334
47
    case NT_ARM_VFP:
11335
47
      if (note->namesz == 6
11336
47
    && strcmp (note->namedata, "LINUX") == 0)
11337
0
  return elfcore_grok_arm_vfp (abfd, note);
11338
47
      else
11339
47
  return true;
11340
11341
4
    case NT_ARM_TLS:
11342
4
      if (note->namesz == 6
11343
4
    && strcmp (note->namedata, "LINUX") == 0)
11344
0
  return elfcore_grok_aarch_tls (abfd, note);
11345
4
      else
11346
4
  return true;
11347
11348
4
    case NT_ARM_HW_BREAK:
11349
4
      if (note->namesz == 6
11350
4
    && strcmp (note->namedata, "LINUX") == 0)
11351
0
  return elfcore_grok_aarch_hw_break (abfd, note);
11352
4
      else
11353
4
  return true;
11354
11355
15
    case NT_ARM_HW_WATCH:
11356
15
      if (note->namesz == 6
11357
15
    && strcmp (note->namedata, "LINUX") == 0)
11358
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
11359
15
      else
11360
15
  return true;
11361
11362
0
    case NT_ARM_SVE:
11363
0
      if (note->namesz == 6
11364
0
    && strcmp (note->namedata, "LINUX") == 0)
11365
0
  return elfcore_grok_aarch_sve (abfd, note);
11366
0
      else
11367
0
  return true;
11368
11369
11
    case NT_ARM_PAC_MASK:
11370
11
      if (note->namesz == 6
11371
11
    && strcmp (note->namedata, "LINUX") == 0)
11372
0
  return elfcore_grok_aarch_pauth (abfd, note);
11373
11
      else
11374
11
  return true;
11375
11376
9
    case NT_ARM_TAGGED_ADDR_CTRL:
11377
9
      if (note->namesz == 6
11378
9
    && strcmp (note->namedata, "LINUX") == 0)
11379
0
  return elfcore_grok_aarch_mte (abfd, note);
11380
9
      else
11381
9
  return true;
11382
11383
17
    case NT_ARM_SSVE:
11384
17
      if (note->namesz == 6
11385
17
    && strcmp (note->namedata, "LINUX") == 0)
11386
0
  return elfcore_grok_aarch_ssve (abfd, note);
11387
17
      else
11388
17
  return true;
11389
11390
0
    case NT_ARM_ZA:
11391
0
      if (note->namesz == 6
11392
0
    && strcmp (note->namedata, "LINUX") == 0)
11393
0
  return elfcore_grok_aarch_za (abfd, note);
11394
0
      else
11395
0
  return true;
11396
11397
0
    case NT_ARM_ZT:
11398
0
      if (note->namesz == 6
11399
0
    && strcmp (note->namedata, "LINUX") == 0)
11400
0
  return elfcore_grok_aarch_zt (abfd, note);
11401
0
      else
11402
0
  return true;
11403
11404
38
    case NT_GDB_TDESC:
11405
38
      if (note->namesz == 4
11406
38
    && strcmp (note->namedata, "GDB") == 0)
11407
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11408
38
      else
11409
38
  return true;
11410
11411
12
    case NT_RISCV_CSR:
11412
12
      if (note->namesz == 4
11413
12
    && strcmp (note->namedata, "GDB") == 0)
11414
0
  return elfcore_grok_riscv_csr (abfd, note);
11415
12
      else
11416
12
  return true;
11417
11418
58
    case NT_LARCH_CPUCFG:
11419
58
      if (note->namesz == 6
11420
58
    && strcmp (note->namedata, "LINUX") == 0)
11421
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
11422
58
      else
11423
58
  return true;
11424
11425
10
    case NT_LARCH_LBT:
11426
10
      if (note->namesz == 6
11427
10
    && strcmp (note->namedata, "LINUX") == 0)
11428
0
  return elfcore_grok_loongarch_lbt (abfd, note);
11429
10
      else
11430
10
  return true;
11431
11432
25
    case NT_LARCH_LSX:
11433
25
      if (note->namesz == 6
11434
25
    && strcmp (note->namedata, "LINUX") == 0)
11435
0
  return elfcore_grok_loongarch_lsx (abfd, note);
11436
25
      else
11437
25
  return true;
11438
11439
32
    case NT_LARCH_LASX:
11440
32
      if (note->namesz == 6
11441
32
    && strcmp (note->namedata, "LINUX") == 0)
11442
0
  return elfcore_grok_loongarch_lasx (abfd, note);
11443
32
      else
11444
32
  return true;
11445
11446
741
    case NT_PRPSINFO:
11447
799
    case NT_PSINFO:
11448
799
      if (bed->elf_backend_grok_psinfo)
11449
425
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11450
3
    return true;
11451
796
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11452
796
      return elfcore_grok_psinfo (abfd, note);
11453
#else
11454
      return true;
11455
#endif
11456
11457
66
    case NT_AUXV:
11458
66
      return elfcore_make_auxv_note_section (abfd, note, 0);
11459
11460
4
    case NT_FILE:
11461
4
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11462
4
                note);
11463
11464
14
    case NT_SIGINFO:
11465
14
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11466
14
                note);
11467
11468
17.3k
    }
11469
17.3k
}
11470
11471
static bool
11472
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11473
1.01k
{
11474
1.01k
  struct bfd_build_id* build_id;
11475
11476
1.01k
  if (note->descsz == 0)
11477
39
    return false;
11478
11479
978
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11480
978
  if (build_id == NULL)
11481
0
    return false;
11482
11483
978
  build_id->size = note->descsz;
11484
978
  memcpy (build_id->data, note->descdata, note->descsz);
11485
978
  abfd->build_id = build_id;
11486
11487
978
  return true;
11488
978
}
11489
11490
static bool
11491
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11492
3.03k
{
11493
3.03k
  switch (note->type)
11494
3.03k
    {
11495
1.36k
    default:
11496
1.36k
      return true;
11497
11498
652
    case NT_GNU_PROPERTY_TYPE_0:
11499
652
      return _bfd_elf_parse_gnu_properties (abfd, note);
11500
11501
1.01k
    case NT_GNU_BUILD_ID:
11502
1.01k
      return elfobj_grok_gnu_build_id (abfd, note);
11503
3.03k
    }
11504
3.03k
}
11505
11506
static bool
11507
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11508
0
{
11509
0
  struct sdt_note *cur =
11510
0
    (struct sdt_note *) bfd_alloc (abfd,
11511
0
           sizeof (struct sdt_note) + note->descsz);
11512
11513
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11514
0
  cur->size = (bfd_size_type) note->descsz;
11515
0
  memcpy (cur->data, note->descdata, note->descsz);
11516
11517
0
  elf_tdata (abfd)->sdt_note_head = cur;
11518
11519
0
  return true;
11520
0
}
11521
11522
static bool
11523
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11524
0
{
11525
0
  switch (note->type)
11526
0
    {
11527
0
    case NT_STAPSDT:
11528
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11529
11530
0
    default:
11531
0
      return true;
11532
0
    }
11533
0
}
11534
11535
static bool
11536
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11537
0
{
11538
0
  size_t offset;
11539
11540
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11541
0
    {
11542
0
    case ELFCLASS32:
11543
0
      if (note->descsz < 108)
11544
0
  return false;
11545
0
      break;
11546
11547
0
    case ELFCLASS64:
11548
0
      if (note->descsz < 120)
11549
0
  return false;
11550
0
      break;
11551
11552
0
    default:
11553
0
      return false;
11554
0
    }
11555
11556
  /* Check for version 1 in pr_version.  */
11557
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11558
0
    return false;
11559
11560
0
  offset = 4;
11561
11562
  /* Skip over pr_psinfosz. */
11563
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11564
0
    offset += 4;
11565
0
  else
11566
0
    {
11567
0
      offset += 4;  /* Padding before pr_psinfosz. */
11568
0
      offset += 8;
11569
0
    }
11570
11571
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11572
0
  elf_tdata (abfd)->core->program
11573
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11574
0
  offset += 17;
11575
11576
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11577
0
  elf_tdata (abfd)->core->command
11578
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11579
0
  offset += 81;
11580
11581
  /* Padding before pr_pid.  */
11582
0
  offset += 2;
11583
11584
  /* The pr_pid field was added in version "1a".  */
11585
0
  if (note->descsz < offset + 4)
11586
0
    return true;
11587
11588
0
  elf_tdata (abfd)->core->pid
11589
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11590
11591
0
  return true;
11592
0
}
11593
11594
static bool
11595
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11596
0
{
11597
0
  size_t offset;
11598
0
  size_t size;
11599
0
  size_t min_size;
11600
11601
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11602
     Also compute minimum size of this note.  */
11603
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11604
0
    {
11605
0
    case ELFCLASS32:
11606
0
      offset = 4 + 4;
11607
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11608
0
      break;
11609
11610
0
    case ELFCLASS64:
11611
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11612
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11613
0
      break;
11614
11615
0
    default:
11616
0
      return false;
11617
0
    }
11618
11619
0
  if (note->descsz < min_size)
11620
0
    return false;
11621
11622
  /* Check for version 1 in pr_version.  */
11623
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11624
0
    return false;
11625
11626
  /* Extract size of pr_reg from pr_gregsetsz.  */
11627
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11628
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11629
0
    {
11630
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11631
0
      offset += 4 * 2;
11632
0
    }
11633
0
  else
11634
0
    {
11635
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11636
0
      offset += 8 * 2;
11637
0
    }
11638
11639
  /* Skip over pr_osreldate.  */
11640
0
  offset += 4;
11641
11642
  /* Read signal from pr_cursig.  */
11643
0
  if (elf_tdata (abfd)->core->signal == 0)
11644
0
    elf_tdata (abfd)->core->signal
11645
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11646
0
  offset += 4;
11647
11648
  /* Read TID from pr_pid.  */
11649
0
  elf_tdata (abfd)->core->lwpid
11650
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11651
0
  offset += 4;
11652
11653
  /* Padding before pr_reg.  */
11654
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11655
0
    offset += 4;
11656
11657
  /* Make sure that there is enough data remaining in the note.  */
11658
0
  if ((note->descsz - offset) < size)
11659
0
    return false;
11660
11661
  /* Make a ".reg/999" section and a ".reg" section.  */
11662
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11663
0
            size, note->descpos + offset);
11664
0
}
11665
11666
static bool
11667
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11668
13
{
11669
13
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11670
11671
13
  switch (note->type)
11672
13
    {
11673
0
    case NT_PRSTATUS:
11674
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11675
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11676
0
    return true;
11677
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11678
11679
1
    case NT_FPREGSET:
11680
1
      return elfcore_grok_prfpreg (abfd, note);
11681
11682
0
    case NT_PRPSINFO:
11683
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11684
11685
0
    case NT_FREEBSD_THRMISC:
11686
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11687
11688
0
    case NT_FREEBSD_PROCSTAT_PROC:
11689
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11690
0
                note);
11691
11692
0
    case NT_FREEBSD_PROCSTAT_FILES:
11693
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11694
0
                note);
11695
11696
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11697
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11698
0
                note);
11699
11700
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11701
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11702
11703
6
    case NT_FREEBSD_X86_SEGBASES:
11704
6
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11705
11706
0
    case NT_X86_XSTATE:
11707
0
      return elfcore_grok_xstatereg (abfd, note);
11708
11709
0
    case NT_FREEBSD_PTLWPINFO:
11710
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11711
0
                note);
11712
11713
0
    case NT_ARM_TLS:
11714
0
      return elfcore_grok_aarch_tls (abfd, note);
11715
11716
0
    case NT_ARM_VFP:
11717
0
      return elfcore_grok_arm_vfp (abfd, note);
11718
11719
6
    default:
11720
6
      return true;
11721
13
    }
11722
13
}
11723
11724
static bool
11725
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11726
0
{
11727
0
  char *cp;
11728
11729
0
  cp = strchr (note->namedata, '@');
11730
0
  if (cp != NULL)
11731
0
    {
11732
0
      *lwpidp = atoi(cp + 1);
11733
0
      return true;
11734
0
    }
11735
0
  return false;
11736
0
}
11737
11738
static bool
11739
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11740
0
{
11741
0
  if (note->descsz <= 0x7c + 31)
11742
0
    return false;
11743
11744
  /* Signal number at offset 0x08. */
11745
0
  elf_tdata (abfd)->core->signal
11746
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11747
11748
  /* Process ID at offset 0x50. */
11749
0
  elf_tdata (abfd)->core->pid
11750
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11751
11752
  /* Command name at 0x7c (max 32 bytes, including nul). */
11753
0
  elf_tdata (abfd)->core->command
11754
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11755
11756
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11757
0
            note);
11758
0
}
11759
11760
static bool
11761
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11762
0
{
11763
0
  int lwp;
11764
11765
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11766
0
    elf_tdata (abfd)->core->lwpid = lwp;
11767
11768
0
  switch (note->type)
11769
0
    {
11770
0
    case NT_NETBSDCORE_PROCINFO:
11771
      /* NetBSD-specific core "procinfo".  Note that we expect to
11772
   find this note before any of the others, which is fine,
11773
   since the kernel writes this note out first when it
11774
   creates a core file.  */
11775
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11776
0
    case NT_NETBSDCORE_AUXV:
11777
      /* NetBSD-specific Elf Auxiliary Vector data. */
11778
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11779
0
    case NT_NETBSDCORE_LWPSTATUS:
11780
0
      return elfcore_make_note_pseudosection (abfd,
11781
0
                ".note.netbsdcore.lwpstatus",
11782
0
                note);
11783
0
    default:
11784
0
      break;
11785
0
    }
11786
11787
  /* As of March 2020 there are no other machine-independent notes
11788
     defined for NetBSD core files.  If the note type is less
11789
     than the start of the machine-dependent note types, we don't
11790
     understand it.  */
11791
11792
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11793
0
    return true;
11794
11795
11796
0
  switch (bfd_get_arch (abfd))
11797
0
    {
11798
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11799
   PT_GETFPREGS == mach+2.  */
11800
11801
0
    case bfd_arch_aarch64:
11802
0
    case bfd_arch_alpha:
11803
0
    case bfd_arch_sparc:
11804
0
      switch (note->type)
11805
0
  {
11806
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11807
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11808
11809
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11810
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11811
11812
0
  default:
11813
0
    return true;
11814
0
  }
11815
11816
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11817
   There's also old PT___GETREGS40 == mach + 1 for old reg
11818
   structure which lacks GBR.  */
11819
11820
0
    case bfd_arch_sh:
11821
0
      switch (note->type)
11822
0
  {
11823
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11824
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11825
11826
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11827
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11828
11829
0
  default:
11830
0
    return true;
11831
0
  }
11832
11833
      /* On all other arch's, PT_GETREGS == mach+1 and
11834
   PT_GETFPREGS == mach+3.  */
11835
11836
0
    default:
11837
0
      switch (note->type)
11838
0
  {
11839
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11840
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11841
11842
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11843
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11844
11845
0
  default:
11846
0
    return true;
11847
0
  }
11848
0
    }
11849
    /* NOTREACHED */
11850
0
}
11851
11852
static bool
11853
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11854
0
{
11855
0
  if (note->descsz <= 0x48 + 31)
11856
0
    return false;
11857
11858
  /* Signal number at offset 0x08. */
11859
0
  elf_tdata (abfd)->core->signal
11860
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11861
11862
  /* Process ID at offset 0x20. */
11863
0
  elf_tdata (abfd)->core->pid
11864
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11865
11866
  /* Command name at 0x48 (max 32 bytes, including nul). */
11867
0
  elf_tdata (abfd)->core->command
11868
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11869
11870
0
  return true;
11871
0
}
11872
11873
/* Processes Solaris's process status note.
11874
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11875
   pid_off ~ offsetof(prstatus_t, pr_pid)
11876
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11877
   gregset_size ~ sizeof(gregset_t)
11878
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11879
11880
static bool
11881
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11882
             int pid_off, int lwpid_off, size_t gregset_size,
11883
             size_t gregset_offset)
11884
0
{
11885
0
  asection *sect = NULL;
11886
0
  elf_tdata (abfd)->core->signal
11887
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11888
0
  elf_tdata (abfd)->core->pid
11889
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11890
0
  elf_tdata (abfd)->core->lwpid
11891
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11892
11893
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11894
0
  if (sect != NULL)
11895
0
    sect->size = gregset_size;
11896
11897
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11898
0
            note->descpos + gregset_offset);
11899
0
}
11900
11901
/* Gets program and arguments from a core.
11902
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11903
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11904
11905
static bool
11906
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11907
        int prog_off, int comm_off)
11908
0
{
11909
0
  elf_tdata (abfd)->core->program
11910
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11911
0
  elf_tdata (abfd)->core->command
11912
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11913
11914
0
  return true;
11915
0
}
11916
11917
/* Processes Solaris's LWP status note.
11918
   gregset_size ~ sizeof(gregset_t)
11919
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11920
   fpregset_size ~ sizeof(fpregset_t)
11921
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11922
11923
static bool
11924
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11925
        size_t gregset_size, int gregset_off,
11926
        size_t fpregset_size, int fpregset_off)
11927
0
{
11928
0
  asection *sect = NULL;
11929
0
  char reg2_section_name[16] = { 0 };
11930
11931
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11932
0
       elf_tdata (abfd)->core->lwpid);
11933
11934
  /* offsetof(lwpstatus_t, pr_lwpid) */
11935
0
  elf_tdata (abfd)->core->lwpid
11936
0
    = bfd_get_32 (abfd, note->descdata + 4);
11937
  /* offsetof(lwpstatus_t, pr_cursig) */
11938
0
  elf_tdata (abfd)->core->signal
11939
0
    = bfd_get_16 (abfd, note->descdata + 12);
11940
11941
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11942
0
  if (sect != NULL)
11943
0
    sect->size = gregset_size;
11944
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11945
0
               note->descpos + gregset_off))
11946
0
    return false;
11947
11948
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11949
0
  if (sect != NULL)
11950
0
    {
11951
0
      sect->size = fpregset_size;
11952
0
      sect->filepos = note->descpos + fpregset_off;
11953
0
      sect->alignment_power = 2;
11954
0
    }
11955
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11956
0
               note->descpos + fpregset_off))
11957
0
    return false;
11958
11959
0
  return true;
11960
0
}
11961
11962
static bool
11963
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11964
2.57k
{
11965
2.57k
  if (note == NULL)
11966
0
    return false;
11967
11968
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11969
     by the size of the descsz which matches the sizeof()
11970
     the type appropriate for that note type (e.g., prstatus_t for
11971
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11972
     on Solaris. The core file bitness may differ from the bitness of
11973
     gdb itself, so fixed values are used instead of sizeof().
11974
     Appropriate fixed offsets are also used to obtain data from
11975
     the note.  */
11976
11977
2.57k
  switch ((int) note->type)
11978
2.57k
    {
11979
669
    case SOLARIS_NT_PRSTATUS:
11980
669
      switch (note->descsz)
11981
669
  {
11982
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11983
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11984
0
                 136, 216, 308, 152, 356);
11985
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11986
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11987
0
                 264, 360, 520, 304, 600);
11988
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11989
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11990
0
                 136, 216, 308, 76, 356);
11991
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11992
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11993
0
                 264, 360, 520, 224, 600);
11994
669
  default:
11995
669
    return true;
11996
669
  }
11997
11998
0
    case SOLARIS_NT_PSINFO:
11999
160
    case SOLARIS_NT_PRPSINFO:
12000
160
      switch (note->descsz)
12001
160
  {
12002
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12003
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
12004
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12005
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
12006
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12007
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
12008
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12009
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
12010
160
  default:
12011
160
    return true;
12012
160
  }
12013
12014
0
    case SOLARIS_NT_LWPSTATUS:
12015
0
      switch (note->descsz)
12016
0
  {
12017
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12018
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12019
0
            152, 344, 400, 496);
12020
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12021
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12022
0
            304, 544, 544, 848);
12023
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12024
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12025
0
            76, 344, 380, 420);
12026
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12027
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12028
0
            224, 544, 528, 768);
12029
0
  default:
12030
0
    return true;
12031
0
  }
12032
12033
0
    case SOLARIS_NT_LWPSINFO:
12034
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12035
0
      if (note->descsz == 128 || note->descsz == 152)
12036
0
  elf_tdata (abfd)->core->lwpid =
12037
0
    bfd_get_32 (abfd, note->descdata + 4);
12038
0
      break;
12039
12040
1.74k
    default:
12041
1.74k
      break;
12042
2.57k
    }
12043
12044
1.74k
  return true;
12045
2.57k
}
12046
12047
/* For name starting with "CORE" this may be either a Solaris
12048
   core file or a gdb-generated core file.  Do Solaris-specific
12049
   processing on selected note types first with
12050
   elfcore_grok_solaris_note(), then process the note
12051
   in elfcore_grok_note().  */
12052
12053
static bool
12054
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12055
2.57k
{
12056
2.57k
  if (!elfcore_grok_solaris_note_impl (abfd, note))
12057
0
    return false;
12058
12059
2.57k
  return elfcore_grok_note (abfd, note);
12060
2.57k
}
12061
12062
static bool
12063
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12064
0
{
12065
0
  if (note->type == NT_OPENBSD_PROCINFO)
12066
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
12067
12068
0
  if (note->type == NT_OPENBSD_REGS)
12069
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
12070
12071
0
  if (note->type == NT_OPENBSD_FPREGS)
12072
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12073
12074
0
  if (note->type == NT_OPENBSD_XFPREGS)
12075
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12076
12077
0
  if (note->type == NT_OPENBSD_AUXV)
12078
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
12079
12080
0
  if (note->type == NT_OPENBSD_WCOOKIE)
12081
0
    {
12082
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12083
0
                 SEC_HAS_CONTENTS);
12084
12085
0
      if (sect == NULL)
12086
0
  return false;
12087
0
      sect->size = note->descsz;
12088
0
      sect->filepos = note->descpos;
12089
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12090
12091
0
      return true;
12092
0
    }
12093
12094
0
  return true;
12095
0
}
12096
12097
static bool
12098
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12099
0
{
12100
0
  void *ddata = note->descdata;
12101
0
  char buf[100];
12102
0
  char *name;
12103
0
  asection *sect;
12104
0
  short sig;
12105
0
  unsigned flags;
12106
12107
0
  if (note->descsz < 16)
12108
0
    return false;
12109
12110
  /* nto_procfs_status 'pid' field is at offset 0.  */
12111
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12112
12113
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
12114
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12115
12116
  /* nto_procfs_status 'flags' field is at offset 8.  */
12117
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12118
12119
  /* nto_procfs_status 'what' field is at offset 14.  */
12120
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12121
0
    {
12122
0
      elf_tdata (abfd)->core->signal = sig;
12123
0
      elf_tdata (abfd)->core->lwpid = *tid;
12124
0
    }
12125
12126
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
12127
     do not come from signals so we make sure we set the current
12128
     thread just in case.  */
12129
0
  if (flags & 0x00000080)
12130
0
    elf_tdata (abfd)->core->lwpid = *tid;
12131
12132
  /* Make a ".qnx_core_status/%d" section.  */
12133
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
12134
12135
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12136
0
  if (name == NULL)
12137
0
    return false;
12138
0
  strcpy (name, buf);
12139
12140
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12141
0
  if (sect == NULL)
12142
0
    return false;
12143
12144
0
  sect->size    = note->descsz;
12145
0
  sect->filepos   = note->descpos;
12146
0
  sect->alignment_power = 2;
12147
12148
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12149
0
}
12150
12151
static bool
12152
elfcore_grok_nto_regs (bfd *abfd,
12153
           Elf_Internal_Note *note,
12154
           long tid,
12155
           char *base)
12156
0
{
12157
0
  char buf[100];
12158
0
  char *name;
12159
0
  asection *sect;
12160
12161
  /* Make a "(base)/%d" section.  */
12162
0
  sprintf (buf, "%s/%ld", base, tid);
12163
12164
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12165
0
  if (name == NULL)
12166
0
    return false;
12167
0
  strcpy (name, buf);
12168
12169
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12170
0
  if (sect == NULL)
12171
0
    return false;
12172
12173
0
  sect->size    = note->descsz;
12174
0
  sect->filepos   = note->descpos;
12175
0
  sect->alignment_power = 2;
12176
12177
  /* This is the current thread.  */
12178
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12179
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12180
12181
0
  return true;
12182
0
}
12183
12184
static bool
12185
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12186
1
{
12187
  /* Every GREG section has a STATUS section before it.  Store the
12188
     tid from the previous call to pass down to the next gregs
12189
     function.  */
12190
1
  static long tid = 1;
12191
12192
1
  switch (note->type)
12193
1
    {
12194
0
    case QNT_CORE_INFO:
12195
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12196
0
    case QNT_CORE_STATUS:
12197
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12198
0
    case QNT_CORE_GREG:
12199
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12200
0
    case QNT_CORE_FPREG:
12201
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12202
1
    default:
12203
1
      return true;
12204
1
    }
12205
1
}
12206
12207
static bool
12208
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12209
3
{
12210
3
  char *name;
12211
3
  asection *sect;
12212
3
  size_t len;
12213
12214
  /* Use note name as section name.  */
12215
3
  len = note->namesz;
12216
3
  name = (char *) bfd_alloc (abfd, len);
12217
3
  if (name == NULL)
12218
0
    return false;
12219
3
  memcpy (name, note->namedata, len);
12220
3
  name[len - 1] = '\0';
12221
12222
3
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12223
3
  if (sect == NULL)
12224
0
    return false;
12225
12226
3
  sect->size    = note->descsz;
12227
3
  sect->filepos   = note->descpos;
12228
3
  sect->alignment_power = 1;
12229
12230
3
  return true;
12231
3
}
12232
12233
/* Function: elfcore_write_note
12234
12235
   Inputs:
12236
     buffer to hold note, and current size of buffer
12237
     name of note
12238
     type of note
12239
     data for note
12240
     size of data for note
12241
12242
   Writes note to end of buffer.  ELF64 notes are written exactly as
12243
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12244
   that they ought to have 8-byte namesz and descsz field, and have
12245
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12246
12247
   Return:
12248
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12249
12250
char *
12251
elfcore_write_note (bfd *abfd,
12252
        char *buf,
12253
        int *bufsiz,
12254
        const char *name,
12255
        int type,
12256
        const void *input,
12257
        int size)
12258
0
{
12259
0
  Elf_External_Note *xnp;
12260
0
  size_t namesz;
12261
0
  size_t newspace;
12262
0
  char *dest;
12263
12264
0
  namesz = 0;
12265
0
  if (name != NULL)
12266
0
    namesz = strlen (name) + 1;
12267
12268
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12269
12270
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12271
0
  if (buf == NULL)
12272
0
    return buf;
12273
0
  dest = buf + *bufsiz;
12274
0
  *bufsiz += newspace;
12275
0
  xnp = (Elf_External_Note *) dest;
12276
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12277
0
  H_PUT_32 (abfd, size, xnp->descsz);
12278
0
  H_PUT_32 (abfd, type, xnp->type);
12279
0
  dest = xnp->name;
12280
0
  if (name != NULL)
12281
0
    {
12282
0
      memcpy (dest, name, namesz);
12283
0
      dest += namesz;
12284
0
      while (namesz & 3)
12285
0
  {
12286
0
    *dest++ = '\0';
12287
0
    ++namesz;
12288
0
  }
12289
0
    }
12290
0
  memcpy (dest, input, size);
12291
0
  dest += size;
12292
0
  while (size & 3)
12293
0
    {
12294
0
      *dest++ = '\0';
12295
0
      ++size;
12296
0
    }
12297
0
  return buf;
12298
0
}
12299
12300
/* gcc-8 warns (*) on all the strncpy calls in this function about
12301
   possible string truncation.  The "truncation" is not a bug.  We
12302
   have an external representation of structs with fields that are not
12303
   necessarily NULL terminated and corresponding internal
12304
   representation fields that are one larger so that they can always
12305
   be NULL terminated.
12306
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12307
   diagnostics inside functions, giving a hard error if you try to use
12308
   the finer control available with later versions.
12309
   gcc prior to 4.2 warns about diagnostic push and pop.
12310
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12311
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12312
   (*) Depending on your system header files!  */
12313
#if GCC_VERSION >= 8000
12314
# pragma GCC diagnostic push
12315
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12316
#endif
12317
char *
12318
elfcore_write_prpsinfo (bfd  *abfd,
12319
      char *buf,
12320
      int  *bufsiz,
12321
      const char *fname,
12322
      const char *psargs)
12323
0
{
12324
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12325
12326
0
  if (bed->elf_backend_write_core_note != NULL)
12327
0
    {
12328
0
      char *ret;
12329
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12330
0
             NT_PRPSINFO, fname, psargs);
12331
0
      if (ret != NULL)
12332
0
  return ret;
12333
0
    }
12334
12335
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12336
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12337
0
  if (bed->s->elfclass == ELFCLASS32)
12338
0
    {
12339
#  if defined (HAVE_PSINFO32_T)
12340
      psinfo32_t data;
12341
      int note_type = NT_PSINFO;
12342
#  else
12343
0
      prpsinfo32_t data;
12344
0
      int note_type = NT_PRPSINFO;
12345
0
#  endif
12346
12347
0
      memset (&data, 0, sizeof (data));
12348
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12349
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12350
0
      return elfcore_write_note (abfd, buf, bufsiz,
12351
0
         "CORE", note_type, &data, sizeof (data));
12352
0
    }
12353
0
  else
12354
0
# endif
12355
0
    {
12356
# if defined (HAVE_PSINFO_T)
12357
      psinfo_t data;
12358
      int note_type = NT_PSINFO;
12359
# else
12360
0
      prpsinfo_t data;
12361
0
      int note_type = NT_PRPSINFO;
12362
0
# endif
12363
12364
0
      memset (&data, 0, sizeof (data));
12365
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12366
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12367
0
      return elfcore_write_note (abfd, buf, bufsiz,
12368
0
         "CORE", note_type, &data, sizeof (data));
12369
0
    }
12370
0
#endif  /* PSINFO_T or PRPSINFO_T */
12371
12372
0
  free (buf);
12373
0
  return NULL;
12374
0
}
12375
#if GCC_VERSION >= 8000
12376
# pragma GCC diagnostic pop
12377
#endif
12378
12379
char *
12380
elfcore_write_linux_prpsinfo32
12381
  (bfd *abfd, char *buf, int *bufsiz,
12382
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12383
0
{
12384
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12385
0
    {
12386
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12387
12388
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12389
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12390
0
         &data, sizeof (data));
12391
0
    }
12392
0
  else
12393
0
    {
12394
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12395
12396
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12397
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12398
0
         &data, sizeof (data));
12399
0
    }
12400
0
}
12401
12402
char *
12403
elfcore_write_linux_prpsinfo64
12404
  (bfd *abfd, char *buf, int *bufsiz,
12405
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12406
0
{
12407
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12408
0
    {
12409
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12410
12411
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12412
0
      return elfcore_write_note (abfd, buf, bufsiz,
12413
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12414
0
    }
12415
0
  else
12416
0
    {
12417
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12418
12419
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12420
0
      return elfcore_write_note (abfd, buf, bufsiz,
12421
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12422
0
    }
12423
0
}
12424
12425
char *
12426
elfcore_write_prstatus (bfd *abfd,
12427
      char *buf,
12428
      int *bufsiz,
12429
      long pid,
12430
      int cursig,
12431
      const void *gregs)
12432
0
{
12433
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12434
12435
0
  if (bed->elf_backend_write_core_note != NULL)
12436
0
    {
12437
0
      char *ret;
12438
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12439
0
             NT_PRSTATUS,
12440
0
             pid, cursig, gregs);
12441
0
      if (ret != NULL)
12442
0
  return ret;
12443
0
    }
12444
12445
0
#if defined (HAVE_PRSTATUS_T)
12446
0
#if defined (HAVE_PRSTATUS32_T)
12447
0
  if (bed->s->elfclass == ELFCLASS32)
12448
0
    {
12449
0
      prstatus32_t prstat;
12450
12451
0
      memset (&prstat, 0, sizeof (prstat));
12452
0
      prstat.pr_pid = pid;
12453
0
      prstat.pr_cursig = cursig;
12454
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12455
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12456
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12457
0
    }
12458
0
  else
12459
0
#endif
12460
0
    {
12461
0
      prstatus_t prstat;
12462
12463
0
      memset (&prstat, 0, sizeof (prstat));
12464
0
      prstat.pr_pid = pid;
12465
0
      prstat.pr_cursig = cursig;
12466
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12467
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12468
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12469
0
    }
12470
0
#endif /* HAVE_PRSTATUS_T */
12471
12472
0
  free (buf);
12473
0
  return NULL;
12474
0
}
12475
12476
#if defined (HAVE_LWPSTATUS_T)
12477
char *
12478
elfcore_write_lwpstatus (bfd *abfd,
12479
       char *buf,
12480
       int *bufsiz,
12481
       long pid,
12482
       int cursig,
12483
       const void *gregs)
12484
{
12485
  lwpstatus_t lwpstat;
12486
  const char *note_name = "CORE";
12487
12488
  memset (&lwpstat, 0, sizeof (lwpstat));
12489
  lwpstat.pr_lwpid  = pid >> 16;
12490
  lwpstat.pr_cursig = cursig;
12491
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12492
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12493
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12494
#if !defined(gregs)
12495
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12496
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12497
#else
12498
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12499
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12500
#endif
12501
#endif
12502
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
12503
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12504
}
12505
#endif /* HAVE_LWPSTATUS_T */
12506
12507
#if defined (HAVE_PSTATUS_T)
12508
char *
12509
elfcore_write_pstatus (bfd *abfd,
12510
           char *buf,
12511
           int *bufsiz,
12512
           long pid,
12513
           int cursig ATTRIBUTE_UNUSED,
12514
           const void *gregs ATTRIBUTE_UNUSED)
12515
{
12516
  const char *note_name = "CORE";
12517
#if defined (HAVE_PSTATUS32_T)
12518
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12519
12520
  if (bed->s->elfclass == ELFCLASS32)
12521
    {
12522
      pstatus32_t pstat;
12523
12524
      memset (&pstat, 0, sizeof (pstat));
12525
      pstat.pr_pid = pid & 0xffff;
12526
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12527
        NT_PSTATUS, &pstat, sizeof (pstat));
12528
      return buf;
12529
    }
12530
  else
12531
#endif
12532
    {
12533
      pstatus_t pstat;
12534
12535
      memset (&pstat, 0, sizeof (pstat));
12536
      pstat.pr_pid = pid & 0xffff;
12537
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12538
        NT_PSTATUS, &pstat, sizeof (pstat));
12539
      return buf;
12540
    }
12541
}
12542
#endif /* HAVE_PSTATUS_T */
12543
12544
char *
12545
elfcore_write_prfpreg (bfd *abfd,
12546
           char *buf,
12547
           int *bufsiz,
12548
           const void *fpregs,
12549
           int size)
12550
0
{
12551
0
  const char *note_name = "CORE";
12552
0
  return elfcore_write_note (abfd, buf, bufsiz,
12553
0
           note_name, NT_FPREGSET, fpregs, size);
12554
0
}
12555
12556
char *
12557
elfcore_write_prxfpreg (bfd *abfd,
12558
      char *buf,
12559
      int *bufsiz,
12560
      const void *xfpregs,
12561
      int size)
12562
0
{
12563
0
  char *note_name = "LINUX";
12564
0
  return elfcore_write_note (abfd, buf, bufsiz,
12565
0
           note_name, NT_PRXFPREG, xfpregs, size);
12566
0
}
12567
12568
char *
12569
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12570
       const void *xfpregs, int size)
12571
0
{
12572
0
  char *note_name;
12573
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12574
0
    note_name = "FreeBSD";
12575
0
  else
12576
0
    note_name = "LINUX";
12577
0
  return elfcore_write_note (abfd, buf, bufsiz,
12578
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12579
0
}
12580
12581
static char *
12582
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12583
          const void *ssp, int size)
12584
0
{
12585
0
  const char *note_name = "LINUX";
12586
0
  return elfcore_write_note (abfd, buf, bufsiz,
12587
0
           note_name, NT_X86_SHSTK, ssp, size);
12588
0
}
12589
12590
char *
12591
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12592
          const void *regs, int size)
12593
0
{
12594
0
  char *note_name = "FreeBSD";
12595
0
  return elfcore_write_note (abfd, buf, bufsiz,
12596
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12597
0
}
12598
12599
char *
12600
elfcore_write_ppc_vmx (bfd *abfd,
12601
           char *buf,
12602
           int *bufsiz,
12603
           const void *ppc_vmx,
12604
           int size)
12605
0
{
12606
0
  char *note_name = "LINUX";
12607
0
  return elfcore_write_note (abfd, buf, bufsiz,
12608
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
12609
0
}
12610
12611
char *
12612
elfcore_write_ppc_vsx (bfd *abfd,
12613
           char *buf,
12614
           int *bufsiz,
12615
           const void *ppc_vsx,
12616
           int size)
12617
0
{
12618
0
  char *note_name = "LINUX";
12619
0
  return elfcore_write_note (abfd, buf, bufsiz,
12620
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
12621
0
}
12622
12623
char *
12624
elfcore_write_ppc_tar (bfd *abfd,
12625
           char *buf,
12626
           int *bufsiz,
12627
           const void *ppc_tar,
12628
           int size)
12629
0
{
12630
0
  char *note_name = "LINUX";
12631
0
  return elfcore_write_note (abfd, buf, bufsiz,
12632
0
           note_name, NT_PPC_TAR, ppc_tar, size);
12633
0
}
12634
12635
char *
12636
elfcore_write_ppc_ppr (bfd *abfd,
12637
           char *buf,
12638
           int *bufsiz,
12639
           const void *ppc_ppr,
12640
           int size)
12641
0
{
12642
0
  char *note_name = "LINUX";
12643
0
  return elfcore_write_note (abfd, buf, bufsiz,
12644
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12645
0
}
12646
12647
char *
12648
elfcore_write_ppc_dscr (bfd *abfd,
12649
      char *buf,
12650
      int *bufsiz,
12651
      const void *ppc_dscr,
12652
      int size)
12653
0
{
12654
0
  char *note_name = "LINUX";
12655
0
  return elfcore_write_note (abfd, buf, bufsiz,
12656
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12657
0
}
12658
12659
char *
12660
elfcore_write_ppc_ebb (bfd *abfd,
12661
           char *buf,
12662
           int *bufsiz,
12663
           const void *ppc_ebb,
12664
           int size)
12665
0
{
12666
0
  char *note_name = "LINUX";
12667
0
  return elfcore_write_note (abfd, buf, bufsiz,
12668
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12669
0
}
12670
12671
char *
12672
elfcore_write_ppc_pmu (bfd *abfd,
12673
           char *buf,
12674
           int *bufsiz,
12675
           const void *ppc_pmu,
12676
           int size)
12677
0
{
12678
0
  char *note_name = "LINUX";
12679
0
  return elfcore_write_note (abfd, buf, bufsiz,
12680
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12681
0
}
12682
12683
char *
12684
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12685
         char *buf,
12686
         int *bufsiz,
12687
         const void *ppc_tm_cgpr,
12688
         int size)
12689
0
{
12690
0
  char *note_name = "LINUX";
12691
0
  return elfcore_write_note (abfd, buf, bufsiz,
12692
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12693
0
}
12694
12695
char *
12696
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12697
         char *buf,
12698
         int *bufsiz,
12699
         const void *ppc_tm_cfpr,
12700
         int size)
12701
0
{
12702
0
  char *note_name = "LINUX";
12703
0
  return elfcore_write_note (abfd, buf, bufsiz,
12704
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12705
0
}
12706
12707
char *
12708
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12709
         char *buf,
12710
         int *bufsiz,
12711
         const void *ppc_tm_cvmx,
12712
         int size)
12713
0
{
12714
0
  char *note_name = "LINUX";
12715
0
  return elfcore_write_note (abfd, buf, bufsiz,
12716
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12717
0
}
12718
12719
char *
12720
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12721
         char *buf,
12722
         int *bufsiz,
12723
         const void *ppc_tm_cvsx,
12724
         int size)
12725
0
{
12726
0
  char *note_name = "LINUX";
12727
0
  return elfcore_write_note (abfd, buf, bufsiz,
12728
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12729
0
}
12730
12731
char *
12732
elfcore_write_ppc_tm_spr (bfd *abfd,
12733
        char *buf,
12734
        int *bufsiz,
12735
        const void *ppc_tm_spr,
12736
        int size)
12737
0
{
12738
0
  char *note_name = "LINUX";
12739
0
  return elfcore_write_note (abfd, buf, bufsiz,
12740
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12741
0
}
12742
12743
char *
12744
elfcore_write_ppc_tm_ctar (bfd *abfd,
12745
         char *buf,
12746
         int *bufsiz,
12747
         const void *ppc_tm_ctar,
12748
         int size)
12749
0
{
12750
0
  char *note_name = "LINUX";
12751
0
  return elfcore_write_note (abfd, buf, bufsiz,
12752
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12753
0
}
12754
12755
char *
12756
elfcore_write_ppc_tm_cppr (bfd *abfd,
12757
         char *buf,
12758
         int *bufsiz,
12759
         const void *ppc_tm_cppr,
12760
         int size)
12761
0
{
12762
0
  char *note_name = "LINUX";
12763
0
  return elfcore_write_note (abfd, buf, bufsiz,
12764
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12765
0
}
12766
12767
char *
12768
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12769
          char *buf,
12770
          int *bufsiz,
12771
          const void *ppc_tm_cdscr,
12772
          int size)
12773
0
{
12774
0
  char *note_name = "LINUX";
12775
0
  return elfcore_write_note (abfd, buf, bufsiz,
12776
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12777
0
}
12778
12779
static char *
12780
elfcore_write_s390_high_gprs (bfd *abfd,
12781
            char *buf,
12782
            int *bufsiz,
12783
            const void *s390_high_gprs,
12784
            int size)
12785
0
{
12786
0
  char *note_name = "LINUX";
12787
0
  return elfcore_write_note (abfd, buf, bufsiz,
12788
0
           note_name, NT_S390_HIGH_GPRS,
12789
0
           s390_high_gprs, size);
12790
0
}
12791
12792
char *
12793
elfcore_write_s390_timer (bfd *abfd,
12794
        char *buf,
12795
        int *bufsiz,
12796
        const void *s390_timer,
12797
        int size)
12798
0
{
12799
0
  char *note_name = "LINUX";
12800
0
  return elfcore_write_note (abfd, buf, bufsiz,
12801
0
           note_name, NT_S390_TIMER, s390_timer, size);
12802
0
}
12803
12804
char *
12805
elfcore_write_s390_todcmp (bfd *abfd,
12806
         char *buf,
12807
         int *bufsiz,
12808
         const void *s390_todcmp,
12809
         int size)
12810
0
{
12811
0
  char *note_name = "LINUX";
12812
0
  return elfcore_write_note (abfd, buf, bufsiz,
12813
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12814
0
}
12815
12816
char *
12817
elfcore_write_s390_todpreg (bfd *abfd,
12818
          char *buf,
12819
          int *bufsiz,
12820
          const void *s390_todpreg,
12821
          int size)
12822
0
{
12823
0
  char *note_name = "LINUX";
12824
0
  return elfcore_write_note (abfd, buf, bufsiz,
12825
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12826
0
}
12827
12828
char *
12829
elfcore_write_s390_ctrs (bfd *abfd,
12830
       char *buf,
12831
       int *bufsiz,
12832
       const void *s390_ctrs,
12833
       int size)
12834
0
{
12835
0
  char *note_name = "LINUX";
12836
0
  return elfcore_write_note (abfd, buf, bufsiz,
12837
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12838
0
}
12839
12840
char *
12841
elfcore_write_s390_prefix (bfd *abfd,
12842
         char *buf,
12843
         int *bufsiz,
12844
         const void *s390_prefix,
12845
         int size)
12846
0
{
12847
0
  char *note_name = "LINUX";
12848
0
  return elfcore_write_note (abfd, buf, bufsiz,
12849
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12850
0
}
12851
12852
char *
12853
elfcore_write_s390_last_break (bfd *abfd,
12854
             char *buf,
12855
             int *bufsiz,
12856
             const void *s390_last_break,
12857
             int size)
12858
0
{
12859
0
  char *note_name = "LINUX";
12860
0
  return elfcore_write_note (abfd, buf, bufsiz,
12861
0
           note_name, NT_S390_LAST_BREAK,
12862
0
           s390_last_break, size);
12863
0
}
12864
12865
char *
12866
elfcore_write_s390_system_call (bfd *abfd,
12867
        char *buf,
12868
        int *bufsiz,
12869
        const void *s390_system_call,
12870
        int size)
12871
0
{
12872
0
  char *note_name = "LINUX";
12873
0
  return elfcore_write_note (abfd, buf, bufsiz,
12874
0
           note_name, NT_S390_SYSTEM_CALL,
12875
0
           s390_system_call, size);
12876
0
}
12877
12878
char *
12879
elfcore_write_s390_tdb (bfd *abfd,
12880
      char *buf,
12881
      int *bufsiz,
12882
      const void *s390_tdb,
12883
      int size)
12884
0
{
12885
0
  char *note_name = "LINUX";
12886
0
  return elfcore_write_note (abfd, buf, bufsiz,
12887
0
           note_name, NT_S390_TDB, s390_tdb, size);
12888
0
}
12889
12890
char *
12891
elfcore_write_s390_vxrs_low (bfd *abfd,
12892
           char *buf,
12893
           int *bufsiz,
12894
           const void *s390_vxrs_low,
12895
           int size)
12896
0
{
12897
0
  char *note_name = "LINUX";
12898
0
  return elfcore_write_note (abfd, buf, bufsiz,
12899
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12900
0
}
12901
12902
char *
12903
elfcore_write_s390_vxrs_high (bfd *abfd,
12904
           char *buf,
12905
           int *bufsiz,
12906
           const void *s390_vxrs_high,
12907
           int size)
12908
0
{
12909
0
  char *note_name = "LINUX";
12910
0
  return elfcore_write_note (abfd, buf, bufsiz,
12911
0
           note_name, NT_S390_VXRS_HIGH,
12912
0
           s390_vxrs_high, size);
12913
0
}
12914
12915
char *
12916
elfcore_write_s390_gs_cb (bfd *abfd,
12917
        char *buf,
12918
        int *bufsiz,
12919
        const void *s390_gs_cb,
12920
        int size)
12921
0
{
12922
0
  char *note_name = "LINUX";
12923
0
  return elfcore_write_note (abfd, buf, bufsiz,
12924
0
           note_name, NT_S390_GS_CB,
12925
0
           s390_gs_cb, size);
12926
0
}
12927
12928
char *
12929
elfcore_write_s390_gs_bc (bfd *abfd,
12930
        char *buf,
12931
        int *bufsiz,
12932
        const void *s390_gs_bc,
12933
        int size)
12934
0
{
12935
0
  char *note_name = "LINUX";
12936
0
  return elfcore_write_note (abfd, buf, bufsiz,
12937
0
           note_name, NT_S390_GS_BC,
12938
0
           s390_gs_bc, size);
12939
0
}
12940
12941
char *
12942
elfcore_write_arm_vfp (bfd *abfd,
12943
           char *buf,
12944
           int *bufsiz,
12945
           const void *arm_vfp,
12946
           int size)
12947
0
{
12948
0
  char *note_name = "LINUX";
12949
0
  return elfcore_write_note (abfd, buf, bufsiz,
12950
0
           note_name, NT_ARM_VFP, arm_vfp, size);
12951
0
}
12952
12953
char *
12954
elfcore_write_aarch_tls (bfd *abfd,
12955
           char *buf,
12956
           int *bufsiz,
12957
           const void *aarch_tls,
12958
           int size)
12959
0
{
12960
0
  char *note_name = "LINUX";
12961
0
  return elfcore_write_note (abfd, buf, bufsiz,
12962
0
           note_name, NT_ARM_TLS, aarch_tls, size);
12963
0
}
12964
12965
char *
12966
elfcore_write_aarch_hw_break (bfd *abfd,
12967
          char *buf,
12968
          int *bufsiz,
12969
          const void *aarch_hw_break,
12970
          int size)
12971
0
{
12972
0
  char *note_name = "LINUX";
12973
0
  return elfcore_write_note (abfd, buf, bufsiz,
12974
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12975
0
}
12976
12977
char *
12978
elfcore_write_aarch_hw_watch (bfd *abfd,
12979
          char *buf,
12980
          int *bufsiz,
12981
          const void *aarch_hw_watch,
12982
          int size)
12983
0
{
12984
0
  char *note_name = "LINUX";
12985
0
  return elfcore_write_note (abfd, buf, bufsiz,
12986
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12987
0
}
12988
12989
char *
12990
elfcore_write_aarch_sve (bfd *abfd,
12991
       char *buf,
12992
       int *bufsiz,
12993
       const void *aarch_sve,
12994
       int size)
12995
0
{
12996
0
  char *note_name = "LINUX";
12997
0
  return elfcore_write_note (abfd, buf, bufsiz,
12998
0
           note_name, NT_ARM_SVE, aarch_sve, size);
12999
0
}
13000
13001
char *
13002
elfcore_write_aarch_pauth (bfd *abfd,
13003
         char *buf,
13004
         int *bufsiz,
13005
         const void *aarch_pauth,
13006
         int size)
13007
0
{
13008
0
  char *note_name = "LINUX";
13009
0
  return elfcore_write_note (abfd, buf, bufsiz,
13010
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13011
0
}
13012
13013
char *
13014
elfcore_write_aarch_mte (bfd *abfd,
13015
              char *buf,
13016
              int *bufsiz,
13017
              const void *aarch_mte,
13018
              int size)
13019
0
{
13020
0
  char *note_name = "LINUX";
13021
0
  return elfcore_write_note (abfd, buf, bufsiz,
13022
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
13023
0
           aarch_mte,
13024
0
           size);
13025
0
}
13026
13027
char *
13028
elfcore_write_aarch_ssve (bfd *abfd,
13029
        char *buf,
13030
        int *bufsiz,
13031
        const void *aarch_ssve,
13032
        int size)
13033
0
{
13034
0
  char *note_name = "LINUX";
13035
0
  return elfcore_write_note (abfd, buf, bufsiz,
13036
0
           note_name, NT_ARM_SSVE,
13037
0
           aarch_ssve,
13038
0
           size);
13039
0
}
13040
13041
char *
13042
elfcore_write_aarch_za (bfd *abfd,
13043
      char *buf,
13044
      int *bufsiz,
13045
      const void *aarch_za,
13046
      int size)
13047
0
{
13048
0
  char *note_name = "LINUX";
13049
0
  return elfcore_write_note (abfd, buf, bufsiz,
13050
0
           note_name, NT_ARM_ZA,
13051
0
           aarch_za,
13052
0
           size);
13053
0
}
13054
13055
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
13056
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13057
   written into.  Return a pointer to the new start of the note buffer, to
13058
   replace BUF which may no longer be valid.  */
13059
13060
char *
13061
elfcore_write_aarch_zt (bfd *abfd,
13062
      char *buf,
13063
      int *bufsiz,
13064
      const void *aarch_zt,
13065
      int size)
13066
0
{
13067
0
  char *note_name = "LINUX";
13068
0
  return elfcore_write_note (abfd, buf, bufsiz,
13069
0
           note_name, NT_ARM_ZT,
13070
0
           aarch_zt,
13071
0
           size);
13072
0
}
13073
13074
char *
13075
elfcore_write_arc_v2 (bfd *abfd,
13076
          char *buf,
13077
          int *bufsiz,
13078
          const void *arc_v2,
13079
          int size)
13080
0
{
13081
0
  char *note_name = "LINUX";
13082
0
  return elfcore_write_note (abfd, buf, bufsiz,
13083
0
           note_name, NT_ARC_V2, arc_v2, size);
13084
0
}
13085
13086
char *
13087
elfcore_write_loongarch_cpucfg (bfd *abfd,
13088
        char *buf,
13089
        int *bufsiz,
13090
        const void *loongarch_cpucfg,
13091
        int size)
13092
0
{
13093
0
  char *note_name = "LINUX";
13094
0
  return elfcore_write_note (abfd, buf, bufsiz,
13095
0
           note_name, NT_LARCH_CPUCFG,
13096
0
           loongarch_cpucfg, size);
13097
0
}
13098
13099
char *
13100
elfcore_write_loongarch_lbt (bfd *abfd,
13101
           char *buf,
13102
           int *bufsiz,
13103
           const void *loongarch_lbt,
13104
           int size)
13105
0
{
13106
0
  char *note_name = "LINUX";
13107
0
  return elfcore_write_note (abfd, buf, bufsiz,
13108
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
13109
0
}
13110
13111
char *
13112
elfcore_write_loongarch_lsx (bfd *abfd,
13113
           char *buf,
13114
           int *bufsiz,
13115
           const void *loongarch_lsx,
13116
           int size)
13117
0
{
13118
0
  char *note_name = "LINUX";
13119
0
  return elfcore_write_note (abfd, buf, bufsiz,
13120
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
13121
0
}
13122
13123
char *
13124
elfcore_write_loongarch_lasx (bfd *abfd,
13125
            char *buf,
13126
            int *bufsiz,
13127
            const void *loongarch_lasx,
13128
            int size)
13129
0
{
13130
0
  char *note_name = "LINUX";
13131
0
  return elfcore_write_note (abfd, buf, bufsiz,
13132
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
13133
0
}
13134
13135
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13136
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13137
   written into.  Return a pointer to the new start of the note buffer, to
13138
   replace BUF which may no longer be valid.  */
13139
13140
char *
13141
elfcore_write_riscv_csr (bfd *abfd,
13142
       char *buf,
13143
       int *bufsiz,
13144
       const void *csrs,
13145
       int size)
13146
0
{
13147
0
  const char *note_name = "GDB";
13148
0
  return elfcore_write_note (abfd, buf, bufsiz,
13149
0
           note_name, NT_RISCV_CSR, csrs, size);
13150
0
}
13151
13152
/* Write the target description (a string) pointed to by TDESC, length
13153
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13154
   note is being written into.  Return a pointer to the new start of the
13155
   note buffer, to replace BUF which may no longer be valid.  */
13156
13157
char *
13158
elfcore_write_gdb_tdesc (bfd *abfd,
13159
       char *buf,
13160
       int *bufsiz,
13161
       const void *tdesc,
13162
       int size)
13163
0
{
13164
0
  const char *note_name = "GDB";
13165
0
  return elfcore_write_note (abfd, buf, bufsiz,
13166
0
           note_name, NT_GDB_TDESC, tdesc, size);
13167
0
}
13168
13169
char *
13170
elfcore_write_register_note (bfd *abfd,
13171
           char *buf,
13172
           int *bufsiz,
13173
           const char *section,
13174
           const void *data,
13175
           int size)
13176
0
{
13177
0
  if (strcmp (section, ".reg2") == 0)
13178
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13179
0
  if (strcmp (section, ".reg-xfp") == 0)
13180
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13181
0
  if (strcmp (section, ".reg-xstate") == 0)
13182
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13183
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
13184
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13185
0
  if (strcmp (section, ".reg-ssp") == 0)
13186
0
    return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
13187
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
13188
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13189
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
13190
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13191
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
13192
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13193
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
13194
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13195
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
13196
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13197
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
13198
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13199
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
13200
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13201
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13202
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13203
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13204
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13205
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13206
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13207
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13208
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13209
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13210
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13211
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13212
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13213
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13214
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13215
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13216
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13217
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
13218
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13219
0
  if (strcmp (section, ".reg-s390-timer") == 0)
13220
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13221
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
13222
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13223
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
13224
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13225
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
13226
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13227
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
13228
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13229
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
13230
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13231
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
13232
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13233
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
13234
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13235
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13236
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13237
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13238
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13239
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
13240
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13241
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
13242
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13243
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
13244
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13245
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
13246
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13247
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
13248
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13249
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13250
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13251
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
13252
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13253
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
13254
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13255
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
13256
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13257
0
  if (strcmp (section, ".reg-aarch-ssve") == 0)
13258
0
    return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13259
0
  if (strcmp (section, ".reg-aarch-za") == 0)
13260
0
    return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13261
0
  if (strcmp (section, ".reg-aarch-zt") == 0)
13262
0
    return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13263
0
  if (strcmp (section, ".reg-arc-v2") == 0)
13264
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13265
0
  if (strcmp (section, ".gdb-tdesc") == 0)
13266
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13267
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
13268
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13269
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13270
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13271
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
13272
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13273
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
13274
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13275
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
13276
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13277
0
  return NULL;
13278
0
}
13279
13280
char *
13281
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13282
       const void *buf, int bufsiz)
13283
0
{
13284
0
  return elfcore_write_note (obfd, note_data, note_size,
13285
0
           "CORE", NT_FILE, buf, bufsiz);
13286
0
}
13287
13288
static bool
13289
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13290
     size_t align)
13291
62.5k
{
13292
62.5k
  char *p;
13293
13294
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13295
     gABI specifies that PT_NOTE alignment should be aligned to 4
13296
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13297
     align is less than 4, we use 4 byte alignment.   */
13298
62.5k
  if (align < 4)
13299
23.3k
    align = 4;
13300
62.5k
  if (align != 4 && align != 8)
13301
25.4k
    return false;
13302
13303
37.0k
  p = buf;
13304
106k
  while (p < buf + size)
13305
101k
    {
13306
101k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13307
101k
      Elf_Internal_Note in;
13308
13309
101k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13310
7.80k
  return false;
13311
13312
93.8k
      in.type = H_GET_32 (abfd, xnp->type);
13313
13314
93.8k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13315
93.8k
      in.namedata = xnp->name;
13316
93.8k
      if (in.namesz > buf - in.namedata + size)
13317
15.9k
  return false;
13318
13319
77.8k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13320
77.8k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13321
77.8k
      in.descpos = offset + (in.descdata - buf);
13322
77.8k
      if (in.descsz != 0
13323
77.8k
    && (in.descdata >= buf + size
13324
28.4k
        || in.descsz > buf - in.descdata + size))
13325
7.50k
  return false;
13326
13327
70.3k
      switch (bfd_get_format (abfd))
13328
70.3k
  {
13329
0
  default:
13330
0
    return true;
13331
13332
17.4k
  case bfd_core:
13333
17.4k
    {
13334
139k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13335
17.4k
      struct
13336
17.4k
      {
13337
17.4k
        const char * string;
13338
17.4k
        size_t len;
13339
17.4k
        bool (*func) (bfd *, Elf_Internal_Note *);
13340
17.4k
      }
13341
17.4k
      grokers[] =
13342
17.4k
      {
13343
17.4k
        GROKER_ELEMENT ("", elfcore_grok_note),
13344
17.4k
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13345
17.4k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13346
17.4k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13347
17.4k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13348
17.4k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13349
17.4k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13350
17.4k
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13351
17.4k
      };
13352
17.4k
#undef GROKER_ELEMENT
13353
17.4k
      int i;
13354
13355
120k
      for (i = ARRAY_SIZE (grokers); i--;)
13356
120k
        {
13357
120k
    if (in.namesz >= grokers[i].len
13358
120k
        && strncmp (in.namedata, grokers[i].string,
13359
53.4k
        grokers[i].len) == 0)
13360
17.4k
      {
13361
17.4k
        if (! grokers[i].func (abfd, & in))
13362
42
          return false;
13363
17.4k
        break;
13364
17.4k
      }
13365
120k
        }
13366
17.4k
      break;
13367
17.4k
    }
13368
13369
52.9k
  case bfd_object:
13370
52.9k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13371
2.90k
      {
13372
2.90k
        if (! elfobj_grok_gnu_note (abfd, &in))
13373
438
    return false;
13374
2.90k
      }
13375
50.0k
    else if (in.namesz == sizeof "stapsdt"
13376
50.0k
       && strcmp (in.namedata, "stapsdt") == 0)
13377
0
      {
13378
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13379
0
    return false;
13380
0
      }
13381
52.4k
    break;
13382
70.3k
  }
13383
13384
69.9k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13385
69.9k
    }
13386
13387
5.37k
  return true;
13388
37.0k
}
13389
13390
bool
13391
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13392
    size_t align)
13393
5.63k
{
13394
5.63k
  char *buf;
13395
13396
5.63k
  if (size == 0 || (size + 1) == 0)
13397
593
    return true;
13398
13399
5.03k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13400
1.11k
    return false;
13401
13402
3.92k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13403
3.92k
  if (buf == NULL)
13404
1.51k
    return false;
13405
13406
  /* PR 17512: file: ec08f814
13407
     0-termintate the buffer so that string searches will not overflow.  */
13408
2.41k
  buf[size] = 0;
13409
13410
2.41k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13411
2.31k
    {
13412
2.31k
      free (buf);
13413
2.31k
      return false;
13414
2.31k
    }
13415
13416
95
  free (buf);
13417
95
  return true;
13418
2.41k
}
13419

13420
/* Providing external access to the ELF program header table.  */
13421
13422
/* Return an upper bound on the number of bytes required to store a
13423
   copy of ABFD's program header table entries.  Return -1 if an error
13424
   occurs; bfd_get_error will return an appropriate code.  */
13425
13426
long
13427
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13428
0
{
13429
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13430
0
    {
13431
0
      bfd_set_error (bfd_error_wrong_format);
13432
0
      return -1;
13433
0
    }
13434
13435
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13436
0
}
13437
13438
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13439
   will be stored as an array of Elf_Internal_Phdr structures, as
13440
   defined in include/elf/internal.h.  To find out how large the
13441
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13442
13443
   Return the number of program header table entries read, or -1 if an
13444
   error occurs; bfd_get_error will return an appropriate code.  */
13445
13446
int
13447
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13448
0
{
13449
0
  int num_phdrs;
13450
13451
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13452
0
    {
13453
0
      bfd_set_error (bfd_error_wrong_format);
13454
0
      return -1;
13455
0
    }
13456
13457
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13458
0
  if (num_phdrs != 0)
13459
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13460
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13461
13462
0
  return num_phdrs;
13463
0
}
13464
13465
enum elf_reloc_type_class
13466
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13467
         const asection *rel_sec ATTRIBUTE_UNUSED,
13468
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13469
0
{
13470
0
  return reloc_class_normal;
13471
0
}
13472
13473
/* For RELA architectures, return the relocation value for a
13474
   relocation against a local symbol.  */
13475
13476
bfd_vma
13477
_bfd_elf_rela_local_sym (bfd *abfd,
13478
       Elf_Internal_Sym *sym,
13479
       asection **psec,
13480
       Elf_Internal_Rela *rel)
13481
0
{
13482
0
  asection *sec = *psec;
13483
0
  bfd_vma relocation;
13484
13485
0
  relocation = (sec->output_section->vma
13486
0
    + sec->output_offset
13487
0
    + sym->st_value);
13488
0
  if ((sec->flags & SEC_MERGE)
13489
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13490
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13491
0
    {
13492
0
      rel->r_addend =
13493
0
  _bfd_merged_section_offset (abfd, psec,
13494
0
            elf_section_data (sec)->sec_info,
13495
0
            sym->st_value + rel->r_addend);
13496
0
      if (sec != *psec)
13497
0
  {
13498
    /* If we have changed the section, and our original section is
13499
       marked with SEC_EXCLUDE, it means that the original
13500
       SEC_MERGE section has been completely subsumed in some
13501
       other SEC_MERGE section.  In this case, we need to leave
13502
       some info around for --emit-relocs.  */
13503
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13504
0
      sec->kept_section = *psec;
13505
0
    sec = *psec;
13506
0
  }
13507
0
      rel->r_addend -= relocation;
13508
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13509
0
    }
13510
0
  return relocation;
13511
0
}
13512
13513
bfd_vma
13514
_bfd_elf_rel_local_sym (bfd *abfd,
13515
      Elf_Internal_Sym *sym,
13516
      asection **psec,
13517
      bfd_vma addend)
13518
0
{
13519
0
  asection *sec = *psec;
13520
13521
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13522
0
    return sym->st_value + addend;
13523
13524
0
  return _bfd_merged_section_offset (abfd, psec,
13525
0
             elf_section_data (sec)->sec_info,
13526
0
             sym->st_value + addend);
13527
0
}
13528
13529
/* Adjust an address within a section.  Given OFFSET within SEC, return
13530
   the new offset within the section, based upon changes made to the
13531
   section.  Returns -1 if the offset is now invalid.
13532
   The offset (in abnd out) is in target sized bytes, however big a
13533
   byte may be.  */
13534
13535
bfd_vma
13536
_bfd_elf_section_offset (bfd *abfd,
13537
       struct bfd_link_info *info,
13538
       asection *sec,
13539
       bfd_vma offset)
13540
0
{
13541
0
  switch (sec->sec_info_type)
13542
0
    {
13543
0
    case SEC_INFO_TYPE_STABS:
13544
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13545
0
               offset);
13546
0
    case SEC_INFO_TYPE_EH_FRAME:
13547
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13548
13549
0
    default:
13550
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13551
0
  {
13552
    /* Reverse the offset.  */
13553
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13554
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13555
13556
    /* address_size and sec->size are in octets.  Convert
13557
       to bytes before subtracting the original offset.  */
13558
0
    offset = ((sec->size - address_size)
13559
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13560
0
  }
13561
0
      return offset;
13562
0
    }
13563
0
}
13564

13565
long
13566
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13567
             long symcount ATTRIBUTE_UNUSED,
13568
             asymbol **syms ATTRIBUTE_UNUSED,
13569
             long dynsymcount,
13570
             asymbol **dynsyms,
13571
             asymbol **ret)
13572
4.72k
{
13573
4.72k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13574
4.72k
  asection *relplt;
13575
4.72k
  asymbol *s;
13576
4.72k
  const char *relplt_name;
13577
4.72k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13578
4.72k
  arelent *p;
13579
4.72k
  long count, i, n;
13580
4.72k
  size_t size;
13581
4.72k
  Elf_Internal_Shdr *hdr;
13582
4.72k
  char *names;
13583
4.72k
  asection *plt;
13584
13585
4.72k
  *ret = NULL;
13586
13587
4.72k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13588
4.63k
    return 0;
13589
13590
88
  if (dynsymcount <= 0)
13591
55
    return 0;
13592
13593
33
  if (!bed->plt_sym_val)
13594
21
    return 0;
13595
13596
12
  relplt_name = bed->relplt_name;
13597
12
  if (relplt_name == NULL)
13598
12
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13599
12
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13600
12
  if (relplt == NULL)
13601
0
    return 0;
13602
13603
12
  hdr = &elf_section_data (relplt)->this_hdr;
13604
12
  if (hdr->sh_link != elf_dynsymtab (abfd)
13605
12
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13606
0
    return 0;
13607
13608
12
  plt = bfd_get_section_by_name (abfd, ".plt");
13609
12
  if (plt == NULL)
13610
0
    return 0;
13611
13612
12
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13613
12
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13614
0
    return -1;
13615
13616
12
  count = NUM_SHDR_ENTRIES (hdr);
13617
12
  size = count * sizeof (asymbol);
13618
12
  p = relplt->relocation;
13619
2.27k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13620
2.26k
    {
13621
2.26k
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13622
2.26k
      if (p->addend != 0)
13623
0
  {
13624
0
#ifdef BFD64
13625
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13626
#else
13627
    size += sizeof ("+0x") - 1 + 8;
13628
#endif
13629
0
  }
13630
2.26k
    }
13631
13632
12
  s = *ret = (asymbol *) bfd_malloc (size);
13633
12
  if (s == NULL)
13634
0
    return -1;
13635
13636
12
  names = (char *) (s + count);
13637
12
  p = relplt->relocation;
13638
12
  n = 0;
13639
2.27k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13640
2.26k
    {
13641
2.26k
      size_t len;
13642
2.26k
      bfd_vma addr;
13643
13644
2.26k
      addr = bed->plt_sym_val (i, plt, p);
13645
2.26k
      if (addr == (bfd_vma) -1)
13646
0
  continue;
13647
13648
2.26k
      *s = **p->sym_ptr_ptr;
13649
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13650
   we are defining a symbol, ensure one of them is set.  */
13651
2.26k
      if ((s->flags & BSF_LOCAL) == 0)
13652
2.26k
  s->flags |= BSF_GLOBAL;
13653
2.26k
      s->flags |= BSF_SYNTHETIC;
13654
2.26k
      s->section = plt;
13655
2.26k
      s->value = addr - plt->vma;
13656
2.26k
      s->name = names;
13657
2.26k
      s->udata.p = NULL;
13658
2.26k
      len = strlen ((*p->sym_ptr_ptr)->name);
13659
2.26k
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13660
2.26k
      names += len;
13661
2.26k
      if (p->addend != 0)
13662
0
  {
13663
0
    char buf[30], *a;
13664
13665
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13666
0
    names += sizeof ("+0x") - 1;
13667
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13668
0
    for (a = buf; *a == '0'; ++a)
13669
0
      ;
13670
0
    len = strlen (a);
13671
0
    memcpy (names, a, len);
13672
0
    names += len;
13673
0
  }
13674
2.26k
      memcpy (names, "@plt", sizeof ("@plt"));
13675
2.26k
      names += sizeof ("@plt");
13676
2.26k
      ++s, ++n;
13677
2.26k
    }
13678
13679
12
  return n;
13680
12
}
13681
13682
/* It is only used by x86-64 so far.
13683
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
13684
   but current usage would allow all of _bfd_std_section to be zero.  */
13685
static const asymbol lcomm_sym
13686
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13687
asection _bfd_elf_large_com_section
13688
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13689
          "LARGE_COMMON", 0, SEC_IS_COMMON);
13690
13691
bool
13692
_bfd_elf_final_write_processing (bfd *abfd)
13693
95
{
13694
95
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13695
13696
95
  i_ehdrp = elf_elfheader (abfd);
13697
13698
95
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13699
27
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13700
13701
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13702
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13703
     or STB_GNU_UNIQUE binding.  */
13704
95
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13705
0
    {
13706
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13707
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13708
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13709
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13710
0
  {
13711
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13712
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13713
0
          "and FreeBSD targets"));
13714
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13715
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13716
0
          "only by GNU and FreeBSD targets"));
13717
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13718
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13719
0
          "only by GNU and FreeBSD targets"));
13720
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13721
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13722
0
          "only by GNU and FreeBSD targets"));
13723
0
    bfd_set_error (bfd_error_sorry);
13724
0
    return false;
13725
0
  }
13726
0
    }
13727
95
  return true;
13728
95
}
13729
13730
13731
/* Return TRUE for ELF symbol types that represent functions.
13732
   This is the default version of this function, which is sufficient for
13733
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13734
13735
bool
13736
_bfd_elf_is_function_type (unsigned int type)
13737
0
{
13738
0
  return (type == STT_FUNC
13739
0
    || type == STT_GNU_IFUNC);
13740
0
}
13741
13742
/* If the ELF symbol SYM might be a function in SEC, return the
13743
   function size and set *CODE_OFF to the function's entry point,
13744
   otherwise return zero.  */
13745
13746
bfd_size_type
13747
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13748
           bfd_vma *code_off)
13749
46.6M
{
13750
46.6M
  bfd_size_type size;
13751
46.6M
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13752
13753
46.6M
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13754
46.6M
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13755
46.6M
      || sym->section != sec)
13756
41.4M
    return 0;
13757
13758
5.23M
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13759
13760
  /* In theory we should check that the symbol's type satisfies
13761
     _bfd_elf_is_function_type(), but there are some function-like
13762
     symbols which would fail this test.  (eg _start).  Instead
13763
     we check for hidden, local, notype symbols with zero size.
13764
     This type of symbol is generated by the annobin plugin for gcc
13765
     and clang, and should not be considered to be a function symbol.  */
13766
5.23M
  if (size == 0
13767
5.23M
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13768
5.23M
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13769
5.23M
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13770
51
    return 0;
13771
13772
5.23M
  *code_off = sym->value;
13773
  /* Do not return 0 for the function's size.  */
13774
5.23M
  return size ? size : 1;
13775
5.23M
}
13776
13777
/* Set to non-zero to enable some debug messages.  */
13778
#define DEBUG_SECONDARY_RELOCS   0
13779
13780
/* An internal-to-the-bfd-library only section type
13781
   used to indicate a cached secondary reloc section.  */
13782
17.9k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13783
13784
/* Create a BFD section to hold a secondary reloc section.  */
13785
13786
bool
13787
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13788
               Elf_Internal_Shdr *hdr,
13789
               const char * name,
13790
               unsigned int shindex)
13791
8.56k
{
13792
  /* We only support RELA secondary relocs.  */
13793
8.56k
  if (hdr->sh_type != SHT_RELA)
13794
3.87k
    return false;
13795
13796
#if DEBUG_SECONDARY_RELOCS
13797
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13798
#endif
13799
4.69k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13800
4.69k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13801
8.56k
}
13802
13803
/* Read in any secondary relocs associated with SEC.  */
13804
13805
bool
13806
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13807
          asection *  sec,
13808
          asymbol **  symbols,
13809
          bool dynamic)
13810
17.1k
{
13811
17.1k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13812
17.1k
  asection * relsec;
13813
17.1k
  bool result = true;
13814
17.1k
  bfd_vma (*r_sym) (bfd_vma);
13815
17.1k
  ufile_ptr filesize;
13816
13817
17.1k
#if BFD_DEFAULT_TARGET_SIZE > 32
13818
17.1k
  if (bfd_arch_bits_per_address (abfd) != 32)
13819
15.1k
    r_sym = elf64_r_sym;
13820
2.02k
  else
13821
2.02k
#endif
13822
2.02k
    r_sym = elf32_r_sym;
13823
13824
17.1k
  if (!elf_section_data (sec)->has_secondary_relocs)
13825
17.1k
    return true;
13826
13827
  /* Discover if there are any secondary reloc sections
13828
     associated with SEC.  */
13829
77
  filesize = bfd_get_file_size (abfd);
13830
6.66k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13831
6.58k
    {
13832
6.58k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13833
13834
6.58k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13835
6.58k
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13836
6.58k
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13837
77
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13838
77
  {
13839
77
    bfd_byte * native_relocs;
13840
77
    bfd_byte * native_reloc;
13841
77
    arelent * internal_relocs;
13842
77
    arelent * internal_reloc;
13843
77
    size_t i;
13844
77
    unsigned int entsize;
13845
77
    unsigned int symcount;
13846
77
    bfd_size_type reloc_count;
13847
77
    size_t amt;
13848
13849
77
    if (ebd->elf_info_to_howto == NULL)
13850
0
      return false;
13851
13852
#if DEBUG_SECONDARY_RELOCS
13853
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13854
       sec->name, relsec->name);
13855
#endif
13856
77
    entsize = hdr->sh_entsize;
13857
13858
77
    if (filesize != 0
13859
77
        && ((ufile_ptr) hdr->sh_offset > filesize
13860
77
      || hdr->sh_size > filesize - hdr->sh_offset))
13861
20
      {
13862
20
        bfd_set_error (bfd_error_file_truncated);
13863
20
        result = false;
13864
20
        continue;
13865
20
      }
13866
13867
57
    native_relocs = bfd_malloc (hdr->sh_size);
13868
57
    if (native_relocs == NULL)
13869
0
      {
13870
0
        result = false;
13871
0
        continue;
13872
0
      }
13873
13874
57
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13875
57
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13876
0
      {
13877
0
        free (native_relocs);
13878
0
        bfd_set_error (bfd_error_file_too_big);
13879
0
        result = false;
13880
0
        continue;
13881
0
      }
13882
13883
57
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13884
57
    if (internal_relocs == NULL)
13885
0
      {
13886
0
        free (native_relocs);
13887
0
        result = false;
13888
0
        continue;
13889
0
      }
13890
13891
57
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13892
57
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13893
0
      {
13894
0
        free (native_relocs);
13895
        /* The internal_relocs will be freed when
13896
     the memory for the bfd is released.  */
13897
0
        result = false;
13898
0
        continue;
13899
0
      }
13900
13901
57
    if (dynamic)
13902
0
      symcount = bfd_get_dynamic_symcount (abfd);
13903
57
    else
13904
57
      symcount = bfd_get_symcount (abfd);
13905
13906
57
    for (i = 0, internal_reloc = internal_relocs,
13907
57
     native_reloc = native_relocs;
13908
548
         i < reloc_count;
13909
491
         i++, internal_reloc++, native_reloc += entsize)
13910
491
      {
13911
491
        bool res;
13912
491
        Elf_Internal_Rela rela;
13913
13914
491
        if (entsize == ebd->s->sizeof_rel)
13915
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13916
491
        else /* entsize == ebd->s->sizeof_rela */
13917
491
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13918
13919
        /* The address of an ELF reloc is section relative for an object
13920
     file, and absolute for an executable file or shared library.
13921
     The address of a normal BFD reloc is always section relative,
13922
     and the address of a dynamic reloc is absolute..  */
13923
491
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13924
491
    internal_reloc->address = rela.r_offset;
13925
0
        else
13926
0
    internal_reloc->address = rela.r_offset - sec->vma;
13927
13928
491
        if (r_sym (rela.r_info) == STN_UNDEF)
13929
63
    {
13930
      /* FIXME: This and the error case below mean that we
13931
         have a symbol on relocs that is not elf_symbol_type.  */
13932
63
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13933
63
    }
13934
428
        else if (r_sym (rela.r_info) > symcount)
13935
0
    {
13936
0
      _bfd_error_handler
13937
        /* xgettext:c-format */
13938
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13939
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13940
0
      bfd_set_error (bfd_error_bad_value);
13941
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13942
0
      result = false;
13943
0
    }
13944
428
        else
13945
428
    {
13946
428
      asymbol **ps;
13947
13948
428
      ps = symbols + r_sym (rela.r_info) - 1;
13949
428
      internal_reloc->sym_ptr_ptr = ps;
13950
      /* Make sure that this symbol is not removed by strip.  */
13951
428
      (*ps)->flags |= BSF_KEEP;
13952
428
    }
13953
13954
491
        internal_reloc->addend = rela.r_addend;
13955
13956
491
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13957
491
        if (! res || internal_reloc->howto == NULL)
13958
22
    {
13959
#if DEBUG_SECONDARY_RELOCS
13960
      fprintf (stderr,
13961
         "there is no howto associated with reloc %lx\n",
13962
         rela.r_info);
13963
#endif
13964
22
      result = false;
13965
22
    }
13966
491
      }
13967
13968
57
    free (native_relocs);
13969
    /* Store the internal relocs.  */
13970
57
    elf_section_data (relsec)->sec_info = internal_relocs;
13971
57
  }
13972
6.58k
    }
13973
13974
77
  return result;
13975
77
}
13976
13977
/* Set the ELF section header fields of an output secondary reloc section.  */
13978
13979
bool
13980
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13981
              bfd *obfd ATTRIBUTE_UNUSED,
13982
              const Elf_Internal_Shdr *isection,
13983
              Elf_Internal_Shdr *osection)
13984
82
{
13985
82
  asection * isec;
13986
82
  asection * osec;
13987
82
  struct bfd_elf_section_data * esd;
13988
13989
82
  if (isection == NULL)
13990
0
    return false;
13991
13992
82
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13993
82
    return true;
13994
13995
0
  isec = isection->bfd_section;
13996
0
  if (isec == NULL)
13997
0
    return false;
13998
13999
0
  osec = osection->bfd_section;
14000
0
  if (osec == NULL)
14001
0
    return false;
14002
14003
0
  esd = elf_section_data (osec);
14004
0
  BFD_ASSERT (esd->sec_info == NULL);
14005
0
  esd->sec_info = elf_section_data (isec)->sec_info;
14006
0
  osection->sh_type = SHT_RELA;
14007
0
  osection->sh_link = elf_onesymtab (obfd);
14008
0
  if (osection->sh_link == 0)
14009
0
    {
14010
      /* There is no symbol table - we are hosed...  */
14011
0
      _bfd_error_handler
14012
  /* xgettext:c-format */
14013
0
  (_("%pB(%pA): link section cannot be set"
14014
0
     " because the output file does not have a symbol table"),
14015
0
  obfd, osec);
14016
0
      bfd_set_error (bfd_error_bad_value);
14017
0
      return false;
14018
0
    }
14019
14020
  /* Find the output section that corresponds to the isection's
14021
     sh_info link.  */
14022
0
  if (isection->sh_info == 0
14023
0
      || isection->sh_info >= elf_numsections (ibfd))
14024
0
    {
14025
0
      _bfd_error_handler
14026
  /* xgettext:c-format */
14027
0
  (_("%pB(%pA): info section index is invalid"),
14028
0
  obfd, osec);
14029
0
      bfd_set_error (bfd_error_bad_value);
14030
0
      return false;
14031
0
    }
14032
14033
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
14034
14035
0
  if (isection == NULL
14036
0
      || isection->bfd_section == NULL
14037
0
      || isection->bfd_section->output_section == NULL)
14038
0
    {
14039
0
      _bfd_error_handler
14040
  /* xgettext:c-format */
14041
0
  (_("%pB(%pA): info section index cannot be set"
14042
0
     " because the section is not in the output"),
14043
0
  obfd, osec);
14044
0
      bfd_set_error (bfd_error_bad_value);
14045
0
      return false;
14046
0
    }
14047
14048
0
  esd = elf_section_data (isection->bfd_section->output_section);
14049
0
  BFD_ASSERT (esd != NULL);
14050
0
  osection->sh_info = esd->this_idx;
14051
0
  esd->has_secondary_relocs = true;
14052
#if DEBUG_SECONDARY_RELOCS
14053
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14054
     osec->name, osection->sh_link, osection->sh_info);
14055
  fprintf (stderr, "mark section %s as having secondary relocs\n",
14056
     bfd_section_name (isection->bfd_section->output_section));
14057
#endif
14058
14059
0
  return true;
14060
0
}
14061
14062
/* Write out a secondary reloc section.
14063
14064
   FIXME: Currently this function can result in a serious performance penalty
14065
   for files with secondary relocs and lots of sections.  The proper way to
14066
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14067
   relocs together and then to have this function just walk that chain.  */
14068
14069
bool
14070
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14071
0
{
14072
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14073
0
  bfd_vma addr_offset;
14074
0
  asection * relsec;
14075
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
14076
0
  bool result = true;
14077
14078
0
  if (sec == NULL)
14079
0
    return false;
14080
14081
0
#if BFD_DEFAULT_TARGET_SIZE > 32
14082
0
  if (bfd_arch_bits_per_address (abfd) != 32)
14083
0
    r_info = elf64_r_info;
14084
0
  else
14085
0
#endif
14086
0
    r_info = elf32_r_info;
14087
14088
  /* The address of an ELF reloc is section relative for an object
14089
     file, and absolute for an executable file or shared library.
14090
     The address of a BFD reloc is always section relative.  */
14091
0
  addr_offset = 0;
14092
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14093
0
    addr_offset = sec->vma;
14094
14095
  /* Discover if there are any secondary reloc sections
14096
     associated with SEC.  */
14097
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14098
0
    {
14099
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14100
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14101
14102
0
      if (hdr->sh_type == SHT_RELA
14103
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14104
0
  {
14105
0
    asymbol *    last_sym;
14106
0
    int          last_sym_idx;
14107
0
    size_t       reloc_count;
14108
0
    size_t       idx;
14109
0
    bfd_size_type entsize;
14110
0
    arelent *    src_irel;
14111
0
    bfd_byte *   dst_rela;
14112
14113
0
    if (hdr->contents != NULL)
14114
0
      {
14115
0
        _bfd_error_handler
14116
    /* xgettext:c-format */
14117
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
14118
0
     abfd, relsec);
14119
0
        bfd_set_error (bfd_error_bad_value);
14120
0
        result = false;
14121
0
        continue;
14122
0
      }
14123
14124
0
    entsize = hdr->sh_entsize;
14125
0
    if (entsize == 0)
14126
0
      {
14127
0
        _bfd_error_handler
14128
    /* xgettext:c-format */
14129
0
    (_("%pB(%pA): error: secondary reloc section"
14130
0
       " has zero sized entries"),
14131
0
     abfd, relsec);
14132
0
        bfd_set_error (bfd_error_bad_value);
14133
0
        result = false;
14134
0
        continue;
14135
0
      }
14136
0
    else if (entsize != ebd->s->sizeof_rel
14137
0
       && entsize != ebd->s->sizeof_rela)
14138
0
      {
14139
0
        _bfd_error_handler
14140
    /* xgettext:c-format */
14141
0
    (_("%pB(%pA): error: secondary reloc section"
14142
0
       " has non-standard sized entries"),
14143
0
     abfd, relsec);
14144
0
        bfd_set_error (bfd_error_bad_value);
14145
0
        result = false;
14146
0
        continue;
14147
0
      }
14148
14149
0
    reloc_count = hdr->sh_size / entsize;
14150
0
    hdr->sh_size = entsize * reloc_count;
14151
0
    if (reloc_count == 0)
14152
0
      {
14153
0
        _bfd_error_handler
14154
    /* xgettext:c-format */
14155
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
14156
0
     abfd, relsec);
14157
0
        bfd_set_error (bfd_error_bad_value);
14158
0
        result = false;
14159
0
        continue;
14160
0
      }
14161
14162
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14163
0
    if (hdr->contents == NULL)
14164
0
      continue;
14165
0
    relsec->alloced = 1;
14166
14167
#if DEBUG_SECONDARY_RELOCS
14168
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14169
       reloc_count, sec->name, relsec->name);
14170
#endif
14171
0
    last_sym = NULL;
14172
0
    last_sym_idx = 0;
14173
0
    dst_rela = hdr->contents;
14174
0
    src_irel = (arelent *) esd->sec_info;
14175
0
    if (src_irel == NULL)
14176
0
      {
14177
0
        _bfd_error_handler
14178
    /* xgettext:c-format */
14179
0
    (_("%pB(%pA): error: internal relocs missing"
14180
0
       " for secondary reloc section"),
14181
0
     abfd, relsec);
14182
0
        bfd_set_error (bfd_error_bad_value);
14183
0
        result = false;
14184
0
        continue;
14185
0
      }
14186
14187
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14188
0
      {
14189
0
        Elf_Internal_Rela src_rela;
14190
0
        arelent *ptr;
14191
0
        asymbol *sym;
14192
0
        int n;
14193
14194
0
        ptr = src_irel + idx;
14195
0
        if (ptr == NULL)
14196
0
    {
14197
0
      _bfd_error_handler
14198
        /* xgettext:c-format */
14199
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14200
0
         abfd, relsec, idx);
14201
0
      bfd_set_error (bfd_error_bad_value);
14202
0
      result = false;
14203
0
      break;
14204
0
    }
14205
14206
0
        if (ptr->sym_ptr_ptr == NULL)
14207
0
    {
14208
      /* FIXME: Is this an error ? */
14209
0
      n = 0;
14210
0
    }
14211
0
        else
14212
0
    {
14213
0
      sym = *ptr->sym_ptr_ptr;
14214
14215
0
      if (sym == last_sym)
14216
0
        n = last_sym_idx;
14217
0
      else
14218
0
        {
14219
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14220
0
          if (n < 0)
14221
0
      {
14222
0
        _bfd_error_handler
14223
          /* xgettext:c-format */
14224
0
          (_("%pB(%pA): error: secondary reloc %zu"
14225
0
             " references a missing symbol"),
14226
0
           abfd, relsec, idx);
14227
0
        bfd_set_error (bfd_error_bad_value);
14228
0
        result = false;
14229
0
        n = 0;
14230
0
      }
14231
14232
0
          last_sym = sym;
14233
0
          last_sym_idx = n;
14234
0
        }
14235
14236
0
      if (sym->the_bfd != NULL
14237
0
          && sym->the_bfd->xvec != abfd->xvec
14238
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14239
0
        {
14240
0
          _bfd_error_handler
14241
      /* xgettext:c-format */
14242
0
      (_("%pB(%pA): error: secondary reloc %zu"
14243
0
         " references a deleted symbol"),
14244
0
       abfd, relsec, idx);
14245
0
          bfd_set_error (bfd_error_bad_value);
14246
0
          result = false;
14247
0
          n = 0;
14248
0
        }
14249
0
    }
14250
14251
0
        src_rela.r_offset = ptr->address + addr_offset;
14252
0
        if (ptr->howto == NULL)
14253
0
    {
14254
0
      _bfd_error_handler
14255
        /* xgettext:c-format */
14256
0
        (_("%pB(%pA): error: secondary reloc %zu"
14257
0
           " is of an unknown type"),
14258
0
         abfd, relsec, idx);
14259
0
      bfd_set_error (bfd_error_bad_value);
14260
0
      result = false;
14261
0
      src_rela.r_info = r_info (0, 0);
14262
0
    }
14263
0
        else
14264
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14265
0
        src_rela.r_addend = ptr->addend;
14266
14267
0
        if (entsize == ebd->s->sizeof_rel)
14268
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14269
0
        else /* entsize == ebd->s->sizeof_rela */
14270
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14271
0
      }
14272
0
  }
14273
0
    }
14274
14275
0
  return result;
14276
0
}
14277
14278
/* Mmap in section contents.  */
14279
14280
static bool
14281
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14282
68.1k
{
14283
68.1k
#ifdef USE_MMAP
14284
68.1k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14285
68.1k
  if (bed->use_mmap
14286
68.1k
      && sec->compress_status == COMPRESS_SECTION_NONE
14287
68.1k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14288
19.4k
    {
14289
      /* Use mmap only if section size >= the minimum mmap section
14290
   size.  */
14291
19.4k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14292
19.4k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14293
19.4k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14294
4.39k
  {
14295
4.39k
    if (sec->contents != NULL)
14296
0
      {
14297
0
        if (!sec->mmapped_p)
14298
0
    abort ();
14299
0
        *buf = sec->contents;
14300
0
        return true;
14301
0
      }
14302
4.39k
    if (sec->mmapped_p)
14303
0
      abort ();
14304
4.39k
    sec->mmapped_p = 1;
14305
14306
    /* We can't use the final link preallocated buffer for mmap.  */
14307
4.39k
    *buf = NULL;
14308
4.39k
  }
14309
19.4k
    }
14310
68.1k
#endif
14311
  /* FIXME: We should not get here if sec->alloced is set.  */
14312
68.1k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14313
68.1k
  if (ret && sec->mmapped_p)
14314
1.32k
    *buf = sec->contents;
14315
68.1k
  return ret;
14316
68.1k
}
14317
14318
/* Mmap in section contents.  */
14319
14320
bool
14321
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14322
68.1k
{
14323
68.1k
  *buf = NULL;
14324
68.1k
  return elf_mmap_section_contents (abfd, sec, buf);
14325
68.1k
}
14326
14327
/* Mmap in the full section contents for the final link.  */
14328
14329
bool
14330
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14331
             bfd_byte **buf)
14332
0
{
14333
0
  return elf_mmap_section_contents (abfd, sec, buf);
14334
0
}
14335
14336
/* Munmap section contents.  */
14337
14338
void
14339
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14340
2.15M
{
14341
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14342
     CONTENTS may be NULL.  */
14343
2.15M
  if (contents == NULL)
14344
2.08M
    return;
14345
14346
66.0k
  if (sec->alloced
14347
      /* What a tangled web we weave with section contents.
14348
   FIXME: We shouldn't need to test anything but sec->alloced
14349
   here, but there are cases where a buffer is allocated for a
14350
   section but then another buffer is malloc'd anyway.  eg.
14351
   trace through ld-elf/eh4 testcase on x86_64.  */
14352
66.0k
      && (sec->contents == contents
14353
5.22k
    || elf_section_data (sec)->this_hdr.contents == contents))
14354
5.22k
    return;
14355
14356
  /* Don't leave pointers to data we are about to munmap or free.  */
14357
60.8k
  if (sec->contents == contents)
14358
1.32k
    sec->contents = NULL;
14359
60.8k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14360
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14361
14362
60.8k
#ifdef USE_MMAP
14363
60.8k
  if (sec->mmapped_p)
14364
1.32k
    {
14365
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14366
   malloced, CONTENTS_ADDR is set to NULL.  */
14367
1.32k
      if (elf_section_data (sec)->contents_addr != NULL)
14368
1.32k
  {
14369
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14370
1.32k
    if (munmap (elf_section_data (sec)->contents_addr,
14371
1.32k
          elf_section_data (sec)->contents_size) != 0)
14372
0
      abort ();
14373
1.32k
    sec->mmapped_p = 0;
14374
1.32k
    elf_section_data (sec)->contents_addr = NULL;
14375
1.32k
    elf_section_data (sec)->contents_size = 0;
14376
1.32k
    return;
14377
1.32k
  }
14378
1.32k
    }
14379
59.5k
#endif
14380
14381
59.5k
  free (contents);
14382
59.5k
}
14383
14384
/* Munmap the full section contents for the final link.  */
14385
14386
void
14387
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14388
0
{
14389
0
#ifdef USE_MMAP
14390
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14391
0
    {
14392
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14393
   malloced, CONTENTS_ADDR is set to NULL.  */
14394
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14395
0
      if (munmap (elf_section_data (sec)->contents_addr,
14396
0
      elf_section_data (sec)->contents_size) != 0)
14397
0
  abort ();
14398
0
      sec->mmapped_p = 0;
14399
0
      sec->contents = NULL;
14400
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14401
0
      elf_section_data (sec)->contents_addr = NULL;
14402
0
      elf_section_data (sec)->contents_size = 0;
14403
0
    }
14404
0
#endif
14405
0
}