Coverage Report

Created: 2023-06-29 07:13

/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-2023 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
101
{
70
101
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
101
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
101
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
101
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
101
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
101
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
101
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
101
}
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
19
{
102
19
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
103
19
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
104
19
}
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
419
{
124
419
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
419
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
419
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
419
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
419
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
419
}
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
711
{
152
711
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
711
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
711
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
711
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
711
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
711
}
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
60.3k
{
180
60.3k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
60.3k
}
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
13.9M
{
231
13.9M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232
13.9M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
233
13.9M
  if (abfd->tdata.any == NULL)
234
0
    return false;
235
236
13.9M
  elf_object_id (abfd) = object_id;
237
13.9M
  if (abfd->direction != read_direction)
238
864
    {
239
864
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240
864
      if (o == NULL)
241
0
  return false;
242
864
      elf_tdata (abfd)->o = o;
243
864
      elf_program_header_size (abfd) = (bfd_size_type) -1;
244
864
    }
245
13.9M
  return true;
246
13.9M
}
247
248
249
bool
250
bfd_elf_make_object (bfd *abfd)
251
6.68M
{
252
6.68M
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253
6.68M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254
6.68M
          bed->target_id);
255
6.68M
}
256
257
bool
258
bfd_elf_mkcorefile (bfd *abfd)
259
513k
{
260
  /* I think this can be done just like an object file.  */
261
513k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262
0
    return false;
263
513k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264
513k
  return elf_tdata (abfd)->core != NULL;
265
513k
}
266
267
char *
268
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
269
311k
{
270
311k
  Elf_Internal_Shdr **i_shdrp;
271
311k
  bfd_byte *shstrtab = NULL;
272
311k
  file_ptr offset;
273
311k
  bfd_size_type shstrtabsize;
274
275
311k
  i_shdrp = elf_elfsections (abfd);
276
311k
  if (i_shdrp == 0
277
311k
      || shindex >= elf_numsections (abfd)
278
311k
      || i_shdrp[shindex] == 0)
279
0
    return NULL;
280
281
311k
  shstrtab = i_shdrp[shindex]->contents;
282
311k
  if (shstrtab == NULL)
283
311k
    {
284
      /* No cached one, attempt to read, and cache what we read.  */
285
311k
      offset = i_shdrp[shindex]->sh_offset;
286
311k
      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
311k
      if (shstrtabsize + 1 <= 1
291
311k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
292
311k
    || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293
226k
                shstrtabsize)) == NULL)
294
89.9k
  {
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
89.9k
    i_shdrp[shindex]->sh_size = 0;
299
89.9k
  }
300
221k
      else
301
221k
  shstrtab[shstrtabsize] = '\0';
302
311k
      i_shdrp[shindex]->contents = shstrtab;
303
311k
    }
304
311k
  return (char *) shstrtab;
305
311k
}
306
307
char *
308
bfd_elf_string_from_elf_section (bfd *abfd,
309
         unsigned int shindex,
310
         unsigned int strindex)
311
5.12M
{
312
5.12M
  Elf_Internal_Shdr *hdr;
313
314
5.12M
  if (strindex == 0)
315
2.06M
    return "";
316
317
3.06M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318
84.1k
    return NULL;
319
320
2.97M
  hdr = elf_elfsections (abfd)[shindex];
321
322
2.97M
  if (hdr->contents == NULL)
323
319k
    {
324
319k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325
7.97k
  {
326
    /* PR 17512: file: f057ec89.  */
327
    /* xgettext:c-format */
328
7.97k
    _bfd_error_handler (_("%pB: attempt to load strings from"
329
7.97k
        " a non-string section (number %d)"),
330
7.97k
            abfd, shindex);
331
7.97k
    return NULL;
332
7.97k
  }
333
334
311k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335
89.9k
  return NULL;
336
311k
    }
337
2.66M
  else
338
2.66M
    {
339
      /* PR 24273: The string section's contents may have already
340
   been loaded elsewhere, eg because a corrupt file has the
341
   string section index in the ELF header pointing at a group
342
   section.  So be paranoid, and test that the last byte of
343
   the section is zero.  */
344
2.66M
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345
75.2k
  return NULL;
346
2.66M
    }
347
348
2.80M
  if (strindex >= hdr->sh_size)
349
181k
    {
350
181k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351
181k
      _bfd_error_handler
352
  /* xgettext:c-format */
353
181k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354
181k
   abfd, strindex, (uint64_t) hdr->sh_size,
355
181k
   (shindex == shstrndx && strindex == hdr->sh_name
356
181k
    ? ".shstrtab"
357
181k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
181k
      return NULL;
359
181k
    }
360
361
2.62M
  return ((char *) hdr->contents) + strindex;
362
2.80M
}
363
364
/* Read and convert symbols to internal format.
365
   SYMCOUNT specifies the number of symbols to read, starting from
366
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367
   are non-NULL, they are used to store the internal symbols, external
368
   symbols, and symbol section index extensions, respectively.
369
   Returns a pointer to the internal symbol buffer (malloced if necessary)
370
   or NULL if there were no symbols or some kind of problem.  */
371
372
Elf_Internal_Sym *
373
bfd_elf_get_elf_syms (bfd *ibfd,
374
          Elf_Internal_Shdr *symtab_hdr,
375
          size_t symcount,
376
          size_t symoffset,
377
          Elf_Internal_Sym *intsym_buf,
378
          void *extsym_buf,
379
          Elf_External_Sym_Shndx *extshndx_buf)
380
48.0k
{
381
48.0k
  Elf_Internal_Shdr *shndx_hdr;
382
48.0k
  void *alloc_ext;
383
48.0k
  const bfd_byte *esym;
384
48.0k
  Elf_External_Sym_Shndx *alloc_extshndx;
385
48.0k
  Elf_External_Sym_Shndx *shndx;
386
48.0k
  Elf_Internal_Sym *alloc_intsym;
387
48.0k
  Elf_Internal_Sym *isym;
388
48.0k
  Elf_Internal_Sym *isymend;
389
48.0k
  const struct elf_backend_data *bed;
390
48.0k
  size_t extsym_size;
391
48.0k
  size_t amt;
392
48.0k
  file_ptr pos;
393
394
48.0k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
0
    abort ();
396
397
48.0k
  if (symcount == 0)
398
0
    return intsym_buf;
399
400
  /* Normal syms might have section extension entries.  */
401
48.0k
  shndx_hdr = NULL;
402
48.0k
  if (elf_symtab_shndx_list (ibfd) != NULL)
403
2.74k
    {
404
2.74k
      elf_section_list * entry;
405
2.74k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
406
407
      /* Find an index section that is linked to this symtab section.  */
408
5.86k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
409
4.06k
  {
410
    /* PR 20063.  */
411
4.06k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
412
35
      continue;
413
414
4.02k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
415
947
      {
416
947
        shndx_hdr = & entry->hdr;
417
947
        break;
418
3.08k
      };
419
3.08k
  }
420
421
2.74k
      if (shndx_hdr == NULL)
422
1.79k
  {
423
1.79k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
424
      /* Not really accurate, but this was how the old code used
425
         to work.  */
426
1.79k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
427
    /* Otherwise we do nothing.  The assumption is that
428
       the index table will not be needed.  */
429
1.79k
  }
430
2.74k
    }
431
432
  /* Read the symbols.  */
433
48.0k
  alloc_ext = NULL;
434
48.0k
  alloc_extshndx = NULL;
435
48.0k
  alloc_intsym = NULL;
436
48.0k
  bed = get_elf_backend_data (ibfd);
437
48.0k
  extsym_size = bed->s->sizeof_sym;
438
48.0k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
439
0
    {
440
0
      bfd_set_error (bfd_error_file_too_big);
441
0
      intsym_buf = NULL;
442
0
      goto out;
443
0
    }
444
48.0k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445
48.0k
  if (extsym_buf == NULL)
446
4.25k
    {
447
4.25k
      alloc_ext = bfd_malloc (amt);
448
4.25k
      extsym_buf = alloc_ext;
449
4.25k
    }
450
48.0k
  if (extsym_buf == NULL
451
48.0k
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
452
48.0k
      || bfd_bread (extsym_buf, amt, ibfd) != amt)
453
2.08k
    {
454
2.08k
      intsym_buf = NULL;
455
2.08k
      goto out;
456
2.08k
    }
457
458
45.9k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
459
43.7k
    extshndx_buf = NULL;
460
2.19k
  else
461
2.19k
    {
462
2.19k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
463
0
  {
464
0
    bfd_set_error (bfd_error_file_too_big);
465
0
    intsym_buf = NULL;
466
0
    goto out;
467
0
  }
468
2.19k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
469
2.19k
      if (extshndx_buf == NULL)
470
489
  {
471
489
    alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
472
489
    extshndx_buf = alloc_extshndx;
473
489
  }
474
2.19k
      if (extshndx_buf == NULL
475
2.19k
    || bfd_seek (ibfd, pos, SEEK_SET) != 0
476
2.19k
    || bfd_bread (extshndx_buf, amt, ibfd) != amt)
477
97
  {
478
97
    intsym_buf = NULL;
479
97
    goto out;
480
97
  }
481
2.19k
    }
482
483
45.8k
  if (intsym_buf == NULL)
484
4.20k
    {
485
4.20k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
486
0
  {
487
0
    bfd_set_error (bfd_error_file_too_big);
488
0
    goto out;
489
0
  }
490
4.20k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
491
4.20k
      intsym_buf = alloc_intsym;
492
4.20k
      if (intsym_buf == NULL)
493
0
  goto out;
494
4.20k
    }
495
496
  /* Convert the symbols to internal form.  */
497
45.8k
  isymend = intsym_buf + symcount;
498
45.8k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
499
45.8k
     shndx = extshndx_buf;
500
984k
       isym < isymend;
501
938k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
502
939k
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
503
1.06k
      {
504
1.06k
  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
505
  /* xgettext:c-format */
506
1.06k
  _bfd_error_handler (_("%pB symbol number %lu references"
507
1.06k
            " nonexistent SHT_SYMTAB_SHNDX section"),
508
1.06k
          ibfd, (unsigned long) symoffset);
509
1.06k
  free (alloc_intsym);
510
1.06k
  intsym_buf = NULL;
511
1.06k
  goto out;
512
1.06k
      }
513
514
48.0k
 out:
515
48.0k
  free (alloc_ext);
516
48.0k
  free (alloc_extshndx);
517
518
48.0k
  return intsym_buf;
519
45.8k
}
520
521
/* Look up a symbol name.  */
522
const char *
523
bfd_elf_sym_name (bfd *abfd,
524
      Elf_Internal_Shdr *symtab_hdr,
525
      Elf_Internal_Sym *isym,
526
      asection *sym_sec)
527
915k
{
528
915k
  const char *name;
529
915k
  unsigned int iname = isym->st_name;
530
915k
  unsigned int shindex = symtab_hdr->sh_link;
531
532
915k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
533
      /* Check for a bogus st_shndx to avoid crashing.  */
534
915k
      && isym->st_shndx < elf_numsections (abfd))
535
55.1k
    {
536
55.1k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
537
55.1k
      shindex = elf_elfheader (abfd)->e_shstrndx;
538
55.1k
    }
539
540
915k
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
541
915k
  if (name == NULL)
542
388k
    name = "(null)";
543
526k
  else if (sym_sec && *name == '\0')
544
0
    name = bfd_section_name (sym_sec);
545
546
915k
  return name;
547
915k
}
548
549
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
550
   sections.  The first element is the flags, the rest are section
551
   pointers.  */
552
553
typedef union elf_internal_group {
554
  Elf_Internal_Shdr *shdr;
555
  unsigned int flags;
556
} Elf_Internal_Group;
557
558
/* Return the name of the group signature symbol.  Why isn't the
559
   signature just a string?  */
560
561
static const char *
562
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
563
46.4k
{
564
46.4k
  Elf_Internal_Shdr *hdr;
565
46.4k
  unsigned char esym[sizeof (Elf64_External_Sym)];
566
46.4k
  Elf_External_Sym_Shndx eshndx;
567
46.4k
  Elf_Internal_Sym isym;
568
569
  /* First we need to ensure the symbol table is available.  Make sure
570
     that it is a symbol table section.  */
571
46.4k
  if (ghdr->sh_link >= elf_numsections (abfd))
572
13
    return NULL;
573
46.4k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
574
46.4k
  if (hdr->sh_type != SHT_SYMTAB
575
46.4k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
576
2.64k
    return NULL;
577
578
  /* Go read the symbol.  */
579
43.7k
  hdr = &elf_tdata (abfd)->symtab_hdr;
580
43.7k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
581
43.7k
          &isym, esym, &eshndx) == NULL)
582
3.08k
    return NULL;
583
584
40.6k
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
585
43.7k
}
586
587
/* Set next_in_group list pointer, and group name for NEWSECT.  */
588
589
static bool
590
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
591
350k
{
592
350k
  unsigned int num_group = elf_tdata (abfd)->num_group;
593
594
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
595
     is set to -1 if there are no SHT_GROUP sections.  */
596
350k
  if (num_group == 0)
597
139k
    {
598
139k
      unsigned int i, shnum;
599
600
      /* First count the number of groups.  If we have a SHT_GROUP
601
   section with just a flag word (ie. sh_size is 4), ignore it.  */
602
139k
      shnum = elf_numsections (abfd);
603
139k
      num_group = 0;
604
605
139k
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)  \
606
3.37M
  (   (shdr)->sh_type == SHT_GROUP    \
607
3.37M
   && (shdr)->sh_size >= minsize      \
608
3.37M
   && (shdr)->sh_entsize == GRP_ENTRY_SIZE  \
609
3.37M
   && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
610
611
2.64M
      for (i = 0; i < shnum; i++)
612
2.50M
  {
613
2.50M
    Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
614
615
2.50M
    if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
616
61.2k
      num_group += 1;
617
2.50M
  }
618
619
139k
      if (num_group == 0)
620
109k
  {
621
109k
    num_group = (unsigned) -1;
622
109k
    elf_tdata (abfd)->num_group = num_group;
623
109k
    elf_tdata (abfd)->group_sect_ptr = NULL;
624
109k
  }
625
29.9k
      else
626
29.9k
  {
627
    /* We keep a list of elf section headers for group sections,
628
       so we can find them quickly.  */
629
29.9k
    size_t amt;
630
631
29.9k
    elf_tdata (abfd)->num_group = num_group;
632
29.9k
    amt = num_group * sizeof (Elf_Internal_Shdr *);
633
29.9k
    elf_tdata (abfd)->group_sect_ptr
634
29.9k
      = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
635
29.9k
    if (elf_tdata (abfd)->group_sect_ptr == NULL)
636
0
      return false;
637
29.9k
    num_group = 0;
638
639
771k
    for (i = 0; i < shnum; i++)
640
742k
      {
641
742k
        Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
642
643
742k
        if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
644
61.0k
    {
645
61.0k
      unsigned char *src;
646
61.0k
      Elf_Internal_Group *dest;
647
648
      /* Make sure the group section has a BFD section
649
         attached to it.  */
650
61.0k
      if (!bfd_section_from_shdr (abfd, i))
651
1.19k
        return false;
652
653
      /* Add to list of sections.  */
654
59.8k
      elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
655
59.8k
      num_group += 1;
656
657
      /* Read the raw contents.  */
658
59.8k
      BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
659
59.8k
      shdr->contents = NULL;
660
59.8k
      if (_bfd_mul_overflow (shdr->sh_size,
661
59.8k
           sizeof (*dest) / 4, &amt)
662
59.8k
          || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
663
59.8k
          || !(shdr->contents
664
58.3k
         = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
665
5.55k
        {
666
5.55k
          _bfd_error_handler
667
      /* xgettext:c-format */
668
5.55k
      (_("%pB: invalid size field in group section"
669
5.55k
         " header: %#" PRIx64 ""),
670
5.55k
       abfd, (uint64_t) shdr->sh_size);
671
5.55k
          bfd_set_error (bfd_error_bad_value);
672
5.55k
          -- num_group;
673
5.55k
          continue;
674
5.55k
        }
675
676
      /* Translate raw contents, a flag word followed by an
677
         array of elf section indices all in target byte order,
678
         to the flag word followed by an array of elf section
679
         pointers.  */
680
54.3k
      src = shdr->contents + shdr->sh_size;
681
54.3k
      dest = (Elf_Internal_Group *) (shdr->contents + amt);
682
683
2.65M
      while (1)
684
2.65M
        {
685
2.65M
          unsigned int idx;
686
687
2.65M
          src -= 4;
688
2.65M
          --dest;
689
2.65M
          idx = H_GET_32 (abfd, src);
690
2.65M
          if (src == shdr->contents)
691
54.3k
      {
692
54.3k
        dest->shdr = NULL;
693
54.3k
        dest->flags = idx;
694
54.3k
        if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
695
19.7k
          shdr->bfd_section->flags
696
19.7k
            |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
697
54.3k
        break;
698
54.3k
      }
699
2.60M
          if (idx < shnum)
700
713k
      {
701
713k
        dest->shdr = elf_elfsections (abfd)[idx];
702
        /* PR binutils/23199: All sections in a
703
           section group should be marked with
704
           SHF_GROUP.  But some tools generate
705
           broken objects without SHF_GROUP.  Fix
706
           them up here.  */
707
713k
        dest->shdr->sh_flags |= SHF_GROUP;
708
713k
      }
709
2.60M
          if (idx >= shnum
710
2.60M
        || dest->shdr->sh_type == SHT_GROUP)
711
1.89M
      {
712
1.89M
        _bfd_error_handler
713
1.89M
          (_("%pB: invalid entry in SHT_GROUP section [%u]"),
714
1.89M
             abfd, i);
715
1.89M
        dest->shdr = NULL;
716
1.89M
      }
717
2.60M
        }
718
54.3k
    }
719
742k
      }
720
721
    /* PR 17510: Corrupt binaries might contain invalid groups.  */
722
28.7k
    if (num_group != (unsigned) elf_tdata (abfd)->num_group)
723
5.05k
      {
724
5.05k
        elf_tdata (abfd)->num_group = num_group;
725
726
        /* If all groups are invalid then fail.  */
727
5.05k
        if (num_group == 0)
728
4.14k
    {
729
4.14k
      elf_tdata (abfd)->group_sect_ptr = NULL;
730
4.14k
      elf_tdata (abfd)->num_group = num_group = -1;
731
4.14k
      _bfd_error_handler
732
4.14k
        (_("%pB: no valid group sections found"), abfd);
733
4.14k
      bfd_set_error (bfd_error_bad_value);
734
4.14k
    }
735
5.05k
      }
736
28.7k
  }
737
139k
    }
738
739
349k
  if (num_group != (unsigned) -1)
740
132k
    {
741
132k
      unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
742
132k
      unsigned int j;
743
744
768k
      for (j = 0; j < num_group; j++)
745
641k
  {
746
    /* Begin search from previous found group.  */
747
641k
    unsigned i = (j + search_offset) % num_group;
748
749
641k
    Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
750
641k
    Elf_Internal_Group *idx;
751
641k
    bfd_size_type n_elt;
752
753
641k
    if (shdr == NULL)
754
5.16k
      continue;
755
756
636k
    idx = (Elf_Internal_Group *) shdr->contents;
757
636k
    if (idx == NULL || shdr->sh_size < 4)
758
257
      {
759
        /* See PR 21957 for a reproducer.  */
760
        /* xgettext:c-format */
761
257
        _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
762
257
          abfd, shdr->bfd_section);
763
257
        elf_tdata (abfd)->group_sect_ptr[i] = NULL;
764
257
        bfd_set_error (bfd_error_bad_value);
765
257
        return false;
766
257
      }
767
636k
    n_elt = shdr->sh_size / 4;
768
769
    /* Look through this group's sections to see if current
770
       section is a member.  */
771
6.98M
    while (--n_elt != 0)
772
6.44M
      if ((++idx)->shdr == hdr)
773
97.5k
        {
774
97.5k
    asection *s = NULL;
775
776
    /* We are a member of this group.  Go looking through
777
       other members to see if any others are linked via
778
       next_in_group.  */
779
97.5k
    idx = (Elf_Internal_Group *) shdr->contents;
780
97.5k
    n_elt = shdr->sh_size / 4;
781
3.28M
    while (--n_elt != 0)
782
3.24M
      if ((++idx)->shdr != NULL
783
3.24M
          && (s = idx->shdr->bfd_section) != NULL
784
3.24M
          && elf_next_in_group (s) != NULL)
785
51.1k
        break;
786
97.5k
    if (n_elt != 0)
787
51.1k
      {
788
        /* Snarf the group name from other member, and
789
           insert current section in circular list.  */
790
51.1k
        elf_group_name (newsect) = elf_group_name (s);
791
51.1k
        elf_next_in_group (newsect) = elf_next_in_group (s);
792
51.1k
        elf_next_in_group (s) = newsect;
793
51.1k
      }
794
46.4k
    else
795
46.4k
      {
796
46.4k
        const char *gname;
797
798
46.4k
        gname = group_signature (abfd, shdr);
799
46.4k
        if (gname == NULL)
800
5.73k
          return false;
801
40.6k
        elf_group_name (newsect) = gname;
802
803
        /* Start a circular list with one element.  */
804
40.6k
        elf_next_in_group (newsect) = newsect;
805
40.6k
      }
806
807
    /* If the group section has been created, point to the
808
       new member.  */
809
91.8k
    if (shdr->bfd_section != NULL)
810
91.8k
      elf_next_in_group (shdr->bfd_section) = newsect;
811
812
91.8k
    elf_tdata (abfd)->group_search_offset = i;
813
91.8k
    j = num_group - 1;
814
91.8k
    break;
815
97.5k
        }
816
636k
  }
817
132k
    }
818
819
343k
  if (elf_group_name (newsect) == NULL)
820
251k
    {
821
      /* xgettext:c-format */
822
251k
      _bfd_error_handler (_("%pB: no group info for section '%pA'"),
823
251k
        abfd, newsect);
824
      /* PR 29532: Return true here, even though the group info has not been
825
   read.  Separate debug info files can have empty group sections, but
826
   we do not want this to prevent them from being loaded as otherwise
827
   GDB will not be able to use them.  */
828
251k
      return true;
829
251k
    }
830
91.8k
  return true;
831
343k
}
832
833
bool
834
_bfd_elf_setup_sections (bfd *abfd)
835
216k
{
836
216k
  unsigned int i;
837
216k
  unsigned int num_group = elf_tdata (abfd)->num_group;
838
216k
  bool result = true;
839
216k
  asection *s;
840
841
  /* Process SHF_LINK_ORDER.  */
842
1.79M
  for (s = abfd->sections; s != NULL; s = s->next)
843
1.58M
    {
844
1.58M
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
845
1.58M
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
846
209k
  {
847
209k
    unsigned int elfsec = this_hdr->sh_link;
848
    /* An sh_link value of 0 is now allowed.  It indicates that linked
849
       to section has already been discarded, but that the current
850
       section has been retained for some other reason.  This linking
851
       section is still a candidate for later garbage collection
852
       however.  */
853
209k
    if (elfsec == 0)
854
170k
      {
855
170k
        elf_linked_to_section (s) = NULL;
856
170k
      }
857
39.2k
    else
858
39.2k
      {
859
39.2k
        asection *linksec = NULL;
860
861
39.2k
        if (elfsec < elf_numsections (abfd))
862
39.1k
    {
863
39.1k
      this_hdr = elf_elfsections (abfd)[elfsec];
864
39.1k
      linksec = this_hdr->bfd_section;
865
39.1k
    }
866
867
        /* PR 1991, 2008:
868
     Some strip/objcopy may leave an incorrect value in
869
     sh_link.  We don't want to proceed.  */
870
39.2k
        if (linksec == NULL)
871
2.46k
    {
872
2.46k
      _bfd_error_handler
873
        /* xgettext:c-format */
874
2.46k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
875
2.46k
         s->owner, elfsec, s);
876
2.46k
      result = false;
877
2.46k
    }
878
879
39.2k
        elf_linked_to_section (s) = linksec;
880
39.2k
      }
881
209k
  }
882
1.37M
      else if (this_hdr->sh_type == SHT_GROUP
883
1.37M
         && elf_next_in_group (s) == NULL)
884
7.26k
  {
885
7.26k
    _bfd_error_handler
886
      /* xgettext:c-format */
887
7.26k
      (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
888
7.26k
       abfd, elf_section_data (s)->this_idx);
889
7.26k
    result = false;
890
7.26k
  }
891
1.58M
    }
892
893
  /* Process section groups.  */
894
216k
  if (num_group == (unsigned) -1)
895
97.6k
    return result;
896
897
157k
  for (i = 0; i < num_group; i++)
898
38.7k
    {
899
38.7k
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
900
38.7k
      Elf_Internal_Group *idx;
901
38.7k
      unsigned int n_elt;
902
903
      /* PR binutils/18758: Beware of corrupt binaries with invalid
904
   group data.  */
905
38.7k
      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
906
29
  {
907
29
    _bfd_error_handler
908
      /* xgettext:c-format */
909
29
      (_("%pB: section group entry number %u is corrupt"),
910
29
       abfd, i);
911
29
    result = false;
912
29
    continue;
913
29
  }
914
915
38.7k
      idx = (Elf_Internal_Group *) shdr->contents;
916
38.7k
      n_elt = shdr->sh_size / 4;
917
918
1.94M
      while (--n_elt != 0)
919
1.90M
  {
920
1.90M
    ++ idx;
921
922
1.90M
    if (idx->shdr == NULL)
923
1.44M
      continue;
924
457k
    else if (idx->shdr->bfd_section)
925
101k
      elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
926
356k
    else if (idx->shdr->sh_type != SHT_RELA
927
356k
       && idx->shdr->sh_type != SHT_REL)
928
238k
      {
929
        /* There are some unknown sections in the group.  */
930
238k
        _bfd_error_handler
931
    /* xgettext:c-format */
932
238k
    (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
933
238k
     abfd,
934
238k
     idx->shdr->sh_type,
935
238k
     bfd_elf_string_from_elf_section (abfd,
936
238k
              (elf_elfheader (abfd)
937
238k
               ->e_shstrndx),
938
238k
              idx->shdr->sh_name),
939
238k
     shdr->bfd_section);
940
238k
        result = false;
941
238k
      }
942
1.90M
  }
943
38.7k
    }
944
945
118k
  return result;
946
216k
}
947
948
bool
949
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
950
0
{
951
0
  return elf_next_in_group (sec) != NULL;
952
0
}
953
954
const char *
955
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
956
0
{
957
0
  if (elf_sec_group (sec) != NULL)
958
0
    return elf_group_name (sec);
959
0
  return NULL;
960
0
}
961
962
/* This a copy of lto_section defined in GCC (lto-streamer.h).  */
963
964
struct lto_section
965
{
966
  int16_t major_version;
967
  int16_t minor_version;
968
  unsigned char slim_object;
969
970
  /* Flags is a private field that is not defined publicly.  */
971
  uint16_t flags;
972
};
973
974
/* Make a BFD section from an ELF section.  We store a pointer to the
975
   BFD section in the bfd_section field of the header.  */
976
977
bool
978
_bfd_elf_make_section_from_shdr (bfd *abfd,
979
         Elf_Internal_Shdr *hdr,
980
         const char *name,
981
         int shindex)
982
2.08M
{
983
2.08M
  asection *newsect;
984
2.08M
  flagword flags;
985
2.08M
  const struct elf_backend_data *bed;
986
2.08M
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
987
988
2.08M
  if (hdr->bfd_section != NULL)
989
199k
    return true;
990
991
1.88M
  newsect = bfd_make_section_anyway (abfd, name);
992
1.88M
  if (newsect == NULL)
993
0
    return false;
994
995
1.88M
  hdr->bfd_section = newsect;
996
1.88M
  elf_section_data (newsect)->this_hdr = *hdr;
997
1.88M
  elf_section_data (newsect)->this_idx = shindex;
998
999
  /* Always use the real type/flags.  */
1000
1.88M
  elf_section_type (newsect) = hdr->sh_type;
1001
1.88M
  elf_section_flags (newsect) = hdr->sh_flags;
1002
1003
1.88M
  newsect->filepos = hdr->sh_offset;
1004
1005
1.88M
  flags = SEC_NO_FLAGS;
1006
1.88M
  if (hdr->sh_type != SHT_NOBITS)
1007
1.83M
    flags |= SEC_HAS_CONTENTS;
1008
1.88M
  if (hdr->sh_type == SHT_GROUP)
1009
64.8k
    flags |= SEC_GROUP;
1010
1.88M
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
1011
692k
    {
1012
692k
      flags |= SEC_ALLOC;
1013
692k
      if (hdr->sh_type != SHT_NOBITS)
1014
648k
  flags |= SEC_LOAD;
1015
692k
    }
1016
1.88M
  if ((hdr->sh_flags & SHF_WRITE) == 0)
1017
1.43M
    flags |= SEC_READONLY;
1018
1.88M
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1019
400k
    flags |= SEC_CODE;
1020
1.48M
  else if ((flags & SEC_LOAD) != 0)
1021
354k
    flags |= SEC_DATA;
1022
1.88M
  if ((hdr->sh_flags & SHF_MERGE) != 0)
1023
305k
    {
1024
305k
      flags |= SEC_MERGE;
1025
305k
      newsect->entsize = hdr->sh_entsize;
1026
305k
    }
1027
1.88M
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
1028
360k
    flags |= SEC_STRINGS;
1029
1.88M
  if (hdr->sh_flags & SHF_GROUP)
1030
350k
    if (!setup_group (abfd, hdr, newsect))
1031
7.18k
      return false;
1032
1.88M
  if ((hdr->sh_flags & SHF_TLS) != 0)
1033
185k
    flags |= SEC_THREAD_LOCAL;
1034
1.88M
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1035
237k
    flags |= SEC_EXCLUDE;
1036
1037
1.88M
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1038
1.88M
    {
1039
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1040
   but binutils as of 2019-07-23 did not set the EI_OSABI header
1041
   byte.  */
1042
420k
    case ELFOSABI_GNU:
1043
691k
    case ELFOSABI_FREEBSD:
1044
691k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1045
117k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1046
      /* Fall through */
1047
1.27M
    case ELFOSABI_NONE:
1048
1.27M
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1049
136k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1050
1.27M
      break;
1051
1.88M
    }
1052
1053
1.88M
  if ((flags & SEC_ALLOC) == 0)
1054
1.19M
    {
1055
      /* The debugging sections appear to be recognized only by name,
1056
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
1057
1.19M
      if (name [0] == '.')
1058
367k
  {
1059
367k
    if (startswith (name, ".debug")
1060
367k
        || startswith (name, ".gnu.debuglto_.debug_")
1061
367k
        || startswith (name, ".gnu.linkonce.wi.")
1062
367k
        || startswith (name, ".zdebug"))
1063
68.7k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1064
298k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1065
298k
       || startswith (name, ".note.gnu"))
1066
4.12k
      {
1067
4.12k
        flags |= SEC_ELF_OCTETS;
1068
4.12k
        opb = 1;
1069
4.12k
      }
1070
294k
    else if (startswith (name, ".line")
1071
294k
       || startswith (name, ".stab")
1072
294k
       || strcmp (name, ".gdb_index") == 0)
1073
18.6k
      flags |= SEC_DEBUGGING;
1074
367k
  }
1075
1.19M
    }
1076
1077
1.88M
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1078
1.88M
      || !bfd_set_section_size (newsect, hdr->sh_size)
1079
1.88M
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1080
1.88M
              & -hdr->sh_addralign)))
1081
3.95k
    return false;
1082
1083
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
1084
     only link a single copy of the section.  This is used to support
1085
     g++.  g++ will emit each template expansion in its own section.
1086
     The symbols will be defined as weak, so that multiple definitions
1087
     are permitted.  The GNU linker extension is to actually discard
1088
     all but one of the sections.  */
1089
1.87M
  if (startswith (name, ".gnu.linkonce")
1090
1.87M
      && elf_next_in_group (newsect) == NULL)
1091
16.5k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1092
1093
1.87M
  if (!bfd_set_section_flags (newsect, flags))
1094
0
    return false;
1095
1096
1.87M
  bed = get_elf_backend_data (abfd);
1097
1.87M
  if (bed->elf_backend_section_flags)
1098
130k
    if (!bed->elf_backend_section_flags (hdr))
1099
0
      return false;
1100
1101
  /* We do not parse the PT_NOTE segments as we are interested even in the
1102
     separate debug info files which may have the segments offsets corrupted.
1103
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1104
1.87M
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1105
59.0k
    {
1106
59.0k
      bfd_byte *contents;
1107
1108
59.0k
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1109
6.13k
  return false;
1110
1111
52.8k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1112
52.8k
           hdr->sh_offset, hdr->sh_addralign);
1113
52.8k
      free (contents);
1114
52.8k
    }
1115
1116
1.87M
  if ((newsect->flags & SEC_ALLOC) != 0)
1117
690k
    {
1118
690k
      Elf_Internal_Phdr *phdr;
1119
690k
      unsigned int i, nload;
1120
1121
      /* Some ELF linkers produce binaries with all the program header
1122
   p_paddr fields zero.  If we have such a binary with more than
1123
   one PT_LOAD header, then leave the section lma equal to vma
1124
   so that we don't create sections with overlapping lma.  */
1125
690k
      phdr = elf_tdata (abfd)->phdr;
1126
983k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1127
582k
  if (phdr->p_paddr != 0)
1128
289k
    break;
1129
293k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1130
70.6k
    ++nload;
1131
690k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1132
18.7k
  return true;
1133
1134
671k
      phdr = elf_tdata (abfd)->phdr;
1135
14.4M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1136
13.8M
  {
1137
13.8M
    if (((phdr->p_type == PT_LOAD
1138
13.8M
    && (hdr->sh_flags & SHF_TLS) == 0)
1139
13.8M
         || phdr->p_type == PT_TLS)
1140
13.8M
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1141
64.0k
      {
1142
64.0k
        if ((newsect->flags & SEC_LOAD) == 0)
1143
11.4k
    newsect->lma = (phdr->p_paddr
1144
11.4k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1145
52.6k
        else
1146
    /* We used to use the same adjustment for SEC_LOAD
1147
       sections, but that doesn't work if the segment
1148
       is packed with code from multiple VMAs.
1149
       Instead we calculate the section LMA based on
1150
       the segment LMA.  It is assumed that the
1151
       segment will contain sections with contiguous
1152
       LMAs, even if the VMAs are not.  */
1153
52.6k
    newsect->lma = (phdr->p_paddr
1154
52.6k
        + hdr->sh_offset - phdr->p_offset) / opb;
1155
1156
        /* With contiguous segments, we can't tell from file
1157
     offsets whether a section with zero size should
1158
     be placed at the end of one segment or the
1159
     beginning of the next.  Decide based on vaddr.  */
1160
64.0k
        if (hdr->sh_addr >= phdr->p_vaddr
1161
64.0k
      && (hdr->sh_addr + hdr->sh_size
1162
64.0k
          <= phdr->p_vaddr + phdr->p_memsz))
1163
59.7k
    break;
1164
64.0k
      }
1165
13.8M
  }
1166
671k
    }
1167
1168
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1169
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1170
1.85M
  if ((newsect->flags & SEC_DEBUGGING) != 0
1171
1.85M
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1172
1.85M
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1173
67.7k
    {
1174
67.7k
      enum { nothing, compress, decompress } action = nothing;
1175
67.7k
      int compression_header_size;
1176
67.7k
      bfd_size_type uncompressed_size;
1177
67.7k
      unsigned int uncompressed_align_power;
1178
67.7k
      enum compression_type ch_type = ch_none;
1179
67.7k
      bool compressed
1180
67.7k
  = bfd_is_section_compressed_info (abfd, newsect,
1181
67.7k
            &compression_header_size,
1182
67.7k
            &uncompressed_size,
1183
67.7k
            &uncompressed_align_power,
1184
67.7k
            &ch_type);
1185
1186
      /* Should we decompress?  */
1187
67.7k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1188
107
  action = decompress;
1189
1190
      /* Should we compress?  Or convert to a different compression?  */
1191
67.6k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1192
67.6k
         && newsect->size != 0
1193
67.6k
         && compression_header_size >= 0
1194
67.6k
         && uncompressed_size > 0)
1195
10.0k
  {
1196
10.0k
    if (!compressed)
1197
10.0k
      action = compress;
1198
0
    else
1199
0
      {
1200
0
        enum compression_type new_ch_type = ch_none;
1201
0
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1202
0
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1203
0
             ? ch_compress_zstd : ch_compress_zlib);
1204
0
        if (new_ch_type != ch_type)
1205
0
    action = compress;
1206
0
      }
1207
10.0k
  }
1208
1209
67.7k
      if (action == compress)
1210
10.0k
  {
1211
10.0k
    if (!bfd_init_section_compress_status (abfd, newsect))
1212
25
      {
1213
25
        _bfd_error_handler
1214
    /* xgettext:c-format */
1215
25
    (_("%pB: unable to compress section %s"), abfd, name);
1216
25
        return false;
1217
25
      }
1218
10.0k
  }
1219
57.7k
      else if (action == decompress)
1220
107
  {
1221
107
    if (!bfd_init_section_decompress_status (abfd, newsect))
1222
107
      {
1223
107
        _bfd_error_handler
1224
    /* xgettext:c-format */
1225
107
    (_("%pB: unable to decompress section %s"), abfd, name);
1226
107
        return false;
1227
107
      }
1228
0
#ifndef HAVE_ZSTD
1229
0
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1230
0
      {
1231
0
        _bfd_error_handler
1232
      /* xgettext:c-format */
1233
0
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1234
0
          "is not built with zstd support"),
1235
0
       abfd, name);
1236
0
        newsect->compress_status = COMPRESS_SECTION_NONE;
1237
0
        return false;
1238
0
      }
1239
0
#endif
1240
0
    if (abfd->is_linker_input
1241
0
        && name[1] == 'z')
1242
0
      {
1243
        /* Rename section from .zdebug_* to .debug_* so that ld
1244
     scripts will see this section as a debug section.  */
1245
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1246
0
        if (new_name == NULL)
1247
0
    return false;
1248
0
        bfd_rename_section (newsect, new_name);
1249
0
      }
1250
0
  }
1251
67.7k
    }
1252
1253
  /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1254
     section.  */
1255
1.85M
  if (startswith (name, ".gnu.lto_.lto."))
1256
6.94k
    {
1257
6.94k
      struct lto_section lsection;
1258
6.94k
      if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1259
6.94k
            sizeof (struct lto_section)))
1260
4.29k
  abfd->lto_slim_object = lsection.slim_object;
1261
6.94k
    }
1262
1263
1.85M
  return true;
1264
1.85M
}
1265
1266
const char *const bfd_elf_section_type_names[] =
1267
{
1268
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1269
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1270
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1271
};
1272
1273
/* ELF relocs are against symbols.  If we are producing relocatable
1274
   output, and the reloc is against an external symbol, and nothing
1275
   has given us any additional addend, the resulting reloc will also
1276
   be against the same symbol.  In such a case, we don't want to
1277
   change anything about the way the reloc is handled, since it will
1278
   all be done at final link time.  Rather than put special case code
1279
   into bfd_perform_relocation, all the reloc types use this howto
1280
   function, or should call this function for relocatable output.  */
1281
1282
bfd_reloc_status_type
1283
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1284
           arelent *reloc_entry,
1285
           asymbol *symbol,
1286
           void *data ATTRIBUTE_UNUSED,
1287
           asection *input_section,
1288
           bfd *output_bfd,
1289
           char **error_message ATTRIBUTE_UNUSED)
1290
275k
{
1291
275k
  if (output_bfd != NULL
1292
275k
      && (symbol->flags & BSF_SECTION_SYM) == 0
1293
275k
      && (! reloc_entry->howto->partial_inplace
1294
0
    || reloc_entry->addend == 0))
1295
0
    {
1296
0
      reloc_entry->address += input_section->output_offset;
1297
0
      return bfd_reloc_ok;
1298
0
    }
1299
1300
  /* In some cases the relocation should be treated as output section
1301
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1302
     targets lack section relative relocations and instead use
1303
     ordinary absolute relocations for references between DWARF
1304
     sections.  That is arguably a bug in those targets but it happens
1305
     to work for the usual case of linking to non-loaded ELF debug
1306
     sections with VMAs forced to zero.  PE COFF on the other hand
1307
     doesn't allow a section VMA of zero.  */
1308
275k
  if (output_bfd == NULL
1309
275k
      && !reloc_entry->howto->pc_relative
1310
275k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1311
275k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1312
41.6k
    reloc_entry->addend -= symbol->section->output_section->vma;
1313
1314
275k
  return bfd_reloc_continue;
1315
275k
}
1316

1317
/* Returns TRUE if section A matches section B.
1318
   Names, addresses and links may be different, but everything else
1319
   should be the same.  */
1320
1321
static bool
1322
section_match (const Elf_Internal_Shdr * a,
1323
         const Elf_Internal_Shdr * b)
1324
2
{
1325
2
  if (a->sh_type != b->sh_type
1326
2
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1327
2
      || a->sh_addralign != b->sh_addralign
1328
2
      || a->sh_entsize != b->sh_entsize)
1329
0
    return false;
1330
2
  if (a->sh_type == SHT_SYMTAB
1331
2
      || a->sh_type == SHT_STRTAB)
1332
0
    return true;
1333
2
  return a->sh_size == b->sh_size;
1334
2
}
1335
1336
/* Find a section in OBFD that has the same characteristics
1337
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1338
   none can be found.  Check's section HINT first, as this is likely
1339
   to be the correct section.  */
1340
1341
static unsigned int
1342
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1343
     const unsigned int hint)
1344
2
{
1345
2
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1346
2
  unsigned int i;
1347
1348
2
  BFD_ASSERT (iheader != NULL);
1349
1350
  /* See PR 20922 for a reproducer of the NULL test.  */
1351
2
  if (hint < elf_numsections (obfd)
1352
2
      && oheaders[hint] != NULL
1353
2
      && section_match (oheaders[hint], iheader))
1354
2
    return hint;
1355
1356
0
  for (i = 1; i < elf_numsections (obfd); i++)
1357
0
    {
1358
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1359
1360
0
      if (oheader == NULL)
1361
0
  continue;
1362
0
      if (section_match (oheader, iheader))
1363
  /* FIXME: Do we care if there is a potential for
1364
     multiple matches ?  */
1365
0
  return i;
1366
0
    }
1367
1368
0
  return SHN_UNDEF;
1369
0
}
1370
1371
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1372
   Processor specific section, based upon a matching input section.
1373
   Returns TRUE upon success, FALSE otherwise.  */
1374
1375
static bool
1376
copy_special_section_fields (const bfd *ibfd,
1377
           bfd *obfd,
1378
           const Elf_Internal_Shdr *iheader,
1379
           Elf_Internal_Shdr *oheader,
1380
           const unsigned int secnum)
1381
298
{
1382
298
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1383
298
  const Elf_Internal_Shdr **iheaders
1384
298
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1385
298
  bool changed = false;
1386
298
  unsigned int sh_link;
1387
1388
298
  if (oheader->sh_type == SHT_NOBITS)
1389
80
    {
1390
      /* This is a feature for objcopy --only-keep-debug:
1391
   When a section's type is changed to NOBITS, we preserve
1392
   the sh_link and sh_info fields so that they can be
1393
   matched up with the original.
1394
1395
   Note: Strictly speaking these assignments are wrong.
1396
   The sh_link and sh_info fields should point to the
1397
   relevent sections in the output BFD, which may not be in
1398
   the same location as they were in the input BFD.  But
1399
   the whole point of this action is to preserve the
1400
   original values of the sh_link and sh_info fields, so
1401
   that they can be matched up with the section headers in
1402
   the original file.  So strictly speaking we may be
1403
   creating an invalid ELF file, but it is only for a file
1404
   that just contains debug info and only for sections
1405
   without any contents.  */
1406
80
      if (oheader->sh_link == 0)
1407
80
  oheader->sh_link = iheader->sh_link;
1408
80
      if (oheader->sh_info == 0)
1409
80
  oheader->sh_info = iheader->sh_info;
1410
80
      return true;
1411
80
    }
1412
1413
  /* Allow the target a chance to decide how these fields should be set.  */
1414
218
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1415
218
                iheader, oheader))
1416
215
    return true;
1417
1418
  /* We have an iheader which might match oheader, and which has non-zero
1419
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1420
     the section in the output bfd which corresponds to the linked section
1421
     in the input bfd.  */
1422
3
  if (iheader->sh_link != SHN_UNDEF)
1423
2
    {
1424
      /* See PR 20931 for a reproducer.  */
1425
2
      if (iheader->sh_link >= elf_numsections (ibfd))
1426
0
  {
1427
0
    _bfd_error_handler
1428
      /* xgettext:c-format */
1429
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1430
0
       ibfd, iheader->sh_link, secnum);
1431
0
    return false;
1432
0
  }
1433
1434
2
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1435
2
      if (sh_link != SHN_UNDEF)
1436
2
  {
1437
2
    oheader->sh_link = sh_link;
1438
2
    changed = true;
1439
2
  }
1440
0
      else
1441
  /* FIXME: Should we install iheader->sh_link
1442
     if we could not find a match ?  */
1443
0
  _bfd_error_handler
1444
    /* xgettext:c-format */
1445
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1446
2
    }
1447
1448
3
  if (iheader->sh_info)
1449
0
    {
1450
      /* The sh_info field can hold arbitrary information, but if the
1451
   SHF_LINK_INFO flag is set then it should be interpreted as a
1452
   section index.  */
1453
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1454
0
  {
1455
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1456
0
             iheader->sh_info);
1457
0
    if (sh_link != SHN_UNDEF)
1458
0
      oheader->sh_flags |= SHF_INFO_LINK;
1459
0
  }
1460
0
      else
1461
  /* No idea what it means - just copy it.  */
1462
0
  sh_link = iheader->sh_info;
1463
1464
0
      if (sh_link != SHN_UNDEF)
1465
0
  {
1466
0
    oheader->sh_info = sh_link;
1467
0
    changed = true;
1468
0
  }
1469
0
      else
1470
0
  _bfd_error_handler
1471
    /* xgettext:c-format */
1472
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1473
0
    }
1474
1475
3
  return changed;
1476
3
}
1477
1478
/* Copy the program header and other data from one object module to
1479
   another.  */
1480
1481
bool
1482
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1483
189
{
1484
189
  const Elf_Internal_Shdr **iheaders
1485
189
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1486
189
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1487
189
  const struct elf_backend_data *bed;
1488
189
  unsigned int i;
1489
1490
189
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1491
189
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1492
0
    return true;
1493
1494
189
  if (!elf_flags_init (obfd))
1495
187
    {
1496
187
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1497
187
      elf_flags_init (obfd) = true;
1498
187
    }
1499
1500
189
  elf_gp (obfd) = elf_gp (ibfd);
1501
1502
  /* Also copy the EI_OSABI field.  */
1503
189
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1504
189
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1505
1506
  /* If set, copy the EI_ABIVERSION field.  */
1507
189
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1508
7
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1509
7
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1510
1511
  /* Copy object attributes.  */
1512
189
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1513
1514
189
  if (iheaders == NULL || oheaders == NULL)
1515
103
    return true;
1516
1517
86
  bed = get_elf_backend_data (obfd);
1518
1519
  /* Possibly copy other fields in the section header.  */
1520
6.24k
  for (i = 1; i < elf_numsections (obfd); i++)
1521
6.15k
    {
1522
6.15k
      unsigned int j;
1523
6.15k
      Elf_Internal_Shdr * oheader = oheaders[i];
1524
1525
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1526
   because of a special case need for generating separate debug info
1527
   files.  See below for more details.  */
1528
6.15k
      if (oheader == NULL
1529
6.15k
    || (oheader->sh_type != SHT_NOBITS
1530
6.15k
        && oheader->sh_type < SHT_LOOS))
1531
5.82k
  continue;
1532
1533
      /* Ignore empty sections, and sections whose
1534
   fields have already been initialised.  */
1535
327
      if (oheader->sh_size == 0
1536
327
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1537
29
  continue;
1538
1539
      /* Scan for the matching section in the input bfd.
1540
   First we try for a direct mapping between the input and
1541
   output sections.  */
1542
14.0k
      for (j = 1; j < elf_numsections (ibfd); j++)
1543
14.0k
  {
1544
14.0k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1545
1546
14.0k
    if (iheader == NULL)
1547
0
      continue;
1548
1549
14.0k
    if (oheader->bfd_section != NULL
1550
14.0k
        && iheader->bfd_section != NULL
1551
14.0k
        && iheader->bfd_section->output_section != NULL
1552
14.0k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1553
298
      {
1554
        /* We have found a connection from the input section to
1555
     the output section.  Attempt to copy the header fields.
1556
     If this fails then do not try any further sections -
1557
     there should only be a one-to-one mapping between
1558
     input and output.  */
1559
298
        if (!copy_special_section_fields (ibfd, obfd,
1560
298
            iheader, oheader, i))
1561
1
    j = elf_numsections (ibfd);
1562
298
        break;
1563
298
      }
1564
14.0k
  }
1565
1566
298
      if (j < elf_numsections (ibfd))
1567
297
  continue;
1568
1569
      /* That failed.  So try to deduce the corresponding input section.
1570
   Unfortunately we cannot compare names as the output string table
1571
   is empty, so instead we check size, address and type.  */
1572
28
      for (j = 1; j < elf_numsections (ibfd); j++)
1573
27
  {
1574
27
    const Elf_Internal_Shdr * iheader = iheaders[j];
1575
1576
27
    if (iheader == NULL)
1577
0
      continue;
1578
1579
    /* Try matching fields in the input section's header.
1580
       Since --only-keep-debug turns all non-debug sections into
1581
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1582
       input type.  */
1583
27
    if ((oheader->sh_type == SHT_NOBITS
1584
27
         || iheader->sh_type == oheader->sh_type)
1585
27
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1586
1
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1587
27
        && iheader->sh_addralign == oheader->sh_addralign
1588
27
        && iheader->sh_entsize == oheader->sh_entsize
1589
27
        && iheader->sh_size == oheader->sh_size
1590
27
        && iheader->sh_addr == oheader->sh_addr
1591
27
        && (iheader->sh_info != oheader->sh_info
1592
1
      || iheader->sh_link != oheader->sh_link))
1593
0
      {
1594
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1595
0
    break;
1596
0
      }
1597
27
  }
1598
1599
1
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1600
1
  {
1601
    /* Final attempt.  Call the backend copy function
1602
       with a NULL input section.  */
1603
1
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1604
1
                     NULL, oheader);
1605
1
  }
1606
1
    }
1607
1608
86
  return true;
1609
189
}
1610
1611
static const char *
1612
get_segment_type (unsigned int p_type)
1613
277k
{
1614
277k
  const char *pt;
1615
277k
  switch (p_type)
1616
277k
    {
1617
51.7k
    case PT_NULL: pt = "NULL"; break;
1618
3.42k
    case PT_LOAD: pt = "LOAD"; break;
1619
1.32k
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1620
986
    case PT_INTERP: pt = "INTERP"; break;
1621
418
    case PT_NOTE: pt = "NOTE"; break;
1622
434
    case PT_SHLIB: pt = "SHLIB"; break;
1623
1.79k
    case PT_PHDR: pt = "PHDR"; break;
1624
244
    case PT_TLS: pt = "TLS"; break;
1625
146
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1626
117
    case PT_GNU_STACK: pt = "STACK"; break;
1627
127
    case PT_GNU_RELRO: pt = "RELRO"; break;
1628
4
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1629
216k
    default: pt = NULL; break;
1630
277k
    }
1631
277k
  return pt;
1632
277k
}
1633
1634
/* Print out the program headers.  */
1635
1636
bool
1637
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1638
7.27k
{
1639
7.27k
  FILE *f = (FILE *) farg;
1640
7.27k
  Elf_Internal_Phdr *p;
1641
7.27k
  asection *s;
1642
7.27k
  bfd_byte *dynbuf = NULL;
1643
1644
7.27k
  p = elf_tdata (abfd)->phdr;
1645
7.27k
  if (p != NULL)
1646
6.25k
    {
1647
6.25k
      unsigned int i, c;
1648
1649
6.25k
      fprintf (f, _("\nProgram Header:\n"));
1650
6.25k
      c = elf_elfheader (abfd)->e_phnum;
1651
283k
      for (i = 0; i < c; i++, p++)
1652
277k
  {
1653
277k
    const char *pt = get_segment_type (p->p_type);
1654
277k
    char buf[20];
1655
1656
277k
    if (pt == NULL)
1657
216k
      {
1658
216k
        sprintf (buf, "0x%lx", p->p_type);
1659
216k
        pt = buf;
1660
216k
      }
1661
277k
    fprintf (f, "%8s off    0x", pt);
1662
277k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1663
277k
    fprintf (f, " vaddr 0x");
1664
277k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1665
277k
    fprintf (f, " paddr 0x");
1666
277k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1667
277k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1668
277k
    fprintf (f, "         filesz 0x");
1669
277k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1670
277k
    fprintf (f, " memsz 0x");
1671
277k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1672
277k
    fprintf (f, " flags %c%c%c",
1673
277k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1674
277k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1675
277k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1676
277k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1677
209k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1678
277k
    fprintf (f, "\n");
1679
277k
  }
1680
6.25k
    }
1681
1682
7.27k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1683
7.27k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1684
181
    {
1685
181
      unsigned int elfsec;
1686
181
      unsigned long shlink;
1687
181
      bfd_byte *extdyn, *extdynend;
1688
181
      size_t extdynsize;
1689
181
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1690
1691
181
      fprintf (f, _("\nDynamic Section:\n"));
1692
1693
181
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1694
9
  goto error_return;
1695
1696
172
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1697
172
      if (elfsec == SHN_BAD)
1698
0
  goto error_return;
1699
172
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1700
1701
172
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1702
172
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1703
1704
172
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1705
2.92k
     (size_t) (extdynend - extdyn) >= extdynsize;
1706
2.75k
     extdyn += extdynsize)
1707
2.87k
  {
1708
2.87k
    Elf_Internal_Dyn dyn;
1709
2.87k
    const char *name = "";
1710
2.87k
    char ab[20];
1711
2.87k
    bool stringp;
1712
2.87k
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1713
1714
2.87k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1715
1716
2.87k
    if (dyn.d_tag == DT_NULL)
1717
115
      break;
1718
1719
2.76k
    stringp = false;
1720
2.76k
    switch (dyn.d_tag)
1721
2.76k
      {
1722
961
      default:
1723
961
        if (bed->elf_backend_get_target_dtag)
1724
66
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1725
1726
961
        if (!strcmp (name, ""))
1727
945
    {
1728
945
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1729
945
      name = ab;
1730
945
    }
1731
961
        break;
1732
1733
188
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1734
67
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1735
73
      case DT_PLTGOT: name = "PLTGOT"; break;
1736
51
      case DT_HASH: name = "HASH"; break;
1737
74
      case DT_STRTAB: name = "STRTAB"; break;
1738
76
      case DT_SYMTAB: name = "SYMTAB"; break;
1739
53
      case DT_RELA: name = "RELA"; break;
1740
54
      case DT_RELASZ: name = "RELASZ"; break;
1741
53
      case DT_RELAENT: name = "RELAENT"; break;
1742
74
      case DT_STRSZ: name = "STRSZ"; break;
1743
72
      case DT_SYMENT: name = "SYMENT"; break;
1744
82
      case DT_INIT: name = "INIT"; break;
1745
64
      case DT_FINI: name = "FINI"; break;
1746
4
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1747
11
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1748
2
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1749
29
      case DT_REL: name = "REL"; break;
1750
21
      case DT_RELSZ: name = "RELSZ"; break;
1751
29
      case DT_RELENT: name = "RELENT"; break;
1752
1
      case DT_RELR: name = "RELR"; break;
1753
6
      case DT_RELRSZ: name = "RELRSZ"; break;
1754
1
      case DT_RELRENT: name = "RELRENT"; break;
1755
61
      case DT_PLTREL: name = "PLTREL"; break;
1756
80
      case DT_DEBUG: name = "DEBUG"; break;
1757
2
      case DT_TEXTREL: name = "TEXTREL"; break;
1758
60
      case DT_JMPREL: name = "JMPREL"; break;
1759
1
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1760
39
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1761
25
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1762
33
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1763
33
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1764
7
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1765
27
      case DT_FLAGS: name = "FLAGS"; break;
1766
2
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1767
6
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1768
6
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1769
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1770
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1771
2
      case DT_MOVESZ: name = "MOVESZ"; break;
1772
6
      case DT_FEATURE: name = "FEATURE"; break;
1773
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1774
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1775
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1776
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1777
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1778
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1779
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1780
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1781
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1782
29
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1783
10
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1784
32
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1785
66
      case DT_VERSYM: name = "VERSYM"; break;
1786
2
      case DT_VERDEF: name = "VERDEF"; break;
1787
6
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1788
70
      case DT_VERNEED: name = "VERNEED"; break;
1789
63
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1790
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1791
0
      case DT_USED: name = "USED"; break;
1792
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1793
46
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1794
2.76k
      }
1795
1796
2.76k
    fprintf (f, "  %-20s ", name);
1797
2.76k
    if (! stringp)
1798
2.55k
      {
1799
2.55k
        fprintf (f, "0x");
1800
2.55k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1801
2.55k
      }
1802
210
    else
1803
210
      {
1804
210
        const char *string;
1805
210
        unsigned int tagv = dyn.d_un.d_val;
1806
1807
210
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1808
210
        if (string == NULL)
1809
9
    goto error_return;
1810
201
        fprintf (f, "%s", string);
1811
201
      }
1812
2.75k
    fprintf (f, "\n");
1813
2.75k
  }
1814
1815
163
      free (dynbuf);
1816
163
      dynbuf = NULL;
1817
163
    }
1818
1819
7.25k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1820
7.25k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1821
199
    {
1822
199
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1823
69
  return false;
1824
199
    }
1825
1826
7.18k
  if (elf_dynverdef (abfd) != 0)
1827
22
    {
1828
22
      Elf_Internal_Verdef *t;
1829
1830
22
      fprintf (f, _("\nVersion definitions:\n"));
1831
44
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1832
22
  {
1833
22
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1834
22
       t->vd_flags, t->vd_hash,
1835
22
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1836
22
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1837
0
      {
1838
0
        Elf_Internal_Verdaux *a;
1839
1840
0
        fprintf (f, "\t");
1841
0
        for (a = t->vd_auxptr->vda_nextptr;
1842
0
       a != NULL;
1843
0
       a = a->vda_nextptr)
1844
0
    fprintf (f, "%s ",
1845
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1846
0
        fprintf (f, "\n");
1847
0
      }
1848
22
  }
1849
22
    }
1850
1851
7.18k
  if (elf_dynverref (abfd) != 0)
1852
108
    {
1853
108
      Elf_Internal_Verneed *t;
1854
1855
108
      fprintf (f, _("\nVersion References:\n"));
1856
301
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1857
193
  {
1858
193
    Elf_Internal_Vernaux *a;
1859
1860
193
    fprintf (f, _("  required from %s:\n"),
1861
193
       t->vn_filename ? t->vn_filename : "<corrupt>");
1862
595
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863
402
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1864
402
         a->vna_flags, a->vna_other,
1865
402
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1866
193
  }
1867
108
    }
1868
1869
7.18k
  return true;
1870
1871
18
 error_return:
1872
18
  free (dynbuf);
1873
18
  return false;
1874
7.25k
}
1875
1876
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1877
   and return symbol version for symbol version itself.   */
1878
1879
const char *
1880
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1881
            bool base_p,
1882
            bool *hidden)
1883
1.12M
{
1884
1.12M
  const char *version_string = NULL;
1885
1.12M
  if (elf_dynversym (abfd) != 0
1886
1.12M
      && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1887
615k
    {
1888
615k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1889
1890
615k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
1891
615k
      vernum &= VERSYM_VERSION;
1892
1893
615k
      if (vernum == 0)
1894
105k
  version_string = "";
1895
510k
      else if (vernum == 1
1896
510k
         && (vernum > elf_tdata (abfd)->cverdefs
1897
508k
       || (elf_tdata (abfd)->verdef[0].vd_flags
1898
0
           == VER_FLG_BASE)))
1899
508k
  version_string = base_p ? "Base" : "";
1900
1.49k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
1901
0
  {
1902
0
    const char *nodename
1903
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1904
0
    version_string = "";
1905
0
    if (base_p
1906
0
        || nodename == NULL
1907
0
        || symbol->name == NULL
1908
0
        || strcmp (symbol->name, nodename) != 0)
1909
0
      version_string = nodename;
1910
0
  }
1911
1.49k
      else
1912
1.49k
  {
1913
1.49k
    Elf_Internal_Verneed *t;
1914
1915
1.49k
    version_string = _("<corrupt>");
1916
1.49k
    for (t = elf_tdata (abfd)->verref;
1917
4.46k
         t != NULL;
1918
2.97k
         t = t->vn_nextref)
1919
2.97k
      {
1920
2.97k
        Elf_Internal_Vernaux *a;
1921
1922
8.59k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1923
7.11k
    {
1924
7.11k
      if (a->vna_other == vernum)
1925
1.49k
        {
1926
1.49k
          *hidden = true;
1927
1.49k
          version_string = a->vna_nodename;
1928
1.49k
          break;
1929
1.49k
        }
1930
7.11k
    }
1931
2.97k
      }
1932
1.49k
  }
1933
615k
    }
1934
1.12M
  return version_string;
1935
1.12M
}
1936
1937
/* Display ELF-specific fields of a symbol.  */
1938
1939
void
1940
bfd_elf_print_symbol (bfd *abfd,
1941
          void *filep,
1942
          asymbol *symbol,
1943
          bfd_print_symbol_type how)
1944
0
{
1945
0
  FILE *file = (FILE *) filep;
1946
0
  switch (how)
1947
0
    {
1948
0
    case bfd_print_symbol_name:
1949
0
      fprintf (file, "%s", symbol->name);
1950
0
      break;
1951
0
    case bfd_print_symbol_more:
1952
0
      fprintf (file, "elf ");
1953
0
      bfd_fprintf_vma (abfd, file, symbol->value);
1954
0
      fprintf (file, " %x", symbol->flags);
1955
0
      break;
1956
0
    case bfd_print_symbol_all:
1957
0
      {
1958
0
  const char *section_name;
1959
0
  const char *name = NULL;
1960
0
  const struct elf_backend_data *bed;
1961
0
  unsigned char st_other;
1962
0
  bfd_vma val;
1963
0
  const char *version_string;
1964
0
  bool hidden;
1965
1966
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
1967
1968
0
  bed = get_elf_backend_data (abfd);
1969
0
  if (bed->elf_backend_print_symbol_all)
1970
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1971
1972
0
  if (name == NULL)
1973
0
    {
1974
0
      name = symbol->name;
1975
0
      bfd_print_symbol_vandf (abfd, file, symbol);
1976
0
    }
1977
1978
0
  fprintf (file, " %s\t", section_name);
1979
  /* Print the "other" value for a symbol.  For common symbols,
1980
     we've already printed the size; now print the alignment.
1981
     For other symbols, we have no specified alignment, and
1982
     we've printed the address; now print the size.  */
1983
0
  if (symbol->section && bfd_is_com_section (symbol->section))
1984
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1985
0
  else
1986
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1987
0
  bfd_fprintf_vma (abfd, file, val);
1988
1989
  /* If we have version information, print it.  */
1990
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
1991
0
                   symbol,
1992
0
                   true,
1993
0
                   &hidden);
1994
0
  if (version_string)
1995
0
    {
1996
0
      if (!hidden)
1997
0
        fprintf (file, "  %-11s", version_string);
1998
0
      else
1999
0
        {
2000
0
    int i;
2001
2002
0
    fprintf (file, " (%s)", version_string);
2003
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2004
0
      putc (' ', file);
2005
0
        }
2006
0
    }
2007
2008
  /* If the st_other field is not zero, print it.  */
2009
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2010
2011
0
  switch (st_other)
2012
0
    {
2013
0
    case 0: break;
2014
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2015
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2016
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2017
0
    default:
2018
      /* Some other non-defined flags are also present, so print
2019
         everything hex.  */
2020
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2021
0
    }
2022
2023
0
  fprintf (file, " %s", name);
2024
0
      }
2025
0
      break;
2026
0
    }
2027
0
}
2028

2029
/* ELF .o/exec file reading */
2030
2031
/* Create a new bfd section from an ELF section header.  */
2032
2033
bool
2034
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2035
3.79M
{
2036
3.79M
  Elf_Internal_Shdr *hdr;
2037
3.79M
  Elf_Internal_Ehdr *ehdr;
2038
3.79M
  const struct elf_backend_data *bed;
2039
3.79M
  const char *name;
2040
3.79M
  bool ret = true;
2041
2042
3.79M
  if (shindex >= elf_numsections (abfd))
2043
0
    return false;
2044
2045
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2046
     sh_link or sh_info.  Detect this here, by refusing to load a
2047
     section that we are already in the process of loading.  */
2048
3.79M
  if (elf_tdata (abfd)->being_created[shindex])
2049
610
    {
2050
610
      _bfd_error_handler
2051
610
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2052
610
      return false;
2053
610
    }
2054
3.79M
  elf_tdata (abfd)->being_created[shindex] = true;
2055
2056
3.79M
  hdr = elf_elfsections (abfd)[shindex];
2057
3.79M
  ehdr = elf_elfheader (abfd);
2058
3.79M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2059
3.79M
            hdr->sh_name);
2060
3.79M
  if (name == NULL)
2061
14.5k
    goto fail;
2062
2063
3.77M
  bed = get_elf_backend_data (abfd);
2064
3.77M
  switch (hdr->sh_type)
2065
3.77M
    {
2066
930k
    case SHT_NULL:
2067
      /* Inactive section. Throw it away.  */
2068
930k
      goto success;
2069
2070
627k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2071
682k
    case SHT_NOBITS:   /* .bss section.  */
2072
698k
    case SHT_HASH:   /* .hash section.  */
2073
776k
    case SHT_NOTE:   /* .note section.  */
2074
811k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2075
829k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2076
852k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2077
852k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2078
855k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2079
855k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2080
855k
      goto success;
2081
2082
17.6k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2083
17.6k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2084
28
  goto fail;
2085
2086
17.6k
      if (hdr->sh_link > elf_numsections (abfd))
2087
259
  {
2088
    /* PR 10478: Accept Solaris binaries with a sh_link field
2089
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2090
259
    switch (bfd_get_arch (abfd))
2091
259
      {
2092
247
      case bfd_arch_i386:
2093
259
      case bfd_arch_sparc:
2094
259
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2095
259
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2096
259
    break;
2097
        /* Otherwise fall through.  */
2098
0
      default:
2099
0
        goto fail;
2100
259
      }
2101
259
  }
2102
17.3k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2103
0
  goto fail;
2104
17.3k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2105
13.7k
  {
2106
13.7k
    Elf_Internal_Shdr *dynsymhdr;
2107
2108
    /* The shared libraries distributed with hpux11 have a bogus
2109
       sh_link field for the ".dynamic" section.  Find the
2110
       string table for the ".dynsym" section instead.  */
2111
13.7k
    if (elf_dynsymtab (abfd) != 0)
2112
1.33k
      {
2113
1.33k
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2114
1.33k
        hdr->sh_link = dynsymhdr->sh_link;
2115
1.33k
      }
2116
12.3k
    else
2117
12.3k
      {
2118
12.3k
        unsigned int i, num_sec;
2119
2120
12.3k
        num_sec = elf_numsections (abfd);
2121
211k
        for (i = 1; i < num_sec; i++)
2122
203k
    {
2123
203k
      dynsymhdr = elf_elfsections (abfd)[i];
2124
203k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2125
4.03k
        {
2126
4.03k
          hdr->sh_link = dynsymhdr->sh_link;
2127
4.03k
          break;
2128
4.03k
        }
2129
203k
    }
2130
12.3k
      }
2131
13.7k
  }
2132
17.6k
      goto success;
2133
2134
241k
    case SHT_SYMTAB:   /* A symbol table.  */
2135
241k
      if (elf_onesymtab (abfd) == shindex)
2136
176k
  goto success;
2137
2138
65.1k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2139
529
  goto fail;
2140
2141
64.6k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2142
1.03k
  {
2143
1.03k
    if (hdr->sh_size != 0)
2144
119
      goto fail;
2145
    /* Some assemblers erroneously set sh_info to one with a
2146
       zero sh_size.  ld sees this as a global symbol count
2147
       of (unsigned) -1.  Fix it here.  */
2148
918
    hdr->sh_info = 0;
2149
918
    goto success;
2150
1.03k
  }
2151
2152
      /* PR 18854: A binary might contain more than one symbol table.
2153
   Unusual, but possible.  Warn, but continue.  */
2154
63.6k
      if (elf_onesymtab (abfd) != 0)
2155
14.2k
  {
2156
14.2k
    _bfd_error_handler
2157
      /* xgettext:c-format */
2158
14.2k
      (_("%pB: warning: multiple symbol tables detected"
2159
14.2k
         " - ignoring the table in section %u"),
2160
14.2k
       abfd, shindex);
2161
14.2k
    goto success;
2162
14.2k
  }
2163
49.3k
      elf_onesymtab (abfd) = shindex;
2164
49.3k
      elf_symtab_hdr (abfd) = *hdr;
2165
49.3k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2166
49.3k
      abfd->flags |= HAS_SYMS;
2167
2168
      /* Sometimes a shared object will map in the symbol table.  If
2169
   SHF_ALLOC is set, and this is a shared object, then we also
2170
   treat this section as a BFD section.  We can not base the
2171
   decision purely on SHF_ALLOC, because that flag is sometimes
2172
   set in a relocatable object file, which would confuse the
2173
   linker.  */
2174
49.3k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2175
49.3k
    && (abfd->flags & DYNAMIC) != 0
2176
49.3k
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2177
1.16k
            shindex))
2178
0
  goto fail;
2179
2180
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2181
   can't read symbols without that section loaded as well.  It
2182
   is most likely specified by the next section header.  */
2183
49.3k
      {
2184
49.3k
  elf_section_list * entry;
2185
49.3k
  unsigned int i, num_sec;
2186
2187
52.3k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2188
4.91k
    if (entry->hdr.sh_link == shindex)
2189
1.91k
      goto success;
2190
2191
47.4k
  num_sec = elf_numsections (abfd);
2192
142k
  for (i = shindex + 1; i < num_sec; i++)
2193
95.0k
    {
2194
95.0k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2195
2196
95.0k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2197
95.0k
    && hdr2->sh_link == shindex)
2198
26
        break;
2199
95.0k
    }
2200
2201
47.4k
  if (i == num_sec)
2202
1.15M
    for (i = 1; i < shindex; i++)
2203
1.10M
      {
2204
1.10M
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2205
2206
1.10M
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2207
1.10M
      && hdr2->sh_link == shindex)
2208
473
    break;
2209
1.10M
      }
2210
2211
47.4k
  if (i != shindex)
2212
499
    ret = bfd_section_from_shdr (abfd, i);
2213
  /* else FIXME: we have failed to find the symbol table.
2214
     Should we issue an error?  */
2215
47.4k
  goto success;
2216
49.3k
      }
2217
2218
17.1k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2219
17.1k
      if (elf_dynsymtab (abfd) == shindex)
2220
6.59k
  goto success;
2221
2222
10.5k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2223
248
  goto fail;
2224
2225
10.3k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2226
380
  {
2227
380
    if (hdr->sh_size != 0)
2228
68
      goto fail;
2229
2230
    /* Some linkers erroneously set sh_info to one with a
2231
       zero sh_size.  ld sees this as a global symbol count
2232
       of (unsigned) -1.  Fix it here.  */
2233
312
    hdr->sh_info = 0;
2234
312
    goto success;
2235
380
  }
2236
2237
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2238
   Unusual, but possible.  Warn, but continue.  */
2239
9.93k
      if (elf_dynsymtab (abfd) != 0)
2240
510
  {
2241
510
    _bfd_error_handler
2242
      /* xgettext:c-format */
2243
510
      (_("%pB: warning: multiple dynamic symbol tables detected"
2244
510
         " - ignoring the table in section %u"),
2245
510
       abfd, shindex);
2246
510
    goto success;
2247
510
  }
2248
9.42k
      elf_dynsymtab (abfd) = shindex;
2249
9.42k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2250
9.42k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2251
9.42k
      abfd->flags |= HAS_SYMS;
2252
2253
      /* Besides being a symbol table, we also treat this as a regular
2254
   section, so that objcopy can handle it.  */
2255
9.42k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2256
9.42k
      goto success;
2257
2258
17.1k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2259
17.1k
      {
2260
17.1k
  elf_section_list * entry;
2261
2262
24.7k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2263
7.98k
    if (entry->ndx == shindex)
2264
428
      goto success;
2265
2266
16.7k
  entry = bfd_alloc (abfd, sizeof (*entry));
2267
16.7k
  if (entry == NULL)
2268
0
    goto fail;
2269
16.7k
  entry->ndx = shindex;
2270
16.7k
  entry->hdr = * hdr;
2271
16.7k
  entry->next = elf_symtab_shndx_list (abfd);
2272
16.7k
  elf_symtab_shndx_list (abfd) = entry;
2273
16.7k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2274
16.7k
  goto success;
2275
16.7k
      }
2276
2277
312k
    case SHT_STRTAB:   /* A string table.  */
2278
312k
      if (hdr->bfd_section != NULL)
2279
536
  goto success;
2280
2281
311k
      if (ehdr->e_shstrndx == shindex)
2282
288k
  {
2283
288k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2284
288k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2285
288k
    goto success;
2286
288k
  }
2287
2288
23.1k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2289
2.35k
  {
2290
2.39k
  symtab_strtab:
2291
2.39k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2292
2.39k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2293
2.39k
    goto success;
2294
2.35k
  }
2295
2296
20.7k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2297
2.87k
  {
2298
2.95k
  dynsymtab_strtab:
2299
2.95k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2300
2.95k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2301
2.95k
    elf_elfsections (abfd)[shindex] = hdr;
2302
    /* We also treat this as a regular section, so that objcopy
2303
       can handle it.  */
2304
2.95k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2305
2.95k
             shindex);
2306
2.95k
    goto success;
2307
2.87k
  }
2308
2309
      /* If the string table isn't one of the above, then treat it as a
2310
   regular section.  We need to scan all the headers to be sure,
2311
   just in case this strtab section appeared before the above.  */
2312
17.8k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2313
17.5k
  {
2314
17.5k
    unsigned int i, num_sec;
2315
2316
17.5k
    num_sec = elf_numsections (abfd);
2317
306k
    for (i = 1; i < num_sec; i++)
2318
289k
      {
2319
289k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2320
289k
        if (hdr2->sh_link == shindex)
2321
4.70k
    {
2322
      /* Prevent endless recursion on broken objects.  */
2323
4.70k
      if (i == shindex)
2324
52
        goto fail;
2325
4.65k
      if (! bfd_section_from_shdr (abfd, i))
2326
864
        goto fail;
2327
3.79k
      if (elf_onesymtab (abfd) == i)
2328
33
        goto symtab_strtab;
2329
3.75k
      if (elf_dynsymtab (abfd) == i)
2330
84
        goto dynsymtab_strtab;
2331
3.75k
    }
2332
289k
      }
2333
17.5k
  }
2334
16.8k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2335
16.8k
      goto success;
2336
2337
56.5k
    case SHT_REL:
2338
248k
    case SHT_RELA:
2339
249k
    case SHT_RELR:
2340
      /* *These* do a lot of work -- but build no sections!  */
2341
249k
      {
2342
249k
  asection *target_sect;
2343
249k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2344
249k
  unsigned int num_sec = elf_numsections (abfd);
2345
249k
  struct bfd_elf_section_data *esdt;
2346
249k
  bfd_size_type size;
2347
2348
249k
  if (hdr->sh_type == SHT_REL)
2349
56.5k
    size = bed->s->sizeof_rel;
2350
192k
  else if (hdr->sh_type == SHT_RELA)
2351
191k
    size = bed->s->sizeof_rela;
2352
1.01k
  else
2353
1.01k
    size = bed->s->arch_size / 8;
2354
249k
  if (hdr->sh_entsize != size)
2355
1.47k
    goto fail;
2356
2357
  /* Check for a bogus link to avoid crashing.  */
2358
247k
  if (hdr->sh_link >= num_sec)
2359
99
    {
2360
99
      _bfd_error_handler
2361
        /* xgettext:c-format */
2362
99
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2363
99
         abfd, hdr->sh_link, name, shindex);
2364
99
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2365
99
      goto success;
2366
99
    }
2367
2368
  /* Get the symbol table.  */
2369
247k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2370
247k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2371
247k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2372
225
    goto fail;
2373
2374
  /* If this is an alloc section in an executable or shared
2375
     library, or the reloc section does not use the main symbol
2376
     table we don't treat it as a reloc section.  BFD can't
2377
     adequately represent such a section, so at least for now,
2378
     we don't try.  We just present it as a normal section.  We
2379
     also can't use it as a reloc section if it points to the
2380
     null section, an invalid section, another reloc section, or
2381
     its sh_link points to the null section.  */
2382
247k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2383
247k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2384
247k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2385
247k
      || hdr->sh_type == SHT_RELR
2386
247k
      || hdr->sh_link == SHN_UNDEF
2387
247k
      || hdr->sh_link != elf_onesymtab (abfd)
2388
247k
      || hdr->sh_info == SHN_UNDEF
2389
247k
      || hdr->sh_info >= num_sec
2390
247k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2391
247k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2392
112k
    {
2393
112k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2394
112k
      goto success;
2395
112k
    }
2396
2397
135k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2398
65
    goto fail;
2399
2400
135k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2401
135k
  if (target_sect == NULL)
2402
59
    goto fail;
2403
2404
135k
  esdt = elf_section_data (target_sect);
2405
135k
  if (hdr->sh_type == SHT_RELA)
2406
122k
    p_hdr = &esdt->rela.hdr;
2407
12.2k
  else
2408
12.2k
    p_hdr = &esdt->rel.hdr;
2409
2410
  /* PR 17512: file: 0b4f81b7.
2411
     Also see PR 24456, for a file which deliberately has two reloc
2412
     sections.  */
2413
135k
  if (*p_hdr != NULL)
2414
2.76k
    {
2415
2.76k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2416
35
        {
2417
35
    _bfd_error_handler
2418
      /* xgettext:c-format */
2419
35
      (_("%pB: warning: secondary relocation section '%s' "
2420
35
         "for section %pA found - ignoring"),
2421
35
       abfd, name, target_sect);
2422
35
        }
2423
2.72k
      else
2424
2.72k
        esdt->has_secondary_relocs = true;
2425
2.76k
      goto success;
2426
2.76k
    }
2427
2428
132k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2429
132k
  if (hdr2 == NULL)
2430
0
    goto fail;
2431
132k
  *hdr2 = *hdr;
2432
132k
  *p_hdr = hdr2;
2433
132k
  elf_elfsections (abfd)[shindex] = hdr2;
2434
132k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2435
132k
             * bed->s->int_rels_per_ext_rel);
2436
132k
  target_sect->flags |= SEC_RELOC;
2437
132k
  target_sect->relocation = NULL;
2438
132k
  target_sect->rel_filepos = hdr->sh_offset;
2439
  /* In the section to which the relocations apply, mark whether
2440
     its relocations are of the REL or RELA variety.  */
2441
132k
  if (hdr->sh_size != 0)
2442
126k
    {
2443
126k
      if (hdr->sh_type == SHT_RELA)
2444
119k
        target_sect->use_rela_p = 1;
2445
126k
    }
2446
132k
  abfd->flags |= HAS_RELOC;
2447
132k
  goto success;
2448
132k
      }
2449
2450
912
    case SHT_GNU_verdef:
2451
912
      if (hdr->sh_info != 0)
2452
542
  elf_dynverdef (abfd) = shindex;
2453
912
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2454
912
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2455
912
      goto success;
2456
2457
1.67k
    case SHT_GNU_versym:
2458
1.67k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2459
66
  goto fail;
2460
2461
1.60k
      elf_dynversym (abfd) = shindex;
2462
1.60k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2463
1.60k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2464
1.60k
      goto success;
2465
2466
7.81k
    case SHT_GNU_verneed:
2467
7.81k
      if (hdr->sh_info != 0)
2468
6.79k
  elf_dynverref (abfd) = shindex;
2469
7.81k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2470
7.81k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2471
7.81k
      goto success;
2472
2473
12.1k
    case SHT_SHLIB:
2474
12.1k
      goto success;
2475
2476
122k
    case SHT_GROUP:
2477
122k
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2478
3.27k
  goto fail;
2479
2480
119k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2481
846
  goto fail;
2482
2483
118k
      goto success;
2484
2485
989k
    default:
2486
      /* Possibly an attributes section.  */
2487
989k
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2488
989k
    || hdr->sh_type == bed->obj_attrs_section_type)
2489
33.4k
  {
2490
33.4k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2491
0
      goto fail;
2492
33.4k
    _bfd_elf_parse_attributes (abfd, hdr);
2493
33.4k
    goto success;
2494
33.4k
  }
2495
2496
      /* Check for any processor-specific section types.  */
2497
956k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2498
832k
  goto success;
2499
2500
123k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2501
58.2k
  {
2502
58.2k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2503
      /* FIXME: How to properly handle allocated section reserved
2504
         for applications?  */
2505
12.3k
      _bfd_error_handler
2506
        /* xgettext:c-format */
2507
12.3k
        (_("%pB: unknown type [%#x] section `%s'"),
2508
12.3k
         abfd, hdr->sh_type, name);
2509
45.8k
    else
2510
45.8k
      {
2511
        /* Allow sections reserved for applications.  */
2512
45.8k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2513
45.8k
        goto success;
2514
45.8k
      }
2515
58.2k
  }
2516
65.2k
      else if (hdr->sh_type >= SHT_LOPROC
2517
65.2k
         && hdr->sh_type <= SHT_HIPROC)
2518
  /* FIXME: We should handle this section.  */
2519
10.0k
  _bfd_error_handler
2520
    /* xgettext:c-format */
2521
10.0k
    (_("%pB: unknown type [%#x] section `%s'"),
2522
10.0k
     abfd, hdr->sh_type, name);
2523
55.1k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2524
19.6k
  {
2525
    /* Unrecognised OS-specific sections.  */
2526
19.6k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2527
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2528
         required to correctly process the section and the file should
2529
         be rejected with an error message.  */
2530
1.80k
      _bfd_error_handler
2531
        /* xgettext:c-format */
2532
1.80k
        (_("%pB: unknown type [%#x] section `%s'"),
2533
1.80k
         abfd, hdr->sh_type, name);
2534
17.8k
    else
2535
17.8k
      {
2536
        /* Otherwise it should be processed.  */
2537
17.8k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2538
17.8k
        goto success;
2539
17.8k
      }
2540
19.6k
  }
2541
35.5k
      else
2542
  /* FIXME: We should handle this section.  */
2543
35.5k
  _bfd_error_handler
2544
    /* xgettext:c-format */
2545
35.5k
    (_("%pB: unknown type [%#x] section `%s'"),
2546
35.5k
     abfd, hdr->sh_type, name);
2547
2548
59.7k
      goto fail;
2549
3.77M
    }
2550
2551
82.2k
 fail:
2552
82.2k
  ret = false;
2553
3.79M
 success:
2554
3.79M
  elf_tdata (abfd)->being_created[shindex] = false;
2555
3.79M
  return ret;
2556
82.2k
}
2557
2558
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
2559
2560
Elf_Internal_Sym *
2561
bfd_sym_from_r_symndx (struct sym_cache *cache,
2562
           bfd *abfd,
2563
           unsigned long r_symndx)
2564
0
{
2565
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2566
2567
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2568
0
    {
2569
0
      Elf_Internal_Shdr *symtab_hdr;
2570
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
2571
0
      Elf_External_Sym_Shndx eshndx;
2572
2573
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2574
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2575
0
        &cache->sym[ent], esym, &eshndx) == NULL)
2576
0
  return NULL;
2577
2578
0
      if (cache->abfd != abfd)
2579
0
  {
2580
0
    memset (cache->indx, -1, sizeof (cache->indx));
2581
0
    cache->abfd = abfd;
2582
0
  }
2583
0
      cache->indx[ent] = r_symndx;
2584
0
    }
2585
2586
0
  return &cache->sym[ent];
2587
0
}
2588
2589
/* Given an ELF section number, retrieve the corresponding BFD
2590
   section.  */
2591
2592
asection *
2593
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2594
736k
{
2595
736k
  if (sec_index >= elf_numsections (abfd))
2596
274k
    return NULL;
2597
462k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2598
736k
}
2599
2600
static const struct bfd_elf_special_section special_sections_b[] =
2601
{
2602
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2603
  { NULL,       0,  0, 0,    0 }
2604
};
2605
2606
static const struct bfd_elf_special_section special_sections_c[] =
2607
{
2608
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2609
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
2610
  { NULL,     0, 0, 0,      0 }
2611
};
2612
2613
static const struct bfd_elf_special_section special_sections_d[] =
2614
{
2615
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2616
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2617
  /* There are more DWARF sections than these, but they needn't be added here
2618
     unless you have to cope with broken compilers that don't emit section
2619
     attributes or you want to help the user writing assembler.  */
2620
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
2621
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
2622
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
2623
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
2624
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2625
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
2626
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
2627
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
2628
  { NULL,          0,  0, 0,      0 }
2629
};
2630
2631
static const struct bfd_elf_special_section special_sections_f[] =
2632
{
2633
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2634
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2635
  { NULL,        0 , 0, 0,      0 }
2636
};
2637
2638
static const struct bfd_elf_special_section special_sections_g[] =
2639
{
2640
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2641
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2642
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2643
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
2644
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2645
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2646
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2647
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2648
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2649
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2650
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
2651
  { NULL,      0,    0, 0,         0 }
2652
};
2653
2654
static const struct bfd_elf_special_section special_sections_h[] =
2655
{
2656
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
2657
  { NULL,        0, 0, 0,    0 }
2658
};
2659
2660
static const struct bfd_elf_special_section special_sections_i[] =
2661
{
2662
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2663
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2664
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
2665
  { NULL,          0,      0, 0,      0 }
2666
};
2667
2668
static const struct bfd_elf_special_section special_sections_l[] =
2669
{
2670
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2671
  { NULL,        0, 0, 0,    0 }
2672
};
2673
2674
static const struct bfd_elf_special_section special_sections_n[] =
2675
{
2676
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2677
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2678
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
2679
  { NULL,        0,     0, 0,      0 }
2680
};
2681
2682
static const struct bfd_elf_special_section special_sections_p[] =
2683
{
2684
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
2685
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
2686
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2687
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
2688
  { NULL,       0,      0, 0,     0 }
2689
};
2690
2691
static const struct bfd_elf_special_section special_sections_r[] =
2692
{
2693
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2694
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2695
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2696
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2697
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2698
  { NULL,       0,     0, 0,      0 }
2699
};
2700
2701
static const struct bfd_elf_special_section special_sections_s[] =
2702
{
2703
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2704
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2705
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2706
  /* See struct bfd_elf_special_section declaration for the semantics of
2707
     this special case where .prefix_length != strlen (.prefix).  */
2708
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
2709
  { NULL,     0,  0, 0,    0 }
2710
};
2711
2712
static const struct bfd_elf_special_section special_sections_t[] =
2713
{
2714
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2715
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2716
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2717
  { NULL,         0,  0, 0,      0 }
2718
};
2719
2720
static const struct bfd_elf_special_section special_sections_z[] =
2721
{
2722
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2723
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2724
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2725
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2726
  { NULL,         0,  0, 0,      0 }
2727
};
2728
2729
static const struct bfd_elf_special_section * const special_sections[] =
2730
{
2731
  special_sections_b,   /* 'b' */
2732
  special_sections_c,   /* 'c' */
2733
  special_sections_d,   /* 'd' */
2734
  NULL,       /* 'e' */
2735
  special_sections_f,   /* 'f' */
2736
  special_sections_g,   /* 'g' */
2737
  special_sections_h,   /* 'h' */
2738
  special_sections_i,   /* 'i' */
2739
  NULL,       /* 'j' */
2740
  NULL,       /* 'k' */
2741
  special_sections_l,   /* 'l' */
2742
  NULL,       /* 'm' */
2743
  special_sections_n,   /* 'n' */
2744
  NULL,       /* 'o' */
2745
  special_sections_p,   /* 'p' */
2746
  NULL,       /* 'q' */
2747
  special_sections_r,   /* 'r' */
2748
  special_sections_s,   /* 's' */
2749
  special_sections_t,   /* 't' */
2750
  NULL,       /* 'u' */
2751
  NULL,       /* 'v' */
2752
  NULL,       /* 'w' */
2753
  NULL,       /* 'x' */
2754
  NULL,       /* 'y' */
2755
  special_sections_z    /* 'z' */
2756
};
2757
2758
const struct bfd_elf_special_section *
2759
_bfd_elf_get_special_section (const char *name,
2760
            const struct bfd_elf_special_section *spec,
2761
            unsigned int rela)
2762
2.07M
{
2763
2.07M
  int i;
2764
2.07M
  int len;
2765
2766
2.07M
  len = strlen (name);
2767
2768
11.6M
  for (i = 0; spec[i].prefix != NULL; i++)
2769
9.96M
    {
2770
9.96M
      int suffix_len;
2771
9.96M
      int prefix_len = spec[i].prefix_length;
2772
2773
9.96M
      if (len < prefix_len)
2774
5.52M
  continue;
2775
4.44M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2776
3.91M
  continue;
2777
2778
533k
      suffix_len = spec[i].suffix_length;
2779
533k
      if (suffix_len <= 0)
2780
514k
  {
2781
514k
    if (name[prefix_len] != 0)
2782
356k
      {
2783
356k
        if (suffix_len == 0)
2784
97.3k
    continue;
2785
259k
        if (name[prefix_len] != '.'
2786
259k
      && (suffix_len == -2
2787
60.0k
          || (rela && spec[i].type == SHT_REL)))
2788
44.2k
    continue;
2789
259k
      }
2790
514k
  }
2791
18.3k
      else
2792
18.3k
  {
2793
18.3k
    if (len < prefix_len + suffix_len)
2794
11.8k
      continue;
2795
6.54k
    if (memcmp (name + len - suffix_len,
2796
6.54k
          spec[i].prefix + prefix_len,
2797
6.54k
          suffix_len) != 0)
2798
3.71k
      continue;
2799
6.54k
  }
2800
375k
      return &spec[i];
2801
533k
    }
2802
2803
1.70M
  return NULL;
2804
2.07M
}
2805
2806
const struct bfd_elf_special_section *
2807
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2808
3.08M
{
2809
3.08M
  int i;
2810
3.08M
  const struct bfd_elf_special_section *spec;
2811
3.08M
  const struct elf_backend_data *bed;
2812
2813
  /* See if this is one of the special sections.  */
2814
3.08M
  if (sec->name == NULL)
2815
0
    return NULL;
2816
2817
3.08M
  bed = get_elf_backend_data (abfd);
2818
3.08M
  spec = bed->special_sections;
2819
3.08M
  if (spec)
2820
1.41M
    {
2821
1.41M
      spec = _bfd_elf_get_special_section (sec->name,
2822
1.41M
             bed->special_sections,
2823
1.41M
             sec->use_rela_p);
2824
1.41M
      if (spec != NULL)
2825
8.45k
  return spec;
2826
1.41M
    }
2827
2828
3.07M
  if (sec->name[0] != '.')
2829
2.40M
    return NULL;
2830
2831
670k
  i = sec->name[1] - 'b';
2832
670k
  if (i < 0 || i > 'z' - 'b')
2833
32.6k
    return NULL;
2834
2835
638k
  spec = special_sections[i];
2836
2837
638k
  if (spec == NULL)
2838
24.9k
    return NULL;
2839
2840
613k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2841
638k
}
2842
2843
bool
2844
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2845
3.08M
{
2846
3.08M
  struct bfd_elf_section_data *sdata;
2847
3.08M
  const struct elf_backend_data *bed;
2848
3.08M
  const struct bfd_elf_special_section *ssect;
2849
2850
3.08M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2851
3.08M
  if (sdata == NULL)
2852
2.56M
    {
2853
2.56M
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2854
2.56M
                sizeof (*sdata));
2855
2.56M
      if (sdata == NULL)
2856
0
  return false;
2857
2.56M
      sec->used_by_bfd = sdata;
2858
2.56M
    }
2859
2860
  /* Indicate whether or not this section should use RELA relocations.  */
2861
3.08M
  bed = get_elf_backend_data (abfd);
2862
3.08M
  sec->use_rela_p = bed->default_use_rela_p;
2863
2864
  /* Set up ELF section type and flags for newly created sections, if
2865
     there is an ABI mandated section.  */
2866
3.08M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
2867
3.08M
  if (ssect != NULL)
2868
375k
    {
2869
375k
      elf_section_type (sec) = ssect->type;
2870
375k
      elf_section_flags (sec) = ssect->attr;
2871
375k
    }
2872
2873
3.08M
  return _bfd_generic_new_section_hook (abfd, sec);
2874
3.08M
}
2875
2876
/* Create a new bfd section from an ELF program header.
2877
2878
   Since program segments have no names, we generate a synthetic name
2879
   of the form segment<NUM>, where NUM is generally the index in the
2880
   program header table.  For segments that are split (see below) we
2881
   generate the names segment<NUM>a and segment<NUM>b.
2882
2883
   Note that some program segments may have a file size that is different than
2884
   (less than) the memory size.  All this means is that at execution the
2885
   system must allocate the amount of memory specified by the memory size,
2886
   but only initialize it with the first "file size" bytes read from the
2887
   file.  This would occur for example, with program segments consisting
2888
   of combined data+bss.
2889
2890
   To handle the above situation, this routine generates TWO bfd sections
2891
   for the single program segment.  The first has the length specified by
2892
   the file size of the segment, and the second has the length specified
2893
   by the difference between the two sizes.  In effect, the segment is split
2894
   into its initialized and uninitialized parts.  */
2895
2896
bool
2897
_bfd_elf_make_section_from_phdr (bfd *abfd,
2898
         Elf_Internal_Phdr *hdr,
2899
         int hdr_index,
2900
         const char *type_name)
2901
936k
{
2902
936k
  asection *newsect;
2903
936k
  char *name;
2904
936k
  char namebuf[64];
2905
936k
  size_t len;
2906
936k
  int split;
2907
936k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2908
2909
936k
  split = ((hdr->p_memsz > 0)
2910
936k
      && (hdr->p_filesz > 0)
2911
936k
      && (hdr->p_memsz > hdr->p_filesz));
2912
2913
936k
  if (hdr->p_filesz > 0)
2914
773k
    {
2915
773k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2916
773k
      len = strlen (namebuf) + 1;
2917
773k
      name = (char *) bfd_alloc (abfd, len);
2918
773k
      if (!name)
2919
0
  return false;
2920
773k
      memcpy (name, namebuf, len);
2921
773k
      newsect = bfd_make_section (abfd, name);
2922
773k
      if (newsect == NULL)
2923
0
  return false;
2924
773k
      newsect->vma = hdr->p_vaddr / opb;
2925
773k
      newsect->lma = hdr->p_paddr / opb;
2926
773k
      newsect->size = hdr->p_filesz;
2927
773k
      newsect->filepos = hdr->p_offset;
2928
773k
      newsect->flags |= SEC_HAS_CONTENTS;
2929
773k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2930
773k
      if (hdr->p_type == PT_LOAD)
2931
10.2k
  {
2932
10.2k
    newsect->flags |= SEC_ALLOC;
2933
10.2k
    newsect->flags |= SEC_LOAD;
2934
10.2k
    if (hdr->p_flags & PF_X)
2935
5.97k
      {
2936
        /* FIXME: all we known is that it has execute PERMISSION,
2937
     may be data.  */
2938
5.97k
        newsect->flags |= SEC_CODE;
2939
5.97k
      }
2940
10.2k
  }
2941
773k
      if (!(hdr->p_flags & PF_W))
2942
531k
  {
2943
531k
    newsect->flags |= SEC_READONLY;
2944
531k
  }
2945
773k
    }
2946
2947
936k
  if (hdr->p_memsz > hdr->p_filesz)
2948
381k
    {
2949
381k
      bfd_vma align;
2950
2951
381k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2952
381k
      len = strlen (namebuf) + 1;
2953
381k
      name = (char *) bfd_alloc (abfd, len);
2954
381k
      if (!name)
2955
0
  return false;
2956
381k
      memcpy (name, namebuf, len);
2957
381k
      newsect = bfd_make_section (abfd, name);
2958
381k
      if (newsect == NULL)
2959
0
  return false;
2960
381k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2961
381k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
2962
381k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2963
381k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2964
381k
      align = newsect->vma & -newsect->vma;
2965
381k
      if (align == 0 || align > hdr->p_align)
2966
91.2k
  align = hdr->p_align;
2967
381k
      newsect->alignment_power = bfd_log2 (align);
2968
381k
      if (hdr->p_type == PT_LOAD)
2969
5.38k
  {
2970
5.38k
    newsect->flags |= SEC_ALLOC;
2971
5.38k
    if (hdr->p_flags & PF_X)
2972
2.75k
      newsect->flags |= SEC_CODE;
2973
5.38k
  }
2974
381k
      if (!(hdr->p_flags & PF_W))
2975
267k
  newsect->flags |= SEC_READONLY;
2976
381k
    }
2977
2978
936k
  return true;
2979
936k
}
2980
2981
static bool
2982
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
2983
11.9k
{
2984
  /* The return value is ignored.  Build-ids are considered optional.  */
2985
11.9k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
2986
11.9k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
2987
11.9k
      (templ, offset);
2988
0
  return false;
2989
11.9k
}
2990
2991
bool
2992
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2993
936k
{
2994
936k
  const struct elf_backend_data *bed;
2995
2996
936k
  switch (hdr->p_type)
2997
936k
    {
2998
177k
    case PT_NULL:
2999
177k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3000
3001
11.9k
    case PT_LOAD:
3002
11.9k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3003
0
  return false;
3004
11.9k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3005
11.9k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3006
11.9k
      return true;
3007
3008
4.45k
    case PT_DYNAMIC:
3009
4.45k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3010
3011
2.94k
    case PT_INTERP:
3012
2.94k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3013
3014
2.11k
    case PT_NOTE:
3015
2.11k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3016
0
  return false;
3017
2.11k
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3018
2.11k
          hdr->p_align))
3019
1.20k
  return false;
3020
903
      return true;
3021
3022
1.22k
    case PT_SHLIB:
3023
1.22k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3024
3025
5.64k
    case PT_PHDR:
3026
5.64k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3027
3028
73
    case PT_GNU_EH_FRAME:
3029
73
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3030
73
                "eh_frame_hdr");
3031
3032
55
    case PT_GNU_STACK:
3033
55
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3034
3035
92
    case PT_GNU_RELRO:
3036
92
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3037
3038
8
    case PT_GNU_SFRAME:
3039
8
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040
8
                "sframe");
3041
3042
730k
    default:
3043
      /* Check for any processor-specific program segment types.  */
3044
730k
      bed = get_elf_backend_data (abfd);
3045
730k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3046
936k
    }
3047
936k
}
3048
3049
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3050
   REL or RELA.  */
3051
3052
Elf_Internal_Shdr *
3053
_bfd_elf_single_rel_hdr (asection *sec)
3054
0
{
3055
0
  if (elf_section_data (sec)->rel.hdr)
3056
0
    {
3057
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3058
0
      return elf_section_data (sec)->rel.hdr;
3059
0
    }
3060
0
  else
3061
0
    return elf_section_data (sec)->rela.hdr;
3062
0
}
3063
3064
static bool
3065
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3066
          Elf_Internal_Shdr *rel_hdr,
3067
          const char *sec_name,
3068
          bool use_rela_p)
3069
1.47k
{
3070
1.47k
  char *name = (char *) bfd_alloc (abfd,
3071
1.47k
           sizeof ".rela" + strlen (sec_name));
3072
1.47k
  if (name == NULL)
3073
0
    return false;
3074
3075
1.47k
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3076
1.47k
  rel_hdr->sh_name =
3077
1.47k
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3078
1.47k
          false);
3079
1.47k
  if (rel_hdr->sh_name == (unsigned int) -1)
3080
0
    return false;
3081
3082
1.47k
  return true;
3083
1.47k
}
3084
3085
/* Allocate and initialize a section-header for a new reloc section,
3086
   containing relocations against ASECT.  It is stored in RELDATA.  If
3087
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3088
   relocations.  */
3089
3090
static bool
3091
_bfd_elf_init_reloc_shdr (bfd *abfd,
3092
        struct bfd_elf_section_reloc_data *reldata,
3093
        const char *sec_name,
3094
        bool use_rela_p,
3095
        bool delay_st_name_p)
3096
1.47k
{
3097
1.47k
  Elf_Internal_Shdr *rel_hdr;
3098
1.47k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3099
3100
1.47k
  BFD_ASSERT (reldata->hdr == NULL);
3101
1.47k
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3102
1.47k
  reldata->hdr = rel_hdr;
3103
3104
1.47k
  if (delay_st_name_p)
3105
0
    rel_hdr->sh_name = (unsigned int) -1;
3106
1.47k
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3107
1.47k
          use_rela_p))
3108
0
    return false;
3109
1.47k
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3110
1.47k
  rel_hdr->sh_entsize = (use_rela_p
3111
1.47k
       ? bed->s->sizeof_rela
3112
1.47k
       : bed->s->sizeof_rel);
3113
1.47k
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3114
1.47k
  rel_hdr->sh_flags = 0;
3115
1.47k
  rel_hdr->sh_addr = 0;
3116
1.47k
  rel_hdr->sh_size = 0;
3117
1.47k
  rel_hdr->sh_offset = 0;
3118
3119
1.47k
  return true;
3120
1.47k
}
3121
3122
/* Return the default section type based on the passed in section flags.  */
3123
3124
int
3125
bfd_elf_get_default_section_type (flagword flags)
3126
4.56k
{
3127
4.56k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3128
4.56k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3129
90
    return SHT_NOBITS;
3130
4.47k
  return SHT_PROGBITS;
3131
4.56k
}
3132
3133
struct fake_section_arg
3134
{
3135
  struct bfd_link_info *link_info;
3136
  bool failed;
3137
};
3138
3139
/* Set up an ELF internal section header for a section.  */
3140
3141
static void
3142
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3143
4.97k
{
3144
4.97k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3145
4.97k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3146
4.97k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3147
4.97k
  Elf_Internal_Shdr *this_hdr;
3148
4.97k
  unsigned int sh_type;
3149
4.97k
  const char *name = asect->name;
3150
4.97k
  bool delay_st_name_p = false;
3151
4.97k
  bfd_vma mask;
3152
3153
4.97k
  if (arg->failed)
3154
0
    {
3155
      /* We already failed; just get out of the bfd_map_over_sections
3156
   loop.  */
3157
0
      return;
3158
0
    }
3159
3160
4.97k
  this_hdr = &esd->this_hdr;
3161
3162
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3163
4.97k
  if (arg->link_info
3164
4.97k
      && (abfd->flags & BFD_COMPRESS) != 0
3165
4.97k
      && (asect->flags & SEC_DEBUGGING) != 0
3166
4.97k
      && name[1] == 'd'
3167
4.97k
      && name[6] == '_')
3168
0
    {
3169
      /* If this section will be compressed, delay adding section
3170
   name to section name section after it is compressed in
3171
   _bfd_elf_assign_file_positions_for_non_load.  */
3172
0
      delay_st_name_p = true;
3173
0
    }
3174
3175
4.97k
  if (delay_st_name_p)
3176
0
    this_hdr->sh_name = (unsigned int) -1;
3177
4.97k
  else
3178
4.97k
    {
3179
4.97k
      this_hdr->sh_name
3180
4.97k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3181
4.97k
                name, false);
3182
4.97k
      if (this_hdr->sh_name == (unsigned int) -1)
3183
0
  {
3184
0
    arg->failed = true;
3185
0
    return;
3186
0
  }
3187
4.97k
    }
3188
3189
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3190
3191
4.97k
  if ((asect->flags & SEC_ALLOC) != 0
3192
4.97k
      || asect->user_set_vma)
3193
4.97k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3194
0
  else
3195
0
    this_hdr->sh_addr = 0;
3196
3197
4.97k
  this_hdr->sh_offset = 0;
3198
4.97k
  this_hdr->sh_size = asect->size;
3199
4.97k
  this_hdr->sh_link = 0;
3200
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3201
4.97k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3202
0
    {
3203
0
      _bfd_error_handler
3204
  /* xgettext:c-format */
3205
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3206
0
   abfd, asect->alignment_power, asect);
3207
0
      arg->failed = true;
3208
0
      return;
3209
0
    }
3210
  /* Set sh_addralign to the highest power of two given by alignment
3211
     consistent with the section VMA.  Linker scripts can force VMA.  */
3212
4.97k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3213
4.97k
  this_hdr->sh_addralign = mask & -mask;
3214
  /* The sh_entsize and sh_info fields may have been set already by
3215
     copy_private_section_data.  */
3216
3217
4.97k
  this_hdr->bfd_section = asect;
3218
4.97k
  this_hdr->contents = NULL;
3219
3220
  /* If the section type is unspecified, we set it based on
3221
     asect->flags.  */
3222
4.97k
  if (asect->type != 0)
3223
0
    sh_type = asect->type;
3224
4.97k
  else if ((asect->flags & SEC_GROUP) != 0)
3225
577
    sh_type = SHT_GROUP;
3226
4.39k
  else
3227
4.39k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3228
3229
4.97k
  if (this_hdr->sh_type == SHT_NULL)
3230
9
    this_hdr->sh_type = sh_type;
3231
4.96k
  else if (this_hdr->sh_type == SHT_NOBITS
3232
4.96k
     && sh_type == SHT_PROGBITS
3233
4.96k
     && (asect->flags & SEC_ALLOC) != 0)
3234
0
    {
3235
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3236
   allow the link to proceed.  This can happen when users link
3237
   non-bss input sections to bss output sections, or emit data
3238
   to a bss output section via a linker script.  */
3239
0
      _bfd_error_handler
3240
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3241
0
      this_hdr->sh_type = sh_type;
3242
0
    }
3243
3244
4.97k
  switch (this_hdr->sh_type)
3245
4.97k
    {
3246
83
    default:
3247
83
      break;
3248
3249
83
    case SHT_STRTAB:
3250
129
    case SHT_NOTE:
3251
219
    case SHT_NOBITS:
3252
3.74k
    case SHT_PROGBITS:
3253
3.74k
      break;
3254
3255
72
    case SHT_INIT_ARRAY:
3256
120
    case SHT_FINI_ARRAY:
3257
120
    case SHT_PREINIT_ARRAY:
3258
120
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3259
120
      break;
3260
3261
47
    case SHT_HASH:
3262
47
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3263
47
      break;
3264
3265
67
    case SHT_DYNSYM:
3266
67
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3267
67
      break;
3268
3269
66
    case SHT_DYNAMIC:
3270
66
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3271
66
      break;
3272
3273
148
    case SHT_RELA:
3274
148
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3275
106
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3276
148
      break;
3277
3278
18
     case SHT_REL:
3279
18
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3280
18
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3281
18
      break;
3282
3283
31
     case SHT_GNU_versym:
3284
31
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3285
31
      break;
3286
3287
0
     case SHT_GNU_verdef:
3288
0
      this_hdr->sh_entsize = 0;
3289
      /* objcopy or strip will copy over sh_info, but may not set
3290
   cverdefs.  The linker will set cverdefs, but sh_info will be
3291
   zero.  */
3292
0
      if (this_hdr->sh_info == 0)
3293
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3294
0
      else
3295
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3296
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3297
0
      break;
3298
3299
46
    case SHT_GNU_verneed:
3300
46
      this_hdr->sh_entsize = 0;
3301
      /* objcopy or strip will copy over sh_info, but may not set
3302
   cverrefs.  The linker will set cverrefs, but sh_info will be
3303
   zero.  */
3304
46
      if (this_hdr->sh_info == 0)
3305
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3306
46
      else
3307
46
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3308
46
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3309
46
      break;
3310
3311
577
    case SHT_GROUP:
3312
577
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3313
577
      break;
3314
3315
25
    case SHT_GNU_HASH:
3316
25
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3317
25
      break;
3318
4.97k
    }
3319
3320
4.97k
  if ((asect->flags & SEC_ALLOC) != 0)
3321
3.67k
    this_hdr->sh_flags |= SHF_ALLOC;
3322
4.97k
  if ((asect->flags & SEC_READONLY) == 0)
3323
1.24k
    this_hdr->sh_flags |= SHF_WRITE;
3324
4.97k
  if ((asect->flags & SEC_CODE) != 0)
3325
956
    this_hdr->sh_flags |= SHF_EXECINSTR;
3326
4.97k
  if ((asect->flags & SEC_MERGE) != 0)
3327
264
    {
3328
264
      this_hdr->sh_flags |= SHF_MERGE;
3329
264
      this_hdr->sh_entsize = asect->entsize;
3330
264
    }
3331
4.97k
  if ((asect->flags & SEC_STRINGS) != 0)
3332
218
    this_hdr->sh_flags |= SHF_STRINGS;
3333
4.97k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3334
1.75k
    this_hdr->sh_flags |= SHF_GROUP;
3335
4.97k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3336
0
    {
3337
0
      this_hdr->sh_flags |= SHF_TLS;
3338
0
      if (asect->size == 0
3339
0
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3340
0
  {
3341
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3342
3343
0
    this_hdr->sh_size = 0;
3344
0
    if (o != NULL)
3345
0
      {
3346
0
        this_hdr->sh_size = o->offset + o->size;
3347
0
        if (this_hdr->sh_size != 0)
3348
0
    this_hdr->sh_type = SHT_NOBITS;
3349
0
      }
3350
0
  }
3351
0
    }
3352
4.97k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3353
29
    this_hdr->sh_flags |= SHF_EXCLUDE;
3354
3355
  /* If the section has relocs, set up a section header for the
3356
     SHT_REL[A] section.  If two relocation sections are required for
3357
     this section, it is up to the processor-specific back-end to
3358
     create the other.  */
3359
4.97k
  if ((asect->flags & SEC_RELOC) != 0)
3360
1.47k
    {
3361
      /* When doing a relocatable link, create both REL and RELA sections if
3362
   needed.  */
3363
1.47k
      if (arg->link_info
3364
    /* Do the normal setup if we wouldn't create any sections here.  */
3365
1.47k
    && esd->rel.count + esd->rela.count > 0
3366
1.47k
    && (bfd_link_relocatable (arg->link_info)
3367
0
        || arg->link_info->emitrelocations))
3368
0
  {
3369
0
    if (esd->rel.count && esd->rel.hdr == NULL
3370
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3371
0
              false, delay_st_name_p))
3372
0
      {
3373
0
        arg->failed = true;
3374
0
        return;
3375
0
      }
3376
0
    if (esd->rela.count && esd->rela.hdr == NULL
3377
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3378
0
              true, delay_st_name_p))
3379
0
      {
3380
0
        arg->failed = true;
3381
0
        return;
3382
0
      }
3383
0
  }
3384
1.47k
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3385
1.47k
            (asect->use_rela_p
3386
1.47k
             ? &esd->rela : &esd->rel),
3387
1.47k
            name,
3388
1.47k
            asect->use_rela_p,
3389
1.47k
            delay_st_name_p))
3390
0
  {
3391
0
    arg->failed = true;
3392
0
    return;
3393
0
  }
3394
1.47k
    }
3395
3396
  /* Check for processor-specific section types.  */
3397
4.97k
  sh_type = this_hdr->sh_type;
3398
4.97k
  if (bed->elf_backend_fake_sections
3399
4.97k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3400
0
    {
3401
0
      arg->failed = true;
3402
0
      return;
3403
0
    }
3404
3405
4.97k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3406
90
    {
3407
      /* Don't change the header type from NOBITS if we are being
3408
   called for objcopy --only-keep-debug.  */
3409
90
      this_hdr->sh_type = sh_type;
3410
90
    }
3411
4.97k
}
3412
3413
/* Fill in the contents of a SHT_GROUP section.  Called from
3414
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3415
   when ELF targets use the generic linker, ld.  Called for ld -r
3416
   from bfd_elf_final_link.  */
3417
3418
void
3419
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3420
4.97k
{
3421
4.97k
  bool *failedptr = (bool *) failedptrarg;
3422
4.97k
  asection *elt, *first;
3423
4.97k
  unsigned char *loc;
3424
4.97k
  bool gas;
3425
3426
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3427
     elfxx-ia64.c.  */
3428
4.97k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3429
4.97k
      || sec->size == 0
3430
4.97k
      || *failedptr)
3431
4.42k
    return;
3432
3433
551
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3434
551
    {
3435
551
      unsigned long symindx = 0;
3436
3437
      /* elf_group_id will have been set up by objcopy and the
3438
   generic linker.  */
3439
551
      if (elf_group_id (sec) != NULL)
3440
551
  symindx = elf_group_id (sec)->udata.i;
3441
3442
551
      if (symindx == 0)
3443
1
  {
3444
    /* If called from the assembler, swap_out_syms will have set up
3445
       elf_section_syms.
3446
       PR 25699: A corrupt input file could contain bogus group info.  */
3447
1
    if (sec->index >= elf_num_section_syms (abfd)
3448
1
        || elf_section_syms (abfd)[sec->index] == NULL)
3449
1
      {
3450
1
        *failedptr = true;
3451
1
        return;
3452
1
      }
3453
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3454
0
  }
3455
550
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3456
550
    }
3457
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3458
0
    {
3459
      /* The ELF backend linker sets sh_info to -2 when the group
3460
   signature symbol is global, and thus the index can't be
3461
   set until all local symbols are output.  */
3462
0
      asection *igroup;
3463
0
      struct bfd_elf_section_data *sec_data;
3464
0
      unsigned long symndx;
3465
0
      unsigned long extsymoff;
3466
0
      struct elf_link_hash_entry *h;
3467
3468
      /* The point of this little dance to the first SHF_GROUP section
3469
   then back to the SHT_GROUP section is that this gets us to
3470
   the SHT_GROUP in the input object.  */
3471
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3472
0
      sec_data = elf_section_data (igroup);
3473
0
      symndx = sec_data->this_hdr.sh_info;
3474
0
      extsymoff = 0;
3475
0
      if (!elf_bad_symtab (igroup->owner))
3476
0
  {
3477
0
    Elf_Internal_Shdr *symtab_hdr;
3478
3479
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3480
0
    extsymoff = symtab_hdr->sh_info;
3481
0
  }
3482
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3483
0
      while (h->root.type == bfd_link_hash_indirect
3484
0
       || h->root.type == bfd_link_hash_warning)
3485
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3486
3487
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3488
0
    }
3489
3490
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3491
550
  gas = true;
3492
550
  if (sec->contents == NULL)
3493
550
    {
3494
550
      gas = false;
3495
550
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3496
3497
      /* Arrange for the section to be written out.  */
3498
550
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3499
550
      if (sec->contents == NULL)
3500
0
  {
3501
0
    *failedptr = true;
3502
0
    return;
3503
0
  }
3504
550
    }
3505
3506
550
  loc = sec->contents + sec->size;
3507
3508
  /* Get the pointer to the first section in the group that gas
3509
     squirreled away here.  objcopy arranges for this to be set to the
3510
     start of the input section group.  */
3511
550
  first = elt = elf_next_in_group (sec);
3512
3513
  /* First element is a flag word.  Rest of section is elf section
3514
     indices for all the sections of the group.  Write them backwards
3515
     just to keep the group in the same order as given in .section
3516
     directives, not that it matters.  */
3517
1.67k
  while (elt != NULL)
3518
1.67k
    {
3519
1.67k
      asection *s;
3520
3521
1.67k
      s = elt;
3522
1.67k
      if (!gas)
3523
1.67k
  s = s->output_section;
3524
1.67k
      if (s != NULL
3525
1.67k
    && !bfd_is_abs_section (s))
3526
1.67k
  {
3527
1.67k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3528
1.67k
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3529
3530
1.67k
    if (elf_sec->rel.hdr != NULL
3531
1.67k
        && (gas
3532
0
      || (input_elf_sec->rel.hdr != NULL
3533
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3534
0
      {
3535
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3536
0
        loc -= 4;
3537
0
        if (loc == sec->contents)
3538
0
    break;
3539
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3540
0
      }
3541
1.67k
    if (elf_sec->rela.hdr != NULL
3542
1.67k
        && (gas
3543
1.14k
      || (input_elf_sec->rela.hdr != NULL
3544
1.14k
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3545
1.14k
      {
3546
1.14k
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3547
1.14k
        loc -= 4;
3548
1.14k
        if (loc == sec->contents)
3549
0
    break;
3550
1.14k
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3551
1.14k
      }
3552
1.67k
    loc -= 4;
3553
1.67k
    if (loc == sec->contents)
3554
0
      break;
3555
1.67k
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
3556
1.67k
  }
3557
1.67k
      elt = elf_next_in_group (elt);
3558
1.67k
      if (elt == first)
3559
550
  break;
3560
1.67k
    }
3561
3562
  /* We should always get here with loc == sec->contents + 4, but it is
3563
     possible to craft bogus SHT_GROUP sections that will cause segfaults
3564
     in objcopy without checking loc here and in the loop above.  */
3565
550
  if (loc == sec->contents)
3566
0
    BFD_ASSERT (0);
3567
550
  else
3568
550
    {
3569
550
      loc -= 4;
3570
550
      if (loc != sec->contents)
3571
0
  {
3572
0
    BFD_ASSERT (0);
3573
0
    memset (sec->contents + 4, 0, loc - sec->contents);
3574
0
    loc = sec->contents;
3575
0
  }
3576
550
    }
3577
3578
550
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3579
550
}
3580
3581
/* Given NAME, the name of a relocation section stripped of its
3582
   .rel/.rela prefix, return the section in ABFD to which the
3583
   relocations apply.  */
3584
3585
asection *
3586
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3587
144
{
3588
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3589
     section likely apply to .got.plt or .got section.  */
3590
144
  if (get_elf_backend_data (abfd)->want_got_plt
3591
144
      && strcmp (name, ".plt") == 0)
3592
23
    {
3593
23
      asection *sec;
3594
3595
23
      name = ".got.plt";
3596
23
      sec = bfd_get_section_by_name (abfd, name);
3597
23
      if (sec != NULL)
3598
3
  return sec;
3599
20
      name = ".got";
3600
20
    }
3601
3602
141
  return bfd_get_section_by_name (abfd, name);
3603
144
}
3604
3605
/* Return the section to which RELOC_SEC applies.  */
3606
3607
static asection *
3608
elf_get_reloc_section (asection *reloc_sec)
3609
166
{
3610
166
  const char *name;
3611
166
  unsigned int type;
3612
166
  bfd *abfd;
3613
166
  const struct elf_backend_data *bed;
3614
3615
166
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3616
166
  if (type != SHT_REL && type != SHT_RELA)
3617
0
    return NULL;
3618
3619
  /* We look up the section the relocs apply to by name.  */
3620
166
  name = reloc_sec->name;
3621
166
  if (!startswith (name, ".rel"))
3622
22
    return NULL;
3623
144
  name += 4;
3624
144
  if (type == SHT_RELA && *name++ != 'a')
3625
0
    return NULL;
3626
3627
144
  abfd = reloc_sec->owner;
3628
144
  bed = get_elf_backend_data (abfd);
3629
144
  return bed->get_reloc_section (abfd, name);
3630
144
}
3631
3632
/* Assign all ELF section numbers.  The dummy first section is handled here
3633
   too.  The link/info pointers for the standard section types are filled
3634
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
3635
   here for gas, objcopy, and when using the generic ELF linker.  */
3636
3637
static bool
3638
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3639
198
{
3640
198
  struct elf_obj_tdata *t = elf_tdata (abfd);
3641
198
  asection *sec;
3642
198
  unsigned int section_number;
3643
198
  Elf_Internal_Shdr **i_shdrp;
3644
198
  struct bfd_elf_section_data *d;
3645
198
  bool need_symtab;
3646
198
  size_t amt;
3647
3648
198
  section_number = 1;
3649
3650
198
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3651
3652
  /* SHT_GROUP sections are in relocatable files only.  */
3653
198
  if (link_info == NULL || !link_info->resolve_section_groups)
3654
198
    {
3655
198
      size_t reloc_count = 0;
3656
3657
      /* Put SHT_GROUP sections first.  */
3658
5.17k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
3659
4.97k
  {
3660
4.97k
    d = elf_section_data (sec);
3661
3662
4.97k
    if (d->this_hdr.sh_type == SHT_GROUP)
3663
577
      {
3664
577
        if (sec->flags & SEC_LINKER_CREATED)
3665
0
    {
3666
      /* Remove the linker created SHT_GROUP sections.  */
3667
0
      bfd_section_list_remove (abfd, sec);
3668
0
      abfd->section_count--;
3669
0
    }
3670
577
        else
3671
577
    d->this_idx = section_number++;
3672
577
      }
3673
3674
    /* Count relocations.  */
3675
4.97k
    reloc_count += sec->reloc_count;
3676
4.97k
  }
3677
3678
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
3679
198
      if (reloc_count == 0)
3680
170
  abfd->flags &= ~HAS_RELOC;
3681
28
      else
3682
28
  abfd->flags |= HAS_RELOC;
3683
198
    }
3684
3685
5.17k
  for (sec = abfd->sections; sec; sec = sec->next)
3686
4.97k
    {
3687
4.97k
      d = elf_section_data (sec);
3688
3689
4.97k
      if (d->this_hdr.sh_type != SHT_GROUP)
3690
4.39k
  d->this_idx = section_number++;
3691
4.97k
      if (d->this_hdr.sh_name != (unsigned int) -1)
3692
4.97k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3693
4.97k
      if (d->rel.hdr)
3694
0
  {
3695
0
    d->rel.idx = section_number++;
3696
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
3697
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3698
0
  }
3699
4.97k
      else
3700
4.97k
  d->rel.idx = 0;
3701
3702
4.97k
      if (d->rela.hdr)
3703
1.47k
  {
3704
1.47k
    d->rela.idx = section_number++;
3705
1.47k
    if (d->rela.hdr->sh_name != (unsigned int) -1)
3706
1.47k
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3707
1.47k
  }
3708
3.50k
      else
3709
3.50k
  d->rela.idx = 0;
3710
4.97k
    }
3711
3712
198
  need_symtab = (bfd_get_symcount (abfd) > 0
3713
198
     || (link_info == NULL
3714
112
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3715
112
       == HAS_RELOC)));
3716
198
  if (need_symtab)
3717
86
    {
3718
86
      elf_onesymtab (abfd) = section_number++;
3719
86
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3720
86
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3721
0
  {
3722
0
    elf_section_list *entry;
3723
3724
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3725
3726
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
3727
0
    entry->ndx = section_number++;
3728
0
    elf_symtab_shndx_list (abfd) = entry;
3729
0
    entry->hdr.sh_name
3730
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3731
0
              ".symtab_shndx", false);
3732
0
    if (entry->hdr.sh_name == (unsigned int) -1)
3733
0
      return false;
3734
0
  }
3735
86
      elf_strtab_sec (abfd) = section_number++;
3736
86
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3737
86
    }
3738
3739
198
  elf_shstrtab_sec (abfd) = section_number++;
3740
198
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3741
198
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3742
3743
198
  if (section_number >= SHN_LORESERVE)
3744
0
    {
3745
      /* xgettext:c-format */
3746
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
3747
0
        abfd, section_number);
3748
0
      return false;
3749
0
    }
3750
3751
198
  elf_numsections (abfd) = section_number;
3752
198
  elf_elfheader (abfd)->e_shnum = section_number;
3753
3754
  /* Set up the list of section header pointers, in agreement with the
3755
     indices.  */
3756
198
  amt = section_number * sizeof (Elf_Internal_Shdr *);
3757
198
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3758
198
  if (i_shdrp == NULL)
3759
0
    return false;
3760
3761
198
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3762
198
             sizeof (Elf_Internal_Shdr));
3763
198
  if (i_shdrp[0] == NULL)
3764
0
    {
3765
0
      bfd_release (abfd, i_shdrp);
3766
0
      return false;
3767
0
    }
3768
3769
198
  elf_elfsections (abfd) = i_shdrp;
3770
3771
198
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3772
198
  if (need_symtab)
3773
86
    {
3774
86
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3775
86
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3776
0
  {
3777
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
3778
0
    BFD_ASSERT (entry != NULL);
3779
0
    i_shdrp[entry->ndx] = & entry->hdr;
3780
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
3781
0
  }
3782
86
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3783
86
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3784
86
    }
3785
3786
5.17k
  for (sec = abfd->sections; sec; sec = sec->next)
3787
4.97k
    {
3788
4.97k
      asection *s;
3789
3790
4.97k
      d = elf_section_data (sec);
3791
3792
4.97k
      i_shdrp[d->this_idx] = &d->this_hdr;
3793
4.97k
      if (d->rel.idx != 0)
3794
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
3795
4.97k
      if (d->rela.idx != 0)
3796
1.47k
  i_shdrp[d->rela.idx] = d->rela.hdr;
3797
3798
      /* Fill in the sh_link and sh_info fields while we're at it.  */
3799
3800
      /* sh_link of a reloc section is the section index of the symbol
3801
   table.  sh_info is the section index of the section to which
3802
   the relocation entries apply.  */
3803
4.97k
      if (d->rel.idx != 0)
3804
0
  {
3805
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
3806
0
    d->rel.hdr->sh_info = d->this_idx;
3807
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3808
0
  }
3809
4.97k
      if (d->rela.idx != 0)
3810
1.47k
  {
3811
1.47k
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
3812
1.47k
    d->rela.hdr->sh_info = d->this_idx;
3813
1.47k
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3814
1.47k
  }
3815
3816
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
3817
4.97k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3818
1
  {
3819
1
    s = elf_linked_to_section (sec);
3820
    /* We can now have a NULL linked section pointer.
3821
       This happens when the sh_link field is 0, which is done
3822
       when a linked to section is discarded but the linking
3823
       section has been retained for some reason.  */
3824
1
    if (s)
3825
1
      {
3826
        /* Check discarded linkonce section.  */
3827
1
        if (discarded_section (s))
3828
0
    {
3829
0
      asection *kept;
3830
0
      _bfd_error_handler
3831
        /* xgettext:c-format */
3832
0
        (_("%pB: sh_link of section `%pA' points to"
3833
0
           " discarded section `%pA' of `%pB'"),
3834
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
3835
      /* Point to the kept section if it has the same
3836
         size as the discarded one.  */
3837
0
      kept = _bfd_elf_check_kept_section (s, link_info);
3838
0
      if (kept == NULL)
3839
0
        {
3840
0
          bfd_set_error (bfd_error_bad_value);
3841
0
          return false;
3842
0
        }
3843
0
      s = kept;
3844
0
    }
3845
        /* Handle objcopy. */
3846
1
        else if (s->output_section == NULL)
3847
0
    {
3848
0
      _bfd_error_handler
3849
        /* xgettext:c-format */
3850
0
        (_("%pB: sh_link of section `%pA' points to"
3851
0
           " removed section `%pA' of `%pB'"),
3852
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
3853
0
      bfd_set_error (bfd_error_bad_value);
3854
0
      return false;
3855
0
    }
3856
1
        s = s->output_section;
3857
1
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3858
1
      }
3859
1
  }
3860
3861
4.97k
      switch (d->this_hdr.sh_type)
3862
4.97k
  {
3863
18
  case SHT_REL:
3864
166
  case SHT_RELA:
3865
    /* sh_link is the section index of the symbol table.
3866
       sh_info is the section index of the section to which the
3867
       relocation entries apply.  */
3868
166
    if (d->this_hdr.sh_link == 0)
3869
166
      {
3870
        /* FIXME maybe: If this is a reloc section which we are
3871
     treating as a normal section then we likely should
3872
     not be assuming its sh_link is .dynsym or .symtab.  */
3873
166
        if ((sec->flags & SEC_ALLOC) != 0)
3874
166
    {
3875
166
      s = bfd_get_section_by_name (abfd, ".dynsym");
3876
166
      if (s != NULL)
3877
123
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3878
166
    }
3879
0
        else
3880
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
3881
166
      }
3882
3883
166
    s = elf_get_reloc_section (sec);
3884
166
    if (s != NULL)
3885
111
      {
3886
111
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3887
111
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
3888
111
      }
3889
166
    break;
3890
3891
64
  case SHT_STRTAB:
3892
    /* We assume that a section named .stab*str is a stabs
3893
       string section.  We look for a section with the same name
3894
       but without the trailing ``str'', and set its sh_link
3895
       field to point to this section.  */
3896
64
    if (startswith (sec->name, ".stab")
3897
64
        && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3898
0
      {
3899
0
        size_t len;
3900
0
        char *alc;
3901
3902
0
        len = strlen (sec->name);
3903
0
        alc = (char *) bfd_malloc (len - 2);
3904
0
        if (alc == NULL)
3905
0
    return false;
3906
0
        memcpy (alc, sec->name, len - 3);
3907
0
        alc[len - 3] = '\0';
3908
0
        s = bfd_get_section_by_name (abfd, alc);
3909
0
        free (alc);
3910
0
        if (s != NULL)
3911
0
    {
3912
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3913
3914
      /* This is a .stab section.  */
3915
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
3916
0
    }
3917
0
      }
3918
64
    break;
3919
3920
66
  case SHT_DYNAMIC:
3921
133
  case SHT_DYNSYM:
3922
179
  case SHT_GNU_verneed:
3923
179
  case SHT_GNU_verdef:
3924
    /* sh_link is the section header index of the string table
3925
       used for the dynamic entries, or the symbol table, or the
3926
       version strings.  */
3927
179
    s = bfd_get_section_by_name (abfd, ".dynstr");
3928
179
    if (s != NULL)
3929
89
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3930
179
    break;
3931
3932
0
  case SHT_GNU_LIBLIST:
3933
    /* sh_link is the section header index of the prelink library
3934
       list used for the dynamic entries, or the symbol table, or
3935
       the version strings.  */
3936
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3937
0
                ? ".dynstr" : ".gnu.libstr"));
3938
0
    if (s != NULL)
3939
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3940
0
    break;
3941
3942
47
  case SHT_HASH:
3943
72
  case SHT_GNU_HASH:
3944
103
  case SHT_GNU_versym:
3945
    /* sh_link is the section header index of the symbol table
3946
       this hash table or version table is for.  */
3947
103
    s = bfd_get_section_by_name (abfd, ".dynsym");
3948
103
    if (s != NULL)
3949
70
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3950
103
    break;
3951
3952
577
  case SHT_GROUP:
3953
577
    d->this_hdr.sh_link = elf_onesymtab (abfd);
3954
4.97k
  }
3955
4.97k
    }
3956
3957
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3958
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3959
     debug section name from .debug_* to .zdebug_* if needed.  */
3960
3961
198
  return true;
3962
198
}
3963
3964
static bool
3965
sym_is_global (bfd *abfd, asymbol *sym)
3966
78.6k
{
3967
  /* If the backend has a special mapping, use it.  */
3968
78.6k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3969
78.6k
  if (bed->elf_backend_sym_is_global)
3970
14.6k
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3971
3972
63.9k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3973
63.9k
    || bfd_is_und_section (bfd_asymbol_section (sym))
3974
63.9k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
3975
78.6k
}
3976
3977
/* Filter global symbols of ABFD to include in the import library.  All
3978
   SYMCOUNT symbols of ABFD can be examined from their pointers in
3979
   SYMS.  Pointers of symbols to keep should be stored contiguously at
3980
   the beginning of that array.
3981
3982
   Returns the number of symbols to keep.  */
3983
3984
unsigned int
3985
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3986
        asymbol **syms, long symcount)
3987
0
{
3988
0
  long src_count, dst_count = 0;
3989
3990
0
  for (src_count = 0; src_count < symcount; src_count++)
3991
0
    {
3992
0
      asymbol *sym = syms[src_count];
3993
0
      char *name = (char *) bfd_asymbol_name (sym);
3994
0
      struct bfd_link_hash_entry *h;
3995
3996
0
      if (!sym_is_global (abfd, sym))
3997
0
  continue;
3998
3999
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4000
0
      if (h == NULL)
4001
0
  continue;
4002
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4003
0
  continue;
4004
0
      if (h->linker_def || h->ldscript_def)
4005
0
  continue;
4006
4007
0
      syms[dst_count++] = sym;
4008
0
    }
4009
4010
0
  syms[dst_count] = NULL;
4011
4012
0
  return dst_count;
4013
0
}
4014
4015
/* Don't output section symbols for sections that are not going to be
4016
   output, that are duplicates or there is no BFD section.  */
4017
4018
static bool
4019
ignore_section_sym (bfd *abfd, asymbol *sym)
4020
46.5k
{
4021
46.5k
  elf_symbol_type *type_ptr;
4022
4023
46.5k
  if (sym == NULL)
4024
0
    return false;
4025
4026
46.5k
  if ((sym->flags & BSF_SECTION_SYM) == 0)
4027
28.2k
    return false;
4028
4029
  /* Ignore the section symbol if it isn't used.  */
4030
18.2k
  if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4031
9.47k
    return true;
4032
4033
8.78k
  if (sym->section == NULL)
4034
0
    return true;
4035
4036
8.78k
  type_ptr = elf_symbol_from (sym);
4037
8.78k
  return ((type_ptr != NULL
4038
8.78k
     && type_ptr->internal_elf_sym.st_shndx != 0
4039
8.78k
     && bfd_is_abs_section (sym->section))
4040
8.78k
    || !(sym->section->owner == abfd
4041
6.58k
         || (sym->section->output_section != NULL
4042
5.97k
       && sym->section->output_section->owner == abfd
4043
5.97k
       && sym->section->output_offset == 0)
4044
6.58k
         || bfd_is_abs_section (sym->section)));
4045
8.78k
}
4046
4047
/* Map symbol from it's internal number to the external number, moving
4048
   all local symbols to be at the head of the list.  */
4049
4050
static bool
4051
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4052
86
{
4053
86
  unsigned int symcount = bfd_get_symcount (abfd);
4054
86
  asymbol **syms = bfd_get_outsymbols (abfd);
4055
86
  asymbol **sect_syms;
4056
86
  unsigned int num_locals = 0;
4057
86
  unsigned int num_globals = 0;
4058
86
  unsigned int num_locals2 = 0;
4059
86
  unsigned int num_globals2 = 0;
4060
86
  unsigned int max_index = 0;
4061
86
  unsigned int idx;
4062
86
  asection *asect;
4063
86
  asymbol **new_syms;
4064
86
  size_t amt;
4065
4066
#ifdef DEBUG
4067
  fprintf (stderr, "elf_map_symbols\n");
4068
  fflush (stderr);
4069
#endif
4070
4071
4.83k
  for (asect = abfd->sections; asect; asect = asect->next)
4072
4.74k
    {
4073
4.74k
      if (max_index < asect->index)
4074
4.66k
  max_index = asect->index;
4075
4.74k
    }
4076
4077
86
  max_index++;
4078
86
  amt = max_index * sizeof (asymbol *);
4079
86
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4080
86
  if (sect_syms == NULL)
4081
0
    return false;
4082
86
  elf_section_syms (abfd) = sect_syms;
4083
86
  elf_num_section_syms (abfd) = max_index;
4084
4085
  /* Init sect_syms entries for any section symbols we have already
4086
     decided to output.  */
4087
39.1k
  for (idx = 0; idx < symcount; idx++)
4088
39.0k
    {
4089
39.0k
      asymbol *sym = syms[idx];
4090
4091
39.0k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4092
39.0k
    && sym->value == 0
4093
39.0k
    && !ignore_section_sym (abfd, sym)
4094
39.0k
    && !bfd_is_abs_section (sym->section))
4095
1.92k
  {
4096
1.92k
    asection *sec = sym->section;
4097
4098
1.92k
    if (sec->owner != abfd)
4099
1.92k
      sec = sec->output_section;
4100
4101
1.92k
    sect_syms[sec->index] = syms[idx];
4102
1.92k
  }
4103
39.0k
    }
4104
4105
  /* Classify all of the symbols.  */
4106
39.1k
  for (idx = 0; idx < symcount; idx++)
4107
39.0k
    {
4108
39.0k
      if (sym_is_global (abfd, syms[idx]))
4109
21.6k
  num_globals++;
4110
17.4k
      else if (!ignore_section_sym (abfd, syms[idx]))
4111
16.0k
  num_locals++;
4112
39.0k
    }
4113
4114
  /* We will be adding a section symbol for each normal BFD section.  Most
4115
     sections will already have a section symbol in outsymbols, but
4116
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4117
     at least in that case.  */
4118
4.83k
  for (asect = abfd->sections; asect; asect = asect->next)
4119
4.74k
    {
4120
4.74k
      asymbol *sym = asect->symbol;
4121
      /* Don't include ignored section symbols.  */
4122
4.74k
      if (!ignore_section_sym (abfd, sym)
4123
4.74k
    && sect_syms[asect->index] == NULL)
4124
306
  {
4125
306
    if (!sym_is_global (abfd, asect->symbol))
4126
306
      num_locals++;
4127
0
    else
4128
0
      num_globals++;
4129
306
  }
4130
4.74k
    }
4131
4132
  /* Now sort the symbols so the local symbols are first.  */
4133
86
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4134
86
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4135
86
  if (new_syms == NULL)
4136
0
    return false;
4137
4138
39.1k
  for (idx = 0; idx < symcount; idx++)
4139
39.0k
    {
4140
39.0k
      asymbol *sym = syms[idx];
4141
39.0k
      unsigned int i;
4142
4143
39.0k
      if (sym_is_global (abfd, sym))
4144
21.6k
  i = num_locals + num_globals2++;
4145
      /* Don't include ignored section symbols.  */
4146
17.4k
      else if (!ignore_section_sym (abfd, sym))
4147
16.0k
  i = num_locals2++;
4148
1.35k
      else
4149
1.35k
  continue;
4150
37.6k
      new_syms[i] = sym;
4151
37.6k
      sym->udata.i = i + 1;
4152
37.6k
    }
4153
4.83k
  for (asect = abfd->sections; asect; asect = asect->next)
4154
4.74k
    {
4155
4.74k
      asymbol *sym = asect->symbol;
4156
4.74k
      if (!ignore_section_sym (abfd, sym)
4157
4.74k
    && sect_syms[asect->index] == NULL)
4158
306
  {
4159
306
    unsigned int i;
4160
4161
306
    sect_syms[asect->index] = sym;
4162
306
    if (!sym_is_global (abfd, sym))
4163
306
      i = num_locals2++;
4164
0
    else
4165
0
      i = num_locals + num_globals2++;
4166
306
    new_syms[i] = sym;
4167
306
    sym->udata.i = i + 1;
4168
306
  }
4169
4.74k
    }
4170
4171
86
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4172
4173
86
  *pnum_locals = num_locals;
4174
86
  return true;
4175
86
}
4176
4177
/* Align to the maximum file alignment that could be required for any
4178
   ELF data structure.  */
4179
4180
static inline file_ptr
4181
align_file_position (file_ptr off, int align)
4182
188
{
4183
188
  return (off + align - 1) & ~(align - 1);
4184
188
}
4185
4186
/* Assign a file position to a section, optionally aligning to the
4187
   required section alignment.  */
4188
4189
file_ptr
4190
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4191
             file_ptr offset,
4192
             bool align)
4193
5.60k
{
4194
5.60k
  if (align && i_shdrp->sh_addralign > 1)
4195
4.27k
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4196
5.60k
  i_shdrp->sh_offset = offset;
4197
5.60k
  if (i_shdrp->bfd_section != NULL)
4198
3.77k
    i_shdrp->bfd_section->filepos = offset;
4199
5.60k
  if (i_shdrp->sh_type != SHT_NOBITS)
4200
5.55k
    offset += i_shdrp->sh_size;
4201
5.60k
  return offset;
4202
5.60k
}
4203
4204
/* Compute the file positions we are going to put the sections at, and
4205
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4206
   is not NULL, this is being called by the ELF backend linker.  */
4207
4208
bool
4209
_bfd_elf_compute_section_file_positions (bfd *abfd,
4210
           struct bfd_link_info *link_info)
4211
198
{
4212
198
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4213
198
  struct fake_section_arg fsargs;
4214
198
  bool failed;
4215
198
  struct elf_strtab_hash *strtab = NULL;
4216
198
  Elf_Internal_Shdr *shstrtab_hdr;
4217
198
  bool need_symtab;
4218
4219
198
  if (abfd->output_has_begun)
4220
0
    return true;
4221
4222
  /* Do any elf backend specific processing first.  */
4223
198
  if (bed->elf_backend_begin_write_processing)
4224
4
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4225
4226
198
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4227
0
    return false;
4228
4229
198
  fsargs.failed = false;
4230
198
  fsargs.link_info = link_info;
4231
198
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4232
198
  if (fsargs.failed)
4233
0
    return false;
4234
4235
198
  if (!assign_section_numbers (abfd, link_info))
4236
0
    return false;
4237
4238
  /* The backend linker builds symbol table information itself.  */
4239
198
  need_symtab = (link_info == NULL
4240
198
     && (bfd_get_symcount (abfd) > 0
4241
198
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4242
112
       == HAS_RELOC)));
4243
198
  if (need_symtab)
4244
86
    {
4245
      /* Non-zero if doing a relocatable link.  */
4246
86
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4247
4248
86
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4249
0
  return false;
4250
86
    }
4251
4252
198
  failed = false;
4253
198
  if (link_info == NULL)
4254
198
    {
4255
198
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4256
198
      if (failed)
4257
1
  goto err_free_strtab;
4258
198
    }
4259
4260
197
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4261
  /* sh_name was set in init_file_header.  */
4262
197
  shstrtab_hdr->sh_type = SHT_STRTAB;
4263
197
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4264
197
  shstrtab_hdr->sh_addr = 0;
4265
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4266
197
  shstrtab_hdr->sh_entsize = 0;
4267
197
  shstrtab_hdr->sh_link = 0;
4268
197
  shstrtab_hdr->sh_info = 0;
4269
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4270
197
  shstrtab_hdr->sh_addralign = 1;
4271
4272
197
  if (!assign_file_positions_except_relocs (abfd, link_info))
4273
8
    goto err_free_strtab;
4274
4275
189
  if (strtab != NULL)
4276
78
    {
4277
78
      file_ptr off;
4278
78
      Elf_Internal_Shdr *hdr;
4279
4280
78
      off = elf_next_file_pos (abfd);
4281
4282
78
      hdr = & elf_symtab_hdr (abfd);
4283
78
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4284
4285
78
      if (elf_symtab_shndx_list (abfd) != NULL)
4286
0
  {
4287
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4288
0
    if (hdr->sh_size != 0)
4289
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4290
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4291
0
  }
4292
4293
78
      hdr = &elf_tdata (abfd)->strtab_hdr;
4294
78
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4295
4296
78
      elf_next_file_pos (abfd) = off;
4297
4298
      /* Now that we know where the .strtab section goes, write it
4299
   out.  */
4300
78
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4301
78
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4302
0
  goto err_free_strtab;
4303
78
      _bfd_elf_strtab_free (strtab);
4304
78
    }
4305
4306
189
  abfd->output_has_begun = true;
4307
189
  return true;
4308
4309
9
 err_free_strtab:
4310
9
  if (strtab != NULL)
4311
8
    _bfd_elf_strtab_free (strtab);
4312
9
  return false;
4313
189
}
4314
4315
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4316
   function effectively returns whether --eh-frame-hdr is given on the
4317
   command line.  After size_dynamic_sections the result reflects
4318
   whether .eh_frame_hdr will actually be output (sizing isn't done
4319
   until ldemul_after_allocation).  */
4320
4321
static asection *
4322
elf_eh_frame_hdr (const struct bfd_link_info *info)
4323
0
{
4324
0
  if (info != NULL && is_elf_hash_table (info->hash))
4325
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4326
0
  return NULL;
4327
0
}
4328
4329
/* Make an initial estimate of the size of the program header.  If we
4330
   get the number wrong here, we'll redo section placement.  */
4331
4332
static bfd_size_type
4333
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4334
0
{
4335
0
  size_t segs;
4336
0
  asection *s;
4337
0
  const struct elf_backend_data *bed;
4338
4339
  /* Assume we will need exactly two PT_LOAD segments: one for text
4340
     and one for data.  */
4341
0
  segs = 2;
4342
4343
0
  s = bfd_get_section_by_name (abfd, ".interp");
4344
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4345
0
    {
4346
      /* If we have a loadable interpreter section, we need a
4347
   PT_INTERP segment.  In this case, assume we also need a
4348
   PT_PHDR segment, although that may not be true for all
4349
   targets.  */
4350
0
      segs += 2;
4351
0
    }
4352
4353
0
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4354
0
    {
4355
      /* We need a PT_DYNAMIC segment.  */
4356
0
      ++segs;
4357
0
    }
4358
4359
0
  if (info != NULL && info->relro)
4360
0
    {
4361
      /* We need a PT_GNU_RELRO segment.  */
4362
0
      ++segs;
4363
0
    }
4364
4365
0
  if (elf_eh_frame_hdr (info))
4366
0
    {
4367
      /* We need a PT_GNU_EH_FRAME segment.  */
4368
0
      ++segs;
4369
0
    }
4370
4371
0
  if (elf_stack_flags (abfd))
4372
0
    {
4373
      /* We need a PT_GNU_STACK segment.  */
4374
0
      ++segs;
4375
0
    }
4376
4377
0
  if (elf_sframe (abfd))
4378
0
    {
4379
      /* We need a PT_GNU_SFRAME segment.  */
4380
0
      ++segs;
4381
0
    }
4382
4383
0
  s = bfd_get_section_by_name (abfd,
4384
0
             NOTE_GNU_PROPERTY_SECTION_NAME);
4385
0
  if (s != NULL && s->size != 0)
4386
0
    {
4387
      /* We need a PT_GNU_PROPERTY segment.  */
4388
0
      ++segs;
4389
0
    }
4390
4391
0
  for (s = abfd->sections; s != NULL; s = s->next)
4392
0
    {
4393
0
      if ((s->flags & SEC_LOAD) != 0
4394
0
    && elf_section_type (s) == SHT_NOTE)
4395
0
  {
4396
0
    unsigned int alignment_power;
4397
    /* We need a PT_NOTE segment.  */
4398
0
    ++segs;
4399
    /* Try to create just one PT_NOTE segment for all adjacent
4400
       loadable SHT_NOTE sections.  gABI requires that within a
4401
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4402
       each note should have the same alignment.  So we check
4403
       whether the sections are correctly aligned.  */
4404
0
    alignment_power = s->alignment_power;
4405
0
    while (s->next != NULL
4406
0
     && s->next->alignment_power == alignment_power
4407
0
     && (s->next->flags & SEC_LOAD) != 0
4408
0
     && elf_section_type (s->next) == SHT_NOTE)
4409
0
      s = s->next;
4410
0
  }
4411
0
    }
4412
4413
0
  for (s = abfd->sections; s != NULL; s = s->next)
4414
0
    {
4415
0
      if (s->flags & SEC_THREAD_LOCAL)
4416
0
  {
4417
    /* We need a PT_TLS segment.  */
4418
0
    ++segs;
4419
0
    break;
4420
0
  }
4421
0
    }
4422
4423
0
  bed = get_elf_backend_data (abfd);
4424
4425
0
  if ((abfd->flags & D_PAGED) != 0
4426
0
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4427
0
    {
4428
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4429
0
      bfd_vma commonpagesize;
4430
0
      unsigned int page_align_power;
4431
4432
0
      if (info != NULL)
4433
0
  commonpagesize = info->commonpagesize;
4434
0
      else
4435
0
  commonpagesize = bed->commonpagesize;
4436
0
      page_align_power = bfd_log2 (commonpagesize);
4437
0
      for (s = abfd->sections; s != NULL; s = s->next)
4438
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4439
0
    {
4440
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4441
0
        {
4442
0
    _bfd_error_handler
4443
      /* xgettext:c-format */
4444
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4445
0
         "sh_info field: %d"),
4446
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4447
0
    continue;
4448
0
        }
4449
      /* Align mbind section to page size.  */
4450
0
      if (s->alignment_power < page_align_power)
4451
0
        s->alignment_power = page_align_power;
4452
0
      segs ++;
4453
0
    }
4454
0
    }
4455
4456
  /* Let the backend count up any program headers it might need.  */
4457
0
  if (bed->elf_backend_additional_program_headers)
4458
0
    {
4459
0
      int a;
4460
4461
0
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4462
0
      if (a == -1)
4463
0
  abort ();
4464
0
      segs += a;
4465
0
    }
4466
4467
0
  return segs * bed->s->sizeof_phdr;
4468
0
}
4469
4470
/* Find the segment that contains the output_section of section.  */
4471
4472
Elf_Internal_Phdr *
4473
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4474
0
{
4475
0
  struct elf_segment_map *m;
4476
0
  Elf_Internal_Phdr *p;
4477
4478
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4479
0
       m != NULL;
4480
0
       m = m->next, p++)
4481
0
    {
4482
0
      int i;
4483
4484
0
      for (i = m->count - 1; i >= 0; i--)
4485
0
  if (m->sections[i] == section)
4486
0
    return p;
4487
0
    }
4488
4489
0
  return NULL;
4490
0
}
4491
4492
/* Create a mapping from a set of sections to a program segment.  */
4493
4494
static struct elf_segment_map *
4495
make_mapping (bfd *abfd,
4496
        asection **sections,
4497
        unsigned int from,
4498
        unsigned int to,
4499
        bool phdr)
4500
0
{
4501
0
  struct elf_segment_map *m;
4502
0
  unsigned int i;
4503
0
  asection **hdrpp;
4504
0
  size_t amt;
4505
4506
0
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4507
0
  amt += (to - from) * sizeof (asection *);
4508
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4509
0
  if (m == NULL)
4510
0
    return NULL;
4511
0
  m->next = NULL;
4512
0
  m->p_type = PT_LOAD;
4513
0
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4514
0
    m->sections[i - from] = *hdrpp;
4515
0
  m->count = to - from;
4516
4517
0
  if (from == 0 && phdr)
4518
0
    {
4519
      /* Include the headers in the first PT_LOAD segment.  */
4520
0
      m->includes_filehdr = 1;
4521
0
      m->includes_phdrs = 1;
4522
0
    }
4523
4524
0
  return m;
4525
0
}
4526
4527
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
4528
   on failure.  */
4529
4530
struct elf_segment_map *
4531
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4532
0
{
4533
0
  struct elf_segment_map *m;
4534
4535
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
4536
0
               sizeof (struct elf_segment_map));
4537
0
  if (m == NULL)
4538
0
    return NULL;
4539
0
  m->next = NULL;
4540
0
  m->p_type = PT_DYNAMIC;
4541
0
  m->count = 1;
4542
0
  m->sections[0] = dynsec;
4543
4544
0
  return m;
4545
0
}
4546
4547
/* Possibly add or remove segments from the segment map.  */
4548
4549
static bool
4550
elf_modify_segment_map (bfd *abfd,
4551
      struct bfd_link_info *info,
4552
      bool remove_empty_load)
4553
93
{
4554
93
  struct elf_segment_map **m;
4555
93
  const struct elf_backend_data *bed;
4556
4557
  /* The placement algorithm assumes that non allocated sections are
4558
     not in PT_LOAD segments.  We ensure this here by removing such
4559
     sections from the segment map.  We also remove excluded
4560
     sections.  Finally, any PT_LOAD segment without sections is
4561
     removed.  */
4562
93
  m = &elf_seg_map (abfd);
4563
471
  while (*m)
4564
378
    {
4565
378
      unsigned int i, new_count;
4566
4567
1.88k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
4568
1.50k
  {
4569
1.50k
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4570
1.50k
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4571
1.50k
      || (*m)->p_type != PT_LOAD))
4572
1.50k
      {
4573
1.50k
        (*m)->sections[new_count] = (*m)->sections[i];
4574
1.50k
        new_count++;
4575
1.50k
      }
4576
1.50k
  }
4577
378
      (*m)->count = new_count;
4578
4579
378
      if (remove_empty_load
4580
378
    && (*m)->p_type == PT_LOAD
4581
378
    && (*m)->count == 0
4582
378
    && !(*m)->includes_phdrs)
4583
0
  *m = (*m)->next;
4584
378
      else
4585
378
  m = &(*m)->next;
4586
378
    }
4587
4588
93
  bed = get_elf_backend_data (abfd);
4589
93
  if (bed->elf_backend_modify_segment_map != NULL)
4590
8
    {
4591
8
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4592
0
  return false;
4593
8
    }
4594
4595
93
  return true;
4596
93
}
4597
4598
#define IS_TBSS(s) \
4599
70
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4600
4601
/* Set up a mapping from BFD sections to program segments.  Update
4602
   NEED_LAYOUT if the section layout is changed.  */
4603
4604
bool
4605
_bfd_elf_map_sections_to_segments (bfd *abfd,
4606
           struct bfd_link_info *info,
4607
           bool *need_layout)
4608
93
{
4609
93
  unsigned int count;
4610
93
  struct elf_segment_map *m;
4611
93
  asection **sections = NULL;
4612
93
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4613
93
  bool no_user_phdrs;
4614
4615
93
  no_user_phdrs = elf_seg_map (abfd) == NULL;
4616
4617
93
  if (info != NULL)
4618
0
    {
4619
0
      info->user_phdrs = !no_user_phdrs;
4620
4621
      /* Size the relative relocations if DT_RELR is enabled.  */
4622
0
      if (info->enable_dt_relr
4623
0
    && need_layout != NULL
4624
0
    && bed->size_relative_relocs
4625
0
    && !bed->size_relative_relocs (info, need_layout))
4626
0
  info->callbacks->einfo
4627
0
    (_("%F%P: failed to size relative relocations\n"));
4628
0
    }
4629
4630
93
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4631
0
    {
4632
0
      asection *s;
4633
0
      unsigned int i;
4634
0
      struct elf_segment_map *mfirst;
4635
0
      struct elf_segment_map **pm;
4636
0
      asection *last_hdr;
4637
0
      bfd_vma last_size;
4638
0
      unsigned int hdr_index;
4639
0
      bfd_vma maxpagesize;
4640
0
      asection **hdrpp;
4641
0
      bool phdr_in_segment;
4642
0
      bool writable;
4643
0
      bool executable;
4644
0
      unsigned int tls_count = 0;
4645
0
      asection *first_tls = NULL;
4646
0
      asection *first_mbind = NULL;
4647
0
      asection *dynsec, *eh_frame_hdr;
4648
0
      asection *sframe;
4649
0
      size_t amt;
4650
0
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
4651
0
      bfd_size_type phdr_size;  /* Octets/bytes.  */
4652
0
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4653
4654
      /* Select the allocated sections, and sort them.  */
4655
4656
0
      amt = bfd_count_sections (abfd) * sizeof (asection *);
4657
0
      sections = (asection **) bfd_malloc (amt);
4658
0
      if (sections == NULL)
4659
0
  goto error_return;
4660
4661
      /* Calculate top address, avoiding undefined behaviour of shift
4662
   left operator when shift count is equal to size of type
4663
   being shifted.  */
4664
0
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4665
0
      addr_mask = (addr_mask << 1) + 1;
4666
4667
0
      i = 0;
4668
0
      for (s = abfd->sections; s != NULL; s = s->next)
4669
0
  {
4670
0
    if ((s->flags & SEC_ALLOC) != 0)
4671
0
      {
4672
        /* target_index is unused until bfd_elf_final_link
4673
     starts output of section symbols.  Use it to make
4674
     qsort stable.  */
4675
0
        s->target_index = i;
4676
0
        sections[i] = s;
4677
0
        ++i;
4678
        /* A wrapping section potentially clashes with header.  */
4679
0
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4680
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
4681
0
      }
4682
0
  }
4683
0
      BFD_ASSERT (i <= bfd_count_sections (abfd));
4684
0
      count = i;
4685
4686
0
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4687
4688
0
      phdr_size = elf_program_header_size (abfd);
4689
0
      if (phdr_size == (bfd_size_type) -1)
4690
0
  phdr_size = get_program_header_size (abfd, info);
4691
0
      phdr_size += bed->s->sizeof_ehdr;
4692
      /* phdr_size is compared to LMA values which are in bytes.  */
4693
0
      phdr_size /= opb;
4694
0
      if (info != NULL)
4695
0
  maxpagesize = info->maxpagesize;
4696
0
      else
4697
0
  maxpagesize = bed->maxpagesize;
4698
0
      if (maxpagesize == 0)
4699
0
  maxpagesize = 1;
4700
0
      phdr_in_segment = info != NULL && info->load_phdrs;
4701
0
      if (count != 0
4702
0
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4703
0
        >= (phdr_size & (maxpagesize - 1))))
4704
  /* For compatibility with old scripts that may not be using
4705
     SIZEOF_HEADERS, add headers when it looks like space has
4706
     been left for them.  */
4707
0
  phdr_in_segment = true;
4708
4709
      /* Build the mapping.  */
4710
0
      mfirst = NULL;
4711
0
      pm = &mfirst;
4712
4713
      /* If we have a .interp section, then create a PT_PHDR segment for
4714
   the program headers and a PT_INTERP segment for the .interp
4715
   section.  */
4716
0
      s = bfd_get_section_by_name (abfd, ".interp");
4717
0
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4718
0
  {
4719
0
    amt = sizeof (struct elf_segment_map);
4720
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4721
0
    if (m == NULL)
4722
0
      goto error_return;
4723
0
    m->next = NULL;
4724
0
    m->p_type = PT_PHDR;
4725
0
    m->p_flags = PF_R;
4726
0
    m->p_flags_valid = 1;
4727
0
    m->includes_phdrs = 1;
4728
0
    phdr_in_segment = true;
4729
0
    *pm = m;
4730
0
    pm = &m->next;
4731
4732
0
    amt = sizeof (struct elf_segment_map);
4733
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4734
0
    if (m == NULL)
4735
0
      goto error_return;
4736
0
    m->next = NULL;
4737
0
    m->p_type = PT_INTERP;
4738
0
    m->count = 1;
4739
0
    m->sections[0] = s;
4740
4741
0
    *pm = m;
4742
0
    pm = &m->next;
4743
0
  }
4744
4745
      /* Look through the sections.  We put sections in the same program
4746
   segment when the start of the second section can be placed within
4747
   a few bytes of the end of the first section.  */
4748
0
      last_hdr = NULL;
4749
0
      last_size = 0;
4750
0
      hdr_index = 0;
4751
0
      writable = false;
4752
0
      executable = false;
4753
0
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4754
0
      if (dynsec != NULL
4755
0
    && (dynsec->flags & SEC_LOAD) == 0)
4756
0
  dynsec = NULL;
4757
4758
0
      if ((abfd->flags & D_PAGED) == 0)
4759
0
  phdr_in_segment = false;
4760
4761
      /* Deal with -Ttext or something similar such that the first section
4762
   is not adjacent to the program headers.  This is an
4763
   approximation, since at this point we don't know exactly how many
4764
   program headers we will need.  */
4765
0
      if (phdr_in_segment && count > 0)
4766
0
  {
4767
0
    bfd_vma phdr_lma;  /* Bytes.  */
4768
0
    bool separate_phdr = false;
4769
4770
0
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4771
0
    if (info != NULL
4772
0
        && info->separate_code
4773
0
        && (sections[0]->flags & SEC_CODE) != 0)
4774
0
      {
4775
        /* If data sections should be separate from code and
4776
     thus not executable, and the first section is
4777
     executable then put the file and program headers in
4778
     their own PT_LOAD.  */
4779
0
        separate_phdr = true;
4780
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4781
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
4782
0
    {
4783
      /* The file and program headers are currently on the
4784
         same page as the first section.  Put them on the
4785
         previous page if we can.  */
4786
0
      if (phdr_lma >= maxpagesize)
4787
0
        phdr_lma -= maxpagesize;
4788
0
      else
4789
0
        separate_phdr = false;
4790
0
    }
4791
0
      }
4792
0
    if ((sections[0]->lma & addr_mask) < phdr_lma
4793
0
        || (sections[0]->lma & addr_mask) < phdr_size)
4794
      /* If file and program headers would be placed at the end
4795
         of memory then it's probably better to omit them.  */
4796
0
      phdr_in_segment = false;
4797
0
    else if (phdr_lma < wrap_to)
4798
      /* If a section wraps around to where we'll be placing
4799
         file and program headers, then the headers will be
4800
         overwritten.  */
4801
0
      phdr_in_segment = false;
4802
0
    else if (separate_phdr)
4803
0
      {
4804
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4805
0
        if (m == NULL)
4806
0
    goto error_return;
4807
0
        m->p_paddr = phdr_lma * opb;
4808
0
        m->p_vaddr_offset
4809
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4810
0
        m->p_paddr_valid = 1;
4811
0
        *pm = m;
4812
0
        pm = &m->next;
4813
0
        phdr_in_segment = false;
4814
0
      }
4815
0
  }
4816
4817
0
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4818
0
  {
4819
0
    asection *hdr;
4820
0
    bool new_segment;
4821
4822
0
    hdr = *hdrpp;
4823
4824
    /* See if this section and the last one will fit in the same
4825
       segment.  */
4826
4827
0
    if (last_hdr == NULL)
4828
0
      {
4829
        /* If we don't have a segment yet, then we don't need a new
4830
     one (we build the last one after this loop).  */
4831
0
        new_segment = false;
4832
0
      }
4833
0
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4834
0
      {
4835
        /* If this section has a different relation between the
4836
     virtual address and the load address, then we need a new
4837
     segment.  */
4838
0
        new_segment = true;
4839
0
      }
4840
0
    else if (hdr->lma < last_hdr->lma + last_size
4841
0
       || last_hdr->lma + last_size < last_hdr->lma)
4842
0
      {
4843
        /* If this section has a load address that makes it overlap
4844
     the previous section, then we need a new segment.  */
4845
0
        new_segment = true;
4846
0
      }
4847
0
    else if ((abfd->flags & D_PAGED) != 0
4848
0
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4849
0
           == (hdr->lma & -maxpagesize)))
4850
0
      {
4851
        /* If we are demand paged then we can't map two disk
4852
     pages onto the same memory page.  */
4853
0
        new_segment = false;
4854
0
      }
4855
    /* In the next test we have to be careful when last_hdr->lma is close
4856
       to the end of the address space.  If the aligned address wraps
4857
       around to the start of the address space, then there are no more
4858
       pages left in memory and it is OK to assume that the current
4859
       section can be included in the current segment.  */
4860
0
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4861
0
        + maxpagesize > last_hdr->lma)
4862
0
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4863
0
           + maxpagesize <= hdr->lma))
4864
0
      {
4865
        /* If putting this section in this segment would force us to
4866
     skip a page in the segment, then we need a new segment.  */
4867
0
        new_segment = true;
4868
0
      }
4869
0
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4870
0
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4871
0
      {
4872
        /* We don't want to put a loaded section after a
4873
     nonloaded (ie. bss style) section in the same segment
4874
     as that will force the non-loaded section to be loaded.
4875
     Consider .tbss sections as loaded for this purpose.  */
4876
0
        new_segment = true;
4877
0
      }
4878
0
    else if ((abfd->flags & D_PAGED) == 0)
4879
0
      {
4880
        /* If the file is not demand paged, which means that we
4881
     don't require the sections to be correctly aligned in the
4882
     file, then there is no other reason for a new segment.  */
4883
0
        new_segment = false;
4884
0
      }
4885
0
    else if (info != NULL
4886
0
       && info->separate_code
4887
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
4888
0
      {
4889
0
        new_segment = true;
4890
0
      }
4891
0
    else if (! writable
4892
0
       && (hdr->flags & SEC_READONLY) == 0)
4893
0
      {
4894
        /* We don't want to put a writable section in a read only
4895
     segment.  */
4896
0
        new_segment = true;
4897
0
      }
4898
0
    else
4899
0
      {
4900
        /* Otherwise, we can use the same segment.  */
4901
0
        new_segment = false;
4902
0
      }
4903
4904
    /* Allow interested parties a chance to override our decision.  */
4905
0
    if (last_hdr != NULL
4906
0
        && info != NULL
4907
0
        && info->callbacks->override_segment_assignment != NULL)
4908
0
      new_segment
4909
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
4910
0
                    last_hdr,
4911
0
                    new_segment);
4912
4913
0
    if (! new_segment)
4914
0
      {
4915
0
        if ((hdr->flags & SEC_READONLY) == 0)
4916
0
    writable = true;
4917
0
        if ((hdr->flags & SEC_CODE) != 0)
4918
0
    executable = true;
4919
0
        last_hdr = hdr;
4920
        /* .tbss sections effectively have zero size.  */
4921
0
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4922
0
        continue;
4923
0
      }
4924
4925
    /* We need a new program segment.  We must create a new program
4926
       header holding all the sections from hdr_index until hdr.  */
4927
4928
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4929
0
    if (m == NULL)
4930
0
      goto error_return;
4931
4932
0
    *pm = m;
4933
0
    pm = &m->next;
4934
4935
0
    if ((hdr->flags & SEC_READONLY) == 0)
4936
0
      writable = true;
4937
0
    else
4938
0
      writable = false;
4939
4940
0
    if ((hdr->flags & SEC_CODE) == 0)
4941
0
      executable = false;
4942
0
    else
4943
0
      executable = true;
4944
4945
0
    last_hdr = hdr;
4946
    /* .tbss sections effectively have zero size.  */
4947
0
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4948
0
    hdr_index = i;
4949
0
    phdr_in_segment = false;
4950
0
  }
4951
4952
      /* Create a final PT_LOAD program segment, but not if it's just
4953
   for .tbss.  */
4954
0
      if (last_hdr != NULL
4955
0
    && (i - hdr_index != 1
4956
0
        || !IS_TBSS (last_hdr)))
4957
0
  {
4958
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4959
0
    if (m == NULL)
4960
0
      goto error_return;
4961
4962
0
    *pm = m;
4963
0
    pm = &m->next;
4964
0
  }
4965
4966
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
4967
0
      if (dynsec != NULL)
4968
0
  {
4969
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4970
0
    if (m == NULL)
4971
0
      goto error_return;
4972
0
    *pm = m;
4973
0
    pm = &m->next;
4974
0
  }
4975
4976
      /* For each batch of consecutive loadable SHT_NOTE  sections,
4977
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
4978
   because if we link together nonloadable .note sections and
4979
   loadable .note sections, we will generate two .note sections
4980
   in the output file.  */
4981
0
      for (s = abfd->sections; s != NULL; s = s->next)
4982
0
  {
4983
0
    if ((s->flags & SEC_LOAD) != 0
4984
0
        && elf_section_type (s) == SHT_NOTE)
4985
0
      {
4986
0
        asection *s2;
4987
0
        unsigned int alignment_power = s->alignment_power;
4988
4989
0
        count = 1;
4990
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
4991
0
    {
4992
0
      if (s2->next->alignment_power == alignment_power
4993
0
          && (s2->next->flags & SEC_LOAD) != 0
4994
0
          && elf_section_type (s2->next) == SHT_NOTE
4995
0
          && align_power (s2->lma + s2->size / opb,
4996
0
              alignment_power)
4997
0
          == s2->next->lma)
4998
0
        count++;
4999
0
      else
5000
0
        break;
5001
0
    }
5002
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5003
0
        amt += count * sizeof (asection *);
5004
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5005
0
        if (m == NULL)
5006
0
    goto error_return;
5007
0
        m->next = NULL;
5008
0
        m->p_type = PT_NOTE;
5009
0
        m->count = count;
5010
0
        while (count > 1)
5011
0
    {
5012
0
      m->sections[m->count - count--] = s;
5013
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5014
0
      s = s->next;
5015
0
    }
5016
0
        m->sections[m->count - 1] = s;
5017
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5018
0
        *pm = m;
5019
0
        pm = &m->next;
5020
0
      }
5021
0
    if (s->flags & SEC_THREAD_LOCAL)
5022
0
      {
5023
0
        if (! tls_count)
5024
0
    first_tls = s;
5025
0
        tls_count++;
5026
0
      }
5027
0
    if (first_mbind == NULL
5028
0
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5029
0
      first_mbind = s;
5030
0
  }
5031
5032
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5033
0
      if (tls_count > 0)
5034
0
  {
5035
0
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5036
0
    amt += tls_count * sizeof (asection *);
5037
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5038
0
    if (m == NULL)
5039
0
      goto error_return;
5040
0
    m->next = NULL;
5041
0
    m->p_type = PT_TLS;
5042
0
    m->count = tls_count;
5043
    /* Mandated PF_R.  */
5044
0
    m->p_flags = PF_R;
5045
0
    m->p_flags_valid = 1;
5046
0
    s = first_tls;
5047
0
    for (i = 0; i < tls_count; ++i)
5048
0
      {
5049
0
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5050
0
    {
5051
0
      _bfd_error_handler
5052
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5053
0
      s = first_tls;
5054
0
      i = 0;
5055
0
      while (i < tls_count)
5056
0
        {
5057
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5058
0
      {
5059
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5060
0
        i++;
5061
0
      }
5062
0
          else
5063
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5064
0
          s = s->next;
5065
0
        }
5066
0
      bfd_set_error (bfd_error_bad_value);
5067
0
      goto error_return;
5068
0
    }
5069
0
        m->sections[i] = s;
5070
0
        s = s->next;
5071
0
      }
5072
5073
0
    *pm = m;
5074
0
    pm = &m->next;
5075
0
  }
5076
5077
0
      if (first_mbind
5078
0
    && (abfd->flags & D_PAGED) != 0
5079
0
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5080
0
  for (s = first_mbind; s != NULL; s = s->next)
5081
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5082
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5083
0
      {
5084
        /* Mandated PF_R.  */
5085
0
        unsigned long p_flags = PF_R;
5086
0
        if ((s->flags & SEC_READONLY) == 0)
5087
0
    p_flags |= PF_W;
5088
0
        if ((s->flags & SEC_CODE) != 0)
5089
0
    p_flags |= PF_X;
5090
5091
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5092
0
        m = bfd_zalloc (abfd, amt);
5093
0
        if (m == NULL)
5094
0
    goto error_return;
5095
0
        m->next = NULL;
5096
0
        m->p_type = (PT_GNU_MBIND_LO
5097
0
         + elf_section_data (s)->this_hdr.sh_info);
5098
0
        m->count = 1;
5099
0
        m->p_flags_valid = 1;
5100
0
        m->sections[0] = s;
5101
0
        m->p_flags = p_flags;
5102
5103
0
        *pm = m;
5104
0
        pm = &m->next;
5105
0
      }
5106
5107
0
      s = bfd_get_section_by_name (abfd,
5108
0
           NOTE_GNU_PROPERTY_SECTION_NAME);
5109
0
      if (s != NULL && s->size != 0)
5110
0
  {
5111
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5112
0
    m = bfd_zalloc (abfd, amt);
5113
0
    if (m == NULL)
5114
0
      goto error_return;
5115
0
    m->next = NULL;
5116
0
    m->p_type = PT_GNU_PROPERTY;
5117
0
    m->count = 1;
5118
0
    m->p_flags_valid = 1;
5119
0
    m->sections[0] = s;
5120
0
    m->p_flags = PF_R;
5121
0
    *pm = m;
5122
0
    pm = &m->next;
5123
0
  }
5124
5125
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5126
   segment.  */
5127
0
      eh_frame_hdr = elf_eh_frame_hdr (info);
5128
0
      if (eh_frame_hdr != NULL
5129
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5130
0
  {
5131
0
    amt = sizeof (struct elf_segment_map);
5132
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5133
0
    if (m == NULL)
5134
0
      goto error_return;
5135
0
    m->next = NULL;
5136
0
    m->p_type = PT_GNU_EH_FRAME;
5137
0
    m->count = 1;
5138
0
    m->sections[0] = eh_frame_hdr->output_section;
5139
5140
0
    *pm = m;
5141
0
    pm = &m->next;
5142
0
  }
5143
5144
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5145
   segment.  */
5146
0
      sframe = elf_sframe (abfd);
5147
0
      if (sframe != NULL
5148
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5149
0
    && sframe->size != 0)
5150
0
  {
5151
0
    amt = sizeof (struct elf_segment_map);
5152
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5153
0
    if (m == NULL)
5154
0
      goto error_return;
5155
0
    m->next = NULL;
5156
0
    m->p_type = PT_GNU_SFRAME;
5157
0
    m->count = 1;
5158
0
    m->sections[0] = sframe->output_section;
5159
5160
0
    *pm = m;
5161
0
    pm = &m->next;
5162
0
  }
5163
5164
0
      if (elf_stack_flags (abfd))
5165
0
  {
5166
0
    amt = sizeof (struct elf_segment_map);
5167
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5168
0
    if (m == NULL)
5169
0
      goto error_return;
5170
0
    m->next = NULL;
5171
0
    m->p_type = PT_GNU_STACK;
5172
0
    m->p_flags = elf_stack_flags (abfd);
5173
0
    m->p_align = bed->stack_align;
5174
0
    m->p_flags_valid = 1;
5175
0
    m->p_align_valid = m->p_align != 0;
5176
0
    if (info->stacksize > 0)
5177
0
      {
5178
0
        m->p_size = info->stacksize;
5179
0
        m->p_size_valid = 1;
5180
0
      }
5181
5182
0
    *pm = m;
5183
0
    pm = &m->next;
5184
0
  }
5185
5186
0
      if (info != NULL && info->relro)
5187
0
  {
5188
0
    for (m = mfirst; m != NULL; m = m->next)
5189
0
      {
5190
0
        if (m->p_type == PT_LOAD
5191
0
      && m->count != 0
5192
0
      && m->sections[0]->vma >= info->relro_start
5193
0
      && m->sections[0]->vma < info->relro_end)
5194
0
    {
5195
0
      i = m->count;
5196
0
      while (--i != (unsigned) -1)
5197
0
        {
5198
0
          if (m->sections[i]->size > 0
5199
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5200
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5201
0
      break;
5202
0
        }
5203
5204
0
      if (i != (unsigned) -1)
5205
0
        break;
5206
0
    }
5207
0
      }
5208
5209
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5210
0
    if (m != NULL)
5211
0
      {
5212
0
        amt = sizeof (struct elf_segment_map);
5213
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5214
0
        if (m == NULL)
5215
0
    goto error_return;
5216
0
        m->next = NULL;
5217
0
        m->p_type = PT_GNU_RELRO;
5218
0
        *pm = m;
5219
0
        pm = &m->next;
5220
0
      }
5221
0
  }
5222
5223
0
      free (sections);
5224
0
      elf_seg_map (abfd) = mfirst;
5225
0
    }
5226
5227
93
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5228
0
    return false;
5229
5230
471
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5231
378
    ++count;
5232
93
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5233
5234
93
  return true;
5235
5236
0
 error_return:
5237
0
  free (sections);
5238
0
  return false;
5239
93
}
5240
5241
/* Sort sections by address.  */
5242
5243
static int
5244
elf_sort_sections (const void *arg1, const void *arg2)
5245
2.30k
{
5246
2.30k
  const asection *sec1 = *(const asection **) arg1;
5247
2.30k
  const asection *sec2 = *(const asection **) arg2;
5248
2.30k
  bfd_size_type size1, size2;
5249
5250
  /* Sort by LMA first, since this is the address used to
5251
     place the section into a segment.  */
5252
2.30k
  if (sec1->lma < sec2->lma)
5253
2.12k
    return -1;
5254
179
  else if (sec1->lma > sec2->lma)
5255
131
    return 1;
5256
5257
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5258
     the same, and this will do nothing.  */
5259
48
  if (sec1->vma < sec2->vma)
5260
0
    return -1;
5261
48
  else if (sec1->vma > sec2->vma)
5262
9
    return 1;
5263
5264
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5265
5266
78
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5267
78
      && (x)->size != 0)
5268
5269
39
  if (TOEND (sec1))
5270
39
    {
5271
39
      if (!TOEND (sec2))
5272
0
  return 1;
5273
39
    }
5274
0
  else if (TOEND (sec2))
5275
0
    return -1;
5276
5277
39
#undef TOEND
5278
5279
  /* Sort by size, to put zero sized sections
5280
     before others at the same address.  */
5281
5282
39
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5283
39
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5284
5285
39
  if (size1 < size2)
5286
0
    return -1;
5287
39
  if (size1 > size2)
5288
0
    return 1;
5289
5290
39
  return sec1->target_index - sec2->target_index;
5291
39
}
5292
5293
/* This qsort comparison functions sorts PT_LOAD segments first and
5294
   by p_paddr, for assign_file_positions_for_load_sections.  */
5295
5296
static int
5297
elf_sort_segments (const void *arg1, const void *arg2)
5298
703
{
5299
703
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5300
703
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5301
5302
703
  if (m1->p_type != m2->p_type)
5303
610
    {
5304
610
      if (m1->p_type == PT_NULL)
5305
1
  return 1;
5306
609
      if (m2->p_type == PT_NULL)
5307
3
  return -1;
5308
606
      return m1->p_type < m2->p_type ? -1 : 1;
5309
609
    }
5310
93
  if (m1->includes_filehdr != m2->includes_filehdr)
5311
40
    return m1->includes_filehdr ? -1 : 1;
5312
53
  if (m1->no_sort_lma != m2->no_sort_lma)
5313
0
    return m1->no_sort_lma ? -1 : 1;
5314
53
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5315
34
    {
5316
34
      bfd_vma lma1, lma2;  /* Octets.  */
5317
34
      lma1 = 0;
5318
34
      if (m1->p_paddr_valid)
5319
34
  lma1 = m1->p_paddr;
5320
0
      else if (m1->count != 0)
5321
0
  {
5322
0
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5323
0
              m1->sections[0]);
5324
0
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5325
0
  }
5326
34
      lma2 = 0;
5327
34
      if (m2->p_paddr_valid)
5328
34
  lma2 = m2->p_paddr;
5329
0
      else if (m2->count != 0)
5330
0
  {
5331
0
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5332
0
              m2->sections[0]);
5333
0
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5334
0
  }
5335
34
      if (lma1 != lma2)
5336
34
  return lma1 < lma2 ? -1 : 1;
5337
34
    }
5338
19
  if (m1->idx != m2->idx)
5339
19
    return m1->idx < m2->idx ? -1 : 1;
5340
0
  return 0;
5341
19
}
5342
5343
/* Ian Lance Taylor writes:
5344
5345
   We shouldn't be using % with a negative signed number.  That's just
5346
   not good.  We have to make sure either that the number is not
5347
   negative, or that the number has an unsigned type.  When the types
5348
   are all the same size they wind up as unsigned.  When file_ptr is a
5349
   larger signed type, the arithmetic winds up as signed long long,
5350
   which is wrong.
5351
5352
   What we're trying to say here is something like ``increase OFF by
5353
   the least amount that will cause it to be equal to the VMA modulo
5354
   the page size.''  */
5355
/* In other words, something like:
5356
5357
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5358
   off_offset = off % bed->maxpagesize;
5359
   if (vma_offset < off_offset)
5360
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5361
   else
5362
     adjustment = vma_offset - off_offset;
5363
5364
   which can be collapsed into the expression below.  */
5365
5366
static file_ptr
5367
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5368
167
{
5369
  /* PR binutils/16199: Handle an alignment of zero.  */
5370
167
  if (maxpagesize == 0)
5371
0
    maxpagesize = 1;
5372
167
  return ((vma - off) % maxpagesize);
5373
167
}
5374
5375
static void
5376
print_segment_map (const struct elf_segment_map *m)
5377
16
{
5378
16
  unsigned int j;
5379
16
  const char *pt = get_segment_type (m->p_type);
5380
16
  char buf[32];
5381
5382
16
  if (pt == NULL)
5383
0
    {
5384
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5385
0
  sprintf (buf, "LOPROC+%7.7x",
5386
0
     (unsigned int) (m->p_type - PT_LOPROC));
5387
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5388
0
  sprintf (buf, "LOOS+%7.7x",
5389
0
     (unsigned int) (m->p_type - PT_LOOS));
5390
0
      else
5391
0
  snprintf (buf, sizeof (buf), "%8.8x",
5392
0
      (unsigned int) m->p_type);
5393
0
      pt = buf;
5394
0
    }
5395
16
  fflush (stdout);
5396
16
  fprintf (stderr, "%s:", pt);
5397
304
  for (j = 0; j < m->count; j++)
5398
288
    fprintf (stderr, " %s", m->sections [j]->name);
5399
16
  putc ('\n',stderr);
5400
16
  fflush (stderr);
5401
16
}
5402
5403
/* Assign file positions to the sections based on the mapping from
5404
   sections to segments.  This function also sets up some fields in
5405
   the file header.  */
5406
5407
static bool
5408
assign_file_positions_for_load_sections (bfd *abfd,
5409
           struct bfd_link_info *link_info)
5410
93
{
5411
93
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5412
93
  struct elf_segment_map *m;
5413
93
  struct elf_segment_map *phdr_load_seg;
5414
93
  Elf_Internal_Phdr *phdrs;
5415
93
  Elf_Internal_Phdr *p;
5416
93
  file_ptr off;  /* Octets.  */
5417
93
  bfd_size_type maxpagesize;
5418
93
  unsigned int alloc, actual;
5419
93
  unsigned int i, j;
5420
93
  struct elf_segment_map **sorted_seg_map;
5421
93
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5422
5423
93
  if (link_info == NULL
5424
93
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5425
0
    return false;
5426
5427
93
  alloc = 0;
5428
471
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5429
378
    m->idx = alloc++;
5430
5431
93
  if (alloc)
5432
44
    {
5433
44
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5434
44
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5435
44
    }
5436
49
  else
5437
49
    {
5438
      /* PR binutils/12467.  */
5439
49
      elf_elfheader (abfd)->e_phoff = 0;
5440
49
      elf_elfheader (abfd)->e_phentsize = 0;
5441
49
    }
5442
5443
93
  elf_elfheader (abfd)->e_phnum = alloc;
5444
5445
93
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5446
0
    {
5447
0
      actual = alloc;
5448
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5449
0
    }
5450
93
  else
5451
93
    {
5452
93
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5453
93
      BFD_ASSERT (elf_program_header_size (abfd)
5454
93
      == actual * bed->s->sizeof_phdr);
5455
93
      BFD_ASSERT (actual >= alloc);
5456
93
    }
5457
5458
93
  if (alloc == 0)
5459
49
    {
5460
49
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5461
49
      return true;
5462
49
    }
5463
5464
  /* We're writing the size in elf_program_header_size (abfd),
5465
     see assign_file_positions_except_relocs, so make sure we have
5466
     that amount allocated, with trailing space cleared.
5467
     The variable alloc contains the computed need, while
5468
     elf_program_header_size (abfd) contains the size used for the
5469
     layout.
5470
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5471
     where the layout is forced to according to a larger size in the
5472
     last iterations for the testcase ld-elf/header.  */
5473
44
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5474
44
           + alloc * sizeof (*sorted_seg_map)));
5475
44
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5476
44
  elf_tdata (abfd)->phdr = phdrs;
5477
44
  if (phdrs == NULL)
5478
0
    return false;
5479
5480
422
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5481
378
    {
5482
378
      sorted_seg_map[j] = m;
5483
      /* If elf_segment_map is not from map_sections_to_segments, the
5484
   sections may not be correctly ordered.  NOTE: sorting should
5485
   not be done to the PT_NOTE section of a corefile, which may
5486
   contain several pseudo-sections artificially created by bfd.
5487
   Sorting these pseudo-sections breaks things badly.  */
5488
378
      if (m->count > 1
5489
378
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5490
173
         && m->p_type == PT_NOTE))
5491
173
  {
5492
1.58k
    for (i = 0; i < m->count; i++)
5493
1.41k
      m->sections[i]->target_index = i;
5494
173
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5495
173
     elf_sort_sections);
5496
173
  }
5497
378
    }
5498
44
  if (alloc > 1)
5499
43
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5500
43
     elf_sort_segments);
5501
5502
44
  maxpagesize = 1;
5503
44
  if ((abfd->flags & D_PAGED) != 0)
5504
42
    {
5505
42
      if (link_info != NULL)
5506
0
  maxpagesize = link_info->maxpagesize;
5507
42
      else
5508
42
  maxpagesize = bed->maxpagesize;
5509
42
    }
5510
5511
  /* Sections must map to file offsets past the ELF file header.  */
5512
44
  off = bed->s->sizeof_ehdr;
5513
  /* And if one of the PT_LOAD headers doesn't include the program
5514
     headers then we'll be mapping program headers in the usual
5515
     position after the ELF file header.  */
5516
44
  phdr_load_seg = NULL;
5517
50
  for (j = 0; j < alloc; j++)
5518
49
    {
5519
49
      m = sorted_seg_map[j];
5520
49
      if (m->p_type != PT_LOAD)
5521
3
  break;
5522
46
      if (m->includes_phdrs)
5523
40
  {
5524
40
    phdr_load_seg = m;
5525
40
    break;
5526
40
  }
5527
46
    }
5528
44
  if (phdr_load_seg == NULL)
5529
4
    off += actual * bed->s->sizeof_phdr;
5530
5531
396
  for (j = 0; j < alloc; j++)
5532
360
    {
5533
360
      asection **secpp;
5534
360
      bfd_vma off_adjust;  /* Octets.  */
5535
360
      bool no_contents;
5536
360
      bfd_size_type p_align;
5537
360
      bool p_align_p;
5538
5539
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5540
   number of sections with contents contributing to both p_filesz
5541
   and p_memsz, followed by a number of sections with no contents
5542
   that just contribute to p_memsz.  In this loop, OFF tracks next
5543
   available file offset for PT_LOAD and PT_NOTE segments.  */
5544
360
      m = sorted_seg_map[j];
5545
360
      p = phdrs + m->idx;
5546
360
      p->p_type = m->p_type;
5547
360
      p->p_flags = m->p_flags;
5548
360
      p_align = bed->p_align;
5549
360
      p_align_p = false;
5550
5551
360
      if (m->count == 0)
5552
100
  p->p_vaddr = m->p_vaddr_offset * opb;
5553
260
      else
5554
260
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5555
5556
360
      if (m->p_paddr_valid)
5557
330
  p->p_paddr = m->p_paddr;
5558
30
      else if (m->count == 0)
5559
12
  p->p_paddr = 0;
5560
18
      else
5561
18
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5562
5563
360
      if (p->p_type == PT_LOAD
5564
360
    && (abfd->flags & D_PAGED) != 0)
5565
116
  {
5566
    /* p_align in demand paged PT_LOAD segments effectively stores
5567
       the maximum page size.  When copying an executable with
5568
       objcopy, we set m->p_align from the input file.  Use this
5569
       value for maxpagesize rather than bed->maxpagesize, which
5570
       may be different.  Note that we use maxpagesize for PT_TLS
5571
       segment alignment later in this function, so we are relying
5572
       on at least one PT_LOAD segment appearing before a PT_TLS
5573
       segment.  */
5574
116
    if (m->p_align_valid)
5575
116
      maxpagesize = m->p_align;
5576
0
    else if (p_align != 0
5577
0
       && (link_info == NULL
5578
0
           || !link_info->maxpagesize_is_set))
5579
      /* Set p_align to the default p_align value while laying
5580
         out segments aligning to the maximum page size or the
5581
         largest section alignment.  The run-time loader can
5582
         align segments to the default p_align value or the
5583
         maximum page size, depending on system page size.  */
5584
0
      p_align_p = true;
5585
5586
116
    p->p_align = maxpagesize;
5587
116
  }
5588
244
      else if (m->p_align_valid)
5589
236
  p->p_align = m->p_align;
5590
8
      else if (m->count == 0)
5591
0
  p->p_align = 1 << bed->s->log_file_align;
5592
5593
360
      if (m == phdr_load_seg)
5594
40
  {
5595
40
    if (!m->includes_filehdr)
5596
0
      p->p_offset = off;
5597
40
    off += actual * bed->s->sizeof_phdr;
5598
40
  }
5599
5600
360
      no_contents = false;
5601
360
      off_adjust = 0;
5602
360
      if (p->p_type == PT_LOAD
5603
360
    && m->count > 0)
5604
109
  {
5605
109
    bfd_size_type align;  /* Bytes.  */
5606
109
    unsigned int align_power = 0;
5607
5608
109
    if (m->p_align_valid)
5609
108
      align = p->p_align;
5610
1
    else
5611
1
      {
5612
2
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5613
1
    {
5614
1
      unsigned int secalign;
5615
5616
1
      secalign = bfd_section_alignment (*secpp);
5617
1
      if (secalign > align_power)
5618
1
        align_power = secalign;
5619
1
    }
5620
1
        align = (bfd_size_type) 1 << align_power;
5621
1
        if (align < maxpagesize)
5622
0
    {
5623
      /* If a section requires alignment higher than the
5624
         default p_align value, don't set p_align to the
5625
         default p_align value.  */
5626
0
      if (align > p_align)
5627
0
        p_align_p = false;
5628
0
      align = maxpagesize;
5629
0
    }
5630
1
        else
5631
1
    {
5632
      /* If a section requires alignment higher than the
5633
         maximum page size, set p_align to the section
5634
         alignment.  */
5635
1
      p_align_p = true;
5636
1
      p_align = align;
5637
1
    }
5638
1
      }
5639
5640
990
    for (i = 0; i < m->count; i++)
5641
881
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5642
        /* If we aren't making room for this section, then
5643
     it must be SHT_NOBITS regardless of what we've
5644
     set via struct bfd_elf_special_section.  */
5645
43
        elf_section_type (m->sections[i]) = SHT_NOBITS;
5646
5647
    /* Find out whether this segment contains any loadable
5648
       sections.  */
5649
109
    no_contents = true;
5650
110
    for (i = 0; i < m->count; i++)
5651
109
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5652
108
        {
5653
108
    no_contents = false;
5654
108
    break;
5655
108
        }
5656
5657
109
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5658
5659
    /* Broken hardware and/or kernel require that files do not
5660
       map the same page with different permissions on some hppa
5661
       processors.  */
5662
109
    if (j != 0
5663
109
        && (abfd->flags & D_PAGED) != 0
5664
109
        && bed->no_page_alias
5665
109
        && (off & (maxpagesize - 1)) != 0
5666
109
        && ((off & -maxpagesize)
5667
1
      == ((off + off_adjust) & -maxpagesize)))
5668
1
      off_adjust += maxpagesize;
5669
109
    off += off_adjust;
5670
109
    if (no_contents)
5671
1
      {
5672
        /* We shouldn't need to align the segment on disk since
5673
     the segment doesn't need file space, but the gABI
5674
     arguably requires the alignment and glibc ld.so
5675
     checks it.  So to comply with the alignment
5676
     requirement but not waste file space, we adjust
5677
     p_offset for just this segment.  (OFF_ADJUST is
5678
     subtracted from OFF later.)  This may put p_offset
5679
     past the end of file, but that shouldn't matter.  */
5680
1
      }
5681
108
    else
5682
108
      off_adjust = 0;
5683
109
  }
5684
      /* Make sure the .dynamic section is the first section in the
5685
   PT_DYNAMIC segment.  */
5686
251
      else if (p->p_type == PT_DYNAMIC
5687
251
         && m->count > 1
5688
251
         && strcmp (m->sections[0]->name, ".dynamic") != 0)
5689
7
  {
5690
7
    _bfd_error_handler
5691
7
      (_("%pB: The first section in the PT_DYNAMIC segment"
5692
7
         " is not the .dynamic section"),
5693
7
       abfd);
5694
7
    bfd_set_error (bfd_error_bad_value);
5695
7
    return false;
5696
7
  }
5697
      /* Set the note section type to SHT_NOTE.  */
5698
244
      else if (p->p_type == PT_NOTE)
5699
93
  for (i = 0; i < m->count; i++)
5700
51
    elf_section_type (m->sections[i]) = SHT_NOTE;
5701
5702
353
      if (m->includes_filehdr)
5703
40
  {
5704
40
    if (!m->p_flags_valid)
5705
0
      p->p_flags |= PF_R;
5706
40
    p->p_filesz = bed->s->sizeof_ehdr;
5707
40
    p->p_memsz = bed->s->sizeof_ehdr;
5708
40
    if (p->p_type == PT_LOAD)
5709
40
      {
5710
40
        if (m->count > 0)
5711
38
    {
5712
38
      if (p->p_vaddr < (bfd_vma) off
5713
38
          || (!m->p_paddr_valid
5714
38
        && p->p_paddr < (bfd_vma) off))
5715
0
        {
5716
0
          _bfd_error_handler
5717
0
      (_("%pB: not enough room for program headers,"
5718
0
         " try linking with -N"),
5719
0
       abfd);
5720
0
          bfd_set_error (bfd_error_bad_value);
5721
0
          return false;
5722
0
        }
5723
38
      p->p_vaddr -= off;
5724
38
      if (!m->p_paddr_valid)
5725
5
        p->p_paddr -= off;
5726
38
    }
5727
40
      }
5728
0
    else if (sorted_seg_map[0]->includes_filehdr)
5729
0
      {
5730
0
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5731
0
        p->p_vaddr = filehdr->p_vaddr;
5732
0
        if (!m->p_paddr_valid)
5733
0
    p->p_paddr = filehdr->p_paddr;
5734
0
      }
5735
40
  }
5736
5737
353
      if (m->includes_phdrs)
5738
74
  {
5739
74
    if (!m->p_flags_valid)
5740
0
      p->p_flags |= PF_R;
5741
74
    p->p_filesz += actual * bed->s->sizeof_phdr;
5742
74
    p->p_memsz += actual * bed->s->sizeof_phdr;
5743
74
    if (!m->includes_filehdr)
5744
34
      {
5745
34
        if (p->p_type == PT_LOAD)
5746
0
    {
5747
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
5748
0
      if (m->count > 0)
5749
0
        {
5750
0
          p->p_vaddr -= off - p->p_offset;
5751
0
          if (!m->p_paddr_valid)
5752
0
      p->p_paddr -= off - p->p_offset;
5753
0
        }
5754
0
    }
5755
34
        else if (phdr_load_seg != NULL)
5756
32
    {
5757
32
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5758
32
      bfd_vma phdr_off = 0;  /* Octets.  */
5759
32
      if (phdr_load_seg->includes_filehdr)
5760
32
        phdr_off = bed->s->sizeof_ehdr;
5761
32
      p->p_vaddr = phdr->p_vaddr + phdr_off;
5762
32
      if (!m->p_paddr_valid)
5763
2
        p->p_paddr = phdr->p_paddr + phdr_off;
5764
32
      p->p_offset = phdr->p_offset + phdr_off;
5765
32
    }
5766
2
        else
5767
2
    p->p_offset = bed->s->sizeof_ehdr;
5768
34
      }
5769
74
  }
5770
5771
353
      if (p->p_type == PT_LOAD
5772
353
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5773
117
  {
5774
117
    if (!m->includes_filehdr && !m->includes_phdrs)
5775
77
      {
5776
77
        p->p_offset = off;
5777
77
        if (no_contents)
5778
1
    {
5779
      /* Put meaningless p_offset for PT_LOAD segments
5780
         without file contents somewhere within the first
5781
         page, in an attempt to not point past EOF.  */
5782
1
      bfd_size_type align = maxpagesize;
5783
1
      if (align < p->p_align)
5784
0
        align = p->p_align;
5785
1
      if (align < 1)
5786
0
        align = 1;
5787
1
      p->p_offset = off % align;
5788
1
    }
5789
77
      }
5790
40
    else
5791
40
      {
5792
40
        file_ptr adjust;  /* Octets.  */
5793
5794
40
        adjust = off - (p->p_offset + p->p_filesz);
5795
40
        if (!no_contents)
5796
40
    p->p_filesz += adjust;
5797
40
        p->p_memsz += adjust;
5798
40
      }
5799
117
  }
5800
5801
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5802
   maps.  Set filepos for sections in PT_LOAD segments, and in
5803
   core files, for sections in PT_NOTE segments.
5804
   assign_file_positions_for_non_load_sections will set filepos
5805
   for other sections and update p_filesz for other segments.  */
5806
1.53k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5807
1.18k
  {
5808
1.18k
    asection *sec;
5809
1.18k
    bfd_size_type align;
5810
1.18k
    Elf_Internal_Shdr *this_hdr;
5811
5812
1.18k
    sec = *secpp;
5813
1.18k
    this_hdr = &elf_section_data (sec)->this_hdr;
5814
1.18k
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5815
5816
1.18k
    if ((p->p_type == PT_LOAD
5817
1.18k
         || p->p_type == PT_TLS)
5818
1.18k
        && (this_hdr->sh_type != SHT_NOBITS
5819
881
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5820
43
          && ((this_hdr->sh_flags & SHF_TLS) == 0
5821
43
        || p->p_type == PT_TLS))))
5822
881
      {
5823
881
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
5824
881
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
5825
881
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
5826
881
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
5827
5828
881
        if (adjust != 0
5829
881
      && (s_start < p_end
5830
246
          || p_end < p_start))
5831
36
    {
5832
36
      _bfd_error_handler
5833
        /* xgettext:c-format */
5834
36
        (_("%pB: section %pA lma %#" PRIx64
5835
36
           " adjusted to %#" PRIx64),
5836
36
         abfd, sec, (uint64_t) s_start / opb,
5837
36
         (uint64_t) p_end / opb);
5838
36
      adjust = 0;
5839
36
      sec->lma = p_end / opb;
5840
36
    }
5841
881
        p->p_memsz += adjust;
5842
5843
881
        if (p->p_type == PT_LOAD)
5844
881
    {
5845
881
      if (this_hdr->sh_type != SHT_NOBITS)
5846
838
        {
5847
838
          off_adjust = 0;
5848
838
          if (p->p_filesz + adjust < p->p_memsz)
5849
2
      {
5850
        /* We have a PROGBITS section following NOBITS ones.
5851
           Allocate file space for the NOBITS section(s).
5852
           We don't need to write out the zeros, posix
5853
           fseek past the end of data already written
5854
           followed by a write at that location is
5855
           guaranteed to result in zeros being read
5856
           from the gap.  */
5857
2
        adjust = p->p_memsz - p->p_filesz;
5858
2
      }
5859
838
        }
5860
      /* We only adjust sh_offset in SHT_NOBITS sections
5861
         as would seem proper for their address when the
5862
         section is first in the segment.  sh_offset
5863
         doesn't really have any significance for
5864
         SHT_NOBITS anyway, apart from a notional position
5865
         relative to other sections.  Historically we
5866
         didn't bother with adjusting sh_offset and some
5867
         programs depend on it not being adjusted.  See
5868
         pr12921 and pr25662.  */
5869
881
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5870
839
        {
5871
839
          off += adjust;
5872
839
          if (this_hdr->sh_type == SHT_NOBITS)
5873
1
      off_adjust += adjust;
5874
839
        }
5875
881
    }
5876
881
        if (this_hdr->sh_type != SHT_NOBITS)
5877
838
    p->p_filesz += adjust;
5878
881
      }
5879
5880
1.18k
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5881
0
      {
5882
        /* The section at i == 0 is the one that actually contains
5883
     everything.  */
5884
0
        if (i == 0)
5885
0
    {
5886
0
      this_hdr->sh_offset = sec->filepos = off;
5887
0
      off += this_hdr->sh_size;
5888
0
      p->p_filesz = this_hdr->sh_size;
5889
0
      p->p_memsz = 0;
5890
0
      p->p_align = 1;
5891
0
    }
5892
0
        else
5893
0
    {
5894
      /* The rest are fake sections that shouldn't be written.  */
5895
0
      sec->filepos = 0;
5896
0
      sec->size = 0;
5897
0
      sec->flags = 0;
5898
0
      continue;
5899
0
    }
5900
0
      }
5901
1.18k
    else
5902
1.18k
      {
5903
1.18k
        if (p->p_type == PT_LOAD)
5904
881
    {
5905
881
      this_hdr->sh_offset = sec->filepos = off;
5906
881
      if (this_hdr->sh_type != SHT_NOBITS)
5907
838
        off += this_hdr->sh_size;
5908
881
    }
5909
301
        else if (this_hdr->sh_type == SHT_NOBITS
5910
301
           && (this_hdr->sh_flags & SHF_TLS) != 0
5911
301
           && this_hdr->sh_offset == 0)
5912
0
    {
5913
      /* This is a .tbss section that didn't get a PT_LOAD.
5914
         (See _bfd_elf_map_sections_to_segments "Create a
5915
         final PT_LOAD".)  Set sh_offset to the value it
5916
         would have if we had created a zero p_filesz and
5917
         p_memsz PT_LOAD header for the section.  This
5918
         also makes the PT_TLS header have the same
5919
         p_offset value.  */
5920
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5921
0
                off, align);
5922
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
5923
0
    }
5924
5925
1.18k
        if (this_hdr->sh_type != SHT_NOBITS)
5926
1.13k
    {
5927
1.13k
      p->p_filesz += this_hdr->sh_size;
5928
      /* A load section without SHF_ALLOC is something like
5929
         a note section in a PT_NOTE segment.  These take
5930
         file space but are not loaded into memory.  */
5931
1.13k
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5932
1.10k
        p->p_memsz += this_hdr->sh_size;
5933
1.13k
    }
5934
49
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5935
49
    {
5936
49
      if (p->p_type == PT_TLS)
5937
0
        p->p_memsz += this_hdr->sh_size;
5938
5939
      /* .tbss is special.  It doesn't contribute to p_memsz of
5940
         normal segments.  */
5941
49
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5942
49
        p->p_memsz += this_hdr->sh_size;
5943
49
    }
5944
5945
1.18k
        if (align > p->p_align
5946
1.18k
      && !m->p_align_valid
5947
1.18k
      && (p->p_type != PT_LOAD
5948
1
          || (abfd->flags & D_PAGED) == 0))
5949
1
    p->p_align = align;
5950
1.18k
      }
5951
5952
1.18k
    if (!m->p_flags_valid)
5953
0
      {
5954
0
        p->p_flags |= PF_R;
5955
0
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5956
0
    p->p_flags |= PF_X;
5957
0
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5958
0
    p->p_flags |= PF_W;
5959
0
      }
5960
1.18k
  }
5961
5962
353
      off -= off_adjust;
5963
5964
      /* PR ld/20815 - Check that the program header segment, if
5965
   present, will be loaded into memory.  */
5966
353
      if (p->p_type == PT_PHDR
5967
353
    && phdr_load_seg == NULL
5968
353
    && !(bed->elf_backend_allow_non_load_phdr != NULL
5969
1
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5970
1
  {
5971
    /* The fix for this error is usually to edit the linker script being
5972
       used and set up the program headers manually.  Either that or
5973
       leave room for the headers at the start of the SECTIONS.  */
5974
1
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5975
1
        " by LOAD segment"),
5976
1
            abfd);
5977
1
    if (link_info == NULL)
5978
1
      return false;
5979
    /* Arrange for the linker to exit with an error, deleting
5980
       the output file unless --noinhibit-exec is given.  */
5981
0
    link_info->callbacks->info ("%X");
5982
0
  }
5983
5984
      /* Check that all sections are in a PT_LOAD segment.
5985
   Don't check funky gdb generated core files.  */
5986
352
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5987
116
  {
5988
116
    bool check_vma = true;
5989
5990
888
    for (i = 1; i < m->count; i++)
5991
772
      if (m->sections[i]->vma == m->sections[i - 1]->vma
5992
772
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5993
0
               ->this_hdr), p) != 0
5994
772
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5995
0
               ->this_hdr), p) != 0)
5996
0
        {
5997
    /* Looks like we have overlays packed into the segment.  */
5998
0
    check_vma = false;
5999
0
    break;
6000
0
        }
6001
6002
996
    for (i = 0; i < m->count; i++)
6003
880
      {
6004
880
        Elf_Internal_Shdr *this_hdr;
6005
880
        asection *sec;
6006
6007
880
        sec = m->sections[i];
6008
880
        this_hdr = &(elf_section_data(sec)->this_hdr);
6009
880
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6010
880
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6011
16
    {
6012
16
      _bfd_error_handler
6013
        /* xgettext:c-format */
6014
16
        (_("%pB: section `%pA' can't be allocated in segment %d"),
6015
16
         abfd, sec, j);
6016
16
      print_segment_map (m);
6017
16
    }
6018
880
      }
6019
6020
116
    if (p_align_p)
6021
0
      p->p_align = p_align;
6022
116
  }
6023
352
    }
6024
6025
36
  elf_next_file_pos (abfd) = off;
6026
6027
36
  if (link_info != NULL
6028
36
      && phdr_load_seg != NULL
6029
36
      && phdr_load_seg->includes_filehdr)
6030
0
    {
6031
      /* There is a segment that contains both the file headers and the
6032
   program headers, so provide a symbol __ehdr_start pointing there.
6033
   A program can use this to examine itself robustly.  */
6034
6035
0
      struct elf_link_hash_entry *hash
6036
0
  = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6037
0
        false, false, true);
6038
      /* If the symbol was referenced and not defined, define it.  */
6039
0
      if (hash != NULL
6040
0
    && (hash->root.type == bfd_link_hash_new
6041
0
        || hash->root.type == bfd_link_hash_undefined
6042
0
        || hash->root.type == bfd_link_hash_undefweak
6043
0
        || hash->root.type == bfd_link_hash_common))
6044
0
  {
6045
0
    asection *s = NULL;
6046
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6047
6048
0
    if (phdr_load_seg->count != 0)
6049
      /* The segment contains sections, so use the first one.  */
6050
0
      s = phdr_load_seg->sections[0];
6051
0
    else
6052
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6053
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6054
0
        if (m->p_type == PT_LOAD && m->count != 0)
6055
0
    {
6056
0
      s = m->sections[0];
6057
0
      break;
6058
0
    }
6059
6060
0
    if (s != NULL)
6061
0
      {
6062
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6063
0
        hash->root.u.def.section = s;
6064
0
      }
6065
0
    else
6066
0
      {
6067
0
        hash->root.u.def.value = filehdr_vaddr;
6068
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6069
0
      }
6070
6071
0
    hash->root.type = bfd_link_hash_defined;
6072
0
    hash->def_regular = 1;
6073
0
    hash->non_elf = 0;
6074
0
  }
6075
0
    }
6076
6077
36
  return true;
6078
44
}
6079
6080
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6081
   is no defined method for detecting such files, so we have to
6082
   use heuristics instead.  */
6083
6084
bool
6085
is_debuginfo_file (bfd *abfd)
6086
58
{
6087
58
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6088
0
    return false;
6089
6090
58
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6091
58
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6092
58
  Elf_Internal_Shdr **headerp;
6093
6094
116
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6095
116
    {
6096
116
      Elf_Internal_Shdr *header = * headerp;
6097
6098
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6099
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6100
116
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6101
116
    && header->sh_type != SHT_NOBITS
6102
116
    && header->sh_type != SHT_NOTE)
6103
58
  return false;
6104
116
    }
6105
6106
0
  return true;
6107
58
}
6108
6109
/* Assign file positions for other sections, except for compressed debug
6110
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6111
6112
static bool
6113
assign_file_positions_for_non_load_sections (bfd *abfd,
6114
               struct bfd_link_info *link_info)
6115
85
{
6116
85
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6117
85
  Elf_Internal_Shdr **i_shdrpp;
6118
85
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6119
85
  Elf_Internal_Phdr *phdrs;
6120
85
  Elf_Internal_Phdr *p;
6121
85
  struct elf_segment_map *m;
6122
85
  file_ptr off;
6123
85
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6124
85
  bfd_vma maxpagesize;
6125
6126
85
  if (link_info != NULL)
6127
0
    maxpagesize = link_info->maxpagesize;
6128
85
  else
6129
85
    maxpagesize = bed->maxpagesize;
6130
85
  i_shdrpp = elf_elfsections (abfd);
6131
85
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6132
85
  off = elf_next_file_pos (abfd);
6133
1.28k
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6134
1.19k
    {
6135
1.19k
      Elf_Internal_Shdr *hdr;
6136
1.19k
      bfd_vma align;
6137
6138
1.19k
      hdr = *hdrpp;
6139
1.19k
      if (hdr->bfd_section != NULL
6140
1.19k
    && (hdr->bfd_section->filepos != 0
6141
1.05k
        || (hdr->sh_type == SHT_NOBITS
6142
289
      && hdr->contents == NULL)))
6143
769
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6144
426
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6145
58
  {
6146
58
    if (hdr->sh_size != 0
6147
        /* PR 24717 - debuginfo files are known to be not strictly
6148
     compliant with the ELF standard.  In particular they often
6149
     have .note.gnu.property sections that are outside of any
6150
     loadable segment.  This is not a problem for such files,
6151
     so do not warn about them.  */
6152
58
        && ! is_debuginfo_file (abfd))
6153
58
      _bfd_error_handler
6154
        /* xgettext:c-format */
6155
58
        (_("%pB: warning: allocated section `%s' not in segment"),
6156
58
         abfd,
6157
58
         (hdr->bfd_section == NULL
6158
58
    ? "*unknown*"
6159
58
    : hdr->bfd_section->name));
6160
    /* We don't need to page align empty sections.  */
6161
58
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6162
58
      align = maxpagesize;
6163
0
    else
6164
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6165
58
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6166
58
    off = _bfd_elf_assign_file_position_for_section (hdr, off,
6167
58
                 false);
6168
58
  }
6169
368
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6170
368
    && hdr->bfd_section == NULL)
6171
         /* We don't know the offset of these sections yet:
6172
      their size has not been decided.  */
6173
368
         || (abfd->is_linker_output
6174
368
       && hdr->bfd_section != NULL
6175
368
       && (hdr->sh_name == -1u
6176
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6177
368
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6178
368
         || (elf_symtab_shndx_list (abfd) != NULL
6179
342
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6180
368
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6181
368
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6182
137
  hdr->sh_offset = -1;
6183
231
      else
6184
231
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6185
1.19k
    }
6186
85
  elf_next_file_pos (abfd) = off;
6187
6188
  /* Now that we have set the section file positions, we can set up
6189
     the file positions for the non PT_LOAD segments.  */
6190
85
  phdrs = elf_tdata (abfd)->phdr;
6191
415
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6192
330
    {
6193
330
      if (p->p_type == PT_GNU_RELRO)
6194
21
  {
6195
21
    bfd_vma start, end;  /* Bytes.  */
6196
21
    bool ok;
6197
6198
21
    if (link_info != NULL)
6199
0
      {
6200
        /* During linking the range of the RELRO segment is passed
6201
     in link_info.  Note that there may be padding between
6202
     relro_start and the first RELRO section.  */
6203
0
        start = link_info->relro_start;
6204
0
        end = link_info->relro_end;
6205
0
      }
6206
21
    else if (m->count != 0)
6207
21
      {
6208
21
        if (!m->p_size_valid)
6209
0
    abort ();
6210
21
        start = m->sections[0]->vma;
6211
21
        end = start + m->p_size / opb;
6212
21
      }
6213
0
    else
6214
0
      {
6215
0
        start = 0;
6216
0
        end = 0;
6217
0
      }
6218
6219
21
    ok = false;
6220
21
    if (start < end)
6221
21
      {
6222
21
        struct elf_segment_map *lm;
6223
21
        const Elf_Internal_Phdr *lp;
6224
21
        unsigned int i;
6225
6226
        /* Find a LOAD segment containing a section in the RELRO
6227
     segment.  */
6228
21
        for (lm = elf_seg_map (abfd), lp = phdrs;
6229
113
       lm != NULL;
6230
92
       lm = lm->next, lp++)
6231
113
    {
6232
113
      if (lp->p_type == PT_LOAD
6233
113
          && lm->count != 0
6234
113
          && (lm->sections[lm->count - 1]->vma
6235
70
        + (!IS_TBSS (lm->sections[lm->count - 1])
6236
70
           ? lm->sections[lm->count - 1]->size / opb
6237
70
           : 0)) > start
6238
113
          && lm->sections[0]->vma < end)
6239
21
        break;
6240
113
    }
6241
6242
21
        if (lm != NULL)
6243
21
    {
6244
      /* Find the section starting the RELRO segment.  */
6245
33
      for (i = 0; i < lm->count; i++)
6246
32
        {
6247
32
          asection *s = lm->sections[i];
6248
32
          if (s->vma >= start
6249
32
        && s->vma < end
6250
32
        && s->size != 0)
6251
20
      break;
6252
32
        }
6253
6254
21
      if (i < lm->count)
6255
20
        {
6256
20
          p->p_vaddr = lm->sections[i]->vma * opb;
6257
20
          p->p_paddr = lm->sections[i]->lma * opb;
6258
20
          p->p_offset = lm->sections[i]->filepos;
6259
20
          p->p_memsz = end * opb - p->p_vaddr;
6260
20
          p->p_filesz = p->p_memsz;
6261
6262
          /* The RELRO segment typically ends a few bytes
6263
       into .got.plt but other layouts are possible.
6264
       In cases where the end does not match any
6265
       loaded section (for instance is in file
6266
       padding), trim p_filesz back to correspond to
6267
       the end of loaded section contents.  */
6268
20
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6269
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6270
6271
          /* Preserve the alignment and flags if they are
6272
       valid.  The gold linker generates RW/4 for
6273
       the PT_GNU_RELRO section.  It is better for
6274
       objcopy/strip to honor these attributes
6275
       otherwise gdb will choke when using separate
6276
       debug files.  */
6277
20
          if (!m->p_align_valid)
6278
0
      p->p_align = 1;
6279
20
          if (!m->p_flags_valid)
6280
0
      p->p_flags = PF_R;
6281
20
          ok = true;
6282
20
        }
6283
21
    }
6284
21
      }
6285
6286
21
    if (!ok)
6287
1
      {
6288
1
        if (link_info != NULL)
6289
0
    _bfd_error_handler
6290
0
      (_("%pB: warning: unable to allocate any sections"
6291
0
         " to PT_GNU_RELRO segment"),
6292
0
       abfd);
6293
1
        memset (p, 0, sizeof *p);
6294
1
      }
6295
21
  }
6296
309
      else if (p->p_type == PT_GNU_STACK)
6297
21
  {
6298
21
    if (m->p_size_valid)
6299
21
      p->p_memsz = m->p_size;
6300
21
  }
6301
288
      else if (m->count != 0)
6302
214
  {
6303
214
    unsigned int i;
6304
6305
214
    if (p->p_type != PT_LOAD
6306
214
        && (p->p_type != PT_NOTE
6307
117
      || bfd_get_format (abfd) != bfd_core))
6308
117
      {
6309
        /* A user specified segment layout may include a PHDR
6310
     segment that overlaps with a LOAD segment...  */
6311
117
        if (p->p_type == PT_PHDR)
6312
0
    {
6313
0
      m->count = 0;
6314
0
      continue;
6315
0
    }
6316
6317
117
        if (m->includes_filehdr || m->includes_phdrs)
6318
0
    {
6319
      /* PR 17512: file: 2195325e.  */
6320
0
      _bfd_error_handler
6321
0
        (_("%pB: error: non-load segment %d includes file header "
6322
0
           "and/or program header"),
6323
0
         abfd, (int) (p - phdrs));
6324
0
      return false;
6325
0
    }
6326
6327
117
        p->p_filesz = 0;
6328
117
        p->p_offset = m->sections[0]->filepos;
6329
119
        for (i = m->count; i-- != 0;)
6330
119
    {
6331
119
      asection *sect = m->sections[i];
6332
119
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6333
119
      if (hdr->sh_type != SHT_NOBITS)
6334
117
        {
6335
117
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6336
          /* NB: p_memsz of the loadable PT_NOTE segment
6337
       should be the same as p_filesz.  */
6338
117
          if (p->p_type == PT_NOTE
6339
117
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6340
27
      p->p_memsz = p->p_filesz;
6341
117
          break;
6342
117
        }
6343
119
    }
6344
117
      }
6345
214
  }
6346
330
    }
6347
6348
85
  return true;
6349
85
}
6350
6351
static elf_section_list *
6352
find_section_in_list (unsigned int i, elf_section_list * list)
6353
0
{
6354
0
  for (;list != NULL; list = list->next)
6355
0
    if (list->ndx == i)
6356
0
      break;
6357
0
  return list;
6358
0
}
6359
6360
/* Work out the file positions of all the sections.  This is called by
6361
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6362
   VMAs must be known before this is called.
6363
6364
   Reloc sections come in two flavours: Those processed specially as
6365
   "side-channel" data attached to a section to which they apply, and
6366
   those that bfd doesn't process as relocations.  The latter sort are
6367
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6368
   consider the former sort here, unless they form part of the loadable
6369
   image.  Reloc sections not assigned here (and compressed debugging
6370
   sections and CTF sections which nothing else in the file can rely
6371
   upon) will be handled later by assign_file_positions_for_relocs.
6372
6373
   We also don't set the positions of the .symtab and .strtab here.  */
6374
6375
static bool
6376
assign_file_positions_except_relocs (bfd *abfd,
6377
             struct bfd_link_info *link_info)
6378
197
{
6379
197
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6380
197
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6381
197
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6382
197
  unsigned int alloc;
6383
6384
197
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6385
197
      && bfd_get_format (abfd) != bfd_core)
6386
104
    {
6387
104
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6388
104
      unsigned int num_sec = elf_numsections (abfd);
6389
104
      Elf_Internal_Shdr **hdrpp;
6390
104
      unsigned int i;
6391
104
      file_ptr off;
6392
6393
      /* Start after the ELF header.  */
6394
104
      off = i_ehdrp->e_ehsize;
6395
6396
      /* We are not creating an executable, which means that we are
6397
   not creating a program header, and that the actual order of
6398
   the sections in the file is unimportant.  */
6399
5.17k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6400
5.07k
  {
6401
5.07k
    Elf_Internal_Shdr *hdr;
6402
6403
5.07k
    hdr = *hdrpp;
6404
5.07k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6405
5.07k
         && hdr->bfd_section == NULL)
6406
        /* Do not assign offsets for these sections yet: we don't know
6407
     their sizes.  */
6408
5.07k
        || (abfd->is_linker_output
6409
3.69k
      && hdr->bfd_section != NULL
6410
3.69k
      && (hdr->sh_name == -1u
6411
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6412
5.07k
        || i == elf_onesymtab (abfd)
6413
5.07k
        || (elf_symtab_shndx_list (abfd) != NULL
6414
3.64k
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6415
5.07k
        || i == elf_strtab_sec (abfd)
6416
5.07k
        || i == elf_shstrtab_sec (abfd))
6417
1.58k
      {
6418
1.58k
        hdr->sh_offset = -1;
6419
1.58k
      }
6420
3.48k
    else
6421
3.48k
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6422
5.07k
  }
6423
6424
104
      elf_next_file_pos (abfd) = off;
6425
104
      elf_program_header_size (abfd) = 0;
6426
104
    }
6427
93
  else
6428
93
    {
6429
      /* Assign file positions for the loaded sections based on the
6430
   assignment of sections to segments.  */
6431
93
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6432
8
  return false;
6433
6434
      /* And for non-load sections.  */
6435
85
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6436
0
  return false;
6437
85
    }
6438
6439
189
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6440
0
    return false;
6441
6442
  /* Write out the program headers.  */
6443
189
  alloc = i_ehdrp->e_phnum;
6444
189
  if (alloc != 0)
6445
36
    {
6446
36
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6447
0
  {
6448
    /* Memory resident segments with non-zero size and RWX
6449
       permissions are a security risk, so we generate a warning
6450
       here if we are creating any.  */
6451
0
    unsigned int i;
6452
6453
0
    for (i = 0; i < alloc; i++)
6454
0
      {
6455
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6456
6457
0
        if (phdr->p_memsz == 0)
6458
0
    continue;
6459
6460
0
        if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6461
0
    _bfd_error_handler (_("warning: %pB has a TLS segment"
6462
0
              " with execute permission"),
6463
0
            abfd);
6464
0
        else if (phdr->p_type == PT_LOAD
6465
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6466
0
         == (PF_R | PF_W | PF_X)))
6467
0
    _bfd_error_handler (_("warning: %pB has a LOAD segment"
6468
0
              " with RWX permissions"),
6469
0
            abfd);
6470
0
      }
6471
0
  }
6472
6473
36
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6474
36
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6475
0
  return false;
6476
36
    }
6477
6478
189
  return true;
6479
189
}
6480
6481
bool
6482
_bfd_elf_init_file_header (bfd *abfd,
6483
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6484
198
{
6485
198
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
6486
198
  struct elf_strtab_hash *shstrtab;
6487
198
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6488
6489
198
  i_ehdrp = elf_elfheader (abfd);
6490
6491
198
  shstrtab = _bfd_elf_strtab_init ();
6492
198
  if (shstrtab == NULL)
6493
0
    return false;
6494
6495
198
  elf_shstrtab (abfd) = shstrtab;
6496
6497
198
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6498
198
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6499
198
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6500
198
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6501
6502
198
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6503
198
  i_ehdrp->e_ident[EI_DATA] =
6504
198
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6505
198
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6506
6507
198
  if ((abfd->flags & DYNAMIC) != 0)
6508
50
    i_ehdrp->e_type = ET_DYN;
6509
148
  else if ((abfd->flags & EXEC_P) != 0)
6510
41
    i_ehdrp->e_type = ET_EXEC;
6511
107
  else if (bfd_get_format (abfd) == bfd_core)
6512
2
    i_ehdrp->e_type = ET_CORE;
6513
105
  else
6514
105
    i_ehdrp->e_type = ET_REL;
6515
6516
198
  switch (bfd_get_arch (abfd))
6517
198
    {
6518
0
    case bfd_arch_unknown:
6519
0
      i_ehdrp->e_machine = EM_NONE;
6520
0
      break;
6521
6522
      /* There used to be a long list of cases here, each one setting
6523
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6524
   in the corresponding bfd definition.  To avoid duplication,
6525
   the switch was removed.  Machines that need special handling
6526
   can generally do it in elf_backend_final_write_processing(),
6527
   unless they need the information earlier than the final write.
6528
   Such need can generally be supplied by replacing the tests for
6529
   e_machine with the conditions used to determine it.  */
6530
198
    default:
6531
198
      i_ehdrp->e_machine = bed->elf_machine_code;
6532
198
    }
6533
6534
198
  i_ehdrp->e_version = bed->s->ev_current;
6535
198
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6536
6537
  /* No program header, for now.  */
6538
198
  i_ehdrp->e_phoff = 0;
6539
198
  i_ehdrp->e_phentsize = 0;
6540
198
  i_ehdrp->e_phnum = 0;
6541
6542
  /* Each bfd section is section header entry.  */
6543
198
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
6544
198
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6545
6546
198
  elf_tdata (abfd)->symtab_hdr.sh_name =
6547
198
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6548
198
  elf_tdata (abfd)->strtab_hdr.sh_name =
6549
198
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6550
198
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
6551
198
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6552
198
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6553
198
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6554
198
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6555
0
    return false;
6556
6557
198
  return true;
6558
198
}
6559
6560
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6561
6562
   FIXME: We used to have code here to sort the PT_LOAD segments into
6563
   ascending order, as per the ELF spec.  But this breaks some programs,
6564
   including the Linux kernel.  But really either the spec should be
6565
   changed or the programs updated.  */
6566
6567
bool
6568
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6569
189
{
6570
189
  if (link_info != NULL && bfd_link_pie (link_info))
6571
0
    {
6572
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6573
0
      unsigned int num_segments = i_ehdrp->e_phnum;
6574
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
6575
0
      Elf_Internal_Phdr *segment = tdata->phdr;
6576
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
6577
6578
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
6579
0
      bfd_vma p_vaddr = (bfd_vma) -1;
6580
0
      for (; segment < end_segment; segment++)
6581
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6582
0
    p_vaddr = segment->p_vaddr;
6583
6584
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6585
   segments is non-zero.  */
6586
0
      if (p_vaddr)
6587
0
  i_ehdrp->e_type = ET_EXEC;
6588
0
    }
6589
189
  return true;
6590
189
}
6591
6592
/* Assign file positions for all the reloc sections which are not part
6593
   of the loadable file image, and the file position of section headers.  */
6594
6595
static bool
6596
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6597
188
{
6598
188
  file_ptr off;
6599
188
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6600
188
  Elf_Internal_Shdr *shdrp;
6601
188
  Elf_Internal_Ehdr *i_ehdrp;
6602
188
  const struct elf_backend_data *bed;
6603
6604
188
  off = elf_next_file_pos (abfd);
6605
6606
188
  shdrpp = elf_elfsections (abfd);
6607
188
  end_shdrpp = shdrpp + elf_numsections (abfd);
6608
6.20k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6609
6.01k
    {
6610
6.01k
      shdrp = *shdrpp;
6611
6.01k
      if (shdrp->sh_offset == -1)
6612
1.48k
  {
6613
1.48k
    asection *sec = shdrp->bfd_section;
6614
1.48k
    if (sec == NULL
6615
1.48k
        || shdrp->sh_type == SHT_REL
6616
1.48k
        || shdrp->sh_type == SHT_RELA)
6617
1.48k
      ;
6618
0
    else if (bfd_section_is_ctf (sec))
6619
0
      {
6620
        /* Update section size and contents.  */
6621
0
        shdrp->sh_size = sec->size;
6622
0
        shdrp->contents = sec->contents;
6623
0
      }
6624
0
    else if (shdrp->sh_name == -1u)
6625
0
      {
6626
0
        const char *name = sec->name;
6627
0
        struct bfd_elf_section_data *d;
6628
6629
        /* Compress DWARF debug sections.  */
6630
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
6631
0
    return false;
6632
6633
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
6634
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
6635
0
      && name[1] == 'd')
6636
0
    {
6637
      /* If section is compressed with zlib-gnu, convert
6638
         section name from .debug_* to .zdebug_*.  */
6639
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
6640
0
      if (new_name == NULL)
6641
0
        return false;
6642
0
      name = new_name;
6643
0
    }
6644
        /* Add section name to section name section.  */
6645
0
        shdrp->sh_name
6646
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6647
0
                  name, false);
6648
0
        d = elf_section_data (sec);
6649
6650
        /* Add reloc section name to section name section.  */
6651
0
        if (d->rel.hdr
6652
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6653
0
              name, false))
6654
0
    return false;
6655
0
        if (d->rela.hdr
6656
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6657
0
              name, true))
6658
0
    return false;
6659
6660
        /* Update section size and contents.  */
6661
0
        shdrp->sh_size = sec->size;
6662
0
        shdrp->contents = sec->contents;
6663
0
        sec->contents = NULL;
6664
0
      }
6665
6666
1.48k
    off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6667
1.48k
  }
6668
6.01k
    }
6669
6670
  /* Place section name section after DWARF debug sections have been
6671
     compressed.  */
6672
188
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6673
188
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6674
188
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6675
188
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6676
6677
  /* Place the section headers.  */
6678
188
  i_ehdrp = elf_elfheader (abfd);
6679
188
  bed = get_elf_backend_data (abfd);
6680
188
  off = align_file_position (off, 1 << bed->s->log_file_align);
6681
188
  i_ehdrp->e_shoff = off;
6682
188
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6683
188
  elf_next_file_pos (abfd) = off;
6684
6685
188
  return true;
6686
188
}
6687
6688
bool
6689
_bfd_elf_write_object_contents (bfd *abfd)
6690
189
{
6691
189
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6692
189
  Elf_Internal_Shdr **i_shdrp;
6693
189
  bool failed;
6694
189
  unsigned int count, num_sec;
6695
189
  struct elf_obj_tdata *t;
6696
6697
189
  if (! abfd->output_has_begun
6698
189
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6699
0
    return false;
6700
  /* Do not rewrite ELF data when the BFD has been opened for update.
6701
     abfd->output_has_begun was set to TRUE on opening, so creation of
6702
     new sections, and modification of existing section sizes was
6703
     restricted.  This means the ELF header, program headers and
6704
     section headers can't have changed.  If the contents of any
6705
     sections has been modified, then those changes have already been
6706
     written to the BFD.  */
6707
189
  else if (abfd->direction == both_direction)
6708
0
    {
6709
0
      BFD_ASSERT (abfd->output_has_begun);
6710
0
      return true;
6711
0
    }
6712
6713
189
  i_shdrp = elf_elfsections (abfd);
6714
6715
189
  failed = false;
6716
189
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6717
189
  if (failed)
6718
1
    return false;
6719
6720
188
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6721
0
    return false;
6722
6723
  /* After writing the headers, we need to write the sections too...  */
6724
188
  num_sec = elf_numsections (abfd);
6725
6.20k
  for (count = 1; count < num_sec; count++)
6726
6.01k
    {
6727
6.01k
      i_shdrp[count]->sh_name
6728
6.01k
  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6729
6.01k
          i_shdrp[count]->sh_name);
6730
6.01k
      if (bed->elf_backend_section_processing)
6731
429
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6732
0
    return false;
6733
6.01k
      if (i_shdrp[count]->contents)
6734
1.87k
  {
6735
1.87k
    bfd_size_type amt = i_shdrp[count]->sh_size;
6736
6737
1.87k
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6738
1.87k
        || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6739
0
      return false;
6740
1.87k
  }
6741
6.01k
    }
6742
6743
  /* Write out the section header names.  */
6744
188
  t = elf_tdata (abfd);
6745
188
  if (elf_shstrtab (abfd) != NULL
6746
188
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6747
188
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6748
0
    return false;
6749
6750
188
  if (!(*bed->elf_backend_final_write_processing) (abfd))
6751
0
    return false;
6752
6753
188
  if (!bed->s->write_shdrs_and_ehdr (abfd))
6754
0
    return false;
6755
6756
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
6757
188
  if (t->o->build_id.after_write_object_contents != NULL
6758
188
      && !(*t->o->build_id.after_write_object_contents) (abfd))
6759
0
    return false;
6760
188
  if (t->o->package_metadata.after_write_object_contents != NULL
6761
188
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6762
0
    return false;
6763
6764
188
  return true;
6765
188
}
6766
6767
bool
6768
_bfd_elf_write_corefile_contents (bfd *abfd)
6769
2
{
6770
  /* Hopefully this can be done just like an object file.  */
6771
2
  return _bfd_elf_write_object_contents (abfd);
6772
2
}
6773
6774
/* Given a section, search the header to find them.  */
6775
6776
unsigned int
6777
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6778
23.2k
{
6779
23.2k
  const struct elf_backend_data *bed;
6780
23.2k
  unsigned int sec_index;
6781
6782
23.2k
  if (elf_section_data (asect) != NULL
6783
23.2k
      && elf_section_data (asect)->this_idx != 0)
6784
10.1k
    return elf_section_data (asect)->this_idx;
6785
6786
13.1k
  if (bfd_is_abs_section (asect))
6787
0
    sec_index = SHN_ABS;
6788
13.1k
  else if (bfd_is_com_section (asect))
6789
2
    sec_index = SHN_COMMON;
6790
13.1k
  else if (bfd_is_und_section (asect))
6791
13.1k
    sec_index = SHN_UNDEF;
6792
1
  else
6793
1
    sec_index = SHN_BAD;
6794
6795
13.1k
  bed = get_elf_backend_data (abfd);
6796
13.1k
  if (bed->elf_backend_section_from_bfd_section)
6797
3.91k
    {
6798
3.91k
      int retval = sec_index;
6799
6800
3.91k
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6801
2
  return retval;
6802
3.91k
    }
6803
6804
13.1k
  if (sec_index == SHN_BAD)
6805
0
    bfd_set_error (bfd_error_nonrepresentable_section);
6806
6807
13.1k
  return sec_index;
6808
13.1k
}
6809
6810
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6811
   on error.  */
6812
6813
int
6814
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6815
36.8k
{
6816
36.8k
  asymbol *asym_ptr = *asym_ptr_ptr;
6817
36.8k
  int idx;
6818
36.8k
  flagword flags = asym_ptr->flags;
6819
6820
  /* When gas creates relocations against local labels, it creates its
6821
     own symbol for the section, but does put the symbol into the
6822
     symbol chain, so udata is 0.  When the linker is generating
6823
     relocatable output, this section symbol may be for one of the
6824
     input sections rather than the output section.  */
6825
36.8k
  if (asym_ptr->udata.i == 0
6826
36.8k
      && (flags & BSF_SECTION_SYM)
6827
36.8k
      && asym_ptr->section)
6828
1
    {
6829
1
      asection *sec;
6830
6831
1
      sec = asym_ptr->section;
6832
1
      if (sec->owner != abfd && sec->output_section != NULL)
6833
1
  sec = sec->output_section;
6834
1
      if (sec->owner == abfd
6835
1
    && sec->index < elf_num_section_syms (abfd)
6836
1
    && elf_section_syms (abfd)[sec->index] != NULL)
6837
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6838
1
    }
6839
6840
36.8k
  idx = asym_ptr->udata.i;
6841
6842
36.8k
  if (idx == 0)
6843
1
    {
6844
      /* This case can occur when using --strip-symbol on a symbol
6845
   which is used in a relocation entry.  */
6846
1
      _bfd_error_handler
6847
  /* xgettext:c-format */
6848
1
  (_("%pB: symbol `%s' required but not present"),
6849
1
   abfd, bfd_asymbol_name (asym_ptr));
6850
1
      bfd_set_error (bfd_error_no_symbols);
6851
1
      return -1;
6852
1
    }
6853
6854
#if DEBUG & 4
6855
  {
6856
    fprintf (stderr,
6857
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6858
       " flags = 0x%.8x\n",
6859
       (long) asym_ptr, asym_ptr->name, idx, flags);
6860
    fflush (stderr);
6861
  }
6862
#endif
6863
6864
36.8k
  return idx;
6865
36.8k
}
6866
6867
static inline bfd_vma
6868
segment_size (Elf_Internal_Phdr *segment)
6869
13.8k
{
6870
13.8k
  return (segment->p_memsz > segment->p_filesz
6871
13.8k
    ? segment->p_memsz : segment->p_filesz);
6872
13.8k
}
6873
6874
6875
/* Returns the end address of the segment + 1.  */
6876
static inline bfd_vma
6877
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6878
49
{
6879
49
  return start + segment_size (segment);
6880
49
}
6881
6882
static inline bfd_size_type
6883
section_size (asection *section, Elf_Internal_Phdr *segment)
6884
14.6k
{
6885
14.6k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
6886
14.6k
      || (section->flags & SEC_THREAD_LOCAL) == 0
6887
14.6k
      || segment->p_type == PT_TLS)
6888
14.6k
    return section->size;
6889
0
  return 0;
6890
14.6k
}
6891
6892
/* Returns TRUE if the given section is contained within the given
6893
   segment.  LMA addresses are compared against PADDR when
6894
   USE_VADDR is false, VMA against VADDR when true.  */
6895
static bool
6896
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6897
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6898
     bool use_vaddr)
6899
7.71k
{
6900
7.71k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
6901
7.71k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
6902
7.71k
  bfd_vma octet;
6903
7.71k
  if (_bfd_mul_overflow (addr, opb, &octet))
6904
0
    return false;
6905
  /* The third and fourth lines below are testing that the section end
6906
     address is within the segment.  It's written this way to avoid
6907
     overflow.  Add seg_addr + section_size to both sides of the
6908
     inequality to make it obvious.  */
6909
7.71k
  return (octet >= seg_addr
6910
7.71k
    && segment_size (segment) >= section_size (section, segment)
6911
7.71k
    && (octet - seg_addr
6912
6.12k
        <= segment_size (segment) - section_size (section, segment)));
6913
7.71k
}
6914
6915
/* Handle PT_NOTE segment.  */
6916
static bool
6917
is_note (asection *s, Elf_Internal_Phdr *p)
6918
4.19k
{
6919
4.19k
  return (p->p_type == PT_NOTE
6920
4.19k
    && elf_section_type (s) == SHT_NOTE
6921
4.19k
    && (ufile_ptr) s->filepos >= p->p_offset
6922
4.19k
    && p->p_filesz >= s->size
6923
4.19k
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
6924
4.19k
}
6925
6926
/* Rewrite program header information.  */
6927
6928
static bool
6929
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6930
33
{
6931
33
  Elf_Internal_Ehdr *iehdr;
6932
33
  struct elf_segment_map *map;
6933
33
  struct elf_segment_map *map_first;
6934
33
  struct elf_segment_map **pointer_to_map;
6935
33
  Elf_Internal_Phdr *segment;
6936
33
  asection *section;
6937
33
  unsigned int i;
6938
33
  unsigned int num_segments;
6939
33
  bool phdr_included = false;
6940
33
  bool p_paddr_valid;
6941
33
  struct elf_segment_map *phdr_adjust_seg = NULL;
6942
33
  unsigned int phdr_adjust_num = 0;
6943
33
  const struct elf_backend_data *bed;
6944
33
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6945
6946
33
  bed = get_elf_backend_data (ibfd);
6947
33
  iehdr = elf_elfheader (ibfd);
6948
6949
33
  map_first = NULL;
6950
33
  pointer_to_map = &map_first;
6951
6952
33
  num_segments = elf_elfheader (ibfd)->e_phnum;
6953
6954
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
6955
     linker, which generates a PT_INTERP section with p_vaddr and
6956
     p_memsz set to 0.  */
6957
33
#define IS_SOLARIS_PT_INTERP(p, s)          \
6958
572
  (p->p_vaddr == 0              \
6959
572
   && p->p_paddr == 0              \
6960
572
   && p->p_memsz == 0              \
6961
572
   && p->p_filesz > 0              \
6962
572
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
6963
572
   && s->size > 0              \
6964
572
   && (bfd_vma) s->filepos >= p->p_offset        \
6965
572
   && ((bfd_vma) s->filepos + s->size         \
6966
15
       <= p->p_offset + p->p_filesz))
6967
6968
  /* Decide if the given section should be included in the given segment.
6969
     A section will be included if:
6970
       1. It is within the address space of the segment -- we use the LMA
6971
    if that is set for the segment and the VMA otherwise,
6972
       2. It is an allocated section or a NOTE section in a PT_NOTE
6973
    segment.
6974
       3. There is an output section associated with it,
6975
       4. The section has not already been allocated to a previous segment.
6976
       5. PT_GNU_STACK segments do not include any sections.
6977
       6. PT_TLS segment includes only SHF_TLS sections.
6978
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6979
       8. PT_DYNAMIC should not contain empty sections at the beginning
6980
    (with the possible exception of .dynamic).  */
6981
33
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)   \
6982
7.73k
  (((is_contained_by (section, segment, segment->p_paddr,    \
6983
6.53k
          segment->p_vaddr, opb,        \
6984
6.53k
          bed->want_p_paddr_set_to_zero)      \
6985
6.53k
     && (section->flags & SEC_ALLOC) != 0)       \
6986
6.53k
    || is_note (section, segment))         \
6987
6.53k
   && segment->p_type != PT_GNU_STACK          \
6988
6.53k
   && (segment->p_type != PT_TLS          \
6989
2.34k
       || (section->flags & SEC_THREAD_LOCAL))        \
6990
6.53k
   && (segment->p_type == PT_LOAD          \
6991
2.34k
       || segment->p_type == PT_TLS          \
6992
2.34k
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
6993
6.53k
   && (segment->p_type != PT_DYNAMIC          \
6994
2.34k
       || section_size (section, segment) > 0        \
6995
2.34k
       || (segment->p_paddr            \
6996
0
     ? segment->p_paddr != section->lma * (opb)      \
6997
0
     : segment->p_vaddr != section->vma * (opb))      \
6998
2.34k
       || (strcmp (bfd_section_name (section), ".dynamic") == 0))  \
6999
7.73k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7000
7001
/* If the output section of a section in the input segment is NULL,
7002
   it is removed from the corresponding output segment.   */
7003
33
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)    \
7004
1.20k
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)   \
7005
1.20k
   && section->output_section != NULL)
7006
7007
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7008
33
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7009
81
  (seg1->field >= segment_end (seg2, seg2->field))
7010
7011
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7012
     their VMA address ranges and their LMA address ranges overlap.
7013
     It is possible to have overlapping VMA ranges without overlapping LMA
7014
     ranges.  RedBoot images for example can have both .data and .bss mapped
7015
     to the same VMA range, but with the .data section mapped to a different
7016
     LMA.  */
7017
33
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7018
33
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7019
32
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7020
32
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7021
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7022
7023
  /* Initialise the segment mark field, and discard stupid alignment.  */
7024
1.10k
  for (section = ibfd->sections; section != NULL; section = section->next)
7025
1.06k
    {
7026
1.06k
      asection *o = section->output_section;
7027
1.06k
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7028
0
  o->alignment_power = 0;
7029
1.06k
      section->segment_mark = false;
7030
1.06k
    }
7031
7032
  /* The Solaris linker creates program headers in which all the
7033
     p_paddr fields are zero.  When we try to objcopy or strip such a
7034
     file, we get confused.  Check for this case, and if we find it
7035
     don't set the p_paddr_valid fields.  */
7036
33
  p_paddr_valid = false;
7037
33
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7038
178
       i < num_segments;
7039
145
       i++, segment++)
7040
149
    if (segment->p_paddr != 0)
7041
4
      {
7042
4
  p_paddr_valid = true;
7043
4
  break;
7044
4
      }
7045
7046
  /* Scan through the segments specified in the program header
7047
     of the input BFD.  For this first scan we look for overlaps
7048
     in the loadable segments.  These can be created by weird
7049
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7050
33
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7051
195
       i < num_segments;
7052
162
       i++, segment++)
7053
162
    {
7054
162
      unsigned int j;
7055
162
      Elf_Internal_Phdr *segment2;
7056
7057
162
      if (segment->p_type == PT_INTERP)
7058
589
  for (section = ibfd->sections; section; section = section->next)
7059
572
    if (IS_SOLARIS_PT_INTERP (segment, section))
7060
15
      {
7061
        /* Mininal change so that the normal section to segment
7062
     assignment code will work.  */
7063
15
        segment->p_vaddr = section->vma * opb;
7064
15
        break;
7065
15
      }
7066
7067
162
      if (segment->p_type != PT_LOAD)
7068
97
  {
7069
    /* Remove PT_GNU_RELRO segment.  */
7070
97
    if (segment->p_type == PT_GNU_RELRO)
7071
0
      segment->p_type = PT_NULL;
7072
97
    continue;
7073
97
  }
7074
7075
      /* Determine if this segment overlaps any previous segments.  */
7076
226
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7077
161
  {
7078
161
    bfd_signed_vma extra_length;
7079
7080
161
    if (segment2->p_type != PT_LOAD
7081
161
        || !SEGMENT_OVERLAPS (segment, segment2))
7082
161
      continue;
7083
7084
    /* Merge the two segments together.  */
7085
0
    if (segment2->p_vaddr < segment->p_vaddr)
7086
0
      {
7087
        /* Extend SEGMENT2 to include SEGMENT and then delete
7088
     SEGMENT.  */
7089
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7090
0
            - segment_end (segment2, segment2->p_vaddr));
7091
7092
0
        if (extra_length > 0)
7093
0
    {
7094
0
      segment2->p_memsz += extra_length;
7095
0
      segment2->p_filesz += extra_length;
7096
0
    }
7097
7098
0
        segment->p_type = PT_NULL;
7099
7100
        /* Since we have deleted P we must restart the outer loop.  */
7101
0
        i = 0;
7102
0
        segment = elf_tdata (ibfd)->phdr;
7103
0
        break;
7104
0
      }
7105
0
    else
7106
0
      {
7107
        /* Extend SEGMENT to include SEGMENT2 and then delete
7108
     SEGMENT2.  */
7109
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7110
0
            - segment_end (segment, segment->p_vaddr));
7111
7112
0
        if (extra_length > 0)
7113
0
    {
7114
0
      segment->p_memsz += extra_length;
7115
0
      segment->p_filesz += extra_length;
7116
0
    }
7117
7118
0
        segment2->p_type = PT_NULL;
7119
0
      }
7120
0
  }
7121
65
    }
7122
7123
  /* The second scan attempts to assign sections to segments.  */
7124
33
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7125
195
       i < num_segments;
7126
162
       i++, segment++)
7127
162
    {
7128
162
      unsigned int section_count;
7129
162
      asection **sections;
7130
162
      asection *output_section;
7131
162
      unsigned int isec;
7132
162
      asection *matching_lma;
7133
162
      asection *suggested_lma;
7134
162
      unsigned int j;
7135
162
      size_t amt;
7136
162
      asection *first_section;
7137
7138
162
      if (segment->p_type == PT_NULL)
7139
1
  continue;
7140
7141
161
      first_section = NULL;
7142
      /* Compute how many sections might be placed into this segment.  */
7143
161
      for (section = ibfd->sections, section_count = 0;
7144
5.49k
     section != NULL;
7145
5.33k
     section = section->next)
7146
5.33k
  {
7147
    /* Find the first section in the input segment, which may be
7148
       removed from the corresponding output segment.   */
7149
5.33k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7150
1.17k
      {
7151
1.17k
        if (first_section == NULL)
7152
72
    first_section = section;
7153
1.17k
        if (section->output_section != NULL)
7154
1.17k
    ++section_count;
7155
1.17k
      }
7156
5.33k
  }
7157
7158
      /* Allocate a segment map big enough to contain
7159
   all of the sections we have selected.  */
7160
161
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7161
161
      amt += section_count * sizeof (asection *);
7162
161
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7163
161
      if (map == NULL)
7164
0
  return false;
7165
7166
      /* Initialise the fields of the segment map.  Default to
7167
   using the physical address of the segment in the input BFD.  */
7168
161
      map->next = NULL;
7169
161
      map->p_type = segment->p_type;
7170
161
      map->p_flags = segment->p_flags;
7171
161
      map->p_flags_valid = 1;
7172
7173
161
      if (map->p_type == PT_LOAD
7174
161
    && (ibfd->flags & D_PAGED) != 0
7175
161
    && maxpagesize > 1
7176
161
    && segment->p_align > 1)
7177
64
  {
7178
64
    map->p_align = segment->p_align;
7179
64
    if (segment->p_align > maxpagesize)
7180
0
      map->p_align = maxpagesize;
7181
64
    map->p_align_valid = 1;
7182
64
  }
7183
7184
      /* If the first section in the input segment is removed, there is
7185
   no need to preserve segment physical address in the corresponding
7186
   output segment.  */
7187
161
      if (!first_section || first_section->output_section != NULL)
7188
161
  {
7189
161
    map->p_paddr = segment->p_paddr;
7190
161
    map->p_paddr_valid = p_paddr_valid;
7191
161
  }
7192
7193
      /* Determine if this segment contains the ELF file header
7194
   and if it contains the program headers themselves.  */
7195
161
      map->includes_filehdr = (segment->p_offset == 0
7196
161
             && segment->p_filesz >= iehdr->e_ehsize);
7197
161
      map->includes_phdrs = 0;
7198
7199
161
      if (!phdr_included || segment->p_type != PT_LOAD)
7200
129
  {
7201
129
    map->includes_phdrs =
7202
129
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7203
129
       && (segment->p_offset + segment->p_filesz
7204
65
     >= ((bfd_vma) iehdr->e_phoff
7205
65
         + iehdr->e_phnum * iehdr->e_phentsize)));
7206
7207
129
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7208
32
      phdr_included = true;
7209
129
  }
7210
7211
161
      if (section_count == 0)
7212
89
  {
7213
    /* Special segments, such as the PT_PHDR segment, may contain
7214
       no sections, but ordinary, loadable segments should contain
7215
       something.  They are allowed by the ELF spec however, so only
7216
       a warning is produced.
7217
       Don't warn if an empty PT_LOAD contains the program headers.
7218
       There is however the valid use case of embedded systems which
7219
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7220
       flash memory with zeros.  No warning is shown for that case.  */
7221
89
    if (segment->p_type == PT_LOAD
7222
89
        && !map->includes_phdrs
7223
89
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7224
      /* xgettext:c-format */
7225
29
      _bfd_error_handler
7226
29
        (_("%pB: warning: empty loadable segment detected"
7227
29
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7228
29
         ibfd, (uint64_t) segment->p_vaddr);
7229
7230
89
    map->p_vaddr_offset = segment->p_vaddr / opb;
7231
89
    map->count = 0;
7232
89
    *pointer_to_map = map;
7233
89
    pointer_to_map = &map->next;
7234
7235
89
    continue;
7236
89
  }
7237
7238
      /* Now scan the sections in the input BFD again and attempt
7239
   to add their corresponding output sections to the segment map.
7240
   The problem here is how to handle an output section which has
7241
   been moved (ie had its LMA changed).  There are four possibilities:
7242
7243
   1. None of the sections have been moved.
7244
      In this case we can continue to use the segment LMA from the
7245
      input BFD.
7246
7247
   2. All of the sections have been moved by the same amount.
7248
      In this case we can change the segment's LMA to match the LMA
7249
      of the first section.
7250
7251
   3. Some of the sections have been moved, others have not.
7252
      In this case those sections which have not been moved can be
7253
      placed in the current segment which will have to have its size,
7254
      and possibly its LMA changed, and a new segment or segments will
7255
      have to be created to contain the other sections.
7256
7257
   4. The sections have been moved, but not by the same amount.
7258
      In this case we can change the segment's LMA to match the LMA
7259
      of the first section and we will have to create a new segment
7260
      or segments to contain the other sections.
7261
7262
   In order to save time, we allocate an array to hold the section
7263
   pointers that we are interested in.  As these sections get assigned
7264
   to a segment, they are removed from this array.  */
7265
7266
72
      amt = section_count * sizeof (asection *);
7267
72
      sections = (asection **) bfd_malloc (amt);
7268
72
      if (sections == NULL)
7269
0
  return false;
7270
7271
      /* Step One: Scan for segment vs section LMA conflicts.
7272
   Also add the sections to the section array allocated above.
7273
   Also add the sections to the current segment.  In the common
7274
   case, where the sections have not been moved, this means that
7275
   we have completely filled the segment, and there is nothing
7276
   more to do.  */
7277
72
      isec = 0;
7278
72
      matching_lma = NULL;
7279
72
      suggested_lma = NULL;
7280
7281
72
      for (section = first_section, j = 0;
7282
1.20k
     section != NULL;
7283
1.12k
     section = section->next)
7284
1.20k
  {
7285
1.20k
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7286
1.17k
      {
7287
1.17k
        output_section = section->output_section;
7288
7289
1.17k
        sections[j++] = section;
7290
7291
        /* The Solaris native linker always sets p_paddr to 0.
7292
     We try to catch that case here, and set it to the
7293
     correct value.  Note - some backends require that
7294
     p_paddr be left as zero.  */
7295
1.17k
        if (!p_paddr_valid
7296
1.17k
      && segment->p_vaddr != 0
7297
1.17k
      && !bed->want_p_paddr_set_to_zero
7298
1.17k
      && isec == 0
7299
1.17k
      && output_section->lma != 0
7300
1.17k
      && (align_power (segment->p_vaddr
7301
59
           + (map->includes_filehdr
7302
59
              ? iehdr->e_ehsize : 0)
7303
59
           + (map->includes_phdrs
7304
59
              ? iehdr->e_phnum * iehdr->e_phentsize
7305
59
              : 0),
7306
59
           output_section->alignment_power * opb)
7307
59
          == (output_section->vma * opb)))
7308
14
    map->p_paddr = segment->p_vaddr;
7309
7310
        /* Match up the physical address of the segment with the
7311
     LMA address of the output section.  */
7312
1.17k
        if (is_contained_by (output_section, segment, map->p_paddr,
7313
1.17k
           0, opb, false)
7314
1.17k
      || is_note (section, segment))
7315
1.17k
    {
7316
1.17k
      if (matching_lma == NULL
7317
1.17k
          || output_section->lma < matching_lma->lma)
7318
80
        matching_lma = output_section;
7319
7320
      /* We assume that if the section fits within the segment
7321
         then it does not overlap any other section within that
7322
         segment.  */
7323
1.17k
      map->sections[isec++] = output_section;
7324
1.17k
    }
7325
0
        else if (suggested_lma == NULL)
7326
0
    suggested_lma = output_section;
7327
7328
1.17k
        if (j == section_count)
7329
72
    break;
7330
1.17k
      }
7331
1.20k
  }
7332
7333
72
      BFD_ASSERT (j == section_count);
7334
7335
      /* Step Two: Adjust the physical address of the current segment,
7336
   if necessary.  */
7337
72
      if (isec == section_count)
7338
72
  {
7339
    /* All of the sections fitted within the segment as currently
7340
       specified.  This is the default case.  Add the segment to
7341
       the list of built segments and carry on to process the next
7342
       program header in the input BFD.  */
7343
72
    map->count = section_count;
7344
72
    *pointer_to_map = map;
7345
72
    pointer_to_map = &map->next;
7346
7347
72
    if (p_paddr_valid
7348
72
        && !bed->want_p_paddr_set_to_zero)
7349
11
      {
7350
11
        bfd_vma hdr_size = 0;
7351
11
        if (map->includes_filehdr)
7352
3
    hdr_size = iehdr->e_ehsize;
7353
11
        if (map->includes_phdrs)
7354
3
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7355
7356
        /* Account for padding before the first section in the
7357
     segment.  */
7358
11
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7359
11
             - matching_lma->lma);
7360
11
      }
7361
7362
72
    free (sections);
7363
72
    continue;
7364
72
  }
7365
0
      else
7366
0
  {
7367
    /* Change the current segment's physical address to match
7368
       the LMA of the first section that fitted, or if no
7369
       section fitted, the first section.  */
7370
0
    if (matching_lma == NULL)
7371
0
      matching_lma = suggested_lma;
7372
7373
0
    map->p_paddr = matching_lma->lma * opb;
7374
7375
    /* Offset the segment physical address from the lma
7376
       to allow for space taken up by elf headers.  */
7377
0
    if (map->includes_phdrs)
7378
0
      {
7379
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7380
7381
        /* iehdr->e_phnum is just an estimate of the number
7382
     of program headers that we will need.  Make a note
7383
     here of the number we used and the segment we chose
7384
     to hold these headers, so that we can adjust the
7385
     offset when we know the correct value.  */
7386
0
        phdr_adjust_num = iehdr->e_phnum;
7387
0
        phdr_adjust_seg = map;
7388
0
      }
7389
7390
0
    if (map->includes_filehdr)
7391
0
      {
7392
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7393
0
        map->p_paddr -= iehdr->e_ehsize;
7394
        /* We've subtracted off the size of headers from the
7395
     first section lma, but there may have been some
7396
     alignment padding before that section too.  Try to
7397
     account for that by adjusting the segment lma down to
7398
     the same alignment.  */
7399
0
        if (segment->p_align != 0 && segment->p_align < align)
7400
0
    align = segment->p_align;
7401
0
        map->p_paddr &= -(align * opb);
7402
0
      }
7403
0
  }
7404
7405
      /* Step Three: Loop over the sections again, this time assigning
7406
   those that fit to the current segment and removing them from the
7407
   sections array; but making sure not to leave large gaps.  Once all
7408
   possible sections have been assigned to the current segment it is
7409
   added to the list of built segments and if sections still remain
7410
   to be assigned, a new segment is constructed before repeating
7411
   the loop.  */
7412
0
      isec = 0;
7413
0
      do
7414
0
  {
7415
0
    map->count = 0;
7416
0
    suggested_lma = NULL;
7417
7418
    /* Fill the current segment with sections that fit.  */
7419
0
    for (j = 0; j < section_count; j++)
7420
0
      {
7421
0
        section = sections[j];
7422
7423
0
        if (section == NULL)
7424
0
    continue;
7425
7426
0
        output_section = section->output_section;
7427
7428
0
        BFD_ASSERT (output_section != NULL);
7429
7430
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7431
0
           0, opb, false)
7432
0
      || is_note (section, segment))
7433
0
    {
7434
0
      if (map->count == 0)
7435
0
        {
7436
          /* If the first section in a segment does not start at
7437
       the beginning of the segment, then something is
7438
       wrong.  */
7439
0
          if (align_power (map->p_paddr
7440
0
               + (map->includes_filehdr
7441
0
            ? iehdr->e_ehsize : 0)
7442
0
               + (map->includes_phdrs
7443
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7444
0
            : 0),
7445
0
               output_section->alignment_power * opb)
7446
0
        != output_section->lma * opb)
7447
0
      goto sorry;
7448
0
        }
7449
0
      else
7450
0
        {
7451
0
          asection *prev_sec;
7452
7453
0
          prev_sec = map->sections[map->count - 1];
7454
7455
          /* If the gap between the end of the previous section
7456
       and the start of this section is more than
7457
       maxpagesize then we need to start a new segment.  */
7458
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7459
0
              maxpagesize)
7460
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7461
0
        || (prev_sec->lma + prev_sec->size
7462
0
            > output_section->lma))
7463
0
      {
7464
0
        if (suggested_lma == NULL)
7465
0
          suggested_lma = output_section;
7466
7467
0
        continue;
7468
0
      }
7469
0
        }
7470
7471
0
      map->sections[map->count++] = output_section;
7472
0
      ++isec;
7473
0
      sections[j] = NULL;
7474
0
      if (segment->p_type == PT_LOAD)
7475
0
        section->segment_mark = true;
7476
0
    }
7477
0
        else if (suggested_lma == NULL)
7478
0
    suggested_lma = output_section;
7479
0
      }
7480
7481
    /* PR 23932.  A corrupt input file may contain sections that cannot
7482
       be assigned to any segment - because for example they have a
7483
       negative size - or segments that do not contain any sections.
7484
       But there are also valid reasons why a segment can be empty.
7485
       So allow a count of zero.  */
7486
7487
    /* Add the current segment to the list of built segments.  */
7488
0
    *pointer_to_map = map;
7489
0
    pointer_to_map = &map->next;
7490
7491
0
    if (isec < section_count)
7492
0
      {
7493
        /* We still have not allocated all of the sections to
7494
     segments.  Create a new segment here, initialise it
7495
     and carry on looping.  */
7496
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7497
0
        amt += section_count * sizeof (asection *);
7498
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7499
0
        if (map == NULL)
7500
0
    {
7501
0
      free (sections);
7502
0
      return false;
7503
0
    }
7504
7505
        /* Initialise the fields of the segment map.  Set the physical
7506
     physical address to the LMA of the first section that has
7507
     not yet been assigned.  */
7508
0
        map->next = NULL;
7509
0
        map->p_type = segment->p_type;
7510
0
        map->p_flags = segment->p_flags;
7511
0
        map->p_flags_valid = 1;
7512
0
        map->p_paddr = suggested_lma->lma * opb;
7513
0
        map->p_paddr_valid = p_paddr_valid;
7514
0
        map->includes_filehdr = 0;
7515
0
        map->includes_phdrs = 0;
7516
0
      }
7517
7518
0
    continue;
7519
0
  sorry:
7520
0
    bfd_set_error (bfd_error_sorry);
7521
0
    free (sections);
7522
0
    return false;
7523
0
  }
7524
0
      while (isec < section_count);
7525
7526
0
      free (sections);
7527
0
    }
7528
7529
33
  elf_seg_map (obfd) = map_first;
7530
7531
  /* If we had to estimate the number of program headers that were
7532
     going to be needed, then check our estimate now and adjust
7533
     the offset if necessary.  */
7534
33
  if (phdr_adjust_seg != NULL)
7535
0
    {
7536
0
      unsigned int count;
7537
7538
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
7539
0
  count++;
7540
7541
0
      if (count > phdr_adjust_num)
7542
0
  phdr_adjust_seg->p_paddr
7543
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7544
7545
0
      for (map = map_first; map != NULL; map = map->next)
7546
0
  if (map->p_type == PT_PHDR)
7547
0
    {
7548
0
      bfd_vma adjust
7549
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7550
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7551
0
      break;
7552
0
    }
7553
0
    }
7554
7555
33
#undef IS_SOLARIS_PT_INTERP
7556
33
#undef IS_SECTION_IN_INPUT_SEGMENT
7557
33
#undef INCLUDE_SECTION_IN_SEGMENT
7558
33
#undef SEGMENT_AFTER_SEGMENT
7559
33
#undef SEGMENT_OVERLAPS
7560
33
  return true;
7561
33
}
7562
7563
/* Return true if p_align in the ELF program header in ABFD is valid.  */
7564
7565
static bool
7566
elf_is_p_align_valid (bfd *abfd)
7567
36
{
7568
36
  unsigned int i;
7569
36
  Elf_Internal_Phdr *segment;
7570
36
  unsigned int num_segments;
7571
36
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7572
36
  bfd_size_type maxpagesize = bed->maxpagesize;
7573
36
  bfd_size_type p_align = bed->p_align;
7574
7575
  /* Return true if the default p_align value isn't set or the maximum
7576
     page size is the same as the minimum page size.  */
7577
36
  if (p_align == 0 || maxpagesize == bed->minpagesize)
7578
36
    return true;
7579
7580
  /* When the default p_align value is set, p_align may be set to the
7581
     default p_align value while segments are aligned to the maximum
7582
     page size.  In this case, the input p_align will be ignored and
7583
     the maximum page size will be used to align the output segments.  */
7584
0
  segment = elf_tdata (abfd)->phdr;
7585
0
  num_segments = elf_elfheader (abfd)->e_phnum;
7586
0
  for (i = 0; i < num_segments; i++, segment++)
7587
0
    if (segment->p_type == PT_LOAD
7588
0
  && (segment->p_align != p_align
7589
0
      || vma_page_aligned_bias (segment->p_vaddr,
7590
0
              segment->p_offset,
7591
0
              maxpagesize) != 0))
7592
0
      return true;
7593
7594
0
  return false;
7595
0
}
7596
7597
/* Copy ELF program header information.  */
7598
7599
static bool
7600
copy_elf_program_header (bfd *ibfd, bfd *obfd)
7601
36
{
7602
36
  Elf_Internal_Ehdr *iehdr;
7603
36
  struct elf_segment_map *map;
7604
36
  struct elf_segment_map *map_first;
7605
36
  struct elf_segment_map **pointer_to_map;
7606
36
  Elf_Internal_Phdr *segment;
7607
36
  unsigned int i;
7608
36
  unsigned int num_segments;
7609
36
  bool phdr_included = false;
7610
36
  bool p_paddr_valid;
7611
36
  bool p_palign_valid;
7612
36
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7613
7614
36
  iehdr = elf_elfheader (ibfd);
7615
7616
36
  map_first = NULL;
7617
36
  pointer_to_map = &map_first;
7618
7619
  /* If all the segment p_paddr fields are zero, don't set
7620
     map->p_paddr_valid.  */
7621
36
  p_paddr_valid = false;
7622
36
  num_segments = elf_elfheader (ibfd)->e_phnum;
7623
36
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7624
52
       i < num_segments;
7625
36
       i++, segment++)
7626
49
    if (segment->p_paddr != 0)
7627
33
      {
7628
33
  p_paddr_valid = true;
7629
33
  break;
7630
33
      }
7631
7632
36
  p_palign_valid = elf_is_p_align_valid (ibfd);
7633
7634
36
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7635
378
       i < num_segments;
7636
342
       i++, segment++)
7637
342
    {
7638
342
      asection *section;
7639
342
      unsigned int section_count;
7640
342
      size_t amt;
7641
342
      Elf_Internal_Shdr *this_hdr;
7642
342
      asection *first_section = NULL;
7643
342
      asection *lowest_section;
7644
7645
      /* Compute how many sections are in this segment.  */
7646
342
      for (section = ibfd->sections, section_count = 0;
7647
11.3k
     section != NULL;
7648
11.0k
     section = section->next)
7649
11.0k
  {
7650
11.0k
    this_hdr = &(elf_section_data(section)->this_hdr);
7651
11.0k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7652
1.09k
      {
7653
1.09k
        if (first_section == NULL)
7654
245
    first_section = section;
7655
1.09k
        section_count++;
7656
1.09k
      }
7657
11.0k
  }
7658
7659
      /* Allocate a segment map big enough to contain
7660
   all of the sections we have selected.  */
7661
342
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7662
342
      amt += section_count * sizeof (asection *);
7663
342
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7664
342
      if (map == NULL)
7665
0
  return false;
7666
7667
      /* Initialize the fields of the output segment map with the
7668
   input segment.  */
7669
342
      map->next = NULL;
7670
342
      map->p_type = segment->p_type;
7671
342
      map->p_flags = segment->p_flags;
7672
342
      map->p_flags_valid = 1;
7673
342
      map->p_paddr = segment->p_paddr;
7674
342
      map->p_paddr_valid = p_paddr_valid;
7675
342
      map->p_align = segment->p_align;
7676
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
7677
342
      map->p_align_valid = (map->p_type == PT_GNU_STACK
7678
342
          || p_palign_valid);
7679
342
      map->p_vaddr_offset = 0;
7680
7681
342
      if (map->p_type == PT_GNU_RELRO
7682
342
    || map->p_type == PT_GNU_STACK)
7683
44
  {
7684
    /* The PT_GNU_RELRO segment may contain the first a few
7685
       bytes in the .got.plt section even if the whole .got.plt
7686
       section isn't in the PT_GNU_RELRO segment.  We won't
7687
       change the size of the PT_GNU_RELRO segment.
7688
       Similarly, PT_GNU_STACK size is significant on uclinux
7689
       systems.    */
7690
44
    map->p_size = segment->p_memsz;
7691
44
    map->p_size_valid = 1;
7692
44
  }
7693
7694
      /* Determine if this segment contains the ELF file header
7695
   and if it contains the program headers themselves.  */
7696
342
      map->includes_filehdr = (segment->p_offset == 0
7697
342
             && segment->p_filesz >= iehdr->e_ehsize);
7698
7699
342
      map->includes_phdrs = 0;
7700
342
      if (! phdr_included || segment->p_type != PT_LOAD)
7701
278
  {
7702
278
    map->includes_phdrs =
7703
278
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7704
278
       && (segment->p_offset + segment->p_filesz
7705
91
     >= ((bfd_vma) iehdr->e_phoff
7706
91
         + iehdr->e_phnum * iehdr->e_phentsize)));
7707
7708
278
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7709
33
      phdr_included = true;
7710
278
  }
7711
7712
342
      lowest_section = NULL;
7713
342
      if (section_count != 0)
7714
245
  {
7715
245
    unsigned int isec = 0;
7716
7717
245
    for (section = first_section;
7718
1.13k
         section != NULL;
7719
885
         section = section->next)
7720
1.13k
      {
7721
1.13k
        this_hdr = &(elf_section_data(section)->this_hdr);
7722
1.13k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7723
1.09k
    {
7724
1.09k
      map->sections[isec++] = section->output_section;
7725
1.09k
      if ((section->flags & SEC_ALLOC) != 0)
7726
1.06k
        {
7727
1.06k
          bfd_vma seg_off;
7728
7729
1.06k
          if (lowest_section == NULL
7730
1.06k
        || section->lma < lowest_section->lma)
7731
244
      lowest_section = section;
7732
7733
          /* Section lmas are set up from PT_LOAD header
7734
       p_paddr in _bfd_elf_make_section_from_shdr.
7735
       If this header has a p_paddr that disagrees
7736
       with the section lma, flag the p_paddr as
7737
       invalid.  */
7738
1.06k
          if ((section->flags & SEC_LOAD) != 0)
7739
1.01k
      seg_off = this_hdr->sh_offset - segment->p_offset;
7740
45
          else
7741
45
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
7742
1.06k
          if (section->lma * opb - segment->p_paddr != seg_off)
7743
61
      map->p_paddr_valid = false;
7744
1.06k
        }
7745
1.09k
      if (isec == section_count)
7746
245
        break;
7747
1.09k
    }
7748
1.13k
      }
7749
245
  }
7750
7751
342
      if (section_count == 0)
7752
97
  map->p_vaddr_offset = segment->p_vaddr / opb;
7753
245
      else if (map->p_paddr_valid)
7754
237
  {
7755
    /* Account for padding before the first section in the segment.  */
7756
237
    bfd_vma hdr_size = 0;
7757
237
    if (map->includes_filehdr)
7758
31
      hdr_size = iehdr->e_ehsize;
7759
237
    if (map->includes_phdrs)
7760
31
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7761
7762
237
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7763
237
         - (lowest_section ? lowest_section->lma : 0));
7764
237
  }
7765
7766
342
      map->count = section_count;
7767
342
      *pointer_to_map = map;
7768
342
      pointer_to_map = &map->next;
7769
342
    }
7770
7771
36
  elf_seg_map (obfd) = map_first;
7772
36
  return true;
7773
36
}
7774
7775
/* Copy private BFD data.  This copies or rewrites ELF program header
7776
   information.  */
7777
7778
static bool
7779
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7780
69
{
7781
69
  bfd_vma maxpagesize;
7782
7783
69
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7784
69
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7785
0
    return true;
7786
7787
69
  if (elf_tdata (ibfd)->phdr == NULL)
7788
0
    return true;
7789
7790
69
  if (ibfd->xvec == obfd->xvec)
7791
69
    {
7792
      /* Check to see if any sections in the input BFD
7793
   covered by ELF program header have changed.  */
7794
69
      Elf_Internal_Phdr *segment;
7795
69
      asection *section, *osec;
7796
69
      unsigned int i, num_segments;
7797
69
      Elf_Internal_Shdr *this_hdr;
7798
69
      const struct elf_backend_data *bed;
7799
7800
69
      bed = get_elf_backend_data (ibfd);
7801
7802
      /* Regenerate the segment map if p_paddr is set to 0.  */
7803
69
      if (bed->want_p_paddr_set_to_zero)
7804
1
  goto rewrite;
7805
7806
      /* Initialize the segment mark field.  */
7807
2.22k
      for (section = obfd->sections; section != NULL;
7808
2.16k
     section = section->next)
7809
2.16k
  section->segment_mark = false;
7810
7811
68
      num_segments = elf_elfheader (ibfd)->e_phnum;
7812
68
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
7813
442
     i < num_segments;
7814
374
     i++, segment++)
7815
406
  {
7816
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
7817
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7818
       which severly confuses things, so always regenerate the segment
7819
       map in this case.  */
7820
406
    if (segment->p_paddr == 0
7821
406
        && segment->p_memsz == 0
7822
406
        && (segment->p_type == PT_INTERP
7823
56
      || segment->p_type == PT_DYNAMIC))
7824
32
      goto rewrite;
7825
7826
374
    for (section = ibfd->sections;
7827
12.4k
         section != NULL; section = section->next)
7828
12.1k
      {
7829
        /* We mark the output section so that we know it comes
7830
     from the input BFD.  */
7831
12.1k
        osec = section->output_section;
7832
12.1k
        if (osec)
7833
12.1k
    osec->segment_mark = true;
7834
7835
        /* Check if this section is covered by the segment.  */
7836
12.1k
        this_hdr = &(elf_section_data(section)->this_hdr);
7837
12.1k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7838
1.09k
    {
7839
      /* FIXME: Check if its output section is changed or
7840
         removed.  What else do we need to check?  */
7841
1.09k
      if (osec == NULL
7842
1.09k
          || section->flags != osec->flags
7843
1.09k
          || section->lma != osec->lma
7844
1.09k
          || section->vma != osec->vma
7845
1.09k
          || section->size != osec->size
7846
1.09k
          || section->rawsize != osec->rawsize
7847
1.09k
          || section->alignment_power != osec->alignment_power)
7848
0
        goto rewrite;
7849
1.09k
    }
7850
12.1k
      }
7851
374
  }
7852
7853
      /* Check to see if any output section do not come from the
7854
   input BFD.  */
7855
1.12k
      for (section = obfd->sections; section != NULL;
7856
1.09k
     section = section->next)
7857
1.09k
  {
7858
1.09k
    if (!section->segment_mark)
7859
0
      goto rewrite;
7860
1.09k
    else
7861
1.09k
      section->segment_mark = false;
7862
1.09k
  }
7863
7864
36
      return copy_elf_program_header (ibfd, obfd);
7865
36
    }
7866
7867
33
 rewrite:
7868
33
  maxpagesize = 0;
7869
33
  if (ibfd->xvec == obfd->xvec)
7870
33
    {
7871
      /* When rewriting program header, set the output maxpagesize to
7872
   the maximum alignment of input PT_LOAD segments.  */
7873
33
      Elf_Internal_Phdr *segment;
7874
33
      unsigned int i;
7875
33
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7876
7877
33
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
7878
195
     i < num_segments;
7879
162
     i++, segment++)
7880
162
  if (segment->p_type == PT_LOAD
7881
162
      && maxpagesize < segment->p_align)
7882
33
    {
7883
      /* PR 17512: file: f17299af.  */
7884
33
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7885
        /* xgettext:c-format */
7886
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7887
0
            PRIx64 " is too large"),
7888
0
          ibfd, (uint64_t) segment->p_align);
7889
33
      else
7890
33
        maxpagesize = segment->p_align;
7891
33
    }
7892
33
    }
7893
33
  if (maxpagesize == 0)
7894
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7895
7896
33
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7897
69
}
7898
7899
/* Initialize private output section information from input section.  */
7900
7901
bool
7902
_bfd_elf_init_private_section_data (bfd *ibfd,
7903
            asection *isec,
7904
            bfd *obfd,
7905
            asection *osec,
7906
            struct bfd_link_info *link_info)
7907
7908
5.11k
{
7909
5.11k
  Elf_Internal_Shdr *ihdr, *ohdr;
7910
5.11k
  bool final_link = (link_info != NULL
7911
5.11k
         && !bfd_link_relocatable (link_info));
7912
7913
5.11k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
7914
5.11k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
7915
0
    return true;
7916
7917
5.11k
  BFD_ASSERT (elf_section_data (osec) != NULL);
7918
7919
  /* If this is a known ABI section, ELF section type and flags may
7920
     have been set up when OSEC was created.  For normal sections we
7921
     allow the user to override the type and flags other than
7922
     SHF_MASKOS and SHF_MASKPROC.  */
7923
5.11k
  if (elf_section_type (osec) == SHT_PROGBITS
7924
5.11k
      || elf_section_type (osec) == SHT_NOTE
7925
5.11k
      || elf_section_type (osec) == SHT_NOBITS)
7926
1.83k
    elf_section_type (osec) = SHT_NULL;
7927
  /* For objcopy and relocatable link, copy the ELF section type from
7928
     the input file if the BFD section flags are the same.  (If they
7929
     are different the user may be doing something like
7930
     "objcopy --set-section-flags .text=alloc,data".)  For a final
7931
     link allow some flags that the linker clears to differ.  */
7932
5.11k
  if (elf_section_type (osec) == SHT_NULL
7933
5.11k
      && (osec->flags == isec->flags
7934
4.62k
    || (final_link
7935
0
        && ((osec->flags ^ isec->flags)
7936
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7937
4.62k
    elf_section_type (osec) = elf_section_type (isec);
7938
7939
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
7940
5.11k
  elf_section_flags (osec) = (elf_section_flags (isec)
7941
5.11k
            & (SHF_MASKOS | SHF_MASKPROC));
7942
7943
  /* Copy sh_info from input for mbind section.  */
7944
5.11k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7945
5.11k
      && elf_section_flags (isec) & SHF_GNU_MBIND)
7946
0
    elf_section_data (osec)->this_hdr.sh_info
7947
0
      = elf_section_data (isec)->this_hdr.sh_info;
7948
7949
  /* Set things up for objcopy and relocatable link.  The output
7950
     SHT_GROUP section will have its elf_next_in_group pointing back
7951
     to the input group members.  Ignore linker created group section.
7952
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
7953
5.11k
  if ((link_info == NULL
7954
5.11k
       || !link_info->resolve_section_groups)
7955
5.11k
      && (elf_sec_group (isec) == NULL
7956
5.11k
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7957
5.11k
    {
7958
5.11k
      if (elf_section_flags (isec) & SHF_GROUP)
7959
1.83k
  elf_section_flags (osec) |= SHF_GROUP;
7960
5.11k
      elf_next_in_group (osec) = elf_next_in_group (isec);
7961
5.11k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
7962
5.11k
    }
7963
7964
  /* If not decompress, preserve SHF_COMPRESSED.  */
7965
5.11k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7966
5.11k
    elf_section_flags (osec) |= (elf_section_flags (isec)
7967
5.11k
         & SHF_COMPRESSED);
7968
7969
5.11k
  ihdr = &elf_section_data (isec)->this_hdr;
7970
7971
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7972
     don't use the output section of the linked-to section since it
7973
     may be NULL at this point.  */
7974
5.11k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7975
1
    {
7976
1
      ohdr = &elf_section_data (osec)->this_hdr;
7977
1
      ohdr->sh_flags |= SHF_LINK_ORDER;
7978
1
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
7979
1
    }
7980
7981
5.11k
  osec->use_rela_p = isec->use_rela_p;
7982
7983
5.11k
  return true;
7984
5.11k
}
7985
7986
/* Copy private section information.  This copies over the entsize
7987
   field, and sometimes the info field.  */
7988
7989
bool
7990
_bfd_elf_copy_private_section_data (bfd *ibfd,
7991
            asection *isec,
7992
            bfd *obfd,
7993
            asection *osec)
7994
5.11k
{
7995
5.11k
  Elf_Internal_Shdr *ihdr, *ohdr;
7996
7997
5.11k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
7998
5.11k
      || obfd->xvec->flavour != bfd_target_elf_flavour)
7999
0
    return true;
8000
8001
5.11k
  ihdr = &elf_section_data (isec)->this_hdr;
8002
5.11k
  ohdr = &elf_section_data (osec)->this_hdr;
8003
8004
5.11k
  ohdr->sh_entsize = ihdr->sh_entsize;
8005
8006
5.11k
  if (ihdr->sh_type == SHT_SYMTAB
8007
5.11k
      || ihdr->sh_type == SHT_DYNSYM
8008
5.11k
      || ihdr->sh_type == SHT_GNU_verneed
8009
5.11k
      || ihdr->sh_type == SHT_GNU_verdef)
8010
113
    ohdr->sh_info = ihdr->sh_info;
8011
8012
5.11k
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8013
5.11k
               NULL);
8014
5.11k
}
8015
8016
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8017
   necessary if we are removing either the SHT_GROUP section or any of
8018
   the group member sections.  DISCARDED is the value that a section's
8019
   output_section has if the section will be discarded, NULL when this
8020
   function is called from objcopy, bfd_abs_section_ptr when called
8021
   from the linker.  */
8022
8023
bool
8024
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8025
99
{
8026
99
  asection *isec;
8027
8028
5.21k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8029
5.11k
    if (elf_section_type (isec) == SHT_GROUP)
8030
603
      {
8031
603
  asection *first = elf_next_in_group (isec);
8032
603
  asection *s = first;
8033
603
  bfd_size_type removed = 0;
8034
8035
1.82k
  while (s != NULL)
8036
1.82k
    {
8037
      /* If this member section is being output but the
8038
         SHT_GROUP section is not, then clear the group info
8039
         set up by _bfd_elf_copy_private_section_data.  */
8040
1.82k
      if (s->output_section != discarded
8041
1.82k
    && isec->output_section == discarded)
8042
0
        {
8043
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8044
0
    elf_group_name (s->output_section) = NULL;
8045
0
        }
8046
1.82k
      else
8047
1.82k
        {
8048
1.82k
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8049
1.82k
    if (s->output_section == discarded
8050
1.82k
        && isec->output_section != discarded)
8051
0
      {
8052
        /* Conversely, if the member section is not being
8053
           output but the SHT_GROUP section is, then adjust
8054
           its size.  */
8055
0
        removed += 4;
8056
0
        if (elf_sec->rel.hdr != NULL
8057
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8058
0
          removed += 4;
8059
0
        if (elf_sec->rela.hdr != NULL
8060
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8061
0
          removed += 4;
8062
0
      }
8063
1.82k
    else
8064
1.82k
      {
8065
        /* Also adjust for zero-sized relocation member
8066
           section.  */
8067
1.82k
        if (elf_sec->rel.hdr != NULL
8068
1.82k
      && elf_sec->rel.hdr->sh_size == 0)
8069
0
          removed += 4;
8070
1.82k
        if (elf_sec->rela.hdr != NULL
8071
1.82k
      && elf_sec->rela.hdr->sh_size == 0)
8072
0
          removed += 4;
8073
1.82k
      }
8074
1.82k
        }
8075
1.82k
      s = elf_next_in_group (s);
8076
1.82k
      if (s == first)
8077
603
        break;
8078
1.82k
    }
8079
603
  if (removed != 0)
8080
0
    {
8081
0
      if (discarded != NULL)
8082
0
        {
8083
    /* If we've been called for ld -r, then we need to
8084
       adjust the input section size.  */
8085
0
    if (isec->rawsize == 0)
8086
0
      isec->rawsize = isec->size;
8087
0
    isec->size = isec->rawsize - removed;
8088
0
    if (isec->size <= 4)
8089
0
      {
8090
0
        isec->size = 0;
8091
0
        isec->flags |= SEC_EXCLUDE;
8092
0
      }
8093
0
        }
8094
0
      else if (isec->output_section != NULL)
8095
0
        {
8096
    /* Adjust the output section size when called from
8097
       objcopy. */
8098
0
    isec->output_section->size -= removed;
8099
0
    if (isec->output_section->size <= 4)
8100
0
      {
8101
0
        isec->output_section->size = 0;
8102
0
        isec->output_section->flags |= SEC_EXCLUDE;
8103
0
      }
8104
0
        }
8105
0
    }
8106
603
      }
8107
8108
99
  return true;
8109
99
}
8110
8111
/* Copy private header information.  */
8112
8113
bool
8114
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8115
99
{
8116
99
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8117
99
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8118
0
    return true;
8119
8120
  /* Copy over private BFD data if it has not already been copied.
8121
     This must be done here, rather than in the copy_private_bfd_data
8122
     entry point, because the latter is called after the section
8123
     contents have been set, which means that the program headers have
8124
     already been worked out.  */
8125
99
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8126
69
    {
8127
69
      if (! copy_private_bfd_data (ibfd, obfd))
8128
0
  return false;
8129
69
    }
8130
8131
99
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8132
99
}
8133
8134
/* Copy private symbol information.  If this symbol is in a section
8135
   which we did not map into a BFD section, try to map the section
8136
   index correctly.  We use special macro definitions for the mapped
8137
   section indices; these definitions are interpreted by the
8138
   swap_out_syms function.  */
8139
8140
1
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8141
1
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8142
0
#define MAP_STRTAB    (SHN_HIOS + 3)
8143
0
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8144
1
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8145
8146
bool
8147
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8148
           asymbol *isymarg,
8149
           bfd *obfd,
8150
           asymbol *osymarg)
8151
120k
{
8152
120k
  elf_symbol_type *isym, *osym;
8153
8154
120k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8155
120k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8156
0
    return true;
8157
8158
120k
  isym = elf_symbol_from (isymarg);
8159
120k
  osym = elf_symbol_from (osymarg);
8160
8161
120k
  if (isym != NULL
8162
120k
      && isym->internal_elf_sym.st_shndx != 0
8163
120k
      && osym != NULL
8164
120k
      && bfd_is_abs_section (isym->symbol.section))
8165
0
    {
8166
0
      unsigned int shndx;
8167
8168
0
      shndx = isym->internal_elf_sym.st_shndx;
8169
0
      if (shndx == elf_onesymtab (ibfd))
8170
0
  shndx = MAP_ONESYMTAB;
8171
0
      else if (shndx == elf_dynsymtab (ibfd))
8172
0
  shndx = MAP_DYNSYMTAB;
8173
0
      else if (shndx == elf_strtab_sec (ibfd))
8174
0
  shndx = MAP_STRTAB;
8175
0
      else if (shndx == elf_shstrtab_sec (ibfd))
8176
0
  shndx = MAP_SHSTRTAB;
8177
0
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8178
0
  shndx = MAP_SYM_SHNDX;
8179
0
      osym->internal_elf_sym.st_shndx = shndx;
8180
0
    }
8181
8182
120k
  return true;
8183
120k
}
8184
8185
/* Swap out the symbols.  */
8186
8187
static bool
8188
swap_out_syms (bfd *abfd,
8189
         struct elf_strtab_hash **sttp,
8190
         int relocatable_p,
8191
         struct bfd_link_info *info)
8192
86
{
8193
86
  const struct elf_backend_data *bed;
8194
86
  unsigned int symcount;
8195
86
  asymbol **syms;
8196
86
  struct elf_strtab_hash *stt;
8197
86
  Elf_Internal_Shdr *symtab_hdr;
8198
86
  Elf_Internal_Shdr *symtab_shndx_hdr;
8199
86
  Elf_Internal_Shdr *symstrtab_hdr;
8200
86
  struct elf_sym_strtab *symstrtab;
8201
86
  bfd_byte *outbound_syms;
8202
86
  bfd_byte *outbound_shndx;
8203
86
  unsigned long outbound_syms_index;
8204
86
  unsigned int idx;
8205
86
  unsigned int num_locals;
8206
86
  size_t amt;
8207
86
  bool name_local_sections;
8208
8209
86
  if (!elf_map_symbols (abfd, &num_locals))
8210
0
    return false;
8211
8212
  /* Dump out the symtabs.  */
8213
86
  stt = _bfd_elf_strtab_init ();
8214
86
  if (stt == NULL)
8215
0
    return false;
8216
8217
86
  bed = get_elf_backend_data (abfd);
8218
86
  symcount = bfd_get_symcount (abfd);
8219
86
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8220
86
  symtab_hdr->sh_type = SHT_SYMTAB;
8221
86
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8222
86
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8223
86
  symtab_hdr->sh_info = num_locals + 1;
8224
86
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8225
8226
86
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8227
86
  symstrtab_hdr->sh_type = SHT_STRTAB;
8228
8229
  /* Allocate buffer to swap out the .strtab section.  */
8230
86
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8231
86
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8232
0
    {
8233
0
      bfd_set_error (bfd_error_no_memory);
8234
0
      _bfd_elf_strtab_free (stt);
8235
0
      return false;
8236
0
    }
8237
8238
86
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8239
86
      || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8240
0
    {
8241
0
    error_no_mem:
8242
0
      bfd_set_error (bfd_error_no_memory);
8243
0
    error_return:
8244
0
      free (symstrtab);
8245
0
      _bfd_elf_strtab_free (stt);
8246
0
      return false;
8247
0
    }
8248
86
  symtab_hdr->contents = outbound_syms;
8249
86
  outbound_syms_index = 0;
8250
8251
86
  outbound_shndx = NULL;
8252
8253
86
  if (elf_symtab_shndx_list (abfd))
8254
0
    {
8255
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8256
0
      if (symtab_shndx_hdr->sh_name != 0)
8257
0
  {
8258
0
    if (_bfd_mul_overflow (symcount + 1,
8259
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8260
0
      goto error_no_mem;
8261
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8262
0
    if (outbound_shndx == NULL)
8263
0
      goto error_return;
8264
8265
0
    symtab_shndx_hdr->contents = outbound_shndx;
8266
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8267
0
    symtab_shndx_hdr->sh_size = amt;
8268
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8269
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8270
0
  }
8271
      /* FIXME: What about any other headers in the list ?  */
8272
0
    }
8273
8274
  /* Now generate the data (for "contents").  */
8275
86
  {
8276
    /* Fill in zeroth symbol and swap it out.  */
8277
86
    Elf_Internal_Sym sym;
8278
86
    sym.st_name = 0;
8279
86
    sym.st_value = 0;
8280
86
    sym.st_size = 0;
8281
86
    sym.st_info = 0;
8282
86
    sym.st_other = 0;
8283
86
    sym.st_shndx = SHN_UNDEF;
8284
86
    sym.st_target_internal = 0;
8285
86
    symstrtab[0].sym = sym;
8286
86
    symstrtab[0].dest_index = outbound_syms_index;
8287
86
    outbound_syms_index++;
8288
86
  }
8289
8290
86
  name_local_sections
8291
86
    = (bed->elf_backend_name_local_section_symbols
8292
86
       && bed->elf_backend_name_local_section_symbols (abfd));
8293
8294
86
  syms = bfd_get_outsymbols (abfd);
8295
38.0k
  for (idx = 0; idx < symcount;)
8296
37.9k
    {
8297
37.9k
      Elf_Internal_Sym sym;
8298
37.9k
      bfd_vma value = syms[idx]->value;
8299
37.9k
      elf_symbol_type *type_ptr;
8300
37.9k
      flagword flags = syms[idx]->flags;
8301
37.9k
      int type;
8302
8303
37.9k
      if (!name_local_sections
8304
37.9k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8305
2.37k
  {
8306
    /* Local section symbols have no name.  */
8307
2.37k
    sym.st_name = (unsigned long) -1;
8308
2.37k
  }
8309
35.6k
      else
8310
35.6k
  {
8311
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8312
       to get the final offset for st_name.  */
8313
35.6k
    sym.st_name
8314
35.6k
      = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8315
35.6k
               false);
8316
35.6k
    if (sym.st_name == (unsigned long) -1)
8317
0
      goto error_return;
8318
35.6k
  }
8319
8320
37.9k
      type_ptr = elf_symbol_from (syms[idx]);
8321
8322
37.9k
      if ((flags & BSF_SECTION_SYM) == 0
8323
37.9k
    && bfd_is_com_section (syms[idx]->section))
8324
2
  {
8325
    /* ELF common symbols put the alignment into the `value' field,
8326
       and the size into the `size' field.  This is backwards from
8327
       how BFD handles it, so reverse it here.  */
8328
2
    sym.st_size = value;
8329
2
    if (type_ptr == NULL
8330
2
        || type_ptr->internal_elf_sym.st_value == 0)
8331
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8332
2
    else
8333
2
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8334
2
    sym.st_shndx = _bfd_elf_section_from_bfd_section
8335
2
      (abfd, syms[idx]->section);
8336
2
  }
8337
37.9k
      else
8338
37.9k
  {
8339
37.9k
    asection *sec = syms[idx]->section;
8340
37.9k
    unsigned int shndx;
8341
8342
37.9k
    if (sec->output_section)
8343
37.6k
      {
8344
37.6k
        value += sec->output_offset;
8345
37.6k
        sec = sec->output_section;
8346
37.6k
      }
8347
8348
    /* Don't add in the section vma for relocatable output.  */
8349
37.9k
    if (! relocatable_p)
8350
14.2k
      value += sec->vma;
8351
37.9k
    sym.st_value = value;
8352
37.9k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8353
8354
37.9k
    if (bfd_is_abs_section (sec)
8355
37.9k
        && type_ptr != NULL
8356
37.9k
        && type_ptr->internal_elf_sym.st_shndx != 0)
8357
14.8k
      {
8358
        /* This symbol is in a real ELF section which we did
8359
     not create as a BFD section.  Undo the mapping done
8360
     by copy_private_symbol_data.  */
8361
14.8k
        shndx = type_ptr->internal_elf_sym.st_shndx;
8362
14.8k
        switch (shndx)
8363
14.8k
    {
8364
1
    case MAP_ONESYMTAB:
8365
1
      shndx = elf_onesymtab (abfd);
8366
1
      break;
8367
1
    case MAP_DYNSYMTAB:
8368
1
      shndx = elf_dynsymtab (abfd);
8369
1
      break;
8370
0
    case MAP_STRTAB:
8371
0
      shndx = elf_strtab_sec (abfd);
8372
0
      break;
8373
0
    case MAP_SHSTRTAB:
8374
0
      shndx = elf_shstrtab_sec (abfd);
8375
0
      break;
8376
1
    case MAP_SYM_SHNDX:
8377
1
      if (elf_symtab_shndx_list (abfd))
8378
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8379
1
      break;
8380
0
    case SHN_COMMON:
8381
146
    case SHN_ABS:
8382
146
      shndx = SHN_ABS;
8383
146
      break;
8384
14.7k
    default:
8385
14.7k
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8386
26
        {
8387
26
          if (bed->symbol_section_index)
8388
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8389
          /* Otherwise just leave the index alone.  */
8390
26
        }
8391
14.7k
      else
8392
14.7k
        {
8393
14.7k
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8394
55
      _bfd_error_handler (_("%pB: \
8395
55
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8396
55
            abfd, shndx);
8397
14.7k
          shndx = SHN_ABS;
8398
14.7k
        }
8399
14.7k
      break;
8400
14.8k
    }
8401
14.8k
      }
8402
23.0k
    else
8403
23.0k
      {
8404
23.0k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8405
8406
23.0k
        if (shndx == SHN_BAD)
8407
0
    {
8408
0
      asection *sec2;
8409
8410
      /* Writing this would be a hell of a lot easier if
8411
         we had some decent documentation on bfd, and
8412
         knew what to expect of the library, and what to
8413
         demand of applications.  For example, it
8414
         appears that `objcopy' might not set the
8415
         section of a symbol to be a section that is
8416
         actually in the output file.  */
8417
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8418
0
      if (sec2 != NULL)
8419
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8420
0
      if (shndx == SHN_BAD)
8421
0
        {
8422
          /* xgettext:c-format */
8423
0
          _bfd_error_handler
8424
0
      (_("unable to find equivalent output section"
8425
0
         " for symbol '%s' from section '%s'"),
8426
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8427
0
       sec->name);
8428
0
          bfd_set_error (bfd_error_invalid_operation);
8429
0
          goto error_return;
8430
0
        }
8431
0
    }
8432
23.0k
      }
8433
8434
37.9k
    sym.st_shndx = shndx;
8435
37.9k
  }
8436
8437
37.9k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8438
705
  type = STT_TLS;
8439
37.2k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8440
1.12k
  type = STT_GNU_IFUNC;
8441
36.1k
      else if ((flags & BSF_FUNCTION) != 0)
8442
5.48k
  type = STT_FUNC;
8443
30.6k
      else if ((flags & BSF_OBJECT) != 0)
8444
6.57k
  type = STT_OBJECT;
8445
24.0k
      else if ((flags & BSF_RELC) != 0)
8446
702
  type = STT_RELC;
8447
23.3k
      else if ((flags & BSF_SRELC) != 0)
8448
1.81k
  type = STT_SRELC;
8449
21.5k
      else
8450
21.5k
  type = STT_NOTYPE;
8451
8452
37.9k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8453
0
  type = STT_TLS;
8454
8455
      /* Processor-specific types.  */
8456
37.9k
      if (type_ptr != NULL
8457
37.9k
    && bed->elf_backend_get_symbol_type)
8458
0
  type = ((*bed->elf_backend_get_symbol_type)
8459
0
    (&type_ptr->internal_elf_sym, type));
8460
8461
37.9k
      if (flags & BSF_SECTION_SYM)
8462
2.39k
  {
8463
2.39k
    if (flags & BSF_GLOBAL)
8464
24
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8465
2.37k
    else
8466
2.37k
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8467
2.39k
  }
8468
35.5k
      else if (bfd_is_com_section (syms[idx]->section))
8469
2
  {
8470
2
    if (type != STT_TLS)
8471
2
      {
8472
2
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8473
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8474
0
      ? STT_COMMON : STT_OBJECT);
8475
2
        else
8476
2
    type = ((flags & BSF_ELF_COMMON) != 0
8477
2
      ? STT_COMMON : STT_OBJECT);
8478
2
      }
8479
2
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8480
2
  }
8481
35.5k
      else if (bfd_is_und_section (syms[idx]->section))
8482
12.9k
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8483
35.5k
            ? STB_WEAK
8484
35.5k
            : STB_GLOBAL),
8485
35.5k
           type);
8486
22.5k
      else if (flags & BSF_FILE)
8487
745
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8488
21.8k
      else
8489
21.8k
  {
8490
21.8k
    int bind = STB_LOCAL;
8491
8492
21.8k
    if (flags & BSF_LOCAL)
8493
10.3k
      bind = STB_LOCAL;
8494
11.5k
    else if (flags & BSF_GNU_UNIQUE)
8495
324
      bind = STB_GNU_UNIQUE;
8496
11.2k
    else if (flags & BSF_WEAK)
8497
899
      bind = STB_WEAK;
8498
10.3k
    else if (flags & BSF_GLOBAL)
8499
2.47k
      bind = STB_GLOBAL;
8500
8501
21.8k
    sym.st_info = ELF_ST_INFO (bind, type);
8502
21.8k
  }
8503
8504
37.9k
      if (type_ptr != NULL)
8505
37.9k
  {
8506
37.9k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
8507
37.9k
    sym.st_target_internal
8508
37.9k
      = type_ptr->internal_elf_sym.st_target_internal;
8509
37.9k
  }
8510
0
      else
8511
0
  {
8512
0
    sym.st_other = 0;
8513
0
    sym.st_target_internal = 0;
8514
0
  }
8515
8516
37.9k
      idx++;
8517
37.9k
      symstrtab[idx].sym = sym;
8518
37.9k
      symstrtab[idx].dest_index = outbound_syms_index;
8519
8520
37.9k
      outbound_syms_index++;
8521
37.9k
    }
8522
8523
  /* Finalize the .strtab section.  */
8524
86
  _bfd_elf_strtab_finalize (stt);
8525
8526
  /* Swap out the .strtab section.  */
8527
38.1k
  for (idx = 0; idx <= symcount; idx++)
8528
38.0k
    {
8529
38.0k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
8530
38.0k
      if (elfsym->sym.st_name == (unsigned long) -1)
8531
2.37k
  elfsym->sym.st_name = 0;
8532
35.6k
      else
8533
35.6k
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8534
35.6k
                  elfsym->sym.st_name);
8535
38.0k
      if (info && info->callbacks->ctf_new_symbol)
8536
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
8537
0
           &elfsym->sym);
8538
8539
      /* Inform the linker of the addition of this symbol.  */
8540
8541
38.0k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
8542
38.0k
             (outbound_syms
8543
38.0k
        + (elfsym->dest_index
8544
38.0k
           * bed->s->sizeof_sym)),
8545
38.0k
             NPTR_ADD (outbound_shndx,
8546
38.0k
           (elfsym->dest_index
8547
38.0k
            * sizeof (Elf_External_Sym_Shndx))));
8548
38.0k
    }
8549
86
  free (symstrtab);
8550
8551
86
  *sttp = stt;
8552
86
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8553
86
  symstrtab_hdr->sh_type = SHT_STRTAB;
8554
86
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8555
86
  symstrtab_hdr->sh_addr = 0;
8556
86
  symstrtab_hdr->sh_entsize = 0;
8557
86
  symstrtab_hdr->sh_link = 0;
8558
86
  symstrtab_hdr->sh_info = 0;
8559
86
  symstrtab_hdr->sh_addralign = 1;
8560
8561
86
  return true;
8562
86
}
8563
8564
/* Return the number of bytes required to hold the symtab vector.
8565
8566
   Note that we base it on the count plus 1, since we will null terminate
8567
   the vector allocated based on this size.  However, the ELF symbol table
8568
   always has a dummy entry as symbol #0, so it ends up even.  */
8569
8570
long
8571
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
8572
4.44k
{
8573
4.44k
  bfd_size_type symcount;
8574
4.44k
  long symtab_size;
8575
4.44k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8576
8577
4.44k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8578
4.44k
  if (symcount > LONG_MAX / sizeof (asymbol *))
8579
0
    {
8580
0
      bfd_set_error (bfd_error_file_too_big);
8581
0
      return -1;
8582
0
    }
8583
4.44k
  symtab_size = symcount * (sizeof (asymbol *));
8584
4.44k
  if (symcount == 0)
8585
298
    symtab_size = sizeof (asymbol *);
8586
4.14k
  else if (!bfd_write_p (abfd))
8587
4.14k
    {
8588
4.14k
      ufile_ptr filesize = bfd_get_file_size (abfd);
8589
8590
4.14k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
8591
50
  {
8592
50
    bfd_set_error (bfd_error_file_truncated);
8593
50
    return -1;
8594
50
  }
8595
4.14k
    }
8596
8597
4.39k
  return symtab_size;
8598
4.44k
}
8599
8600
long
8601
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8602
5.97k
{
8603
5.97k
  bfd_size_type symcount;
8604
5.97k
  long symtab_size;
8605
5.97k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8606
8607
5.97k
  if (elf_dynsymtab (abfd) == 0)
8608
5.69k
    {
8609
5.69k
      bfd_set_error (bfd_error_invalid_operation);
8610
5.69k
      return -1;
8611
5.69k
    }
8612
8613
273
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8614
273
  if (symcount > LONG_MAX / sizeof (asymbol *))
8615
0
    {
8616
0
      bfd_set_error (bfd_error_file_too_big);
8617
0
      return -1;
8618
0
    }
8619
273
  symtab_size = symcount * (sizeof (asymbol *));
8620
273
  if (symcount == 0)
8621
28
    symtab_size = sizeof (asymbol *);
8622
245
  else if (!bfd_write_p (abfd))
8623
245
    {
8624
245
      ufile_ptr filesize = bfd_get_file_size (abfd);
8625
8626
245
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
8627
12
  {
8628
12
    bfd_set_error (bfd_error_file_truncated);
8629
12
    return -1;
8630
12
  }
8631
245
    }
8632
8633
261
  return symtab_size;
8634
273
}
8635
8636
long
8637
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8638
24.1k
{
8639
24.1k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8640
20.7k
    {
8641
      /* Sanity check reloc section size.  */
8642
20.7k
      ufile_ptr filesize = bfd_get_file_size (abfd);
8643
8644
20.7k
      if (filesize != 0)
8645
20.7k
  {
8646
20.7k
    struct bfd_elf_section_data *d = elf_section_data (asect);
8647
20.7k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8648
20.7k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8649
8650
20.7k
    if (rel_size + rela_size > filesize
8651
20.7k
        || rel_size + rela_size < rel_size)
8652
458
      {
8653
458
        bfd_set_error (bfd_error_file_truncated);
8654
458
        return -1;
8655
458
      }
8656
20.7k
  }
8657
20.7k
    }
8658
8659
#if SIZEOF_LONG == SIZEOF_INT
8660
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8661
    {
8662
      bfd_set_error (bfd_error_file_too_big);
8663
      return -1;
8664
    }
8665
#endif
8666
23.7k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
8667
24.1k
}
8668
8669
/* Canonicalize the relocs.  */
8670
8671
long
8672
_bfd_elf_canonicalize_reloc (bfd *abfd,
8673
           sec_ptr section,
8674
           arelent **relptr,
8675
           asymbol **symbols)
8676
23.5k
{
8677
23.5k
  arelent *tblptr;
8678
23.5k
  unsigned int i;
8679
23.5k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8680
8681
23.5k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8682
2.32k
    return -1;
8683
8684
21.2k
  tblptr = section->relocation;
8685
1.07M
  for (i = 0; i < section->reloc_count; i++)
8686
1.05M
    *relptr++ = tblptr++;
8687
8688
21.2k
  *relptr = NULL;
8689
8690
21.2k
  return section->reloc_count;
8691
23.5k
}
8692
8693
long
8694
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8695
4.39k
{
8696
4.39k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8697
4.39k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8698
8699
4.39k
  if (symcount >= 0)
8700
4.24k
    abfd->symcount = symcount;
8701
4.39k
  return symcount;
8702
4.39k
}
8703
8704
long
8705
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8706
              asymbol **allocation)
8707
194
{
8708
194
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8709
194
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8710
8711
194
  if (symcount >= 0)
8712
157
    abfd->dynsymcount = symcount;
8713
194
  return symcount;
8714
194
}
8715
8716
/* Return the size required for the dynamic reloc entries.  Any loadable
8717
   section that was actually installed in the BFD, and has type SHT_REL
8718
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8719
   dynamic reloc section.  */
8720
8721
long
8722
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8723
5.49k
{
8724
5.49k
  bfd_size_type count, ext_rel_size;
8725
5.49k
  asection *s;
8726
8727
5.49k
  if (elf_dynsymtab (abfd) == 0)
8728
5.43k
    {
8729
5.43k
      bfd_set_error (bfd_error_invalid_operation);
8730
5.43k
      return -1;
8731
5.43k
    }
8732
8733
67
  count = 1;
8734
67
  ext_rel_size = 0;
8735
1.85k
  for (s = abfd->sections; s != NULL; s = s->next)
8736
1.78k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8737
1.78k
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8738
251
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8739
1.78k
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8740
106
      {
8741
106
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
8742
106
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
8743
0
    {
8744
0
      bfd_set_error (bfd_error_file_truncated);
8745
0
      return -1;
8746
0
    }
8747
106
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8748
106
  if (count > LONG_MAX / sizeof (arelent *))
8749
0
    {
8750
0
      bfd_set_error (bfd_error_file_too_big);
8751
0
      return -1;
8752
0
    }
8753
106
      }
8754
67
  if (count > 1 && !bfd_write_p (abfd))
8755
65
    {
8756
      /* Sanity check reloc section sizes.  */
8757
65
      ufile_ptr filesize = bfd_get_file_size (abfd);
8758
65
      if (filesize != 0 && ext_rel_size > filesize)
8759
6
  {
8760
6
    bfd_set_error (bfd_error_file_truncated);
8761
6
    return -1;
8762
6
  }
8763
65
    }
8764
61
  return count * sizeof (arelent *);
8765
67
}
8766
8767
/* Canonicalize the dynamic relocation entries.  Note that we return the
8768
   dynamic relocations as a single block, although they are actually
8769
   associated with particular sections; the interface, which was
8770
   designed for SunOS style shared libraries, expects that there is only
8771
   one set of dynamic relocs.  Any loadable section that was actually
8772
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8773
   dynamic symbol table, is considered to be a dynamic reloc section.  */
8774
8775
long
8776
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8777
             arelent **storage,
8778
             asymbol **syms)
8779
57
{
8780
57
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8781
57
  asection *s;
8782
57
  long ret;
8783
8784
57
  if (elf_dynsymtab (abfd) == 0)
8785
0
    {
8786
0
      bfd_set_error (bfd_error_invalid_operation);
8787
0
      return -1;
8788
0
    }
8789
8790
57
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8791
57
  ret = 0;
8792
1.49k
  for (s = abfd->sections; s != NULL; s = s->next)
8793
1.44k
    {
8794
1.44k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8795
1.44k
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8796
222
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8797
1.44k
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8798
96
  {
8799
96
    arelent *p;
8800
96
    long count, i;
8801
8802
96
    if (! (*slurp_relocs) (abfd, s, syms, true))
8803
6
      return -1;
8804
90
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8805
90
    p = s->relocation;
8806
34.3k
    for (i = 0; i < count; i++)
8807
34.2k
      *storage++ = p++;
8808
90
    ret += count;
8809
90
  }
8810
1.44k
    }
8811
8812
51
  *storage = NULL;
8813
8814
51
  return ret;
8815
57
}
8816

8817
/* Read in the version information.  */
8818
8819
bool
8820
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8821
284
{
8822
284
  bfd_byte *contents = NULL;
8823
284
  unsigned int freeidx = 0;
8824
284
  size_t amt;
8825
8826
284
  if (elf_dynverref (abfd) != 0)
8827
224
    {
8828
224
      Elf_Internal_Shdr *hdr;
8829
224
      Elf_External_Verneed *everneed;
8830
224
      Elf_Internal_Verneed *iverneed;
8831
224
      unsigned int i;
8832
224
      bfd_byte *contents_end;
8833
8834
224
      hdr = &elf_tdata (abfd)->dynverref_hdr;
8835
8836
224
      if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8837
8
  {
8838
41
  error_return_bad_verref:
8839
41
    _bfd_error_handler
8840
41
      (_("%pB: .gnu.version_r invalid entry"), abfd);
8841
41
    bfd_set_error (bfd_error_bad_value);
8842
51
  error_return_verref:
8843
51
    elf_tdata (abfd)->verref = NULL;
8844
51
    elf_tdata (abfd)->cverrefs = 0;
8845
51
    goto error_return;
8846
41
  }
8847
8848
216
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8849
3
  goto error_return_verref;
8850
213
      contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8851
213
      if (contents == NULL)
8852
7
  goto error_return_verref;
8853
8854
206
      if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8855
0
  {
8856
0
    bfd_set_error (bfd_error_file_too_big);
8857
0
    goto error_return_verref;
8858
0
  }
8859
206
      if (amt == 0)
8860
0
  goto error_return_verref;
8861
206
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
8862
206
      if (elf_tdata (abfd)->verref == NULL)
8863
0
  goto error_return_verref;
8864
8865
206
      BFD_ASSERT (sizeof (Elf_External_Verneed)
8866
206
      == sizeof (Elf_External_Vernaux));
8867
206
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8868
206
      everneed = (Elf_External_Verneed *) contents;
8869
206
      iverneed = elf_tdata (abfd)->verref;
8870
420
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
8871
419
  {
8872
419
    Elf_External_Vernaux *evernaux;
8873
419
    Elf_Internal_Vernaux *ivernaux;
8874
419
    unsigned int j;
8875
8876
419
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8877
8878
419
    iverneed->vn_bfd = abfd;
8879
8880
419
    iverneed->vn_filename =
8881
419
      bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8882
419
               iverneed->vn_file);
8883
419
    if (iverneed->vn_filename == NULL)
8884
24
      goto error_return_bad_verref;
8885
8886
395
    if (iverneed->vn_cnt == 0)
8887
13
      iverneed->vn_auxptr = NULL;
8888
382
    else
8889
382
      {
8890
382
        if (_bfd_mul_overflow (iverneed->vn_cnt,
8891
382
             sizeof (Elf_Internal_Vernaux), &amt))
8892
0
    {
8893
0
      bfd_set_error (bfd_error_file_too_big);
8894
0
      goto error_return_verref;
8895
0
    }
8896
382
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8897
382
    bfd_alloc (abfd, amt);
8898
382
        if (iverneed->vn_auxptr == NULL)
8899
0
    goto error_return_verref;
8900
382
      }
8901
8902
395
    if (iverneed->vn_aux
8903
395
        > (size_t) (contents_end - (bfd_byte *) everneed))
8904
2
      goto error_return_bad_verref;
8905
8906
393
    evernaux = ((Elf_External_Vernaux *)
8907
393
          ((bfd_byte *) everneed + iverneed->vn_aux));
8908
393
    ivernaux = iverneed->vn_auxptr;
8909
724
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8910
711
      {
8911
711
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8912
8913
711
        ivernaux->vna_nodename =
8914
711
    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8915
711
             ivernaux->vna_name);
8916
711
        if (ivernaux->vna_nodename == NULL)
8917
0
    goto error_return_bad_verref;
8918
8919
711
        if (ivernaux->vna_other > freeidx)
8920
200
    freeidx = ivernaux->vna_other;
8921
8922
711
        ivernaux->vna_nextptr = NULL;
8923
711
        if (ivernaux->vna_next == 0)
8924
377
    {
8925
377
      iverneed->vn_cnt = j + 1;
8926
377
      break;
8927
377
    }
8928
334
        if (j + 1 < iverneed->vn_cnt)
8929
333
    ivernaux->vna_nextptr = ivernaux + 1;
8930
8931
334
        if (ivernaux->vna_next
8932
334
      > (size_t) (contents_end - (bfd_byte *) evernaux))
8933
3
    goto error_return_bad_verref;
8934
8935
331
        evernaux = ((Elf_External_Vernaux *)
8936
331
        ((bfd_byte *) evernaux + ivernaux->vna_next));
8937
331
      }
8938
8939
390
    iverneed->vn_nextref = NULL;
8940
390
    if (iverneed->vn_next == 0)
8941
172
      break;
8942
218
    if (i + 1 < hdr->sh_info)
8943
213
      iverneed->vn_nextref = iverneed + 1;
8944
8945
218
    if (iverneed->vn_next
8946
218
        > (size_t) (contents_end - (bfd_byte *) everneed))
8947
4
      goto error_return_bad_verref;
8948
8949
214
    everneed = ((Elf_External_Verneed *)
8950
214
          ((bfd_byte *) everneed + iverneed->vn_next));
8951
214
  }
8952
173
      elf_tdata (abfd)->cverrefs = i;
8953
8954
173
      free (contents);
8955
173
      contents = NULL;
8956
173
    }
8957
8958
233
  if (elf_dynverdef (abfd) != 0)
8959
60
    {
8960
60
      Elf_Internal_Shdr *hdr;
8961
60
      Elf_External_Verdef *everdef;
8962
60
      Elf_Internal_Verdef *iverdef;
8963
60
      Elf_Internal_Verdef *iverdefarr;
8964
60
      Elf_Internal_Verdef iverdefmem;
8965
60
      unsigned int i;
8966
60
      unsigned int maxidx;
8967
60
      bfd_byte *contents_end_def, *contents_end_aux;
8968
8969
60
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
8970
8971
60
      if (hdr->sh_size < sizeof (Elf_External_Verdef))
8972
1
  {
8973
31
  error_return_bad_verdef:
8974
31
    _bfd_error_handler
8975
31
      (_("%pB: .gnu.version_d invalid entry"), abfd);
8976
31
    bfd_set_error (bfd_error_bad_value);
8977
38
  error_return_verdef:
8978
38
    elf_tdata (abfd)->verdef = NULL;
8979
38
    elf_tdata (abfd)->cverdefs = 0;
8980
38
    goto error_return;
8981
31
  }
8982
8983
59
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8984
2
  goto error_return_verdef;
8985
57
      contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8986
57
      if (contents == NULL)
8987
5
  goto error_return_verdef;
8988
8989
52
      BFD_ASSERT (sizeof (Elf_External_Verdef)
8990
52
      >= sizeof (Elf_External_Verdaux));
8991
52
      contents_end_def = contents + hdr->sh_size
8992
52
       - sizeof (Elf_External_Verdef);
8993
52
      contents_end_aux = contents + hdr->sh_size
8994
52
       - sizeof (Elf_External_Verdaux);
8995
8996
      /* We know the number of entries in the section but not the maximum
8997
   index.  Therefore we have to run through all entries and find
8998
   the maximum.  */
8999
52
      everdef = (Elf_External_Verdef *) contents;
9000
52
      maxidx = 0;
9001
64
      for (i = 0; i < hdr->sh_info; ++i)
9002
60
  {
9003
60
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9004
9005
60
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9006
8
      goto error_return_bad_verdef;
9007
52
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9008
44
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9009
9010
52
    if (iverdefmem.vd_next == 0)
9011
37
      break;
9012
9013
15
    if (iverdefmem.vd_next
9014
15
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9015
3
      goto error_return_bad_verdef;
9016
9017
12
    everdef = ((Elf_External_Verdef *)
9018
12
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9019
12
  }
9020
9021
41
      if (default_imported_symver)
9022
0
  {
9023
0
    if (freeidx > maxidx)
9024
0
      maxidx = ++freeidx;
9025
0
    else
9026
0
      freeidx = ++maxidx;
9027
0
  }
9028
41
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9029
0
  {
9030
0
    bfd_set_error (bfd_error_file_too_big);
9031
0
    goto error_return_verdef;
9032
0
  }
9033
9034
41
      if (amt == 0)
9035
0
  goto error_return_verdef;
9036
41
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9037
41
      if (elf_tdata (abfd)->verdef == NULL)
9038
0
  goto error_return_verdef;
9039
9040
41
      elf_tdata (abfd)->cverdefs = maxidx;
9041
9042
41
      everdef = (Elf_External_Verdef *) contents;
9043
41
      iverdefarr = elf_tdata (abfd)->verdef;
9044
41
      for (i = 0; i < hdr->sh_info; i++)
9045
41
  {
9046
41
    Elf_External_Verdaux *everdaux;
9047
41
    Elf_Internal_Verdaux *iverdaux;
9048
41
    unsigned int j;
9049
9050
41
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9051
9052
41
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9053
0
      goto error_return_bad_verdef;
9054
9055
41
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9056
41
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9057
9058
41
    iverdef->vd_bfd = abfd;
9059
9060
41
    if (iverdef->vd_cnt == 0)
9061
20
      iverdef->vd_auxptr = NULL;
9062
21
    else
9063
21
      {
9064
21
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9065
21
             sizeof (Elf_Internal_Verdaux), &amt))
9066
0
    {
9067
0
      bfd_set_error (bfd_error_file_too_big);
9068
0
      goto error_return_verdef;
9069
0
    }
9070
21
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9071
21
    bfd_alloc (abfd, amt);
9072
21
        if (iverdef->vd_auxptr == NULL)
9073
0
    goto error_return_verdef;
9074
21
      }
9075
9076
41
    if (iverdef->vd_aux
9077
41
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9078
2
      goto error_return_bad_verdef;
9079
9080
39
    everdaux = ((Elf_External_Verdaux *)
9081
39
          ((bfd_byte *) everdef + iverdef->vd_aux));
9082
39
    iverdaux = iverdef->vd_auxptr;
9083
39
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9084
19
      {
9085
19
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9086
9087
19
        iverdaux->vda_nodename =
9088
19
    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9089
19
             iverdaux->vda_name);
9090
19
        if (iverdaux->vda_nodename == NULL)
9091
17
    goto error_return_bad_verdef;
9092
9093
2
        iverdaux->vda_nextptr = NULL;
9094
2
        if (iverdaux->vda_next == 0)
9095
2
    {
9096
2
      iverdef->vd_cnt = j + 1;
9097
2
      break;
9098
2
    }
9099
0
        if (j + 1 < iverdef->vd_cnt)
9100
0
    iverdaux->vda_nextptr = iverdaux + 1;
9101
9102
0
        if (iverdaux->vda_next
9103
0
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9104
0
    goto error_return_bad_verdef;
9105
9106
0
        everdaux = ((Elf_External_Verdaux *)
9107
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9108
0
      }
9109
9110
22
    iverdef->vd_nodename = NULL;
9111
22
    if (iverdef->vd_cnt)
9112
2
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9113
9114
22
    iverdef->vd_nextdef = NULL;
9115
22
    if (iverdef->vd_next == 0)
9116
22
      break;
9117
0
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9118
0
      iverdef->vd_nextdef = iverdef + 1;
9119
9120
0
    everdef = ((Elf_External_Verdef *)
9121
0
         ((bfd_byte *) everdef + iverdef->vd_next));
9122
0
  }
9123
9124
22
      free (contents);
9125
22
      contents = NULL;
9126
22
    }
9127
173
  else if (default_imported_symver)
9128
0
    {
9129
0
      if (freeidx < 3)
9130
0
  freeidx = 3;
9131
0
      else
9132
0
  freeidx++;
9133
9134
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9135
0
  {
9136
0
    bfd_set_error (bfd_error_file_too_big);
9137
0
    goto error_return;
9138
0
  }
9139
0
      if (amt == 0)
9140
0
  goto error_return;
9141
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9142
0
      if (elf_tdata (abfd)->verdef == NULL)
9143
0
  goto error_return;
9144
9145
0
      elf_tdata (abfd)->cverdefs = freeidx;
9146
0
    }
9147
9148
  /* Create a default version based on the soname.  */
9149
195
  if (default_imported_symver)
9150
0
    {
9151
0
      Elf_Internal_Verdef *iverdef;
9152
0
      Elf_Internal_Verdaux *iverdaux;
9153
9154
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9155
9156
0
      iverdef->vd_version = VER_DEF_CURRENT;
9157
0
      iverdef->vd_flags = 0;
9158
0
      iverdef->vd_ndx = freeidx;
9159
0
      iverdef->vd_cnt = 1;
9160
9161
0
      iverdef->vd_bfd = abfd;
9162
9163
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9164
0
      if (iverdef->vd_nodename == NULL)
9165
0
  goto error_return_verdef;
9166
0
      iverdef->vd_nextdef = NULL;
9167
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9168
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9169
0
      if (iverdef->vd_auxptr == NULL)
9170
0
  goto error_return_verdef;
9171
9172
0
      iverdaux = iverdef->vd_auxptr;
9173
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9174
0
    }
9175
9176
195
  return true;
9177
9178
89
 error_return:
9179
89
  free (contents);
9180
89
  return false;
9181
195
}
9182

9183
asymbol *
9184
_bfd_elf_make_empty_symbol (bfd *abfd)
9185
3.43M
{
9186
3.43M
  elf_symbol_type *newsym;
9187
9188
3.43M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9189
3.43M
  if (!newsym)
9190
0
    return NULL;
9191
3.43M
  newsym->symbol.the_bfd = abfd;
9192
3.43M
  return &newsym->symbol;
9193
3.43M
}
9194
9195
void
9196
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9197
        asymbol *symbol,
9198
        symbol_info *ret)
9199
182k
{
9200
182k
  bfd_symbol_info (symbol, ret);
9201
182k
}
9202
9203
/* Return whether a symbol name implies a local symbol.  Most targets
9204
   use this function for the is_local_label_name entry point, but some
9205
   override it.  */
9206
9207
bool
9208
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9209
            const char *name)
9210
64.8k
{
9211
  /* Normal local symbols start with ``.L''.  */
9212
64.8k
  if (name[0] == '.' && name[1] == 'L')
9213
35.0k
    return true;
9214
9215
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9216
     DWARF debugging symbols starting with ``..''.  */
9217
29.7k
  if (name[0] == '.' && name[1] == '.')
9218
637
    return true;
9219
9220
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9221
     emitting DWARF debugging output.  I suspect this is actually a
9222
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9223
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9224
     underscore to be emitted on some ELF targets).  For ease of use,
9225
     we treat such symbols as local.  */
9226
29.1k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9227
0
    return true;
9228
9229
  /* Treat assembler generated fake symbols, dollar local labels and
9230
     forward-backward labels (aka local labels) as locals.
9231
     These labels have the form:
9232
9233
       L0^A.*              (fake symbols)
9234
9235
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9236
9237
     Versions which start with .L will have already been matched above,
9238
     so we only need to match the rest.  */
9239
29.1k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9240
239
    {
9241
239
      bool ret = false;
9242
239
      const char * p;
9243
239
      char c;
9244
9245
783
      for (p = name + 2; (c = *p); p++)
9246
709
  {
9247
709
    if (c == 1 || c == 2)
9248
0
      {
9249
0
        if (c == 1 && p == name + 2)
9250
    /* A fake symbol.  */
9251
0
    return true;
9252
9253
        /* FIXME: We are being paranoid here and treating symbols like
9254
     L0^Bfoo as if there were non-local, on the grounds that the
9255
     assembler will never generate them.  But can any symbol
9256
     containing an ASCII value in the range 1-31 ever be anything
9257
     other than some kind of local ?  */
9258
0
        ret = true;
9259
0
      }
9260
9261
709
    if (! ISDIGIT (c))
9262
165
      {
9263
165
        ret = false;
9264
165
        break;
9265
165
      }
9266
709
  }
9267
239
      return ret;
9268
239
    }
9269
9270
28.8k
  return false;
9271
29.1k
}
9272
9273
alent *
9274
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9275
         asymbol *symbol ATTRIBUTE_UNUSED)
9276
0
{
9277
0
  abort ();
9278
0
  return NULL;
9279
0
}
9280
9281
bool
9282
_bfd_elf_set_arch_mach (bfd *abfd,
9283
      enum bfd_architecture arch,
9284
      unsigned long machine)
9285
863
{
9286
  /* If this isn't the right architecture for this backend, and this
9287
     isn't the generic backend, fail.  */
9288
863
  if (arch != get_elf_backend_data (abfd)->arch
9289
863
      && arch != bfd_arch_unknown
9290
863
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9291
0
    return false;
9292
9293
863
  return bfd_default_set_arch_mach (abfd, arch, machine);
9294
863
}
9295
9296
/* Find the nearest line to a particular section and offset,
9297
   for error reporting.  */
9298
9299
bool
9300
_bfd_elf_find_nearest_line (bfd *abfd,
9301
          asymbol **symbols,
9302
          asection *section,
9303
          bfd_vma offset,
9304
          const char **filename_ptr,
9305
          const char **functionname_ptr,
9306
          unsigned int *line_ptr,
9307
          unsigned int *discriminator_ptr)
9308
86.6k
{
9309
86.6k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9310
86.6k
                offset, filename_ptr,
9311
86.6k
                functionname_ptr, line_ptr,
9312
86.6k
                discriminator_ptr);
9313
86.6k
}
9314
9315
/* Find the nearest line to a particular section and offset,
9316
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9317
   can be optionally specified.  */
9318
9319
bool
9320
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9321
             const char *alt_filename,
9322
             asymbol **symbols,
9323
             asection *section,
9324
             bfd_vma offset,
9325
             const char **filename_ptr,
9326
             const char **functionname_ptr,
9327
             unsigned int *line_ptr,
9328
             unsigned int *discriminator_ptr)
9329
86.6k
{
9330
86.6k
  bool found;
9331
9332
86.6k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9333
86.6k
                section, offset, filename_ptr,
9334
86.6k
                functionname_ptr, line_ptr,
9335
86.6k
                discriminator_ptr,
9336
86.6k
                dwarf_debug_sections,
9337
86.6k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9338
26.3k
    return true;
9339
9340
60.2k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9341
60.2k
             filename_ptr, functionname_ptr, line_ptr))
9342
0
    {
9343
0
      if (!*functionname_ptr)
9344
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9345
0
        *filename_ptr ? NULL : filename_ptr,
9346
0
        functionname_ptr);
9347
0
      return true;
9348
0
    }
9349
9350
60.2k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9351
60.2k
               &found, filename_ptr,
9352
60.2k
               functionname_ptr, line_ptr,
9353
60.2k
               &elf_tdata (abfd)->line_info))
9354
0
    return false;
9355
60.2k
  if (found && (*functionname_ptr || *line_ptr))
9356
0
    return true;
9357
9358
60.2k
  if (symbols == NULL)
9359
2.48k
    return false;
9360
9361
57.7k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9362
57.7k
        filename_ptr, functionname_ptr))
9363
45.4k
    return false;
9364
9365
12.3k
  *line_ptr = 0;
9366
12.3k
  return true;
9367
57.7k
}
9368
9369
/* Find the line for a symbol.  */
9370
9371
bool
9372
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9373
        const char **filename_ptr, unsigned int *line_ptr)
9374
43.6k
{
9375
43.6k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9376
43.6k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9377
43.6k
          filename_ptr, NULL, line_ptr, NULL,
9378
43.6k
          dwarf_debug_sections,
9379
43.6k
          &tdata->dwarf2_find_line_info);
9380
43.6k
}
9381
9382
/* After a call to bfd_find_nearest_line, successive calls to
9383
   bfd_find_inliner_info can be used to get source information about
9384
   each level of function inlining that terminated at the address
9385
   passed to bfd_find_nearest_line.  Currently this is only supported
9386
   for DWARF2 with appropriate DWARF3 extensions. */
9387
9388
bool
9389
_bfd_elf_find_inliner_info (bfd *abfd,
9390
          const char **filename_ptr,
9391
          const char **functionname_ptr,
9392
          unsigned int *line_ptr)
9393
0
{
9394
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9395
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9396
0
          functionname_ptr, line_ptr,
9397
0
          &tdata->dwarf2_find_line_info);
9398
0
}
9399
9400
int
9401
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9402
0
{
9403
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9404
0
  int ret = bed->s->sizeof_ehdr;
9405
9406
0
  if (!bfd_link_relocatable (info))
9407
0
    {
9408
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
9409
9410
0
      if (phdr_size == (bfd_size_type) -1)
9411
0
  {
9412
0
    struct elf_segment_map *m;
9413
9414
0
    phdr_size = 0;
9415
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9416
0
      phdr_size += bed->s->sizeof_phdr;
9417
9418
0
    if (phdr_size == 0)
9419
0
      phdr_size = get_program_header_size (abfd, info);
9420
0
  }
9421
9422
0
      elf_program_header_size (abfd) = phdr_size;
9423
0
      ret += phdr_size;
9424
0
    }
9425
9426
0
  return ret;
9427
0
}
9428
9429
bool
9430
_bfd_elf_set_section_contents (bfd *abfd,
9431
             sec_ptr section,
9432
             const void *location,
9433
             file_ptr offset,
9434
             bfd_size_type count)
9435
3.87k
{
9436
3.87k
  Elf_Internal_Shdr *hdr;
9437
9438
3.87k
  if (! abfd->output_has_begun
9439
3.87k
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9440
9
    return false;
9441
9442
3.86k
  if (!count)
9443
0
    return true;
9444
9445
3.86k
  hdr = &elf_section_data (section)->this_hdr;
9446
3.86k
  if (hdr->sh_offset == (file_ptr) -1)
9447
0
    {
9448
0
      unsigned char *contents;
9449
9450
0
      if (bfd_section_is_ctf (section))
9451
  /* Nothing to do with this section: the contents are generated
9452
     later.  */
9453
0
  return true;
9454
9455
0
      if ((offset + count) > hdr->sh_size)
9456
0
  {
9457
0
    _bfd_error_handler
9458
0
      (_("%pB:%pA: error: attempting to write"
9459
0
         " over the end of the section"),
9460
0
       abfd, section);
9461
9462
0
    bfd_set_error (bfd_error_invalid_operation);
9463
0
    return false;
9464
0
  }
9465
9466
0
      contents = hdr->contents;
9467
0
      if (contents == NULL)
9468
0
  {
9469
0
    _bfd_error_handler
9470
0
      (_("%pB:%pA: error: attempting to write"
9471
0
         " section into an empty buffer"),
9472
0
       abfd, section);
9473
9474
0
    bfd_set_error (bfd_error_invalid_operation);
9475
0
    return false;
9476
0
  }
9477
9478
0
      memcpy (contents + offset, location, count);
9479
0
      return true;
9480
0
    }
9481
9482
3.86k
  return _bfd_generic_set_section_contents (abfd, section,
9483
3.86k
              location, offset, count);
9484
3.86k
}
9485
9486
bool
9487
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9488
         arelent *cache_ptr ATTRIBUTE_UNUSED,
9489
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9490
0
{
9491
0
  abort ();
9492
0
  return false;
9493
0
}
9494
9495
/* Try to convert a non-ELF reloc into an ELF one.  */
9496
9497
bool
9498
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9499
0
{
9500
  /* Check whether we really have an ELF howto.  */
9501
9502
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9503
0
    {
9504
0
      bfd_reloc_code_real_type code;
9505
0
      reloc_howto_type *howto;
9506
9507
      /* Alien reloc: Try to determine its type to replace it with an
9508
   equivalent ELF reloc.  */
9509
9510
0
      if (areloc->howto->pc_relative)
9511
0
  {
9512
0
    switch (areloc->howto->bitsize)
9513
0
      {
9514
0
      case 8:
9515
0
        code = BFD_RELOC_8_PCREL;
9516
0
        break;
9517
0
      case 12:
9518
0
        code = BFD_RELOC_12_PCREL;
9519
0
        break;
9520
0
      case 16:
9521
0
        code = BFD_RELOC_16_PCREL;
9522
0
        break;
9523
0
      case 24:
9524
0
        code = BFD_RELOC_24_PCREL;
9525
0
        break;
9526
0
      case 32:
9527
0
        code = BFD_RELOC_32_PCREL;
9528
0
        break;
9529
0
      case 64:
9530
0
        code = BFD_RELOC_64_PCREL;
9531
0
        break;
9532
0
      default:
9533
0
        goto fail;
9534
0
      }
9535
9536
0
    howto = bfd_reloc_type_lookup (abfd, code);
9537
9538
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9539
0
      {
9540
0
        if (howto->pcrel_offset)
9541
0
    areloc->addend += areloc->address;
9542
0
        else
9543
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
9544
0
      }
9545
0
  }
9546
0
      else
9547
0
  {
9548
0
    switch (areloc->howto->bitsize)
9549
0
      {
9550
0
      case 8:
9551
0
        code = BFD_RELOC_8;
9552
0
        break;
9553
0
      case 14:
9554
0
        code = BFD_RELOC_14;
9555
0
        break;
9556
0
      case 16:
9557
0
        code = BFD_RELOC_16;
9558
0
        break;
9559
0
      case 26:
9560
0
        code = BFD_RELOC_26;
9561
0
        break;
9562
0
      case 32:
9563
0
        code = BFD_RELOC_32;
9564
0
        break;
9565
0
      case 64:
9566
0
        code = BFD_RELOC_64;
9567
0
        break;
9568
0
      default:
9569
0
        goto fail;
9570
0
      }
9571
9572
0
    howto = bfd_reloc_type_lookup (abfd, code);
9573
0
  }
9574
9575
0
      if (howto)
9576
0
  areloc->howto = howto;
9577
0
      else
9578
0
  goto fail;
9579
0
    }
9580
9581
0
  return true;
9582
9583
0
 fail:
9584
  /* xgettext:c-format */
9585
0
  _bfd_error_handler (_("%pB: %s unsupported"),
9586
0
          abfd, areloc->howto->name);
9587
0
  bfd_set_error (bfd_error_sorry);
9588
0
  return false;
9589
0
}
9590
9591
bool
9592
_bfd_elf_free_cached_info (bfd *abfd)
9593
628k
{
9594
628k
  struct elf_obj_tdata *tdata;
9595
9596
628k
  if ((bfd_get_format (abfd) == bfd_object
9597
628k
       || bfd_get_format (abfd) == bfd_core)
9598
628k
      && (tdata = elf_tdata (abfd)) != NULL)
9599
227k
    {
9600
227k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
9601
198
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
9602
227k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9603
227k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
9604
227k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
9605
227k
    }
9606
9607
628k
  return _bfd_generic_bfd_free_cached_info (abfd);
9608
628k
}
9609
9610
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9611
   in the relocation's offset.  Thus we cannot allow any sort of sanity
9612
   range-checking to interfere.  There is nothing else to do in processing
9613
   this reloc.  */
9614
9615
bfd_reloc_status_type
9616
_bfd_elf_rel_vtable_reloc_fn
9617
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9618
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9619
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9620
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9621
27
{
9622
27
  return bfd_reloc_ok;
9623
27
}
9624

9625
/* Elf core file support.  Much of this only works on native
9626
   toolchains, since we rely on knowing the
9627
   machine-dependent procfs structure in order to pick
9628
   out details about the corefile.  */
9629
9630
#ifdef HAVE_SYS_PROCFS_H
9631
# include <sys/procfs.h>
9632
#endif
9633
9634
/* Return a PID that identifies a "thread" for threaded cores, or the
9635
   PID of the main process for non-threaded cores.  */
9636
9637
static int
9638
elfcore_make_pid (bfd *abfd)
9639
90
{
9640
90
  int pid;
9641
9642
90
  pid = elf_tdata (abfd)->core->lwpid;
9643
90
  if (pid == 0)
9644
89
    pid = elf_tdata (abfd)->core->pid;
9645
9646
90
  return pid;
9647
90
}
9648
9649
/* If there isn't a section called NAME, make one, using data from
9650
   SECT.  Note, this function will generate a reference to NAME, so
9651
   you shouldn't deallocate or overwrite it.  */
9652
9653
static bool
9654
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9655
90
{
9656
90
  asection *sect2;
9657
9658
90
  if (bfd_get_section_by_name (abfd, name) != NULL)
9659
24
    return true;
9660
9661
66
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9662
66
  if (sect2 == NULL)
9663
0
    return false;
9664
9665
66
  sect2->size = sect->size;
9666
66
  sect2->filepos = sect->filepos;
9667
66
  sect2->alignment_power = sect->alignment_power;
9668
66
  return true;
9669
66
}
9670
9671
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
9672
   actually creates up to two pseudosections:
9673
   - For the single-threaded case, a section named NAME, unless
9674
     such a section already exists.
9675
   - For the multi-threaded case, a section named "NAME/PID", where
9676
     PID is elfcore_make_pid (abfd).
9677
   Both pseudosections have identical contents.  */
9678
bool
9679
_bfd_elfcore_make_pseudosection (bfd *abfd,
9680
         char *name,
9681
         size_t size,
9682
         ufile_ptr filepos)
9683
90
{
9684
90
  char buf[100];
9685
90
  char *threaded_name;
9686
90
  size_t len;
9687
90
  asection *sect;
9688
9689
  /* Build the section name.  */
9690
9691
90
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9692
90
  len = strlen (buf) + 1;
9693
90
  threaded_name = (char *) bfd_alloc (abfd, len);
9694
90
  if (threaded_name == NULL)
9695
0
    return false;
9696
90
  memcpy (threaded_name, buf, len);
9697
9698
90
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9699
90
               SEC_HAS_CONTENTS);
9700
90
  if (sect == NULL)
9701
0
    return false;
9702
90
  sect->size = size;
9703
90
  sect->filepos = filepos;
9704
90
  sect->alignment_power = 2;
9705
9706
90
  return elfcore_maybe_make_sect (abfd, name, sect);
9707
90
}
9708
9709
static bool
9710
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9711
        size_t offs)
9712
8
{
9713
8
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9714
8
                   SEC_HAS_CONTENTS);
9715
9716
8
  if (sect == NULL)
9717
0
    return false;
9718
9719
8
  sect->size = note->descsz - offs;
9720
8
  sect->filepos = note->descpos + offs;
9721
8
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9722
9723
8
  return true;
9724
8
}
9725
9726
/* prstatus_t exists on:
9727
     solaris 2.5+
9728
     linux 2.[01] + glibc
9729
     unixware 4.2
9730
*/
9731
9732
#if defined (HAVE_PRSTATUS_T)
9733
9734
static bool
9735
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9736
206
{
9737
206
  size_t size;
9738
206
  int offset;
9739
9740
206
  if (note->descsz == sizeof (prstatus_t))
9741
1
    {
9742
1
      prstatus_t prstat;
9743
9744
1
      size = sizeof (prstat.pr_reg);
9745
1
      offset   = offsetof (prstatus_t, pr_reg);
9746
1
      memcpy (&prstat, note->descdata, sizeof (prstat));
9747
9748
      /* Do not overwrite the core signal if it
9749
   has already been set by another thread.  */
9750
1
      if (elf_tdata (abfd)->core->signal == 0)
9751
1
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9752
1
      if (elf_tdata (abfd)->core->pid == 0)
9753
1
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
9754
9755
      /* pr_who exists on:
9756
   solaris 2.5+
9757
   unixware 4.2
9758
   pr_who doesn't exist on:
9759
   linux 2.[01]
9760
   */
9761
#if defined (HAVE_PRSTATUS_T_PR_WHO)
9762
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9763
#else
9764
1
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9765
1
#endif
9766
1
    }
9767
205
#if defined (HAVE_PRSTATUS32_T)
9768
205
  else if (note->descsz == sizeof (prstatus32_t))
9769
0
    {
9770
      /* 64-bit host, 32-bit corefile */
9771
0
      prstatus32_t prstat;
9772
9773
0
      size = sizeof (prstat.pr_reg);
9774
0
      offset   = offsetof (prstatus32_t, pr_reg);
9775
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
9776
9777
      /* Do not overwrite the core signal if it
9778
   has already been set by another thread.  */
9779
0
      if (elf_tdata (abfd)->core->signal == 0)
9780
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9781
0
      if (elf_tdata (abfd)->core->pid == 0)
9782
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
9783
9784
      /* pr_who exists on:
9785
   solaris 2.5+
9786
   unixware 4.2
9787
   pr_who doesn't exist on:
9788
   linux 2.[01]
9789
   */
9790
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
9791
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9792
#else
9793
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9794
0
#endif
9795
0
    }
9796
205
#endif /* HAVE_PRSTATUS32_T */
9797
205
  else
9798
205
    {
9799
      /* Fail - we don't know how to handle any other
9800
   note size (ie. data object type).  */
9801
205
      return true;
9802
205
    }
9803
9804
  /* Make a ".reg/999" section and a ".reg" section.  */
9805
1
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9806
1
            size, note->descpos + offset);
9807
206
}
9808
#endif /* defined (HAVE_PRSTATUS_T) */
9809
9810
/* Create a pseudosection containing the exact contents of NOTE.  */
9811
static bool
9812
elfcore_make_note_pseudosection (bfd *abfd,
9813
         char *name,
9814
         Elf_Internal_Note *note)
9815
89
{
9816
89
  return _bfd_elfcore_make_pseudosection (abfd, name,
9817
89
            note->descsz, note->descpos);
9818
89
}
9819
9820
/* There isn't a consistent prfpregset_t across platforms,
9821
   but it doesn't matter, because we don't have to pick this
9822
   data structure apart.  */
9823
9824
static bool
9825
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9826
50
{
9827
50
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9828
50
}
9829
9830
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9831
   type of NT_PRXFPREG.  Just include the whole note's contents
9832
   literally.  */
9833
9834
static bool
9835
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9836
0
{
9837
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9838
0
}
9839
9840
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9841
   with a note type of NT_X86_XSTATE.  Just include the whole note's
9842
   contents literally.  */
9843
9844
static bool
9845
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9846
0
{
9847
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9848
0
}
9849
9850
static bool
9851
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9852
0
{
9853
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9854
0
}
9855
9856
static bool
9857
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9858
0
{
9859
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9860
0
}
9861
9862
static bool
9863
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9864
0
{
9865
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9866
0
}
9867
9868
static bool
9869
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9870
0
{
9871
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9872
0
}
9873
9874
static bool
9875
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9876
0
{
9877
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9878
0
}
9879
9880
static bool
9881
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9882
0
{
9883
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9884
0
}
9885
9886
static bool
9887
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9888
0
{
9889
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9890
0
}
9891
9892
static bool
9893
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9894
0
{
9895
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9896
0
}
9897
9898
static bool
9899
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9900
0
{
9901
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9902
0
}
9903
9904
static bool
9905
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9906
0
{
9907
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9908
0
}
9909
9910
static bool
9911
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9912
0
{
9913
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9914
0
}
9915
9916
static bool
9917
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9918
0
{
9919
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9920
0
}
9921
9922
static bool
9923
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9924
0
{
9925
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9926
0
}
9927
9928
static bool
9929
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9930
0
{
9931
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9932
0
}
9933
9934
static bool
9935
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9936
0
{
9937
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9938
0
}
9939
9940
static bool
9941
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9942
0
{
9943
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9944
0
}
9945
9946
static bool
9947
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9948
0
{
9949
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9950
0
}
9951
9952
static bool
9953
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9954
0
{
9955
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9956
0
}
9957
9958
static bool
9959
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9960
0
{
9961
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9962
0
}
9963
9964
static bool
9965
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9966
0
{
9967
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9968
0
}
9969
9970
static bool
9971
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9972
0
{
9973
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9974
0
}
9975
9976
static bool
9977
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9978
0
{
9979
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9980
0
}
9981
9982
static bool
9983
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9984
0
{
9985
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9986
0
}
9987
9988
static bool
9989
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9990
0
{
9991
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9992
0
}
9993
9994
static bool
9995
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9996
0
{
9997
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9998
0
}
9999
10000
static bool
10001
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10002
0
{
10003
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10004
0
}
10005
10006
static bool
10007
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10008
0
{
10009
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10010
0
}
10011
10012
static bool
10013
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10014
0
{
10015
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10016
0
}
10017
10018
static bool
10019
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10020
0
{
10021
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10022
0
}
10023
10024
static bool
10025
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10026
0
{
10027
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10028
0
}
10029
10030
static bool
10031
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10032
0
{
10033
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10034
0
}
10035
10036
static bool
10037
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10038
0
{
10039
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10040
0
}
10041
10042
static bool
10043
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10044
0
{
10045
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10046
0
}
10047
10048
static bool
10049
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10050
0
{
10051
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10052
0
}
10053
10054
static bool
10055
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10056
0
{
10057
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10058
0
            note);
10059
0
}
10060
10061
static bool
10062
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10063
0
{
10064
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10065
0
}
10066
10067
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10068
   successful otherwise, return FALSE.  */
10069
10070
static bool
10071
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10072
0
{
10073
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10074
0
}
10075
10076
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10077
   successful otherwise, return FALSE.  */
10078
10079
static bool
10080
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10081
0
{
10082
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10083
0
}
10084
10085
static bool
10086
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10087
0
{
10088
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10089
0
}
10090
10091
static bool
10092
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10093
0
{
10094
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10095
0
}
10096
10097
static bool
10098
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10099
0
{
10100
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10101
0
}
10102
10103
static bool
10104
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10105
0
{
10106
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10107
0
}
10108
10109
#if defined (HAVE_PRPSINFO_T)
10110
typedef prpsinfo_t   elfcore_psinfo_t;
10111
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10112
typedef prpsinfo32_t elfcore_psinfo32_t;
10113
#endif
10114
#endif
10115
10116
#if defined (HAVE_PSINFO_T)
10117
typedef psinfo_t   elfcore_psinfo_t;
10118
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10119
typedef psinfo32_t elfcore_psinfo32_t;
10120
#endif
10121
#endif
10122
10123
/* return a malloc'ed copy of a string at START which is at
10124
   most MAX bytes long, possibly without a terminating '\0'.
10125
   the copy will always have a terminating '\0'.  */
10126
10127
char *
10128
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10129
8
{
10130
8
  char *dups;
10131
8
  char *end = (char *) memchr (start, '\0', max);
10132
8
  size_t len;
10133
10134
8
  if (end == NULL)
10135
0
    len = max;
10136
8
  else
10137
8
    len = end - start;
10138
10139
8
  dups = (char *) bfd_alloc (abfd, len + 1);
10140
8
  if (dups == NULL)
10141
0
    return NULL;
10142
10143
8
  memcpy (dups, start, len);
10144
8
  dups[len] = '\0';
10145
10146
8
  return dups;
10147
8
}
10148
10149
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10150
static bool
10151
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10152
87
{
10153
87
  if (note->descsz == sizeof (elfcore_psinfo_t))
10154
1
    {
10155
1
      elfcore_psinfo_t psinfo;
10156
10157
1
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10158
10159
1
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10160
1
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10161
1
#endif
10162
1
      elf_tdata (abfd)->core->program
10163
1
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10164
1
        sizeof (psinfo.pr_fname));
10165
10166
1
      elf_tdata (abfd)->core->command
10167
1
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10168
1
        sizeof (psinfo.pr_psargs));
10169
1
    }
10170
86
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10171
86
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10172
0
    {
10173
      /* 64-bit host, 32-bit corefile */
10174
0
      elfcore_psinfo32_t psinfo;
10175
10176
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10177
10178
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10179
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10180
0
#endif
10181
0
      elf_tdata (abfd)->core->program
10182
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10183
0
        sizeof (psinfo.pr_fname));
10184
10185
0
      elf_tdata (abfd)->core->command
10186
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10187
0
        sizeof (psinfo.pr_psargs));
10188
0
    }
10189
86
#endif
10190
10191
86
  else
10192
86
    {
10193
      /* Fail - we don't know how to handle any other
10194
   note size (ie. data object type).  */
10195
86
      return true;
10196
86
    }
10197
10198
  /* Note that for some reason, a spurious space is tacked
10199
     onto the end of the args in some (at least one anyway)
10200
     implementations, so strip it off if it exists.  */
10201
10202
1
  {
10203
1
    char *command = elf_tdata (abfd)->core->command;
10204
1
    int n = strlen (command);
10205
10206
1
    if (0 < n && command[n - 1] == ' ')
10207
0
      command[n - 1] = '\0';
10208
1
  }
10209
10210
1
  return true;
10211
87
}
10212
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10213
10214
#if defined (HAVE_PSTATUS_T)
10215
static bool
10216
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10217
{
10218
  if (note->descsz == sizeof (pstatus_t)
10219
#if defined (HAVE_PXSTATUS_T)
10220
      || note->descsz == sizeof (pxstatus_t)
10221
#endif
10222
      )
10223
    {
10224
      pstatus_t pstat;
10225
10226
      memcpy (&pstat, note->descdata, sizeof (pstat));
10227
10228
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10229
    }
10230
#if defined (HAVE_PSTATUS32_T)
10231
  else if (note->descsz == sizeof (pstatus32_t))
10232
    {
10233
      /* 64-bit host, 32-bit corefile */
10234
      pstatus32_t pstat;
10235
10236
      memcpy (&pstat, note->descdata, sizeof (pstat));
10237
10238
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10239
    }
10240
#endif
10241
  /* Could grab some more details from the "representative"
10242
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10243
     NT_LWPSTATUS note, presumably.  */
10244
10245
  return true;
10246
}
10247
#endif /* defined (HAVE_PSTATUS_T) */
10248
10249
#if defined (HAVE_LWPSTATUS_T)
10250
static bool
10251
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10252
{
10253
  lwpstatus_t lwpstat;
10254
  char buf[100];
10255
  char *name;
10256
  size_t len;
10257
  asection *sect;
10258
10259
  if (note->descsz != sizeof (lwpstat)
10260
#if defined (HAVE_LWPXSTATUS_T)
10261
      && note->descsz != sizeof (lwpxstatus_t)
10262
#endif
10263
      )
10264
    return true;
10265
10266
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10267
10268
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10269
  /* Do not overwrite the core signal if it has already been set by
10270
     another thread.  */
10271
  if (elf_tdata (abfd)->core->signal == 0)
10272
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10273
10274
  /* Make a ".reg/999" section.  */
10275
10276
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10277
  len = strlen (buf) + 1;
10278
  name = bfd_alloc (abfd, len);
10279
  if (name == NULL)
10280
    return false;
10281
  memcpy (name, buf, len);
10282
10283
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10284
  if (sect == NULL)
10285
    return false;
10286
10287
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10288
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10289
  sect->filepos = note->descpos
10290
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10291
#endif
10292
10293
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10294
  sect->size = sizeof (lwpstat.pr_reg);
10295
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10296
#endif
10297
10298
  sect->alignment_power = 2;
10299
10300
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10301
    return false;
10302
10303
  /* Make a ".reg2/999" section */
10304
10305
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10306
  len = strlen (buf) + 1;
10307
  name = bfd_alloc (abfd, len);
10308
  if (name == NULL)
10309
    return false;
10310
  memcpy (name, buf, len);
10311
10312
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10313
  if (sect == NULL)
10314
    return false;
10315
10316
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10317
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10318
  sect->filepos = note->descpos
10319
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10320
#endif
10321
10322
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10323
  sect->size = sizeof (lwpstat.pr_fpreg);
10324
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10325
#endif
10326
10327
  sect->alignment_power = 2;
10328
10329
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10330
}
10331
#endif /* defined (HAVE_LWPSTATUS_T) */
10332
10333
/* These constants, and the structure offsets used below, are defined by
10334
   Cygwin's core_dump.h */
10335
0
#define NOTE_INFO_PROCESS  1
10336
0
#define NOTE_INFO_THREAD   2
10337
0
#define NOTE_INFO_MODULE   3
10338
0
#define NOTE_INFO_MODULE64 4
10339
10340
static bool
10341
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10342
29
{
10343
29
  char buf[30];
10344
29
  char *name;
10345
29
  size_t len;
10346
29
  unsigned int name_size;
10347
29
  asection *sect;
10348
29
  unsigned int type;
10349
29
  int is_active_thread;
10350
29
  bfd_vma base_addr;
10351
10352
29
  if (note->descsz < 4)
10353
19
    return true;
10354
10355
10
  if (! startswith (note->namedata, "win32"))
10356
10
    return true;
10357
10358
0
  type = bfd_get_32 (abfd, note->descdata);
10359
10360
0
  struct
10361
0
  {
10362
0
    const char *type_name;
10363
0
    unsigned long min_size;
10364
0
  } size_check[] =
10365
0
      {
10366
0
       { "NOTE_INFO_PROCESS", 12 },
10367
0
       { "NOTE_INFO_THREAD", 12 },
10368
0
       { "NOTE_INFO_MODULE", 12 },
10369
0
       { "NOTE_INFO_MODULE64", 16 },
10370
0
      };
10371
10372
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10373
0
      return true;
10374
10375
0
  if (note->descsz < size_check[type - 1].min_size)
10376
0
    {
10377
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10378
0
          " is too small"),
10379
0
        abfd, size_check[type - 1].type_name, note->descsz);
10380
0
      return true;
10381
0
    }
10382
10383
0
  switch (type)
10384
0
    {
10385
0
    case NOTE_INFO_PROCESS:
10386
      /* FIXME: need to add ->core->command.  */
10387
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10388
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10389
0
      break;
10390
10391
0
    case NOTE_INFO_THREAD:
10392
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10393
   structure. */
10394
      /* thread_info.tid */
10395
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10396
10397
0
      len = strlen (buf) + 1;
10398
0
      name = (char *) bfd_alloc (abfd, len);
10399
0
      if (name == NULL)
10400
0
  return false;
10401
10402
0
      memcpy (name, buf, len);
10403
10404
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10405
0
      if (sect == NULL)
10406
0
  return false;
10407
10408
      /* sizeof (thread_info.thread_context) */
10409
0
      sect->size = note->descsz - 12;
10410
      /* offsetof (thread_info.thread_context) */
10411
0
      sect->filepos = note->descpos + 12;
10412
0
      sect->alignment_power = 2;
10413
10414
      /* thread_info.is_active_thread */
10415
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10416
10417
0
      if (is_active_thread)
10418
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10419
0
    return false;
10420
0
      break;
10421
10422
0
    case NOTE_INFO_MODULE:
10423
0
    case NOTE_INFO_MODULE64:
10424
      /* Make a ".module/xxxxxxxx" section.  */
10425
0
      if (type == NOTE_INFO_MODULE)
10426
0
  {
10427
    /* module_info.base_address */
10428
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
10429
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10430
    /* module_info.module_name_size */
10431
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
10432
0
  }
10433
0
      else /* NOTE_INFO_MODULE64 */
10434
0
  {
10435
    /* module_info.base_address */
10436
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
10437
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10438
    /* module_info.module_name_size */
10439
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
10440
0
  }
10441
10442
0
      len = strlen (buf) + 1;
10443
0
      name = (char *) bfd_alloc (abfd, len);
10444
0
      if (name == NULL)
10445
0
  return false;
10446
10447
0
      memcpy (name, buf, len);
10448
10449
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10450
10451
0
      if (sect == NULL)
10452
0
  return false;
10453
10454
0
      if (note->descsz < 12 + name_size)
10455
0
  {
10456
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10457
0
        " is too small to contain a name of size %u"),
10458
0
            abfd, note->descsz, name_size);
10459
0
    return true;
10460
0
  }
10461
10462
0
      sect->size = note->descsz;
10463
0
      sect->filepos = note->descpos;
10464
0
      sect->alignment_power = 2;
10465
0
      break;
10466
10467
0
    default:
10468
0
      return true;
10469
0
    }
10470
10471
0
  return true;
10472
0
}
10473
10474
static bool
10475
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10476
1.66k
{
10477
1.66k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10478
10479
1.66k
  switch (note->type)
10480
1.66k
    {
10481
682
    default:
10482
682
      return true;
10483
10484
206
    case NT_PRSTATUS:
10485
206
      if (bed->elf_backend_grok_prstatus)
10486
104
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10487
0
    return true;
10488
206
#if defined (HAVE_PRSTATUS_T)
10489
206
      return elfcore_grok_prstatus (abfd, note);
10490
#else
10491
      return true;
10492
#endif
10493
10494
#if defined (HAVE_PSTATUS_T)
10495
    case NT_PSTATUS:
10496
      return elfcore_grok_pstatus (abfd, note);
10497
#endif
10498
10499
#if defined (HAVE_LWPSTATUS_T)
10500
    case NT_LWPSTATUS:
10501
      return elfcore_grok_lwpstatus (abfd, note);
10502
#endif
10503
10504
50
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
10505
50
      return elfcore_grok_prfpreg (abfd, note);
10506
10507
29
    case NT_WIN32PSTATUS:
10508
29
      return elfcore_grok_win32pstatus (abfd, note);
10509
10510
0
    case NT_PRXFPREG:   /* Linux SSE extension */
10511
0
      if (note->namesz == 6
10512
0
    && strcmp (note->namedata, "LINUX") == 0)
10513
0
  return elfcore_grok_prxfpreg (abfd, note);
10514
0
      else
10515
0
  return true;
10516
10517
15
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
10518
15
      if (note->namesz == 6
10519
15
    && strcmp (note->namedata, "LINUX") == 0)
10520
0
  return elfcore_grok_xstatereg (abfd, note);
10521
15
      else
10522
15
  return true;
10523
10524
54
    case NT_PPC_VMX:
10525
54
      if (note->namesz == 6
10526
54
    && strcmp (note->namedata, "LINUX") == 0)
10527
0
  return elfcore_grok_ppc_vmx (abfd, note);
10528
54
      else
10529
54
  return true;
10530
10531
20
    case NT_PPC_VSX:
10532
20
      if (note->namesz == 6
10533
20
    && strcmp (note->namedata, "LINUX") == 0)
10534
0
  return elfcore_grok_ppc_vsx (abfd, note);
10535
20
      else
10536
20
  return true;
10537
10538
16
    case NT_PPC_TAR:
10539
16
      if (note->namesz == 6
10540
16
    && strcmp (note->namedata, "LINUX") == 0)
10541
0
  return elfcore_grok_ppc_tar (abfd, note);
10542
16
      else
10543
16
  return true;
10544
10545
33
    case NT_PPC_PPR:
10546
33
      if (note->namesz == 6
10547
33
    && strcmp (note->namedata, "LINUX") == 0)
10548
0
  return elfcore_grok_ppc_ppr (abfd, note);
10549
33
      else
10550
33
  return true;
10551
10552
28
    case NT_PPC_DSCR:
10553
28
      if (note->namesz == 6
10554
28
    && strcmp (note->namedata, "LINUX") == 0)
10555
0
  return elfcore_grok_ppc_dscr (abfd, note);
10556
28
      else
10557
28
  return true;
10558
10559
37
    case NT_PPC_EBB:
10560
37
      if (note->namesz == 6
10561
37
    && strcmp (note->namedata, "LINUX") == 0)
10562
0
  return elfcore_grok_ppc_ebb (abfd, note);
10563
37
      else
10564
37
  return true;
10565
10566
25
    case NT_PPC_PMU:
10567
25
      if (note->namesz == 6
10568
25
    && strcmp (note->namedata, "LINUX") == 0)
10569
0
  return elfcore_grok_ppc_pmu (abfd, note);
10570
25
      else
10571
25
  return true;
10572
10573
22
    case NT_PPC_TM_CGPR:
10574
22
      if (note->namesz == 6
10575
22
    && strcmp (note->namedata, "LINUX") == 0)
10576
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
10577
22
      else
10578
22
  return true;
10579
10580
5
    case NT_PPC_TM_CFPR:
10581
5
      if (note->namesz == 6
10582
5
    && strcmp (note->namedata, "LINUX") == 0)
10583
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
10584
5
      else
10585
5
  return true;
10586
10587
0
    case NT_PPC_TM_CVMX:
10588
0
      if (note->namesz == 6
10589
0
    && strcmp (note->namedata, "LINUX") == 0)
10590
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
10591
0
      else
10592
0
  return true;
10593
10594
4
    case NT_PPC_TM_CVSX:
10595
4
      if (note->namesz == 6
10596
4
    && strcmp (note->namedata, "LINUX") == 0)
10597
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
10598
4
      else
10599
4
  return true;
10600
10601
11
    case NT_PPC_TM_SPR:
10602
11
      if (note->namesz == 6
10603
11
    && strcmp (note->namedata, "LINUX") == 0)
10604
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
10605
11
      else
10606
11
  return true;
10607
10608
15
    case NT_PPC_TM_CTAR:
10609
15
      if (note->namesz == 6
10610
15
    && strcmp (note->namedata, "LINUX") == 0)
10611
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
10612
15
      else
10613
15
  return true;
10614
10615
6
    case NT_PPC_TM_CPPR:
10616
6
      if (note->namesz == 6
10617
6
    && strcmp (note->namedata, "LINUX") == 0)
10618
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
10619
6
      else
10620
6
  return true;
10621
10622
26
    case NT_PPC_TM_CDSCR:
10623
26
      if (note->namesz == 6
10624
26
    && strcmp (note->namedata, "LINUX") == 0)
10625
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
10626
26
      else
10627
26
  return true;
10628
10629
32
    case NT_S390_HIGH_GPRS:
10630
32
      if (note->namesz == 6
10631
32
    && strcmp (note->namedata, "LINUX") == 0)
10632
0
  return elfcore_grok_s390_high_gprs (abfd, note);
10633
32
      else
10634
32
  return true;
10635
10636
10
    case NT_S390_TIMER:
10637
10
      if (note->namesz == 6
10638
10
    && strcmp (note->namedata, "LINUX") == 0)
10639
0
  return elfcore_grok_s390_timer (abfd, note);
10640
10
      else
10641
10
  return true;
10642
10643
0
    case NT_S390_TODCMP:
10644
0
      if (note->namesz == 6
10645
0
    && strcmp (note->namedata, "LINUX") == 0)
10646
0
  return elfcore_grok_s390_todcmp (abfd, note);
10647
0
      else
10648
0
  return true;
10649
10650
17
    case NT_S390_TODPREG:
10651
17
      if (note->namesz == 6
10652
17
    && strcmp (note->namedata, "LINUX") == 0)
10653
0
  return elfcore_grok_s390_todpreg (abfd, note);
10654
17
      else
10655
17
  return true;
10656
10657
4
    case NT_S390_CTRS:
10658
4
      if (note->namesz == 6
10659
4
    && strcmp (note->namedata, "LINUX") == 0)
10660
0
  return elfcore_grok_s390_ctrs (abfd, note);
10661
4
      else
10662
4
  return true;
10663
10664
14
    case NT_S390_PREFIX:
10665
14
      if (note->namesz == 6
10666
14
    && strcmp (note->namedata, "LINUX") == 0)
10667
0
  return elfcore_grok_s390_prefix (abfd, note);
10668
14
      else
10669
14
  return true;
10670
10671
6
    case NT_S390_LAST_BREAK:
10672
6
      if (note->namesz == 6
10673
6
    && strcmp (note->namedata, "LINUX") == 0)
10674
0
  return elfcore_grok_s390_last_break (abfd, note);
10675
6
      else
10676
6
  return true;
10677
10678
2
    case NT_S390_SYSTEM_CALL:
10679
2
      if (note->namesz == 6
10680
2
    && strcmp (note->namedata, "LINUX") == 0)
10681
0
  return elfcore_grok_s390_system_call (abfd, note);
10682
2
      else
10683
2
  return true;
10684
10685
1
    case NT_S390_TDB:
10686
1
      if (note->namesz == 6
10687
1
    && strcmp (note->namedata, "LINUX") == 0)
10688
0
  return elfcore_grok_s390_tdb (abfd, note);
10689
1
      else
10690
1
  return true;
10691
10692
3
    case NT_S390_VXRS_LOW:
10693
3
      if (note->namesz == 6
10694
3
    && strcmp (note->namedata, "LINUX") == 0)
10695
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
10696
3
      else
10697
3
  return true;
10698
10699
4
    case NT_S390_VXRS_HIGH:
10700
4
      if (note->namesz == 6
10701
4
    && strcmp (note->namedata, "LINUX") == 0)
10702
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
10703
4
      else
10704
4
  return true;
10705
10706
7
    case NT_S390_GS_CB:
10707
7
      if (note->namesz == 6
10708
7
    && strcmp (note->namedata, "LINUX") == 0)
10709
0
  return elfcore_grok_s390_gs_cb (abfd, note);
10710
7
      else
10711
7
  return true;
10712
10713
4
    case NT_S390_GS_BC:
10714
4
      if (note->namesz == 6
10715
4
    && strcmp (note->namedata, "LINUX") == 0)
10716
0
  return elfcore_grok_s390_gs_bc (abfd, note);
10717
4
      else
10718
4
  return true;
10719
10720
22
    case NT_ARC_V2:
10721
22
      if (note->namesz == 6
10722
22
    && strcmp (note->namedata, "LINUX") == 0)
10723
0
  return elfcore_grok_arc_v2 (abfd, note);
10724
22
      else
10725
22
  return true;
10726
10727
25
    case NT_ARM_VFP:
10728
25
      if (note->namesz == 6
10729
25
    && strcmp (note->namedata, "LINUX") == 0)
10730
0
  return elfcore_grok_arm_vfp (abfd, note);
10731
25
      else
10732
25
  return true;
10733
10734
18
    case NT_ARM_TLS:
10735
18
      if (note->namesz == 6
10736
18
    && strcmp (note->namedata, "LINUX") == 0)
10737
0
  return elfcore_grok_aarch_tls (abfd, note);
10738
18
      else
10739
18
  return true;
10740
10741
15
    case NT_ARM_HW_BREAK:
10742
15
      if (note->namesz == 6
10743
15
    && strcmp (note->namedata, "LINUX") == 0)
10744
0
  return elfcore_grok_aarch_hw_break (abfd, note);
10745
15
      else
10746
15
  return true;
10747
10748
1
    case NT_ARM_HW_WATCH:
10749
1
      if (note->namesz == 6
10750
1
    && strcmp (note->namedata, "LINUX") == 0)
10751
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
10752
1
      else
10753
1
  return true;
10754
10755
22
    case NT_ARM_SVE:
10756
22
      if (note->namesz == 6
10757
22
    && strcmp (note->namedata, "LINUX") == 0)
10758
0
  return elfcore_grok_aarch_sve (abfd, note);
10759
22
      else
10760
22
  return true;
10761
10762
8
    case NT_ARM_PAC_MASK:
10763
8
      if (note->namesz == 6
10764
8
    && strcmp (note->namedata, "LINUX") == 0)
10765
0
  return elfcore_grok_aarch_pauth (abfd, note);
10766
8
      else
10767
8
  return true;
10768
10769
3
    case NT_ARM_TAGGED_ADDR_CTRL:
10770
3
      if (note->namesz == 6
10771
3
    && strcmp (note->namedata, "LINUX") == 0)
10772
0
  return elfcore_grok_aarch_mte (abfd, note);
10773
3
      else
10774
3
  return true;
10775
10776
19
    case NT_GDB_TDESC:
10777
19
      if (note->namesz == 4
10778
19
    && strcmp (note->namedata, "GDB") == 0)
10779
0
  return elfcore_grok_gdb_tdesc (abfd, note);
10780
19
      else
10781
19
  return true;
10782
10783
6
    case NT_RISCV_CSR:
10784
6
      if (note->namesz == 4
10785
6
    && strcmp (note->namedata, "GDB") == 0)
10786
0
  return elfcore_grok_riscv_csr (abfd, note);
10787
6
      else
10788
6
  return true;
10789
10790
20
    case NT_LARCH_CPUCFG:
10791
20
      if (note->namesz == 6
10792
20
    && strcmp (note->namedata, "LINUX") == 0)
10793
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
10794
20
      else
10795
20
  return true;
10796
10797
1
    case NT_LARCH_LBT:
10798
1
      if (note->namesz == 6
10799
1
    && strcmp (note->namedata, "LINUX") == 0)
10800
0
  return elfcore_grok_loongarch_lbt (abfd, note);
10801
1
      else
10802
1
  return true;
10803
10804
14
    case NT_LARCH_LSX:
10805
14
      if (note->namesz == 6
10806
14
    && strcmp (note->namedata, "LINUX") == 0)
10807
0
  return elfcore_grok_loongarch_lsx (abfd, note);
10808
14
      else
10809
14
  return true;
10810
10811
4
    case NT_LARCH_LASX:
10812
4
      if (note->namesz == 6
10813
4
    && strcmp (note->namedata, "LINUX") == 0)
10814
0
  return elfcore_grok_loongarch_lasx (abfd, note);
10815
4
      else
10816
4
  return true;
10817
10818
46
    case NT_PRPSINFO:
10819
90
    case NT_PSINFO:
10820
90
      if (bed->elf_backend_grok_psinfo)
10821
61
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10822
3
    return true;
10823
87
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10824
87
      return elfcore_grok_psinfo (abfd, note);
10825
#else
10826
      return true;
10827
#endif
10828
10829
7
    case NT_AUXV:
10830
7
      return elfcore_make_auxv_note_section (abfd, note, 0);
10831
10832
0
    case NT_FILE:
10833
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10834
0
                note);
10835
10836
0
    case NT_SIGINFO:
10837
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10838
0
                note);
10839
10840
1.66k
    }
10841
1.66k
}
10842
10843
static bool
10844
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10845
761
{
10846
761
  struct bfd_build_id* build_id;
10847
10848
761
  if (note->descsz == 0)
10849
65
    return false;
10850
10851
696
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10852
696
  if (build_id == NULL)
10853
0
    return false;
10854
10855
696
  build_id->size = note->descsz;
10856
696
  memcpy (build_id->data, note->descdata, note->descsz);
10857
696
  abfd->build_id = build_id;
10858
10859
696
  return true;
10860
696
}
10861
10862
static bool
10863
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10864
2.29k
{
10865
2.29k
  switch (note->type)
10866
2.29k
    {
10867
912
    default:
10868
912
      return true;
10869
10870
625
    case NT_GNU_PROPERTY_TYPE_0:
10871
625
      return _bfd_elf_parse_gnu_properties (abfd, note);
10872
10873
761
    case NT_GNU_BUILD_ID:
10874
761
      return elfobj_grok_gnu_build_id (abfd, note);
10875
2.29k
    }
10876
2.29k
}
10877
10878
static bool
10879
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10880
0
{
10881
0
  struct sdt_note *cur =
10882
0
    (struct sdt_note *) bfd_alloc (abfd,
10883
0
           sizeof (struct sdt_note) + note->descsz);
10884
10885
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10886
0
  cur->size = (bfd_size_type) note->descsz;
10887
0
  memcpy (cur->data, note->descdata, note->descsz);
10888
10889
0
  elf_tdata (abfd)->sdt_note_head = cur;
10890
10891
0
  return true;
10892
0
}
10893
10894
static bool
10895
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10896
0
{
10897
0
  switch (note->type)
10898
0
    {
10899
0
    case NT_STAPSDT:
10900
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
10901
10902
0
    default:
10903
0
      return true;
10904
0
    }
10905
0
}
10906
10907
static bool
10908
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10909
0
{
10910
0
  size_t offset;
10911
10912
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10913
0
    {
10914
0
    case ELFCLASS32:
10915
0
      if (note->descsz < 108)
10916
0
  return false;
10917
0
      break;
10918
10919
0
    case ELFCLASS64:
10920
0
      if (note->descsz < 120)
10921
0
  return false;
10922
0
      break;
10923
10924
0
    default:
10925
0
      return false;
10926
0
    }
10927
10928
  /* Check for version 1 in pr_version.  */
10929
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10930
0
    return false;
10931
10932
0
  offset = 4;
10933
10934
  /* Skip over pr_psinfosz. */
10935
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10936
0
    offset += 4;
10937
0
  else
10938
0
    {
10939
0
      offset += 4;  /* Padding before pr_psinfosz. */
10940
0
      offset += 8;
10941
0
    }
10942
10943
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
10944
0
  elf_tdata (abfd)->core->program
10945
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10946
0
  offset += 17;
10947
10948
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
10949
0
  elf_tdata (abfd)->core->command
10950
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10951
0
  offset += 81;
10952
10953
  /* Padding before pr_pid.  */
10954
0
  offset += 2;
10955
10956
  /* The pr_pid field was added in version "1a".  */
10957
0
  if (note->descsz < offset + 4)
10958
0
    return true;
10959
10960
0
  elf_tdata (abfd)->core->pid
10961
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10962
10963
0
  return true;
10964
0
}
10965
10966
static bool
10967
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10968
1
{
10969
1
  size_t offset;
10970
1
  size_t size;
10971
1
  size_t min_size;
10972
10973
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
10974
     Also compute minimum size of this note.  */
10975
1
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10976
1
    {
10977
0
    case ELFCLASS32:
10978
0
      offset = 4 + 4;
10979
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
10980
0
      break;
10981
10982
1
    case ELFCLASS64:
10983
1
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
10984
1
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10985
1
      break;
10986
10987
0
    default:
10988
0
      return false;
10989
1
    }
10990
10991
1
  if (note->descsz < min_size)
10992
1
    return false;
10993
10994
  /* Check for version 1 in pr_version.  */
10995
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10996
0
    return false;
10997
10998
  /* Extract size of pr_reg from pr_gregsetsz.  */
10999
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11000
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11001
0
    {
11002
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11003
0
      offset += 4 * 2;
11004
0
    }
11005
0
  else
11006
0
    {
11007
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11008
0
      offset += 8 * 2;
11009
0
    }
11010
11011
  /* Skip over pr_osreldate.  */
11012
0
  offset += 4;
11013
11014
  /* Read signal from pr_cursig.  */
11015
0
  if (elf_tdata (abfd)->core->signal == 0)
11016
0
    elf_tdata (abfd)->core->signal
11017
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11018
0
  offset += 4;
11019
11020
  /* Read TID from pr_pid.  */
11021
0
  elf_tdata (abfd)->core->lwpid
11022
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11023
0
  offset += 4;
11024
11025
  /* Padding before pr_reg.  */
11026
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11027
0
    offset += 4;
11028
11029
  /* Make sure that there is enough data remaining in the note.  */
11030
0
  if ((note->descsz - offset) < size)
11031
0
    return false;
11032
11033
  /* Make a ".reg/999" section and a ".reg" section.  */
11034
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11035
0
            size, note->descpos + offset);
11036
0
}
11037
11038
static bool
11039
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11040
46
{
11041
46
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11042
11043
46
  switch (note->type)
11044
46
    {
11045
1
    case NT_PRSTATUS:
11046
1
      if (bed->elf_backend_grok_freebsd_prstatus)
11047
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11048
0
    return true;
11049
1
      return elfcore_grok_freebsd_prstatus (abfd, note);
11050
11051
0
    case NT_FPREGSET:
11052
0
      return elfcore_grok_prfpreg (abfd, note);
11053
11054
0
    case NT_PRPSINFO:
11055
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11056
11057
6
    case NT_FREEBSD_THRMISC:
11058
6
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11059
11060
11
    case NT_FREEBSD_PROCSTAT_PROC:
11061
11
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11062
11
                note);
11063
11064
6
    case NT_FREEBSD_PROCSTAT_FILES:
11065
6
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11066
6
                note);
11067
11068
15
    case NT_FREEBSD_PROCSTAT_VMMAP:
11069
15
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11070
15
                note);
11071
11072
1
    case NT_FREEBSD_PROCSTAT_AUXV:
11073
1
      return elfcore_make_auxv_note_section (abfd, note, 4);
11074
11075
1
    case NT_FREEBSD_X86_SEGBASES:
11076
1
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11077
11078
0
    case NT_X86_XSTATE:
11079
0
      return elfcore_grok_xstatereg (abfd, note);
11080
11081
0
    case NT_FREEBSD_PTLWPINFO:
11082
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11083
0
                note);
11084
11085
0
    case NT_ARM_TLS:
11086
0
      return elfcore_grok_aarch_tls (abfd, note);
11087
11088
0
    case NT_ARM_VFP:
11089
0
      return elfcore_grok_arm_vfp (abfd, note);
11090
11091
5
    default:
11092
5
      return true;
11093
46
    }
11094
46
}
11095
11096
static bool
11097
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11098
4
{
11099
4
  char *cp;
11100
11101
4
  cp = strchr (note->namedata, '@');
11102
4
  if (cp != NULL)
11103
0
    {
11104
0
      *lwpidp = atoi(cp + 1);
11105
0
      return true;
11106
0
    }
11107
4
  return false;
11108
4
}
11109
11110
static bool
11111
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11112
0
{
11113
0
  if (note->descsz <= 0x7c + 31)
11114
0
    return false;
11115
11116
  /* Signal number at offset 0x08. */
11117
0
  elf_tdata (abfd)->core->signal
11118
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11119
11120
  /* Process ID at offset 0x50. */
11121
0
  elf_tdata (abfd)->core->pid
11122
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11123
11124
  /* Command name at 0x7c (max 32 bytes, including nul). */
11125
0
  elf_tdata (abfd)->core->command
11126
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11127
11128
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11129
0
            note);
11130
0
}
11131
11132
static bool
11133
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11134
4
{
11135
4
  int lwp;
11136
11137
4
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11138
0
    elf_tdata (abfd)->core->lwpid = lwp;
11139
11140
4
  switch (note->type)
11141
4
    {
11142
0
    case NT_NETBSDCORE_PROCINFO:
11143
      /* NetBSD-specific core "procinfo".  Note that we expect to
11144
   find this note before any of the others, which is fine,
11145
   since the kernel writes this note out first when it
11146
   creates a core file.  */
11147
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11148
0
    case NT_NETBSDCORE_AUXV:
11149
      /* NetBSD-specific Elf Auxiliary Vector data. */
11150
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11151
0
    case NT_NETBSDCORE_LWPSTATUS:
11152
0
      return elfcore_make_note_pseudosection (abfd,
11153
0
                ".note.netbsdcore.lwpstatus",
11154
0
                note);
11155
4
    default:
11156
4
      break;
11157
4
    }
11158
11159
  /* As of March 2020 there are no other machine-independent notes
11160
     defined for NetBSD core files.  If the note type is less
11161
     than the start of the machine-dependent note types, we don't
11162
     understand it.  */
11163
11164
4
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11165
0
    return true;
11166
11167
11168
4
  switch (bfd_get_arch (abfd))
11169
4
    {
11170
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11171
   PT_GETFPREGS == mach+2.  */
11172
11173
0
    case bfd_arch_aarch64:
11174
0
    case bfd_arch_alpha:
11175
0
    case bfd_arch_sparc:
11176
0
      switch (note->type)
11177
0
  {
11178
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11179
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11180
11181
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11182
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11183
11184
0
  default:
11185
0
    return true;
11186
0
  }
11187
11188
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11189
   There's also old PT___GETREGS40 == mach + 1 for old reg
11190
   structure which lacks GBR.  */
11191
11192
0
    case bfd_arch_sh:
11193
0
      switch (note->type)
11194
0
  {
11195
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11196
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11197
11198
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11199
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11200
11201
0
  default:
11202
0
    return true;
11203
0
  }
11204
11205
      /* On all other arch's, PT_GETREGS == mach+1 and
11206
   PT_GETFPREGS == mach+3.  */
11207
11208
4
    default:
11209
4
      switch (note->type)
11210
4
  {
11211
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11212
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11213
11214
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11215
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11216
11217
4
  default:
11218
4
    return true;
11219
4
  }
11220
4
    }
11221
    /* NOTREACHED */
11222
4
}
11223
11224
static bool
11225
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11226
0
{
11227
0
  if (note->descsz <= 0x48 + 31)
11228
0
    return false;
11229
11230
  /* Signal number at offset 0x08. */
11231
0
  elf_tdata (abfd)->core->signal
11232
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11233
11234
  /* Process ID at offset 0x20. */
11235
0
  elf_tdata (abfd)->core->pid
11236
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11237
11238
  /* Command name at 0x48 (max 32 bytes, including nul). */
11239
0
  elf_tdata (abfd)->core->command
11240
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11241
11242
0
  return true;
11243
0
}
11244
11245
/* Processes Solaris's process status note.
11246
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11247
   pid_off ~ offsetof(prstatus_t, pr_pid)
11248
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11249
   gregset_size ~ sizeof(gregset_t)
11250
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11251
11252
static bool
11253
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11254
             int pid_off, int lwpid_off, size_t gregset_size,
11255
             size_t gregset_offset)
11256
0
{
11257
0
  asection *sect = NULL;
11258
0
  elf_tdata (abfd)->core->signal
11259
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11260
0
  elf_tdata (abfd)->core->pid
11261
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11262
0
  elf_tdata (abfd)->core->lwpid
11263
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11264
11265
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11266
0
  if (sect != NULL)
11267
0
    sect->size = gregset_size;
11268
11269
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11270
0
            note->descpos + gregset_offset);
11271
0
}
11272
11273
/* Gets program and arguments from a core.
11274
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11275
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11276
11277
static bool
11278
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11279
        int prog_off, int comm_off)
11280
0
{
11281
0
  elf_tdata (abfd)->core->program
11282
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11283
0
  elf_tdata (abfd)->core->command
11284
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11285
11286
0
  return true;
11287
0
}
11288
11289
/* Processes Solaris's LWP status note.
11290
   gregset_size ~ sizeof(gregset_t)
11291
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11292
   fpregset_size ~ sizeof(fpregset_t)
11293
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11294
11295
static bool
11296
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11297
        size_t gregset_size, int gregset_off,
11298
        size_t fpregset_size, int fpregset_off)
11299
0
{
11300
0
  asection *sect = NULL;
11301
0
  char reg2_section_name[16] = { 0 };
11302
11303
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11304
0
       elf_tdata (abfd)->core->lwpid);
11305
11306
  /* offsetof(lwpstatus_t, pr_lwpid) */
11307
0
  elf_tdata (abfd)->core->lwpid
11308
0
    = bfd_get_32 (abfd, note->descdata + 4);
11309
  /* offsetof(lwpstatus_t, pr_cursig) */
11310
0
  elf_tdata (abfd)->core->signal
11311
0
    = bfd_get_16 (abfd, note->descdata + 12);
11312
11313
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11314
0
  if (sect != NULL)
11315
0
    sect->size = gregset_size;
11316
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11317
0
               note->descpos + gregset_off))
11318
0
    return false;
11319
11320
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11321
0
  if (sect != NULL)
11322
0
    {
11323
0
      sect->size = fpregset_size;
11324
0
      sect->filepos = note->descpos + fpregset_off;
11325
0
      sect->alignment_power = 2;
11326
0
    }
11327
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11328
0
               note->descpos + fpregset_off))
11329
0
    return false;
11330
11331
0
  return true;
11332
0
}
11333
11334
static bool
11335
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11336
7
{
11337
7
  if (note == NULL)
11338
0
    return false;
11339
11340
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11341
     by the size of the descsz which matches the sizeof()
11342
     the type appropriate for that note type (e.g., prstatus_t for
11343
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11344
     on Solaris. The core file bitness may differ from the bitness of
11345
     gdb itself, so fixed values are used instead of sizeof().
11346
     Appropriate fixed offsets are also used to obtain data from
11347
     the note.  */
11348
11349
7
  switch ((int) note->type)
11350
7
    {
11351
2
    case SOLARIS_NT_PRSTATUS:
11352
2
      switch (note->descsz)
11353
2
  {
11354
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11355
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11356
0
                 136, 216, 308, 152, 356);
11357
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11358
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11359
0
                 264, 360, 520, 304, 600);
11360
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11361
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11362
0
                 136, 216, 308, 76, 356);
11363
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11364
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11365
0
                 264, 360, 520, 224, 600);
11366
2
  default:
11367
2
    return true;
11368
2
  }
11369
11370
0
    case SOLARIS_NT_PSINFO:
11371
0
    case SOLARIS_NT_PRPSINFO:
11372
0
      switch (note->descsz)
11373
0
  {
11374
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11375
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11376
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11377
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11378
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11379
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11380
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11381
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11382
0
  default:
11383
0
    return true;
11384
0
  }
11385
11386
4
    case SOLARIS_NT_LWPSTATUS:
11387
4
      switch (note->descsz)
11388
4
  {
11389
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11390
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11391
0
            152, 344, 400, 496);
11392
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11393
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11394
0
            304, 544, 544, 848);
11395
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11396
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11397
0
            76, 344, 380, 420);
11398
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11399
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11400
0
            224, 544, 528, 768);
11401
4
  default:
11402
4
    return true;
11403
4
  }
11404
11405
0
    case SOLARIS_NT_LWPSINFO:
11406
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11407
0
      if (note->descsz == 128 || note->descsz == 152)
11408
0
  elf_tdata (abfd)->core->lwpid =
11409
0
    bfd_get_32 (abfd, note->descdata + 4);
11410
0
      break;
11411
11412
1
    default:
11413
1
      break;
11414
7
    }
11415
11416
1
  return true;
11417
7
}
11418
11419
/* For name starting with "CORE" this may be either a Solaris
11420
   core file or a gdb-generated core file.  Do Solaris-specific
11421
   processing on selected note types first with
11422
   elfcore_grok_solaris_note(), then process the note
11423
   in elfcore_grok_note().  */
11424
11425
static bool
11426
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11427
7
{
11428
7
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11429
0
    return false;
11430
11431
7
  return elfcore_grok_note (abfd, note);
11432
7
}
11433
11434
static bool
11435
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11436
0
{
11437
0
  if (note->type == NT_OPENBSD_PROCINFO)
11438
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11439
11440
0
  if (note->type == NT_OPENBSD_REGS)
11441
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11442
11443
0
  if (note->type == NT_OPENBSD_FPREGS)
11444
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11445
11446
0
  if (note->type == NT_OPENBSD_XFPREGS)
11447
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11448
11449
0
  if (note->type == NT_OPENBSD_AUXV)
11450
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11451
11452
0
  if (note->type == NT_OPENBSD_WCOOKIE)
11453
0
    {
11454
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11455
0
                 SEC_HAS_CONTENTS);
11456
11457
0
      if (sect == NULL)
11458
0
  return false;
11459
0
      sect->size = note->descsz;
11460
0
      sect->filepos = note->descpos;
11461
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11462
11463
0
      return true;
11464
0
    }
11465
11466
0
  return true;
11467
0
}
11468
11469
static bool
11470
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11471
0
{
11472
0
  void *ddata = note->descdata;
11473
0
  char buf[100];
11474
0
  char *name;
11475
0
  asection *sect;
11476
0
  short sig;
11477
0
  unsigned flags;
11478
11479
0
  if (note->descsz < 16)
11480
0
    return false;
11481
11482
  /* nto_procfs_status 'pid' field is at offset 0.  */
11483
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11484
11485
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11486
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11487
11488
  /* nto_procfs_status 'flags' field is at offset 8.  */
11489
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11490
11491
  /* nto_procfs_status 'what' field is at offset 14.  */
11492
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11493
0
    {
11494
0
      elf_tdata (abfd)->core->signal = sig;
11495
0
      elf_tdata (abfd)->core->lwpid = *tid;
11496
0
    }
11497
11498
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11499
     do not come from signals so we make sure we set the current
11500
     thread just in case.  */
11501
0
  if (flags & 0x00000080)
11502
0
    elf_tdata (abfd)->core->lwpid = *tid;
11503
11504
  /* Make a ".qnx_core_status/%d" section.  */
11505
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11506
11507
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11508
0
  if (name == NULL)
11509
0
    return false;
11510
0
  strcpy (name, buf);
11511
11512
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11513
0
  if (sect == NULL)
11514
0
    return false;
11515
11516
0
  sect->size    = note->descsz;
11517
0
  sect->filepos   = note->descpos;
11518
0
  sect->alignment_power = 2;
11519
11520
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11521
0
}
11522
11523
static bool
11524
elfcore_grok_nto_regs (bfd *abfd,
11525
           Elf_Internal_Note *note,
11526
           long tid,
11527
           char *base)
11528
0
{
11529
0
  char buf[100];
11530
0
  char *name;
11531
0
  asection *sect;
11532
11533
  /* Make a "(base)/%d" section.  */
11534
0
  sprintf (buf, "%s/%ld", base, tid);
11535
11536
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11537
0
  if (name == NULL)
11538
0
    return false;
11539
0
  strcpy (name, buf);
11540
11541
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11542
0
  if (sect == NULL)
11543
0
    return false;
11544
11545
0
  sect->size    = note->descsz;
11546
0
  sect->filepos   = note->descpos;
11547
0
  sect->alignment_power = 2;
11548
11549
  /* This is the current thread.  */
11550
0
  if (elf_tdata (abfd)->core->lwpid == tid)
11551
0
    return elfcore_maybe_make_sect (abfd, base, sect);
11552
11553
0
  return true;
11554
0
}
11555
11556
static bool
11557
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11558
5
{
11559
  /* Every GREG section has a STATUS section before it.  Store the
11560
     tid from the previous call to pass down to the next gregs
11561
     function.  */
11562
5
  static long tid = 1;
11563
11564
5
  switch (note->type)
11565
5
    {
11566
0
    case QNT_CORE_INFO:
11567
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11568
0
    case QNT_CORE_STATUS:
11569
0
      return elfcore_grok_nto_status (abfd, note, &tid);
11570
0
    case QNT_CORE_GREG:
11571
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11572
0
    case QNT_CORE_FPREG:
11573
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11574
5
    default:
11575
5
      return true;
11576
5
    }
11577
5
}
11578
11579
static bool
11580
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11581
0
{
11582
0
  char *name;
11583
0
  asection *sect;
11584
0
  size_t len;
11585
11586
  /* Use note name as section name.  */
11587
0
  len = note->namesz;
11588
0
  name = (char *) bfd_alloc (abfd, len);
11589
0
  if (name == NULL)
11590
0
    return false;
11591
0
  memcpy (name, note->namedata, len);
11592
0
  name[len - 1] = '\0';
11593
11594
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11595
0
  if (sect == NULL)
11596
0
    return false;
11597
11598
0
  sect->size    = note->descsz;
11599
0
  sect->filepos   = note->descpos;
11600
0
  sect->alignment_power = 1;
11601
11602
0
  return true;
11603
0
}
11604
11605
/* Function: elfcore_write_note
11606
11607
   Inputs:
11608
     buffer to hold note, and current size of buffer
11609
     name of note
11610
     type of note
11611
     data for note
11612
     size of data for note
11613
11614
   Writes note to end of buffer.  ELF64 notes are written exactly as
11615
   for ELF32, despite the current (as of 2006) ELF gabi specifying
11616
   that they ought to have 8-byte namesz and descsz field, and have
11617
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
11618
11619
   Return:
11620
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
11621
11622
char *
11623
elfcore_write_note (bfd *abfd,
11624
        char *buf,
11625
        int *bufsiz,
11626
        const char *name,
11627
        int type,
11628
        const void *input,
11629
        int size)
11630
0
{
11631
0
  Elf_External_Note *xnp;
11632
0
  size_t namesz;
11633
0
  size_t newspace;
11634
0
  char *dest;
11635
11636
0
  namesz = 0;
11637
0
  if (name != NULL)
11638
0
    namesz = strlen (name) + 1;
11639
11640
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11641
11642
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
11643
0
  if (buf == NULL)
11644
0
    return buf;
11645
0
  dest = buf + *bufsiz;
11646
0
  *bufsiz += newspace;
11647
0
  xnp = (Elf_External_Note *) dest;
11648
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
11649
0
  H_PUT_32 (abfd, size, xnp->descsz);
11650
0
  H_PUT_32 (abfd, type, xnp->type);
11651
0
  dest = xnp->name;
11652
0
  if (name != NULL)
11653
0
    {
11654
0
      memcpy (dest, name, namesz);
11655
0
      dest += namesz;
11656
0
      while (namesz & 3)
11657
0
  {
11658
0
    *dest++ = '\0';
11659
0
    ++namesz;
11660
0
  }
11661
0
    }
11662
0
  memcpy (dest, input, size);
11663
0
  dest += size;
11664
0
  while (size & 3)
11665
0
    {
11666
0
      *dest++ = '\0';
11667
0
      ++size;
11668
0
    }
11669
0
  return buf;
11670
0
}
11671
11672
/* gcc-8 warns (*) on all the strncpy calls in this function about
11673
   possible string truncation.  The "truncation" is not a bug.  We
11674
   have an external representation of structs with fields that are not
11675
   necessarily NULL terminated and corresponding internal
11676
   representation fields that are one larger so that they can always
11677
   be NULL terminated.
11678
   gcc versions between 4.2 and 4.6 do not allow pragma control of
11679
   diagnostics inside functions, giving a hard error if you try to use
11680
   the finer control available with later versions.
11681
   gcc prior to 4.2 warns about diagnostic push and pop.
11682
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11683
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11684
   (*) Depending on your system header files!  */
11685
#if GCC_VERSION >= 8000
11686
# pragma GCC diagnostic push
11687
# pragma GCC diagnostic ignored "-Wstringop-truncation"
11688
#endif
11689
char *
11690
elfcore_write_prpsinfo (bfd  *abfd,
11691
      char *buf,
11692
      int  *bufsiz,
11693
      const char *fname,
11694
      const char *psargs)
11695
0
{
11696
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11697
11698
0
  if (bed->elf_backend_write_core_note != NULL)
11699
0
    {
11700
0
      char *ret;
11701
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11702
0
             NT_PRPSINFO, fname, psargs);
11703
0
      if (ret != NULL)
11704
0
  return ret;
11705
0
    }
11706
11707
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11708
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11709
0
  if (bed->s->elfclass == ELFCLASS32)
11710
0
    {
11711
#  if defined (HAVE_PSINFO32_T)
11712
      psinfo32_t data;
11713
      int note_type = NT_PSINFO;
11714
#  else
11715
0
      prpsinfo32_t data;
11716
0
      int note_type = NT_PRPSINFO;
11717
0
#  endif
11718
11719
0
      memset (&data, 0, sizeof (data));
11720
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11721
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11722
0
      return elfcore_write_note (abfd, buf, bufsiz,
11723
0
         "CORE", note_type, &data, sizeof (data));
11724
0
    }
11725
0
  else
11726
0
# endif
11727
0
    {
11728
# if defined (HAVE_PSINFO_T)
11729
      psinfo_t data;
11730
      int note_type = NT_PSINFO;
11731
# else
11732
0
      prpsinfo_t data;
11733
0
      int note_type = NT_PRPSINFO;
11734
0
# endif
11735
11736
0
      memset (&data, 0, sizeof (data));
11737
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11738
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11739
0
      return elfcore_write_note (abfd, buf, bufsiz,
11740
0
         "CORE", note_type, &data, sizeof (data));
11741
0
    }
11742
0
#endif  /* PSINFO_T or PRPSINFO_T */
11743
11744
0
  free (buf);
11745
0
  return NULL;
11746
0
}
11747
#if GCC_VERSION >= 8000
11748
# pragma GCC diagnostic pop
11749
#endif
11750
11751
char *
11752
elfcore_write_linux_prpsinfo32
11753
  (bfd *abfd, char *buf, int *bufsiz,
11754
   const struct elf_internal_linux_prpsinfo *prpsinfo)
11755
0
{
11756
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11757
0
    {
11758
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
11759
11760
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11761
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11762
0
         &data, sizeof (data));
11763
0
    }
11764
0
  else
11765
0
    {
11766
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
11767
11768
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11769
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11770
0
         &data, sizeof (data));
11771
0
    }
11772
0
}
11773
11774
char *
11775
elfcore_write_linux_prpsinfo64
11776
  (bfd *abfd, char *buf, int *bufsiz,
11777
   const struct elf_internal_linux_prpsinfo *prpsinfo)
11778
0
{
11779
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11780
0
    {
11781
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
11782
11783
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11784
0
      return elfcore_write_note (abfd, buf, bufsiz,
11785
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
11786
0
    }
11787
0
  else
11788
0
    {
11789
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
11790
11791
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11792
0
      return elfcore_write_note (abfd, buf, bufsiz,
11793
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
11794
0
    }
11795
0
}
11796
11797
char *
11798
elfcore_write_prstatus (bfd *abfd,
11799
      char *buf,
11800
      int *bufsiz,
11801
      long pid,
11802
      int cursig,
11803
      const void *gregs)
11804
0
{
11805
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11806
11807
0
  if (bed->elf_backend_write_core_note != NULL)
11808
0
    {
11809
0
      char *ret;
11810
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11811
0
             NT_PRSTATUS,
11812
0
             pid, cursig, gregs);
11813
0
      if (ret != NULL)
11814
0
  return ret;
11815
0
    }
11816
11817
0
#if defined (HAVE_PRSTATUS_T)
11818
0
#if defined (HAVE_PRSTATUS32_T)
11819
0
  if (bed->s->elfclass == ELFCLASS32)
11820
0
    {
11821
0
      prstatus32_t prstat;
11822
11823
0
      memset (&prstat, 0, sizeof (prstat));
11824
0
      prstat.pr_pid = pid;
11825
0
      prstat.pr_cursig = cursig;
11826
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11827
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11828
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
11829
0
    }
11830
0
  else
11831
0
#endif
11832
0
    {
11833
0
      prstatus_t prstat;
11834
11835
0
      memset (&prstat, 0, sizeof (prstat));
11836
0
      prstat.pr_pid = pid;
11837
0
      prstat.pr_cursig = cursig;
11838
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11839
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11840
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
11841
0
    }
11842
0
#endif /* HAVE_PRSTATUS_T */
11843
11844
0
  free (buf);
11845
0
  return NULL;
11846
0
}
11847
11848
#if defined (HAVE_LWPSTATUS_T)
11849
char *
11850
elfcore_write_lwpstatus (bfd *abfd,
11851
       char *buf,
11852
       int *bufsiz,
11853
       long pid,
11854
       int cursig,
11855
       const void *gregs)
11856
{
11857
  lwpstatus_t lwpstat;
11858
  const char *note_name = "CORE";
11859
11860
  memset (&lwpstat, 0, sizeof (lwpstat));
11861
  lwpstat.pr_lwpid  = pid >> 16;
11862
  lwpstat.pr_cursig = cursig;
11863
#if defined (HAVE_LWPSTATUS_T_PR_REG)
11864
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11865
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11866
#if !defined(gregs)
11867
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11868
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11869
#else
11870
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11871
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11872
#endif
11873
#endif
11874
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
11875
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11876
}
11877
#endif /* HAVE_LWPSTATUS_T */
11878
11879
#if defined (HAVE_PSTATUS_T)
11880
char *
11881
elfcore_write_pstatus (bfd *abfd,
11882
           char *buf,
11883
           int *bufsiz,
11884
           long pid,
11885
           int cursig ATTRIBUTE_UNUSED,
11886
           const void *gregs ATTRIBUTE_UNUSED)
11887
{
11888
  const char *note_name = "CORE";
11889
#if defined (HAVE_PSTATUS32_T)
11890
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11891
11892
  if (bed->s->elfclass == ELFCLASS32)
11893
    {
11894
      pstatus32_t pstat;
11895
11896
      memset (&pstat, 0, sizeof (pstat));
11897
      pstat.pr_pid = pid & 0xffff;
11898
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11899
        NT_PSTATUS, &pstat, sizeof (pstat));
11900
      return buf;
11901
    }
11902
  else
11903
#endif
11904
    {
11905
      pstatus_t pstat;
11906
11907
      memset (&pstat, 0, sizeof (pstat));
11908
      pstat.pr_pid = pid & 0xffff;
11909
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11910
        NT_PSTATUS, &pstat, sizeof (pstat));
11911
      return buf;
11912
    }
11913
}
11914
#endif /* HAVE_PSTATUS_T */
11915
11916
char *
11917
elfcore_write_prfpreg (bfd *abfd,
11918
           char *buf,
11919
           int *bufsiz,
11920
           const void *fpregs,
11921
           int size)
11922
0
{
11923
0
  const char *note_name = "CORE";
11924
0
  return elfcore_write_note (abfd, buf, bufsiz,
11925
0
           note_name, NT_FPREGSET, fpregs, size);
11926
0
}
11927
11928
char *
11929
elfcore_write_prxfpreg (bfd *abfd,
11930
      char *buf,
11931
      int *bufsiz,
11932
      const void *xfpregs,
11933
      int size)
11934
0
{
11935
0
  char *note_name = "LINUX";
11936
0
  return elfcore_write_note (abfd, buf, bufsiz,
11937
0
           note_name, NT_PRXFPREG, xfpregs, size);
11938
0
}
11939
11940
char *
11941
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11942
       const void *xfpregs, int size)
11943
0
{
11944
0
  char *note_name;
11945
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11946
0
    note_name = "FreeBSD";
11947
0
  else
11948
0
    note_name = "LINUX";
11949
0
  return elfcore_write_note (abfd, buf, bufsiz,
11950
0
           note_name, NT_X86_XSTATE, xfpregs, size);
11951
0
}
11952
11953
char *
11954
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11955
          const void *regs, int size)
11956
0
{
11957
0
  char *note_name = "FreeBSD";
11958
0
  return elfcore_write_note (abfd, buf, bufsiz,
11959
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11960
0
}
11961
11962
char *
11963
elfcore_write_ppc_vmx (bfd *abfd,
11964
           char *buf,
11965
           int *bufsiz,
11966
           const void *ppc_vmx,
11967
           int size)
11968
0
{
11969
0
  char *note_name = "LINUX";
11970
0
  return elfcore_write_note (abfd, buf, bufsiz,
11971
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
11972
0
}
11973
11974
char *
11975
elfcore_write_ppc_vsx (bfd *abfd,
11976
           char *buf,
11977
           int *bufsiz,
11978
           const void *ppc_vsx,
11979
           int size)
11980
0
{
11981
0
  char *note_name = "LINUX";
11982
0
  return elfcore_write_note (abfd, buf, bufsiz,
11983
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
11984
0
}
11985
11986
char *
11987
elfcore_write_ppc_tar (bfd *abfd,
11988
           char *buf,
11989
           int *bufsiz,
11990
           const void *ppc_tar,
11991
           int size)
11992
0
{
11993
0
  char *note_name = "LINUX";
11994
0
  return elfcore_write_note (abfd, buf, bufsiz,
11995
0
           note_name, NT_PPC_TAR, ppc_tar, size);
11996
0
}
11997
11998
char *
11999
elfcore_write_ppc_ppr (bfd *abfd,
12000
           char *buf,
12001
           int *bufsiz,
12002
           const void *ppc_ppr,
12003
           int size)
12004
0
{
12005
0
  char *note_name = "LINUX";
12006
0
  return elfcore_write_note (abfd, buf, bufsiz,
12007
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12008
0
}
12009
12010
char *
12011
elfcore_write_ppc_dscr (bfd *abfd,
12012
      char *buf,
12013
      int *bufsiz,
12014
      const void *ppc_dscr,
12015
      int size)
12016
0
{
12017
0
  char *note_name = "LINUX";
12018
0
  return elfcore_write_note (abfd, buf, bufsiz,
12019
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12020
0
}
12021
12022
char *
12023
elfcore_write_ppc_ebb (bfd *abfd,
12024
           char *buf,
12025
           int *bufsiz,
12026
           const void *ppc_ebb,
12027
           int size)
12028
0
{
12029
0
  char *note_name = "LINUX";
12030
0
  return elfcore_write_note (abfd, buf, bufsiz,
12031
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12032
0
}
12033
12034
char *
12035
elfcore_write_ppc_pmu (bfd *abfd,
12036
           char *buf,
12037
           int *bufsiz,
12038
           const void *ppc_pmu,
12039
           int size)
12040
0
{
12041
0
  char *note_name = "LINUX";
12042
0
  return elfcore_write_note (abfd, buf, bufsiz,
12043
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12044
0
}
12045
12046
char *
12047
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12048
         char *buf,
12049
         int *bufsiz,
12050
         const void *ppc_tm_cgpr,
12051
         int size)
12052
0
{
12053
0
  char *note_name = "LINUX";
12054
0
  return elfcore_write_note (abfd, buf, bufsiz,
12055
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12056
0
}
12057
12058
char *
12059
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12060
         char *buf,
12061
         int *bufsiz,
12062
         const void *ppc_tm_cfpr,
12063
         int size)
12064
0
{
12065
0
  char *note_name = "LINUX";
12066
0
  return elfcore_write_note (abfd, buf, bufsiz,
12067
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12068
0
}
12069
12070
char *
12071
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12072
         char *buf,
12073
         int *bufsiz,
12074
         const void *ppc_tm_cvmx,
12075
         int size)
12076
0
{
12077
0
  char *note_name = "LINUX";
12078
0
  return elfcore_write_note (abfd, buf, bufsiz,
12079
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12080
0
}
12081
12082
char *
12083
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12084
         char *buf,
12085
         int *bufsiz,
12086
         const void *ppc_tm_cvsx,
12087
         int size)
12088
0
{
12089
0
  char *note_name = "LINUX";
12090
0
  return elfcore_write_note (abfd, buf, bufsiz,
12091
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12092
0
}
12093
12094
char *
12095
elfcore_write_ppc_tm_spr (bfd *abfd,
12096
        char *buf,
12097
        int *bufsiz,
12098
        const void *ppc_tm_spr,
12099
        int size)
12100
0
{
12101
0
  char *note_name = "LINUX";
12102
0
  return elfcore_write_note (abfd, buf, bufsiz,
12103
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12104
0
}
12105
12106
char *
12107
elfcore_write_ppc_tm_ctar (bfd *abfd,
12108
         char *buf,
12109
         int *bufsiz,
12110
         const void *ppc_tm_ctar,
12111
         int size)
12112
0
{
12113
0
  char *note_name = "LINUX";
12114
0
  return elfcore_write_note (abfd, buf, bufsiz,
12115
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12116
0
}
12117
12118
char *
12119
elfcore_write_ppc_tm_cppr (bfd *abfd,
12120
         char *buf,
12121
         int *bufsiz,
12122
         const void *ppc_tm_cppr,
12123
         int size)
12124
0
{
12125
0
  char *note_name = "LINUX";
12126
0
  return elfcore_write_note (abfd, buf, bufsiz,
12127
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12128
0
}
12129
12130
char *
12131
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12132
          char *buf,
12133
          int *bufsiz,
12134
          const void *ppc_tm_cdscr,
12135
          int size)
12136
0
{
12137
0
  char *note_name = "LINUX";
12138
0
  return elfcore_write_note (abfd, buf, bufsiz,
12139
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12140
0
}
12141
12142
static char *
12143
elfcore_write_s390_high_gprs (bfd *abfd,
12144
            char *buf,
12145
            int *bufsiz,
12146
            const void *s390_high_gprs,
12147
            int size)
12148
0
{
12149
0
  char *note_name = "LINUX";
12150
0
  return elfcore_write_note (abfd, buf, bufsiz,
12151
0
           note_name, NT_S390_HIGH_GPRS,
12152
0
           s390_high_gprs, size);
12153
0
}
12154
12155
char *
12156
elfcore_write_s390_timer (bfd *abfd,
12157
        char *buf,
12158
        int *bufsiz,
12159
        const void *s390_timer,
12160
        int size)
12161
0
{
12162
0
  char *note_name = "LINUX";
12163
0
  return elfcore_write_note (abfd, buf, bufsiz,
12164
0
           note_name, NT_S390_TIMER, s390_timer, size);
12165
0
}
12166
12167
char *
12168
elfcore_write_s390_todcmp (bfd *abfd,
12169
         char *buf,
12170
         int *bufsiz,
12171
         const void *s390_todcmp,
12172
         int size)
12173
0
{
12174
0
  char *note_name = "LINUX";
12175
0
  return elfcore_write_note (abfd, buf, bufsiz,
12176
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12177
0
}
12178
12179
char *
12180
elfcore_write_s390_todpreg (bfd *abfd,
12181
          char *buf,
12182
          int *bufsiz,
12183
          const void *s390_todpreg,
12184
          int size)
12185
0
{
12186
0
  char *note_name = "LINUX";
12187
0
  return elfcore_write_note (abfd, buf, bufsiz,
12188
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12189
0
}
12190
12191
char *
12192
elfcore_write_s390_ctrs (bfd *abfd,
12193
       char *buf,
12194
       int *bufsiz,
12195
       const void *s390_ctrs,
12196
       int size)
12197
0
{
12198
0
  char *note_name = "LINUX";
12199
0
  return elfcore_write_note (abfd, buf, bufsiz,
12200
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12201
0
}
12202
12203
char *
12204
elfcore_write_s390_prefix (bfd *abfd,
12205
         char *buf,
12206
         int *bufsiz,
12207
         const void *s390_prefix,
12208
         int size)
12209
0
{
12210
0
  char *note_name = "LINUX";
12211
0
  return elfcore_write_note (abfd, buf, bufsiz,
12212
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12213
0
}
12214
12215
char *
12216
elfcore_write_s390_last_break (bfd *abfd,
12217
             char *buf,
12218
             int *bufsiz,
12219
             const void *s390_last_break,
12220
             int size)
12221
0
{
12222
0
  char *note_name = "LINUX";
12223
0
  return elfcore_write_note (abfd, buf, bufsiz,
12224
0
           note_name, NT_S390_LAST_BREAK,
12225
0
           s390_last_break, size);
12226
0
}
12227
12228
char *
12229
elfcore_write_s390_system_call (bfd *abfd,
12230
        char *buf,
12231
        int *bufsiz,
12232
        const void *s390_system_call,
12233
        int size)
12234
0
{
12235
0
  char *note_name = "LINUX";
12236
0
  return elfcore_write_note (abfd, buf, bufsiz,
12237
0
           note_name, NT_S390_SYSTEM_CALL,
12238
0
           s390_system_call, size);
12239
0
}
12240
12241
char *
12242
elfcore_write_s390_tdb (bfd *abfd,
12243
      char *buf,
12244
      int *bufsiz,
12245
      const void *s390_tdb,
12246
      int size)
12247
0
{
12248
0
  char *note_name = "LINUX";
12249
0
  return elfcore_write_note (abfd, buf, bufsiz,
12250
0
           note_name, NT_S390_TDB, s390_tdb, size);
12251
0
}
12252
12253
char *
12254
elfcore_write_s390_vxrs_low (bfd *abfd,
12255
           char *buf,
12256
           int *bufsiz,
12257
           const void *s390_vxrs_low,
12258
           int size)
12259
0
{
12260
0
  char *note_name = "LINUX";
12261
0
  return elfcore_write_note (abfd, buf, bufsiz,
12262
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12263
0
}
12264
12265
char *
12266
elfcore_write_s390_vxrs_high (bfd *abfd,
12267
           char *buf,
12268
           int *bufsiz,
12269
           const void *s390_vxrs_high,
12270
           int size)
12271
0
{
12272
0
  char *note_name = "LINUX";
12273
0
  return elfcore_write_note (abfd, buf, bufsiz,
12274
0
           note_name, NT_S390_VXRS_HIGH,
12275
0
           s390_vxrs_high, size);
12276
0
}
12277
12278
char *
12279
elfcore_write_s390_gs_cb (bfd *abfd,
12280
        char *buf,
12281
        int *bufsiz,
12282
        const void *s390_gs_cb,
12283
        int size)
12284
0
{
12285
0
  char *note_name = "LINUX";
12286
0
  return elfcore_write_note (abfd, buf, bufsiz,
12287
0
           note_name, NT_S390_GS_CB,
12288
0
           s390_gs_cb, size);
12289
0
}
12290
12291
char *
12292
elfcore_write_s390_gs_bc (bfd *abfd,
12293
        char *buf,
12294
        int *bufsiz,
12295
        const void *s390_gs_bc,
12296
        int size)
12297
0
{
12298
0
  char *note_name = "LINUX";
12299
0
  return elfcore_write_note (abfd, buf, bufsiz,
12300
0
           note_name, NT_S390_GS_BC,
12301
0
           s390_gs_bc, size);
12302
0
}
12303
12304
char *
12305
elfcore_write_arm_vfp (bfd *abfd,
12306
           char *buf,
12307
           int *bufsiz,
12308
           const void *arm_vfp,
12309
           int size)
12310
0
{
12311
0
  char *note_name = "LINUX";
12312
0
  return elfcore_write_note (abfd, buf, bufsiz,
12313
0
           note_name, NT_ARM_VFP, arm_vfp, size);
12314
0
}
12315
12316
char *
12317
elfcore_write_aarch_tls (bfd *abfd,
12318
           char *buf,
12319
           int *bufsiz,
12320
           const void *aarch_tls,
12321
           int size)
12322
0
{
12323
0
  char *note_name = "LINUX";
12324
0
  return elfcore_write_note (abfd, buf, bufsiz,
12325
0
           note_name, NT_ARM_TLS, aarch_tls, size);
12326
0
}
12327
12328
char *
12329
elfcore_write_aarch_hw_break (bfd *abfd,
12330
          char *buf,
12331
          int *bufsiz,
12332
          const void *aarch_hw_break,
12333
          int size)
12334
0
{
12335
0
  char *note_name = "LINUX";
12336
0
  return elfcore_write_note (abfd, buf, bufsiz,
12337
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12338
0
}
12339
12340
char *
12341
elfcore_write_aarch_hw_watch (bfd *abfd,
12342
          char *buf,
12343
          int *bufsiz,
12344
          const void *aarch_hw_watch,
12345
          int size)
12346
0
{
12347
0
  char *note_name = "LINUX";
12348
0
  return elfcore_write_note (abfd, buf, bufsiz,
12349
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12350
0
}
12351
12352
char *
12353
elfcore_write_aarch_sve (bfd *abfd,
12354
       char *buf,
12355
       int *bufsiz,
12356
       const void *aarch_sve,
12357
       int size)
12358
0
{
12359
0
  char *note_name = "LINUX";
12360
0
  return elfcore_write_note (abfd, buf, bufsiz,
12361
0
           note_name, NT_ARM_SVE, aarch_sve, size);
12362
0
}
12363
12364
char *
12365
elfcore_write_aarch_pauth (bfd *abfd,
12366
         char *buf,
12367
         int *bufsiz,
12368
         const void *aarch_pauth,
12369
         int size)
12370
0
{
12371
0
  char *note_name = "LINUX";
12372
0
  return elfcore_write_note (abfd, buf, bufsiz,
12373
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12374
0
}
12375
12376
char *
12377
elfcore_write_aarch_mte (bfd *abfd,
12378
              char *buf,
12379
              int *bufsiz,
12380
              const void *aarch_mte,
12381
              int size)
12382
0
{
12383
0
  char *note_name = "LINUX";
12384
0
  return elfcore_write_note (abfd, buf, bufsiz,
12385
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
12386
0
           aarch_mte,
12387
0
           size);
12388
0
}
12389
12390
char *
12391
elfcore_write_arc_v2 (bfd *abfd,
12392
          char *buf,
12393
          int *bufsiz,
12394
          const void *arc_v2,
12395
          int size)
12396
0
{
12397
0
  char *note_name = "LINUX";
12398
0
  return elfcore_write_note (abfd, buf, bufsiz,
12399
0
           note_name, NT_ARC_V2, arc_v2, size);
12400
0
}
12401
12402
char *
12403
elfcore_write_loongarch_cpucfg (bfd *abfd,
12404
        char *buf,
12405
        int *bufsiz,
12406
        const void *loongarch_cpucfg,
12407
        int size)
12408
0
{
12409
0
  char *note_name = "LINUX";
12410
0
  return elfcore_write_note (abfd, buf, bufsiz,
12411
0
           note_name, NT_LARCH_CPUCFG,
12412
0
           loongarch_cpucfg, size);
12413
0
}
12414
12415
char *
12416
elfcore_write_loongarch_lbt (bfd *abfd,
12417
           char *buf,
12418
           int *bufsiz,
12419
           const void *loongarch_lbt,
12420
           int size)
12421
0
{
12422
0
  char *note_name = "LINUX";
12423
0
  return elfcore_write_note (abfd, buf, bufsiz,
12424
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
12425
0
}
12426
12427
char *
12428
elfcore_write_loongarch_lsx (bfd *abfd,
12429
           char *buf,
12430
           int *bufsiz,
12431
           const void *loongarch_lsx,
12432
           int size)
12433
0
{
12434
0
  char *note_name = "LINUX";
12435
0
  return elfcore_write_note (abfd, buf, bufsiz,
12436
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
12437
0
}
12438
12439
char *
12440
elfcore_write_loongarch_lasx (bfd *abfd,
12441
            char *buf,
12442
            int *bufsiz,
12443
            const void *loongarch_lasx,
12444
            int size)
12445
0
{
12446
0
  char *note_name = "LINUX";
12447
0
  return elfcore_write_note (abfd, buf, bufsiz,
12448
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
12449
0
}
12450
12451
/* Write the buffer of csr values in CSRS (length SIZE) into the note
12452
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12453
   written into.  Return a pointer to the new start of the note buffer, to
12454
   replace BUF which may no longer be valid.  */
12455
12456
char *
12457
elfcore_write_riscv_csr (bfd *abfd,
12458
       char *buf,
12459
       int *bufsiz,
12460
       const void *csrs,
12461
       int size)
12462
0
{
12463
0
  const char *note_name = "GDB";
12464
0
  return elfcore_write_note (abfd, buf, bufsiz,
12465
0
           note_name, NT_RISCV_CSR, csrs, size);
12466
0
}
12467
12468
/* Write the target description (a string) pointed to by TDESC, length
12469
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
12470
   note is being written into.  Return a pointer to the new start of the
12471
   note buffer, to replace BUF which may no longer be valid.  */
12472
12473
char *
12474
elfcore_write_gdb_tdesc (bfd *abfd,
12475
       char *buf,
12476
       int *bufsiz,
12477
       const void *tdesc,
12478
       int size)
12479
0
{
12480
0
  const char *note_name = "GDB";
12481
0
  return elfcore_write_note (abfd, buf, bufsiz,
12482
0
           note_name, NT_GDB_TDESC, tdesc, size);
12483
0
}
12484
12485
char *
12486
elfcore_write_register_note (bfd *abfd,
12487
           char *buf,
12488
           int *bufsiz,
12489
           const char *section,
12490
           const void *data,
12491
           int size)
12492
0
{
12493
0
  if (strcmp (section, ".reg2") == 0)
12494
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12495
0
  if (strcmp (section, ".reg-xfp") == 0)
12496
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12497
0
  if (strcmp (section, ".reg-xstate") == 0)
12498
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12499
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
12500
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12501
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
12502
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12503
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
12504
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12505
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
12506
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12507
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
12508
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12509
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
12510
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12511
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
12512
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12513
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
12514
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12515
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12516
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12517
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12518
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12519
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12520
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12521
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12522
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12523
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12524
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12525
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12526
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12527
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12528
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12529
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12530
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12531
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
12532
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12533
0
  if (strcmp (section, ".reg-s390-timer") == 0)
12534
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12535
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
12536
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12537
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
12538
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12539
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
12540
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12541
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
12542
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12543
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
12544
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12545
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
12546
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12547
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
12548
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12549
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12550
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12551
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12552
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12553
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
12554
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12555
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
12556
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12557
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
12558
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12559
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
12560
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12561
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
12562
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12563
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12564
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12565
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
12566
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12567
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
12568
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12569
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
12570
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12571
0
  if (strcmp (section, ".reg-arc-v2") == 0)
12572
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12573
0
  if (strcmp (section, ".gdb-tdesc") == 0)
12574
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12575
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
12576
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12577
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12578
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12579
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
12580
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12581
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
12582
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12583
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
12584
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12585
0
  return NULL;
12586
0
}
12587
12588
char *
12589
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12590
       const void *buf, int bufsiz)
12591
0
{
12592
0
  return elfcore_write_note (obfd, note_data, note_size,
12593
0
           "CORE", NT_FILE, buf, bufsiz);
12594
0
}
12595
12596
static bool
12597
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12598
     size_t align)
12599
54.7k
{
12600
54.7k
  char *p;
12601
12602
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12603
     gABI specifies that PT_NOTE alignment should be aligned to 4
12604
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
12605
     align is less than 4, we use 4 byte alignment.   */
12606
54.7k
  if (align < 4)
12607
21.1k
    align = 4;
12608
54.7k
  if (align != 4 && align != 8)
12609
21.8k
    return false;
12610
12611
32.9k
  p = buf;
12612
100k
  while (p < buf + size)
12613
92.1k
    {
12614
92.1k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
12615
92.1k
      Elf_Internal_Note in;
12616
12617
92.1k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
12618
4.91k
  return false;
12619
12620
87.2k
      in.type = H_GET_32 (abfd, xnp->type);
12621
12622
87.2k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
12623
87.2k
      in.namedata = xnp->name;
12624
87.2k
      if (in.namesz > buf - in.namedata + size)
12625
11.6k
  return false;
12626
12627
75.5k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
12628
75.5k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12629
75.5k
      in.descpos = offset + (in.descdata - buf);
12630
75.5k
      if (in.descsz != 0
12631
75.5k
    && (in.descdata >= buf + size
12632
29.9k
        || in.descsz > buf - in.descdata + size))
12633
7.38k
  return false;
12634
12635
68.1k
      switch (bfd_get_format (abfd))
12636
68.1k
  {
12637
0
  default:
12638
0
    return true;
12639
12640
1.97k
  case bfd_core:
12641
1.97k
    {
12642
15.7k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12643
1.97k
      struct
12644
1.97k
      {
12645
1.97k
        const char * string;
12646
1.97k
        size_t len;
12647
1.97k
        bool (*func) (bfd *, Elf_Internal_Note *);
12648
1.97k
      }
12649
1.97k
      grokers[] =
12650
1.97k
      {
12651
1.97k
        GROKER_ELEMENT ("", elfcore_grok_note),
12652
1.97k
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12653
1.97k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12654
1.97k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12655
1.97k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12656
1.97k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12657
1.97k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12658
1.97k
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12659
1.97k
      };
12660
1.97k
#undef GROKER_ELEMENT
12661
1.97k
      int i;
12662
12663
14.1k
      for (i = ARRAY_SIZE (grokers); i--;)
12664
14.1k
        {
12665
14.1k
    if (in.namesz >= grokers[i].len
12666
14.1k
        && strncmp (in.namedata, grokers[i].string,
12667
5.30k
        grokers[i].len) == 0)
12668
1.97k
      {
12669
1.97k
        if (! grokers[i].func (abfd, & in))
12670
64
          return false;
12671
1.90k
        break;
12672
1.97k
      }
12673
14.1k
        }
12674
1.90k
      break;
12675
1.97k
    }
12676
12677
66.2k
  case bfd_object:
12678
66.2k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12679
2.04k
      {
12680
2.04k
        if (! elfobj_grok_gnu_note (abfd, &in))
12681
262
    return false;
12682
2.04k
      }
12683
64.1k
    else if (in.namesz == sizeof "stapsdt"
12684
64.1k
       && strcmp (in.namedata, "stapsdt") == 0)
12685
0
      {
12686
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
12687
0
    return false;
12688
0
      }
12689
65.9k
    break;
12690
68.1k
  }
12691
12692
67.8k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12693
67.8k
    }
12694
12695
8.63k
  return true;
12696
32.9k
}
12697
12698
bool
12699
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12700
    size_t align)
12701
5.09k
{
12702
5.09k
  char *buf;
12703
12704
5.09k
  if (size == 0 || (size + 1) == 0)
12705
797
    return true;
12706
12707
4.30k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12708
581
    return false;
12709
12710
3.72k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12711
3.72k
  if (buf == NULL)
12712
1.87k
    return false;
12713
12714
  /* PR 17512: file: ec08f814
12715
     0-termintate the buffer so that string searches will not overflow.  */
12716
1.84k
  buf[size] = 0;
12717
12718
1.84k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
12719
1.69k
    {
12720
1.69k
      free (buf);
12721
1.69k
      return false;
12722
1.69k
    }
12723
12724
148
  free (buf);
12725
148
  return true;
12726
1.84k
}
12727

12728
/* Providing external access to the ELF program header table.  */
12729
12730
/* Return an upper bound on the number of bytes required to store a
12731
   copy of ABFD's program header table entries.  Return -1 if an error
12732
   occurs; bfd_get_error will return an appropriate code.  */
12733
12734
long
12735
bfd_get_elf_phdr_upper_bound (bfd *abfd)
12736
0
{
12737
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
12738
0
    {
12739
0
      bfd_set_error (bfd_error_wrong_format);
12740
0
      return -1;
12741
0
    }
12742
12743
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12744
0
}
12745
12746
/* Copy ABFD's program header table entries to *PHDRS.  The entries
12747
   will be stored as an array of Elf_Internal_Phdr structures, as
12748
   defined in include/elf/internal.h.  To find out how large the
12749
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12750
12751
   Return the number of program header table entries read, or -1 if an
12752
   error occurs; bfd_get_error will return an appropriate code.  */
12753
12754
int
12755
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12756
0
{
12757
0
  int num_phdrs;
12758
12759
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
12760
0
    {
12761
0
      bfd_set_error (bfd_error_wrong_format);
12762
0
      return -1;
12763
0
    }
12764
12765
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
12766
0
  if (num_phdrs != 0)
12767
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
12768
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
12769
12770
0
  return num_phdrs;
12771
0
}
12772
12773
enum elf_reloc_type_class
12774
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12775
         const asection *rel_sec ATTRIBUTE_UNUSED,
12776
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12777
0
{
12778
0
  return reloc_class_normal;
12779
0
}
12780
12781
/* For RELA architectures, return the relocation value for a
12782
   relocation against a local symbol.  */
12783
12784
bfd_vma
12785
_bfd_elf_rela_local_sym (bfd *abfd,
12786
       Elf_Internal_Sym *sym,
12787
       asection **psec,
12788
       Elf_Internal_Rela *rel)
12789
0
{
12790
0
  asection *sec = *psec;
12791
0
  bfd_vma relocation;
12792
12793
0
  relocation = (sec->output_section->vma
12794
0
    + sec->output_offset
12795
0
    + sym->st_value);
12796
0
  if ((sec->flags & SEC_MERGE)
12797
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12798
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12799
0
    {
12800
0
      rel->r_addend =
12801
0
  _bfd_merged_section_offset (abfd, psec,
12802
0
            elf_section_data (sec)->sec_info,
12803
0
            sym->st_value + rel->r_addend);
12804
0
      if (sec != *psec)
12805
0
  {
12806
    /* If we have changed the section, and our original section is
12807
       marked with SEC_EXCLUDE, it means that the original
12808
       SEC_MERGE section has been completely subsumed in some
12809
       other SEC_MERGE section.  In this case, we need to leave
12810
       some info around for --emit-relocs.  */
12811
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
12812
0
      sec->kept_section = *psec;
12813
0
    sec = *psec;
12814
0
  }
12815
0
      rel->r_addend -= relocation;
12816
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
12817
0
    }
12818
0
  return relocation;
12819
0
}
12820
12821
bfd_vma
12822
_bfd_elf_rel_local_sym (bfd *abfd,
12823
      Elf_Internal_Sym *sym,
12824
      asection **psec,
12825
      bfd_vma addend)
12826
0
{
12827
0
  asection *sec = *psec;
12828
12829
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12830
0
    return sym->st_value + addend;
12831
12832
0
  return _bfd_merged_section_offset (abfd, psec,
12833
0
             elf_section_data (sec)->sec_info,
12834
0
             sym->st_value + addend);
12835
0
}
12836
12837
/* Adjust an address within a section.  Given OFFSET within SEC, return
12838
   the new offset within the section, based upon changes made to the
12839
   section.  Returns -1 if the offset is now invalid.
12840
   The offset (in abnd out) is in target sized bytes, however big a
12841
   byte may be.  */
12842
12843
bfd_vma
12844
_bfd_elf_section_offset (bfd *abfd,
12845
       struct bfd_link_info *info,
12846
       asection *sec,
12847
       bfd_vma offset)
12848
0
{
12849
0
  switch (sec->sec_info_type)
12850
0
    {
12851
0
    case SEC_INFO_TYPE_STABS:
12852
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12853
0
               offset);
12854
0
    case SEC_INFO_TYPE_EH_FRAME:
12855
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12856
12857
0
    default:
12858
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12859
0
  {
12860
    /* Reverse the offset.  */
12861
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12862
0
    bfd_size_type address_size = bed->s->arch_size / 8;
12863
12864
    /* address_size and sec->size are in octets.  Convert
12865
       to bytes before subtracting the original offset.  */
12866
0
    offset = ((sec->size - address_size)
12867
0
        / bfd_octets_per_byte (abfd, sec) - offset);
12868
0
  }
12869
0
      return offset;
12870
0
    }
12871
0
}
12872

12873
long
12874
_bfd_elf_get_synthetic_symtab (bfd *abfd,
12875
             long symcount ATTRIBUTE_UNUSED,
12876
             asymbol **syms ATTRIBUTE_UNUSED,
12877
             long dynsymcount,
12878
             asymbol **dynsyms,
12879
             asymbol **ret)
12880
5.07k
{
12881
5.07k
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12882
5.07k
  asection *relplt;
12883
5.07k
  asymbol *s;
12884
5.07k
  const char *relplt_name;
12885
5.07k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12886
5.07k
  arelent *p;
12887
5.07k
  long count, i, n;
12888
5.07k
  size_t size;
12889
5.07k
  Elf_Internal_Shdr *hdr;
12890
5.07k
  char *names;
12891
5.07k
  asection *plt;
12892
12893
5.07k
  *ret = NULL;
12894
12895
5.07k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12896
5.02k
    return 0;
12897
12898
54
  if (dynsymcount <= 0)
12899
41
    return 0;
12900
12901
13
  if (!bed->plt_sym_val)
12902
6
    return 0;
12903
12904
7
  relplt_name = bed->relplt_name;
12905
7
  if (relplt_name == NULL)
12906
7
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12907
7
  relplt = bfd_get_section_by_name (abfd, relplt_name);
12908
7
  if (relplt == NULL)
12909
0
    return 0;
12910
12911
7
  hdr = &elf_section_data (relplt)->this_hdr;
12912
7
  if (hdr->sh_link != elf_dynsymtab (abfd)
12913
7
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12914
0
    return 0;
12915
12916
7
  plt = bfd_get_section_by_name (abfd, ".plt");
12917
7
  if (plt == NULL)
12918
0
    return 0;
12919
12920
7
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12921
7
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12922
0
    return -1;
12923
12924
7
  count = NUM_SHDR_ENTRIES (hdr);
12925
7
  size = count * sizeof (asymbol);
12926
7
  p = relplt->relocation;
12927
1.45k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12928
1.45k
    {
12929
1.45k
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12930
1.45k
      if (p->addend != 0)
12931
0
  {
12932
0
#ifdef BFD64
12933
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12934
#else
12935
    size += sizeof ("+0x") - 1 + 8;
12936
#endif
12937
0
  }
12938
1.45k
    }
12939
12940
7
  s = *ret = (asymbol *) bfd_malloc (size);
12941
7
  if (s == NULL)
12942
0
    return -1;
12943
12944
7
  names = (char *) (s + count);
12945
7
  p = relplt->relocation;
12946
7
  n = 0;
12947
1.45k
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12948
1.45k
    {
12949
1.45k
      size_t len;
12950
1.45k
      bfd_vma addr;
12951
12952
1.45k
      addr = bed->plt_sym_val (i, plt, p);
12953
1.45k
      if (addr == (bfd_vma) -1)
12954
0
  continue;
12955
12956
1.45k
      *s = **p->sym_ptr_ptr;
12957
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
12958
   we are defining a symbol, ensure one of them is set.  */
12959
1.45k
      if ((s->flags & BSF_LOCAL) == 0)
12960
1.45k
  s->flags |= BSF_GLOBAL;
12961
1.45k
      s->flags |= BSF_SYNTHETIC;
12962
1.45k
      s->section = plt;
12963
1.45k
      s->value = addr - plt->vma;
12964
1.45k
      s->name = names;
12965
1.45k
      s->udata.p = NULL;
12966
1.45k
      len = strlen ((*p->sym_ptr_ptr)->name);
12967
1.45k
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
12968
1.45k
      names += len;
12969
1.45k
      if (p->addend != 0)
12970
0
  {
12971
0
    char buf[30], *a;
12972
12973
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
12974
0
    names += sizeof ("+0x") - 1;
12975
0
    bfd_sprintf_vma (abfd, buf, p->addend);
12976
0
    for (a = buf; *a == '0'; ++a)
12977
0
      ;
12978
0
    len = strlen (a);
12979
0
    memcpy (names, a, len);
12980
0
    names += len;
12981
0
  }
12982
1.45k
      memcpy (names, "@plt", sizeof ("@plt"));
12983
1.45k
      names += sizeof ("@plt");
12984
1.45k
      ++s, ++n;
12985
1.45k
    }
12986
12987
7
  return n;
12988
7
}
12989
12990
/* It is only used by x86-64 so far.
12991
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
12992
   but current usage would allow all of _bfd_std_section to be zero.  */
12993
static const asymbol lcomm_sym
12994
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12995
asection _bfd_elf_large_com_section
12996
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12997
          "LARGE_COMMON", 0, SEC_IS_COMMON);
12998
12999
bool
13000
_bfd_elf_final_write_processing (bfd *abfd)
13001
188
{
13002
188
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13003
13004
188
  i_ehdrp = elf_elfheader (abfd);
13005
13006
188
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13007
135
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13008
13009
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13010
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13011
     or STB_GNU_UNIQUE binding.  */
13012
188
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13013
0
    {
13014
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13015
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13016
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13017
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13018
0
  {
13019
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13020
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13021
0
          "and FreeBSD targets"));
13022
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13023
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13024
0
          "only by GNU and FreeBSD targets"));
13025
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13026
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13027
0
          "only by GNU and FreeBSD targets"));
13028
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13029
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13030
0
          "only by GNU and FreeBSD targets"));
13031
0
    bfd_set_error (bfd_error_sorry);
13032
0
    return false;
13033
0
  }
13034
0
    }
13035
188
  return true;
13036
188
}
13037
13038
13039
/* Return TRUE for ELF symbol types that represent functions.
13040
   This is the default version of this function, which is sufficient for
13041
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13042
13043
bool
13044
_bfd_elf_is_function_type (unsigned int type)
13045
0
{
13046
0
  return (type == STT_FUNC
13047
0
    || type == STT_GNU_IFUNC);
13048
0
}
13049
13050
/* If the ELF symbol SYM might be a function in SEC, return the
13051
   function size and set *CODE_OFF to the function's entry point,
13052
   otherwise return zero.  */
13053
13054
bfd_size_type
13055
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13056
           bfd_vma *code_off)
13057
48.9M
{
13058
48.9M
  bfd_size_type size;
13059
48.9M
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13060
13061
48.9M
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13062
48.9M
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13063
48.9M
      || sym->section != sec)
13064
45.9M
    return 0;
13065
13066
2.94M
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13067
13068
  /* In theory we should check that the symbol's type satisfies
13069
     _bfd_elf_is_function_type(), but there are some function-like
13070
     symbols which would fail this test.  (eg _start).  Instead
13071
     we check for hidden, local, notype symbols with zero size.
13072
     This type of symbol is generated by the annobin plugin for gcc
13073
     and clang, and should not be considered to be a function symbol.  */
13074
2.94M
  if (size == 0
13075
2.94M
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13076
2.94M
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13077
2.94M
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13078
385
    return 0;
13079
13080
2.94M
  *code_off = sym->value;
13081
  /* Do not return 0 for the function's size.  */
13082
2.94M
  return size ? size : 1;
13083
2.94M
}
13084
13085
/* Set to non-zero to enable some debug messages.  */
13086
#define DEBUG_SECONDARY_RELOCS   0
13087
13088
/* An internal-to-the-bfd-library only section type
13089
   used to indicate a cached secondary reloc section.  */
13090
20.1k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13091
13092
/* Create a BFD section to hold a secondary reloc section.  */
13093
13094
bool
13095
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13096
               Elf_Internal_Shdr *hdr,
13097
               const char * name,
13098
               unsigned int shindex)
13099
2.76k
{
13100
  /* We only support RELA secondary relocs.  */
13101
2.76k
  if (hdr->sh_type != SHT_RELA)
13102
6
    return false;
13103
13104
#if DEBUG_SECONDARY_RELOCS
13105
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13106
#endif
13107
2.75k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13108
2.75k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13109
2.76k
}
13110
13111
/* Read in any secondary relocs associated with SEC.  */
13112
13113
bool
13114
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13115
          asection *  sec,
13116
          asymbol **  symbols,
13117
          bool dynamic)
13118
15.4k
{
13119
15.4k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13120
15.4k
  asection * relsec;
13121
15.4k
  bool result = true;
13122
15.4k
  bfd_vma (*r_sym) (bfd_vma);
13123
15.4k
  ufile_ptr filesize;
13124
13125
15.4k
#if BFD_DEFAULT_TARGET_SIZE > 32
13126
15.4k
  if (bfd_arch_bits_per_address (abfd) != 32)
13127
13.6k
    r_sym = elf64_r_sym;
13128
1.81k
  else
13129
1.81k
#endif
13130
1.81k
    r_sym = elf32_r_sym;
13131
13132
15.4k
  if (!elf_section_data (sec)->has_secondary_relocs)
13133
15.0k
    return true;
13134
13135
  /* Discover if there are any secondary reloc sections
13136
     associated with SEC.  */
13137
423
  filesize = bfd_get_file_size (abfd);
13138
8.99k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13139
8.57k
    {
13140
8.57k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13141
13142
8.57k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13143
8.57k
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13144
8.57k
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13145
424
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13146
424
  {
13147
424
    bfd_byte * native_relocs;
13148
424
    bfd_byte * native_reloc;
13149
424
    arelent * internal_relocs;
13150
424
    arelent * internal_reloc;
13151
424
    size_t i;
13152
424
    unsigned int entsize;
13153
424
    unsigned int symcount;
13154
424
    bfd_size_type reloc_count;
13155
424
    size_t amt;
13156
13157
424
    if (ebd->elf_info_to_howto == NULL)
13158
0
      return false;
13159
13160
#if DEBUG_SECONDARY_RELOCS
13161
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13162
       sec->name, relsec->name);
13163
#endif
13164
424
    entsize = hdr->sh_entsize;
13165
13166
424
    if (filesize != 0
13167
424
        && ((ufile_ptr) hdr->sh_offset > filesize
13168
424
      || hdr->sh_size > filesize - hdr->sh_offset))
13169
288
      {
13170
288
        bfd_set_error (bfd_error_file_truncated);
13171
288
        result = false;
13172
288
        continue;
13173
288
      }
13174
13175
136
    native_relocs = bfd_malloc (hdr->sh_size);
13176
136
    if (native_relocs == NULL)
13177
0
      {
13178
0
        result = false;
13179
0
        continue;
13180
0
      }
13181
13182
136
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13183
136
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13184
0
      {
13185
0
        free (native_relocs);
13186
0
        bfd_set_error (bfd_error_file_too_big);
13187
0
        result = false;
13188
0
        continue;
13189
0
      }
13190
13191
136
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13192
136
    if (internal_relocs == NULL)
13193
0
      {
13194
0
        free (native_relocs);
13195
0
        result = false;
13196
0
        continue;
13197
0
      }
13198
13199
136
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13200
136
        || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13201
136
      != hdr->sh_size))
13202
1
      {
13203
1
        free (native_relocs);
13204
        /* The internal_relocs will be freed when
13205
     the memory for the bfd is released.  */
13206
1
        result = false;
13207
1
        continue;
13208
1
      }
13209
13210
135
    if (dynamic)
13211
0
      symcount = bfd_get_dynamic_symcount (abfd);
13212
135
    else
13213
135
      symcount = bfd_get_symcount (abfd);
13214
13215
135
    for (i = 0, internal_reloc = internal_relocs,
13216
135
     native_reloc = native_relocs;
13217
1.15k
         i < reloc_count;
13218
1.01k
         i++, internal_reloc++, native_reloc += entsize)
13219
1.01k
      {
13220
1.01k
        bool res;
13221
1.01k
        Elf_Internal_Rela rela;
13222
13223
1.01k
        if (entsize == ebd->s->sizeof_rel)
13224
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13225
1.01k
        else /* entsize == ebd->s->sizeof_rela */
13226
1.01k
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13227
13228
        /* The address of an ELF reloc is section relative for an object
13229
     file, and absolute for an executable file or shared library.
13230
     The address of a normal BFD reloc is always section relative,
13231
     and the address of a dynamic reloc is absolute..  */
13232
1.01k
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13233
918
    internal_reloc->address = rela.r_offset;
13234
98
        else
13235
98
    internal_reloc->address = rela.r_offset - sec->vma;
13236
13237
1.01k
        if (r_sym (rela.r_info) == STN_UNDEF)
13238
171
    {
13239
      /* FIXME: This and the error case below mean that we
13240
         have a symbol on relocs that is not elf_symbol_type.  */
13241
171
      internal_reloc->sym_ptr_ptr =
13242
171
        bfd_abs_section_ptr->symbol_ptr_ptr;
13243
171
    }
13244
845
        else if (r_sym (rela.r_info) > symcount)
13245
0
    {
13246
0
      _bfd_error_handler
13247
        /* xgettext:c-format */
13248
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13249
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13250
0
      bfd_set_error (bfd_error_bad_value);
13251
0
      internal_reloc->sym_ptr_ptr =
13252
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
13253
0
      result = false;
13254
0
    }
13255
845
        else
13256
845
    {
13257
845
      asymbol **ps;
13258
13259
845
      ps = symbols + r_sym (rela.r_info) - 1;
13260
845
      internal_reloc->sym_ptr_ptr = ps;
13261
      /* Make sure that this symbol is not removed by strip.  */
13262
845
      (*ps)->flags |= BSF_KEEP;
13263
845
    }
13264
13265
1.01k
        internal_reloc->addend = rela.r_addend;
13266
13267
1.01k
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13268
1.01k
        if (! res || internal_reloc->howto == NULL)
13269
53
    {
13270
#if DEBUG_SECONDARY_RELOCS
13271
      fprintf (stderr,
13272
         "there is no howto associated with reloc %lx\n",
13273
         rela.r_info);
13274
#endif
13275
53
      result = false;
13276
53
    }
13277
1.01k
      }
13278
13279
135
    free (native_relocs);
13280
    /* Store the internal relocs.  */
13281
135
    elf_section_data (relsec)->sec_info = internal_relocs;
13282
135
  }
13283
8.57k
    }
13284
13285
423
  return result;
13286
423
}
13287
13288
/* Set the ELF section header fields of an output secondary reloc section.  */
13289
13290
bool
13291
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13292
              bfd *obfd ATTRIBUTE_UNUSED,
13293
              const Elf_Internal_Shdr *isection,
13294
              Elf_Internal_Shdr *osection)
13295
214
{
13296
214
  asection * isec;
13297
214
  asection * osec;
13298
214
  struct bfd_elf_section_data * esd;
13299
13300
214
  if (isection == NULL)
13301
0
    return false;
13302
13303
214
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13304
214
    return true;
13305
13306
0
  isec = isection->bfd_section;
13307
0
  if (isec == NULL)
13308
0
    return false;
13309
13310
0
  osec = osection->bfd_section;
13311
0
  if (osec == NULL)
13312
0
    return false;
13313
13314
0
  esd = elf_section_data (osec);
13315
0
  BFD_ASSERT (esd->sec_info == NULL);
13316
0
  esd->sec_info = elf_section_data (isec)->sec_info;
13317
0
  osection->sh_type = SHT_RELA;
13318
0
  osection->sh_link = elf_onesymtab (obfd);
13319
0
  if (osection->sh_link == 0)
13320
0
    {
13321
      /* There is no symbol table - we are hosed...  */
13322
0
      _bfd_error_handler
13323
  /* xgettext:c-format */
13324
0
  (_("%pB(%pA): link section cannot be set"
13325
0
     " because the output file does not have a symbol table"),
13326
0
  obfd, osec);
13327
0
      bfd_set_error (bfd_error_bad_value);
13328
0
      return false;
13329
0
    }
13330
13331
  /* Find the output section that corresponds to the isection's
13332
     sh_info link.  */
13333
0
  if (isection->sh_info == 0
13334
0
      || isection->sh_info >= elf_numsections (ibfd))
13335
0
    {
13336
0
      _bfd_error_handler
13337
  /* xgettext:c-format */
13338
0
  (_("%pB(%pA): info section index is invalid"),
13339
0
  obfd, osec);
13340
0
      bfd_set_error (bfd_error_bad_value);
13341
0
      return false;
13342
0
    }
13343
13344
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
13345
13346
0
  if (isection == NULL
13347
0
      || isection->bfd_section == NULL
13348
0
      || isection->bfd_section->output_section == NULL)
13349
0
    {
13350
0
      _bfd_error_handler
13351
  /* xgettext:c-format */
13352
0
  (_("%pB(%pA): info section index cannot be set"
13353
0
     " because the section is not in the output"),
13354
0
  obfd, osec);
13355
0
      bfd_set_error (bfd_error_bad_value);
13356
0
      return false;
13357
0
    }
13358
13359
0
  esd = elf_section_data (isection->bfd_section->output_section);
13360
0
  BFD_ASSERT (esd != NULL);
13361
0
  osection->sh_info = esd->this_idx;
13362
0
  esd->has_secondary_relocs = true;
13363
#if DEBUG_SECONDARY_RELOCS
13364
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13365
     osec->name, osection->sh_link, osection->sh_info);
13366
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13367
     bfd_section_name (isection->bfd_section->output_section));
13368
#endif
13369
13370
0
  return true;
13371
0
}
13372
13373
/* Write out a secondary reloc section.
13374
13375
   FIXME: Currently this function can result in a serious performance penalty
13376
   for files with secondary relocs and lots of sections.  The proper way to
13377
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13378
   relocs together and then to have this function just walk that chain.  */
13379
13380
bool
13381
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13382
0
{
13383
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13384
0
  bfd_vma addr_offset;
13385
0
  asection * relsec;
13386
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13387
0
  bool result = true;
13388
13389
0
  if (sec == NULL)
13390
0
    return false;
13391
13392
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13393
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13394
0
    r_info = elf64_r_info;
13395
0
  else
13396
0
#endif
13397
0
    r_info = elf32_r_info;
13398
13399
  /* The address of an ELF reloc is section relative for an object
13400
     file, and absolute for an executable file or shared library.
13401
     The address of a BFD reloc is always section relative.  */
13402
0
  addr_offset = 0;
13403
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13404
0
    addr_offset = sec->vma;
13405
13406
  /* Discover if there are any secondary reloc sections
13407
     associated with SEC.  */
13408
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13409
0
    {
13410
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13411
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13412
13413
0
      if (hdr->sh_type == SHT_RELA
13414
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13415
0
  {
13416
0
    asymbol *    last_sym;
13417
0
    int          last_sym_idx;
13418
0
    size_t       reloc_count;
13419
0
    size_t       idx;
13420
0
    bfd_size_type entsize;
13421
0
    arelent *    src_irel;
13422
0
    bfd_byte *   dst_rela;
13423
13424
0
    if (hdr->contents != NULL)
13425
0
      {
13426
0
        _bfd_error_handler
13427
    /* xgettext:c-format */
13428
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13429
0
     abfd, relsec);
13430
0
        bfd_set_error (bfd_error_bad_value);
13431
0
        result = false;
13432
0
        continue;
13433
0
      }
13434
13435
0
    entsize = hdr->sh_entsize;
13436
0
    if (entsize == 0)
13437
0
      {
13438
0
        _bfd_error_handler
13439
    /* xgettext:c-format */
13440
0
    (_("%pB(%pA): error: secondary reloc section"
13441
0
       " has zero sized entries"),
13442
0
     abfd, relsec);
13443
0
        bfd_set_error (bfd_error_bad_value);
13444
0
        result = false;
13445
0
        continue;
13446
0
      }
13447
0
    else if (entsize != ebd->s->sizeof_rel
13448
0
       && entsize != ebd->s->sizeof_rela)
13449
0
      {
13450
0
        _bfd_error_handler
13451
    /* xgettext:c-format */
13452
0
    (_("%pB(%pA): error: secondary reloc section"
13453
0
       " has non-standard sized entries"),
13454
0
     abfd, relsec);
13455
0
        bfd_set_error (bfd_error_bad_value);
13456
0
        result = false;
13457
0
        continue;
13458
0
      }
13459
13460
0
    reloc_count = hdr->sh_size / entsize;
13461
0
    hdr->sh_size = entsize * reloc_count;
13462
0
    if (reloc_count == 0)
13463
0
      {
13464
0
        _bfd_error_handler
13465
    /* xgettext:c-format */
13466
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13467
0
     abfd, relsec);
13468
0
        bfd_set_error (bfd_error_bad_value);
13469
0
        result = false;
13470
0
        continue;
13471
0
      }
13472
13473
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13474
0
    if (hdr->contents == NULL)
13475
0
      continue;
13476
13477
#if DEBUG_SECONDARY_RELOCS
13478
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13479
       reloc_count, sec->name, relsec->name);
13480
#endif
13481
0
    last_sym = NULL;
13482
0
    last_sym_idx = 0;
13483
0
    dst_rela = hdr->contents;
13484
0
    src_irel = (arelent *) esd->sec_info;
13485
0
    if (src_irel == NULL)
13486
0
      {
13487
0
        _bfd_error_handler
13488
    /* xgettext:c-format */
13489
0
    (_("%pB(%pA): error: internal relocs missing"
13490
0
       " for secondary reloc section"),
13491
0
     abfd, relsec);
13492
0
        bfd_set_error (bfd_error_bad_value);
13493
0
        result = false;
13494
0
        continue;
13495
0
      }
13496
13497
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13498
0
      {
13499
0
        Elf_Internal_Rela src_rela;
13500
0
        arelent *ptr;
13501
0
        asymbol *sym;
13502
0
        int n;
13503
13504
0
        ptr = src_irel + idx;
13505
0
        if (ptr == NULL)
13506
0
    {
13507
0
      _bfd_error_handler
13508
        /* xgettext:c-format */
13509
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
13510
0
         abfd, relsec, idx);
13511
0
      bfd_set_error (bfd_error_bad_value);
13512
0
      result = false;
13513
0
      break;
13514
0
    }
13515
13516
0
        if (ptr->sym_ptr_ptr == NULL)
13517
0
    {
13518
      /* FIXME: Is this an error ? */
13519
0
      n = 0;
13520
0
    }
13521
0
        else
13522
0
    {
13523
0
      sym = *ptr->sym_ptr_ptr;
13524
13525
0
      if (sym == last_sym)
13526
0
        n = last_sym_idx;
13527
0
      else
13528
0
        {
13529
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13530
0
          if (n < 0)
13531
0
      {
13532
0
        _bfd_error_handler
13533
          /* xgettext:c-format */
13534
0
          (_("%pB(%pA): error: secondary reloc %zu"
13535
0
             " references a missing symbol"),
13536
0
           abfd, relsec, idx);
13537
0
        bfd_set_error (bfd_error_bad_value);
13538
0
        result = false;
13539
0
        n = 0;
13540
0
      }
13541
13542
0
          last_sym = sym;
13543
0
          last_sym_idx = n;
13544
0
        }
13545
13546
0
      if (sym->the_bfd != NULL
13547
0
          && sym->the_bfd->xvec != abfd->xvec
13548
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
13549
0
        {
13550
0
          _bfd_error_handler
13551
      /* xgettext:c-format */
13552
0
      (_("%pB(%pA): error: secondary reloc %zu"
13553
0
         " references a deleted symbol"),
13554
0
       abfd, relsec, idx);
13555
0
          bfd_set_error (bfd_error_bad_value);
13556
0
          result = false;
13557
0
          n = 0;
13558
0
        }
13559
0
    }
13560
13561
0
        src_rela.r_offset = ptr->address + addr_offset;
13562
0
        if (ptr->howto == NULL)
13563
0
    {
13564
0
      _bfd_error_handler
13565
        /* xgettext:c-format */
13566
0
        (_("%pB(%pA): error: secondary reloc %zu"
13567
0
           " is of an unknown type"),
13568
0
         abfd, relsec, idx);
13569
0
      bfd_set_error (bfd_error_bad_value);
13570
0
      result = false;
13571
0
      src_rela.r_info = r_info (0, 0);
13572
0
    }
13573
0
        else
13574
0
    src_rela.r_info = r_info (n, ptr->howto->type);
13575
0
        src_rela.r_addend = ptr->addend;
13576
13577
0
        if (entsize == ebd->s->sizeof_rel)
13578
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13579
0
        else /* entsize == ebd->s->sizeof_rela */
13580
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13581
0
      }
13582
0
  }
13583
0
    }
13584
13585
0
  return result;
13586
0
}