Coverage Report

Created: 2024-05-21 06:29

/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-2024 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
13
{
70
13
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
13
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
13
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
13
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
13
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
13
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
13
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
13
}
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
2
{
102
2
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
103
2
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
104
2
}
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
317
{
124
317
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
317
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
317
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
317
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
317
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
317
}
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
613
{
152
613
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
613
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
613
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
613
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
613
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
613
}
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
54.8k
{
180
54.8k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
54.8k
}
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 to OBJECT_ID.  */
226
bool
227
bfd_elf_allocate_object (bfd *abfd,
228
       size_t object_size,
229
       enum elf_target_id object_id)
230
14.3M
{
231
14.3M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232
14.3M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
233
14.3M
  if (abfd->tdata.any == NULL)
234
0
    return false;
235
236
14.3M
  elf_object_id (abfd) = object_id;
237
14.3M
  if (abfd->direction != read_direction)
238
770
    {
239
770
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240
770
      if (o == NULL)
241
0
  return false;
242
770
      elf_tdata (abfd)->o = o;
243
770
      elf_program_header_size (abfd) = (bfd_size_type) -1;
244
770
    }
245
14.3M
  return true;
246
14.3M
}
247
248
249
bool
250
bfd_elf_make_object (bfd *abfd)
251
6.70M
{
252
6.70M
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253
6.70M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254
6.70M
          bed->target_id);
255
6.70M
}
256
257
bool
258
bfd_elf_mkcorefile (bfd *abfd)
259
287k
{
260
  /* I think this can be done just like an object file.  */
261
287k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262
0
    return false;
263
287k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264
287k
  return elf_tdata (abfd)->core != NULL;
265
287k
}
266
267
char *
268
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
269
226k
{
270
226k
  Elf_Internal_Shdr **i_shdrp;
271
226k
  bfd_byte *shstrtab = NULL;
272
226k
  file_ptr offset;
273
226k
  bfd_size_type shstrtabsize;
274
275
226k
  i_shdrp = elf_elfsections (abfd);
276
226k
  if (i_shdrp == 0
277
226k
      || shindex >= elf_numsections (abfd)
278
226k
      || i_shdrp[shindex] == 0)
279
0
    return NULL;
280
281
226k
  shstrtab = i_shdrp[shindex]->contents;
282
226k
  if (shstrtab == NULL)
283
226k
    {
284
      /* No cached one, attempt to read, and cache what we read.  */
285
226k
      offset = i_shdrp[shindex]->sh_offset;
286
226k
      shstrtabsize = i_shdrp[shindex]->sh_size;
287
288
      /* Allocate and clear an extra byte at the end, to prevent crashes
289
   in case the string table is not terminated.  */
290
226k
      if (shstrtabsize + 1 <= 1
291
226k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
292
226k
    || (shstrtab
293
186k
        = _bfd_mmap_readonly_persistent (abfd, shstrtabsize)) == NULL)
294
46.4k
  {
295
    /* Once we've failed to read it, make sure we don't keep
296
       trying.  Otherwise, we'll keep allocating space for
297
       the string table over and over.  */
298
46.4k
    i_shdrp[shindex]->sh_size = 0;
299
46.4k
  }
300
180k
      else if (shstrtab[shstrtabsize - 1] != '\0')
301
14.3k
  {
302
    /* It is an error if a string table isn't terminated.  */
303
14.3k
    _bfd_error_handler
304
      /* xgettext:c-format */
305
14.3k
      (_("%pB(%pA): string table is corrupt"),
306
14.3k
       abfd, i_shdrp[shindex]->bfd_section);
307
14.3k
    return NULL;
308
14.3k
  }
309
212k
      i_shdrp[shindex]->contents = shstrtab;
310
212k
    }
311
212k
  return (char *) shstrtab;
312
226k
}
313
314
char *
315
bfd_elf_string_from_elf_section (bfd *abfd,
316
         unsigned int shindex,
317
         unsigned int strindex)
318
4.10M
{
319
4.10M
  Elf_Internal_Shdr *hdr;
320
321
4.10M
  if (strindex == 0)
322
1.89M
    return "";
323
324
2.20M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
325
51.0k
    return NULL;
326
327
2.15M
  hdr = elf_elfsections (abfd)[shindex];
328
329
2.15M
  if (hdr->contents == NULL)
330
232k
    {
331
232k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
332
6.02k
  {
333
    /* PR 17512: file: f057ec89.  */
334
    /* xgettext:c-format */
335
6.02k
    _bfd_error_handler (_("%pB: attempt to load strings from"
336
6.02k
        " a non-string section (number %d)"),
337
6.02k
            abfd, shindex);
338
6.02k
    return NULL;
339
6.02k
  }
340
341
226k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
342
60.8k
  return NULL;
343
226k
    }
344
1.92M
  else
345
1.92M
    {
346
      /* PR 24273: The string section's contents may have already
347
   been loaded elsewhere, eg because a corrupt file has the
348
   string section index in the ELF header pointing at a group
349
   section.  So be paranoid, and test that the last byte of
350
   the section is zero.  */
351
1.92M
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
352
16
  return NULL;
353
1.92M
    }
354
355
2.08M
  if (strindex >= hdr->sh_size)
356
184k
    {
357
184k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
358
184k
      _bfd_error_handler
359
  /* xgettext:c-format */
360
184k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
361
184k
   abfd, strindex, (uint64_t) hdr->sh_size,
362
184k
   (shindex == shstrndx && strindex == hdr->sh_name
363
184k
    ? ".shstrtab"
364
184k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
365
184k
      return NULL;
366
184k
    }
367
368
1.90M
  return ((char *) hdr->contents) + strindex;
369
2.08M
}
370
371
/* Read and convert symbols to internal format.
372
   SYMCOUNT specifies the number of symbols to read, starting from
373
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
374
   are non-NULL, they are used to store the internal symbols, external
375
   symbols, and symbol section index extensions, respectively.
376
   Returns a pointer to the internal symbol buffer (malloced if necessary)
377
   or NULL if there were no symbols or some kind of problem.  */
378
379
Elf_Internal_Sym *
380
bfd_elf_get_elf_syms (bfd *ibfd,
381
          Elf_Internal_Shdr *symtab_hdr,
382
          size_t symcount,
383
          size_t symoffset,
384
          Elf_Internal_Sym *intsym_buf,
385
          void *extsym_buf,
386
          Elf_External_Sym_Shndx *extshndx_buf)
387
31.0k
{
388
31.0k
  Elf_Internal_Shdr *shndx_hdr;
389
31.0k
  void *alloc_ext;
390
31.0k
  const bfd_byte *esym;
391
31.0k
  Elf_External_Sym_Shndx *alloc_extshndx;
392
31.0k
  Elf_External_Sym_Shndx *shndx;
393
31.0k
  Elf_Internal_Sym *alloc_intsym;
394
31.0k
  Elf_Internal_Sym *isym;
395
31.0k
  Elf_Internal_Sym *isymend;
396
31.0k
  const struct elf_backend_data *bed;
397
31.0k
  size_t extsym_size;
398
31.0k
  size_t amt;
399
31.0k
  file_ptr pos;
400
401
31.0k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
402
0
    abort ();
403
404
31.0k
  if (symcount == 0)
405
0
    return intsym_buf;
406
407
31.0k
  if (elf_use_dt_symtab_p (ibfd))
408
0
    {
409
      /* Use dynamic symbol table.  */
410
0
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
411
0
  {
412
0
    bfd_set_error (bfd_error_invalid_operation);
413
0
    return NULL;
414
0
  }
415
0
      return elf_tdata (ibfd)->dt_symtab + symoffset;
416
0
    }
417
418
  /* Normal syms might have section extension entries.  */
419
31.0k
  shndx_hdr = NULL;
420
31.0k
  if (elf_symtab_shndx_list (ibfd) != NULL)
421
2.31k
    {
422
2.31k
      elf_section_list * entry;
423
2.31k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
424
425
      /* Find an index section that is linked to this symtab section.  */
426
4.28k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
427
2.50k
  {
428
    /* PR 20063.  */
429
2.50k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
430
22
      continue;
431
432
2.47k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
433
529
      {
434
529
        shndx_hdr = & entry->hdr;
435
529
        break;
436
1.94k
      };
437
1.94k
  }
438
439
2.31k
      if (shndx_hdr == NULL)
440
1.78k
  {
441
1.78k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
442
      /* Not really accurate, but this was how the old code used
443
         to work.  */
444
1.78k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
445
    /* Otherwise we do nothing.  The assumption is that
446
       the index table will not be needed.  */
447
1.78k
  }
448
2.31k
    }
449
450
  /* Read the symbols.  */
451
31.0k
  alloc_ext = NULL;
452
31.0k
  alloc_extshndx = NULL;
453
31.0k
  alloc_intsym = NULL;
454
31.0k
  bed = get_elf_backend_data (ibfd);
455
31.0k
  extsym_size = bed->s->sizeof_sym;
456
31.0k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
457
0
    {
458
0
      bfd_set_error (bfd_error_file_too_big);
459
0
      return NULL;
460
0
    }
461
31.0k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
462
31.0k
  size_t alloc_ext_size = amt;
463
31.0k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
464
31.0k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
465
30.9k
            &alloc_ext, ibfd, false))
466
1.21k
    {
467
1.21k
      intsym_buf = NULL;
468
1.21k
      goto out2;
469
1.21k
    }
470
471
29.7k
  size_t alloc_extshndx_size = 0;
472
29.7k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
473
28.1k
    extshndx_buf = NULL;
474
1.65k
  else
475
1.65k
    {
476
1.65k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
477
0
  {
478
0
    bfd_set_error (bfd_error_file_too_big);
479
0
    intsym_buf = NULL;
480
0
    goto out1;
481
0
  }
482
1.65k
      alloc_extshndx_size = amt;
483
1.65k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
484
1.65k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
485
1.65k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
486
1.61k
          &alloc_extshndx_size,
487
1.61k
          (void **) &alloc_extshndx,
488
1.61k
          ibfd, false))
489
786
  {
490
786
    intsym_buf = NULL;
491
786
    goto out1;
492
786
  }
493
1.65k
    }
494
495
29.0k
  if (intsym_buf == NULL)
496
2.72k
    {
497
2.72k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
498
0
  {
499
0
    bfd_set_error (bfd_error_file_too_big);
500
0
    goto out1;
501
0
  }
502
2.72k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
503
2.72k
      intsym_buf = alloc_intsym;
504
2.72k
      if (intsym_buf == NULL)
505
0
  goto out1;
506
2.72k
    }
507
508
  /* Convert the symbols to internal form.  */
509
29.0k
  isymend = intsym_buf + symcount;
510
29.0k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
511
29.0k
     shndx = extshndx_buf;
512
708k
       isym < isymend;
513
679k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
514
680k
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
515
1.20k
      {
516
1.20k
  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
517
  /* xgettext:c-format */
518
1.20k
  _bfd_error_handler (_("%pB symbol number %lu references"
519
1.20k
            " nonexistent SHT_SYMTAB_SHNDX section"),
520
1.20k
          ibfd, (unsigned long) symoffset);
521
1.20k
  free (alloc_intsym);
522
1.20k
  intsym_buf = NULL;
523
1.20k
  goto out1;
524
1.20k
      }
525
526
29.7k
 out1:
527
29.7k
  _bfd_munmap_readonly_temporary (alloc_extshndx, alloc_extshndx_size);
528
31.0k
 out2:
529
31.0k
  _bfd_munmap_readonly_temporary (alloc_ext, alloc_ext_size);
530
531
31.0k
  return intsym_buf;
532
29.7k
}
533
534
/* Look up a symbol name.  */
535
const char *
536
bfd_elf_sym_name (bfd *abfd,
537
      Elf_Internal_Shdr *symtab_hdr,
538
      Elf_Internal_Sym *isym,
539
      asection *sym_sec)
540
668k
{
541
668k
  const char *name;
542
668k
  unsigned int iname = isym->st_name;
543
668k
  unsigned int shindex = symtab_hdr->sh_link;
544
545
668k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
546
      /* Check for a bogus st_shndx to avoid crashing.  */
547
668k
      && isym->st_shndx < elf_numsections (abfd))
548
38.5k
    {
549
38.5k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
550
38.5k
      shindex = elf_elfheader (abfd)->e_shstrndx;
551
38.5k
    }
552
553
668k
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
554
668k
  if (name == NULL)
555
239k
    name = "(null)";
556
428k
  else if (sym_sec && *name == '\0')
557
0
    name = bfd_section_name (sym_sec);
558
559
668k
  return name;
560
668k
}
561
562
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
563
   sections.  The first element is the flags, the rest are section
564
   pointers.  */
565
566
typedef union elf_internal_group {
567
  Elf_Internal_Shdr *shdr;
568
  unsigned int flags;
569
} Elf_Internal_Group;
570
571
/* Return the name of the group signature symbol.  Why isn't the
572
   signature just a string?  */
573
574
static const char *
575
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
576
30.5k
{
577
30.5k
  Elf_Internal_Shdr *hdr;
578
30.5k
  unsigned char esym[sizeof (Elf64_External_Sym)];
579
30.5k
  Elf_External_Sym_Shndx eshndx;
580
30.5k
  Elf_Internal_Sym isym;
581
582
  /* First we need to ensure the symbol table is available.  Make sure
583
     that it is a symbol table section.  */
584
30.5k
  if (ghdr->sh_link >= elf_numsections (abfd))
585
13
    return NULL;
586
30.5k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
587
30.5k
  if (hdr->sh_type != SHT_SYMTAB
588
30.5k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
589
2.31k
    return NULL;
590
591
  /* Go read the symbol.  */
592
28.2k
  hdr = &elf_tdata (abfd)->symtab_hdr;
593
28.2k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
594
28.2k
          &isym, esym, &eshndx) == NULL)
595
3.11k
    return NULL;
596
597
25.1k
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
598
28.2k
}
599
600
/* Set next_in_group list pointer, and group name for NEWSECT.  */
601
602
static bool
603
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
604
335k
{
605
335k
  unsigned int num_group = elf_tdata (abfd)->num_group;
606
607
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
608
     is set to -1 if there are no SHT_GROUP sections.  */
609
335k
  if (num_group == 0)
610
121k
    {
611
121k
      unsigned int i, shnum;
612
613
      /* First count the number of groups.  If we have a SHT_GROUP
614
   section with just a flag word (ie. sh_size is 4), ignore it.  */
615
121k
      shnum = elf_numsections (abfd);
616
121k
      num_group = 0;
617
618
121k
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)  \
619
2.77M
  (   (shdr)->sh_type == SHT_GROUP    \
620
2.77M
   && (shdr)->sh_size >= minsize      \
621
2.77M
   && (shdr)->sh_entsize == GRP_ENTRY_SIZE  \
622
2.77M
   && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
623
624
2.30M
      for (i = 0; i < shnum; i++)
625
2.18M
  {
626
2.18M
    Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
627
628
2.18M
    if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
629
42.5k
      num_group += 1;
630
2.18M
  }
631
632
121k
      if (num_group == 0)
633
100k
  {
634
100k
    num_group = (unsigned) -1;
635
100k
    elf_tdata (abfd)->num_group = num_group;
636
100k
    elf_tdata (abfd)->group_sect_ptr = NULL;
637
100k
  }
638
20.9k
      else
639
20.9k
  {
640
    /* We keep a list of elf section headers for group sections,
641
       so we can find them quickly.  */
642
20.9k
    size_t amt;
643
644
20.9k
    elf_tdata (abfd)->num_group = num_group;
645
20.9k
    amt = num_group * sizeof (Elf_Internal_Shdr *);
646
20.9k
    elf_tdata (abfd)->group_sect_ptr
647
20.9k
      = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
648
20.9k
    if (elf_tdata (abfd)->group_sect_ptr == NULL)
649
0
      return false;
650
20.9k
    num_group = 0;
651
652
528k
    for (i = 0; i < shnum; i++)
653
508k
      {
654
508k
        Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
655
656
508k
        if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
657
42.5k
    {
658
42.5k
      unsigned char *src;
659
42.5k
      Elf_Internal_Group *dest;
660
661
      /* Make sure the group section has a BFD section
662
         attached to it.  */
663
42.5k
      if (!bfd_section_from_shdr (abfd, i))
664
1.13k
        return false;
665
666
      /* Add to list of sections.  */
667
41.4k
      elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
668
41.4k
      num_group += 1;
669
670
      /* Read the raw contents.  */
671
41.4k
      BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
672
41.4k
      shdr->contents = NULL;
673
41.4k
      if (_bfd_mul_overflow (shdr->sh_size,
674
41.4k
           sizeof (*dest) / 4, &amt)
675
41.4k
          || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
676
41.4k
          || !(shdr->contents
677
40.8k
         = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
678
2.52k
        {
679
2.52k
          _bfd_error_handler
680
      /* xgettext:c-format */
681
2.52k
      (_("%pB: invalid size field in group section"
682
2.52k
         " header: %#" PRIx64 ""),
683
2.52k
       abfd, (uint64_t) shdr->sh_size);
684
2.52k
          bfd_set_error (bfd_error_bad_value);
685
2.52k
          -- num_group;
686
2.52k
          continue;
687
2.52k
        }
688
689
      /* Translate raw contents, a flag word followed by an
690
         array of elf section indices all in target byte order,
691
         to the flag word followed by an array of elf section
692
         pointers.  */
693
38.8k
      src = shdr->contents + shdr->sh_size;
694
38.8k
      dest = (Elf_Internal_Group *) (shdr->contents + amt);
695
696
2.06M
      while (1)
697
2.06M
        {
698
2.06M
          unsigned int idx;
699
700
2.06M
          src -= 4;
701
2.06M
          --dest;
702
2.06M
          idx = H_GET_32 (abfd, src);
703
2.06M
          if (src == shdr->contents)
704
38.8k
      {
705
38.8k
        dest->shdr = NULL;
706
38.8k
        dest->flags = idx;
707
38.8k
        if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
708
13.5k
          shdr->bfd_section->flags
709
13.5k
            |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
710
38.8k
        break;
711
38.8k
      }
712
2.02M
          if (idx < shnum)
713
556k
      {
714
556k
        dest->shdr = elf_elfsections (abfd)[idx];
715
        /* PR binutils/23199: All sections in a
716
           section group should be marked with
717
           SHF_GROUP.  But some tools generate
718
           broken objects without SHF_GROUP.  Fix
719
           them up here.  */
720
556k
        dest->shdr->sh_flags |= SHF_GROUP;
721
556k
      }
722
2.02M
          if (idx >= shnum
723
2.02M
        || dest->shdr->sh_type == SHT_GROUP)
724
1.47M
      {
725
1.47M
        _bfd_error_handler
726
1.47M
          (_("%pB: invalid entry in SHT_GROUP section [%u]"),
727
1.47M
             abfd, i);
728
1.47M
        dest->shdr = NULL;
729
1.47M
      }
730
2.02M
        }
731
38.8k
    }
732
508k
      }
733
734
    /* PR 17510: Corrupt binaries might contain invalid groups.  */
735
19.8k
    if (num_group != (unsigned) elf_tdata (abfd)->num_group)
736
2.05k
      {
737
2.05k
        elf_tdata (abfd)->num_group = num_group;
738
739
        /* If all groups are invalid then fail.  */
740
2.05k
        if (num_group == 0)
741
1.62k
    {
742
1.62k
      elf_tdata (abfd)->group_sect_ptr = NULL;
743
1.62k
      elf_tdata (abfd)->num_group = num_group = -1;
744
1.62k
      _bfd_error_handler
745
1.62k
        (_("%pB: no valid group sections found"), abfd);
746
1.62k
      bfd_set_error (bfd_error_bad_value);
747
1.62k
    }
748
2.05k
      }
749
19.8k
  }
750
121k
    }
751
752
334k
  if (num_group != (unsigned) -1)
753
88.7k
    {
754
88.7k
      unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
755
88.7k
      unsigned int j;
756
757
619k
      for (j = 0; j < num_group; j++)
758
536k
  {
759
    /* Begin search from previous found group.  */
760
536k
    unsigned i = (j + search_offset) % num_group;
761
762
536k
    Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
763
536k
    Elf_Internal_Group *idx;
764
536k
    bfd_size_type n_elt;
765
766
536k
    if (shdr == NULL)
767
3.59k
      continue;
768
769
532k
    idx = (Elf_Internal_Group *) shdr->contents;
770
532k
    if (idx == NULL || shdr->sh_size < 4)
771
163
      {
772
        /* See PR 21957 for a reproducer.  */
773
        /* xgettext:c-format */
774
163
        _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
775
163
          abfd, shdr->bfd_section);
776
163
        elf_tdata (abfd)->group_sect_ptr[i] = NULL;
777
163
        bfd_set_error (bfd_error_bad_value);
778
163
        return false;
779
163
      }
780
532k
    n_elt = shdr->sh_size / 4;
781
782
    /* Look through this group's sections to see if current
783
       section is a member.  */
784
5.03M
    while (--n_elt != 0)
785
4.56M
      if ((++idx)->shdr == hdr)
786
59.4k
        {
787
59.4k
    asection *s = NULL;
788
789
    /* We are a member of this group.  Go looking through
790
       other members to see if any others are linked via
791
       next_in_group.  */
792
59.4k
    idx = (Elf_Internal_Group *) shdr->contents;
793
59.4k
    n_elt = shdr->sh_size / 4;
794
2.13M
    while (--n_elt != 0)
795
2.10M
      if ((++idx)->shdr != NULL
796
2.10M
          && (s = idx->shdr->bfd_section) != NULL
797
2.10M
          && elf_next_in_group (s) != NULL)
798
28.8k
        break;
799
59.4k
    if (n_elt != 0)
800
28.8k
      {
801
        /* Snarf the group name from other member, and
802
           insert current section in circular list.  */
803
28.8k
        elf_group_name (newsect) = elf_group_name (s);
804
28.8k
        elf_next_in_group (newsect) = elf_next_in_group (s);
805
28.8k
        elf_next_in_group (s) = newsect;
806
28.8k
      }
807
30.5k
    else
808
30.5k
      {
809
30.5k
        const char *gname;
810
811
30.5k
        gname = group_signature (abfd, shdr);
812
30.5k
        if (gname == NULL)
813
5.43k
          return false;
814
25.1k
        elf_group_name (newsect) = gname;
815
816
        /* Start a circular list with one element.  */
817
25.1k
        elf_next_in_group (newsect) = newsect;
818
25.1k
      }
819
820
    /* If the group section has been created, point to the
821
       new member.  */
822
53.9k
    if (shdr->bfd_section != NULL)
823
53.9k
      elf_next_in_group (shdr->bfd_section) = newsect;
824
825
53.9k
    elf_tdata (abfd)->group_search_offset = i;
826
53.9k
    j = num_group - 1;
827
53.9k
    break;
828
59.4k
        }
829
532k
  }
830
88.7k
    }
831
832
329k
  if (elf_group_name (newsect) == NULL)
833
275k
    {
834
      /* xgettext:c-format */
835
275k
      _bfd_error_handler (_("%pB: no group info for section '%pA'"),
836
275k
        abfd, newsect);
837
      /* PR 29532: Return true here, even though the group info has not been
838
   read.  Separate debug info files can have empty group sections, but
839
   we do not want this to prevent them from being loaded as otherwise
840
   GDB will not be able to use them.  */
841
275k
      return true;
842
275k
    }
843
53.9k
  return true;
844
329k
}
845
846
bool
847
_bfd_elf_setup_sections (bfd *abfd)
848
187k
{
849
187k
  unsigned int i;
850
187k
  unsigned int num_group = elf_tdata (abfd)->num_group;
851
187k
  bool result = true;
852
187k
  asection *s;
853
854
  /* Process SHF_LINK_ORDER.  */
855
1.40M
  for (s = abfd->sections; s != NULL; s = s->next)
856
1.21M
    {
857
1.21M
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
858
1.21M
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
859
167k
  {
860
167k
    unsigned int elfsec = this_hdr->sh_link;
861
    /* An sh_link value of 0 is now allowed.  It indicates that linked
862
       to section has already been discarded, but that the current
863
       section has been retained for some other reason.  This linking
864
       section is still a candidate for later garbage collection
865
       however.  */
866
167k
    if (elfsec == 0)
867
140k
      {
868
140k
        elf_linked_to_section (s) = NULL;
869
140k
      }
870
27.3k
    else
871
27.3k
      {
872
27.3k
        asection *linksec = NULL;
873
874
27.3k
        if (elfsec < elf_numsections (abfd))
875
27.3k
    {
876
27.3k
      this_hdr = elf_elfsections (abfd)[elfsec];
877
27.3k
      linksec = this_hdr->bfd_section;
878
27.3k
    }
879
880
        /* PR 1991, 2008:
881
     Some strip/objcopy may leave an incorrect value in
882
     sh_link.  We don't want to proceed.  */
883
27.3k
        if (linksec == NULL)
884
1.66k
    {
885
1.66k
      _bfd_error_handler
886
        /* xgettext:c-format */
887
1.66k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888
1.66k
         s->owner, elfsec, s);
889
1.66k
      result = false;
890
1.66k
    }
891
892
27.3k
        elf_linked_to_section (s) = linksec;
893
27.3k
      }
894
167k
  }
895
1.04M
      else if (this_hdr->sh_type == SHT_GROUP
896
1.04M
         && elf_next_in_group (s) == NULL)
897
6.82k
  {
898
6.82k
    _bfd_error_handler
899
      /* xgettext:c-format */
900
6.82k
      (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901
6.82k
       abfd, elf_section_data (s)->this_idx);
902
6.82k
    result = false;
903
6.82k
  }
904
1.21M
    }
905
906
  /* Process section groups.  */
907
187k
  if (num_group == (unsigned) -1)
908
89.9k
    return result;
909
910
122k
  for (i = 0; i < num_group; i++)
911
25.2k
    {
912
25.2k
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
913
25.2k
      Elf_Internal_Group *idx;
914
25.2k
      unsigned int n_elt;
915
916
      /* PR binutils/18758: Beware of corrupt binaries with invalid
917
   group data.  */
918
25.2k
      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
919
28
  {
920
28
    _bfd_error_handler
921
      /* xgettext:c-format */
922
28
      (_("%pB: section group entry number %u is corrupt"),
923
28
       abfd, i);
924
28
    result = false;
925
28
    continue;
926
28
  }
927
928
25.2k
      idx = (Elf_Internal_Group *) shdr->contents;
929
25.2k
      n_elt = shdr->sh_size / 4;
930
931
1.42M
      while (--n_elt != 0)
932
1.39M
  {
933
1.39M
    ++ idx;
934
935
1.39M
    if (idx->shdr == NULL)
936
1.04M
      continue;
937
346k
    else if (idx->shdr->bfd_section)
938
55.9k
      elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
939
290k
    else if (idx->shdr->sh_type != SHT_RELA
940
290k
       && idx->shdr->sh_type != SHT_REL)
941
212k
      {
942
        /* There are some unknown sections in the group.  */
943
212k
        _bfd_error_handler
944
    /* xgettext:c-format */
945
212k
    (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
946
212k
     abfd,
947
212k
     idx->shdr->sh_type,
948
212k
     bfd_elf_string_from_elf_section (abfd,
949
212k
              (elf_elfheader (abfd)
950
212k
               ->e_shstrndx),
951
212k
              idx->shdr->sh_name),
952
212k
     shdr->bfd_section);
953
212k
        result = false;
954
212k
      }
955
1.39M
  }
956
25.2k
    }
957
958
97.7k
  return result;
959
187k
}
960
961
bool
962
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
963
0
{
964
0
  return elf_next_in_group (sec) != NULL;
965
0
}
966
967
const char *
968
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
969
0
{
970
0
  if (elf_sec_group (sec) != NULL)
971
0
    return elf_group_name (sec);
972
0
  return NULL;
973
0
}
974
975
/* Make a BFD section from an ELF section.  We store a pointer to the
976
   BFD section in the bfd_section field of the header.  */
977
978
bool
979
_bfd_elf_make_section_from_shdr (bfd *abfd,
980
         Elf_Internal_Shdr *hdr,
981
         const char *name,
982
         int shindex)
983
1.54M
{
984
1.54M
  asection *newsect;
985
1.54M
  flagword flags;
986
1.54M
  const struct elf_backend_data *bed;
987
1.54M
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
988
989
1.54M
  if (hdr->bfd_section != NULL)
990
141k
    return true;
991
992
1.40M
  newsect = bfd_make_section_anyway (abfd, name);
993
1.40M
  if (newsect == NULL)
994
0
    return false;
995
996
1.40M
  hdr->bfd_section = newsect;
997
1.40M
  elf_section_data (newsect)->this_hdr = *hdr;
998
1.40M
  elf_section_data (newsect)->this_idx = shindex;
999
1000
  /* Always use the real type/flags.  */
1001
1.40M
  elf_section_type (newsect) = hdr->sh_type;
1002
1.40M
  elf_section_flags (newsect) = hdr->sh_flags;
1003
1004
1.40M
  newsect->filepos = hdr->sh_offset;
1005
1006
1.40M
  flags = SEC_NO_FLAGS;
1007
1.40M
  if (hdr->sh_type != SHT_NOBITS)
1008
1.34M
    flags |= SEC_HAS_CONTENTS;
1009
1.40M
  if (hdr->sh_type == SHT_GROUP)
1010
44.3k
    flags |= SEC_GROUP;
1011
1.40M
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
1012
508k
    {
1013
508k
      flags |= SEC_ALLOC;
1014
508k
      if (hdr->sh_type != SHT_NOBITS)
1015
465k
  flags |= SEC_LOAD;
1016
508k
    }
1017
1.40M
  if ((hdr->sh_flags & SHF_WRITE) == 0)
1018
1.08M
    flags |= SEC_READONLY;
1019
1.40M
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1020
297k
    flags |= SEC_CODE;
1021
1.10M
  else if ((flags & SEC_LOAD) != 0)
1022
251k
    flags |= SEC_DATA;
1023
1.40M
  if ((hdr->sh_flags & SHF_MERGE) != 0)
1024
202k
    {
1025
202k
      flags |= SEC_MERGE;
1026
202k
      newsect->entsize = hdr->sh_entsize;
1027
202k
    }
1028
1.40M
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
1029
266k
    flags |= SEC_STRINGS;
1030
1.40M
  if (hdr->sh_flags & SHF_GROUP)
1031
335k
    if (!setup_group (abfd, hdr, newsect))
1032
6.72k
      return false;
1033
1.39M
  if ((hdr->sh_flags & SHF_TLS) != 0)
1034
195k
    flags |= SEC_THREAD_LOCAL;
1035
1.39M
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1036
154k
    flags |= SEC_EXCLUDE;
1037
1038
1.39M
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1039
1.39M
    {
1040
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1041
   but binutils as of 2019-07-23 did not set the EI_OSABI header
1042
   byte.  */
1043
277k
    case ELFOSABI_GNU:
1044
598k
    case ELFOSABI_FREEBSD:
1045
598k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1046
117k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1047
      /* Fall through */
1048
759k
    case ELFOSABI_NONE:
1049
759k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1050
75.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1051
759k
      break;
1052
1.39M
    }
1053
1054
1.39M
  if ((flags & SEC_ALLOC) == 0)
1055
890k
    {
1056
      /* The debugging sections appear to be recognized only by name,
1057
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
1058
890k
      if (name [0] == '.')
1059
188k
  {
1060
188k
    if (startswith (name, ".debug")
1061
188k
        || startswith (name, ".gnu.debuglto_.debug_")
1062
188k
        || startswith (name, ".gnu.linkonce.wi.")
1063
188k
        || startswith (name, ".zdebug"))
1064
28.4k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1065
160k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1066
160k
       || startswith (name, ".note.gnu"))
1067
1.69k
      {
1068
1.69k
        flags |= SEC_ELF_OCTETS;
1069
1.69k
        opb = 1;
1070
1.69k
      }
1071
158k
    else if (startswith (name, ".line")
1072
158k
       || startswith (name, ".stab")
1073
158k
       || strcmp (name, ".gdb_index") == 0)
1074
11.5k
      flags |= SEC_DEBUGGING;
1075
188k
  }
1076
890k
    }
1077
1078
1.39M
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1079
1.39M
      || !bfd_set_section_size (newsect, hdr->sh_size)
1080
1.39M
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1081
1.39M
              & -hdr->sh_addralign)))
1082
1.03k
    return false;
1083
1084
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
1085
     only link a single copy of the section.  This is used to support
1086
     g++.  g++ will emit each template expansion in its own section.
1087
     The symbols will be defined as weak, so that multiple definitions
1088
     are permitted.  The GNU linker extension is to actually discard
1089
     all but one of the sections.  */
1090
1.39M
  if (startswith (name, ".gnu.linkonce")
1091
1.39M
      && elf_next_in_group (newsect) == NULL)
1092
4.76k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1093
1094
1.39M
  if (!bfd_set_section_flags (newsect, flags))
1095
0
    return false;
1096
1097
1.39M
  bed = get_elf_backend_data (abfd);
1098
1.39M
  if (bed->elf_backend_section_flags)
1099
261k
    if (!bed->elf_backend_section_flags (hdr))
1100
0
      return false;
1101
1102
  /* We do not parse the PT_NOTE segments as we are interested even in the
1103
     separate debug info files which may have the segments offsets corrupted.
1104
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1105
1.39M
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1106
43.0k
    {
1107
43.0k
      bfd_byte *contents;
1108
1109
43.0k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1110
1.73k
  return false;
1111
1112
41.3k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1113
41.3k
           hdr->sh_offset, hdr->sh_addralign);
1114
41.3k
      _bfd_elf_munmap_section_contents (newsect, contents);
1115
41.3k
    }
1116
1117
1.39M
  if ((newsect->flags & SEC_ALLOC) != 0)
1118
506k
    {
1119
506k
      Elf_Internal_Phdr *phdr;
1120
506k
      unsigned int i, nload;
1121
1122
      /* Some ELF linkers produce binaries with all the program header
1123
   p_paddr fields zero.  If we have such a binary with more than
1124
   one PT_LOAD header, then leave the section lma equal to vma
1125
   so that we don't create sections with overlapping lma.  */
1126
506k
      phdr = elf_tdata (abfd)->phdr;
1127
645k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1128
316k
  if (phdr->p_paddr != 0)
1129
177k
    break;
1130
138k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1131
23.9k
    ++nload;
1132
506k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1133
6.41k
  return true;
1134
1135
500k
      phdr = elf_tdata (abfd)->phdr;
1136
42.0M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1137
41.6M
  {
1138
41.6M
    if (((phdr->p_type == PT_LOAD
1139
41.6M
    && (hdr->sh_flags & SHF_TLS) == 0)
1140
41.6M
         || phdr->p_type == PT_TLS)
1141
41.6M
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1142
38.5k
      {
1143
38.5k
        if ((newsect->flags & SEC_LOAD) == 0)
1144
8.75k
    newsect->lma = (phdr->p_paddr
1145
8.75k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1146
29.8k
        else
1147
    /* We used to use the same adjustment for SEC_LOAD
1148
       sections, but that doesn't work if the segment
1149
       is packed with code from multiple VMAs.
1150
       Instead we calculate the section LMA based on
1151
       the segment LMA.  It is assumed that the
1152
       segment will contain sections with contiguous
1153
       LMAs, even if the VMAs are not.  */
1154
29.8k
    newsect->lma = (phdr->p_paddr
1155
29.8k
        + hdr->sh_offset - phdr->p_offset) / opb;
1156
1157
        /* With contiguous segments, we can't tell from file
1158
     offsets whether a section with zero size should
1159
     be placed at the end of one segment or the
1160
     beginning of the next.  Decide based on vaddr.  */
1161
38.5k
        if (hdr->sh_addr >= phdr->p_vaddr
1162
38.5k
      && (hdr->sh_addr + hdr->sh_size
1163
38.5k
          <= phdr->p_vaddr + phdr->p_memsz))
1164
38.4k
    break;
1165
38.5k
      }
1166
41.6M
  }
1167
500k
    }
1168
1169
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1170
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1171
1.38M
  if ((newsect->flags & SEC_DEBUGGING) != 0
1172
1.38M
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1173
1.38M
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1174
28.3k
    {
1175
28.3k
      enum { nothing, compress, decompress } action = nothing;
1176
28.3k
      int compression_header_size;
1177
28.3k
      bfd_size_type uncompressed_size;
1178
28.3k
      unsigned int uncompressed_align_power;
1179
28.3k
      enum compression_type ch_type = ch_none;
1180
28.3k
      bool compressed
1181
28.3k
  = bfd_is_section_compressed_info (abfd, newsect,
1182
28.3k
            &compression_header_size,
1183
28.3k
            &uncompressed_size,
1184
28.3k
            &uncompressed_align_power,
1185
28.3k
            &ch_type);
1186
1187
      /* Should we decompress?  */
1188
28.3k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1189
55
  action = decompress;
1190
1191
      /* Should we compress?  Or convert to a different compression?  */
1192
28.2k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1193
28.2k
         && newsect->size != 0
1194
28.2k
         && compression_header_size >= 0
1195
28.2k
         && uncompressed_size > 0)
1196
269
  {
1197
269
    if (!compressed)
1198
269
      action = compress;
1199
0
    else
1200
0
      {
1201
0
        enum compression_type new_ch_type = ch_none;
1202
0
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1203
0
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1204
0
             ? ch_compress_zstd : ch_compress_zlib);
1205
0
        if (new_ch_type != ch_type)
1206
0
    action = compress;
1207
0
      }
1208
269
  }
1209
1210
28.3k
      if (action == compress)
1211
269
  {
1212
269
    if (!bfd_init_section_compress_status (abfd, newsect))
1213
0
      {
1214
0
        _bfd_error_handler
1215
    /* xgettext:c-format */
1216
0
    (_("%pB: unable to compress section %s"), abfd, name);
1217
0
        return false;
1218
0
      }
1219
269
  }
1220
28.0k
      else if (action == decompress)
1221
55
  {
1222
55
    if (!bfd_init_section_decompress_status (abfd, newsect))
1223
51
      {
1224
51
        _bfd_error_handler
1225
    /* xgettext:c-format */
1226
51
    (_("%pB: unable to decompress section %s"), abfd, name);
1227
51
        return false;
1228
51
      }
1229
4
#ifndef HAVE_ZSTD
1230
4
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1231
0
      {
1232
0
        _bfd_error_handler
1233
      /* xgettext:c-format */
1234
0
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1235
0
          "is not built with zstd support"),
1236
0
       abfd, name);
1237
0
        newsect->compress_status = COMPRESS_SECTION_NONE;
1238
0
        return false;
1239
0
      }
1240
4
#endif
1241
4
    if (abfd->is_linker_input
1242
4
        && name[1] == 'z')
1243
0
      {
1244
        /* Rename section from .zdebug_* to .debug_* so that ld
1245
     scripts will see this section as a debug section.  */
1246
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1247
0
        if (new_name == NULL)
1248
0
    return false;
1249
0
        bfd_rename_section (newsect, new_name);
1250
0
      }
1251
4
  }
1252
28.3k
    }
1253
1254
1.38M
  return true;
1255
1.38M
}
1256
1257
const char *const bfd_elf_section_type_names[] =
1258
{
1259
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1260
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1261
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1262
};
1263
1264
/* ELF relocs are against symbols.  If we are producing relocatable
1265
   output, and the reloc is against an external symbol, and nothing
1266
   has given us any additional addend, the resulting reloc will also
1267
   be against the same symbol.  In such a case, we don't want to
1268
   change anything about the way the reloc is handled, since it will
1269
   all be done at final link time.  Rather than put special case code
1270
   into bfd_perform_relocation, all the reloc types use this howto
1271
   function, or should call this function for relocatable output.  */
1272
1273
bfd_reloc_status_type
1274
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1275
           arelent *reloc_entry,
1276
           asymbol *symbol,
1277
           void *data ATTRIBUTE_UNUSED,
1278
           asection *input_section,
1279
           bfd *output_bfd,
1280
           char **error_message ATTRIBUTE_UNUSED)
1281
197k
{
1282
197k
  if (output_bfd != NULL
1283
197k
      && (symbol->flags & BSF_SECTION_SYM) == 0
1284
197k
      && (! reloc_entry->howto->partial_inplace
1285
0
    || reloc_entry->addend == 0))
1286
0
    {
1287
0
      reloc_entry->address += input_section->output_offset;
1288
0
      return bfd_reloc_ok;
1289
0
    }
1290
1291
  /* In some cases the relocation should be treated as output section
1292
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1293
     targets lack section relative relocations and instead use
1294
     ordinary absolute relocations for references between DWARF
1295
     sections.  That is arguably a bug in those targets but it happens
1296
     to work for the usual case of linking to non-loaded ELF debug
1297
     sections with VMAs forced to zero.  PE COFF on the other hand
1298
     doesn't allow a section VMA of zero.  */
1299
197k
  if (output_bfd == NULL
1300
197k
      && !reloc_entry->howto->pc_relative
1301
197k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1302
197k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1303
27.7k
    reloc_entry->addend -= symbol->section->output_section->vma;
1304
1305
197k
  return bfd_reloc_continue;
1306
197k
}
1307

1308
/* Returns TRUE if section A matches section B.
1309
   Names, addresses and links may be different, but everything else
1310
   should be the same.  */
1311
1312
static bool
1313
section_match (const Elf_Internal_Shdr * a,
1314
         const Elf_Internal_Shdr * b)
1315
0
{
1316
0
  if (a->sh_type != b->sh_type
1317
0
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1318
0
      || a->sh_addralign != b->sh_addralign
1319
0
      || a->sh_entsize != b->sh_entsize)
1320
0
    return false;
1321
0
  if (a->sh_type == SHT_SYMTAB
1322
0
      || a->sh_type == SHT_STRTAB)
1323
0
    return true;
1324
0
  return a->sh_size == b->sh_size;
1325
0
}
1326
1327
/* Find a section in OBFD that has the same characteristics
1328
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1329
   none can be found.  Check's section HINT first, as this is likely
1330
   to be the correct section.  */
1331
1332
static unsigned int
1333
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1334
     const unsigned int hint)
1335
0
{
1336
0
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1337
0
  unsigned int i;
1338
1339
0
  BFD_ASSERT (iheader != NULL);
1340
1341
  /* See PR 20922 for a reproducer of the NULL test.  */
1342
0
  if (hint < elf_numsections (obfd)
1343
0
      && oheaders[hint] != NULL
1344
0
      && section_match (oheaders[hint], iheader))
1345
0
    return hint;
1346
1347
0
  for (i = 1; i < elf_numsections (obfd); i++)
1348
0
    {
1349
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1350
1351
0
      if (oheader == NULL)
1352
0
  continue;
1353
0
      if (section_match (oheader, iheader))
1354
  /* FIXME: Do we care if there is a potential for
1355
     multiple matches ?  */
1356
0
  return i;
1357
0
    }
1358
1359
0
  return SHN_UNDEF;
1360
0
}
1361
1362
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1363
   Processor specific section, based upon a matching input section.
1364
   Returns TRUE upon success, FALSE otherwise.  */
1365
1366
static bool
1367
copy_special_section_fields (const bfd *ibfd,
1368
           bfd *obfd,
1369
           const Elf_Internal_Shdr *iheader,
1370
           Elf_Internal_Shdr *oheader,
1371
           const unsigned int secnum)
1372
33
{
1373
33
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1374
33
  const Elf_Internal_Shdr **iheaders
1375
33
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1376
33
  bool changed = false;
1377
33
  unsigned int sh_link;
1378
1379
33
  if (oheader->sh_type == SHT_NOBITS)
1380
12
    {
1381
      /* This is a feature for objcopy --only-keep-debug:
1382
   When a section's type is changed to NOBITS, we preserve
1383
   the sh_link and sh_info fields so that they can be
1384
   matched up with the original.
1385
1386
   Note: Strictly speaking these assignments are wrong.
1387
   The sh_link and sh_info fields should point to the
1388
   relevent sections in the output BFD, which may not be in
1389
   the same location as they were in the input BFD.  But
1390
   the whole point of this action is to preserve the
1391
   original values of the sh_link and sh_info fields, so
1392
   that they can be matched up with the section headers in
1393
   the original file.  So strictly speaking we may be
1394
   creating an invalid ELF file, but it is only for a file
1395
   that just contains debug info and only for sections
1396
   without any contents.  */
1397
12
      if (oheader->sh_link == 0)
1398
12
  oheader->sh_link = iheader->sh_link;
1399
12
      if (oheader->sh_info == 0)
1400
12
  oheader->sh_info = iheader->sh_info;
1401
12
      return true;
1402
12
    }
1403
1404
  /* Allow the target a chance to decide how these fields should be set.  */
1405
21
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1406
21
                iheader, oheader))
1407
21
    return true;
1408
1409
  /* We have an iheader which might match oheader, and which has non-zero
1410
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1411
     the section in the output bfd which corresponds to the linked section
1412
     in the input bfd.  */
1413
0
  if (iheader->sh_link != SHN_UNDEF)
1414
0
    {
1415
      /* See PR 20931 for a reproducer.  */
1416
0
      if (iheader->sh_link >= elf_numsections (ibfd))
1417
0
  {
1418
0
    _bfd_error_handler
1419
      /* xgettext:c-format */
1420
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1421
0
       ibfd, iheader->sh_link, secnum);
1422
0
    return false;
1423
0
  }
1424
1425
0
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1426
0
      if (sh_link != SHN_UNDEF)
1427
0
  {
1428
0
    oheader->sh_link = sh_link;
1429
0
    changed = true;
1430
0
  }
1431
0
      else
1432
  /* FIXME: Should we install iheader->sh_link
1433
     if we could not find a match ?  */
1434
0
  _bfd_error_handler
1435
    /* xgettext:c-format */
1436
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1437
0
    }
1438
1439
0
  if (iheader->sh_info)
1440
0
    {
1441
      /* The sh_info field can hold arbitrary information, but if the
1442
   SHF_LINK_INFO flag is set then it should be interpreted as a
1443
   section index.  */
1444
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1445
0
  {
1446
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1447
0
             iheader->sh_info);
1448
0
    if (sh_link != SHN_UNDEF)
1449
0
      oheader->sh_flags |= SHF_INFO_LINK;
1450
0
  }
1451
0
      else
1452
  /* No idea what it means - just copy it.  */
1453
0
  sh_link = iheader->sh_info;
1454
1455
0
      if (sh_link != SHN_UNDEF)
1456
0
  {
1457
0
    oheader->sh_info = sh_link;
1458
0
    changed = true;
1459
0
  }
1460
0
      else
1461
0
  _bfd_error_handler
1462
    /* xgettext:c-format */
1463
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1464
0
    }
1465
1466
0
  return changed;
1467
0
}
1468
1469
/* Copy the program header and other data from one object module to
1470
   another.  */
1471
1472
bool
1473
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1474
27
{
1475
27
  const Elf_Internal_Shdr **iheaders
1476
27
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1477
27
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1478
27
  const struct elf_backend_data *bed;
1479
27
  unsigned int i;
1480
1481
27
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1482
27
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1483
0
    return true;
1484
1485
27
  if (!elf_flags_init (obfd))
1486
27
    {
1487
27
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1488
27
      elf_flags_init (obfd) = true;
1489
27
    }
1490
1491
27
  elf_gp (obfd) = elf_gp (ibfd);
1492
1493
  /* Also copy the EI_OSABI field.  */
1494
27
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1495
27
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1496
1497
  /* If set, copy the EI_ABIVERSION field.  */
1498
27
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1499
0
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1500
0
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1501
1502
  /* Copy object attributes.  */
1503
27
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1504
1505
27
  if (iheaders == NULL || oheaders == NULL)
1506
16
    return true;
1507
1508
11
  bed = get_elf_backend_data (obfd);
1509
1510
  /* Possibly copy other fields in the section header.  */
1511
1.69k
  for (i = 1; i < elf_numsections (obfd); i++)
1512
1.68k
    {
1513
1.68k
      unsigned int j;
1514
1.68k
      Elf_Internal_Shdr * oheader = oheaders[i];
1515
1516
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1517
   because of a special case need for generating separate debug info
1518
   files.  See below for more details.  */
1519
1.68k
      if (oheader == NULL
1520
1.68k
    || (oheader->sh_type != SHT_NOBITS
1521
1.68k
        && oheader->sh_type < SHT_LOOS))
1522
1.64k
  continue;
1523
1524
      /* Ignore empty sections, and sections whose
1525
   fields have already been initialised.  */
1526
39
      if (oheader->sh_size == 0
1527
39
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1528
6
  continue;
1529
1530
      /* Scan for the matching section in the input bfd.
1531
   First we try for a direct mapping between the input and
1532
   output sections.  */
1533
3.91k
      for (j = 1; j < elf_numsections (ibfd); j++)
1534
3.91k
  {
1535
3.91k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1536
1537
3.91k
    if (iheader == NULL)
1538
0
      continue;
1539
1540
3.91k
    if (oheader->bfd_section != NULL
1541
3.91k
        && iheader->bfd_section != NULL
1542
3.91k
        && iheader->bfd_section->output_section != NULL
1543
3.91k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1544
33
      {
1545
        /* We have found a connection from the input section to
1546
     the output section.  Attempt to copy the header fields.
1547
     If this fails then do not try any further sections -
1548
     there should only be a one-to-one mapping between
1549
     input and output.  */
1550
33
        if (!copy_special_section_fields (ibfd, obfd,
1551
33
            iheader, oheader, i))
1552
0
    j = elf_numsections (ibfd);
1553
33
        break;
1554
33
      }
1555
3.91k
  }
1556
1557
33
      if (j < elf_numsections (ibfd))
1558
33
  continue;
1559
1560
      /* That failed.  So try to deduce the corresponding input section.
1561
   Unfortunately we cannot compare names as the output string table
1562
   is empty, so instead we check size, address and type.  */
1563
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1564
0
  {
1565
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1566
1567
0
    if (iheader == NULL)
1568
0
      continue;
1569
1570
    /* Try matching fields in the input section's header.
1571
       Since --only-keep-debug turns all non-debug sections into
1572
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1573
       input type.  */
1574
0
    if ((oheader->sh_type == SHT_NOBITS
1575
0
         || iheader->sh_type == oheader->sh_type)
1576
0
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1577
0
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1578
0
        && iheader->sh_addralign == oheader->sh_addralign
1579
0
        && iheader->sh_entsize == oheader->sh_entsize
1580
0
        && iheader->sh_size == oheader->sh_size
1581
0
        && iheader->sh_addr == oheader->sh_addr
1582
0
        && (iheader->sh_info != oheader->sh_info
1583
0
      || iheader->sh_link != oheader->sh_link))
1584
0
      {
1585
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1586
0
    break;
1587
0
      }
1588
0
  }
1589
1590
0
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1591
0
  {
1592
    /* Final attempt.  Call the backend copy function
1593
       with a NULL input section.  */
1594
0
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1595
0
                     NULL, oheader);
1596
0
  }
1597
0
    }
1598
1599
11
  return true;
1600
27
}
1601
1602
static const char *
1603
get_segment_type (unsigned int p_type)
1604
102k
{
1605
102k
  const char *pt;
1606
102k
  switch (p_type)
1607
102k
    {
1608
21.4k
    case PT_NULL: pt = "NULL"; break;
1609
1.20k
    case PT_LOAD: pt = "LOAD"; break;
1610
407
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1611
460
    case PT_INTERP: pt = "INTERP"; break;
1612
134
    case PT_NOTE: pt = "NOTE"; break;
1613
194
    case PT_SHLIB: pt = "SHLIB"; break;
1614
1.31k
    case PT_PHDR: pt = "PHDR"; break;
1615
43
    case PT_TLS: pt = "TLS"; break;
1616
65
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1617
60
    case PT_GNU_STACK: pt = "STACK"; break;
1618
57
    case PT_GNU_RELRO: pt = "RELRO"; break;
1619
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1620
76.7k
    default: pt = NULL; break;
1621
102k
    }
1622
102k
  return pt;
1623
102k
}
1624
1625
/* Print out the program headers.  */
1626
1627
bool
1628
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1629
3.94k
{
1630
3.94k
  FILE *f = (FILE *) farg;
1631
3.94k
  Elf_Internal_Phdr *p;
1632
3.94k
  asection *s;
1633
3.94k
  bfd_byte *dynbuf = NULL;
1634
1635
3.94k
  p = elf_tdata (abfd)->phdr;
1636
3.94k
  if (p != NULL)
1637
3.20k
    {
1638
3.20k
      unsigned int i, c;
1639
1640
3.20k
      fprintf (f, _("\nProgram Header:\n"));
1641
3.20k
      c = elf_elfheader (abfd)->e_phnum;
1642
105k
      for (i = 0; i < c; i++, p++)
1643
102k
  {
1644
102k
    const char *pt = get_segment_type (p->p_type);
1645
102k
    char buf[20];
1646
1647
102k
    if (pt == NULL)
1648
76.7k
      {
1649
76.7k
        sprintf (buf, "0x%lx", p->p_type);
1650
76.7k
        pt = buf;
1651
76.7k
      }
1652
102k
    fprintf (f, "%8s off    0x", pt);
1653
102k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1654
102k
    fprintf (f, " vaddr 0x");
1655
102k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1656
102k
    fprintf (f, " paddr 0x");
1657
102k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1658
102k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1659
102k
    fprintf (f, "         filesz 0x");
1660
102k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1661
102k
    fprintf (f, " memsz 0x");
1662
102k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1663
102k
    fprintf (f, " flags %c%c%c",
1664
102k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1665
102k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1666
102k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1667
102k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1668
73.9k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1669
102k
    fprintf (f, "\n");
1670
102k
  }
1671
3.20k
    }
1672
1673
3.94k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1674
3.94k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1675
128
    {
1676
128
      unsigned int elfsec;
1677
128
      unsigned long shlink;
1678
128
      bfd_byte *extdyn, *extdynend;
1679
128
      size_t extdynsize;
1680
128
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1681
1682
128
      fprintf (f, _("\nDynamic Section:\n"));
1683
1684
128
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1685
0
  goto error_return;
1686
1687
128
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1688
128
      if (elfsec == SHN_BAD)
1689
0
  goto error_return;
1690
128
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1691
1692
128
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1693
128
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1694
1695
128
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1696
3.06k
     (size_t) (extdynend - extdyn) >= extdynsize;
1697
2.93k
     extdyn += extdynsize)
1698
3.04k
  {
1699
3.04k
    Elf_Internal_Dyn dyn;
1700
3.04k
    const char *name = "";
1701
3.04k
    char ab[20];
1702
3.04k
    bool stringp;
1703
3.04k
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1704
1705
3.04k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1706
1707
3.04k
    if (dyn.d_tag == DT_NULL)
1708
103
      break;
1709
1710
2.93k
    stringp = false;
1711
2.93k
    switch (dyn.d_tag)
1712
2.93k
      {
1713
710
      default:
1714
710
        if (bed->elf_backend_get_target_dtag)
1715
16
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1716
1717
710
        if (!strcmp (name, ""))
1718
694
    {
1719
694
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1720
694
      name = ab;
1721
694
    }
1722
710
        break;
1723
1724
267
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1725
88
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1726
95
      case DT_PLTGOT: name = "PLTGOT"; break;
1727
64
      case DT_HASH: name = "HASH"; break;
1728
93
      case DT_STRTAB: name = "STRTAB"; break;
1729
93
      case DT_SYMTAB: name = "SYMTAB"; break;
1730
64
      case DT_RELA: name = "RELA"; break;
1731
62
      case DT_RELASZ: name = "RELASZ"; break;
1732
63
      case DT_RELAENT: name = "RELAENT"; break;
1733
95
      case DT_STRSZ: name = "STRSZ"; break;
1734
93
      case DT_SYMENT: name = "SYMENT"; break;
1735
92
      case DT_INIT: name = "INIT"; break;
1736
91
      case DT_FINI: name = "FINI"; break;
1737
4
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1738
21
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1739
0
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1740
32
      case DT_REL: name = "REL"; break;
1741
32
      case DT_RELSZ: name = "RELSZ"; break;
1742
32
      case DT_RELENT: name = "RELENT"; break;
1743
0
      case DT_RELR: name = "RELR"; break;
1744
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1745
0
      case DT_RELRENT: name = "RELRENT"; break;
1746
89
      case DT_PLTREL: name = "PLTREL"; break;
1747
91
      case DT_DEBUG: name = "DEBUG"; break;
1748
0
      case DT_TEXTREL: name = "TEXTREL"; break;
1749
86
      case DT_JMPREL: name = "JMPREL"; break;
1750
0
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1751
33
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1752
34
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1753
34
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1754
34
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1755
18
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1756
24
      case DT_FLAGS: name = "FLAGS"; break;
1757
0
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1758
0
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1759
17
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1760
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1761
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1762
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1763
17
      case DT_FEATURE: name = "FEATURE"; break;
1764
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1765
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1766
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1767
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1768
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1769
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1770
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1771
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1772
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1773
26
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1774
6
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1775
41
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1776
72
      case DT_VERSYM: name = "VERSYM"; break;
1777
1
      case DT_VERDEF: name = "VERDEF"; break;
1778
0
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1779
89
      case DT_VERNEED: name = "VERNEED"; break;
1780
89
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1781
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1782
0
      case DT_USED: name = "USED"; break;
1783
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1784
46
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1785
2.93k
      }
1786
1787
2.93k
    fprintf (f, "  %-20s ", name);
1788
2.93k
    if (! stringp)
1789
2.62k
      {
1790
2.62k
        fprintf (f, "0x");
1791
2.62k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1792
2.62k
      }
1793
310
    else
1794
310
      {
1795
310
        const char *string;
1796
310
        unsigned int tagv = dyn.d_un.d_val;
1797
1798
310
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1799
310
        if (string == NULL)
1800
0
    goto error_return;
1801
310
        fprintf (f, "%s", string);
1802
310
      }
1803
2.93k
    fprintf (f, "\n");
1804
2.93k
  }
1805
1806
128
      _bfd_elf_munmap_section_contents (s, dynbuf);
1807
128
      dynbuf = NULL;
1808
128
    }
1809
1810
3.94k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1811
3.94k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1812
121
    {
1813
121
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1814
28
  return false;
1815
121
    }
1816
1817
3.91k
  if (elf_dynverdef (abfd) != 0)
1818
0
    {
1819
0
      Elf_Internal_Verdef *t;
1820
1821
0
      fprintf (f, _("\nVersion definitions:\n"));
1822
0
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1823
0
  {
1824
0
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1825
0
       t->vd_flags, t->vd_hash,
1826
0
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1827
0
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1828
0
      {
1829
0
        Elf_Internal_Verdaux *a;
1830
1831
0
        fprintf (f, "\t");
1832
0
        for (a = t->vd_auxptr->vda_nextptr;
1833
0
       a != NULL;
1834
0
       a = a->vda_nextptr)
1835
0
    fprintf (f, "%s ",
1836
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1837
0
        fprintf (f, "\n");
1838
0
      }
1839
0
  }
1840
0
    }
1841
1842
3.91k
  if (elf_dynverref (abfd) != 0)
1843
93
    {
1844
93
      Elf_Internal_Verneed *t;
1845
1846
93
      fprintf (f, _("\nVersion References:\n"));
1847
318
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1848
225
  {
1849
225
    Elf_Internal_Vernaux *a;
1850
1851
225
    fprintf (f, _("  required from %s:\n"),
1852
225
       t->vn_filename ? t->vn_filename : "<corrupt>");
1853
709
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1854
484
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1855
484
         a->vna_flags, a->vna_other,
1856
484
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1857
225
  }
1858
93
    }
1859
1860
3.91k
  return true;
1861
1862
0
 error_return:
1863
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
1864
0
  return false;
1865
3.94k
}
1866
1867
/* Find the file offset corresponding to VMA by using the program
1868
   headers.  */
1869
1870
static file_ptr
1871
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1872
     size_t size, size_t *max_size_p)
1873
817
{
1874
817
  Elf_Internal_Phdr *seg;
1875
817
  size_t i;
1876
1877
3.09k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1878
2.99k
    if (seg->p_type == PT_LOAD
1879
2.99k
  && vma >= (seg->p_vaddr & -seg->p_align)
1880
2.99k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1881
719
      {
1882
719
  if (max_size_p)
1883
12
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1884
719
  return vma - seg->p_vaddr + seg->p_offset;
1885
719
      }
1886
1887
98
  if (max_size_p)
1888
7
    *max_size_p = 0;
1889
98
  bfd_set_error (bfd_error_invalid_operation);
1890
98
  return (file_ptr) -1;
1891
817
}
1892
1893
/* Convert hash table to internal form.  */
1894
1895
static bfd_vma *
1896
get_hash_table_data (bfd *abfd, bfd_size_type number,
1897
         unsigned int ent_size, bfd_size_type filesize)
1898
146
{
1899
146
  unsigned char *e_data = NULL;
1900
146
  bfd_vma *i_data = NULL;
1901
146
  bfd_size_type size;
1902
146
  void *e_data_addr;
1903
146
  size_t e_data_size ATTRIBUTE_UNUSED;
1904
1905
146
  if (ent_size != 4 && ent_size != 8)
1906
0
    return NULL;
1907
1908
146
  if ((size_t) number != number)
1909
0
    {
1910
0
      bfd_set_error (bfd_error_file_too_big);
1911
0
      return NULL;
1912
0
    }
1913
1914
146
  size = ent_size * number;
1915
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1916
     attempting to allocate memory when the read is bound to fail.  */
1917
146
  if (size > filesize
1918
146
      || number >= ~(size_t) 0 / ent_size
1919
146
      || number >= ~(size_t) 0 / sizeof (*i_data))
1920
14
    {
1921
14
      bfd_set_error (bfd_error_file_too_big);
1922
14
      return NULL;
1923
14
    }
1924
1925
132
  e_data = _bfd_mmap_readonly_temporary (abfd, size, &e_data_addr,
1926
132
           &e_data_size);
1927
132
  if (e_data == NULL)
1928
5
    return NULL;
1929
1930
127
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1931
127
  if (i_data == NULL)
1932
0
    {
1933
0
      free (e_data);
1934
0
      return NULL;
1935
0
    }
1936
1937
127
  if (ent_size == 4)
1938
108k
    while (number--)
1939
108k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1940
0
  else
1941
0
    while (number--)
1942
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1943
1944
127
  _bfd_munmap_readonly_temporary (e_data_addr, e_data_size);
1945
127
  return i_data;
1946
127
}
1947
1948
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1949
   support DT_MIPS_XHASH?  */
1950
267k
#define DT_MIPS_XHASH        0x70000036
1951
1952
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1953
1954
bool
1955
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1956
            Elf_Internal_Phdr *phdrs, size_t phnum,
1957
            bfd_size_type filesize)
1958
4.24k
{
1959
4.24k
  bfd_byte *extdyn, *extdynend;
1960
4.24k
  size_t extdynsize;
1961
4.24k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1962
4.24k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1963
4.24k
        Elf_Internal_Sym *);
1964
4.24k
  Elf_Internal_Dyn dyn;
1965
4.24k
  bfd_vma dt_hash = 0;
1966
4.24k
  bfd_vma dt_gnu_hash = 0;
1967
4.24k
  bfd_vma dt_mips_xhash = 0;
1968
4.24k
  bfd_vma dt_strtab = 0;
1969
4.24k
  bfd_vma dt_symtab = 0;
1970
4.24k
  size_t dt_strsz = 0;
1971
4.24k
  bfd_vma dt_versym = 0;
1972
4.24k
  bfd_vma dt_verdef = 0;
1973
4.24k
  bfd_vma dt_verneed = 0;
1974
4.24k
  bfd_byte *dynbuf = NULL;
1975
4.24k
  char *strbuf = NULL;
1976
4.24k
  bfd_vma *gnubuckets = NULL;
1977
4.24k
  bfd_vma *gnuchains = NULL;
1978
4.24k
  bfd_vma *mipsxlat = NULL;
1979
4.24k
  file_ptr saved_filepos, filepos;
1980
4.24k
  bool res = false;
1981
4.24k
  size_t amt;
1982
4.24k
  bfd_byte *esymbuf = NULL, *esym;
1983
4.24k
  bfd_size_type symcount;
1984
4.24k
  Elf_Internal_Sym *isymbuf = NULL;
1985
4.24k
  Elf_Internal_Sym *isym, *isymend;
1986
4.24k
  bfd_byte *versym = NULL;
1987
4.24k
  bfd_byte *verdef = NULL;
1988
4.24k
  bfd_byte *verneed = NULL;
1989
4.24k
  size_t verdef_size = 0;
1990
4.24k
  size_t verneed_size = 0;
1991
4.24k
  size_t extsym_size;
1992
4.24k
  const struct elf_backend_data *bed;
1993
4.24k
  void *dynbuf_addr = NULL;
1994
4.24k
  void *esymbuf_addr = NULL;
1995
4.24k
  size_t dynbuf_size = 0;
1996
4.24k
  size_t esymbuf_size = 0;
1997
1998
  /* Return TRUE if symbol table is bad.  */
1999
4.24k
  if (elf_bad_symtab (abfd))
2000
0
    return true;
2001
2002
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
2003
4.24k
  if (elf_tdata (abfd)->dt_strtab != NULL)
2004
0
    return true;
2005
2006
4.24k
  bed = get_elf_backend_data (abfd);
2007
2008
  /* Save file position for elf_object_p.  */
2009
4.24k
  saved_filepos = bfd_tell (abfd);
2010
2011
4.24k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2012
0
    goto error_return;
2013
2014
4.24k
  dynbuf_size = phdr->p_filesz;
2015
4.24k
  dynbuf = _bfd_mmap_readonly_temporary (abfd, dynbuf_size,
2016
4.24k
           &dynbuf_addr, &dynbuf_size);
2017
4.24k
  if (dynbuf == NULL)
2018
347
    goto error_return;
2019
2020
3.89k
  extsym_size = bed->s->sizeof_sym;
2021
3.89k
  extdynsize = bed->s->sizeof_dyn;
2022
3.89k
  swap_dyn_in = bed->s->swap_dyn_in;
2023
2024
3.89k
  extdyn = dynbuf;
2025
3.89k
  if (phdr->p_filesz < extdynsize)
2026
50
    goto error_return;
2027
3.84k
  extdynend = extdyn + phdr->p_filesz;
2028
141k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2029
139k
    {
2030
139k
      swap_dyn_in (abfd, extdyn, &dyn);
2031
2032
139k
      if (dyn.d_tag == DT_NULL)
2033
1.75k
  break;
2034
2035
137k
      switch (dyn.d_tag)
2036
137k
  {
2037
1.13k
  case DT_HASH:
2038
1.13k
    dt_hash = dyn.d_un.d_val;
2039
1.13k
    break;
2040
255
  case DT_GNU_HASH:
2041
255
    if (bed->elf_machine_code != EM_MIPS
2042
255
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
2043
255
      dt_gnu_hash = dyn.d_un.d_val;
2044
255
    break;
2045
736
  case DT_STRTAB:
2046
736
    dt_strtab = dyn.d_un.d_val;
2047
736
    break;
2048
450
  case DT_SYMTAB:
2049
450
    dt_symtab = dyn.d_un.d_val;
2050
450
    break;
2051
540
  case DT_STRSZ:
2052
540
    dt_strsz = dyn.d_un.d_val;
2053
540
    break;
2054
245
  case DT_SYMENT:
2055
245
    if (dyn.d_un.d_val != extsym_size)
2056
40
      goto error_return;
2057
205
    break;
2058
205
  case DT_VERSYM:
2059
140
    dt_versym = dyn.d_un.d_val;
2060
140
    break;
2061
52
  case DT_VERDEF:
2062
52
    dt_verdef = dyn.d_un.d_val;
2063
52
    break;
2064
86
  case DT_VERNEED:
2065
86
    dt_verneed = dyn.d_un.d_val;
2066
86
    break;
2067
133k
  default:
2068
133k
    if (dyn.d_tag == DT_MIPS_XHASH
2069
133k
        && (bed->elf_machine_code == EM_MIPS
2070
7
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
2071
0
      {
2072
0
        dt_gnu_hash = dyn.d_un.d_val;
2073
0
        dt_mips_xhash = dyn.d_un.d_val;
2074
0
      }
2075
133k
    break;
2076
137k
  }
2077
137k
    }
2078
2079
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2080
     segment.  */
2081
3.80k
  if ((!dt_hash && !dt_gnu_hash)
2082
3.80k
      || !dt_strtab
2083
3.80k
      || !dt_symtab
2084
3.80k
      || !dt_strsz)
2085
3.56k
    goto error_return;
2086
2087
  /* Get dynamic string table.  */
2088
246
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2089
246
  if (filepos == (file_ptr) -1
2090
246
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2091
24
    goto error_return;
2092
2093
  /* Dynamic string table must be valid until ABFD is closed.  */
2094
222
  strbuf = (char *) _bfd_mmap_readonly_persistent (abfd, dt_strsz);
2095
222
  if (strbuf == NULL)
2096
3
    goto error_return;
2097
219
  if (strbuf[dt_strsz - 1] != 0)
2098
11
    {
2099
      /* It is an error if a string table is't terminated.  */
2100
11
      _bfd_error_handler
2101
  /* xgettext:c-format */
2102
11
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2103
11
      goto error_return;
2104
11
    }
2105
2106
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2107
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2108
208
  if (dt_hash)
2109
73
    {
2110
73
      unsigned char nb[16];
2111
73
      unsigned int hash_ent_size;
2112
2113
73
      switch (bed->elf_machine_code)
2114
73
  {
2115
0
  case EM_ALPHA:
2116
0
  case EM_S390:
2117
0
  case EM_S390_OLD:
2118
0
    if (bed->s->elfclass == ELFCLASS64)
2119
0
      {
2120
0
        hash_ent_size = 8;
2121
0
        break;
2122
0
      }
2123
    /* FALLTHROUGH */
2124
73
  default:
2125
73
    hash_ent_size = 4;
2126
73
    break;
2127
73
  }
2128
2129
73
      filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2130
73
         NULL);
2131
73
      if (filepos == (file_ptr) -1
2132
73
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2133
73
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2134
23
  goto error_return;
2135
2136
      /* The number of dynamic symbol table entries equals the number
2137
   of chains.  */
2138
50
      if (hash_ent_size == 8)
2139
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2140
50
      else
2141
50
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2142
50
    }
2143
135
  else
2144
135
    {
2145
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2146
   bindings are in hash table.  Since in dynamic symbol table,
2147
   all symbols with STB_LOCAL binding are placed before symbols
2148
   with other bindings and all undefined symbols are placed
2149
   before defined ones, the highest symbol index in DT_GNU_HASH
2150
   is the highest dynamic symbol table index.  */
2151
135
      unsigned char nb[16];
2152
135
      bfd_vma ngnubuckets;
2153
135
      bfd_vma gnusymidx;
2154
135
      size_t i, ngnuchains;
2155
135
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2156
135
      bfd_vma buckets_vma;
2157
2158
135
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2159
135
         sizeof (nb), NULL);
2160
135
      if (filepos == (file_ptr) -1
2161
135
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2162
135
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2163
15
  goto error_return;
2164
2165
120
      ngnubuckets = bfd_get_32 (abfd, nb);
2166
120
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2167
120
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2168
120
      buckets_vma = dt_gnu_hash + 16;
2169
120
      if (bed->s->elfclass == ELFCLASS32)
2170
0
  buckets_vma += bitmaskwords * 4;
2171
120
      else
2172
120
  buckets_vma += bitmaskwords * 8;
2173
120
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2174
120
      if (filepos == (file_ptr) -1
2175
120
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2176
13
  goto error_return;
2177
2178
107
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2179
107
      if (gnubuckets == NULL)
2180
14
  goto error_return;
2181
2182
103k
      for (i = 0; i < ngnubuckets; i++)
2183
102k
  if (gnubuckets[i] != 0)
2184
78.2k
    {
2185
78.2k
      if (gnubuckets[i] < gnusymidx)
2186
6
        goto error_return;
2187
2188
78.2k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2189
4.22k
        maxchain = gnubuckets[i];
2190
78.2k
    }
2191
2192
87
      if (maxchain == 0xffffffff)
2193
14
  {
2194
14
    symcount = 0;
2195
14
    goto empty_gnu_hash;
2196
14
  }
2197
2198
73
      maxchain -= gnusymidx;
2199
73
      filepos = offset_from_vma (phdrs, phnum,
2200
73
         (buckets_vma +
2201
73
          4 * (ngnubuckets + maxchain)),
2202
73
         4, NULL);
2203
73
      if (filepos == (file_ptr) -1
2204
73
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2205
33
  goto error_return;
2206
2207
40
      do
2208
872
  {
2209
872
    if (bfd_read (nb, 4, abfd) != 4)
2210
1
      goto error_return;
2211
871
    ++maxchain;
2212
871
    if (maxchain == 0)
2213
0
      goto error_return;
2214
871
  }
2215
871
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2216
2217
39
      filepos = offset_from_vma (phdrs, phnum,
2218
39
         (buckets_vma + 4 * ngnubuckets),
2219
39
         4, NULL);
2220
39
      if (filepos == (file_ptr) -1
2221
39
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2222
0
  goto error_return;
2223
2224
39
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2225
39
      if (gnuchains == NULL)
2226
5
  goto error_return;
2227
34
      ngnuchains = maxchain;
2228
2229
34
      if (dt_mips_xhash)
2230
0
  {
2231
0
    filepos = offset_from_vma (phdrs, phnum,
2232
0
             (buckets_vma
2233
0
              + 4 * (ngnubuckets + maxchain)),
2234
0
             4, NULL);
2235
0
    if (filepos == (file_ptr) -1
2236
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2237
0
      goto error_return;
2238
2239
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2240
0
    if (mipsxlat == NULL)
2241
0
      goto error_return;
2242
0
  }
2243
2244
34
      symcount = 0;
2245
234
      for (i = 0; i < ngnubuckets; ++i)
2246
200
  if (gnubuckets[i] != 0)
2247
82
    {
2248
82
      bfd_vma si = gnubuckets[i];
2249
82
      bfd_vma off = si - gnusymidx;
2250
82
      do
2251
946
        {
2252
946
    if (mipsxlat)
2253
0
      {
2254
0
        if (mipsxlat[off] >= symcount)
2255
0
          symcount = mipsxlat[off] + 1;
2256
0
      }
2257
946
    else
2258
946
      {
2259
946
        if (si >= symcount)
2260
672
          symcount = si + 1;
2261
946
      }
2262
946
    si++;
2263
946
        }
2264
946
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2265
82
    }
2266
34
    }
2267
2268
  /* Swap in dynamic symbol table.  */
2269
84
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2270
0
    {
2271
0
      bfd_set_error (bfd_error_file_too_big);
2272
0
      goto error_return;
2273
0
    }
2274
2275
84
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2276
84
  if (filepos == (file_ptr) -1
2277
84
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2278
26
    goto error_return;
2279
58
  esymbuf_size = amt;
2280
58
  esymbuf = _bfd_mmap_readonly_temporary (abfd, esymbuf_size,
2281
58
            &esymbuf_addr,
2282
58
            &esymbuf_size);
2283
58
  if (esymbuf == NULL)
2284
2
    goto error_return;
2285
2286
56
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2287
0
    {
2288
0
      bfd_set_error (bfd_error_file_too_big);
2289
0
      goto error_return;
2290
0
    }
2291
2292
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2293
56
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2294
56
  if (isymbuf == NULL)
2295
0
    goto error_return;
2296
2297
56
  swap_symbol_in = bed->s->swap_symbol_in;
2298
2299
  /* Convert the symbols to internal form.  */
2300
56
  isymend = isymbuf + symcount;
2301
56
  for (esym = esymbuf, isym = isymbuf;
2302
90
       isym < isymend;
2303
56
       esym += extsym_size, isym++)
2304
49
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2305
49
  || isym->st_name >= dt_strsz)
2306
15
      {
2307
15
  bfd_set_error (bfd_error_invalid_operation);
2308
15
  goto error_return;
2309
15
      }
2310
2311
41
  if (dt_versym)
2312
28
    {
2313
      /* Swap in DT_VERSYM.  */
2314
28
      if (_bfd_mul_overflow (symcount, 2, &amt))
2315
0
  {
2316
0
    bfd_set_error (bfd_error_file_too_big);
2317
0
    goto error_return;
2318
0
  }
2319
2320
28
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2321
28
      if (filepos == (file_ptr) -1
2322
28
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2323
6
  goto error_return;
2324
2325
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2326
22
      versym = _bfd_mmap_readonly_persistent (abfd, amt);
2327
2328
22
      if (dt_verdef)
2329
11
  {
2330
    /* Read in DT_VERDEF.  */
2331
11
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2332
11
             0, &verdef_size);
2333
11
    if (filepos == (file_ptr) -1
2334
11
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2335
6
      goto error_return;
2336
2337
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2338
5
    verdef = _bfd_mmap_readonly_persistent (abfd, verdef_size);
2339
5
  }
2340
2341
16
      if (dt_verneed)
2342
8
  {
2343
    /* Read in DT_VERNEED.  */
2344
8
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2345
8
             0, &verneed_size);
2346
8
    if (filepos == (file_ptr) -1
2347
8
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2348
8
      goto error_return;
2349
2350
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2351
0
    verneed = _bfd_mmap_readonly_persistent (abfd, verneed_size);
2352
0
  }
2353
16
    }
2354
2355
35
 empty_gnu_hash:
2356
35
  elf_tdata (abfd)->dt_strtab = strbuf;
2357
35
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2358
35
  elf_tdata (abfd)->dt_symtab = isymbuf;
2359
35
  elf_tdata (abfd)->dt_symtab_count = symcount;
2360
35
  elf_tdata (abfd)->dt_versym = versym;
2361
35
  elf_tdata (abfd)->dt_verdef = verdef;
2362
35
  elf_tdata (abfd)->dt_verneed = verneed;
2363
35
  elf_tdata (abfd)->dt_verdef_count
2364
35
    = verdef_size / sizeof (Elf_External_Verdef);
2365
35
  elf_tdata (abfd)->dt_verneed_count
2366
35
    = verneed_size / sizeof (Elf_External_Verneed);
2367
2368
35
  res = true;
2369
2370
4.24k
 error_return:
2371
  /* Restore file position for elf_object_p.  */
2372
4.24k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2373
0
    res = false;
2374
4.24k
  _bfd_munmap_readonly_temporary (dynbuf_addr, dynbuf_size);
2375
4.24k
  _bfd_munmap_readonly_temporary (esymbuf_addr, esymbuf_size);
2376
4.24k
  free (gnubuckets);
2377
4.24k
  free (gnuchains);
2378
4.24k
  free (mipsxlat);
2379
4.24k
  return res;
2380
35
}
2381
2382
/* Reconstruct section from dynamic symbol.  */
2383
2384
asection *
2385
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2386
            Elf_Internal_Sym *isym)
2387
0
{
2388
0
  asection *sec;
2389
0
  flagword flags;
2390
2391
0
  if (!elf_use_dt_symtab_p (abfd))
2392
0
    return NULL;
2393
2394
0
  flags = SEC_ALLOC | SEC_LOAD;
2395
0
  switch (ELF_ST_TYPE (isym->st_info))
2396
0
    {
2397
0
    case STT_FUNC:
2398
0
    case STT_GNU_IFUNC:
2399
0
      sec = bfd_get_section_by_name (abfd, ".text");
2400
0
      if (sec == NULL)
2401
0
  sec = bfd_make_section_with_flags (abfd,
2402
0
             ".text",
2403
0
             flags | SEC_CODE);
2404
0
      break;
2405
0
    case STT_COMMON:
2406
0
      sec = bfd_com_section_ptr;
2407
0
      break;
2408
0
    case STT_OBJECT:
2409
0
      sec = bfd_get_section_by_name (abfd, ".data");
2410
0
      if (sec == NULL)
2411
0
  sec = bfd_make_section_with_flags (abfd,
2412
0
             ".data",
2413
0
             flags | SEC_DATA);
2414
0
      break;
2415
0
    case STT_TLS:
2416
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2417
0
      if (sec == NULL)
2418
0
  sec = bfd_make_section_with_flags (abfd,
2419
0
             ".tdata",
2420
0
             (flags
2421
0
              | SEC_DATA
2422
0
              | SEC_THREAD_LOCAL));
2423
0
      break;
2424
0
    default:
2425
0
      sec = bfd_abs_section_ptr;
2426
0
      break;
2427
0
    }
2428
2429
0
  return sec;
2430
0
}
2431
2432
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2433
   and return symbol version for symbol version itself.   */
2434
2435
const char *
2436
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2437
            bool base_p,
2438
            bool *hidden)
2439
1.77M
{
2440
1.77M
  const char *version_string = NULL;
2441
1.77M
  if ((elf_dynversym (abfd) != 0
2442
1.77M
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2443
1.77M
      || (elf_tdata (abfd)->dt_versym != NULL
2444
1.05M
    && (elf_tdata (abfd)->dt_verdef != NULL
2445
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2446
725k
    {
2447
725k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2448
2449
725k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2450
725k
      vernum &= VERSYM_VERSION;
2451
2452
725k
      if (vernum == 0)
2453
251k
  version_string = "";
2454
474k
      else if (vernum == 1
2455
474k
         && (vernum > elf_tdata (abfd)->cverdefs
2456
472k
       || (elf_tdata (abfd)->verdef[0].vd_flags
2457
0
           == VER_FLG_BASE)))
2458
472k
  version_string = base_p ? "Base" : "";
2459
1.71k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2460
0
  {
2461
0
    const char *nodename
2462
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2463
0
    version_string = "";
2464
0
    if (base_p
2465
0
        || nodename == NULL
2466
0
        || symbol->name == NULL
2467
0
        || strcmp (symbol->name, nodename) != 0)
2468
0
      version_string = nodename;
2469
0
  }
2470
1.71k
      else
2471
1.71k
  {
2472
1.71k
    Elf_Internal_Verneed *t;
2473
2474
1.71k
    version_string = _("<corrupt>");
2475
1.71k
    for (t = elf_tdata (abfd)->verref;
2476
5.52k
         t != NULL;
2477
3.81k
         t = t->vn_nextref)
2478
3.81k
      {
2479
3.81k
        Elf_Internal_Vernaux *a;
2480
2481
10.5k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2482
8.41k
    {
2483
8.41k
      if (a->vna_other == vernum)
2484
1.68k
        {
2485
1.68k
          *hidden = true;
2486
1.68k
          version_string = a->vna_nodename;
2487
1.68k
          break;
2488
1.68k
        }
2489
8.41k
    }
2490
3.81k
      }
2491
1.71k
  }
2492
725k
    }
2493
1.77M
  return version_string;
2494
1.77M
}
2495
2496
/* Display ELF-specific fields of a symbol.  */
2497
2498
void
2499
bfd_elf_print_symbol (bfd *abfd,
2500
          void *filep,
2501
          asymbol *symbol,
2502
          bfd_print_symbol_type how)
2503
0
{
2504
0
  FILE *file = (FILE *) filep;
2505
0
  switch (how)
2506
0
    {
2507
0
    case bfd_print_symbol_name:
2508
0
      fprintf (file, "%s", symbol->name);
2509
0
      break;
2510
0
    case bfd_print_symbol_more:
2511
0
      fprintf (file, "elf ");
2512
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2513
0
      fprintf (file, " %x", symbol->flags);
2514
0
      break;
2515
0
    case bfd_print_symbol_all:
2516
0
      {
2517
0
  const char *section_name;
2518
0
  const char *name = NULL;
2519
0
  const struct elf_backend_data *bed;
2520
0
  unsigned char st_other;
2521
0
  bfd_vma val;
2522
0
  const char *version_string;
2523
0
  bool hidden;
2524
2525
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2526
2527
0
  bed = get_elf_backend_data (abfd);
2528
0
  if (bed->elf_backend_print_symbol_all)
2529
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2530
2531
0
  if (name == NULL)
2532
0
    {
2533
0
      name = symbol->name;
2534
0
      bfd_print_symbol_vandf (abfd, file, symbol);
2535
0
    }
2536
2537
0
  fprintf (file, " %s\t", section_name);
2538
  /* Print the "other" value for a symbol.  For common symbols,
2539
     we've already printed the size; now print the alignment.
2540
     For other symbols, we have no specified alignment, and
2541
     we've printed the address; now print the size.  */
2542
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2543
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2544
0
  else
2545
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2546
0
  bfd_fprintf_vma (abfd, file, val);
2547
2548
  /* If we have version information, print it.  */
2549
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2550
0
                   symbol,
2551
0
                   true,
2552
0
                   &hidden);
2553
0
  if (version_string)
2554
0
    {
2555
0
      if (!hidden)
2556
0
        fprintf (file, "  %-11s", version_string);
2557
0
      else
2558
0
        {
2559
0
    int i;
2560
2561
0
    fprintf (file, " (%s)", version_string);
2562
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2563
0
      putc (' ', file);
2564
0
        }
2565
0
    }
2566
2567
  /* If the st_other field is not zero, print it.  */
2568
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2569
2570
0
  switch (st_other)
2571
0
    {
2572
0
    case 0: break;
2573
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2574
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2575
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2576
0
    default:
2577
      /* Some other non-defined flags are also present, so print
2578
         everything hex.  */
2579
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2580
0
    }
2581
2582
0
  fprintf (file, " %s", name);
2583
0
      }
2584
0
      break;
2585
0
    }
2586
0
}
2587

2588
/* ELF .o/exec file reading */
2589
2590
/* Create a new bfd section from an ELF section header.  */
2591
2592
bool
2593
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2594
3.03M
{
2595
3.03M
  Elf_Internal_Shdr *hdr;
2596
3.03M
  Elf_Internal_Ehdr *ehdr;
2597
3.03M
  const struct elf_backend_data *bed;
2598
3.03M
  const char *name;
2599
3.03M
  bool ret = true;
2600
2601
3.03M
  if (shindex >= elf_numsections (abfd))
2602
0
    return false;
2603
2604
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2605
     sh_link or sh_info.  Detect this here, by refusing to load a
2606
     section that we are already in the process of loading.  */
2607
3.03M
  if (elf_tdata (abfd)->being_created[shindex])
2608
481
    {
2609
481
      _bfd_error_handler
2610
481
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2611
481
      return false;
2612
481
    }
2613
3.03M
  elf_tdata (abfd)->being_created[shindex] = true;
2614
2615
3.03M
  hdr = elf_elfsections (abfd)[shindex];
2616
3.03M
  ehdr = elf_elfheader (abfd);
2617
3.03M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2618
3.03M
            hdr->sh_name);
2619
3.03M
  if (name == NULL)
2620
14.3k
    goto fail;
2621
2622
3.02M
  bed = get_elf_backend_data (abfd);
2623
3.02M
  switch (hdr->sh_type)
2624
3.02M
    {
2625
910k
    case SHT_NULL:
2626
      /* Inactive section. Throw it away.  */
2627
910k
      goto success;
2628
2629
393k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2630
449k
    case SHT_NOBITS:   /* .bss section.  */
2631
457k
    case SHT_HASH:   /* .hash section.  */
2632
524k
    case SHT_NOTE:   /* .note section.  */
2633
549k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2634
561k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2635
579k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2636
579k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2637
581k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2638
581k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2639
581k
      goto success;
2640
2641
3.84k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2642
3.84k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2643
15
  goto fail;
2644
2645
3.83k
      if (hdr->sh_link > elf_numsections (abfd))
2646
227
  {
2647
    /* PR 10478: Accept Solaris binaries with a sh_link field
2648
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2649
227
    switch (bfd_get_arch (abfd))
2650
227
      {
2651
227
      case bfd_arch_i386:
2652
227
      case bfd_arch_sparc:
2653
227
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2654
227
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2655
227
    break;
2656
        /* Otherwise fall through.  */
2657
0
      default:
2658
0
        goto fail;
2659
227
      }
2660
227
  }
2661
3.60k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2662
0
  goto fail;
2663
3.60k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2664
2.26k
  {
2665
2.26k
    Elf_Internal_Shdr *dynsymhdr;
2666
2667
    /* The shared libraries distributed with hpux11 have a bogus
2668
       sh_link field for the ".dynamic" section.  Find the
2669
       string table for the ".dynsym" section instead.  */
2670
2.26k
    if (elf_dynsymtab (abfd) != 0)
2671
88
      {
2672
88
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2673
88
        hdr->sh_link = dynsymhdr->sh_link;
2674
88
      }
2675
2.17k
    else
2676
2.17k
      {
2677
2.17k
        unsigned int i, num_sec;
2678
2679
2.17k
        num_sec = elf_numsections (abfd);
2680
34.4k
        for (i = 1; i < num_sec; i++)
2681
32.3k
    {
2682
32.3k
      dynsymhdr = elf_elfsections (abfd)[i];
2683
32.3k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2684
106
        {
2685
106
          hdr->sh_link = dynsymhdr->sh_link;
2686
106
          break;
2687
106
        }
2688
32.3k
    }
2689
2.17k
      }
2690
2.26k
  }
2691
3.83k
      goto success;
2692
2693
178k
    case SHT_SYMTAB:   /* A symbol table.  */
2694
178k
      if (elf_onesymtab (abfd) == shindex)
2695
126k
  goto success;
2696
2697
51.9k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2698
775
  goto fail;
2699
2700
51.2k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2701
2.52k
  {
2702
2.52k
    if (hdr->sh_size != 0)
2703
132
      goto fail;
2704
    /* Some assemblers erroneously set sh_info to one with a
2705
       zero sh_size.  ld sees this as a global symbol count
2706
       of (unsigned) -1.  Fix it here.  */
2707
2.39k
    hdr->sh_info = 0;
2708
2.39k
    goto success;
2709
2.52k
  }
2710
2711
      /* PR 18854: A binary might contain more than one symbol table.
2712
   Unusual, but possible.  Warn, but continue.  */
2713
48.6k
      if (elf_onesymtab (abfd) != 0)
2714
8.98k
  {
2715
8.98k
    _bfd_error_handler
2716
      /* xgettext:c-format */
2717
8.98k
      (_("%pB: warning: multiple symbol tables detected"
2718
8.98k
         " - ignoring the table in section %u"),
2719
8.98k
       abfd, shindex);
2720
8.98k
    goto success;
2721
8.98k
  }
2722
39.7k
      elf_onesymtab (abfd) = shindex;
2723
39.7k
      elf_symtab_hdr (abfd) = *hdr;
2724
39.7k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2725
39.7k
      abfd->flags |= HAS_SYMS;
2726
2727
      /* Sometimes a shared object will map in the symbol table.  If
2728
   SHF_ALLOC is set, and this is a shared object, then we also
2729
   treat this section as a BFD section.  We can not base the
2730
   decision purely on SHF_ALLOC, because that flag is sometimes
2731
   set in a relocatable object file, which would confuse the
2732
   linker.  */
2733
39.7k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2734
39.7k
    && (abfd->flags & DYNAMIC) != 0
2735
39.7k
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2736
3.41k
            shindex))
2737
0
  goto fail;
2738
2739
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2740
   can't read symbols without that section loaded as well.  It
2741
   is most likely specified by the next section header.  */
2742
39.7k
      {
2743
39.7k
  elf_section_list * entry;
2744
39.7k
  unsigned int i, num_sec;
2745
2746
44.1k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2747
5.02k
    if (entry->hdr.sh_link == shindex)
2748
535
      goto success;
2749
2750
39.1k
  num_sec = elf_numsections (abfd);
2751
91.6k
  for (i = shindex + 1; i < num_sec; i++)
2752
52.5k
    {
2753
52.5k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2754
2755
52.5k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2756
52.5k
    && hdr2->sh_link == shindex)
2757
15
        break;
2758
52.5k
    }
2759
2760
39.1k
  if (i == num_sec)
2761
931k
    for (i = 1; i < shindex; i++)
2762
892k
      {
2763
892k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2764
2765
892k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2766
892k
      && hdr2->sh_link == shindex)
2767
381
    break;
2768
892k
      }
2769
2770
39.1k
  if (i != shindex)
2771
396
    ret = bfd_section_from_shdr (abfd, i);
2772
  /* else FIXME: we have failed to find the symbol table.
2773
     Should we issue an error?  */
2774
39.1k
  goto success;
2775
39.7k
      }
2776
2777
5.32k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2778
5.32k
      if (elf_dynsymtab (abfd) == shindex)
2779
2.48k
  goto success;
2780
2781
2.83k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2782
433
  goto fail;
2783
2784
2.39k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2785
150
  {
2786
150
    if (hdr->sh_size != 0)
2787
49
      goto fail;
2788
2789
    /* Some linkers erroneously set sh_info to one with a
2790
       zero sh_size.  ld sees this as a global symbol count
2791
       of (unsigned) -1.  Fix it here.  */
2792
101
    hdr->sh_info = 0;
2793
101
    goto success;
2794
150
  }
2795
2796
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2797
   Unusual, but possible.  Warn, but continue.  */
2798
2.24k
      if (elf_dynsymtab (abfd) != 0)
2799
21
  {
2800
21
    _bfd_error_handler
2801
      /* xgettext:c-format */
2802
21
      (_("%pB: warning: multiple dynamic symbol tables detected"
2803
21
         " - ignoring the table in section %u"),
2804
21
       abfd, shindex);
2805
21
    goto success;
2806
21
  }
2807
2.22k
      elf_dynsymtab (abfd) = shindex;
2808
2.22k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2809
2.22k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2810
2.22k
      abfd->flags |= HAS_SYMS;
2811
2812
      /* Besides being a symbol table, we also treat this as a regular
2813
   section, so that objcopy can handle it.  */
2814
2.22k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2815
2.22k
      goto success;
2816
2817
29.2k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2818
29.2k
      {
2819
29.2k
  elf_section_list * entry;
2820
2821
39.1k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2822
10.2k
    if (entry->ndx == shindex)
2823
331
      goto success;
2824
2825
28.9k
  entry = bfd_alloc (abfd, sizeof (*entry));
2826
28.9k
  if (entry == NULL)
2827
0
    goto fail;
2828
28.9k
  entry->ndx = shindex;
2829
28.9k
  entry->hdr = * hdr;
2830
28.9k
  entry->next = elf_symtab_shndx_list (abfd);
2831
28.9k
  elf_symtab_shndx_list (abfd) = entry;
2832
28.9k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2833
28.9k
  goto success;
2834
28.9k
      }
2835
2836
250k
    case SHT_STRTAB:   /* A string table.  */
2837
250k
      if (hdr->bfd_section != NULL)
2838
465
  goto success;
2839
2840
249k
      if (ehdr->e_shstrndx == shindex)
2841
233k
  {
2842
233k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2843
233k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2844
233k
    goto success;
2845
233k
  }
2846
2847
15.9k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2848
847
  {
2849
887
  symtab_strtab:
2850
887
    elf_tdata (abfd)->strtab_hdr = *hdr;
2851
887
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2852
887
    goto success;
2853
847
  }
2854
2855
15.0k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2856
1.42k
  {
2857
1.46k
  dynsymtab_strtab:
2858
1.46k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2859
1.46k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2860
1.46k
    elf_elfsections (abfd)[shindex] = hdr;
2861
    /* We also treat this as a regular section, so that objcopy
2862
       can handle it.  */
2863
1.46k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2864
1.46k
             shindex);
2865
1.46k
    goto success;
2866
1.42k
  }
2867
2868
      /* If the string table isn't one of the above, then treat it as a
2869
   regular section.  We need to scan all the headers to be sure,
2870
   just in case this strtab section appeared before the above.  */
2871
13.6k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2872
13.4k
  {
2873
13.4k
    unsigned int i, num_sec;
2874
2875
13.4k
    num_sec = elf_numsections (abfd);
2876
290k
    for (i = 1; i < num_sec; i++)
2877
277k
      {
2878
277k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2879
277k
        if (hdr2->sh_link == shindex)
2880
3.92k
    {
2881
      /* Prevent endless recursion on broken objects.  */
2882
3.92k
      if (i == shindex)
2883
39
        goto fail;
2884
3.88k
      if (! bfd_section_from_shdr (abfd, i))
2885
171
        goto fail;
2886
3.71k
      if (elf_onesymtab (abfd) == i)
2887
40
        goto symtab_strtab;
2888
3.67k
      if (elf_dynsymtab (abfd) == i)
2889
37
        goto dynsymtab_strtab;
2890
3.67k
    }
2891
277k
      }
2892
13.4k
  }
2893
13.3k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2894
13.3k
      goto success;
2895
2896
31.2k
    case SHT_REL:
2897
164k
    case SHT_RELA:
2898
166k
    case SHT_RELR:
2899
      /* *These* do a lot of work -- but build no sections!  */
2900
166k
      {
2901
166k
  asection *target_sect;
2902
166k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2903
166k
  unsigned int num_sec = elf_numsections (abfd);
2904
166k
  struct bfd_elf_section_data *esdt;
2905
166k
  bfd_size_type size;
2906
2907
166k
  if (hdr->sh_type == SHT_REL)
2908
31.2k
    size = bed->s->sizeof_rel;
2909
135k
  else if (hdr->sh_type == SHT_RELA)
2910
133k
    size = bed->s->sizeof_rela;
2911
1.64k
  else
2912
1.64k
    size = bed->s->arch_size / 8;
2913
166k
  if (hdr->sh_entsize != size)
2914
1.11k
    goto fail;
2915
2916
  /* Check for a bogus link to avoid crashing.  */
2917
165k
  if (hdr->sh_link >= num_sec)
2918
125
    {
2919
125
      _bfd_error_handler
2920
        /* xgettext:c-format */
2921
125
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2922
125
         abfd, hdr->sh_link, name, shindex);
2923
125
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2924
125
      goto success;
2925
125
    }
2926
2927
  /* Get the symbol table.  */
2928
165k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2929
165k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2930
165k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2931
172
    goto fail;
2932
2933
  /* If this is an alloc section in an executable or shared
2934
     library, or the reloc section does not use the main symbol
2935
     table we don't treat it as a reloc section.  BFD can't
2936
     adequately represent such a section, so at least for now,
2937
     we don't try.  We just present it as a normal section.  We
2938
     also can't use it as a reloc section if it points to the
2939
     null section, an invalid section, another reloc section, or
2940
     its sh_link points to the null section.  */
2941
164k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2942
164k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2943
164k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2944
164k
      || hdr->sh_type == SHT_RELR
2945
164k
      || hdr->sh_link == SHN_UNDEF
2946
164k
      || hdr->sh_link != elf_onesymtab (abfd)
2947
164k
      || hdr->sh_info == SHN_UNDEF
2948
164k
      || hdr->sh_info >= num_sec
2949
164k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2950
164k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2951
67.8k
    {
2952
67.8k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2953
67.8k
      goto success;
2954
67.8k
    }
2955
2956
97.0k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2957
58
    goto fail;
2958
2959
97.0k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2960
97.0k
  if (target_sect == NULL)
2961
74
    goto fail;
2962
2963
96.9k
  esdt = elf_section_data (target_sect);
2964
96.9k
  if (hdr->sh_type == SHT_RELA)
2965
92.9k
    p_hdr = &esdt->rela.hdr;
2966
4.02k
  else
2967
4.02k
    p_hdr = &esdt->rel.hdr;
2968
2969
  /* PR 17512: file: 0b4f81b7.
2970
     Also see PR 24456, for a file which deliberately has two reloc
2971
     sections.  */
2972
96.9k
  if (*p_hdr != NULL)
2973
10.5k
    {
2974
10.5k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2975
19
        {
2976
19
    _bfd_error_handler
2977
      /* xgettext:c-format */
2978
19
      (_("%pB: warning: secondary relocation section '%s' "
2979
19
         "for section %pA found - ignoring"),
2980
19
       abfd, name, target_sect);
2981
19
        }
2982
10.4k
      else
2983
10.4k
        esdt->has_secondary_relocs = true;
2984
10.5k
      goto success;
2985
10.5k
    }
2986
2987
86.4k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2988
86.4k
  if (hdr2 == NULL)
2989
0
    goto fail;
2990
86.4k
  *hdr2 = *hdr;
2991
86.4k
  *p_hdr = hdr2;
2992
86.4k
  elf_elfsections (abfd)[shindex] = hdr2;
2993
86.4k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2994
86.4k
             * bed->s->int_rels_per_ext_rel);
2995
86.4k
  target_sect->flags |= SEC_RELOC;
2996
86.4k
  target_sect->relocation = NULL;
2997
86.4k
  target_sect->rel_filepos = hdr->sh_offset;
2998
  /* In the section to which the relocations apply, mark whether
2999
     its relocations are of the REL or RELA variety.  */
3000
86.4k
  if (hdr->sh_size != 0)
3001
84.5k
    {
3002
84.5k
      if (hdr->sh_type == SHT_RELA)
3003
80.5k
        target_sect->use_rela_p = 1;
3004
84.5k
    }
3005
86.4k
  abfd->flags |= HAS_RELOC;
3006
86.4k
  goto success;
3007
86.4k
      }
3008
3009
1.51k
    case SHT_GNU_verdef:
3010
1.51k
      if (hdr->sh_info != 0)
3011
480
  elf_dynverdef (abfd) = shindex;
3012
1.51k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
3013
1.51k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3014
1.51k
      goto success;
3015
3016
989
    case SHT_GNU_versym:
3017
989
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3018
45
  goto fail;
3019
3020
944
      elf_dynversym (abfd) = shindex;
3021
944
      elf_tdata (abfd)->dynversym_hdr = *hdr;
3022
944
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3023
944
      goto success;
3024
3025
6.31k
    case SHT_GNU_verneed:
3026
6.31k
      if (hdr->sh_info != 0)
3027
6.01k
  elf_dynverref (abfd) = shindex;
3028
6.31k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
3029
6.31k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3030
6.31k
      goto success;
3031
3032
3.03k
    case SHT_SHLIB:
3033
3.03k
      goto success;
3034
3035
84.5k
    case SHT_GROUP:
3036
84.5k
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3037
3.05k
  goto fail;
3038
3039
81.4k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3040
523
  goto fail;
3041
3042
80.9k
      goto success;
3043
3044
803k
    default:
3045
      /* Possibly an attributes section.  */
3046
803k
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3047
803k
    || hdr->sh_type == bed->obj_attrs_section_type)
3048
37.1k
  {
3049
37.1k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3050
20
      goto fail;
3051
37.0k
    _bfd_elf_parse_attributes (abfd, hdr);
3052
37.0k
    goto success;
3053
37.1k
  }
3054
3055
      /* Check for any processor-specific section types.  */
3056
766k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3057
679k
  goto success;
3058
3059
86.5k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3060
46.1k
  {
3061
46.1k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
3062
      /* FIXME: How to properly handle allocated section reserved
3063
         for applications?  */
3064
8.24k
      _bfd_error_handler
3065
        /* xgettext:c-format */
3066
8.24k
        (_("%pB: unknown type [%#x] section `%s'"),
3067
8.24k
         abfd, hdr->sh_type, name);
3068
37.8k
    else
3069
37.8k
      {
3070
        /* Allow sections reserved for applications.  */
3071
37.8k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3072
37.8k
        goto success;
3073
37.8k
      }
3074
46.1k
  }
3075
40.4k
      else if (hdr->sh_type >= SHT_LOPROC
3076
40.4k
         && hdr->sh_type <= SHT_HIPROC)
3077
  /* FIXME: We should handle this section.  */
3078
2.31k
  _bfd_error_handler
3079
    /* xgettext:c-format */
3080
2.31k
    (_("%pB: unknown type [%#x] section `%s'"),
3081
2.31k
     abfd, hdr->sh_type, name);
3082
38.1k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3083
11.4k
  {
3084
    /* Unrecognised OS-specific sections.  */
3085
11.4k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3086
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
3087
         required to correctly process the section and the file should
3088
         be rejected with an error message.  */
3089
1.31k
      _bfd_error_handler
3090
        /* xgettext:c-format */
3091
1.31k
        (_("%pB: unknown type [%#x] section `%s'"),
3092
1.31k
         abfd, hdr->sh_type, name);
3093
10.1k
    else
3094
10.1k
      {
3095
        /* Otherwise it should be processed.  */
3096
10.1k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3097
10.1k
        goto success;
3098
10.1k
      }
3099
11.4k
  }
3100
26.7k
      else
3101
  /* FIXME: We should handle this section.  */
3102
26.7k
  _bfd_error_handler
3103
    /* xgettext:c-format */
3104
26.7k
    (_("%pB: unknown type [%#x] section `%s'"),
3105
26.7k
     abfd, hdr->sh_type, name);
3106
3107
38.5k
      goto fail;
3108
3.02M
    }
3109
3110
59.5k
 fail:
3111
59.5k
  ret = false;
3112
3.03M
 success:
3113
3.03M
  elf_tdata (abfd)->being_created[shindex] = false;
3114
3.03M
  return ret;
3115
59.5k
}
3116
3117
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3118
3119
Elf_Internal_Sym *
3120
bfd_sym_from_r_symndx (struct sym_cache *cache,
3121
           bfd *abfd,
3122
           unsigned long r_symndx)
3123
0
{
3124
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3125
3126
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3127
0
    {
3128
0
      Elf_Internal_Shdr *symtab_hdr;
3129
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3130
0
      Elf_External_Sym_Shndx eshndx;
3131
3132
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3133
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3134
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3135
0
  return NULL;
3136
3137
0
      if (cache->abfd != abfd)
3138
0
  {
3139
0
    memset (cache->indx, -1, sizeof (cache->indx));
3140
0
    cache->abfd = abfd;
3141
0
  }
3142
0
      cache->indx[ent] = r_symndx;
3143
0
    }
3144
3145
0
  return &cache->sym[ent];
3146
0
}
3147
3148
/* Given an ELF section number, retrieve the corresponding BFD
3149
   section.  */
3150
3151
asection *
3152
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3153
531k
{
3154
531k
  if (sec_index >= elf_numsections (abfd))
3155
170k
    return NULL;
3156
360k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3157
531k
}
3158
3159
static const struct bfd_elf_special_section special_sections_b[] =
3160
{
3161
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3162
  { NULL,       0,  0, 0,    0 }
3163
};
3164
3165
static const struct bfd_elf_special_section special_sections_c[] =
3166
{
3167
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3168
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3169
  { NULL,     0, 0, 0,      0 }
3170
};
3171
3172
static const struct bfd_elf_special_section special_sections_d[] =
3173
{
3174
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3175
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3176
  /* There are more DWARF sections than these, but they needn't be added here
3177
     unless you have to cope with broken compilers that don't emit section
3178
     attributes or you want to help the user writing assembler.  */
3179
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3180
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3181
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3182
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3183
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3184
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3185
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3186
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3187
  { NULL,          0,  0, 0,      0 }
3188
};
3189
3190
static const struct bfd_elf_special_section special_sections_f[] =
3191
{
3192
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3193
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3194
  { NULL,        0 , 0, 0,      0 }
3195
};
3196
3197
static const struct bfd_elf_special_section special_sections_g[] =
3198
{
3199
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3200
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3201
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3202
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3203
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3204
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3205
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3206
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3207
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3208
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3209
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3210
  { NULL,      0,    0, 0,         0 }
3211
};
3212
3213
static const struct bfd_elf_special_section special_sections_h[] =
3214
{
3215
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3216
  { NULL,        0, 0, 0,    0 }
3217
};
3218
3219
static const struct bfd_elf_special_section special_sections_i[] =
3220
{
3221
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3222
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3223
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3224
  { NULL,          0,      0, 0,      0 }
3225
};
3226
3227
static const struct bfd_elf_special_section special_sections_l[] =
3228
{
3229
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3230
  { NULL,        0, 0, 0,    0 }
3231
};
3232
3233
static const struct bfd_elf_special_section special_sections_n[] =
3234
{
3235
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3236
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3237
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3238
  { NULL,        0,     0, 0,      0 }
3239
};
3240
3241
static const struct bfd_elf_special_section special_sections_p[] =
3242
{
3243
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3244
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3245
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3246
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3247
  { NULL,       0,      0, 0,     0 }
3248
};
3249
3250
static const struct bfd_elf_special_section special_sections_r[] =
3251
{
3252
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3253
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3254
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3255
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3256
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3257
  { NULL,       0,     0, 0,      0 }
3258
};
3259
3260
static const struct bfd_elf_special_section special_sections_s[] =
3261
{
3262
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3263
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3264
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3265
  /* See struct bfd_elf_special_section declaration for the semantics of
3266
     this special case where .prefix_length != strlen (.prefix).  */
3267
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3268
  { NULL,     0,  0, 0,    0 }
3269
};
3270
3271
static const struct bfd_elf_special_section special_sections_t[] =
3272
{
3273
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3274
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3275
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3276
  { NULL,         0,  0, 0,      0 }
3277
};
3278
3279
static const struct bfd_elf_special_section special_sections_z[] =
3280
{
3281
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3282
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3283
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3284
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3285
  { NULL,         0,  0, 0,      0 }
3286
};
3287
3288
static const struct bfd_elf_special_section * const special_sections[] =
3289
{
3290
  special_sections_b,   /* 'b' */
3291
  special_sections_c,   /* 'c' */
3292
  special_sections_d,   /* 'd' */
3293
  NULL,       /* 'e' */
3294
  special_sections_f,   /* 'f' */
3295
  special_sections_g,   /* 'g' */
3296
  special_sections_h,   /* 'h' */
3297
  special_sections_i,   /* 'i' */
3298
  NULL,       /* 'j' */
3299
  NULL,       /* 'k' */
3300
  special_sections_l,   /* 'l' */
3301
  NULL,       /* 'm' */
3302
  special_sections_n,   /* 'n' */
3303
  NULL,       /* 'o' */
3304
  special_sections_p,   /* 'p' */
3305
  NULL,       /* 'q' */
3306
  special_sections_r,   /* 'r' */
3307
  special_sections_s,   /* 's' */
3308
  special_sections_t,   /* 't' */
3309
  NULL,       /* 'u' */
3310
  NULL,       /* 'v' */
3311
  NULL,       /* 'w' */
3312
  NULL,       /* 'x' */
3313
  NULL,       /* 'y' */
3314
  special_sections_z    /* 'z' */
3315
};
3316
3317
const struct bfd_elf_special_section *
3318
_bfd_elf_get_special_section (const char *name,
3319
            const struct bfd_elf_special_section *spec,
3320
            unsigned int rela)
3321
1.49M
{
3322
1.49M
  int i;
3323
1.49M
  int len;
3324
3325
1.49M
  len = strlen (name);
3326
3327
8.75M
  for (i = 0; spec[i].prefix != NULL; i++)
3328
7.45M
    {
3329
7.45M
      int suffix_len;
3330
7.45M
      int prefix_len = spec[i].prefix_length;
3331
3332
7.45M
      if (len < prefix_len)
3333
4.47M
  continue;
3334
2.97M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3335
2.59M
  continue;
3336
3337
384k
      suffix_len = spec[i].suffix_length;
3338
384k
      if (suffix_len <= 0)
3339
379k
  {
3340
379k
    if (name[prefix_len] != 0)
3341
298k
      {
3342
298k
        if (suffix_len == 0)
3343
54.6k
    continue;
3344
243k
        if (name[prefix_len] != '.'
3345
243k
      && (suffix_len == -2
3346
135k
          || (rela && spec[i].type == SHT_REL)))
3347
130k
    continue;
3348
243k
      }
3349
379k
  }
3350
5.04k
      else
3351
5.04k
  {
3352
5.04k
    if (len < prefix_len + suffix_len)
3353
3.03k
      continue;
3354
2.01k
    if (memcmp (name + len - suffix_len,
3355
2.01k
          spec[i].prefix + prefix_len,
3356
2.01k
          suffix_len) != 0)
3357
792
      continue;
3358
2.01k
  }
3359
195k
      return &spec[i];
3360
384k
    }
3361
3362
1.29M
  return NULL;
3363
1.49M
}
3364
3365
const struct bfd_elf_special_section *
3366
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3367
1.84M
{
3368
1.84M
  int i;
3369
1.84M
  const struct bfd_elf_special_section *spec;
3370
1.84M
  const struct elf_backend_data *bed;
3371
3372
  /* See if this is one of the special sections.  */
3373
1.84M
  if (sec->name == NULL)
3374
0
    return NULL;
3375
3376
1.84M
  bed = get_elf_backend_data (abfd);
3377
1.84M
  spec = bed->special_sections;
3378
1.84M
  if (spec)
3379
1.04M
    {
3380
1.04M
      spec = _bfd_elf_get_special_section (sec->name,
3381
1.04M
             bed->special_sections,
3382
1.04M
             sec->use_rela_p);
3383
1.04M
      if (spec != NULL)
3384
5.45k
  return spec;
3385
1.04M
    }
3386
3387
1.83M
  if (sec->name[0] != '.')
3388
1.38M
    return NULL;
3389
3390
454k
  i = sec->name[1] - 'b';
3391
454k
  if (i < 0 || i > 'z' - 'b')
3392
11.9k
    return NULL;
3393
3394
442k
  spec = special_sections[i];
3395
3396
442k
  if (spec == NULL)
3397
18.2k
    return NULL;
3398
3399
424k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3400
442k
}
3401
3402
bool
3403
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3404
1.83M
{
3405
1.83M
  struct bfd_elf_section_data *sdata;
3406
1.83M
  const struct elf_backend_data *bed;
3407
1.83M
  const struct bfd_elf_special_section *ssect;
3408
3409
1.83M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3410
1.83M
  if (sdata == NULL)
3411
1.43M
    {
3412
1.43M
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3413
1.43M
                sizeof (*sdata));
3414
1.43M
      if (sdata == NULL)
3415
0
  return false;
3416
1.43M
      sec->used_by_bfd = sdata;
3417
1.43M
    }
3418
3419
  /* Indicate whether or not this section should use RELA relocations.  */
3420
1.83M
  bed = get_elf_backend_data (abfd);
3421
1.83M
  sec->use_rela_p = bed->default_use_rela_p;
3422
3423
  /* Set up ELF section type and flags for newly created sections, if
3424
     there is an ABI mandated section.  */
3425
1.83M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3426
1.83M
  if (ssect != NULL)
3427
195k
    {
3428
195k
      elf_section_type (sec) = ssect->type;
3429
195k
      elf_section_flags (sec) = ssect->attr;
3430
195k
    }
3431
3432
1.83M
  return _bfd_generic_new_section_hook (abfd, sec);
3433
1.83M
}
3434
3435
/* Create a new bfd section from an ELF program header.
3436
3437
   Since program segments have no names, we generate a synthetic name
3438
   of the form segment<NUM>, where NUM is generally the index in the
3439
   program header table.  For segments that are split (see below) we
3440
   generate the names segment<NUM>a and segment<NUM>b.
3441
3442
   Note that some program segments may have a file size that is different than
3443
   (less than) the memory size.  All this means is that at execution the
3444
   system must allocate the amount of memory specified by the memory size,
3445
   but only initialize it with the first "file size" bytes read from the
3446
   file.  This would occur for example, with program segments consisting
3447
   of combined data+bss.
3448
3449
   To handle the above situation, this routine generates TWO bfd sections
3450
   for the single program segment.  The first has the length specified by
3451
   the file size of the segment, and the second has the length specified
3452
   by the difference between the two sizes.  In effect, the segment is split
3453
   into its initialized and uninitialized parts.  */
3454
3455
bool
3456
_bfd_elf_make_section_from_phdr (bfd *abfd,
3457
         Elf_Internal_Phdr *hdr,
3458
         int hdr_index,
3459
         const char *type_name)
3460
240k
{
3461
240k
  asection *newsect;
3462
240k
  char *name;
3463
240k
  char namebuf[64];
3464
240k
  size_t len;
3465
240k
  int split;
3466
240k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3467
3468
240k
  split = ((hdr->p_memsz > 0)
3469
240k
      && (hdr->p_filesz > 0)
3470
240k
      && (hdr->p_memsz > hdr->p_filesz));
3471
3472
240k
  if (hdr->p_filesz > 0)
3473
204k
    {
3474
204k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3475
204k
      len = strlen (namebuf) + 1;
3476
204k
      name = (char *) bfd_alloc (abfd, len);
3477
204k
      if (!name)
3478
0
  return false;
3479
204k
      memcpy (name, namebuf, len);
3480
204k
      newsect = bfd_make_section (abfd, name);
3481
204k
      if (newsect == NULL)
3482
0
  return false;
3483
204k
      newsect->vma = hdr->p_vaddr / opb;
3484
204k
      newsect->lma = hdr->p_paddr / opb;
3485
204k
      newsect->size = hdr->p_filesz;
3486
204k
      newsect->filepos = hdr->p_offset;
3487
204k
      newsect->flags |= SEC_HAS_CONTENTS;
3488
204k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3489
204k
      if (hdr->p_type == PT_LOAD)
3490
1.43k
  {
3491
1.43k
    newsect->flags |= SEC_ALLOC;
3492
1.43k
    newsect->flags |= SEC_LOAD;
3493
1.43k
    if (hdr->p_flags & PF_X)
3494
653
      {
3495
        /* FIXME: all we known is that it has execute PERMISSION,
3496
     may be data.  */
3497
653
        newsect->flags |= SEC_CODE;
3498
653
      }
3499
1.43k
  }
3500
204k
      if (!(hdr->p_flags & PF_W))
3501
144k
  {
3502
144k
    newsect->flags |= SEC_READONLY;
3503
144k
  }
3504
204k
    }
3505
3506
240k
  if (hdr->p_memsz > hdr->p_filesz)
3507
95.1k
    {
3508
95.1k
      bfd_vma align;
3509
3510
95.1k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3511
95.1k
      len = strlen (namebuf) + 1;
3512
95.1k
      name = (char *) bfd_alloc (abfd, len);
3513
95.1k
      if (!name)
3514
0
  return false;
3515
95.1k
      memcpy (name, namebuf, len);
3516
95.1k
      newsect = bfd_make_section (abfd, name);
3517
95.1k
      if (newsect == NULL)
3518
0
  return false;
3519
95.1k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3520
95.1k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3521
95.1k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3522
95.1k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3523
95.1k
      align = newsect->vma & -newsect->vma;
3524
95.1k
      if (align == 0 || align > hdr->p_align)
3525
18.9k
  align = hdr->p_align;
3526
95.1k
      newsect->alignment_power = bfd_log2 (align);
3527
95.1k
      if (hdr->p_type == PT_LOAD)
3528
795
  {
3529
795
    newsect->flags |= SEC_ALLOC;
3530
795
    if (hdr->p_flags & PF_X)
3531
338
      newsect->flags |= SEC_CODE;
3532
795
  }
3533
95.1k
      if (!(hdr->p_flags & PF_W))
3534
66.9k
  newsect->flags |= SEC_READONLY;
3535
95.1k
    }
3536
3537
240k
  return true;
3538
240k
}
3539
3540
static bool
3541
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3542
1.56k
{
3543
  /* The return value is ignored.  Build-ids are considered optional.  */
3544
1.56k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3545
1.56k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3546
1.56k
      (templ, offset);
3547
0
  return false;
3548
1.56k
}
3549
3550
bool
3551
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3552
240k
{
3553
240k
  const struct elf_backend_data *bed;
3554
3555
240k
  switch (hdr->p_type)
3556
240k
    {
3557
42.2k
    case PT_NULL:
3558
42.2k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3559
3560
1.56k
    case PT_LOAD:
3561
1.56k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3562
0
  return false;
3563
1.56k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3564
1.56k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3565
1.56k
      return true;
3566
3567
706
    case PT_DYNAMIC:
3568
706
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3569
3570
974
    case PT_INTERP:
3571
974
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3572
3573
141
    case PT_NOTE:
3574
141
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3575
0
  return false;
3576
141
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3577
141
          hdr->p_align))
3578
63
  return false;
3579
78
      return true;
3580
3581
315
    case PT_SHLIB:
3582
315
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3583
3584
3.59k
    case PT_PHDR:
3585
3.59k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3586
3587
3
    case PT_GNU_EH_FRAME:
3588
3
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3589
3
                "eh_frame_hdr");
3590
3591
36
    case PT_GNU_STACK:
3592
36
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3593
3594
6
    case PT_GNU_RELRO:
3595
6
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3596
3597
0
    case PT_GNU_SFRAME:
3598
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3599
0
                "sframe");
3600
3601
190k
    default:
3602
      /* Check for any processor-specific program segment types.  */
3603
190k
      bed = get_elf_backend_data (abfd);
3604
190k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3605
240k
    }
3606
240k
}
3607
3608
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3609
   REL or RELA.  */
3610
3611
Elf_Internal_Shdr *
3612
_bfd_elf_single_rel_hdr (asection *sec)
3613
0
{
3614
0
  if (elf_section_data (sec)->rel.hdr)
3615
0
    {
3616
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3617
0
      return elf_section_data (sec)->rel.hdr;
3618
0
    }
3619
0
  else
3620
0
    return elf_section_data (sec)->rela.hdr;
3621
0
}
3622
3623
static bool
3624
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3625
          Elf_Internal_Shdr *rel_hdr,
3626
          const char *sec_name,
3627
          bool use_rela_p)
3628
509
{
3629
509
  char *name = (char *) bfd_alloc (abfd,
3630
509
           sizeof ".rela" + strlen (sec_name));
3631
509
  if (name == NULL)
3632
0
    return false;
3633
3634
509
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3635
509
  rel_hdr->sh_name =
3636
509
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3637
509
          false);
3638
509
  if (rel_hdr->sh_name == (unsigned int) -1)
3639
0
    return false;
3640
3641
509
  return true;
3642
509
}
3643
3644
/* Allocate and initialize a section-header for a new reloc section,
3645
   containing relocations against ASECT.  It is stored in RELDATA.  If
3646
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3647
   relocations.  */
3648
3649
static bool
3650
_bfd_elf_init_reloc_shdr (bfd *abfd,
3651
        struct bfd_elf_section_reloc_data *reldata,
3652
        const char *sec_name,
3653
        bool use_rela_p,
3654
        bool delay_st_name_p)
3655
509
{
3656
509
  Elf_Internal_Shdr *rel_hdr;
3657
509
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3658
3659
509
  BFD_ASSERT (reldata->hdr == NULL);
3660
509
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3661
509
  if (rel_hdr == NULL)
3662
0
    return false;
3663
509
  reldata->hdr = rel_hdr;
3664
3665
509
  if (delay_st_name_p)
3666
0
    rel_hdr->sh_name = (unsigned int) -1;
3667
509
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3668
509
          use_rela_p))
3669
0
    return false;
3670
509
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3671
509
  rel_hdr->sh_entsize = (use_rela_p
3672
509
       ? bed->s->sizeof_rela
3673
509
       : bed->s->sizeof_rel);
3674
509
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3675
509
  rel_hdr->sh_flags = 0;
3676
509
  rel_hdr->sh_addr = 0;
3677
509
  rel_hdr->sh_size = 0;
3678
509
  rel_hdr->sh_offset = 0;
3679
3680
509
  return true;
3681
509
}
3682
3683
/* Return the default section type based on the passed in section flags.  */
3684
3685
int
3686
bfd_elf_get_default_section_type (flagword flags)
3687
1.11k
{
3688
1.11k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3689
1.11k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3690
13
    return SHT_NOBITS;
3691
1.10k
  return SHT_PROGBITS;
3692
1.11k
}
3693
3694
struct fake_section_arg
3695
{
3696
  struct bfd_link_info *link_info;
3697
  bool failed;
3698
};
3699
3700
/* Set up an ELF internal section header for a section.  */
3701
3702
static void
3703
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3704
1.18k
{
3705
1.18k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3706
1.18k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3707
1.18k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3708
1.18k
  Elf_Internal_Shdr *this_hdr;
3709
1.18k
  unsigned int sh_type;
3710
1.18k
  const char *name = asect->name;
3711
1.18k
  bool delay_st_name_p = false;
3712
1.18k
  bfd_vma mask;
3713
3714
1.18k
  if (arg->failed)
3715
0
    {
3716
      /* We already failed; just get out of the bfd_map_over_sections
3717
   loop.  */
3718
0
      return;
3719
0
    }
3720
3721
1.18k
  this_hdr = &esd->this_hdr;
3722
3723
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3724
1.18k
  if (arg->link_info
3725
1.18k
      && (abfd->flags & BFD_COMPRESS) != 0
3726
1.18k
      && (asect->flags & SEC_DEBUGGING) != 0
3727
1.18k
      && name[1] == 'd'
3728
1.18k
      && name[6] == '_')
3729
0
    {
3730
      /* If this section will be compressed, delay adding section
3731
   name to section name section after it is compressed in
3732
   _bfd_elf_assign_file_positions_for_non_load.  */
3733
0
      delay_st_name_p = true;
3734
0
    }
3735
3736
1.18k
  if (delay_st_name_p)
3737
0
    this_hdr->sh_name = (unsigned int) -1;
3738
1.18k
  else
3739
1.18k
    {
3740
1.18k
      this_hdr->sh_name
3741
1.18k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3742
1.18k
                name, false);
3743
1.18k
      if (this_hdr->sh_name == (unsigned int) -1)
3744
0
  {
3745
0
    arg->failed = true;
3746
0
    return;
3747
0
  }
3748
1.18k
    }
3749
3750
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3751
3752
1.18k
  if ((asect->flags & SEC_ALLOC) != 0
3753
1.18k
      || asect->user_set_vma)
3754
1.18k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3755
0
  else
3756
0
    this_hdr->sh_addr = 0;
3757
3758
1.18k
  this_hdr->sh_offset = 0;
3759
1.18k
  this_hdr->sh_size = asect->size;
3760
1.18k
  this_hdr->sh_link = 0;
3761
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3762
1.18k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3763
0
    {
3764
0
      _bfd_error_handler
3765
  /* xgettext:c-format */
3766
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3767
0
   abfd, asect->alignment_power, asect);
3768
0
      arg->failed = true;
3769
0
      return;
3770
0
    }
3771
  /* Set sh_addralign to the highest power of two given by alignment
3772
     consistent with the section VMA.  Linker scripts can force VMA.  */
3773
1.18k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3774
1.18k
  this_hdr->sh_addralign = mask & -mask;
3775
  /* The sh_entsize and sh_info fields may have been set already by
3776
     copy_private_section_data.  */
3777
3778
1.18k
  this_hdr->bfd_section = asect;
3779
1.18k
  this_hdr->contents = NULL;
3780
3781
  /* If the section type is unspecified, we set it based on
3782
     asect->flags.  */
3783
1.18k
  if (asect->type != 0)
3784
0
    sh_type = asect->type;
3785
1.18k
  else if ((asect->flags & SEC_GROUP) != 0)
3786
221
    sh_type = SHT_GROUP;
3787
960
  else
3788
960
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3789
3790
1.18k
  if (this_hdr->sh_type == SHT_NULL)
3791
0
    this_hdr->sh_type = sh_type;
3792
1.18k
  else if (this_hdr->sh_type == SHT_NOBITS
3793
1.18k
     && sh_type == SHT_PROGBITS
3794
1.18k
     && (asect->flags & SEC_ALLOC) != 0)
3795
0
    {
3796
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3797
   allow the link to proceed.  This can happen when users link
3798
   non-bss input sections to bss output sections, or emit data
3799
   to a bss output section via a linker script.  */
3800
0
      _bfd_error_handler
3801
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3802
0
      this_hdr->sh_type = sh_type;
3803
0
    }
3804
3805
1.18k
  switch (this_hdr->sh_type)
3806
1.18k
    {
3807
12
    default:
3808
12
      break;
3809
3810
12
    case SHT_STRTAB:
3811
15
    case SHT_NOTE:
3812
28
    case SHT_NOBITS:
3813
876
    case SHT_PROGBITS:
3814
876
      break;
3815
3816
12
    case SHT_INIT_ARRAY:
3817
19
    case SHT_FINI_ARRAY:
3818
19
    case SHT_PREINIT_ARRAY:
3819
19
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3820
19
      break;
3821
3822
6
    case SHT_HASH:
3823
6
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3824
6
      break;
3825
3826
7
    case SHT_DYNSYM:
3827
7
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3828
7
      break;
3829
3830
7
    case SHT_DYNAMIC:
3831
7
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3832
7
      break;
3833
3834
12
    case SHT_RELA:
3835
12
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3836
9
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3837
12
      break;
3838
3839
4
     case SHT_REL:
3840
4
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3841
4
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3842
4
      break;
3843
3844
6
     case SHT_GNU_versym:
3845
6
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3846
6
      break;
3847
3848
0
     case SHT_GNU_verdef:
3849
0
      this_hdr->sh_entsize = 0;
3850
      /* objcopy or strip will copy over sh_info, but may not set
3851
   cverdefs.  The linker will set cverdefs, but sh_info will be
3852
   zero.  */
3853
0
      if (this_hdr->sh_info == 0)
3854
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3855
0
      else
3856
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3857
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3858
0
      break;
3859
3860
7
    case SHT_GNU_verneed:
3861
7
      this_hdr->sh_entsize = 0;
3862
      /* objcopy or strip will copy over sh_info, but may not set
3863
   cverrefs.  The linker will set cverrefs, but sh_info will be
3864
   zero.  */
3865
7
      if (this_hdr->sh_info == 0)
3866
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3867
7
      else
3868
7
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3869
7
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3870
7
      break;
3871
3872
221
    case SHT_GROUP:
3873
221
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3874
221
      break;
3875
3876
4
    case SHT_GNU_HASH:
3877
4
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3878
4
      break;
3879
1.18k
    }
3880
3881
1.18k
  if ((asect->flags & SEC_ALLOC) != 0)
3882
904
    this_hdr->sh_flags |= SHF_ALLOC;
3883
1.18k
  if ((asect->flags & SEC_READONLY) == 0)
3884
301
    this_hdr->sh_flags |= SHF_WRITE;
3885
1.18k
  if ((asect->flags & SEC_CODE) != 0)
3886
264
    this_hdr->sh_flags |= SHF_EXECINSTR;
3887
1.18k
  if ((asect->flags & SEC_MERGE) != 0)
3888
23
    {
3889
23
      this_hdr->sh_flags |= SHF_MERGE;
3890
23
      this_hdr->sh_entsize = asect->entsize;
3891
23
    }
3892
1.18k
  if ((asect->flags & SEC_STRINGS) != 0)
3893
19
    this_hdr->sh_flags |= SHF_STRINGS;
3894
1.18k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3895
675
    this_hdr->sh_flags |= SHF_GROUP;
3896
1.18k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3897
0
    {
3898
0
      this_hdr->sh_flags |= SHF_TLS;
3899
0
      if (asect->size == 0
3900
0
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3901
0
  {
3902
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3903
3904
0
    this_hdr->sh_size = 0;
3905
0
    if (o != NULL)
3906
0
      {
3907
0
        this_hdr->sh_size = o->offset + o->size;
3908
0
        if (this_hdr->sh_size != 0)
3909
0
    this_hdr->sh_type = SHT_NOBITS;
3910
0
      }
3911
0
  }
3912
0
    }
3913
1.18k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3914
5
    this_hdr->sh_flags |= SHF_EXCLUDE;
3915
3916
  /* If the section has relocs, set up a section header for the
3917
     SHT_REL[A] section.  If two relocation sections are required for
3918
     this section, it is up to the processor-specific back-end to
3919
     create the other.  */
3920
1.18k
  if ((asect->flags & SEC_RELOC) != 0)
3921
509
    {
3922
      /* When doing a relocatable link, create both REL and RELA sections if
3923
   needed.  */
3924
509
      if (arg->link_info
3925
    /* Do the normal setup if we wouldn't create any sections here.  */
3926
509
    && esd->rel.count + esd->rela.count > 0
3927
509
    && (bfd_link_relocatable (arg->link_info)
3928
0
        || arg->link_info->emitrelocations))
3929
0
  {
3930
0
    if (esd->rel.count && esd->rel.hdr == NULL
3931
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3932
0
              false, delay_st_name_p))
3933
0
      {
3934
0
        arg->failed = true;
3935
0
        return;
3936
0
      }
3937
0
    if (esd->rela.count && esd->rela.hdr == NULL
3938
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3939
0
              true, delay_st_name_p))
3940
0
      {
3941
0
        arg->failed = true;
3942
0
        return;
3943
0
      }
3944
0
  }
3945
509
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3946
509
            (asect->use_rela_p
3947
509
             ? &esd->rela : &esd->rel),
3948
509
            name,
3949
509
            asect->use_rela_p,
3950
509
            delay_st_name_p))
3951
0
  {
3952
0
    arg->failed = true;
3953
0
    return;
3954
0
  }
3955
509
    }
3956
3957
  /* Check for processor-specific section types.  */
3958
1.18k
  sh_type = this_hdr->sh_type;
3959
1.18k
  if (bed->elf_backend_fake_sections
3960
1.18k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3961
0
    {
3962
0
      arg->failed = true;
3963
0
      return;
3964
0
    }
3965
3966
1.18k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3967
13
    {
3968
      /* Don't change the header type from NOBITS if we are being
3969
   called for objcopy --only-keep-debug.  */
3970
13
      this_hdr->sh_type = sh_type;
3971
13
    }
3972
1.18k
}
3973
3974
/* Fill in the contents of a SHT_GROUP section.  Called from
3975
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3976
   when ELF targets use the generic linker, ld.  Called for ld -r
3977
   from bfd_elf_final_link.  */
3978
3979
void
3980
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3981
1.18k
{
3982
1.18k
  bool *failedptr = (bool *) failedptrarg;
3983
1.18k
  asection *elt, *first;
3984
1.18k
  unsigned char *loc;
3985
1.18k
  bool gas;
3986
3987
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3988
     elfxx-ia64.c.  */
3989
1.18k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3990
1.18k
      || sec->size == 0
3991
1.18k
      || *failedptr)
3992
960
    return;
3993
3994
221
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3995
221
    {
3996
221
      unsigned long symindx = 0;
3997
3998
      /* elf_group_id will have been set up by objcopy and the
3999
   generic linker.  */
4000
221
      if (elf_group_id (sec) != NULL)
4001
221
  symindx = elf_group_id (sec)->udata.i;
4002
4003
221
      if (symindx == 0)
4004
0
  {
4005
    /* If called from the assembler, swap_out_syms will have set up
4006
       elf_section_syms.
4007
       PR 25699: A corrupt input file could contain bogus group info.  */
4008
0
    if (sec->index >= elf_num_section_syms (abfd)
4009
0
        || elf_section_syms (abfd)[sec->index] == NULL)
4010
0
      {
4011
0
        *failedptr = true;
4012
0
        return;
4013
0
      }
4014
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4015
0
  }
4016
221
      elf_section_data (sec)->this_hdr.sh_info = symindx;
4017
221
    }
4018
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4019
0
    {
4020
      /* The ELF backend linker sets sh_info to -2 when the group
4021
   signature symbol is global, and thus the index can't be
4022
   set until all local symbols are output.  */
4023
0
      asection *igroup;
4024
0
      struct bfd_elf_section_data *sec_data;
4025
0
      unsigned long symndx;
4026
0
      unsigned long extsymoff;
4027
0
      struct elf_link_hash_entry *h;
4028
4029
      /* The point of this little dance to the first SHF_GROUP section
4030
   then back to the SHT_GROUP section is that this gets us to
4031
   the SHT_GROUP in the input object.  */
4032
0
      igroup = elf_sec_group (elf_next_in_group (sec));
4033
0
      sec_data = elf_section_data (igroup);
4034
0
      symndx = sec_data->this_hdr.sh_info;
4035
0
      extsymoff = 0;
4036
0
      if (!elf_bad_symtab (igroup->owner))
4037
0
  {
4038
0
    Elf_Internal_Shdr *symtab_hdr;
4039
4040
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4041
0
    extsymoff = symtab_hdr->sh_info;
4042
0
  }
4043
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4044
0
      while (h->root.type == bfd_link_hash_indirect
4045
0
       || h->root.type == bfd_link_hash_warning)
4046
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4047
4048
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
4049
0
    }
4050
4051
  /* The contents won't be allocated for "ld -r" or objcopy.  */
4052
221
  gas = true;
4053
221
  if (sec->contents == NULL)
4054
221
    {
4055
221
      gas = false;
4056
221
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4057
4058
      /* Arrange for the section to be written out.  */
4059
221
      elf_section_data (sec)->this_hdr.contents = sec->contents;
4060
221
      if (sec->contents == NULL)
4061
0
  {
4062
0
    *failedptr = true;
4063
0
    return;
4064
0
  }
4065
221
    }
4066
4067
221
  loc = sec->contents + sec->size;
4068
4069
  /* Get the pointer to the first section in the group that gas
4070
     squirreled away here.  objcopy arranges for this to be set to the
4071
     start of the input section group.  */
4072
221
  first = elt = elf_next_in_group (sec);
4073
4074
  /* First element is a flag word.  Rest of section is elf section
4075
     indices for all the sections of the group.  Write them backwards
4076
     just to keep the group in the same order as given in .section
4077
     directives, not that it matters.  */
4078
675
  while (elt != NULL)
4079
675
    {
4080
675
      asection *s;
4081
4082
675
      s = elt;
4083
675
      if (!gas)
4084
675
  s = s->output_section;
4085
675
      if (s != NULL
4086
675
    && !bfd_is_abs_section (s))
4087
675
  {
4088
675
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4089
675
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4090
4091
675
    if (elf_sec->rel.hdr != NULL
4092
675
        && (gas
4093
0
      || (input_elf_sec->rel.hdr != NULL
4094
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4095
0
      {
4096
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4097
0
        loc -= 4;
4098
0
        if (loc == sec->contents)
4099
0
    break;
4100
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4101
0
      }
4102
675
    if (elf_sec->rela.hdr != NULL
4103
675
        && (gas
4104
459
      || (input_elf_sec->rela.hdr != NULL
4105
459
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4106
459
      {
4107
459
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4108
459
        loc -= 4;
4109
459
        if (loc == sec->contents)
4110
0
    break;
4111
459
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4112
459
      }
4113
675
    loc -= 4;
4114
675
    if (loc == sec->contents)
4115
0
      break;
4116
675
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4117
675
  }
4118
675
      elt = elf_next_in_group (elt);
4119
675
      if (elt == first)
4120
221
  break;
4121
675
    }
4122
4123
  /* We should always get here with loc == sec->contents + 4, but it is
4124
     possible to craft bogus SHT_GROUP sections that will cause segfaults
4125
     in objcopy without checking loc here and in the loop above.  */
4126
221
  if (loc == sec->contents)
4127
0
    BFD_ASSERT (0);
4128
221
  else
4129
221
    {
4130
221
      loc -= 4;
4131
221
      if (loc != sec->contents)
4132
0
  {
4133
0
    BFD_ASSERT (0);
4134
0
    memset (sec->contents + 4, 0, loc - sec->contents);
4135
0
    loc = sec->contents;
4136
0
  }
4137
221
    }
4138
4139
221
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4140
221
}
4141
4142
/* Given NAME, the name of a relocation section stripped of its
4143
   .rel/.rela prefix, return the section in ABFD to which the
4144
   relocations apply.  */
4145
4146
asection *
4147
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4148
16
{
4149
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4150
     section likely apply to .got.plt or .got section.  */
4151
16
  if (get_elf_backend_data (abfd)->want_got_plt
4152
16
      && strcmp (name, ".plt") == 0)
4153
3
    {
4154
3
      asection *sec;
4155
4156
3
      name = ".got.plt";
4157
3
      sec = bfd_get_section_by_name (abfd, name);
4158
3
      if (sec != NULL)
4159
1
  return sec;
4160
2
      name = ".got";
4161
2
    }
4162
4163
15
  return bfd_get_section_by_name (abfd, name);
4164
16
}
4165
4166
/* Return the section to which RELOC_SEC applies.  */
4167
4168
static asection *
4169
elf_get_reloc_section (asection *reloc_sec)
4170
16
{
4171
16
  const char *name;
4172
16
  unsigned int type;
4173
16
  bfd *abfd;
4174
16
  const struct elf_backend_data *bed;
4175
4176
16
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4177
16
  if (type != SHT_REL && type != SHT_RELA)
4178
0
    return NULL;
4179
4180
  /* We look up the section the relocs apply to by name.  */
4181
16
  name = reloc_sec->name;
4182
16
  if (!startswith (name, ".rel"))
4183
0
    return NULL;
4184
16
  name += 4;
4185
16
  if (type == SHT_RELA && *name++ != 'a')
4186
0
    return NULL;
4187
4188
16
  abfd = reloc_sec->owner;
4189
16
  bed = get_elf_backend_data (abfd);
4190
16
  return bed->get_reloc_section (abfd, name);
4191
16
}
4192
4193
/* Assign all ELF section numbers.  The dummy first section is handled here
4194
   too.  The link/info pointers for the standard section types are filled
4195
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4196
   here for gas, objcopy, and when using the generic ELF linker.  */
4197
4198
static bool
4199
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4200
28
{
4201
28
  struct elf_obj_tdata *t = elf_tdata (abfd);
4202
28
  asection *sec;
4203
28
  unsigned int section_number;
4204
28
  Elf_Internal_Shdr **i_shdrp;
4205
28
  struct bfd_elf_section_data *d;
4206
28
  bool need_symtab;
4207
28
  size_t amt;
4208
4209
28
  section_number = 1;
4210
4211
28
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4212
4213
  /* SHT_GROUP sections are in relocatable files only.  */
4214
28
  if (link_info == NULL || !link_info->resolve_section_groups)
4215
28
    {
4216
28
      size_t reloc_count = 0;
4217
4218
      /* Put SHT_GROUP sections first.  */
4219
1.20k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4220
1.18k
  {
4221
1.18k
    d = elf_section_data (sec);
4222
4223
1.18k
    if (d->this_hdr.sh_type == SHT_GROUP)
4224
221
      {
4225
221
        if (sec->flags & SEC_LINKER_CREATED)
4226
0
    {
4227
      /* Remove the linker created SHT_GROUP sections.  */
4228
0
      bfd_section_list_remove (abfd, sec);
4229
0
      abfd->section_count--;
4230
0
    }
4231
221
        else
4232
221
    d->this_idx = section_number++;
4233
221
      }
4234
4235
    /* Count relocations.  */
4236
1.18k
    reloc_count += sec->reloc_count;
4237
1.18k
  }
4238
4239
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4240
28
      if (reloc_count == 0)
4241
23
  abfd->flags &= ~HAS_RELOC;
4242
5
      else
4243
5
  abfd->flags |= HAS_RELOC;
4244
28
    }
4245
4246
1.20k
  for (sec = abfd->sections; sec; sec = sec->next)
4247
1.18k
    {
4248
1.18k
      d = elf_section_data (sec);
4249
4250
1.18k
      if (d->this_hdr.sh_type != SHT_GROUP)
4251
960
  d->this_idx = section_number++;
4252
1.18k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4253
1.18k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4254
1.18k
      if (d->rel.hdr)
4255
0
  {
4256
0
    d->rel.idx = section_number++;
4257
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4258
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4259
0
  }
4260
1.18k
      else
4261
1.18k
  d->rel.idx = 0;
4262
4263
1.18k
      if (d->rela.hdr)
4264
509
  {
4265
509
    d->rela.idx = section_number++;
4266
509
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4267
509
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4268
509
  }
4269
672
      else
4270
672
  d->rela.idx = 0;
4271
1.18k
    }
4272
4273
28
  need_symtab = (bfd_get_symcount (abfd) > 0
4274
28
     || (link_info == NULL
4275
21
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4276
21
       == HAS_RELOC)));
4277
28
  if (need_symtab)
4278
7
    {
4279
7
      elf_onesymtab (abfd) = section_number++;
4280
7
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4281
7
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4282
0
  {
4283
0
    elf_section_list *entry;
4284
4285
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4286
4287
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4288
0
    entry->ndx = section_number++;
4289
0
    elf_symtab_shndx_list (abfd) = entry;
4290
0
    entry->hdr.sh_name
4291
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4292
0
              ".symtab_shndx", false);
4293
0
    if (entry->hdr.sh_name == (unsigned int) -1)
4294
0
      return false;
4295
0
  }
4296
7
      elf_strtab_sec (abfd) = section_number++;
4297
7
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4298
7
    }
4299
4300
28
  elf_shstrtab_sec (abfd) = section_number++;
4301
28
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4302
28
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4303
4304
28
  if (section_number >= SHN_LORESERVE)
4305
0
    {
4306
      /* xgettext:c-format */
4307
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4308
0
        abfd, section_number);
4309
0
      return false;
4310
0
    }
4311
4312
28
  elf_numsections (abfd) = section_number;
4313
28
  elf_elfheader (abfd)->e_shnum = section_number;
4314
4315
  /* Set up the list of section header pointers, in agreement with the
4316
     indices.  */
4317
28
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4318
28
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4319
28
  if (i_shdrp == NULL)
4320
0
    return false;
4321
4322
28
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4323
28
             sizeof (Elf_Internal_Shdr));
4324
28
  if (i_shdrp[0] == NULL)
4325
0
    {
4326
0
      bfd_release (abfd, i_shdrp);
4327
0
      return false;
4328
0
    }
4329
4330
28
  elf_elfsections (abfd) = i_shdrp;
4331
4332
28
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4333
28
  if (need_symtab)
4334
7
    {
4335
7
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4336
7
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4337
0
  {
4338
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4339
0
    BFD_ASSERT (entry != NULL);
4340
0
    i_shdrp[entry->ndx] = & entry->hdr;
4341
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4342
0
  }
4343
7
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4344
7
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4345
7
    }
4346
4347
1.20k
  for (sec = abfd->sections; sec; sec = sec->next)
4348
1.18k
    {
4349
1.18k
      asection *s;
4350
4351
1.18k
      d = elf_section_data (sec);
4352
4353
1.18k
      i_shdrp[d->this_idx] = &d->this_hdr;
4354
1.18k
      if (d->rel.idx != 0)
4355
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4356
1.18k
      if (d->rela.idx != 0)
4357
509
  i_shdrp[d->rela.idx] = d->rela.hdr;
4358
4359
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4360
4361
      /* sh_link of a reloc section is the section index of the symbol
4362
   table.  sh_info is the section index of the section to which
4363
   the relocation entries apply.  */
4364
1.18k
      if (d->rel.idx != 0)
4365
0
  {
4366
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4367
0
    d->rel.hdr->sh_info = d->this_idx;
4368
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4369
0
  }
4370
1.18k
      if (d->rela.idx != 0)
4371
509
  {
4372
509
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4373
509
    d->rela.hdr->sh_info = d->this_idx;
4374
509
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4375
509
  }
4376
4377
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4378
1.18k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4379
0
  {
4380
0
    s = elf_linked_to_section (sec);
4381
    /* We can now have a NULL linked section pointer.
4382
       This happens when the sh_link field is 0, which is done
4383
       when a linked to section is discarded but the linking
4384
       section has been retained for some reason.  */
4385
0
    if (s)
4386
0
      {
4387
        /* Check discarded linkonce section.  */
4388
0
        if (discarded_section (s))
4389
0
    {
4390
0
      asection *kept;
4391
0
      _bfd_error_handler
4392
        /* xgettext:c-format */
4393
0
        (_("%pB: sh_link of section `%pA' points to"
4394
0
           " discarded section `%pA' of `%pB'"),
4395
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4396
      /* Point to the kept section if it has the same
4397
         size as the discarded one.  */
4398
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4399
0
      if (kept == NULL)
4400
0
        {
4401
0
          bfd_set_error (bfd_error_bad_value);
4402
0
          return false;
4403
0
        }
4404
0
      s = kept;
4405
0
    }
4406
        /* Handle objcopy. */
4407
0
        else if (s->output_section == NULL)
4408
0
    {
4409
0
      _bfd_error_handler
4410
        /* xgettext:c-format */
4411
0
        (_("%pB: sh_link of section `%pA' points to"
4412
0
           " removed section `%pA' of `%pB'"),
4413
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4414
0
      bfd_set_error (bfd_error_bad_value);
4415
0
      return false;
4416
0
    }
4417
0
        s = s->output_section;
4418
0
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4419
0
      }
4420
0
  }
4421
4422
1.18k
      switch (d->this_hdr.sh_type)
4423
1.18k
  {
4424
4
  case SHT_REL:
4425
16
  case SHT_RELA:
4426
    /* sh_link is the section index of the symbol table.
4427
       sh_info is the section index of the section to which the
4428
       relocation entries apply.  */
4429
16
    if (d->this_hdr.sh_link == 0)
4430
16
      {
4431
        /* FIXME maybe: If this is a reloc section which we are
4432
     treating as a normal section then we likely should
4433
     not be assuming its sh_link is .dynsym or .symtab.  */
4434
16
        if ((sec->flags & SEC_ALLOC) != 0)
4435
16
    {
4436
16
      s = bfd_get_section_by_name (abfd, ".dynsym");
4437
16
      if (s != NULL)
4438
13
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4439
16
    }
4440
0
        else
4441
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4442
16
      }
4443
4444
16
    s = elf_get_reloc_section (sec);
4445
16
    if (s != NULL)
4446
8
      {
4447
8
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4448
8
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4449
8
      }
4450
16
    break;
4451
4452
7
  case SHT_STRTAB:
4453
    /* We assume that a section named .stab*str is a stabs
4454
       string section.  We look for a section with the same name
4455
       but without the trailing ``str'', and set its sh_link
4456
       field to point to this section.  */
4457
7
    if (startswith (sec->name, ".stab")
4458
7
        && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4459
0
      {
4460
0
        size_t len;
4461
0
        char *alc;
4462
4463
0
        len = strlen (sec->name);
4464
0
        alc = (char *) bfd_malloc (len - 2);
4465
0
        if (alc == NULL)
4466
0
    return false;
4467
0
        memcpy (alc, sec->name, len - 3);
4468
0
        alc[len - 3] = '\0';
4469
0
        s = bfd_get_section_by_name (abfd, alc);
4470
0
        free (alc);
4471
0
        if (s != NULL)
4472
0
    {
4473
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4474
4475
      /* This is a .stab section.  */
4476
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4477
0
    }
4478
0
      }
4479
7
    break;
4480
4481
7
  case SHT_DYNAMIC:
4482
14
  case SHT_DYNSYM:
4483
21
  case SHT_GNU_verneed:
4484
21
  case SHT_GNU_verdef:
4485
    /* sh_link is the section header index of the string table
4486
       used for the dynamic entries, or the symbol table, or the
4487
       version strings.  */
4488
21
    s = bfd_get_section_by_name (abfd, ".dynstr");
4489
21
    if (s != NULL)
4490
21
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4491
21
    break;
4492
4493
0
  case SHT_GNU_LIBLIST:
4494
    /* sh_link is the section header index of the prelink library
4495
       list used for the dynamic entries, or the symbol table, or
4496
       the version strings.  */
4497
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4498
0
                ? ".dynstr" : ".gnu.libstr"));
4499
0
    if (s != NULL)
4500
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4501
0
    break;
4502
4503
6
  case SHT_HASH:
4504
10
  case SHT_GNU_HASH:
4505
16
  case SHT_GNU_versym:
4506
    /* sh_link is the section header index of the symbol table
4507
       this hash table or version table is for.  */
4508
16
    s = bfd_get_section_by_name (abfd, ".dynsym");
4509
16
    if (s != NULL)
4510
16
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4511
16
    break;
4512
4513
221
  case SHT_GROUP:
4514
221
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4515
1.18k
  }
4516
1.18k
    }
4517
4518
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4519
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4520
     debug section name from .debug_* to .zdebug_* if needed.  */
4521
4522
28
  return true;
4523
28
}
4524
4525
static bool
4526
sym_is_global (bfd *abfd, asymbol *sym)
4527
13.0k
{
4528
  /* If the backend has a special mapping, use it.  */
4529
13.0k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4530
13.0k
  if (bed->elf_backend_sym_is_global)
4531
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4532
4533
13.0k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4534
13.0k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4535
13.0k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4536
13.0k
}
4537
4538
/* Filter global symbols of ABFD to include in the import library.  All
4539
   SYMCOUNT symbols of ABFD can be examined from their pointers in
4540
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4541
   the beginning of that array.
4542
4543
   Returns the number of symbols to keep.  */
4544
4545
unsigned int
4546
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4547
        asymbol **syms, long symcount)
4548
0
{
4549
0
  long src_count, dst_count = 0;
4550
4551
0
  for (src_count = 0; src_count < symcount; src_count++)
4552
0
    {
4553
0
      asymbol *sym = syms[src_count];
4554
0
      char *name = (char *) bfd_asymbol_name (sym);
4555
0
      struct bfd_link_hash_entry *h;
4556
4557
0
      if (!sym_is_global (abfd, sym))
4558
0
  continue;
4559
4560
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4561
0
      if (h == NULL)
4562
0
  continue;
4563
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4564
0
  continue;
4565
0
      if (h->linker_def || h->ldscript_def)
4566
0
  continue;
4567
4568
0
      syms[dst_count++] = sym;
4569
0
    }
4570
4571
0
  syms[dst_count] = NULL;
4572
4573
0
  return dst_count;
4574
0
}
4575
4576
/* Don't output symbols for sections that are not going to be output,
4577
   that are duplicates or there is no BFD section.  */
4578
4579
static bool
4580
ignore_sym (asymbol *sym)
4581
15.8k
{
4582
15.8k
  if (sym == NULL)
4583
0
    return false;
4584
4585
15.8k
  if (sym->section == NULL)
4586
0
    return true;
4587
4588
15.8k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4589
4.26k
    {
4590
4.26k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4591
2.02k
  return true;
4592
      /* With ld -r on generic elf targets it is possible to have
4593
   multiple section symbols in the output for a given section.
4594
   We'd like to get rid of all but the first one.  This drops
4595
   them if the first input section is non-zero size, but fails
4596
   to do so if the first input section is zero sized.  */
4597
2.23k
      if (sym->section->output_offset != 0)
4598
0
  return true;
4599
2.23k
    }
4600
4601
13.8k
  return discarded_section (sym->section);
4602
15.8k
}
4603
4604
/* Map symbol from it's internal number to the external number, moving
4605
   all local symbols to be at the head of the list.  */
4606
4607
static bool
4608
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4609
7
{
4610
7
  unsigned int symcount = bfd_get_symcount (abfd);
4611
7
  asymbol **syms = bfd_get_outsymbols (abfd);
4612
7
  asymbol **sect_syms;
4613
7
  unsigned int num_locals = 0;
4614
7
  unsigned int num_globals = 0;
4615
7
  unsigned int max_index = 0;
4616
7
  unsigned int idx;
4617
7
  asection *asect;
4618
7
  asymbol **new_syms;
4619
7
  size_t amt;
4620
4621
#ifdef DEBUG
4622
  fprintf (stderr, "elf_map_symbols\n");
4623
  fflush (stderr);
4624
#endif
4625
4626
1.05k
  for (asect = abfd->sections; asect; asect = asect->next)
4627
1.04k
    {
4628
1.04k
      if (max_index < asect->index)
4629
1.03k
  max_index = asect->index;
4630
1.04k
    }
4631
4632
7
  max_index++;
4633
7
  amt = max_index * sizeof (asymbol *);
4634
7
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4635
7
  if (sect_syms == NULL)
4636
0
    return false;
4637
7
  elf_section_syms (abfd) = sect_syms;
4638
7
  elf_num_section_syms (abfd) = max_index;
4639
4640
  /* Init sect_syms entries for any section symbols we have already
4641
     decided to output.  */
4642
6.54k
  for (idx = 0; idx < symcount; idx++)
4643
6.53k
    {
4644
6.53k
      asymbol *sym = syms[idx];
4645
4646
6.53k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4647
6.53k
    && sym->value == 0
4648
6.53k
    && !ignore_sym (sym)
4649
6.53k
    && !bfd_is_abs_section (sym->section))
4650
714
  {
4651
714
    asection *sec = sym->section;
4652
4653
714
    if (sec->owner != abfd)
4654
714
      sec = sec->output_section;
4655
4656
714
    sect_syms[sec->index] = syms[idx];
4657
714
  }
4658
6.53k
    }
4659
4660
  /* Classify all of the symbols.  */
4661
6.54k
  for (idx = 0; idx < symcount; idx++)
4662
6.53k
    {
4663
6.53k
      if (ignore_sym (syms[idx]))
4664
0
  continue;
4665
6.53k
      if (sym_is_global (abfd, syms[idx]))
4666
2.77k
  num_globals++;
4667
3.76k
      else
4668
3.76k
  num_locals++;
4669
6.53k
    }
4670
4671
  /* We will be adding a section symbol for each normal BFD section.  Most
4672
     sections will already have a section symbol in outsymbols, but
4673
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4674
     at least in that case.  */
4675
1.05k
  for (asect = abfd->sections; asect; asect = asect->next)
4676
1.04k
    {
4677
1.04k
      asymbol *sym = asect->symbol;
4678
      /* Don't include ignored section symbols.  */
4679
1.04k
      if (!ignore_sym (sym)
4680
1.04k
    && sect_syms[asect->index] == NULL)
4681
0
  {
4682
0
    if (sym_is_global (abfd, asect->symbol))
4683
0
      num_globals++;
4684
0
    else
4685
0
      num_locals++;
4686
0
  }
4687
1.04k
    }
4688
4689
  /* Now sort the symbols so the local symbols are first.  */
4690
7
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4691
7
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4692
7
  if (new_syms == NULL)
4693
0
    return false;
4694
4695
7
  unsigned int num_globals2 = 0;
4696
7
  unsigned int num_locals2 = 0;
4697
6.54k
  for (idx = 0; idx < symcount; idx++)
4698
6.53k
    {
4699
6.53k
      asymbol *sym = syms[idx];
4700
6.53k
      unsigned int i;
4701
4702
6.53k
      if (ignore_sym (sym))
4703
0
  continue;
4704
4705
6.53k
      if (sym_is_global (abfd, sym))
4706
2.77k
  i = num_locals + num_globals2++;
4707
3.76k
      else
4708
3.76k
  i = num_locals2++;
4709
6.53k
      new_syms[i] = sym;
4710
6.53k
      sym->udata.i = i + 1;
4711
6.53k
    }
4712
1.05k
  for (asect = abfd->sections; asect; asect = asect->next)
4713
1.04k
    {
4714
1.04k
      asymbol *sym = asect->symbol;
4715
1.04k
      if (!ignore_sym (sym)
4716
1.04k
    && sect_syms[asect->index] == NULL)
4717
0
  {
4718
0
    unsigned int i;
4719
4720
0
    sect_syms[asect->index] = sym;
4721
0
    if (sym_is_global (abfd, sym))
4722
0
      i = num_locals + num_globals2++;
4723
0
    else
4724
0
      i = num_locals2++;
4725
0
    new_syms[i] = sym;
4726
0
    sym->udata.i = i + 1;
4727
0
  }
4728
1.04k
    }
4729
4730
7
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4731
4732
7
  *pnum_locals = num_locals;
4733
7
  return true;
4734
7
}
4735
4736
/* Align to the maximum file alignment that could be required for any
4737
   ELF data structure.  */
4738
4739
static inline file_ptr
4740
align_file_position (file_ptr off, int align)
4741
27
{
4742
27
  return (off + align - 1) & ~(align - 1);
4743
27
}
4744
4745
/* Assign a file position to a section, optionally aligning to the
4746
   required section alignment.  */
4747
4748
file_ptr
4749
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4750
             file_ptr offset,
4751
             bool align)
4752
1.59k
{
4753
1.59k
  if (align && i_shdrp->sh_addralign > 1)
4754
1.27k
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4755
1.59k
  i_shdrp->sh_offset = offset;
4756
1.59k
  if (i_shdrp->bfd_section != NULL)
4757
1.02k
    i_shdrp->bfd_section->filepos = offset;
4758
1.59k
  if (i_shdrp->sh_type != SHT_NOBITS)
4759
1.59k
    offset += i_shdrp->sh_size;
4760
1.59k
  return offset;
4761
1.59k
}
4762
4763
/* Compute the file positions we are going to put the sections at, and
4764
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4765
   is not NULL, this is being called by the ELF backend linker.  */
4766
4767
bool
4768
_bfd_elf_compute_section_file_positions (bfd *abfd,
4769
           struct bfd_link_info *link_info)
4770
28
{
4771
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4772
28
  struct fake_section_arg fsargs;
4773
28
  bool failed;
4774
28
  struct elf_strtab_hash *strtab = NULL;
4775
28
  Elf_Internal_Shdr *shstrtab_hdr;
4776
28
  bool need_symtab;
4777
4778
28
  if (abfd->output_has_begun)
4779
0
    return true;
4780
4781
  /* Do any elf backend specific processing first.  */
4782
28
  if (bed->elf_backend_begin_write_processing)
4783
1
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4784
4785
28
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4786
0
    return false;
4787
4788
28
  fsargs.failed = false;
4789
28
  fsargs.link_info = link_info;
4790
28
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4791
28
  if (fsargs.failed)
4792
0
    return false;
4793
4794
28
  if (!assign_section_numbers (abfd, link_info))
4795
0
    return false;
4796
4797
  /* The backend linker builds symbol table information itself.  */
4798
28
  need_symtab = (link_info == NULL
4799
28
     && (bfd_get_symcount (abfd) > 0
4800
28
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4801
21
       == HAS_RELOC)));
4802
28
  if (need_symtab)
4803
7
    {
4804
      /* Non-zero if doing a relocatable link.  */
4805
7
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4806
4807
7
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4808
0
  return false;
4809
7
    }
4810
4811
28
  failed = false;
4812
28
  if (link_info == NULL)
4813
28
    {
4814
28
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4815
28
      if (failed)
4816
0
  goto err_free_strtab;
4817
28
    }
4818
4819
28
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4820
  /* sh_name was set in init_file_header.  */
4821
28
  shstrtab_hdr->sh_type = SHT_STRTAB;
4822
28
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4823
28
  shstrtab_hdr->sh_addr = 0;
4824
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4825
28
  shstrtab_hdr->sh_entsize = 0;
4826
28
  shstrtab_hdr->sh_link = 0;
4827
28
  shstrtab_hdr->sh_info = 0;
4828
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4829
28
  shstrtab_hdr->sh_addralign = 1;
4830
4831
28
  if (!assign_file_positions_except_relocs (abfd, link_info))
4832
1
    goto err_free_strtab;
4833
4834
27
  if (strtab != NULL)
4835
6
    {
4836
6
      file_ptr off;
4837
6
      Elf_Internal_Shdr *hdr;
4838
4839
6
      off = elf_next_file_pos (abfd);
4840
4841
6
      hdr = & elf_symtab_hdr (abfd);
4842
6
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4843
4844
6
      if (elf_symtab_shndx_list (abfd) != NULL)
4845
0
  {
4846
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4847
0
    if (hdr->sh_size != 0)
4848
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4849
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4850
0
  }
4851
4852
6
      hdr = &elf_tdata (abfd)->strtab_hdr;
4853
6
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4854
4855
6
      elf_next_file_pos (abfd) = off;
4856
4857
      /* Now that we know where the .strtab section goes, write it
4858
   out.  */
4859
6
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4860
6
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4861
0
  goto err_free_strtab;
4862
6
      _bfd_elf_strtab_free (strtab);
4863
6
    }
4864
4865
27
  abfd->output_has_begun = true;
4866
27
  return true;
4867
4868
1
 err_free_strtab:
4869
1
  if (strtab != NULL)
4870
1
    _bfd_elf_strtab_free (strtab);
4871
1
  return false;
4872
27
}
4873
4874
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4875
   function effectively returns whether --eh-frame-hdr is given on the
4876
   command line.  After size_dynamic_sections the result reflects
4877
   whether .eh_frame_hdr will actually be output (sizing isn't done
4878
   until ldemul_after_allocation).  */
4879
4880
static asection *
4881
elf_eh_frame_hdr (const struct bfd_link_info *info)
4882
0
{
4883
0
  if (info != NULL && is_elf_hash_table (info->hash))
4884
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4885
0
  return NULL;
4886
0
}
4887
4888
/* Make an initial estimate of the size of the program header.  If we
4889
   get the number wrong here, we'll redo section placement.  */
4890
4891
static bfd_size_type
4892
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4893
0
{
4894
0
  size_t segs;
4895
0
  asection *s;
4896
0
  const struct elf_backend_data *bed;
4897
4898
  /* Assume we will need exactly two PT_LOAD segments: one for text
4899
     and one for data.  */
4900
0
  segs = 2;
4901
4902
0
  s = bfd_get_section_by_name (abfd, ".interp");
4903
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4904
0
    {
4905
      /* If we have a loadable interpreter section, we need a
4906
   PT_INTERP segment.  In this case, assume we also need a
4907
   PT_PHDR segment, although that may not be true for all
4908
   targets.  */
4909
0
      segs += 2;
4910
0
    }
4911
4912
0
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4913
0
    {
4914
      /* We need a PT_DYNAMIC segment.  */
4915
0
      ++segs;
4916
0
    }
4917
4918
0
  if (info != NULL && info->relro)
4919
0
    {
4920
      /* We need a PT_GNU_RELRO segment.  */
4921
0
      ++segs;
4922
0
    }
4923
4924
0
  if (elf_eh_frame_hdr (info))
4925
0
    {
4926
      /* We need a PT_GNU_EH_FRAME segment.  */
4927
0
      ++segs;
4928
0
    }
4929
4930
0
  if (elf_stack_flags (abfd))
4931
0
    {
4932
      /* We need a PT_GNU_STACK segment.  */
4933
0
      ++segs;
4934
0
    }
4935
4936
0
  if (elf_sframe (abfd))
4937
0
    {
4938
      /* We need a PT_GNU_SFRAME segment.  */
4939
0
      ++segs;
4940
0
    }
4941
4942
0
  s = bfd_get_section_by_name (abfd,
4943
0
             NOTE_GNU_PROPERTY_SECTION_NAME);
4944
0
  if (s != NULL && s->size != 0)
4945
0
    {
4946
      /* We need a PT_GNU_PROPERTY segment.  */
4947
0
      ++segs;
4948
0
    }
4949
4950
0
  for (s = abfd->sections; s != NULL; s = s->next)
4951
0
    {
4952
0
      if ((s->flags & SEC_LOAD) != 0
4953
0
    && elf_section_type (s) == SHT_NOTE)
4954
0
  {
4955
0
    unsigned int alignment_power;
4956
    /* We need a PT_NOTE segment.  */
4957
0
    ++segs;
4958
    /* Try to create just one PT_NOTE segment for all adjacent
4959
       loadable SHT_NOTE sections.  gABI requires that within a
4960
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4961
       each note should have the same alignment.  So we check
4962
       whether the sections are correctly aligned.  */
4963
0
    alignment_power = s->alignment_power;
4964
0
    while (s->next != NULL
4965
0
     && s->next->alignment_power == alignment_power
4966
0
     && (s->next->flags & SEC_LOAD) != 0
4967
0
     && elf_section_type (s->next) == SHT_NOTE)
4968
0
      s = s->next;
4969
0
  }
4970
0
    }
4971
4972
0
  for (s = abfd->sections; s != NULL; s = s->next)
4973
0
    {
4974
0
      if (s->flags & SEC_THREAD_LOCAL)
4975
0
  {
4976
    /* We need a PT_TLS segment.  */
4977
0
    ++segs;
4978
0
    break;
4979
0
  }
4980
0
    }
4981
4982
0
  bed = get_elf_backend_data (abfd);
4983
4984
0
  if ((abfd->flags & D_PAGED) != 0
4985
0
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4986
0
    {
4987
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4988
0
      bfd_vma commonpagesize;
4989
0
      unsigned int page_align_power;
4990
4991
0
      if (info != NULL)
4992
0
  commonpagesize = info->commonpagesize;
4993
0
      else
4994
0
  commonpagesize = bed->commonpagesize;
4995
0
      page_align_power = bfd_log2 (commonpagesize);
4996
0
      for (s = abfd->sections; s != NULL; s = s->next)
4997
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4998
0
    {
4999
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5000
0
        {
5001
0
    _bfd_error_handler
5002
      /* xgettext:c-format */
5003
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
5004
0
         "sh_info field: %d"),
5005
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
5006
0
    continue;
5007
0
        }
5008
      /* Align mbind section to page size.  */
5009
0
      if (s->alignment_power < page_align_power)
5010
0
        s->alignment_power = page_align_power;
5011
0
      segs ++;
5012
0
    }
5013
0
    }
5014
5015
  /* Let the backend count up any program headers it might need.  */
5016
0
  if (bed->elf_backend_additional_program_headers)
5017
0
    {
5018
0
      int a;
5019
5020
0
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5021
0
      if (a == -1)
5022
0
  abort ();
5023
0
      segs += a;
5024
0
    }
5025
5026
0
  return segs * bed->s->sizeof_phdr;
5027
0
}
5028
5029
/* Find the segment that contains the output_section of section.  */
5030
5031
Elf_Internal_Phdr *
5032
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5033
0
{
5034
0
  struct elf_segment_map *m;
5035
0
  Elf_Internal_Phdr *p;
5036
5037
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5038
0
       m != NULL;
5039
0
       m = m->next, p++)
5040
0
    {
5041
0
      int i;
5042
5043
0
      for (i = m->count - 1; i >= 0; i--)
5044
0
  if (m->sections[i] == section)
5045
0
    return p;
5046
0
    }
5047
5048
0
  return NULL;
5049
0
}
5050
5051
/* Create a mapping from a set of sections to a program segment.  */
5052
5053
static struct elf_segment_map *
5054
make_mapping (bfd *abfd,
5055
        asection **sections,
5056
        unsigned int from,
5057
        unsigned int to,
5058
        bool phdr)
5059
0
{
5060
0
  struct elf_segment_map *m;
5061
0
  unsigned int i;
5062
0
  asection **hdrpp;
5063
0
  size_t amt;
5064
5065
0
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5066
0
  amt += (to - from) * sizeof (asection *);
5067
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5068
0
  if (m == NULL)
5069
0
    return NULL;
5070
0
  m->next = NULL;
5071
0
  m->p_type = PT_LOAD;
5072
0
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5073
0
    m->sections[i - from] = *hdrpp;
5074
0
  m->count = to - from;
5075
5076
0
  if (from == 0 && phdr)
5077
0
    {
5078
      /* Include the headers in the first PT_LOAD segment.  */
5079
0
      m->includes_filehdr = 1;
5080
0
      m->includes_phdrs = 1;
5081
0
    }
5082
5083
0
  return m;
5084
0
}
5085
5086
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5087
   on failure.  */
5088
5089
struct elf_segment_map *
5090
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5091
0
{
5092
0
  struct elf_segment_map *m;
5093
5094
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
5095
0
               sizeof (struct elf_segment_map));
5096
0
  if (m == NULL)
5097
0
    return NULL;
5098
0
  m->next = NULL;
5099
0
  m->p_type = PT_DYNAMIC;
5100
0
  m->count = 1;
5101
0
  m->sections[0] = dynsec;
5102
5103
0
  return m;
5104
0
}
5105
5106
/* Possibly add or remove segments from the segment map.  */
5107
5108
static bool
5109
elf_modify_segment_map (bfd *abfd,
5110
      struct bfd_link_info *info,
5111
      bool remove_empty_load)
5112
14
{
5113
14
  struct elf_segment_map **m;
5114
14
  const struct elf_backend_data *bed;
5115
5116
  /* The placement algorithm assumes that non allocated sections are
5117
     not in PT_LOAD segments.  We ensure this here by removing such
5118
     sections from the segment map.  We also remove excluded
5119
     sections.  Finally, any PT_LOAD segment without sections is
5120
     removed.  */
5121
14
  m = &elf_seg_map (abfd);
5122
67
  while (*m)
5123
53
    {
5124
53
      unsigned int i, new_count;
5125
5126
271
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5127
218
  {
5128
218
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5129
218
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5130
218
      || (*m)->p_type != PT_LOAD))
5131
218
      {
5132
218
        (*m)->sections[new_count] = (*m)->sections[i];
5133
218
        new_count++;
5134
218
      }
5135
218
  }
5136
53
      (*m)->count = new_count;
5137
5138
53
      if (remove_empty_load
5139
53
    && (*m)->p_type == PT_LOAD
5140
53
    && (*m)->count == 0
5141
53
    && !(*m)->includes_phdrs)
5142
0
  *m = (*m)->next;
5143
53
      else
5144
53
  m = &(*m)->next;
5145
53
    }
5146
5147
14
  bed = get_elf_backend_data (abfd);
5148
14
  if (bed->elf_backend_modify_segment_map != NULL)
5149
3
    {
5150
3
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5151
0
  return false;
5152
3
    }
5153
5154
14
  return true;
5155
14
}
5156
5157
#define IS_TBSS(s) \
5158
6
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5159
5160
/* Set up a mapping from BFD sections to program segments.  Update
5161
   NEED_LAYOUT if the section layout is changed.  */
5162
5163
bool
5164
_bfd_elf_map_sections_to_segments (bfd *abfd,
5165
           struct bfd_link_info *info,
5166
           bool *need_layout)
5167
14
{
5168
14
  unsigned int count;
5169
14
  struct elf_segment_map *m;
5170
14
  asection **sections = NULL;
5171
14
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5172
14
  bool no_user_phdrs;
5173
5174
14
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5175
5176
14
  if (info != NULL)
5177
0
    {
5178
0
      info->user_phdrs = !no_user_phdrs;
5179
5180
      /* Size the relative relocations if DT_RELR is enabled.  */
5181
0
      if (info->enable_dt_relr
5182
0
    && need_layout != NULL
5183
0
    && bed->size_relative_relocs
5184
0
    && !bed->size_relative_relocs (info, need_layout))
5185
0
  info->callbacks->einfo
5186
0
    (_("%F%P: failed to size relative relocations\n"));
5187
0
    }
5188
5189
14
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5190
0
    {
5191
0
      asection *s;
5192
0
      unsigned int i;
5193
0
      struct elf_segment_map *mfirst;
5194
0
      struct elf_segment_map **pm;
5195
0
      asection *last_hdr;
5196
0
      bfd_vma last_size;
5197
0
      unsigned int hdr_index;
5198
0
      bfd_vma maxpagesize;
5199
0
      asection **hdrpp;
5200
0
      bool phdr_in_segment;
5201
0
      bool writable;
5202
0
      bool executable;
5203
0
      unsigned int tls_count = 0;
5204
0
      asection *first_tls = NULL;
5205
0
      asection *first_mbind = NULL;
5206
0
      asection *dynsec, *eh_frame_hdr;
5207
0
      asection *sframe;
5208
0
      size_t amt;
5209
0
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5210
0
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5211
0
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5212
5213
      /* Select the allocated sections, and sort them.  */
5214
5215
0
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5216
0
      sections = (asection **) bfd_malloc (amt);
5217
0
      if (sections == NULL)
5218
0
  goto error_return;
5219
5220
      /* Calculate top address, avoiding undefined behaviour of shift
5221
   left operator when shift count is equal to size of type
5222
   being shifted.  */
5223
0
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5224
0
      addr_mask = (addr_mask << 1) + 1;
5225
5226
0
      i = 0;
5227
0
      for (s = abfd->sections; s != NULL; s = s->next)
5228
0
  {
5229
0
    if ((s->flags & SEC_ALLOC) != 0)
5230
0
      {
5231
        /* target_index is unused until bfd_elf_final_link
5232
     starts output of section symbols.  Use it to make
5233
     qsort stable.  */
5234
0
        s->target_index = i;
5235
0
        sections[i] = s;
5236
0
        ++i;
5237
        /* A wrapping section potentially clashes with header.  */
5238
0
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5239
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5240
0
      }
5241
0
  }
5242
0
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5243
0
      count = i;
5244
5245
0
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5246
5247
0
      phdr_size = elf_program_header_size (abfd);
5248
0
      if (phdr_size == (bfd_size_type) -1)
5249
0
  phdr_size = get_program_header_size (abfd, info);
5250
0
      phdr_size += bed->s->sizeof_ehdr;
5251
      /* phdr_size is compared to LMA values which are in bytes.  */
5252
0
      phdr_size /= opb;
5253
0
      if (info != NULL)
5254
0
  maxpagesize = info->maxpagesize;
5255
0
      else
5256
0
  maxpagesize = bed->maxpagesize;
5257
0
      if (maxpagesize == 0)
5258
0
  maxpagesize = 1;
5259
0
      phdr_in_segment = info != NULL && info->load_phdrs;
5260
0
      if (count != 0
5261
0
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5262
0
        >= (phdr_size & (maxpagesize - 1))))
5263
  /* For compatibility with old scripts that may not be using
5264
     SIZEOF_HEADERS, add headers when it looks like space has
5265
     been left for them.  */
5266
0
  phdr_in_segment = true;
5267
5268
      /* Build the mapping.  */
5269
0
      mfirst = NULL;
5270
0
      pm = &mfirst;
5271
5272
      /* If we have a .interp section, then create a PT_PHDR segment for
5273
   the program headers and a PT_INTERP segment for the .interp
5274
   section.  */
5275
0
      s = bfd_get_section_by_name (abfd, ".interp");
5276
0
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5277
0
  {
5278
0
    amt = sizeof (struct elf_segment_map);
5279
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5280
0
    if (m == NULL)
5281
0
      goto error_return;
5282
0
    m->next = NULL;
5283
0
    m->p_type = PT_PHDR;
5284
0
    m->p_flags = PF_R;
5285
0
    m->p_flags_valid = 1;
5286
0
    m->includes_phdrs = 1;
5287
0
    phdr_in_segment = true;
5288
0
    *pm = m;
5289
0
    pm = &m->next;
5290
5291
0
    amt = sizeof (struct elf_segment_map);
5292
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5293
0
    if (m == NULL)
5294
0
      goto error_return;
5295
0
    m->next = NULL;
5296
0
    m->p_type = PT_INTERP;
5297
0
    m->count = 1;
5298
0
    m->sections[0] = s;
5299
5300
0
    *pm = m;
5301
0
    pm = &m->next;
5302
0
  }
5303
5304
      /* Look through the sections.  We put sections in the same program
5305
   segment when the start of the second section can be placed within
5306
   a few bytes of the end of the first section.  */
5307
0
      last_hdr = NULL;
5308
0
      last_size = 0;
5309
0
      hdr_index = 0;
5310
0
      writable = false;
5311
0
      executable = false;
5312
0
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5313
0
      if (dynsec != NULL
5314
0
    && (dynsec->flags & SEC_LOAD) == 0)
5315
0
  dynsec = NULL;
5316
5317
0
      if ((abfd->flags & D_PAGED) == 0)
5318
0
  phdr_in_segment = false;
5319
5320
      /* Deal with -Ttext or something similar such that the first section
5321
   is not adjacent to the program headers.  This is an
5322
   approximation, since at this point we don't know exactly how many
5323
   program headers we will need.  */
5324
0
      if (phdr_in_segment && count > 0)
5325
0
  {
5326
0
    bfd_vma phdr_lma;  /* Bytes.  */
5327
0
    bool separate_phdr = false;
5328
5329
0
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5330
0
    if (info != NULL
5331
0
        && info->separate_code
5332
0
        && (sections[0]->flags & SEC_CODE) != 0)
5333
0
      {
5334
        /* If data sections should be separate from code and
5335
     thus not executable, and the first section is
5336
     executable then put the file and program headers in
5337
     their own PT_LOAD.  */
5338
0
        separate_phdr = true;
5339
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5340
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5341
0
    {
5342
      /* The file and program headers are currently on the
5343
         same page as the first section.  Put them on the
5344
         previous page if we can.  */
5345
0
      if (phdr_lma >= maxpagesize)
5346
0
        phdr_lma -= maxpagesize;
5347
0
      else
5348
0
        separate_phdr = false;
5349
0
    }
5350
0
      }
5351
0
    if ((sections[0]->lma & addr_mask) < phdr_lma
5352
0
        || (sections[0]->lma & addr_mask) < phdr_size)
5353
      /* If file and program headers would be placed at the end
5354
         of memory then it's probably better to omit them.  */
5355
0
      phdr_in_segment = false;
5356
0
    else if (phdr_lma < wrap_to)
5357
      /* If a section wraps around to where we'll be placing
5358
         file and program headers, then the headers will be
5359
         overwritten.  */
5360
0
      phdr_in_segment = false;
5361
0
    else if (separate_phdr)
5362
0
      {
5363
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5364
0
        if (m == NULL)
5365
0
    goto error_return;
5366
0
        m->p_paddr = phdr_lma * opb;
5367
0
        m->p_vaddr_offset
5368
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5369
0
        m->p_paddr_valid = 1;
5370
0
        *pm = m;
5371
0
        pm = &m->next;
5372
0
        phdr_in_segment = false;
5373
0
      }
5374
0
  }
5375
5376
0
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5377
0
  {
5378
0
    asection *hdr;
5379
0
    bool new_segment;
5380
5381
0
    hdr = *hdrpp;
5382
5383
    /* See if this section and the last one will fit in the same
5384
       segment.  */
5385
5386
0
    if (last_hdr == NULL)
5387
0
      {
5388
        /* If we don't have a segment yet, then we don't need a new
5389
     one (we build the last one after this loop).  */
5390
0
        new_segment = false;
5391
0
      }
5392
0
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5393
0
      {
5394
        /* If this section has a different relation between the
5395
     virtual address and the load address, then we need a new
5396
     segment.  */
5397
0
        new_segment = true;
5398
0
      }
5399
0
    else if (hdr->lma < last_hdr->lma + last_size
5400
0
       || last_hdr->lma + last_size < last_hdr->lma)
5401
0
      {
5402
        /* If this section has a load address that makes it overlap
5403
     the previous section, then we need a new segment.  */
5404
0
        new_segment = true;
5405
0
      }
5406
0
    else if ((abfd->flags & D_PAGED) != 0
5407
0
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5408
0
           == (hdr->lma & -maxpagesize)))
5409
0
      {
5410
        /* If we are demand paged then we can't map two disk
5411
     pages onto the same memory page.  */
5412
0
        new_segment = false;
5413
0
      }
5414
    /* In the next test we have to be careful when last_hdr->lma is close
5415
       to the end of the address space.  If the aligned address wraps
5416
       around to the start of the address space, then there are no more
5417
       pages left in memory and it is OK to assume that the current
5418
       section can be included in the current segment.  */
5419
0
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5420
0
        + maxpagesize > last_hdr->lma)
5421
0
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5422
0
           + maxpagesize <= hdr->lma))
5423
0
      {
5424
        /* If putting this section in this segment would force us to
5425
     skip a page in the segment, then we need a new segment.  */
5426
0
        new_segment = true;
5427
0
      }
5428
0
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5429
0
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5430
0
      {
5431
        /* We don't want to put a loaded section after a
5432
     nonloaded (ie. bss style) section in the same segment
5433
     as that will force the non-loaded section to be loaded.
5434
     Consider .tbss sections as loaded for this purpose.  */
5435
0
        new_segment = true;
5436
0
      }
5437
0
    else if ((abfd->flags & D_PAGED) == 0)
5438
0
      {
5439
        /* If the file is not demand paged, which means that we
5440
     don't require the sections to be correctly aligned in the
5441
     file, then there is no other reason for a new segment.  */
5442
0
        new_segment = false;
5443
0
      }
5444
0
    else if (info != NULL
5445
0
       && info->separate_code
5446
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5447
0
      {
5448
0
        new_segment = true;
5449
0
      }
5450
0
    else if (! writable
5451
0
       && (hdr->flags & SEC_READONLY) == 0)
5452
0
      {
5453
        /* We don't want to put a writable section in a read only
5454
     segment.  */
5455
0
        new_segment = true;
5456
0
      }
5457
0
    else
5458
0
      {
5459
        /* Otherwise, we can use the same segment.  */
5460
0
        new_segment = false;
5461
0
      }
5462
5463
    /* Allow interested parties a chance to override our decision.  */
5464
0
    if (last_hdr != NULL
5465
0
        && info != NULL
5466
0
        && info->callbacks->override_segment_assignment != NULL)
5467
0
      new_segment
5468
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5469
0
                    last_hdr,
5470
0
                    new_segment);
5471
5472
0
    if (! new_segment)
5473
0
      {
5474
0
        if ((hdr->flags & SEC_READONLY) == 0)
5475
0
    writable = true;
5476
0
        if ((hdr->flags & SEC_CODE) != 0)
5477
0
    executable = true;
5478
0
        last_hdr = hdr;
5479
        /* .tbss sections effectively have zero size.  */
5480
0
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5481
0
        continue;
5482
0
      }
5483
5484
    /* We need a new program segment.  We must create a new program
5485
       header holding all the sections from hdr_index until hdr.  */
5486
5487
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5488
0
    if (m == NULL)
5489
0
      goto error_return;
5490
5491
0
    *pm = m;
5492
0
    pm = &m->next;
5493
5494
0
    if ((hdr->flags & SEC_READONLY) == 0)
5495
0
      writable = true;
5496
0
    else
5497
0
      writable = false;
5498
5499
0
    if ((hdr->flags & SEC_CODE) == 0)
5500
0
      executable = false;
5501
0
    else
5502
0
      executable = true;
5503
5504
0
    last_hdr = hdr;
5505
    /* .tbss sections effectively have zero size.  */
5506
0
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5507
0
    hdr_index = i;
5508
0
    phdr_in_segment = false;
5509
0
  }
5510
5511
      /* Create a final PT_LOAD program segment, but not if it's just
5512
   for .tbss.  */
5513
0
      if (last_hdr != NULL
5514
0
    && (i - hdr_index != 1
5515
0
        || !IS_TBSS (last_hdr)))
5516
0
  {
5517
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5518
0
    if (m == NULL)
5519
0
      goto error_return;
5520
5521
0
    *pm = m;
5522
0
    pm = &m->next;
5523
0
  }
5524
5525
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5526
0
      if (dynsec != NULL)
5527
0
  {
5528
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5529
0
    if (m == NULL)
5530
0
      goto error_return;
5531
0
    *pm = m;
5532
0
    pm = &m->next;
5533
0
  }
5534
5535
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5536
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5537
   because if we link together nonloadable .note sections and
5538
   loadable .note sections, we will generate two .note sections
5539
   in the output file.  */
5540
0
      for (s = abfd->sections; s != NULL; s = s->next)
5541
0
  {
5542
0
    if ((s->flags & SEC_LOAD) != 0
5543
0
        && elf_section_type (s) == SHT_NOTE)
5544
0
      {
5545
0
        asection *s2;
5546
0
        unsigned int alignment_power = s->alignment_power;
5547
5548
0
        count = 1;
5549
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5550
0
    {
5551
0
      if (s2->next->alignment_power == alignment_power
5552
0
          && (s2->next->flags & SEC_LOAD) != 0
5553
0
          && elf_section_type (s2->next) == SHT_NOTE
5554
0
          && align_power (s2->lma + s2->size / opb,
5555
0
              alignment_power)
5556
0
          == s2->next->lma)
5557
0
        count++;
5558
0
      else
5559
0
        break;
5560
0
    }
5561
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5562
0
        amt += count * sizeof (asection *);
5563
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5564
0
        if (m == NULL)
5565
0
    goto error_return;
5566
0
        m->next = NULL;
5567
0
        m->p_type = PT_NOTE;
5568
0
        m->count = count;
5569
0
        while (count > 1)
5570
0
    {
5571
0
      m->sections[m->count - count--] = s;
5572
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5573
0
      s = s->next;
5574
0
    }
5575
0
        m->sections[m->count - 1] = s;
5576
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5577
0
        *pm = m;
5578
0
        pm = &m->next;
5579
0
      }
5580
0
    if (s->flags & SEC_THREAD_LOCAL)
5581
0
      {
5582
0
        if (! tls_count)
5583
0
    first_tls = s;
5584
0
        tls_count++;
5585
0
      }
5586
0
    if (first_mbind == NULL
5587
0
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5588
0
      first_mbind = s;
5589
0
  }
5590
5591
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5592
0
      if (tls_count > 0)
5593
0
  {
5594
0
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5595
0
    amt += tls_count * sizeof (asection *);
5596
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5597
0
    if (m == NULL)
5598
0
      goto error_return;
5599
0
    m->next = NULL;
5600
0
    m->p_type = PT_TLS;
5601
0
    m->count = tls_count;
5602
    /* Mandated PF_R.  */
5603
0
    m->p_flags = PF_R;
5604
0
    m->p_flags_valid = 1;
5605
0
    s = first_tls;
5606
0
    for (i = 0; i < tls_count; ++i)
5607
0
      {
5608
0
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5609
0
    {
5610
0
      _bfd_error_handler
5611
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5612
0
      s = first_tls;
5613
0
      i = 0;
5614
0
      while (i < tls_count)
5615
0
        {
5616
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5617
0
      {
5618
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5619
0
        i++;
5620
0
      }
5621
0
          else
5622
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5623
0
          s = s->next;
5624
0
        }
5625
0
      bfd_set_error (bfd_error_bad_value);
5626
0
      goto error_return;
5627
0
    }
5628
0
        m->sections[i] = s;
5629
0
        s = s->next;
5630
0
      }
5631
5632
0
    *pm = m;
5633
0
    pm = &m->next;
5634
0
  }
5635
5636
0
      if (first_mbind
5637
0
    && (abfd->flags & D_PAGED) != 0
5638
0
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5639
0
  for (s = first_mbind; s != NULL; s = s->next)
5640
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5641
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5642
0
      {
5643
        /* Mandated PF_R.  */
5644
0
        unsigned long p_flags = PF_R;
5645
0
        if ((s->flags & SEC_READONLY) == 0)
5646
0
    p_flags |= PF_W;
5647
0
        if ((s->flags & SEC_CODE) != 0)
5648
0
    p_flags |= PF_X;
5649
5650
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5651
0
        m = bfd_zalloc (abfd, amt);
5652
0
        if (m == NULL)
5653
0
    goto error_return;
5654
0
        m->next = NULL;
5655
0
        m->p_type = (PT_GNU_MBIND_LO
5656
0
         + elf_section_data (s)->this_hdr.sh_info);
5657
0
        m->count = 1;
5658
0
        m->p_flags_valid = 1;
5659
0
        m->sections[0] = s;
5660
0
        m->p_flags = p_flags;
5661
5662
0
        *pm = m;
5663
0
        pm = &m->next;
5664
0
      }
5665
5666
0
      s = bfd_get_section_by_name (abfd,
5667
0
           NOTE_GNU_PROPERTY_SECTION_NAME);
5668
0
      if (s != NULL && s->size != 0)
5669
0
  {
5670
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5671
0
    m = bfd_zalloc (abfd, amt);
5672
0
    if (m == NULL)
5673
0
      goto error_return;
5674
0
    m->next = NULL;
5675
0
    m->p_type = PT_GNU_PROPERTY;
5676
0
    m->count = 1;
5677
0
    m->p_flags_valid = 1;
5678
0
    m->sections[0] = s;
5679
0
    m->p_flags = PF_R;
5680
0
    *pm = m;
5681
0
    pm = &m->next;
5682
0
  }
5683
5684
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5685
   segment.  */
5686
0
      eh_frame_hdr = elf_eh_frame_hdr (info);
5687
0
      if (eh_frame_hdr != NULL
5688
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5689
0
  {
5690
0
    amt = sizeof (struct elf_segment_map);
5691
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5692
0
    if (m == NULL)
5693
0
      goto error_return;
5694
0
    m->next = NULL;
5695
0
    m->p_type = PT_GNU_EH_FRAME;
5696
0
    m->count = 1;
5697
0
    m->sections[0] = eh_frame_hdr->output_section;
5698
5699
0
    *pm = m;
5700
0
    pm = &m->next;
5701
0
  }
5702
5703
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5704
   segment.  */
5705
0
      sframe = elf_sframe (abfd);
5706
0
      if (sframe != NULL
5707
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5708
0
    && sframe->size != 0)
5709
0
  {
5710
0
    amt = sizeof (struct elf_segment_map);
5711
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5712
0
    if (m == NULL)
5713
0
      goto error_return;
5714
0
    m->next = NULL;
5715
0
    m->p_type = PT_GNU_SFRAME;
5716
0
    m->count = 1;
5717
0
    m->sections[0] = sframe->output_section;
5718
5719
0
    *pm = m;
5720
0
    pm = &m->next;
5721
0
  }
5722
5723
0
      if (elf_stack_flags (abfd))
5724
0
  {
5725
0
    amt = sizeof (struct elf_segment_map);
5726
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5727
0
    if (m == NULL)
5728
0
      goto error_return;
5729
0
    m->next = NULL;
5730
0
    m->p_type = PT_GNU_STACK;
5731
0
    m->p_flags = elf_stack_flags (abfd);
5732
0
    m->p_align = bed->stack_align;
5733
0
    m->p_flags_valid = 1;
5734
0
    m->p_align_valid = m->p_align != 0;
5735
0
    if (info->stacksize > 0)
5736
0
      {
5737
0
        m->p_size = info->stacksize;
5738
0
        m->p_size_valid = 1;
5739
0
      }
5740
5741
0
    *pm = m;
5742
0
    pm = &m->next;
5743
0
  }
5744
5745
0
      if (info != NULL && info->relro)
5746
0
  {
5747
0
    for (m = mfirst; m != NULL; m = m->next)
5748
0
      {
5749
0
        if (m->p_type == PT_LOAD
5750
0
      && m->count != 0
5751
0
      && m->sections[0]->vma >= info->relro_start
5752
0
      && m->sections[0]->vma < info->relro_end)
5753
0
    {
5754
0
      i = m->count;
5755
0
      while (--i != (unsigned) -1)
5756
0
        {
5757
0
          if (m->sections[i]->size > 0
5758
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5759
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5760
0
      break;
5761
0
        }
5762
5763
0
      if (i != (unsigned) -1)
5764
0
        break;
5765
0
    }
5766
0
      }
5767
5768
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5769
0
    if (m != NULL)
5770
0
      {
5771
0
        amt = sizeof (struct elf_segment_map);
5772
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5773
0
        if (m == NULL)
5774
0
    goto error_return;
5775
0
        m->next = NULL;
5776
0
        m->p_type = PT_GNU_RELRO;
5777
0
        *pm = m;
5778
0
        pm = &m->next;
5779
0
      }
5780
0
  }
5781
5782
0
      free (sections);
5783
0
      elf_seg_map (abfd) = mfirst;
5784
0
    }
5785
5786
14
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5787
0
    return false;
5788
5789
67
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5790
53
    ++count;
5791
14
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5792
5793
14
  return true;
5794
5795
0
 error_return:
5796
0
  free (sections);
5797
0
  return false;
5798
14
}
5799
5800
/* Sort sections by address.  */
5801
5802
static int
5803
elf_sort_sections (const void *arg1, const void *arg2)
5804
330
{
5805
330
  const asection *sec1 = *(const asection **) arg1;
5806
330
  const asection *sec2 = *(const asection **) arg2;
5807
330
  bfd_size_type size1, size2;
5808
5809
  /* Sort by LMA first, since this is the address used to
5810
     place the section into a segment.  */
5811
330
  if (sec1->lma < sec2->lma)
5812
330
    return -1;
5813
0
  else if (sec1->lma > sec2->lma)
5814
0
    return 1;
5815
5816
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5817
     the same, and this will do nothing.  */
5818
0
  if (sec1->vma < sec2->vma)
5819
0
    return -1;
5820
0
  else if (sec1->vma > sec2->vma)
5821
0
    return 1;
5822
5823
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5824
5825
0
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5826
0
      && (x)->size != 0)
5827
5828
0
  if (TOEND (sec1))
5829
0
    {
5830
0
      if (!TOEND (sec2))
5831
0
  return 1;
5832
0
    }
5833
0
  else if (TOEND (sec2))
5834
0
    return -1;
5835
5836
0
#undef TOEND
5837
5838
  /* Sort by size, to put zero sized sections
5839
     before others at the same address.  */
5840
5841
0
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5842
0
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5843
5844
0
  if (size1 < size2)
5845
0
    return -1;
5846
0
  if (size1 > size2)
5847
0
    return 1;
5848
5849
0
  return sec1->target_index - sec2->target_index;
5850
0
}
5851
5852
/* This qsort comparison functions sorts PT_LOAD segments first and
5853
   by p_paddr, for assign_file_positions_for_load_sections.  */
5854
5855
static int
5856
elf_sort_segments (const void *arg1, const void *arg2)
5857
88
{
5858
88
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5859
88
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5860
5861
88
  if (m1->p_type != m2->p_type)
5862
82
    {
5863
82
      if (m1->p_type == PT_NULL)
5864
0
  return 1;
5865
82
      if (m2->p_type == PT_NULL)
5866
0
  return -1;
5867
82
      return m1->p_type < m2->p_type ? -1 : 1;
5868
82
    }
5869
6
  if (m1->includes_filehdr != m2->includes_filehdr)
5870
6
    return m1->includes_filehdr ? -1 : 1;
5871
0
  if (m1->no_sort_lma != m2->no_sort_lma)
5872
0
    return m1->no_sort_lma ? -1 : 1;
5873
0
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5874
0
    {
5875
0
      bfd_vma lma1, lma2;  /* Octets.  */
5876
0
      lma1 = 0;
5877
0
      if (m1->p_paddr_valid)
5878
0
  lma1 = m1->p_paddr;
5879
0
      else if (m1->count != 0)
5880
0
  {
5881
0
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5882
0
              m1->sections[0]);
5883
0
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5884
0
  }
5885
0
      lma2 = 0;
5886
0
      if (m2->p_paddr_valid)
5887
0
  lma2 = m2->p_paddr;
5888
0
      else if (m2->count != 0)
5889
0
  {
5890
0
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5891
0
              m2->sections[0]);
5892
0
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5893
0
  }
5894
0
      if (lma1 != lma2)
5895
0
  return lma1 < lma2 ? -1 : 1;
5896
0
    }
5897
0
  if (m1->idx != m2->idx)
5898
0
    return m1->idx < m2->idx ? -1 : 1;
5899
0
  return 0;
5900
0
}
5901
5902
/* Ian Lance Taylor writes:
5903
5904
   We shouldn't be using % with a negative signed number.  That's just
5905
   not good.  We have to make sure either that the number is not
5906
   negative, or that the number has an unsigned type.  When the types
5907
   are all the same size they wind up as unsigned.  When file_ptr is a
5908
   larger signed type, the arithmetic winds up as signed long long,
5909
   which is wrong.
5910
5911
   What we're trying to say here is something like ``increase OFF by
5912
   the least amount that will cause it to be equal to the VMA modulo
5913
   the page size.''  */
5914
/* In other words, something like:
5915
5916
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5917
   off_offset = off % bed->maxpagesize;
5918
   if (vma_offset < off_offset)
5919
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5920
   else
5921
     adjustment = vma_offset - off_offset;
5922
5923
   which can be collapsed into the expression below.  */
5924
5925
static file_ptr
5926
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5927
28
{
5928
  /* PR binutils/16199: Handle an alignment of zero.  */
5929
28
  if (maxpagesize == 0)
5930
0
    maxpagesize = 1;
5931
28
  return ((vma - off) % maxpagesize);
5932
28
}
5933
5934
static void
5935
print_segment_map (const struct elf_segment_map *m)
5936
0
{
5937
0
  unsigned int j;
5938
0
  const char *pt = get_segment_type (m->p_type);
5939
0
  char buf[32];
5940
5941
0
  if (pt == NULL)
5942
0
    {
5943
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5944
0
  sprintf (buf, "LOPROC+%7.7x",
5945
0
     (unsigned int) (m->p_type - PT_LOPROC));
5946
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5947
0
  sprintf (buf, "LOOS+%7.7x",
5948
0
     (unsigned int) (m->p_type - PT_LOOS));
5949
0
      else
5950
0
  snprintf (buf, sizeof (buf), "%8.8x",
5951
0
      (unsigned int) m->p_type);
5952
0
      pt = buf;
5953
0
    }
5954
0
  fflush (stdout);
5955
0
  fprintf (stderr, "%s:", pt);
5956
0
  for (j = 0; j < m->count; j++)
5957
0
    fprintf (stderr, " %s", m->sections [j]->name);
5958
0
  putc ('\n',stderr);
5959
0
  fflush (stderr);
5960
0
}
5961
5962
/* Assign file positions to the sections based on the mapping from
5963
   sections to segments.  This function also sets up some fields in
5964
   the file header.  */
5965
5966
static bool
5967
assign_file_positions_for_load_sections (bfd *abfd,
5968
           struct bfd_link_info *link_info)
5969
14
{
5970
14
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5971
14
  struct elf_segment_map *m;
5972
14
  struct elf_segment_map *phdr_load_seg;
5973
14
  Elf_Internal_Phdr *phdrs;
5974
14
  Elf_Internal_Phdr *p;
5975
14
  file_ptr off;  /* Octets.  */
5976
14
  bfd_size_type maxpagesize;
5977
14
  unsigned int alloc, actual;
5978
14
  unsigned int i, j;
5979
14
  struct elf_segment_map **sorted_seg_map;
5980
14
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5981
5982
14
  if (link_info == NULL
5983
14
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5984
0
    return false;
5985
5986
14
  alloc = 0;
5987
67
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5988
53
    m->idx = alloc++;
5989
5990
14
  if (alloc)
5991
7
    {
5992
7
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5993
7
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5994
7
    }
5995
7
  else
5996
7
    {
5997
      /* PR binutils/12467.  */
5998
7
      elf_elfheader (abfd)->e_phoff = 0;
5999
7
      elf_elfheader (abfd)->e_phentsize = 0;
6000
7
    }
6001
6002
14
  elf_elfheader (abfd)->e_phnum = alloc;
6003
6004
14
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6005
0
    {
6006
0
      actual = alloc;
6007
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6008
0
    }
6009
14
  else
6010
14
    {
6011
14
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6012
14
      BFD_ASSERT (elf_program_header_size (abfd)
6013
14
      == actual * bed->s->sizeof_phdr);
6014
14
      BFD_ASSERT (actual >= alloc);
6015
14
    }
6016
6017
14
  if (alloc == 0)
6018
7
    {
6019
7
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6020
7
      return true;
6021
7
    }
6022
6023
  /* We're writing the size in elf_program_header_size (abfd),
6024
     see assign_file_positions_except_relocs, so make sure we have
6025
     that amount allocated, with trailing space cleared.
6026
     The variable alloc contains the computed need, while
6027
     elf_program_header_size (abfd) contains the size used for the
6028
     layout.
6029
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6030
     where the layout is forced to according to a larger size in the
6031
     last iterations for the testcase ld-elf/header.  */
6032
7
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6033
7
           + alloc * sizeof (*sorted_seg_map)));
6034
7
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6035
7
  elf_tdata (abfd)->phdr = phdrs;
6036
7
  if (phdrs == NULL)
6037
0
    return false;
6038
6039
60
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6040
53
    {
6041
53
      sorted_seg_map[j] = m;
6042
      /* If elf_segment_map is not from map_sections_to_segments, the
6043
   sections may not be correctly ordered.  NOTE: sorting should
6044
   not be done to the PT_NOTE section of a corefile, which may
6045
   contain several pseudo-sections artificially created by bfd.
6046
   Sorting these pseudo-sections breaks things badly.  */
6047
53
      if (m->count > 1
6048
53
    && !(elf_elfheader (abfd)->e_type == ET_CORE
6049
21
         && m->p_type == PT_NOTE))
6050
21
  {
6051
219
    for (i = 0; i < m->count; i++)
6052
198
      m->sections[i]->target_index = i;
6053
21
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
6054
21
     elf_sort_sections);
6055
21
  }
6056
53
    }
6057
7
  if (alloc > 1)
6058
7
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6059
7
     elf_sort_segments);
6060
6061
7
  maxpagesize = 1;
6062
7
  if ((abfd->flags & D_PAGED) != 0)
6063
7
    {
6064
7
      if (link_info != NULL)
6065
0
  maxpagesize = link_info->maxpagesize;
6066
7
      else
6067
7
  maxpagesize = bed->maxpagesize;
6068
7
    }
6069
6070
  /* Sections must map to file offsets past the ELF file header.  */
6071
7
  off = bed->s->sizeof_ehdr;
6072
  /* And if one of the PT_LOAD headers doesn't include the program
6073
     headers then we'll be mapping program headers in the usual
6074
     position after the ELF file header.  */
6075
7
  phdr_load_seg = NULL;
6076
8
  for (j = 0; j < alloc; j++)
6077
8
    {
6078
8
      m = sorted_seg_map[j];
6079
8
      if (m->p_type != PT_LOAD)
6080
1
  break;
6081
7
      if (m->includes_phdrs)
6082
6
  {
6083
6
    phdr_load_seg = m;
6084
6
    break;
6085
6
  }
6086
7
    }
6087
7
  if (phdr_load_seg == NULL)
6088
1
    off += actual * bed->s->sizeof_phdr;
6089
6090
60
  for (j = 0; j < alloc; j++)
6091
53
    {
6092
53
      asection **secpp;
6093
53
      bfd_vma off_adjust;  /* Octets.  */
6094
53
      bool no_contents;
6095
53
      bfd_size_type p_align;
6096
53
      bool p_align_p;
6097
6098
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6099
   number of sections with contents contributing to both p_filesz
6100
   and p_memsz, followed by a number of sections with no contents
6101
   that just contribute to p_memsz.  In this loop, OFF tracks next
6102
   available file offset for PT_LOAD and PT_NOTE segments.  */
6103
53
      m = sorted_seg_map[j];
6104
53
      p = phdrs + m->idx;
6105
53
      p->p_type = m->p_type;
6106
53
      p->p_flags = m->p_flags;
6107
53
      p_align = bed->p_align;
6108
53
      p_align_p = false;
6109
6110
53
      if (m->count == 0)
6111
12
  p->p_vaddr = m->p_vaddr_offset * opb;
6112
41
      else
6113
41
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6114
6115
53
      if (m->p_paddr_valid)
6116
48
  p->p_paddr = m->p_paddr;
6117
5
      else if (m->count == 0)
6118
1
  p->p_paddr = 0;
6119
4
      else
6120
4
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6121
6122
53
      if (p->p_type == PT_LOAD
6123
53
    && (abfd->flags & D_PAGED) != 0)
6124
13
  {
6125
    /* p_align in demand paged PT_LOAD segments effectively stores
6126
       the maximum page size.  When copying an executable with
6127
       objcopy, we set m->p_align from the input file.  Use this
6128
       value for maxpagesize rather than bed->maxpagesize, which
6129
       may be different.  Note that we use maxpagesize for PT_TLS
6130
       segment alignment later in this function, so we are relying
6131
       on at least one PT_LOAD segment appearing before a PT_TLS
6132
       segment.  */
6133
13
    if (m->p_align_valid)
6134
12
      maxpagesize = m->p_align;
6135
1
    else if (p_align != 0
6136
1
       && (link_info == NULL
6137
1
           || !link_info->maxpagesize_is_set))
6138
      /* Set p_align to the default p_align value while laying
6139
         out segments aligning to the maximum page size or the
6140
         largest section alignment.  The run-time loader can
6141
         align segments to the default p_align value or the
6142
         maximum page size, depending on system page size.  */
6143
1
      p_align_p = true;
6144
6145
13
    p->p_align = maxpagesize;
6146
13
  }
6147
40
      else if (m->p_align_valid)
6148
35
  p->p_align = m->p_align;
6149
5
      else if (m->count == 0)
6150
1
  p->p_align = 1 << bed->s->log_file_align;
6151
6152
53
      if (m == phdr_load_seg)
6153
6
  {
6154
6
    if (!m->includes_filehdr)
6155
0
      p->p_offset = off;
6156
6
    off += actual * bed->s->sizeof_phdr;
6157
6
  }
6158
6159
53
      no_contents = false;
6160
53
      off_adjust = 0;
6161
53
      if (p->p_type == PT_LOAD
6162
53
    && m->count > 0)
6163
13
  {
6164
13
    bfd_size_type align;  /* Bytes.  */
6165
13
    unsigned int align_power = 0;
6166
6167
13
    if (m->p_align_valid)
6168
12
      align = p->p_align;
6169
1
    else
6170
1
      {
6171
8
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6172
7
    {
6173
7
      unsigned int secalign;
6174
6175
7
      secalign = bfd_section_alignment (*secpp);
6176
7
      if (secalign > align_power)
6177
2
        align_power = secalign;
6178
7
    }
6179
1
        align = (bfd_size_type) 1 << align_power;
6180
1
        if (align < maxpagesize)
6181
1
    {
6182
      /* If a section requires alignment higher than the
6183
         default p_align value, don't set p_align to the
6184
         default p_align value.  */
6185
1
      if (align > p_align)
6186
0
        p_align_p = false;
6187
1
      align = maxpagesize;
6188
1
    }
6189
0
        else
6190
0
    {
6191
      /* If a section requires alignment higher than the
6192
         maximum page size, set p_align to the section
6193
         alignment.  */
6194
0
      p_align_p = true;
6195
0
      p_align = align;
6196
0
    }
6197
1
      }
6198
6199
172
    for (i = 0; i < m->count; i++)
6200
159
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6201
        /* If we aren't making room for this section, then
6202
     it must be SHT_NOBITS regardless of what we've
6203
     set via struct bfd_elf_special_section.  */
6204
9
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6205
6206
    /* Find out whether this segment contains any loadable
6207
       sections.  */
6208
13
    no_contents = true;
6209
13
    for (i = 0; i < m->count; i++)
6210
13
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6211
13
        {
6212
13
    no_contents = false;
6213
13
    break;
6214
13
        }
6215
6216
13
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6217
6218
    /* Broken hardware and/or kernel require that files do not
6219
       map the same page with different permissions on some hppa
6220
       processors.  */
6221
13
    if (j != 0
6222
13
        && (abfd->flags & D_PAGED) != 0
6223
13
        && bed->no_page_alias
6224
13
        && (off & (maxpagesize - 1)) != 0
6225
13
        && ((off & -maxpagesize)
6226
0
      == ((off + off_adjust) & -maxpagesize)))
6227
0
      off_adjust += maxpagesize;
6228
13
    off += off_adjust;
6229
13
    if (no_contents)
6230
0
      {
6231
        /* We shouldn't need to align the segment on disk since
6232
     the segment doesn't need file space, but the gABI
6233
     arguably requires the alignment and glibc ld.so
6234
     checks it.  So to comply with the alignment
6235
     requirement but not waste file space, we adjust
6236
     p_offset for just this segment.  (OFF_ADJUST is
6237
     subtracted from OFF later.)  This may put p_offset
6238
     past the end of file, but that shouldn't matter.  */
6239
0
      }
6240
13
    else
6241
13
      off_adjust = 0;
6242
13
  }
6243
      /* Make sure the .dynamic section is the first section in the
6244
   PT_DYNAMIC segment.  */
6245
40
      else if (p->p_type == PT_DYNAMIC
6246
40
         && m->count > 1
6247
40
         && strcmp (m->sections[0]->name, ".dynamic") != 0)
6248
0
  {
6249
0
    _bfd_error_handler
6250
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6251
0
         " is not the .dynamic section"),
6252
0
       abfd);
6253
0
    bfd_set_error (bfd_error_bad_value);
6254
0
    return false;
6255
0
  }
6256
      /* Set the note section type to SHT_NOTE.  */
6257
40
      else if (p->p_type == PT_NOTE)
6258
13
  for (i = 0; i < m->count; i++)
6259
8
    elf_section_type (m->sections[i]) = SHT_NOTE;
6260
6261
53
      if (m->includes_filehdr)
6262
7
  {
6263
7
    if (!m->p_flags_valid)
6264
0
      p->p_flags |= PF_R;
6265
7
    p->p_filesz = bed->s->sizeof_ehdr;
6266
7
    p->p_memsz = bed->s->sizeof_ehdr;
6267
7
    if (p->p_type == PT_LOAD)
6268
6
      {
6269
6
        if (m->count > 0)
6270
6
    {
6271
6
      if (p->p_vaddr < (bfd_vma) off
6272
6
          || (!m->p_paddr_valid
6273
6
        && p->p_paddr < (bfd_vma) off))
6274
0
        {
6275
0
          _bfd_error_handler
6276
0
      (_("%pB: not enough room for program headers,"
6277
0
         " try linking with -N"),
6278
0
       abfd);
6279
0
          bfd_set_error (bfd_error_bad_value);
6280
0
          return false;
6281
0
        }
6282
6
      p->p_vaddr -= off;
6283
6
      if (!m->p_paddr_valid)
6284
1
        p->p_paddr -= off;
6285
6
    }
6286
6
      }
6287
1
    else if (sorted_seg_map[0]->includes_filehdr)
6288
0
      {
6289
0
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6290
0
        p->p_vaddr = filehdr->p_vaddr;
6291
0
        if (!m->p_paddr_valid)
6292
0
    p->p_paddr = filehdr->p_paddr;
6293
0
      }
6294
7
  }
6295
6296
53
      if (m->includes_phdrs)
6297
13
  {
6298
13
    if (!m->p_flags_valid)
6299
0
      p->p_flags |= PF_R;
6300
13
    p->p_filesz += actual * bed->s->sizeof_phdr;
6301
13
    p->p_memsz += actual * bed->s->sizeof_phdr;
6302
13
    if (!m->includes_filehdr)
6303
6
      {
6304
6
        if (p->p_type == PT_LOAD)
6305
0
    {
6306
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6307
0
      if (m->count > 0)
6308
0
        {
6309
0
          p->p_vaddr -= off - p->p_offset;
6310
0
          if (!m->p_paddr_valid)
6311
0
      p->p_paddr -= off - p->p_offset;
6312
0
        }
6313
0
    }
6314
6
        else if (phdr_load_seg != NULL)
6315
6
    {
6316
6
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6317
6
      bfd_vma phdr_off = 0;  /* Octets.  */
6318
6
      if (phdr_load_seg->includes_filehdr)
6319
6
        phdr_off = bed->s->sizeof_ehdr;
6320
6
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6321
6
      if (!m->p_paddr_valid)
6322
1
        p->p_paddr = phdr->p_paddr + phdr_off;
6323
6
      p->p_offset = phdr->p_offset + phdr_off;
6324
6
    }
6325
0
        else
6326
0
    p->p_offset = bed->s->sizeof_ehdr;
6327
6
      }
6328
13
  }
6329
6330
53
      if (p->p_type == PT_LOAD
6331
53
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6332
13
  {
6333
13
    if (!m->includes_filehdr && !m->includes_phdrs)
6334
7
      {
6335
7
        p->p_offset = off;
6336
7
        if (no_contents)
6337
0
    {
6338
      /* Put meaningless p_offset for PT_LOAD segments
6339
         without file contents somewhere within the first
6340
         page, in an attempt to not point past EOF.  */
6341
0
      bfd_size_type align = maxpagesize;
6342
0
      if (align < p->p_align)
6343
0
        align = p->p_align;
6344
0
      if (align < 1)
6345
0
        align = 1;
6346
0
      p->p_offset = off % align;
6347
0
    }
6348
7
      }
6349
6
    else
6350
6
      {
6351
6
        file_ptr adjust;  /* Octets.  */
6352
6353
6
        adjust = off - (p->p_offset + p->p_filesz);
6354
6
        if (!no_contents)
6355
6
    p->p_filesz += adjust;
6356
6
        p->p_memsz += adjust;
6357
6
      }
6358
13
  }
6359
6360
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6361
   maps.  Set filepos for sections in PT_LOAD segments, and in
6362
   core files, for sections in PT_NOTE segments.
6363
   assign_file_positions_for_non_load_sections will set filepos
6364
   for other sections and update p_filesz for other segments.  */
6365
271
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6366
218
  {
6367
218
    asection *sec;
6368
218
    bfd_size_type align;
6369
218
    Elf_Internal_Shdr *this_hdr;
6370
6371
218
    sec = *secpp;
6372
218
    this_hdr = &elf_section_data (sec)->this_hdr;
6373
218
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6374
6375
218
    if ((p->p_type == PT_LOAD
6376
218
         || p->p_type == PT_TLS)
6377
218
        && (this_hdr->sh_type != SHT_NOBITS
6378
159
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6379
9
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6380
9
        || p->p_type == PT_TLS))))
6381
159
      {
6382
159
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6383
159
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6384
159
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6385
159
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6386
6387
159
        if (adjust != 0
6388
159
      && (s_start < p_end
6389
28
          || p_end < p_start))
6390
0
    {
6391
0
      _bfd_error_handler
6392
        /* xgettext:c-format */
6393
0
        (_("%pB: section %pA lma %#" PRIx64
6394
0
           " adjusted to %#" PRIx64),
6395
0
         abfd, sec, (uint64_t) s_start / opb,
6396
0
         (uint64_t) p_end / opb);
6397
0
      adjust = 0;
6398
0
      sec->lma = p_end / opb;
6399
0
    }
6400
159
        p->p_memsz += adjust;
6401
6402
159
        if (p->p_type == PT_LOAD)
6403
159
    {
6404
159
      if (this_hdr->sh_type != SHT_NOBITS)
6405
150
        {
6406
150
          off_adjust = 0;
6407
150
          if (p->p_filesz + adjust < p->p_memsz)
6408
0
      {
6409
        /* We have a PROGBITS section following NOBITS ones.
6410
           Allocate file space for the NOBITS section(s).
6411
           We don't need to write out the zeros, posix
6412
           fseek past the end of data already written
6413
           followed by a write at that location is
6414
           guaranteed to result in zeros being read
6415
           from the gap.  */
6416
0
        adjust = p->p_memsz - p->p_filesz;
6417
0
      }
6418
150
        }
6419
      /* We only adjust sh_offset in SHT_NOBITS sections
6420
         as would seem proper for their address when the
6421
         section is first in the segment.  sh_offset
6422
         doesn't really have any significance for
6423
         SHT_NOBITS anyway, apart from a notional position
6424
         relative to other sections.  Historically we
6425
         didn't bother with adjusting sh_offset and some
6426
         programs depend on it not being adjusted.  See
6427
         pr12921 and pr25662.  */
6428
159
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6429
150
        {
6430
150
          off += adjust;
6431
150
          if (this_hdr->sh_type == SHT_NOBITS)
6432
0
      off_adjust += adjust;
6433
150
        }
6434
159
    }
6435
159
        if (this_hdr->sh_type != SHT_NOBITS)
6436
150
    p->p_filesz += adjust;
6437
159
      }
6438
6439
218
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6440
0
      {
6441
        /* The section at i == 0 is the one that actually contains
6442
     everything.  */
6443
0
        if (i == 0)
6444
0
    {
6445
0
      this_hdr->sh_offset = sec->filepos = off;
6446
0
      off += this_hdr->sh_size;
6447
0
      p->p_filesz = this_hdr->sh_size;
6448
0
      p->p_memsz = 0;
6449
0
      p->p_align = 1;
6450
0
    }
6451
0
        else
6452
0
    {
6453
      /* The rest are fake sections that shouldn't be written.  */
6454
0
      sec->filepos = 0;
6455
0
      sec->size = 0;
6456
0
      sec->flags = 0;
6457
0
      continue;
6458
0
    }
6459
0
      }
6460
218
    else
6461
218
      {
6462
218
        if (p->p_type == PT_LOAD)
6463
159
    {
6464
159
      this_hdr->sh_offset = sec->filepos = off;
6465
159
      if (this_hdr->sh_type != SHT_NOBITS)
6466
150
        off += this_hdr->sh_size;
6467
159
    }
6468
59
        else if (this_hdr->sh_type == SHT_NOBITS
6469
59
           && (this_hdr->sh_flags & SHF_TLS) != 0
6470
59
           && this_hdr->sh_offset == 0)
6471
0
    {
6472
      /* This is a .tbss section that didn't get a PT_LOAD.
6473
         (See _bfd_elf_map_sections_to_segments "Create a
6474
         final PT_LOAD".)  Set sh_offset to the value it
6475
         would have if we had created a zero p_filesz and
6476
         p_memsz PT_LOAD header for the section.  This
6477
         also makes the PT_TLS header have the same
6478
         p_offset value.  */
6479
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6480
0
                off, align);
6481
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
6482
0
    }
6483
6484
218
        if (this_hdr->sh_type != SHT_NOBITS)
6485
209
    {
6486
209
      p->p_filesz += this_hdr->sh_size;
6487
      /* A load section without SHF_ALLOC is something like
6488
         a note section in a PT_NOTE segment.  These take
6489
         file space but are not loaded into memory.  */
6490
209
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6491
209
        p->p_memsz += this_hdr->sh_size;
6492
209
    }
6493
9
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6494
9
    {
6495
9
      if (p->p_type == PT_TLS)
6496
0
        p->p_memsz += this_hdr->sh_size;
6497
6498
      /* .tbss is special.  It doesn't contribute to p_memsz of
6499
         normal segments.  */
6500
9
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6501
9
        p->p_memsz += this_hdr->sh_size;
6502
9
    }
6503
6504
218
        if (align > p->p_align
6505
218
      && !m->p_align_valid
6506
218
      && (p->p_type != PT_LOAD
6507
6
          || (abfd->flags & D_PAGED) == 0))
6508
6
    p->p_align = align;
6509
218
      }
6510
6511
218
    if (!m->p_flags_valid)
6512
0
      {
6513
0
        p->p_flags |= PF_R;
6514
0
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6515
0
    p->p_flags |= PF_X;
6516
0
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6517
0
    p->p_flags |= PF_W;
6518
0
      }
6519
218
  }
6520
6521
53
      off -= off_adjust;
6522
6523
      /* PR ld/20815 - Check that the program header segment, if
6524
   present, will be loaded into memory.  */
6525
53
      if (p->p_type == PT_PHDR
6526
53
    && phdr_load_seg == NULL
6527
53
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6528
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6529
0
  {
6530
    /* The fix for this error is usually to edit the linker script being
6531
       used and set up the program headers manually.  Either that or
6532
       leave room for the headers at the start of the SECTIONS.  */
6533
0
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6534
0
        " by LOAD segment"),
6535
0
            abfd);
6536
0
    if (link_info == NULL)
6537
0
      return false;
6538
    /* Arrange for the linker to exit with an error, deleting
6539
       the output file unless --noinhibit-exec is given.  */
6540
0
    link_info->callbacks->info ("%X");
6541
0
  }
6542
6543
      /* Check that all sections are in a PT_LOAD segment.
6544
   Don't check funky gdb generated core files.  */
6545
53
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6546
13
  {
6547
13
    bool check_vma = true;
6548
6549
159
    for (i = 1; i < m->count; i++)
6550
146
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6551
146
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6552
0
               ->this_hdr), p) != 0
6553
146
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6554
0
               ->this_hdr), p) != 0)
6555
0
        {
6556
    /* Looks like we have overlays packed into the segment.  */
6557
0
    check_vma = false;
6558
0
    break;
6559
0
        }
6560
6561
172
    for (i = 0; i < m->count; i++)
6562
159
      {
6563
159
        Elf_Internal_Shdr *this_hdr;
6564
159
        asection *sec;
6565
6566
159
        sec = m->sections[i];
6567
159
        this_hdr = &(elf_section_data(sec)->this_hdr);
6568
159
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6569
159
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6570
0
    {
6571
0
      _bfd_error_handler
6572
        /* xgettext:c-format */
6573
0
        (_("%pB: section `%pA' can't be allocated in segment %d"),
6574
0
         abfd, sec, j);
6575
0
      print_segment_map (m);
6576
0
    }
6577
159
      }
6578
6579
13
    if (p_align_p)
6580
1
      p->p_align = p_align;
6581
13
  }
6582
53
    }
6583
6584
7
  elf_next_file_pos (abfd) = off;
6585
6586
7
  if (link_info != NULL
6587
7
      && phdr_load_seg != NULL
6588
7
      && phdr_load_seg->includes_filehdr)
6589
0
    {
6590
      /* There is a segment that contains both the file headers and the
6591
   program headers, so provide a symbol __ehdr_start pointing there.
6592
   A program can use this to examine itself robustly.  */
6593
6594
0
      struct elf_link_hash_entry *hash
6595
0
  = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6596
0
        false, false, true);
6597
      /* If the symbol was referenced and not defined, define it.  */
6598
0
      if (hash != NULL
6599
0
    && (hash->root.type == bfd_link_hash_new
6600
0
        || hash->root.type == bfd_link_hash_undefined
6601
0
        || hash->root.type == bfd_link_hash_undefweak
6602
0
        || hash->root.type == bfd_link_hash_common))
6603
0
  {
6604
0
    asection *s = NULL;
6605
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6606
6607
0
    if (phdr_load_seg->count != 0)
6608
      /* The segment contains sections, so use the first one.  */
6609
0
      s = phdr_load_seg->sections[0];
6610
0
    else
6611
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6612
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6613
0
        if (m->p_type == PT_LOAD && m->count != 0)
6614
0
    {
6615
0
      s = m->sections[0];
6616
0
      break;
6617
0
    }
6618
6619
0
    if (s != NULL)
6620
0
      {
6621
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6622
0
        hash->root.u.def.section = s;
6623
0
      }
6624
0
    else
6625
0
      {
6626
0
        hash->root.u.def.value = filehdr_vaddr;
6627
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6628
0
      }
6629
6630
0
    hash->root.type = bfd_link_hash_defined;
6631
0
    hash->def_regular = 1;
6632
0
    hash->non_elf = 0;
6633
0
  }
6634
0
    }
6635
6636
7
  return true;
6637
7
}
6638
6639
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6640
   is no defined method for detecting such files, so we have to
6641
   use heuristics instead.  */
6642
6643
bool
6644
is_debuginfo_file (bfd *abfd)
6645
15
{
6646
15
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6647
0
    return false;
6648
6649
15
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6650
15
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6651
15
  Elf_Internal_Shdr **headerp;
6652
6653
30
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6654
30
    {
6655
30
      Elf_Internal_Shdr *header = * headerp;
6656
6657
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6658
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6659
30
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6660
30
    && header->sh_type != SHT_NOBITS
6661
30
    && header->sh_type != SHT_NOTE)
6662
15
  return false;
6663
30
    }
6664
6665
0
  return true;
6666
15
}
6667
6668
/* Assign file positions for other sections, except for compressed debug
6669
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6670
6671
static bool
6672
assign_file_positions_for_non_load_sections (bfd *abfd,
6673
               struct bfd_link_info *link_info)
6674
14
{
6675
14
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6676
14
  Elf_Internal_Shdr **i_shdrpp;
6677
14
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6678
14
  Elf_Internal_Phdr *phdrs;
6679
14
  Elf_Internal_Phdr *p;
6680
14
  struct elf_segment_map *m;
6681
14
  file_ptr off;
6682
14
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6683
14
  bfd_vma maxpagesize;
6684
6685
14
  if (link_info != NULL)
6686
0
    maxpagesize = link_info->maxpagesize;
6687
14
  else
6688
14
    maxpagesize = bed->maxpagesize;
6689
14
  i_shdrpp = elf_elfsections (abfd);
6690
14
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6691
14
  off = elf_next_file_pos (abfd);
6692
222
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6693
208
    {
6694
208
      Elf_Internal_Shdr *hdr;
6695
208
      bfd_vma align;
6696
6697
208
      hdr = *hdrpp;
6698
208
      if (hdr->bfd_section != NULL
6699
208
    && (hdr->bfd_section->filepos != 0
6700
190
        || (hdr->sh_type == SHT_NOBITS
6701
31
      && hdr->contents == NULL)))
6702
159
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6703
49
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6704
15
  {
6705
15
    if (hdr->sh_size != 0
6706
        /* PR 24717 - debuginfo files are known to be not strictly
6707
     compliant with the ELF standard.  In particular they often
6708
     have .note.gnu.property sections that are outside of any
6709
     loadable segment.  This is not a problem for such files,
6710
     so do not warn about them.  */
6711
15
        && ! is_debuginfo_file (abfd))
6712
15
      _bfd_error_handler
6713
        /* xgettext:c-format */
6714
15
        (_("%pB: warning: allocated section `%s' not in segment"),
6715
15
         abfd,
6716
15
         (hdr->bfd_section == NULL
6717
15
    ? "*unknown*"
6718
15
    : hdr->bfd_section->name));
6719
    /* We don't need to page align empty sections.  */
6720
15
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6721
15
      align = maxpagesize;
6722
0
    else
6723
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6724
15
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6725
15
    off = _bfd_elf_assign_file_position_for_section (hdr, off,
6726
15
                 false);
6727
15
  }
6728
34
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6729
34
    && hdr->bfd_section == NULL)
6730
         /* We don't know the offset of these sections yet:
6731
      their size has not been decided.  */
6732
34
         || (abfd->is_linker_output
6733
34
       && hdr->bfd_section != NULL
6734
34
       && (hdr->sh_name == -1u
6735
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6736
34
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6737
34
         || (elf_symtab_shndx_list (abfd) != NULL
6738
32
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6739
34
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6740
34
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6741
18
  hdr->sh_offset = -1;
6742
16
      else
6743
16
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6744
208
    }
6745
14
  elf_next_file_pos (abfd) = off;
6746
6747
  /* Now that we have set the section file positions, we can set up
6748
     the file positions for the non PT_LOAD segments.  */
6749
14
  phdrs = elf_tdata (abfd)->phdr;
6750
64
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6751
51
    {
6752
51
      if (p->p_type == PT_GNU_RELRO)
6753
3
  {
6754
3
    bfd_vma start, end;  /* Bytes.  */
6755
3
    bool ok;
6756
6757
3
    if (link_info != NULL)
6758
0
      {
6759
        /* During linking the range of the RELRO segment is passed
6760
     in link_info.  Note that there may be padding between
6761
     relro_start and the first RELRO section.  */
6762
0
        start = link_info->relro_start;
6763
0
        end = link_info->relro_end;
6764
0
      }
6765
3
    else if (m->count != 0)
6766
3
      {
6767
3
        if (!m->p_size_valid)
6768
0
    abort ();
6769
3
        start = m->sections[0]->vma;
6770
3
        end = start + m->p_size / opb;
6771
3
      }
6772
0
    else
6773
0
      {
6774
0
        start = 0;
6775
0
        end = 0;
6776
0
      }
6777
6778
3
    ok = false;
6779
3
    if (start < end)
6780
3
      {
6781
3
        struct elf_segment_map *lm;
6782
3
        const Elf_Internal_Phdr *lp;
6783
3
        unsigned int i;
6784
6785
        /* Find a LOAD segment containing a section in the RELRO
6786
     segment.  */
6787
3
        for (lm = elf_seg_map (abfd), lp = phdrs;
6788
12
       lm != NULL;
6789
9
       lm = lm->next, lp++)
6790
12
    {
6791
12
      if (lp->p_type == PT_LOAD
6792
12
          && lm->count != 0
6793
12
          && (lm->sections[lm->count - 1]->vma
6794
6
        + (!IS_TBSS (lm->sections[lm->count - 1])
6795
6
           ? lm->sections[lm->count - 1]->size / opb
6796
6
           : 0)) > start
6797
12
          && lm->sections[0]->vma < end)
6798
3
        break;
6799
12
    }
6800
6801
3
        if (lm != NULL)
6802
3
    {
6803
      /* Find the section starting the RELRO segment.  */
6804
3
      for (i = 0; i < lm->count; i++)
6805
3
        {
6806
3
          asection *s = lm->sections[i];
6807
3
          if (s->vma >= start
6808
3
        && s->vma < end
6809
3
        && s->size != 0)
6810
3
      break;
6811
3
        }
6812
6813
3
      if (i < lm->count)
6814
3
        {
6815
3
          p->p_vaddr = lm->sections[i]->vma * opb;
6816
3
          p->p_paddr = lm->sections[i]->lma * opb;
6817
3
          p->p_offset = lm->sections[i]->filepos;
6818
3
          p->p_memsz = end * opb - p->p_vaddr;
6819
3
          p->p_filesz = p->p_memsz;
6820
6821
          /* The RELRO segment typically ends a few bytes
6822
       into .got.plt but other layouts are possible.
6823
       In cases where the end does not match any
6824
       loaded section (for instance is in file
6825
       padding), trim p_filesz back to correspond to
6826
       the end of loaded section contents.  */
6827
3
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6828
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6829
6830
          /* Preserve the alignment and flags if they are
6831
       valid.  The gold linker generates RW/4 for
6832
       the PT_GNU_RELRO section.  It is better for
6833
       objcopy/strip to honor these attributes
6834
       otherwise gdb will choke when using separate
6835
       debug files.  */
6836
3
          if (!m->p_align_valid)
6837
0
      p->p_align = 1;
6838
3
          if (!m->p_flags_valid)
6839
0
      p->p_flags = PF_R;
6840
3
          ok = true;
6841
3
        }
6842
3
    }
6843
3
      }
6844
6845
3
    if (!ok)
6846
0
      {
6847
0
        if (link_info != NULL)
6848
0
    _bfd_error_handler
6849
0
      (_("%pB: warning: unable to allocate any sections"
6850
0
         " to PT_GNU_RELRO segment"),
6851
0
       abfd);
6852
0
        memset (p, 0, sizeof *p);
6853
0
      }
6854
3
  }
6855
48
      else if (p->p_type == PT_GNU_STACK)
6856
4
  {
6857
4
    if (m->p_size_valid)
6858
4
      p->p_memsz = m->p_size;
6859
4
  }
6860
44
      else if (m->count != 0)
6861
36
  {
6862
36
    unsigned int i;
6863
6864
36
    if (p->p_type != PT_LOAD
6865
36
        && (p->p_type != PT_NOTE
6866
24
      || bfd_get_format (abfd) != bfd_core))
6867
24
      {
6868
        /* A user specified segment layout may include a PHDR
6869
     segment that overlaps with a LOAD segment...  */
6870
24
        if (p->p_type == PT_PHDR)
6871
0
    {
6872
0
      m->count = 0;
6873
0
      continue;
6874
0
    }
6875
6876
24
        if (m->includes_filehdr || m->includes_phdrs)
6877
1
    {
6878
      /* PR 17512: file: 2195325e.  */
6879
1
      _bfd_error_handler
6880
1
        (_("%pB: error: non-load segment %d includes file header "
6881
1
           "and/or program header"),
6882
1
         abfd, (int) (p - phdrs));
6883
1
      return false;
6884
1
    }
6885
6886
23
        p->p_filesz = 0;
6887
23
        p->p_offset = m->sections[0]->filepos;
6888
23
        for (i = m->count; i-- != 0;)
6889
23
    {
6890
23
      asection *sect = m->sections[i];
6891
23
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6892
23
      if (hdr->sh_type != SHT_NOBITS)
6893
23
        {
6894
23
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6895
          /* NB: p_memsz of the loadable PT_NOTE segment
6896
       should be the same as p_filesz.  */
6897
23
          if (p->p_type == PT_NOTE
6898
23
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6899
5
      p->p_memsz = p->p_filesz;
6900
23
          break;
6901
23
        }
6902
23
    }
6903
23
      }
6904
36
  }
6905
51
    }
6906
6907
13
  return true;
6908
14
}
6909
6910
static elf_section_list *
6911
find_section_in_list (unsigned int i, elf_section_list * list)
6912
1.57k
{
6913
1.57k
  for (;list != NULL; list = list->next)
6914
0
    if (list->ndx == i)
6915
0
      break;
6916
1.57k
  return list;
6917
1.57k
}
6918
6919
/* Work out the file positions of all the sections.  This is called by
6920
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6921
   VMAs must be known before this is called.
6922
6923
   Reloc sections come in two flavours: Those processed specially as
6924
   "side-channel" data attached to a section to which they apply, and
6925
   those that bfd doesn't process as relocations.  The latter sort are
6926
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6927
   consider the former sort here, unless they form part of the loadable
6928
   image.  Reloc sections not assigned here (and compressed debugging
6929
   sections and CTF sections which nothing else in the file can rely
6930
   upon) will be handled later by assign_file_positions_for_relocs.
6931
6932
   We also don't set the positions of the .symtab and .strtab here.  */
6933
6934
static bool
6935
assign_file_positions_except_relocs (bfd *abfd,
6936
             struct bfd_link_info *link_info)
6937
28
{
6938
28
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6939
28
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6940
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6941
28
  unsigned int alloc;
6942
6943
28
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6944
28
      && bfd_get_format (abfd) != bfd_core)
6945
14
    {
6946
14
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6947
14
      unsigned int num_sec = elf_numsections (abfd);
6948
14
      Elf_Internal_Shdr **hdrpp;
6949
14
      unsigned int i;
6950
14
      file_ptr off;
6951
6952
      /* Start after the ELF header.  */
6953
14
      off = i_ehdrp->e_ehsize;
6954
6955
      /* We are not creating an executable, which means that we are
6956
   not creating a program header, and that the actual order of
6957
   the sections in the file is unimportant.  */
6958
1.53k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6959
1.52k
  {
6960
1.52k
    Elf_Internal_Shdr *hdr;
6961
6962
1.52k
    hdr = *hdrpp;
6963
1.52k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6964
1.52k
         && hdr->bfd_section == NULL)
6965
        /* Do not assign offsets for these sections yet: we don't know
6966
     their sizes.  */
6967
1.52k
        || (abfd->is_linker_output
6968
1.01k
      && hdr->bfd_section != NULL
6969
1.01k
      && (hdr->sh_name == -1u
6970
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6971
1.52k
        || i == elf_onesymtab (abfd)
6972
1.52k
        || (elf_symtab_shndx_list (abfd) != NULL
6973
1.01k
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6974
1.52k
        || i == elf_strtab_sec (abfd)
6975
1.52k
        || i == elf_shstrtab_sec (abfd))
6976
533
      {
6977
533
        hdr->sh_offset = -1;
6978
533
      }
6979
991
    else
6980
991
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6981
1.52k
  }
6982
6983
14
      elf_next_file_pos (abfd) = off;
6984
14
      elf_program_header_size (abfd) = 0;
6985
14
    }
6986
14
  else
6987
14
    {
6988
      /* Assign file positions for the loaded sections based on the
6989
   assignment of sections to segments.  */
6990
14
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6991
0
  return false;
6992
6993
      /* And for non-load sections.  */
6994
14
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6995
1
  return false;
6996
14
    }
6997
6998
27
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6999
0
    return false;
7000
7001
  /* Write out the program headers.  */
7002
27
  alloc = i_ehdrp->e_phnum;
7003
27
  if (alloc != 0)
7004
6
    {
7005
6
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7006
0
  {
7007
0
    bool warned_tls = false;
7008
0
    bool warned_rwx = false;
7009
7010
    /* Memory resident segments with non-zero size and RWX
7011
       permissions are a security risk, so we generate a warning
7012
       here if we are creating any.  */
7013
0
    unsigned int i;
7014
7015
0
    for (i = 0; i < alloc; i++)
7016
0
      {
7017
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7018
7019
0
        if (phdr->p_memsz == 0)
7020
0
    continue;
7021
7022
0
        if (! warned_tls
7023
0
      && phdr->p_type == PT_TLS
7024
0
      && (phdr->p_flags & PF_X))
7025
0
    {
7026
0
      if (link_info->warn_is_error_for_rwx_segments)
7027
0
        {
7028
0
          _bfd_error_handler (_("\
7029
0
error: %pB has a TLS segment with execute permission"),
7030
0
            abfd);
7031
0
          return false;
7032
0
        }
7033
7034
0
      _bfd_error_handler (_("\
7035
0
warning: %pB has a TLS segment with execute permission"),
7036
0
              abfd);
7037
0
      if (warned_rwx)
7038
0
        break;
7039
7040
0
      warned_tls = true;
7041
0
    }
7042
0
        else if (! warned_rwx
7043
0
           && phdr->p_type == PT_LOAD
7044
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7045
0
         == (PF_R | PF_W | PF_X)))
7046
0
    {
7047
0
      if (link_info->warn_is_error_for_rwx_segments)
7048
0
        {
7049
0
          _bfd_error_handler (_("\
7050
0
error: %pB has a LOAD segment with RWX permissions"),
7051
0
            abfd);
7052
0
          return false;
7053
0
        }
7054
7055
0
      _bfd_error_handler (_("\
7056
0
warning: %pB has a LOAD segment with RWX permissions"),
7057
0
              abfd);
7058
0
      if (warned_tls)
7059
0
        break;
7060
7061
0
      warned_rwx = true;
7062
0
    }
7063
0
      }
7064
0
  }
7065
7066
6
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7067
6
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7068
0
  return false;
7069
6
    }
7070
7071
27
  return true;
7072
27
}
7073
7074
bool
7075
_bfd_elf_init_file_header (bfd *abfd,
7076
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7077
28
{
7078
28
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7079
28
  struct elf_strtab_hash *shstrtab;
7080
28
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7081
7082
28
  i_ehdrp = elf_elfheader (abfd);
7083
7084
28
  shstrtab = _bfd_elf_strtab_init ();
7085
28
  if (shstrtab == NULL)
7086
0
    return false;
7087
7088
28
  elf_shstrtab (abfd) = shstrtab;
7089
7090
28
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7091
28
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7092
28
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7093
28
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7094
7095
28
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7096
28
  i_ehdrp->e_ident[EI_DATA] =
7097
28
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7098
28
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7099
7100
28
  if ((abfd->flags & DYNAMIC) != 0)
7101
3
    i_ehdrp->e_type = ET_DYN;
7102
25
  else if ((abfd->flags & EXEC_P) != 0)
7103
11
    i_ehdrp->e_type = ET_EXEC;
7104
14
  else if (bfd_get_format (abfd) == bfd_core)
7105
0
    i_ehdrp->e_type = ET_CORE;
7106
14
  else
7107
14
    i_ehdrp->e_type = ET_REL;
7108
7109
28
  switch (bfd_get_arch (abfd))
7110
28
    {
7111
0
    case bfd_arch_unknown:
7112
0
      i_ehdrp->e_machine = EM_NONE;
7113
0
      break;
7114
7115
      /* There used to be a long list of cases here, each one setting
7116
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7117
   in the corresponding bfd definition.  To avoid duplication,
7118
   the switch was removed.  Machines that need special handling
7119
   can generally do it in elf_backend_final_write_processing(),
7120
   unless they need the information earlier than the final write.
7121
   Such need can generally be supplied by replacing the tests for
7122
   e_machine with the conditions used to determine it.  */
7123
28
    default:
7124
28
      i_ehdrp->e_machine = bed->elf_machine_code;
7125
28
    }
7126
7127
28
  i_ehdrp->e_version = bed->s->ev_current;
7128
28
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7129
7130
  /* No program header, for now.  */
7131
28
  i_ehdrp->e_phoff = 0;
7132
28
  i_ehdrp->e_phentsize = 0;
7133
28
  i_ehdrp->e_phnum = 0;
7134
7135
  /* Each bfd section is section header entry.  */
7136
28
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7137
28
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7138
7139
28
  elf_tdata (abfd)->symtab_hdr.sh_name =
7140
28
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7141
28
  elf_tdata (abfd)->strtab_hdr.sh_name =
7142
28
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7143
28
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7144
28
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7145
28
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7146
28
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7147
28
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7148
0
    return false;
7149
7150
28
  return true;
7151
28
}
7152
7153
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7154
7155
   FIXME: We used to have code here to sort the PT_LOAD segments into
7156
   ascending order, as per the ELF spec.  But this breaks some programs,
7157
   including the Linux kernel.  But really either the spec should be
7158
   changed or the programs updated.  */
7159
7160
bool
7161
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7162
27
{
7163
27
  if (link_info != NULL && bfd_link_pie (link_info))
7164
0
    {
7165
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7166
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7167
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7168
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7169
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7170
7171
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7172
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7173
0
      for (; segment < end_segment; segment++)
7174
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7175
0
    p_vaddr = segment->p_vaddr;
7176
7177
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7178
   segments is non-zero.  */
7179
0
      if (p_vaddr)
7180
0
  i_ehdrp->e_type = ET_EXEC;
7181
0
    }
7182
27
  return true;
7183
27
}
7184
7185
/* Assign file positions for all the reloc sections which are not part
7186
   of the loadable file image, and the file position of section headers.  */
7187
7188
static bool
7189
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7190
27
{
7191
27
  file_ptr off;
7192
27
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7193
27
  Elf_Internal_Shdr *shdrp;
7194
27
  Elf_Internal_Ehdr *i_ehdrp;
7195
27
  const struct elf_backend_data *bed;
7196
7197
  /* Skip non-load sections without section header.  */
7198
27
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7199
0
    return true;
7200
7201
27
  off = elf_next_file_pos (abfd);
7202
7203
27
  shdrpp = elf_elfsections (abfd);
7204
27
  end_shdrpp = shdrpp + elf_numsections (abfd);
7205
1.73k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7206
1.70k
    {
7207
1.70k
      shdrp = *shdrpp;
7208
1.70k
      if (shdrp->sh_offset == -1)
7209
536
  {
7210
536
    asection *sec = shdrp->bfd_section;
7211
536
    if (sec == NULL
7212
536
        || shdrp->sh_type == SHT_REL
7213
536
        || shdrp->sh_type == SHT_RELA)
7214
536
      ;
7215
0
    else if (bfd_section_is_ctf (sec))
7216
0
      {
7217
        /* Update section size and contents.  */
7218
0
        shdrp->sh_size = sec->size;
7219
0
        shdrp->contents = sec->contents;
7220
0
      }
7221
0
    else if (shdrp->sh_name == -1u)
7222
0
      {
7223
0
        const char *name = sec->name;
7224
0
        struct bfd_elf_section_data *d;
7225
7226
        /* Compress DWARF debug sections.  */
7227
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7228
0
    return false;
7229
7230
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7231
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7232
0
      && name[1] == 'd')
7233
0
    {
7234
      /* If section is compressed with zlib-gnu, convert
7235
         section name from .debug_* to .zdebug_*.  */
7236
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7237
0
      if (new_name == NULL)
7238
0
        return false;
7239
0
      name = new_name;
7240
0
    }
7241
        /* Add section name to section name section.  */
7242
0
        shdrp->sh_name
7243
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7244
0
                  name, false);
7245
0
        d = elf_section_data (sec);
7246
7247
        /* Add reloc section name to section name section.  */
7248
0
        if (d->rel.hdr
7249
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7250
0
              name, false))
7251
0
    return false;
7252
0
        if (d->rela.hdr
7253
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7254
0
              name, true))
7255
0
    return false;
7256
7257
        /* Update section size and contents.  */
7258
0
        shdrp->sh_size = sec->size;
7259
0
        shdrp->contents = sec->contents;
7260
0
        sec->contents = NULL;
7261
0
      }
7262
7263
536
    off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7264
536
  }
7265
1.70k
    }
7266
7267
  /* Place section name section after DWARF debug sections have been
7268
     compressed.  */
7269
27
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7270
27
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7271
27
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7272
27
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7273
7274
  /* Place the section headers.  */
7275
27
  i_ehdrp = elf_elfheader (abfd);
7276
27
  bed = get_elf_backend_data (abfd);
7277
27
  off = align_file_position (off, 1 << bed->s->log_file_align);
7278
27
  i_ehdrp->e_shoff = off;
7279
27
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7280
27
  elf_next_file_pos (abfd) = off;
7281
7282
27
  return true;
7283
27
}
7284
7285
bool
7286
_bfd_elf_write_object_contents (bfd *abfd)
7287
27
{
7288
27
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7289
27
  Elf_Internal_Shdr **i_shdrp;
7290
27
  bool failed;
7291
27
  unsigned int count, num_sec;
7292
27
  struct elf_obj_tdata *t;
7293
7294
27
  if (! abfd->output_has_begun
7295
27
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7296
0
    return false;
7297
  /* Do not rewrite ELF data when the BFD has been opened for update.
7298
     abfd->output_has_begun was set to TRUE on opening, so creation of
7299
     new sections, and modification of existing section sizes was
7300
     restricted.  This means the ELF header, program headers and
7301
     section headers can't have changed.  If the contents of any
7302
     sections has been modified, then those changes have already been
7303
     written to the BFD.  */
7304
27
  else if (abfd->direction == both_direction)
7305
0
    {
7306
0
      BFD_ASSERT (abfd->output_has_begun);
7307
0
      return true;
7308
0
    }
7309
7310
27
  i_shdrp = elf_elfsections (abfd);
7311
7312
27
  failed = false;
7313
27
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7314
27
  if (failed)
7315
0
    return false;
7316
7317
27
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7318
0
    return false;
7319
7320
  /* After writing the headers, we need to write the sections too...  */
7321
27
  num_sec = elf_numsections (abfd);
7322
1.73k
  for (count = 1; count < num_sec; count++)
7323
1.70k
    {
7324
      /* Don't set the sh_name field without section header.  */
7325
1.70k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7326
1.70k
  i_shdrp[count]->sh_name
7327
1.70k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7328
1.70k
            i_shdrp[count]->sh_name);
7329
1.70k
      if (bed->elf_backend_section_processing)
7330
29
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7331
0
    return false;
7332
1.70k
      if (i_shdrp[count]->contents)
7333
736
  {
7334
736
    bfd_size_type amt = i_shdrp[count]->sh_size;
7335
7336
736
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7337
736
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7338
0
      return false;
7339
736
  }
7340
1.70k
    }
7341
7342
  /* Write out the section header names.  */
7343
27
  t = elf_tdata (abfd);
7344
27
  if (elf_shstrtab (abfd) != NULL
7345
27
      && t->shstrtab_hdr.sh_offset != -1
7346
27
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7347
27
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7348
0
    return false;
7349
7350
27
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7351
0
    return false;
7352
7353
27
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7354
0
    return false;
7355
7356
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7357
27
  if (t->o->build_id.after_write_object_contents != NULL
7358
27
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7359
0
    return false;
7360
27
  if (t->o->package_metadata.after_write_object_contents != NULL
7361
27
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7362
0
    return false;
7363
7364
27
  return true;
7365
27
}
7366
7367
bool
7368
_bfd_elf_write_corefile_contents (bfd *abfd)
7369
0
{
7370
  /* Hopefully this can be done just like an object file.  */
7371
0
  return _bfd_elf_write_object_contents (abfd);
7372
0
}
7373
7374
/* Given a section, search the header to find them.  */
7375
7376
unsigned int
7377
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7378
5.08k
{
7379
5.08k
  const struct elf_backend_data *bed;
7380
5.08k
  unsigned int sec_index;
7381
7382
5.08k
  if (elf_section_data (asect) != NULL
7383
5.08k
      && elf_section_data (asect)->this_idx != 0)
7384
3.38k
    return elf_section_data (asect)->this_idx;
7385
7386
1.69k
  if (bfd_is_abs_section (asect))
7387
0
    sec_index = SHN_ABS;
7388
1.69k
  else if (bfd_is_com_section (asect))
7389
0
    sec_index = SHN_COMMON;
7390
1.69k
  else if (bfd_is_und_section (asect))
7391
1.69k
    sec_index = SHN_UNDEF;
7392
0
  else
7393
0
    sec_index = SHN_BAD;
7394
7395
1.69k
  bed = get_elf_backend_data (abfd);
7396
1.69k
  if (bed->elf_backend_section_from_bfd_section)
7397
862
    {
7398
862
      int retval = sec_index;
7399
7400
862
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7401
0
  return retval;
7402
862
    }
7403
7404
1.69k
  if (sec_index == SHN_BAD)
7405
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7406
7407
1.69k
  return sec_index;
7408
1.69k
}
7409
7410
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7411
   on error.  */
7412
7413
int
7414
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7415
24.5k
{
7416
24.5k
  asymbol *asym_ptr = *asym_ptr_ptr;
7417
24.5k
  int idx;
7418
24.5k
  flagword flags = asym_ptr->flags;
7419
7420
  /* When gas creates relocations against local labels, it creates its
7421
     own symbol for the section, but does put the symbol into the
7422
     symbol chain, so udata is 0.  When the linker is generating
7423
     relocatable output, this section symbol may be for one of the
7424
     input sections rather than the output section.  */
7425
24.5k
  if (asym_ptr->udata.i == 0
7426
24.5k
      && (flags & BSF_SECTION_SYM)
7427
24.5k
      && asym_ptr->section)
7428
0
    {
7429
0
      asection *sec;
7430
7431
0
      sec = asym_ptr->section;
7432
0
      if (sec->owner != abfd && sec->output_section != NULL)
7433
0
  sec = sec->output_section;
7434
0
      if (sec->owner == abfd
7435
0
    && sec->index < elf_num_section_syms (abfd)
7436
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7437
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7438
0
    }
7439
7440
24.5k
  idx = asym_ptr->udata.i;
7441
7442
24.5k
  if (idx == 0)
7443
0
    {
7444
      /* This case can occur when using --strip-symbol on a symbol
7445
   which is used in a relocation entry.  */
7446
0
      _bfd_error_handler
7447
  /* xgettext:c-format */
7448
0
  (_("%pB: symbol `%s' required but not present"),
7449
0
   abfd, bfd_asymbol_name (asym_ptr));
7450
0
      bfd_set_error (bfd_error_no_symbols);
7451
0
      return -1;
7452
0
    }
7453
7454
#if DEBUG & 4
7455
  {
7456
    fprintf (stderr,
7457
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7458
       " flags = 0x%.8x\n",
7459
       (long) asym_ptr, asym_ptr->name, idx, flags);
7460
    fflush (stderr);
7461
  }
7462
#endif
7463
7464
24.5k
  return idx;
7465
24.5k
}
7466
7467
static inline bfd_vma
7468
segment_size (Elf_Internal_Phdr *segment)
7469
419
{
7470
419
  return (segment->p_memsz > segment->p_filesz
7471
419
    ? segment->p_memsz : segment->p_filesz);
7472
419
}
7473
7474
7475
/* Returns the end address of the segment + 1.  */
7476
static inline bfd_vma
7477
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7478
1
{
7479
1
  return start + segment_size (segment);
7480
1
}
7481
7482
static inline bfd_size_type
7483
section_size (asection *section, Elf_Internal_Phdr *segment)
7484
422
{
7485
422
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7486
422
      || (section->flags & SEC_THREAD_LOCAL) == 0
7487
422
      || segment->p_type == PT_TLS)
7488
422
    return section->size;
7489
0
  return 0;
7490
422
}
7491
7492
/* Returns TRUE if the given section is contained within the given
7493
   segment.  LMA addresses are compared against PADDR when
7494
   USE_VADDR is false, VMA against VADDR when true.  */
7495
static bool
7496
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7497
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7498
     bool use_vaddr)
7499
339
{
7500
339
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7501
339
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7502
339
  bfd_vma octet;
7503
339
  if (_bfd_mul_overflow (addr, opb, &octet))
7504
0
    return false;
7505
  /* The third and fourth lines below are testing that the section end
7506
     address is within the segment.  It's written this way to avoid
7507
     overflow.  Add seg_addr + section_size to both sides of the
7508
     inequality to make it obvious.  */
7509
339
  return (octet >= seg_addr
7510
339
    && segment_size (segment) >= section_size (section, segment)
7511
339
    && (octet - seg_addr
7512
191
        <= segment_size (segment) - section_size (section, segment)));
7513
339
}
7514
7515
/* Handle PT_NOTE segment.  */
7516
static bool
7517
is_note (asection *s, Elf_Internal_Phdr *p)
7518
189
{
7519
189
  return (p->p_type == PT_NOTE
7520
189
    && elf_section_type (s) == SHT_NOTE
7521
189
    && (ufile_ptr) s->filepos >= p->p_offset
7522
189
    && p->p_filesz >= s->size
7523
189
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7524
189
}
7525
7526
/* Rewrite program header information.  */
7527
7528
static bool
7529
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7530
2
{
7531
2
  Elf_Internal_Ehdr *iehdr;
7532
2
  struct elf_segment_map *map;
7533
2
  struct elf_segment_map *map_first;
7534
2
  struct elf_segment_map **pointer_to_map;
7535
2
  Elf_Internal_Phdr *segment;
7536
2
  asection *section;
7537
2
  unsigned int i;
7538
2
  unsigned int num_segments;
7539
2
  bool phdr_included = false;
7540
2
  bool p_paddr_valid;
7541
2
  struct elf_segment_map *phdr_adjust_seg = NULL;
7542
2
  unsigned int phdr_adjust_num = 0;
7543
2
  const struct elf_backend_data *bed;
7544
2
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7545
7546
2
  bed = get_elf_backend_data (ibfd);
7547
2
  iehdr = elf_elfheader (ibfd);
7548
7549
2
  map_first = NULL;
7550
2
  pointer_to_map = &map_first;
7551
7552
2
  num_segments = elf_elfheader (ibfd)->e_phnum;
7553
7554
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7555
     linker, which generates a PT_INTERP section with p_vaddr and
7556
     p_memsz set to 0.  */
7557
2
#define IS_SOLARIS_PT_INTERP(p, s)          \
7558
2
  (p->p_vaddr == 0              \
7559
1
   && p->p_paddr == 0              \
7560
1
   && p->p_memsz == 0              \
7561
1
   && p->p_filesz > 0              \
7562
1
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7563
1
   && s->size > 0              \
7564
1
   && (bfd_vma) s->filepos >= p->p_offset        \
7565
1
   && ((bfd_vma) s->filepos + s->size         \
7566
1
       <= p->p_offset + p->p_filesz))
7567
7568
  /* Decide if the given section should be included in the given segment.
7569
     A section will be included if:
7570
       1. It is within the address space of the segment -- we use the LMA
7571
    if that is set for the segment and the VMA otherwise,
7572
       2. It is an allocated section or a NOTE section in a PT_NOTE
7573
    segment.
7574
       3. There is an output section associated with it,
7575
       4. The section has not already been allocated to a previous segment.
7576
       5. PT_GNU_STACK segments do not include any sections.
7577
       6. PT_TLS segment includes only SHF_TLS sections.
7578
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7579
       8. PT_DYNAMIC should not contain empty sections at the beginning
7580
    (with the possible exception of .dynamic).  */
7581
2
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7582
339
  (((is_contained_by (section, segment, segment->p_paddr,    \
7583
289
          segment->p_vaddr, opb, !paddr_valid)    \
7584
289
     && (section->flags & SEC_ALLOC) != 0)       \
7585
289
    || is_note (section, segment))         \
7586
289
   && segment->p_type != PT_GNU_STACK          \
7587
289
   && (segment->p_type != PT_TLS          \
7588
100
       || (section->flags & SEC_THREAD_LOCAL))        \
7589
289
   && (segment->p_type == PT_LOAD          \
7590
100
       || segment->p_type == PT_TLS          \
7591
100
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7592
289
   && (segment->p_type != PT_DYNAMIC          \
7593
100
       || section_size (section, segment) > 0        \
7594
100
       || (segment->p_paddr            \
7595
0
     ? segment->p_paddr != section->lma * (opb)      \
7596
0
     : segment->p_vaddr != section->vma * (opb))      \
7597
100
       || (strcmp (bfd_section_name (section), ".dynamic") == 0))  \
7598
339
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7599
7600
/* If the output section of a section in the input segment is NULL,
7601
   it is removed from the corresponding output segment.   */
7602
2
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7603
50
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7604
50
   && section->output_section != NULL)
7605
7606
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7607
2
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7608
2
  (seg1->field >= segment_end (seg2, seg2->field))
7609
7610
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7611
     their VMA address ranges and their LMA address ranges overlap.
7612
     It is possible to have overlapping VMA ranges without overlapping LMA
7613
     ranges.  RedBoot images for example can have both .data and .bss mapped
7614
     to the same VMA range, but with the .data section mapped to a different
7615
     LMA.  */
7616
2
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7617
2
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7618
1
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7619
1
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7620
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7621
7622
  /* Initialise the segment mark field, and discard stupid alignment.  */
7623
59
  for (section = ibfd->sections; section != NULL; section = section->next)
7624
57
    {
7625
57
      asection *o = section->output_section;
7626
57
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7627
0
  o->alignment_power = 0;
7628
57
      section->segment_mark = false;
7629
57
    }
7630
7631
  /* The Solaris linker creates program headers in which all the
7632
     p_paddr fields are zero.  When we try to objcopy or strip such a
7633
     file, we get confused.  Check for this case, and if we find it
7634
     don't set the p_paddr_valid fields.  */
7635
2
  p_paddr_valid = false;
7636
2
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7637
8
       i < num_segments;
7638
6
       i++, segment++)
7639
7
    if (segment->p_paddr != 0)
7640
1
      {
7641
1
  p_paddr_valid = true;
7642
1
  break;
7643
1
      }
7644
7645
  /* Scan through the segments specified in the program header
7646
     of the input BFD.  For this first scan we look for overlaps
7647
     in the loadable segments.  These can be created by weird
7648
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7649
2
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7650
10
       i < num_segments;
7651
8
       i++, segment++)
7652
8
    {
7653
8
      unsigned int j;
7654
8
      Elf_Internal_Phdr *segment2;
7655
7656
8
      if (segment->p_type == PT_INTERP)
7657
1
  for (section = ibfd->sections; section; section = section->next)
7658
1
    if (IS_SOLARIS_PT_INTERP (segment, section))
7659
1
      {
7660
        /* Mininal change so that the normal section to segment
7661
     assignment code will work.  */
7662
1
        segment->p_vaddr = section->vma * opb;
7663
1
        break;
7664
1
      }
7665
7666
8
      if (segment->p_type != PT_LOAD)
7667
5
  {
7668
    /* Remove PT_GNU_RELRO segment.  */
7669
5
    if (segment->p_type == PT_GNU_RELRO)
7670
0
      segment->p_type = PT_NULL;
7671
5
    continue;
7672
5
  }
7673
7674
      /* Determine if this segment overlaps any previous segments.  */
7675
9
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7676
6
  {
7677
6
    bfd_signed_vma extra_length;
7678
7679
6
    if (segment2->p_type != PT_LOAD
7680
6
        || !SEGMENT_OVERLAPS (segment, segment2))
7681
6
      continue;
7682
7683
    /* Merge the two segments together.  */
7684
0
    if (segment2->p_vaddr < segment->p_vaddr)
7685
0
      {
7686
        /* Extend SEGMENT2 to include SEGMENT and then delete
7687
     SEGMENT.  */
7688
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7689
0
            - segment_end (segment2, segment2->p_vaddr));
7690
7691
0
        if (extra_length > 0)
7692
0
    {
7693
0
      segment2->p_memsz += extra_length;
7694
0
      segment2->p_filesz += extra_length;
7695
0
    }
7696
7697
0
        segment->p_type = PT_NULL;
7698
7699
        /* Since we have deleted P we must restart the outer loop.  */
7700
0
        i = 0;
7701
0
        segment = elf_tdata (ibfd)->phdr;
7702
0
        break;
7703
0
      }
7704
0
    else
7705
0
      {
7706
        /* Extend SEGMENT to include SEGMENT2 and then delete
7707
     SEGMENT2.  */
7708
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7709
0
            - segment_end (segment, segment->p_vaddr));
7710
7711
0
        if (extra_length > 0)
7712
0
    {
7713
0
      segment->p_memsz += extra_length;
7714
0
      segment->p_filesz += extra_length;
7715
0
    }
7716
7717
0
        segment2->p_type = PT_NULL;
7718
0
      }
7719
0
  }
7720
3
    }
7721
7722
  /* The second scan attempts to assign sections to segments.  */
7723
2
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7724
10
       i < num_segments;
7725
8
       i++, segment++)
7726
8
    {
7727
8
      unsigned int section_count;
7728
8
      asection **sections;
7729
8
      asection *output_section;
7730
8
      unsigned int isec;
7731
8
      asection *matching_lma;
7732
8
      asection *suggested_lma;
7733
8
      unsigned int j;
7734
8
      size_t amt;
7735
8
      asection *first_section;
7736
7737
8
      if (segment->p_type == PT_NULL)
7738
0
  continue;
7739
7740
8
      first_section = NULL;
7741
      /* Compute how many sections might be placed into this segment.  */
7742
8
      for (section = ibfd->sections, section_count = 0;
7743
247
     section != NULL;
7744
239
     section = section->next)
7745
239
  {
7746
    /* Find the first section in the input segment, which may be
7747
       removed from the corresponding output segment.   */
7748
239
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7749
50
      {
7750
50
        if (first_section == NULL)
7751
7
    first_section = section;
7752
50
        if (section->output_section != NULL)
7753
50
    ++section_count;
7754
50
      }
7755
239
  }
7756
7757
      /* Allocate a segment map big enough to contain
7758
   all of the sections we have selected.  */
7759
8
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7760
8
      amt += section_count * sizeof (asection *);
7761
8
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7762
8
      if (map == NULL)
7763
0
  return false;
7764
7765
      /* Initialise the fields of the segment map.  Default to
7766
   using the physical address of the segment in the input BFD.  */
7767
8
      map->next = NULL;
7768
8
      map->p_type = segment->p_type;
7769
8
      map->p_flags = segment->p_flags;
7770
8
      map->p_flags_valid = 1;
7771
7772
8
      if (map->p_type == PT_LOAD
7773
8
    && (ibfd->flags & D_PAGED) != 0
7774
8
    && maxpagesize > 1
7775
8
    && segment->p_align > 1)
7776
2
  {
7777
2
    map->p_align = segment->p_align;
7778
2
    if (segment->p_align > maxpagesize)
7779
0
      map->p_align = maxpagesize;
7780
2
    map->p_align_valid = 1;
7781
2
  }
7782
7783
      /* If the first section in the input segment is removed, there is
7784
   no need to preserve segment physical address in the corresponding
7785
   output segment.  */
7786
8
      if (!first_section || first_section->output_section != NULL)
7787
8
  {
7788
8
    map->p_paddr = segment->p_paddr;
7789
8
    map->p_paddr_valid = p_paddr_valid;
7790
8
  }
7791
7792
      /* Determine if this segment contains the ELF file header
7793
   and if it contains the program headers themselves.  */
7794
8
      map->includes_filehdr = (segment->p_offset == 0
7795
8
             && segment->p_filesz >= iehdr->e_ehsize);
7796
8
      map->includes_phdrs = 0;
7797
7798
8
      if (!phdr_included || segment->p_type != PT_LOAD)
7799
7
  {
7800
7
    map->includes_phdrs =
7801
7
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7802
7
       && (segment->p_offset + segment->p_filesz
7803
3
     >= ((bfd_vma) iehdr->e_phoff
7804
3
         + iehdr->e_phnum * iehdr->e_phentsize)));
7805
7806
7
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7807
1
      phdr_included = true;
7808
7
  }
7809
7810
8
      if (section_count == 0)
7811
1
  {
7812
    /* Special segments, such as the PT_PHDR segment, may contain
7813
       no sections, but ordinary, loadable segments should contain
7814
       something.  They are allowed by the ELF spec however, so only
7815
       a warning is produced.
7816
       Don't warn if an empty PT_LOAD contains the program headers.
7817
       There is however the valid use case of embedded systems which
7818
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7819
       flash memory with zeros.  No warning is shown for that case.  */
7820
1
    if (segment->p_type == PT_LOAD
7821
1
        && !map->includes_phdrs
7822
1
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7823
      /* xgettext:c-format */
7824
0
      _bfd_error_handler
7825
0
        (_("%pB: warning: empty loadable segment detected"
7826
0
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7827
0
         ibfd, (uint64_t) segment->p_vaddr);
7828
7829
1
    map->p_vaddr_offset = segment->p_vaddr / opb;
7830
1
    map->count = 0;
7831
1
    *pointer_to_map = map;
7832
1
    pointer_to_map = &map->next;
7833
7834
1
    continue;
7835
1
  }
7836
7837
      /* Now scan the sections in the input BFD again and attempt
7838
   to add their corresponding output sections to the segment map.
7839
   The problem here is how to handle an output section which has
7840
   been moved (ie had its LMA changed).  There are four possibilities:
7841
7842
   1. None of the sections have been moved.
7843
      In this case we can continue to use the segment LMA from the
7844
      input BFD.
7845
7846
   2. All of the sections have been moved by the same amount.
7847
      In this case we can change the segment's LMA to match the LMA
7848
      of the first section.
7849
7850
   3. Some of the sections have been moved, others have not.
7851
      In this case those sections which have not been moved can be
7852
      placed in the current segment which will have to have its size,
7853
      and possibly its LMA changed, and a new segment or segments will
7854
      have to be created to contain the other sections.
7855
7856
   4. The sections have been moved, but not by the same amount.
7857
      In this case we can change the segment's LMA to match the LMA
7858
      of the first section and we will have to create a new segment
7859
      or segments to contain the other sections.
7860
7861
   In order to save time, we allocate an array to hold the section
7862
   pointers that we are interested in.  As these sections get assigned
7863
   to a segment, they are removed from this array.  */
7864
7865
7
      amt = section_count * sizeof (asection *);
7866
7
      sections = (asection **) bfd_malloc (amt);
7867
7
      if (sections == NULL)
7868
0
  return false;
7869
7870
      /* Step One: Scan for segment vs section LMA conflicts.
7871
   Also add the sections to the section array allocated above.
7872
   Also add the sections to the current segment.  In the common
7873
   case, where the sections have not been moved, this means that
7874
   we have completely filled the segment, and there is nothing
7875
   more to do.  */
7876
7
      isec = 0;
7877
7
      matching_lma = NULL;
7878
7
      suggested_lma = NULL;
7879
7880
7
      for (section = first_section, j = 0;
7881
50
     section != NULL;
7882
43
     section = section->next)
7883
50
  {
7884
50
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7885
50
      {
7886
50
        output_section = section->output_section;
7887
7888
50
        sections[j++] = section;
7889
7890
        /* The Solaris native linker always sets p_paddr to 0.
7891
     We try to catch that case here, and set it to the
7892
     correct value.  Note - some backends require that
7893
     p_paddr be left as zero.  */
7894
50
        if (!p_paddr_valid
7895
50
      && segment->p_vaddr != 0
7896
50
      && !bed->want_p_paddr_set_to_zero
7897
50
      && isec == 0
7898
50
      && output_section->lma != 0
7899
50
      && (align_power (segment->p_vaddr
7900
4
           + (map->includes_filehdr
7901
4
              ? iehdr->e_ehsize : 0)
7902
4
           + (map->includes_phdrs
7903
4
              ? iehdr->e_phnum * iehdr->e_phentsize
7904
4
              : 0),
7905
4
           output_section->alignment_power * opb)
7906
4
          == (output_section->vma * opb)))
7907
4
    map->p_paddr = segment->p_vaddr;
7908
7909
        /* Match up the physical address of the segment with the
7910
     LMA address of the output section.  */
7911
50
        if (is_contained_by (output_section, segment, map->p_paddr,
7912
50
           0, opb, false)
7913
50
      || is_note (section, segment))
7914
50
    {
7915
50
      if (matching_lma == NULL
7916
50
          || output_section->lma < matching_lma->lma)
7917
7
        matching_lma = output_section;
7918
7919
      /* We assume that if the section fits within the segment
7920
         then it does not overlap any other section within that
7921
         segment.  */
7922
50
      map->sections[isec++] = output_section;
7923
50
    }
7924
0
        else if (suggested_lma == NULL)
7925
0
    suggested_lma = output_section;
7926
7927
50
        if (j == section_count)
7928
7
    break;
7929
50
      }
7930
50
  }
7931
7932
7
      BFD_ASSERT (j == section_count);
7933
7934
      /* Step Two: Adjust the physical address of the current segment,
7935
   if necessary.  */
7936
7
      if (isec == section_count)
7937
7
  {
7938
    /* All of the sections fitted within the segment as currently
7939
       specified.  This is the default case.  Add the segment to
7940
       the list of built segments and carry on to process the next
7941
       program header in the input BFD.  */
7942
7
    map->count = section_count;
7943
7
    *pointer_to_map = map;
7944
7
    pointer_to_map = &map->next;
7945
7946
7
    if (p_paddr_valid
7947
7
        && !bed->want_p_paddr_set_to_zero)
7948
3
      {
7949
3
        bfd_vma hdr_size = 0;
7950
3
        if (map->includes_filehdr)
7951
1
    hdr_size = iehdr->e_ehsize;
7952
3
        if (map->includes_phdrs)
7953
1
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7954
7955
        /* Account for padding before the first section in the
7956
     segment.  */
7957
3
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7958
3
             - matching_lma->lma);
7959
3
      }
7960
7961
7
    free (sections);
7962
7
    continue;
7963
7
  }
7964
0
      else
7965
0
  {
7966
    /* Change the current segment's physical address to match
7967
       the LMA of the first section that fitted, or if no
7968
       section fitted, the first section.  */
7969
0
    if (matching_lma == NULL)
7970
0
      matching_lma = suggested_lma;
7971
7972
0
    map->p_paddr = matching_lma->lma * opb;
7973
7974
    /* Offset the segment physical address from the lma
7975
       to allow for space taken up by elf headers.  */
7976
0
    if (map->includes_phdrs)
7977
0
      {
7978
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7979
7980
        /* iehdr->e_phnum is just an estimate of the number
7981
     of program headers that we will need.  Make a note
7982
     here of the number we used and the segment we chose
7983
     to hold these headers, so that we can adjust the
7984
     offset when we know the correct value.  */
7985
0
        phdr_adjust_num = iehdr->e_phnum;
7986
0
        phdr_adjust_seg = map;
7987
0
      }
7988
7989
0
    if (map->includes_filehdr)
7990
0
      {
7991
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7992
0
        map->p_paddr -= iehdr->e_ehsize;
7993
        /* We've subtracted off the size of headers from the
7994
     first section lma, but there may have been some
7995
     alignment padding before that section too.  Try to
7996
     account for that by adjusting the segment lma down to
7997
     the same alignment.  */
7998
0
        if (segment->p_align != 0 && segment->p_align < align)
7999
0
    align = segment->p_align;
8000
0
        map->p_paddr &= -(align * opb);
8001
0
      }
8002
0
  }
8003
8004
      /* Step Three: Loop over the sections again, this time assigning
8005
   those that fit to the current segment and removing them from the
8006
   sections array; but making sure not to leave large gaps.  Once all
8007
   possible sections have been assigned to the current segment it is
8008
   added to the list of built segments and if sections still remain
8009
   to be assigned, a new segment is constructed before repeating
8010
   the loop.  */
8011
0
      isec = 0;
8012
0
      do
8013
0
  {
8014
0
    map->count = 0;
8015
0
    suggested_lma = NULL;
8016
8017
    /* Fill the current segment with sections that fit.  */
8018
0
    for (j = 0; j < section_count; j++)
8019
0
      {
8020
0
        section = sections[j];
8021
8022
0
        if (section == NULL)
8023
0
    continue;
8024
8025
0
        output_section = section->output_section;
8026
8027
0
        BFD_ASSERT (output_section != NULL);
8028
8029
0
        if (is_contained_by (output_section, segment, map->p_paddr,
8030
0
           0, opb, false)
8031
0
      || is_note (section, segment))
8032
0
    {
8033
0
      if (map->count == 0)
8034
0
        {
8035
          /* If the first section in a segment does not start at
8036
       the beginning of the segment, then something is
8037
       wrong.  */
8038
0
          if (align_power (map->p_paddr
8039
0
               + (map->includes_filehdr
8040
0
            ? iehdr->e_ehsize : 0)
8041
0
               + (map->includes_phdrs
8042
0
            ? iehdr->e_phnum * iehdr->e_phentsize
8043
0
            : 0),
8044
0
               output_section->alignment_power * opb)
8045
0
        != output_section->lma * opb)
8046
0
      goto sorry;
8047
0
        }
8048
0
      else
8049
0
        {
8050
0
          asection *prev_sec;
8051
8052
0
          prev_sec = map->sections[map->count - 1];
8053
8054
          /* If the gap between the end of the previous section
8055
       and the start of this section is more than
8056
       maxpagesize then we need to start a new segment.  */
8057
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8058
0
              maxpagesize)
8059
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
8060
0
        || (prev_sec->lma + prev_sec->size
8061
0
            > output_section->lma))
8062
0
      {
8063
0
        if (suggested_lma == NULL)
8064
0
          suggested_lma = output_section;
8065
8066
0
        continue;
8067
0
      }
8068
0
        }
8069
8070
0
      map->sections[map->count++] = output_section;
8071
0
      ++isec;
8072
0
      sections[j] = NULL;
8073
0
      if (segment->p_type == PT_LOAD)
8074
0
        section->segment_mark = true;
8075
0
    }
8076
0
        else if (suggested_lma == NULL)
8077
0
    suggested_lma = output_section;
8078
0
      }
8079
8080
    /* PR 23932.  A corrupt input file may contain sections that cannot
8081
       be assigned to any segment - because for example they have a
8082
       negative size - or segments that do not contain any sections.
8083
       But there are also valid reasons why a segment can be empty.
8084
       So allow a count of zero.  */
8085
8086
    /* Add the current segment to the list of built segments.  */
8087
0
    *pointer_to_map = map;
8088
0
    pointer_to_map = &map->next;
8089
8090
0
    if (isec < section_count)
8091
0
      {
8092
        /* We still have not allocated all of the sections to
8093
     segments.  Create a new segment here, initialise it
8094
     and carry on looping.  */
8095
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8096
0
        amt += section_count * sizeof (asection *);
8097
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8098
0
        if (map == NULL)
8099
0
    {
8100
0
      free (sections);
8101
0
      return false;
8102
0
    }
8103
8104
        /* Initialise the fields of the segment map.  Set the physical
8105
     physical address to the LMA of the first section that has
8106
     not yet been assigned.  */
8107
0
        map->next = NULL;
8108
0
        map->p_type = segment->p_type;
8109
0
        map->p_flags = segment->p_flags;
8110
0
        map->p_flags_valid = 1;
8111
0
        map->p_paddr = suggested_lma->lma * opb;
8112
0
        map->p_paddr_valid = p_paddr_valid;
8113
0
        map->includes_filehdr = 0;
8114
0
        map->includes_phdrs = 0;
8115
0
      }
8116
8117
0
    continue;
8118
0
  sorry:
8119
0
    bfd_set_error (bfd_error_sorry);
8120
0
    free (sections);
8121
0
    return false;
8122
0
  }
8123
0
      while (isec < section_count);
8124
8125
0
      free (sections);
8126
0
    }
8127
8128
2
  elf_seg_map (obfd) = map_first;
8129
8130
  /* If we had to estimate the number of program headers that were
8131
     going to be needed, then check our estimate now and adjust
8132
     the offset if necessary.  */
8133
2
  if (phdr_adjust_seg != NULL)
8134
0
    {
8135
0
      unsigned int count;
8136
8137
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8138
0
  count++;
8139
8140
0
      if (count > phdr_adjust_num)
8141
0
  phdr_adjust_seg->p_paddr
8142
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8143
8144
0
      for (map = map_first; map != NULL; map = map->next)
8145
0
  if (map->p_type == PT_PHDR)
8146
0
    {
8147
0
      bfd_vma adjust
8148
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8149
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8150
0
      break;
8151
0
    }
8152
0
    }
8153
8154
2
#undef IS_SOLARIS_PT_INTERP
8155
2
#undef IS_SECTION_IN_INPUT_SEGMENT
8156
2
#undef INCLUDE_SECTION_IN_SEGMENT
8157
2
#undef SEGMENT_AFTER_SEGMENT
8158
2
#undef SEGMENT_OVERLAPS
8159
2
  return true;
8160
2
}
8161
8162
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8163
8164
static bool
8165
elf_is_p_align_valid (bfd *abfd)
8166
5
{
8167
5
  unsigned int i;
8168
5
  Elf_Internal_Phdr *segment;
8169
5
  unsigned int num_segments;
8170
5
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8171
5
  bfd_size_type maxpagesize = bed->maxpagesize;
8172
5
  bfd_size_type p_align = bed->p_align;
8173
8174
  /* Return true if the default p_align value isn't set or the maximum
8175
     page size is the same as the minimum page size.  */
8176
5
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8177
5
    return true;
8178
8179
  /* When the default p_align value is set, p_align may be set to the
8180
     default p_align value while segments are aligned to the maximum
8181
     page size.  In this case, the input p_align will be ignored and
8182
     the maximum page size will be used to align the output segments.  */
8183
0
  segment = elf_tdata (abfd)->phdr;
8184
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8185
0
  for (i = 0; i < num_segments; i++, segment++)
8186
0
    if (segment->p_type == PT_LOAD
8187
0
  && (segment->p_align != p_align
8188
0
      || vma_page_aligned_bias (segment->p_vaddr,
8189
0
              segment->p_offset,
8190
0
              maxpagesize) != 0))
8191
0
      return true;
8192
8193
0
  return false;
8194
0
}
8195
8196
/* Copy ELF program header information.  */
8197
8198
static bool
8199
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8200
5
{
8201
5
  Elf_Internal_Ehdr *iehdr;
8202
5
  struct elf_segment_map *map;
8203
5
  struct elf_segment_map *map_first;
8204
5
  struct elf_segment_map **pointer_to_map;
8205
5
  Elf_Internal_Phdr *segment;
8206
5
  unsigned int i;
8207
5
  unsigned int num_segments;
8208
5
  bool phdr_included = false;
8209
5
  bool p_paddr_valid;
8210
5
  bool p_palign_valid;
8211
5
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8212
8213
5
  iehdr = elf_elfheader (ibfd);
8214
8215
5
  map_first = NULL;
8216
5
  pointer_to_map = &map_first;
8217
8218
  /* If all the segment p_paddr fields are zero, don't set
8219
     map->p_paddr_valid.  */
8220
5
  p_paddr_valid = false;
8221
5
  num_segments = elf_elfheader (ibfd)->e_phnum;
8222
5
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8223
5
       i < num_segments;
8224
5
       i++, segment++)
8225
5
    if (segment->p_paddr != 0)
8226
5
      {
8227
5
  p_paddr_valid = true;
8228
5
  break;
8229
5
      }
8230
8231
5
  p_palign_valid = elf_is_p_align_valid (ibfd);
8232
8233
5
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8234
50
       i < num_segments;
8235
45
       i++, segment++)
8236
45
    {
8237
45
      asection *section;
8238
45
      unsigned int section_count;
8239
45
      size_t amt;
8240
45
      Elf_Internal_Shdr *this_hdr;
8241
45
      asection *first_section = NULL;
8242
45
      asection *lowest_section;
8243
8244
      /* Compute how many sections are in this segment.  */
8245
45
      for (section = ibfd->sections, section_count = 0;
8246
1.24k
     section != NULL;
8247
1.19k
     section = section->next)
8248
1.19k
  {
8249
1.19k
    this_hdr = &(elf_section_data(section)->this_hdr);
8250
1.19k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8251
168
      {
8252
168
        if (first_section == NULL)
8253
34
    first_section = section;
8254
168
        section_count++;
8255
168
      }
8256
1.19k
  }
8257
8258
      /* Allocate a segment map big enough to contain
8259
   all of the sections we have selected.  */
8260
45
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8261
45
      amt += section_count * sizeof (asection *);
8262
45
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8263
45
      if (map == NULL)
8264
0
  return false;
8265
8266
      /* Initialize the fields of the output segment map with the
8267
   input segment.  */
8268
45
      map->next = NULL;
8269
45
      map->p_type = segment->p_type;
8270
45
      map->p_flags = segment->p_flags;
8271
45
      map->p_flags_valid = 1;
8272
45
      map->p_paddr = segment->p_paddr;
8273
45
      map->p_paddr_valid = p_paddr_valid;
8274
45
      map->p_align = segment->p_align;
8275
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8276
45
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8277
45
          || p_palign_valid);
8278
45
      map->p_vaddr_offset = 0;
8279
8280
45
      if (map->p_type == PT_GNU_RELRO
8281
45
    || map->p_type == PT_GNU_STACK)
8282
7
  {
8283
    /* The PT_GNU_RELRO segment may contain the first a few
8284
       bytes in the .got.plt section even if the whole .got.plt
8285
       section isn't in the PT_GNU_RELRO segment.  We won't
8286
       change the size of the PT_GNU_RELRO segment.
8287
       Similarly, PT_GNU_STACK size is significant on uclinux
8288
       systems.    */
8289
7
    map->p_size = segment->p_memsz;
8290
7
    map->p_size_valid = 1;
8291
7
  }
8292
8293
      /* Determine if this segment contains the ELF file header
8294
   and if it contains the program headers themselves.  */
8295
45
      map->includes_filehdr = (segment->p_offset == 0
8296
45
             && segment->p_filesz >= iehdr->e_ehsize);
8297
8298
45
      map->includes_phdrs = 0;
8299
45
      if (! phdr_included || segment->p_type != PT_LOAD)
8300
40
  {
8301
40
    map->includes_phdrs =
8302
40
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8303
40
       && (segment->p_offset + segment->p_filesz
8304
16
     >= ((bfd_vma) iehdr->e_phoff
8305
16
         + iehdr->e_phnum * iehdr->e_phentsize)));
8306
8307
40
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8308
5
      phdr_included = true;
8309
40
  }
8310
8311
45
      lowest_section = NULL;
8312
45
      if (section_count != 0)
8313
34
  {
8314
34
    unsigned int isec = 0;
8315
8316
34
    for (section = first_section;
8317
168
         section != NULL;
8318
134
         section = section->next)
8319
168
      {
8320
168
        this_hdr = &(elf_section_data(section)->this_hdr);
8321
168
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8322
168
    {
8323
168
      map->sections[isec++] = section->output_section;
8324
168
      if ((section->flags & SEC_ALLOC) != 0)
8325
168
        {
8326
168
          bfd_vma seg_off;
8327
8328
168
          if (lowest_section == NULL
8329
168
        || section->lma < lowest_section->lma)
8330
34
      lowest_section = section;
8331
8332
          /* Section lmas are set up from PT_LOAD header
8333
       p_paddr in _bfd_elf_make_section_from_shdr.
8334
       If this header has a p_paddr that disagrees
8335
       with the section lma, flag the p_paddr as
8336
       invalid.  */
8337
168
          if ((section->flags & SEC_LOAD) != 0)
8338
161
      seg_off = this_hdr->sh_offset - segment->p_offset;
8339
7
          else
8340
7
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8341
168
          if (section->lma * opb - segment->p_paddr != seg_off)
8342
0
      map->p_paddr_valid = false;
8343
168
        }
8344
168
      if (isec == section_count)
8345
34
        break;
8346
168
    }
8347
168
      }
8348
34
  }
8349
8350
45
      if (section_count == 0)
8351
11
  map->p_vaddr_offset = segment->p_vaddr / opb;
8352
34
      else if (map->p_paddr_valid)
8353
34
  {
8354
    /* Account for padding before the first section in the segment.  */
8355
34
    bfd_vma hdr_size = 0;
8356
34
    if (map->includes_filehdr)
8357
5
      hdr_size = iehdr->e_ehsize;
8358
34
    if (map->includes_phdrs)
8359
5
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8360
8361
34
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8362
34
         - (lowest_section ? lowest_section->lma : 0));
8363
34
  }
8364
8365
45
      map->count = section_count;
8366
45
      *pointer_to_map = map;
8367
45
      pointer_to_map = &map->next;
8368
45
    }
8369
8370
5
  elf_seg_map (obfd) = map_first;
8371
5
  return true;
8372
5
}
8373
8374
/* Copy private BFD data.  This copies or rewrites ELF program header
8375
   information.  */
8376
8377
static bool
8378
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8379
7
{
8380
7
  bfd_vma maxpagesize;
8381
8382
7
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8383
7
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8384
0
    return true;
8385
8386
7
  if (elf_tdata (ibfd)->phdr == NULL)
8387
0
    return true;
8388
8389
7
  if (ibfd->xvec == obfd->xvec)
8390
7
    {
8391
      /* Check to see if any sections in the input BFD
8392
   covered by ELF program header have changed.  */
8393
7
      Elf_Internal_Phdr *segment;
8394
7
      asection * section;
8395
7
      asection * osec;
8396
7
      asection * prev;
8397
7
      unsigned int i, num_segments;
8398
7
      Elf_Internal_Shdr *this_hdr;
8399
7
      const struct elf_backend_data *bed;
8400
8401
7
      bed = get_elf_backend_data (ibfd);
8402
8403
      /* Regenerate the segment map if p_paddr is set to 0.  */
8404
7
      if (bed->want_p_paddr_set_to_zero)
8405
0
  goto rewrite;
8406
8407
      /* Initialize the segment mark field.  */
8408
197
      for (section = obfd->sections; section != NULL;
8409
190
     section = section->next)
8410
190
  section->segment_mark = false;
8411
8412
7
      num_segments = elf_elfheader (ibfd)->e_phnum;
8413
7
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8414
54
     i < num_segments;
8415
47
     i++, segment++)
8416
49
  {
8417
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8418
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8419
       which severly confuses things, so always regenerate the segment
8420
       map in this case.  */
8421
49
    if (segment->p_paddr == 0
8422
49
        && segment->p_memsz == 0
8423
49
        && (segment->p_type == PT_INTERP
8424
7
      || segment->p_type == PT_DYNAMIC))
8425
1
      goto rewrite;
8426
8427
48
    for (section = ibfd->sections, prev = NULL;
8428
1.31k
         section != NULL; section = section->next)
8429
1.27k
      {
8430
        /* We mark the output section so that we know it comes
8431
     from the input BFD.  */
8432
1.27k
        osec = section->output_section;
8433
1.27k
        if (osec)
8434
1.27k
    osec->segment_mark = true;
8435
8436
        /* Check if this section is covered by the segment.  */
8437
1.27k
        this_hdr = &(elf_section_data(section)->this_hdr);
8438
1.27k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8439
187
    {
8440
      /* FIXME: Check if its output section is changed or
8441
         removed.  What else do we need to check?  */
8442
187
      if (osec == NULL
8443
187
          || section->flags != osec->flags
8444
187
          || section->lma != osec->lma
8445
187
          || section->vma != osec->vma
8446
187
          || section->size != osec->size
8447
187
          || section->rawsize != osec->rawsize
8448
187
          || section->alignment_power != osec->alignment_power)
8449
0
        goto rewrite;
8450
8451
      /* PR 31450: If this is an allocated section then make sure
8452
         that this section's vma to lma relationship is the same
8453
         as previous (allocated) section's.  */
8454
187
      if (prev != NULL
8455
187
          && section->flags & SEC_ALLOC
8456
187
          && section->lma - section->vma != prev->lma - prev->vma)
8457
1
        goto rewrite;
8458
8459
186
      if (section->flags & SEC_ALLOC)
8460
186
        prev = section;
8461
186
    }
8462
1.27k
      }
8463
48
  }
8464
8465
      /* Check to see if any output section do not come from the
8466
   input BFD.  */
8467
138
      for (section = obfd->sections; section != NULL;
8468
133
     section = section->next)
8469
133
  {
8470
133
    if (!section->segment_mark)
8471
0
      goto rewrite;
8472
133
    else
8473
133
      section->segment_mark = false;
8474
133
  }
8475
8476
5
      return copy_elf_program_header (ibfd, obfd);
8477
5
    }
8478
8479
2
 rewrite:
8480
2
  maxpagesize = 0;
8481
2
  if (ibfd->xvec == obfd->xvec)
8482
2
    {
8483
      /* When rewriting program header, set the output maxpagesize to
8484
   the maximum alignment of input PT_LOAD segments.  */
8485
2
      Elf_Internal_Phdr *segment;
8486
2
      unsigned int i;
8487
2
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8488
8489
2
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8490
10
     i < num_segments;
8491
8
     i++, segment++)
8492
8
  if (segment->p_type == PT_LOAD
8493
8
      && maxpagesize < segment->p_align)
8494
2
    {
8495
      /* PR 17512: file: f17299af.  */
8496
2
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8497
        /* xgettext:c-format */
8498
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8499
0
            PRIx64 " is too large"),
8500
0
          ibfd, (uint64_t) segment->p_align);
8501
2
      else
8502
2
        maxpagesize = segment->p_align;
8503
2
    }
8504
2
    }
8505
2
  if (maxpagesize == 0)
8506
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8507
8508
2
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8509
7
}
8510
8511
/* Initialize private output section information from input section.  */
8512
8513
bool
8514
_bfd_elf_init_private_section_data (bfd *ibfd,
8515
            asection *isec,
8516
            bfd *obfd,
8517
            asection *osec,
8518
            struct bfd_link_info *link_info)
8519
8520
1.18k
{
8521
1.18k
  Elf_Internal_Shdr *ihdr, *ohdr;
8522
1.18k
  bool final_link = (link_info != NULL
8523
1.18k
         && !bfd_link_relocatable (link_info));
8524
8525
1.18k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8526
1.18k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8527
0
    return true;
8528
8529
1.18k
  BFD_ASSERT (elf_section_data (osec) != NULL);
8530
8531
  /* If this is a known ABI section, ELF section type and flags may
8532
     have been set up when OSEC was created.  For normal sections we
8533
     allow the user to override the type and flags other than
8534
     SHF_MASKOS and SHF_MASKPROC.  */
8535
1.18k
  if (elf_section_type (osec) == SHT_PROGBITS
8536
1.18k
      || elf_section_type (osec) == SHT_NOTE
8537
1.18k
      || elf_section_type (osec) == SHT_NOBITS)
8538
385
    elf_section_type (osec) = SHT_NULL;
8539
  /* For objcopy and relocatable link, copy the ELF section type from
8540
     the input file if the BFD section flags are the same.  (If they
8541
     are different the user may be doing something like
8542
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8543
     link allow some flags that the linker clears to differ.  */
8544
1.18k
  if (elf_section_type (osec) == SHT_NULL
8545
1.18k
      && (osec->flags == isec->flags
8546
1.10k
    || (final_link
8547
0
        && ((osec->flags ^ isec->flags)
8548
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8549
1.10k
    elf_section_type (osec) = elf_section_type (isec);
8550
8551
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
8552
1.18k
  elf_section_flags (osec) = (elf_section_flags (isec)
8553
1.18k
            & (SHF_MASKOS | SHF_MASKPROC));
8554
8555
  /* Copy sh_info from input for mbind section.  */
8556
1.18k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8557
1.18k
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8558
0
    elf_section_data (osec)->this_hdr.sh_info
8559
0
      = elf_section_data (isec)->this_hdr.sh_info;
8560
8561
  /* Set things up for objcopy and relocatable link.  The output
8562
     SHT_GROUP section will have its elf_next_in_group pointing back
8563
     to the input group members.  Ignore linker created group section.
8564
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8565
1.18k
  if ((link_info == NULL
8566
1.18k
       || !link_info->resolve_section_groups)
8567
1.18k
      && (elf_sec_group (isec) == NULL
8568
1.18k
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8569
1.18k
    {
8570
1.18k
      if (elf_section_flags (isec) & SHF_GROUP)
8571
675
  elf_section_flags (osec) |= SHF_GROUP;
8572
1.18k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8573
1.18k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8574
1.18k
    }
8575
8576
  /* If not decompress, preserve SHF_COMPRESSED.  */
8577
1.18k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8578
1.18k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8579
1.18k
         & SHF_COMPRESSED);
8580
8581
1.18k
  ihdr = &elf_section_data (isec)->this_hdr;
8582
8583
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8584
     don't use the output section of the linked-to section since it
8585
     may be NULL at this point.  */
8586
1.18k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8587
0
    {
8588
0
      ohdr = &elf_section_data (osec)->this_hdr;
8589
0
      ohdr->sh_flags |= SHF_LINK_ORDER;
8590
0
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8591
0
    }
8592
8593
1.18k
  osec->use_rela_p = isec->use_rela_p;
8594
8595
1.18k
  return true;
8596
1.18k
}
8597
8598
/* Copy private section information.  This copies over the entsize
8599
   field, and sometimes the info field.  */
8600
8601
bool
8602
_bfd_elf_copy_private_section_data (bfd *ibfd,
8603
            asection *isec,
8604
            bfd *obfd,
8605
            asection *osec)
8606
1.18k
{
8607
1.18k
  Elf_Internal_Shdr *ihdr, *ohdr;
8608
8609
1.18k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8610
1.18k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8611
0
    return true;
8612
8613
1.18k
  ihdr = &elf_section_data (isec)->this_hdr;
8614
1.18k
  ohdr = &elf_section_data (osec)->this_hdr;
8615
8616
1.18k
  ohdr->sh_entsize = ihdr->sh_entsize;
8617
8618
1.18k
  if (ihdr->sh_type == SHT_SYMTAB
8619
1.18k
      || ihdr->sh_type == SHT_DYNSYM
8620
1.18k
      || ihdr->sh_type == SHT_GNU_verneed
8621
1.18k
      || ihdr->sh_type == SHT_GNU_verdef)
8622
14
    ohdr->sh_info = ihdr->sh_info;
8623
8624
1.18k
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8625
1.18k
               NULL);
8626
1.18k
}
8627
8628
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8629
   necessary if we are removing either the SHT_GROUP section or any of
8630
   the group member sections.  DISCARDED is the value that a section's
8631
   output_section has if the section will be discarded, NULL when this
8632
   function is called from objcopy, bfd_abs_section_ptr when called
8633
   from the linker.  */
8634
8635
bool
8636
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8637
12
{
8638
12
  asection *isec;
8639
8640
1.19k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8641
1.17k
    if (elf_section_type (isec) == SHT_GROUP)
8642
221
      {
8643
221
  asection *first = elf_next_in_group (isec);
8644
221
  asection *s = first;
8645
221
  bfd_size_type removed = 0;
8646
8647
675
  while (s != NULL)
8648
675
    {
8649
      /* If this member section is being output but the
8650
         SHT_GROUP section is not, then clear the group info
8651
         set up by _bfd_elf_copy_private_section_data.  */
8652
675
      if (s->output_section != discarded
8653
675
    && isec->output_section == discarded)
8654
0
        {
8655
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8656
0
    elf_group_name (s->output_section) = NULL;
8657
0
        }
8658
675
      else
8659
675
        {
8660
675
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8661
675
    if (s->output_section == discarded
8662
675
        && isec->output_section != discarded)
8663
0
      {
8664
        /* Conversely, if the member section is not being
8665
           output but the SHT_GROUP section is, then adjust
8666
           its size.  */
8667
0
        removed += 4;
8668
0
        if (elf_sec->rel.hdr != NULL
8669
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8670
0
          removed += 4;
8671
0
        if (elf_sec->rela.hdr != NULL
8672
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8673
0
          removed += 4;
8674
0
      }
8675
675
    else
8676
675
      {
8677
        /* Also adjust for zero-sized relocation member
8678
           section.  */
8679
675
        if (elf_sec->rel.hdr != NULL
8680
675
      && elf_sec->rel.hdr->sh_size == 0)
8681
0
          removed += 4;
8682
675
        if (elf_sec->rela.hdr != NULL
8683
675
      && elf_sec->rela.hdr->sh_size == 0)
8684
0
          removed += 4;
8685
675
      }
8686
675
        }
8687
675
      s = elf_next_in_group (s);
8688
675
      if (s == first)
8689
221
        break;
8690
675
    }
8691
221
  if (removed != 0)
8692
0
    {
8693
0
      if (discarded != NULL)
8694
0
        {
8695
    /* If we've been called for ld -r, then we need to
8696
       adjust the input section size.  */
8697
0
    if (isec->rawsize == 0)
8698
0
      isec->rawsize = isec->size;
8699
0
    isec->size = isec->rawsize - removed;
8700
0
    if (isec->size <= 4)
8701
0
      {
8702
0
        isec->size = 0;
8703
0
        isec->flags |= SEC_EXCLUDE;
8704
0
      }
8705
0
        }
8706
0
      else if (isec->output_section != NULL)
8707
0
        {
8708
    /* Adjust the output section size when called from
8709
       objcopy. */
8710
0
    isec->output_section->size -= removed;
8711
0
    if (isec->output_section->size <= 4)
8712
0
      {
8713
0
        isec->output_section->size = 0;
8714
0
        isec->output_section->flags |= SEC_EXCLUDE;
8715
0
      }
8716
0
        }
8717
0
    }
8718
221
      }
8719
8720
12
  return true;
8721
12
}
8722
8723
/* Copy private header information.  */
8724
8725
bool
8726
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8727
12
{
8728
12
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8729
12
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8730
0
    return true;
8731
8732
  /* Copy over private BFD data if it has not already been copied.
8733
     This must be done here, rather than in the copy_private_bfd_data
8734
     entry point, because the latter is called after the section
8735
     contents have been set, which means that the program headers have
8736
     already been worked out.  */
8737
12
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8738
7
    {
8739
7
      if (! copy_private_bfd_data (ibfd, obfd))
8740
0
  return false;
8741
7
    }
8742
8743
12
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8744
12
}
8745
8746
/* Copy private symbol information.  If this symbol is in a section
8747
   which we did not map into a BFD section, try to map the section
8748
   index correctly.  We use special macro definitions for the mapped
8749
   section indices; these definitions are interpreted by the
8750
   swap_out_syms function.  */
8751
8752
2
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8753
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8754
12
#define MAP_STRTAB    (SHN_HIOS + 3)
8755
2
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8756
0
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8757
8758
bool
8759
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8760
           asymbol *isymarg,
8761
           bfd *obfd,
8762
           asymbol *osymarg)
8763
170k
{
8764
170k
  elf_symbol_type *isym, *osym;
8765
8766
170k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8767
170k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8768
0
    return true;
8769
8770
170k
  isym = elf_symbol_from (isymarg);
8771
170k
  osym = elf_symbol_from (osymarg);
8772
8773
170k
  if (isym != NULL
8774
170k
      && isym->internal_elf_sym.st_shndx != 0
8775
170k
      && osym != NULL
8776
170k
      && bfd_is_abs_section (isym->symbol.section))
8777
1.58k
    {
8778
1.58k
      unsigned int shndx;
8779
8780
1.58k
      shndx = isym->internal_elf_sym.st_shndx;
8781
1.58k
      if (shndx == elf_onesymtab (ibfd))
8782
1
  shndx = MAP_ONESYMTAB;
8783
1.57k
      else if (shndx == elf_dynsymtab (ibfd))
8784
0
  shndx = MAP_DYNSYMTAB;
8785
1.57k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8786
6
  shndx = MAP_STRTAB;
8787
1.57k
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8788
1
  shndx = MAP_SHSTRTAB;
8789
1.57k
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8790
0
  shndx = MAP_SYM_SHNDX;
8791
1.58k
      osym->internal_elf_sym.st_shndx = shndx;
8792
1.58k
    }
8793
8794
170k
  return true;
8795
170k
}
8796
8797
/* Swap out the symbols.  */
8798
8799
static bool
8800
swap_out_syms (bfd *abfd,
8801
         struct elf_strtab_hash **sttp,
8802
         int relocatable_p,
8803
         struct bfd_link_info *info)
8804
7
{
8805
7
  const struct elf_backend_data *bed;
8806
7
  unsigned int symcount;
8807
7
  asymbol **syms;
8808
7
  struct elf_strtab_hash *stt;
8809
7
  Elf_Internal_Shdr *symtab_hdr;
8810
7
  Elf_Internal_Shdr *symtab_shndx_hdr;
8811
7
  Elf_Internal_Shdr *symstrtab_hdr;
8812
7
  struct elf_sym_strtab *symstrtab;
8813
7
  bfd_byte *outbound_syms;
8814
7
  bfd_byte *outbound_shndx;
8815
7
  unsigned long outbound_syms_index;
8816
7
  unsigned int idx;
8817
7
  unsigned int num_locals;
8818
7
  size_t amt;
8819
7
  bool name_local_sections;
8820
8821
7
  if (!elf_map_symbols (abfd, &num_locals))
8822
0
    return false;
8823
8824
  /* Dump out the symtabs.  */
8825
7
  stt = _bfd_elf_strtab_init ();
8826
7
  if (stt == NULL)
8827
0
    return false;
8828
8829
7
  bed = get_elf_backend_data (abfd);
8830
7
  symcount = bfd_get_symcount (abfd);
8831
7
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8832
7
  symtab_hdr->sh_type = SHT_SYMTAB;
8833
7
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8834
7
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8835
7
  symtab_hdr->sh_info = num_locals + 1;
8836
7
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8837
8838
7
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8839
7
  symstrtab_hdr->sh_type = SHT_STRTAB;
8840
8841
  /* Allocate buffer to swap out the .strtab section.  */
8842
7
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8843
7
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8844
0
    {
8845
0
      bfd_set_error (bfd_error_no_memory);
8846
0
      _bfd_elf_strtab_free (stt);
8847
0
      return false;
8848
0
    }
8849
8850
7
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8851
7
      || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8852
0
    {
8853
0
    error_no_mem:
8854
0
      bfd_set_error (bfd_error_no_memory);
8855
0
    error_return:
8856
0
      free (symstrtab);
8857
0
      _bfd_elf_strtab_free (stt);
8858
0
      return false;
8859
0
    }
8860
7
  symtab_hdr->contents = outbound_syms;
8861
7
  outbound_syms_index = 0;
8862
8863
7
  outbound_shndx = NULL;
8864
8865
7
  if (elf_symtab_shndx_list (abfd))
8866
0
    {
8867
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8868
0
      if (symtab_shndx_hdr->sh_name != 0)
8869
0
  {
8870
0
    if (_bfd_mul_overflow (symcount + 1,
8871
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8872
0
      goto error_no_mem;
8873
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8874
0
    if (outbound_shndx == NULL)
8875
0
      goto error_return;
8876
8877
0
    symtab_shndx_hdr->contents = outbound_shndx;
8878
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8879
0
    symtab_shndx_hdr->sh_size = amt;
8880
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8881
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8882
0
  }
8883
      /* FIXME: What about any other headers in the list ?  */
8884
0
    }
8885
8886
  /* Now generate the data (for "contents").  */
8887
7
  {
8888
    /* Fill in zeroth symbol and swap it out.  */
8889
7
    Elf_Internal_Sym sym;
8890
7
    sym.st_name = 0;
8891
7
    sym.st_value = 0;
8892
7
    sym.st_size = 0;
8893
7
    sym.st_info = 0;
8894
7
    sym.st_other = 0;
8895
7
    sym.st_shndx = SHN_UNDEF;
8896
7
    sym.st_target_internal = 0;
8897
7
    symstrtab[outbound_syms_index].sym = sym;
8898
7
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8899
7
    outbound_syms_index++;
8900
7
  }
8901
8902
7
  name_local_sections
8903
7
    = (bed->elf_backend_name_local_section_symbols
8904
7
       && bed->elf_backend_name_local_section_symbols (abfd));
8905
8906
7
  syms = bfd_get_outsymbols (abfd);
8907
6.54k
  for (idx = 0; idx < symcount; idx++)
8908
6.53k
    {
8909
6.53k
      Elf_Internal_Sym sym;
8910
8911
6.53k
      flagword flags = syms[idx]->flags;
8912
6.53k
      if (!name_local_sections
8913
6.53k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8914
727
  {
8915
    /* Local section symbols have no name.  */
8916
727
    sym.st_name = (unsigned long) -1;
8917
727
  }
8918
5.81k
      else
8919
5.81k
  {
8920
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8921
       to get the final offset for st_name.  */
8922
5.81k
    sym.st_name
8923
5.81k
      = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8924
5.81k
               false);
8925
5.81k
    if (sym.st_name == (unsigned long) -1)
8926
0
      goto error_return;
8927
5.81k
  }
8928
8929
6.53k
      bfd_vma value = syms[idx]->value;
8930
6.53k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8931
6.53k
      asection *sec = syms[idx]->section;
8932
8933
6.53k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8934
0
  {
8935
    /* ELF common symbols put the alignment into the `value' field,
8936
       and the size into the `size' field.  This is backwards from
8937
       how BFD handles it, so reverse it here.  */
8938
0
    sym.st_size = value;
8939
0
    if (type_ptr == NULL
8940
0
        || type_ptr->internal_elf_sym.st_value == 0)
8941
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8942
0
    else
8943
0
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8944
0
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8945
0
  }
8946
6.53k
      else
8947
6.53k
  {
8948
6.53k
    unsigned int shndx;
8949
8950
6.53k
    if (sec->output_section)
8951
6.53k
      {
8952
6.53k
        value += sec->output_offset;
8953
6.53k
        sec = sec->output_section;
8954
6.53k
      }
8955
8956
    /* Don't add in the section vma for relocatable output.  */
8957
6.53k
    if (! relocatable_p)
8958
3.52k
      value += sec->vma;
8959
6.53k
    sym.st_value = value;
8960
6.53k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8961
8962
6.53k
    if (bfd_is_abs_section (sec)
8963
6.53k
        && type_ptr != NULL
8964
6.53k
        && type_ptr->internal_elf_sym.st_shndx != 0)
8965
1.58k
      {
8966
        /* This symbol is in a real ELF section which we did
8967
     not create as a BFD section.  Undo the mapping done
8968
     by copy_private_symbol_data.  */
8969
1.58k
        shndx = type_ptr->internal_elf_sym.st_shndx;
8970
1.58k
        switch (shndx)
8971
1.58k
    {
8972
1
    case MAP_ONESYMTAB:
8973
1
      shndx = elf_onesymtab (abfd);
8974
1
      break;
8975
0
    case MAP_DYNSYMTAB:
8976
0
      shndx = elf_dynsymtab (abfd);
8977
0
      break;
8978
6
    case MAP_STRTAB:
8979
6
      shndx = elf_strtab_sec (abfd);
8980
6
      break;
8981
1
    case MAP_SHSTRTAB:
8982
1
      shndx = elf_shstrtab_sec (abfd);
8983
1
      break;
8984
0
    case MAP_SYM_SHNDX:
8985
0
      if (elf_symtab_shndx_list (abfd))
8986
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8987
0
      break;
8988
0
    case SHN_COMMON:
8989
82
    case SHN_ABS:
8990
82
      shndx = SHN_ABS;
8991
82
      break;
8992
1.49k
    default:
8993
1.49k
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8994
0
        {
8995
0
          if (bed->symbol_section_index)
8996
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8997
          /* Otherwise just leave the index alone.  */
8998
0
        }
8999
1.49k
      else
9000
1.49k
        {
9001
1.49k
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
9002
0
      _bfd_error_handler (_("%pB: \
9003
0
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
9004
0
            abfd, shndx);
9005
1.49k
          shndx = SHN_ABS;
9006
1.49k
        }
9007
1.49k
      break;
9008
1.58k
    }
9009
1.58k
      }
9010
4.95k
    else
9011
4.95k
      {
9012
4.95k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9013
9014
4.95k
        if (shndx == SHN_BAD)
9015
0
    {
9016
0
      asection *sec2;
9017
9018
      /* Writing this would be a hell of a lot easier if
9019
         we had some decent documentation on bfd, and
9020
         knew what to expect of the library, and what to
9021
         demand of applications.  For example, it
9022
         appears that `objcopy' might not set the
9023
         section of a symbol to be a section that is
9024
         actually in the output file.  */
9025
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
9026
0
      if (sec2 != NULL)
9027
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
9028
0
      if (shndx == SHN_BAD)
9029
0
        {
9030
          /* xgettext:c-format */
9031
0
          _bfd_error_handler
9032
0
      (_("unable to find equivalent output section"
9033
0
         " for symbol '%s' from section '%s'"),
9034
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
9035
0
       sec->name);
9036
0
          bfd_set_error (bfd_error_invalid_operation);
9037
0
          goto error_return;
9038
0
        }
9039
0
    }
9040
4.95k
      }
9041
9042
6.53k
    sym.st_shndx = shndx;
9043
6.53k
  }
9044
9045
6.53k
      int type;
9046
6.53k
      if ((flags & BSF_THREAD_LOCAL) != 0)
9047
6
  type = STT_TLS;
9048
6.53k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9049
5
  type = STT_GNU_IFUNC;
9050
6.52k
      else if ((flags & BSF_FUNCTION) != 0)
9051
1.91k
  type = STT_FUNC;
9052
4.61k
      else if ((flags & BSF_OBJECT) != 0)
9053
1.47k
  type = STT_OBJECT;
9054
3.13k
      else if ((flags & BSF_RELC) != 0)
9055
9
  type = STT_RELC;
9056
3.12k
      else if ((flags & BSF_SRELC) != 0)
9057
4
  type = STT_SRELC;
9058
3.12k
      else
9059
3.12k
  type = STT_NOTYPE;
9060
9061
6.53k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9062
0
  type = STT_TLS;
9063
9064
      /* Processor-specific types.  */
9065
6.53k
      if (type_ptr != NULL
9066
6.53k
    && bed->elf_backend_get_symbol_type)
9067
0
  type = ((*bed->elf_backend_get_symbol_type)
9068
0
    (&type_ptr->internal_elf_sym, type));
9069
9070
6.53k
      if (flags & BSF_SECTION_SYM)
9071
727
  {
9072
727
    if (flags & BSF_GLOBAL)
9073
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9074
727
    else
9075
727
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9076
727
  }
9077
5.81k
      else if (bfd_is_com_section (syms[idx]->section))
9078
0
  {
9079
0
    if (type != STT_TLS)
9080
0
      {
9081
0
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9082
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9083
0
      ? STT_COMMON : STT_OBJECT);
9084
0
        else
9085
0
    type = ((flags & BSF_ELF_COMMON) != 0
9086
0
      ? STT_COMMON : STT_OBJECT);
9087
0
      }
9088
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9089
0
  }
9090
5.81k
      else if (bfd_is_und_section (syms[idx]->section))
9091
1.69k
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9092
5.81k
            ? STB_WEAK
9093
5.81k
            : STB_GLOBAL),
9094
5.81k
           type);
9095
4.12k
      else if (flags & BSF_FILE)
9096
89
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9097
4.03k
      else
9098
4.03k
  {
9099
4.03k
    int bind = STB_LOCAL;
9100
9101
4.03k
    if (flags & BSF_LOCAL)
9102
2.94k
      bind = STB_LOCAL;
9103
1.08k
    else if (flags & BSF_GNU_UNIQUE)
9104
0
      bind = STB_GNU_UNIQUE;
9105
1.08k
    else if (flags & BSF_WEAK)
9106
254
      bind = STB_WEAK;
9107
831
    else if (flags & BSF_GLOBAL)
9108
822
      bind = STB_GLOBAL;
9109
9110
4.03k
    sym.st_info = ELF_ST_INFO (bind, type);
9111
4.03k
  }
9112
9113
6.53k
      if (type_ptr != NULL)
9114
6.53k
  {
9115
6.53k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9116
6.53k
    sym.st_target_internal
9117
6.53k
      = type_ptr->internal_elf_sym.st_target_internal;
9118
6.53k
  }
9119
0
      else
9120
0
  {
9121
0
    sym.st_other = 0;
9122
0
    sym.st_target_internal = 0;
9123
0
  }
9124
9125
6.53k
      symstrtab[outbound_syms_index].sym = sym;
9126
6.53k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9127
6.53k
      outbound_syms_index++;
9128
6.53k
    }
9129
9130
  /* Finalize the .strtab section.  */
9131
7
  _bfd_elf_strtab_finalize (stt);
9132
9133
  /* Swap out the .strtab section.  */
9134
6.55k
  for (idx = 0; idx < outbound_syms_index; idx++)
9135
6.54k
    {
9136
6.54k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9137
6.54k
      if (elfsym->sym.st_name == (unsigned long) -1)
9138
727
  elfsym->sym.st_name = 0;
9139
5.81k
      else
9140
5.81k
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9141
5.81k
                  elfsym->sym.st_name);
9142
6.54k
      if (info && info->callbacks->ctf_new_symbol)
9143
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9144
0
           &elfsym->sym);
9145
9146
      /* Inform the linker of the addition of this symbol.  */
9147
9148
6.54k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9149
6.54k
             (outbound_syms
9150
6.54k
        + (elfsym->dest_index
9151
6.54k
           * bed->s->sizeof_sym)),
9152
6.54k
             NPTR_ADD (outbound_shndx,
9153
6.54k
           (elfsym->dest_index
9154
6.54k
            * sizeof (Elf_External_Sym_Shndx))));
9155
6.54k
    }
9156
7
  free (symstrtab);
9157
9158
7
  *sttp = stt;
9159
7
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9160
7
  symstrtab_hdr->sh_type = SHT_STRTAB;
9161
7
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9162
7
  symstrtab_hdr->sh_addr = 0;
9163
7
  symstrtab_hdr->sh_entsize = 0;
9164
7
  symstrtab_hdr->sh_link = 0;
9165
7
  symstrtab_hdr->sh_info = 0;
9166
7
  symstrtab_hdr->sh_addralign = 1;
9167
9168
7
  return true;
9169
7
}
9170
9171
/* Return the number of bytes required to hold the symtab vector.
9172
9173
   Note that we base it on the count plus 1, since we will null terminate
9174
   the vector allocated based on this size.  However, the ELF symbol table
9175
   always has a dummy entry as symbol #0, so it ends up even.  */
9176
9177
long
9178
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9179
2.84k
{
9180
2.84k
  bfd_size_type symcount;
9181
2.84k
  long symtab_size;
9182
2.84k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9183
9184
2.84k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9185
2.84k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9186
0
    {
9187
0
      bfd_set_error (bfd_error_file_too_big);
9188
0
      return -1;
9189
0
    }
9190
2.84k
  symtab_size = symcount * (sizeof (asymbol *));
9191
2.84k
  if (symcount == 0)
9192
129
    symtab_size = sizeof (asymbol *);
9193
2.71k
  else if (!bfd_write_p (abfd))
9194
2.71k
    {
9195
2.71k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9196
9197
2.71k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9198
69
  {
9199
69
    bfd_set_error (bfd_error_file_truncated);
9200
69
    return -1;
9201
69
  }
9202
2.71k
    }
9203
9204
2.77k
  return symtab_size;
9205
2.84k
}
9206
9207
long
9208
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9209
4.05k
{
9210
4.05k
  bfd_size_type symcount;
9211
4.05k
  long symtab_size;
9212
4.05k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9213
9214
4.05k
  if (elf_dynsymtab (abfd) == 0)
9215
3.79k
    {
9216
      /* Check if there is dynamic symbol table.  */
9217
3.79k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9218
3.79k
      if (symcount)
9219
0
  goto compute_symtab_size;
9220
9221
3.79k
      bfd_set_error (bfd_error_invalid_operation);
9222
3.79k
      return -1;
9223
3.79k
    }
9224
9225
258
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9226
258
  if (symcount > LONG_MAX / sizeof (asymbol *))
9227
0
    {
9228
0
      bfd_set_error (bfd_error_file_too_big);
9229
0
      return -1;
9230
0
    }
9231
9232
258
 compute_symtab_size:
9233
258
  symtab_size = symcount * (sizeof (asymbol *));
9234
258
  if (symcount == 0)
9235
3
    symtab_size = sizeof (asymbol *);
9236
255
  else if (!bfd_write_p (abfd))
9237
255
    {
9238
255
      ufile_ptr filesize = bfd_get_file_size (abfd);
9239
9240
255
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9241
9
  {
9242
9
    bfd_set_error (bfd_error_file_truncated);
9243
9
    return -1;
9244
9
  }
9245
255
    }
9246
9247
249
  return symtab_size;
9248
258
}
9249
9250
long
9251
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9252
18.4k
{
9253
18.4k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9254
17.4k
    {
9255
      /* Sanity check reloc section size.  */
9256
17.4k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9257
9258
17.4k
      if (filesize != 0)
9259
17.4k
  {
9260
17.4k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9261
17.4k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9262
17.4k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9263
9264
17.4k
    if (rel_size + rela_size > filesize
9265
17.4k
        || rel_size + rela_size < rel_size)
9266
714
      {
9267
714
        bfd_set_error (bfd_error_file_truncated);
9268
714
        return -1;
9269
714
      }
9270
17.4k
  }
9271
17.4k
    }
9272
9273
#if SIZEOF_LONG == SIZEOF_INT
9274
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9275
    {
9276
      bfd_set_error (bfd_error_file_too_big);
9277
      return -1;
9278
    }
9279
#endif
9280
17.7k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9281
18.4k
}
9282
9283
/* Canonicalize the relocs.  */
9284
9285
long
9286
_bfd_elf_canonicalize_reloc (bfd *abfd,
9287
           sec_ptr section,
9288
           arelent **relptr,
9289
           asymbol **symbols)
9290
17.6k
{
9291
17.6k
  arelent *tblptr;
9292
17.6k
  unsigned int i;
9293
17.6k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9294
9295
17.6k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9296
2.64k
    return -1;
9297
9298
15.0k
  tblptr = section->relocation;
9299
926k
  for (i = 0; i < section->reloc_count; i++)
9300
911k
    *relptr++ = tblptr++;
9301
9302
15.0k
  *relptr = NULL;
9303
9304
15.0k
  return section->reloc_count;
9305
17.6k
}
9306
9307
long
9308
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9309
2.77k
{
9310
2.77k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9311
2.77k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9312
9313
2.77k
  if (symcount >= 0)
9314
2.68k
    abfd->symcount = symcount;
9315
2.77k
  return symcount;
9316
2.77k
}
9317
9318
long
9319
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9320
              asymbol **allocation)
9321
145
{
9322
145
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9323
145
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9324
9325
145
  if (symcount >= 0)
9326
117
    abfd->dynsymcount = symcount;
9327
145
  return symcount;
9328
145
}
9329
9330
/* Return the size required for the dynamic reloc entries.  Any loadable
9331
   section that was actually installed in the BFD, and has type SHT_REL
9332
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9333
   dynamic reloc section.  */
9334
9335
long
9336
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9337
3.72k
{
9338
3.72k
  bfd_size_type count, ext_rel_size;
9339
3.72k
  asection *s;
9340
9341
3.72k
  if (elf_dynsymtab (abfd) == 0)
9342
3.60k
    {
9343
3.60k
      bfd_set_error (bfd_error_invalid_operation);
9344
3.60k
      return -1;
9345
3.60k
    }
9346
9347
122
  count = 1;
9348
122
  ext_rel_size = 0;
9349
3.77k
  for (s = abfd->sections; s != NULL; s = s->next)
9350
3.65k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9351
3.65k
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9352
487
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9353
3.65k
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9354
246
      {
9355
246
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9356
246
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9357
0
    {
9358
0
      bfd_set_error (bfd_error_file_truncated);
9359
0
      return -1;
9360
0
    }
9361
246
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9362
246
  if (count > LONG_MAX / sizeof (arelent *))
9363
0
    {
9364
0
      bfd_set_error (bfd_error_file_too_big);
9365
0
      return -1;
9366
0
    }
9367
246
      }
9368
122
  if (count > 1 && !bfd_write_p (abfd))
9369
114
    {
9370
      /* Sanity check reloc section sizes.  */
9371
114
      ufile_ptr filesize = bfd_get_file_size (abfd);
9372
114
      if (filesize != 0 && ext_rel_size > filesize)
9373
0
  {
9374
0
    bfd_set_error (bfd_error_file_truncated);
9375
0
    return -1;
9376
0
  }
9377
114
    }
9378
122
  return count * sizeof (arelent *);
9379
122
}
9380
9381
/* Canonicalize the dynamic relocation entries.  Note that we return the
9382
   dynamic relocations as a single block, although they are actually
9383
   associated with particular sections; the interface, which was
9384
   designed for SunOS style shared libraries, expects that there is only
9385
   one set of dynamic relocs.  Any loadable section that was actually
9386
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9387
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9388
9389
long
9390
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9391
             arelent **storage,
9392
             asymbol **syms)
9393
118
{
9394
118
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9395
118
  asection *s;
9396
118
  long ret;
9397
9398
118
  if (elf_dynsymtab (abfd) == 0)
9399
0
    {
9400
0
      bfd_set_error (bfd_error_invalid_operation);
9401
0
      return -1;
9402
0
    }
9403
9404
118
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9405
118
  ret = 0;
9406
3.44k
  for (s = abfd->sections; s != NULL; s = s->next)
9407
3.33k
    {
9408
3.33k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9409
3.33k
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9410
469
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9411
3.33k
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9412
237
  {
9413
237
    arelent *p;
9414
237
    long count, i;
9415
9416
237
    if (! (*slurp_relocs) (abfd, s, syms, true))
9417
8
      return -1;
9418
229
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9419
229
    p = s->relocation;
9420
39.6k
    for (i = 0; i < count; i++)
9421
39.3k
      *storage++ = p++;
9422
229
    ret += count;
9423
229
  }
9424
3.33k
    }
9425
9426
110
  *storage = NULL;
9427
9428
110
  return ret;
9429
118
}
9430

9431
/* Read in the version information.  */
9432
9433
bool
9434
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9435
166
{
9436
166
  bfd_byte *contents = NULL;
9437
166
  unsigned int freeidx = 0;
9438
166
  size_t amt;
9439
166
  void *contents_addr = NULL;
9440
166
  size_t contents_size = 0;
9441
9442
166
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9443
154
    {
9444
154
      Elf_Internal_Shdr *hdr;
9445
154
      Elf_External_Verneed *everneed;
9446
154
      Elf_Internal_Verneed *iverneed;
9447
154
      unsigned int i;
9448
154
      bfd_byte *contents_end;
9449
154
      size_t verneed_count;
9450
154
      size_t verneed_size;
9451
9452
154
      if (elf_tdata (abfd)->dt_verneed != NULL)
9453
0
  {
9454
0
    hdr = NULL;
9455
0
    contents = elf_tdata (abfd)->dt_verneed;
9456
0
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9457
0
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9458
0
  }
9459
154
      else
9460
154
  {
9461
154
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9462
9463
154
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9464
1
      {
9465
38
      error_return_bad_verref:
9466
38
        _bfd_error_handler
9467
38
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9468
38
        bfd_set_error (bfd_error_bad_value);
9469
40
      error_return_verref:
9470
40
        elf_tdata (abfd)->verref = NULL;
9471
40
        elf_tdata (abfd)->cverrefs = 0;
9472
40
        goto error_return;
9473
38
      }
9474
9475
153
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9476
1
      goto error_return_verref;
9477
152
    contents_size = hdr->sh_size;
9478
152
    contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
9479
152
               &contents_addr,
9480
152
               &contents_size);
9481
152
    if (contents == NULL)
9482
1
      goto error_return_verref;
9483
9484
151
    verneed_size = hdr->sh_size;
9485
151
    verneed_count = hdr->sh_info;
9486
151
  }
9487
9488
151
      if (_bfd_mul_overflow (verneed_count,
9489
151
           sizeof (Elf_Internal_Verneed), &amt))
9490
0
  {
9491
0
    bfd_set_error (bfd_error_file_too_big);
9492
0
    goto error_return_verref;
9493
0
  }
9494
151
      if (amt == 0)
9495
0
  goto error_return_verref;
9496
151
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9497
151
      if (elf_tdata (abfd)->verref == NULL)
9498
0
  goto error_return_verref;
9499
9500
151
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9501
151
      == sizeof (Elf_External_Vernaux));
9502
151
      contents_end = (contents + verneed_size
9503
151
          - sizeof (Elf_External_Verneed));
9504
151
      everneed = (Elf_External_Verneed *) contents;
9505
151
      iverneed = elf_tdata (abfd)->verref;
9506
317
      for (i = 0; i < verneed_count; i++, iverneed++)
9507
317
  {
9508
317
    Elf_External_Vernaux *evernaux;
9509
317
    Elf_Internal_Vernaux *ivernaux;
9510
317
    unsigned int j;
9511
9512
317
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9513
9514
317
    iverneed->vn_bfd = abfd;
9515
9516
317
    if (elf_use_dt_symtab_p (abfd))
9517
0
      {
9518
0
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9519
0
    iverneed->vn_filename
9520
0
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9521
0
        else
9522
0
    iverneed->vn_filename = NULL;
9523
0
      }
9524
317
    else if (hdr == NULL)
9525
0
      goto error_return_bad_verref;
9526
317
    else
9527
317
      iverneed->vn_filename
9528
317
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9529
317
             iverneed->vn_file);
9530
317
    if (iverneed->vn_filename == NULL)
9531
24
      goto error_return_bad_verref;
9532
9533
293
    if (iverneed->vn_cnt == 0)
9534
9
      iverneed->vn_auxptr = NULL;
9535
284
    else
9536
284
      {
9537
284
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9538
284
             sizeof (Elf_Internal_Vernaux), &amt))
9539
0
    {
9540
0
      bfd_set_error (bfd_error_file_too_big);
9541
0
      goto error_return_verref;
9542
0
    }
9543
284
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9544
284
    bfd_alloc (abfd, amt);
9545
284
        if (iverneed->vn_auxptr == NULL)
9546
0
    goto error_return_verref;
9547
284
      }
9548
9549
293
    if (iverneed->vn_aux
9550
293
        > (size_t) (contents_end - (bfd_byte *) everneed))
9551
3
      goto error_return_bad_verref;
9552
9553
290
    evernaux = ((Elf_External_Vernaux *)
9554
290
          ((bfd_byte *) everneed + iverneed->vn_aux));
9555
290
    ivernaux = iverneed->vn_auxptr;
9556
620
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9557
613
      {
9558
613
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9559
9560
613
        if (elf_use_dt_symtab_p (abfd))
9561
0
    {
9562
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9563
0
        ivernaux->vna_nodename
9564
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9565
0
      else
9566
0
        ivernaux->vna_nodename = NULL;
9567
0
    }
9568
613
        else if (hdr == NULL)
9569
0
    goto error_return_bad_verref;
9570
613
        else
9571
613
    ivernaux->vna_nodename
9572
613
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9573
613
                 ivernaux->vna_name);
9574
613
        if (ivernaux->vna_nodename == NULL)
9575
5
    goto error_return_bad_verref;
9576
9577
608
        if (ivernaux->vna_other > freeidx)
9578
109
    freeidx = ivernaux->vna_other;
9579
9580
608
        ivernaux->vna_nextptr = NULL;
9581
608
        if (ivernaux->vna_next == 0)
9582
275
    {
9583
275
      iverneed->vn_cnt = j + 1;
9584
275
      break;
9585
275
    }
9586
333
        if (j + 1 < iverneed->vn_cnt)
9587
332
    ivernaux->vna_nextptr = ivernaux + 1;
9588
9589
333
        if (ivernaux->vna_next
9590
333
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9591
3
    goto error_return_bad_verref;
9592
9593
330
        evernaux = ((Elf_External_Vernaux *)
9594
330
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9595
330
      }
9596
9597
282
    iverneed->vn_nextref = NULL;
9598
282
    if (iverneed->vn_next == 0)
9599
114
      break;
9600
168
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9601
166
      iverneed->vn_nextref = iverneed + 1;
9602
9603
168
    if (iverneed->vn_next
9604
168
        > (size_t) (contents_end - (bfd_byte *) everneed))
9605
2
      goto error_return_bad_verref;
9606
9607
166
    everneed = ((Elf_External_Verneed *)
9608
166
          ((bfd_byte *) everneed + iverneed->vn_next));
9609
166
  }
9610
114
      elf_tdata (abfd)->cverrefs = i;
9611
9612
114
      if (contents != elf_tdata (abfd)->dt_verneed)
9613
114
  _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9614
114
      contents = NULL;
9615
114
      contents_addr = NULL;
9616
114
    }
9617
9618
126
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9619
12
    {
9620
12
      Elf_Internal_Shdr *hdr;
9621
12
      Elf_External_Verdef *everdef;
9622
12
      Elf_Internal_Verdef *iverdef;
9623
12
      Elf_Internal_Verdef *iverdefarr;
9624
12
      Elf_Internal_Verdef iverdefmem;
9625
12
      unsigned int i;
9626
12
      unsigned int maxidx;
9627
12
      bfd_byte *contents_end_def, *contents_end_aux;
9628
12
      size_t verdef_count;
9629
12
      size_t verdef_size;
9630
9631
12
      if (elf_tdata (abfd)->dt_verdef != NULL)
9632
0
  {
9633
0
    hdr = NULL;
9634
0
    contents = elf_tdata (abfd)->dt_verdef;
9635
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9636
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9637
0
  }
9638
12
      else
9639
12
  {
9640
12
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9641
9642
12
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9643
0
      {
9644
11
      error_return_bad_verdef:
9645
11
        _bfd_error_handler
9646
11
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9647
11
        bfd_set_error (bfd_error_bad_value);
9648
12
      error_return_verdef:
9649
12
        elf_tdata (abfd)->verdef = NULL;
9650
12
        elf_tdata (abfd)->cverdefs = 0;
9651
12
        goto error_return;
9652
11
      }
9653
9654
12
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9655
0
      goto error_return_verdef;
9656
12
    contents_size = hdr->sh_size;
9657
12
    contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
9658
12
               &contents_addr,
9659
12
               &contents_size);
9660
12
    if (contents == NULL)
9661
1
      goto error_return_verdef;
9662
9663
11
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9664
11
          >= sizeof (Elf_External_Verdaux));
9665
9666
11
    verdef_count = hdr->sh_info;
9667
11
    verdef_size = hdr->sh_size;
9668
11
  }
9669
9670
11
      contents_end_def = (contents + verdef_size
9671
11
        - sizeof (Elf_External_Verdef));
9672
11
      contents_end_aux = (contents + verdef_size
9673
11
        - sizeof (Elf_External_Verdaux));
9674
9675
      /* We know the number of entries in the section but not the maximum
9676
   index.  Therefore we have to run through all entries and find
9677
   the maximum.  */
9678
11
      everdef = (Elf_External_Verdef *) contents;
9679
11
      maxidx = 0;
9680
13
      for (i = 0; i < verdef_count; ++i)
9681
11
  {
9682
11
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9683
9684
11
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9685
2
      goto error_return_bad_verdef;
9686
9
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9687
9
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9688
9689
9
    if (iverdefmem.vd_next == 0)
9690
0
      break;
9691
9692
9
    if (iverdefmem.vd_next
9693
9
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9694
7
      goto error_return_bad_verdef;
9695
9696
2
    everdef = ((Elf_External_Verdef *)
9697
2
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9698
2
  }
9699
9700
2
      if (default_imported_symver)
9701
0
  {
9702
0
    if (freeidx > maxidx)
9703
0
      maxidx = ++freeidx;
9704
0
    else
9705
0
      freeidx = ++maxidx;
9706
0
  }
9707
2
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9708
0
  {
9709
0
    bfd_set_error (bfd_error_file_too_big);
9710
0
    goto error_return_verdef;
9711
0
  }
9712
9713
2
      if (amt == 0)
9714
0
  goto error_return_verdef;
9715
2
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9716
2
      if (elf_tdata (abfd)->verdef == NULL)
9717
0
  goto error_return_verdef;
9718
9719
2
      elf_tdata (abfd)->cverdefs = maxidx;
9720
9721
2
      everdef = (Elf_External_Verdef *) contents;
9722
2
      iverdefarr = elf_tdata (abfd)->verdef;
9723
2
      for (i = 0; i < verdef_count; ++i)
9724
2
  {
9725
2
    Elf_External_Verdaux *everdaux;
9726
2
    Elf_Internal_Verdaux *iverdaux;
9727
2
    unsigned int j;
9728
9729
2
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9730
9731
2
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9732
0
      goto error_return_bad_verdef;
9733
9734
2
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9735
2
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9736
9737
2
    iverdef->vd_bfd = abfd;
9738
9739
2
    if (iverdef->vd_cnt == 0)
9740
0
      iverdef->vd_auxptr = NULL;
9741
2
    else
9742
2
      {
9743
2
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9744
2
             sizeof (Elf_Internal_Verdaux), &amt))
9745
0
    {
9746
0
      bfd_set_error (bfd_error_file_too_big);
9747
0
      goto error_return_verdef;
9748
0
    }
9749
2
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9750
2
    bfd_alloc (abfd, amt);
9751
2
        if (iverdef->vd_auxptr == NULL)
9752
0
    goto error_return_verdef;
9753
2
      }
9754
9755
2
    if (iverdef->vd_aux
9756
2
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9757
0
      goto error_return_bad_verdef;
9758
9759
2
    everdaux = ((Elf_External_Verdaux *)
9760
2
          ((bfd_byte *) everdef + iverdef->vd_aux));
9761
2
    iverdaux = iverdef->vd_auxptr;
9762
2
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9763
2
      {
9764
2
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9765
9766
2
        if (elf_use_dt_symtab_p (abfd))
9767
0
    {
9768
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9769
0
        iverdaux->vda_nodename
9770
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9771
0
      else
9772
0
        iverdaux->vda_nodename = NULL;
9773
0
    }
9774
2
        else
9775
2
    iverdaux->vda_nodename
9776
2
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9777
2
                 iverdaux->vda_name);
9778
2
        if (iverdaux->vda_nodename == NULL)
9779
2
    goto error_return_bad_verdef;
9780
9781
0
        iverdaux->vda_nextptr = NULL;
9782
0
        if (iverdaux->vda_next == 0)
9783
0
    {
9784
0
      iverdef->vd_cnt = j + 1;
9785
0
      break;
9786
0
    }
9787
0
        if (j + 1 < iverdef->vd_cnt)
9788
0
    iverdaux->vda_nextptr = iverdaux + 1;
9789
9790
0
        if (iverdaux->vda_next
9791
0
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9792
0
    goto error_return_bad_verdef;
9793
9794
0
        everdaux = ((Elf_External_Verdaux *)
9795
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9796
0
      }
9797
9798
0
    iverdef->vd_nodename = NULL;
9799
0
    if (iverdef->vd_cnt)
9800
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9801
9802
0
    iverdef->vd_nextdef = NULL;
9803
0
    if (iverdef->vd_next == 0)
9804
0
      break;
9805
0
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9806
0
      iverdef->vd_nextdef = iverdef + 1;
9807
9808
0
    everdef = ((Elf_External_Verdef *)
9809
0
         ((bfd_byte *) everdef + iverdef->vd_next));
9810
0
  }
9811
9812
0
      if (contents != elf_tdata (abfd)->dt_verdef)
9813
0
  _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9814
0
      contents = NULL;
9815
0
      contents_addr = NULL;
9816
0
    }
9817
114
  else if (default_imported_symver)
9818
0
    {
9819
0
      if (freeidx < 3)
9820
0
  freeidx = 3;
9821
0
      else
9822
0
  freeidx++;
9823
9824
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9825
0
  {
9826
0
    bfd_set_error (bfd_error_file_too_big);
9827
0
    goto error_return;
9828
0
  }
9829
0
      if (amt == 0)
9830
0
  goto error_return;
9831
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9832
0
      if (elf_tdata (abfd)->verdef == NULL)
9833
0
  goto error_return;
9834
9835
0
      elf_tdata (abfd)->cverdefs = freeidx;
9836
0
    }
9837
9838
  /* Create a default version based on the soname.  */
9839
114
  if (default_imported_symver)
9840
0
    {
9841
0
      Elf_Internal_Verdef *iverdef;
9842
0
      Elf_Internal_Verdaux *iverdaux;
9843
9844
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9845
9846
0
      iverdef->vd_version = VER_DEF_CURRENT;
9847
0
      iverdef->vd_flags = 0;
9848
0
      iverdef->vd_ndx = freeidx;
9849
0
      iverdef->vd_cnt = 1;
9850
9851
0
      iverdef->vd_bfd = abfd;
9852
9853
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9854
0
      if (iverdef->vd_nodename == NULL)
9855
0
  goto error_return_verdef;
9856
0
      iverdef->vd_nextdef = NULL;
9857
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9858
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9859
0
      if (iverdef->vd_auxptr == NULL)
9860
0
  goto error_return_verdef;
9861
9862
0
      iverdaux = iverdef->vd_auxptr;
9863
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9864
0
    }
9865
9866
114
  return true;
9867
9868
52
 error_return:
9869
52
  if (contents != elf_tdata (abfd)->dt_verneed
9870
52
      && contents != elf_tdata (abfd)->dt_verdef)
9871
48
    _bfd_munmap_readonly_temporary (contents_addr, contents_size);
9872
52
  return false;
9873
114
}
9874

9875
asymbol *
9876
_bfd_elf_make_empty_symbol (bfd *abfd)
9877
2.56M
{
9878
2.56M
  elf_symbol_type *newsym;
9879
9880
2.56M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9881
2.56M
  if (!newsym)
9882
0
    return NULL;
9883
2.56M
  newsym->symbol.the_bfd = abfd;
9884
2.56M
  return &newsym->symbol;
9885
2.56M
}
9886
9887
void
9888
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9889
        asymbol *symbol,
9890
        symbol_info *ret)
9891
150k
{
9892
150k
  bfd_symbol_info (symbol, ret);
9893
150k
}
9894
9895
/* Return whether a symbol name implies a local symbol.  Most targets
9896
   use this function for the is_local_label_name entry point, but some
9897
   override it.  */
9898
9899
bool
9900
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9901
            const char *name)
9902
337k
{
9903
  /* Normal local symbols start with ``.L''.  */
9904
337k
  if (name[0] == '.' && name[1] == 'L')
9905
19.3k
    return true;
9906
9907
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9908
     DWARF debugging symbols starting with ``..''.  */
9909
318k
  if (name[0] == '.' && name[1] == '.')
9910
674
    return true;
9911
9912
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9913
     emitting DWARF debugging output.  I suspect this is actually a
9914
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9915
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9916
     underscore to be emitted on some ELF targets).  For ease of use,
9917
     we treat such symbols as local.  */
9918
317k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9919
0
    return true;
9920
9921
  /* Treat assembler generated fake symbols, dollar local labels and
9922
     forward-backward labels (aka local labels) as locals.
9923
     These labels have the form:
9924
9925
       L0^A.*              (fake symbols)
9926
9927
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9928
9929
     Versions which start with .L will have already been matched above,
9930
     so we only need to match the rest.  */
9931
317k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9932
8
    {
9933
8
      bool ret = false;
9934
8
      const char * p;
9935
8
      char c;
9936
9937
13
      for (p = name + 2; (c = *p); p++)
9938
6
  {
9939
6
    if (c == 1 || c == 2)
9940
0
      {
9941
0
        if (c == 1 && p == name + 2)
9942
    /* A fake symbol.  */
9943
0
    return true;
9944
9945
        /* FIXME: We are being paranoid here and treating symbols like
9946
     L0^Bfoo as if there were non-local, on the grounds that the
9947
     assembler will never generate them.  But can any symbol
9948
     containing an ASCII value in the range 1-31 ever be anything
9949
     other than some kind of local ?  */
9950
0
        ret = true;
9951
0
      }
9952
9953
6
    if (! ISDIGIT (c))
9954
1
      {
9955
1
        ret = false;
9956
1
        break;
9957
1
      }
9958
6
  }
9959
8
      return ret;
9960
8
    }
9961
9962
317k
  return false;
9963
317k
}
9964
9965
alent *
9966
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9967
         asymbol *symbol ATTRIBUTE_UNUSED)
9968
0
{
9969
0
  abort ();
9970
0
  return NULL;
9971
0
}
9972
9973
bool
9974
_bfd_elf_set_arch_mach (bfd *abfd,
9975
      enum bfd_architecture arch,
9976
      unsigned long machine)
9977
770
{
9978
  /* If this isn't the right architecture for this backend, and this
9979
     isn't the generic backend, fail.  */
9980
770
  if (arch != get_elf_backend_data (abfd)->arch
9981
770
      && arch != bfd_arch_unknown
9982
770
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9983
0
    return false;
9984
9985
770
  return bfd_default_set_arch_mach (abfd, arch, machine);
9986
770
}
9987
9988
/* Find the nearest line to a particular section and offset,
9989
   for error reporting.  */
9990
9991
bool
9992
_bfd_elf_find_nearest_line (bfd *abfd,
9993
          asymbol **symbols,
9994
          asection *section,
9995
          bfd_vma offset,
9996
          const char **filename_ptr,
9997
          const char **functionname_ptr,
9998
          unsigned int *line_ptr,
9999
          unsigned int *discriminator_ptr)
10000
133k
{
10001
133k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
10002
133k
                offset, filename_ptr,
10003
133k
                functionname_ptr, line_ptr,
10004
133k
                discriminator_ptr);
10005
133k
}
10006
10007
/* Find the nearest line to a particular section and offset,
10008
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
10009
   can be optionally specified.  */
10010
10011
bool
10012
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
10013
             const char *alt_filename,
10014
             asymbol **symbols,
10015
             asection *section,
10016
             bfd_vma offset,
10017
             const char **filename_ptr,
10018
             const char **functionname_ptr,
10019
             unsigned int *line_ptr,
10020
             unsigned int *discriminator_ptr)
10021
133k
{
10022
133k
  bool found;
10023
10024
133k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
10025
133k
                section, offset, filename_ptr,
10026
133k
                functionname_ptr, line_ptr,
10027
133k
                discriminator_ptr,
10028
133k
                dwarf_debug_sections,
10029
133k
                &elf_tdata (abfd)->dwarf2_find_line_info))
10030
21.2k
    return true;
10031
10032
112k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
10033
112k
             filename_ptr, functionname_ptr, line_ptr))
10034
0
    {
10035
0
      if (!*functionname_ptr)
10036
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
10037
0
        *filename_ptr ? NULL : filename_ptr,
10038
0
        functionname_ptr);
10039
0
      return true;
10040
0
    }
10041
10042
112k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
10043
112k
               &found, filename_ptr,
10044
112k
               functionname_ptr, line_ptr,
10045
112k
               &elf_tdata (abfd)->line_info))
10046
0
    return false;
10047
112k
  if (found && (*functionname_ptr || *line_ptr))
10048
0
    return true;
10049
10050
112k
  if (symbols == NULL)
10051
1.58k
    return false;
10052
10053
110k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
10054
110k
        filename_ptr, functionname_ptr))
10055
97.4k
    return false;
10056
10057
13.3k
  *line_ptr = 0;
10058
13.3k
  return true;
10059
110k
}
10060
10061
/* Find the line for a symbol.  */
10062
10063
bool
10064
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
10065
        const char **filename_ptr, unsigned int *line_ptr)
10066
34.6k
{
10067
34.6k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
10068
34.6k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
10069
34.6k
          filename_ptr, NULL, line_ptr, NULL,
10070
34.6k
          dwarf_debug_sections,
10071
34.6k
          &tdata->dwarf2_find_line_info);
10072
34.6k
}
10073
10074
/* After a call to bfd_find_nearest_line, successive calls to
10075
   bfd_find_inliner_info can be used to get source information about
10076
   each level of function inlining that terminated at the address
10077
   passed to bfd_find_nearest_line.  Currently this is only supported
10078
   for DWARF2 with appropriate DWARF3 extensions. */
10079
10080
bool
10081
_bfd_elf_find_inliner_info (bfd *abfd,
10082
          const char **filename_ptr,
10083
          const char **functionname_ptr,
10084
          unsigned int *line_ptr)
10085
0
{
10086
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
10087
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10088
0
          functionname_ptr, line_ptr,
10089
0
          &tdata->dwarf2_find_line_info);
10090
0
}
10091
10092
int
10093
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10094
0
{
10095
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10096
0
  int ret = bed->s->sizeof_ehdr;
10097
10098
0
  if (!bfd_link_relocatable (info))
10099
0
    {
10100
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10101
10102
0
      if (phdr_size == (bfd_size_type) -1)
10103
0
  {
10104
0
    struct elf_segment_map *m;
10105
10106
0
    phdr_size = 0;
10107
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10108
0
      phdr_size += bed->s->sizeof_phdr;
10109
10110
0
    if (phdr_size == 0)
10111
0
      phdr_size = get_program_header_size (abfd, info);
10112
0
  }
10113
10114
0
      elf_program_header_size (abfd) = phdr_size;
10115
0
      ret += phdr_size;
10116
0
    }
10117
10118
0
  return ret;
10119
0
}
10120
10121
bool
10122
_bfd_elf_set_section_contents (bfd *abfd,
10123
             sec_ptr section,
10124
             const void *location,
10125
             file_ptr offset,
10126
             bfd_size_type count)
10127
912
{
10128
912
  Elf_Internal_Shdr *hdr;
10129
10130
912
  if (! abfd->output_has_begun
10131
912
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10132
1
    return false;
10133
10134
911
  if (!count)
10135
0
    return true;
10136
10137
911
  hdr = &elf_section_data (section)->this_hdr;
10138
911
  if (hdr->sh_offset == (file_ptr) -1)
10139
0
    {
10140
0
      unsigned char *contents;
10141
10142
0
      if (bfd_section_is_ctf (section))
10143
  /* Nothing to do with this section: the contents are generated
10144
     later.  */
10145
0
  return true;
10146
10147
0
      if ((offset + count) > hdr->sh_size)
10148
0
  {
10149
0
    _bfd_error_handler
10150
0
      (_("%pB:%pA: error: attempting to write"
10151
0
         " over the end of the section"),
10152
0
       abfd, section);
10153
10154
0
    bfd_set_error (bfd_error_invalid_operation);
10155
0
    return false;
10156
0
  }
10157
10158
0
      contents = hdr->contents;
10159
0
      if (contents == NULL)
10160
0
  {
10161
0
    _bfd_error_handler
10162
0
      (_("%pB:%pA: error: attempting to write"
10163
0
         " section into an empty buffer"),
10164
0
       abfd, section);
10165
10166
0
    bfd_set_error (bfd_error_invalid_operation);
10167
0
    return false;
10168
0
  }
10169
10170
0
      memcpy (contents + offset, location, count);
10171
0
      return true;
10172
0
    }
10173
10174
911
  return _bfd_generic_set_section_contents (abfd, section,
10175
911
              location, offset, count);
10176
911
}
10177
10178
bool
10179
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10180
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10181
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10182
0
{
10183
0
  abort ();
10184
0
  return false;
10185
0
}
10186
10187
/* Try to convert a non-ELF reloc into an ELF one.  */
10188
10189
bool
10190
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10191
0
{
10192
  /* Check whether we really have an ELF howto.  */
10193
10194
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10195
0
    {
10196
0
      bfd_reloc_code_real_type code;
10197
0
      reloc_howto_type *howto;
10198
10199
      /* Alien reloc: Try to determine its type to replace it with an
10200
   equivalent ELF reloc.  */
10201
10202
0
      if (areloc->howto->pc_relative)
10203
0
  {
10204
0
    switch (areloc->howto->bitsize)
10205
0
      {
10206
0
      case 8:
10207
0
        code = BFD_RELOC_8_PCREL;
10208
0
        break;
10209
0
      case 12:
10210
0
        code = BFD_RELOC_12_PCREL;
10211
0
        break;
10212
0
      case 16:
10213
0
        code = BFD_RELOC_16_PCREL;
10214
0
        break;
10215
0
      case 24:
10216
0
        code = BFD_RELOC_24_PCREL;
10217
0
        break;
10218
0
      case 32:
10219
0
        code = BFD_RELOC_32_PCREL;
10220
0
        break;
10221
0
      case 64:
10222
0
        code = BFD_RELOC_64_PCREL;
10223
0
        break;
10224
0
      default:
10225
0
        goto fail;
10226
0
      }
10227
10228
0
    howto = bfd_reloc_type_lookup (abfd, code);
10229
10230
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10231
0
      {
10232
0
        if (howto->pcrel_offset)
10233
0
    areloc->addend += areloc->address;
10234
0
        else
10235
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10236
0
      }
10237
0
  }
10238
0
      else
10239
0
  {
10240
0
    switch (areloc->howto->bitsize)
10241
0
      {
10242
0
      case 8:
10243
0
        code = BFD_RELOC_8;
10244
0
        break;
10245
0
      case 14:
10246
0
        code = BFD_RELOC_14;
10247
0
        break;
10248
0
      case 16:
10249
0
        code = BFD_RELOC_16;
10250
0
        break;
10251
0
      case 26:
10252
0
        code = BFD_RELOC_26;
10253
0
        break;
10254
0
      case 32:
10255
0
        code = BFD_RELOC_32;
10256
0
        break;
10257
0
      case 64:
10258
0
        code = BFD_RELOC_64;
10259
0
        break;
10260
0
      default:
10261
0
        goto fail;
10262
0
      }
10263
10264
0
    howto = bfd_reloc_type_lookup (abfd, code);
10265
0
  }
10266
10267
0
      if (howto)
10268
0
  areloc->howto = howto;
10269
0
      else
10270
0
  goto fail;
10271
0
    }
10272
10273
0
  return true;
10274
10275
0
 fail:
10276
  /* xgettext:c-format */
10277
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10278
0
          abfd, areloc->howto->name);
10279
0
  bfd_set_error (bfd_error_sorry);
10280
0
  return false;
10281
0
}
10282
10283
bool
10284
_bfd_elf_free_cached_info (bfd *abfd)
10285
614k
{
10286
614k
  struct elf_obj_tdata *tdata;
10287
10288
614k
  if ((bfd_get_format (abfd) == bfd_object
10289
614k
       || bfd_get_format (abfd) == bfd_core)
10290
614k
      && (tdata = elf_tdata (abfd)) != NULL)
10291
237k
    {
10292
237k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10293
28
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10294
237k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10295
237k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10296
237k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10297
237k
    }
10298
10299
614k
  return _bfd_generic_bfd_free_cached_info (abfd);
10300
614k
}
10301
10302
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10303
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10304
   range-checking to interfere.  There is nothing else to do in processing
10305
   this reloc.  */
10306
10307
bfd_reloc_status_type
10308
_bfd_elf_rel_vtable_reloc_fn
10309
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10310
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10311
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10312
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10313
4
{
10314
4
  return bfd_reloc_ok;
10315
4
}
10316

10317
/* Elf core file support.  Much of this only works on native
10318
   toolchains, since we rely on knowing the
10319
   machine-dependent procfs structure in order to pick
10320
   out details about the corefile.  */
10321
10322
#ifdef HAVE_SYS_PROCFS_H
10323
# include <sys/procfs.h>
10324
#endif
10325
10326
/* Return a PID that identifies a "thread" for threaded cores, or the
10327
   PID of the main process for non-threaded cores.  */
10328
10329
static int
10330
elfcore_make_pid (bfd *abfd)
10331
0
{
10332
0
  int pid;
10333
10334
0
  pid = elf_tdata (abfd)->core->lwpid;
10335
0
  if (pid == 0)
10336
0
    pid = elf_tdata (abfd)->core->pid;
10337
10338
0
  return pid;
10339
0
}
10340
10341
/* If there isn't a section called NAME, make one, using data from
10342
   SECT.  Note, this function will generate a reference to NAME, so
10343
   you shouldn't deallocate or overwrite it.  */
10344
10345
static bool
10346
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10347
0
{
10348
0
  asection *sect2;
10349
10350
0
  if (bfd_get_section_by_name (abfd, name) != NULL)
10351
0
    return true;
10352
10353
0
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10354
0
  if (sect2 == NULL)
10355
0
    return false;
10356
10357
0
  sect2->size = sect->size;
10358
0
  sect2->filepos = sect->filepos;
10359
0
  sect2->alignment_power = sect->alignment_power;
10360
0
  return true;
10361
0
}
10362
10363
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10364
   actually creates up to two pseudosections:
10365
   - For the single-threaded case, a section named NAME, unless
10366
     such a section already exists.
10367
   - For the multi-threaded case, a section named "NAME/PID", where
10368
     PID is elfcore_make_pid (abfd).
10369
   Both pseudosections have identical contents.  */
10370
bool
10371
_bfd_elfcore_make_pseudosection (bfd *abfd,
10372
         char *name,
10373
         size_t size,
10374
         ufile_ptr filepos)
10375
0
{
10376
0
  char buf[100];
10377
0
  char *threaded_name;
10378
0
  size_t len;
10379
0
  asection *sect;
10380
10381
  /* Build the section name.  */
10382
10383
0
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10384
0
  len = strlen (buf) + 1;
10385
0
  threaded_name = (char *) bfd_alloc (abfd, len);
10386
0
  if (threaded_name == NULL)
10387
0
    return false;
10388
0
  memcpy (threaded_name, buf, len);
10389
10390
0
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10391
0
               SEC_HAS_CONTENTS);
10392
0
  if (sect == NULL)
10393
0
    return false;
10394
0
  sect->size = size;
10395
0
  sect->filepos = filepos;
10396
0
  sect->alignment_power = 2;
10397
10398
0
  return elfcore_maybe_make_sect (abfd, name, sect);
10399
0
}
10400
10401
static bool
10402
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10403
        size_t offs)
10404
0
{
10405
0
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10406
0
                   SEC_HAS_CONTENTS);
10407
10408
0
  if (sect == NULL)
10409
0
    return false;
10410
10411
0
  sect->size = note->descsz - offs;
10412
0
  sect->filepos = note->descpos + offs;
10413
0
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10414
10415
0
  return true;
10416
0
}
10417
10418
/* prstatus_t exists on:
10419
     solaris 2.5+
10420
     linux 2.[01] + glibc
10421
     unixware 4.2
10422
*/
10423
10424
#if defined (HAVE_PRSTATUS_T)
10425
10426
static bool
10427
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10428
4
{
10429
4
  size_t size;
10430
4
  int offset;
10431
10432
4
  if (note->descsz == sizeof (prstatus_t))
10433
0
    {
10434
0
      prstatus_t prstat;
10435
10436
0
      size = sizeof (prstat.pr_reg);
10437
0
      offset   = offsetof (prstatus_t, pr_reg);
10438
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10439
10440
      /* Do not overwrite the core signal if it
10441
   has already been set by another thread.  */
10442
0
      if (elf_tdata (abfd)->core->signal == 0)
10443
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10444
0
      if (elf_tdata (abfd)->core->pid == 0)
10445
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10446
10447
      /* pr_who exists on:
10448
   solaris 2.5+
10449
   unixware 4.2
10450
   pr_who doesn't exist on:
10451
   linux 2.[01]
10452
   */
10453
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10454
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10455
#else
10456
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10457
0
#endif
10458
0
    }
10459
4
#if defined (HAVE_PRSTATUS32_T)
10460
4
  else if (note->descsz == sizeof (prstatus32_t))
10461
0
    {
10462
      /* 64-bit host, 32-bit corefile */
10463
0
      prstatus32_t prstat;
10464
10465
0
      size = sizeof (prstat.pr_reg);
10466
0
      offset   = offsetof (prstatus32_t, pr_reg);
10467
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10468
10469
      /* Do not overwrite the core signal if it
10470
   has already been set by another thread.  */
10471
0
      if (elf_tdata (abfd)->core->signal == 0)
10472
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10473
0
      if (elf_tdata (abfd)->core->pid == 0)
10474
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10475
10476
      /* pr_who exists on:
10477
   solaris 2.5+
10478
   unixware 4.2
10479
   pr_who doesn't exist on:
10480
   linux 2.[01]
10481
   */
10482
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10483
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10484
#else
10485
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10486
0
#endif
10487
0
    }
10488
4
#endif /* HAVE_PRSTATUS32_T */
10489
4
  else
10490
4
    {
10491
      /* Fail - we don't know how to handle any other
10492
   note size (ie. data object type).  */
10493
4
      return true;
10494
4
    }
10495
10496
  /* Make a ".reg/999" section and a ".reg" section.  */
10497
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10498
0
            size, note->descpos + offset);
10499
4
}
10500
#endif /* defined (HAVE_PRSTATUS_T) */
10501
10502
/* Create a pseudosection containing the exact contents of NOTE.  */
10503
static bool
10504
elfcore_make_note_pseudosection (bfd *abfd,
10505
         char *name,
10506
         Elf_Internal_Note *note)
10507
0
{
10508
0
  return _bfd_elfcore_make_pseudosection (abfd, name,
10509
0
            note->descsz, note->descpos);
10510
0
}
10511
10512
/* There isn't a consistent prfpregset_t across platforms,
10513
   but it doesn't matter, because we don't have to pick this
10514
   data structure apart.  */
10515
10516
static bool
10517
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10518
0
{
10519
0
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10520
0
}
10521
10522
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10523
   type of NT_PRXFPREG.  Just include the whole note's contents
10524
   literally.  */
10525
10526
static bool
10527
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10528
0
{
10529
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10530
0
}
10531
10532
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10533
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10534
   contents literally.  */
10535
10536
static bool
10537
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10538
0
{
10539
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10540
0
}
10541
10542
static bool
10543
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10544
0
{
10545
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10546
0
}
10547
10548
static bool
10549
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10550
0
{
10551
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10552
0
}
10553
10554
static bool
10555
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10556
0
{
10557
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10558
0
}
10559
10560
static bool
10561
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10562
0
{
10563
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10564
0
}
10565
10566
static bool
10567
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10568
0
{
10569
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10570
0
}
10571
10572
static bool
10573
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10574
0
{
10575
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10576
0
}
10577
10578
static bool
10579
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10580
0
{
10581
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10582
0
}
10583
10584
static bool
10585
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10586
0
{
10587
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10588
0
}
10589
10590
static bool
10591
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10592
0
{
10593
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10594
0
}
10595
10596
static bool
10597
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10598
0
{
10599
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10600
0
}
10601
10602
static bool
10603
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10604
0
{
10605
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10606
0
}
10607
10608
static bool
10609
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10610
0
{
10611
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10612
0
}
10613
10614
static bool
10615
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10616
0
{
10617
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10618
0
}
10619
10620
static bool
10621
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10622
0
{
10623
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10624
0
}
10625
10626
static bool
10627
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10628
0
{
10629
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10630
0
}
10631
10632
static bool
10633
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10634
0
{
10635
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10636
0
}
10637
10638
static bool
10639
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10640
0
{
10641
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10642
0
}
10643
10644
static bool
10645
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10646
0
{
10647
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10648
0
}
10649
10650
static bool
10651
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10652
0
{
10653
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10654
0
}
10655
10656
static bool
10657
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10658
0
{
10659
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10660
0
}
10661
10662
static bool
10663
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10664
0
{
10665
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10666
0
}
10667
10668
static bool
10669
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10670
0
{
10671
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10672
0
}
10673
10674
static bool
10675
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10676
0
{
10677
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10678
0
}
10679
10680
static bool
10681
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10682
0
{
10683
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10684
0
}
10685
10686
static bool
10687
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10688
0
{
10689
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10690
0
}
10691
10692
static bool
10693
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10694
0
{
10695
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10696
0
}
10697
10698
static bool
10699
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10700
0
{
10701
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10702
0
}
10703
10704
static bool
10705
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10706
0
{
10707
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10708
0
}
10709
10710
static bool
10711
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10712
0
{
10713
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10714
0
}
10715
10716
static bool
10717
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10718
0
{
10719
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10720
0
}
10721
10722
static bool
10723
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10724
0
{
10725
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10726
0
}
10727
10728
static bool
10729
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10730
0
{
10731
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10732
0
}
10733
10734
static bool
10735
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10736
0
{
10737
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10738
0
}
10739
10740
static bool
10741
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10742
0
{
10743
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10744
0
}
10745
10746
static bool
10747
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10748
0
{
10749
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10750
0
            note);
10751
0
}
10752
10753
static bool
10754
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10755
0
{
10756
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10757
0
}
10758
10759
static bool
10760
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10761
0
{
10762
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10763
0
}
10764
10765
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10766
   successful, otherwise return FALSE.  */
10767
10768
static bool
10769
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10770
0
{
10771
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10772
0
}
10773
10774
static bool
10775
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10776
0
{
10777
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10778
0
}
10779
10780
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10781
   successful otherwise, return FALSE.  */
10782
10783
static bool
10784
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10785
0
{
10786
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10787
0
}
10788
10789
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10790
   successful otherwise, return FALSE.  */
10791
10792
static bool
10793
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10794
0
{
10795
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10796
0
}
10797
10798
static bool
10799
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10800
0
{
10801
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10802
0
}
10803
10804
static bool
10805
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10806
0
{
10807
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10808
0
}
10809
10810
static bool
10811
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10812
0
{
10813
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10814
0
}
10815
10816
static bool
10817
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10818
0
{
10819
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10820
0
}
10821
10822
#if defined (HAVE_PRPSINFO_T)
10823
typedef prpsinfo_t   elfcore_psinfo_t;
10824
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10825
typedef prpsinfo32_t elfcore_psinfo32_t;
10826
#endif
10827
#endif
10828
10829
#if defined (HAVE_PSINFO_T)
10830
typedef psinfo_t   elfcore_psinfo_t;
10831
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10832
typedef psinfo32_t elfcore_psinfo32_t;
10833
#endif
10834
#endif
10835
10836
/* return a malloc'ed copy of a string at START which is at
10837
   most MAX bytes long, possibly without a terminating '\0'.
10838
   the copy will always have a terminating '\0'.  */
10839
10840
char *
10841
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10842
0
{
10843
0
  char *dups;
10844
0
  char *end = (char *) memchr (start, '\0', max);
10845
0
  size_t len;
10846
10847
0
  if (end == NULL)
10848
0
    len = max;
10849
0
  else
10850
0
    len = end - start;
10851
10852
0
  dups = (char *) bfd_alloc (abfd, len + 1);
10853
0
  if (dups == NULL)
10854
0
    return NULL;
10855
10856
0
  memcpy (dups, start, len);
10857
0
  dups[len] = '\0';
10858
10859
0
  return dups;
10860
0
}
10861
10862
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10863
static bool
10864
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10865
0
{
10866
0
  if (note->descsz == sizeof (elfcore_psinfo_t))
10867
0
    {
10868
0
      elfcore_psinfo_t psinfo;
10869
10870
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10871
10872
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10873
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10874
0
#endif
10875
0
      elf_tdata (abfd)->core->program
10876
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10877
0
        sizeof (psinfo.pr_fname));
10878
10879
0
      elf_tdata (abfd)->core->command
10880
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10881
0
        sizeof (psinfo.pr_psargs));
10882
0
    }
10883
0
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10884
0
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10885
0
    {
10886
      /* 64-bit host, 32-bit corefile */
10887
0
      elfcore_psinfo32_t psinfo;
10888
10889
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10890
10891
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10892
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10893
0
#endif
10894
0
      elf_tdata (abfd)->core->program
10895
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10896
0
        sizeof (psinfo.pr_fname));
10897
10898
0
      elf_tdata (abfd)->core->command
10899
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10900
0
        sizeof (psinfo.pr_psargs));
10901
0
    }
10902
0
#endif
10903
10904
0
  else
10905
0
    {
10906
      /* Fail - we don't know how to handle any other
10907
   note size (ie. data object type).  */
10908
0
      return true;
10909
0
    }
10910
10911
  /* Note that for some reason, a spurious space is tacked
10912
     onto the end of the args in some (at least one anyway)
10913
     implementations, so strip it off if it exists.  */
10914
10915
0
  {
10916
0
    char *command = elf_tdata (abfd)->core->command;
10917
0
    int n = strlen (command);
10918
10919
0
    if (0 < n && command[n - 1] == ' ')
10920
0
      command[n - 1] = '\0';
10921
0
  }
10922
10923
0
  return true;
10924
0
}
10925
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10926
10927
#if defined (HAVE_PSTATUS_T)
10928
static bool
10929
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10930
{
10931
  if (note->descsz == sizeof (pstatus_t)
10932
#if defined (HAVE_PXSTATUS_T)
10933
      || note->descsz == sizeof (pxstatus_t)
10934
#endif
10935
      )
10936
    {
10937
      pstatus_t pstat;
10938
10939
      memcpy (&pstat, note->descdata, sizeof (pstat));
10940
10941
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10942
    }
10943
#if defined (HAVE_PSTATUS32_T)
10944
  else if (note->descsz == sizeof (pstatus32_t))
10945
    {
10946
      /* 64-bit host, 32-bit corefile */
10947
      pstatus32_t pstat;
10948
10949
      memcpy (&pstat, note->descdata, sizeof (pstat));
10950
10951
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10952
    }
10953
#endif
10954
  /* Could grab some more details from the "representative"
10955
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10956
     NT_LWPSTATUS note, presumably.  */
10957
10958
  return true;
10959
}
10960
#endif /* defined (HAVE_PSTATUS_T) */
10961
10962
#if defined (HAVE_LWPSTATUS_T)
10963
static bool
10964
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10965
{
10966
  lwpstatus_t lwpstat;
10967
  char buf[100];
10968
  char *name;
10969
  size_t len;
10970
  asection *sect;
10971
10972
  if (note->descsz != sizeof (lwpstat)
10973
#if defined (HAVE_LWPXSTATUS_T)
10974
      && note->descsz != sizeof (lwpxstatus_t)
10975
#endif
10976
      )
10977
    return true;
10978
10979
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10980
10981
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10982
  /* Do not overwrite the core signal if it has already been set by
10983
     another thread.  */
10984
  if (elf_tdata (abfd)->core->signal == 0)
10985
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10986
10987
  /* Make a ".reg/999" section.  */
10988
10989
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10990
  len = strlen (buf) + 1;
10991
  name = bfd_alloc (abfd, len);
10992
  if (name == NULL)
10993
    return false;
10994
  memcpy (name, buf, len);
10995
10996
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10997
  if (sect == NULL)
10998
    return false;
10999
11000
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11001
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
11002
  sect->filepos = note->descpos
11003
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
11004
#endif
11005
11006
#if defined (HAVE_LWPSTATUS_T_PR_REG)
11007
  sect->size = sizeof (lwpstat.pr_reg);
11008
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
11009
#endif
11010
11011
  sect->alignment_power = 2;
11012
11013
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
11014
    return false;
11015
11016
  /* Make a ".reg2/999" section */
11017
11018
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
11019
  len = strlen (buf) + 1;
11020
  name = bfd_alloc (abfd, len);
11021
  if (name == NULL)
11022
    return false;
11023
  memcpy (name, buf, len);
11024
11025
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11026
  if (sect == NULL)
11027
    return false;
11028
11029
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11030
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11031
  sect->filepos = note->descpos
11032
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11033
#endif
11034
11035
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11036
  sect->size = sizeof (lwpstat.pr_fpreg);
11037
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11038
#endif
11039
11040
  sect->alignment_power = 2;
11041
11042
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
11043
}
11044
#endif /* defined (HAVE_LWPSTATUS_T) */
11045
11046
/* These constants, and the structure offsets used below, are defined by
11047
   Cygwin's core_dump.h */
11048
0
#define NOTE_INFO_PROCESS  1
11049
0
#define NOTE_INFO_THREAD   2
11050
0
#define NOTE_INFO_MODULE   3
11051
0
#define NOTE_INFO_MODULE64 4
11052
11053
static bool
11054
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11055
0
{
11056
0
  char buf[30];
11057
0
  char *name;
11058
0
  size_t len;
11059
0
  unsigned int name_size;
11060
0
  asection *sect;
11061
0
  unsigned int type;
11062
0
  int is_active_thread;
11063
0
  bfd_vma base_addr;
11064
11065
0
  if (note->descsz < 4)
11066
0
    return true;
11067
11068
0
  if (! startswith (note->namedata, "win32"))
11069
0
    return true;
11070
11071
0
  type = bfd_get_32 (abfd, note->descdata);
11072
11073
0
  struct
11074
0
  {
11075
0
    const char *type_name;
11076
0
    unsigned long min_size;
11077
0
  } size_check[] =
11078
0
      {
11079
0
       { "NOTE_INFO_PROCESS", 12 },
11080
0
       { "NOTE_INFO_THREAD", 12 },
11081
0
       { "NOTE_INFO_MODULE", 12 },
11082
0
       { "NOTE_INFO_MODULE64", 16 },
11083
0
      };
11084
11085
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11086
0
      return true;
11087
11088
0
  if (note->descsz < size_check[type - 1].min_size)
11089
0
    {
11090
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11091
0
          " is too small"),
11092
0
        abfd, size_check[type - 1].type_name, note->descsz);
11093
0
      return true;
11094
0
    }
11095
11096
0
  switch (type)
11097
0
    {
11098
0
    case NOTE_INFO_PROCESS:
11099
      /* FIXME: need to add ->core->command.  */
11100
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11101
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11102
0
      break;
11103
11104
0
    case NOTE_INFO_THREAD:
11105
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11106
   structure. */
11107
      /* thread_info.tid */
11108
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11109
11110
0
      len = strlen (buf) + 1;
11111
0
      name = (char *) bfd_alloc (abfd, len);
11112
0
      if (name == NULL)
11113
0
  return false;
11114
11115
0
      memcpy (name, buf, len);
11116
11117
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11118
0
      if (sect == NULL)
11119
0
  return false;
11120
11121
      /* sizeof (thread_info.thread_context) */
11122
0
      sect->size = note->descsz - 12;
11123
      /* offsetof (thread_info.thread_context) */
11124
0
      sect->filepos = note->descpos + 12;
11125
0
      sect->alignment_power = 2;
11126
11127
      /* thread_info.is_active_thread */
11128
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11129
11130
0
      if (is_active_thread)
11131
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11132
0
    return false;
11133
0
      break;
11134
11135
0
    case NOTE_INFO_MODULE:
11136
0
    case NOTE_INFO_MODULE64:
11137
      /* Make a ".module/xxxxxxxx" section.  */
11138
0
      if (type == NOTE_INFO_MODULE)
11139
0
  {
11140
    /* module_info.base_address */
11141
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11142
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11143
    /* module_info.module_name_size */
11144
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11145
0
  }
11146
0
      else /* NOTE_INFO_MODULE64 */
11147
0
  {
11148
    /* module_info.base_address */
11149
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11150
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11151
    /* module_info.module_name_size */
11152
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11153
0
  }
11154
11155
0
      len = strlen (buf) + 1;
11156
0
      name = (char *) bfd_alloc (abfd, len);
11157
0
      if (name == NULL)
11158
0
  return false;
11159
11160
0
      memcpy (name, buf, len);
11161
11162
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11163
11164
0
      if (sect == NULL)
11165
0
  return false;
11166
11167
0
      if (note->descsz < 12 + name_size)
11168
0
  {
11169
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11170
0
        " is too small to contain a name of size %u"),
11171
0
            abfd, note->descsz, name_size);
11172
0
    return true;
11173
0
  }
11174
11175
0
      sect->size = note->descsz;
11176
0
      sect->filepos = note->descpos;
11177
0
      sect->alignment_power = 2;
11178
0
      break;
11179
11180
0
    default:
11181
0
      return true;
11182
0
    }
11183
11184
0
  return true;
11185
0
}
11186
11187
static bool
11188
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11189
76
{
11190
76
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11191
11192
76
  switch (note->type)
11193
76
    {
11194
63
    default:
11195
63
      return true;
11196
11197
4
    case NT_PRSTATUS:
11198
4
      if (bed->elf_backend_grok_prstatus)
11199
2
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11200
0
    return true;
11201
4
#if defined (HAVE_PRSTATUS_T)
11202
4
      return elfcore_grok_prstatus (abfd, note);
11203
#else
11204
      return true;
11205
#endif
11206
11207
#if defined (HAVE_PSTATUS_T)
11208
    case NT_PSTATUS:
11209
      return elfcore_grok_pstatus (abfd, note);
11210
#endif
11211
11212
#if defined (HAVE_LWPSTATUS_T)
11213
    case NT_LWPSTATUS:
11214
      return elfcore_grok_lwpstatus (abfd, note);
11215
#endif
11216
11217
0
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
11218
0
      return elfcore_grok_prfpreg (abfd, note);
11219
11220
0
    case NT_WIN32PSTATUS:
11221
0
      return elfcore_grok_win32pstatus (abfd, note);
11222
11223
0
    case NT_PRXFPREG:   /* Linux SSE extension */
11224
0
      if (note->namesz == 6
11225
0
    && strcmp (note->namedata, "LINUX") == 0)
11226
0
  return elfcore_grok_prxfpreg (abfd, note);
11227
0
      else
11228
0
  return true;
11229
11230
0
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
11231
0
      if (note->namesz == 6
11232
0
    && strcmp (note->namedata, "LINUX") == 0)
11233
0
  return elfcore_grok_xstatereg (abfd, note);
11234
0
      else
11235
0
  return true;
11236
11237
0
    case NT_PPC_VMX:
11238
0
      if (note->namesz == 6
11239
0
    && strcmp (note->namedata, "LINUX") == 0)
11240
0
  return elfcore_grok_ppc_vmx (abfd, note);
11241
0
      else
11242
0
  return true;
11243
11244
0
    case NT_PPC_VSX:
11245
0
      if (note->namesz == 6
11246
0
    && strcmp (note->namedata, "LINUX") == 0)
11247
0
  return elfcore_grok_ppc_vsx (abfd, note);
11248
0
      else
11249
0
  return true;
11250
11251
0
    case NT_PPC_TAR:
11252
0
      if (note->namesz == 6
11253
0
    && strcmp (note->namedata, "LINUX") == 0)
11254
0
  return elfcore_grok_ppc_tar (abfd, note);
11255
0
      else
11256
0
  return true;
11257
11258
0
    case NT_PPC_PPR:
11259
0
      if (note->namesz == 6
11260
0
    && strcmp (note->namedata, "LINUX") == 0)
11261
0
  return elfcore_grok_ppc_ppr (abfd, note);
11262
0
      else
11263
0
  return true;
11264
11265
0
    case NT_PPC_DSCR:
11266
0
      if (note->namesz == 6
11267
0
    && strcmp (note->namedata, "LINUX") == 0)
11268
0
  return elfcore_grok_ppc_dscr (abfd, note);
11269
0
      else
11270
0
  return true;
11271
11272
0
    case NT_PPC_EBB:
11273
0
      if (note->namesz == 6
11274
0
    && strcmp (note->namedata, "LINUX") == 0)
11275
0
  return elfcore_grok_ppc_ebb (abfd, note);
11276
0
      else
11277
0
  return true;
11278
11279
0
    case NT_PPC_PMU:
11280
0
      if (note->namesz == 6
11281
0
    && strcmp (note->namedata, "LINUX") == 0)
11282
0
  return elfcore_grok_ppc_pmu (abfd, note);
11283
0
      else
11284
0
  return true;
11285
11286
0
    case NT_PPC_TM_CGPR:
11287
0
      if (note->namesz == 6
11288
0
    && strcmp (note->namedata, "LINUX") == 0)
11289
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
11290
0
      else
11291
0
  return true;
11292
11293
0
    case NT_PPC_TM_CFPR:
11294
0
      if (note->namesz == 6
11295
0
    && strcmp (note->namedata, "LINUX") == 0)
11296
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
11297
0
      else
11298
0
  return true;
11299
11300
0
    case NT_PPC_TM_CVMX:
11301
0
      if (note->namesz == 6
11302
0
    && strcmp (note->namedata, "LINUX") == 0)
11303
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
11304
0
      else
11305
0
  return true;
11306
11307
0
    case NT_PPC_TM_CVSX:
11308
0
      if (note->namesz == 6
11309
0
    && strcmp (note->namedata, "LINUX") == 0)
11310
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
11311
0
      else
11312
0
  return true;
11313
11314
0
    case NT_PPC_TM_SPR:
11315
0
      if (note->namesz == 6
11316
0
    && strcmp (note->namedata, "LINUX") == 0)
11317
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
11318
0
      else
11319
0
  return true;
11320
11321
0
    case NT_PPC_TM_CTAR:
11322
0
      if (note->namesz == 6
11323
0
    && strcmp (note->namedata, "LINUX") == 0)
11324
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
11325
0
      else
11326
0
  return true;
11327
11328
0
    case NT_PPC_TM_CPPR:
11329
0
      if (note->namesz == 6
11330
0
    && strcmp (note->namedata, "LINUX") == 0)
11331
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
11332
0
      else
11333
0
  return true;
11334
11335
0
    case NT_PPC_TM_CDSCR:
11336
0
      if (note->namesz == 6
11337
0
    && strcmp (note->namedata, "LINUX") == 0)
11338
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
11339
0
      else
11340
0
  return true;
11341
11342
8
    case NT_S390_HIGH_GPRS:
11343
8
      if (note->namesz == 6
11344
8
    && strcmp (note->namedata, "LINUX") == 0)
11345
0
  return elfcore_grok_s390_high_gprs (abfd, note);
11346
8
      else
11347
8
  return true;
11348
11349
0
    case NT_S390_TIMER:
11350
0
      if (note->namesz == 6
11351
0
    && strcmp (note->namedata, "LINUX") == 0)
11352
0
  return elfcore_grok_s390_timer (abfd, note);
11353
0
      else
11354
0
  return true;
11355
11356
0
    case NT_S390_TODCMP:
11357
0
      if (note->namesz == 6
11358
0
    && strcmp (note->namedata, "LINUX") == 0)
11359
0
  return elfcore_grok_s390_todcmp (abfd, note);
11360
0
      else
11361
0
  return true;
11362
11363
0
    case NT_S390_TODPREG:
11364
0
      if (note->namesz == 6
11365
0
    && strcmp (note->namedata, "LINUX") == 0)
11366
0
  return elfcore_grok_s390_todpreg (abfd, note);
11367
0
      else
11368
0
  return true;
11369
11370
0
    case NT_S390_CTRS:
11371
0
      if (note->namesz == 6
11372
0
    && strcmp (note->namedata, "LINUX") == 0)
11373
0
  return elfcore_grok_s390_ctrs (abfd, note);
11374
0
      else
11375
0
  return true;
11376
11377
0
    case NT_S390_PREFIX:
11378
0
      if (note->namesz == 6
11379
0
    && strcmp (note->namedata, "LINUX") == 0)
11380
0
  return elfcore_grok_s390_prefix (abfd, note);
11381
0
      else
11382
0
  return true;
11383
11384
0
    case NT_S390_LAST_BREAK:
11385
0
      if (note->namesz == 6
11386
0
    && strcmp (note->namedata, "LINUX") == 0)
11387
0
  return elfcore_grok_s390_last_break (abfd, note);
11388
0
      else
11389
0
  return true;
11390
11391
0
    case NT_S390_SYSTEM_CALL:
11392
0
      if (note->namesz == 6
11393
0
    && strcmp (note->namedata, "LINUX") == 0)
11394
0
  return elfcore_grok_s390_system_call (abfd, note);
11395
0
      else
11396
0
  return true;
11397
11398
0
    case NT_S390_TDB:
11399
0
      if (note->namesz == 6
11400
0
    && strcmp (note->namedata, "LINUX") == 0)
11401
0
  return elfcore_grok_s390_tdb (abfd, note);
11402
0
      else
11403
0
  return true;
11404
11405
0
    case NT_S390_VXRS_LOW:
11406
0
      if (note->namesz == 6
11407
0
    && strcmp (note->namedata, "LINUX") == 0)
11408
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
11409
0
      else
11410
0
  return true;
11411
11412
0
    case NT_S390_VXRS_HIGH:
11413
0
      if (note->namesz == 6
11414
0
    && strcmp (note->namedata, "LINUX") == 0)
11415
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
11416
0
      else
11417
0
  return true;
11418
11419
0
    case NT_S390_GS_CB:
11420
0
      if (note->namesz == 6
11421
0
    && strcmp (note->namedata, "LINUX") == 0)
11422
0
  return elfcore_grok_s390_gs_cb (abfd, note);
11423
0
      else
11424
0
  return true;
11425
11426
0
    case NT_S390_GS_BC:
11427
0
      if (note->namesz == 6
11428
0
    && strcmp (note->namedata, "LINUX") == 0)
11429
0
  return elfcore_grok_s390_gs_bc (abfd, note);
11430
0
      else
11431
0
  return true;
11432
11433
0
    case NT_ARC_V2:
11434
0
      if (note->namesz == 6
11435
0
    && strcmp (note->namedata, "LINUX") == 0)
11436
0
  return elfcore_grok_arc_v2 (abfd, note);
11437
0
      else
11438
0
  return true;
11439
11440
0
    case NT_ARM_VFP:
11441
0
      if (note->namesz == 6
11442
0
    && strcmp (note->namedata, "LINUX") == 0)
11443
0
  return elfcore_grok_arm_vfp (abfd, note);
11444
0
      else
11445
0
  return true;
11446
11447
0
    case NT_ARM_TLS:
11448
0
      if (note->namesz == 6
11449
0
    && strcmp (note->namedata, "LINUX") == 0)
11450
0
  return elfcore_grok_aarch_tls (abfd, note);
11451
0
      else
11452
0
  return true;
11453
11454
0
    case NT_ARM_HW_BREAK:
11455
0
      if (note->namesz == 6
11456
0
    && strcmp (note->namedata, "LINUX") == 0)
11457
0
  return elfcore_grok_aarch_hw_break (abfd, note);
11458
0
      else
11459
0
  return true;
11460
11461
0
    case NT_ARM_HW_WATCH:
11462
0
      if (note->namesz == 6
11463
0
    && strcmp (note->namedata, "LINUX") == 0)
11464
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
11465
0
      else
11466
0
  return true;
11467
11468
0
    case NT_ARM_SVE:
11469
0
      if (note->namesz == 6
11470
0
    && strcmp (note->namedata, "LINUX") == 0)
11471
0
  return elfcore_grok_aarch_sve (abfd, note);
11472
0
      else
11473
0
  return true;
11474
11475
0
    case NT_ARM_PAC_MASK:
11476
0
      if (note->namesz == 6
11477
0
    && strcmp (note->namedata, "LINUX") == 0)
11478
0
  return elfcore_grok_aarch_pauth (abfd, note);
11479
0
      else
11480
0
  return true;
11481
11482
0
    case NT_ARM_TAGGED_ADDR_CTRL:
11483
0
      if (note->namesz == 6
11484
0
    && strcmp (note->namedata, "LINUX") == 0)
11485
0
  return elfcore_grok_aarch_mte (abfd, note);
11486
0
      else
11487
0
  return true;
11488
11489
0
    case NT_ARM_SSVE:
11490
0
      if (note->namesz == 6
11491
0
    && strcmp (note->namedata, "LINUX") == 0)
11492
0
  return elfcore_grok_aarch_ssve (abfd, note);
11493
0
      else
11494
0
  return true;
11495
11496
0
    case NT_ARM_ZA:
11497
0
      if (note->namesz == 6
11498
0
    && strcmp (note->namedata, "LINUX") == 0)
11499
0
  return elfcore_grok_aarch_za (abfd, note);
11500
0
      else
11501
0
  return true;
11502
11503
0
    case NT_ARM_ZT:
11504
0
      if (note->namesz == 6
11505
0
    && strcmp (note->namedata, "LINUX") == 0)
11506
0
  return elfcore_grok_aarch_zt (abfd, note);
11507
0
      else
11508
0
  return true;
11509
11510
0
    case NT_GDB_TDESC:
11511
0
      if (note->namesz == 4
11512
0
    && strcmp (note->namedata, "GDB") == 0)
11513
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11514
0
      else
11515
0
  return true;
11516
11517
1
    case NT_RISCV_CSR:
11518
1
      if (note->namesz == 4
11519
1
    && strcmp (note->namedata, "GDB") == 0)
11520
0
  return elfcore_grok_riscv_csr (abfd, note);
11521
1
      else
11522
1
  return true;
11523
11524
0
    case NT_LARCH_CPUCFG:
11525
0
      if (note->namesz == 6
11526
0
    && strcmp (note->namedata, "LINUX") == 0)
11527
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
11528
0
      else
11529
0
  return true;
11530
11531
0
    case NT_LARCH_LBT:
11532
0
      if (note->namesz == 6
11533
0
    && strcmp (note->namedata, "LINUX") == 0)
11534
0
  return elfcore_grok_loongarch_lbt (abfd, note);
11535
0
      else
11536
0
  return true;
11537
11538
0
    case NT_LARCH_LSX:
11539
0
      if (note->namesz == 6
11540
0
    && strcmp (note->namedata, "LINUX") == 0)
11541
0
  return elfcore_grok_loongarch_lsx (abfd, note);
11542
0
      else
11543
0
  return true;
11544
11545
0
    case NT_LARCH_LASX:
11546
0
      if (note->namesz == 6
11547
0
    && strcmp (note->namedata, "LINUX") == 0)
11548
0
  return elfcore_grok_loongarch_lasx (abfd, note);
11549
0
      else
11550
0
  return true;
11551
11552
0
    case NT_PRPSINFO:
11553
0
    case NT_PSINFO:
11554
0
      if (bed->elf_backend_grok_psinfo)
11555
0
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11556
0
    return true;
11557
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11558
0
      return elfcore_grok_psinfo (abfd, note);
11559
#else
11560
      return true;
11561
#endif
11562
11563
0
    case NT_AUXV:
11564
0
      return elfcore_make_auxv_note_section (abfd, note, 0);
11565
11566
0
    case NT_FILE:
11567
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11568
0
                note);
11569
11570
0
    case NT_SIGINFO:
11571
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11572
0
                note);
11573
11574
76
    }
11575
76
}
11576
11577
static bool
11578
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11579
413
{
11580
413
  struct bfd_build_id* build_id;
11581
11582
413
  if (note->descsz == 0)
11583
29
    return false;
11584
11585
384
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11586
384
  if (build_id == NULL)
11587
0
    return false;
11588
11589
384
  build_id->size = note->descsz;
11590
384
  memcpy (build_id->data, note->descdata, note->descsz);
11591
384
  abfd->build_id = build_id;
11592
11593
384
  return true;
11594
384
}
11595
11596
static bool
11597
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11598
1.22k
{
11599
1.22k
  switch (note->type)
11600
1.22k
    {
11601
608
    default:
11602
608
      return true;
11603
11604
203
    case NT_GNU_PROPERTY_TYPE_0:
11605
203
      return _bfd_elf_parse_gnu_properties (abfd, note);
11606
11607
413
    case NT_GNU_BUILD_ID:
11608
413
      return elfobj_grok_gnu_build_id (abfd, note);
11609
1.22k
    }
11610
1.22k
}
11611
11612
static bool
11613
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11614
0
{
11615
0
  struct sdt_note *cur =
11616
0
    (struct sdt_note *) bfd_alloc (abfd,
11617
0
           sizeof (struct sdt_note) + note->descsz);
11618
11619
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11620
0
  cur->size = (bfd_size_type) note->descsz;
11621
0
  memcpy (cur->data, note->descdata, note->descsz);
11622
11623
0
  elf_tdata (abfd)->sdt_note_head = cur;
11624
11625
0
  return true;
11626
0
}
11627
11628
static bool
11629
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11630
0
{
11631
0
  switch (note->type)
11632
0
    {
11633
0
    case NT_STAPSDT:
11634
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11635
11636
0
    default:
11637
0
      return true;
11638
0
    }
11639
0
}
11640
11641
static bool
11642
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11643
0
{
11644
0
  size_t offset;
11645
11646
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11647
0
    {
11648
0
    case ELFCLASS32:
11649
0
      if (note->descsz < 108)
11650
0
  return false;
11651
0
      break;
11652
11653
0
    case ELFCLASS64:
11654
0
      if (note->descsz < 120)
11655
0
  return false;
11656
0
      break;
11657
11658
0
    default:
11659
0
      return false;
11660
0
    }
11661
11662
  /* Check for version 1 in pr_version.  */
11663
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11664
0
    return false;
11665
11666
0
  offset = 4;
11667
11668
  /* Skip over pr_psinfosz. */
11669
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11670
0
    offset += 4;
11671
0
  else
11672
0
    {
11673
0
      offset += 4;  /* Padding before pr_psinfosz. */
11674
0
      offset += 8;
11675
0
    }
11676
11677
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11678
0
  elf_tdata (abfd)->core->program
11679
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11680
0
  offset += 17;
11681
11682
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11683
0
  elf_tdata (abfd)->core->command
11684
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11685
0
  offset += 81;
11686
11687
  /* Padding before pr_pid.  */
11688
0
  offset += 2;
11689
11690
  /* The pr_pid field was added in version "1a".  */
11691
0
  if (note->descsz < offset + 4)
11692
0
    return true;
11693
11694
0
  elf_tdata (abfd)->core->pid
11695
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11696
11697
0
  return true;
11698
0
}
11699
11700
static bool
11701
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11702
0
{
11703
0
  size_t offset;
11704
0
  size_t size;
11705
0
  size_t min_size;
11706
11707
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11708
     Also compute minimum size of this note.  */
11709
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11710
0
    {
11711
0
    case ELFCLASS32:
11712
0
      offset = 4 + 4;
11713
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11714
0
      break;
11715
11716
0
    case ELFCLASS64:
11717
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11718
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11719
0
      break;
11720
11721
0
    default:
11722
0
      return false;
11723
0
    }
11724
11725
0
  if (note->descsz < min_size)
11726
0
    return false;
11727
11728
  /* Check for version 1 in pr_version.  */
11729
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11730
0
    return false;
11731
11732
  /* Extract size of pr_reg from pr_gregsetsz.  */
11733
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11734
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11735
0
    {
11736
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11737
0
      offset += 4 * 2;
11738
0
    }
11739
0
  else
11740
0
    {
11741
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11742
0
      offset += 8 * 2;
11743
0
    }
11744
11745
  /* Skip over pr_osreldate.  */
11746
0
  offset += 4;
11747
11748
  /* Read signal from pr_cursig.  */
11749
0
  if (elf_tdata (abfd)->core->signal == 0)
11750
0
    elf_tdata (abfd)->core->signal
11751
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11752
0
  offset += 4;
11753
11754
  /* Read TID from pr_pid.  */
11755
0
  elf_tdata (abfd)->core->lwpid
11756
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11757
0
  offset += 4;
11758
11759
  /* Padding before pr_reg.  */
11760
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11761
0
    offset += 4;
11762
11763
  /* Make sure that there is enough data remaining in the note.  */
11764
0
  if ((note->descsz - offset) < size)
11765
0
    return false;
11766
11767
  /* Make a ".reg/999" section and a ".reg" section.  */
11768
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11769
0
            size, note->descpos + offset);
11770
0
}
11771
11772
static bool
11773
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11774
0
{
11775
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11776
11777
0
  switch (note->type)
11778
0
    {
11779
0
    case NT_PRSTATUS:
11780
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11781
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11782
0
    return true;
11783
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11784
11785
0
    case NT_FPREGSET:
11786
0
      return elfcore_grok_prfpreg (abfd, note);
11787
11788
0
    case NT_PRPSINFO:
11789
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11790
11791
0
    case NT_FREEBSD_THRMISC:
11792
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11793
11794
0
    case NT_FREEBSD_PROCSTAT_PROC:
11795
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11796
0
                note);
11797
11798
0
    case NT_FREEBSD_PROCSTAT_FILES:
11799
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11800
0
                note);
11801
11802
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11803
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11804
0
                note);
11805
11806
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11807
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11808
11809
0
    case NT_FREEBSD_X86_SEGBASES:
11810
0
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11811
11812
0
    case NT_X86_XSTATE:
11813
0
      return elfcore_grok_xstatereg (abfd, note);
11814
11815
0
    case NT_FREEBSD_PTLWPINFO:
11816
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11817
0
                note);
11818
11819
0
    case NT_ARM_TLS:
11820
0
      return elfcore_grok_aarch_tls (abfd, note);
11821
11822
0
    case NT_ARM_VFP:
11823
0
      return elfcore_grok_arm_vfp (abfd, note);
11824
11825
0
    default:
11826
0
      return true;
11827
0
    }
11828
0
}
11829
11830
static bool
11831
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11832
0
{
11833
0
  char *cp;
11834
11835
0
  cp = strchr (note->namedata, '@');
11836
0
  if (cp != NULL)
11837
0
    {
11838
0
      *lwpidp = atoi(cp + 1);
11839
0
      return true;
11840
0
    }
11841
0
  return false;
11842
0
}
11843
11844
static bool
11845
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11846
0
{
11847
0
  if (note->descsz <= 0x7c + 31)
11848
0
    return false;
11849
11850
  /* Signal number at offset 0x08. */
11851
0
  elf_tdata (abfd)->core->signal
11852
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11853
11854
  /* Process ID at offset 0x50. */
11855
0
  elf_tdata (abfd)->core->pid
11856
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11857
11858
  /* Command name at 0x7c (max 32 bytes, including nul). */
11859
0
  elf_tdata (abfd)->core->command
11860
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11861
11862
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11863
0
            note);
11864
0
}
11865
11866
static bool
11867
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11868
0
{
11869
0
  int lwp;
11870
11871
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11872
0
    elf_tdata (abfd)->core->lwpid = lwp;
11873
11874
0
  switch (note->type)
11875
0
    {
11876
0
    case NT_NETBSDCORE_PROCINFO:
11877
      /* NetBSD-specific core "procinfo".  Note that we expect to
11878
   find this note before any of the others, which is fine,
11879
   since the kernel writes this note out first when it
11880
   creates a core file.  */
11881
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11882
0
    case NT_NETBSDCORE_AUXV:
11883
      /* NetBSD-specific Elf Auxiliary Vector data. */
11884
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11885
0
    case NT_NETBSDCORE_LWPSTATUS:
11886
0
      return elfcore_make_note_pseudosection (abfd,
11887
0
                ".note.netbsdcore.lwpstatus",
11888
0
                note);
11889
0
    default:
11890
0
      break;
11891
0
    }
11892
11893
  /* As of March 2020 there are no other machine-independent notes
11894
     defined for NetBSD core files.  If the note type is less
11895
     than the start of the machine-dependent note types, we don't
11896
     understand it.  */
11897
11898
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11899
0
    return true;
11900
11901
11902
0
  switch (bfd_get_arch (abfd))
11903
0
    {
11904
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11905
   PT_GETFPREGS == mach+2.  */
11906
11907
0
    case bfd_arch_aarch64:
11908
0
    case bfd_arch_alpha:
11909
0
    case bfd_arch_sparc:
11910
0
      switch (note->type)
11911
0
  {
11912
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11913
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11914
11915
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11916
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11917
11918
0
  default:
11919
0
    return true;
11920
0
  }
11921
11922
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11923
   There's also old PT___GETREGS40 == mach + 1 for old reg
11924
   structure which lacks GBR.  */
11925
11926
0
    case bfd_arch_sh:
11927
0
      switch (note->type)
11928
0
  {
11929
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11930
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11931
11932
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11933
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11934
11935
0
  default:
11936
0
    return true;
11937
0
  }
11938
11939
      /* On all other arch's, PT_GETREGS == mach+1 and
11940
   PT_GETFPREGS == mach+3.  */
11941
11942
0
    default:
11943
0
      switch (note->type)
11944
0
  {
11945
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11946
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11947
11948
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11949
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11950
11951
0
  default:
11952
0
    return true;
11953
0
  }
11954
0
    }
11955
    /* NOTREACHED */
11956
0
}
11957
11958
static bool
11959
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11960
0
{
11961
0
  if (note->descsz <= 0x48 + 31)
11962
0
    return false;
11963
11964
  /* Signal number at offset 0x08. */
11965
0
  elf_tdata (abfd)->core->signal
11966
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11967
11968
  /* Process ID at offset 0x20. */
11969
0
  elf_tdata (abfd)->core->pid
11970
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11971
11972
  /* Command name at 0x48 (max 32 bytes, including nul). */
11973
0
  elf_tdata (abfd)->core->command
11974
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11975
11976
0
  return true;
11977
0
}
11978
11979
/* Processes Solaris's process status note.
11980
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11981
   pid_off ~ offsetof(prstatus_t, pr_pid)
11982
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11983
   gregset_size ~ sizeof(gregset_t)
11984
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11985
11986
static bool
11987
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11988
             int pid_off, int lwpid_off, size_t gregset_size,
11989
             size_t gregset_offset)
11990
0
{
11991
0
  asection *sect = NULL;
11992
0
  elf_tdata (abfd)->core->signal
11993
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11994
0
  elf_tdata (abfd)->core->pid
11995
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11996
0
  elf_tdata (abfd)->core->lwpid
11997
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11998
11999
0
  sect = bfd_get_section_by_name (abfd, ".reg");
12000
0
  if (sect != NULL)
12001
0
    sect->size = gregset_size;
12002
12003
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12004
0
            note->descpos + gregset_offset);
12005
0
}
12006
12007
/* Gets program and arguments from a core.
12008
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
12009
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
12010
12011
static bool
12012
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
12013
        int prog_off, int comm_off)
12014
0
{
12015
0
  elf_tdata (abfd)->core->program
12016
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
12017
0
  elf_tdata (abfd)->core->command
12018
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
12019
12020
0
  return true;
12021
0
}
12022
12023
/* Processes Solaris's LWP status note.
12024
   gregset_size ~ sizeof(gregset_t)
12025
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
12026
   fpregset_size ~ sizeof(fpregset_t)
12027
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
12028
12029
static bool
12030
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
12031
        size_t gregset_size, int gregset_off,
12032
        size_t fpregset_size, int fpregset_off)
12033
0
{
12034
0
  asection *sect = NULL;
12035
0
  char reg2_section_name[16] = { 0 };
12036
12037
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
12038
0
       elf_tdata (abfd)->core->lwpid);
12039
12040
  /* offsetof(lwpstatus_t, pr_lwpid) */
12041
0
  elf_tdata (abfd)->core->lwpid
12042
0
    = bfd_get_32 (abfd, note->descdata + 4);
12043
  /* offsetof(lwpstatus_t, pr_cursig) */
12044
0
  elf_tdata (abfd)->core->signal
12045
0
    = bfd_get_16 (abfd, note->descdata + 12);
12046
12047
0
  sect = bfd_get_section_by_name (abfd, ".reg");
12048
0
  if (sect != NULL)
12049
0
    sect->size = gregset_size;
12050
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12051
0
               note->descpos + gregset_off))
12052
0
    return false;
12053
12054
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
12055
0
  if (sect != NULL)
12056
0
    {
12057
0
      sect->size = fpregset_size;
12058
0
      sect->filepos = note->descpos + fpregset_off;
12059
0
      sect->alignment_power = 2;
12060
0
    }
12061
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
12062
0
               note->descpos + fpregset_off))
12063
0
    return false;
12064
12065
0
  return true;
12066
0
}
12067
12068
static bool
12069
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
12070
0
{
12071
0
  if (note == NULL)
12072
0
    return false;
12073
12074
  /* core files are identified as 32- or 64-bit, SPARC or x86,
12075
     by the size of the descsz which matches the sizeof()
12076
     the type appropriate for that note type (e.g., prstatus_t for
12077
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
12078
     on Solaris. The core file bitness may differ from the bitness of
12079
     gdb itself, so fixed values are used instead of sizeof().
12080
     Appropriate fixed offsets are also used to obtain data from
12081
     the note.  */
12082
12083
0
  switch ((int) note->type)
12084
0
    {
12085
0
    case SOLARIS_NT_PRSTATUS:
12086
0
      switch (note->descsz)
12087
0
  {
12088
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12089
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12090
0
                 136, 216, 308, 152, 356);
12091
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12092
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12093
0
                 264, 360, 520, 304, 600);
12094
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
12095
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12096
0
                 136, 216, 308, 76, 356);
12097
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
12098
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12099
0
                 264, 360, 520, 224, 600);
12100
0
  default:
12101
0
    return true;
12102
0
  }
12103
12104
0
    case SOLARIS_NT_PSINFO:
12105
0
    case SOLARIS_NT_PRPSINFO:
12106
0
      switch (note->descsz)
12107
0
  {
12108
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12109
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
12110
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12111
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
12112
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12113
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
12114
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12115
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
12116
0
  default:
12117
0
    return true;
12118
0
  }
12119
12120
0
    case SOLARIS_NT_LWPSTATUS:
12121
0
      switch (note->descsz)
12122
0
  {
12123
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12124
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12125
0
            152, 344, 400, 496);
12126
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12127
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12128
0
            304, 544, 544, 848);
12129
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12130
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12131
0
            76, 344, 380, 420);
12132
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12133
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12134
0
            224, 544, 528, 768);
12135
0
  default:
12136
0
    return true;
12137
0
  }
12138
12139
0
    case SOLARIS_NT_LWPSINFO:
12140
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12141
0
      if (note->descsz == 128 || note->descsz == 152)
12142
0
  elf_tdata (abfd)->core->lwpid =
12143
0
    bfd_get_32 (abfd, note->descdata + 4);
12144
0
      break;
12145
12146
0
    default:
12147
0
      break;
12148
0
    }
12149
12150
0
  return true;
12151
0
}
12152
12153
/* For name starting with "CORE" this may be either a Solaris
12154
   core file or a gdb-generated core file.  Do Solaris-specific
12155
   processing on selected note types first with
12156
   elfcore_grok_solaris_note(), then process the note
12157
   in elfcore_grok_note().  */
12158
12159
static bool
12160
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12161
0
{
12162
0
  if (!elfcore_grok_solaris_note_impl (abfd, note))
12163
0
    return false;
12164
12165
0
  return elfcore_grok_note (abfd, note);
12166
0
}
12167
12168
static bool
12169
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12170
0
{
12171
0
  if (note->type == NT_OPENBSD_PROCINFO)
12172
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
12173
12174
0
  if (note->type == NT_OPENBSD_REGS)
12175
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
12176
12177
0
  if (note->type == NT_OPENBSD_FPREGS)
12178
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12179
12180
0
  if (note->type == NT_OPENBSD_XFPREGS)
12181
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12182
12183
0
  if (note->type == NT_OPENBSD_AUXV)
12184
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
12185
12186
0
  if (note->type == NT_OPENBSD_WCOOKIE)
12187
0
    {
12188
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12189
0
                 SEC_HAS_CONTENTS);
12190
12191
0
      if (sect == NULL)
12192
0
  return false;
12193
0
      sect->size = note->descsz;
12194
0
      sect->filepos = note->descpos;
12195
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12196
12197
0
      return true;
12198
0
    }
12199
12200
0
  return true;
12201
0
}
12202
12203
static bool
12204
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12205
0
{
12206
0
  void *ddata = note->descdata;
12207
0
  char buf[100];
12208
0
  char *name;
12209
0
  asection *sect;
12210
0
  short sig;
12211
0
  unsigned flags;
12212
12213
0
  if (note->descsz < 16)
12214
0
    return false;
12215
12216
  /* nto_procfs_status 'pid' field is at offset 0.  */
12217
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12218
12219
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
12220
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12221
12222
  /* nto_procfs_status 'flags' field is at offset 8.  */
12223
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12224
12225
  /* nto_procfs_status 'what' field is at offset 14.  */
12226
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12227
0
    {
12228
0
      elf_tdata (abfd)->core->signal = sig;
12229
0
      elf_tdata (abfd)->core->lwpid = *tid;
12230
0
    }
12231
12232
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
12233
     do not come from signals so we make sure we set the current
12234
     thread just in case.  */
12235
0
  if (flags & 0x00000080)
12236
0
    elf_tdata (abfd)->core->lwpid = *tid;
12237
12238
  /* Make a ".qnx_core_status/%d" section.  */
12239
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
12240
12241
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12242
0
  if (name == NULL)
12243
0
    return false;
12244
0
  strcpy (name, buf);
12245
12246
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12247
0
  if (sect == NULL)
12248
0
    return false;
12249
12250
0
  sect->size    = note->descsz;
12251
0
  sect->filepos   = note->descpos;
12252
0
  sect->alignment_power = 2;
12253
12254
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12255
0
}
12256
12257
static bool
12258
elfcore_grok_nto_regs (bfd *abfd,
12259
           Elf_Internal_Note *note,
12260
           long tid,
12261
           char *base)
12262
0
{
12263
0
  char buf[100];
12264
0
  char *name;
12265
0
  asection *sect;
12266
12267
  /* Make a "(base)/%d" section.  */
12268
0
  sprintf (buf, "%s/%ld", base, tid);
12269
12270
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12271
0
  if (name == NULL)
12272
0
    return false;
12273
0
  strcpy (name, buf);
12274
12275
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12276
0
  if (sect == NULL)
12277
0
    return false;
12278
12279
0
  sect->size    = note->descsz;
12280
0
  sect->filepos   = note->descpos;
12281
0
  sect->alignment_power = 2;
12282
12283
  /* This is the current thread.  */
12284
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12285
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12286
12287
0
  return true;
12288
0
}
12289
12290
static bool
12291
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12292
0
{
12293
  /* Every GREG section has a STATUS section before it.  Store the
12294
     tid from the previous call to pass down to the next gregs
12295
     function.  */
12296
0
  static long tid = 1;
12297
12298
0
  switch (note->type)
12299
0
    {
12300
0
    case QNT_CORE_INFO:
12301
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12302
0
    case QNT_CORE_STATUS:
12303
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12304
0
    case QNT_CORE_GREG:
12305
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12306
0
    case QNT_CORE_FPREG:
12307
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12308
0
    default:
12309
0
      return true;
12310
0
    }
12311
0
}
12312
12313
static bool
12314
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12315
0
{
12316
0
  char *name;
12317
0
  asection *sect;
12318
0
  size_t len;
12319
12320
  /* Use note name as section name.  */
12321
0
  len = note->namesz;
12322
0
  name = (char *) bfd_alloc (abfd, len);
12323
0
  if (name == NULL)
12324
0
    return false;
12325
0
  memcpy (name, note->namedata, len);
12326
0
  name[len - 1] = '\0';
12327
12328
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12329
0
  if (sect == NULL)
12330
0
    return false;
12331
12332
0
  sect->size    = note->descsz;
12333
0
  sect->filepos   = note->descpos;
12334
0
  sect->alignment_power = 1;
12335
12336
0
  return true;
12337
0
}
12338
12339
/* Function: elfcore_write_note
12340
12341
   Inputs:
12342
     buffer to hold note, and current size of buffer
12343
     name of note
12344
     type of note
12345
     data for note
12346
     size of data for note
12347
12348
   Writes note to end of buffer.  ELF64 notes are written exactly as
12349
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12350
   that they ought to have 8-byte namesz and descsz field, and have
12351
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12352
12353
   Return:
12354
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12355
12356
char *
12357
elfcore_write_note (bfd *abfd,
12358
        char *buf,
12359
        int *bufsiz,
12360
        const char *name,
12361
        int type,
12362
        const void *input,
12363
        int size)
12364
0
{
12365
0
  Elf_External_Note *xnp;
12366
0
  size_t namesz;
12367
0
  size_t newspace;
12368
0
  char *dest;
12369
12370
0
  namesz = 0;
12371
0
  if (name != NULL)
12372
0
    namesz = strlen (name) + 1;
12373
12374
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12375
12376
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12377
0
  if (buf == NULL)
12378
0
    return buf;
12379
0
  dest = buf + *bufsiz;
12380
0
  *bufsiz += newspace;
12381
0
  xnp = (Elf_External_Note *) dest;
12382
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12383
0
  H_PUT_32 (abfd, size, xnp->descsz);
12384
0
  H_PUT_32 (abfd, type, xnp->type);
12385
0
  dest = xnp->name;
12386
0
  if (name != NULL)
12387
0
    {
12388
0
      memcpy (dest, name, namesz);
12389
0
      dest += namesz;
12390
0
      while (namesz & 3)
12391
0
  {
12392
0
    *dest++ = '\0';
12393
0
    ++namesz;
12394
0
  }
12395
0
    }
12396
0
  memcpy (dest, input, size);
12397
0
  dest += size;
12398
0
  while (size & 3)
12399
0
    {
12400
0
      *dest++ = '\0';
12401
0
      ++size;
12402
0
    }
12403
0
  return buf;
12404
0
}
12405
12406
/* gcc-8 warns (*) on all the strncpy calls in this function about
12407
   possible string truncation.  The "truncation" is not a bug.  We
12408
   have an external representation of structs with fields that are not
12409
   necessarily NULL terminated and corresponding internal
12410
   representation fields that are one larger so that they can always
12411
   be NULL terminated.
12412
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12413
   diagnostics inside functions, giving a hard error if you try to use
12414
   the finer control available with later versions.
12415
   gcc prior to 4.2 warns about diagnostic push and pop.
12416
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12417
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12418
   (*) Depending on your system header files!  */
12419
#if GCC_VERSION >= 8000
12420
# pragma GCC diagnostic push
12421
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12422
#endif
12423
char *
12424
elfcore_write_prpsinfo (bfd  *abfd,
12425
      char *buf,
12426
      int  *bufsiz,
12427
      const char *fname,
12428
      const char *psargs)
12429
0
{
12430
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12431
12432
0
  if (bed->elf_backend_write_core_note != NULL)
12433
0
    {
12434
0
      char *ret;
12435
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12436
0
             NT_PRPSINFO, fname, psargs);
12437
0
      if (ret != NULL)
12438
0
  return ret;
12439
0
    }
12440
12441
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12442
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12443
0
  if (bed->s->elfclass == ELFCLASS32)
12444
0
    {
12445
#  if defined (HAVE_PSINFO32_T)
12446
      psinfo32_t data;
12447
      int note_type = NT_PSINFO;
12448
#  else
12449
0
      prpsinfo32_t data;
12450
0
      int note_type = NT_PRPSINFO;
12451
0
#  endif
12452
12453
0
      memset (&data, 0, sizeof (data));
12454
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12455
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12456
0
      return elfcore_write_note (abfd, buf, bufsiz,
12457
0
         "CORE", note_type, &data, sizeof (data));
12458
0
    }
12459
0
  else
12460
0
# endif
12461
0
    {
12462
# if defined (HAVE_PSINFO_T)
12463
      psinfo_t data;
12464
      int note_type = NT_PSINFO;
12465
# else
12466
0
      prpsinfo_t data;
12467
0
      int note_type = NT_PRPSINFO;
12468
0
# endif
12469
12470
0
      memset (&data, 0, sizeof (data));
12471
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12472
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12473
0
      return elfcore_write_note (abfd, buf, bufsiz,
12474
0
         "CORE", note_type, &data, sizeof (data));
12475
0
    }
12476
0
#endif  /* PSINFO_T or PRPSINFO_T */
12477
12478
0
  free (buf);
12479
0
  return NULL;
12480
0
}
12481
#if GCC_VERSION >= 8000
12482
# pragma GCC diagnostic pop
12483
#endif
12484
12485
char *
12486
elfcore_write_linux_prpsinfo32
12487
  (bfd *abfd, char *buf, int *bufsiz,
12488
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12489
0
{
12490
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12491
0
    {
12492
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12493
12494
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12495
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12496
0
         &data, sizeof (data));
12497
0
    }
12498
0
  else
12499
0
    {
12500
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12501
12502
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12503
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12504
0
         &data, sizeof (data));
12505
0
    }
12506
0
}
12507
12508
char *
12509
elfcore_write_linux_prpsinfo64
12510
  (bfd *abfd, char *buf, int *bufsiz,
12511
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12512
0
{
12513
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12514
0
    {
12515
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12516
12517
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12518
0
      return elfcore_write_note (abfd, buf, bufsiz,
12519
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12520
0
    }
12521
0
  else
12522
0
    {
12523
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12524
12525
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12526
0
      return elfcore_write_note (abfd, buf, bufsiz,
12527
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12528
0
    }
12529
0
}
12530
12531
char *
12532
elfcore_write_prstatus (bfd *abfd,
12533
      char *buf,
12534
      int *bufsiz,
12535
      long pid,
12536
      int cursig,
12537
      const void *gregs)
12538
0
{
12539
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12540
12541
0
  if (bed->elf_backend_write_core_note != NULL)
12542
0
    {
12543
0
      char *ret;
12544
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12545
0
             NT_PRSTATUS,
12546
0
             pid, cursig, gregs);
12547
0
      if (ret != NULL)
12548
0
  return ret;
12549
0
    }
12550
12551
0
#if defined (HAVE_PRSTATUS_T)
12552
0
#if defined (HAVE_PRSTATUS32_T)
12553
0
  if (bed->s->elfclass == ELFCLASS32)
12554
0
    {
12555
0
      prstatus32_t prstat;
12556
12557
0
      memset (&prstat, 0, sizeof (prstat));
12558
0
      prstat.pr_pid = pid;
12559
0
      prstat.pr_cursig = cursig;
12560
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12561
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12562
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12563
0
    }
12564
0
  else
12565
0
#endif
12566
0
    {
12567
0
      prstatus_t prstat;
12568
12569
0
      memset (&prstat, 0, sizeof (prstat));
12570
0
      prstat.pr_pid = pid;
12571
0
      prstat.pr_cursig = cursig;
12572
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12573
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12574
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12575
0
    }
12576
0
#endif /* HAVE_PRSTATUS_T */
12577
12578
0
  free (buf);
12579
0
  return NULL;
12580
0
}
12581
12582
#if defined (HAVE_LWPSTATUS_T)
12583
char *
12584
elfcore_write_lwpstatus (bfd *abfd,
12585
       char *buf,
12586
       int *bufsiz,
12587
       long pid,
12588
       int cursig,
12589
       const void *gregs)
12590
{
12591
  lwpstatus_t lwpstat;
12592
  const char *note_name = "CORE";
12593
12594
  memset (&lwpstat, 0, sizeof (lwpstat));
12595
  lwpstat.pr_lwpid  = pid >> 16;
12596
  lwpstat.pr_cursig = cursig;
12597
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12598
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12599
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12600
#if !defined(gregs)
12601
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12602
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12603
#else
12604
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12605
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12606
#endif
12607
#endif
12608
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
12609
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12610
}
12611
#endif /* HAVE_LWPSTATUS_T */
12612
12613
#if defined (HAVE_PSTATUS_T)
12614
char *
12615
elfcore_write_pstatus (bfd *abfd,
12616
           char *buf,
12617
           int *bufsiz,
12618
           long pid,
12619
           int cursig ATTRIBUTE_UNUSED,
12620
           const void *gregs ATTRIBUTE_UNUSED)
12621
{
12622
  const char *note_name = "CORE";
12623
#if defined (HAVE_PSTATUS32_T)
12624
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12625
12626
  if (bed->s->elfclass == ELFCLASS32)
12627
    {
12628
      pstatus32_t pstat;
12629
12630
      memset (&pstat, 0, sizeof (pstat));
12631
      pstat.pr_pid = pid & 0xffff;
12632
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12633
        NT_PSTATUS, &pstat, sizeof (pstat));
12634
      return buf;
12635
    }
12636
  else
12637
#endif
12638
    {
12639
      pstatus_t pstat;
12640
12641
      memset (&pstat, 0, sizeof (pstat));
12642
      pstat.pr_pid = pid & 0xffff;
12643
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12644
        NT_PSTATUS, &pstat, sizeof (pstat));
12645
      return buf;
12646
    }
12647
}
12648
#endif /* HAVE_PSTATUS_T */
12649
12650
char *
12651
elfcore_write_prfpreg (bfd *abfd,
12652
           char *buf,
12653
           int *bufsiz,
12654
           const void *fpregs,
12655
           int size)
12656
0
{
12657
0
  const char *note_name = "CORE";
12658
0
  return elfcore_write_note (abfd, buf, bufsiz,
12659
0
           note_name, NT_FPREGSET, fpregs, size);
12660
0
}
12661
12662
char *
12663
elfcore_write_prxfpreg (bfd *abfd,
12664
      char *buf,
12665
      int *bufsiz,
12666
      const void *xfpregs,
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_PRXFPREG, xfpregs, size);
12672
0
}
12673
12674
char *
12675
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12676
       const void *xfpregs, int size)
12677
0
{
12678
0
  char *note_name;
12679
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12680
0
    note_name = "FreeBSD";
12681
0
  else
12682
0
    note_name = "LINUX";
12683
0
  return elfcore_write_note (abfd, buf, bufsiz,
12684
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12685
0
}
12686
12687
char *
12688
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12689
          const void *regs, int size)
12690
0
{
12691
0
  char *note_name = "FreeBSD";
12692
0
  return elfcore_write_note (abfd, buf, bufsiz,
12693
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12694
0
}
12695
12696
char *
12697
elfcore_write_ppc_vmx (bfd *abfd,
12698
           char *buf,
12699
           int *bufsiz,
12700
           const void *ppc_vmx,
12701
           int size)
12702
0
{
12703
0
  char *note_name = "LINUX";
12704
0
  return elfcore_write_note (abfd, buf, bufsiz,
12705
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
12706
0
}
12707
12708
char *
12709
elfcore_write_ppc_vsx (bfd *abfd,
12710
           char *buf,
12711
           int *bufsiz,
12712
           const void *ppc_vsx,
12713
           int size)
12714
0
{
12715
0
  char *note_name = "LINUX";
12716
0
  return elfcore_write_note (abfd, buf, bufsiz,
12717
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
12718
0
}
12719
12720
char *
12721
elfcore_write_ppc_tar (bfd *abfd,
12722
           char *buf,
12723
           int *bufsiz,
12724
           const void *ppc_tar,
12725
           int size)
12726
0
{
12727
0
  char *note_name = "LINUX";
12728
0
  return elfcore_write_note (abfd, buf, bufsiz,
12729
0
           note_name, NT_PPC_TAR, ppc_tar, size);
12730
0
}
12731
12732
char *
12733
elfcore_write_ppc_ppr (bfd *abfd,
12734
           char *buf,
12735
           int *bufsiz,
12736
           const void *ppc_ppr,
12737
           int size)
12738
0
{
12739
0
  char *note_name = "LINUX";
12740
0
  return elfcore_write_note (abfd, buf, bufsiz,
12741
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12742
0
}
12743
12744
char *
12745
elfcore_write_ppc_dscr (bfd *abfd,
12746
      char *buf,
12747
      int *bufsiz,
12748
      const void *ppc_dscr,
12749
      int size)
12750
0
{
12751
0
  char *note_name = "LINUX";
12752
0
  return elfcore_write_note (abfd, buf, bufsiz,
12753
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12754
0
}
12755
12756
char *
12757
elfcore_write_ppc_ebb (bfd *abfd,
12758
           char *buf,
12759
           int *bufsiz,
12760
           const void *ppc_ebb,
12761
           int size)
12762
0
{
12763
0
  char *note_name = "LINUX";
12764
0
  return elfcore_write_note (abfd, buf, bufsiz,
12765
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12766
0
}
12767
12768
char *
12769
elfcore_write_ppc_pmu (bfd *abfd,
12770
           char *buf,
12771
           int *bufsiz,
12772
           const void *ppc_pmu,
12773
           int size)
12774
0
{
12775
0
  char *note_name = "LINUX";
12776
0
  return elfcore_write_note (abfd, buf, bufsiz,
12777
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12778
0
}
12779
12780
char *
12781
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12782
         char *buf,
12783
         int *bufsiz,
12784
         const void *ppc_tm_cgpr,
12785
         int size)
12786
0
{
12787
0
  char *note_name = "LINUX";
12788
0
  return elfcore_write_note (abfd, buf, bufsiz,
12789
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12790
0
}
12791
12792
char *
12793
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12794
         char *buf,
12795
         int *bufsiz,
12796
         const void *ppc_tm_cfpr,
12797
         int size)
12798
0
{
12799
0
  char *note_name = "LINUX";
12800
0
  return elfcore_write_note (abfd, buf, bufsiz,
12801
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12802
0
}
12803
12804
char *
12805
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12806
         char *buf,
12807
         int *bufsiz,
12808
         const void *ppc_tm_cvmx,
12809
         int size)
12810
0
{
12811
0
  char *note_name = "LINUX";
12812
0
  return elfcore_write_note (abfd, buf, bufsiz,
12813
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12814
0
}
12815
12816
char *
12817
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12818
         char *buf,
12819
         int *bufsiz,
12820
         const void *ppc_tm_cvsx,
12821
         int size)
12822
0
{
12823
0
  char *note_name = "LINUX";
12824
0
  return elfcore_write_note (abfd, buf, bufsiz,
12825
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12826
0
}
12827
12828
char *
12829
elfcore_write_ppc_tm_spr (bfd *abfd,
12830
        char *buf,
12831
        int *bufsiz,
12832
        const void *ppc_tm_spr,
12833
        int size)
12834
0
{
12835
0
  char *note_name = "LINUX";
12836
0
  return elfcore_write_note (abfd, buf, bufsiz,
12837
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12838
0
}
12839
12840
char *
12841
elfcore_write_ppc_tm_ctar (bfd *abfd,
12842
         char *buf,
12843
         int *bufsiz,
12844
         const void *ppc_tm_ctar,
12845
         int size)
12846
0
{
12847
0
  char *note_name = "LINUX";
12848
0
  return elfcore_write_note (abfd, buf, bufsiz,
12849
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12850
0
}
12851
12852
char *
12853
elfcore_write_ppc_tm_cppr (bfd *abfd,
12854
         char *buf,
12855
         int *bufsiz,
12856
         const void *ppc_tm_cppr,
12857
         int size)
12858
0
{
12859
0
  char *note_name = "LINUX";
12860
0
  return elfcore_write_note (abfd, buf, bufsiz,
12861
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12862
0
}
12863
12864
char *
12865
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12866
          char *buf,
12867
          int *bufsiz,
12868
          const void *ppc_tm_cdscr,
12869
          int size)
12870
0
{
12871
0
  char *note_name = "LINUX";
12872
0
  return elfcore_write_note (abfd, buf, bufsiz,
12873
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12874
0
}
12875
12876
static char *
12877
elfcore_write_s390_high_gprs (bfd *abfd,
12878
            char *buf,
12879
            int *bufsiz,
12880
            const void *s390_high_gprs,
12881
            int size)
12882
0
{
12883
0
  char *note_name = "LINUX";
12884
0
  return elfcore_write_note (abfd, buf, bufsiz,
12885
0
           note_name, NT_S390_HIGH_GPRS,
12886
0
           s390_high_gprs, size);
12887
0
}
12888
12889
char *
12890
elfcore_write_s390_timer (bfd *abfd,
12891
        char *buf,
12892
        int *bufsiz,
12893
        const void *s390_timer,
12894
        int size)
12895
0
{
12896
0
  char *note_name = "LINUX";
12897
0
  return elfcore_write_note (abfd, buf, bufsiz,
12898
0
           note_name, NT_S390_TIMER, s390_timer, size);
12899
0
}
12900
12901
char *
12902
elfcore_write_s390_todcmp (bfd *abfd,
12903
         char *buf,
12904
         int *bufsiz,
12905
         const void *s390_todcmp,
12906
         int size)
12907
0
{
12908
0
  char *note_name = "LINUX";
12909
0
  return elfcore_write_note (abfd, buf, bufsiz,
12910
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12911
0
}
12912
12913
char *
12914
elfcore_write_s390_todpreg (bfd *abfd,
12915
          char *buf,
12916
          int *bufsiz,
12917
          const void *s390_todpreg,
12918
          int size)
12919
0
{
12920
0
  char *note_name = "LINUX";
12921
0
  return elfcore_write_note (abfd, buf, bufsiz,
12922
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12923
0
}
12924
12925
char *
12926
elfcore_write_s390_ctrs (bfd *abfd,
12927
       char *buf,
12928
       int *bufsiz,
12929
       const void *s390_ctrs,
12930
       int size)
12931
0
{
12932
0
  char *note_name = "LINUX";
12933
0
  return elfcore_write_note (abfd, buf, bufsiz,
12934
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12935
0
}
12936
12937
char *
12938
elfcore_write_s390_prefix (bfd *abfd,
12939
         char *buf,
12940
         int *bufsiz,
12941
         const void *s390_prefix,
12942
         int size)
12943
0
{
12944
0
  char *note_name = "LINUX";
12945
0
  return elfcore_write_note (abfd, buf, bufsiz,
12946
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12947
0
}
12948
12949
char *
12950
elfcore_write_s390_last_break (bfd *abfd,
12951
             char *buf,
12952
             int *bufsiz,
12953
             const void *s390_last_break,
12954
             int size)
12955
0
{
12956
0
  char *note_name = "LINUX";
12957
0
  return elfcore_write_note (abfd, buf, bufsiz,
12958
0
           note_name, NT_S390_LAST_BREAK,
12959
0
           s390_last_break, size);
12960
0
}
12961
12962
char *
12963
elfcore_write_s390_system_call (bfd *abfd,
12964
        char *buf,
12965
        int *bufsiz,
12966
        const void *s390_system_call,
12967
        int size)
12968
0
{
12969
0
  char *note_name = "LINUX";
12970
0
  return elfcore_write_note (abfd, buf, bufsiz,
12971
0
           note_name, NT_S390_SYSTEM_CALL,
12972
0
           s390_system_call, size);
12973
0
}
12974
12975
char *
12976
elfcore_write_s390_tdb (bfd *abfd,
12977
      char *buf,
12978
      int *bufsiz,
12979
      const void *s390_tdb,
12980
      int size)
12981
0
{
12982
0
  char *note_name = "LINUX";
12983
0
  return elfcore_write_note (abfd, buf, bufsiz,
12984
0
           note_name, NT_S390_TDB, s390_tdb, size);
12985
0
}
12986
12987
char *
12988
elfcore_write_s390_vxrs_low (bfd *abfd,
12989
           char *buf,
12990
           int *bufsiz,
12991
           const void *s390_vxrs_low,
12992
           int size)
12993
0
{
12994
0
  char *note_name = "LINUX";
12995
0
  return elfcore_write_note (abfd, buf, bufsiz,
12996
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12997
0
}
12998
12999
char *
13000
elfcore_write_s390_vxrs_high (bfd *abfd,
13001
           char *buf,
13002
           int *bufsiz,
13003
           const void *s390_vxrs_high,
13004
           int size)
13005
0
{
13006
0
  char *note_name = "LINUX";
13007
0
  return elfcore_write_note (abfd, buf, bufsiz,
13008
0
           note_name, NT_S390_VXRS_HIGH,
13009
0
           s390_vxrs_high, size);
13010
0
}
13011
13012
char *
13013
elfcore_write_s390_gs_cb (bfd *abfd,
13014
        char *buf,
13015
        int *bufsiz,
13016
        const void *s390_gs_cb,
13017
        int size)
13018
0
{
13019
0
  char *note_name = "LINUX";
13020
0
  return elfcore_write_note (abfd, buf, bufsiz,
13021
0
           note_name, NT_S390_GS_CB,
13022
0
           s390_gs_cb, size);
13023
0
}
13024
13025
char *
13026
elfcore_write_s390_gs_bc (bfd *abfd,
13027
        char *buf,
13028
        int *bufsiz,
13029
        const void *s390_gs_bc,
13030
        int size)
13031
0
{
13032
0
  char *note_name = "LINUX";
13033
0
  return elfcore_write_note (abfd, buf, bufsiz,
13034
0
           note_name, NT_S390_GS_BC,
13035
0
           s390_gs_bc, size);
13036
0
}
13037
13038
char *
13039
elfcore_write_arm_vfp (bfd *abfd,
13040
           char *buf,
13041
           int *bufsiz,
13042
           const void *arm_vfp,
13043
           int size)
13044
0
{
13045
0
  char *note_name = "LINUX";
13046
0
  return elfcore_write_note (abfd, buf, bufsiz,
13047
0
           note_name, NT_ARM_VFP, arm_vfp, size);
13048
0
}
13049
13050
char *
13051
elfcore_write_aarch_tls (bfd *abfd,
13052
           char *buf,
13053
           int *bufsiz,
13054
           const void *aarch_tls,
13055
           int size)
13056
0
{
13057
0
  char *note_name = "LINUX";
13058
0
  return elfcore_write_note (abfd, buf, bufsiz,
13059
0
           note_name, NT_ARM_TLS, aarch_tls, size);
13060
0
}
13061
13062
char *
13063
elfcore_write_aarch_hw_break (bfd *abfd,
13064
          char *buf,
13065
          int *bufsiz,
13066
          const void *aarch_hw_break,
13067
          int size)
13068
0
{
13069
0
  char *note_name = "LINUX";
13070
0
  return elfcore_write_note (abfd, buf, bufsiz,
13071
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13072
0
}
13073
13074
char *
13075
elfcore_write_aarch_hw_watch (bfd *abfd,
13076
          char *buf,
13077
          int *bufsiz,
13078
          const void *aarch_hw_watch,
13079
          int size)
13080
0
{
13081
0
  char *note_name = "LINUX";
13082
0
  return elfcore_write_note (abfd, buf, bufsiz,
13083
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13084
0
}
13085
13086
char *
13087
elfcore_write_aarch_sve (bfd *abfd,
13088
       char *buf,
13089
       int *bufsiz,
13090
       const void *aarch_sve,
13091
       int size)
13092
0
{
13093
0
  char *note_name = "LINUX";
13094
0
  return elfcore_write_note (abfd, buf, bufsiz,
13095
0
           note_name, NT_ARM_SVE, aarch_sve, size);
13096
0
}
13097
13098
char *
13099
elfcore_write_aarch_pauth (bfd *abfd,
13100
         char *buf,
13101
         int *bufsiz,
13102
         const void *aarch_pauth,
13103
         int size)
13104
0
{
13105
0
  char *note_name = "LINUX";
13106
0
  return elfcore_write_note (abfd, buf, bufsiz,
13107
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13108
0
}
13109
13110
char *
13111
elfcore_write_aarch_mte (bfd *abfd,
13112
              char *buf,
13113
              int *bufsiz,
13114
              const void *aarch_mte,
13115
              int size)
13116
0
{
13117
0
  char *note_name = "LINUX";
13118
0
  return elfcore_write_note (abfd, buf, bufsiz,
13119
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
13120
0
           aarch_mte,
13121
0
           size);
13122
0
}
13123
13124
char *
13125
elfcore_write_aarch_ssve (bfd *abfd,
13126
        char *buf,
13127
        int *bufsiz,
13128
        const void *aarch_ssve,
13129
        int size)
13130
0
{
13131
0
  char *note_name = "LINUX";
13132
0
  return elfcore_write_note (abfd, buf, bufsiz,
13133
0
           note_name, NT_ARM_SSVE,
13134
0
           aarch_ssve,
13135
0
           size);
13136
0
}
13137
13138
char *
13139
elfcore_write_aarch_za (bfd *abfd,
13140
      char *buf,
13141
      int *bufsiz,
13142
      const void *aarch_za,
13143
      int size)
13144
0
{
13145
0
  char *note_name = "LINUX";
13146
0
  return elfcore_write_note (abfd, buf, bufsiz,
13147
0
           note_name, NT_ARM_ZA,
13148
0
           aarch_za,
13149
0
           size);
13150
0
}
13151
13152
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
13153
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13154
   written into.  Return a pointer to the new start of the note buffer, to
13155
   replace BUF which may no longer be valid.  */
13156
13157
char *
13158
elfcore_write_aarch_zt (bfd *abfd,
13159
      char *buf,
13160
      int *bufsiz,
13161
      const void *aarch_zt,
13162
      int size)
13163
0
{
13164
0
  char *note_name = "LINUX";
13165
0
  return elfcore_write_note (abfd, buf, bufsiz,
13166
0
           note_name, NT_ARM_ZT,
13167
0
           aarch_zt,
13168
0
           size);
13169
0
}
13170
13171
char *
13172
elfcore_write_arc_v2 (bfd *abfd,
13173
          char *buf,
13174
          int *bufsiz,
13175
          const void *arc_v2,
13176
          int size)
13177
0
{
13178
0
  char *note_name = "LINUX";
13179
0
  return elfcore_write_note (abfd, buf, bufsiz,
13180
0
           note_name, NT_ARC_V2, arc_v2, size);
13181
0
}
13182
13183
char *
13184
elfcore_write_loongarch_cpucfg (bfd *abfd,
13185
        char *buf,
13186
        int *bufsiz,
13187
        const void *loongarch_cpucfg,
13188
        int size)
13189
0
{
13190
0
  char *note_name = "LINUX";
13191
0
  return elfcore_write_note (abfd, buf, bufsiz,
13192
0
           note_name, NT_LARCH_CPUCFG,
13193
0
           loongarch_cpucfg, size);
13194
0
}
13195
13196
char *
13197
elfcore_write_loongarch_lbt (bfd *abfd,
13198
           char *buf,
13199
           int *bufsiz,
13200
           const void *loongarch_lbt,
13201
           int size)
13202
0
{
13203
0
  char *note_name = "LINUX";
13204
0
  return elfcore_write_note (abfd, buf, bufsiz,
13205
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
13206
0
}
13207
13208
char *
13209
elfcore_write_loongarch_lsx (bfd *abfd,
13210
           char *buf,
13211
           int *bufsiz,
13212
           const void *loongarch_lsx,
13213
           int size)
13214
0
{
13215
0
  char *note_name = "LINUX";
13216
0
  return elfcore_write_note (abfd, buf, bufsiz,
13217
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
13218
0
}
13219
13220
char *
13221
elfcore_write_loongarch_lasx (bfd *abfd,
13222
            char *buf,
13223
            int *bufsiz,
13224
            const void *loongarch_lasx,
13225
            int size)
13226
0
{
13227
0
  char *note_name = "LINUX";
13228
0
  return elfcore_write_note (abfd, buf, bufsiz,
13229
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
13230
0
}
13231
13232
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13233
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13234
   written into.  Return a pointer to the new start of the note buffer, to
13235
   replace BUF which may no longer be valid.  */
13236
13237
char *
13238
elfcore_write_riscv_csr (bfd *abfd,
13239
       char *buf,
13240
       int *bufsiz,
13241
       const void *csrs,
13242
       int size)
13243
0
{
13244
0
  const char *note_name = "GDB";
13245
0
  return elfcore_write_note (abfd, buf, bufsiz,
13246
0
           note_name, NT_RISCV_CSR, csrs, size);
13247
0
}
13248
13249
/* Write the target description (a string) pointed to by TDESC, length
13250
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13251
   note is being written into.  Return a pointer to the new start of the
13252
   note buffer, to replace BUF which may no longer be valid.  */
13253
13254
char *
13255
elfcore_write_gdb_tdesc (bfd *abfd,
13256
       char *buf,
13257
       int *bufsiz,
13258
       const void *tdesc,
13259
       int size)
13260
0
{
13261
0
  const char *note_name = "GDB";
13262
0
  return elfcore_write_note (abfd, buf, bufsiz,
13263
0
           note_name, NT_GDB_TDESC, tdesc, size);
13264
0
}
13265
13266
char *
13267
elfcore_write_register_note (bfd *abfd,
13268
           char *buf,
13269
           int *bufsiz,
13270
           const char *section,
13271
           const void *data,
13272
           int size)
13273
0
{
13274
0
  if (strcmp (section, ".reg2") == 0)
13275
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13276
0
  if (strcmp (section, ".reg-xfp") == 0)
13277
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13278
0
  if (strcmp (section, ".reg-xstate") == 0)
13279
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13280
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
13281
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13282
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
13283
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13284
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
13285
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13286
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
13287
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13288
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
13289
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13290
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
13291
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13292
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
13293
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13294
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
13295
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13296
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13297
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13298
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13299
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13300
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13301
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13302
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13303
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13304
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13305
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13306
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13307
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13308
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13309
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13310
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13311
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13312
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
13313
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13314
0
  if (strcmp (section, ".reg-s390-timer") == 0)
13315
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13316
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
13317
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13318
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
13319
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13320
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
13321
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13322
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
13323
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13324
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
13325
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13326
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
13327
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13328
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
13329
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13330
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13331
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13332
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13333
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13334
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
13335
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13336
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
13337
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13338
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
13339
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13340
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
13341
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13342
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
13343
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13344
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13345
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13346
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
13347
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13348
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
13349
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13350
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
13351
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13352
0
  if (strcmp (section, ".reg-aarch-ssve") == 0)
13353
0
    return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13354
0
  if (strcmp (section, ".reg-aarch-za") == 0)
13355
0
    return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13356
0
  if (strcmp (section, ".reg-aarch-zt") == 0)
13357
0
    return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13358
0
  if (strcmp (section, ".reg-arc-v2") == 0)
13359
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13360
0
  if (strcmp (section, ".gdb-tdesc") == 0)
13361
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13362
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
13363
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13364
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13365
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13366
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
13367
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13368
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
13369
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13370
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
13371
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13372
0
  return NULL;
13373
0
}
13374
13375
char *
13376
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13377
       const void *buf, int bufsiz)
13378
0
{
13379
0
  return elfcore_write_note (obfd, note_data, note_size,
13380
0
           "CORE", NT_FILE, buf, bufsiz);
13381
0
}
13382
13383
static bool
13384
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13385
     size_t align)
13386
41.3k
{
13387
41.3k
  char *p;
13388
13389
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13390
     gABI specifies that PT_NOTE alignment should be aligned to 4
13391
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13392
     align is less than 4, we use 4 byte alignment.   */
13393
41.3k
  if (align < 4)
13394
17.9k
    align = 4;
13395
41.3k
  if (align != 4 && align != 8)
13396
15.5k
    return false;
13397
13398
25.8k
  p = buf;
13399
61.7k
  while (p < buf + size)
13400
58.2k
    {
13401
58.2k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13402
58.2k
      Elf_Internal_Note in;
13403
13404
58.2k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13405
2.99k
  return false;
13406
13407
55.2k
      in.type = H_GET_32 (abfd, xnp->type);
13408
13409
55.2k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13410
55.2k
      in.namedata = xnp->name;
13411
55.2k
      if (in.namesz > buf - in.namedata + size)
13412
11.1k
  return false;
13413
13414
44.0k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13415
44.0k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13416
44.0k
      in.descpos = offset + (in.descdata - buf);
13417
44.0k
      if (in.descsz != 0
13418
44.0k
    && (in.descdata >= buf + size
13419
19.8k
        || in.descsz > buf - in.descdata + size))
13420
8.00k
  return false;
13421
13422
36.0k
      switch (bfd_get_format (abfd))
13423
36.0k
  {
13424
0
  default:
13425
0
    return true;
13426
13427
76
  case bfd_core:
13428
76
    {
13429
608
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13430
76
      struct
13431
76
      {
13432
76
        const char * string;
13433
76
        size_t len;
13434
76
        bool (*func) (bfd *, Elf_Internal_Note *);
13435
76
      }
13436
76
      grokers[] =
13437
76
      {
13438
76
        GROKER_ELEMENT ("", elfcore_grok_note),
13439
76
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13440
76
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13441
76
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13442
76
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13443
76
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13444
76
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13445
76
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13446
76
      };
13447
76
#undef GROKER_ELEMENT
13448
76
      int i;
13449
13450
608
      for (i = ARRAY_SIZE (grokers); i--;)
13451
608
        {
13452
608
    if (in.namesz >= grokers[i].len
13453
608
        && strncmp (in.namedata, grokers[i].string,
13454
140
        grokers[i].len) == 0)
13455
76
      {
13456
76
        if (! grokers[i].func (abfd, & in))
13457
0
          return false;
13458
76
        break;
13459
76
      }
13460
608
        }
13461
76
      break;
13462
76
    }
13463
13464
36.0k
  case bfd_object:
13465
36.0k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13466
1.22k
      {
13467
1.22k
        if (! elfobj_grok_gnu_note (abfd, &in))
13468
198
    return false;
13469
1.22k
      }
13470
34.7k
    else if (in.namesz == sizeof "stapsdt"
13471
34.7k
       && strcmp (in.namedata, "stapsdt") == 0)
13472
0
      {
13473
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13474
0
    return false;
13475
0
      }
13476
35.8k
    break;
13477
36.0k
  }
13478
13479
35.8k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13480
35.8k
    }
13481
13482
3.48k
  return true;
13483
25.8k
}
13484
13485
bool
13486
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13487
    size_t align)
13488
179
{
13489
179
  char *buf;
13490
13491
179
  if (size == 0 || (size + 1) == 0)
13492
78
    return true;
13493
13494
101
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13495
7
    return false;
13496
13497
94
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13498
94
  if (buf == NULL)
13499
59
    return false;
13500
13501
  /* PR 17512: file: ec08f814
13502
     0-termintate the buffer so that string searches will not overflow.  */
13503
35
  buf[size] = 0;
13504
13505
35
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13506
35
    {
13507
35
      free (buf);
13508
35
      return false;
13509
35
    }
13510
13511
0
  free (buf);
13512
0
  return true;
13513
35
}
13514

13515
/* Providing external access to the ELF program header table.  */
13516
13517
/* Return an upper bound on the number of bytes required to store a
13518
   copy of ABFD's program header table entries.  Return -1 if an error
13519
   occurs; bfd_get_error will return an appropriate code.  */
13520
13521
long
13522
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13523
0
{
13524
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13525
0
    {
13526
0
      bfd_set_error (bfd_error_wrong_format);
13527
0
      return -1;
13528
0
    }
13529
13530
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13531
0
}
13532
13533
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13534
   will be stored as an array of Elf_Internal_Phdr structures, as
13535
   defined in include/elf/internal.h.  To find out how large the
13536
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13537
13538
   Return the number of program header table entries read, or -1 if an
13539
   error occurs; bfd_get_error will return an appropriate code.  */
13540
13541
int
13542
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13543
0
{
13544
0
  int num_phdrs;
13545
13546
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13547
0
    {
13548
0
      bfd_set_error (bfd_error_wrong_format);
13549
0
      return -1;
13550
0
    }
13551
13552
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13553
0
  if (num_phdrs != 0)
13554
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13555
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13556
13557
0
  return num_phdrs;
13558
0
}
13559
13560
enum elf_reloc_type_class
13561
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13562
         const asection *rel_sec ATTRIBUTE_UNUSED,
13563
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13564
0
{
13565
0
  return reloc_class_normal;
13566
0
}
13567
13568
/* For RELA architectures, return the relocation value for a
13569
   relocation against a local symbol.  */
13570
13571
bfd_vma
13572
_bfd_elf_rela_local_sym (bfd *abfd,
13573
       Elf_Internal_Sym *sym,
13574
       asection **psec,
13575
       Elf_Internal_Rela *rel)
13576
0
{
13577
0
  asection *sec = *psec;
13578
0
  bfd_vma relocation;
13579
13580
0
  relocation = (sec->output_section->vma
13581
0
    + sec->output_offset
13582
0
    + sym->st_value);
13583
0
  if ((sec->flags & SEC_MERGE)
13584
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13585
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13586
0
    {
13587
0
      rel->r_addend =
13588
0
  _bfd_merged_section_offset (abfd, psec,
13589
0
            elf_section_data (sec)->sec_info,
13590
0
            sym->st_value + rel->r_addend);
13591
0
      if (sec != *psec)
13592
0
  {
13593
    /* If we have changed the section, and our original section is
13594
       marked with SEC_EXCLUDE, it means that the original
13595
       SEC_MERGE section has been completely subsumed in some
13596
       other SEC_MERGE section.  In this case, we need to leave
13597
       some info around for --emit-relocs.  */
13598
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13599
0
      sec->kept_section = *psec;
13600
0
    sec = *psec;
13601
0
  }
13602
0
      rel->r_addend -= relocation;
13603
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13604
0
    }
13605
0
  return relocation;
13606
0
}
13607
13608
bfd_vma
13609
_bfd_elf_rel_local_sym (bfd *abfd,
13610
      Elf_Internal_Sym *sym,
13611
      asection **psec,
13612
      bfd_vma addend)
13613
0
{
13614
0
  asection *sec = *psec;
13615
13616
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13617
0
    return sym->st_value + addend;
13618
13619
0
  return _bfd_merged_section_offset (abfd, psec,
13620
0
             elf_section_data (sec)->sec_info,
13621
0
             sym->st_value + addend);
13622
0
}
13623
13624
/* Adjust an address within a section.  Given OFFSET within SEC, return
13625
   the new offset within the section, based upon changes made to the
13626
   section.  Returns -1 if the offset is now invalid.
13627
   The offset (in abnd out) is in target sized bytes, however big a
13628
   byte may be.  */
13629
13630
bfd_vma
13631
_bfd_elf_section_offset (bfd *abfd,
13632
       struct bfd_link_info *info,
13633
       asection *sec,
13634
       bfd_vma offset)
13635
0
{
13636
0
  switch (sec->sec_info_type)
13637
0
    {
13638
0
    case SEC_INFO_TYPE_STABS:
13639
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13640
0
               offset);
13641
0
    case SEC_INFO_TYPE_EH_FRAME:
13642
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13643
13644
0
    default:
13645
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13646
0
  {
13647
    /* Reverse the offset.  */
13648
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13649
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13650
13651
    /* address_size and sec->size are in octets.  Convert
13652
       to bytes before subtracting the original offset.  */
13653
0
    offset = ((sec->size - address_size)
13654
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13655
0
  }
13656
0
      return offset;
13657
0
    }
13658
0
}
13659

13660
long
13661
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13662
             long symcount ATTRIBUTE_UNUSED,
13663
             asymbol **syms ATTRIBUTE_UNUSED,
13664
             long dynsymcount,
13665
             asymbol **dynsyms,
13666
             asymbol **ret)
13667
3.28k
{
13668
3.28k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13669
3.28k
  asection *relplt;
13670
3.28k
  asymbol *s;
13671
3.28k
  const char *relplt_name;
13672
3.28k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13673
3.28k
  arelent *p;
13674
3.28k
  long count, i, n;
13675
3.28k
  size_t size;
13676
3.28k
  Elf_Internal_Shdr *hdr;
13677
3.28k
  char *names;
13678
3.28k
  asection *plt;
13679
13680
3.28k
  *ret = NULL;
13681
13682
3.28k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13683
3.16k
    return 0;
13684
13685
115
  if (dynsymcount <= 0)
13686
76
    return 0;
13687
13688
39
  if (!bed->plt_sym_val)
13689
11
    return 0;
13690
13691
28
  relplt_name = bed->relplt_name;
13692
28
  if (relplt_name == NULL)
13693
28
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13694
28
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13695
28
  if (relplt == NULL)
13696
0
    return 0;
13697
13698
28
  hdr = &elf_section_data (relplt)->this_hdr;
13699
28
  if (hdr->sh_link != elf_dynsymtab (abfd)
13700
28
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13701
0
    return 0;
13702
13703
28
  plt = bfd_get_section_by_name (abfd, ".plt");
13704
28
  if (plt == NULL)
13705
0
    return 0;
13706
13707
28
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13708
28
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13709
3
    return -1;
13710
13711
25
  count = NUM_SHDR_ENTRIES (hdr);
13712
25
  size = count * sizeof (asymbol);
13713
25
  p = relplt->relocation;
13714
3.52k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13715
3.50k
    {
13716
3.50k
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13717
3.50k
      if (p->addend != 0)
13718
1
  {
13719
1
#ifdef BFD64
13720
1
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13721
#else
13722
    size += sizeof ("+0x") - 1 + 8;
13723
#endif
13724
1
  }
13725
3.50k
    }
13726
13727
25
  s = *ret = (asymbol *) bfd_malloc (size);
13728
25
  if (s == NULL)
13729
0
    return -1;
13730
13731
25
  names = (char *) (s + count);
13732
25
  p = relplt->relocation;
13733
25
  n = 0;
13734
3.52k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13735
3.50k
    {
13736
3.50k
      size_t len;
13737
3.50k
      bfd_vma addr;
13738
13739
3.50k
      addr = bed->plt_sym_val (i, plt, p);
13740
3.50k
      if (addr == (bfd_vma) -1)
13741
0
  continue;
13742
13743
3.50k
      *s = **p->sym_ptr_ptr;
13744
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13745
   we are defining a symbol, ensure one of them is set.  */
13746
3.50k
      if ((s->flags & BSF_LOCAL) == 0)
13747
3.49k
  s->flags |= BSF_GLOBAL;
13748
3.50k
      s->flags |= BSF_SYNTHETIC;
13749
3.50k
      s->section = plt;
13750
3.50k
      s->value = addr - plt->vma;
13751
3.50k
      s->name = names;
13752
3.50k
      s->udata.p = NULL;
13753
3.50k
      len = strlen ((*p->sym_ptr_ptr)->name);
13754
3.50k
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13755
3.50k
      names += len;
13756
3.50k
      if (p->addend != 0)
13757
1
  {
13758
1
    char buf[30], *a;
13759
13760
1
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13761
1
    names += sizeof ("+0x") - 1;
13762
1
    bfd_sprintf_vma (abfd, buf, p->addend);
13763
6
    for (a = buf; *a == '0'; ++a)
13764
5
      ;
13765
1
    len = strlen (a);
13766
1
    memcpy (names, a, len);
13767
1
    names += len;
13768
1
  }
13769
3.50k
      memcpy (names, "@plt", sizeof ("@plt"));
13770
3.50k
      names += sizeof ("@plt");
13771
3.50k
      ++s, ++n;
13772
3.50k
    }
13773
13774
25
  return n;
13775
25
}
13776
13777
/* It is only used by x86-64 so far.
13778
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
13779
   but current usage would allow all of _bfd_std_section to be zero.  */
13780
static const asymbol lcomm_sym
13781
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13782
asection _bfd_elf_large_com_section
13783
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13784
          "LARGE_COMMON", 0, SEC_IS_COMMON);
13785
13786
bool
13787
_bfd_elf_final_write_processing (bfd *abfd)
13788
27
{
13789
27
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13790
13791
27
  i_ehdrp = elf_elfheader (abfd);
13792
13793
27
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13794
12
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13795
13796
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13797
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13798
     or STB_GNU_UNIQUE binding.  */
13799
27
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13800
0
    {
13801
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13802
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13803
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13804
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13805
0
  {
13806
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13807
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13808
0
          "and FreeBSD targets"));
13809
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13810
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13811
0
          "only by GNU and FreeBSD targets"));
13812
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13813
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13814
0
          "only by GNU and FreeBSD targets"));
13815
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13816
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13817
0
          "only by GNU and FreeBSD targets"));
13818
0
    bfd_set_error (bfd_error_sorry);
13819
0
    return false;
13820
0
  }
13821
0
    }
13822
27
  return true;
13823
27
}
13824
13825
13826
/* Return TRUE for ELF symbol types that represent functions.
13827
   This is the default version of this function, which is sufficient for
13828
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13829
13830
bool
13831
_bfd_elf_is_function_type (unsigned int type)
13832
0
{
13833
0
  return (type == STT_FUNC
13834
0
    || type == STT_GNU_IFUNC);
13835
0
}
13836
13837
/* If the ELF symbol SYM might be a function in SEC, return the
13838
   function size and set *CODE_OFF to the function's entry point,
13839
   otherwise return zero.  */
13840
13841
bfd_size_type
13842
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13843
           bfd_vma *code_off)
13844
61.3M
{
13845
61.3M
  bfd_size_type size;
13846
61.3M
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13847
13848
61.3M
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13849
61.3M
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13850
61.3M
      || sym->section != sec)
13851
58.9M
    return 0;
13852
13853
2.43M
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13854
13855
  /* In theory we should check that the symbol's type satisfies
13856
     _bfd_elf_is_function_type(), but there are some function-like
13857
     symbols which would fail this test.  (eg _start).  Instead
13858
     we check for hidden, local, notype symbols with zero size.
13859
     This type of symbol is generated by the annobin plugin for gcc
13860
     and clang, and should not be considered to be a function symbol.  */
13861
2.43M
  if (size == 0
13862
2.43M
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13863
2.43M
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13864
2.43M
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13865
368
    return 0;
13866
13867
2.43M
  *code_off = sym->value;
13868
  /* Do not return 0 for the function's size.  */
13869
2.43M
  return size ? size : 1;
13870
2.43M
}
13871
13872
/* Set to non-zero to enable some debug messages.  */
13873
#define DEBUG_SECONDARY_RELOCS   0
13874
13875
/* An internal-to-the-bfd-library only section type
13876
   used to indicate a cached secondary reloc section.  */
13877
23.7k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13878
13879
/* Create a BFD section to hold a secondary reloc section.  */
13880
13881
bool
13882
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13883
               Elf_Internal_Shdr *hdr,
13884
               const char * name,
13885
               unsigned int shindex)
13886
10.5k
{
13887
  /* We only support RELA secondary relocs.  */
13888
10.5k
  if (hdr->sh_type != SHT_RELA)
13889
15
    return false;
13890
13891
#if DEBUG_SECONDARY_RELOCS
13892
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13893
#endif
13894
10.4k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13895
10.4k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13896
10.5k
}
13897
13898
/* Read in any secondary relocs associated with SEC.  */
13899
13900
bool
13901
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13902
          asection *  sec,
13903
          asymbol **  symbols,
13904
          bool dynamic)
13905
12.5k
{
13906
12.5k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13907
12.5k
  asection * relsec;
13908
12.5k
  bool result = true;
13909
12.5k
  bfd_vma (*r_sym) (bfd_vma);
13910
12.5k
  ufile_ptr filesize;
13911
13912
12.5k
#if BFD_DEFAULT_TARGET_SIZE > 32
13913
12.5k
  if (bfd_arch_bits_per_address (abfd) != 32)
13914
11.3k
    r_sym = elf64_r_sym;
13915
1.17k
  else
13916
1.17k
#endif
13917
1.17k
    r_sym = elf32_r_sym;
13918
13919
12.5k
  if (!elf_section_data (sec)->has_secondary_relocs)
13920
12.2k
    return true;
13921
13922
  /* Discover if there are any secondary reloc sections
13923
     associated with SEC.  */
13924
278
  filesize = bfd_get_file_size (abfd);
13925
6.90k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13926
6.62k
    {
13927
6.62k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13928
13929
6.62k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13930
6.62k
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13931
6.62k
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13932
285
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13933
285
  {
13934
285
    bfd_byte * native_relocs;
13935
285
    bfd_byte * native_reloc;
13936
285
    arelent * internal_relocs;
13937
285
    arelent * internal_reloc;
13938
285
    size_t i;
13939
285
    unsigned int entsize;
13940
285
    unsigned int symcount;
13941
285
    bfd_size_type reloc_count;
13942
285
    size_t amt;
13943
13944
285
    if (ebd->elf_info_to_howto == NULL)
13945
0
      return false;
13946
13947
#if DEBUG_SECONDARY_RELOCS
13948
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13949
       sec->name, relsec->name);
13950
#endif
13951
285
    entsize = hdr->sh_entsize;
13952
13953
285
    if (filesize != 0
13954
285
        && ((ufile_ptr) hdr->sh_offset > filesize
13955
285
      || hdr->sh_size > filesize - hdr->sh_offset))
13956
206
      {
13957
206
        bfd_set_error (bfd_error_file_truncated);
13958
206
        result = false;
13959
206
        continue;
13960
206
      }
13961
13962
79
    native_relocs = bfd_malloc (hdr->sh_size);
13963
79
    if (native_relocs == NULL)
13964
0
      {
13965
0
        result = false;
13966
0
        continue;
13967
0
      }
13968
13969
79
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13970
79
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13971
0
      {
13972
0
        free (native_relocs);
13973
0
        bfd_set_error (bfd_error_file_too_big);
13974
0
        result = false;
13975
0
        continue;
13976
0
      }
13977
13978
79
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13979
79
    if (internal_relocs == NULL)
13980
0
      {
13981
0
        free (native_relocs);
13982
0
        result = false;
13983
0
        continue;
13984
0
      }
13985
13986
79
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13987
79
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13988
2
      {
13989
2
        free (native_relocs);
13990
        /* The internal_relocs will be freed when
13991
     the memory for the bfd is released.  */
13992
2
        result = false;
13993
2
        continue;
13994
2
      }
13995
13996
77
    if (dynamic)
13997
0
      symcount = bfd_get_dynamic_symcount (abfd);
13998
77
    else
13999
77
      symcount = bfd_get_symcount (abfd);
14000
14001
77
    for (i = 0, internal_reloc = internal_relocs,
14002
77
     native_reloc = native_relocs;
14003
1.41k
         i < reloc_count;
14004
1.33k
         i++, internal_reloc++, native_reloc += entsize)
14005
1.33k
      {
14006
1.33k
        bool res;
14007
1.33k
        Elf_Internal_Rela rela;
14008
14009
1.33k
        if (entsize == ebd->s->sizeof_rel)
14010
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
14011
1.33k
        else /* entsize == ebd->s->sizeof_rela */
14012
1.33k
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
14013
14014
        /* The address of an ELF reloc is section relative for an object
14015
     file, and absolute for an executable file or shared library.
14016
     The address of a normal BFD reloc is always section relative,
14017
     and the address of a dynamic reloc is absolute..  */
14018
1.33k
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
14019
1.29k
    internal_reloc->address = rela.r_offset;
14020
37
        else
14021
37
    internal_reloc->address = rela.r_offset - sec->vma;
14022
14023
1.33k
        if (r_sym (rela.r_info) == STN_UNDEF)
14024
96
    {
14025
      /* FIXME: This and the error case below mean that we
14026
         have a symbol on relocs that is not elf_symbol_type.  */
14027
96
      internal_reloc->sym_ptr_ptr =
14028
96
        bfd_abs_section_ptr->symbol_ptr_ptr;
14029
96
    }
14030
1.24k
        else if (r_sym (rela.r_info) > symcount)
14031
0
    {
14032
0
      _bfd_error_handler
14033
        /* xgettext:c-format */
14034
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
14035
0
         abfd, sec, i, (long) r_sym (rela.r_info));
14036
0
      bfd_set_error (bfd_error_bad_value);
14037
0
      internal_reloc->sym_ptr_ptr =
14038
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
14039
0
      result = false;
14040
0
    }
14041
1.24k
        else
14042
1.24k
    {
14043
1.24k
      asymbol **ps;
14044
14045
1.24k
      ps = symbols + r_sym (rela.r_info) - 1;
14046
1.24k
      internal_reloc->sym_ptr_ptr = ps;
14047
      /* Make sure that this symbol is not removed by strip.  */
14048
1.24k
      (*ps)->flags |= BSF_KEEP;
14049
1.24k
    }
14050
14051
1.33k
        internal_reloc->addend = rela.r_addend;
14052
14053
1.33k
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
14054
1.33k
        if (! res || internal_reloc->howto == NULL)
14055
10
    {
14056
#if DEBUG_SECONDARY_RELOCS
14057
      fprintf (stderr,
14058
         "there is no howto associated with reloc %lx\n",
14059
         rela.r_info);
14060
#endif
14061
10
      result = false;
14062
10
    }
14063
1.33k
      }
14064
14065
77
    free (native_relocs);
14066
    /* Store the internal relocs.  */
14067
77
    elf_section_data (relsec)->sec_info = internal_relocs;
14068
77
  }
14069
6.62k
    }
14070
14071
278
  return result;
14072
278
}
14073
14074
/* Set the ELF section header fields of an output secondary reloc section.  */
14075
14076
bool
14077
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14078
              bfd *obfd ATTRIBUTE_UNUSED,
14079
              const Elf_Internal_Shdr *isection,
14080
              Elf_Internal_Shdr *osection)
14081
21
{
14082
21
  asection * isec;
14083
21
  asection * osec;
14084
21
  struct bfd_elf_section_data * esd;
14085
14086
21
  if (isection == NULL)
14087
0
    return false;
14088
14089
21
  if (isection->sh_type != SHT_SECONDARY_RELOC)
14090
21
    return true;
14091
14092
0
  isec = isection->bfd_section;
14093
0
  if (isec == NULL)
14094
0
    return false;
14095
14096
0
  osec = osection->bfd_section;
14097
0
  if (osec == NULL)
14098
0
    return false;
14099
14100
0
  esd = elf_section_data (osec);
14101
0
  BFD_ASSERT (esd->sec_info == NULL);
14102
0
  esd->sec_info = elf_section_data (isec)->sec_info;
14103
0
  osection->sh_type = SHT_RELA;
14104
0
  osection->sh_link = elf_onesymtab (obfd);
14105
0
  if (osection->sh_link == 0)
14106
0
    {
14107
      /* There is no symbol table - we are hosed...  */
14108
0
      _bfd_error_handler
14109
  /* xgettext:c-format */
14110
0
  (_("%pB(%pA): link section cannot be set"
14111
0
     " because the output file does not have a symbol table"),
14112
0
  obfd, osec);
14113
0
      bfd_set_error (bfd_error_bad_value);
14114
0
      return false;
14115
0
    }
14116
14117
  /* Find the output section that corresponds to the isection's
14118
     sh_info link.  */
14119
0
  if (isection->sh_info == 0
14120
0
      || isection->sh_info >= elf_numsections (ibfd))
14121
0
    {
14122
0
      _bfd_error_handler
14123
  /* xgettext:c-format */
14124
0
  (_("%pB(%pA): info section index is invalid"),
14125
0
  obfd, osec);
14126
0
      bfd_set_error (bfd_error_bad_value);
14127
0
      return false;
14128
0
    }
14129
14130
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
14131
14132
0
  if (isection == NULL
14133
0
      || isection->bfd_section == NULL
14134
0
      || isection->bfd_section->output_section == NULL)
14135
0
    {
14136
0
      _bfd_error_handler
14137
  /* xgettext:c-format */
14138
0
  (_("%pB(%pA): info section index cannot be set"
14139
0
     " because the section is not in the output"),
14140
0
  obfd, osec);
14141
0
      bfd_set_error (bfd_error_bad_value);
14142
0
      return false;
14143
0
    }
14144
14145
0
  esd = elf_section_data (isection->bfd_section->output_section);
14146
0
  BFD_ASSERT (esd != NULL);
14147
0
  osection->sh_info = esd->this_idx;
14148
0
  esd->has_secondary_relocs = true;
14149
#if DEBUG_SECONDARY_RELOCS
14150
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14151
     osec->name, osection->sh_link, osection->sh_info);
14152
  fprintf (stderr, "mark section %s as having secondary relocs\n",
14153
     bfd_section_name (isection->bfd_section->output_section));
14154
#endif
14155
14156
0
  return true;
14157
0
}
14158
14159
/* Write out a secondary reloc section.
14160
14161
   FIXME: Currently this function can result in a serious performance penalty
14162
   for files with secondary relocs and lots of sections.  The proper way to
14163
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14164
   relocs together and then to have this function just walk that chain.  */
14165
14166
bool
14167
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14168
0
{
14169
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14170
0
  bfd_vma addr_offset;
14171
0
  asection * relsec;
14172
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
14173
0
  bool result = true;
14174
14175
0
  if (sec == NULL)
14176
0
    return false;
14177
14178
0
#if BFD_DEFAULT_TARGET_SIZE > 32
14179
0
  if (bfd_arch_bits_per_address (abfd) != 32)
14180
0
    r_info = elf64_r_info;
14181
0
  else
14182
0
#endif
14183
0
    r_info = elf32_r_info;
14184
14185
  /* The address of an ELF reloc is section relative for an object
14186
     file, and absolute for an executable file or shared library.
14187
     The address of a BFD reloc is always section relative.  */
14188
0
  addr_offset = 0;
14189
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14190
0
    addr_offset = sec->vma;
14191
14192
  /* Discover if there are any secondary reloc sections
14193
     associated with SEC.  */
14194
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14195
0
    {
14196
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14197
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14198
14199
0
      if (hdr->sh_type == SHT_RELA
14200
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14201
0
  {
14202
0
    asymbol *    last_sym;
14203
0
    int          last_sym_idx;
14204
0
    size_t       reloc_count;
14205
0
    size_t       idx;
14206
0
    bfd_size_type entsize;
14207
0
    arelent *    src_irel;
14208
0
    bfd_byte *   dst_rela;
14209
14210
0
    if (hdr->contents != NULL)
14211
0
      {
14212
0
        _bfd_error_handler
14213
    /* xgettext:c-format */
14214
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
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
    entsize = hdr->sh_entsize;
14222
0
    if (entsize == 0)
14223
0
      {
14224
0
        _bfd_error_handler
14225
    /* xgettext:c-format */
14226
0
    (_("%pB(%pA): error: secondary reloc section"
14227
0
       " has zero sized entries"),
14228
0
     abfd, relsec);
14229
0
        bfd_set_error (bfd_error_bad_value);
14230
0
        result = false;
14231
0
        continue;
14232
0
      }
14233
0
    else if (entsize != ebd->s->sizeof_rel
14234
0
       && entsize != ebd->s->sizeof_rela)
14235
0
      {
14236
0
        _bfd_error_handler
14237
    /* xgettext:c-format */
14238
0
    (_("%pB(%pA): error: secondary reloc section"
14239
0
       " has non-standard sized entries"),
14240
0
     abfd, relsec);
14241
0
        bfd_set_error (bfd_error_bad_value);
14242
0
        result = false;
14243
0
        continue;
14244
0
      }
14245
14246
0
    reloc_count = hdr->sh_size / entsize;
14247
0
    hdr->sh_size = entsize * reloc_count;
14248
0
    if (reloc_count == 0)
14249
0
      {
14250
0
        _bfd_error_handler
14251
    /* xgettext:c-format */
14252
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
14253
0
     abfd, relsec);
14254
0
        bfd_set_error (bfd_error_bad_value);
14255
0
        result = false;
14256
0
        continue;
14257
0
      }
14258
14259
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14260
0
    if (hdr->contents == NULL)
14261
0
      continue;
14262
14263
#if DEBUG_SECONDARY_RELOCS
14264
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14265
       reloc_count, sec->name, relsec->name);
14266
#endif
14267
0
    last_sym = NULL;
14268
0
    last_sym_idx = 0;
14269
0
    dst_rela = hdr->contents;
14270
0
    src_irel = (arelent *) esd->sec_info;
14271
0
    if (src_irel == NULL)
14272
0
      {
14273
0
        _bfd_error_handler
14274
    /* xgettext:c-format */
14275
0
    (_("%pB(%pA): error: internal relocs missing"
14276
0
       " for secondary reloc section"),
14277
0
     abfd, relsec);
14278
0
        bfd_set_error (bfd_error_bad_value);
14279
0
        result = false;
14280
0
        continue;
14281
0
      }
14282
14283
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14284
0
      {
14285
0
        Elf_Internal_Rela src_rela;
14286
0
        arelent *ptr;
14287
0
        asymbol *sym;
14288
0
        int n;
14289
14290
0
        ptr = src_irel + idx;
14291
0
        if (ptr == NULL)
14292
0
    {
14293
0
      _bfd_error_handler
14294
        /* xgettext:c-format */
14295
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14296
0
         abfd, relsec, idx);
14297
0
      bfd_set_error (bfd_error_bad_value);
14298
0
      result = false;
14299
0
      break;
14300
0
    }
14301
14302
0
        if (ptr->sym_ptr_ptr == NULL)
14303
0
    {
14304
      /* FIXME: Is this an error ? */
14305
0
      n = 0;
14306
0
    }
14307
0
        else
14308
0
    {
14309
0
      sym = *ptr->sym_ptr_ptr;
14310
14311
0
      if (sym == last_sym)
14312
0
        n = last_sym_idx;
14313
0
      else
14314
0
        {
14315
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14316
0
          if (n < 0)
14317
0
      {
14318
0
        _bfd_error_handler
14319
          /* xgettext:c-format */
14320
0
          (_("%pB(%pA): error: secondary reloc %zu"
14321
0
             " references a missing symbol"),
14322
0
           abfd, relsec, idx);
14323
0
        bfd_set_error (bfd_error_bad_value);
14324
0
        result = false;
14325
0
        n = 0;
14326
0
      }
14327
14328
0
          last_sym = sym;
14329
0
          last_sym_idx = n;
14330
0
        }
14331
14332
0
      if (sym->the_bfd != NULL
14333
0
          && sym->the_bfd->xvec != abfd->xvec
14334
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14335
0
        {
14336
0
          _bfd_error_handler
14337
      /* xgettext:c-format */
14338
0
      (_("%pB(%pA): error: secondary reloc %zu"
14339
0
         " references a deleted symbol"),
14340
0
       abfd, relsec, idx);
14341
0
          bfd_set_error (bfd_error_bad_value);
14342
0
          result = false;
14343
0
          n = 0;
14344
0
        }
14345
0
    }
14346
14347
0
        src_rela.r_offset = ptr->address + addr_offset;
14348
0
        if (ptr->howto == NULL)
14349
0
    {
14350
0
      _bfd_error_handler
14351
        /* xgettext:c-format */
14352
0
        (_("%pB(%pA): error: secondary reloc %zu"
14353
0
           " is of an unknown type"),
14354
0
         abfd, relsec, idx);
14355
0
      bfd_set_error (bfd_error_bad_value);
14356
0
      result = false;
14357
0
      src_rela.r_info = r_info (0, 0);
14358
0
    }
14359
0
        else
14360
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14361
0
        src_rela.r_addend = ptr->addend;
14362
14363
0
        if (entsize == ebd->s->sizeof_rel)
14364
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14365
0
        else /* entsize == ebd->s->sizeof_rela */
14366
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14367
0
      }
14368
0
  }
14369
0
    }
14370
14371
0
  return result;
14372
0
}
14373
14374
/* Mmap in section contents.  If FINAL_LINK is false, set *BUF to NULL
14375
   before calling bfd_get_full_section_contents.  */
14376
14377
static bool
14378
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf,
14379
         bool final_link)
14380
43.2k
{
14381
43.2k
#ifdef USE_MMAP
14382
43.2k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14383
43.2k
  if (bed->use_mmap
14384
43.2k
      && sec->compress_status == COMPRESS_SECTION_NONE
14385
43.2k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14386
1.49k
    {
14387
      /* Use mmap only if section size >= the minimum mmap section
14388
   size.  */
14389
1.49k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14390
1.49k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14391
1.49k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14392
446
  {
14393
446
    if (sec->contents != NULL)
14394
0
      {
14395
0
        if (!sec->mmapped_p)
14396
0
    abort ();
14397
0
        *buf = sec->contents;
14398
0
        return true;
14399
0
      }
14400
446
    if (sec->mmapped_p)
14401
0
      abort ();
14402
446
    sec->mmapped_p = 1;
14403
14404
    /* Never use the preallocated buffer if mmapp is used.  */
14405
446
    *buf = NULL;
14406
446
  }
14407
1.49k
    }
14408
43.2k
#endif
14409
  /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is
14410
     true.  If FINAL_LINK is false, *BUF is set to the preallocated
14411
     buffer if USE_MMAP is undefined and *BUF is set to NULL if
14412
     USE_MMAP is defined.  */
14413
43.2k
  if (!final_link)
14414
43.2k
    *buf = NULL;
14415
43.2k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14416
43.2k
  if (ret && sec->mmapped_p)
14417
0
    *buf = sec->contents;
14418
43.2k
  return ret;
14419
43.2k
}
14420
14421
/* Mmap in section contents.  */
14422
14423
bool
14424
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14425
43.2k
{
14426
43.2k
  return elf_mmap_section_contents (abfd, sec, buf, false);
14427
43.2k
}
14428
14429
/* Mmap in the full section contents for the final link.  */
14430
14431
bool
14432
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14433
             bfd_byte **buf)
14434
0
{
14435
0
  return elf_mmap_section_contents (abfd, sec, buf, true);
14436
0
}
14437
14438
/* Munmap section contents.  */
14439
14440
void
14441
_bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
14442
          void *contents)
14443
41.5k
{
14444
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14445
     CONTENTS may be NULL.  */
14446
41.5k
  if (contents == NULL)
14447
75
    return;
14448
14449
41.4k
#ifdef USE_MMAP
14450
41.4k
  if (sec->mmapped_p)
14451
0
    {
14452
      /* _bfd_elf_mmap_section_contents may return the previously
14453
   mapped section contents.  Munmap the section contents only
14454
   if they haven't been cached.  */
14455
0
      if (elf_section_data (sec)->this_hdr.contents == contents)
14456
0
  return;
14457
14458
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14459
   malloced, CONTENTS_ADDR is set to NULL.  */
14460
0
      if (elf_section_data (sec)->contents_addr != NULL)
14461
0
  {
14462
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14463
0
    if (munmap (elf_section_data (sec)->contents_addr,
14464
0
          elf_section_data (sec)->contents_size) != 0)
14465
0
      abort ();
14466
0
    sec->mmapped_p = 0;
14467
0
    sec->contents = NULL;
14468
0
    elf_section_data (sec)->contents_addr = NULL;
14469
0
    elf_section_data (sec)->contents_size = 0;
14470
0
    return;
14471
0
  }
14472
0
    }
14473
41.4k
#endif
14474
14475
41.4k
  free (contents);
14476
41.4k
}
14477
14478
/* Munmap the full section contents for the final link.  */
14479
14480
void
14481
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14482
0
{
14483
0
#ifdef USE_MMAP
14484
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14485
0
    {
14486
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14487
   malloced, CONTENTS_ADDR is set to NULL.  */
14488
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14489
0
      if (munmap (elf_section_data (sec)->contents_addr,
14490
0
      elf_section_data (sec)->contents_size) != 0)
14491
0
  abort ();
14492
0
      sec->mmapped_p = 0;
14493
0
      sec->contents = NULL;
14494
0
      elf_section_data (sec)->contents_addr = NULL;
14495
0
      elf_section_data (sec)->contents_size = 0;
14496
0
    }
14497
0
#endif
14498
0
}