Coverage Report

Created: 2025-07-08 11:15

/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
20
{
70
20
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
20
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
20
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
20
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
20
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
20
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
20
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
20
}
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
1.30k
{
124
1.30k
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
1.30k
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
1.30k
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
1.30k
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
1.30k
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
1.30k
}
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
2.11k
{
152
2.11k
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
2.11k
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
2.11k
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
2.11k
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
2.11k
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
2.11k
}
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
208k
{
180
208k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
208k
}
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
22.8M
{
230
22.8M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
231
22.8M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
232
22.8M
  if (abfd->tdata.any == NULL)
233
0
    return false;
234
235
22.8M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
236
22.8M
  if (abfd->direction != read_direction)
237
281
    {
238
281
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
239
281
      if (o == NULL)
240
0
  return false;
241
281
      elf_tdata (abfd)->o = o;
242
281
      elf_program_header_size (abfd) = (bfd_size_type) -1;
243
281
    }
244
22.8M
  return true;
245
22.8M
}
246
247
248
bool
249
bfd_elf_make_object (bfd *abfd)
250
9.92M
{
251
9.92M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
252
9.92M
}
253
254
bool
255
bfd_elf_mkcorefile (bfd *abfd)
256
263k
{
257
  /* I think this can be done just like an object file.  */
258
263k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
259
0
    return false;
260
263k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
261
263k
  return elf_tdata (abfd)->core != NULL;
262
263k
}
263
264
char *
265
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
266
502k
{
267
502k
  Elf_Internal_Shdr **i_shdrp;
268
502k
  bfd_byte *shstrtab = NULL;
269
502k
  file_ptr offset;
270
502k
  bfd_size_type shstrtabsize;
271
272
502k
  i_shdrp = elf_elfsections (abfd);
273
502k
  if (i_shdrp == 0
274
502k
      || shindex >= elf_numsections (abfd)
275
502k
      || i_shdrp[shindex] == 0)
276
0
    return NULL;
277
278
502k
  shstrtab = i_shdrp[shindex]->contents;
279
502k
  if (shstrtab == NULL)
280
502k
    {
281
      /* No cached one, attempt to read, and cache what we read.  */
282
502k
      offset = i_shdrp[shindex]->sh_offset;
283
502k
      shstrtabsize = i_shdrp[shindex]->sh_size;
284
285
502k
      if (shstrtabsize == 0
286
502k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
287
502k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
288
13.9k
  {
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
13.9k
    i_shdrp[shindex]->sh_size = 0;
293
13.9k
  }
294
489k
      else if (shstrtab[shstrtabsize - 1] != 0)
295
66.8k
  {
296
    /* It is an error if a string table isn't terminated.  */
297
66.8k
    _bfd_error_handler
298
      /* xgettext:c-format */
299
66.8k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
300
66.8k
    shstrtab[shstrtabsize - 1] = 0;
301
66.8k
  }
302
502k
      i_shdrp[shindex]->contents = shstrtab;
303
502k
    }
304
502k
  return (char *) shstrtab;
305
502k
}
306
307
char *
308
bfd_elf_string_from_elf_section (bfd *abfd,
309
         unsigned int shindex,
310
         unsigned int strindex)
311
11.7M
{
312
11.7M
  Elf_Internal_Shdr *hdr;
313
314
11.7M
  if (strindex == 0)
315
3.72M
    return "";
316
317
8.05M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318
100
    return NULL;
319
320
8.05M
  hdr = elf_elfsections (abfd)[shindex];
321
322
8.05M
  if (hdr->contents == NULL)
323
507k
    {
324
507k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325
4.95k
  {
326
    /* PR 17512: file: f057ec89.  */
327
    /* xgettext:c-format */
328
4.95k
    _bfd_error_handler (_("%pB: attempt to load strings from"
329
4.95k
        " a non-string section (number %d)"),
330
4.95k
            abfd, shindex);
331
4.95k
    return NULL;
332
4.95k
  }
333
334
502k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335
13.9k
  return NULL;
336
502k
    }
337
7.54M
  else
338
7.54M
    {
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
7.54M
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345
0
  return NULL;
346
7.54M
    }
347
348
8.03M
  if (strindex >= hdr->sh_size)
349
108k
    {
350
108k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351
108k
      _bfd_error_handler
352
  /* xgettext:c-format */
353
108k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354
108k
   abfd, strindex, (uint64_t) hdr->sh_size,
355
108k
   (shindex == shstrndx && strindex == hdr->sh_name
356
108k
    ? ".shstrtab"
357
108k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
108k
      return NULL;
359
108k
    }
360
361
7.92M
  return ((char *) hdr->contents) + strindex;
362
8.03M
}
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
91.4k
{
381
91.4k
  Elf_Internal_Shdr *shndx_hdr;
382
91.4k
  void *alloc_ext;
383
91.4k
  const bfd_byte *esym;
384
91.4k
  Elf_External_Sym_Shndx *alloc_extshndx;
385
91.4k
  Elf_External_Sym_Shndx *shndx;
386
91.4k
  Elf_Internal_Sym *alloc_intsym;
387
91.4k
  Elf_Internal_Sym *isym;
388
91.4k
  Elf_Internal_Sym *isymend;
389
91.4k
  const struct elf_backend_data *bed;
390
91.4k
  size_t extsym_size;
391
91.4k
  size_t amt;
392
91.4k
  file_ptr pos;
393
394
91.4k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
0
    abort ();
396
397
91.4k
  if (symcount == 0)
398
0
    return intsym_buf;
399
400
91.4k
  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
91.4k
  shndx_hdr = NULL;
413
91.4k
  if (elf_symtab_shndx_list (ibfd) != NULL)
414
13.6k
    {
415
13.6k
      elf_section_list * entry;
416
13.6k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418
      /* Find an index section that is linked to this symtab section.  */
419
26.1k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420
14.8k
  {
421
    /* PR 20063.  */
422
14.8k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
423
19
      continue;
424
425
14.8k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
426
2.31k
      {
427
2.31k
        shndx_hdr = & entry->hdr;
428
2.31k
        break;
429
12.5k
      };
430
12.5k
  }
431
432
13.6k
      if (shndx_hdr == NULL)
433
11.2k
  {
434
11.2k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
435
      /* Not really accurate, but this was how the old code used
436
         to work.  */
437
11.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
11.2k
  }
441
13.6k
    }
442
443
  /* Read the symbols.  */
444
91.4k
  alloc_ext = NULL;
445
91.4k
  alloc_extshndx = NULL;
446
91.4k
  alloc_intsym = NULL;
447
91.4k
  bed = get_elf_backend_data (ibfd);
448
91.4k
  extsym_size = bed->s->sizeof_sym;
449
91.4k
  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
91.4k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455
91.4k
  size_t alloc_ext_size = amt;
456
91.4k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
457
91.4k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
458
90.7k
            &alloc_ext, ibfd, false))
459
2.67k
    {
460
2.67k
      intsym_buf = NULL;
461
2.67k
      goto out2;
462
2.67k
    }
463
464
88.7k
  size_t alloc_extshndx_size = 0;
465
88.7k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466
78.8k
    extshndx_buf = NULL;
467
9.88k
  else
468
9.88k
    {
469
9.88k
      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
9.88k
      alloc_extshndx_size = amt;
476
9.88k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477
9.88k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
478
9.88k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
479
9.28k
          &alloc_extshndx_size,
480
9.28k
          (void **) &alloc_extshndx,
481
9.28k
          ibfd, false))
482
751
  {
483
751
    intsym_buf = NULL;
484
751
    goto out1;
485
751
  }
486
9.88k
    }
487
488
87.9k
  if (intsym_buf == NULL)
489
7.41k
    {
490
7.41k
      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
7.41k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
496
7.41k
      intsym_buf = alloc_intsym;
497
7.41k
      if (intsym_buf == NULL)
498
0
  goto out1;
499
7.41k
    }
500
501
  /* Convert the symbols to internal form.  */
502
87.9k
  isymend = intsym_buf + symcount;
503
87.9k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
504
87.9k
     shndx = extshndx_buf;
505
1.63M
       isym < isymend;
506
1.55M
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
507
1.55M
    {
508
1.55M
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
509
1.66k
  {
510
1.66k
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
511
    /* xgettext:c-format */
512
1.66k
    _bfd_error_handler (_("%pB symbol number %lu references"
513
1.66k
        " nonexistent SHT_SYMTAB_SHNDX section"),
514
1.66k
            ibfd, (unsigned long) symoffset);
515
1.66k
    free (alloc_intsym);
516
1.66k
    intsym_buf = NULL;
517
1.66k
    goto out1;
518
1.66k
  }
519
520
      /* PR 33019: Do not accept unsupported binding values - they will
521
   likely cause problems later on.  */
522
1.55M
      int bind = ELF_ST_BIND (isym->st_info);
523
1.55M
      if (bind > STB_WEAK && bind < STB_LOOS)
524
2.52k
  { 
525
    /* xgettext:c-format */
526
2.52k
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
527
2.52k
            ibfd, (unsigned long) (isym - intsym_buf), bind);
528
2.52k
    free (alloc_intsym);
529
2.52k
    intsym_buf = NULL;
530
2.52k
    goto out1;
531
2.52k
  }
532
533
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
534
1.55M
      int t = ELF_ST_TYPE (isym->st_info);
535
1.55M
      if (t == 7)
536
837
  {
537
    /* xgettext:c-format */
538
837
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
539
837
            ibfd, (unsigned long) (isym - intsym_buf), t);
540
837
    free (alloc_intsym);
541
837
    intsym_buf = NULL;
542
837
    goto out1;
543
837
  }
544
1.55M
    }
545
546
88.7k
 out1:
547
88.7k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
548
91.4k
 out2:
549
91.4k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
550
551
91.4k
  return intsym_buf;
552
88.7k
}
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
1.49M
{
560
1.49M
  unsigned int iname = isym->st_name;
561
1.49M
  unsigned int shindex = symtab_hdr->sh_link;
562
563
1.49M
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
564
      /* Check for a bogus st_shndx to avoid crashing.  */
565
1.49M
      && isym->st_shndx < elf_numsections (abfd))
566
150k
    {
567
150k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
568
150k
      shindex = elf_elfheader (abfd)->e_shstrndx;
569
150k
    }
570
571
1.49M
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
572
1.49M
}
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
1.41M
{
580
1.41M
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
581
1.41M
  if (name == NULL)
582
101k
    name = bfd_symbol_error_name;
583
1.31M
  else if (sym_sec && *name == '\0')
584
0
    name = bfd_section_name (sym_sec);
585
586
1.41M
  return name;
587
1.41M
}
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
87.8k
{
595
87.8k
  Elf_Internal_Shdr *hdr;
596
87.8k
  unsigned char esym[sizeof (Elf64_External_Sym)];
597
87.8k
  Elf_External_Sym_Shndx eshndx;
598
87.8k
  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
87.8k
  if (ghdr->sh_link >= elf_numsections (abfd))
603
13
    return NULL;
604
87.8k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
605
87.8k
  if (hdr->sh_type != SHT_SYMTAB
606
87.8k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
607
3.96k
    return NULL;
608
609
  /* Go read the symbol.  */
610
83.8k
  hdr = &elf_tdata (abfd)->symtab_hdr;
611
83.8k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
612
83.8k
          &isym, esym, &eshndx) == NULL)
613
7.38k
    return NULL;
614
615
76.4k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
616
83.8k
}
617
618
static bool
619
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
620
161k
{
621
161k
  return (shdr->sh_size >= minsize
622
161k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
623
161k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
624
161k
    && shdr->bfd_section != NULL);
625
161k
}
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
154k
{
634
154k
  unsigned char *contents;
635
636
  /* Read the raw contents.  */
637
154k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
638
1.54k
    {
639
1.54k
      _bfd_error_handler
640
  /* xgettext:c-format */
641
1.54k
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
642
1.54k
      return false;
643
1.54k
    }
644
645
153k
  asection *last_elt = NULL;
646
153k
  const char *gname = NULL;
647
153k
  unsigned char *p = contents + ghdr->sh_size;
648
7.39M
  while (1)
649
7.39M
    {
650
7.39M
      unsigned int idx;
651
7.39M
      Elf_Internal_Shdr *shdr;
652
7.39M
      asection *elt;
653
654
7.39M
      p -= 4;
655
7.39M
      idx = H_GET_32 (abfd, p);
656
7.39M
      if (p == contents)
657
139k
  {
658
139k
    if ((idx & GRP_COMDAT) != 0)
659
40.9k
      ghdr->bfd_section->flags
660
40.9k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
661
139k
    break;
662
139k
  }
663
664
7.25M
      if (idx == 0
665
7.25M
    || idx >= elf_numsections (abfd)
666
7.25M
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
667
7.25M
    || ((elt = shdr->bfd_section) != NULL
668
502k
        && elf_sec_group (elt) != NULL
669
502k
        && elf_sec_group (elt) != ghdr->bfd_section))
670
6.78M
  {
671
6.78M
    _bfd_error_handler
672
6.78M
      (_("%pB: invalid entry (%#x) in group [%u]"),
673
6.78M
       abfd, idx, gidx);
674
6.78M
    continue;
675
6.78M
  }
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
469k
      shdr->sh_flags |= SHF_GROUP;
681
682
469k
      if (elt == NULL)
683
202k
  {
684
202k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
685
80.9k
      {
686
80.9k
        const char *name = bfd_elf_string_from_elf_section
687
80.9k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
688
689
80.9k
        _bfd_error_handler
690
    /* xgettext:c-format */
691
80.9k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
692
80.9k
     abfd, shdr->sh_type, name, gidx);
693
80.9k
      }
694
202k
    continue;
695
202k
  }
696
697
      /* Don't try to add a section to elf_next_in_group list twice.  */
698
266k
      if (elf_sec_group (elt) != NULL)
699
62.2k
  continue;
700
701
204k
      if (last_elt == NULL)
702
87.8k
  {
703
    /* Start a circular list with one element.
704
       It will be in reverse order to match what gas does.  */
705
87.8k
    elf_next_in_group (elt) = elt;
706
    /* Point the group section to it.  */
707
87.8k
    elf_next_in_group (ghdr->bfd_section) = elt;
708
87.8k
    gname = group_signature (abfd, ghdr);
709
87.8k
    if (gname == NULL)
710
13.3k
      {
711
13.3k
        free (contents);
712
13.3k
        return false;
713
13.3k
      }
714
87.8k
  }
715
116k
      else
716
116k
  {
717
116k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
718
116k
    elf_next_in_group (last_elt) = elt;
719
116k
  }
720
191k
      last_elt = elt;
721
191k
      elf_group_name (elt) = gname;
722
191k
      elf_sec_group (elt) = ghdr->bfd_section;
723
191k
    }
724
725
139k
  free (contents);
726
139k
  return true;
727
153k
}
728
729
bool
730
_bfd_elf_setup_sections (bfd *abfd)
731
501k
{
732
501k
  bool result = true;
733
734
  /* Process SHF_LINK_ORDER.  */
735
4.31M
  for (asection *s = abfd->sections; s != NULL; s = s->next)
736
3.81M
    {
737
3.81M
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
738
3.81M
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
739
399k
  {
740
399k
    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
399k
    if (elfsec == 0)
747
285k
      {
748
285k
        elf_linked_to_section (s) = NULL;
749
285k
      }
750
114k
    else
751
114k
      {
752
114k
        asection *linksec = NULL;
753
754
114k
        if (elfsec < elf_numsections (abfd))
755
114k
    {
756
114k
      this_hdr = elf_elfsections (abfd)[elfsec];
757
114k
      linksec = this_hdr->bfd_section;
758
114k
    }
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
114k
        if (linksec == NULL)
764
3.11k
    {
765
3.11k
      _bfd_error_handler
766
        /* xgettext:c-format */
767
3.11k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
768
3.11k
         s->owner, elfsec, s);
769
3.11k
      result = false;
770
3.11k
    }
771
772
114k
        elf_linked_to_section (s) = linksec;
773
114k
      }
774
399k
  }
775
3.81M
    }
776
777
  /* Process section groups.  */
778
7.12M
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
779
6.61M
    {
780
6.61M
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
781
782
6.61M
      if (shdr && shdr->sh_type == SHT_GROUP)
783
161k
  {
784
161k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
785
156k
      {
786
156k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
787
156k
      && !process_sht_group_entries (abfd, shdr, i))
788
14.9k
    result = false;
789
156k
      }
790
4.91k
    else
791
4.91k
      {
792
        /* PR binutils/18758: Beware of corrupt binaries with
793
     invalid group data.  */
794
4.91k
        _bfd_error_handler
795
    /* xgettext:c-format */
796
4.91k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
797
4.91k
        result = false;
798
4.91k
      }
799
161k
  }
800
6.61M
    }
801
802
501k
  return result;
803
501k
}
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.67M
{
828
5.67M
  asection *newsect;
829
5.67M
  flagword flags;
830
5.67M
  const struct elf_backend_data *bed;
831
5.67M
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
832
833
5.67M
  if (hdr->bfd_section != NULL)
834
654k
    return true;
835
836
5.02M
  newsect = bfd_make_section_anyway (abfd, name);
837
5.02M
  if (newsect == NULL)
838
0
    return false;
839
840
5.02M
  hdr->bfd_section = newsect;
841
5.02M
  elf_section_data (newsect)->this_hdr = *hdr;
842
5.02M
  elf_section_data (newsect)->this_idx = shindex;
843
844
  /* Always use the real type/flags.  */
845
5.02M
  elf_section_type (newsect) = hdr->sh_type;
846
5.02M
  elf_section_flags (newsect) = hdr->sh_flags;
847
848
5.02M
  newsect->filepos = hdr->sh_offset;
849
850
5.02M
  flags = SEC_NO_FLAGS;
851
5.02M
  if (hdr->sh_type != SHT_NOBITS)
852
4.93M
    flags |= SEC_HAS_CONTENTS;
853
5.02M
  if (hdr->sh_type == SHT_GROUP)
854
272k
    flags |= SEC_GROUP;
855
5.02M
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
856
1.90M
    {
857
1.90M
      flags |= SEC_ALLOC;
858
1.90M
      if (hdr->sh_type != SHT_NOBITS)
859
1.85M
  flags |= SEC_LOAD;
860
1.90M
    }
861
5.02M
  if ((hdr->sh_flags & SHF_WRITE) == 0)
862
3.96M
    flags |= SEC_READONLY;
863
5.02M
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
864
1.01M
    flags |= SEC_CODE;
865
4.00M
  else if ((flags & SEC_LOAD) != 0)
866
1.05M
    flags |= SEC_DATA;
867
5.02M
  if ((hdr->sh_flags & SHF_MERGE) != 0)
868
732k
    {
869
732k
      flags |= SEC_MERGE;
870
732k
      newsect->entsize = hdr->sh_entsize;
871
732k
    }
872
5.02M
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
873
793k
    {
874
793k
      flags |= SEC_STRINGS;
875
793k
      newsect->entsize = hdr->sh_entsize;
876
793k
    }
877
5.02M
  if ((hdr->sh_flags & SHF_TLS) != 0)
878
410k
    flags |= SEC_THREAD_LOCAL;
879
5.02M
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
880
392k
    flags |= SEC_EXCLUDE;
881
882
5.02M
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
883
5.02M
    {
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.98M
    case ELFOSABI_GNU:
888
2.72M
    case ELFOSABI_FREEBSD:
889
2.72M
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
890
368k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
891
      /* Fall through */
892
3.85M
    case ELFOSABI_NONE:
893
3.85M
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
894
209k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
895
3.85M
      break;
896
5.02M
    }
897
898
5.02M
  if ((flags & SEC_ALLOC) == 0)
899
3.11M
    {
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.11M
      if (name [0] == '.')
903
976k
  {
904
976k
    if (startswith (name, ".debug")
905
976k
        || startswith (name, ".gnu.debuglto_.debug_")
906
976k
        || startswith (name, ".gnu.linkonce.wi.")
907
976k
        || startswith (name, ".zdebug"))
908
221k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
909
755k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
910
755k
       || startswith (name, ".note.gnu"))
911
13.3k
      {
912
13.3k
        flags |= SEC_ELF_OCTETS;
913
13.3k
        opb = 1;
914
13.3k
      }
915
742k
    else if (startswith (name, ".line")
916
742k
       || startswith (name, ".stab")
917
742k
       || strcmp (name, ".gdb_index") == 0)
918
31.7k
      flags |= SEC_DEBUGGING;
919
976k
  }
920
3.11M
    }
921
922
5.02M
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
923
5.02M
      || !bfd_set_section_size (newsect, hdr->sh_size)
924
5.02M
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
925
5.02M
              & -hdr->sh_addralign)))
926
3.07k
    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.01M
  if (startswith (name, ".gnu.linkonce")
935
5.01M
      && elf_next_in_group (newsect) == NULL)
936
17.6k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
937
938
5.01M
  if (!bfd_set_section_flags (newsect, flags))
939
0
    return false;
940
941
5.01M
  bed = get_elf_backend_data (abfd);
942
5.01M
  if (bed->elf_backend_section_flags)
943
1.57M
    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.01M
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
950
62.1k
    {
951
62.1k
      bfd_byte *contents;
952
953
62.1k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
954
6.21k
  return false;
955
956
55.9k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
957
55.9k
           hdr->sh_offset, hdr->sh_addralign);
958
55.9k
      _bfd_elf_munmap_section_contents (newsect, contents);
959
55.9k
    }
960
961
5.01M
  if ((newsect->flags & SEC_ALLOC) != 0)
962
1.89M
    {
963
1.89M
      Elf_Internal_Phdr *phdr;
964
1.89M
      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.89M
      phdr = elf_tdata (abfd)->phdr;
971
2.38M
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
972
882k
  if (phdr->p_paddr != 0)
973
396k
    break;
974
486k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
975
114k
    ++nload;
976
1.89M
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
977
49.5k
  return true;
978
979
1.85M
      phdr = elf_tdata (abfd)->phdr;
980
29.8M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
981
28.0M
  {
982
28.0M
    if (((phdr->p_type == PT_LOAD
983
28.0M
    && (hdr->sh_flags & SHF_TLS) == 0)
984
28.0M
         || phdr->p_type == PT_TLS)
985
28.0M
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
986
93.1k
      {
987
93.1k
        if ((newsect->flags & SEC_LOAD) == 0)
988
5.69k
    newsect->lma = (phdr->p_paddr
989
5.69k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
990
87.4k
        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
87.4k
    newsect->lma = (phdr->p_paddr
999
87.4k
        + 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
93.1k
        if (hdr->sh_addr >= phdr->p_vaddr
1006
93.1k
      && (hdr->sh_addr + hdr->sh_size
1007
93.1k
          <= phdr->p_vaddr + phdr->p_memsz))
1008
84.7k
    break;
1009
93.1k
      }
1010
28.0M
  }
1011
1.85M
    }
1012
1013
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1014
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1015
4.96M
  if ((newsect->flags & SEC_DEBUGGING) != 0
1016
4.96M
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1017
4.96M
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1018
218k
    {
1019
218k
      enum { nothing, compress, decompress } action = nothing;
1020
218k
      int compression_header_size;
1021
218k
      bfd_size_type uncompressed_size;
1022
218k
      unsigned int uncompressed_align_power;
1023
218k
      enum compression_type ch_type = ch_none;
1024
218k
      bool compressed
1025
218k
  = bfd_is_section_compressed_info (abfd, newsect,
1026
218k
            &compression_header_size,
1027
218k
            &uncompressed_size,
1028
218k
            &uncompressed_align_power,
1029
218k
            &ch_type);
1030
1031
      /* Should we decompress?  */
1032
218k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1033
456
  action = decompress;
1034
1035
      /* Should we compress?  Or convert to a different compression?  */
1036
217k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1037
217k
         && newsect->size != 0
1038
217k
         && compression_header_size >= 0
1039
217k
         && uncompressed_size > 0)
1040
17.9k
  {
1041
17.9k
    if (!compressed)
1042
17.9k
      action = compress;
1043
2
    else
1044
2
      {
1045
2
        enum compression_type new_ch_type = ch_none;
1046
2
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1047
2
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1048
2
             ? ch_compress_zstd : ch_compress_zlib);
1049
2
        if (new_ch_type != ch_type)
1050
2
    action = compress;
1051
2
      }
1052
17.9k
  }
1053
1054
218k
      if (action == compress)
1055
17.9k
  {
1056
17.9k
    if (!bfd_init_section_compress_status (abfd, newsect))
1057
83
      {
1058
83
        _bfd_error_handler
1059
    /* xgettext:c-format */
1060
83
    (_("%pB: unable to compress section %s"), abfd, name);
1061
83
        return false;
1062
83
      }
1063
17.9k
  }
1064
200k
      else if (action == decompress)
1065
456
  {
1066
456
    if (!bfd_init_section_decompress_status (abfd, newsect))
1067
166
      {
1068
166
        _bfd_error_handler
1069
    /* xgettext:c-format */
1070
166
    (_("%pB: unable to decompress section %s"), abfd, name);
1071
166
        return false;
1072
166
      }
1073
290
#ifndef HAVE_ZSTD
1074
290
    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
289
#endif
1085
289
    if (abfd->is_linker_input
1086
289
        && 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
289
  }
1096
218k
    }
1097
1098
4.96M
  return true;
1099
4.96M
}
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
564k
{
1126
564k
  if (output_bfd != NULL
1127
564k
      && (symbol->flags & BSF_SECTION_SYM) == 0
1128
564k
      && (! 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
564k
  if (output_bfd == NULL
1144
564k
      && !reloc_entry->howto->pc_relative
1145
564k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1146
564k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1147
84.1k
    reloc_entry->addend -= symbol->section->output_section->vma;
1148
1149
564k
  return bfd_reloc_continue;
1150
564k
}
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
2
{
1160
2
  if (a->sh_type != b->sh_type
1161
2
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1162
2
      || a->sh_addralign != b->sh_addralign
1163
2
      || a->sh_entsize != b->sh_entsize)
1164
0
    return false;
1165
2
  if (a->sh_type == SHT_SYMTAB
1166
2
      || a->sh_type == SHT_STRTAB)
1167
0
    return true;
1168
2
  return a->sh_size == b->sh_size;
1169
2
}
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
2
{
1180
2
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1181
2
  unsigned int i;
1182
1183
2
  BFD_ASSERT (iheader != NULL);
1184
1185
  /* See PR 20922 for a reproducer of the NULL test.  */
1186
2
  if (hint < elf_numsections (obfd)
1187
2
      && oheaders[hint] != NULL
1188
2
      && section_match (oheaders[hint], iheader))
1189
2
    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
215
{
1217
215
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1218
215
  const Elf_Internal_Shdr **iheaders
1219
215
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1220
215
  bool changed = false;
1221
215
  unsigned int sh_link;
1222
1223
215
  if (oheader->sh_type == SHT_NOBITS)
1224
69
    {
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
69
      if (oheader->sh_link == 0)
1242
69
  oheader->sh_link = iheader->sh_link;
1243
69
      if (oheader->sh_info == 0)
1244
69
  oheader->sh_info = iheader->sh_info;
1245
69
      return true;
1246
69
    }
1247
1248
  /* Allow the target a chance to decide how these fields should be set.  */
1249
146
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1250
146
                iheader, oheader))
1251
143
    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
3
  if (iheader->sh_link != SHN_UNDEF)
1258
2
    {
1259
      /* See PR 20931 for a reproducer.  */
1260
2
      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
2
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1270
2
      if (sh_link != SHN_UNDEF)
1271
2
  {
1272
2
    oheader->sh_link = sh_link;
1273
2
    changed = true;
1274
2
  }
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
2
    }
1282
1283
3
  if (iheader->sh_info)
1284
0
    {
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
0
      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
0
      else
1296
  /* No idea what it means - just copy it.  */
1297
0
  sh_link = iheader->sh_info;
1298
1299
0
      if (sh_link != SHN_UNDEF)
1300
0
  {
1301
0
    oheader->sh_info = sh_link;
1302
0
    changed = true;
1303
0
  }
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
0
    }
1309
1310
3
  return changed;
1311
3
}
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
197
{
1319
197
  const Elf_Internal_Shdr **iheaders
1320
197
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1321
197
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1322
197
  const struct elf_backend_data *bed;
1323
197
  unsigned int i;
1324
1325
197
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1326
197
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1327
0
    return true;
1328
1329
197
  if (!elf_flags_init (obfd))
1330
195
    {
1331
195
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1332
195
      elf_flags_init (obfd) = true;
1333
195
    }
1334
1335
197
  elf_gp (obfd) = elf_gp (ibfd);
1336
1337
  /* Also copy the EI_OSABI field.  */
1338
197
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1339
197
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1340
1341
  /* If set, copy the EI_ABIVERSION field.  */
1342
197
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1343
1
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1344
1
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1345
1346
  /* Copy object attributes.  */
1347
197
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1348
1349
197
  if (iheaders == NULL || oheaders == NULL)
1350
112
    return true;
1351
1352
85
  bed = get_elf_backend_data (obfd);
1353
1354
  /* Possibly copy other fields in the section header.  */
1355
5.08k
  for (i = 1; i < elf_numsections (obfd); i++)
1356
5.00k
    {
1357
5.00k
      unsigned int j;
1358
5.00k
      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
5.00k
      if (oheader == NULL
1364
5.00k
    || (oheader->sh_type != SHT_NOBITS
1365
5.00k
        && oheader->sh_type < SHT_LOOS))
1366
4.75k
  continue;
1367
1368
      /* Ignore empty sections, and sections whose
1369
   fields have already been initialised.  */
1370
253
      if (oheader->sh_size == 0
1371
253
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1372
38
  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
10.1k
      for (j = 1; j < elf_numsections (ibfd); j++)
1378
10.1k
  {
1379
10.1k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1380
1381
10.1k
    if (iheader == NULL)
1382
0
      continue;
1383
1384
10.1k
    if (oheader->bfd_section != NULL
1385
10.1k
        && iheader->bfd_section != NULL
1386
10.1k
        && iheader->bfd_section->output_section != NULL
1387
10.1k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1388
215
      {
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
215
        if (!copy_special_section_fields (ibfd, obfd,
1395
215
            iheader, oheader, i))
1396
1
    j = elf_numsections (ibfd);
1397
215
        break;
1398
215
      }
1399
10.1k
  }
1400
1401
215
      if (j < elf_numsections (ibfd))
1402
214
  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
85
  return true;
1444
197
}
1445
1446
static const char *
1447
get_segment_type (unsigned int p_type)
1448
262k
{
1449
262k
  const char *pt;
1450
262k
  switch (p_type)
1451
262k
    {
1452
60.0k
    case PT_NULL: pt = "NULL"; break;
1453
4.47k
    case PT_LOAD: pt = "LOAD"; break;
1454
2.20k
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1455
1.30k
    case PT_INTERP: pt = "INTERP"; break;
1456
1.25k
    case PT_NOTE: pt = "NOTE"; break;
1457
730
    case PT_SHLIB: pt = "SHLIB"; break;
1458
1.95k
    case PT_PHDR: pt = "PHDR"; break;
1459
224
    case PT_TLS: pt = "TLS"; break;
1460
505
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1461
400
    case PT_GNU_STACK: pt = "STACK"; break;
1462
365
    case PT_GNU_RELRO: pt = "RELRO"; break;
1463
1
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1464
189k
    default: pt = NULL; break;
1465
262k
    }
1466
262k
  return pt;
1467
262k
}
1468
1469
/* Print out the program headers.  */
1470
1471
bool
1472
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1473
5.19k
{
1474
5.19k
  FILE *f = (FILE *) farg;
1475
5.19k
  Elf_Internal_Phdr *p;
1476
5.19k
  asection *s;
1477
5.19k
  bfd_byte *dynbuf = NULL;
1478
1479
5.19k
  p = elf_tdata (abfd)->phdr;
1480
5.19k
  if (p != NULL)
1481
3.52k
    {
1482
3.52k
      unsigned int i, c;
1483
1484
3.52k
      fprintf (f, _("\nProgram Header:\n"));
1485
3.52k
      c = elf_elfheader (abfd)->e_phnum;
1486
266k
      for (i = 0; i < c; i++, p++)
1487
262k
  {
1488
262k
    const char *pt = get_segment_type (p->p_type);
1489
262k
    char buf[20];
1490
1491
262k
    if (pt == NULL)
1492
189k
      {
1493
189k
        sprintf (buf, "0x%lx", p->p_type);
1494
189k
        pt = buf;
1495
189k
      }
1496
262k
    fprintf (f, "%8s off    0x", pt);
1497
262k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1498
262k
    fprintf (f, " vaddr 0x");
1499
262k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1500
262k
    fprintf (f, " paddr 0x");
1501
262k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1502
262k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1503
262k
    fprintf (f, "         filesz 0x");
1504
262k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1505
262k
    fprintf (f, " memsz 0x");
1506
262k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1507
262k
    fprintf (f, " flags %c%c%c",
1508
262k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1509
262k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1510
262k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1511
262k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1512
185k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1513
262k
    fprintf (f, "\n");
1514
262k
  }
1515
3.52k
    }
1516
1517
5.19k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1518
5.19k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1519
741
    {
1520
741
      unsigned int elfsec;
1521
741
      unsigned long shlink;
1522
741
      bfd_byte *extdyn, *extdynend;
1523
741
      size_t extdynsize;
1524
741
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1525
1526
741
      fprintf (f, _("\nDynamic Section:\n"));
1527
1528
741
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1529
16
  goto error_return;
1530
1531
725
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1532
725
      if (elfsec == SHN_BAD)
1533
0
  goto error_return;
1534
725
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1535
1536
725
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1537
725
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1538
1539
725
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1540
16.3k
     (size_t) (extdynend - extdyn) >= extdynsize;
1541
15.6k
     extdyn += extdynsize)
1542
16.2k
  {
1543
16.2k
    Elf_Internal_Dyn dyn;
1544
16.2k
    const char *name = "";
1545
16.2k
    char ab[20];
1546
16.2k
    bool stringp;
1547
16.2k
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1548
1549
16.2k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1550
1551
16.2k
    if (dyn.d_tag == DT_NULL)
1552
592
      break;
1553
1554
15.6k
    stringp = false;
1555
15.6k
    switch (dyn.d_tag)
1556
15.6k
      {
1557
3.75k
      default:
1558
3.75k
        if (bed->elf_backend_get_target_dtag)
1559
70
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1560
1561
3.75k
        if (!strcmp (name, ""))
1562
3.68k
    {
1563
3.68k
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1564
3.68k
      name = ab;
1565
3.68k
    }
1566
3.75k
        break;
1567
1568
1.08k
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1569
376
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1570
529
      case DT_PLTGOT: name = "PLTGOT"; break;
1571
340
      case DT_HASH: name = "HASH"; break;
1572
520
      case DT_STRTAB: name = "STRTAB"; break;
1573
523
      case DT_SYMTAB: name = "SYMTAB"; break;
1574
428
      case DT_RELA: name = "RELA"; break;
1575
440
      case DT_RELASZ: name = "RELASZ"; break;
1576
430
      case DT_RELAENT: name = "RELAENT"; break;
1577
510
      case DT_STRSZ: name = "STRSZ"; break;
1578
508
      case DT_SYMENT: name = "SYMENT"; break;
1579
499
      case DT_INIT: name = "INIT"; break;
1580
502
      case DT_FINI: name = "FINI"; break;
1581
12
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1582
143
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1583
5
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1584
91
      case DT_REL: name = "REL"; break;
1585
102
      case DT_RELSZ: name = "RELSZ"; break;
1586
80
      case DT_RELENT: name = "RELENT"; break;
1587
5
      case DT_RELR: name = "RELR"; break;
1588
3
      case DT_RELRSZ: name = "RELRSZ"; break;
1589
10
      case DT_RELRENT: name = "RELRENT"; break;
1590
369
      case DT_PLTREL: name = "PLTREL"; break;
1591
498
      case DT_DEBUG: name = "DEBUG"; break;
1592
4
      case DT_TEXTREL: name = "TEXTREL"; break;
1593
371
      case DT_JMPREL: name = "JMPREL"; break;
1594
12
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1595
217
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1596
217
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1597
217
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1598
214
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1599
95
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1600
233
      case DT_FLAGS: name = "FLAGS"; break;
1601
6
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1602
3
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1603
94
      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
94
      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
2
      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
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1616
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1617
201
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1618
36
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1619
290
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1620
358
      case DT_VERSYM: name = "VERSYM"; break;
1621
0
      case DT_VERDEF: name = "VERDEF"; break;
1622
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1623
452
      case DT_VERNEED: name = "VERNEED"; break;
1624
449
      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
304
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1629
15.6k
      }
1630
1631
15.6k
    fprintf (f, "  %-20s ", name);
1632
15.6k
    if (! stringp)
1633
14.2k
      {
1634
14.2k
        fprintf (f, "0x");
1635
14.2k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1636
14.2k
      }
1637
1.33k
    else
1638
1.33k
      {
1639
1.33k
        const char *string;
1640
1.33k
        unsigned int tagv = dyn.d_un.d_val;
1641
1642
1.33k
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1643
1.33k
        if (string == NULL)
1644
17
    goto error_return;
1645
1.31k
        fprintf (f, "%s", string);
1646
1.31k
      }
1647
15.6k
    fprintf (f, "\n");
1648
15.6k
  }
1649
1650
708
      _bfd_elf_munmap_section_contents (s, dynbuf);
1651
708
      dynbuf = NULL;
1652
708
    }
1653
1654
5.16k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1655
5.16k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1656
664
    {
1657
664
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1658
142
  return false;
1659
664
    }
1660
1661
5.02k
  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
5.02k
  if (elf_dynverref (abfd) != 0)
1687
522
    {
1688
522
      Elf_Internal_Verneed *t;
1689
1690
522
      fprintf (f, _("\nVersion References:\n"));
1691
1.52k
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1692
1.00k
  {
1693
1.00k
    Elf_Internal_Vernaux *a;
1694
1695
1.00k
    fprintf (f, _("  required from %s:\n"),
1696
1.00k
       t->vn_filename ? t->vn_filename : "<corrupt>");
1697
2.84k
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1698
1.84k
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1699
1.84k
         a->vna_flags, a->vna_other,
1700
1.84k
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1701
1.00k
  }
1702
522
    }
1703
1704
5.02k
  return true;
1705
1706
33
 error_return:
1707
33
  _bfd_elf_munmap_section_contents (s, dynbuf);
1708
33
  return false;
1709
5.16k
}
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
880
{
1718
880
  Elf_Internal_Phdr *seg;
1719
880
  size_t i;
1720
1721
3.23k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1722
3.06k
    if (seg->p_type == PT_LOAD
1723
3.06k
  && vma >= (seg->p_vaddr & -seg->p_align)
1724
3.06k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1725
703
      {
1726
703
  if (max_size_p)
1727
21
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1728
703
  return vma - seg->p_vaddr + seg->p_offset;
1729
703
      }
1730
1731
177
  if (max_size_p)
1732
11
    *max_size_p = 0;
1733
177
  bfd_set_error (bfd_error_invalid_operation);
1734
177
  return (file_ptr) -1;
1735
880
}
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
106
{
1743
106
  unsigned char *e_data = NULL;
1744
106
  bfd_vma *i_data = NULL;
1745
106
  bfd_size_type size;
1746
106
  void *e_data_addr;
1747
106
  size_t e_data_size ATTRIBUTE_UNUSED;
1748
1749
106
  if (ent_size != 4 && ent_size != 8)
1750
0
    return NULL;
1751
1752
106
  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
106
  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
106
  if (size > filesize
1762
106
      || number >= ~(size_t) 0 / ent_size
1763
106
      || number >= ~(size_t) 0 / sizeof (*i_data))
1764
5
    {
1765
5
      bfd_set_error (bfd_error_file_too_big);
1766
5
      return NULL;
1767
5
    }
1768
1769
101
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1770
101
  if (e_data == NULL)
1771
13
    return NULL;
1772
1773
88
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1774
88
  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
88
  if (ent_size == 4)
1781
71.7k
    while (number--)
1782
71.6k
      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
88
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1788
88
  return i_data;
1789
88
}
1790
1791
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1792
   support DT_MIPS_XHASH?  */
1793
504k
#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
12.1k
{
1802
12.1k
  bfd_byte *extdyn, *extdynend;
1803
12.1k
  size_t extdynsize;
1804
12.1k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1805
12.1k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1806
12.1k
        Elf_Internal_Sym *);
1807
12.1k
  Elf_Internal_Dyn dyn;
1808
12.1k
  bfd_vma dt_hash = 0;
1809
12.1k
  bfd_vma dt_gnu_hash = 0;
1810
12.1k
  bfd_vma dt_mips_xhash = 0;
1811
12.1k
  bfd_vma dt_strtab = 0;
1812
12.1k
  bfd_vma dt_symtab = 0;
1813
12.1k
  size_t dt_strsz = 0;
1814
12.1k
  bfd_vma dt_versym = 0;
1815
12.1k
  bfd_vma dt_verdef = 0;
1816
12.1k
  bfd_vma dt_verneed = 0;
1817
12.1k
  bfd_byte *dynbuf = NULL;
1818
12.1k
  char *strbuf = NULL;
1819
12.1k
  bfd_vma *gnubuckets = NULL;
1820
12.1k
  bfd_vma *gnuchains = NULL;
1821
12.1k
  bfd_vma *mipsxlat = NULL;
1822
12.1k
  file_ptr saved_filepos, filepos;
1823
12.1k
  bool res = false;
1824
12.1k
  size_t amt;
1825
12.1k
  bfd_byte *esymbuf = NULL, *esym;
1826
12.1k
  bfd_size_type symcount;
1827
12.1k
  Elf_Internal_Sym *isymbuf = NULL;
1828
12.1k
  Elf_Internal_Sym *isym, *isymend;
1829
12.1k
  bfd_byte *versym = NULL;
1830
12.1k
  bfd_byte *verdef = NULL;
1831
12.1k
  bfd_byte *verneed = NULL;
1832
12.1k
  size_t verdef_size = 0;
1833
12.1k
  size_t verneed_size = 0;
1834
12.1k
  size_t extsym_size;
1835
12.1k
  const struct elf_backend_data *bed;
1836
12.1k
  void *dynbuf_addr = NULL;
1837
12.1k
  void *esymbuf_addr = NULL;
1838
12.1k
  size_t dynbuf_size = 0;
1839
12.1k
  size_t esymbuf_size = 0;
1840
1841
  /* Return TRUE if symbol table is bad.  */
1842
12.1k
  if (elf_bad_symtab (abfd))
1843
0
    return true;
1844
1845
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1846
12.1k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1847
0
    return true;
1848
1849
12.1k
  bed = get_elf_backend_data (abfd);
1850
1851
  /* Save file position for elf_object_p.  */
1852
12.1k
  saved_filepos = bfd_tell (abfd);
1853
1854
12.1k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1855
0
    goto error_return;
1856
1857
12.1k
  dynbuf_size = phdr->p_filesz;
1858
12.1k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1859
12.1k
  if (dynbuf == NULL)
1860
1.48k
    goto error_return;
1861
1862
10.6k
  extsym_size = bed->s->sizeof_sym;
1863
10.6k
  extdynsize = bed->s->sizeof_dyn;
1864
10.6k
  swap_dyn_in = bed->s->swap_dyn_in;
1865
1866
10.6k
  extdyn = dynbuf;
1867
10.6k
  if (phdr->p_filesz < extdynsize)
1868
1.04k
    goto error_return;
1869
9.64k
  extdynend = extdyn + phdr->p_filesz;
1870
271k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1871
266k
    {
1872
266k
      swap_dyn_in (abfd, extdyn, &dyn);
1873
1874
266k
      if (dyn.d_tag == DT_NULL)
1875
3.31k
  break;
1876
1877
262k
      switch (dyn.d_tag)
1878
262k
  {
1879
2.03k
  case DT_HASH:
1880
2.03k
    dt_hash = dyn.d_un.d_val;
1881
2.03k
    break;
1882
265
  case DT_GNU_HASH:
1883
265
    if (bed->elf_machine_code != EM_MIPS
1884
265
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1885
226
      dt_gnu_hash = dyn.d_un.d_val;
1886
265
    break;
1887
2.13k
  case DT_STRTAB:
1888
2.13k
    dt_strtab = dyn.d_un.d_val;
1889
2.13k
    break;
1890
2.71k
  case DT_SYMTAB:
1891
2.71k
    dt_symtab = dyn.d_un.d_val;
1892
2.71k
    break;
1893
1.27k
  case DT_STRSZ:
1894
1.27k
    dt_strsz = dyn.d_un.d_val;
1895
1.27k
    break;
1896
1.16k
  case DT_SYMENT:
1897
1.16k
    if (dyn.d_un.d_val != extsym_size)
1898
1.07k
      goto error_return;
1899
84
    break;
1900
87
  case DT_VERSYM:
1901
87
    dt_versym = dyn.d_un.d_val;
1902
87
    break;
1903
76
  case DT_VERDEF:
1904
76
    dt_verdef = dyn.d_un.d_val;
1905
76
    break;
1906
638
  case DT_VERNEED:
1907
638
    dt_verneed = dyn.d_un.d_val;
1908
638
    break;
1909
252k
  default:
1910
252k
    if (dyn.d_tag == DT_MIPS_XHASH
1911
252k
        && (bed->elf_machine_code == EM_MIPS
1912
37
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1913
0
      {
1914
0
        dt_gnu_hash = dyn.d_un.d_val;
1915
0
        dt_mips_xhash = dyn.d_un.d_val;
1916
0
      }
1917
252k
    break;
1918
262k
  }
1919
262k
    }
1920
1921
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1922
     segment.  */
1923
8.56k
  if ((!dt_hash && !dt_gnu_hash)
1924
8.56k
      || !dt_strtab
1925
8.56k
      || !dt_symtab
1926
8.56k
      || !dt_strsz)
1927
8.23k
    goto error_return;
1928
1929
  /* Get dynamic string table.  */
1930
334
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1931
334
  if (filepos == (file_ptr) -1
1932
334
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1933
129
    goto error_return;
1934
1935
  /* Dynamic string table must be valid until ABFD is closed.  */
1936
205
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1937
205
  if (strbuf == NULL)
1938
3
    goto error_return;
1939
202
  if (strbuf[dt_strsz - 1] != 0)
1940
66
    {
1941
      /* It is an error if a string table is't terminated.  */
1942
66
      _bfd_error_handler
1943
  /* xgettext:c-format */
1944
66
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
1945
66
      strbuf[dt_strsz - 1] = 0;
1946
66
    }
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
202
  if (dt_hash)
1951
88
    {
1952
88
      unsigned char nb[16];
1953
88
      unsigned int hash_ent_size;
1954
1955
88
      switch (bed->elf_machine_code)
1956
88
  {
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
88
  default:
1967
88
    hash_ent_size = 4;
1968
88
    break;
1969
88
  }
1970
1971
88
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
1972
88
         NULL);
1973
88
      if (filepos == (file_ptr) -1
1974
88
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
1975
88
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1976
14
  goto error_return;
1977
1978
      /* The number of dynamic symbol table entries equals the number
1979
   of chains.  */
1980
74
      if (hash_ent_size == 8)
1981
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1982
74
      else
1983
74
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1984
74
    }
1985
114
  else
1986
114
    {
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
114
      unsigned char nb[16];
1994
114
      bfd_vma ngnubuckets;
1995
114
      bfd_vma gnusymidx;
1996
114
      size_t i, ngnuchains;
1997
114
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
1998
114
      bfd_vma buckets_vma;
1999
2000
114
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2001
114
         sizeof (nb), NULL);
2002
114
      if (filepos == (file_ptr) -1
2003
114
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2004
114
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2005
21
  goto error_return;
2006
2007
93
      ngnubuckets = bfd_get_32 (abfd, nb);
2008
93
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2009
93
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2010
93
      buckets_vma = dt_gnu_hash + 16;
2011
93
      if (bed->s->elfclass == ELFCLASS32)
2012
0
  buckets_vma += bitmaskwords * 4;
2013
93
      else
2014
93
  buckets_vma += bitmaskwords * 8;
2015
93
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2016
93
      if (filepos == (file_ptr) -1
2017
93
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2018
14
  goto error_return;
2019
2020
79
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2021
79
      if (gnubuckets == NULL)
2022
12
  goto error_return;
2023
2024
53.2k
      for (i = 0; i < ngnubuckets; i++)
2025
53.1k
  if (gnubuckets[i] != 0)
2026
38.1k
    {
2027
38.1k
      if (gnubuckets[i] < gnusymidx)
2028
7
        goto error_return;
2029
2030
38.1k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2031
3.84k
        maxchain = gnubuckets[i];
2032
38.1k
    }
2033
2034
60
      if (maxchain == 0xffffffff)
2035
9
  {
2036
9
    symcount = 0;
2037
9
    goto empty_gnu_hash;
2038
9
  }
2039
2040
51
      maxchain -= gnusymidx;
2041
51
      filepos = offset_from_vma (phdrs, phnum,
2042
51
         buckets_vma + 4 * (ngnubuckets + maxchain),
2043
51
         4, NULL);
2044
51
      if (filepos == (file_ptr) -1
2045
51
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2046
19
  goto error_return;
2047
2048
32
      do
2049
340
  {
2050
340
    if (bfd_read (nb, 4, abfd) != 4)
2051
5
      goto error_return;
2052
335
    ++maxchain;
2053
335
    if (maxchain == 0)
2054
0
      goto error_return;
2055
335
  }
2056
335
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2057
2058
27
      filepos = offset_from_vma (phdrs, phnum,
2059
27
         buckets_vma + 4 * ngnubuckets,
2060
27
         4, NULL);
2061
27
      if (filepos == (file_ptr) -1
2062
27
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2063
0
  goto error_return;
2064
2065
27
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2066
27
      if (gnuchains == NULL)
2067
6
  goto error_return;
2068
21
      ngnuchains = maxchain;
2069
2070
21
      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
21
      symcount = 0;
2085
4.05k
      for (i = 0; i < ngnubuckets; ++i)
2086
4.03k
  if (gnubuckets[i] != 0)
2087
2.15k
    {
2088
2.15k
      bfd_vma si = gnubuckets[i];
2089
2.15k
      bfd_vma off = si - gnusymidx;
2090
2.15k
      do
2091
3.09k
        {
2092
3.09k
    if (mipsxlat)
2093
0
      {
2094
0
        if (mipsxlat[off] >= symcount)
2095
0
          symcount = mipsxlat[off] + 1;
2096
0
      }
2097
3.09k
    else
2098
3.09k
      {
2099
3.09k
        if (si >= symcount)
2100
246
          symcount = si + 1;
2101
3.09k
      }
2102
3.09k
    si++;
2103
3.09k
        }
2104
3.09k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2105
2.15k
    }
2106
21
    }
2107
2108
  /* Swap in dynamic symbol table.  */
2109
95
  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
95
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2116
95
  if (filepos == (file_ptr) -1
2117
95
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2118
8
    goto error_return;
2119
87
  esymbuf_size = amt;
2120
87
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2121
87
         &esymbuf_addr, &esymbuf_size);
2122
87
  if (esymbuf == NULL)
2123
16
    goto error_return;
2124
2125
71
  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
71
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2133
71
  if (isymbuf == NULL)
2134
0
    goto error_return;
2135
2136
71
  swap_symbol_in = bed->s->swap_symbol_in;
2137
2138
  /* Convert the symbols to internal form.  */
2139
71
  isymend = isymbuf + symcount;
2140
71
  for (esym = esymbuf, isym = isymbuf;
2141
340
       isym < isymend;
2142
269
       esym += extsym_size, isym++)
2143
283
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2144
283
  || isym->st_name >= dt_strsz)
2145
14
      {
2146
14
  bfd_set_error (bfd_error_invalid_operation);
2147
14
  goto error_return;
2148
14
      }
2149
2150
57
  if (dt_versym)
2151
46
    {
2152
      /* Swap in DT_VERSYM.  */
2153
46
      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
46
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2160
46
      if (filepos == (file_ptr) -1
2161
46
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2162
14
  goto error_return;
2163
2164
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2165
32
      versym = _bfd_mmap_persistent (abfd, amt);
2166
2167
32
      if (dt_verdef)
2168
16
  {
2169
    /* Read in DT_VERDEF.  */
2170
16
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2171
16
             0, &verdef_size);
2172
16
    if (filepos == (file_ptr) -1
2173
16
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2174
13
      goto error_return;
2175
2176
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2177
3
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2178
3
  }
2179
2180
19
      if (dt_verneed)
2181
16
  {
2182
    /* Read in DT_VERNEED.  */
2183
16
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2184
16
             0, &verneed_size);
2185
16
    if (filepos == (file_ptr) -1
2186
16
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2187
9
      goto error_return;
2188
2189
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2190
7
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2191
7
  }
2192
19
    }
2193
2194
30
 empty_gnu_hash:
2195
30
  elf_tdata (abfd)->dt_strtab = strbuf;
2196
30
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2197
30
  elf_tdata (abfd)->dt_symtab = isymbuf;
2198
30
  elf_tdata (abfd)->dt_symtab_count = symcount;
2199
30
  elf_tdata (abfd)->dt_versym = versym;
2200
30
  elf_tdata (abfd)->dt_verdef = verdef;
2201
30
  elf_tdata (abfd)->dt_verneed = verneed;
2202
30
  elf_tdata (abfd)->dt_verdef_count
2203
30
    = verdef_size / sizeof (Elf_External_Verdef);
2204
30
  elf_tdata (abfd)->dt_verneed_count
2205
30
    = verneed_size / sizeof (Elf_External_Verneed);
2206
2207
30
  res = true;
2208
2209
12.1k
 error_return:
2210
  /* Restore file position for elf_object_p.  */
2211
12.1k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2212
0
    res = false;
2213
12.1k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2214
12.1k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2215
12.1k
  free (gnubuckets);
2216
12.1k
  free (gnuchains);
2217
12.1k
  free (mipsxlat);
2218
12.1k
  return res;
2219
30
}
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
5.00M
{
2279
5.00M
  const char *version_string = NULL;
2280
5.00M
  if ((elf_dynversym (abfd) != 0
2281
5.00M
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2282
5.00M
      || (elf_tdata (abfd)->dt_versym != NULL
2283
1.57M
    && (elf_tdata (abfd)->dt_verdef != NULL
2284
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2285
3.42M
    {
2286
3.42M
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2287
2288
3.42M
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2289
3.42M
      vernum &= VERSYM_VERSION;
2290
2291
3.42M
      if (vernum == 0)
2292
471k
  version_string = "";
2293
2.95M
      else if (vernum == 1
2294
2.95M
         && (vernum > elf_tdata (abfd)->cverdefs
2295
2.93M
       || (elf_tdata (abfd)->verdef[0].vd_flags
2296
0
           == VER_FLG_BASE)))
2297
2.93M
  version_string = base_p ? "Base" : "";
2298
15.2k
      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
15.2k
      else
2310
15.2k
  {
2311
15.2k
    Elf_Internal_Verneed *t;
2312
2313
15.2k
    version_string = _("<corrupt>");
2314
15.2k
    for (t = elf_tdata (abfd)->verref;
2315
43.1k
         t != NULL;
2316
27.9k
         t = t->vn_nextref)
2317
27.9k
      {
2318
27.9k
        Elf_Internal_Vernaux *a;
2319
2320
75.4k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2321
61.1k
    {
2322
61.1k
      if (a->vna_other == vernum)
2323
13.6k
        {
2324
13.6k
          *hidden = true;
2325
13.6k
          version_string = a->vna_nodename;
2326
13.6k
          break;
2327
13.6k
        }
2328
61.1k
    }
2329
27.9k
      }
2330
15.2k
  }
2331
3.42M
    }
2332
5.00M
  return version_string;
2333
5.00M
}
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
10.0M
{
2436
10.0M
  Elf_Internal_Shdr *hdr;
2437
10.0M
  Elf_Internal_Ehdr *ehdr;
2438
10.0M
  const struct elf_backend_data *bed;
2439
10.0M
  const char *name;
2440
10.0M
  bool ret = true;
2441
2442
10.0M
  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
10.0M
  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
10.0M
  elf_tdata (abfd)->being_created[shindex] = true;
2455
2456
10.0M
  hdr = elf_elfsections (abfd)[shindex];
2457
10.0M
  ehdr = elf_elfheader (abfd);
2458
10.0M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2459
10.0M
            hdr->sh_name);
2460
10.0M
  if (name == NULL)
2461
22.1k
    goto fail;
2462
2463
10.0M
  bed = get_elf_backend_data (abfd);
2464
10.0M
  switch (hdr->sh_type)
2465
10.0M
    {
2466
1.92M
    case SHT_NULL:
2467
      /* Inactive section. Throw it away.  */
2468
1.92M
      goto success;
2469
2470
2.25M
    case SHT_PROGBITS:   /* Normal section with contents.  */
2471
2.34M
    case SHT_NOBITS:   /* .bss section.  */
2472
2.38M
    case SHT_HASH:   /* .hash section.  */
2473
2.49M
    case SHT_NOTE:   /* .note section.  */
2474
2.65M
    case SHT_INIT_ARRAY: /* .init_array section.  */
2475
2.73M
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2476
2.79M
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2477
2.80M
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2478
2.80M
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2479
2.80M
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2480
2.80M
      goto success;
2481
2482
36.5k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2483
36.5k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2484
21
  goto fail;
2485
2486
36.4k
      if (hdr->sh_link > elf_numsections (abfd))
2487
74
  {
2488
    /* PR 10478: Accept Solaris binaries with a sh_link field
2489
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2490
74
    switch (bfd_get_arch (abfd))
2491
74
      {
2492
74
      case bfd_arch_i386:
2493
74
      case bfd_arch_sparc:
2494
74
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2495
74
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2496
74
    break;
2497
        /* Otherwise fall through.  */
2498
0
      default:
2499
0
        goto fail;
2500
74
      }
2501
74
  }
2502
36.4k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2503
0
  goto fail;
2504
36.4k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2505
17.0k
  {
2506
17.0k
    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
17.0k
    if (elf_dynsymtab (abfd) != 0)
2512
583
      {
2513
583
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2514
583
        hdr->sh_link = dynsymhdr->sh_link;
2515
583
      }
2516
16.4k
    else
2517
16.4k
      {
2518
16.4k
        unsigned int i, num_sec;
2519
2520
16.4k
        num_sec = elf_numsections (abfd);
2521
306k
        for (i = 1; i < num_sec; i++)
2522
290k
    {
2523
290k
      dynsymhdr = elf_elfsections (abfd)[i];
2524
290k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2525
711
        {
2526
711
          hdr->sh_link = dynsymhdr->sh_link;
2527
711
          break;
2528
711
        }
2529
290k
    }
2530
16.4k
      }
2531
17.0k
  }
2532
36.4k
      goto success;
2533
2534
868k
    case SHT_SYMTAB:   /* A symbol table.  */
2535
868k
      if (elf_onesymtab (abfd) == shindex)
2536
708k
  goto success;
2537
2538
159k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2539
1.84k
  goto fail;
2540
2541
158k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2542
24.0k
  {
2543
24.0k
    if (hdr->sh_size != 0)
2544
337
      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
23.6k
    hdr->sh_info = 0;
2549
23.6k
    goto success;
2550
24.0k
  }
2551
2552
      /* PR 18854: A binary might contain more than one symbol table.
2553
   Unusual, but possible.  Warn, but continue.  */
2554
134k
      if (elf_onesymtab (abfd) != 0)
2555
3.06k
  {
2556
3.06k
    _bfd_error_handler
2557
      /* xgettext:c-format */
2558
3.06k
      (_("%pB: warning: multiple symbol tables detected"
2559
3.06k
         " - ignoring the table in section %u"),
2560
3.06k
       abfd, shindex);
2561
3.06k
    goto success;
2562
3.06k
  }
2563
130k
      elf_onesymtab (abfd) = shindex;
2564
130k
      elf_symtab_hdr (abfd) = *hdr;
2565
130k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2566
130k
      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
130k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2575
130k
    && (abfd->flags & DYNAMIC) != 0
2576
130k
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2577
7.39k
            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
130k
      {
2584
130k
  elf_section_list * entry;
2585
130k
  unsigned int i, num_sec;
2586
2587
143k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2588
14.1k
    if (entry->hdr.sh_link == shindex)
2589
1.15k
      goto success;
2590
2591
129k
  num_sec = elf_numsections (abfd);
2592
399k
  for (i = shindex + 1; i < num_sec; i++)
2593
270k
    {
2594
270k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2595
2596
270k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2597
270k
    && hdr2->sh_link == shindex)
2598
960
        break;
2599
270k
    }
2600
2601
129k
  if (i == num_sec)
2602
3.69M
    for (i = 1; i < shindex; i++)
2603
3.57M
      {
2604
3.57M
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2605
2606
3.57M
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2607
3.57M
      && hdr2->sh_link == shindex)
2608
8.98k
    break;
2609
3.57M
      }
2610
2611
129k
  if (i != shindex)
2612
9.94k
    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
129k
  goto success;
2616
130k
      }
2617
2618
27.5k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2619
27.5k
      if (elf_dynsymtab (abfd) == shindex)
2620
14.1k
  goto success;
2621
2622
13.3k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2623
753
  goto fail;
2624
2625
12.5k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2626
1.78k
  {
2627
1.78k
    if (hdr->sh_size != 0)
2628
60
      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.72k
    hdr->sh_info = 0;
2634
1.72k
    goto success;
2635
1.78k
  }
2636
2637
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2638
   Unusual, but possible.  Warn, but continue.  */
2639
10.7k
      if (elf_dynsymtab (abfd) != 0)
2640
496
  {
2641
496
    _bfd_error_handler
2642
      /* xgettext:c-format */
2643
496
      (_("%pB: warning: multiple dynamic symbol tables detected"
2644
496
         " - ignoring the table in section %u"),
2645
496
       abfd, shindex);
2646
496
    goto success;
2647
496
  }
2648
10.2k
      elf_dynsymtab (abfd) = shindex;
2649
10.2k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2650
10.2k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2651
10.2k
      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
10.2k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2656
10.2k
      goto success;
2657
2658
90.1k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2659
90.1k
      {
2660
90.1k
  elf_section_list * entry;
2661
2662
129k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2663
47.7k
    if (entry->ndx == shindex)
2664
8.25k
      goto success;
2665
2666
81.9k
  entry = bfd_alloc (abfd, sizeof (*entry));
2667
81.9k
  if (entry == NULL)
2668
0
    goto fail;
2669
81.9k
  entry->ndx = shindex;
2670
81.9k
  entry->hdr = * hdr;
2671
81.9k
  entry->next = elf_symtab_shndx_list (abfd);
2672
81.9k
  elf_symtab_shndx_list (abfd) = entry;
2673
81.9k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2674
81.9k
  goto success;
2675
81.9k
      }
2676
2677
746k
    case SHT_STRTAB:   /* A string table.  */
2678
746k
      if (hdr->bfd_section != NULL)
2679
6.25k
  goto success;
2680
2681
740k
      if (ehdr->e_shstrndx == shindex)
2682
643k
  {
2683
643k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2684
643k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2685
643k
    goto success;
2686
643k
  }
2687
2688
96.8k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2689
30.9k
  {
2690
31.6k
  symtab_strtab:
2691
31.6k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2692
31.6k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2693
31.6k
    goto success;
2694
30.9k
  }
2695
2696
65.8k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2697
5.15k
  {
2698
5.18k
  dynsymtab_strtab:
2699
5.18k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2700
5.18k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2701
5.18k
    elf_elfsections (abfd)[shindex] = hdr;
2702
    /* We also treat this as a regular section, so that objcopy
2703
       can handle it.  */
2704
5.18k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2705
5.18k
             shindex);
2706
5.18k
    goto success;
2707
5.15k
  }
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
60.7k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2713
60.4k
  {
2714
60.4k
    unsigned int i, num_sec;
2715
2716
60.4k
    num_sec = elf_numsections (abfd);
2717
1.18M
    for (i = 1; i < num_sec; i++)
2718
1.12M
      {
2719
1.12M
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2720
1.12M
        if (hdr2->sh_link == shindex)
2721
28.1k
    {
2722
      /* Prevent endless recursion on broken objects.  */
2723
28.1k
      if (i == shindex)
2724
329
        goto fail;
2725
27.7k
      if (! bfd_section_from_shdr (abfd, i))
2726
888
        goto fail;
2727
26.8k
      if (elf_onesymtab (abfd) == i)
2728
734
        goto symtab_strtab;
2729
26.1k
      if (elf_dynsymtab (abfd) == i)
2730
37
        goto dynsymtab_strtab;
2731
26.1k
    }
2732
1.12M
      }
2733
60.4k
  }
2734
58.7k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2735
58.7k
      goto success;
2736
2737
191k
    case SHT_REL:
2738
1.08M
    case SHT_RELA:
2739
1.08M
    case SHT_RELR:
2740
      /* *These* do a lot of work -- but build no sections!  */
2741
1.08M
      {
2742
1.08M
  asection *target_sect;
2743
1.08M
  Elf_Internal_Shdr *hdr2, **p_hdr;
2744
1.08M
  unsigned int num_sec = elf_numsections (abfd);
2745
1.08M
  struct bfd_elf_section_data *esdt;
2746
1.08M
  bfd_size_type size;
2747
2748
1.08M
  if (hdr->sh_type == SHT_REL)
2749
191k
    size = bed->s->sizeof_rel;
2750
894k
  else if (hdr->sh_type == SHT_RELA)
2751
893k
    size = bed->s->sizeof_rela;
2752
1.51k
  else
2753
1.51k
    size = bed->s->arch_size / 8;
2754
1.08M
  if (hdr->sh_entsize != size)
2755
2.56k
    goto fail;
2756
2757
  /* Check for a bogus link to avoid crashing.  */
2758
1.08M
  if (hdr->sh_link >= num_sec)
2759
76
    {
2760
76
      _bfd_error_handler
2761
        /* xgettext:c-format */
2762
76
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2763
76
         abfd, hdr->sh_link, name, shindex);
2764
76
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2765
76
      goto success;
2766
76
    }
2767
2768
  /* Get the symbol table.  */
2769
1.08M
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2770
1.08M
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2771
1.08M
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2772
581
    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
1.08M
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2783
1.08M
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2784
1.08M
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2785
1.08M
      || hdr->sh_type == SHT_RELR
2786
1.08M
      || hdr->sh_link == SHN_UNDEF
2787
1.08M
      || hdr->sh_link != elf_onesymtab (abfd)
2788
1.08M
      || hdr->sh_info == SHN_UNDEF
2789
1.08M
      || hdr->sh_info >= num_sec
2790
1.08M
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2791
1.08M
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2792
446k
    {
2793
446k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2794
446k
      goto success;
2795
446k
    }
2796
2797
636k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2798
1.80k
    goto fail;
2799
2800
635k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2801
635k
  if (target_sect == NULL)
2802
211
    goto fail;
2803
2804
634k
  esdt = elf_section_data (target_sect);
2805
634k
  if (hdr->sh_type == SHT_RELA)
2806
601k
    p_hdr = &esdt->rela.hdr;
2807
33.2k
  else
2808
33.2k
    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
634k
  if (*p_hdr != NULL)
2814
11.5k
    {
2815
11.5k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2816
3.75k
        {
2817
3.75k
    _bfd_error_handler
2818
      /* xgettext:c-format */
2819
3.75k
      (_("%pB: warning: secondary relocation section '%s' "
2820
3.75k
         "for section %pA found - ignoring"),
2821
3.75k
       abfd, name, target_sect);
2822
3.75k
        }
2823
7.82k
      else
2824
7.82k
        esdt->has_secondary_relocs = true;
2825
11.5k
      goto success;
2826
11.5k
    }
2827
2828
623k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2829
623k
  if (hdr2 == NULL)
2830
0
    goto fail;
2831
623k
  *hdr2 = *hdr;
2832
623k
  *p_hdr = hdr2;
2833
623k
  elf_elfsections (abfd)[shindex] = hdr2;
2834
623k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2835
623k
             * bed->s->int_rels_per_ext_rel);
2836
623k
  target_sect->flags |= SEC_RELOC;
2837
623k
  target_sect->relocation = NULL;
2838
623k
  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
623k
  if (hdr->sh_size != 0)
2842
615k
    {
2843
615k
      if (hdr->sh_type == SHT_RELA)
2844
590k
        target_sect->use_rela_p = 1;
2845
615k
    }
2846
623k
  abfd->flags |= HAS_RELOC;
2847
623k
  goto success;
2848
623k
      }
2849
2850
7.73k
    case SHT_GNU_verdef:
2851
7.73k
      if (hdr->sh_info != 0)
2852
4.50k
  elf_dynverdef (abfd) = shindex;
2853
7.73k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2854
7.73k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2855
7.73k
      goto success;
2856
2857
3.43k
    case SHT_GNU_versym:
2858
3.43k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2859
177
  goto fail;
2860
2861
3.25k
      elf_dynversym (abfd) = shindex;
2862
3.25k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2863
3.25k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2864
3.25k
      goto success;
2865
2866
8.66k
    case SHT_GNU_verneed:
2867
8.66k
      if (hdr->sh_info != 0)
2868
6.63k
  elf_dynverref (abfd) = shindex;
2869
8.66k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2870
8.66k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2871
8.66k
      goto success;
2872
2873
58.3k
    case SHT_SHLIB:
2874
58.3k
      goto success;
2875
2876
272k
    case SHT_GROUP:
2877
272k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2878
27
  goto fail;
2879
2880
272k
      goto success;
2881
2882
2.13M
    default:
2883
      /* Possibly an attributes section.  */
2884
2.13M
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2885
2.13M
    || hdr->sh_type == bed->obj_attrs_section_type)
2886
115k
  {
2887
115k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2888
9
      goto fail;
2889
115k
    _bfd_elf_parse_attributes (abfd, hdr);
2890
115k
    goto success;
2891
115k
  }
2892
2893
      /* Check for any processor-specific section types.  */
2894
2.02M
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2895
1.73M
  goto success;
2896
2897
291k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2898
133k
  {
2899
133k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2900
      /* FIXME: How to properly handle allocated section reserved
2901
         for applications?  */
2902
18.9k
      _bfd_error_handler
2903
        /* xgettext:c-format */
2904
18.9k
        (_("%pB: unknown type [%#x] section `%s'"),
2905
18.9k
         abfd, hdr->sh_type, name);
2906
114k
    else
2907
114k
      {
2908
        /* Allow sections reserved for applications.  */
2909
114k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2910
114k
        goto success;
2911
114k
      }
2912
133k
  }
2913
157k
      else if (hdr->sh_type >= SHT_LOPROC
2914
157k
         && hdr->sh_type <= SHT_HIPROC)
2915
  /* FIXME: We should handle this section.  */
2916
23.1k
  _bfd_error_handler
2917
    /* xgettext:c-format */
2918
23.1k
    (_("%pB: unknown type [%#x] section `%s'"),
2919
23.1k
     abfd, hdr->sh_type, name);
2920
134k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2921
47.3k
  {
2922
    /* Unrecognised OS-specific sections.  */
2923
47.3k
    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.56k
      _bfd_error_handler
2928
        /* xgettext:c-format */
2929
3.56k
        (_("%pB: unknown type [%#x] section `%s'"),
2930
3.56k
         abfd, hdr->sh_type, name);
2931
43.7k
    else
2932
43.7k
      {
2933
        /* Otherwise it should be processed.  */
2934
43.7k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2935
43.7k
        goto success;
2936
43.7k
      }
2937
47.3k
  }
2938
87.1k
      else
2939
  /* FIXME: We should handle this section.  */
2940
87.1k
  _bfd_error_handler
2941
    /* xgettext:c-format */
2942
87.1k
    (_("%pB: unknown type [%#x] section `%s'"),
2943
87.1k
     abfd, hdr->sh_type, name);
2944
2945
132k
      goto fail;
2946
10.0M
    }
2947
2948
164k
 fail:
2949
164k
  ret = false;
2950
10.0M
 success:
2951
10.0M
  elf_tdata (abfd)->being_created[shindex] = false;
2952
10.0M
  return ret;
2953
164k
}
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.84M
{
2992
1.84M
  if (sec_index >= elf_numsections (abfd))
2993
75.1k
    return NULL;
2994
1.76M
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2995
1.84M
}
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
4.95M
{
3167
4.95M
  int i;
3168
4.95M
  int len;
3169
3170
4.95M
  len = strlen (name);
3171
3172
29.6M
  for (i = 0; spec[i].prefix != NULL; i++)
3173
25.8M
    {
3174
25.8M
      int suffix_len;
3175
25.8M
      int prefix_len = spec[i].prefix_length;
3176
3177
25.8M
      if (len < prefix_len)
3178
14.3M
  continue;
3179
11.5M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3180
9.83M
  continue;
3181
3182
1.71M
      suffix_len = spec[i].suffix_length;
3183
1.71M
      if (suffix_len <= 0)
3184
1.69M
  {
3185
1.69M
    if (name[prefix_len] != 0)
3186
1.22M
      {
3187
1.22M
        if (suffix_len == 0)
3188
343k
    continue;
3189
878k
        if (name[prefix_len] != '.'
3190
878k
      && (suffix_len == -2
3191
199k
          || (rela && spec[i].type == SHT_REL)))
3192
168k
    continue;
3193
878k
      }
3194
1.69M
  }
3195
26.9k
      else
3196
26.9k
  {
3197
26.9k
    if (len < prefix_len + suffix_len)
3198
7.58k
      continue;
3199
19.3k
    if (memcmp (name + len - suffix_len,
3200
19.3k
          spec[i].prefix + prefix_len,
3201
19.3k
          suffix_len) != 0)
3202
8.38k
      continue;
3203
19.3k
  }
3204
1.19M
      return &spec[i];
3205
1.71M
    }
3206
3207
3.76M
  return NULL;
3208
4.95M
}
3209
3210
const struct bfd_elf_special_section *
3211
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3212
5.76M
{
3213
5.76M
  int i;
3214
5.76M
  const struct bfd_elf_special_section *spec;
3215
5.76M
  const struct elf_backend_data *bed;
3216
3217
  /* See if this is one of the special sections.  */
3218
5.76M
  if (sec->name == NULL)
3219
0
    return NULL;
3220
3221
5.76M
  bed = get_elf_backend_data (abfd);
3222
5.76M
  spec = bed->special_sections;
3223
5.76M
  if (spec)
3224
2.98M
    {
3225
2.98M
      spec = _bfd_elf_get_special_section (sec->name,
3226
2.98M
             bed->special_sections,
3227
2.98M
             sec->use_rela_p);
3228
2.98M
      if (spec != NULL)
3229
8.88k
  return spec;
3230
2.98M
    }
3231
3232
5.75M
  if (sec->name[0] != '.')
3233
3.79M
    return NULL;
3234
3235
1.95M
  i = sec->name[1] - 'b';
3236
1.95M
  if (i < 0 || i > 'z' - 'b')
3237
62.2k
    return NULL;
3238
3239
1.89M
  spec = special_sections[i];
3240
3241
1.89M
  if (spec == NULL)
3242
65.8k
    return NULL;
3243
3244
1.83M
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3245
1.89M
}
3246
3247
bool
3248
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3249
5.76M
{
3250
5.76M
  struct bfd_elf_section_data *sdata;
3251
5.76M
  const struct elf_backend_data *bed;
3252
5.76M
  const struct bfd_elf_special_section *ssect;
3253
3254
5.76M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3255
5.76M
  if (sdata == NULL)
3256
4.60M
    {
3257
4.60M
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3258
4.60M
                sizeof (*sdata));
3259
4.60M
      if (sdata == NULL)
3260
0
  return false;
3261
4.60M
      sec->used_by_bfd = sdata;
3262
4.60M
    }
3263
3264
  /* Indicate whether or not this section should use RELA relocations.  */
3265
5.76M
  bed = get_elf_backend_data (abfd);
3266
5.76M
  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
5.76M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3271
5.76M
  if (ssect != NULL)
3272
1.19M
    {
3273
1.19M
      elf_section_type (sec) = ssect->type;
3274
1.19M
      elf_section_flags (sec) = ssect->attr;
3275
1.19M
    }
3276
3277
5.76M
  return _bfd_generic_new_section_hook (abfd, sec);
3278
5.76M
}
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
463k
{
3306
463k
  asection *newsect;
3307
463k
  char *name;
3308
463k
  char namebuf[64];
3309
463k
  size_t len;
3310
463k
  int split;
3311
463k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3312
3313
463k
  split = ((hdr->p_memsz > 0)
3314
463k
      && (hdr->p_filesz > 0)
3315
463k
      && (hdr->p_memsz > hdr->p_filesz));
3316
3317
463k
  if (hdr->p_filesz > 0)
3318
403k
    {
3319
403k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3320
403k
      len = strlen (namebuf) + 1;
3321
403k
      name = (char *) bfd_alloc (abfd, len);
3322
403k
      if (!name)
3323
0
  return false;
3324
403k
      memcpy (name, namebuf, len);
3325
403k
      newsect = bfd_make_section (abfd, name);
3326
403k
      if (newsect == NULL)
3327
0
  return false;
3328
403k
      newsect->vma = hdr->p_vaddr / opb;
3329
403k
      newsect->lma = hdr->p_paddr / opb;
3330
403k
      newsect->size = hdr->p_filesz;
3331
403k
      newsect->filepos = hdr->p_offset;
3332
403k
      newsect->flags |= SEC_HAS_CONTENTS;
3333
403k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3334
403k
      if (hdr->p_type == PT_LOAD)
3335
4.06k
  {
3336
4.06k
    newsect->flags |= SEC_ALLOC;
3337
4.06k
    newsect->flags |= SEC_LOAD;
3338
4.06k
    if (hdr->p_flags & PF_X)
3339
1.70k
      {
3340
        /* FIXME: all we known is that it has execute PERMISSION,
3341
     may be data.  */
3342
1.70k
        newsect->flags |= SEC_CODE;
3343
1.70k
      }
3344
4.06k
  }
3345
403k
      if (!(hdr->p_flags & PF_W))
3346
281k
  {
3347
281k
    newsect->flags |= SEC_READONLY;
3348
281k
  }
3349
403k
    }
3350
3351
463k
  if (hdr->p_memsz > hdr->p_filesz)
3352
190k
    {
3353
190k
      bfd_vma align;
3354
3355
190k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3356
190k
      len = strlen (namebuf) + 1;
3357
190k
      name = (char *) bfd_alloc (abfd, len);
3358
190k
      if (!name)
3359
0
  return false;
3360
190k
      memcpy (name, namebuf, len);
3361
190k
      newsect = bfd_make_section (abfd, name);
3362
190k
      if (newsect == NULL)
3363
0
  return false;
3364
190k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3365
190k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3366
190k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3367
190k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3368
190k
      align = newsect->vma & -newsect->vma;
3369
190k
      if (align == 0 || align > hdr->p_align)
3370
30.8k
  align = hdr->p_align;
3371
190k
      newsect->alignment_power = bfd_log2 (align);
3372
190k
      if (hdr->p_type == PT_LOAD)
3373
2.44k
  {
3374
2.44k
    newsect->flags |= SEC_ALLOC;
3375
2.44k
    if (hdr->p_flags & PF_X)
3376
954
      newsect->flags |= SEC_CODE;
3377
2.44k
  }
3378
190k
      if (!(hdr->p_flags & PF_W))
3379
133k
  newsect->flags |= SEC_READONLY;
3380
190k
    }
3381
3382
463k
  return true;
3383
463k
}
3384
3385
static bool
3386
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3387
4.63k
{
3388
  /* The return value is ignored.  Build-ids are considered optional.  */
3389
4.63k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3390
4.63k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3391
4.63k
      (templ, offset);
3392
0
  return false;
3393
4.63k
}
3394
3395
bool
3396
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3397
463k
{
3398
463k
  const struct elf_backend_data *bed;
3399
3400
463k
  switch (hdr->p_type)
3401
463k
    {
3402
89.6k
    case PT_NULL:
3403
89.6k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3404
3405
4.65k
    case PT_LOAD:
3406
4.65k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3407
0
  return false;
3408
4.65k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3409
4.63k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3410
4.65k
      return true;
3411
3412
2.01k
    case PT_DYNAMIC:
3413
2.01k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3414
3415
1.01k
    case PT_INTERP:
3416
1.01k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3417
3418
1.03k
    case PT_NOTE:
3419
1.03k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3420
0
  return false;
3421
1.03k
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3422
1.03k
          hdr->p_align))
3423
696
  return false;
3424
340
      return true;
3425
3426
740
    case PT_SHLIB:
3427
740
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3428
3429
3.19k
    case PT_PHDR:
3430
3.19k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3431
3432
113
    case PT_GNU_EH_FRAME:
3433
113
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3434
113
                "eh_frame_hdr");
3435
3436
39
    case PT_GNU_STACK:
3437
39
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3438
3439
41
    case PT_GNU_RELRO:
3440
41
      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
361k
    default:
3447
      /* Check for any processor-specific program segment types.  */
3448
361k
      bed = get_elf_backend_data (abfd);
3449
361k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3450
463k
    }
3451
463k
}
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.30k
{
3474
1.30k
  char *name = (char *) bfd_alloc (abfd,
3475
1.30k
           sizeof ".rela" + strlen (sec_name));
3476
1.30k
  if (name == NULL)
3477
0
    return false;
3478
3479
1.30k
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3480
1.30k
  rel_hdr->sh_name =
3481
1.30k
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3482
1.30k
          false);
3483
1.30k
  if (rel_hdr->sh_name == (unsigned int) -1)
3484
0
    return false;
3485
3486
1.30k
  return true;
3487
1.30k
}
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.30k
{
3501
1.30k
  Elf_Internal_Shdr *rel_hdr;
3502
1.30k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3503
3504
1.30k
  BFD_ASSERT (reldata->hdr == NULL);
3505
1.30k
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3506
1.30k
  if (rel_hdr == NULL)
3507
0
    return false;
3508
1.30k
  reldata->hdr = rel_hdr;
3509
3510
1.30k
  if (delay_sh_name_p)
3511
0
    rel_hdr->sh_name = (unsigned int) -1;
3512
1.30k
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3513
1.30k
          use_rela_p))
3514
0
    return false;
3515
1.30k
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3516
1.30k
  rel_hdr->sh_entsize = (use_rela_p
3517
1.30k
       ? bed->s->sizeof_rela
3518
1.30k
       : bed->s->sizeof_rel);
3519
1.30k
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3520
1.30k
  rel_hdr->sh_flags = 0;
3521
1.30k
  rel_hdr->sh_addr = 0;
3522
1.30k
  rel_hdr->sh_size = 0;
3523
1.30k
  rel_hdr->sh_offset = 0;
3524
3525
1.30k
  return true;
3526
1.30k
}
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
3.24k
{
3533
3.24k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3534
3.24k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3535
73
    return SHT_NOBITS;
3536
3.16k
  return SHT_PROGBITS;
3537
3.24k
}
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
3.61k
{
3550
3.61k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3551
3.61k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3552
3.61k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3553
3.61k
  Elf_Internal_Shdr *this_hdr;
3554
3.61k
  unsigned int sh_type;
3555
3.61k
  const char *name = asect->name;
3556
3.61k
  bool delay_sh_name_p = false;
3557
3.61k
  bfd_vma mask;
3558
3559
3.61k
  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
3.61k
  this_hdr = &esd->this_hdr;
3567
3568
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3569
3.61k
  if (arg->link_info
3570
3.61k
      && (abfd->flags & BFD_COMPRESS) != 0
3571
3.61k
      && (asect->flags & SEC_DEBUGGING) != 0
3572
3.61k
      && (asect->flags & SEC_ALLOC) == 0
3573
3.61k
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3574
3.61k
      && name[1] == 'd'
3575
3.61k
      && 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
3.61k
  if (delay_sh_name_p)
3584
0
    this_hdr->sh_name = (unsigned int) -1;
3585
3.61k
  else
3586
3.61k
    {
3587
3.61k
      this_hdr->sh_name
3588
3.61k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3589
3.61k
                name, false);
3590
3.61k
      if (this_hdr->sh_name == (unsigned int) -1)
3591
0
  {
3592
0
    arg->failed = true;
3593
0
    return;
3594
0
  }
3595
3.61k
    }
3596
3597
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3598
3599
3.61k
  if ((asect->flags & SEC_ALLOC) != 0
3600
3.61k
      || asect->user_set_vma)
3601
3.61k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3602
0
  else
3603
0
    this_hdr->sh_addr = 0;
3604
3605
3.61k
  this_hdr->sh_offset = 0;
3606
3.61k
  this_hdr->sh_size = asect->size;
3607
3.61k
  this_hdr->sh_link = 0;
3608
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3609
3.61k
  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
3.61k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3621
3.61k
  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
3.61k
  this_hdr->bfd_section = asect;
3626
3.61k
  this_hdr->contents = NULL;
3627
3628
  /* If the section type is unspecified, we set it based on
3629
     asect->flags.  */
3630
3.61k
  if (asect->type != 0)
3631
0
    sh_type = asect->type;
3632
3.61k
  else if ((asect->flags & SEC_GROUP) != 0)
3633
376
    sh_type = SHT_GROUP;
3634
3.24k
  else
3635
3.24k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3636
3637
3.61k
  if (this_hdr->sh_type == SHT_NULL)
3638
12
    this_hdr->sh_type = sh_type;
3639
3.60k
  else if (this_hdr->sh_type == SHT_NOBITS
3640
3.60k
     && sh_type == SHT_PROGBITS
3641
3.60k
     && (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
3.61k
  switch (this_hdr->sh_type)
3653
3.61k
    {
3654
95
    default:
3655
95
      break;
3656
3657
95
    case SHT_STRTAB:
3658
52
    case SHT_NOTE:
3659
125
    case SHT_NOBITS:
3660
2.70k
    case SHT_PROGBITS:
3661
2.70k
      break;
3662
3663
76
    case SHT_INIT_ARRAY:
3664
127
    case SHT_FINI_ARRAY:
3665
127
    case SHT_PREINIT_ARRAY:
3666
127
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3667
127
      break;
3668
3669
37
    case SHT_HASH:
3670
37
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3671
37
      break;
3672
3673
42
    case SHT_DYNSYM:
3674
42
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3675
42
      break;
3676
3677
43
    case SHT_DYNAMIC:
3678
43
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3679
43
      break;
3680
3681
36
    case SHT_RELA:
3682
36
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3683
36
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3684
36
      break;
3685
3686
58
     case SHT_REL:
3687
58
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3688
56
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3689
58
      break;
3690
3691
33
     case SHT_GNU_versym:
3692
33
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3693
33
      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
42
    case SHT_GNU_verneed:
3708
42
      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
42
      if (this_hdr->sh_info == 0)
3713
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3714
42
      else
3715
42
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3716
42
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3717
42
      break;
3718
3719
376
    case SHT_GROUP:
3720
376
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3721
376
      break;
3722
3723
19
    case SHT_GNU_HASH:
3724
19
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3725
19
      break;
3726
3.61k
    }
3727
3728
3.61k
  if ((asect->flags & SEC_ALLOC) != 0)
3729
2.70k
    this_hdr->sh_flags |= SHF_ALLOC;
3730
3.61k
  if ((asect->flags & SEC_READONLY) == 0)
3731
956
    this_hdr->sh_flags |= SHF_WRITE;
3732
3.61k
  if ((asect->flags & SEC_CODE) != 0)
3733
743
    this_hdr->sh_flags |= SHF_EXECINSTR;
3734
3.61k
  if ((asect->flags & SEC_MERGE) != 0)
3735
174
    {
3736
174
      this_hdr->sh_flags |= SHF_MERGE;
3737
174
      this_hdr->sh_entsize = asect->entsize;
3738
174
    }
3739
3.61k
  if ((asect->flags & SEC_STRINGS) != 0)
3740
162
    {
3741
162
      this_hdr->sh_flags |= SHF_STRINGS;
3742
162
      this_hdr->sh_entsize = asect->entsize;
3743
162
    }
3744
3.61k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3745
1.14k
    this_hdr->sh_flags |= SHF_GROUP;
3746
3.61k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3747
2
    {
3748
2
      this_hdr->sh_flags |= SHF_TLS;
3749
2
      if (asect->size == 0
3750
2
    && (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
2
    }
3763
3.61k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3764
59
    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
3.61k
  if ((asect->flags & SEC_RELOC) != 0)
3771
1.30k
    {
3772
      /* When doing a relocatable link, create both REL and RELA sections if
3773
   needed.  */
3774
1.30k
      if (arg->link_info
3775
    /* Do the normal setup if we wouldn't create any sections here.  */
3776
1.30k
    && esd->rel.count + esd->rela.count > 0
3777
1.30k
    && (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.30k
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3796
1.30k
            (asect->use_rela_p
3797
1.30k
             ? &esd->rela : &esd->rel),
3798
1.30k
            name,
3799
1.30k
            asect->use_rela_p,
3800
1.30k
            delay_sh_name_p))
3801
0
  {
3802
0
    arg->failed = true;
3803
0
    return;
3804
0
  }
3805
1.30k
    }
3806
3807
  /* Check for processor-specific section types.  */
3808
3.61k
  sh_type = this_hdr->sh_type;
3809
3.61k
  if (bed->elf_backend_fake_sections
3810
3.61k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3811
0
    {
3812
0
      arg->failed = true;
3813
0
      return;
3814
0
    }
3815
3816
3.61k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3817
73
    {
3818
      /* Don't change the header type from NOBITS if we are being
3819
   called for objcopy --only-keep-debug.  */
3820
73
      this_hdr->sh_type = sh_type;
3821
73
    }
3822
3.61k
}
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
3.61k
{
3832
3.61k
  bool *failedptr = (bool *) failedptrarg;
3833
3.61k
  asection *elt, *first;
3834
3.61k
  unsigned char *loc;
3835
3.61k
  bool gas;
3836
3837
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3838
     elfxx-ia64.c.  */
3839
3.61k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3840
3.61k
      || sec->size == 0
3841
3.61k
      || *failedptr)
3842
3.24k
    return;
3843
3844
376
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3845
376
    {
3846
376
      unsigned long symindx = 0;
3847
3848
      /* elf_group_id will have been set up by objcopy and the
3849
   generic linker.  */
3850
376
      if (elf_group_id (sec) != NULL)
3851
376
  symindx = elf_group_id (sec)->udata.i;
3852
3853
376
      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
376
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3867
376
    }
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
376
  gas = true;
3903
376
  if (sec->contents == NULL)
3904
376
    {
3905
376
      gas = false;
3906
376
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3907
3908
      /* Arrange for the section to be written out.  */
3909
376
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3910
376
      if (sec->contents == NULL)
3911
0
  {
3912
0
    *failedptr = true;
3913
0
    return;
3914
0
  }
3915
376
      sec->alloced = 1;
3916
376
    }
3917
3918
376
  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
376
  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.14k
  while (elt != NULL)
3930
1.14k
    {
3931
1.14k
      asection *s;
3932
3933
1.14k
      s = elt;
3934
1.14k
      if (!gas)
3935
1.14k
  s = s->output_section;
3936
1.14k
      if (s != NULL
3937
1.14k
    && !bfd_is_abs_section (s))
3938
1.14k
  {
3939
1.14k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3940
1.14k
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3941
3942
1.14k
    if (elf_sec->rel.hdr != NULL
3943
1.14k
        && (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.14k
    if (elf_sec->rela.hdr != NULL
3954
1.14k
        && (gas
3955
794
      || (input_elf_sec->rela.hdr != NULL
3956
794
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3957
794
      {
3958
794
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3959
794
        loc -= 4;
3960
794
        if (loc == sec->contents)
3961
0
    break;
3962
794
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3963
794
      }
3964
1.14k
    loc -= 4;
3965
1.14k
    if (loc == sec->contents)
3966
0
      break;
3967
1.14k
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
3968
1.14k
  }
3969
1.14k
      elt = elf_next_in_group (elt);
3970
1.14k
      if (elt == first)
3971
376
  break;
3972
1.14k
    }
3973
3974
  /* We should always get here with loc == sec->contents + 4.  Return
3975
     an error for bogus SHT_GROUP sections.  */
3976
376
  loc -= 4;
3977
376
  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
375
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3988
375
}
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
88
{
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
88
  if (get_elf_backend_data (abfd)->want_got_plt
4000
88
      && strcmp (name, ".plt") == 0)
4001
33
    {
4002
33
      asection *sec;
4003
4004
33
      name = ".got.plt";
4005
33
      sec = bfd_get_section_by_name (abfd, name);
4006
33
      if (sec != NULL)
4007
14
  return sec;
4008
19
      name = ".got";
4009
19
    }
4010
4011
74
  return bfd_get_section_by_name (abfd, name);
4012
88
}
4013
4014
/* Return the section to which RELOC_SEC applies.  */
4015
4016
static asection *
4017
elf_get_reloc_section (asection *reloc_sec)
4018
94
{
4019
94
  const char *name;
4020
94
  unsigned int type;
4021
94
  bfd *abfd;
4022
94
  const struct elf_backend_data *bed;
4023
4024
94
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4025
94
  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
94
  name = reloc_sec->name;
4030
94
  if (!startswith (name, ".rel"))
4031
6
    return NULL;
4032
88
  name += 4;
4033
88
  if (type == SHT_RELA && *name++ != 'a')
4034
0
    return NULL;
4035
4036
88
  abfd = reloc_sec->owner;
4037
88
  bed = get_elf_backend_data (abfd);
4038
88
  return bed->get_reloc_section (abfd, name);
4039
88
}
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
201
{
4049
201
  struct elf_obj_tdata *t = elf_tdata (abfd);
4050
201
  asection *sec;
4051
201
  unsigned int section_number;
4052
201
  Elf_Internal_Shdr **i_shdrp;
4053
201
  struct bfd_elf_section_data *d;
4054
201
  bool need_symtab;
4055
201
  size_t amt;
4056
4057
201
  section_number = 1;
4058
4059
201
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4060
4061
  /* SHT_GROUP sections are in relocatable files only.  */
4062
201
  if (link_info == NULL || !link_info->resolve_section_groups)
4063
201
    {
4064
201
      size_t reloc_count = 0;
4065
4066
      /* Put SHT_GROUP sections first.  */
4067
3.81k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4068
3.61k
  {
4069
3.61k
    d = elf_section_data (sec);
4070
4071
3.61k
    if (d->this_hdr.sh_type == SHT_GROUP)
4072
376
      {
4073
376
        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
376
        else
4080
376
    d->this_idx = section_number++;
4081
376
      }
4082
4083
    /* Count relocations.  */
4084
3.61k
    reloc_count += sec->reloc_count;
4085
3.61k
  }
4086
4087
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4088
201
      if (reloc_count == 0)
4089
156
  abfd->flags &= ~HAS_RELOC;
4090
45
      else
4091
45
  abfd->flags |= HAS_RELOC;
4092
201
    }
4093
4094
3.81k
  for (sec = abfd->sections; sec; sec = sec->next)
4095
3.61k
    {
4096
3.61k
      d = elf_section_data (sec);
4097
4098
3.61k
      if (d->this_hdr.sh_type != SHT_GROUP)
4099
3.24k
  d->this_idx = section_number++;
4100
3.61k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4101
3.61k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4102
3.61k
      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
3.61k
      else
4109
3.61k
  d->rel.idx = 0;
4110
4111
3.61k
      if (d->rela.hdr)
4112
1.30k
  {
4113
1.30k
    d->rela.idx = section_number++;
4114
1.30k
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4115
1.30k
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4116
1.30k
  }
4117
2.31k
      else
4118
2.31k
  d->rela.idx = 0;
4119
3.61k
    }
4120
4121
201
  need_symtab = (bfd_get_symcount (abfd) > 0
4122
201
     || (link_info == NULL
4123
118
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4124
118
       == HAS_RELOC)));
4125
201
  if (need_symtab)
4126
83
    {
4127
83
      elf_onesymtab (abfd) = section_number++;
4128
83
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4129
83
      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
83
      elf_strtab_sec (abfd) = section_number++;
4145
83
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4146
83
    }
4147
4148
201
  elf_shstrtab_sec (abfd) = section_number++;
4149
201
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4150
201
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4151
4152
201
  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
201
  elf_numsections (abfd) = section_number;
4161
201
  elf_elfheader (abfd)->e_shnum = section_number;
4162
4163
  /* Set up the list of section header pointers, in agreement with the
4164
     indices.  */
4165
201
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4166
201
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4167
201
  if (i_shdrp == NULL)
4168
0
    return false;
4169
4170
201
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4171
201
             sizeof (Elf_Internal_Shdr));
4172
201
  if (i_shdrp[0] == NULL)
4173
0
    {
4174
0
      bfd_release (abfd, i_shdrp);
4175
0
      return false;
4176
0
    }
4177
4178
201
  elf_elfsections (abfd) = i_shdrp;
4179
4180
201
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4181
201
  if (need_symtab)
4182
83
    {
4183
83
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4184
83
      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
83
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4192
83
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4193
83
    }
4194
4195
3.81k
  for (sec = abfd->sections; sec; sec = sec->next)
4196
3.61k
    {
4197
3.61k
      asection *s;
4198
4199
3.61k
      d = elf_section_data (sec);
4200
4201
3.61k
      i_shdrp[d->this_idx] = &d->this_hdr;
4202
3.61k
      if (d->rel.idx != 0)
4203
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4204
3.61k
      if (d->rela.idx != 0)
4205
1.30k
  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
3.61k
      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
3.61k
      if (d->rela.idx != 0)
4219
1.30k
  {
4220
1.30k
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4221
1.30k
    d->rela.hdr->sh_info = d->this_idx;
4222
1.30k
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4223
1.30k
  }
4224
4225
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4226
3.61k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4227
4
  {
4228
4
    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
4
    if (s)
4234
3
      {
4235
        /* Check discarded linkonce section.  */
4236
3
        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
3
        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
3
        s = s->output_section;
4266
3
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4267
3
      }
4268
4
  }
4269
4270
3.61k
      switch (d->this_hdr.sh_type)
4271
3.61k
  {
4272
58
  case SHT_REL:
4273
94
  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
94
    if (d->this_hdr.sh_link == 0)
4278
93
      {
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
93
        if ((sec->flags & SEC_ALLOC) != 0)
4283
92
    {
4284
92
      s = bfd_get_section_by_name (abfd, ".dynsym");
4285
92
      if (s != NULL)
4286
86
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4287
92
    }
4288
1
        else
4289
1
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4290
93
      }
4291
4292
94
    s = elf_get_reloc_section (sec);
4293
94
    if (s != NULL)
4294
54
      {
4295
54
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4296
54
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4297
54
      }
4298
94
    break;
4299
4300
42
  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
42
    if (startswith (sec->name, ".stab")
4306
42
        && 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
42
    break;
4328
4329
43
  case SHT_DYNAMIC:
4330
85
  case SHT_DYNSYM:
4331
127
  case SHT_GNU_verneed:
4332
127
  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
127
    s = bfd_get_section_by_name (abfd, ".dynstr");
4337
127
    if (s != NULL)
4338
117
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4339
127
    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
37
  case SHT_HASH:
4352
56
  case SHT_GNU_HASH:
4353
89
  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
89
    s = bfd_get_section_by_name (abfd, ".dynsym");
4357
89
    if (s != NULL)
4358
88
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4359
89
    break;
4360
4361
376
  case SHT_GROUP:
4362
376
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4363
3.61k
  }
4364
3.61k
    }
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
201
  return true;
4371
201
}
4372
4373
static bool
4374
sym_is_global (bfd *abfd, asymbol *sym)
4375
65.2k
{
4376
  /* If the backend has a special mapping, use it.  */
4377
65.2k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4378
65.2k
  if (bed->elf_backend_sym_is_global)
4379
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4380
4381
65.2k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4382
65.2k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4383
65.2k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4384
65.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
75.4k
{
4430
75.4k
  if (sym == NULL)
4431
0
    return false;
4432
4433
75.4k
  if (sym->section == NULL)
4434
0
    return true;
4435
4436
75.4k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4437
13.8k
    {
4438
13.8k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4439
8.18k
  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.68k
      if (sym->section->output_offset != 0)
4446
0
  return true;
4447
5.68k
    }
4448
4449
67.2k
  return discarded_section (sym->section);
4450
75.4k
}
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
83
{
4458
83
  unsigned int symcount = bfd_get_symcount (abfd);
4459
83
  asymbol **syms = bfd_get_outsymbols (abfd);
4460
83
  asymbol **sect_syms;
4461
83
  unsigned int num_locals = 0;
4462
83
  unsigned int num_globals = 0;
4463
83
  unsigned int max_index = 0;
4464
83
  unsigned int idx;
4465
83
  asection *asect;
4466
83
  asymbol **new_syms;
4467
83
  size_t amt;
4468
4469
#ifdef DEBUG
4470
  fprintf (stderr, "elf_map_symbols\n");
4471
  fflush (stderr);
4472
#endif
4473
4474
3.55k
  for (asect = abfd->sections; asect; asect = asect->next)
4475
3.47k
    {
4476
3.47k
      if (max_index < asect->index)
4477
3.39k
  max_index = asect->index;
4478
3.47k
    }
4479
4480
83
  max_index++;
4481
83
  amt = max_index * sizeof (asymbol *);
4482
83
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4483
83
  if (sect_syms == NULL)
4484
0
    return false;
4485
83
  elf_section_syms (abfd) = sect_syms;
4486
83
  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
33.1k
  for (idx = 0; idx < symcount; idx++)
4491
33.0k
    {
4492
33.0k
      asymbol *sym = syms[idx];
4493
4494
33.0k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4495
33.0k
    && sym->value == 0
4496
33.0k
    && !ignore_sym (sym)
4497
33.0k
    && !bfd_is_abs_section (sym->section))
4498
1.65k
  {
4499
1.65k
    asection *sec = sym->section;
4500
4501
1.65k
    if (sec->owner != abfd)
4502
1.65k
      {
4503
1.65k
        sec = sec->output_section;
4504
1.65k
        if (sec == NULL)
4505
0
    return false;
4506
1.65k
      }
4507
4508
1.65k
    sect_syms[sec->index] = syms[idx];
4509
1.65k
  }
4510
33.0k
    }
4511
4512
  /* Classify all of the symbols.  */
4513
33.1k
  for (idx = 0; idx < symcount; idx++)
4514
33.0k
    {
4515
33.0k
      if (ignore_sym (syms[idx]))
4516
613
  continue;
4517
32.4k
      if (sym_is_global (abfd, syms[idx]))
4518
11.5k
  num_globals++;
4519
20.9k
      else
4520
20.9k
  num_locals++;
4521
32.4k
    }
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
3.55k
  for (asect = abfd->sections; asect; asect = asect->next)
4528
3.47k
    {
4529
3.47k
      asymbol *sym = asect->symbol;
4530
      /* Don't include ignored section symbols.  */
4531
3.47k
      if (!ignore_sym (sym)
4532
3.47k
    && sect_syms[asect->index] == NULL)
4533
135
  {
4534
135
    if (sym_is_global (abfd, asect->symbol))
4535
0
      num_globals++;
4536
135
    else
4537
135
      num_locals++;
4538
135
  }
4539
3.47k
    }
4540
4541
  /* Now sort the symbols so the local symbols are first.  */
4542
83
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4543
83
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4544
83
  if (new_syms == NULL)
4545
0
    return false;
4546
4547
83
  unsigned int num_globals2 = 0;
4548
83
  unsigned int num_locals2 = 0;
4549
33.1k
  for (idx = 0; idx < symcount; idx++)
4550
33.0k
    {
4551
33.0k
      asymbol *sym = syms[idx];
4552
33.0k
      unsigned int i;
4553
4554
33.0k
      if (ignore_sym (sym))
4555
613
  continue;
4556
4557
32.4k
      if (sym_is_global (abfd, sym))
4558
11.5k
  i = num_locals + num_globals2++;
4559
20.9k
      else
4560
20.9k
  i = num_locals2++;
4561
32.4k
      new_syms[i] = sym;
4562
32.4k
      sym->udata.i = i + 1;
4563
32.4k
    }
4564
3.55k
  for (asect = abfd->sections; asect; asect = asect->next)
4565
3.47k
    {
4566
3.47k
      asymbol *sym = asect->symbol;
4567
3.47k
      if (!ignore_sym (sym)
4568
3.47k
    && sect_syms[asect->index] == NULL)
4569
135
  {
4570
135
    unsigned int i;
4571
4572
135
    sect_syms[asect->index] = sym;
4573
135
    if (sym_is_global (abfd, sym))
4574
0
      i = num_locals + num_globals2++;
4575
135
    else
4576
135
      i = num_locals2++;
4577
135
    new_syms[i] = sym;
4578
135
    sym->udata.i = i + 1;
4579
135
  }
4580
3.47k
    }
4581
4582
83
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4583
4584
83
  *pnum_locals = num_locals;
4585
83
  return true;
4586
83
}
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
4.40k
{
4597
4.40k
  if (i_shdrp->sh_addralign > 1)
4598
3.26k
    {
4599
3.26k
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4600
4601
3.26k
      if (align)
4602
282
  offset = BFD_ALIGN (offset, salign);
4603
2.97k
      else if (log_file_align)
4604
2.97k
  {
4605
    /* Heuristic: Cap alignment at log_file_align.  */
4606
2.97k
    file_ptr falign = 1u << log_file_align;
4607
4608
2.97k
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4609
2.97k
  }
4610
3.26k
    }
4611
4.40k
  i_shdrp->sh_offset = offset;
4612
4.40k
  if (i_shdrp->bfd_section != NULL)
4613
2.59k
    i_shdrp->bfd_section->filepos = offset;
4614
4.40k
  if (i_shdrp->sh_type != SHT_NOBITS)
4615
4.37k
    offset += i_shdrp->sh_size;
4616
4.40k
  return offset;
4617
4.40k
}
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
201
{
4627
201
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4628
201
  struct fake_section_arg fsargs;
4629
201
  bool failed;
4630
201
  struct elf_strtab_hash *strtab = NULL;
4631
201
  Elf_Internal_Shdr *shstrtab_hdr;
4632
201
  bool need_symtab;
4633
4634
201
  if (abfd->output_has_begun)
4635
0
    return true;
4636
4637
  /* Do any elf backend specific processing first.  */
4638
201
  if (bed->elf_backend_begin_write_processing)
4639
3
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4640
4641
201
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4642
0
    return false;
4643
4644
201
  fsargs.failed = false;
4645
201
  fsargs.link_info = link_info;
4646
201
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4647
201
  if (fsargs.failed)
4648
0
    return false;
4649
4650
201
  if (!assign_section_numbers (abfd, link_info))
4651
0
    return false;
4652
4653
  /* The backend linker builds symbol table information itself.  */
4654
201
  need_symtab = (link_info == NULL
4655
201
     && (bfd_get_symcount (abfd) > 0
4656
201
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4657
118
       == HAS_RELOC)));
4658
201
  if (need_symtab)
4659
83
    {
4660
      /* Non-zero if doing a relocatable link.  */
4661
83
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4662
4663
83
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4664
0
  return false;
4665
83
    }
4666
4667
201
  failed = false;
4668
201
  if (link_info == NULL)
4669
201
    {
4670
201
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4671
201
      if (failed)
4672
1
  goto err_free_strtab;
4673
201
    }
4674
4675
200
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4676
  /* sh_name was set in init_file_header.  */
4677
200
  shstrtab_hdr->sh_type = SHT_STRTAB;
4678
200
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4679
200
  shstrtab_hdr->sh_addr = 0;
4680
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4681
200
  shstrtab_hdr->sh_entsize = 0;
4682
200
  shstrtab_hdr->sh_link = 0;
4683
200
  shstrtab_hdr->sh_info = 0;
4684
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4685
200
  shstrtab_hdr->sh_addralign = 1;
4686
4687
200
  if (!assign_file_positions_except_relocs (abfd, link_info))
4688
3
    goto err_free_strtab;
4689
4690
197
  if (strtab != NULL)
4691
81
    {
4692
81
      file_ptr off;
4693
81
      Elf_Internal_Shdr *hdr;
4694
4695
81
      off = elf_next_file_pos (abfd);
4696
4697
81
      hdr = & elf_symtab_hdr (abfd);
4698
81
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4699
4700
81
      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
81
      hdr = &elf_tdata (abfd)->strtab_hdr;
4709
81
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4710
4711
81
      elf_next_file_pos (abfd) = off;
4712
4713
      /* Now that we know where the .strtab section goes, write it
4714
   out.  */
4715
81
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4716
81
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4717
0
  goto err_free_strtab;
4718
81
      _bfd_elf_strtab_free (strtab);
4719
81
    }
4720
4721
197
  abfd->output_has_begun = true;
4722
197
  return true;
4723
4724
4
 err_free_strtab:
4725
4
  if (strtab != NULL)
4726
2
    _bfd_elf_strtab_free (strtab);
4727
4
  return false;
4728
197
}
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
4
{
4739
4
  if (info != NULL && is_elf_hash_table (info->hash))
4740
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4741
4
  return NULL;
4742
4
}
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
2
{
4750
2
  size_t segs;
4751
2
  asection *s;
4752
2
  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
2
  segs = 2;
4757
4758
2
  s = bfd_get_section_by_name (abfd, ".interp");
4759
2
  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
2
  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
2
  if (info != NULL && info->relro)
4775
0
    {
4776
      /* We need a PT_GNU_RELRO segment.  */
4777
0
      ++segs;
4778
0
    }
4779
4780
2
  if (elf_eh_frame_hdr (info))
4781
0
    {
4782
      /* We need a PT_GNU_EH_FRAME segment.  */
4783
0
      ++segs;
4784
0
    }
4785
4786
2
  if (elf_stack_flags (abfd))
4787
0
    {
4788
      /* We need a PT_GNU_STACK segment.  */
4789
0
      ++segs;
4790
0
    }
4791
4792
2
  if (elf_sframe (abfd))
4793
0
    {
4794
      /* We need a PT_GNU_SFRAME segment.  */
4795
0
      ++segs;
4796
0
    }
4797
4798
2
  s = bfd_get_section_by_name (abfd,
4799
2
             NOTE_GNU_PROPERTY_SECTION_NAME);
4800
2
  if (s != NULL && s->size != 0)
4801
0
    {
4802
      /* We need a PT_GNU_PROPERTY segment.  */
4803
0
      ++segs;
4804
0
    }
4805
4806
31
  for (s = abfd->sections; s != NULL; s = s->next)
4807
29
    {
4808
29
      if ((s->flags & SEC_LOAD) != 0
4809
29
    && 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
29
    }
4827
4828
23
  for (s = abfd->sections; s != NULL; s = s->next)
4829
22
    {
4830
22
      if (s->flags & SEC_THREAD_LOCAL)
4831
1
  {
4832
    /* We need a PT_TLS segment.  */
4833
1
    ++segs;
4834
1
    break;
4835
1
  }
4836
22
    }
4837
4838
2
  bed = get_elf_backend_data (abfd);
4839
4840
2
  if ((abfd->flags & D_PAGED) != 0
4841
2
      && (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
2
  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
2
  return segs * bed->s->sizeof_phdr;
4883
2
}
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
19
{
4916
19
  struct elf_segment_map *m;
4917
19
  unsigned int i;
4918
19
  asection **hdrpp;
4919
19
  size_t amt;
4920
4921
19
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4922
19
  amt += (to - from) * sizeof (asection *);
4923
19
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4924
19
  if (m == NULL)
4925
0
    return NULL;
4926
19
  m->next = NULL;
4927
19
  m->p_type = PT_LOAD;
4928
38
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4929
19
    m->sections[i - from] = *hdrpp;
4930
19
  m->count = to - from;
4931
4932
19
  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
19
  return m;
4940
19
}
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
86
{
4969
86
  struct elf_segment_map **m;
4970
86
  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
86
  m = &elf_seg_map (abfd);
4978
346
  while (*m)
4979
260
    {
4980
260
      unsigned int i, new_count;
4981
4982
1.29k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
4983
1.03k
  {
4984
1.03k
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4985
1.03k
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4986
1.03k
      || (*m)->p_type != PT_LOAD))
4987
1.03k
      {
4988
1.03k
        (*m)->sections[new_count] = (*m)->sections[i];
4989
1.03k
        new_count++;
4990
1.03k
      }
4991
1.03k
  }
4992
260
      (*m)->count = new_count;
4993
4994
260
      if (remove_empty_load
4995
260
    && (*m)->p_type == PT_LOAD
4996
260
    && (*m)->count == 0
4997
260
    && !(*m)->includes_phdrs)
4998
1
  *m = (*m)->next;
4999
259
      else
5000
259
  m = &(*m)->next;
5001
260
    }
5002
5003
86
  bed = get_elf_backend_data (abfd);
5004
86
  if (bed->elf_backend_modify_segment_map != NULL)
5005
5
    {
5006
5
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5007
0
  return false;
5008
5
    }
5009
5010
86
  return true;
5011
86
}
5012
5013
#define IS_TBSS(s) \
5014
31
  ((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
86
{
5024
86
  unsigned int count;
5025
86
  struct elf_segment_map *m;
5026
86
  asection **sections = NULL;
5027
86
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5028
86
  bool no_user_phdrs;
5029
5030
86
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5031
5032
86
  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
86
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5046
2
    {
5047
2
      asection *s;
5048
2
      unsigned int i;
5049
2
      struct elf_segment_map *mfirst;
5050
2
      struct elf_segment_map **pm;
5051
2
      asection *last_hdr;
5052
2
      bfd_vma last_size;
5053
2
      unsigned int hdr_index;
5054
2
      bfd_vma maxpagesize;
5055
2
      asection **hdrpp;
5056
2
      bool phdr_in_segment;
5057
2
      bool writable;
5058
2
      bool executable;
5059
2
      unsigned int tls_count = 0;
5060
2
      asection *first_tls = NULL;
5061
2
      asection *first_mbind = NULL;
5062
2
      asection *dynsec, *eh_frame_hdr;
5063
2
      asection *sframe;
5064
2
      size_t amt;
5065
2
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5066
2
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5067
2
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5068
5069
      /* Select the allocated sections, and sort them.  */
5070
5071
2
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5072
2
      sections = (asection **) bfd_malloc (amt);
5073
2
      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
2
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5080
2
      addr_mask = (addr_mask << 1) + 1;
5081
5082
2
      i = 0;
5083
31
      for (s = abfd->sections; s != NULL; s = s->next)
5084
29
  {
5085
29
    if ((s->flags & SEC_ALLOC) != 0)
5086
19
      {
5087
        /* target_index is unused until bfd_elf_final_link
5088
     starts output of section symbols.  Use it to make
5089
     qsort stable.  */
5090
19
        s->target_index = i;
5091
19
        sections[i] = s;
5092
19
        ++i;
5093
        /* A wrapping section potentially clashes with header.  */
5094
19
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5095
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5096
19
      }
5097
29
  }
5098
2
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5099
2
      count = i;
5100
5101
2
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5102
5103
2
      phdr_size = elf_program_header_size (abfd);
5104
2
      if (phdr_size == (bfd_size_type) -1)
5105
2
  phdr_size = get_program_header_size (abfd, info);
5106
2
      phdr_size += bed->s->sizeof_ehdr;
5107
      /* phdr_size is compared to LMA values which are in bytes.  */
5108
2
      phdr_size /= opb;
5109
2
      if (info != NULL)
5110
0
  maxpagesize = info->maxpagesize;
5111
2
      else
5112
2
  maxpagesize = bed->maxpagesize;
5113
2
      if (maxpagesize == 0)
5114
0
  maxpagesize = 1;
5115
2
      phdr_in_segment = info != NULL && info->load_phdrs;
5116
2
      if (count != 0
5117
2
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5118
2
        >= (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
2
      mfirst = NULL;
5126
2
      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
2
      s = bfd_get_section_by_name (abfd, ".interp");
5132
2
      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
2
      last_hdr = NULL;
5164
2
      last_size = 0;
5165
2
      hdr_index = 0;
5166
2
      writable = false;
5167
2
      executable = false;
5168
2
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5169
2
      if (dynsec != NULL
5170
2
    && (dynsec->flags & SEC_LOAD) == 0)
5171
0
  dynsec = NULL;
5172
5173
2
      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
2
      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
21
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5235
19
  {
5236
19
    asection *hdr;
5237
19
    bool new_segment;
5238
5239
19
    hdr = *hdrpp;
5240
5241
    /* See if this section and the last one will fit in the same
5242
       segment.  */
5243
5244
19
    if (last_hdr == NULL)
5245
2
      {
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
2
        new_segment = false;
5249
2
      }
5250
17
    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
17
    else if (hdr->lma < last_hdr->lma + last_size
5258
17
       || last_hdr->lma + last_size < last_hdr->lma)
5259
16
      {
5260
        /* If this section has a load address that makes it overlap
5261
     the previous section, then we need a new segment.  */
5262
16
        new_segment = true;
5263
16
      }
5264
1
    else if ((abfd->flags & D_PAGED) != 0
5265
1
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5266
1
           == (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
1
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5278
1
        + maxpagesize > last_hdr->lma)
5279
1
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5280
1
           + 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
1
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5287
1
       && (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
1
    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
1
    else if (info != NULL
5303
1
       && info->separate_code
5304
1
       && executable != ((hdr->flags & SEC_CODE) != 0))
5305
0
      {
5306
0
        new_segment = true;
5307
0
      }
5308
1
    else if (! writable
5309
1
       && (hdr->flags & SEC_READONLY) == 0)
5310
1
      {
5311
        /* We don't want to put a writable section in a read only
5312
     segment.  */
5313
1
        new_segment = true;
5314
1
      }
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
19
    if (last_hdr != NULL
5323
19
        && info != NULL
5324
19
        && 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
19
    if (! new_segment)
5331
2
      {
5332
2
        if ((hdr->flags & SEC_READONLY) == 0)
5333
1
    writable = true;
5334
2
        if ((hdr->flags & SEC_CODE) != 0)
5335
1
    executable = true;
5336
2
        last_hdr = hdr;
5337
        /* .tbss sections effectively have zero size.  */
5338
2
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5339
2
        continue;
5340
2
      }
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
17
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5346
17
    if (m == NULL)
5347
0
      goto error_return;
5348
5349
17
    *pm = m;
5350
17
    pm = &m->next;
5351
5352
17
    if ((hdr->flags & SEC_READONLY) == 0)
5353
7
      writable = true;
5354
10
    else
5355
10
      writable = false;
5356
5357
17
    if ((hdr->flags & SEC_CODE) == 0)
5358
11
      executable = false;
5359
6
    else
5360
6
      executable = true;
5361
5362
17
    last_hdr = hdr;
5363
    /* .tbss sections effectively have zero size.  */
5364
17
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5365
17
    hdr_index = i;
5366
17
    phdr_in_segment = false;
5367
17
  }
5368
5369
      /* Create a final PT_LOAD program segment, but not if it's just
5370
   for .tbss.  */
5371
2
      if (last_hdr != NULL
5372
2
    && (i - hdr_index != 1
5373
2
        || !IS_TBSS (last_hdr)))
5374
2
  {
5375
2
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5376
2
    if (m == NULL)
5377
0
      goto error_return;
5378
5379
2
    *pm = m;
5380
2
    pm = &m->next;
5381
2
  }
5382
5383
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5384
2
      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
31
      for (s = abfd->sections; s != NULL; s = s->next)
5399
29
  {
5400
29
    if ((s->flags & SEC_LOAD) != 0
5401
29
        && 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
29
    if (s->flags & SEC_THREAD_LOCAL)
5439
1
      {
5440
1
        if (! tls_count)
5441
1
    first_tls = s;
5442
1
        tls_count++;
5443
1
      }
5444
29
    if (first_mbind == NULL
5445
29
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5446
1
      first_mbind = s;
5447
29
  }
5448
5449
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5450
2
      if (tls_count > 0)
5451
1
  {
5452
1
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5453
1
    amt += tls_count * sizeof (asection *);
5454
1
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5455
1
    if (m == NULL)
5456
0
      goto error_return;
5457
1
    m->next = NULL;
5458
1
    m->p_type = PT_TLS;
5459
1
    m->count = tls_count;
5460
    /* Mandated PF_R.  */
5461
1
    m->p_flags = PF_R;
5462
1
    m->p_flags_valid = 1;
5463
1
    s = first_tls;
5464
2
    for (i = 0; i < tls_count; ++i)
5465
1
      {
5466
1
        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
1
        m->sections[i] = s;
5487
1
        s = s->next;
5488
1
      }
5489
5490
1
    *pm = m;
5491
1
    pm = &m->next;
5492
1
  }
5493
5494
2
      if (first_mbind
5495
2
    && (abfd->flags & D_PAGED) != 0
5496
2
    && (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
2
      s = bfd_get_section_by_name (abfd,
5525
2
           NOTE_GNU_PROPERTY_SECTION_NAME);
5526
2
      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
2
      eh_frame_hdr = elf_eh_frame_hdr (info);
5545
2
      if (eh_frame_hdr != NULL
5546
2
    && (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
2
      sframe = elf_sframe (abfd);
5564
2
      if (sframe != NULL
5565
2
    && (sframe->output_section->flags & SEC_LOAD) != 0
5566
2
    && 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
2
      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
2
      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
2
      free (sections);
5641
2
      elf_seg_map (abfd) = mfirst;
5642
2
    }
5643
5644
86
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5645
0
    return false;
5646
5647
345
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5648
259
    ++count;
5649
86
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5650
5651
86
  return true;
5652
5653
0
 error_return:
5654
0
  free (sections);
5655
0
  return false;
5656
86
}
5657
5658
/* Sort sections by address.  */
5659
5660
static int
5661
elf_sort_sections (const void *arg1, const void *arg2)
5662
1.53k
{
5663
1.53k
  const asection *sec1 = *(const asection **) arg1;
5664
1.53k
  const asection *sec2 = *(const asection **) arg2;
5665
1.53k
  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
1.53k
  if (sec1->lma < sec2->lma)
5670
1.48k
    return -1;
5671
53
  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
53
  if (sec1->vma < sec2->vma)
5677
0
    return -1;
5678
53
  else if (sec1->vma > sec2->vma)
5679
0
    return 1;
5680
5681
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5682
5683
106
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5684
106
      && (x)->size != 0)
5685
5686
53
  if (TOEND (sec1))
5687
0
    {
5688
0
      if (!TOEND (sec2))
5689
0
  return 1;
5690
0
    }
5691
53
  else if (TOEND (sec2))
5692
0
    return -1;
5693
5694
53
#undef TOEND
5695
5696
  /* Sort by size, to put zero sized sections
5697
     before others at the same address.  */
5698
5699
53
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5700
53
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5701
5702
53
  if (size1 < size2)
5703
23
    return -1;
5704
30
  if (size1 > size2)
5705
27
    return 1;
5706
5707
3
  return sec1->target_index - sec2->target_index;
5708
30
}
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
359
{
5716
359
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5717
359
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5718
5719
359
  if (m1->p_type != m2->p_type)
5720
280
    {
5721
280
      if (m1->p_type == PT_NULL)
5722
0
  return 1;
5723
280
      if (m2->p_type == PT_NULL)
5724
0
  return -1;
5725
280
      return m1->p_type < m2->p_type ? -1 : 1;
5726
280
    }
5727
79
  if (m1->includes_filehdr != m2->includes_filehdr)
5728
41
    return m1->includes_filehdr ? -1 : 1;
5729
38
  if (m1->no_sort_lma != m2->no_sort_lma)
5730
0
    return m1->no_sort_lma ? -1 : 1;
5731
38
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5732
38
    {
5733
38
      bfd_vma lma1, lma2;  /* Octets.  */
5734
38
      lma1 = 0;
5735
38
      if (m1->p_paddr_valid)
5736
4
  lma1 = m1->p_paddr;
5737
34
      else if (m1->count != 0)
5738
34
  {
5739
34
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5740
34
              m1->sections[0]);
5741
34
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5742
34
  }
5743
38
      lma2 = 0;
5744
38
      if (m2->p_paddr_valid)
5745
4
  lma2 = m2->p_paddr;
5746
34
      else if (m2->count != 0)
5747
34
  {
5748
34
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5749
34
              m2->sections[0]);
5750
34
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5751
34
  }
5752
38
      if (lma1 != lma2)
5753
4
  return lma1 < lma2 ? -1 : 1;
5754
38
    }
5755
34
  if (m1->idx != m2->idx)
5756
34
    return m1->idx < m2->idx ? -1 : 1;
5757
0
  return 0;
5758
34
}
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
105
{
5786
  /* PR binutils/16199: Handle an alignment of zero.  */
5787
105
  if (maxpagesize == 0)
5788
0
    maxpagesize = 1;
5789
105
  return ((vma - off) % maxpagesize);
5790
105
}
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
86
{
5828
86
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5829
86
  struct elf_segment_map *m;
5830
86
  struct elf_segment_map *phdr_load_seg;
5831
86
  Elf_Internal_Phdr *phdrs;
5832
86
  Elf_Internal_Phdr *p;
5833
86
  file_ptr off;  /* Octets.  */
5834
86
  bfd_size_type maxpagesize;
5835
86
  unsigned int alloc, actual;
5836
86
  unsigned int i, j;
5837
86
  struct elf_segment_map **sorted_seg_map;
5838
86
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5839
5840
86
  if (link_info == NULL
5841
86
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5842
0
    return false;
5843
5844
86
  alloc = 0;
5845
345
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5846
259
    m->idx = alloc++;
5847
5848
86
  if (alloc)
5849
45
    {
5850
45
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5851
45
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5852
45
    }
5853
41
  else
5854
41
    {
5855
      /* PR binutils/12467.  */
5856
41
      elf_elfheader (abfd)->e_phoff = 0;
5857
41
      elf_elfheader (abfd)->e_phentsize = 0;
5858
41
    }
5859
5860
86
  elf_elfheader (abfd)->e_phnum = alloc;
5861
5862
86
  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
86
  else
5868
86
    {
5869
86
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5870
86
      BFD_ASSERT (elf_program_header_size (abfd)
5871
86
      == actual * bed->s->sizeof_phdr);
5872
86
      BFD_ASSERT (actual >= alloc);
5873
86
    }
5874
5875
86
  if (alloc == 0)
5876
41
    {
5877
41
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5878
41
      return true;
5879
41
    }
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
45
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5891
45
           + alloc * sizeof (*sorted_seg_map)));
5892
45
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5893
45
  elf_tdata (abfd)->phdr = phdrs;
5894
45
  if (phdrs == NULL)
5895
0
    return false;
5896
5897
304
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5898
259
    {
5899
259
      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
259
      if (m->count > 1
5906
259
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5907
92
         && m->p_type == PT_NOTE))
5908
92
  {
5909
1.02k
    for (i = 0; i < m->count; i++)
5910
929
      m->sections[i]->target_index = i;
5911
92
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5912
92
     elf_sort_sections);
5913
92
  }
5914
259
    }
5915
45
  if (alloc > 1)
5916
44
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5917
44
     elf_sort_segments);
5918
5919
45
  maxpagesize = 1;
5920
45
  if ((abfd->flags & D_PAGED) != 0)
5921
43
    {
5922
43
      if (link_info != NULL)
5923
0
  maxpagesize = link_info->maxpagesize;
5924
43
      else
5925
43
  maxpagesize = bed->maxpagesize;
5926
43
    }
5927
5928
  /* Sections must map to file offsets past the ELF file header.  */
5929
45
  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
45
  phdr_load_seg = NULL;
5934
66
  for (j = 0; j < alloc; j++)
5935
66
    {
5936
66
      m = sorted_seg_map[j];
5937
66
      if (m->p_type != PT_LOAD)
5938
4
  break;
5939
62
      if (m->includes_phdrs)
5940
41
  {
5941
41
    phdr_load_seg = m;
5942
41
    break;
5943
41
  }
5944
62
    }
5945
45
  if (phdr_load_seg == NULL)
5946
4
    off += actual * bed->s->sizeof_phdr;
5947
5948
295
  for (j = 0; j < alloc; j++)
5949
253
    {
5950
253
      asection **secpp;
5951
253
      bfd_vma off_adjust;  /* Octets.  */
5952
253
      bool no_contents;
5953
253
      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
253
      m = sorted_seg_map[j];
5961
253
      p = phdrs + m->idx;
5962
253
      p->p_type = m->p_type;
5963
253
      p->p_flags = m->p_flags;
5964
5965
253
      if (m->count == 0)
5966
56
  p->p_vaddr = m->p_vaddr_offset * opb;
5967
197
      else
5968
197
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5969
5970
253
      if (m->p_paddr_valid)
5971
185
  p->p_paddr = m->p_paddr;
5972
68
      else if (m->count == 0)
5973
14
  p->p_paddr = 0;
5974
54
      else
5975
54
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5976
5977
253
      align_pagesize = 0;
5978
253
      if (p->p_type == PT_LOAD
5979
253
    && (abfd->flags & D_PAGED) != 0)
5980
101
  {
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
101
    if (m->p_align_valid)
5990
83
      maxpagesize = m->p_align;
5991
18
    else if (bed->p_align != 0
5992
18
       && (link_info == NULL
5993
17
           || !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
17
      align_pagesize = bed->p_align;
6002
6003
101
    p->p_align = maxpagesize;
6004
101
  }
6005
152
      else if (m->p_align_valid)
6006
115
  p->p_align = m->p_align;
6007
37
      else if (m->count == 0)
6008
17
  p->p_align = 1 << bed->s->log_file_align;
6009
6010
253
      if (m == phdr_load_seg)
6011
41
  off += actual * bed->s->sizeof_phdr;
6012
6013
253
      no_contents = false;
6014
253
      off_adjust = 0;
6015
253
      if (p->p_type == PT_LOAD
6016
253
    && m->count > 0)
6017
104
  {
6018
104
    bfd_size_type align;  /* Bytes.  */
6019
104
    unsigned int align_power = 0;
6020
6021
104
    if (m->p_align_valid)
6022
83
      align = p->p_align;
6023
21
    else
6024
21
      {
6025
44
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6026
23
    {
6027
23
      unsigned int secalign;
6028
6029
23
      secalign = bfd_section_alignment (*secpp);
6030
23
      if (secalign > align_power)
6031
18
        align_power = secalign;
6032
23
    }
6033
21
        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
21
        if (align > bed->p_align)
6038
4
    align_pagesize = 0;
6039
21
        if (align < maxpagesize)
6040
18
    align = maxpagesize;
6041
3
        else
6042
3
    {
6043
      /* If a section requires alignment higher than the
6044
         maximum page size, set p_align to the section
6045
         alignment.  */
6046
3
      if ((abfd->flags & D_PAGED) != 0)
6047
0
        p->p_align = align;
6048
3
    }
6049
21
      }
6050
6051
1.02k
    for (i = 0; i < m->count; i++)
6052
924
      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
44
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6057
6058
    /* Find out whether this segment contains any loadable
6059
       sections.  */
6060
104
    no_contents = true;
6061
104
    for (i = 0; i < m->count; i++)
6062
104
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6063
104
        {
6064
104
    no_contents = false;
6065
104
    break;
6066
104
        }
6067
6068
104
    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
104
    if (j != 0
6074
104
        && (abfd->flags & D_PAGED) != 0
6075
104
        && bed->no_page_alias
6076
104
        && (off & (maxpagesize - 1)) != 0
6077
104
        && ((off & -maxpagesize)
6078
0
      == ((off + off_adjust) & -maxpagesize)))
6079
0
      off_adjust += maxpagesize;
6080
104
    off += off_adjust;
6081
104
    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
104
    else
6093
104
      off_adjust = 0;
6094
104
  }
6095
      /* Make sure the .dynamic section is the first section in the
6096
   PT_DYNAMIC segment.  */
6097
149
      else if (p->p_type == PT_DYNAMIC
6098
149
         && m->count > 1
6099
149
         && 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
149
      else if (p->p_type == PT_NOTE)
6110
13
  for (i = 0; i < m->count; i++)
6111
8
    elf_section_type (m->sections[i]) = SHT_NOTE;
6112
6113
253
      if (m->includes_filehdr)
6114
41
  {
6115
41
    if (!m->p_flags_valid)
6116
1
      p->p_flags |= PF_R;
6117
41
    p->p_filesz = bed->s->sizeof_ehdr;
6118
41
    p->p_memsz = bed->s->sizeof_ehdr;
6119
41
    if (p->p_type == PT_LOAD)
6120
41
      {
6121
41
        if (m->count > 0)
6122
41
    {
6123
41
      if (p->p_vaddr < (bfd_vma) off
6124
41
          || (!m->p_paddr_valid
6125
41
        && 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
41
      p->p_vaddr -= off;
6135
41
      if (!m->p_paddr_valid)
6136
10
        p->p_paddr -= off;
6137
41
    }
6138
41
      }
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
41
  }
6147
6148
253
      if (m->includes_phdrs)
6149
82
  {
6150
82
    if (!m->p_flags_valid)
6151
1
      p->p_flags |= PF_R;
6152
82
    p->p_filesz += actual * bed->s->sizeof_phdr;
6153
82
    p->p_memsz += actual * bed->s->sizeof_phdr;
6154
82
    if (!m->includes_filehdr)
6155
41
      {
6156
41
        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
41
        else if (phdr_load_seg != NULL)
6168
39
    {
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
39
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6173
39
      bfd_vma phdr_off = 0;  /* Octets.  */
6174
39
      if (phdr_load_seg->includes_filehdr)
6175
39
        phdr_off = bed->s->sizeof_ehdr;
6176
39
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6177
39
      if (!m->p_paddr_valid)
6178
9
        p->p_paddr = phdr->p_paddr + phdr_off;
6179
39
      p->p_offset = phdr->p_offset + phdr_off;
6180
39
    }
6181
2
        else
6182
2
    p->p_offset = bed->s->sizeof_ehdr;
6183
41
      }
6184
82
  }
6185
6186
253
      if (p->p_type == PT_LOAD
6187
253
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6188
104
  {
6189
104
    if (!m->includes_filehdr && !m->includes_phdrs)
6190
63
      {
6191
63
        p->p_offset = off;
6192
63
        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
63
      }
6208
41
    else
6209
41
      {
6210
41
        file_ptr adjust;  /* Octets.  */
6211
6212
41
        adjust = off - (p->p_offset + p->p_filesz);
6213
41
        if (!no_contents)
6214
41
    p->p_filesz += adjust;
6215
41
        p->p_memsz += adjust;
6216
41
      }
6217
104
  }
6218
6219
253
      if (align_pagesize)
6220
17
  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
1.28k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6228
1.03k
  {
6229
1.03k
    asection *sec;
6230
1.03k
    bfd_size_type align;
6231
1.03k
    Elf_Internal_Shdr *this_hdr;
6232
6233
1.03k
    sec = *secpp;
6234
1.03k
    this_hdr = &elf_section_data (sec)->this_hdr;
6235
1.03k
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6236
6237
1.03k
    if ((p->p_type == PT_LOAD
6238
1.03k
         || p->p_type == PT_TLS)
6239
1.03k
        && (this_hdr->sh_type != SHT_NOBITS
6240
924
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6241
44
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6242
44
        || p->p_type == PT_TLS))))
6243
924
      {
6244
924
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6245
924
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6246
924
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6247
924
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6248
6249
924
        if (adjust != 0
6250
924
      && (s_start < p_end
6251
197
          || 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
924
        p->p_memsz += adjust;
6263
6264
924
        if (p->p_type == PT_LOAD)
6265
924
    {
6266
924
      if (this_hdr->sh_type != SHT_NOBITS)
6267
880
        {
6268
880
          off_adjust = 0;
6269
880
          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
880
        }
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
924
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6291
880
        {
6292
880
          off += adjust;
6293
880
          if (this_hdr->sh_type == SHT_NOBITS)
6294
0
      off_adjust += adjust;
6295
880
        }
6296
924
    }
6297
924
        if (this_hdr->sh_type != SHT_NOBITS)
6298
880
    p->p_filesz += adjust;
6299
924
      }
6300
6301
1.03k
    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
1.03k
    else
6323
1.03k
      {
6324
1.03k
        if (this_hdr->sh_type == SHT_NOBITS
6325
1.03k
      && (this_hdr->sh_flags & SHF_TLS) != 0
6326
1.03k
      && 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
1.03k
        else if (p->p_type == PT_LOAD)
6341
924
    {
6342
924
      this_hdr->sh_offset = sec->filepos = off;
6343
924
      if (this_hdr->sh_type != SHT_NOBITS)
6344
880
        off += this_hdr->sh_size;
6345
924
    }
6346
6347
1.03k
        if (this_hdr->sh_type != SHT_NOBITS)
6348
990
    {
6349
990
      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
990
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6354
990
        p->p_memsz += this_hdr->sh_size;
6355
990
    }
6356
44
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6357
44
    {
6358
44
      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
44
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6364
44
        p->p_memsz += this_hdr->sh_size;
6365
44
    }
6366
6367
1.03k
        if (align > p->p_align
6368
1.03k
      && !m->p_align_valid
6369
1.03k
      && (p->p_type != PT_LOAD
6370
20
          || (abfd->flags & D_PAGED) == 0))
6371
20
    p->p_align = align;
6372
1.03k
      }
6373
6374
1.03k
    if (!m->p_flags_valid)
6375
18
      {
6376
18
        p->p_flags |= PF_R;
6377
18
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6378
6
    p->p_flags |= PF_X;
6379
18
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6380
7
    p->p_flags |= PF_W;
6381
18
      }
6382
1.03k
  }
6383
6384
253
      off -= off_adjust;
6385
6386
      /* PR ld/20815 - Check that the program header segment, if
6387
   present, will be loaded into memory.  */
6388
253
      if (p->p_type == PT_PHDR
6389
253
    && phdr_load_seg == NULL
6390
253
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6391
3
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6392
3
  {
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
3
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6397
3
        " by LOAD segment"),
6398
3
            abfd);
6399
3
    if (link_info == NULL)
6400
3
      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
250
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6409
101
  {
6410
101
    bool check_vma = true;
6411
6412
919
    for (i = 1; i < m->count; i++)
6413
818
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6414
818
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6415
0
               ->this_hdr), p) != 0
6416
818
    && 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
1.02k
    for (i = 0; i < m->count; i++)
6425
919
      {
6426
919
        Elf_Internal_Shdr *this_hdr;
6427
919
        asection *sec;
6428
6429
919
        sec = m->sections[i];
6430
919
        this_hdr = &(elf_section_data(sec)->this_hdr);
6431
919
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6432
919
      && !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
919
      }
6441
101
  }
6442
250
    }
6443
6444
42
  elf_next_file_pos (abfd) = off;
6445
6446
42
  if (link_info != NULL
6447
42
      && phdr_load_seg != NULL
6448
42
      && 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
42
  return true;
6497
45
}
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
1
{
6506
1
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6507
0
    return false;
6508
6509
1
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6510
1
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6511
1
  Elf_Internal_Shdr **headerp;
6512
6513
5
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6514
5
    {
6515
5
      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
5
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6520
5
    && header->sh_type != SHT_NOBITS
6521
5
    && header->sh_type != SHT_NOTE)
6522
1
  return false;
6523
5
    }
6524
6525
0
  return true;
6526
1
}
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
83
{
6535
83
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6536
83
  Elf_Internal_Shdr **i_shdrpp;
6537
83
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6538
83
  Elf_Internal_Phdr *phdrs;
6539
83
  Elf_Internal_Phdr *p;
6540
83
  struct elf_segment_map *m;
6541
83
  file_ptr off;
6542
83
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6543
83
  bfd_vma maxpagesize;
6544
6545
83
  if (link_info != NULL)
6546
0
    maxpagesize = link_info->maxpagesize;
6547
83
  else
6548
83
    maxpagesize = bed->maxpagesize;
6549
83
  i_shdrpp = elf_elfsections (abfd);
6550
83
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6551
83
  off = elf_next_file_pos (abfd);
6552
1.43k
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6553
1.35k
    {
6554
1.35k
      Elf_Internal_Shdr *hdr;
6555
1.35k
      bfd_vma align;
6556
6557
1.35k
      hdr = *hdrpp;
6558
1.35k
      if (hdr->bfd_section != NULL
6559
1.35k
    && (hdr->bfd_section->filepos != 0
6560
1.17k
        || (hdr->sh_type == SHT_NOBITS
6561
266
      && hdr->contents == NULL)))
6562
910
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6563
441
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6564
1
  {
6565
1
    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
1
        && ! is_debuginfo_file (abfd))
6572
1
      _bfd_error_handler
6573
        /* xgettext:c-format */
6574
1
        (_("%pB: warning: allocated section `%s' not in segment"),
6575
1
         abfd,
6576
1
         (hdr->bfd_section == NULL
6577
1
    ? "*unknown*"
6578
1
    : hdr->bfd_section->name));
6579
    /* We don't need to page align empty sections.  */
6580
1
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6581
1
      align = maxpagesize;
6582
0
    else
6583
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6584
1
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6585
1
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6586
1
                 bed->s->log_file_align);
6587
1
  }
6588
440
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6589
440
    && hdr->bfd_section == NULL)
6590
         /* We don't know the offset of these sections yet:
6591
      their size has not been decided.  */
6592
440
         || (abfd->is_linker_output
6593
424
       && hdr->bfd_section != NULL
6594
424
       && (hdr->sh_name == -1u
6595
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6596
440
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6597
440
         || (elf_symtab_shndx_list (abfd) != NULL
6598
386
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6599
440
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6600
440
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6601
175
  hdr->sh_offset = -1;
6602
265
      else
6603
265
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6604
1.35k
    }
6605
83
  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
83
  phdrs = elf_tdata (abfd)->phdr;
6610
323
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6611
240
    {
6612
240
      if (p->p_type == PT_GNU_RELRO)
6613
4
  {
6614
4
    bfd_vma start, end;  /* Bytes.  */
6615
4
    bool ok;
6616
6617
4
    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
4
    else if (m->count != 0)
6626
4
      {
6627
4
        if (!m->p_size_valid)
6628
0
    abort ();
6629
4
        start = m->sections[0]->vma;
6630
4
        end = start + m->p_size / opb;
6631
4
      }
6632
0
    else
6633
0
      {
6634
0
        start = 0;
6635
0
        end = 0;
6636
0
      }
6637
6638
4
    ok = false;
6639
4
    if (start < end)
6640
4
      {
6641
4
        struct elf_segment_map *lm;
6642
4
        const Elf_Internal_Phdr *lp;
6643
4
        unsigned int i;
6644
6645
        /* Find a LOAD segment containing a section in the RELRO
6646
     segment.  */
6647
4
        for (lm = elf_seg_map (abfd), lp = phdrs;
6648
18
       lm != NULL;
6649
14
       lm = lm->next, lp++)
6650
18
    {
6651
18
      if (lp->p_type == PT_LOAD
6652
18
          && lm->count != 0
6653
18
          && (lm->sections[lm->count - 1]->vma
6654
10
        + (!IS_TBSS (lm->sections[lm->count - 1])
6655
10
           ? lm->sections[lm->count - 1]->size / opb
6656
10
           : 0)) > start
6657
18
          && lm->sections[0]->vma < end)
6658
4
        break;
6659
18
    }
6660
6661
4
        if (lm != NULL)
6662
4
    {
6663
      /* Find the section starting the RELRO segment.  */
6664
4
      for (i = 0; i < lm->count; i++)
6665
4
        {
6666
4
          asection *s = lm->sections[i];
6667
4
          if (s->vma >= start
6668
4
        && s->vma < end
6669
4
        && s->size != 0)
6670
4
      break;
6671
4
        }
6672
6673
4
      if (i < lm->count)
6674
4
        {
6675
4
          p->p_vaddr = lm->sections[i]->vma * opb;
6676
4
          p->p_paddr = lm->sections[i]->lma * opb;
6677
4
          p->p_offset = lm->sections[i]->filepos;
6678
4
          p->p_memsz = end * opb - p->p_vaddr;
6679
4
          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
4
          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
4
          if (!m->p_align_valid)
6697
0
      p->p_align = 1;
6698
4
          if (!m->p_flags_valid)
6699
0
      p->p_flags = PF_R;
6700
4
          ok = true;
6701
4
        }
6702
4
    }
6703
4
      }
6704
6705
4
    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
4
  }
6715
236
      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
231
      else if (m->count != 0)
6721
188
  {
6722
188
    unsigned int i;
6723
6724
188
    if (p->p_type != PT_LOAD
6725
188
        && (p->p_type != PT_NOTE
6726
88
      || bfd_get_format (abfd) != bfd_core))
6727
88
      {
6728
        /* A user specified segment layout may include a PHDR
6729
     segment that overlaps with a LOAD segment...  */
6730
88
        if (p->p_type == PT_PHDR)
6731
0
    {
6732
0
      m->count = 0;
6733
0
      continue;
6734
0
    }
6735
6736
88
        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
88
        p->p_filesz = 0;
6747
88
        p->p_offset = m->sections[0]->filepos;
6748
88
        for (i = m->count; i-- != 0;)
6749
88
    {
6750
88
      asection *sect = m->sections[i];
6751
88
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6752
88
      if (hdr->sh_type != SHT_NOBITS)
6753
88
        {
6754
88
          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
88
          if (p->p_type == PT_NOTE
6758
88
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6759
5
      p->p_memsz = p->p_filesz;
6760
88
          break;
6761
88
        }
6762
88
    }
6763
88
      }
6764
188
  }
6765
240
    }
6766
6767
83
  return true;
6768
83
}
6769
6770
static elf_section_list *
6771
find_section_in_list (unsigned int i, elf_section_list * list)
6772
7.06k
{
6773
7.06k
  for (;list != NULL; list = list->next)
6774
3
    if (list->ndx == i)
6775
0
      break;
6776
7.06k
  return list;
6777
7.06k
}
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
200
{
6798
200
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6799
200
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6800
200
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6801
200
  unsigned int alloc;
6802
6803
200
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6804
200
      && bfd_get_format (abfd) != bfd_core)
6805
114
    {
6806
114
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6807
114
      unsigned int num_sec = elf_numsections (abfd);
6808
114
      Elf_Internal_Shdr **hdrpp;
6809
114
      unsigned int i;
6810
114
      file_ptr off;
6811
6812
      /* Start after the ELF header.  */
6813
114
      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.88k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6819
3.76k
  {
6820
3.76k
    Elf_Internal_Shdr *hdr;
6821
6822
3.76k
    hdr = *hdrpp;
6823
3.76k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6824
3.76k
         && hdr->bfd_section == NULL)
6825
        /* Do not assign offsets for these sections yet: we don't know
6826
     their sizes.  */
6827
3.76k
        || (abfd->is_linker_output
6828
2.52k
      && hdr->bfd_section != NULL
6829
2.52k
      && (hdr->sh_name == -1u
6830
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6831
3.76k
        || i == elf_onesymtab (abfd)
6832
3.76k
        || (elf_symtab_shndx_list (abfd) != NULL
6833
2.48k
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6834
3.76k
        || i == elf_strtab_sec (abfd)
6835
3.76k
        || i == elf_shstrtab_sec (abfd))
6836
1.44k
      {
6837
1.44k
        hdr->sh_offset = -1;
6838
1.44k
      }
6839
2.32k
    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.32k
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6845
2.32k
                   bed->s->log_file_align);
6846
3.76k
  }
6847
6848
114
      elf_next_file_pos (abfd) = off;
6849
114
      elf_program_header_size (abfd) = 0;
6850
114
    }
6851
86
  else
6852
86
    {
6853
      /* Assign file positions for the loaded sections based on the
6854
   assignment of sections to segments.  */
6855
86
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6856
3
  return false;
6857
6858
      /* And for non-load sections.  */
6859
83
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6860
0
  return false;
6861
83
    }
6862
6863
197
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6864
0
    return false;
6865
6866
  /* Write out the program headers.  */
6867
197
  alloc = i_ehdrp->e_phnum;
6868
197
  if (alloc != 0)
6869
42
    {
6870
42
      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
42
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6932
42
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6933
0
  return false;
6934
42
    }
6935
6936
197
  return true;
6937
197
}
6938
6939
bool
6940
_bfd_elf_init_file_header (bfd *abfd,
6941
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6942
201
{
6943
201
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
6944
201
  struct elf_strtab_hash *shstrtab;
6945
201
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6946
6947
201
  i_ehdrp = elf_elfheader (abfd);
6948
6949
201
  shstrtab = _bfd_elf_strtab_init ();
6950
201
  if (shstrtab == NULL)
6951
0
    return false;
6952
6953
201
  elf_shstrtab (abfd) = shstrtab;
6954
6955
201
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6956
201
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6957
201
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6958
201
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6959
6960
201
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6961
201
  i_ehdrp->e_ident[EI_DATA] =
6962
201
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6963
201
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6964
6965
201
  if ((abfd->flags & DYNAMIC) != 0)
6966
29
    i_ehdrp->e_type = ET_DYN;
6967
172
  else if ((abfd->flags & EXEC_P) != 0)
6968
52
    i_ehdrp->e_type = ET_EXEC;
6969
120
  else if (bfd_get_format (abfd) == bfd_core)
6970
5
    i_ehdrp->e_type = ET_CORE;
6971
115
  else
6972
115
    i_ehdrp->e_type = ET_REL;
6973
6974
201
  switch (bfd_get_arch (abfd))
6975
201
    {
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
201
    default:
6989
201
      i_ehdrp->e_machine = bed->elf_machine_code;
6990
201
    }
6991
6992
201
  i_ehdrp->e_version = bed->s->ev_current;
6993
201
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6994
6995
  /* No program header, for now.  */
6996
201
  i_ehdrp->e_phoff = 0;
6997
201
  i_ehdrp->e_phentsize = 0;
6998
201
  i_ehdrp->e_phnum = 0;
6999
7000
  /* Each bfd section is section header entry.  */
7001
201
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7002
201
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7003
7004
201
  elf_tdata (abfd)->symtab_hdr.sh_name =
7005
201
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7006
201
  elf_tdata (abfd)->strtab_hdr.sh_name =
7007
201
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7008
201
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7009
201
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7010
201
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7011
201
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7012
201
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7013
0
    return false;
7014
7015
201
  return true;
7016
201
}
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
197
{
7028
197
  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
197
  return true;
7048
197
}
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
197
{
7056
197
  file_ptr off;
7057
197
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7058
197
  Elf_Internal_Shdr *shdrp;
7059
197
  Elf_Internal_Ehdr *i_ehdrp;
7060
197
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7061
7062
  /* Skip non-load sections without section header.  */
7063
197
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7064
0
    return true;
7065
7066
197
  off = elf_next_file_pos (abfd);
7067
7068
197
  shdrpp = elf_elfsections (abfd);
7069
197
  end_shdrpp = shdrpp + elf_numsections (abfd);
7070
5.31k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7071
5.11k
    {
7072
5.11k
      shdrp = *shdrpp;
7073
5.11k
      if (shdrp->sh_offset == -1)
7074
1.45k
  {
7075
1.45k
    asection *sec = shdrp->bfd_section;
7076
1.45k
    if (sec == NULL
7077
1.45k
        || shdrp->sh_type == SHT_REL
7078
1.45k
        || shdrp->sh_type == SHT_RELA)
7079
1.45k
      ;
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.45k
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7129
1.45k
      (abfd->flags & (EXEC_P | DYNAMIC))
7130
1.45k
      || bfd_get_format (abfd) == bfd_core,
7131
1.45k
      bed->s->log_file_align);
7132
1.45k
  }
7133
5.11k
    }
7134
7135
  /* Place section name section after DWARF debug sections have been
7136
     compressed.  */
7137
197
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7138
197
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7139
197
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7140
197
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7141
7142
  /* Place the section headers.  */
7143
197
  i_ehdrp = elf_elfheader (abfd);
7144
197
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7145
197
  i_ehdrp->e_shoff = off;
7146
197
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7147
197
  elf_next_file_pos (abfd) = off;
7148
7149
197
  return true;
7150
197
}
7151
7152
bool
7153
_bfd_elf_write_object_contents (bfd *abfd)
7154
197
{
7155
197
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7156
197
  Elf_Internal_Shdr **i_shdrp;
7157
197
  bool failed;
7158
197
  unsigned int count, num_sec;
7159
197
  struct elf_obj_tdata *t;
7160
7161
197
  if (! abfd->output_has_begun
7162
197
      && ! _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
197
  else if (abfd->direction == both_direction)
7172
0
    {
7173
0
      BFD_ASSERT (abfd->output_has_begun);
7174
0
      return true;
7175
0
    }
7176
7177
197
  i_shdrp = elf_elfsections (abfd);
7178
7179
197
  failed = false;
7180
197
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7181
197
  if (failed)
7182
0
    return false;
7183
7184
197
  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
197
  num_sec = elf_numsections (abfd);
7189
5.31k
  for (count = 1; count < num_sec; count++)
7190
5.11k
    {
7191
      /* Don't set the sh_name field without section header.  */
7192
5.11k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7193
5.11k
  i_shdrp[count]->sh_name
7194
5.11k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7195
5.11k
            i_shdrp[count]->sh_name);
7196
5.11k
      if (bed->elf_backend_section_processing)
7197
1
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7198
0
    return false;
7199
5.11k
      if (i_shdrp[count]->contents)
7200
1.71k
  {
7201
1.71k
    bfd_size_type amt = i_shdrp[count]->sh_size;
7202
7203
1.71k
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7204
1.71k
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7205
0
      return false;
7206
1.71k
  }
7207
5.11k
    }
7208
7209
  /* Write out the section header names.  */
7210
197
  t = elf_tdata (abfd);
7211
197
  if (elf_shstrtab (abfd) != NULL
7212
197
      && t->shstrtab_hdr.sh_offset != -1
7213
197
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7214
197
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7215
0
    return false;
7216
7217
197
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7218
0
    return false;
7219
7220
197
  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
197
  if (t->o->build_id.after_write_object_contents != NULL
7225
197
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7226
0
    return false;
7227
197
  if (t->o->package_metadata.after_write_object_contents != NULL
7228
197
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7229
0
    return false;
7230
7231
197
  return true;
7232
197
}
7233
7234
bool
7235
_bfd_elf_write_corefile_contents (bfd *abfd)
7236
3
{
7237
  /* Hopefully this can be done just like an object file.  */
7238
3
  return _bfd_elf_write_object_contents (abfd);
7239
3
}
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
26.4k
{
7246
26.4k
  const struct elf_backend_data *bed;
7247
26.4k
  unsigned int sec_index;
7248
7249
26.4k
  if (elf_section_data (asect) != NULL
7250
26.4k
      && elf_section_data (asect)->this_idx != 0)
7251
19.1k
    return elf_section_data (asect)->this_idx;
7252
7253
7.31k
  if (bfd_is_abs_section (asect))
7254
0
    sec_index = SHN_ABS;
7255
7.31k
  else if (bfd_is_com_section (asect))
7256
1
    sec_index = SHN_COMMON;
7257
7.31k
  else if (bfd_is_und_section (asect))
7258
7.31k
    sec_index = SHN_UNDEF;
7259
0
  else
7260
0
    sec_index = SHN_BAD;
7261
7262
7.31k
  bed = get_elf_backend_data (abfd);
7263
7.31k
  if (bed->elf_backend_section_from_bfd_section)
7264
1.73k
    {
7265
1.73k
      int retval = sec_index;
7266
7267
1.73k
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7268
0
  return retval;
7269
1.73k
    }
7270
7271
7.31k
  if (sec_index == SHN_BAD)
7272
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7273
7274
7.31k
  return sec_index;
7275
7.31k
}
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
30.8k
{
7283
30.8k
  asymbol *asym_ptr = *asym_ptr_ptr;
7284
30.8k
  int idx;
7285
30.8k
  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
30.8k
  if (asym_ptr->udata.i == 0
7293
30.8k
      && (flags & BSF_SECTION_SYM)
7294
30.8k
      && 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
30.8k
  idx = asym_ptr->udata.i;
7308
7309
30.8k
  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
30.8k
  return idx;
7332
30.8k
}
7333
7334
static inline bfd_vma
7335
segment_size (Elf_Internal_Phdr *segment)
7336
2.47k
{
7337
2.47k
  return (segment->p_memsz > segment->p_filesz
7338
2.47k
    ? segment->p_memsz : segment->p_filesz);
7339
2.47k
}
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
10
{
7346
10
  return start + segment_size (segment);
7347
10
}
7348
7349
static inline bfd_size_type
7350
section_size (asection *section, Elf_Internal_Phdr *segment)
7351
2.48k
{
7352
2.48k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7353
2.48k
      || (section->flags & SEC_THREAD_LOCAL) == 0
7354
2.48k
      || segment->p_type == PT_TLS)
7355
2.48k
    return section->size;
7356
0
  return 0;
7357
2.48k
}
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
2.10k
{
7367
2.10k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7368
2.10k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7369
2.10k
  bfd_vma octet;
7370
2.10k
  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
2.10k
  return (octet >= seg_addr
7377
2.10k
    && segment_size (segment) >= section_size (section, segment)
7378
2.10k
    && (octet - seg_addr
7379
1.05k
        <= segment_size (segment) - section_size (section, segment)));
7380
2.10k
}
7381
7382
/* Handle PT_NOTE segment.  */
7383
static bool
7384
is_note (asection *s, Elf_Internal_Phdr *p)
7385
1.37k
{
7386
1.37k
  return (p->p_type == PT_NOTE
7387
1.37k
    && elf_section_type (s) == SHT_NOTE
7388
1.37k
    && (ufile_ptr) s->filepos >= p->p_offset
7389
1.37k
    && p->p_filesz >= s->size
7390
1.37k
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7391
1.37k
}
7392
7393
/* Rewrite program header information.  */
7394
7395
static bool
7396
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7397
11
{
7398
11
  Elf_Internal_Ehdr *iehdr;
7399
11
  struct elf_segment_map *map;
7400
11
  struct elf_segment_map *map_first;
7401
11
  struct elf_segment_map **pointer_to_map;
7402
11
  Elf_Internal_Phdr *segment;
7403
11
  asection *section;
7404
11
  unsigned int i;
7405
11
  unsigned int num_segments;
7406
11
  bool phdr_included = false;
7407
11
  bool p_paddr_valid;
7408
11
  struct elf_segment_map *phdr_adjust_seg = NULL;
7409
11
  unsigned int phdr_adjust_num = 0;
7410
11
  const struct elf_backend_data *bed;
7411
11
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7412
7413
11
  bed = get_elf_backend_data (ibfd);
7414
11
  iehdr = elf_elfheader (ibfd);
7415
7416
11
  map_first = NULL;
7417
11
  pointer_to_map = &map_first;
7418
7419
11
  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
11
#define IS_SOLARIS_PT_INTERP(p, s)          \
7425
48
  (p->p_vaddr == 0              \
7426
48
   && p->p_paddr == 0              \
7427
48
   && p->p_memsz == 0              \
7428
48
   && p->p_filesz > 0              \
7429
48
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7430
48
   && s->size > 0              \
7431
48
   && (bfd_vma) s->filepos >= p->p_offset        \
7432
48
   && ((bfd_vma) s->filepos + s->size         \
7433
40
       <= 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
11
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7449
2.08k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7450
1.84k
          segment->p_vaddr, opb, !paddr_valid)    \
7451
1.84k
     && (section->flags & SEC_ALLOC) != 0)       \
7452
1.84k
    || is_note (section, segment))         \
7453
1.84k
   && segment->p_type != PT_GNU_STACK          \
7454
1.84k
   && (segment->p_type != PT_TLS          \
7455
488
       || (section->flags & SEC_THREAD_LOCAL))        \
7456
1.84k
   && (segment->p_type == PT_LOAD          \
7457
488
       || segment->p_type == PT_TLS          \
7458
488
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7459
1.84k
   && (segment->p_type != PT_DYNAMIC          \
7460
488
       || section_size (section, segment) > 0        \
7461
488
       || (segment->p_paddr            \
7462
0
     ? segment->p_paddr != section->lma * (opb)      \
7463
0
     : segment->p_vaddr != section->vma * (opb))      \
7464
488
       || (strcmp (bfd_section_name (section), ".dynamic") == 0))  \
7465
2.08k
   && (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
11
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7470
244
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7471
244
   && section->output_section != NULL)
7472
7473
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7474
11
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7475
20
  (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
11
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7484
11
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7485
10
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7486
10
   && !(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
326
  for (section = ibfd->sections; section != NULL; section = section->next)
7491
315
    {
7492
315
      asection *o = section->output_section;
7493
315
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7494
0
  o->alignment_power = 0;
7495
315
      section->segment_mark = false;
7496
315
    }
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
11
  p_paddr_valid = false;
7503
11
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7504
61
       i < num_segments;
7505
50
       i++, segment++)
7506
51
    if (segment->p_paddr != 0)
7507
1
      {
7508
1
  p_paddr_valid = true;
7509
1
  break;
7510
1
      }
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
11
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7517
307
       i < num_segments;
7518
296
       i++, segment++)
7519
296
    {
7520
296
      unsigned int j;
7521
296
      Elf_Internal_Phdr *segment2;
7522
7523
296
      if (segment->p_type == PT_INTERP)
7524
50
  for (section = ibfd->sections; section; section = section->next)
7525
48
    if (IS_SOLARIS_PT_INTERP (segment, section))
7526
9
      {
7527
        /* Mininal change so that the normal section to segment
7528
     assignment code will work.  */
7529
9
        segment->p_vaddr = section->vma * opb;
7530
9
        break;
7531
9
      }
7532
7533
296
      if (segment->p_type != PT_LOAD)
7534
275
  {
7535
    /* Remove PT_GNU_RELRO segment.  */
7536
275
    if (segment->p_type == PT_GNU_RELRO)
7537
0
      segment->p_type = PT_NULL;
7538
275
    continue;
7539
275
  }
7540
7541
      /* Determine if this segment overlaps any previous segments.  */
7542
74
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7543
53
  {
7544
53
    bfd_signed_vma extra_length;
7545
7546
53
    if (segment2->p_type != PT_LOAD
7547
53
        || !SEGMENT_OVERLAPS (segment, segment2))
7548
53
      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
21
    }
7588
7589
  /* The second scan attempts to assign sections to segments.  */
7590
11
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7591
307
       i < num_segments;
7592
296
       i++, segment++)
7593
296
    {
7594
296
      unsigned int section_count;
7595
296
      asection **sections;
7596
296
      asection *output_section;
7597
296
      unsigned int isec;
7598
296
      asection *matching_lma;
7599
296
      asection *suggested_lma;
7600
296
      unsigned int j;
7601
296
      size_t amt;
7602
296
      asection *first_section;
7603
7604
296
      if (segment->p_type == PT_NULL)
7605
237
  continue;
7606
7607
59
      first_section = NULL;
7608
      /* Compute how many sections might be placed into this segment.  */
7609
59
      for (section = ibfd->sections, section_count = 0;
7610
1.65k
     section != NULL;
7611
1.59k
     section = section->next)
7612
1.59k
  {
7613
    /* Find the first section in the input segment, which may be
7614
       removed from the corresponding output segment.   */
7615
1.59k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7616
244
      {
7617
244
        if (first_section == NULL)
7618
38
    first_section = section;
7619
244
        if (section->output_section != NULL)
7620
244
    ++section_count;
7621
244
      }
7622
1.59k
  }
7623
7624
      /* Allocate a segment map big enough to contain
7625
   all of the sections we have selected.  */
7626
59
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7627
59
      amt += section_count * sizeof (asection *);
7628
59
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7629
59
      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
59
      map->next = NULL;
7635
59
      map->p_type = segment->p_type;
7636
59
      map->p_flags = segment->p_flags;
7637
59
      map->p_flags_valid = 1;
7638
7639
59
      if (map->p_type == PT_LOAD
7640
59
    && (ibfd->flags & D_PAGED) != 0
7641
59
    && maxpagesize > 1
7642
59
    && segment->p_align > 1)
7643
18
  {
7644
18
    map->p_align = segment->p_align;
7645
18
    if (segment->p_align > maxpagesize)
7646
0
      map->p_align = maxpagesize;
7647
18
    map->p_align_valid = 1;
7648
18
  }
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
59
      if (!first_section || first_section->output_section != NULL)
7654
59
  {
7655
59
    map->p_paddr = segment->p_paddr;
7656
59
    map->p_paddr_valid = p_paddr_valid;
7657
59
  }
7658
7659
      /* Determine if this segment contains the ELF file header
7660
   and if it contains the program headers themselves.  */
7661
59
      map->includes_filehdr = (segment->p_offset == 0
7662
59
             && segment->p_filesz >= iehdr->e_ehsize);
7663
59
      map->includes_phdrs = 0;
7664
7665
59
      if (!phdr_included || segment->p_type != PT_LOAD)
7666
50
  {
7667
50
    map->includes_phdrs =
7668
50
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7669
50
       && (segment->p_offset + segment->p_filesz
7670
26
     >= ((bfd_vma) iehdr->e_phoff
7671
26
         + iehdr->e_phnum * iehdr->e_phentsize)));
7672
7673
50
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7674
9
      phdr_included = true;
7675
50
  }
7676
7677
59
      if (section_count == 0)
7678
21
  {
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
21
    if (segment->p_type == PT_LOAD
7688
21
        && !map->includes_phdrs
7689
21
        && (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
21
    map->p_vaddr_offset = segment->p_vaddr / opb;
7697
21
    map->count = 0;
7698
21
    *pointer_to_map = map;
7699
21
    pointer_to_map = &map->next;
7700
7701
21
    continue;
7702
21
  }
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
38
      amt = section_count * sizeof (asection *);
7733
38
      sections = (asection **) bfd_malloc (amt);
7734
38
      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
38
      isec = 0;
7744
38
      matching_lma = NULL;
7745
38
      suggested_lma = NULL;
7746
7747
38
      for (section = first_section, j = 0;
7748
244
     section != NULL;
7749
206
     section = section->next)
7750
244
  {
7751
244
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7752
244
      {
7753
244
        output_section = section->output_section;
7754
7755
244
        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
244
        if (!p_paddr_valid
7762
244
      && segment->p_vaddr != 0
7763
244
      && !bed->want_p_paddr_set_to_zero
7764
244
      && isec == 0
7765
244
      && output_section->lma != 0
7766
244
      && (align_power (segment->p_vaddr
7767
43
           + (map->includes_filehdr
7768
43
              ? iehdr->e_ehsize : 0)
7769
43
           + (map->includes_phdrs
7770
43
              ? iehdr->e_phnum * iehdr->e_phentsize
7771
43
              : 0),
7772
43
           output_section->alignment_power * opb)
7773
43
          == (output_section->vma * opb)))
7774
33
    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
244
        if (is_contained_by (output_section, segment, map->p_paddr,
7779
244
           0, opb, false)
7780
244
      || is_note (section, segment))
7781
228
    {
7782
228
      if (matching_lma == NULL
7783
228
          || output_section->lma < matching_lma->lma)
7784
37
        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
228
      map->sections[isec++] = output_section;
7790
228
    }
7791
16
        else if (suggested_lma == NULL)
7792
2
    suggested_lma = output_section;
7793
7794
244
        if (j == section_count)
7795
38
    break;
7796
244
      }
7797
244
  }
7798
7799
38
      BFD_ASSERT (j == section_count);
7800
7801
      /* Step Two: Adjust the physical address of the current segment,
7802
   if necessary.  */
7803
38
      if (isec == section_count)
7804
36
  {
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
36
    map->count = section_count;
7810
36
    *pointer_to_map = map;
7811
36
    pointer_to_map = &map->next;
7812
7813
36
    if (p_paddr_valid
7814
36
        && !bed->want_p_paddr_set_to_zero)
7815
2
      {
7816
2
        bfd_vma hdr_size = 0;
7817
2
        if (map->includes_filehdr)
7818
0
    hdr_size = iehdr->e_ehsize;
7819
2
        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
2
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7825
2
             - matching_lma->lma);
7826
2
      }
7827
7828
36
    free (sections);
7829
36
    continue;
7830
36
  }
7831
2
      else
7832
2
  {
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
2
    if (matching_lma == NULL)
7837
1
      matching_lma = suggested_lma;
7838
7839
2
    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
2
    if (map->includes_phdrs)
7844
1
      {
7845
1
        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
1
        phdr_adjust_num = iehdr->e_phnum;
7853
1
        phdr_adjust_seg = map;
7854
1
      }
7855
7856
2
    if (map->includes_filehdr)
7857
1
      {
7858
1
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7859
1
        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
1
        if (segment->p_align != 0 && segment->p_align < align)
7866
0
    align = segment->p_align;
7867
1
        map->p_paddr &= -(align * opb);
7868
1
      }
7869
2
  }
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
2
      isec = 0;
7879
2
      do
7880
2
  {
7881
2
    map->count = 0;
7882
2
    suggested_lma = NULL;
7883
7884
    /* Fill the current segment with sections that fit.  */
7885
24
    for (j = 0; j < section_count; j++)
7886
22
      {
7887
22
        section = sections[j];
7888
7889
22
        if (section == NULL)
7890
0
    continue;
7891
7892
22
        output_section = section->output_section;
7893
7894
22
        BFD_ASSERT (output_section != NULL);
7895
7896
22
        if (is_contained_by (output_section, segment, map->p_paddr,
7897
22
           0, opb, false)
7898
22
      || is_note (section, segment))
7899
22
    {
7900
22
      if (map->count == 0)
7901
2
        {
7902
          /* If the first section in a segment does not start at
7903
       the beginning of the segment, then something is
7904
       wrong.  */
7905
2
          if (align_power (map->p_paddr
7906
2
               + (map->includes_filehdr
7907
2
            ? iehdr->e_ehsize : 0)
7908
2
               + (map->includes_phdrs
7909
2
            ? iehdr->e_phnum * iehdr->e_phentsize
7910
2
            : 0),
7911
2
               output_section->alignment_power * opb)
7912
2
        != output_section->lma * opb)
7913
0
      goto sorry;
7914
2
        }
7915
20
      else
7916
20
        {
7917
20
          asection *prev_sec;
7918
7919
20
          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
20
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7925
20
              maxpagesize)
7926
20
         < BFD_ALIGN (output_section->lma, maxpagesize))
7927
20
        || (prev_sec->lma + prev_sec->size
7928
20
            > output_section->lma))
7929
0
      {
7930
0
        if (suggested_lma == NULL)
7931
0
          suggested_lma = output_section;
7932
7933
0
        continue;
7934
0
      }
7935
20
        }
7936
7937
22
      map->sections[map->count++] = output_section;
7938
22
      ++isec;
7939
22
      sections[j] = NULL;
7940
22
      if (segment->p_type == PT_LOAD)
7941
22
        section->segment_mark = true;
7942
22
    }
7943
0
        else if (suggested_lma == NULL)
7944
0
    suggested_lma = output_section;
7945
22
      }
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
2
    *pointer_to_map = map;
7955
2
    pointer_to_map = &map->next;
7956
7957
2
    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
2
    continue;
7985
2
  sorry:
7986
0
    bfd_set_error (bfd_error_sorry);
7987
0
    free (sections);
7988
0
    return false;
7989
2
  }
7990
2
      while (isec < section_count);
7991
7992
2
      free (sections);
7993
2
    }
7994
7995
11
  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
11
  if (phdr_adjust_seg != NULL)
8001
1
    {
8002
1
      unsigned int count;
8003
8004
6
      for (count = 0, map = map_first; map != NULL; map = map->next)
8005
5
  count++;
8006
8007
1
      if (count > phdr_adjust_num)
8008
0
  phdr_adjust_seg->p_paddr
8009
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8010
8011
1
      for (map = map_first; map != NULL; map = map->next)
8012
1
  if (map->p_type == PT_PHDR)
8013
1
    {
8014
1
      bfd_vma adjust
8015
1
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8016
1
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8017
1
      break;
8018
1
    }
8019
1
    }
8020
8021
11
#undef IS_SOLARIS_PT_INTERP
8022
11
#undef IS_SECTION_IN_INPUT_SEGMENT
8023
11
#undef INCLUDE_SECTION_IN_SEGMENT
8024
11
#undef SEGMENT_AFTER_SEGMENT
8025
11
#undef SEGMENT_OVERLAPS
8026
11
  return true;
8027
11
}
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
33
{
8034
33
  unsigned int i;
8035
33
  Elf_Internal_Phdr *segment;
8036
33
  unsigned int num_segments;
8037
33
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8038
33
  bfd_size_type maxpagesize = bed->maxpagesize;
8039
33
  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
33
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8044
33
    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
33
{
8068
33
  Elf_Internal_Ehdr *iehdr;
8069
33
  struct elf_segment_map *map;
8070
33
  struct elf_segment_map *map_first;
8071
33
  struct elf_segment_map **pointer_to_map;
8072
33
  Elf_Internal_Phdr *segment;
8073
33
  unsigned int i;
8074
33
  unsigned int num_segments;
8075
33
  bool phdr_included = false;
8076
33
  bool p_paddr_valid;
8077
33
  bool p_palign_valid;
8078
33
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8079
8080
33
  iehdr = elf_elfheader (ibfd);
8081
8082
33
  map_first = NULL;
8083
33
  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
33
  p_paddr_valid = false;
8088
33
  num_segments = elf_elfheader (ibfd)->e_phnum;
8089
33
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8090
34
       i < num_segments;
8091
33
       i++, segment++)
8092
34
    if (segment->p_paddr != 0)
8093
33
      {
8094
33
  p_paddr_valid = true;
8095
33
  break;
8096
33
      }
8097
8098
33
  p_palign_valid = elf_is_p_align_valid (ibfd);
8099
8100
33
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8101
222
       i < num_segments;
8102
189
       i++, segment++)
8103
189
    {
8104
189
      asection *section;
8105
189
      unsigned int section_count;
8106
189
      size_t amt;
8107
189
      Elf_Internal_Shdr *this_hdr;
8108
189
      asection *first_section = NULL;
8109
189
      asection *lowest_section;
8110
8111
      /* Compute how many sections are in this segment.  */
8112
189
      for (section = ibfd->sections, section_count = 0;
8113
5.38k
     section != NULL;
8114
5.19k
     section = section->next)
8115
5.19k
  {
8116
5.19k
    this_hdr = &(elf_section_data(section)->this_hdr);
8117
5.19k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8118
801
      {
8119
801
        if (first_section == NULL)
8120
147
    first_section = section;
8121
801
        section_count++;
8122
801
      }
8123
5.19k
  }
8124
8125
      /* Allocate a segment map big enough to contain
8126
   all of the sections we have selected.  */
8127
189
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8128
189
      amt += section_count * sizeof (asection *);
8129
189
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8130
189
      if (map == NULL)
8131
0
  return false;
8132
8133
      /* Initialize the fields of the output segment map with the
8134
   input segment.  */
8135
189
      map->next = NULL;
8136
189
      map->p_type = segment->p_type;
8137
189
      map->p_flags = segment->p_flags;
8138
189
      map->p_flags_valid = 1;
8139
189
      map->p_paddr = segment->p_paddr;
8140
189
      map->p_paddr_valid = p_paddr_valid;
8141
189
      map->p_align = segment->p_align;
8142
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8143
189
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8144
189
          || p_palign_valid);
8145
189
      map->p_vaddr_offset = 0;
8146
8147
189
      if (map->p_type == PT_GNU_RELRO
8148
189
    || map->p_type == PT_GNU_STACK)
8149
10
  {
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
10
    map->p_size = segment->p_memsz;
8157
10
    map->p_size_valid = 1;
8158
10
  }
8159
8160
      /* Determine if this segment contains the ELF file header
8161
   and if it contains the program headers themselves.  */
8162
189
      map->includes_filehdr = (segment->p_offset == 0
8163
189
             && segment->p_filesz >= iehdr->e_ehsize);
8164
8165
189
      map->includes_phdrs = 0;
8166
189
      if (! phdr_included || segment->p_type != PT_LOAD)
8167
155
  {
8168
155
    map->includes_phdrs =
8169
155
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8170
155
       && (segment->p_offset + segment->p_filesz
8171
72
     >= ((bfd_vma) iehdr->e_phoff
8172
72
         + iehdr->e_phnum * iehdr->e_phentsize)));
8173
8174
155
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8175
32
      phdr_included = true;
8176
155
  }
8177
8178
189
      lowest_section = NULL;
8179
189
      if (section_count != 0)
8180
147
  {
8181
147
    unsigned int isec = 0;
8182
8183
147
    for (section = first_section;
8184
801
         section != NULL;
8185
654
         section = section->next)
8186
801
      {
8187
801
        this_hdr = &(elf_section_data(section)->this_hdr);
8188
801
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8189
801
    {
8190
801
      map->sections[isec++] = section->output_section;
8191
801
      if ((section->flags & SEC_ALLOC) != 0)
8192
801
        {
8193
801
          bfd_vma seg_off;
8194
8195
801
          if (lowest_section == NULL
8196
801
        || section->lma < lowest_section->lma)
8197
147
      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
801
          if ((section->flags & SEC_LOAD) != 0)
8205
767
      seg_off = this_hdr->sh_offset - segment->p_offset;
8206
34
          else
8207
34
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8208
801
          if (section->lma * opb - segment->p_paddr != seg_off)
8209
0
      map->p_paddr_valid = false;
8210
801
        }
8211
801
      if (isec == section_count)
8212
147
        break;
8213
801
    }
8214
801
      }
8215
147
  }
8216
8217
189
      if (section_count == 0)
8218
42
  map->p_vaddr_offset = segment->p_vaddr / opb;
8219
147
      else if (map->p_paddr_valid)
8220
147
  {
8221
    /* Account for padding before the first section in the segment.  */
8222
147
    bfd_vma hdr_size = 0;
8223
147
    if (map->includes_filehdr)
8224
32
      hdr_size = iehdr->e_ehsize;
8225
147
    if (map->includes_phdrs)
8226
32
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8227
8228
147
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8229
147
         - (lowest_section ? lowest_section->lma : 0));
8230
147
  }
8231
8232
189
      map->count = section_count;
8233
189
      *pointer_to_map = map;
8234
189
      pointer_to_map = &map->next;
8235
189
    }
8236
8237
33
  elf_seg_map (obfd) = map_first;
8238
33
  return true;
8239
33
}
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
44
{
8247
44
  bfd_vma maxpagesize;
8248
8249
44
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8250
44
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8251
0
    return true;
8252
8253
44
  if (elf_tdata (ibfd)->phdr == NULL)
8254
0
    return true;
8255
8256
44
  if (ibfd->xvec == obfd->xvec)
8257
44
    {
8258
      /* Check to see if any sections in the input BFD
8259
   covered by ELF program header have changed.  */
8260
44
      Elf_Internal_Phdr *segment;
8261
44
      asection * section;
8262
44
      asection * osec;
8263
44
      asection * prev;
8264
44
      unsigned int i, num_segments;
8265
44
      Elf_Internal_Shdr *this_hdr;
8266
44
      const struct elf_backend_data *bed;
8267
8268
44
      bed = get_elf_backend_data (ibfd);
8269
8270
      /* Regenerate the segment map if p_paddr is set to 0.  */
8271
44
      if (bed->want_p_paddr_set_to_zero)
8272
0
  goto rewrite;
8273
8274
      /* Initialize the segment mark field.  */
8275
1.26k
      for (section = obfd->sections; section != NULL;
8276
1.21k
     section = section->next)
8277
1.21k
  section->segment_mark = false;
8278
8279
44
      num_segments = elf_elfheader (ibfd)->e_phnum;
8280
44
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8281
247
     i < num_segments;
8282
203
     i++, segment++)
8283
214
  {
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
214
    if (segment->p_paddr == 0
8289
214
        && segment->p_memsz == 0
8290
214
        && (segment->p_type == PT_INTERP
8291
19
      || segment->p_type == PT_DYNAMIC))
8292
11
      goto rewrite;
8293
8294
203
    for (section = ibfd->sections, prev = NULL;
8295
5.73k
         section != NULL; section = section->next)
8296
5.53k
      {
8297
        /* We mark the output section so that we know it comes
8298
     from the input BFD.  */
8299
5.53k
        osec = section->output_section;
8300
5.53k
        if (osec)
8301
5.53k
    osec->segment_mark = true;
8302
8303
        /* Check if this section is covered by the segment.  */
8304
5.53k
        this_hdr = &(elf_section_data(section)->this_hdr);
8305
5.53k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8306
801
    {
8307
      /* FIXME: Check if its output section is changed or
8308
         removed.  What else do we need to check?  */
8309
801
      if (osec == NULL
8310
801
          || section->flags != osec->flags
8311
801
          || section->lma != osec->lma
8312
801
          || section->vma != osec->vma
8313
801
          || section->size != osec->size
8314
801
          || section->rawsize != osec->rawsize
8315
801
          || 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
801
      if (prev != NULL
8322
801
          && section->flags & SEC_ALLOC
8323
801
          && section->lma - section->vma != prev->lma - prev->vma)
8324
0
        goto rewrite;
8325
8326
801
      if (section->flags & SEC_ALLOC)
8327
801
        prev = section;
8328
801
    }
8329
5.53k
      }
8330
203
  }
8331
8332
      /* Check to see if any output section do not come from the
8333
   input BFD.  */
8334
936
      for (section = obfd->sections; section != NULL;
8335
903
     section = section->next)
8336
903
  {
8337
903
    if (!section->segment_mark)
8338
0
      goto rewrite;
8339
903
    else
8340
903
      section->segment_mark = false;
8341
903
  }
8342
8343
33
      return copy_elf_program_header (ibfd, obfd);
8344
33
    }
8345
8346
11
 rewrite:
8347
11
  maxpagesize = 0;
8348
11
  if (ibfd->xvec == obfd->xvec)
8349
11
    {
8350
      /* When rewriting program header, set the output maxpagesize to
8351
   the maximum alignment of input PT_LOAD segments.  */
8352
11
      Elf_Internal_Phdr *segment;
8353
11
      unsigned int i;
8354
11
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8355
8356
11
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8357
307
     i < num_segments;
8358
296
     i++, segment++)
8359
296
  if (segment->p_type == PT_LOAD
8360
296
      && maxpagesize < segment->p_align)
8361
11
    {
8362
      /* PR 17512: file: f17299af.  */
8363
11
      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
11
      else
8369
11
        maxpagesize = segment->p_align;
8370
11
    }
8371
11
    }
8372
11
  if (maxpagesize == 0)
8373
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8374
8375
11
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8376
44
}
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
4.65k
{
8388
4.65k
  Elf_Internal_Shdr *ihdr, *ohdr;
8389
4.65k
  bool final_link = (link_info != NULL
8390
4.65k
         && !bfd_link_relocatable (link_info));
8391
8392
4.65k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8393
4.65k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8394
0
    return true;
8395
8396
4.65k
  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
4.65k
  if (elf_section_type (osec) == SHT_PROGBITS
8403
4.65k
      || elf_section_type (osec) == SHT_NOTE
8404
4.65k
      || elf_section_type (osec) == SHT_NOBITS)
8405
1.79k
    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
4.65k
  if (elf_section_type (osec) == SHT_NULL
8412
4.65k
      && (osec->flags == isec->flags
8413
4.16k
    || (final_link
8414
0
        && ((osec->flags ^ isec->flags)
8415
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8416
4.16k
    elf_section_type (osec) = elf_section_type (isec);
8417
8418
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
8419
4.65k
  elf_section_flags (osec) = (elf_section_flags (isec)
8420
4.65k
            & (SHF_MASKOS | SHF_MASKPROC));
8421
8422
  /* Copy sh_info from input for mbind section.  */
8423
4.65k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8424
4.65k
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8425
3
    elf_section_data (osec)->this_hdr.sh_info
8426
3
      = 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
4.65k
  if ((link_info == NULL
8433
4.65k
       || !link_info->resolve_section_groups)
8434
4.65k
      && (elf_sec_group (isec) == NULL
8435
4.65k
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8436
4.65k
    {
8437
4.65k
      if (elf_section_flags (isec) & SHF_GROUP)
8438
1.96k
  elf_section_flags (osec) |= SHF_GROUP;
8439
4.65k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8440
4.65k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8441
4.65k
    }
8442
8443
  /* If not decompress, preserve SHF_COMPRESSED.  */
8444
4.65k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8445
4.65k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8446
4.65k
         & SHF_COMPRESSED);
8447
8448
4.65k
  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
4.65k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8454
5
    {
8455
5
      ohdr = &elf_section_data (osec)->this_hdr;
8456
5
      ohdr->sh_flags |= SHF_LINK_ORDER;
8457
5
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8458
5
    }
8459
8460
4.65k
  osec->use_rela_p = isec->use_rela_p;
8461
8462
4.65k
  return true;
8463
4.65k
}
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
4.65k
{
8474
4.65k
  Elf_Internal_Shdr *ihdr, *ohdr;
8475
8476
4.65k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8477
4.65k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8478
0
    return true;
8479
8480
4.65k
  ihdr = &elf_section_data (isec)->this_hdr;
8481
4.65k
  ohdr = &elf_section_data (osec)->this_hdr;
8482
8483
4.65k
  ohdr->sh_entsize = ihdr->sh_entsize;
8484
8485
4.65k
  if (ihdr->sh_type == SHT_SYMTAB
8486
4.65k
      || ihdr->sh_type == SHT_DYNSYM
8487
4.65k
      || ihdr->sh_type == SHT_GNU_verneed
8488
4.65k
      || ihdr->sh_type == SHT_GNU_verdef)
8489
84
    ohdr->sh_info = ihdr->sh_info;
8490
8491
4.65k
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8492
4.65k
               NULL);
8493
4.65k
}
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
109
{
8505
109
  asection *isec;
8506
8507
4.96k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8508
4.85k
    if (elf_section_type (isec) == SHT_GROUP)
8509
638
      {
8510
638
  asection *first = elf_next_in_group (isec);
8511
638
  asection *s = first;
8512
638
  bfd_size_type removed = 0;
8513
8514
1.54k
  while (s != NULL)
8515
1.33k
    {
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.33k
      if (s->output_section != discarded
8520
1.33k
    && 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.33k
      else
8526
1.33k
        {
8527
1.33k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8528
1.33k
    if (s->output_section == discarded
8529
1.33k
        && 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.33k
    else
8543
1.33k
      {
8544
        /* Also adjust for zero-sized relocation member
8545
           section.  */
8546
1.33k
        if (elf_sec->rel.hdr != NULL
8547
1.33k
      && elf_sec->rel.hdr->sh_size == 0)
8548
0
          removed += 4;
8549
1.33k
        if (elf_sec->rela.hdr != NULL
8550
1.33k
      && elf_sec->rela.hdr->sh_size == 0)
8551
0
          removed += 4;
8552
1.33k
      }
8553
1.33k
        }
8554
1.33k
      s = elf_next_in_group (s);
8555
1.33k
      if (s == first)
8556
432
        break;
8557
1.33k
    }
8558
638
  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
638
      }
8586
8587
109
  return true;
8588
109
}
8589
8590
/* Copy private header information.  */
8591
8592
bool
8593
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8594
109
{
8595
109
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8596
109
      || 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
109
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8605
44
    {
8606
44
      if (! copy_private_bfd_data (ibfd, obfd))
8607
0
  return false;
8608
44
    }
8609
8610
109
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8611
109
}
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
76
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8620
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8621
135
#define MAP_STRTAB    (SHN_HIOS + 3)
8622
108
#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
37.7k
{
8631
37.7k
  elf_symbol_type *isym, *osym;
8632
8633
37.7k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8634
37.7k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8635
0
    return true;
8636
8637
37.7k
  isym = elf_symbol_from (isymarg);
8638
37.7k
  osym = elf_symbol_from (osymarg);
8639
8640
37.7k
  if (isym != NULL
8641
37.7k
      && isym->internal_elf_sym.st_shndx != 0
8642
37.7k
      && osym != NULL
8643
37.7k
      && bfd_is_abs_section (isym->symbol.section))
8644
7.22k
    {
8645
7.22k
      unsigned int shndx;
8646
8647
7.22k
      shndx = isym->internal_elf_sym.st_shndx;
8648
7.22k
      if (shndx == elf_onesymtab (ibfd))
8649
38
  shndx = MAP_ONESYMTAB;
8650
7.18k
      else if (shndx == elf_dynsymtab (ibfd))
8651
0
  shndx = MAP_DYNSYMTAB;
8652
7.18k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8653
74
  shndx = MAP_STRTAB;
8654
7.11k
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8655
54
  shndx = MAP_SHSTRTAB;
8656
7.06k
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8657
0
  shndx = MAP_SYM_SHNDX;
8658
7.22k
      osym->internal_elf_sym.st_shndx = shndx;
8659
7.22k
    }
8660
8661
37.7k
  return true;
8662
37.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
83
{
8672
83
  const struct elf_backend_data *bed;
8673
83
  unsigned int symcount;
8674
83
  asymbol **syms;
8675
83
  struct elf_strtab_hash *stt;
8676
83
  Elf_Internal_Shdr *symtab_hdr;
8677
83
  Elf_Internal_Shdr *symtab_shndx_hdr;
8678
83
  Elf_Internal_Shdr *symstrtab_hdr;
8679
83
  struct elf_sym_strtab *symstrtab;
8680
83
  bfd_byte *outbound_syms;
8681
83
  bfd_byte *outbound_shndx;
8682
83
  unsigned long outbound_syms_index;
8683
83
  unsigned int idx;
8684
83
  unsigned int num_locals;
8685
83
  size_t amt;
8686
83
  bool name_local_sections;
8687
8688
83
  if (!elf_map_symbols (abfd, &num_locals))
8689
0
    return false;
8690
8691
  /* Dump out the symtabs.  */
8692
83
  stt = _bfd_elf_strtab_init ();
8693
83
  if (stt == NULL)
8694
0
    return false;
8695
8696
83
  bed = get_elf_backend_data (abfd);
8697
83
  symcount = bfd_get_symcount (abfd);
8698
83
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8699
83
  symtab_hdr->sh_type = SHT_SYMTAB;
8700
83
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8701
83
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8702
83
  symtab_hdr->sh_info = num_locals + 1;
8703
83
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8704
8705
83
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8706
83
  symstrtab_hdr->sh_type = SHT_STRTAB;
8707
8708
  /* Allocate buffer to swap out the .strtab section.  */
8709
83
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8710
83
      || (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
83
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8718
83
      || (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
83
  symtab_hdr->contents = outbound_syms;
8728
83
  outbound_syms_index = 0;
8729
8730
83
  outbound_shndx = NULL;
8731
8732
83
  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
83
  {
8755
    /* Fill in zeroth symbol and swap it out.  */
8756
83
    Elf_Internal_Sym sym;
8757
83
    sym.st_name = 0;
8758
83
    sym.st_value = 0;
8759
83
    sym.st_size = 0;
8760
83
    sym.st_info = 0;
8761
83
    sym.st_other = 0;
8762
83
    sym.st_shndx = SHN_UNDEF;
8763
83
    sym.st_target_internal = 0;
8764
83
    symstrtab[outbound_syms_index].sym = sym;
8765
83
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8766
83
    outbound_syms_index++;
8767
83
  }
8768
8769
83
  name_local_sections
8770
83
    = (bed->elf_backend_name_local_section_symbols
8771
83
       && bed->elf_backend_name_local_section_symbols (abfd));
8772
8773
83
  syms = bfd_get_outsymbols (abfd);
8774
32.6k
  for (idx = 0; idx < symcount; idx++)
8775
32.6k
    {
8776
32.6k
      Elf_Internal_Sym sym;
8777
8778
32.6k
      flagword flags = syms[idx]->flags;
8779
32.6k
      if (!name_local_sections
8780
32.6k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8781
1.82k
  {
8782
    /* Local section symbols have no name.  */
8783
1.82k
    sym.st_name = 0;
8784
1.82k
  }
8785
30.7k
      else
8786
30.7k
  {
8787
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8788
       to get the final offset for st_name.  */
8789
30.7k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8790
30.7k
    if (stridx == (size_t) -1)
8791
0
      goto error_return;
8792
30.7k
    sym.st_name = stridx;
8793
30.7k
  }
8794
8795
32.6k
      bfd_vma value = syms[idx]->value;
8796
32.6k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8797
32.6k
      asection *sec = syms[idx]->section;
8798
8799
32.6k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8800
1
  {
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
1
    sym.st_size = value;
8805
1
    if (type_ptr == NULL
8806
1
        || type_ptr->internal_elf_sym.st_value == 0)
8807
1
      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
1
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8811
1
  }
8812
32.6k
      else
8813
32.6k
  {
8814
32.6k
    unsigned int shndx;
8815
8816
32.6k
    if (sec->output_section)
8817
32.2k
      {
8818
32.2k
        value += sec->output_offset;
8819
32.2k
        sec = sec->output_section;
8820
32.2k
      }
8821
8822
    /* Don't add in the section vma for relocatable output.  */
8823
32.6k
    if (! relocatable_p)
8824
25.6k
      value += sec->vma;
8825
32.6k
    sym.st_value = value;
8826
32.6k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8827
8828
32.6k
    if (bfd_is_abs_section (sec)
8829
32.6k
        && type_ptr != NULL
8830
32.6k
        && type_ptr->internal_elf_sym.st_shndx != 0)
8831
6.91k
      {
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
6.91k
        shndx = type_ptr->internal_elf_sym.st_shndx;
8836
6.91k
        switch (shndx)
8837
6.91k
    {
8838
38
    case MAP_ONESYMTAB:
8839
38
      shndx = elf_onesymtab (abfd);
8840
38
      break;
8841
0
    case MAP_DYNSYMTAB:
8842
0
      shndx = elf_dynsymtab (abfd);
8843
0
      break;
8844
61
    case MAP_STRTAB:
8845
61
      shndx = elf_strtab_sec (abfd);
8846
61
      break;
8847
54
    case MAP_SHSTRTAB:
8848
54
      shndx = elf_shstrtab_sec (abfd);
8849
54
      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
1.03k
    case SHN_ABS:
8856
1.03k
      shndx = SHN_ABS;
8857
1.03k
      break;
8858
5.73k
    default:
8859
5.73k
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8860
7
        {
8861
7
          if (bed->symbol_section_index)
8862
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8863
          /* Otherwise just leave the index alone.  */
8864
7
        }
8865
5.72k
      else
8866
5.72k
        {
8867
5.72k
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8868
3
      _bfd_error_handler (_("%pB: \
8869
3
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8870
3
            abfd, shndx);
8871
5.72k
          shndx = SHN_ABS;
8872
5.72k
        }
8873
5.73k
      break;
8874
6.91k
    }
8875
6.91k
      }
8876
25.6k
    else
8877
25.6k
      {
8878
25.6k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8879
8880
25.6k
        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
25.6k
      }
8907
8908
32.6k
    sym.st_shndx = shndx;
8909
32.6k
  }
8910
8911
32.6k
      int type;
8912
32.6k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8913
282
  type = STT_TLS;
8914
32.3k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8915
7
  type = STT_GNU_IFUNC;
8916
32.3k
      else if ((flags & BSF_FUNCTION) != 0)
8917
10.1k
  type = STT_FUNC;
8918
22.2k
      else if ((flags & BSF_OBJECT) != 0)
8919
7.46k
  type = STT_OBJECT;
8920
14.7k
      else if ((flags & BSF_RELC) != 0)
8921
162
  type = STT_RELC;
8922
14.5k
      else if ((flags & BSF_SRELC) != 0)
8923
4
  type = STT_SRELC;
8924
14.5k
      else
8925
14.5k
  type = STT_NOTYPE;
8926
8927
32.6k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8928
0
  type = STT_TLS;
8929
8930
      /* Processor-specific types.  */
8931
32.6k
      if (type_ptr != NULL
8932
32.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
32.6k
      if (flags & BSF_SECTION_SYM)
8937
1.82k
  {
8938
1.82k
    if (flags & BSF_GLOBAL)
8939
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8940
1.82k
    else
8941
1.82k
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8942
1.82k
  }
8943
30.7k
      else if (bfd_is_com_section (syms[idx]->section))
8944
1
  {
8945
1
    if (type != STT_TLS)
8946
1
      {
8947
1
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8948
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8949
0
      ? STT_COMMON : STT_OBJECT);
8950
1
        else
8951
1
    type = ((flags & BSF_ELF_COMMON) != 0
8952
1
      ? STT_COMMON : STT_OBJECT);
8953
1
      }
8954
1
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8955
1
  }
8956
30.7k
      else if (bfd_is_und_section (syms[idx]->section))
8957
7.31k
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8958
30.7k
            ? STB_WEAK
8959
30.7k
            : STB_GLOBAL),
8960
30.7k
           type);
8961
23.4k
      else if (flags & BSF_FILE)
8962
1.07k
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8963
22.3k
      else
8964
22.3k
  {
8965
22.3k
    int bind = STB_LOCAL;
8966
8967
22.3k
    if (flags & BSF_LOCAL)
8968
17.8k
      bind = STB_LOCAL;
8969
4.51k
    else if (flags & BSF_GNU_UNIQUE)
8970
11
      bind = STB_GNU_UNIQUE;
8971
4.50k
    else if (flags & BSF_WEAK)
8972
485
      bind = STB_WEAK;
8973
4.02k
    else if (flags & BSF_GLOBAL)
8974
3.72k
      bind = STB_GLOBAL;
8975
8976
22.3k
    sym.st_info = ELF_ST_INFO (bind, type);
8977
22.3k
  }
8978
8979
32.6k
      if (type_ptr != NULL)
8980
32.6k
  {
8981
32.6k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
8982
32.6k
    sym.st_target_internal
8983
32.6k
      = type_ptr->internal_elf_sym.st_target_internal;
8984
32.6k
  }
8985
0
      else
8986
0
  {
8987
0
    sym.st_other = 0;
8988
0
    sym.st_target_internal = 0;
8989
0
  }
8990
8991
32.6k
      symstrtab[outbound_syms_index].sym = sym;
8992
32.6k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8993
32.6k
      outbound_syms_index++;
8994
32.6k
    }
8995
8996
  /* Finalize the .strtab section.  */
8997
83
  _bfd_elf_strtab_finalize (stt);
8998
8999
  /* Swap out the .strtab section.  */
9000
32.7k
  for (idx = 0; idx < outbound_syms_index; idx++)
9001
32.6k
    {
9002
32.6k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9003
32.6k
      if (elfsym->sym.st_name != 0)
9004
17.8k
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9005
17.8k
                  elfsym->sym.st_name);
9006
32.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
32.6k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9013
32.6k
             (outbound_syms
9014
32.6k
        + (elfsym->dest_index
9015
32.6k
           * bed->s->sizeof_sym)),
9016
32.6k
             NPTR_ADD (outbound_shndx,
9017
32.6k
           (elfsym->dest_index
9018
32.6k
            * sizeof (Elf_External_Sym_Shndx))));
9019
32.6k
    }
9020
83
  free (symstrtab);
9021
9022
83
  *sttp = stt;
9023
83
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9024
83
  symstrtab_hdr->sh_type = SHT_STRTAB;
9025
83
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9026
83
  symstrtab_hdr->sh_addr = 0;
9027
83
  symstrtab_hdr->sh_entsize = 0;
9028
83
  symstrtab_hdr->sh_link = 0;
9029
83
  symstrtab_hdr->sh_info = 0;
9030
83
  symstrtab_hdr->sh_addralign = 1;
9031
9032
83
  return true;
9033
83
}
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
7.97k
{
9044
7.97k
  bfd_size_type symcount;
9045
7.97k
  long symtab_size;
9046
7.97k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9047
9048
7.97k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9049
7.97k
  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
7.97k
  symtab_size = symcount * (sizeof (asymbol *));
9055
7.97k
  if (symcount == 0)
9056
671
    symtab_size = sizeof (asymbol *);
9057
7.30k
  else if (!bfd_write_p (abfd))
9058
7.30k
    {
9059
7.30k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9060
9061
7.30k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9062
192
  {
9063
192
    bfd_set_error (bfd_error_file_truncated);
9064
192
    return -1;
9065
192
  }
9066
7.30k
    }
9067
9068
7.78k
  return symtab_size;
9069
7.97k
}
9070
9071
long
9072
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9073
3.92k
{
9074
3.92k
  bfd_size_type symcount;
9075
3.92k
  long symtab_size;
9076
3.92k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9077
9078
3.92k
  if (elf_dynsymtab (abfd) == 0)
9079
2.96k
    {
9080
      /* Check if there is dynamic symbol table.  */
9081
2.96k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9082
2.96k
      if (symcount)
9083
6
  goto compute_symtab_size;
9084
9085
2.96k
      bfd_set_error (bfd_error_invalid_operation);
9086
2.96k
      return -1;
9087
2.96k
    }
9088
9089
954
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9090
954
  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
960
 compute_symtab_size:
9097
960
  symtab_size = symcount * (sizeof (asymbol *));
9098
960
  if (symcount == 0)
9099
10
    symtab_size = sizeof (asymbol *);
9100
950
  else if (!bfd_write_p (abfd))
9101
950
    {
9102
950
      ufile_ptr filesize = bfd_get_file_size (abfd);
9103
9104
950
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9105
20
  {
9106
20
    bfd_set_error (bfd_error_file_truncated);
9107
20
    return -1;
9108
20
  }
9109
950
    }
9110
9111
940
  return symtab_size;
9112
960
}
9113
9114
long
9115
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9116
58.5k
{
9117
58.5k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9118
56.1k
    {
9119
      /* Sanity check reloc section size.  */
9120
56.1k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9121
9122
56.1k
      if (filesize != 0)
9123
56.1k
  {
9124
56.1k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9125
56.1k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9126
56.1k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9127
9128
56.1k
    if (rel_size + rela_size > filesize
9129
56.1k
        || rel_size + rela_size < rel_size)
9130
447
      {
9131
447
        bfd_set_error (bfd_error_file_truncated);
9132
447
        return -1;
9133
447
      }
9134
56.1k
  }
9135
56.1k
    }
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
58.1k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9145
58.5k
}
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
58.1k
{
9155
58.1k
  arelent *tblptr;
9156
58.1k
  unsigned int i;
9157
58.1k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9158
9159
58.1k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9160
5.04k
    return -1;
9161
9162
53.0k
  tblptr = section->relocation;
9163
2.76M
  for (i = 0; i < section->reloc_count; i++)
9164
2.70M
    *relptr++ = tblptr++;
9165
9166
53.0k
  *relptr = NULL;
9167
9168
53.0k
  return section->reloc_count;
9169
58.1k
}
9170
9171
long
9172
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9173
7.78k
{
9174
7.78k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9175
7.78k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9176
9177
7.78k
  if (symcount >= 0)
9178
6.76k
    abfd->symcount = symcount;
9179
7.78k
  return symcount;
9180
7.78k
}
9181
9182
long
9183
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9184
              asymbol **allocation)
9185
531
{
9186
531
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9187
531
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9188
9189
531
  if (symcount >= 0)
9190
403
    abfd->dynsymcount = symcount;
9191
531
  return symcount;
9192
531
}
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
3.41k
{
9202
3.41k
  bfd_size_type count, ext_rel_size;
9203
3.41k
  asection *s;
9204
9205
3.41k
  if (elf_dynsymtab (abfd) == 0)
9206
2.84k
    {
9207
2.84k
      bfd_set_error (bfd_error_invalid_operation);
9208
2.84k
      return -1;
9209
2.84k
    }
9210
9211
576
  count = 1;
9212
576
  ext_rel_size = 0;
9213
15.5k
  for (s = abfd->sections; s != NULL; s = s->next)
9214
15.0k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9215
15.0k
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9216
2.22k
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9217
15.0k
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9218
993
      {
9219
993
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9220
993
  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
993
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9226
993
  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
993
      }
9232
576
  if (count > 1 && !bfd_write_p (abfd))
9233
562
    {
9234
      /* Sanity check reloc section sizes.  */
9235
562
      ufile_ptr filesize = bfd_get_file_size (abfd);
9236
562
      if (filesize != 0 && ext_rel_size > filesize)
9237
6
  {
9238
6
    bfd_set_error (bfd_error_file_truncated);
9239
6
    return -1;
9240
6
  }
9241
562
    }
9242
570
  return count * sizeof (arelent *);
9243
576
}
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
544
{
9258
544
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9259
544
  asection *s;
9260
544
  long ret;
9261
9262
544
  if (elf_dynsymtab (abfd) == 0)
9263
0
    {
9264
0
      bfd_set_error (bfd_error_invalid_operation);
9265
0
      return -1;
9266
0
    }
9267
9268
544
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9269
544
  ret = 0;
9270
13.8k
  for (s = abfd->sections; s != NULL; s = s->next)
9271
13.3k
    {
9272
13.3k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9273
13.3k
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9274
2.06k
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9275
13.3k
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9276
913
  {
9277
913
    arelent *p;
9278
913
    long count, i;
9279
9280
913
    if (! (*slurp_relocs) (abfd, s, syms, true))
9281
54
      return -1;
9282
859
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9283
859
    p = s->relocation;
9284
132k
    for (i = 0; i < count; i++)
9285
131k
      *storage++ = p++;
9286
859
    ret += count;
9287
859
  }
9288
13.3k
    }
9289
9290
490
  *storage = NULL;
9291
9292
490
  return ret;
9293
544
}
9294

9295
/* Read in the version information.  */
9296
9297
bool
9298
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9299
821
{
9300
821
  bfd_byte *contents = NULL;
9301
821
  unsigned int freeidx = 0;
9302
821
  size_t amt;
9303
821
  void *contents_addr = NULL;
9304
821
  size_t contents_size = 0;
9305
9306
821
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9307
802
    {
9308
802
      Elf_Internal_Shdr *hdr;
9309
802
      Elf_External_Verneed *everneed;
9310
802
      Elf_Internal_Verneed *iverneed;
9311
802
      unsigned int i;
9312
802
      bfd_byte *contents_end;
9313
802
      size_t verneed_count;
9314
802
      size_t verneed_size;
9315
9316
802
      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
801
      else
9324
801
  {
9325
801
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9326
9327
801
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9328
6
      {
9329
180
      error_return_bad_verref:
9330
180
        _bfd_error_handler
9331
180
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9332
180
        bfd_set_error (bfd_error_bad_value);
9333
206
      error_return_verref:
9334
206
        elf_tdata (abfd)->verref = NULL;
9335
206
        elf_tdata (abfd)->cverrefs = 0;
9336
206
        goto error_return;
9337
180
      }
9338
9339
795
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9340
5
      goto error_return_verref;
9341
790
    contents_size = hdr->sh_size;
9342
790
    contents = _bfd_mmap_temporary (abfd, contents_size,
9343
790
            &contents_addr, &contents_size);
9344
790
    if (contents == NULL)
9345
21
      goto error_return_verref;
9346
9347
769
    verneed_size = hdr->sh_size;
9348
769
    verneed_count = hdr->sh_info;
9349
769
  }
9350
9351
770
      if (_bfd_mul_overflow (verneed_count,
9352
770
           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
770
      if (amt == 0)
9358
0
  goto error_return_verref;
9359
770
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9360
770
      if (elf_tdata (abfd)->verref == NULL)
9361
0
  goto error_return_verref;
9362
9363
770
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9364
770
      == sizeof (Elf_External_Vernaux));
9365
770
      contents_end = (contents + verneed_size
9366
770
          - sizeof (Elf_External_Verneed));
9367
770
      everneed = (Elf_External_Verneed *) contents;
9368
770
      iverneed = elf_tdata (abfd)->verref;
9369
1.30k
      for (i = 0; i < verneed_count; i++, iverneed++)
9370
1.30k
  {
9371
1.30k
    Elf_External_Vernaux *evernaux;
9372
1.30k
    Elf_Internal_Vernaux *ivernaux;
9373
1.30k
    unsigned int j;
9374
9375
1.30k
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9376
9377
1.30k
    iverneed->vn_bfd = abfd;
9378
9379
1.30k
    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
1.30k
    else if (hdr == NULL)
9388
0
      goto error_return_bad_verref;
9389
1.30k
    else
9390
1.30k
      iverneed->vn_filename
9391
1.30k
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9392
1.30k
             iverneed->vn_file);
9393
1.30k
    if (iverneed->vn_filename == NULL)
9394
104
      goto error_return_bad_verref;
9395
9396
1.20k
    if (iverneed->vn_cnt == 0)
9397
37
      iverneed->vn_auxptr = NULL;
9398
1.16k
    else
9399
1.16k
      {
9400
1.16k
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9401
1.16k
             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
1.16k
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9407
1.16k
    bfd_alloc (abfd, amt);
9408
1.16k
        if (iverneed->vn_auxptr == NULL)
9409
0
    goto error_return_verref;
9410
1.16k
      }
9411
9412
1.20k
    if (iverneed->vn_aux
9413
1.20k
        > (size_t) (contents_end - (bfd_byte *) everneed))
9414
23
      goto error_return_bad_verref;
9415
9416
1.17k
    evernaux = ((Elf_External_Vernaux *)
9417
1.17k
          ((bfd_byte *) everneed + iverneed->vn_aux));
9418
1.17k
    ivernaux = iverneed->vn_auxptr;
9419
2.14k
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9420
2.11k
      {
9421
2.11k
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9422
9423
2.11k
        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
2.11k
        else if (hdr == NULL)
9432
0
    goto error_return_bad_verref;
9433
2.11k
        else
9434
2.11k
    ivernaux->vna_nodename
9435
2.11k
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9436
2.11k
                 ivernaux->vna_name);
9437
2.11k
        if (ivernaux->vna_nodename == NULL)
9438
5
    goto error_return_bad_verref;
9439
9440
2.11k
        if (ivernaux->vna_other > freeidx)
9441
527
    freeidx = ivernaux->vna_other;
9442
9443
2.11k
        ivernaux->vna_nextptr = NULL;
9444
2.11k
        if (ivernaux->vna_next == 0)
9445
1.12k
    {
9446
1.12k
      iverneed->vn_cnt = j + 1;
9447
1.12k
      break;
9448
1.12k
    }
9449
991
        if (j + 1 < iverneed->vn_cnt)
9450
972
    ivernaux->vna_nextptr = ivernaux + 1;
9451
9452
991
        if (ivernaux->vna_next
9453
991
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9454
25
    goto error_return_bad_verref;
9455
9456
966
        evernaux = ((Elf_External_Vernaux *)
9457
966
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9458
966
      }
9459
9460
1.14k
    iverneed->vn_nextref = NULL;
9461
1.14k
    if (iverneed->vn_next == 0)
9462
594
      break;
9463
555
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9464
536
      iverneed->vn_nextref = iverneed + 1;
9465
9466
555
    if (iverneed->vn_next
9467
555
        > (size_t) (contents_end - (bfd_byte *) everneed))
9468
17
      goto error_return_bad_verref;
9469
9470
538
    everneed = ((Elf_External_Verneed *)
9471
538
          ((bfd_byte *) everneed + iverneed->vn_next));
9472
538
  }
9473
596
      elf_tdata (abfd)->cverrefs = i;
9474
9475
596
      if (contents != elf_tdata (abfd)->dt_verneed)
9476
595
  _bfd_munmap_temporary (contents_addr, contents_size);
9477
596
      contents = NULL;
9478
596
      contents_addr = NULL;
9479
596
    }
9480
9481
615
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9482
19
    {
9483
19
      Elf_Internal_Shdr *hdr;
9484
19
      Elf_External_Verdef *everdef;
9485
19
      Elf_Internal_Verdef *iverdef;
9486
19
      Elf_Internal_Verdef *iverdefarr;
9487
19
      Elf_Internal_Verdef iverdefmem;
9488
19
      unsigned int i;
9489
19
      unsigned int maxidx;
9490
19
      bfd_byte *contents_end_def, *contents_end_aux;
9491
19
      size_t verdef_count;
9492
19
      size_t verdef_size;
9493
9494
19
      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
19
      else
9502
19
  {
9503
19
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9504
9505
19
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9506
1
      {
9507
9
      error_return_bad_verdef:
9508
9
        _bfd_error_handler
9509
9
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9510
9
        bfd_set_error (bfd_error_bad_value);
9511
17
      error_return_verdef:
9512
17
        elf_tdata (abfd)->verdef = NULL;
9513
17
        elf_tdata (abfd)->cverdefs = 0;
9514
17
        goto error_return;
9515
9
      }
9516
9517
18
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9518
1
      goto error_return_verdef;
9519
17
    contents_size = hdr->sh_size;
9520
17
    contents = _bfd_mmap_temporary (abfd, contents_size,
9521
17
            &contents_addr, &contents_size);
9522
17
    if (contents == NULL)
9523
7
      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
4
      break;
9553
9554
6
    if (iverdefmem.vd_next
9555
6
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9556
1
      goto error_return_bad_verdef;
9557
9558
5
    everdef = ((Elf_External_Verdef *)
9559
5
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9560
5
  }
9561
9562
7
      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
7
      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
7
      if (amt == 0)
9576
0
  goto error_return_verdef;
9577
7
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9578
7
      if (elf_tdata (abfd)->verdef == NULL)
9579
0
  goto error_return_verdef;
9580
9581
7
      elf_tdata (abfd)->cverdefs = maxidx;
9582
9583
7
      everdef = (Elf_External_Verdef *) contents;
9584
7
      iverdefarr = elf_tdata (abfd)->verdef;
9585
9
      for (i = 0; i < verdef_count; ++i)
9586
8
  {
9587
8
    Elf_External_Verdaux *everdaux;
9588
8
    Elf_Internal_Verdaux *iverdaux;
9589
8
    unsigned int j;
9590
9591
8
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9592
9593
8
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9594
0
      goto error_return_bad_verdef;
9595
9596
8
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9597
8
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9598
9599
8
    iverdef->vd_bfd = abfd;
9600
9601
8
    if (iverdef->vd_cnt == 0)
9602
4
      iverdef->vd_auxptr = NULL;
9603
4
    else
9604
4
      {
9605
4
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9606
4
             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
4
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9612
4
    bfd_alloc (abfd, amt);
9613
4
        if (iverdef->vd_auxptr == NULL)
9614
0
    goto error_return_verdef;
9615
4
      }
9616
9617
8
    if (iverdef->vd_aux
9618
8
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9619
2
      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
596
  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
598
  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
598
  return true;
9729
9730
223
 error_return:
9731
223
  if (contents != elf_tdata (abfd)->dt_verneed
9732
223
      && contents != elf_tdata (abfd)->dt_verdef)
9733
182
    _bfd_munmap_temporary (contents_addr, contents_size);
9734
223
  return false;
9735
598
}
9736

9737
asymbol *
9738
_bfd_elf_make_empty_symbol (bfd *abfd)
9739
5.77M
{
9740
5.77M
  elf_symbol_type *newsym;
9741
9742
5.77M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9743
5.77M
  if (!newsym)
9744
0
    return NULL;
9745
5.77M
  newsym->symbol.the_bfd = abfd;
9746
5.77M
  return &newsym->symbol;
9747
5.77M
}
9748
9749
void
9750
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9751
        asymbol *symbol,
9752
        symbol_info *ret)
9753
305k
{
9754
305k
  bfd_symbol_info (symbol, ret);
9755
305k
}
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
16.7k
{
9765
  /* Normal local symbols start with ``.L''.  */
9766
16.7k
  if (name[0] == '.' && name[1] == 'L')
9767
33
    return true;
9768
9769
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9770
     DWARF debugging symbols starting with ``..''.  */
9771
16.7k
  if (name[0] == '.' && name[1] == '.')
9772
651
    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
16.1k
  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
16.1k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9794
17
    {
9795
17
      bool ret = false;
9796
17
      const char * p;
9797
17
      char c;
9798
9799
71
      for (p = name + 2; (c = *p); p++)
9800
54
  {
9801
54
    if (c == 1 || c == 2)
9802
0
      {
9803
0
        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
0
        ret = true;
9813
0
      }
9814
9815
54
    if (! ISDIGIT (c))
9816
0
      {
9817
0
        ret = false;
9818
0
        break;
9819
0
      }
9820
54
  }
9821
17
      return ret;
9822
17
    }
9823
9824
16.0k
  return false;
9825
16.1k
}
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
281
{
9840
  /* If this isn't the right architecture for this backend, and this
9841
     isn't the generic backend, fail.  */
9842
281
  if (arch != get_elf_backend_data (abfd)->arch
9843
281
      && arch != bfd_arch_unknown
9844
281
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9845
0
    return false;
9846
9847
281
  return bfd_default_set_arch_mach (abfd, arch, machine);
9848
281
}
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
265k
{
9863
265k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9864
265k
                offset, filename_ptr,
9865
265k
                functionname_ptr, line_ptr,
9866
265k
                discriminator_ptr);
9867
265k
}
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
265k
{
9884
265k
  bool found;
9885
9886
265k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9887
265k
                section, offset, filename_ptr,
9888
265k
                functionname_ptr, line_ptr,
9889
265k
                discriminator_ptr,
9890
265k
                dwarf_debug_sections,
9891
265k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9892
118k
    return true;
9893
9894
147k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9895
147k
             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
147k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9905
147k
               &found, filename_ptr,
9906
147k
               functionname_ptr, line_ptr,
9907
147k
               &elf_tdata (abfd)->line_info))
9908
0
    return false;
9909
147k
  if (found && (*functionname_ptr || *line_ptr))
9910
0
    return true;
9911
9912
147k
  if (symbols == NULL)
9913
4.63k
    return false;
9914
9915
143k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9916
143k
        filename_ptr, functionname_ptr))
9917
119k
    return false;
9918
9919
23.2k
  *line_ptr = 0;
9920
23.2k
  return true;
9921
143k
}
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
179k
{
9929
179k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9930
179k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9931
179k
          filename_ptr, NULL, line_ptr, NULL,
9932
179k
          dwarf_debug_sections,
9933
179k
          &tdata->dwarf2_find_line_info);
9934
179k
}
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.97k
{
9990
2.97k
  Elf_Internal_Shdr *hdr;
9991
9992
2.97k
  if (! abfd->output_has_begun
9993
2.97k
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9994
4
    return false;
9995
9996
2.96k
  if (!count)
9997
0
    return true;
9998
9999
2.96k
  hdr = &elf_section_data (section)->this_hdr;
10000
2.96k
  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.96k
  return _bfd_generic_set_section_contents (abfd, section,
10037
2.96k
              location, offset, count);
10038
2.96k
}
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.00M
{
10148
1.00M
  struct elf_obj_tdata *tdata;
10149
10150
1.00M
  if ((bfd_get_format (abfd) == bfd_object
10151
1.00M
       || bfd_get_format (abfd) == bfd_core)
10152
1.00M
      && (tdata = elf_tdata (abfd)) != NULL)
10153
427k
    {
10154
427k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10155
201
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10156
427k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10157
427k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10158
427k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10159
2.42M
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10160
2.00M
  {
10161
2.00M
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10162
2.00M
    if (!sec->alloced)
10163
1.98M
      {
10164
1.98M
        free (elf_section_data (sec)->this_hdr.contents);
10165
1.98M
        elf_section_data (sec)->this_hdr.contents = NULL;
10166
1.98M
      }
10167
2.00M
    free (elf_section_data (sec)->relocs);
10168
2.00M
    elf_section_data (sec)->relocs = NULL;
10169
2.00M
    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.00M
  }
10176
427k
      free (tdata->symtab_hdr.contents);
10177
427k
      tdata->symtab_hdr.contents = NULL;
10178
427k
    }
10179
10180
1.00M
  return _bfd_generic_bfd_free_cached_info (abfd);
10181
1.00M
}
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
26
{
10195
26
  return bfd_reloc_ok;
10196
26
}
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
55
{
10213
55
  int pid;
10214
10215
55
  pid = elf_tdata (abfd)->core->lwpid;
10216
55
  if (pid == 0)
10217
55
    pid = elf_tdata (abfd)->core->pid;
10218
10219
55
  return pid;
10220
55
}
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
55
{
10229
55
  asection *sect2;
10230
10231
55
  if (bfd_get_section_by_name (abfd, name) != NULL)
10232
27
    return true;
10233
10234
28
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10235
28
  if (sect2 == NULL)
10236
0
    return false;
10237
10238
28
  sect2->size = sect->size;
10239
28
  sect2->filepos = sect->filepos;
10240
28
  sect2->alignment_power = sect->alignment_power;
10241
28
  return true;
10242
28
}
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
55
{
10257
55
  char buf[100];
10258
55
  char *threaded_name;
10259
55
  size_t len;
10260
55
  asection *sect;
10261
10262
  /* Build the section name.  */
10263
10264
55
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10265
55
  len = strlen (buf) + 1;
10266
55
  threaded_name = (char *) bfd_alloc (abfd, len);
10267
55
  if (threaded_name == NULL)
10268
0
    return false;
10269
55
  memcpy (threaded_name, buf, len);
10270
10271
55
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10272
55
               SEC_HAS_CONTENTS);
10273
55
  if (sect == NULL)
10274
0
    return false;
10275
55
  sect->size = size;
10276
55
  sect->filepos = filepos;
10277
55
  sect->alignment_power = 2;
10278
10279
55
  return elfcore_maybe_make_sect (abfd, name, sect);
10280
55
}
10281
10282
static bool
10283
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10284
        size_t offs)
10285
72
{
10286
72
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10287
72
                   SEC_HAS_CONTENTS);
10288
10289
72
  if (sect == NULL)
10290
0
    return false;
10291
10292
72
  sect->size = note->descsz - offs;
10293
72
  sect->filepos = note->descpos + offs;
10294
72
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10295
10296
72
  return true;
10297
72
}
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
47
{
10310
47
  size_t size;
10311
47
  int offset;
10312
10313
47
  if (note->descsz == sizeof (prstatus_t))
10314
0
    {
10315
0
      prstatus_t prstat;
10316
10317
0
      size = sizeof (prstat.pr_reg);
10318
0
      offset   = offsetof (prstatus_t, pr_reg);
10319
0
      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
0
      if (elf_tdata (abfd)->core->signal == 0)
10324
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10325
0
      if (elf_tdata (abfd)->core->pid == 0)
10326
0
  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
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10338
0
#endif
10339
0
    }
10340
47
#if defined (HAVE_PRSTATUS32_T)
10341
47
  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
47
#endif /* HAVE_PRSTATUS32_T */
10370
47
  else
10371
47
    {
10372
      /* Fail - we don't know how to handle any other
10373
   note size (ie. data object type).  */
10374
47
      return true;
10375
47
    }
10376
10377
  /* Make a ".reg/999" section and a ".reg" section.  */
10378
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10379
0
            size, note->descpos + offset);
10380
47
}
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
55
{
10389
55
  return _bfd_elfcore_make_pseudosection (abfd, name,
10390
55
            note->descsz, note->descpos);
10391
55
}
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
55
{
10400
55
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10401
55
}
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
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10662
   successful, otherwise return FALSE.  */
10663
10664
static bool
10665
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10666
0
{
10667
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-gcs", note);
10668
0
}
10669
10670
static bool
10671
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10672
0
{
10673
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10674
0
}
10675
10676
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10677
   successful otherwise, return FALSE.  */
10678
10679
static bool
10680
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10681
0
{
10682
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10683
0
}
10684
10685
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10686
   successful otherwise, return FALSE.  */
10687
10688
static bool
10689
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10690
0
{
10691
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10692
0
}
10693
10694
static bool
10695
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10696
0
{
10697
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10698
0
}
10699
10700
static bool
10701
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10702
0
{
10703
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10704
0
}
10705
10706
static bool
10707
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10708
0
{
10709
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10710
0
}
10711
10712
static bool
10713
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10714
0
{
10715
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10716
0
}
10717
10718
#if defined (HAVE_PRPSINFO_T)
10719
typedef prpsinfo_t   elfcore_psinfo_t;
10720
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10721
typedef prpsinfo32_t elfcore_psinfo32_t;
10722
#endif
10723
#endif
10724
10725
#if defined (HAVE_PSINFO_T)
10726
typedef psinfo_t   elfcore_psinfo_t;
10727
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10728
typedef psinfo32_t elfcore_psinfo32_t;
10729
#endif
10730
#endif
10731
10732
/* return a malloc'ed copy of a string at START which is at
10733
   most MAX bytes long, possibly without a terminating '\0'.
10734
   the copy will always have a terminating '\0'.  */
10735
10736
char *
10737
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10738
12
{
10739
12
  char *dups;
10740
12
  char *end = (char *) memchr (start, '\0', max);
10741
12
  size_t len;
10742
10743
12
  if (end == NULL)
10744
4
    len = max;
10745
8
  else
10746
8
    len = end - start;
10747
10748
12
  dups = (char *) bfd_alloc (abfd, len + 1);
10749
12
  if (dups == NULL)
10750
0
    return NULL;
10751
10752
12
  memcpy (dups, start, len);
10753
12
  dups[len] = '\0';
10754
10755
12
  return dups;
10756
12
}
10757
10758
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10759
static bool
10760
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10761
197
{
10762
197
  if (note->descsz == sizeof (elfcore_psinfo_t))
10763
0
    {
10764
0
      elfcore_psinfo_t psinfo;
10765
10766
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10767
10768
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10769
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10770
0
#endif
10771
0
      elf_tdata (abfd)->core->program
10772
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10773
0
        sizeof (psinfo.pr_fname));
10774
10775
0
      elf_tdata (abfd)->core->command
10776
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10777
0
        sizeof (psinfo.pr_psargs));
10778
0
    }
10779
197
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10780
197
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10781
6
    {
10782
      /* 64-bit host, 32-bit corefile */
10783
6
      elfcore_psinfo32_t psinfo;
10784
10785
6
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10786
10787
6
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10788
6
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10789
6
#endif
10790
6
      elf_tdata (abfd)->core->program
10791
6
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10792
6
        sizeof (psinfo.pr_fname));
10793
10794
6
      elf_tdata (abfd)->core->command
10795
6
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10796
6
        sizeof (psinfo.pr_psargs));
10797
6
    }
10798
191
#endif
10799
10800
191
  else
10801
191
    {
10802
      /* Fail - we don't know how to handle any other
10803
   note size (ie. data object type).  */
10804
191
      return true;
10805
191
    }
10806
10807
  /* Note that for some reason, a spurious space is tacked
10808
     onto the end of the args in some (at least one anyway)
10809
     implementations, so strip it off if it exists.  */
10810
10811
6
  {
10812
6
    char *command = elf_tdata (abfd)->core->command;
10813
6
    int n = strlen (command);
10814
10815
6
    if (0 < n && command[n - 1] == ' ')
10816
1
      command[n - 1] = '\0';
10817
6
  }
10818
10819
6
  return true;
10820
197
}
10821
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10822
10823
#if defined (HAVE_PSTATUS_T)
10824
static bool
10825
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10826
{
10827
  if (note->descsz == sizeof (pstatus_t)
10828
#if defined (HAVE_PXSTATUS_T)
10829
      || note->descsz == sizeof (pxstatus_t)
10830
#endif
10831
      )
10832
    {
10833
      pstatus_t pstat;
10834
10835
      memcpy (&pstat, note->descdata, sizeof (pstat));
10836
10837
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10838
    }
10839
#if defined (HAVE_PSTATUS32_T)
10840
  else if (note->descsz == sizeof (pstatus32_t))
10841
    {
10842
      /* 64-bit host, 32-bit corefile */
10843
      pstatus32_t pstat;
10844
10845
      memcpy (&pstat, note->descdata, sizeof (pstat));
10846
10847
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10848
    }
10849
#endif
10850
  /* Could grab some more details from the "representative"
10851
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10852
     NT_LWPSTATUS note, presumably.  */
10853
10854
  return true;
10855
}
10856
#endif /* defined (HAVE_PSTATUS_T) */
10857
10858
#if defined (HAVE_LWPSTATUS_T)
10859
static bool
10860
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10861
{
10862
  lwpstatus_t lwpstat;
10863
  char buf[100];
10864
  char *name;
10865
  size_t len;
10866
  asection *sect;
10867
10868
  if (note->descsz != sizeof (lwpstat)
10869
#if defined (HAVE_LWPXSTATUS_T)
10870
      && note->descsz != sizeof (lwpxstatus_t)
10871
#endif
10872
      )
10873
    return true;
10874
10875
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10876
10877
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10878
  /* Do not overwrite the core signal if it has already been set by
10879
     another thread.  */
10880
  if (elf_tdata (abfd)->core->signal == 0)
10881
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10882
10883
  /* Make a ".reg/999" section.  */
10884
10885
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10886
  len = strlen (buf) + 1;
10887
  name = bfd_alloc (abfd, len);
10888
  if (name == NULL)
10889
    return false;
10890
  memcpy (name, buf, len);
10891
10892
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10893
  if (sect == NULL)
10894
    return false;
10895
10896
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10897
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10898
  sect->filepos = note->descpos
10899
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10900
#endif
10901
10902
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10903
  sect->size = sizeof (lwpstat.pr_reg);
10904
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10905
#endif
10906
10907
  sect->alignment_power = 2;
10908
10909
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10910
    return false;
10911
10912
  /* Make a ".reg2/999" section */
10913
10914
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10915
  len = strlen (buf) + 1;
10916
  name = bfd_alloc (abfd, len);
10917
  if (name == NULL)
10918
    return false;
10919
  memcpy (name, buf, len);
10920
10921
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10922
  if (sect == NULL)
10923
    return false;
10924
10925
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10926
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10927
  sect->filepos = note->descpos
10928
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10929
#endif
10930
10931
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10932
  sect->size = sizeof (lwpstat.pr_fpreg);
10933
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10934
#endif
10935
10936
  sect->alignment_power = 2;
10937
10938
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10939
}
10940
#endif /* defined (HAVE_LWPSTATUS_T) */
10941
10942
/* These constants, and the structure offsets used below, are defined by
10943
   Cygwin's core_dump.h */
10944
0
#define NOTE_INFO_PROCESS  1
10945
0
#define NOTE_INFO_THREAD   2
10946
0
#define NOTE_INFO_MODULE   3
10947
0
#define NOTE_INFO_MODULE64 4
10948
10949
static bool
10950
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10951
5
{
10952
5
  char buf[30];
10953
5
  char *name;
10954
5
  size_t len;
10955
5
  unsigned int name_size;
10956
5
  asection *sect;
10957
5
  unsigned int type;
10958
5
  int is_active_thread;
10959
5
  bfd_vma base_addr;
10960
10961
5
  if (note->descsz < 4)
10962
5
    return true;
10963
10964
0
  if (! startswith (note->namedata, "win32"))
10965
0
    return true;
10966
10967
0
  type = bfd_get_32 (abfd, note->descdata);
10968
10969
0
  struct
10970
0
  {
10971
0
    const char *type_name;
10972
0
    unsigned long min_size;
10973
0
  } size_check[] =
10974
0
      {
10975
0
       { "NOTE_INFO_PROCESS", 12 },
10976
0
       { "NOTE_INFO_THREAD", 12 },
10977
0
       { "NOTE_INFO_MODULE", 12 },
10978
0
       { "NOTE_INFO_MODULE64", 16 },
10979
0
      };
10980
10981
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10982
0
      return true;
10983
10984
0
  if (note->descsz < size_check[type - 1].min_size)
10985
0
    {
10986
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10987
0
          " is too small"),
10988
0
        abfd, size_check[type - 1].type_name, note->descsz);
10989
0
      return true;
10990
0
    }
10991
10992
0
  switch (type)
10993
0
    {
10994
0
    case NOTE_INFO_PROCESS:
10995
      /* FIXME: need to add ->core->command.  */
10996
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10997
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10998
0
      break;
10999
11000
0
    case NOTE_INFO_THREAD:
11001
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11002
   structure. */
11003
      /* thread_info.tid */
11004
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11005
11006
0
      len = strlen (buf) + 1;
11007
0
      name = (char *) bfd_alloc (abfd, len);
11008
0
      if (name == NULL)
11009
0
  return false;
11010
11011
0
      memcpy (name, buf, len);
11012
11013
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11014
0
      if (sect == NULL)
11015
0
  return false;
11016
11017
      /* sizeof (thread_info.thread_context) */
11018
0
      sect->size = note->descsz - 12;
11019
      /* offsetof (thread_info.thread_context) */
11020
0
      sect->filepos = note->descpos + 12;
11021
0
      sect->alignment_power = 2;
11022
11023
      /* thread_info.is_active_thread */
11024
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11025
11026
0
      if (is_active_thread)
11027
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11028
0
    return false;
11029
0
      break;
11030
11031
0
    case NOTE_INFO_MODULE:
11032
0
    case NOTE_INFO_MODULE64:
11033
      /* Make a ".module/xxxxxxxx" section.  */
11034
0
      if (type == NOTE_INFO_MODULE)
11035
0
  {
11036
    /* module_info.base_address */
11037
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11038
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11039
    /* module_info.module_name_size */
11040
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11041
0
  }
11042
0
      else /* NOTE_INFO_MODULE64 */
11043
0
  {
11044
    /* module_info.base_address */
11045
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11046
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11047
    /* module_info.module_name_size */
11048
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11049
0
  }
11050
11051
0
      len = strlen (buf) + 1;
11052
0
      name = (char *) bfd_alloc (abfd, len);
11053
0
      if (name == NULL)
11054
0
  return false;
11055
11056
0
      memcpy (name, buf, len);
11057
11058
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11059
11060
0
      if (sect == NULL)
11061
0
  return false;
11062
11063
0
      if (note->descsz < 12 + name_size)
11064
0
  {
11065
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11066
0
        " is too small to contain a name of size %u"),
11067
0
            abfd, note->descsz, name_size);
11068
0
    return true;
11069
0
  }
11070
11071
0
      sect->size = note->descsz;
11072
0
      sect->filepos = note->descpos;
11073
0
      sect->alignment_power = 2;
11074
0
      break;
11075
11076
0
    default:
11077
0
      return true;
11078
0
    }
11079
11080
0
  return true;
11081
0
}
11082
11083
static bool
11084
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11085
1.23k
{
11086
1.23k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11087
11088
1.23k
  switch (note->type)
11089
1.23k
    {
11090
508
    default:
11091
508
      return true;
11092
11093
47
    case NT_PRSTATUS:
11094
47
      if (bed->elf_backend_grok_prstatus)
11095
29
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11096
0
    return true;
11097
47
#if defined (HAVE_PRSTATUS_T)
11098
47
      return elfcore_grok_prstatus (abfd, note);
11099
#else
11100
      return true;
11101
#endif
11102
11103
#if defined (HAVE_PSTATUS_T)
11104
    case NT_PSTATUS:
11105
      return elfcore_grok_pstatus (abfd, note);
11106
#endif
11107
11108
#if defined (HAVE_LWPSTATUS_T)
11109
    case NT_LWPSTATUS:
11110
      return elfcore_grok_lwpstatus (abfd, note);
11111
#endif
11112
11113
54
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
11114
54
      return elfcore_grok_prfpreg (abfd, note);
11115
11116
5
    case NT_WIN32PSTATUS:
11117
5
      return elfcore_grok_win32pstatus (abfd, note);
11118
11119
0
    case NT_PRXFPREG:   /* Linux SSE extension */
11120
0
      if (note->namesz == 6
11121
0
    && strcmp (note->namedata, "LINUX") == 0)
11122
0
  return elfcore_grok_prxfpreg (abfd, note);
11123
0
      else
11124
0
  return true;
11125
11126
4
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
11127
4
      if (note->namesz == 6
11128
4
    && strcmp (note->namedata, "LINUX") == 0)
11129
0
  return elfcore_grok_xstatereg (abfd, note);
11130
4
      else
11131
4
  return true;
11132
11133
0
    case NT_X86_SHSTK:   /* Linux CET extension.  */
11134
0
      if (note->namesz == 6
11135
0
    && strcmp (note->namedata, "LINUX") == 0)
11136
0
  return elfcore_grok_sspreg (abfd, note);
11137
0
      else
11138
0
  return true;
11139
11140
69
    case NT_PPC_VMX:
11141
69
      if (note->namesz == 6
11142
69
    && strcmp (note->namedata, "LINUX") == 0)
11143
0
  return elfcore_grok_ppc_vmx (abfd, note);
11144
69
      else
11145
69
  return true;
11146
11147
6
    case NT_PPC_VSX:
11148
6
      if (note->namesz == 6
11149
6
    && strcmp (note->namedata, "LINUX") == 0)
11150
0
  return elfcore_grok_ppc_vsx (abfd, note);
11151
6
      else
11152
6
  return true;
11153
11154
8
    case NT_PPC_TAR:
11155
8
      if (note->namesz == 6
11156
8
    && strcmp (note->namedata, "LINUX") == 0)
11157
0
  return elfcore_grok_ppc_tar (abfd, note);
11158
8
      else
11159
8
  return true;
11160
11161
4
    case NT_PPC_PPR:
11162
4
      if (note->namesz == 6
11163
4
    && strcmp (note->namedata, "LINUX") == 0)
11164
0
  return elfcore_grok_ppc_ppr (abfd, note);
11165
4
      else
11166
4
  return true;
11167
11168
6
    case NT_PPC_DSCR:
11169
6
      if (note->namesz == 6
11170
6
    && strcmp (note->namedata, "LINUX") == 0)
11171
0
  return elfcore_grok_ppc_dscr (abfd, note);
11172
6
      else
11173
6
  return true;
11174
11175
11
    case NT_PPC_EBB:
11176
11
      if (note->namesz == 6
11177
11
    && strcmp (note->namedata, "LINUX") == 0)
11178
0
  return elfcore_grok_ppc_ebb (abfd, note);
11179
11
      else
11180
11
  return true;
11181
11182
14
    case NT_PPC_PMU:
11183
14
      if (note->namesz == 6
11184
14
    && strcmp (note->namedata, "LINUX") == 0)
11185
0
  return elfcore_grok_ppc_pmu (abfd, note);
11186
14
      else
11187
14
  return true;
11188
11189
14
    case NT_PPC_TM_CGPR:
11190
14
      if (note->namesz == 6
11191
14
    && strcmp (note->namedata, "LINUX") == 0)
11192
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
11193
14
      else
11194
14
  return true;
11195
11196
2
    case NT_PPC_TM_CFPR:
11197
2
      if (note->namesz == 6
11198
2
    && strcmp (note->namedata, "LINUX") == 0)
11199
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
11200
2
      else
11201
2
  return true;
11202
11203
8
    case NT_PPC_TM_CVMX:
11204
8
      if (note->namesz == 6
11205
8
    && strcmp (note->namedata, "LINUX") == 0)
11206
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
11207
8
      else
11208
8
  return true;
11209
11210
6
    case NT_PPC_TM_CVSX:
11211
6
      if (note->namesz == 6
11212
6
    && strcmp (note->namedata, "LINUX") == 0)
11213
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
11214
6
      else
11215
6
  return true;
11216
11217
0
    case NT_PPC_TM_SPR:
11218
0
      if (note->namesz == 6
11219
0
    && strcmp (note->namedata, "LINUX") == 0)
11220
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
11221
0
      else
11222
0
  return true;
11223
11224
0
    case NT_PPC_TM_CTAR:
11225
0
      if (note->namesz == 6
11226
0
    && strcmp (note->namedata, "LINUX") == 0)
11227
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
11228
0
      else
11229
0
  return true;
11230
11231
4
    case NT_PPC_TM_CPPR:
11232
4
      if (note->namesz == 6
11233
4
    && strcmp (note->namedata, "LINUX") == 0)
11234
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
11235
4
      else
11236
4
  return true;
11237
11238
1
    case NT_PPC_TM_CDSCR:
11239
1
      if (note->namesz == 6
11240
1
    && strcmp (note->namedata, "LINUX") == 0)
11241
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
11242
1
      else
11243
1
  return true;
11244
11245
28
    case NT_S390_HIGH_GPRS:
11246
28
      if (note->namesz == 6
11247
28
    && strcmp (note->namedata, "LINUX") == 0)
11248
0
  return elfcore_grok_s390_high_gprs (abfd, note);
11249
28
      else
11250
28
  return true;
11251
11252
14
    case NT_S390_TIMER:
11253
14
      if (note->namesz == 6
11254
14
    && strcmp (note->namedata, "LINUX") == 0)
11255
0
  return elfcore_grok_s390_timer (abfd, note);
11256
14
      else
11257
14
  return true;
11258
11259
10
    case NT_S390_TODCMP:
11260
10
      if (note->namesz == 6
11261
10
    && strcmp (note->namedata, "LINUX") == 0)
11262
0
  return elfcore_grok_s390_todcmp (abfd, note);
11263
10
      else
11264
10
  return true;
11265
11266
3
    case NT_S390_TODPREG:
11267
3
      if (note->namesz == 6
11268
3
    && strcmp (note->namedata, "LINUX") == 0)
11269
0
  return elfcore_grok_s390_todpreg (abfd, note);
11270
3
      else
11271
3
  return true;
11272
11273
4
    case NT_S390_CTRS:
11274
4
      if (note->namesz == 6
11275
4
    && strcmp (note->namedata, "LINUX") == 0)
11276
0
  return elfcore_grok_s390_ctrs (abfd, note);
11277
4
      else
11278
4
  return true;
11279
11280
0
    case NT_S390_PREFIX:
11281
0
      if (note->namesz == 6
11282
0
    && strcmp (note->namedata, "LINUX") == 0)
11283
0
  return elfcore_grok_s390_prefix (abfd, note);
11284
0
      else
11285
0
  return true;
11286
11287
4
    case NT_S390_LAST_BREAK:
11288
4
      if (note->namesz == 6
11289
4
    && strcmp (note->namedata, "LINUX") == 0)
11290
0
  return elfcore_grok_s390_last_break (abfd, note);
11291
4
      else
11292
4
  return true;
11293
11294
4
    case NT_S390_SYSTEM_CALL:
11295
4
      if (note->namesz == 6
11296
4
    && strcmp (note->namedata, "LINUX") == 0)
11297
0
  return elfcore_grok_s390_system_call (abfd, note);
11298
4
      else
11299
4
  return true;
11300
11301
8
    case NT_S390_TDB:
11302
8
      if (note->namesz == 6
11303
8
    && strcmp (note->namedata, "LINUX") == 0)
11304
0
  return elfcore_grok_s390_tdb (abfd, note);
11305
8
      else
11306
8
  return true;
11307
11308
7
    case NT_S390_VXRS_LOW:
11309
7
      if (note->namesz == 6
11310
7
    && strcmp (note->namedata, "LINUX") == 0)
11311
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
11312
7
      else
11313
7
  return true;
11314
11315
1
    case NT_S390_VXRS_HIGH:
11316
1
      if (note->namesz == 6
11317
1
    && strcmp (note->namedata, "LINUX") == 0)
11318
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
11319
1
      else
11320
1
  return true;
11321
11322
2
    case NT_S390_GS_CB:
11323
2
      if (note->namesz == 6
11324
2
    && strcmp (note->namedata, "LINUX") == 0)
11325
0
  return elfcore_grok_s390_gs_cb (abfd, note);
11326
2
      else
11327
2
  return true;
11328
11329
0
    case NT_S390_GS_BC:
11330
0
      if (note->namesz == 6
11331
0
    && strcmp (note->namedata, "LINUX") == 0)
11332
0
  return elfcore_grok_s390_gs_bc (abfd, note);
11333
0
      else
11334
0
  return true;
11335
11336
7
    case NT_ARC_V2:
11337
7
      if (note->namesz == 6
11338
7
    && strcmp (note->namedata, "LINUX") == 0)
11339
0
  return elfcore_grok_arc_v2 (abfd, note);
11340
7
      else
11341
7
  return true;
11342
11343
11
    case NT_ARM_VFP:
11344
11
      if (note->namesz == 6
11345
11
    && strcmp (note->namedata, "LINUX") == 0)
11346
0
  return elfcore_grok_arm_vfp (abfd, note);
11347
11
      else
11348
11
  return true;
11349
11350
0
    case NT_ARM_TLS:
11351
0
      if (note->namesz == 6
11352
0
    && strcmp (note->namedata, "LINUX") == 0)
11353
0
  return elfcore_grok_aarch_tls (abfd, note);
11354
0
      else
11355
0
  return true;
11356
11357
4
    case NT_ARM_HW_BREAK:
11358
4
      if (note->namesz == 6
11359
4
    && strcmp (note->namedata, "LINUX") == 0)
11360
0
  return elfcore_grok_aarch_hw_break (abfd, note);
11361
4
      else
11362
4
  return true;
11363
11364
11
    case NT_ARM_HW_WATCH:
11365
11
      if (note->namesz == 6
11366
11
    && strcmp (note->namedata, "LINUX") == 0)
11367
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
11368
11
      else
11369
11
  return true;
11370
11371
0
    case NT_ARM_SVE:
11372
0
      if (note->namesz == 6
11373
0
    && strcmp (note->namedata, "LINUX") == 0)
11374
0
  return elfcore_grok_aarch_sve (abfd, note);
11375
0
      else
11376
0
  return true;
11377
11378
0
    case NT_ARM_PAC_MASK:
11379
0
      if (note->namesz == 6
11380
0
    && strcmp (note->namedata, "LINUX") == 0)
11381
0
  return elfcore_grok_aarch_pauth (abfd, note);
11382
0
      else
11383
0
  return true;
11384
11385
1
    case NT_ARM_TAGGED_ADDR_CTRL:
11386
1
      if (note->namesz == 6
11387
1
    && strcmp (note->namedata, "LINUX") == 0)
11388
0
  return elfcore_grok_aarch_mte (abfd, note);
11389
1
      else
11390
1
  return true;
11391
11392
0
    case NT_ARM_SSVE:
11393
0
      if (note->namesz == 6
11394
0
    && strcmp (note->namedata, "LINUX") == 0)
11395
0
  return elfcore_grok_aarch_ssve (abfd, note);
11396
0
      else
11397
0
  return true;
11398
11399
0
    case NT_ARM_ZA:
11400
0
      if (note->namesz == 6
11401
0
    && strcmp (note->namedata, "LINUX") == 0)
11402
0
  return elfcore_grok_aarch_za (abfd, note);
11403
0
      else
11404
0
  return true;
11405
11406
0
    case NT_ARM_ZT:
11407
0
      if (note->namesz == 6
11408
0
    && strcmp (note->namedata, "LINUX") == 0)
11409
0
  return elfcore_grok_aarch_zt (abfd, note);
11410
0
      else
11411
0
  return true;
11412
11413
0
    case NT_ARM_GCS:
11414
0
      if (note->namesz == 6 && strcmp (note->namedata, "LINUX") == 0)
11415
0
  return elfcore_grok_aarch_gcs (abfd, note);
11416
0
      else
11417
0
  return true;
11418
11419
30
    case NT_GDB_TDESC:
11420
30
      if (note->namesz == 4
11421
30
    && strcmp (note->namedata, "GDB") == 0)
11422
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11423
30
      else
11424
30
  return true;
11425
11426
8
    case NT_RISCV_CSR:
11427
8
      if (note->namesz == 4
11428
8
    && strcmp (note->namedata, "GDB") == 0)
11429
0
  return elfcore_grok_riscv_csr (abfd, note);
11430
8
      else
11431
8
  return true;
11432
11433
26
    case NT_LARCH_CPUCFG:
11434
26
      if (note->namesz == 6
11435
26
    && strcmp (note->namedata, "LINUX") == 0)
11436
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
11437
26
      else
11438
26
  return true;
11439
11440
4
    case NT_LARCH_LBT:
11441
4
      if (note->namesz == 6
11442
4
    && strcmp (note->namedata, "LINUX") == 0)
11443
0
  return elfcore_grok_loongarch_lbt (abfd, note);
11444
4
      else
11445
4
  return true;
11446
11447
7
    case NT_LARCH_LSX:
11448
7
      if (note->namesz == 6
11449
7
    && strcmp (note->namedata, "LINUX") == 0)
11450
0
  return elfcore_grok_loongarch_lsx (abfd, note);
11451
7
      else
11452
7
  return true;
11453
11454
0
    case NT_LARCH_LASX:
11455
0
      if (note->namesz == 6
11456
0
    && strcmp (note->namedata, "LINUX") == 0)
11457
0
  return elfcore_grok_loongarch_lasx (abfd, note);
11458
0
      else
11459
0
  return true;
11460
11461
195
    case NT_PRPSINFO:
11462
197
    case NT_PSINFO:
11463
197
      if (bed->elf_backend_grok_psinfo)
11464
134
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11465
0
    return true;
11466
197
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11467
197
      return elfcore_grok_psinfo (abfd, note);
11468
#else
11469
      return true;
11470
#endif
11471
11472
72
    case NT_AUXV:
11473
72
      return elfcore_make_auxv_note_section (abfd, note, 0);
11474
11475
0
    case NT_FILE:
11476
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11477
0
                note);
11478
11479
0
    case NT_SIGINFO:
11480
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11481
0
                note);
11482
11483
1.23k
    }
11484
1.23k
}
11485
11486
static bool
11487
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11488
963
{
11489
963
  struct bfd_build_id* build_id;
11490
11491
963
  if (note->descsz == 0)
11492
16
    return false;
11493
11494
947
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11495
947
  if (build_id == NULL)
11496
0
    return false;
11497
11498
947
  build_id->size = note->descsz;
11499
947
  memcpy (build_id->data, note->descdata, note->descsz);
11500
947
  abfd->build_id = build_id;
11501
11502
947
  return true;
11503
947
}
11504
11505
static bool
11506
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11507
2.46k
{
11508
2.46k
  switch (note->type)
11509
2.46k
    {
11510
1.37k
    default:
11511
1.37k
      return true;
11512
11513
124
    case NT_GNU_PROPERTY_TYPE_0:
11514
124
      return _bfd_elf_parse_gnu_properties (abfd, note);
11515
11516
963
    case NT_GNU_BUILD_ID:
11517
963
      return elfobj_grok_gnu_build_id (abfd, note);
11518
2.46k
    }
11519
2.46k
}
11520
11521
static bool
11522
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11523
0
{
11524
0
  struct sdt_note *cur =
11525
0
    (struct sdt_note *) bfd_alloc (abfd,
11526
0
           sizeof (struct sdt_note) + note->descsz);
11527
11528
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11529
0
  cur->size = (bfd_size_type) note->descsz;
11530
0
  memcpy (cur->data, note->descdata, note->descsz);
11531
11532
0
  elf_tdata (abfd)->sdt_note_head = cur;
11533
11534
0
  return true;
11535
0
}
11536
11537
static bool
11538
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11539
0
{
11540
0
  switch (note->type)
11541
0
    {
11542
0
    case NT_STAPSDT:
11543
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11544
11545
0
    default:
11546
0
      return true;
11547
0
    }
11548
0
}
11549
11550
static bool
11551
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11552
0
{
11553
0
  size_t offset;
11554
11555
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11556
0
    {
11557
0
    case ELFCLASS32:
11558
0
      if (note->descsz < 108)
11559
0
  return false;
11560
0
      break;
11561
11562
0
    case ELFCLASS64:
11563
0
      if (note->descsz < 120)
11564
0
  return false;
11565
0
      break;
11566
11567
0
    default:
11568
0
      return false;
11569
0
    }
11570
11571
  /* Check for version 1 in pr_version.  */
11572
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11573
0
    return false;
11574
11575
0
  offset = 4;
11576
11577
  /* Skip over pr_psinfosz. */
11578
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11579
0
    offset += 4;
11580
0
  else
11581
0
    {
11582
0
      offset += 4;  /* Padding before pr_psinfosz. */
11583
0
      offset += 8;
11584
0
    }
11585
11586
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11587
0
  elf_tdata (abfd)->core->program
11588
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11589
0
  offset += 17;
11590
11591
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11592
0
  elf_tdata (abfd)->core->command
11593
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11594
0
  offset += 81;
11595
11596
  /* Padding before pr_pid.  */
11597
0
  offset += 2;
11598
11599
  /* The pr_pid field was added in version "1a".  */
11600
0
  if (note->descsz < offset + 4)
11601
0
    return true;
11602
11603
0
  elf_tdata (abfd)->core->pid
11604
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11605
11606
0
  return true;
11607
0
}
11608
11609
static bool
11610
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11611
0
{
11612
0
  size_t offset;
11613
0
  size_t size;
11614
0
  size_t min_size;
11615
11616
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11617
     Also compute minimum size of this note.  */
11618
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11619
0
    {
11620
0
    case ELFCLASS32:
11621
0
      offset = 4 + 4;
11622
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11623
0
      break;
11624
11625
0
    case ELFCLASS64:
11626
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11627
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11628
0
      break;
11629
11630
0
    default:
11631
0
      return false;
11632
0
    }
11633
11634
0
  if (note->descsz < min_size)
11635
0
    return false;
11636
11637
  /* Check for version 1 in pr_version.  */
11638
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11639
0
    return false;
11640
11641
  /* Extract size of pr_reg from pr_gregsetsz.  */
11642
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11643
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11644
0
    {
11645
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11646
0
      offset += 4 * 2;
11647
0
    }
11648
0
  else
11649
0
    {
11650
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11651
0
      offset += 8 * 2;
11652
0
    }
11653
11654
  /* Skip over pr_osreldate.  */
11655
0
  offset += 4;
11656
11657
  /* Read signal from pr_cursig.  */
11658
0
  if (elf_tdata (abfd)->core->signal == 0)
11659
0
    elf_tdata (abfd)->core->signal
11660
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11661
0
  offset += 4;
11662
11663
  /* Read TID from pr_pid.  */
11664
0
  elf_tdata (abfd)->core->lwpid
11665
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11666
0
  offset += 4;
11667
11668
  /* Padding before pr_reg.  */
11669
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11670
0
    offset += 4;
11671
11672
  /* Make sure that there is enough data remaining in the note.  */
11673
0
  if ((note->descsz - offset) < size)
11674
0
    return false;
11675
11676
  /* Make a ".reg/999" section and a ".reg" section.  */
11677
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11678
0
            size, note->descpos + offset);
11679
0
}
11680
11681
static bool
11682
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11683
6
{
11684
6
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11685
11686
6
  switch (note->type)
11687
6
    {
11688
0
    case NT_PRSTATUS:
11689
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11690
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11691
0
    return true;
11692
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11693
11694
1
    case NT_FPREGSET:
11695
1
      return elfcore_grok_prfpreg (abfd, note);
11696
11697
0
    case NT_PRPSINFO:
11698
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11699
11700
0
    case NT_FREEBSD_THRMISC:
11701
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11702
11703
0
    case NT_FREEBSD_PROCSTAT_PROC:
11704
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11705
0
                note);
11706
11707
0
    case NT_FREEBSD_PROCSTAT_FILES:
11708
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11709
0
                note);
11710
11711
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11712
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11713
0
                note);
11714
11715
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11716
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11717
11718
0
    case NT_FREEBSD_X86_SEGBASES:
11719
0
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11720
11721
0
    case NT_X86_XSTATE:
11722
0
      return elfcore_grok_xstatereg (abfd, note);
11723
11724
0
    case NT_FREEBSD_PTLWPINFO:
11725
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11726
0
                note);
11727
11728
0
    case NT_ARM_TLS:
11729
0
      return elfcore_grok_aarch_tls (abfd, note);
11730
11731
0
    case NT_ARM_VFP:
11732
0
      return elfcore_grok_arm_vfp (abfd, note);
11733
11734
5
    default:
11735
5
      return true;
11736
6
    }
11737
6
}
11738
11739
static bool
11740
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11741
0
{
11742
0
  char *cp;
11743
11744
0
  cp = strchr (note->namedata, '@');
11745
0
  if (cp != NULL)
11746
0
    {
11747
0
      *lwpidp = atoi(cp + 1);
11748
0
      return true;
11749
0
    }
11750
0
  return false;
11751
0
}
11752
11753
static bool
11754
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11755
0
{
11756
0
  if (note->descsz <= 0x7c + 31)
11757
0
    return false;
11758
11759
  /* Signal number at offset 0x08. */
11760
0
  elf_tdata (abfd)->core->signal
11761
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11762
11763
  /* Process ID at offset 0x50. */
11764
0
  elf_tdata (abfd)->core->pid
11765
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11766
11767
  /* Command name at 0x7c (max 32 bytes, including nul). */
11768
0
  elf_tdata (abfd)->core->command
11769
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11770
11771
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11772
0
            note);
11773
0
}
11774
11775
static bool
11776
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11777
0
{
11778
0
  int lwp;
11779
11780
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11781
0
    elf_tdata (abfd)->core->lwpid = lwp;
11782
11783
0
  switch (note->type)
11784
0
    {
11785
0
    case NT_NETBSDCORE_PROCINFO:
11786
      /* NetBSD-specific core "procinfo".  Note that we expect to
11787
   find this note before any of the others, which is fine,
11788
   since the kernel writes this note out first when it
11789
   creates a core file.  */
11790
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11791
0
    case NT_NETBSDCORE_AUXV:
11792
      /* NetBSD-specific Elf Auxiliary Vector data. */
11793
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11794
0
    case NT_NETBSDCORE_LWPSTATUS:
11795
0
      return elfcore_make_note_pseudosection (abfd,
11796
0
                ".note.netbsdcore.lwpstatus",
11797
0
                note);
11798
0
    default:
11799
0
      break;
11800
0
    }
11801
11802
  /* As of March 2020 there are no other machine-independent notes
11803
     defined for NetBSD core files.  If the note type is less
11804
     than the start of the machine-dependent note types, we don't
11805
     understand it.  */
11806
11807
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11808
0
    return true;
11809
11810
11811
0
  switch (bfd_get_arch (abfd))
11812
0
    {
11813
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11814
   PT_GETFPREGS == mach+2.  */
11815
11816
0
    case bfd_arch_aarch64:
11817
0
    case bfd_arch_alpha:
11818
0
    case bfd_arch_sparc:
11819
0
      switch (note->type)
11820
0
  {
11821
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11822
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11823
11824
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11825
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11826
11827
0
  default:
11828
0
    return true;
11829
0
  }
11830
11831
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11832
   There's also old PT___GETREGS40 == mach + 1 for old reg
11833
   structure which lacks GBR.  */
11834
11835
0
    case bfd_arch_sh:
11836
0
      switch (note->type)
11837
0
  {
11838
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11839
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11840
11841
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11842
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11843
11844
0
  default:
11845
0
    return true;
11846
0
  }
11847
11848
      /* On all other arch's, PT_GETREGS == mach+1 and
11849
   PT_GETFPREGS == mach+3.  */
11850
11851
0
    default:
11852
0
      switch (note->type)
11853
0
  {
11854
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11855
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11856
11857
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11858
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11859
11860
0
  default:
11861
0
    return true;
11862
0
  }
11863
0
    }
11864
    /* NOTREACHED */
11865
0
}
11866
11867
static bool
11868
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11869
0
{
11870
0
  if (note->descsz <= 0x48 + 31)
11871
0
    return false;
11872
11873
  /* Signal number at offset 0x08. */
11874
0
  elf_tdata (abfd)->core->signal
11875
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11876
11877
  /* Process ID at offset 0x20. */
11878
0
  elf_tdata (abfd)->core->pid
11879
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11880
11881
  /* Command name at 0x48 (max 32 bytes, including nul). */
11882
0
  elf_tdata (abfd)->core->command
11883
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11884
11885
0
  return true;
11886
0
}
11887
11888
/* Processes Solaris's process status note.
11889
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11890
   pid_off ~ offsetof(prstatus_t, pr_pid)
11891
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11892
   gregset_size ~ sizeof(gregset_t)
11893
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11894
11895
static bool
11896
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11897
             int pid_off, int lwpid_off, size_t gregset_size,
11898
             size_t gregset_offset)
11899
0
{
11900
0
  asection *sect = NULL;
11901
0
  elf_tdata (abfd)->core->signal
11902
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11903
0
  elf_tdata (abfd)->core->pid
11904
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11905
0
  elf_tdata (abfd)->core->lwpid
11906
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11907
11908
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11909
0
  if (sect != NULL)
11910
0
    sect->size = gregset_size;
11911
11912
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11913
0
            note->descpos + gregset_offset);
11914
0
}
11915
11916
/* Gets program and arguments from a core.
11917
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11918
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11919
11920
static bool
11921
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11922
        int prog_off, int comm_off)
11923
0
{
11924
0
  elf_tdata (abfd)->core->program
11925
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11926
0
  elf_tdata (abfd)->core->command
11927
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11928
11929
0
  return true;
11930
0
}
11931
11932
/* Processes Solaris's LWP status note.
11933
   gregset_size ~ sizeof(gregset_t)
11934
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11935
   fpregset_size ~ sizeof(fpregset_t)
11936
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11937
11938
static bool
11939
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11940
        size_t gregset_size, int gregset_off,
11941
        size_t fpregset_size, int fpregset_off)
11942
0
{
11943
0
  asection *sect = NULL;
11944
0
  char reg2_section_name[16] = { 0 };
11945
11946
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11947
0
       elf_tdata (abfd)->core->lwpid);
11948
11949
  /* offsetof(lwpstatus_t, pr_lwpid) */
11950
0
  elf_tdata (abfd)->core->lwpid
11951
0
    = bfd_get_32 (abfd, note->descdata + 4);
11952
  /* offsetof(lwpstatus_t, pr_cursig) */
11953
0
  elf_tdata (abfd)->core->signal
11954
0
    = bfd_get_16 (abfd, note->descdata + 12);
11955
11956
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11957
0
  if (sect != NULL)
11958
0
    sect->size = gregset_size;
11959
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11960
0
               note->descpos + gregset_off))
11961
0
    return false;
11962
11963
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11964
0
  if (sect != NULL)
11965
0
    {
11966
0
      sect->size = fpregset_size;
11967
0
      sect->filepos = note->descpos + fpregset_off;
11968
0
      sect->alignment_power = 2;
11969
0
    }
11970
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11971
0
               note->descpos + fpregset_off))
11972
0
    return false;
11973
11974
0
  return true;
11975
0
}
11976
11977
static bool
11978
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11979
1
{
11980
1
  if (note == NULL)
11981
0
    return false;
11982
11983
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11984
     by the size of the descsz which matches the sizeof()
11985
     the type appropriate for that note type (e.g., prstatus_t for
11986
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11987
     on Solaris. The core file bitness may differ from the bitness of
11988
     gdb itself, so fixed values are used instead of sizeof().
11989
     Appropriate fixed offsets are also used to obtain data from
11990
     the note.  */
11991
11992
1
  switch ((int) note->type)
11993
1
    {
11994
0
    case SOLARIS_NT_PRSTATUS:
11995
0
      switch (note->descsz)
11996
0
  {
11997
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11998
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11999
0
                 136, 216, 308, 152, 356);
12000
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12001
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12002
0
                 264, 360, 520, 304, 600);
12003
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
12004
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12005
0
                 136, 216, 308, 76, 356);
12006
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
12007
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12008
0
                 264, 360, 520, 224, 600);
12009
0
  default:
12010
0
    return true;
12011
0
  }
12012
12013
0
    case SOLARIS_NT_PSINFO:
12014
0
    case SOLARIS_NT_PRPSINFO:
12015
0
      switch (note->descsz)
12016
0
  {
12017
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12018
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
12019
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12020
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
12021
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12022
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
12023
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12024
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
12025
0
  default:
12026
0
    return true;
12027
0
  }
12028
12029
0
    case SOLARIS_NT_LWPSTATUS:
12030
0
      switch (note->descsz)
12031
0
  {
12032
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12033
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12034
0
            152, 344, 400, 496);
12035
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12036
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12037
0
            304, 544, 544, 848);
12038
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12039
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12040
0
            76, 344, 380, 420);
12041
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12042
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12043
0
            224, 544, 528, 768);
12044
0
  default:
12045
0
    return true;
12046
0
  }
12047
12048
0
    case SOLARIS_NT_LWPSINFO:
12049
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12050
0
      if (note->descsz == 128 || note->descsz == 152)
12051
0
  elf_tdata (abfd)->core->lwpid =
12052
0
    bfd_get_32 (abfd, note->descdata + 4);
12053
0
      break;
12054
12055
1
    default:
12056
1
      break;
12057
1
    }
12058
12059
1
  return true;
12060
1
}
12061
12062
/* For name starting with "CORE" this may be either a Solaris
12063
   core file or a gdb-generated core file.  Do Solaris-specific
12064
   processing on selected note types first with
12065
   elfcore_grok_solaris_note(), then process the note
12066
   in elfcore_grok_note().  */
12067
12068
static bool
12069
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12070
1
{
12071
1
  if (!elfcore_grok_solaris_note_impl (abfd, note))
12072
0
    return false;
12073
12074
1
  return elfcore_grok_note (abfd, note);
12075
1
}
12076
12077
static bool
12078
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12079
0
{
12080
0
  if (note->type == NT_OPENBSD_PROCINFO)
12081
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
12082
12083
0
  if (note->type == NT_OPENBSD_REGS)
12084
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
12085
12086
0
  if (note->type == NT_OPENBSD_FPREGS)
12087
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12088
12089
0
  if (note->type == NT_OPENBSD_XFPREGS)
12090
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12091
12092
0
  if (note->type == NT_OPENBSD_AUXV)
12093
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
12094
12095
0
  if (note->type == NT_OPENBSD_WCOOKIE)
12096
0
    {
12097
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12098
0
                 SEC_HAS_CONTENTS);
12099
12100
0
      if (sect == NULL)
12101
0
  return false;
12102
0
      sect->size = note->descsz;
12103
0
      sect->filepos = note->descpos;
12104
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12105
12106
0
      return true;
12107
0
    }
12108
12109
0
  return true;
12110
0
}
12111
12112
static bool
12113
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12114
0
{
12115
0
  void *ddata = note->descdata;
12116
0
  char buf[100];
12117
0
  char *name;
12118
0
  asection *sect;
12119
0
  short sig;
12120
0
  unsigned flags;
12121
12122
0
  if (note->descsz < 16)
12123
0
    return false;
12124
12125
  /* nto_procfs_status 'pid' field is at offset 0.  */
12126
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12127
12128
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
12129
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12130
12131
  /* nto_procfs_status 'flags' field is at offset 8.  */
12132
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12133
12134
  /* nto_procfs_status 'what' field is at offset 14.  */
12135
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12136
0
    {
12137
0
      elf_tdata (abfd)->core->signal = sig;
12138
0
      elf_tdata (abfd)->core->lwpid = *tid;
12139
0
    }
12140
12141
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
12142
     do not come from signals so we make sure we set the current
12143
     thread just in case.  */
12144
0
  if (flags & 0x00000080)
12145
0
    elf_tdata (abfd)->core->lwpid = *tid;
12146
12147
  /* Make a ".qnx_core_status/%d" section.  */
12148
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
12149
12150
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12151
0
  if (name == NULL)
12152
0
    return false;
12153
0
  strcpy (name, buf);
12154
12155
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12156
0
  if (sect == NULL)
12157
0
    return false;
12158
12159
0
  sect->size    = note->descsz;
12160
0
  sect->filepos   = note->descpos;
12161
0
  sect->alignment_power = 2;
12162
12163
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12164
0
}
12165
12166
static bool
12167
elfcore_grok_nto_regs (bfd *abfd,
12168
           Elf_Internal_Note *note,
12169
           long tid,
12170
           char *base)
12171
0
{
12172
0
  char buf[100];
12173
0
  char *name;
12174
0
  asection *sect;
12175
12176
  /* Make a "(base)/%d" section.  */
12177
0
  sprintf (buf, "%s/%ld", base, tid);
12178
12179
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12180
0
  if (name == NULL)
12181
0
    return false;
12182
0
  strcpy (name, buf);
12183
12184
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12185
0
  if (sect == NULL)
12186
0
    return false;
12187
12188
0
  sect->size    = note->descsz;
12189
0
  sect->filepos   = note->descpos;
12190
0
  sect->alignment_power = 2;
12191
12192
  /* This is the current thread.  */
12193
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12194
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12195
12196
0
  return true;
12197
0
}
12198
12199
static bool
12200
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12201
1
{
12202
  /* Every GREG section has a STATUS section before it.  Store the
12203
     tid from the previous call to pass down to the next gregs
12204
     function.  */
12205
1
  static long tid = 1;
12206
12207
1
  switch (note->type)
12208
1
    {
12209
0
    case QNT_CORE_INFO:
12210
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12211
0
    case QNT_CORE_STATUS:
12212
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12213
0
    case QNT_CORE_GREG:
12214
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12215
0
    case QNT_CORE_FPREG:
12216
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12217
1
    default:
12218
1
      return true;
12219
1
    }
12220
1
}
12221
12222
static bool
12223
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12224
3
{
12225
3
  char *name;
12226
3
  asection *sect;
12227
3
  size_t len;
12228
12229
  /* Use note name as section name.  */
12230
3
  len = note->namesz;
12231
3
  name = (char *) bfd_alloc (abfd, len);
12232
3
  if (name == NULL)
12233
0
    return false;
12234
3
  memcpy (name, note->namedata, len);
12235
3
  name[len - 1] = '\0';
12236
12237
3
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12238
3
  if (sect == NULL)
12239
0
    return false;
12240
12241
3
  sect->size    = note->descsz;
12242
3
  sect->filepos   = note->descpos;
12243
3
  sect->alignment_power = 1;
12244
12245
3
  return true;
12246
3
}
12247
12248
/* Function: elfcore_write_note
12249
12250
   Inputs:
12251
     buffer to hold note, and current size of buffer
12252
     name of note
12253
     type of note
12254
     data for note
12255
     size of data for note
12256
12257
   Writes note to end of buffer.  ELF64 notes are written exactly as
12258
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12259
   that they ought to have 8-byte namesz and descsz field, and have
12260
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12261
12262
   Return:
12263
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12264
12265
char *
12266
elfcore_write_note (bfd *abfd,
12267
        char *buf,
12268
        int *bufsiz,
12269
        const char *name,
12270
        int type,
12271
        const void *input,
12272
        int size)
12273
0
{
12274
0
  Elf_External_Note *xnp;
12275
0
  size_t namesz;
12276
0
  size_t newspace;
12277
0
  char *dest;
12278
12279
0
  namesz = 0;
12280
0
  if (name != NULL)
12281
0
    namesz = strlen (name) + 1;
12282
12283
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12284
12285
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12286
0
  if (buf == NULL)
12287
0
    return buf;
12288
0
  dest = buf + *bufsiz;
12289
0
  *bufsiz += newspace;
12290
0
  xnp = (Elf_External_Note *) dest;
12291
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12292
0
  H_PUT_32 (abfd, size, xnp->descsz);
12293
0
  H_PUT_32 (abfd, type, xnp->type);
12294
0
  dest = xnp->name;
12295
0
  if (name != NULL)
12296
0
    {
12297
0
      memcpy (dest, name, namesz);
12298
0
      dest += namesz;
12299
0
      while (namesz & 3)
12300
0
  {
12301
0
    *dest++ = '\0';
12302
0
    ++namesz;
12303
0
  }
12304
0
    }
12305
0
  memcpy (dest, input, size);
12306
0
  dest += size;
12307
0
  while (size & 3)
12308
0
    {
12309
0
      *dest++ = '\0';
12310
0
      ++size;
12311
0
    }
12312
0
  return buf;
12313
0
}
12314
12315
/* gcc-8 warns (*) on all the strncpy calls in this function about
12316
   possible string truncation.  The "truncation" is not a bug.  We
12317
   have an external representation of structs with fields that are not
12318
   necessarily NULL terminated and corresponding internal
12319
   representation fields that are one larger so that they can always
12320
   be NULL terminated.
12321
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12322
   diagnostics inside functions, giving a hard error if you try to use
12323
   the finer control available with later versions.
12324
   gcc prior to 4.2 warns about diagnostic push and pop.
12325
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12326
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12327
   (*) Depending on your system header files!  */
12328
#if GCC_VERSION >= 8000
12329
# pragma GCC diagnostic push
12330
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12331
#endif
12332
char *
12333
elfcore_write_prpsinfo (bfd  *abfd,
12334
      char *buf,
12335
      int  *bufsiz,
12336
      const char *fname,
12337
      const char *psargs)
12338
0
{
12339
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12340
12341
0
  if (bed->elf_backend_write_core_note != NULL)
12342
0
    {
12343
0
      char *ret;
12344
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12345
0
             NT_PRPSINFO, fname, psargs);
12346
0
      if (ret != NULL)
12347
0
  return ret;
12348
0
    }
12349
12350
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12351
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12352
0
  if (bed->s->elfclass == ELFCLASS32)
12353
0
    {
12354
#  if defined (HAVE_PSINFO32_T)
12355
      psinfo32_t data;
12356
      int note_type = NT_PSINFO;
12357
#  else
12358
0
      prpsinfo32_t data;
12359
0
      int note_type = NT_PRPSINFO;
12360
0
#  endif
12361
12362
0
      memset (&data, 0, sizeof (data));
12363
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12364
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12365
0
      return elfcore_write_note (abfd, buf, bufsiz,
12366
0
         "CORE", note_type, &data, sizeof (data));
12367
0
    }
12368
0
  else
12369
0
# endif
12370
0
    {
12371
# if defined (HAVE_PSINFO_T)
12372
      psinfo_t data;
12373
      int note_type = NT_PSINFO;
12374
# else
12375
0
      prpsinfo_t data;
12376
0
      int note_type = NT_PRPSINFO;
12377
0
# endif
12378
12379
0
      memset (&data, 0, sizeof (data));
12380
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12381
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12382
0
      return elfcore_write_note (abfd, buf, bufsiz,
12383
0
         "CORE", note_type, &data, sizeof (data));
12384
0
    }
12385
0
#endif  /* PSINFO_T or PRPSINFO_T */
12386
12387
0
  free (buf);
12388
0
  return NULL;
12389
0
}
12390
#if GCC_VERSION >= 8000
12391
# pragma GCC diagnostic pop
12392
#endif
12393
12394
char *
12395
elfcore_write_linux_prpsinfo32
12396
  (bfd *abfd, char *buf, int *bufsiz,
12397
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12398
0
{
12399
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12400
0
    {
12401
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12402
12403
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12404
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12405
0
         &data, sizeof (data));
12406
0
    }
12407
0
  else
12408
0
    {
12409
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12410
12411
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12412
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12413
0
         &data, sizeof (data));
12414
0
    }
12415
0
}
12416
12417
char *
12418
elfcore_write_linux_prpsinfo64
12419
  (bfd *abfd, char *buf, int *bufsiz,
12420
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12421
0
{
12422
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12423
0
    {
12424
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12425
12426
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12427
0
      return elfcore_write_note (abfd, buf, bufsiz,
12428
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12429
0
    }
12430
0
  else
12431
0
    {
12432
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12433
12434
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12435
0
      return elfcore_write_note (abfd, buf, bufsiz,
12436
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12437
0
    }
12438
0
}
12439
12440
char *
12441
elfcore_write_prstatus (bfd *abfd,
12442
      char *buf,
12443
      int *bufsiz,
12444
      long pid,
12445
      int cursig,
12446
      const void *gregs)
12447
0
{
12448
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12449
12450
0
  if (bed->elf_backend_write_core_note != NULL)
12451
0
    {
12452
0
      char *ret;
12453
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12454
0
             NT_PRSTATUS,
12455
0
             pid, cursig, gregs);
12456
0
      if (ret != NULL)
12457
0
  return ret;
12458
0
    }
12459
12460
0
#if defined (HAVE_PRSTATUS_T)
12461
0
#if defined (HAVE_PRSTATUS32_T)
12462
0
  if (bed->s->elfclass == ELFCLASS32)
12463
0
    {
12464
0
      prstatus32_t prstat;
12465
12466
0
      memset (&prstat, 0, sizeof (prstat));
12467
0
      prstat.pr_pid = pid;
12468
0
      prstat.pr_cursig = cursig;
12469
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12470
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12471
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12472
0
    }
12473
0
  else
12474
0
#endif
12475
0
    {
12476
0
      prstatus_t prstat;
12477
12478
0
      memset (&prstat, 0, sizeof (prstat));
12479
0
      prstat.pr_pid = pid;
12480
0
      prstat.pr_cursig = cursig;
12481
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12482
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12483
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12484
0
    }
12485
0
#endif /* HAVE_PRSTATUS_T */
12486
12487
0
  free (buf);
12488
0
  return NULL;
12489
0
}
12490
12491
#if defined (HAVE_LWPSTATUS_T)
12492
char *
12493
elfcore_write_lwpstatus (bfd *abfd,
12494
       char *buf,
12495
       int *bufsiz,
12496
       long pid,
12497
       int cursig,
12498
       const void *gregs)
12499
{
12500
  lwpstatus_t lwpstat;
12501
  const char *note_name = "CORE";
12502
12503
  memset (&lwpstat, 0, sizeof (lwpstat));
12504
  lwpstat.pr_lwpid  = pid >> 16;
12505
  lwpstat.pr_cursig = cursig;
12506
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12507
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12508
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12509
#if !defined(gregs)
12510
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12511
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12512
#else
12513
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12514
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12515
#endif
12516
#endif
12517
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
12518
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12519
}
12520
#endif /* HAVE_LWPSTATUS_T */
12521
12522
#if defined (HAVE_PSTATUS_T)
12523
char *
12524
elfcore_write_pstatus (bfd *abfd,
12525
           char *buf,
12526
           int *bufsiz,
12527
           long pid,
12528
           int cursig ATTRIBUTE_UNUSED,
12529
           const void *gregs ATTRIBUTE_UNUSED)
12530
{
12531
  const char *note_name = "CORE";
12532
#if defined (HAVE_PSTATUS32_T)
12533
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12534
12535
  if (bed->s->elfclass == ELFCLASS32)
12536
    {
12537
      pstatus32_t pstat;
12538
12539
      memset (&pstat, 0, sizeof (pstat));
12540
      pstat.pr_pid = pid & 0xffff;
12541
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12542
        NT_PSTATUS, &pstat, sizeof (pstat));
12543
      return buf;
12544
    }
12545
  else
12546
#endif
12547
    {
12548
      pstatus_t pstat;
12549
12550
      memset (&pstat, 0, sizeof (pstat));
12551
      pstat.pr_pid = pid & 0xffff;
12552
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12553
        NT_PSTATUS, &pstat, sizeof (pstat));
12554
      return buf;
12555
    }
12556
}
12557
#endif /* HAVE_PSTATUS_T */
12558
12559
char *
12560
elfcore_write_prfpreg (bfd *abfd,
12561
           char *buf,
12562
           int *bufsiz,
12563
           const void *fpregs,
12564
           int size)
12565
0
{
12566
0
  const char *note_name = "CORE";
12567
0
  return elfcore_write_note (abfd, buf, bufsiz,
12568
0
           note_name, NT_FPREGSET, fpregs, size);
12569
0
}
12570
12571
char *
12572
elfcore_write_prxfpreg (bfd *abfd,
12573
      char *buf,
12574
      int *bufsiz,
12575
      const void *xfpregs,
12576
      int size)
12577
0
{
12578
0
  char *note_name = "LINUX";
12579
0
  return elfcore_write_note (abfd, buf, bufsiz,
12580
0
           note_name, NT_PRXFPREG, xfpregs, size);
12581
0
}
12582
12583
char *
12584
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12585
       const void *xfpregs, int size)
12586
0
{
12587
0
  char *note_name;
12588
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12589
0
    note_name = "FreeBSD";
12590
0
  else
12591
0
    note_name = "LINUX";
12592
0
  return elfcore_write_note (abfd, buf, bufsiz,
12593
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12594
0
}
12595
12596
static char *
12597
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12598
          const void *ssp, int size)
12599
0
{
12600
0
  const char *note_name = "LINUX";
12601
0
  return elfcore_write_note (abfd, buf, bufsiz,
12602
0
           note_name, NT_X86_SHSTK, ssp, size);
12603
0
}
12604
12605
char *
12606
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12607
          const void *regs, int size)
12608
0
{
12609
0
  char *note_name = "FreeBSD";
12610
0
  return elfcore_write_note (abfd, buf, bufsiz,
12611
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12612
0
}
12613
12614
char *
12615
elfcore_write_ppc_vmx (bfd *abfd,
12616
           char *buf,
12617
           int *bufsiz,
12618
           const void *ppc_vmx,
12619
           int size)
12620
0
{
12621
0
  char *note_name = "LINUX";
12622
0
  return elfcore_write_note (abfd, buf, bufsiz,
12623
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
12624
0
}
12625
12626
char *
12627
elfcore_write_ppc_vsx (bfd *abfd,
12628
           char *buf,
12629
           int *bufsiz,
12630
           const void *ppc_vsx,
12631
           int size)
12632
0
{
12633
0
  char *note_name = "LINUX";
12634
0
  return elfcore_write_note (abfd, buf, bufsiz,
12635
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
12636
0
}
12637
12638
char *
12639
elfcore_write_ppc_tar (bfd *abfd,
12640
           char *buf,
12641
           int *bufsiz,
12642
           const void *ppc_tar,
12643
           int size)
12644
0
{
12645
0
  char *note_name = "LINUX";
12646
0
  return elfcore_write_note (abfd, buf, bufsiz,
12647
0
           note_name, NT_PPC_TAR, ppc_tar, size);
12648
0
}
12649
12650
char *
12651
elfcore_write_ppc_ppr (bfd *abfd,
12652
           char *buf,
12653
           int *bufsiz,
12654
           const void *ppc_ppr,
12655
           int size)
12656
0
{
12657
0
  char *note_name = "LINUX";
12658
0
  return elfcore_write_note (abfd, buf, bufsiz,
12659
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12660
0
}
12661
12662
char *
12663
elfcore_write_ppc_dscr (bfd *abfd,
12664
      char *buf,
12665
      int *bufsiz,
12666
      const void *ppc_dscr,
12667
      int size)
12668
0
{
12669
0
  char *note_name = "LINUX";
12670
0
  return elfcore_write_note (abfd, buf, bufsiz,
12671
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12672
0
}
12673
12674
char *
12675
elfcore_write_ppc_ebb (bfd *abfd,
12676
           char *buf,
12677
           int *bufsiz,
12678
           const void *ppc_ebb,
12679
           int size)
12680
0
{
12681
0
  char *note_name = "LINUX";
12682
0
  return elfcore_write_note (abfd, buf, bufsiz,
12683
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12684
0
}
12685
12686
char *
12687
elfcore_write_ppc_pmu (bfd *abfd,
12688
           char *buf,
12689
           int *bufsiz,
12690
           const void *ppc_pmu,
12691
           int size)
12692
0
{
12693
0
  char *note_name = "LINUX";
12694
0
  return elfcore_write_note (abfd, buf, bufsiz,
12695
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12696
0
}
12697
12698
char *
12699
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12700
         char *buf,
12701
         int *bufsiz,
12702
         const void *ppc_tm_cgpr,
12703
         int size)
12704
0
{
12705
0
  char *note_name = "LINUX";
12706
0
  return elfcore_write_note (abfd, buf, bufsiz,
12707
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12708
0
}
12709
12710
char *
12711
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12712
         char *buf,
12713
         int *bufsiz,
12714
         const void *ppc_tm_cfpr,
12715
         int size)
12716
0
{
12717
0
  char *note_name = "LINUX";
12718
0
  return elfcore_write_note (abfd, buf, bufsiz,
12719
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12720
0
}
12721
12722
char *
12723
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12724
         char *buf,
12725
         int *bufsiz,
12726
         const void *ppc_tm_cvmx,
12727
         int size)
12728
0
{
12729
0
  char *note_name = "LINUX";
12730
0
  return elfcore_write_note (abfd, buf, bufsiz,
12731
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12732
0
}
12733
12734
char *
12735
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12736
         char *buf,
12737
         int *bufsiz,
12738
         const void *ppc_tm_cvsx,
12739
         int size)
12740
0
{
12741
0
  char *note_name = "LINUX";
12742
0
  return elfcore_write_note (abfd, buf, bufsiz,
12743
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12744
0
}
12745
12746
char *
12747
elfcore_write_ppc_tm_spr (bfd *abfd,
12748
        char *buf,
12749
        int *bufsiz,
12750
        const void *ppc_tm_spr,
12751
        int size)
12752
0
{
12753
0
  char *note_name = "LINUX";
12754
0
  return elfcore_write_note (abfd, buf, bufsiz,
12755
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12756
0
}
12757
12758
char *
12759
elfcore_write_ppc_tm_ctar (bfd *abfd,
12760
         char *buf,
12761
         int *bufsiz,
12762
         const void *ppc_tm_ctar,
12763
         int size)
12764
0
{
12765
0
  char *note_name = "LINUX";
12766
0
  return elfcore_write_note (abfd, buf, bufsiz,
12767
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12768
0
}
12769
12770
char *
12771
elfcore_write_ppc_tm_cppr (bfd *abfd,
12772
         char *buf,
12773
         int *bufsiz,
12774
         const void *ppc_tm_cppr,
12775
         int size)
12776
0
{
12777
0
  char *note_name = "LINUX";
12778
0
  return elfcore_write_note (abfd, buf, bufsiz,
12779
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12780
0
}
12781
12782
char *
12783
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12784
          char *buf,
12785
          int *bufsiz,
12786
          const void *ppc_tm_cdscr,
12787
          int size)
12788
0
{
12789
0
  char *note_name = "LINUX";
12790
0
  return elfcore_write_note (abfd, buf, bufsiz,
12791
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12792
0
}
12793
12794
static char *
12795
elfcore_write_s390_high_gprs (bfd *abfd,
12796
            char *buf,
12797
            int *bufsiz,
12798
            const void *s390_high_gprs,
12799
            int size)
12800
0
{
12801
0
  char *note_name = "LINUX";
12802
0
  return elfcore_write_note (abfd, buf, bufsiz,
12803
0
           note_name, NT_S390_HIGH_GPRS,
12804
0
           s390_high_gprs, size);
12805
0
}
12806
12807
char *
12808
elfcore_write_s390_timer (bfd *abfd,
12809
        char *buf,
12810
        int *bufsiz,
12811
        const void *s390_timer,
12812
        int size)
12813
0
{
12814
0
  char *note_name = "LINUX";
12815
0
  return elfcore_write_note (abfd, buf, bufsiz,
12816
0
           note_name, NT_S390_TIMER, s390_timer, size);
12817
0
}
12818
12819
char *
12820
elfcore_write_s390_todcmp (bfd *abfd,
12821
         char *buf,
12822
         int *bufsiz,
12823
         const void *s390_todcmp,
12824
         int size)
12825
0
{
12826
0
  char *note_name = "LINUX";
12827
0
  return elfcore_write_note (abfd, buf, bufsiz,
12828
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12829
0
}
12830
12831
char *
12832
elfcore_write_s390_todpreg (bfd *abfd,
12833
          char *buf,
12834
          int *bufsiz,
12835
          const void *s390_todpreg,
12836
          int size)
12837
0
{
12838
0
  char *note_name = "LINUX";
12839
0
  return elfcore_write_note (abfd, buf, bufsiz,
12840
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12841
0
}
12842
12843
char *
12844
elfcore_write_s390_ctrs (bfd *abfd,
12845
       char *buf,
12846
       int *bufsiz,
12847
       const void *s390_ctrs,
12848
       int size)
12849
0
{
12850
0
  char *note_name = "LINUX";
12851
0
  return elfcore_write_note (abfd, buf, bufsiz,
12852
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12853
0
}
12854
12855
char *
12856
elfcore_write_s390_prefix (bfd *abfd,
12857
         char *buf,
12858
         int *bufsiz,
12859
         const void *s390_prefix,
12860
         int size)
12861
0
{
12862
0
  char *note_name = "LINUX";
12863
0
  return elfcore_write_note (abfd, buf, bufsiz,
12864
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12865
0
}
12866
12867
char *
12868
elfcore_write_s390_last_break (bfd *abfd,
12869
             char *buf,
12870
             int *bufsiz,
12871
             const void *s390_last_break,
12872
             int size)
12873
0
{
12874
0
  char *note_name = "LINUX";
12875
0
  return elfcore_write_note (abfd, buf, bufsiz,
12876
0
           note_name, NT_S390_LAST_BREAK,
12877
0
           s390_last_break, size);
12878
0
}
12879
12880
char *
12881
elfcore_write_s390_system_call (bfd *abfd,
12882
        char *buf,
12883
        int *bufsiz,
12884
        const void *s390_system_call,
12885
        int size)
12886
0
{
12887
0
  char *note_name = "LINUX";
12888
0
  return elfcore_write_note (abfd, buf, bufsiz,
12889
0
           note_name, NT_S390_SYSTEM_CALL,
12890
0
           s390_system_call, size);
12891
0
}
12892
12893
char *
12894
elfcore_write_s390_tdb (bfd *abfd,
12895
      char *buf,
12896
      int *bufsiz,
12897
      const void *s390_tdb,
12898
      int size)
12899
0
{
12900
0
  char *note_name = "LINUX";
12901
0
  return elfcore_write_note (abfd, buf, bufsiz,
12902
0
           note_name, NT_S390_TDB, s390_tdb, size);
12903
0
}
12904
12905
char *
12906
elfcore_write_s390_vxrs_low (bfd *abfd,
12907
           char *buf,
12908
           int *bufsiz,
12909
           const void *s390_vxrs_low,
12910
           int size)
12911
0
{
12912
0
  char *note_name = "LINUX";
12913
0
  return elfcore_write_note (abfd, buf, bufsiz,
12914
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12915
0
}
12916
12917
char *
12918
elfcore_write_s390_vxrs_high (bfd *abfd,
12919
           char *buf,
12920
           int *bufsiz,
12921
           const void *s390_vxrs_high,
12922
           int size)
12923
0
{
12924
0
  char *note_name = "LINUX";
12925
0
  return elfcore_write_note (abfd, buf, bufsiz,
12926
0
           note_name, NT_S390_VXRS_HIGH,
12927
0
           s390_vxrs_high, size);
12928
0
}
12929
12930
char *
12931
elfcore_write_s390_gs_cb (bfd *abfd,
12932
        char *buf,
12933
        int *bufsiz,
12934
        const void *s390_gs_cb,
12935
        int size)
12936
0
{
12937
0
  char *note_name = "LINUX";
12938
0
  return elfcore_write_note (abfd, buf, bufsiz,
12939
0
           note_name, NT_S390_GS_CB,
12940
0
           s390_gs_cb, size);
12941
0
}
12942
12943
char *
12944
elfcore_write_s390_gs_bc (bfd *abfd,
12945
        char *buf,
12946
        int *bufsiz,
12947
        const void *s390_gs_bc,
12948
        int size)
12949
0
{
12950
0
  char *note_name = "LINUX";
12951
0
  return elfcore_write_note (abfd, buf, bufsiz,
12952
0
           note_name, NT_S390_GS_BC,
12953
0
           s390_gs_bc, size);
12954
0
}
12955
12956
char *
12957
elfcore_write_arm_vfp (bfd *abfd,
12958
           char *buf,
12959
           int *bufsiz,
12960
           const void *arm_vfp,
12961
           int size)
12962
0
{
12963
0
  char *note_name = "LINUX";
12964
0
  return elfcore_write_note (abfd, buf, bufsiz,
12965
0
           note_name, NT_ARM_VFP, arm_vfp, size);
12966
0
}
12967
12968
char *
12969
elfcore_write_aarch_tls (bfd *abfd,
12970
           char *buf,
12971
           int *bufsiz,
12972
           const void *aarch_tls,
12973
           int size)
12974
0
{
12975
0
  char *note_name = "LINUX";
12976
0
  return elfcore_write_note (abfd, buf, bufsiz,
12977
0
           note_name, NT_ARM_TLS, aarch_tls, size);
12978
0
}
12979
12980
char *
12981
elfcore_write_aarch_hw_break (bfd *abfd,
12982
          char *buf,
12983
          int *bufsiz,
12984
          const void *aarch_hw_break,
12985
          int size)
12986
0
{
12987
0
  char *note_name = "LINUX";
12988
0
  return elfcore_write_note (abfd, buf, bufsiz,
12989
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12990
0
}
12991
12992
char *
12993
elfcore_write_aarch_hw_watch (bfd *abfd,
12994
          char *buf,
12995
          int *bufsiz,
12996
          const void *aarch_hw_watch,
12997
          int size)
12998
0
{
12999
0
  char *note_name = "LINUX";
13000
0
  return elfcore_write_note (abfd, buf, bufsiz,
13001
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13002
0
}
13003
13004
char *
13005
elfcore_write_aarch_sve (bfd *abfd,
13006
       char *buf,
13007
       int *bufsiz,
13008
       const void *aarch_sve,
13009
       int size)
13010
0
{
13011
0
  char *note_name = "LINUX";
13012
0
  return elfcore_write_note (abfd, buf, bufsiz,
13013
0
           note_name, NT_ARM_SVE, aarch_sve, size);
13014
0
}
13015
13016
char *
13017
elfcore_write_aarch_pauth (bfd *abfd,
13018
         char *buf,
13019
         int *bufsiz,
13020
         const void *aarch_pauth,
13021
         int size)
13022
0
{
13023
0
  char *note_name = "LINUX";
13024
0
  return elfcore_write_note (abfd, buf, bufsiz,
13025
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13026
0
}
13027
13028
char *
13029
elfcore_write_aarch_mte (bfd *abfd,
13030
              char *buf,
13031
              int *bufsiz,
13032
              const void *aarch_mte,
13033
              int size)
13034
0
{
13035
0
  char *note_name = "LINUX";
13036
0
  return elfcore_write_note (abfd, buf, bufsiz,
13037
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
13038
0
           aarch_mte,
13039
0
           size);
13040
0
}
13041
13042
char *
13043
elfcore_write_aarch_ssve (bfd *abfd,
13044
        char *buf,
13045
        int *bufsiz,
13046
        const void *aarch_ssve,
13047
        int size)
13048
0
{
13049
0
  char *note_name = "LINUX";
13050
0
  return elfcore_write_note (abfd, buf, bufsiz,
13051
0
           note_name, NT_ARM_SSVE,
13052
0
           aarch_ssve,
13053
0
           size);
13054
0
}
13055
13056
char *
13057
elfcore_write_aarch_za (bfd *abfd,
13058
      char *buf,
13059
      int *bufsiz,
13060
      const void *aarch_za,
13061
      int size)
13062
0
{
13063
0
  char *note_name = "LINUX";
13064
0
  return elfcore_write_note (abfd, buf, bufsiz,
13065
0
           note_name, NT_ARM_ZA,
13066
0
           aarch_za,
13067
0
           size);
13068
0
}
13069
13070
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
13071
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13072
   written into.  Return a pointer to the new start of the note buffer, to
13073
   replace BUF which may no longer be valid.  */
13074
13075
char *
13076
elfcore_write_aarch_zt (bfd *abfd,
13077
      char *buf,
13078
      int *bufsiz,
13079
      const void *aarch_zt,
13080
      int size)
13081
0
{
13082
0
  char *note_name = "LINUX";
13083
0
  return elfcore_write_note (abfd, buf, bufsiz,
13084
0
           note_name, NT_ARM_ZT,
13085
0
           aarch_zt,
13086
0
           size);
13087
0
}
13088
13089
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
13090
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13091
   written into.  Return a pointer to the new start of the note buffer, to
13092
   replace BUF which may no longer be valid.  */
13093
13094
static char *
13095
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
13096
       const void *aarch_gcs, int size)
13097
0
{
13098
0
  const char *note_name = "LINUX";
13099
0
  return elfcore_write_note (abfd, buf, bufsiz, note_name, NT_ARM_GCS,
13100
0
           aarch_gcs, size);
13101
0
}
13102
13103
char *
13104
elfcore_write_arc_v2 (bfd *abfd,
13105
          char *buf,
13106
          int *bufsiz,
13107
          const void *arc_v2,
13108
          int size)
13109
0
{
13110
0
  char *note_name = "LINUX";
13111
0
  return elfcore_write_note (abfd, buf, bufsiz,
13112
0
           note_name, NT_ARC_V2, arc_v2, size);
13113
0
}
13114
13115
char *
13116
elfcore_write_loongarch_cpucfg (bfd *abfd,
13117
        char *buf,
13118
        int *bufsiz,
13119
        const void *loongarch_cpucfg,
13120
        int size)
13121
0
{
13122
0
  char *note_name = "LINUX";
13123
0
  return elfcore_write_note (abfd, buf, bufsiz,
13124
0
           note_name, NT_LARCH_CPUCFG,
13125
0
           loongarch_cpucfg, size);
13126
0
}
13127
13128
char *
13129
elfcore_write_loongarch_lbt (bfd *abfd,
13130
           char *buf,
13131
           int *bufsiz,
13132
           const void *loongarch_lbt,
13133
           int size)
13134
0
{
13135
0
  char *note_name = "LINUX";
13136
0
  return elfcore_write_note (abfd, buf, bufsiz,
13137
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
13138
0
}
13139
13140
char *
13141
elfcore_write_loongarch_lsx (bfd *abfd,
13142
           char *buf,
13143
           int *bufsiz,
13144
           const void *loongarch_lsx,
13145
           int size)
13146
0
{
13147
0
  char *note_name = "LINUX";
13148
0
  return elfcore_write_note (abfd, buf, bufsiz,
13149
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
13150
0
}
13151
13152
char *
13153
elfcore_write_loongarch_lasx (bfd *abfd,
13154
            char *buf,
13155
            int *bufsiz,
13156
            const void *loongarch_lasx,
13157
            int size)
13158
0
{
13159
0
  char *note_name = "LINUX";
13160
0
  return elfcore_write_note (abfd, buf, bufsiz,
13161
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
13162
0
}
13163
13164
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13165
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13166
   written into.  Return a pointer to the new start of the note buffer, to
13167
   replace BUF which may no longer be valid.  */
13168
13169
char *
13170
elfcore_write_riscv_csr (bfd *abfd,
13171
       char *buf,
13172
       int *bufsiz,
13173
       const void *csrs,
13174
       int size)
13175
0
{
13176
0
  const char *note_name = "GDB";
13177
0
  return elfcore_write_note (abfd, buf, bufsiz,
13178
0
           note_name, NT_RISCV_CSR, csrs, size);
13179
0
}
13180
13181
/* Write the target description (a string) pointed to by TDESC, length
13182
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13183
   note is being written into.  Return a pointer to the new start of the
13184
   note buffer, to replace BUF which may no longer be valid.  */
13185
13186
char *
13187
elfcore_write_gdb_tdesc (bfd *abfd,
13188
       char *buf,
13189
       int *bufsiz,
13190
       const void *tdesc,
13191
       int size)
13192
0
{
13193
0
  const char *note_name = "GDB";
13194
0
  return elfcore_write_note (abfd, buf, bufsiz,
13195
0
           note_name, NT_GDB_TDESC, tdesc, size);
13196
0
}
13197
13198
char *
13199
elfcore_write_register_note (bfd *abfd,
13200
           char *buf,
13201
           int *bufsiz,
13202
           const char *section,
13203
           const void *data,
13204
           int size)
13205
0
{
13206
0
  if (strcmp (section, ".reg2") == 0)
13207
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13208
0
  if (strcmp (section, ".reg-xfp") == 0)
13209
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13210
0
  if (strcmp (section, ".reg-xstate") == 0)
13211
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13212
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
13213
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13214
0
  if (strcmp (section, ".reg-ssp") == 0)
13215
0
    return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
13216
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
13217
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13218
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
13219
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13220
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
13221
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13222
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
13223
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13224
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
13225
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13226
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
13227
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13228
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
13229
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13230
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13231
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13232
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13233
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13234
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13235
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13236
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13237
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13238
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13239
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13240
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13241
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13242
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13243
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13244
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13245
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13246
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
13247
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13248
0
  if (strcmp (section, ".reg-s390-timer") == 0)
13249
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13250
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
13251
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13252
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
13253
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13254
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
13255
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13256
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
13257
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13258
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
13259
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13260
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
13261
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13262
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
13263
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13264
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13265
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13266
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13267
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13268
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
13269
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13270
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
13271
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13272
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
13273
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13274
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
13275
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13276
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
13277
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13278
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13279
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13280
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
13281
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13282
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
13283
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13284
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
13285
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13286
0
  if (strcmp (section, ".reg-aarch-ssve") == 0)
13287
0
    return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13288
0
  if (strcmp (section, ".reg-aarch-za") == 0)
13289
0
    return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13290
0
  if (strcmp (section, ".reg-aarch-zt") == 0)
13291
0
    return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13292
0
  if (strcmp (section, ".reg-aarch-gcs") == 0)
13293
0
    return elfcore_write_aarch_gcs (abfd, buf, bufsiz, data, size);
13294
0
  if (strcmp (section, ".reg-arc-v2") == 0)
13295
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13296
0
  if (strcmp (section, ".gdb-tdesc") == 0)
13297
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13298
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
13299
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13300
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13301
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13302
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
13303
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13304
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
13305
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13306
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
13307
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13308
0
  return NULL;
13309
0
}
13310
13311
char *
13312
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13313
       const void *buf, int bufsiz)
13314
0
{
13315
0
  return elfcore_write_note (obfd, note_data, note_size,
13316
0
           "CORE", NT_FILE, buf, bufsiz);
13317
0
}
13318
13319
static bool
13320
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13321
     size_t align)
13322
57.8k
{
13323
57.8k
  char *p;
13324
13325
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13326
     gABI specifies that PT_NOTE alignment should be aligned to 4
13327
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13328
     align is less than 4, we use 4 byte alignment.   */
13329
57.8k
  if (align < 4)
13330
19.3k
    align = 4;
13331
57.8k
  if (align != 4 && align != 8)
13332
23.5k
    return false;
13333
13334
34.2k
  p = buf;
13335
105k
  while (p < buf + size)
13336
100k
    {
13337
100k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13338
100k
      Elf_Internal_Note in;
13339
13340
100k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13341
9.51k
  return false;
13342
13343
91.4k
      in.type = H_GET_32 (abfd, xnp->type);
13344
13345
91.4k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13346
91.4k
      in.namedata = xnp->name;
13347
91.4k
      if (in.namesz > buf - in.namedata + size)
13348
12.5k
  return false;
13349
13350
78.8k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13351
78.8k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13352
78.8k
      in.descpos = offset + (in.descdata - buf);
13353
78.8k
      if (in.descsz != 0
13354
78.8k
    && (in.descdata >= buf + size
13355
38.2k
        || in.descsz > buf - in.descdata + size))
13356
7.15k
  return false;
13357
13358
71.7k
      switch (bfd_get_format (abfd))
13359
71.7k
  {
13360
0
  default:
13361
0
    return true;
13362
13363
1.38k
  case bfd_core:
13364
1.38k
    {
13365
11.0k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13366
1.38k
      struct
13367
1.38k
      {
13368
1.38k
        const char * string;
13369
1.38k
        size_t len;
13370
1.38k
        bool (*func) (bfd *, Elf_Internal_Note *);
13371
1.38k
      }
13372
1.38k
      grokers[] =
13373
1.38k
      {
13374
1.38k
        GROKER_ELEMENT ("", elfcore_grok_note),
13375
1.38k
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13376
1.38k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13377
1.38k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13378
1.38k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13379
1.38k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13380
1.38k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13381
1.38k
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13382
1.38k
      };
13383
1.38k
#undef GROKER_ELEMENT
13384
1.38k
      int i;
13385
13386
10.1k
      for (i = ARRAY_SIZE (grokers); i--;)
13387
10.1k
        {
13388
10.1k
    if (in.namesz >= grokers[i].len
13389
10.1k
        && strncmp (in.namedata, grokers[i].string,
13390
3.57k
        grokers[i].len) == 0)
13391
1.38k
      {
13392
1.38k
        if (! grokers[i].func (abfd, & in))
13393
11
          return false;
13394
1.36k
        break;
13395
1.38k
      }
13396
10.1k
        }
13397
1.36k
      break;
13398
1.38k
    }
13399
13400
70.3k
  case bfd_object:
13401
70.3k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13402
2.32k
      {
13403
2.32k
        if (! elfobj_grok_gnu_note (abfd, &in))
13404
84
    return false;
13405
2.32k
      }
13406
68.0k
    else if (in.namesz == sizeof "stapsdt"
13407
68.0k
       && strcmp (in.namedata, "stapsdt") == 0)
13408
0
      {
13409
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13410
0
    return false;
13411
0
      }
13412
70.2k
    break;
13413
71.7k
  }
13414
13415
71.6k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13416
71.6k
    }
13417
13418
4.95k
  return true;
13419
34.2k
}
13420
13421
bool
13422
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13423
    size_t align)
13424
4.14k
{
13425
4.14k
  char *buf;
13426
13427
4.14k
  if (size == 0 || (size + 1) == 0)
13428
310
    return true;
13429
13430
3.83k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13431
667
    return false;
13432
13433
3.16k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13434
3.16k
  if (buf == NULL)
13435
1.24k
    return false;
13436
13437
  /* PR 17512: file: ec08f814
13438
     0-termintate the buffer so that string searches will not overflow.  */
13439
1.92k
  buf[size] = 0;
13440
13441
1.92k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13442
1.78k
    {
13443
1.78k
      free (buf);
13444
1.78k
      return false;
13445
1.78k
    }
13446
13447
134
  free (buf);
13448
134
  return true;
13449
1.92k
}
13450

13451
/* Providing external access to the ELF program header table.  */
13452
13453
/* Return an upper bound on the number of bytes required to store a
13454
   copy of ABFD's program header table entries.  Return -1 if an error
13455
   occurs; bfd_get_error will return an appropriate code.  */
13456
13457
long
13458
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13459
0
{
13460
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13461
0
    {
13462
0
      bfd_set_error (bfd_error_wrong_format);
13463
0
      return -1;
13464
0
    }
13465
13466
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13467
0
}
13468
13469
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13470
   will be stored as an array of Elf_Internal_Phdr structures, as
13471
   defined in include/elf/internal.h.  To find out how large the
13472
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13473
13474
   Return the number of program header table entries read, or -1 if an
13475
   error occurs; bfd_get_error will return an appropriate code.  */
13476
13477
int
13478
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13479
0
{
13480
0
  int num_phdrs;
13481
13482
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13483
0
    {
13484
0
      bfd_set_error (bfd_error_wrong_format);
13485
0
      return -1;
13486
0
    }
13487
13488
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13489
0
  if (num_phdrs != 0)
13490
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13491
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13492
13493
0
  return num_phdrs;
13494
0
}
13495
13496
enum elf_reloc_type_class
13497
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13498
         const asection *rel_sec ATTRIBUTE_UNUSED,
13499
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13500
0
{
13501
0
  return reloc_class_normal;
13502
0
}
13503
13504
/* For RELA architectures, return the relocation value for a
13505
   relocation against a local symbol.  */
13506
13507
bfd_vma
13508
_bfd_elf_rela_local_sym (bfd *abfd,
13509
       Elf_Internal_Sym *sym,
13510
       asection **psec,
13511
       Elf_Internal_Rela *rel)
13512
0
{
13513
0
  asection *sec = *psec;
13514
0
  bfd_vma relocation;
13515
13516
0
  relocation = (sec->output_section->vma
13517
0
    + sec->output_offset
13518
0
    + sym->st_value);
13519
0
  if ((sec->flags & SEC_MERGE)
13520
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13521
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13522
0
    {
13523
0
      rel->r_addend =
13524
0
  _bfd_merged_section_offset (abfd, psec,
13525
0
            elf_section_data (sec)->sec_info,
13526
0
            sym->st_value + rel->r_addend);
13527
0
      if (sec != *psec)
13528
0
  {
13529
    /* If we have changed the section, and our original section is
13530
       marked with SEC_EXCLUDE, it means that the original
13531
       SEC_MERGE section has been completely subsumed in some
13532
       other SEC_MERGE section.  In this case, we need to leave
13533
       some info around for --emit-relocs.  */
13534
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13535
0
      sec->kept_section = *psec;
13536
0
    sec = *psec;
13537
0
  }
13538
0
      rel->r_addend -= relocation;
13539
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13540
0
    }
13541
0
  return relocation;
13542
0
}
13543
13544
bfd_vma
13545
_bfd_elf_rel_local_sym (bfd *abfd,
13546
      Elf_Internal_Sym *sym,
13547
      asection **psec,
13548
      bfd_vma addend)
13549
0
{
13550
0
  asection *sec = *psec;
13551
13552
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13553
0
    return sym->st_value + addend;
13554
13555
0
  return _bfd_merged_section_offset (abfd, psec,
13556
0
             elf_section_data (sec)->sec_info,
13557
0
             sym->st_value + addend);
13558
0
}
13559
13560
/* Adjust an address within a section.  Given OFFSET within SEC, return
13561
   the new offset within the section, based upon changes made to the
13562
   section.  Returns -1 if the offset is now invalid.
13563
   The offset (in abnd out) is in target sized bytes, however big a
13564
   byte may be.  */
13565
13566
bfd_vma
13567
_bfd_elf_section_offset (bfd *abfd,
13568
       struct bfd_link_info *info,
13569
       asection *sec,
13570
       bfd_vma offset)
13571
0
{
13572
0
  switch (sec->sec_info_type)
13573
0
    {
13574
0
    case SEC_INFO_TYPE_STABS:
13575
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13576
0
               offset);
13577
0
    case SEC_INFO_TYPE_EH_FRAME:
13578
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13579
13580
0
    case SEC_INFO_TYPE_SFRAME:
13581
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13582
13583
0
    default:
13584
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13585
0
  {
13586
    /* Reverse the offset.  */
13587
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13588
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13589
13590
    /* address_size and sec->size are in octets.  Convert
13591
       to bytes before subtracting the original offset.  */
13592
0
    offset = ((sec->size - address_size)
13593
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13594
0
  }
13595
0
      return offset;
13596
0
    }
13597
0
}
13598

13599
long
13600
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13601
             long symcount ATTRIBUTE_UNUSED,
13602
             asymbol **syms ATTRIBUTE_UNUSED,
13603
             long dynsymcount,
13604
             asymbol **dynsyms,
13605
             asymbol **ret)
13606
2.14k
{
13607
2.14k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13608
2.14k
  asection *relplt;
13609
2.14k
  asymbol *s;
13610
2.14k
  const char *relplt_name;
13611
2.14k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13612
2.14k
  arelent *p;
13613
2.14k
  long count, i, n;
13614
2.14k
  size_t size;
13615
2.14k
  Elf_Internal_Shdr *hdr;
13616
2.14k
  char *names;
13617
2.14k
  asection *plt;
13618
13619
2.14k
  *ret = NULL;
13620
13621
2.14k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13622
1.98k
    return 0;
13623
13624
166
  if (dynsymcount <= 0)
13625
61
    return 0;
13626
13627
105
  if (!bed->plt_sym_val)
13628
32
    return 0;
13629
13630
73
  relplt_name = bed->relplt_name;
13631
73
  if (relplt_name == NULL)
13632
73
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13633
73
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13634
73
  if (relplt == NULL)
13635
3
    return 0;
13636
13637
70
  hdr = &elf_section_data (relplt)->this_hdr;
13638
70
  if (hdr->sh_link != elf_dynsymtab (abfd)
13639
70
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13640
0
    return 0;
13641
13642
70
  plt = bfd_get_section_by_name (abfd, ".plt");
13643
70
  if (plt == NULL)
13644
0
    return 0;
13645
13646
70
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13647
70
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13648
1
    return -1;
13649
13650
69
  count = NUM_SHDR_ENTRIES (hdr);
13651
69
  size = count * sizeof (asymbol);
13652
69
  p = relplt->relocation;
13653
13.2k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13654
13.2k
    {
13655
13.2k
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13656
13.2k
      if (p->addend != 0)
13657
0
  {
13658
0
#ifdef BFD64
13659
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13660
#else
13661
    size += sizeof ("+0x") - 1 + 8;
13662
#endif
13663
0
  }
13664
13.2k
    }
13665
13666
69
  s = *ret = (asymbol *) bfd_malloc (size);
13667
69
  if (s == NULL)
13668
0
    return -1;
13669
13670
69
  names = (char *) (s + count);
13671
69
  p = relplt->relocation;
13672
69
  n = 0;
13673
13.2k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13674
13.2k
    {
13675
13.2k
      size_t len;
13676
13.2k
      bfd_vma addr;
13677
13678
13.2k
      addr = bed->plt_sym_val (i, plt, p);
13679
13.2k
      if (addr == (bfd_vma) -1)
13680
0
  continue;
13681
13682
13.2k
      *s = **p->sym_ptr_ptr;
13683
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13684
   we are defining a symbol, ensure one of them is set.  */
13685
13.2k
      if ((s->flags & BSF_LOCAL) == 0)
13686
13.2k
  s->flags |= BSF_GLOBAL;
13687
13.2k
      s->flags |= BSF_SYNTHETIC;
13688
13.2k
      s->section = plt;
13689
13.2k
      s->value = addr - plt->vma;
13690
13.2k
      s->name = names;
13691
13.2k
      s->udata.p = NULL;
13692
13.2k
      len = strlen ((*p->sym_ptr_ptr)->name);
13693
13.2k
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13694
13.2k
      names += len;
13695
13.2k
      if (p->addend != 0)
13696
0
  {
13697
0
    char buf[30], *a;
13698
13699
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13700
0
    names += sizeof ("+0x") - 1;
13701
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13702
0
    for (a = buf; *a == '0'; ++a)
13703
0
      ;
13704
0
    len = strlen (a);
13705
0
    memcpy (names, a, len);
13706
0
    names += len;
13707
0
  }
13708
13.2k
      memcpy (names, "@plt", sizeof ("@plt"));
13709
13.2k
      names += sizeof ("@plt");
13710
13.2k
      ++s, ++n;
13711
13.2k
    }
13712
13713
69
  return n;
13714
69
}
13715
13716
/* It is only used by x86-64 so far.
13717
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
13718
   but current usage would allow all of _bfd_std_section to be zero.  */
13719
static const asymbol lcomm_sym
13720
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13721
asection _bfd_elf_large_com_section
13722
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13723
          "LARGE_COMMON", 0, SEC_IS_COMMON);
13724
13725
bool
13726
_bfd_elf_final_write_processing (bfd *abfd)
13727
197
{
13728
197
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13729
13730
197
  i_ehdrp = elf_elfheader (abfd);
13731
13732
197
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13733
83
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13734
13735
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13736
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13737
     or STB_GNU_UNIQUE binding.  */
13738
197
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13739
0
    {
13740
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13741
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13742
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13743
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13744
0
  {
13745
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13746
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13747
0
          "and FreeBSD targets"));
13748
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13749
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13750
0
          "only by GNU and FreeBSD targets"));
13751
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13752
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13753
0
          "only by GNU and FreeBSD targets"));
13754
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13755
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13756
0
          "only by GNU and FreeBSD targets"));
13757
0
    bfd_set_error (bfd_error_sorry);
13758
0
    return false;
13759
0
  }
13760
0
    }
13761
197
  return true;
13762
197
}
13763
13764
13765
/* Return TRUE for ELF symbol types that represent functions.
13766
   This is the default version of this function, which is sufficient for
13767
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13768
13769
bool
13770
_bfd_elf_is_function_type (unsigned int type)
13771
0
{
13772
0
  return (type == STT_FUNC
13773
0
    || type == STT_GNU_IFUNC);
13774
0
}
13775
13776
/* If the ELF symbol SYM might be a function in SEC, return the
13777
   function size and set *CODE_OFF to the function's entry point,
13778
   otherwise return zero.  */
13779
13780
bfd_size_type
13781
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13782
           bfd_vma *code_off)
13783
116M
{
13784
116M
  bfd_size_type size;
13785
116M
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13786
13787
116M
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13788
116M
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13789
116M
      || sym->section != sec)
13790
103M
    return 0;
13791
13792
12.3M
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13793
13794
  /* In theory we should check that the symbol's type satisfies
13795
     _bfd_elf_is_function_type(), but there are some function-like
13796
     symbols which would fail this test.  (eg _start).  Instead
13797
     we check for hidden, local, notype symbols with zero size.
13798
     This type of symbol is generated by the annobin plugin for gcc
13799
     and clang, and should not be considered to be a function symbol.  */
13800
12.3M
  if (size == 0
13801
12.3M
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13802
12.3M
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13803
12.3M
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13804
144
    return 0;
13805
13806
12.3M
  *code_off = sym->value;
13807
  /* Do not return 0 for the function's size.  */
13808
12.3M
  return size ? size : 1;
13809
12.3M
}
13810
13811
/* Set to non-zero to enable some debug messages.  */
13812
#define DEBUG_SECONDARY_RELOCS   0
13813
13814
/* An internal-to-the-bfd-library only section type
13815
   used to indicate a cached secondary reloc section.  */
13816
22.7k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13817
13818
/* Create a BFD section to hold a secondary reloc section.  */
13819
13820
bool
13821
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13822
               Elf_Internal_Shdr *hdr,
13823
               const char * name,
13824
               unsigned int shindex)
13825
11.5k
{
13826
  /* We only support RELA secondary relocs.  */
13827
11.5k
  if (hdr->sh_type != SHT_RELA)
13828
3.75k
    return false;
13829
13830
#if DEBUG_SECONDARY_RELOCS
13831
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13832
#endif
13833
7.82k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13834
7.82k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13835
11.5k
}
13836
13837
/* Read in any secondary relocs associated with SEC.  */
13838
13839
bool
13840
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13841
          asection *  sec,
13842
          asymbol **  symbols,
13843
          bool dynamic)
13844
44.9k
{
13845
44.9k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13846
44.9k
  asection * relsec;
13847
44.9k
  bool result = true;
13848
44.9k
  bfd_vma (*r_sym) (bfd_vma);
13849
44.9k
  ufile_ptr filesize;
13850
13851
44.9k
#if BFD_DEFAULT_TARGET_SIZE > 32
13852
44.9k
  if (bfd_arch_bits_per_address (abfd) != 32)
13853
40.5k
    r_sym = elf64_r_sym;
13854
4.40k
  else
13855
4.40k
#endif
13856
4.40k
    r_sym = elf32_r_sym;
13857
13858
44.9k
  if (!elf_section_data (sec)->has_secondary_relocs)
13859
44.8k
    return true;
13860
13861
  /* Discover if there are any secondary reloc sections
13862
     associated with SEC.  */
13863
106
  filesize = bfd_get_file_size (abfd);
13864
7.48k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13865
7.37k
    {
13866
7.37k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13867
13868
7.37k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13869
7.37k
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13870
7.37k
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13871
106
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13872
106
  {
13873
106
    bfd_byte * native_relocs;
13874
106
    bfd_byte * native_reloc;
13875
106
    arelent * internal_relocs;
13876
106
    arelent * internal_reloc;
13877
106
    size_t i;
13878
106
    unsigned int entsize;
13879
106
    unsigned int symcount;
13880
106
    bfd_size_type reloc_count;
13881
106
    size_t amt;
13882
13883
106
    if (ebd->elf_info_to_howto == NULL)
13884
0
      return false;
13885
13886
#if DEBUG_SECONDARY_RELOCS
13887
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13888
       sec->name, relsec->name);
13889
#endif
13890
106
    entsize = hdr->sh_entsize;
13891
13892
106
    if (filesize != 0
13893
106
        && ((ufile_ptr) hdr->sh_offset > filesize
13894
106
      || hdr->sh_size > filesize - hdr->sh_offset))
13895
17
      {
13896
17
        bfd_set_error (bfd_error_file_truncated);
13897
17
        result = false;
13898
17
        continue;
13899
17
      }
13900
13901
89
    native_relocs = bfd_malloc (hdr->sh_size);
13902
89
    if (native_relocs == NULL)
13903
0
      {
13904
0
        result = false;
13905
0
        continue;
13906
0
      }
13907
13908
89
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13909
89
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13910
0
      {
13911
0
        free (native_relocs);
13912
0
        bfd_set_error (bfd_error_file_too_big);
13913
0
        result = false;
13914
0
        continue;
13915
0
      }
13916
13917
89
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13918
89
    if (internal_relocs == NULL)
13919
0
      {
13920
0
        free (native_relocs);
13921
0
        result = false;
13922
0
        continue;
13923
0
      }
13924
13925
89
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13926
89
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13927
0
      {
13928
0
        free (native_relocs);
13929
        /* The internal_relocs will be freed when
13930
     the memory for the bfd is released.  */
13931
0
        result = false;
13932
0
        continue;
13933
0
      }
13934
13935
89
    if (dynamic)
13936
0
      symcount = bfd_get_dynamic_symcount (abfd);
13937
89
    else
13938
89
      symcount = bfd_get_symcount (abfd);
13939
13940
89
    for (i = 0, internal_reloc = internal_relocs,
13941
89
     native_reloc = native_relocs;
13942
1.55k
         i < reloc_count;
13943
1.46k
         i++, internal_reloc++, native_reloc += entsize)
13944
1.46k
      {
13945
1.46k
        bool res;
13946
1.46k
        Elf_Internal_Rela rela;
13947
13948
1.46k
        if (entsize == ebd->s->sizeof_rel)
13949
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13950
1.46k
        else /* entsize == ebd->s->sizeof_rela */
13951
1.46k
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13952
13953
        /* The address of an ELF reloc is section relative for an object
13954
     file, and absolute for an executable file or shared library.
13955
     The address of a normal BFD reloc is always section relative,
13956
     and the address of a dynamic reloc is absolute..  */
13957
1.46k
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13958
1.44k
    internal_reloc->address = rela.r_offset;
13959
16
        else
13960
16
    internal_reloc->address = rela.r_offset - sec->vma;
13961
13962
1.46k
        if (r_sym (rela.r_info) == STN_UNDEF)
13963
136
    {
13964
      /* FIXME: This and the error case below mean that we
13965
         have a symbol on relocs that is not elf_symbol_type.  */
13966
136
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13967
136
    }
13968
1.32k
        else if (r_sym (rela.r_info) > symcount)
13969
0
    {
13970
0
      _bfd_error_handler
13971
        /* xgettext:c-format */
13972
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13973
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13974
0
      bfd_set_error (bfd_error_bad_value);
13975
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13976
0
      result = false;
13977
0
    }
13978
1.32k
        else
13979
1.32k
    {
13980
1.32k
      asymbol **ps;
13981
13982
1.32k
      ps = symbols + r_sym (rela.r_info) - 1;
13983
1.32k
      internal_reloc->sym_ptr_ptr = ps;
13984
      /* Make sure that this symbol is not removed by strip.  */
13985
1.32k
      (*ps)->flags |= BSF_KEEP;
13986
1.32k
    }
13987
13988
1.46k
        internal_reloc->addend = rela.r_addend;
13989
13990
1.46k
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13991
1.46k
        if (! res || internal_reloc->howto == NULL)
13992
100
    {
13993
#if DEBUG_SECONDARY_RELOCS
13994
      fprintf (stderr,
13995
         "there is no howto associated with reloc %lx\n",
13996
         rela.r_info);
13997
#endif
13998
100
      result = false;
13999
100
    }
14000
1.46k
      }
14001
14002
89
    free (native_relocs);
14003
    /* Store the internal relocs.  */
14004
89
    elf_section_data (relsec)->sec_info = internal_relocs;
14005
89
  }
14006
7.37k
    }
14007
14008
106
  return result;
14009
106
}
14010
14011
/* Set the ELF section header fields of an output secondary reloc section.  */
14012
14013
bool
14014
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14015
              bfd *obfd ATTRIBUTE_UNUSED,
14016
              const Elf_Internal_Shdr *isection,
14017
              Elf_Internal_Shdr *osection)
14018
142
{
14019
142
  asection * isec;
14020
142
  asection * osec;
14021
142
  struct bfd_elf_section_data * esd;
14022
14023
142
  if (isection == NULL)
14024
0
    return false;
14025
14026
142
  if (isection->sh_type != SHT_SECONDARY_RELOC)
14027
142
    return true;
14028
14029
0
  isec = isection->bfd_section;
14030
0
  if (isec == NULL)
14031
0
    return false;
14032
14033
0
  osec = osection->bfd_section;
14034
0
  if (osec == NULL)
14035
0
    return false;
14036
14037
0
  esd = elf_section_data (osec);
14038
0
  BFD_ASSERT (esd->sec_info == NULL);
14039
0
  esd->sec_info = elf_section_data (isec)->sec_info;
14040
0
  osection->sh_type = SHT_RELA;
14041
0
  osection->sh_link = elf_onesymtab (obfd);
14042
0
  if (osection->sh_link == 0)
14043
0
    {
14044
      /* There is no symbol table - we are hosed...  */
14045
0
      _bfd_error_handler
14046
  /* xgettext:c-format */
14047
0
  (_("%pB(%pA): link section cannot be set"
14048
0
     " because the output file does not have a symbol table"),
14049
0
  obfd, osec);
14050
0
      bfd_set_error (bfd_error_bad_value);
14051
0
      return false;
14052
0
    }
14053
14054
  /* Find the output section that corresponds to the isection's
14055
     sh_info link.  */
14056
0
  if (isection->sh_info == 0
14057
0
      || isection->sh_info >= elf_numsections (ibfd))
14058
0
    {
14059
0
      _bfd_error_handler
14060
  /* xgettext:c-format */
14061
0
  (_("%pB(%pA): info section index is invalid"),
14062
0
  obfd, osec);
14063
0
      bfd_set_error (bfd_error_bad_value);
14064
0
      return false;
14065
0
    }
14066
14067
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
14068
14069
0
  if (isection == NULL
14070
0
      || isection->bfd_section == NULL
14071
0
      || isection->bfd_section->output_section == NULL)
14072
0
    {
14073
0
      _bfd_error_handler
14074
  /* xgettext:c-format */
14075
0
  (_("%pB(%pA): info section index cannot be set"
14076
0
     " because the section is not in the output"),
14077
0
  obfd, osec);
14078
0
      bfd_set_error (bfd_error_bad_value);
14079
0
      return false;
14080
0
    }
14081
14082
0
  esd = elf_section_data (isection->bfd_section->output_section);
14083
0
  BFD_ASSERT (esd != NULL);
14084
0
  osection->sh_info = esd->this_idx;
14085
0
  esd->has_secondary_relocs = true;
14086
#if DEBUG_SECONDARY_RELOCS
14087
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14088
     osec->name, osection->sh_link, osection->sh_info);
14089
  fprintf (stderr, "mark section %s as having secondary relocs\n",
14090
     bfd_section_name (isection->bfd_section->output_section));
14091
#endif
14092
14093
0
  return true;
14094
0
}
14095
14096
/* Write out a secondary reloc section.
14097
14098
   FIXME: Currently this function can result in a serious performance penalty
14099
   for files with secondary relocs and lots of sections.  The proper way to
14100
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14101
   relocs together and then to have this function just walk that chain.  */
14102
14103
bool
14104
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14105
0
{
14106
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14107
0
  bfd_vma addr_offset;
14108
0
  asection * relsec;
14109
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
14110
0
  bool result = true;
14111
14112
0
  if (sec == NULL)
14113
0
    return false;
14114
14115
0
#if BFD_DEFAULT_TARGET_SIZE > 32
14116
0
  if (bfd_arch_bits_per_address (abfd) != 32)
14117
0
    r_info = elf64_r_info;
14118
0
  else
14119
0
#endif
14120
0
    r_info = elf32_r_info;
14121
14122
  /* The address of an ELF reloc is section relative for an object
14123
     file, and absolute for an executable file or shared library.
14124
     The address of a BFD reloc is always section relative.  */
14125
0
  addr_offset = 0;
14126
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14127
0
    addr_offset = sec->vma;
14128
14129
  /* Discover if there are any secondary reloc sections
14130
     associated with SEC.  */
14131
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14132
0
    {
14133
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14134
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14135
14136
0
      if (hdr->sh_type == SHT_RELA
14137
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14138
0
  {
14139
0
    asymbol *    last_sym;
14140
0
    int          last_sym_idx;
14141
0
    size_t       reloc_count;
14142
0
    size_t       idx;
14143
0
    bfd_size_type entsize;
14144
0
    arelent *    src_irel;
14145
0
    bfd_byte *   dst_rela;
14146
14147
0
    if (hdr->contents != NULL)
14148
0
      {
14149
0
        _bfd_error_handler
14150
    /* xgettext:c-format */
14151
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
14152
0
     abfd, relsec);
14153
0
        bfd_set_error (bfd_error_bad_value);
14154
0
        result = false;
14155
0
        continue;
14156
0
      }
14157
14158
0
    entsize = hdr->sh_entsize;
14159
0
    if (entsize == 0)
14160
0
      {
14161
0
        _bfd_error_handler
14162
    /* xgettext:c-format */
14163
0
    (_("%pB(%pA): error: secondary reloc section"
14164
0
       " has zero sized entries"),
14165
0
     abfd, relsec);
14166
0
        bfd_set_error (bfd_error_bad_value);
14167
0
        result = false;
14168
0
        continue;
14169
0
      }
14170
0
    else if (entsize != ebd->s->sizeof_rel
14171
0
       && entsize != ebd->s->sizeof_rela)
14172
0
      {
14173
0
        _bfd_error_handler
14174
    /* xgettext:c-format */
14175
0
    (_("%pB(%pA): error: secondary reloc section"
14176
0
       " has non-standard sized entries"),
14177
0
     abfd, relsec);
14178
0
        bfd_set_error (bfd_error_bad_value);
14179
0
        result = false;
14180
0
        continue;
14181
0
      }
14182
14183
0
    reloc_count = hdr->sh_size / entsize;
14184
0
    hdr->sh_size = entsize * reloc_count;
14185
0
    if (reloc_count == 0)
14186
0
      {
14187
0
        _bfd_error_handler
14188
    /* xgettext:c-format */
14189
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
14190
0
     abfd, relsec);
14191
0
        bfd_set_error (bfd_error_bad_value);
14192
0
        result = false;
14193
0
        continue;
14194
0
      }
14195
14196
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14197
0
    if (hdr->contents == NULL)
14198
0
      continue;
14199
0
    relsec->alloced = 1;
14200
14201
#if DEBUG_SECONDARY_RELOCS
14202
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14203
       reloc_count, sec->name, relsec->name);
14204
#endif
14205
0
    last_sym = NULL;
14206
0
    last_sym_idx = 0;
14207
0
    dst_rela = hdr->contents;
14208
0
    src_irel = (arelent *) esd->sec_info;
14209
0
    if (src_irel == NULL)
14210
0
      {
14211
0
        _bfd_error_handler
14212
    /* xgettext:c-format */
14213
0
    (_("%pB(%pA): error: internal relocs missing"
14214
0
       " for secondary reloc section"),
14215
0
     abfd, relsec);
14216
0
        bfd_set_error (bfd_error_bad_value);
14217
0
        result = false;
14218
0
        continue;
14219
0
      }
14220
14221
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14222
0
      {
14223
0
        Elf_Internal_Rela src_rela;
14224
0
        arelent *ptr;
14225
0
        asymbol *sym;
14226
0
        int n;
14227
14228
0
        ptr = src_irel + idx;
14229
0
        if (ptr == NULL)
14230
0
    {
14231
0
      _bfd_error_handler
14232
        /* xgettext:c-format */
14233
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14234
0
         abfd, relsec, idx);
14235
0
      bfd_set_error (bfd_error_bad_value);
14236
0
      result = false;
14237
0
      break;
14238
0
    }
14239
14240
0
        if (ptr->sym_ptr_ptr == NULL)
14241
0
    {
14242
      /* FIXME: Is this an error ? */
14243
0
      n = 0;
14244
0
    }
14245
0
        else
14246
0
    {
14247
0
      sym = *ptr->sym_ptr_ptr;
14248
14249
0
      if (sym == last_sym)
14250
0
        n = last_sym_idx;
14251
0
      else
14252
0
        {
14253
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14254
0
          if (n < 0)
14255
0
      {
14256
0
        _bfd_error_handler
14257
          /* xgettext:c-format */
14258
0
          (_("%pB(%pA): error: secondary reloc %zu"
14259
0
             " references a missing symbol"),
14260
0
           abfd, relsec, idx);
14261
0
        bfd_set_error (bfd_error_bad_value);
14262
0
        result = false;
14263
0
        n = 0;
14264
0
      }
14265
14266
0
          last_sym = sym;
14267
0
          last_sym_idx = n;
14268
0
        }
14269
14270
0
      if (sym->the_bfd != NULL
14271
0
          && sym->the_bfd->xvec != abfd->xvec
14272
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14273
0
        {
14274
0
          _bfd_error_handler
14275
      /* xgettext:c-format */
14276
0
      (_("%pB(%pA): error: secondary reloc %zu"
14277
0
         " references a deleted symbol"),
14278
0
       abfd, relsec, idx);
14279
0
          bfd_set_error (bfd_error_bad_value);
14280
0
          result = false;
14281
0
          n = 0;
14282
0
        }
14283
0
    }
14284
14285
0
        src_rela.r_offset = ptr->address + addr_offset;
14286
0
        if (ptr->howto == NULL)
14287
0
    {
14288
0
      _bfd_error_handler
14289
        /* xgettext:c-format */
14290
0
        (_("%pB(%pA): error: secondary reloc %zu"
14291
0
           " is of an unknown type"),
14292
0
         abfd, relsec, idx);
14293
0
      bfd_set_error (bfd_error_bad_value);
14294
0
      result = false;
14295
0
      src_rela.r_info = r_info (0, 0);
14296
0
    }
14297
0
        else
14298
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14299
0
        src_rela.r_addend = ptr->addend;
14300
14301
0
        if (entsize == ebd->s->sizeof_rel)
14302
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14303
0
        else /* entsize == ebd->s->sizeof_rela */
14304
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14305
0
      }
14306
0
  }
14307
0
    }
14308
14309
0
  return result;
14310
0
}
14311
14312
/* Mmap in section contents.  */
14313
14314
static bool
14315
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14316
63.1k
{
14317
63.1k
#ifdef USE_MMAP
14318
63.1k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14319
63.1k
  if (bed->use_mmap
14320
63.1k
      && sec->compress_status == COMPRESS_SECTION_NONE
14321
63.1k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14322
20.0k
    {
14323
      /* Use mmap only if section size >= the minimum mmap section
14324
   size.  */
14325
20.0k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14326
20.0k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14327
20.0k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14328
3.43k
  {
14329
3.43k
    if (sec->contents != NULL)
14330
0
      {
14331
0
        if (!sec->mmapped_p)
14332
0
    abort ();
14333
0
        *buf = sec->contents;
14334
0
        return true;
14335
0
      }
14336
3.43k
    if (sec->mmapped_p)
14337
0
      abort ();
14338
3.43k
    sec->mmapped_p = 1;
14339
14340
    /* We can't use the final link preallocated buffer for mmap.  */
14341
3.43k
    *buf = NULL;
14342
3.43k
  }
14343
20.0k
    }
14344
63.1k
#endif
14345
  /* FIXME: We should not get here if sec->alloced is set.  */
14346
63.1k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14347
63.1k
  if (ret && sec->mmapped_p)
14348
987
    *buf = sec->contents;
14349
63.1k
  return ret;
14350
63.1k
}
14351
14352
/* Mmap in section contents.  */
14353
14354
bool
14355
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14356
63.1k
{
14357
63.1k
  *buf = NULL;
14358
63.1k
  return elf_mmap_section_contents (abfd, sec, buf);
14359
63.1k
}
14360
14361
/* Mmap in the full section contents for the final link.  */
14362
14363
bool
14364
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14365
             bfd_byte **buf)
14366
0
{
14367
0
  return elf_mmap_section_contents (abfd, sec, buf);
14368
0
}
14369
14370
/* Munmap section contents.  */
14371
14372
void
14373
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14374
2.05M
{
14375
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14376
     CONTENTS may be NULL.  */
14377
2.05M
  if (contents == NULL)
14378
1.98M
    return;
14379
14380
68.4k
  if (sec->alloced
14381
      /* What a tangled web we weave with section contents.
14382
   FIXME: We shouldn't need to test anything but sec->alloced
14383
   here, but there are cases where a buffer is allocated for a
14384
   section but then another buffer is malloc'd anyway.  eg.
14385
   trace through ld-elf/eh4 testcase on x86_64.  */
14386
68.4k
      && (sec->contents == contents
14387
11.4k
    || elf_section_data (sec)->this_hdr.contents == contents))
14388
11.4k
    return;
14389
14390
  /* Don't leave pointers to data we are about to munmap or free.  */
14391
56.9k
  if (sec->contents == contents)
14392
987
    sec->contents = NULL;
14393
56.9k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14394
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14395
14396
56.9k
#ifdef USE_MMAP
14397
56.9k
  if (sec->mmapped_p)
14398
987
    {
14399
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14400
   malloced, CONTENTS_ADDR is set to NULL.  */
14401
987
      if (elf_section_data (sec)->contents_addr != NULL)
14402
987
  {
14403
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14404
987
    if (munmap (elf_section_data (sec)->contents_addr,
14405
987
          elf_section_data (sec)->contents_size) != 0)
14406
0
      abort ();
14407
987
    sec->mmapped_p = 0;
14408
987
    elf_section_data (sec)->contents_addr = NULL;
14409
987
    elf_section_data (sec)->contents_size = 0;
14410
987
    return;
14411
987
  }
14412
987
    }
14413
55.9k
#endif
14414
14415
55.9k
  free (contents);
14416
55.9k
}
14417
14418
/* Munmap the full section contents for the final link.  */
14419
14420
void
14421
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14422
0
{
14423
0
#ifdef USE_MMAP
14424
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14425
0
    {
14426
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14427
   malloced, CONTENTS_ADDR is set to NULL.  */
14428
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14429
0
      if (munmap (elf_section_data (sec)->contents_addr,
14430
0
      elf_section_data (sec)->contents_size) != 0)
14431
0
  abort ();
14432
0
      sec->mmapped_p = 0;
14433
0
      sec->contents = NULL;
14434
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14435
0
      elf_section_data (sec)->contents_addr = NULL;
14436
0
      elf_section_data (sec)->contents_size = 0;
14437
0
    }
14438
0
#endif
14439
0
}