Coverage Report

Created: 2023-08-28 06:23

/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
0
{
70
0
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
0
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
0
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
0
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
0
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
0
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
0
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
0
}
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
0
{
102
0
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
103
0
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
104
0
}
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
0
{
124
0
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
0
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
0
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
0
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
0
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
0
}
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
0
{
152
0
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
0
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
0
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
0
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
0
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
0
}
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
0
{
180
0
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
0
}
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
0
{
231
0
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232
0
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
233
0
  if (abfd->tdata.any == NULL)
234
0
    return false;
235
236
0
  elf_object_id (abfd) = object_id;
237
0
  if (abfd->direction != read_direction)
238
0
    {
239
0
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240
0
      if (o == NULL)
241
0
  return false;
242
0
      elf_tdata (abfd)->o = o;
243
0
      elf_program_header_size (abfd) = (bfd_size_type) -1;
244
0
    }
245
0
  return true;
246
0
}
247
248
249
bool
250
bfd_elf_make_object (bfd *abfd)
251
0
{
252
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253
0
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254
0
          bed->target_id);
255
0
}
256
257
bool
258
bfd_elf_mkcorefile (bfd *abfd)
259
0
{
260
  /* I think this can be done just like an object file.  */
261
0
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262
0
    return false;
263
0
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264
0
  return elf_tdata (abfd)->core != NULL;
265
0
}
266
267
char *
268
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
269
0
{
270
0
  Elf_Internal_Shdr **i_shdrp;
271
0
  bfd_byte *shstrtab = NULL;
272
0
  file_ptr offset;
273
0
  bfd_size_type shstrtabsize;
274
275
0
  i_shdrp = elf_elfsections (abfd);
276
0
  if (i_shdrp == 0
277
0
      || shindex >= elf_numsections (abfd)
278
0
      || i_shdrp[shindex] == 0)
279
0
    return NULL;
280
281
0
  shstrtab = i_shdrp[shindex]->contents;
282
0
  if (shstrtab == NULL)
283
0
    {
284
      /* No cached one, attempt to read, and cache what we read.  */
285
0
      offset = i_shdrp[shindex]->sh_offset;
286
0
      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
0
      if (shstrtabsize + 1 <= 1
291
0
    || bfd_seek (abfd, offset, SEEK_SET) != 0
292
0
    || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293
0
                shstrtabsize)) == NULL)
294
0
  {
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
0
    i_shdrp[shindex]->sh_size = 0;
299
0
  }
300
0
      else
301
0
  shstrtab[shstrtabsize] = '\0';
302
0
      i_shdrp[shindex]->contents = shstrtab;
303
0
    }
304
0
  return (char *) shstrtab;
305
0
}
306
307
char *
308
bfd_elf_string_from_elf_section (bfd *abfd,
309
         unsigned int shindex,
310
         unsigned int strindex)
311
0
{
312
0
  Elf_Internal_Shdr *hdr;
313
314
0
  if (strindex == 0)
315
0
    return "";
316
317
0
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318
0
    return NULL;
319
320
0
  hdr = elf_elfsections (abfd)[shindex];
321
322
0
  if (hdr->contents == NULL)
323
0
    {
324
0
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325
0
  {
326
    /* PR 17512: file: f057ec89.  */
327
    /* xgettext:c-format */
328
0
    _bfd_error_handler (_("%pB: attempt to load strings from"
329
0
        " a non-string section (number %d)"),
330
0
            abfd, shindex);
331
0
    return NULL;
332
0
  }
333
334
0
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335
0
  return NULL;
336
0
    }
337
0
  else
338
0
    {
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
0
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345
0
  return NULL;
346
0
    }
347
348
0
  if (strindex >= hdr->sh_size)
349
0
    {
350
0
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351
0
      _bfd_error_handler
352
  /* xgettext:c-format */
353
0
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354
0
   abfd, strindex, (uint64_t) hdr->sh_size,
355
0
   (shindex == shstrndx && strindex == hdr->sh_name
356
0
    ? ".shstrtab"
357
0
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
0
      return NULL;
359
0
    }
360
361
0
  return ((char *) hdr->contents) + strindex;
362
0
}
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
0
{
381
0
  Elf_Internal_Shdr *shndx_hdr;
382
0
  void *alloc_ext;
383
0
  const bfd_byte *esym;
384
0
  Elf_External_Sym_Shndx *alloc_extshndx;
385
0
  Elf_External_Sym_Shndx *shndx;
386
0
  Elf_Internal_Sym *alloc_intsym;
387
0
  Elf_Internal_Sym *isym;
388
0
  Elf_Internal_Sym *isymend;
389
0
  const struct elf_backend_data *bed;
390
0
  size_t extsym_size;
391
0
  size_t amt;
392
0
  file_ptr pos;
393
394
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
0
    abort ();
396
397
0
  if (symcount == 0)
398
0
    return intsym_buf;
399
400
0
  if (elf_use_dt_symtab_p (ibfd))
401
0
    {
402
      /* Use dynamic symbol table.  */
403
0
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
404
0
  {
405
0
    bfd_set_error (bfd_error_invalid_operation);
406
0
    return NULL;
407
0
  }
408
0
      return elf_tdata (ibfd)->dt_symtab + symoffset;
409
0
    }
410
411
  /* Normal syms might have section extension entries.  */
412
0
  shndx_hdr = NULL;
413
0
  if (elf_symtab_shndx_list (ibfd) != NULL)
414
0
    {
415
0
      elf_section_list * entry;
416
0
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418
      /* Find an index section that is linked to this symtab section.  */
419
0
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420
0
  {
421
    /* PR 20063.  */
422
0
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
423
0
      continue;
424
425
0
    if (sections[entry->hdr.sh_link] == symtab_hdr)
426
0
      {
427
0
        shndx_hdr = & entry->hdr;
428
0
        break;
429
0
      };
430
0
  }
431
432
0
      if (shndx_hdr == NULL)
433
0
  {
434
0
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
435
      /* Not really accurate, but this was how the old code used
436
         to work.  */
437
0
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438
    /* Otherwise we do nothing.  The assumption is that
439
       the index table will not be needed.  */
440
0
  }
441
0
    }
442
443
  /* Read the symbols.  */
444
0
  alloc_ext = NULL;
445
0
  alloc_extshndx = NULL;
446
0
  alloc_intsym = NULL;
447
0
  bed = get_elf_backend_data (ibfd);
448
0
  extsym_size = bed->s->sizeof_sym;
449
0
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
450
0
    {
451
0
      bfd_set_error (bfd_error_file_too_big);
452
0
      intsym_buf = NULL;
453
0
      goto out;
454
0
    }
455
0
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456
0
  if (extsym_buf == NULL)
457
0
    {
458
0
      alloc_ext = bfd_malloc (amt);
459
0
      extsym_buf = alloc_ext;
460
0
    }
461
0
  if (extsym_buf == NULL
462
0
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
463
0
      || bfd_read (extsym_buf, amt, ibfd) != amt)
464
0
    {
465
0
      intsym_buf = NULL;
466
0
      goto out;
467
0
    }
468
469
0
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
470
0
    extshndx_buf = NULL;
471
0
  else
472
0
    {
473
0
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
474
0
  {
475
0
    bfd_set_error (bfd_error_file_too_big);
476
0
    intsym_buf = NULL;
477
0
    goto out;
478
0
  }
479
0
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
480
0
      if (extshndx_buf == NULL)
481
0
  {
482
0
    alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
483
0
    extshndx_buf = alloc_extshndx;
484
0
  }
485
0
      if (extshndx_buf == NULL
486
0
    || bfd_seek (ibfd, pos, SEEK_SET) != 0
487
0
    || bfd_read (extshndx_buf, amt, ibfd) != amt)
488
0
  {
489
0
    intsym_buf = NULL;
490
0
    goto out;
491
0
  }
492
0
    }
493
494
0
  if (intsym_buf == NULL)
495
0
    {
496
0
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
497
0
  {
498
0
    bfd_set_error (bfd_error_file_too_big);
499
0
    goto out;
500
0
  }
501
0
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
502
0
      intsym_buf = alloc_intsym;
503
0
      if (intsym_buf == NULL)
504
0
  goto out;
505
0
    }
506
507
  /* Convert the symbols to internal form.  */
508
0
  isymend = intsym_buf + symcount;
509
0
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
510
0
     shndx = extshndx_buf;
511
0
       isym < isymend;
512
0
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
513
0
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
514
0
      {
515
0
  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
516
  /* xgettext:c-format */
517
0
  _bfd_error_handler (_("%pB symbol number %lu references"
518
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
519
0
          ibfd, (unsigned long) symoffset);
520
0
  free (alloc_intsym);
521
0
  intsym_buf = NULL;
522
0
  goto out;
523
0
      }
524
525
0
 out:
526
0
  free (alloc_ext);
527
0
  free (alloc_extshndx);
528
529
0
  return intsym_buf;
530
0
}
531
532
/* Look up a symbol name.  */
533
const char *
534
bfd_elf_sym_name (bfd *abfd,
535
      Elf_Internal_Shdr *symtab_hdr,
536
      Elf_Internal_Sym *isym,
537
      asection *sym_sec)
538
0
{
539
0
  const char *name;
540
0
  unsigned int iname = isym->st_name;
541
0
  unsigned int shindex = symtab_hdr->sh_link;
542
543
0
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
544
      /* Check for a bogus st_shndx to avoid crashing.  */
545
0
      && isym->st_shndx < elf_numsections (abfd))
546
0
    {
547
0
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
548
0
      shindex = elf_elfheader (abfd)->e_shstrndx;
549
0
    }
550
551
0
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
552
0
  if (name == NULL)
553
0
    name = "(null)";
554
0
  else if (sym_sec && *name == '\0')
555
0
    name = bfd_section_name (sym_sec);
556
557
0
  return name;
558
0
}
559
560
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
561
   sections.  The first element is the flags, the rest are section
562
   pointers.  */
563
564
typedef union elf_internal_group {
565
  Elf_Internal_Shdr *shdr;
566
  unsigned int flags;
567
} Elf_Internal_Group;
568
569
/* Return the name of the group signature symbol.  Why isn't the
570
   signature just a string?  */
571
572
static const char *
573
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
574
0
{
575
0
  Elf_Internal_Shdr *hdr;
576
0
  unsigned char esym[sizeof (Elf64_External_Sym)];
577
0
  Elf_External_Sym_Shndx eshndx;
578
0
  Elf_Internal_Sym isym;
579
580
  /* First we need to ensure the symbol table is available.  Make sure
581
     that it is a symbol table section.  */
582
0
  if (ghdr->sh_link >= elf_numsections (abfd))
583
0
    return NULL;
584
0
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
585
0
  if (hdr->sh_type != SHT_SYMTAB
586
0
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
587
0
    return NULL;
588
589
  /* Go read the symbol.  */
590
0
  hdr = &elf_tdata (abfd)->symtab_hdr;
591
0
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
592
0
          &isym, esym, &eshndx) == NULL)
593
0
    return NULL;
594
595
0
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
596
0
}
597
598
/* Set next_in_group list pointer, and group name for NEWSECT.  */
599
600
static bool
601
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
602
0
{
603
0
  unsigned int num_group = elf_tdata (abfd)->num_group;
604
605
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
606
     is set to -1 if there are no SHT_GROUP sections.  */
607
0
  if (num_group == 0)
608
0
    {
609
0
      unsigned int i, shnum;
610
611
      /* First count the number of groups.  If we have a SHT_GROUP
612
   section with just a flag word (ie. sh_size is 4), ignore it.  */
613
0
      shnum = elf_numsections (abfd);
614
0
      num_group = 0;
615
616
0
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)  \
617
0
  (   (shdr)->sh_type == SHT_GROUP    \
618
0
   && (shdr)->sh_size >= minsize      \
619
0
   && (shdr)->sh_entsize == GRP_ENTRY_SIZE  \
620
0
   && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
621
622
0
      for (i = 0; i < shnum; i++)
623
0
  {
624
0
    Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
625
626
0
    if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627
0
      num_group += 1;
628
0
  }
629
630
0
      if (num_group == 0)
631
0
  {
632
0
    num_group = (unsigned) -1;
633
0
    elf_tdata (abfd)->num_group = num_group;
634
0
    elf_tdata (abfd)->group_sect_ptr = NULL;
635
0
  }
636
0
      else
637
0
  {
638
    /* We keep a list of elf section headers for group sections,
639
       so we can find them quickly.  */
640
0
    size_t amt;
641
642
0
    elf_tdata (abfd)->num_group = num_group;
643
0
    amt = num_group * sizeof (Elf_Internal_Shdr *);
644
0
    elf_tdata (abfd)->group_sect_ptr
645
0
      = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
646
0
    if (elf_tdata (abfd)->group_sect_ptr == NULL)
647
0
      return false;
648
0
    num_group = 0;
649
650
0
    for (i = 0; i < shnum; i++)
651
0
      {
652
0
        Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
653
654
0
        if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
655
0
    {
656
0
      unsigned char *src;
657
0
      Elf_Internal_Group *dest;
658
659
      /* Make sure the group section has a BFD section
660
         attached to it.  */
661
0
      if (!bfd_section_from_shdr (abfd, i))
662
0
        return false;
663
664
      /* Add to list of sections.  */
665
0
      elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
666
0
      num_group += 1;
667
668
      /* Read the raw contents.  */
669
0
      BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
670
0
      shdr->contents = NULL;
671
0
      if (_bfd_mul_overflow (shdr->sh_size,
672
0
           sizeof (*dest) / 4, &amt)
673
0
          || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
674
0
          || !(shdr->contents
675
0
         = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
676
0
        {
677
0
          _bfd_error_handler
678
      /* xgettext:c-format */
679
0
      (_("%pB: invalid size field in group section"
680
0
         " header: %#" PRIx64 ""),
681
0
       abfd, (uint64_t) shdr->sh_size);
682
0
          bfd_set_error (bfd_error_bad_value);
683
0
          -- num_group;
684
0
          continue;
685
0
        }
686
687
      /* Translate raw contents, a flag word followed by an
688
         array of elf section indices all in target byte order,
689
         to the flag word followed by an array of elf section
690
         pointers.  */
691
0
      src = shdr->contents + shdr->sh_size;
692
0
      dest = (Elf_Internal_Group *) (shdr->contents + amt);
693
694
0
      while (1)
695
0
        {
696
0
          unsigned int idx;
697
698
0
          src -= 4;
699
0
          --dest;
700
0
          idx = H_GET_32 (abfd, src);
701
0
          if (src == shdr->contents)
702
0
      {
703
0
        dest->shdr = NULL;
704
0
        dest->flags = idx;
705
0
        if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
706
0
          shdr->bfd_section->flags
707
0
            |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
708
0
        break;
709
0
      }
710
0
          if (idx < shnum)
711
0
      {
712
0
        dest->shdr = elf_elfsections (abfd)[idx];
713
        /* PR binutils/23199: All sections in a
714
           section group should be marked with
715
           SHF_GROUP.  But some tools generate
716
           broken objects without SHF_GROUP.  Fix
717
           them up here.  */
718
0
        dest->shdr->sh_flags |= SHF_GROUP;
719
0
      }
720
0
          if (idx >= shnum
721
0
        || dest->shdr->sh_type == SHT_GROUP)
722
0
      {
723
0
        _bfd_error_handler
724
0
          (_("%pB: invalid entry in SHT_GROUP section [%u]"),
725
0
             abfd, i);
726
0
        dest->shdr = NULL;
727
0
      }
728
0
        }
729
0
    }
730
0
      }
731
732
    /* PR 17510: Corrupt binaries might contain invalid groups.  */
733
0
    if (num_group != (unsigned) elf_tdata (abfd)->num_group)
734
0
      {
735
0
        elf_tdata (abfd)->num_group = num_group;
736
737
        /* If all groups are invalid then fail.  */
738
0
        if (num_group == 0)
739
0
    {
740
0
      elf_tdata (abfd)->group_sect_ptr = NULL;
741
0
      elf_tdata (abfd)->num_group = num_group = -1;
742
0
      _bfd_error_handler
743
0
        (_("%pB: no valid group sections found"), abfd);
744
0
      bfd_set_error (bfd_error_bad_value);
745
0
    }
746
0
      }
747
0
  }
748
0
    }
749
750
0
  if (num_group != (unsigned) -1)
751
0
    {
752
0
      unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
753
0
      unsigned int j;
754
755
0
      for (j = 0; j < num_group; j++)
756
0
  {
757
    /* Begin search from previous found group.  */
758
0
    unsigned i = (j + search_offset) % num_group;
759
760
0
    Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
761
0
    Elf_Internal_Group *idx;
762
0
    bfd_size_type n_elt;
763
764
0
    if (shdr == NULL)
765
0
      continue;
766
767
0
    idx = (Elf_Internal_Group *) shdr->contents;
768
0
    if (idx == NULL || shdr->sh_size < 4)
769
0
      {
770
        /* See PR 21957 for a reproducer.  */
771
        /* xgettext:c-format */
772
0
        _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
773
0
          abfd, shdr->bfd_section);
774
0
        elf_tdata (abfd)->group_sect_ptr[i] = NULL;
775
0
        bfd_set_error (bfd_error_bad_value);
776
0
        return false;
777
0
      }
778
0
    n_elt = shdr->sh_size / 4;
779
780
    /* Look through this group's sections to see if current
781
       section is a member.  */
782
0
    while (--n_elt != 0)
783
0
      if ((++idx)->shdr == hdr)
784
0
        {
785
0
    asection *s = NULL;
786
787
    /* We are a member of this group.  Go looking through
788
       other members to see if any others are linked via
789
       next_in_group.  */
790
0
    idx = (Elf_Internal_Group *) shdr->contents;
791
0
    n_elt = shdr->sh_size / 4;
792
0
    while (--n_elt != 0)
793
0
      if ((++idx)->shdr != NULL
794
0
          && (s = idx->shdr->bfd_section) != NULL
795
0
          && elf_next_in_group (s) != NULL)
796
0
        break;
797
0
    if (n_elt != 0)
798
0
      {
799
        /* Snarf the group name from other member, and
800
           insert current section in circular list.  */
801
0
        elf_group_name (newsect) = elf_group_name (s);
802
0
        elf_next_in_group (newsect) = elf_next_in_group (s);
803
0
        elf_next_in_group (s) = newsect;
804
0
      }
805
0
    else
806
0
      {
807
0
        const char *gname;
808
809
0
        gname = group_signature (abfd, shdr);
810
0
        if (gname == NULL)
811
0
          return false;
812
0
        elf_group_name (newsect) = gname;
813
814
        /* Start a circular list with one element.  */
815
0
        elf_next_in_group (newsect) = newsect;
816
0
      }
817
818
    /* If the group section has been created, point to the
819
       new member.  */
820
0
    if (shdr->bfd_section != NULL)
821
0
      elf_next_in_group (shdr->bfd_section) = newsect;
822
823
0
    elf_tdata (abfd)->group_search_offset = i;
824
0
    j = num_group - 1;
825
0
    break;
826
0
        }
827
0
  }
828
0
    }
829
830
0
  if (elf_group_name (newsect) == NULL)
831
0
    {
832
      /* xgettext:c-format */
833
0
      _bfd_error_handler (_("%pB: no group info for section '%pA'"),
834
0
        abfd, newsect);
835
      /* PR 29532: Return true here, even though the group info has not been
836
   read.  Separate debug info files can have empty group sections, but
837
   we do not want this to prevent them from being loaded as otherwise
838
   GDB will not be able to use them.  */
839
0
      return true;
840
0
    }
841
0
  return true;
842
0
}
843
844
bool
845
_bfd_elf_setup_sections (bfd *abfd)
846
0
{
847
0
  unsigned int i;
848
0
  unsigned int num_group = elf_tdata (abfd)->num_group;
849
0
  bool result = true;
850
0
  asection *s;
851
852
  /* Process SHF_LINK_ORDER.  */
853
0
  for (s = abfd->sections; s != NULL; s = s->next)
854
0
    {
855
0
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
856
0
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
857
0
  {
858
0
    unsigned int elfsec = this_hdr->sh_link;
859
    /* An sh_link value of 0 is now allowed.  It indicates that linked
860
       to section has already been discarded, but that the current
861
       section has been retained for some other reason.  This linking
862
       section is still a candidate for later garbage collection
863
       however.  */
864
0
    if (elfsec == 0)
865
0
      {
866
0
        elf_linked_to_section (s) = NULL;
867
0
      }
868
0
    else
869
0
      {
870
0
        asection *linksec = NULL;
871
872
0
        if (elfsec < elf_numsections (abfd))
873
0
    {
874
0
      this_hdr = elf_elfsections (abfd)[elfsec];
875
0
      linksec = this_hdr->bfd_section;
876
0
    }
877
878
        /* PR 1991, 2008:
879
     Some strip/objcopy may leave an incorrect value in
880
     sh_link.  We don't want to proceed.  */
881
0
        if (linksec == NULL)
882
0
    {
883
0
      _bfd_error_handler
884
        /* xgettext:c-format */
885
0
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
886
0
         s->owner, elfsec, s);
887
0
      result = false;
888
0
    }
889
890
0
        elf_linked_to_section (s) = linksec;
891
0
      }
892
0
  }
893
0
      else if (this_hdr->sh_type == SHT_GROUP
894
0
         && elf_next_in_group (s) == NULL)
895
0
  {
896
0
    _bfd_error_handler
897
      /* xgettext:c-format */
898
0
      (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
899
0
       abfd, elf_section_data (s)->this_idx);
900
0
    result = false;
901
0
  }
902
0
    }
903
904
  /* Process section groups.  */
905
0
  if (num_group == (unsigned) -1)
906
0
    return result;
907
908
0
  for (i = 0; i < num_group; i++)
909
0
    {
910
0
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
911
0
      Elf_Internal_Group *idx;
912
0
      unsigned int n_elt;
913
914
      /* PR binutils/18758: Beware of corrupt binaries with invalid
915
   group data.  */
916
0
      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
917
0
  {
918
0
    _bfd_error_handler
919
      /* xgettext:c-format */
920
0
      (_("%pB: section group entry number %u is corrupt"),
921
0
       abfd, i);
922
0
    result = false;
923
0
    continue;
924
0
  }
925
926
0
      idx = (Elf_Internal_Group *) shdr->contents;
927
0
      n_elt = shdr->sh_size / 4;
928
929
0
      while (--n_elt != 0)
930
0
  {
931
0
    ++ idx;
932
933
0
    if (idx->shdr == NULL)
934
0
      continue;
935
0
    else if (idx->shdr->bfd_section)
936
0
      elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
937
0
    else if (idx->shdr->sh_type != SHT_RELA
938
0
       && idx->shdr->sh_type != SHT_REL)
939
0
      {
940
        /* There are some unknown sections in the group.  */
941
0
        _bfd_error_handler
942
    /* xgettext:c-format */
943
0
    (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
944
0
     abfd,
945
0
     idx->shdr->sh_type,
946
0
     bfd_elf_string_from_elf_section (abfd,
947
0
              (elf_elfheader (abfd)
948
0
               ->e_shstrndx),
949
0
              idx->shdr->sh_name),
950
0
     shdr->bfd_section);
951
0
        result = false;
952
0
      }
953
0
  }
954
0
    }
955
956
0
  return result;
957
0
}
958
959
bool
960
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
961
0
{
962
0
  return elf_next_in_group (sec) != NULL;
963
0
}
964
965
const char *
966
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
967
0
{
968
0
  if (elf_sec_group (sec) != NULL)
969
0
    return elf_group_name (sec);
970
0
  return NULL;
971
0
}
972
973
/* This a copy of lto_section defined in GCC (lto-streamer.h).  */
974
975
struct lto_section
976
{
977
  int16_t major_version;
978
  int16_t minor_version;
979
  unsigned char slim_object;
980
981
  /* Flags is a private field that is not defined publicly.  */
982
  uint16_t flags;
983
};
984
985
/* Make a BFD section from an ELF section.  We store a pointer to the
986
   BFD section in the bfd_section field of the header.  */
987
988
bool
989
_bfd_elf_make_section_from_shdr (bfd *abfd,
990
         Elf_Internal_Shdr *hdr,
991
         const char *name,
992
         int shindex)
993
0
{
994
0
  asection *newsect;
995
0
  flagword flags;
996
0
  const struct elf_backend_data *bed;
997
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
998
999
0
  if (hdr->bfd_section != NULL)
1000
0
    return true;
1001
1002
0
  newsect = bfd_make_section_anyway (abfd, name);
1003
0
  if (newsect == NULL)
1004
0
    return false;
1005
1006
0
  hdr->bfd_section = newsect;
1007
0
  elf_section_data (newsect)->this_hdr = *hdr;
1008
0
  elf_section_data (newsect)->this_idx = shindex;
1009
1010
  /* Always use the real type/flags.  */
1011
0
  elf_section_type (newsect) = hdr->sh_type;
1012
0
  elf_section_flags (newsect) = hdr->sh_flags;
1013
1014
0
  newsect->filepos = hdr->sh_offset;
1015
1016
0
  flags = SEC_NO_FLAGS;
1017
0
  if (hdr->sh_type != SHT_NOBITS)
1018
0
    flags |= SEC_HAS_CONTENTS;
1019
0
  if (hdr->sh_type == SHT_GROUP)
1020
0
    flags |= SEC_GROUP;
1021
0
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
1022
0
    {
1023
0
      flags |= SEC_ALLOC;
1024
0
      if (hdr->sh_type != SHT_NOBITS)
1025
0
  flags |= SEC_LOAD;
1026
0
    }
1027
0
  if ((hdr->sh_flags & SHF_WRITE) == 0)
1028
0
    flags |= SEC_READONLY;
1029
0
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1030
0
    flags |= SEC_CODE;
1031
0
  else if ((flags & SEC_LOAD) != 0)
1032
0
    flags |= SEC_DATA;
1033
0
  if ((hdr->sh_flags & SHF_MERGE) != 0)
1034
0
    {
1035
0
      flags |= SEC_MERGE;
1036
0
      newsect->entsize = hdr->sh_entsize;
1037
0
    }
1038
0
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
1039
0
    flags |= SEC_STRINGS;
1040
0
  if (hdr->sh_flags & SHF_GROUP)
1041
0
    if (!setup_group (abfd, hdr, newsect))
1042
0
      return false;
1043
0
  if ((hdr->sh_flags & SHF_TLS) != 0)
1044
0
    flags |= SEC_THREAD_LOCAL;
1045
0
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1046
0
    flags |= SEC_EXCLUDE;
1047
1048
0
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1049
0
    {
1050
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1051
   but binutils as of 2019-07-23 did not set the EI_OSABI header
1052
   byte.  */
1053
0
    case ELFOSABI_GNU:
1054
0
    case ELFOSABI_FREEBSD:
1055
0
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1056
0
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1057
      /* Fall through */
1058
0
    case ELFOSABI_NONE:
1059
0
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1060
0
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1061
0
      break;
1062
0
    }
1063
1064
0
  if ((flags & SEC_ALLOC) == 0)
1065
0
    {
1066
      /* The debugging sections appear to be recognized only by name,
1067
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
1068
0
      if (name [0] == '.')
1069
0
  {
1070
0
    if (startswith (name, ".debug")
1071
0
        || startswith (name, ".gnu.debuglto_.debug_")
1072
0
        || startswith (name, ".gnu.linkonce.wi.")
1073
0
        || startswith (name, ".zdebug"))
1074
0
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1075
0
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1076
0
       || startswith (name, ".note.gnu"))
1077
0
      {
1078
0
        flags |= SEC_ELF_OCTETS;
1079
0
        opb = 1;
1080
0
      }
1081
0
    else if (startswith (name, ".line")
1082
0
       || startswith (name, ".stab")
1083
0
       || strcmp (name, ".gdb_index") == 0)
1084
0
      flags |= SEC_DEBUGGING;
1085
0
  }
1086
0
    }
1087
1088
0
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1089
0
      || !bfd_set_section_size (newsect, hdr->sh_size)
1090
0
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1091
0
              & -hdr->sh_addralign)))
1092
0
    return false;
1093
1094
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
1095
     only link a single copy of the section.  This is used to support
1096
     g++.  g++ will emit each template expansion in its own section.
1097
     The symbols will be defined as weak, so that multiple definitions
1098
     are permitted.  The GNU linker extension is to actually discard
1099
     all but one of the sections.  */
1100
0
  if (startswith (name, ".gnu.linkonce")
1101
0
      && elf_next_in_group (newsect) == NULL)
1102
0
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1103
1104
0
  if (!bfd_set_section_flags (newsect, flags))
1105
0
    return false;
1106
1107
0
  bed = get_elf_backend_data (abfd);
1108
0
  if (bed->elf_backend_section_flags)
1109
0
    if (!bed->elf_backend_section_flags (hdr))
1110
0
      return false;
1111
1112
  /* We do not parse the PT_NOTE segments as we are interested even in the
1113
     separate debug info files which may have the segments offsets corrupted.
1114
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1115
0
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1116
0
    {
1117
0
      bfd_byte *contents;
1118
1119
0
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1120
0
  return false;
1121
1122
0
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1123
0
           hdr->sh_offset, hdr->sh_addralign);
1124
0
      free (contents);
1125
0
    }
1126
1127
0
  if ((newsect->flags & SEC_ALLOC) != 0)
1128
0
    {
1129
0
      Elf_Internal_Phdr *phdr;
1130
0
      unsigned int i, nload;
1131
1132
      /* Some ELF linkers produce binaries with all the program header
1133
   p_paddr fields zero.  If we have such a binary with more than
1134
   one PT_LOAD header, then leave the section lma equal to vma
1135
   so that we don't create sections with overlapping lma.  */
1136
0
      phdr = elf_tdata (abfd)->phdr;
1137
0
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1138
0
  if (phdr->p_paddr != 0)
1139
0
    break;
1140
0
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1141
0
    ++nload;
1142
0
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1143
0
  return true;
1144
1145
0
      phdr = elf_tdata (abfd)->phdr;
1146
0
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1147
0
  {
1148
0
    if (((phdr->p_type == PT_LOAD
1149
0
    && (hdr->sh_flags & SHF_TLS) == 0)
1150
0
         || phdr->p_type == PT_TLS)
1151
0
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1152
0
      {
1153
0
        if ((newsect->flags & SEC_LOAD) == 0)
1154
0
    newsect->lma = (phdr->p_paddr
1155
0
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1156
0
        else
1157
    /* We used to use the same adjustment for SEC_LOAD
1158
       sections, but that doesn't work if the segment
1159
       is packed with code from multiple VMAs.
1160
       Instead we calculate the section LMA based on
1161
       the segment LMA.  It is assumed that the
1162
       segment will contain sections with contiguous
1163
       LMAs, even if the VMAs are not.  */
1164
0
    newsect->lma = (phdr->p_paddr
1165
0
        + hdr->sh_offset - phdr->p_offset) / opb;
1166
1167
        /* With contiguous segments, we can't tell from file
1168
     offsets whether a section with zero size should
1169
     be placed at the end of one segment or the
1170
     beginning of the next.  Decide based on vaddr.  */
1171
0
        if (hdr->sh_addr >= phdr->p_vaddr
1172
0
      && (hdr->sh_addr + hdr->sh_size
1173
0
          <= phdr->p_vaddr + phdr->p_memsz))
1174
0
    break;
1175
0
      }
1176
0
  }
1177
0
    }
1178
1179
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1180
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1181
0
  if ((newsect->flags & SEC_DEBUGGING) != 0
1182
0
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1183
0
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1184
0
    {
1185
0
      enum { nothing, compress, decompress } action = nothing;
1186
0
      int compression_header_size;
1187
0
      bfd_size_type uncompressed_size;
1188
0
      unsigned int uncompressed_align_power;
1189
0
      enum compression_type ch_type = ch_none;
1190
0
      bool compressed
1191
0
  = bfd_is_section_compressed_info (abfd, newsect,
1192
0
            &compression_header_size,
1193
0
            &uncompressed_size,
1194
0
            &uncompressed_align_power,
1195
0
            &ch_type);
1196
1197
      /* Should we decompress?  */
1198
0
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1199
0
  action = decompress;
1200
1201
      /* Should we compress?  Or convert to a different compression?  */
1202
0
      else if ((abfd->flags & BFD_COMPRESS) != 0
1203
0
         && newsect->size != 0
1204
0
         && compression_header_size >= 0
1205
0
         && uncompressed_size > 0)
1206
0
  {
1207
0
    if (!compressed)
1208
0
      action = compress;
1209
0
    else
1210
0
      {
1211
0
        enum compression_type new_ch_type = ch_none;
1212
0
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1213
0
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1214
0
             ? ch_compress_zstd : ch_compress_zlib);
1215
0
        if (new_ch_type != ch_type)
1216
0
    action = compress;
1217
0
      }
1218
0
  }
1219
1220
0
      if (action == compress)
1221
0
  {
1222
0
    if (!bfd_init_section_compress_status (abfd, newsect))
1223
0
      {
1224
0
        _bfd_error_handler
1225
    /* xgettext:c-format */
1226
0
    (_("%pB: unable to compress section %s"), abfd, name);
1227
0
        return false;
1228
0
      }
1229
0
  }
1230
0
      else if (action == decompress)
1231
0
  {
1232
0
    if (!bfd_init_section_decompress_status (abfd, newsect))
1233
0
      {
1234
0
        _bfd_error_handler
1235
    /* xgettext:c-format */
1236
0
    (_("%pB: unable to decompress section %s"), abfd, name);
1237
0
        return false;
1238
0
      }
1239
0
#ifndef HAVE_ZSTD
1240
0
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1241
0
      {
1242
0
        _bfd_error_handler
1243
      /* xgettext:c-format */
1244
0
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1245
0
          "is not built with zstd support"),
1246
0
       abfd, name);
1247
0
        newsect->compress_status = COMPRESS_SECTION_NONE;
1248
0
        return false;
1249
0
      }
1250
0
#endif
1251
0
    if (abfd->is_linker_input
1252
0
        && name[1] == 'z')
1253
0
      {
1254
        /* Rename section from .zdebug_* to .debug_* so that ld
1255
     scripts will see this section as a debug section.  */
1256
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1257
0
        if (new_name == NULL)
1258
0
    return false;
1259
0
        bfd_rename_section (newsect, new_name);
1260
0
      }
1261
0
  }
1262
0
    }
1263
1264
  /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1265
     section.  */
1266
0
  if (startswith (name, ".gnu.lto_.lto."))
1267
0
    {
1268
0
      struct lto_section lsection;
1269
0
      if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1270
0
            sizeof (struct lto_section)))
1271
0
  abfd->lto_slim_object = lsection.slim_object;
1272
0
    }
1273
1274
0
  return true;
1275
0
}
1276
1277
const char *const bfd_elf_section_type_names[] =
1278
{
1279
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1280
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1281
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1282
};
1283
1284
/* ELF relocs are against symbols.  If we are producing relocatable
1285
   output, and the reloc is against an external symbol, and nothing
1286
   has given us any additional addend, the resulting reloc will also
1287
   be against the same symbol.  In such a case, we don't want to
1288
   change anything about the way the reloc is handled, since it will
1289
   all be done at final link time.  Rather than put special case code
1290
   into bfd_perform_relocation, all the reloc types use this howto
1291
   function, or should call this function for relocatable output.  */
1292
1293
bfd_reloc_status_type
1294
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1295
           arelent *reloc_entry,
1296
           asymbol *symbol,
1297
           void *data ATTRIBUTE_UNUSED,
1298
           asection *input_section,
1299
           bfd *output_bfd,
1300
           char **error_message ATTRIBUTE_UNUSED)
1301
0
{
1302
0
  if (output_bfd != NULL
1303
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1304
0
      && (! reloc_entry->howto->partial_inplace
1305
0
    || reloc_entry->addend == 0))
1306
0
    {
1307
0
      reloc_entry->address += input_section->output_offset;
1308
0
      return bfd_reloc_ok;
1309
0
    }
1310
1311
  /* In some cases the relocation should be treated as output section
1312
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1313
     targets lack section relative relocations and instead use
1314
     ordinary absolute relocations for references between DWARF
1315
     sections.  That is arguably a bug in those targets but it happens
1316
     to work for the usual case of linking to non-loaded ELF debug
1317
     sections with VMAs forced to zero.  PE COFF on the other hand
1318
     doesn't allow a section VMA of zero.  */
1319
0
  if (output_bfd == NULL
1320
0
      && !reloc_entry->howto->pc_relative
1321
0
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1322
0
      && (input_section->flags & SEC_DEBUGGING) != 0)
1323
0
    reloc_entry->addend -= symbol->section->output_section->vma;
1324
1325
0
  return bfd_reloc_continue;
1326
0
}
1327

1328
/* Returns TRUE if section A matches section B.
1329
   Names, addresses and links may be different, but everything else
1330
   should be the same.  */
1331
1332
static bool
1333
section_match (const Elf_Internal_Shdr * a,
1334
         const Elf_Internal_Shdr * b)
1335
0
{
1336
0
  if (a->sh_type != b->sh_type
1337
0
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1338
0
      || a->sh_addralign != b->sh_addralign
1339
0
      || a->sh_entsize != b->sh_entsize)
1340
0
    return false;
1341
0
  if (a->sh_type == SHT_SYMTAB
1342
0
      || a->sh_type == SHT_STRTAB)
1343
0
    return true;
1344
0
  return a->sh_size == b->sh_size;
1345
0
}
1346
1347
/* Find a section in OBFD that has the same characteristics
1348
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1349
   none can be found.  Check's section HINT first, as this is likely
1350
   to be the correct section.  */
1351
1352
static unsigned int
1353
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1354
     const unsigned int hint)
1355
0
{
1356
0
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1357
0
  unsigned int i;
1358
1359
0
  BFD_ASSERT (iheader != NULL);
1360
1361
  /* See PR 20922 for a reproducer of the NULL test.  */
1362
0
  if (hint < elf_numsections (obfd)
1363
0
      && oheaders[hint] != NULL
1364
0
      && section_match (oheaders[hint], iheader))
1365
0
    return hint;
1366
1367
0
  for (i = 1; i < elf_numsections (obfd); i++)
1368
0
    {
1369
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1370
1371
0
      if (oheader == NULL)
1372
0
  continue;
1373
0
      if (section_match (oheader, iheader))
1374
  /* FIXME: Do we care if there is a potential for
1375
     multiple matches ?  */
1376
0
  return i;
1377
0
    }
1378
1379
0
  return SHN_UNDEF;
1380
0
}
1381
1382
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1383
   Processor specific section, based upon a matching input section.
1384
   Returns TRUE upon success, FALSE otherwise.  */
1385
1386
static bool
1387
copy_special_section_fields (const bfd *ibfd,
1388
           bfd *obfd,
1389
           const Elf_Internal_Shdr *iheader,
1390
           Elf_Internal_Shdr *oheader,
1391
           const unsigned int secnum)
1392
0
{
1393
0
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1394
0
  const Elf_Internal_Shdr **iheaders
1395
0
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1396
0
  bool changed = false;
1397
0
  unsigned int sh_link;
1398
1399
0
  if (oheader->sh_type == SHT_NOBITS)
1400
0
    {
1401
      /* This is a feature for objcopy --only-keep-debug:
1402
   When a section's type is changed to NOBITS, we preserve
1403
   the sh_link and sh_info fields so that they can be
1404
   matched up with the original.
1405
1406
   Note: Strictly speaking these assignments are wrong.
1407
   The sh_link and sh_info fields should point to the
1408
   relevent sections in the output BFD, which may not be in
1409
   the same location as they were in the input BFD.  But
1410
   the whole point of this action is to preserve the
1411
   original values of the sh_link and sh_info fields, so
1412
   that they can be matched up with the section headers in
1413
   the original file.  So strictly speaking we may be
1414
   creating an invalid ELF file, but it is only for a file
1415
   that just contains debug info and only for sections
1416
   without any contents.  */
1417
0
      if (oheader->sh_link == 0)
1418
0
  oheader->sh_link = iheader->sh_link;
1419
0
      if (oheader->sh_info == 0)
1420
0
  oheader->sh_info = iheader->sh_info;
1421
0
      return true;
1422
0
    }
1423
1424
  /* Allow the target a chance to decide how these fields should be set.  */
1425
0
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1426
0
                iheader, oheader))
1427
0
    return true;
1428
1429
  /* We have an iheader which might match oheader, and which has non-zero
1430
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1431
     the section in the output bfd which corresponds to the linked section
1432
     in the input bfd.  */
1433
0
  if (iheader->sh_link != SHN_UNDEF)
1434
0
    {
1435
      /* See PR 20931 for a reproducer.  */
1436
0
      if (iheader->sh_link >= elf_numsections (ibfd))
1437
0
  {
1438
0
    _bfd_error_handler
1439
      /* xgettext:c-format */
1440
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1441
0
       ibfd, iheader->sh_link, secnum);
1442
0
    return false;
1443
0
  }
1444
1445
0
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1446
0
      if (sh_link != SHN_UNDEF)
1447
0
  {
1448
0
    oheader->sh_link = sh_link;
1449
0
    changed = true;
1450
0
  }
1451
0
      else
1452
  /* FIXME: Should we install iheader->sh_link
1453
     if we could not find a match ?  */
1454
0
  _bfd_error_handler
1455
    /* xgettext:c-format */
1456
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1457
0
    }
1458
1459
0
  if (iheader->sh_info)
1460
0
    {
1461
      /* The sh_info field can hold arbitrary information, but if the
1462
   SHF_LINK_INFO flag is set then it should be interpreted as a
1463
   section index.  */
1464
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1465
0
  {
1466
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1467
0
             iheader->sh_info);
1468
0
    if (sh_link != SHN_UNDEF)
1469
0
      oheader->sh_flags |= SHF_INFO_LINK;
1470
0
  }
1471
0
      else
1472
  /* No idea what it means - just copy it.  */
1473
0
  sh_link = iheader->sh_info;
1474
1475
0
      if (sh_link != SHN_UNDEF)
1476
0
  {
1477
0
    oheader->sh_info = sh_link;
1478
0
    changed = true;
1479
0
  }
1480
0
      else
1481
0
  _bfd_error_handler
1482
    /* xgettext:c-format */
1483
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1484
0
    }
1485
1486
0
  return changed;
1487
0
}
1488
1489
/* Copy the program header and other data from one object module to
1490
   another.  */
1491
1492
bool
1493
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1494
0
{
1495
0
  const Elf_Internal_Shdr **iheaders
1496
0
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1497
0
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1498
0
  const struct elf_backend_data *bed;
1499
0
  unsigned int i;
1500
1501
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1502
0
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1503
0
    return true;
1504
1505
0
  if (!elf_flags_init (obfd))
1506
0
    {
1507
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1508
0
      elf_flags_init (obfd) = true;
1509
0
    }
1510
1511
0
  elf_gp (obfd) = elf_gp (ibfd);
1512
1513
  /* Also copy the EI_OSABI field.  */
1514
0
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1515
0
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1516
1517
  /* If set, copy the EI_ABIVERSION field.  */
1518
0
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1519
0
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1520
0
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1521
1522
  /* Copy object attributes.  */
1523
0
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1524
1525
0
  if (iheaders == NULL || oheaders == NULL)
1526
0
    return true;
1527
1528
0
  bed = get_elf_backend_data (obfd);
1529
1530
  /* Possibly copy other fields in the section header.  */
1531
0
  for (i = 1; i < elf_numsections (obfd); i++)
1532
0
    {
1533
0
      unsigned int j;
1534
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1535
1536
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1537
   because of a special case need for generating separate debug info
1538
   files.  See below for more details.  */
1539
0
      if (oheader == NULL
1540
0
    || (oheader->sh_type != SHT_NOBITS
1541
0
        && oheader->sh_type < SHT_LOOS))
1542
0
  continue;
1543
1544
      /* Ignore empty sections, and sections whose
1545
   fields have already been initialised.  */
1546
0
      if (oheader->sh_size == 0
1547
0
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1548
0
  continue;
1549
1550
      /* Scan for the matching section in the input bfd.
1551
   First we try for a direct mapping between the input and
1552
   output sections.  */
1553
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1554
0
  {
1555
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1556
1557
0
    if (iheader == NULL)
1558
0
      continue;
1559
1560
0
    if (oheader->bfd_section != NULL
1561
0
        && iheader->bfd_section != NULL
1562
0
        && iheader->bfd_section->output_section != NULL
1563
0
        && iheader->bfd_section->output_section == oheader->bfd_section)
1564
0
      {
1565
        /* We have found a connection from the input section to
1566
     the output section.  Attempt to copy the header fields.
1567
     If this fails then do not try any further sections -
1568
     there should only be a one-to-one mapping between
1569
     input and output.  */
1570
0
        if (!copy_special_section_fields (ibfd, obfd,
1571
0
            iheader, oheader, i))
1572
0
    j = elf_numsections (ibfd);
1573
0
        break;
1574
0
      }
1575
0
  }
1576
1577
0
      if (j < elf_numsections (ibfd))
1578
0
  continue;
1579
1580
      /* That failed.  So try to deduce the corresponding input section.
1581
   Unfortunately we cannot compare names as the output string table
1582
   is empty, so instead we check size, address and type.  */
1583
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1584
0
  {
1585
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1586
1587
0
    if (iheader == NULL)
1588
0
      continue;
1589
1590
    /* Try matching fields in the input section's header.
1591
       Since --only-keep-debug turns all non-debug sections into
1592
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1593
       input type.  */
1594
0
    if ((oheader->sh_type == SHT_NOBITS
1595
0
         || iheader->sh_type == oheader->sh_type)
1596
0
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1597
0
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1598
0
        && iheader->sh_addralign == oheader->sh_addralign
1599
0
        && iheader->sh_entsize == oheader->sh_entsize
1600
0
        && iheader->sh_size == oheader->sh_size
1601
0
        && iheader->sh_addr == oheader->sh_addr
1602
0
        && (iheader->sh_info != oheader->sh_info
1603
0
      || iheader->sh_link != oheader->sh_link))
1604
0
      {
1605
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1606
0
    break;
1607
0
      }
1608
0
  }
1609
1610
0
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1611
0
  {
1612
    /* Final attempt.  Call the backend copy function
1613
       with a NULL input section.  */
1614
0
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1615
0
                     NULL, oheader);
1616
0
  }
1617
0
    }
1618
1619
0
  return true;
1620
0
}
1621
1622
static const char *
1623
get_segment_type (unsigned int p_type)
1624
0
{
1625
0
  const char *pt;
1626
0
  switch (p_type)
1627
0
    {
1628
0
    case PT_NULL: pt = "NULL"; break;
1629
0
    case PT_LOAD: pt = "LOAD"; break;
1630
0
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1631
0
    case PT_INTERP: pt = "INTERP"; break;
1632
0
    case PT_NOTE: pt = "NOTE"; break;
1633
0
    case PT_SHLIB: pt = "SHLIB"; break;
1634
0
    case PT_PHDR: pt = "PHDR"; break;
1635
0
    case PT_TLS: pt = "TLS"; break;
1636
0
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1637
0
    case PT_GNU_STACK: pt = "STACK"; break;
1638
0
    case PT_GNU_RELRO: pt = "RELRO"; break;
1639
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1640
0
    default: pt = NULL; break;
1641
0
    }
1642
0
  return pt;
1643
0
}
1644
1645
/* Print out the program headers.  */
1646
1647
bool
1648
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1649
0
{
1650
0
  FILE *f = (FILE *) farg;
1651
0
  Elf_Internal_Phdr *p;
1652
0
  asection *s;
1653
0
  bfd_byte *dynbuf = NULL;
1654
1655
0
  p = elf_tdata (abfd)->phdr;
1656
0
  if (p != NULL)
1657
0
    {
1658
0
      unsigned int i, c;
1659
1660
0
      fprintf (f, _("\nProgram Header:\n"));
1661
0
      c = elf_elfheader (abfd)->e_phnum;
1662
0
      for (i = 0; i < c; i++, p++)
1663
0
  {
1664
0
    const char *pt = get_segment_type (p->p_type);
1665
0
    char buf[20];
1666
1667
0
    if (pt == NULL)
1668
0
      {
1669
0
        sprintf (buf, "0x%lx", p->p_type);
1670
0
        pt = buf;
1671
0
      }
1672
0
    fprintf (f, "%8s off    0x", pt);
1673
0
    bfd_fprintf_vma (abfd, f, p->p_offset);
1674
0
    fprintf (f, " vaddr 0x");
1675
0
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1676
0
    fprintf (f, " paddr 0x");
1677
0
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1678
0
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1679
0
    fprintf (f, "         filesz 0x");
1680
0
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1681
0
    fprintf (f, " memsz 0x");
1682
0
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1683
0
    fprintf (f, " flags %c%c%c",
1684
0
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1685
0
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1686
0
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1687
0
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1688
0
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1689
0
    fprintf (f, "\n");
1690
0
  }
1691
0
    }
1692
1693
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
1694
0
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1695
0
    {
1696
0
      unsigned int elfsec;
1697
0
      unsigned long shlink;
1698
0
      bfd_byte *extdyn, *extdynend;
1699
0
      size_t extdynsize;
1700
0
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1701
1702
0
      fprintf (f, _("\nDynamic Section:\n"));
1703
1704
0
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1705
0
  goto error_return;
1706
1707
0
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1708
0
      if (elfsec == SHN_BAD)
1709
0
  goto error_return;
1710
0
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1711
1712
0
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1713
0
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1714
1715
0
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1716
0
     (size_t) (extdynend - extdyn) >= extdynsize;
1717
0
     extdyn += extdynsize)
1718
0
  {
1719
0
    Elf_Internal_Dyn dyn;
1720
0
    const char *name = "";
1721
0
    char ab[20];
1722
0
    bool stringp;
1723
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1724
1725
0
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1726
1727
0
    if (dyn.d_tag == DT_NULL)
1728
0
      break;
1729
1730
0
    stringp = false;
1731
0
    switch (dyn.d_tag)
1732
0
      {
1733
0
      default:
1734
0
        if (bed->elf_backend_get_target_dtag)
1735
0
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1736
1737
0
        if (!strcmp (name, ""))
1738
0
    {
1739
0
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1740
0
      name = ab;
1741
0
    }
1742
0
        break;
1743
1744
0
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1745
0
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1746
0
      case DT_PLTGOT: name = "PLTGOT"; break;
1747
0
      case DT_HASH: name = "HASH"; break;
1748
0
      case DT_STRTAB: name = "STRTAB"; break;
1749
0
      case DT_SYMTAB: name = "SYMTAB"; break;
1750
0
      case DT_RELA: name = "RELA"; break;
1751
0
      case DT_RELASZ: name = "RELASZ"; break;
1752
0
      case DT_RELAENT: name = "RELAENT"; break;
1753
0
      case DT_STRSZ: name = "STRSZ"; break;
1754
0
      case DT_SYMENT: name = "SYMENT"; break;
1755
0
      case DT_INIT: name = "INIT"; break;
1756
0
      case DT_FINI: name = "FINI"; break;
1757
0
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1758
0
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1759
0
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1760
0
      case DT_REL: name = "REL"; break;
1761
0
      case DT_RELSZ: name = "RELSZ"; break;
1762
0
      case DT_RELENT: name = "RELENT"; break;
1763
0
      case DT_RELR: name = "RELR"; break;
1764
0
      case DT_RELRSZ: name = "RELRSZ"; break;
1765
0
      case DT_RELRENT: name = "RELRENT"; break;
1766
0
      case DT_PLTREL: name = "PLTREL"; break;
1767
0
      case DT_DEBUG: name = "DEBUG"; break;
1768
0
      case DT_TEXTREL: name = "TEXTREL"; break;
1769
0
      case DT_JMPREL: name = "JMPREL"; break;
1770
0
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1771
0
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1772
0
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1773
0
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1774
0
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1775
0
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1776
0
      case DT_FLAGS: name = "FLAGS"; break;
1777
0
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1778
0
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1779
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1780
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1781
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1782
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1783
0
      case DT_FEATURE: name = "FEATURE"; break;
1784
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1785
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1786
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1787
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1788
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1789
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1790
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1791
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1792
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1793
0
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1794
0
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1795
0
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1796
0
      case DT_VERSYM: name = "VERSYM"; break;
1797
0
      case DT_VERDEF: name = "VERDEF"; break;
1798
0
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1799
0
      case DT_VERNEED: name = "VERNEED"; break;
1800
0
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1801
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1802
0
      case DT_USED: name = "USED"; break;
1803
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1804
0
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1805
0
      }
1806
1807
0
    fprintf (f, "  %-20s ", name);
1808
0
    if (! stringp)
1809
0
      {
1810
0
        fprintf (f, "0x");
1811
0
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1812
0
      }
1813
0
    else
1814
0
      {
1815
0
        const char *string;
1816
0
        unsigned int tagv = dyn.d_un.d_val;
1817
1818
0
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1819
0
        if (string == NULL)
1820
0
    goto error_return;
1821
0
        fprintf (f, "%s", string);
1822
0
      }
1823
0
    fprintf (f, "\n");
1824
0
  }
1825
1826
0
      free (dynbuf);
1827
0
      dynbuf = NULL;
1828
0
    }
1829
1830
0
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1831
0
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1832
0
    {
1833
0
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1834
0
  return false;
1835
0
    }
1836
1837
0
  if (elf_dynverdef (abfd) != 0)
1838
0
    {
1839
0
      Elf_Internal_Verdef *t;
1840
1841
0
      fprintf (f, _("\nVersion definitions:\n"));
1842
0
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1843
0
  {
1844
0
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1845
0
       t->vd_flags, t->vd_hash,
1846
0
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1847
0
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1848
0
      {
1849
0
        Elf_Internal_Verdaux *a;
1850
1851
0
        fprintf (f, "\t");
1852
0
        for (a = t->vd_auxptr->vda_nextptr;
1853
0
       a != NULL;
1854
0
       a = a->vda_nextptr)
1855
0
    fprintf (f, "%s ",
1856
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1857
0
        fprintf (f, "\n");
1858
0
      }
1859
0
  }
1860
0
    }
1861
1862
0
  if (elf_dynverref (abfd) != 0)
1863
0
    {
1864
0
      Elf_Internal_Verneed *t;
1865
1866
0
      fprintf (f, _("\nVersion References:\n"));
1867
0
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1868
0
  {
1869
0
    Elf_Internal_Vernaux *a;
1870
1871
0
    fprintf (f, _("  required from %s:\n"),
1872
0
       t->vn_filename ? t->vn_filename : "<corrupt>");
1873
0
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1874
0
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1875
0
         a->vna_flags, a->vna_other,
1876
0
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1877
0
  }
1878
0
    }
1879
1880
0
  return true;
1881
1882
0
 error_return:
1883
0
  free (dynbuf);
1884
0
  return false;
1885
0
}
1886
1887
/* Find the file offset corresponding to VMA by using the program
1888
   headers.  */
1889
1890
static file_ptr
1891
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1892
     size_t size, size_t *max_size_p)
1893
0
{
1894
0
  Elf_Internal_Phdr *seg;
1895
0
  size_t i;
1896
1897
0
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1898
0
    if (seg->p_type == PT_LOAD
1899
0
  && vma >= (seg->p_vaddr & -seg->p_align)
1900
0
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1901
0
      {
1902
0
  if (max_size_p)
1903
0
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1904
0
  return vma - seg->p_vaddr + seg->p_offset;
1905
0
      }
1906
1907
0
  if (max_size_p)
1908
0
    *max_size_p = 0;
1909
0
  bfd_set_error (bfd_error_invalid_operation);
1910
0
  return (file_ptr) -1;
1911
0
}
1912
1913
/* Convert hash table to internal form.  */
1914
1915
static bfd_vma *
1916
get_hash_table_data (bfd *abfd, bfd_size_type number,
1917
         unsigned int ent_size, bfd_size_type filesize)
1918
0
{
1919
0
  unsigned char *e_data = NULL;
1920
0
  bfd_vma *i_data = NULL;
1921
0
  bfd_size_type size;
1922
1923
0
  if (ent_size != 4 && ent_size != 8)
1924
0
    return NULL;
1925
1926
0
  if ((size_t) number != number)
1927
0
    {
1928
0
      bfd_set_error (bfd_error_file_too_big);
1929
0
      return NULL;
1930
0
    }
1931
1932
0
  size = ent_size * number;
1933
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1934
     attempting to allocate memory when the read is bound to fail.  */
1935
0
  if (size > filesize
1936
0
      || number >= ~(size_t) 0 / ent_size
1937
0
      || number >= ~(size_t) 0 / sizeof (*i_data))
1938
0
    {
1939
0
      bfd_set_error (bfd_error_file_too_big);
1940
0
      return NULL;
1941
0
    }
1942
1943
0
  e_data = _bfd_malloc_and_read (abfd, size, size);
1944
0
  if (e_data == NULL)
1945
0
    return NULL;
1946
1947
0
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1948
0
  if (i_data == NULL)
1949
0
    {
1950
0
      free (e_data);
1951
0
      return NULL;
1952
0
    }
1953
1954
0
  if (ent_size == 4)
1955
0
    while (number--)
1956
0
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1957
0
  else
1958
0
    while (number--)
1959
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1960
1961
0
  free (e_data);
1962
0
  return i_data;
1963
0
}
1964
1965
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1966
   support DT_MIPS_XHASH?  */
1967
0
#define DT_MIPS_XHASH        0x70000036
1968
1969
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1970
1971
bool
1972
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1973
            Elf_Internal_Phdr *phdrs, size_t phnum,
1974
            bfd_size_type filesize)
1975
0
{
1976
0
  bfd_byte *extdyn, *extdynend;
1977
0
  size_t extdynsize;
1978
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1979
0
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1980
0
        Elf_Internal_Sym *);
1981
0
  Elf_Internal_Dyn dyn;
1982
0
  bfd_vma dt_hash = 0;
1983
0
  bfd_vma dt_gnu_hash = 0;
1984
0
  bfd_vma dt_mips_xhash = 0;
1985
0
  bfd_vma dt_strtab = 0;
1986
0
  bfd_vma dt_symtab = 0;
1987
0
  size_t dt_strsz = 0;
1988
0
  bfd_vma dt_versym = 0;
1989
0
  bfd_vma dt_verdef = 0;
1990
0
  bfd_vma dt_verneed = 0;
1991
0
  bfd_byte *dynbuf = NULL;
1992
0
  char *strbuf = NULL;
1993
0
  bfd_vma *gnubuckets = NULL;
1994
0
  bfd_vma *gnuchains = NULL;
1995
0
  bfd_vma *mipsxlat = NULL;
1996
0
  file_ptr saved_filepos, filepos;
1997
0
  bool res = false;
1998
0
  size_t amt;
1999
0
  bfd_byte *esymbuf = NULL, *esym;
2000
0
  bfd_size_type symcount;
2001
0
  Elf_Internal_Sym *isymbuf = NULL;
2002
0
  Elf_Internal_Sym *isym, *isymend;
2003
0
  bfd_byte *versym = NULL;
2004
0
  bfd_byte *verdef = NULL;
2005
0
  bfd_byte *verneed = NULL;
2006
0
  size_t verdef_size = 0;
2007
0
  size_t verneed_size = 0;
2008
0
  size_t extsym_size;
2009
0
  const struct elf_backend_data *bed;
2010
2011
  /* Return TRUE if symbol table is bad.  */
2012
0
  if (elf_bad_symtab (abfd))
2013
0
    return true;
2014
2015
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
2016
0
  if (elf_tdata (abfd)->dt_strtab != NULL)
2017
0
    return true;
2018
2019
0
  bed = get_elf_backend_data (abfd);
2020
2021
  /* Save file position for elf_object_p.  */
2022
0
  saved_filepos = bfd_tell (abfd);
2023
2024
0
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2025
0
    goto error_return;
2026
2027
0
  dynbuf = _bfd_malloc_and_read (abfd, phdr->p_filesz, phdr->p_filesz);
2028
0
  if (dynbuf == NULL)
2029
0
    goto error_return;
2030
2031
0
  extsym_size = bed->s->sizeof_sym;
2032
0
  extdynsize = bed->s->sizeof_dyn;
2033
0
  swap_dyn_in = bed->s->swap_dyn_in;
2034
2035
0
  extdyn = dynbuf;
2036
0
  if (phdr->p_filesz < extdynsize)
2037
0
    goto error_return;
2038
0
  extdynend = extdyn + phdr->p_filesz;
2039
0
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2040
0
    {
2041
0
      swap_dyn_in (abfd, extdyn, &dyn);
2042
2043
0
      if (dyn.d_tag == DT_NULL)
2044
0
  break;
2045
2046
0
      switch (dyn.d_tag)
2047
0
  {
2048
0
  case DT_HASH:
2049
0
    dt_hash = dyn.d_un.d_val;
2050
0
    break;
2051
0
  case DT_GNU_HASH:
2052
0
    if (bed->elf_machine_code != EM_MIPS
2053
0
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
2054
0
      dt_gnu_hash = dyn.d_un.d_val;
2055
0
    break;
2056
0
  case DT_STRTAB:
2057
0
    dt_strtab = dyn.d_un.d_val;
2058
0
    break;
2059
0
  case DT_SYMTAB:
2060
0
    dt_symtab = dyn.d_un.d_val;
2061
0
    break;
2062
0
  case DT_STRSZ:
2063
0
    dt_strsz = dyn.d_un.d_val;
2064
0
    break;
2065
0
  case DT_SYMENT:
2066
0
    if (dyn.d_un.d_val != extsym_size)
2067
0
      goto error_return;
2068
0
    break;
2069
0
  case DT_VERSYM:
2070
0
    dt_versym = dyn.d_un.d_val;
2071
0
    break;
2072
0
  case DT_VERDEF:
2073
0
    dt_verdef = dyn.d_un.d_val;
2074
0
    break;
2075
0
  case DT_VERNEED:
2076
0
    dt_verneed = dyn.d_un.d_val;
2077
0
    break;
2078
0
  default:
2079
0
    if (dyn.d_tag == DT_MIPS_XHASH
2080
0
        && (bed->elf_machine_code == EM_MIPS
2081
0
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
2082
0
      {
2083
0
        dt_gnu_hash = dyn.d_un.d_val;
2084
0
        dt_mips_xhash = dyn.d_un.d_val;
2085
0
      }
2086
0
    break;
2087
0
  }
2088
0
    }
2089
2090
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2091
     segment.  */
2092
0
  if ((!dt_hash && !dt_gnu_hash)
2093
0
      || !dt_strtab
2094
0
      || !dt_symtab
2095
0
      || !dt_strsz)
2096
0
    goto error_return;
2097
2098
  /* Get dynamic string table.  */
2099
0
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2100
0
  if (filepos == (file_ptr) -1
2101
0
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2102
0
    goto error_return;
2103
2104
  /* Dynamic string table must be valid until ABFD is closed.  */
2105
0
  strbuf = (char *) _bfd_alloc_and_read (abfd, dt_strsz, dt_strsz);
2106
0
  if (strbuf == NULL)
2107
0
    goto error_return;
2108
2109
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2110
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2111
0
  if (dt_hash)
2112
0
    {
2113
0
      unsigned char nb[16];
2114
0
      unsigned int hash_ent_size;
2115
2116
0
      switch (bed->elf_machine_code)
2117
0
  {
2118
0
  case EM_ALPHA:
2119
0
  case EM_S390:
2120
0
  case EM_S390_OLD:
2121
0
    if (bed->s->elfclass == ELFCLASS64)
2122
0
      {
2123
0
        hash_ent_size = 8;
2124
0
        break;
2125
0
      }
2126
    /* FALLTHROUGH */
2127
0
  default:
2128
0
    hash_ent_size = 4;
2129
0
    break;
2130
0
  }
2131
2132
0
      filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2133
0
         NULL);
2134
0
      if (filepos == (file_ptr) -1
2135
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2136
0
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2137
0
  goto error_return;
2138
2139
      /* The number of dynamic symbol table entries equals the number
2140
   of chains.  */
2141
0
      if (hash_ent_size == 8)
2142
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2143
0
      else
2144
0
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2145
0
    }
2146
0
  else
2147
0
    {
2148
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2149
   bindings are in hash table.  Since in dynamic symbol table,
2150
   all symbols with STB_LOCAL binding are placed before symbols
2151
   with other bindings and all undefined symbols are placed
2152
   before defined ones, the highest symbol index in DT_GNU_HASH
2153
   is the highest dynamic symbol table index.  */
2154
0
      unsigned char nb[16];
2155
0
      bfd_vma ngnubuckets;
2156
0
      bfd_vma gnusymidx;
2157
0
      size_t i, ngnuchains;
2158
0
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2159
0
      bfd_vma buckets_vma;
2160
2161
0
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2162
0
         sizeof (nb), NULL);
2163
0
      if (filepos == (file_ptr) -1
2164
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2165
0
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2166
0
  goto error_return;
2167
2168
0
      ngnubuckets = bfd_get_32 (abfd, nb);
2169
0
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2170
0
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2171
0
      buckets_vma = dt_gnu_hash + 16;
2172
0
      if (bed->s->elfclass == ELFCLASS32)
2173
0
  buckets_vma += bitmaskwords * 4;
2174
0
      else
2175
0
  buckets_vma += bitmaskwords * 8;
2176
0
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2177
0
      if (filepos == (file_ptr) -1
2178
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2179
0
  goto error_return;
2180
2181
0
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2182
0
      if (gnubuckets == NULL)
2183
0
  goto error_return;
2184
2185
0
      for (i = 0; i < ngnubuckets; i++)
2186
0
  if (gnubuckets[i] != 0)
2187
0
    {
2188
0
      if (gnubuckets[i] < gnusymidx)
2189
0
        goto error_return;
2190
2191
0
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2192
0
        maxchain = gnubuckets[i];
2193
0
    }
2194
2195
0
      if (maxchain == 0xffffffff)
2196
0
  {
2197
0
    symcount = 0;
2198
0
    goto empty_gnu_hash;
2199
0
  }
2200
2201
0
      maxchain -= gnusymidx;
2202
0
      filepos = offset_from_vma (phdrs, phnum,
2203
0
         (buckets_vma +
2204
0
          4 * (ngnubuckets + maxchain)),
2205
0
         4, NULL);
2206
0
      if (filepos == (file_ptr) -1
2207
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2208
0
  goto error_return;
2209
2210
0
      do
2211
0
  {
2212
0
    if (bfd_read (nb, 4, abfd) != 4)
2213
0
      goto error_return;
2214
0
    ++maxchain;
2215
0
    if (maxchain == 0)
2216
0
      goto error_return;
2217
0
  }
2218
0
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2219
2220
0
      filepos = offset_from_vma (phdrs, phnum,
2221
0
         (buckets_vma + 4 * ngnubuckets),
2222
0
         4, NULL);
2223
0
      if (filepos == (file_ptr) -1
2224
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2225
0
  goto error_return;
2226
2227
0
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2228
0
      if (gnubuckets == NULL)
2229
0
  goto error_return;
2230
0
      ngnuchains = maxchain;
2231
2232
0
      if (dt_mips_xhash)
2233
0
  {
2234
0
    filepos = offset_from_vma (phdrs, phnum,
2235
0
             (buckets_vma
2236
0
              + 4 * (ngnubuckets + maxchain)),
2237
0
             4, NULL);
2238
0
    if (filepos == (file_ptr) -1
2239
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2240
0
      goto error_return;
2241
2242
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2243
0
    if (mipsxlat == NULL)
2244
0
      goto error_return;
2245
0
  }
2246
2247
0
      symcount = 0;
2248
0
      for (i = 0; i < ngnubuckets; ++i)
2249
0
  if (gnubuckets[i] != 0)
2250
0
    {
2251
0
      bfd_vma si = gnubuckets[i];
2252
0
      bfd_vma off = si - gnusymidx;
2253
0
      do
2254
0
        {
2255
0
    if (mipsxlat)
2256
0
      {
2257
0
        if (mipsxlat[off] >= symcount)
2258
0
          symcount = mipsxlat[off] + 1;
2259
0
      }
2260
0
    else
2261
0
      {
2262
0
        if (si >= symcount)
2263
0
          symcount = si + 1;
2264
0
      }
2265
0
    si++;
2266
0
        }
2267
0
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2268
0
    }
2269
0
    }
2270
2271
  /* Swap in dynamic symbol table.  */
2272
0
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2273
0
    {
2274
0
      bfd_set_error (bfd_error_file_too_big);
2275
0
      goto error_return;
2276
0
    }
2277
2278
0
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2279
0
  if (filepos == (file_ptr) -1
2280
0
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2281
0
    goto error_return;
2282
0
  esymbuf = _bfd_malloc_and_read (abfd, amt, amt);
2283
0
  if (esymbuf == NULL)
2284
0
    goto error_return;
2285
2286
0
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2287
0
    {
2288
0
      bfd_set_error (bfd_error_file_too_big);
2289
0
      goto error_return;
2290
0
    }
2291
2292
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2293
0
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2294
0
  if (isymbuf == NULL)
2295
0
    goto error_return;
2296
2297
0
  swap_symbol_in = bed->s->swap_symbol_in;
2298
2299
  /* Convert the symbols to internal form.  */
2300
0
  isymend = isymbuf + symcount;
2301
0
  for (esym = esymbuf, isym = isymbuf;
2302
0
       isym < isymend;
2303
0
       esym += extsym_size, isym++)
2304
0
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2305
0
  || isym->st_name >= dt_strsz)
2306
0
      {
2307
0
  bfd_set_error (bfd_error_invalid_operation);
2308
0
  goto error_return;
2309
0
      }
2310
2311
0
  if (dt_versym)
2312
0
    {
2313
      /* Swap in DT_VERSYM.  */
2314
0
      if (_bfd_mul_overflow (symcount, 2, &amt))
2315
0
  {
2316
0
    bfd_set_error (bfd_error_file_too_big);
2317
0
    goto error_return;
2318
0
  }
2319
2320
0
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2321
0
      if (filepos == (file_ptr) -1
2322
0
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2323
0
  goto error_return;
2324
2325
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2326
0
      versym = _bfd_alloc_and_read (abfd, amt, amt);
2327
2328
0
      if (dt_verdef)
2329
0
  {
2330
    /* Read in DT_VERDEF.  */
2331
0
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2332
0
             0, &verdef_size);
2333
0
    if (filepos == (file_ptr) -1
2334
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2335
0
      goto error_return;
2336
2337
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2338
0
    verdef = _bfd_alloc_and_read (abfd, verdef_size,
2339
0
          verdef_size);
2340
0
  }
2341
2342
0
      if (dt_verneed)
2343
0
  {
2344
    /* Read in DT_VERNEED.  */
2345
0
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2346
0
             0, &verneed_size);
2347
0
    if (filepos == (file_ptr) -1
2348
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2349
0
      goto error_return;
2350
2351
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2352
0
    verneed = _bfd_alloc_and_read (abfd, verneed_size,
2353
0
           verneed_size);
2354
0
  }
2355
0
    }
2356
2357
0
 empty_gnu_hash:
2358
0
  elf_tdata (abfd)->dt_strtab = strbuf;
2359
0
  elf_tdata (abfd)->dt_symtab = isymbuf;
2360
0
  elf_tdata (abfd)->dt_symtab_count = symcount;
2361
0
  elf_tdata (abfd)->dt_versym = versym;
2362
0
  elf_tdata (abfd)->dt_verdef = verdef;
2363
0
  elf_tdata (abfd)->dt_verneed = verneed;
2364
0
  elf_tdata (abfd)->dt_verdef_count
2365
0
    = verdef_size / sizeof (Elf_External_Verdef);
2366
0
  elf_tdata (abfd)->dt_verneed_count
2367
0
    = verneed_size / sizeof (Elf_External_Verneed);
2368
2369
0
  res = true;
2370
2371
0
 error_return:
2372
  /* Restore file position for elf_object_p.  */
2373
0
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2374
0
    res = false;
2375
0
  free (dynbuf);
2376
0
  free (esymbuf);
2377
0
  free (gnubuckets);
2378
0
  free (gnuchains);
2379
0
  free (mipsxlat);
2380
0
  return res;
2381
0
}
2382
2383
/* Reconstruct section from dynamic symbol.  */
2384
2385
asection *
2386
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2387
            Elf_Internal_Sym *isym)
2388
0
{
2389
0
  asection *sec;
2390
0
  flagword flags;
2391
2392
0
  if (!elf_use_dt_symtab_p (abfd))
2393
0
    return NULL;
2394
2395
0
  flags = SEC_ALLOC | SEC_LOAD;
2396
0
  switch (ELF_ST_TYPE (isym->st_info))
2397
0
    {
2398
0
    case STT_FUNC:
2399
0
    case STT_GNU_IFUNC:
2400
0
      sec = bfd_get_section_by_name (abfd, ".text");
2401
0
      if (sec == NULL)
2402
0
  sec = bfd_make_section_with_flags (abfd,
2403
0
             ".text",
2404
0
             flags | SEC_CODE);
2405
0
      break;
2406
0
    case STT_COMMON:
2407
0
      sec = bfd_com_section_ptr;
2408
0
      break;
2409
0
    case STT_OBJECT:
2410
0
      sec = bfd_get_section_by_name (abfd, ".data");
2411
0
      if (sec == NULL)
2412
0
  sec = bfd_make_section_with_flags (abfd,
2413
0
             ".data",
2414
0
             flags | SEC_DATA);
2415
0
      break;
2416
0
    case STT_TLS:
2417
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2418
0
      if (sec == NULL)
2419
0
  sec = bfd_make_section_with_flags (abfd,
2420
0
             ".tdata",
2421
0
             (flags
2422
0
              | SEC_DATA
2423
0
              | SEC_THREAD_LOCAL));
2424
0
      break;
2425
0
    default:
2426
0
      sec = bfd_abs_section_ptr;
2427
0
      break;
2428
0
    }
2429
2430
0
  return sec;
2431
0
}
2432
2433
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2434
   and return symbol version for symbol version itself.   */
2435
2436
const char *
2437
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2438
            bool base_p,
2439
            bool *hidden)
2440
0
{
2441
0
  const char *version_string = NULL;
2442
0
  if ((elf_dynversym (abfd) != 0
2443
0
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2444
0
      || (elf_tdata (abfd)->dt_versym != NULL
2445
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2446
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2447
0
    {
2448
0
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2449
2450
0
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2451
0
      vernum &= VERSYM_VERSION;
2452
2453
0
      if (vernum == 0)
2454
0
  version_string = "";
2455
0
      else if (vernum == 1
2456
0
         && (vernum > elf_tdata (abfd)->cverdefs
2457
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2458
0
           == VER_FLG_BASE)))
2459
0
  version_string = base_p ? "Base" : "";
2460
0
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2461
0
  {
2462
0
    const char *nodename
2463
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2464
0
    version_string = "";
2465
0
    if (base_p
2466
0
        || nodename == NULL
2467
0
        || symbol->name == NULL
2468
0
        || strcmp (symbol->name, nodename) != 0)
2469
0
      version_string = nodename;
2470
0
  }
2471
0
      else
2472
0
  {
2473
0
    Elf_Internal_Verneed *t;
2474
2475
0
    version_string = _("<corrupt>");
2476
0
    for (t = elf_tdata (abfd)->verref;
2477
0
         t != NULL;
2478
0
         t = t->vn_nextref)
2479
0
      {
2480
0
        Elf_Internal_Vernaux *a;
2481
2482
0
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2483
0
    {
2484
0
      if (a->vna_other == vernum)
2485
0
        {
2486
0
          *hidden = true;
2487
0
          version_string = a->vna_nodename;
2488
0
          break;
2489
0
        }
2490
0
    }
2491
0
      }
2492
0
  }
2493
0
    }
2494
0
  return version_string;
2495
0
}
2496
2497
/* Display ELF-specific fields of a symbol.  */
2498
2499
void
2500
bfd_elf_print_symbol (bfd *abfd,
2501
          void *filep,
2502
          asymbol *symbol,
2503
          bfd_print_symbol_type how)
2504
0
{
2505
0
  FILE *file = (FILE *) filep;
2506
0
  switch (how)
2507
0
    {
2508
0
    case bfd_print_symbol_name:
2509
0
      fprintf (file, "%s", symbol->name);
2510
0
      break;
2511
0
    case bfd_print_symbol_more:
2512
0
      fprintf (file, "elf ");
2513
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2514
0
      fprintf (file, " %x", symbol->flags);
2515
0
      break;
2516
0
    case bfd_print_symbol_all:
2517
0
      {
2518
0
  const char *section_name;
2519
0
  const char *name = NULL;
2520
0
  const struct elf_backend_data *bed;
2521
0
  unsigned char st_other;
2522
0
  bfd_vma val;
2523
0
  const char *version_string;
2524
0
  bool hidden;
2525
2526
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2527
2528
0
  bed = get_elf_backend_data (abfd);
2529
0
  if (bed->elf_backend_print_symbol_all)
2530
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2531
2532
0
  if (name == NULL)
2533
0
    {
2534
0
      name = symbol->name;
2535
0
      bfd_print_symbol_vandf (abfd, file, symbol);
2536
0
    }
2537
2538
0
  fprintf (file, " %s\t", section_name);
2539
  /* Print the "other" value for a symbol.  For common symbols,
2540
     we've already printed the size; now print the alignment.
2541
     For other symbols, we have no specified alignment, and
2542
     we've printed the address; now print the size.  */
2543
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2544
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2545
0
  else
2546
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2547
0
  bfd_fprintf_vma (abfd, file, val);
2548
2549
  /* If we have version information, print it.  */
2550
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2551
0
                   symbol,
2552
0
                   true,
2553
0
                   &hidden);
2554
0
  if (version_string)
2555
0
    {
2556
0
      if (!hidden)
2557
0
        fprintf (file, "  %-11s", version_string);
2558
0
      else
2559
0
        {
2560
0
    int i;
2561
2562
0
    fprintf (file, " (%s)", version_string);
2563
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2564
0
      putc (' ', file);
2565
0
        }
2566
0
    }
2567
2568
  /* If the st_other field is not zero, print it.  */
2569
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2570
2571
0
  switch (st_other)
2572
0
    {
2573
0
    case 0: break;
2574
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2575
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2576
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2577
0
    default:
2578
      /* Some other non-defined flags are also present, so print
2579
         everything hex.  */
2580
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2581
0
    }
2582
2583
0
  fprintf (file, " %s", name);
2584
0
      }
2585
0
      break;
2586
0
    }
2587
0
}
2588

2589
/* ELF .o/exec file reading */
2590
2591
/* Create a new bfd section from an ELF section header.  */
2592
2593
bool
2594
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2595
0
{
2596
0
  Elf_Internal_Shdr *hdr;
2597
0
  Elf_Internal_Ehdr *ehdr;
2598
0
  const struct elf_backend_data *bed;
2599
0
  const char *name;
2600
0
  bool ret = true;
2601
2602
0
  if (shindex >= elf_numsections (abfd))
2603
0
    return false;
2604
2605
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2606
     sh_link or sh_info.  Detect this here, by refusing to load a
2607
     section that we are already in the process of loading.  */
2608
0
  if (elf_tdata (abfd)->being_created[shindex])
2609
0
    {
2610
0
      _bfd_error_handler
2611
0
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2612
0
      return false;
2613
0
    }
2614
0
  elf_tdata (abfd)->being_created[shindex] = true;
2615
2616
0
  hdr = elf_elfsections (abfd)[shindex];
2617
0
  ehdr = elf_elfheader (abfd);
2618
0
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2619
0
            hdr->sh_name);
2620
0
  if (name == NULL)
2621
0
    goto fail;
2622
2623
0
  bed = get_elf_backend_data (abfd);
2624
0
  switch (hdr->sh_type)
2625
0
    {
2626
0
    case SHT_NULL:
2627
      /* Inactive section. Throw it away.  */
2628
0
      goto success;
2629
2630
0
    case SHT_PROGBITS:   /* Normal section with contents.  */
2631
0
    case SHT_NOBITS:   /* .bss section.  */
2632
0
    case SHT_HASH:   /* .hash section.  */
2633
0
    case SHT_NOTE:   /* .note section.  */
2634
0
    case SHT_INIT_ARRAY: /* .init_array section.  */
2635
0
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2636
0
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2637
0
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2638
0
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2639
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2640
0
      goto success;
2641
2642
0
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2643
0
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2644
0
  goto fail;
2645
2646
0
      if (hdr->sh_link > elf_numsections (abfd))
2647
0
  {
2648
    /* PR 10478: Accept Solaris binaries with a sh_link field
2649
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2650
0
    switch (bfd_get_arch (abfd))
2651
0
      {
2652
0
      case bfd_arch_i386:
2653
0
      case bfd_arch_sparc:
2654
0
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2655
0
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2656
0
    break;
2657
        /* Otherwise fall through.  */
2658
0
      default:
2659
0
        goto fail;
2660
0
      }
2661
0
  }
2662
0
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2663
0
  goto fail;
2664
0
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2665
0
  {
2666
0
    Elf_Internal_Shdr *dynsymhdr;
2667
2668
    /* The shared libraries distributed with hpux11 have a bogus
2669
       sh_link field for the ".dynamic" section.  Find the
2670
       string table for the ".dynsym" section instead.  */
2671
0
    if (elf_dynsymtab (abfd) != 0)
2672
0
      {
2673
0
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2674
0
        hdr->sh_link = dynsymhdr->sh_link;
2675
0
      }
2676
0
    else
2677
0
      {
2678
0
        unsigned int i, num_sec;
2679
2680
0
        num_sec = elf_numsections (abfd);
2681
0
        for (i = 1; i < num_sec; i++)
2682
0
    {
2683
0
      dynsymhdr = elf_elfsections (abfd)[i];
2684
0
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2685
0
        {
2686
0
          hdr->sh_link = dynsymhdr->sh_link;
2687
0
          break;
2688
0
        }
2689
0
    }
2690
0
      }
2691
0
  }
2692
0
      goto success;
2693
2694
0
    case SHT_SYMTAB:   /* A symbol table.  */
2695
0
      if (elf_onesymtab (abfd) == shindex)
2696
0
  goto success;
2697
2698
0
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2699
0
  goto fail;
2700
2701
0
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2702
0
  {
2703
0
    if (hdr->sh_size != 0)
2704
0
      goto fail;
2705
    /* Some assemblers erroneously set sh_info to one with a
2706
       zero sh_size.  ld sees this as a global symbol count
2707
       of (unsigned) -1.  Fix it here.  */
2708
0
    hdr->sh_info = 0;
2709
0
    goto success;
2710
0
  }
2711
2712
      /* PR 18854: A binary might contain more than one symbol table.
2713
   Unusual, but possible.  Warn, but continue.  */
2714
0
      if (elf_onesymtab (abfd) != 0)
2715
0
  {
2716
0
    _bfd_error_handler
2717
      /* xgettext:c-format */
2718
0
      (_("%pB: warning: multiple symbol tables detected"
2719
0
         " - ignoring the table in section %u"),
2720
0
       abfd, shindex);
2721
0
    goto success;
2722
0
  }
2723
0
      elf_onesymtab (abfd) = shindex;
2724
0
      elf_symtab_hdr (abfd) = *hdr;
2725
0
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2726
0
      abfd->flags |= HAS_SYMS;
2727
2728
      /* Sometimes a shared object will map in the symbol table.  If
2729
   SHF_ALLOC is set, and this is a shared object, then we also
2730
   treat this section as a BFD section.  We can not base the
2731
   decision purely on SHF_ALLOC, because that flag is sometimes
2732
   set in a relocatable object file, which would confuse the
2733
   linker.  */
2734
0
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2735
0
    && (abfd->flags & DYNAMIC) != 0
2736
0
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2737
0
            shindex))
2738
0
  goto fail;
2739
2740
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2741
   can't read symbols without that section loaded as well.  It
2742
   is most likely specified by the next section header.  */
2743
0
      {
2744
0
  elf_section_list * entry;
2745
0
  unsigned int i, num_sec;
2746
2747
0
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2748
0
    if (entry->hdr.sh_link == shindex)
2749
0
      goto success;
2750
2751
0
  num_sec = elf_numsections (abfd);
2752
0
  for (i = shindex + 1; i < num_sec; i++)
2753
0
    {
2754
0
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2755
2756
0
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2757
0
    && hdr2->sh_link == shindex)
2758
0
        break;
2759
0
    }
2760
2761
0
  if (i == num_sec)
2762
0
    for (i = 1; i < shindex; i++)
2763
0
      {
2764
0
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2765
2766
0
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2767
0
      && hdr2->sh_link == shindex)
2768
0
    break;
2769
0
      }
2770
2771
0
  if (i != shindex)
2772
0
    ret = bfd_section_from_shdr (abfd, i);
2773
  /* else FIXME: we have failed to find the symbol table.
2774
     Should we issue an error?  */
2775
0
  goto success;
2776
0
      }
2777
2778
0
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2779
0
      if (elf_dynsymtab (abfd) == shindex)
2780
0
  goto success;
2781
2782
0
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2783
0
  goto fail;
2784
2785
0
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2786
0
  {
2787
0
    if (hdr->sh_size != 0)
2788
0
      goto fail;
2789
2790
    /* Some linkers erroneously set sh_info to one with a
2791
       zero sh_size.  ld sees this as a global symbol count
2792
       of (unsigned) -1.  Fix it here.  */
2793
0
    hdr->sh_info = 0;
2794
0
    goto success;
2795
0
  }
2796
2797
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2798
   Unusual, but possible.  Warn, but continue.  */
2799
0
      if (elf_dynsymtab (abfd) != 0)
2800
0
  {
2801
0
    _bfd_error_handler
2802
      /* xgettext:c-format */
2803
0
      (_("%pB: warning: multiple dynamic symbol tables detected"
2804
0
         " - ignoring the table in section %u"),
2805
0
       abfd, shindex);
2806
0
    goto success;
2807
0
  }
2808
0
      elf_dynsymtab (abfd) = shindex;
2809
0
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2810
0
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2811
0
      abfd->flags |= HAS_SYMS;
2812
2813
      /* Besides being a symbol table, we also treat this as a regular
2814
   section, so that objcopy can handle it.  */
2815
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2816
0
      goto success;
2817
2818
0
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2819
0
      {
2820
0
  elf_section_list * entry;
2821
2822
0
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2823
0
    if (entry->ndx == shindex)
2824
0
      goto success;
2825
2826
0
  entry = bfd_alloc (abfd, sizeof (*entry));
2827
0
  if (entry == NULL)
2828
0
    goto fail;
2829
0
  entry->ndx = shindex;
2830
0
  entry->hdr = * hdr;
2831
0
  entry->next = elf_symtab_shndx_list (abfd);
2832
0
  elf_symtab_shndx_list (abfd) = entry;
2833
0
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2834
0
  goto success;
2835
0
      }
2836
2837
0
    case SHT_STRTAB:   /* A string table.  */
2838
0
      if (hdr->bfd_section != NULL)
2839
0
  goto success;
2840
2841
0
      if (ehdr->e_shstrndx == shindex)
2842
0
  {
2843
0
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2844
0
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2845
0
    goto success;
2846
0
  }
2847
2848
0
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2849
0
  {
2850
0
  symtab_strtab:
2851
0
    elf_tdata (abfd)->strtab_hdr = *hdr;
2852
0
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2853
0
    goto success;
2854
0
  }
2855
2856
0
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2857
0
  {
2858
0
  dynsymtab_strtab:
2859
0
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2860
0
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2861
0
    elf_elfsections (abfd)[shindex] = hdr;
2862
    /* We also treat this as a regular section, so that objcopy
2863
       can handle it.  */
2864
0
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2865
0
             shindex);
2866
0
    goto success;
2867
0
  }
2868
2869
      /* If the string table isn't one of the above, then treat it as a
2870
   regular section.  We need to scan all the headers to be sure,
2871
   just in case this strtab section appeared before the above.  */
2872
0
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2873
0
  {
2874
0
    unsigned int i, num_sec;
2875
2876
0
    num_sec = elf_numsections (abfd);
2877
0
    for (i = 1; i < num_sec; i++)
2878
0
      {
2879
0
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2880
0
        if (hdr2->sh_link == shindex)
2881
0
    {
2882
      /* Prevent endless recursion on broken objects.  */
2883
0
      if (i == shindex)
2884
0
        goto fail;
2885
0
      if (! bfd_section_from_shdr (abfd, i))
2886
0
        goto fail;
2887
0
      if (elf_onesymtab (abfd) == i)
2888
0
        goto symtab_strtab;
2889
0
      if (elf_dynsymtab (abfd) == i)
2890
0
        goto dynsymtab_strtab;
2891
0
    }
2892
0
      }
2893
0
  }
2894
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2895
0
      goto success;
2896
2897
0
    case SHT_REL:
2898
0
    case SHT_RELA:
2899
0
    case SHT_RELR:
2900
      /* *These* do a lot of work -- but build no sections!  */
2901
0
      {
2902
0
  asection *target_sect;
2903
0
  Elf_Internal_Shdr *hdr2, **p_hdr;
2904
0
  unsigned int num_sec = elf_numsections (abfd);
2905
0
  struct bfd_elf_section_data *esdt;
2906
0
  bfd_size_type size;
2907
2908
0
  if (hdr->sh_type == SHT_REL)
2909
0
    size = bed->s->sizeof_rel;
2910
0
  else if (hdr->sh_type == SHT_RELA)
2911
0
    size = bed->s->sizeof_rela;
2912
0
  else
2913
0
    size = bed->s->arch_size / 8;
2914
0
  if (hdr->sh_entsize != size)
2915
0
    goto fail;
2916
2917
  /* Check for a bogus link to avoid crashing.  */
2918
0
  if (hdr->sh_link >= num_sec)
2919
0
    {
2920
0
      _bfd_error_handler
2921
        /* xgettext:c-format */
2922
0
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2923
0
         abfd, hdr->sh_link, name, shindex);
2924
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2925
0
      goto success;
2926
0
    }
2927
2928
  /* Get the symbol table.  */
2929
0
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2930
0
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2931
0
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2932
0
    goto fail;
2933
2934
  /* If this is an alloc section in an executable or shared
2935
     library, or the reloc section does not use the main symbol
2936
     table we don't treat it as a reloc section.  BFD can't
2937
     adequately represent such a section, so at least for now,
2938
     we don't try.  We just present it as a normal section.  We
2939
     also can't use it as a reloc section if it points to the
2940
     null section, an invalid section, another reloc section, or
2941
     its sh_link points to the null section.  */
2942
0
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2943
0
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2944
0
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2945
0
      || hdr->sh_type == SHT_RELR
2946
0
      || hdr->sh_link == SHN_UNDEF
2947
0
      || hdr->sh_link != elf_onesymtab (abfd)
2948
0
      || hdr->sh_info == SHN_UNDEF
2949
0
      || hdr->sh_info >= num_sec
2950
0
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2951
0
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2952
0
    {
2953
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2954
0
      goto success;
2955
0
    }
2956
2957
0
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2958
0
    goto fail;
2959
2960
0
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2961
0
  if (target_sect == NULL)
2962
0
    goto fail;
2963
2964
0
  esdt = elf_section_data (target_sect);
2965
0
  if (hdr->sh_type == SHT_RELA)
2966
0
    p_hdr = &esdt->rela.hdr;
2967
0
  else
2968
0
    p_hdr = &esdt->rel.hdr;
2969
2970
  /* PR 17512: file: 0b4f81b7.
2971
     Also see PR 24456, for a file which deliberately has two reloc
2972
     sections.  */
2973
0
  if (*p_hdr != NULL)
2974
0
    {
2975
0
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2976
0
        {
2977
0
    _bfd_error_handler
2978
      /* xgettext:c-format */
2979
0
      (_("%pB: warning: secondary relocation section '%s' "
2980
0
         "for section %pA found - ignoring"),
2981
0
       abfd, name, target_sect);
2982
0
        }
2983
0
      else
2984
0
        esdt->has_secondary_relocs = true;
2985
0
      goto success;
2986
0
    }
2987
2988
0
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2989
0
  if (hdr2 == NULL)
2990
0
    goto fail;
2991
0
  *hdr2 = *hdr;
2992
0
  *p_hdr = hdr2;
2993
0
  elf_elfsections (abfd)[shindex] = hdr2;
2994
0
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2995
0
             * bed->s->int_rels_per_ext_rel);
2996
0
  target_sect->flags |= SEC_RELOC;
2997
0
  target_sect->relocation = NULL;
2998
0
  target_sect->rel_filepos = hdr->sh_offset;
2999
  /* In the section to which the relocations apply, mark whether
3000
     its relocations are of the REL or RELA variety.  */
3001
0
  if (hdr->sh_size != 0)
3002
0
    {
3003
0
      if (hdr->sh_type == SHT_RELA)
3004
0
        target_sect->use_rela_p = 1;
3005
0
    }
3006
0
  abfd->flags |= HAS_RELOC;
3007
0
  goto success;
3008
0
      }
3009
3010
0
    case SHT_GNU_verdef:
3011
0
      if (hdr->sh_info != 0)
3012
0
  elf_dynverdef (abfd) = shindex;
3013
0
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
3014
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3015
0
      goto success;
3016
3017
0
    case SHT_GNU_versym:
3018
0
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3019
0
  goto fail;
3020
3021
0
      elf_dynversym (abfd) = shindex;
3022
0
      elf_tdata (abfd)->dynversym_hdr = *hdr;
3023
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3024
0
      goto success;
3025
3026
0
    case SHT_GNU_verneed:
3027
0
      if (hdr->sh_info != 0)
3028
0
  elf_dynverref (abfd) = shindex;
3029
0
      elf_tdata (abfd)->dynverref_hdr = *hdr;
3030
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3031
0
      goto success;
3032
3033
0
    case SHT_SHLIB:
3034
0
      goto success;
3035
3036
0
    case SHT_GROUP:
3037
0
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3038
0
  goto fail;
3039
3040
0
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3041
0
  goto fail;
3042
3043
0
      goto success;
3044
3045
0
    default:
3046
      /* Possibly an attributes section.  */
3047
0
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3048
0
    || hdr->sh_type == bed->obj_attrs_section_type)
3049
0
  {
3050
0
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3051
0
      goto fail;
3052
0
    _bfd_elf_parse_attributes (abfd, hdr);
3053
0
    goto success;
3054
0
  }
3055
3056
      /* Check for any processor-specific section types.  */
3057
0
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3058
0
  goto success;
3059
3060
0
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3061
0
  {
3062
0
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
3063
      /* FIXME: How to properly handle allocated section reserved
3064
         for applications?  */
3065
0
      _bfd_error_handler
3066
        /* xgettext:c-format */
3067
0
        (_("%pB: unknown type [%#x] section `%s'"),
3068
0
         abfd, hdr->sh_type, name);
3069
0
    else
3070
0
      {
3071
        /* Allow sections reserved for applications.  */
3072
0
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3073
0
        goto success;
3074
0
      }
3075
0
  }
3076
0
      else if (hdr->sh_type >= SHT_LOPROC
3077
0
         && hdr->sh_type <= SHT_HIPROC)
3078
  /* FIXME: We should handle this section.  */
3079
0
  _bfd_error_handler
3080
    /* xgettext:c-format */
3081
0
    (_("%pB: unknown type [%#x] section `%s'"),
3082
0
     abfd, hdr->sh_type, name);
3083
0
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3084
0
  {
3085
    /* Unrecognised OS-specific sections.  */
3086
0
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3087
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
3088
         required to correctly process the section and the file should
3089
         be rejected with an error message.  */
3090
0
      _bfd_error_handler
3091
        /* xgettext:c-format */
3092
0
        (_("%pB: unknown type [%#x] section `%s'"),
3093
0
         abfd, hdr->sh_type, name);
3094
0
    else
3095
0
      {
3096
        /* Otherwise it should be processed.  */
3097
0
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3098
0
        goto success;
3099
0
      }
3100
0
  }
3101
0
      else
3102
  /* FIXME: We should handle this section.  */
3103
0
  _bfd_error_handler
3104
    /* xgettext:c-format */
3105
0
    (_("%pB: unknown type [%#x] section `%s'"),
3106
0
     abfd, hdr->sh_type, name);
3107
3108
0
      goto fail;
3109
0
    }
3110
3111
0
 fail:
3112
0
  ret = false;
3113
0
 success:
3114
0
  elf_tdata (abfd)->being_created[shindex] = false;
3115
0
  return ret;
3116
0
}
3117
3118
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3119
3120
Elf_Internal_Sym *
3121
bfd_sym_from_r_symndx (struct sym_cache *cache,
3122
           bfd *abfd,
3123
           unsigned long r_symndx)
3124
0
{
3125
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3126
3127
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3128
0
    {
3129
0
      Elf_Internal_Shdr *symtab_hdr;
3130
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3131
0
      Elf_External_Sym_Shndx eshndx;
3132
3133
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3134
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3135
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3136
0
  return NULL;
3137
3138
0
      if (cache->abfd != abfd)
3139
0
  {
3140
0
    memset (cache->indx, -1, sizeof (cache->indx));
3141
0
    cache->abfd = abfd;
3142
0
  }
3143
0
      cache->indx[ent] = r_symndx;
3144
0
    }
3145
3146
0
  return &cache->sym[ent];
3147
0
}
3148
3149
/* Given an ELF section number, retrieve the corresponding BFD
3150
   section.  */
3151
3152
asection *
3153
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3154
0
{
3155
0
  if (sec_index >= elf_numsections (abfd))
3156
0
    return NULL;
3157
0
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3158
0
}
3159
3160
static const struct bfd_elf_special_section special_sections_b[] =
3161
{
3162
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3163
  { NULL,       0,  0, 0,    0 }
3164
};
3165
3166
static const struct bfd_elf_special_section special_sections_c[] =
3167
{
3168
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3169
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3170
  { NULL,     0, 0, 0,      0 }
3171
};
3172
3173
static const struct bfd_elf_special_section special_sections_d[] =
3174
{
3175
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3176
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3177
  /* There are more DWARF sections than these, but they needn't be added here
3178
     unless you have to cope with broken compilers that don't emit section
3179
     attributes or you want to help the user writing assembler.  */
3180
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3181
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3182
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3183
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3184
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3185
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3186
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3187
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3188
  { NULL,          0,  0, 0,      0 }
3189
};
3190
3191
static const struct bfd_elf_special_section special_sections_f[] =
3192
{
3193
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3194
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3195
  { NULL,        0 , 0, 0,      0 }
3196
};
3197
3198
static const struct bfd_elf_special_section special_sections_g[] =
3199
{
3200
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3201
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3202
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3203
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3204
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3205
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3206
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3207
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3208
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3209
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3210
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3211
  { NULL,      0,    0, 0,         0 }
3212
};
3213
3214
static const struct bfd_elf_special_section special_sections_h[] =
3215
{
3216
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3217
  { NULL,        0, 0, 0,    0 }
3218
};
3219
3220
static const struct bfd_elf_special_section special_sections_i[] =
3221
{
3222
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3223
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3224
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3225
  { NULL,          0,      0, 0,      0 }
3226
};
3227
3228
static const struct bfd_elf_special_section special_sections_l[] =
3229
{
3230
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3231
  { NULL,        0, 0, 0,    0 }
3232
};
3233
3234
static const struct bfd_elf_special_section special_sections_n[] =
3235
{
3236
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3237
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3238
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3239
  { NULL,        0,     0, 0,      0 }
3240
};
3241
3242
static const struct bfd_elf_special_section special_sections_p[] =
3243
{
3244
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3245
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3246
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3247
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3248
  { NULL,       0,      0, 0,     0 }
3249
};
3250
3251
static const struct bfd_elf_special_section special_sections_r[] =
3252
{
3253
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3254
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3255
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3256
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3257
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3258
  { NULL,       0,     0, 0,      0 }
3259
};
3260
3261
static const struct bfd_elf_special_section special_sections_s[] =
3262
{
3263
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3264
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3265
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3266
  /* See struct bfd_elf_special_section declaration for the semantics of
3267
     this special case where .prefix_length != strlen (.prefix).  */
3268
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3269
  { NULL,     0,  0, 0,    0 }
3270
};
3271
3272
static const struct bfd_elf_special_section special_sections_t[] =
3273
{
3274
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3275
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3276
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3277
  { NULL,         0,  0, 0,      0 }
3278
};
3279
3280
static const struct bfd_elf_special_section special_sections_z[] =
3281
{
3282
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3283
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3284
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3285
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3286
  { NULL,         0,  0, 0,      0 }
3287
};
3288
3289
static const struct bfd_elf_special_section * const special_sections[] =
3290
{
3291
  special_sections_b,   /* 'b' */
3292
  special_sections_c,   /* 'c' */
3293
  special_sections_d,   /* 'd' */
3294
  NULL,       /* 'e' */
3295
  special_sections_f,   /* 'f' */
3296
  special_sections_g,   /* 'g' */
3297
  special_sections_h,   /* 'h' */
3298
  special_sections_i,   /* 'i' */
3299
  NULL,       /* 'j' */
3300
  NULL,       /* 'k' */
3301
  special_sections_l,   /* 'l' */
3302
  NULL,       /* 'm' */
3303
  special_sections_n,   /* 'n' */
3304
  NULL,       /* 'o' */
3305
  special_sections_p,   /* 'p' */
3306
  NULL,       /* 'q' */
3307
  special_sections_r,   /* 'r' */
3308
  special_sections_s,   /* 's' */
3309
  special_sections_t,   /* 't' */
3310
  NULL,       /* 'u' */
3311
  NULL,       /* 'v' */
3312
  NULL,       /* 'w' */
3313
  NULL,       /* 'x' */
3314
  NULL,       /* 'y' */
3315
  special_sections_z    /* 'z' */
3316
};
3317
3318
const struct bfd_elf_special_section *
3319
_bfd_elf_get_special_section (const char *name,
3320
            const struct bfd_elf_special_section *spec,
3321
            unsigned int rela)
3322
0
{
3323
0
  int i;
3324
0
  int len;
3325
3326
0
  len = strlen (name);
3327
3328
0
  for (i = 0; spec[i].prefix != NULL; i++)
3329
0
    {
3330
0
      int suffix_len;
3331
0
      int prefix_len = spec[i].prefix_length;
3332
3333
0
      if (len < prefix_len)
3334
0
  continue;
3335
0
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3336
0
  continue;
3337
3338
0
      suffix_len = spec[i].suffix_length;
3339
0
      if (suffix_len <= 0)
3340
0
  {
3341
0
    if (name[prefix_len] != 0)
3342
0
      {
3343
0
        if (suffix_len == 0)
3344
0
    continue;
3345
0
        if (name[prefix_len] != '.'
3346
0
      && (suffix_len == -2
3347
0
          || (rela && spec[i].type == SHT_REL)))
3348
0
    continue;
3349
0
      }
3350
0
  }
3351
0
      else
3352
0
  {
3353
0
    if (len < prefix_len + suffix_len)
3354
0
      continue;
3355
0
    if (memcmp (name + len - suffix_len,
3356
0
          spec[i].prefix + prefix_len,
3357
0
          suffix_len) != 0)
3358
0
      continue;
3359
0
  }
3360
0
      return &spec[i];
3361
0
    }
3362
3363
0
  return NULL;
3364
0
}
3365
3366
const struct bfd_elf_special_section *
3367
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3368
0
{
3369
0
  int i;
3370
0
  const struct bfd_elf_special_section *spec;
3371
0
  const struct elf_backend_data *bed;
3372
3373
  /* See if this is one of the special sections.  */
3374
0
  if (sec->name == NULL)
3375
0
    return NULL;
3376
3377
0
  bed = get_elf_backend_data (abfd);
3378
0
  spec = bed->special_sections;
3379
0
  if (spec)
3380
0
    {
3381
0
      spec = _bfd_elf_get_special_section (sec->name,
3382
0
             bed->special_sections,
3383
0
             sec->use_rela_p);
3384
0
      if (spec != NULL)
3385
0
  return spec;
3386
0
    }
3387
3388
0
  if (sec->name[0] != '.')
3389
0
    return NULL;
3390
3391
0
  i = sec->name[1] - 'b';
3392
0
  if (i < 0 || i > 'z' - 'b')
3393
0
    return NULL;
3394
3395
0
  spec = special_sections[i];
3396
3397
0
  if (spec == NULL)
3398
0
    return NULL;
3399
3400
0
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3401
0
}
3402
3403
bool
3404
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3405
0
{
3406
0
  struct bfd_elf_section_data *sdata;
3407
0
  const struct elf_backend_data *bed;
3408
0
  const struct bfd_elf_special_section *ssect;
3409
3410
0
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3411
0
  if (sdata == NULL)
3412
0
    {
3413
0
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3414
0
                sizeof (*sdata));
3415
0
      if (sdata == NULL)
3416
0
  return false;
3417
0
      sec->used_by_bfd = sdata;
3418
0
    }
3419
3420
  /* Indicate whether or not this section should use RELA relocations.  */
3421
0
  bed = get_elf_backend_data (abfd);
3422
0
  sec->use_rela_p = bed->default_use_rela_p;
3423
3424
  /* Set up ELF section type and flags for newly created sections, if
3425
     there is an ABI mandated section.  */
3426
0
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3427
0
  if (ssect != NULL)
3428
0
    {
3429
0
      elf_section_type (sec) = ssect->type;
3430
0
      elf_section_flags (sec) = ssect->attr;
3431
0
    }
3432
3433
0
  return _bfd_generic_new_section_hook (abfd, sec);
3434
0
}
3435
3436
/* Create a new bfd section from an ELF program header.
3437
3438
   Since program segments have no names, we generate a synthetic name
3439
   of the form segment<NUM>, where NUM is generally the index in the
3440
   program header table.  For segments that are split (see below) we
3441
   generate the names segment<NUM>a and segment<NUM>b.
3442
3443
   Note that some program segments may have a file size that is different than
3444
   (less than) the memory size.  All this means is that at execution the
3445
   system must allocate the amount of memory specified by the memory size,
3446
   but only initialize it with the first "file size" bytes read from the
3447
   file.  This would occur for example, with program segments consisting
3448
   of combined data+bss.
3449
3450
   To handle the above situation, this routine generates TWO bfd sections
3451
   for the single program segment.  The first has the length specified by
3452
   the file size of the segment, and the second has the length specified
3453
   by the difference between the two sizes.  In effect, the segment is split
3454
   into its initialized and uninitialized parts.  */
3455
3456
bool
3457
_bfd_elf_make_section_from_phdr (bfd *abfd,
3458
         Elf_Internal_Phdr *hdr,
3459
         int hdr_index,
3460
         const char *type_name)
3461
0
{
3462
0
  asection *newsect;
3463
0
  char *name;
3464
0
  char namebuf[64];
3465
0
  size_t len;
3466
0
  int split;
3467
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3468
3469
0
  split = ((hdr->p_memsz > 0)
3470
0
      && (hdr->p_filesz > 0)
3471
0
      && (hdr->p_memsz > hdr->p_filesz));
3472
3473
0
  if (hdr->p_filesz > 0)
3474
0
    {
3475
0
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3476
0
      len = strlen (namebuf) + 1;
3477
0
      name = (char *) bfd_alloc (abfd, len);
3478
0
      if (!name)
3479
0
  return false;
3480
0
      memcpy (name, namebuf, len);
3481
0
      newsect = bfd_make_section (abfd, name);
3482
0
      if (newsect == NULL)
3483
0
  return false;
3484
0
      newsect->vma = hdr->p_vaddr / opb;
3485
0
      newsect->lma = hdr->p_paddr / opb;
3486
0
      newsect->size = hdr->p_filesz;
3487
0
      newsect->filepos = hdr->p_offset;
3488
0
      newsect->flags |= SEC_HAS_CONTENTS;
3489
0
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3490
0
      if (hdr->p_type == PT_LOAD)
3491
0
  {
3492
0
    newsect->flags |= SEC_ALLOC;
3493
0
    newsect->flags |= SEC_LOAD;
3494
0
    if (hdr->p_flags & PF_X)
3495
0
      {
3496
        /* FIXME: all we known is that it has execute PERMISSION,
3497
     may be data.  */
3498
0
        newsect->flags |= SEC_CODE;
3499
0
      }
3500
0
  }
3501
0
      if (!(hdr->p_flags & PF_W))
3502
0
  {
3503
0
    newsect->flags |= SEC_READONLY;
3504
0
  }
3505
0
    }
3506
3507
0
  if (hdr->p_memsz > hdr->p_filesz)
3508
0
    {
3509
0
      bfd_vma align;
3510
3511
0
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3512
0
      len = strlen (namebuf) + 1;
3513
0
      name = (char *) bfd_alloc (abfd, len);
3514
0
      if (!name)
3515
0
  return false;
3516
0
      memcpy (name, namebuf, len);
3517
0
      newsect = bfd_make_section (abfd, name);
3518
0
      if (newsect == NULL)
3519
0
  return false;
3520
0
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3521
0
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3522
0
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3523
0
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3524
0
      align = newsect->vma & -newsect->vma;
3525
0
      if (align == 0 || align > hdr->p_align)
3526
0
  align = hdr->p_align;
3527
0
      newsect->alignment_power = bfd_log2 (align);
3528
0
      if (hdr->p_type == PT_LOAD)
3529
0
  {
3530
0
    newsect->flags |= SEC_ALLOC;
3531
0
    if (hdr->p_flags & PF_X)
3532
0
      newsect->flags |= SEC_CODE;
3533
0
  }
3534
0
      if (!(hdr->p_flags & PF_W))
3535
0
  newsect->flags |= SEC_READONLY;
3536
0
    }
3537
3538
0
  return true;
3539
0
}
3540
3541
static bool
3542
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3543
0
{
3544
  /* The return value is ignored.  Build-ids are considered optional.  */
3545
0
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3546
0
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3547
0
      (templ, offset);
3548
0
  return false;
3549
0
}
3550
3551
bool
3552
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3553
0
{
3554
0
  const struct elf_backend_data *bed;
3555
3556
0
  switch (hdr->p_type)
3557
0
    {
3558
0
    case PT_NULL:
3559
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3560
3561
0
    case PT_LOAD:
3562
0
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3563
0
  return false;
3564
0
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3565
0
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3566
0
      return true;
3567
3568
0
    case PT_DYNAMIC:
3569
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3570
3571
0
    case PT_INTERP:
3572
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3573
3574
0
    case PT_NOTE:
3575
0
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3576
0
  return false;
3577
0
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3578
0
          hdr->p_align))
3579
0
  return false;
3580
0
      return true;
3581
3582
0
    case PT_SHLIB:
3583
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3584
3585
0
    case PT_PHDR:
3586
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3587
3588
0
    case PT_GNU_EH_FRAME:
3589
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3590
0
                "eh_frame_hdr");
3591
3592
0
    case PT_GNU_STACK:
3593
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3594
3595
0
    case PT_GNU_RELRO:
3596
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3597
3598
0
    case PT_GNU_SFRAME:
3599
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3600
0
                "sframe");
3601
3602
0
    default:
3603
      /* Check for any processor-specific program segment types.  */
3604
0
      bed = get_elf_backend_data (abfd);
3605
0
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3606
0
    }
3607
0
}
3608
3609
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3610
   REL or RELA.  */
3611
3612
Elf_Internal_Shdr *
3613
_bfd_elf_single_rel_hdr (asection *sec)
3614
0
{
3615
0
  if (elf_section_data (sec)->rel.hdr)
3616
0
    {
3617
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3618
0
      return elf_section_data (sec)->rel.hdr;
3619
0
    }
3620
0
  else
3621
0
    return elf_section_data (sec)->rela.hdr;
3622
0
}
3623
3624
static bool
3625
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3626
          Elf_Internal_Shdr *rel_hdr,
3627
          const char *sec_name,
3628
          bool use_rela_p)
3629
0
{
3630
0
  char *name = (char *) bfd_alloc (abfd,
3631
0
           sizeof ".rela" + strlen (sec_name));
3632
0
  if (name == NULL)
3633
0
    return false;
3634
3635
0
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3636
0
  rel_hdr->sh_name =
3637
0
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3638
0
          false);
3639
0
  if (rel_hdr->sh_name == (unsigned int) -1)
3640
0
    return false;
3641
3642
0
  return true;
3643
0
}
3644
3645
/* Allocate and initialize a section-header for a new reloc section,
3646
   containing relocations against ASECT.  It is stored in RELDATA.  If
3647
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3648
   relocations.  */
3649
3650
static bool
3651
_bfd_elf_init_reloc_shdr (bfd *abfd,
3652
        struct bfd_elf_section_reloc_data *reldata,
3653
        const char *sec_name,
3654
        bool use_rela_p,
3655
        bool delay_st_name_p)
3656
0
{
3657
0
  Elf_Internal_Shdr *rel_hdr;
3658
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3659
3660
0
  BFD_ASSERT (reldata->hdr == NULL);
3661
0
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3662
0
  reldata->hdr = rel_hdr;
3663
3664
0
  if (delay_st_name_p)
3665
0
    rel_hdr->sh_name = (unsigned int) -1;
3666
0
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3667
0
          use_rela_p))
3668
0
    return false;
3669
0
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3670
0
  rel_hdr->sh_entsize = (use_rela_p
3671
0
       ? bed->s->sizeof_rela
3672
0
       : bed->s->sizeof_rel);
3673
0
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3674
0
  rel_hdr->sh_flags = 0;
3675
0
  rel_hdr->sh_addr = 0;
3676
0
  rel_hdr->sh_size = 0;
3677
0
  rel_hdr->sh_offset = 0;
3678
3679
0
  return true;
3680
0
}
3681
3682
/* Return the default section type based on the passed in section flags.  */
3683
3684
int
3685
bfd_elf_get_default_section_type (flagword flags)
3686
0
{
3687
0
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3688
0
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3689
0
    return SHT_NOBITS;
3690
0
  return SHT_PROGBITS;
3691
0
}
3692
3693
struct fake_section_arg
3694
{
3695
  struct bfd_link_info *link_info;
3696
  bool failed;
3697
};
3698
3699
/* Set up an ELF internal section header for a section.  */
3700
3701
static void
3702
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3703
0
{
3704
0
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3705
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3706
0
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3707
0
  Elf_Internal_Shdr *this_hdr;
3708
0
  unsigned int sh_type;
3709
0
  const char *name = asect->name;
3710
0
  bool delay_st_name_p = false;
3711
0
  bfd_vma mask;
3712
3713
0
  if (arg->failed)
3714
0
    {
3715
      /* We already failed; just get out of the bfd_map_over_sections
3716
   loop.  */
3717
0
      return;
3718
0
    }
3719
3720
0
  this_hdr = &esd->this_hdr;
3721
3722
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3723
0
  if (arg->link_info
3724
0
      && (abfd->flags & BFD_COMPRESS) != 0
3725
0
      && (asect->flags & SEC_DEBUGGING) != 0
3726
0
      && name[1] == 'd'
3727
0
      && name[6] == '_')
3728
0
    {
3729
      /* If this section will be compressed, delay adding section
3730
   name to section name section after it is compressed in
3731
   _bfd_elf_assign_file_positions_for_non_load.  */
3732
0
      delay_st_name_p = true;
3733
0
    }
3734
3735
0
  if (delay_st_name_p)
3736
0
    this_hdr->sh_name = (unsigned int) -1;
3737
0
  else
3738
0
    {
3739
0
      this_hdr->sh_name
3740
0
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3741
0
                name, false);
3742
0
      if (this_hdr->sh_name == (unsigned int) -1)
3743
0
  {
3744
0
    arg->failed = true;
3745
0
    return;
3746
0
  }
3747
0
    }
3748
3749
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3750
3751
0
  if ((asect->flags & SEC_ALLOC) != 0
3752
0
      || asect->user_set_vma)
3753
0
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3754
0
  else
3755
0
    this_hdr->sh_addr = 0;
3756
3757
0
  this_hdr->sh_offset = 0;
3758
0
  this_hdr->sh_size = asect->size;
3759
0
  this_hdr->sh_link = 0;
3760
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3761
0
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3762
0
    {
3763
0
      _bfd_error_handler
3764
  /* xgettext:c-format */
3765
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3766
0
   abfd, asect->alignment_power, asect);
3767
0
      arg->failed = true;
3768
0
      return;
3769
0
    }
3770
  /* Set sh_addralign to the highest power of two given by alignment
3771
     consistent with the section VMA.  Linker scripts can force VMA.  */
3772
0
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3773
0
  this_hdr->sh_addralign = mask & -mask;
3774
  /* The sh_entsize and sh_info fields may have been set already by
3775
     copy_private_section_data.  */
3776
3777
0
  this_hdr->bfd_section = asect;
3778
0
  this_hdr->contents = NULL;
3779
3780
  /* If the section type is unspecified, we set it based on
3781
     asect->flags.  */
3782
0
  if (asect->type != 0)
3783
0
    sh_type = asect->type;
3784
0
  else if ((asect->flags & SEC_GROUP) != 0)
3785
0
    sh_type = SHT_GROUP;
3786
0
  else
3787
0
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3788
3789
0
  if (this_hdr->sh_type == SHT_NULL)
3790
0
    this_hdr->sh_type = sh_type;
3791
0
  else if (this_hdr->sh_type == SHT_NOBITS
3792
0
     && sh_type == SHT_PROGBITS
3793
0
     && (asect->flags & SEC_ALLOC) != 0)
3794
0
    {
3795
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3796
   allow the link to proceed.  This can happen when users link
3797
   non-bss input sections to bss output sections, or emit data
3798
   to a bss output section via a linker script.  */
3799
0
      _bfd_error_handler
3800
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3801
0
      this_hdr->sh_type = sh_type;
3802
0
    }
3803
3804
0
  switch (this_hdr->sh_type)
3805
0
    {
3806
0
    default:
3807
0
      break;
3808
3809
0
    case SHT_STRTAB:
3810
0
    case SHT_NOTE:
3811
0
    case SHT_NOBITS:
3812
0
    case SHT_PROGBITS:
3813
0
      break;
3814
3815
0
    case SHT_INIT_ARRAY:
3816
0
    case SHT_FINI_ARRAY:
3817
0
    case SHT_PREINIT_ARRAY:
3818
0
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3819
0
      break;
3820
3821
0
    case SHT_HASH:
3822
0
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3823
0
      break;
3824
3825
0
    case SHT_DYNSYM:
3826
0
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3827
0
      break;
3828
3829
0
    case SHT_DYNAMIC:
3830
0
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3831
0
      break;
3832
3833
0
    case SHT_RELA:
3834
0
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3835
0
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3836
0
      break;
3837
3838
0
     case SHT_REL:
3839
0
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3840
0
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3841
0
      break;
3842
3843
0
     case SHT_GNU_versym:
3844
0
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3845
0
      break;
3846
3847
0
     case SHT_GNU_verdef:
3848
0
      this_hdr->sh_entsize = 0;
3849
      /* objcopy or strip will copy over sh_info, but may not set
3850
   cverdefs.  The linker will set cverdefs, but sh_info will be
3851
   zero.  */
3852
0
      if (this_hdr->sh_info == 0)
3853
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3854
0
      else
3855
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3856
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3857
0
      break;
3858
3859
0
    case SHT_GNU_verneed:
3860
0
      this_hdr->sh_entsize = 0;
3861
      /* objcopy or strip will copy over sh_info, but may not set
3862
   cverrefs.  The linker will set cverrefs, but sh_info will be
3863
   zero.  */
3864
0
      if (this_hdr->sh_info == 0)
3865
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3866
0
      else
3867
0
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3868
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3869
0
      break;
3870
3871
0
    case SHT_GROUP:
3872
0
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3873
0
      break;
3874
3875
0
    case SHT_GNU_HASH:
3876
0
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3877
0
      break;
3878
0
    }
3879
3880
0
  if ((asect->flags & SEC_ALLOC) != 0)
3881
0
    this_hdr->sh_flags |= SHF_ALLOC;
3882
0
  if ((asect->flags & SEC_READONLY) == 0)
3883
0
    this_hdr->sh_flags |= SHF_WRITE;
3884
0
  if ((asect->flags & SEC_CODE) != 0)
3885
0
    this_hdr->sh_flags |= SHF_EXECINSTR;
3886
0
  if ((asect->flags & SEC_MERGE) != 0)
3887
0
    {
3888
0
      this_hdr->sh_flags |= SHF_MERGE;
3889
0
      this_hdr->sh_entsize = asect->entsize;
3890
0
    }
3891
0
  if ((asect->flags & SEC_STRINGS) != 0)
3892
0
    this_hdr->sh_flags |= SHF_STRINGS;
3893
0
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3894
0
    this_hdr->sh_flags |= SHF_GROUP;
3895
0
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3896
0
    {
3897
0
      this_hdr->sh_flags |= SHF_TLS;
3898
0
      if (asect->size == 0
3899
0
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3900
0
  {
3901
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3902
3903
0
    this_hdr->sh_size = 0;
3904
0
    if (o != NULL)
3905
0
      {
3906
0
        this_hdr->sh_size = o->offset + o->size;
3907
0
        if (this_hdr->sh_size != 0)
3908
0
    this_hdr->sh_type = SHT_NOBITS;
3909
0
      }
3910
0
  }
3911
0
    }
3912
0
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3913
0
    this_hdr->sh_flags |= SHF_EXCLUDE;
3914
3915
  /* If the section has relocs, set up a section header for the
3916
     SHT_REL[A] section.  If two relocation sections are required for
3917
     this section, it is up to the processor-specific back-end to
3918
     create the other.  */
3919
0
  if ((asect->flags & SEC_RELOC) != 0)
3920
0
    {
3921
      /* When doing a relocatable link, create both REL and RELA sections if
3922
   needed.  */
3923
0
      if (arg->link_info
3924
    /* Do the normal setup if we wouldn't create any sections here.  */
3925
0
    && esd->rel.count + esd->rela.count > 0
3926
0
    && (bfd_link_relocatable (arg->link_info)
3927
0
        || arg->link_info->emitrelocations))
3928
0
  {
3929
0
    if (esd->rel.count && esd->rel.hdr == NULL
3930
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3931
0
              false, delay_st_name_p))
3932
0
      {
3933
0
        arg->failed = true;
3934
0
        return;
3935
0
      }
3936
0
    if (esd->rela.count && esd->rela.hdr == NULL
3937
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3938
0
              true, delay_st_name_p))
3939
0
      {
3940
0
        arg->failed = true;
3941
0
        return;
3942
0
      }
3943
0
  }
3944
0
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3945
0
            (asect->use_rela_p
3946
0
             ? &esd->rela : &esd->rel),
3947
0
            name,
3948
0
            asect->use_rela_p,
3949
0
            delay_st_name_p))
3950
0
  {
3951
0
    arg->failed = true;
3952
0
    return;
3953
0
  }
3954
0
    }
3955
3956
  /* Check for processor-specific section types.  */
3957
0
  sh_type = this_hdr->sh_type;
3958
0
  if (bed->elf_backend_fake_sections
3959
0
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3960
0
    {
3961
0
      arg->failed = true;
3962
0
      return;
3963
0
    }
3964
3965
0
  if (sh_type == SHT_NOBITS && asect->size != 0)
3966
0
    {
3967
      /* Don't change the header type from NOBITS if we are being
3968
   called for objcopy --only-keep-debug.  */
3969
0
      this_hdr->sh_type = sh_type;
3970
0
    }
3971
0
}
3972
3973
/* Fill in the contents of a SHT_GROUP section.  Called from
3974
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3975
   when ELF targets use the generic linker, ld.  Called for ld -r
3976
   from bfd_elf_final_link.  */
3977
3978
void
3979
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3980
0
{
3981
0
  bool *failedptr = (bool *) failedptrarg;
3982
0
  asection *elt, *first;
3983
0
  unsigned char *loc;
3984
0
  bool gas;
3985
3986
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3987
     elfxx-ia64.c.  */
3988
0
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3989
0
      || sec->size == 0
3990
0
      || *failedptr)
3991
0
    return;
3992
3993
0
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3994
0
    {
3995
0
      unsigned long symindx = 0;
3996
3997
      /* elf_group_id will have been set up by objcopy and the
3998
   generic linker.  */
3999
0
      if (elf_group_id (sec) != NULL)
4000
0
  symindx = elf_group_id (sec)->udata.i;
4001
4002
0
      if (symindx == 0)
4003
0
  {
4004
    /* If called from the assembler, swap_out_syms will have set up
4005
       elf_section_syms.
4006
       PR 25699: A corrupt input file could contain bogus group info.  */
4007
0
    if (sec->index >= elf_num_section_syms (abfd)
4008
0
        || elf_section_syms (abfd)[sec->index] == NULL)
4009
0
      {
4010
0
        *failedptr = true;
4011
0
        return;
4012
0
      }
4013
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4014
0
  }
4015
0
      elf_section_data (sec)->this_hdr.sh_info = symindx;
4016
0
    }
4017
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4018
0
    {
4019
      /* The ELF backend linker sets sh_info to -2 when the group
4020
   signature symbol is global, and thus the index can't be
4021
   set until all local symbols are output.  */
4022
0
      asection *igroup;
4023
0
      struct bfd_elf_section_data *sec_data;
4024
0
      unsigned long symndx;
4025
0
      unsigned long extsymoff;
4026
0
      struct elf_link_hash_entry *h;
4027
4028
      /* The point of this little dance to the first SHF_GROUP section
4029
   then back to the SHT_GROUP section is that this gets us to
4030
   the SHT_GROUP in the input object.  */
4031
0
      igroup = elf_sec_group (elf_next_in_group (sec));
4032
0
      sec_data = elf_section_data (igroup);
4033
0
      symndx = sec_data->this_hdr.sh_info;
4034
0
      extsymoff = 0;
4035
0
      if (!elf_bad_symtab (igroup->owner))
4036
0
  {
4037
0
    Elf_Internal_Shdr *symtab_hdr;
4038
4039
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4040
0
    extsymoff = symtab_hdr->sh_info;
4041
0
  }
4042
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4043
0
      while (h->root.type == bfd_link_hash_indirect
4044
0
       || h->root.type == bfd_link_hash_warning)
4045
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4046
4047
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
4048
0
    }
4049
4050
  /* The contents won't be allocated for "ld -r" or objcopy.  */
4051
0
  gas = true;
4052
0
  if (sec->contents == NULL)
4053
0
    {
4054
0
      gas = false;
4055
0
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4056
4057
      /* Arrange for the section to be written out.  */
4058
0
      elf_section_data (sec)->this_hdr.contents = sec->contents;
4059
0
      if (sec->contents == NULL)
4060
0
  {
4061
0
    *failedptr = true;
4062
0
    return;
4063
0
  }
4064
0
    }
4065
4066
0
  loc = sec->contents + sec->size;
4067
4068
  /* Get the pointer to the first section in the group that gas
4069
     squirreled away here.  objcopy arranges for this to be set to the
4070
     start of the input section group.  */
4071
0
  first = elt = elf_next_in_group (sec);
4072
4073
  /* First element is a flag word.  Rest of section is elf section
4074
     indices for all the sections of the group.  Write them backwards
4075
     just to keep the group in the same order as given in .section
4076
     directives, not that it matters.  */
4077
0
  while (elt != NULL)
4078
0
    {
4079
0
      asection *s;
4080
4081
0
      s = elt;
4082
0
      if (!gas)
4083
0
  s = s->output_section;
4084
0
      if (s != NULL
4085
0
    && !bfd_is_abs_section (s))
4086
0
  {
4087
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4088
0
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4089
4090
0
    if (elf_sec->rel.hdr != NULL
4091
0
        && (gas
4092
0
      || (input_elf_sec->rel.hdr != NULL
4093
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4094
0
      {
4095
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4096
0
        loc -= 4;
4097
0
        if (loc == sec->contents)
4098
0
    break;
4099
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4100
0
      }
4101
0
    if (elf_sec->rela.hdr != NULL
4102
0
        && (gas
4103
0
      || (input_elf_sec->rela.hdr != NULL
4104
0
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4105
0
      {
4106
0
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4107
0
        loc -= 4;
4108
0
        if (loc == sec->contents)
4109
0
    break;
4110
0
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4111
0
      }
4112
0
    loc -= 4;
4113
0
    if (loc == sec->contents)
4114
0
      break;
4115
0
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4116
0
  }
4117
0
      elt = elf_next_in_group (elt);
4118
0
      if (elt == first)
4119
0
  break;
4120
0
    }
4121
4122
  /* We should always get here with loc == sec->contents + 4, but it is
4123
     possible to craft bogus SHT_GROUP sections that will cause segfaults
4124
     in objcopy without checking loc here and in the loop above.  */
4125
0
  if (loc == sec->contents)
4126
0
    BFD_ASSERT (0);
4127
0
  else
4128
0
    {
4129
0
      loc -= 4;
4130
0
      if (loc != sec->contents)
4131
0
  {
4132
0
    BFD_ASSERT (0);
4133
0
    memset (sec->contents + 4, 0, loc - sec->contents);
4134
0
    loc = sec->contents;
4135
0
  }
4136
0
    }
4137
4138
0
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4139
0
}
4140
4141
/* Given NAME, the name of a relocation section stripped of its
4142
   .rel/.rela prefix, return the section in ABFD to which the
4143
   relocations apply.  */
4144
4145
asection *
4146
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4147
0
{
4148
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4149
     section likely apply to .got.plt or .got section.  */
4150
0
  if (get_elf_backend_data (abfd)->want_got_plt
4151
0
      && strcmp (name, ".plt") == 0)
4152
0
    {
4153
0
      asection *sec;
4154
4155
0
      name = ".got.plt";
4156
0
      sec = bfd_get_section_by_name (abfd, name);
4157
0
      if (sec != NULL)
4158
0
  return sec;
4159
0
      name = ".got";
4160
0
    }
4161
4162
0
  return bfd_get_section_by_name (abfd, name);
4163
0
}
4164
4165
/* Return the section to which RELOC_SEC applies.  */
4166
4167
static asection *
4168
elf_get_reloc_section (asection *reloc_sec)
4169
0
{
4170
0
  const char *name;
4171
0
  unsigned int type;
4172
0
  bfd *abfd;
4173
0
  const struct elf_backend_data *bed;
4174
4175
0
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4176
0
  if (type != SHT_REL && type != SHT_RELA)
4177
0
    return NULL;
4178
4179
  /* We look up the section the relocs apply to by name.  */
4180
0
  name = reloc_sec->name;
4181
0
  if (!startswith (name, ".rel"))
4182
0
    return NULL;
4183
0
  name += 4;
4184
0
  if (type == SHT_RELA && *name++ != 'a')
4185
0
    return NULL;
4186
4187
0
  abfd = reloc_sec->owner;
4188
0
  bed = get_elf_backend_data (abfd);
4189
0
  return bed->get_reloc_section (abfd, name);
4190
0
}
4191
4192
/* Assign all ELF section numbers.  The dummy first section is handled here
4193
   too.  The link/info pointers for the standard section types are filled
4194
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4195
   here for gas, objcopy, and when using the generic ELF linker.  */
4196
4197
static bool
4198
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4199
0
{
4200
0
  struct elf_obj_tdata *t = elf_tdata (abfd);
4201
0
  asection *sec;
4202
0
  unsigned int section_number;
4203
0
  Elf_Internal_Shdr **i_shdrp;
4204
0
  struct bfd_elf_section_data *d;
4205
0
  bool need_symtab;
4206
0
  size_t amt;
4207
4208
0
  section_number = 1;
4209
4210
0
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4211
4212
  /* SHT_GROUP sections are in relocatable files only.  */
4213
0
  if (link_info == NULL || !link_info->resolve_section_groups)
4214
0
    {
4215
0
      size_t reloc_count = 0;
4216
4217
      /* Put SHT_GROUP sections first.  */
4218
0
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4219
0
  {
4220
0
    d = elf_section_data (sec);
4221
4222
0
    if (d->this_hdr.sh_type == SHT_GROUP)
4223
0
      {
4224
0
        if (sec->flags & SEC_LINKER_CREATED)
4225
0
    {
4226
      /* Remove the linker created SHT_GROUP sections.  */
4227
0
      bfd_section_list_remove (abfd, sec);
4228
0
      abfd->section_count--;
4229
0
    }
4230
0
        else
4231
0
    d->this_idx = section_number++;
4232
0
      }
4233
4234
    /* Count relocations.  */
4235
0
    reloc_count += sec->reloc_count;
4236
0
  }
4237
4238
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4239
0
      if (reloc_count == 0)
4240
0
  abfd->flags &= ~HAS_RELOC;
4241
0
      else
4242
0
  abfd->flags |= HAS_RELOC;
4243
0
    }
4244
4245
0
  for (sec = abfd->sections; sec; sec = sec->next)
4246
0
    {
4247
0
      d = elf_section_data (sec);
4248
4249
0
      if (d->this_hdr.sh_type != SHT_GROUP)
4250
0
  d->this_idx = section_number++;
4251
0
      if (d->this_hdr.sh_name != (unsigned int) -1)
4252
0
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4253
0
      if (d->rel.hdr)
4254
0
  {
4255
0
    d->rel.idx = section_number++;
4256
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4257
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4258
0
  }
4259
0
      else
4260
0
  d->rel.idx = 0;
4261
4262
0
      if (d->rela.hdr)
4263
0
  {
4264
0
    d->rela.idx = section_number++;
4265
0
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4266
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4267
0
  }
4268
0
      else
4269
0
  d->rela.idx = 0;
4270
0
    }
4271
4272
0
  need_symtab = (bfd_get_symcount (abfd) > 0
4273
0
     || (link_info == NULL
4274
0
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4275
0
       == HAS_RELOC)));
4276
0
  if (need_symtab)
4277
0
    {
4278
0
      elf_onesymtab (abfd) = section_number++;
4279
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4280
0
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4281
0
  {
4282
0
    elf_section_list *entry;
4283
4284
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4285
4286
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4287
0
    entry->ndx = section_number++;
4288
0
    elf_symtab_shndx_list (abfd) = entry;
4289
0
    entry->hdr.sh_name
4290
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4291
0
              ".symtab_shndx", false);
4292
0
    if (entry->hdr.sh_name == (unsigned int) -1)
4293
0
      return false;
4294
0
  }
4295
0
      elf_strtab_sec (abfd) = section_number++;
4296
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4297
0
    }
4298
4299
0
  elf_shstrtab_sec (abfd) = section_number++;
4300
0
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4301
0
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4302
4303
0
  if (section_number >= SHN_LORESERVE)
4304
0
    {
4305
      /* xgettext:c-format */
4306
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4307
0
        abfd, section_number);
4308
0
      return false;
4309
0
    }
4310
4311
0
  elf_numsections (abfd) = section_number;
4312
0
  elf_elfheader (abfd)->e_shnum = section_number;
4313
4314
  /* Set up the list of section header pointers, in agreement with the
4315
     indices.  */
4316
0
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4317
0
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4318
0
  if (i_shdrp == NULL)
4319
0
    return false;
4320
4321
0
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4322
0
             sizeof (Elf_Internal_Shdr));
4323
0
  if (i_shdrp[0] == NULL)
4324
0
    {
4325
0
      bfd_release (abfd, i_shdrp);
4326
0
      return false;
4327
0
    }
4328
4329
0
  elf_elfsections (abfd) = i_shdrp;
4330
4331
0
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4332
0
  if (need_symtab)
4333
0
    {
4334
0
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4335
0
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4336
0
  {
4337
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4338
0
    BFD_ASSERT (entry != NULL);
4339
0
    i_shdrp[entry->ndx] = & entry->hdr;
4340
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4341
0
  }
4342
0
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4343
0
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4344
0
    }
4345
4346
0
  for (sec = abfd->sections; sec; sec = sec->next)
4347
0
    {
4348
0
      asection *s;
4349
4350
0
      d = elf_section_data (sec);
4351
4352
0
      i_shdrp[d->this_idx] = &d->this_hdr;
4353
0
      if (d->rel.idx != 0)
4354
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4355
0
      if (d->rela.idx != 0)
4356
0
  i_shdrp[d->rela.idx] = d->rela.hdr;
4357
4358
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4359
4360
      /* sh_link of a reloc section is the section index of the symbol
4361
   table.  sh_info is the section index of the section to which
4362
   the relocation entries apply.  */
4363
0
      if (d->rel.idx != 0)
4364
0
  {
4365
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4366
0
    d->rel.hdr->sh_info = d->this_idx;
4367
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4368
0
  }
4369
0
      if (d->rela.idx != 0)
4370
0
  {
4371
0
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4372
0
    d->rela.hdr->sh_info = d->this_idx;
4373
0
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4374
0
  }
4375
4376
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4377
0
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4378
0
  {
4379
0
    s = elf_linked_to_section (sec);
4380
    /* We can now have a NULL linked section pointer.
4381
       This happens when the sh_link field is 0, which is done
4382
       when a linked to section is discarded but the linking
4383
       section has been retained for some reason.  */
4384
0
    if (s)
4385
0
      {
4386
        /* Check discarded linkonce section.  */
4387
0
        if (discarded_section (s))
4388
0
    {
4389
0
      asection *kept;
4390
0
      _bfd_error_handler
4391
        /* xgettext:c-format */
4392
0
        (_("%pB: sh_link of section `%pA' points to"
4393
0
           " discarded section `%pA' of `%pB'"),
4394
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4395
      /* Point to the kept section if it has the same
4396
         size as the discarded one.  */
4397
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4398
0
      if (kept == NULL)
4399
0
        {
4400
0
          bfd_set_error (bfd_error_bad_value);
4401
0
          return false;
4402
0
        }
4403
0
      s = kept;
4404
0
    }
4405
        /* Handle objcopy. */
4406
0
        else if (s->output_section == NULL)
4407
0
    {
4408
0
      _bfd_error_handler
4409
        /* xgettext:c-format */
4410
0
        (_("%pB: sh_link of section `%pA' points to"
4411
0
           " removed section `%pA' of `%pB'"),
4412
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4413
0
      bfd_set_error (bfd_error_bad_value);
4414
0
      return false;
4415
0
    }
4416
0
        s = s->output_section;
4417
0
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4418
0
      }
4419
0
  }
4420
4421
0
      switch (d->this_hdr.sh_type)
4422
0
  {
4423
0
  case SHT_REL:
4424
0
  case SHT_RELA:
4425
    /* sh_link is the section index of the symbol table.
4426
       sh_info is the section index of the section to which the
4427
       relocation entries apply.  */
4428
0
    if (d->this_hdr.sh_link == 0)
4429
0
      {
4430
        /* FIXME maybe: If this is a reloc section which we are
4431
     treating as a normal section then we likely should
4432
     not be assuming its sh_link is .dynsym or .symtab.  */
4433
0
        if ((sec->flags & SEC_ALLOC) != 0)
4434
0
    {
4435
0
      s = bfd_get_section_by_name (abfd, ".dynsym");
4436
0
      if (s != NULL)
4437
0
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4438
0
    }
4439
0
        else
4440
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4441
0
      }
4442
4443
0
    s = elf_get_reloc_section (sec);
4444
0
    if (s != NULL)
4445
0
      {
4446
0
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4447
0
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4448
0
      }
4449
0
    break;
4450
4451
0
  case SHT_STRTAB:
4452
    /* We assume that a section named .stab*str is a stabs
4453
       string section.  We look for a section with the same name
4454
       but without the trailing ``str'', and set its sh_link
4455
       field to point to this section.  */
4456
0
    if (startswith (sec->name, ".stab")
4457
0
        && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4458
0
      {
4459
0
        size_t len;
4460
0
        char *alc;
4461
4462
0
        len = strlen (sec->name);
4463
0
        alc = (char *) bfd_malloc (len - 2);
4464
0
        if (alc == NULL)
4465
0
    return false;
4466
0
        memcpy (alc, sec->name, len - 3);
4467
0
        alc[len - 3] = '\0';
4468
0
        s = bfd_get_section_by_name (abfd, alc);
4469
0
        free (alc);
4470
0
        if (s != NULL)
4471
0
    {
4472
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4473
4474
      /* This is a .stab section.  */
4475
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4476
0
    }
4477
0
      }
4478
0
    break;
4479
4480
0
  case SHT_DYNAMIC:
4481
0
  case SHT_DYNSYM:
4482
0
  case SHT_GNU_verneed:
4483
0
  case SHT_GNU_verdef:
4484
    /* sh_link is the section header index of the string table
4485
       used for the dynamic entries, or the symbol table, or the
4486
       version strings.  */
4487
0
    s = bfd_get_section_by_name (abfd, ".dynstr");
4488
0
    if (s != NULL)
4489
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4490
0
    break;
4491
4492
0
  case SHT_GNU_LIBLIST:
4493
    /* sh_link is the section header index of the prelink library
4494
       list used for the dynamic entries, or the symbol table, or
4495
       the version strings.  */
4496
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4497
0
                ? ".dynstr" : ".gnu.libstr"));
4498
0
    if (s != NULL)
4499
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4500
0
    break;
4501
4502
0
  case SHT_HASH:
4503
0
  case SHT_GNU_HASH:
4504
0
  case SHT_GNU_versym:
4505
    /* sh_link is the section header index of the symbol table
4506
       this hash table or version table is for.  */
4507
0
    s = bfd_get_section_by_name (abfd, ".dynsym");
4508
0
    if (s != NULL)
4509
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4510
0
    break;
4511
4512
0
  case SHT_GROUP:
4513
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4514
0
  }
4515
0
    }
4516
4517
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4518
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4519
     debug section name from .debug_* to .zdebug_* if needed.  */
4520
4521
0
  return true;
4522
0
}
4523
4524
static bool
4525
sym_is_global (bfd *abfd, asymbol *sym)
4526
0
{
4527
  /* If the backend has a special mapping, use it.  */
4528
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4529
0
  if (bed->elf_backend_sym_is_global)
4530
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4531
4532
0
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4533
0
    || bfd_is_und_section (bfd_asymbol_section (sym))
4534
0
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4535
0
}
4536
4537
/* Filter global symbols of ABFD to include in the import library.  All
4538
   SYMCOUNT symbols of ABFD can be examined from their pointers in
4539
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4540
   the beginning of that array.
4541
4542
   Returns the number of symbols to keep.  */
4543
4544
unsigned int
4545
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4546
        asymbol **syms, long symcount)
4547
0
{
4548
0
  long src_count, dst_count = 0;
4549
4550
0
  for (src_count = 0; src_count < symcount; src_count++)
4551
0
    {
4552
0
      asymbol *sym = syms[src_count];
4553
0
      char *name = (char *) bfd_asymbol_name (sym);
4554
0
      struct bfd_link_hash_entry *h;
4555
4556
0
      if (!sym_is_global (abfd, sym))
4557
0
  continue;
4558
4559
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4560
0
      if (h == NULL)
4561
0
  continue;
4562
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4563
0
  continue;
4564
0
      if (h->linker_def || h->ldscript_def)
4565
0
  continue;
4566
4567
0
      syms[dst_count++] = sym;
4568
0
    }
4569
4570
0
  syms[dst_count] = NULL;
4571
4572
0
  return dst_count;
4573
0
}
4574
4575
/* Don't output section symbols for sections that are not going to be
4576
   output, that are duplicates or there is no BFD section.  */
4577
4578
static bool
4579
ignore_section_sym (bfd *abfd, asymbol *sym)
4580
0
{
4581
0
  elf_symbol_type *type_ptr;
4582
4583
0
  if (sym == NULL)
4584
0
    return false;
4585
4586
0
  if ((sym->flags & BSF_SECTION_SYM) == 0)
4587
0
    return false;
4588
4589
  /* Ignore the section symbol if it isn't used.  */
4590
0
  if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4591
0
    return true;
4592
4593
0
  if (sym->section == NULL)
4594
0
    return true;
4595
4596
0
  type_ptr = elf_symbol_from (sym);
4597
0
  return ((type_ptr != NULL
4598
0
     && type_ptr->internal_elf_sym.st_shndx != 0
4599
0
     && bfd_is_abs_section (sym->section))
4600
0
    || !(sym->section->owner == abfd
4601
0
         || (sym->section->output_section != NULL
4602
0
       && sym->section->output_section->owner == abfd
4603
0
       && sym->section->output_offset == 0)
4604
0
         || bfd_is_abs_section (sym->section)));
4605
0
}
4606
4607
/* Map symbol from it's internal number to the external number, moving
4608
   all local symbols to be at the head of the list.  */
4609
4610
static bool
4611
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4612
0
{
4613
0
  unsigned int symcount = bfd_get_symcount (abfd);
4614
0
  asymbol **syms = bfd_get_outsymbols (abfd);
4615
0
  asymbol **sect_syms;
4616
0
  unsigned int num_locals = 0;
4617
0
  unsigned int num_globals = 0;
4618
0
  unsigned int num_locals2 = 0;
4619
0
  unsigned int num_globals2 = 0;
4620
0
  unsigned int max_index = 0;
4621
0
  unsigned int idx;
4622
0
  asection *asect;
4623
0
  asymbol **new_syms;
4624
0
  size_t amt;
4625
4626
#ifdef DEBUG
4627
  fprintf (stderr, "elf_map_symbols\n");
4628
  fflush (stderr);
4629
#endif
4630
4631
0
  for (asect = abfd->sections; asect; asect = asect->next)
4632
0
    {
4633
0
      if (max_index < asect->index)
4634
0
  max_index = asect->index;
4635
0
    }
4636
4637
0
  max_index++;
4638
0
  amt = max_index * sizeof (asymbol *);
4639
0
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4640
0
  if (sect_syms == NULL)
4641
0
    return false;
4642
0
  elf_section_syms (abfd) = sect_syms;
4643
0
  elf_num_section_syms (abfd) = max_index;
4644
4645
  /* Init sect_syms entries for any section symbols we have already
4646
     decided to output.  */
4647
0
  for (idx = 0; idx < symcount; idx++)
4648
0
    {
4649
0
      asymbol *sym = syms[idx];
4650
4651
0
      if ((sym->flags & BSF_SECTION_SYM) != 0
4652
0
    && sym->value == 0
4653
0
    && !ignore_section_sym (abfd, sym)
4654
0
    && !bfd_is_abs_section (sym->section))
4655
0
  {
4656
0
    asection *sec = sym->section;
4657
4658
0
    if (sec->owner != abfd)
4659
0
      sec = sec->output_section;
4660
4661
0
    sect_syms[sec->index] = syms[idx];
4662
0
  }
4663
0
    }
4664
4665
  /* Classify all of the symbols.  */
4666
0
  for (idx = 0; idx < symcount; idx++)
4667
0
    {
4668
0
      if (sym_is_global (abfd, syms[idx]))
4669
0
  num_globals++;
4670
0
      else if (!ignore_section_sym (abfd, syms[idx]))
4671
0
  num_locals++;
4672
0
    }
4673
4674
  /* We will be adding a section symbol for each normal BFD section.  Most
4675
     sections will already have a section symbol in outsymbols, but
4676
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4677
     at least in that case.  */
4678
0
  for (asect = abfd->sections; asect; asect = asect->next)
4679
0
    {
4680
0
      asymbol *sym = asect->symbol;
4681
      /* Don't include ignored section symbols.  */
4682
0
      if (!ignore_section_sym (abfd, sym)
4683
0
    && sect_syms[asect->index] == NULL)
4684
0
  {
4685
0
    if (!sym_is_global (abfd, asect->symbol))
4686
0
      num_locals++;
4687
0
    else
4688
0
      num_globals++;
4689
0
  }
4690
0
    }
4691
4692
  /* Now sort the symbols so the local symbols are first.  */
4693
0
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4694
0
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4695
0
  if (new_syms == NULL)
4696
0
    return false;
4697
4698
0
  for (idx = 0; idx < symcount; idx++)
4699
0
    {
4700
0
      asymbol *sym = syms[idx];
4701
0
      unsigned int i;
4702
4703
0
      if (sym_is_global (abfd, sym))
4704
0
  i = num_locals + num_globals2++;
4705
      /* Don't include ignored section symbols.  */
4706
0
      else if (!ignore_section_sym (abfd, sym))
4707
0
  i = num_locals2++;
4708
0
      else
4709
0
  continue;
4710
0
      new_syms[i] = sym;
4711
0
      sym->udata.i = i + 1;
4712
0
    }
4713
0
  for (asect = abfd->sections; asect; asect = asect->next)
4714
0
    {
4715
0
      asymbol *sym = asect->symbol;
4716
0
      if (!ignore_section_sym (abfd, sym)
4717
0
    && sect_syms[asect->index] == NULL)
4718
0
  {
4719
0
    unsigned int i;
4720
4721
0
    sect_syms[asect->index] = sym;
4722
0
    if (!sym_is_global (abfd, sym))
4723
0
      i = num_locals2++;
4724
0
    else
4725
0
      i = num_locals + num_globals2++;
4726
0
    new_syms[i] = sym;
4727
0
    sym->udata.i = i + 1;
4728
0
  }
4729
0
    }
4730
4731
0
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4732
4733
0
  *pnum_locals = num_locals;
4734
0
  return true;
4735
0
}
4736
4737
/* Align to the maximum file alignment that could be required for any
4738
   ELF data structure.  */
4739
4740
static inline file_ptr
4741
align_file_position (file_ptr off, int align)
4742
0
{
4743
0
  return (off + align - 1) & ~(align - 1);
4744
0
}
4745
4746
/* Assign a file position to a section, optionally aligning to the
4747
   required section alignment.  */
4748
4749
file_ptr
4750
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4751
             file_ptr offset,
4752
             bool align)
4753
0
{
4754
0
  if (align && i_shdrp->sh_addralign > 1)
4755
0
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4756
0
  i_shdrp->sh_offset = offset;
4757
0
  if (i_shdrp->bfd_section != NULL)
4758
0
    i_shdrp->bfd_section->filepos = offset;
4759
0
  if (i_shdrp->sh_type != SHT_NOBITS)
4760
0
    offset += i_shdrp->sh_size;
4761
0
  return offset;
4762
0
}
4763
4764
/* Compute the file positions we are going to put the sections at, and
4765
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4766
   is not NULL, this is being called by the ELF backend linker.  */
4767
4768
bool
4769
_bfd_elf_compute_section_file_positions (bfd *abfd,
4770
           struct bfd_link_info *link_info)
4771
0
{
4772
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4773
0
  struct fake_section_arg fsargs;
4774
0
  bool failed;
4775
0
  struct elf_strtab_hash *strtab = NULL;
4776
0
  Elf_Internal_Shdr *shstrtab_hdr;
4777
0
  bool need_symtab;
4778
4779
0
  if (abfd->output_has_begun)
4780
0
    return true;
4781
4782
  /* Do any elf backend specific processing first.  */
4783
0
  if (bed->elf_backend_begin_write_processing)
4784
0
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4785
4786
0
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4787
0
    return false;
4788
4789
0
  fsargs.failed = false;
4790
0
  fsargs.link_info = link_info;
4791
0
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4792
0
  if (fsargs.failed)
4793
0
    return false;
4794
4795
0
  if (!assign_section_numbers (abfd, link_info))
4796
0
    return false;
4797
4798
  /* The backend linker builds symbol table information itself.  */
4799
0
  need_symtab = (link_info == NULL
4800
0
     && (bfd_get_symcount (abfd) > 0
4801
0
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4802
0
       == HAS_RELOC)));
4803
0
  if (need_symtab)
4804
0
    {
4805
      /* Non-zero if doing a relocatable link.  */
4806
0
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4807
4808
0
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4809
0
  return false;
4810
0
    }
4811
4812
0
  failed = false;
4813
0
  if (link_info == NULL)
4814
0
    {
4815
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4816
0
      if (failed)
4817
0
  goto err_free_strtab;
4818
0
    }
4819
4820
0
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4821
  /* sh_name was set in init_file_header.  */
4822
0
  shstrtab_hdr->sh_type = SHT_STRTAB;
4823
0
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4824
0
  shstrtab_hdr->sh_addr = 0;
4825
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4826
0
  shstrtab_hdr->sh_entsize = 0;
4827
0
  shstrtab_hdr->sh_link = 0;
4828
0
  shstrtab_hdr->sh_info = 0;
4829
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4830
0
  shstrtab_hdr->sh_addralign = 1;
4831
4832
0
  if (!assign_file_positions_except_relocs (abfd, link_info))
4833
0
    goto err_free_strtab;
4834
4835
0
  if (strtab != NULL)
4836
0
    {
4837
0
      file_ptr off;
4838
0
      Elf_Internal_Shdr *hdr;
4839
4840
0
      off = elf_next_file_pos (abfd);
4841
4842
0
      hdr = & elf_symtab_hdr (abfd);
4843
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4844
4845
0
      if (elf_symtab_shndx_list (abfd) != NULL)
4846
0
  {
4847
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4848
0
    if (hdr->sh_size != 0)
4849
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4850
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4851
0
  }
4852
4853
0
      hdr = &elf_tdata (abfd)->strtab_hdr;
4854
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4855
4856
0
      elf_next_file_pos (abfd) = off;
4857
4858
      /* Now that we know where the .strtab section goes, write it
4859
   out.  */
4860
0
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4861
0
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4862
0
  goto err_free_strtab;
4863
0
      _bfd_elf_strtab_free (strtab);
4864
0
    }
4865
4866
0
  abfd->output_has_begun = true;
4867
0
  return true;
4868
4869
0
 err_free_strtab:
4870
0
  if (strtab != NULL)
4871
0
    _bfd_elf_strtab_free (strtab);
4872
0
  return false;
4873
0
}
4874
4875
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4876
   function effectively returns whether --eh-frame-hdr is given on the
4877
   command line.  After size_dynamic_sections the result reflects
4878
   whether .eh_frame_hdr will actually be output (sizing isn't done
4879
   until ldemul_after_allocation).  */
4880
4881
static asection *
4882
elf_eh_frame_hdr (const struct bfd_link_info *info)
4883
0
{
4884
0
  if (info != NULL && is_elf_hash_table (info->hash))
4885
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4886
0
  return NULL;
4887
0
}
4888
4889
/* Make an initial estimate of the size of the program header.  If we
4890
   get the number wrong here, we'll redo section placement.  */
4891
4892
static bfd_size_type
4893
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4894
0
{
4895
0
  size_t segs;
4896
0
  asection *s;
4897
0
  const struct elf_backend_data *bed;
4898
4899
  /* Assume we will need exactly two PT_LOAD segments: one for text
4900
     and one for data.  */
4901
0
  segs = 2;
4902
4903
0
  s = bfd_get_section_by_name (abfd, ".interp");
4904
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4905
0
    {
4906
      /* If we have a loadable interpreter section, we need a
4907
   PT_INTERP segment.  In this case, assume we also need a
4908
   PT_PHDR segment, although that may not be true for all
4909
   targets.  */
4910
0
      segs += 2;
4911
0
    }
4912
4913
0
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4914
0
    {
4915
      /* We need a PT_DYNAMIC segment.  */
4916
0
      ++segs;
4917
0
    }
4918
4919
0
  if (info != NULL && info->relro)
4920
0
    {
4921
      /* We need a PT_GNU_RELRO segment.  */
4922
0
      ++segs;
4923
0
    }
4924
4925
0
  if (elf_eh_frame_hdr (info))
4926
0
    {
4927
      /* We need a PT_GNU_EH_FRAME segment.  */
4928
0
      ++segs;
4929
0
    }
4930
4931
0
  if (elf_stack_flags (abfd))
4932
0
    {
4933
      /* We need a PT_GNU_STACK segment.  */
4934
0
      ++segs;
4935
0
    }
4936
4937
0
  if (elf_sframe (abfd))
4938
0
    {
4939
      /* We need a PT_GNU_SFRAME segment.  */
4940
0
      ++segs;
4941
0
    }
4942
4943
0
  s = bfd_get_section_by_name (abfd,
4944
0
             NOTE_GNU_PROPERTY_SECTION_NAME);
4945
0
  if (s != NULL && s->size != 0)
4946
0
    {
4947
      /* We need a PT_GNU_PROPERTY segment.  */
4948
0
      ++segs;
4949
0
    }
4950
4951
0
  for (s = abfd->sections; s != NULL; s = s->next)
4952
0
    {
4953
0
      if ((s->flags & SEC_LOAD) != 0
4954
0
    && elf_section_type (s) == SHT_NOTE)
4955
0
  {
4956
0
    unsigned int alignment_power;
4957
    /* We need a PT_NOTE segment.  */
4958
0
    ++segs;
4959
    /* Try to create just one PT_NOTE segment for all adjacent
4960
       loadable SHT_NOTE sections.  gABI requires that within a
4961
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4962
       each note should have the same alignment.  So we check
4963
       whether the sections are correctly aligned.  */
4964
0
    alignment_power = s->alignment_power;
4965
0
    while (s->next != NULL
4966
0
     && s->next->alignment_power == alignment_power
4967
0
     && (s->next->flags & SEC_LOAD) != 0
4968
0
     && elf_section_type (s->next) == SHT_NOTE)
4969
0
      s = s->next;
4970
0
  }
4971
0
    }
4972
4973
0
  for (s = abfd->sections; s != NULL; s = s->next)
4974
0
    {
4975
0
      if (s->flags & SEC_THREAD_LOCAL)
4976
0
  {
4977
    /* We need a PT_TLS segment.  */
4978
0
    ++segs;
4979
0
    break;
4980
0
  }
4981
0
    }
4982
4983
0
  bed = get_elf_backend_data (abfd);
4984
4985
0
  if ((abfd->flags & D_PAGED) != 0
4986
0
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4987
0
    {
4988
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4989
0
      bfd_vma commonpagesize;
4990
0
      unsigned int page_align_power;
4991
4992
0
      if (info != NULL)
4993
0
  commonpagesize = info->commonpagesize;
4994
0
      else
4995
0
  commonpagesize = bed->commonpagesize;
4996
0
      page_align_power = bfd_log2 (commonpagesize);
4997
0
      for (s = abfd->sections; s != NULL; s = s->next)
4998
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4999
0
    {
5000
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5001
0
        {
5002
0
    _bfd_error_handler
5003
      /* xgettext:c-format */
5004
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
5005
0
         "sh_info field: %d"),
5006
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
5007
0
    continue;
5008
0
        }
5009
      /* Align mbind section to page size.  */
5010
0
      if (s->alignment_power < page_align_power)
5011
0
        s->alignment_power = page_align_power;
5012
0
      segs ++;
5013
0
    }
5014
0
    }
5015
5016
  /* Let the backend count up any program headers it might need.  */
5017
0
  if (bed->elf_backend_additional_program_headers)
5018
0
    {
5019
0
      int a;
5020
5021
0
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5022
0
      if (a == -1)
5023
0
  abort ();
5024
0
      segs += a;
5025
0
    }
5026
5027
0
  return segs * bed->s->sizeof_phdr;
5028
0
}
5029
5030
/* Find the segment that contains the output_section of section.  */
5031
5032
Elf_Internal_Phdr *
5033
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5034
0
{
5035
0
  struct elf_segment_map *m;
5036
0
  Elf_Internal_Phdr *p;
5037
5038
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5039
0
       m != NULL;
5040
0
       m = m->next, p++)
5041
0
    {
5042
0
      int i;
5043
5044
0
      for (i = m->count - 1; i >= 0; i--)
5045
0
  if (m->sections[i] == section)
5046
0
    return p;
5047
0
    }
5048
5049
0
  return NULL;
5050
0
}
5051
5052
/* Create a mapping from a set of sections to a program segment.  */
5053
5054
static struct elf_segment_map *
5055
make_mapping (bfd *abfd,
5056
        asection **sections,
5057
        unsigned int from,
5058
        unsigned int to,
5059
        bool phdr)
5060
0
{
5061
0
  struct elf_segment_map *m;
5062
0
  unsigned int i;
5063
0
  asection **hdrpp;
5064
0
  size_t amt;
5065
5066
0
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5067
0
  amt += (to - from) * sizeof (asection *);
5068
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5069
0
  if (m == NULL)
5070
0
    return NULL;
5071
0
  m->next = NULL;
5072
0
  m->p_type = PT_LOAD;
5073
0
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5074
0
    m->sections[i - from] = *hdrpp;
5075
0
  m->count = to - from;
5076
5077
0
  if (from == 0 && phdr)
5078
0
    {
5079
      /* Include the headers in the first PT_LOAD segment.  */
5080
0
      m->includes_filehdr = 1;
5081
0
      m->includes_phdrs = 1;
5082
0
    }
5083
5084
0
  return m;
5085
0
}
5086
5087
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5088
   on failure.  */
5089
5090
struct elf_segment_map *
5091
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5092
0
{
5093
0
  struct elf_segment_map *m;
5094
5095
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
5096
0
               sizeof (struct elf_segment_map));
5097
0
  if (m == NULL)
5098
0
    return NULL;
5099
0
  m->next = NULL;
5100
0
  m->p_type = PT_DYNAMIC;
5101
0
  m->count = 1;
5102
0
  m->sections[0] = dynsec;
5103
5104
0
  return m;
5105
0
}
5106
5107
/* Possibly add or remove segments from the segment map.  */
5108
5109
static bool
5110
elf_modify_segment_map (bfd *abfd,
5111
      struct bfd_link_info *info,
5112
      bool remove_empty_load)
5113
0
{
5114
0
  struct elf_segment_map **m;
5115
0
  const struct elf_backend_data *bed;
5116
5117
  /* The placement algorithm assumes that non allocated sections are
5118
     not in PT_LOAD segments.  We ensure this here by removing such
5119
     sections from the segment map.  We also remove excluded
5120
     sections.  Finally, any PT_LOAD segment without sections is
5121
     removed.  */
5122
0
  m = &elf_seg_map (abfd);
5123
0
  while (*m)
5124
0
    {
5125
0
      unsigned int i, new_count;
5126
5127
0
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5128
0
  {
5129
0
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5130
0
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5131
0
      || (*m)->p_type != PT_LOAD))
5132
0
      {
5133
0
        (*m)->sections[new_count] = (*m)->sections[i];
5134
0
        new_count++;
5135
0
      }
5136
0
  }
5137
0
      (*m)->count = new_count;
5138
5139
0
      if (remove_empty_load
5140
0
    && (*m)->p_type == PT_LOAD
5141
0
    && (*m)->count == 0
5142
0
    && !(*m)->includes_phdrs)
5143
0
  *m = (*m)->next;
5144
0
      else
5145
0
  m = &(*m)->next;
5146
0
    }
5147
5148
0
  bed = get_elf_backend_data (abfd);
5149
0
  if (bed->elf_backend_modify_segment_map != NULL)
5150
0
    {
5151
0
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5152
0
  return false;
5153
0
    }
5154
5155
0
  return true;
5156
0
}
5157
5158
#define IS_TBSS(s) \
5159
0
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5160
5161
/* Set up a mapping from BFD sections to program segments.  Update
5162
   NEED_LAYOUT if the section layout is changed.  */
5163
5164
bool
5165
_bfd_elf_map_sections_to_segments (bfd *abfd,
5166
           struct bfd_link_info *info,
5167
           bool *need_layout)
5168
0
{
5169
0
  unsigned int count;
5170
0
  struct elf_segment_map *m;
5171
0
  asection **sections = NULL;
5172
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5173
0
  bool no_user_phdrs;
5174
5175
0
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5176
5177
0
  if (info != NULL)
5178
0
    {
5179
0
      info->user_phdrs = !no_user_phdrs;
5180
5181
      /* Size the relative relocations if DT_RELR is enabled.  */
5182
0
      if (info->enable_dt_relr
5183
0
    && need_layout != NULL
5184
0
    && bed->size_relative_relocs
5185
0
    && !bed->size_relative_relocs (info, need_layout))
5186
0
  info->callbacks->einfo
5187
0
    (_("%F%P: failed to size relative relocations\n"));
5188
0
    }
5189
5190
0
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5191
0
    {
5192
0
      asection *s;
5193
0
      unsigned int i;
5194
0
      struct elf_segment_map *mfirst;
5195
0
      struct elf_segment_map **pm;
5196
0
      asection *last_hdr;
5197
0
      bfd_vma last_size;
5198
0
      unsigned int hdr_index;
5199
0
      bfd_vma maxpagesize;
5200
0
      asection **hdrpp;
5201
0
      bool phdr_in_segment;
5202
0
      bool writable;
5203
0
      bool executable;
5204
0
      unsigned int tls_count = 0;
5205
0
      asection *first_tls = NULL;
5206
0
      asection *first_mbind = NULL;
5207
0
      asection *dynsec, *eh_frame_hdr;
5208
0
      asection *sframe;
5209
0
      size_t amt;
5210
0
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5211
0
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5212
0
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5213
5214
      /* Select the allocated sections, and sort them.  */
5215
5216
0
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5217
0
      sections = (asection **) bfd_malloc (amt);
5218
0
      if (sections == NULL)
5219
0
  goto error_return;
5220
5221
      /* Calculate top address, avoiding undefined behaviour of shift
5222
   left operator when shift count is equal to size of type
5223
   being shifted.  */
5224
0
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5225
0
      addr_mask = (addr_mask << 1) + 1;
5226
5227
0
      i = 0;
5228
0
      for (s = abfd->sections; s != NULL; s = s->next)
5229
0
  {
5230
0
    if ((s->flags & SEC_ALLOC) != 0)
5231
0
      {
5232
        /* target_index is unused until bfd_elf_final_link
5233
     starts output of section symbols.  Use it to make
5234
     qsort stable.  */
5235
0
        s->target_index = i;
5236
0
        sections[i] = s;
5237
0
        ++i;
5238
        /* A wrapping section potentially clashes with header.  */
5239
0
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5240
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5241
0
      }
5242
0
  }
5243
0
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5244
0
      count = i;
5245
5246
0
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5247
5248
0
      phdr_size = elf_program_header_size (abfd);
5249
0
      if (phdr_size == (bfd_size_type) -1)
5250
0
  phdr_size = get_program_header_size (abfd, info);
5251
0
      phdr_size += bed->s->sizeof_ehdr;
5252
      /* phdr_size is compared to LMA values which are in bytes.  */
5253
0
      phdr_size /= opb;
5254
0
      if (info != NULL)
5255
0
  maxpagesize = info->maxpagesize;
5256
0
      else
5257
0
  maxpagesize = bed->maxpagesize;
5258
0
      if (maxpagesize == 0)
5259
0
  maxpagesize = 1;
5260
0
      phdr_in_segment = info != NULL && info->load_phdrs;
5261
0
      if (count != 0
5262
0
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5263
0
        >= (phdr_size & (maxpagesize - 1))))
5264
  /* For compatibility with old scripts that may not be using
5265
     SIZEOF_HEADERS, add headers when it looks like space has
5266
     been left for them.  */
5267
0
  phdr_in_segment = true;
5268
5269
      /* Build the mapping.  */
5270
0
      mfirst = NULL;
5271
0
      pm = &mfirst;
5272
5273
      /* If we have a .interp section, then create a PT_PHDR segment for
5274
   the program headers and a PT_INTERP segment for the .interp
5275
   section.  */
5276
0
      s = bfd_get_section_by_name (abfd, ".interp");
5277
0
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5278
0
  {
5279
0
    amt = sizeof (struct elf_segment_map);
5280
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5281
0
    if (m == NULL)
5282
0
      goto error_return;
5283
0
    m->next = NULL;
5284
0
    m->p_type = PT_PHDR;
5285
0
    m->p_flags = PF_R;
5286
0
    m->p_flags_valid = 1;
5287
0
    m->includes_phdrs = 1;
5288
0
    phdr_in_segment = true;
5289
0
    *pm = m;
5290
0
    pm = &m->next;
5291
5292
0
    amt = sizeof (struct elf_segment_map);
5293
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5294
0
    if (m == NULL)
5295
0
      goto error_return;
5296
0
    m->next = NULL;
5297
0
    m->p_type = PT_INTERP;
5298
0
    m->count = 1;
5299
0
    m->sections[0] = s;
5300
5301
0
    *pm = m;
5302
0
    pm = &m->next;
5303
0
  }
5304
5305
      /* Look through the sections.  We put sections in the same program
5306
   segment when the start of the second section can be placed within
5307
   a few bytes of the end of the first section.  */
5308
0
      last_hdr = NULL;
5309
0
      last_size = 0;
5310
0
      hdr_index = 0;
5311
0
      writable = false;
5312
0
      executable = false;
5313
0
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5314
0
      if (dynsec != NULL
5315
0
    && (dynsec->flags & SEC_LOAD) == 0)
5316
0
  dynsec = NULL;
5317
5318
0
      if ((abfd->flags & D_PAGED) == 0)
5319
0
  phdr_in_segment = false;
5320
5321
      /* Deal with -Ttext or something similar such that the first section
5322
   is not adjacent to the program headers.  This is an
5323
   approximation, since at this point we don't know exactly how many
5324
   program headers we will need.  */
5325
0
      if (phdr_in_segment && count > 0)
5326
0
  {
5327
0
    bfd_vma phdr_lma;  /* Bytes.  */
5328
0
    bool separate_phdr = false;
5329
5330
0
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5331
0
    if (info != NULL
5332
0
        && info->separate_code
5333
0
        && (sections[0]->flags & SEC_CODE) != 0)
5334
0
      {
5335
        /* If data sections should be separate from code and
5336
     thus not executable, and the first section is
5337
     executable then put the file and program headers in
5338
     their own PT_LOAD.  */
5339
0
        separate_phdr = true;
5340
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5341
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5342
0
    {
5343
      /* The file and program headers are currently on the
5344
         same page as the first section.  Put them on the
5345
         previous page if we can.  */
5346
0
      if (phdr_lma >= maxpagesize)
5347
0
        phdr_lma -= maxpagesize;
5348
0
      else
5349
0
        separate_phdr = false;
5350
0
    }
5351
0
      }
5352
0
    if ((sections[0]->lma & addr_mask) < phdr_lma
5353
0
        || (sections[0]->lma & addr_mask) < phdr_size)
5354
      /* If file and program headers would be placed at the end
5355
         of memory then it's probably better to omit them.  */
5356
0
      phdr_in_segment = false;
5357
0
    else if (phdr_lma < wrap_to)
5358
      /* If a section wraps around to where we'll be placing
5359
         file and program headers, then the headers will be
5360
         overwritten.  */
5361
0
      phdr_in_segment = false;
5362
0
    else if (separate_phdr)
5363
0
      {
5364
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5365
0
        if (m == NULL)
5366
0
    goto error_return;
5367
0
        m->p_paddr = phdr_lma * opb;
5368
0
        m->p_vaddr_offset
5369
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5370
0
        m->p_paddr_valid = 1;
5371
0
        *pm = m;
5372
0
        pm = &m->next;
5373
0
        phdr_in_segment = false;
5374
0
      }
5375
0
  }
5376
5377
0
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5378
0
  {
5379
0
    asection *hdr;
5380
0
    bool new_segment;
5381
5382
0
    hdr = *hdrpp;
5383
5384
    /* See if this section and the last one will fit in the same
5385
       segment.  */
5386
5387
0
    if (last_hdr == NULL)
5388
0
      {
5389
        /* If we don't have a segment yet, then we don't need a new
5390
     one (we build the last one after this loop).  */
5391
0
        new_segment = false;
5392
0
      }
5393
0
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5394
0
      {
5395
        /* If this section has a different relation between the
5396
     virtual address and the load address, then we need a new
5397
     segment.  */
5398
0
        new_segment = true;
5399
0
      }
5400
0
    else if (hdr->lma < last_hdr->lma + last_size
5401
0
       || last_hdr->lma + last_size < last_hdr->lma)
5402
0
      {
5403
        /* If this section has a load address that makes it overlap
5404
     the previous section, then we need a new segment.  */
5405
0
        new_segment = true;
5406
0
      }
5407
0
    else if ((abfd->flags & D_PAGED) != 0
5408
0
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5409
0
           == (hdr->lma & -maxpagesize)))
5410
0
      {
5411
        /* If we are demand paged then we can't map two disk
5412
     pages onto the same memory page.  */
5413
0
        new_segment = false;
5414
0
      }
5415
    /* In the next test we have to be careful when last_hdr->lma is close
5416
       to the end of the address space.  If the aligned address wraps
5417
       around to the start of the address space, then there are no more
5418
       pages left in memory and it is OK to assume that the current
5419
       section can be included in the current segment.  */
5420
0
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5421
0
        + maxpagesize > last_hdr->lma)
5422
0
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5423
0
           + maxpagesize <= hdr->lma))
5424
0
      {
5425
        /* If putting this section in this segment would force us to
5426
     skip a page in the segment, then we need a new segment.  */
5427
0
        new_segment = true;
5428
0
      }
5429
0
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5430
0
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5431
0
      {
5432
        /* We don't want to put a loaded section after a
5433
     nonloaded (ie. bss style) section in the same segment
5434
     as that will force the non-loaded section to be loaded.
5435
     Consider .tbss sections as loaded for this purpose.  */
5436
0
        new_segment = true;
5437
0
      }
5438
0
    else if ((abfd->flags & D_PAGED) == 0)
5439
0
      {
5440
        /* If the file is not demand paged, which means that we
5441
     don't require the sections to be correctly aligned in the
5442
     file, then there is no other reason for a new segment.  */
5443
0
        new_segment = false;
5444
0
      }
5445
0
    else if (info != NULL
5446
0
       && info->separate_code
5447
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5448
0
      {
5449
0
        new_segment = true;
5450
0
      }
5451
0
    else if (! writable
5452
0
       && (hdr->flags & SEC_READONLY) == 0)
5453
0
      {
5454
        /* We don't want to put a writable section in a read only
5455
     segment.  */
5456
0
        new_segment = true;
5457
0
      }
5458
0
    else
5459
0
      {
5460
        /* Otherwise, we can use the same segment.  */
5461
0
        new_segment = false;
5462
0
      }
5463
5464
    /* Allow interested parties a chance to override our decision.  */
5465
0
    if (last_hdr != NULL
5466
0
        && info != NULL
5467
0
        && info->callbacks->override_segment_assignment != NULL)
5468
0
      new_segment
5469
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5470
0
                    last_hdr,
5471
0
                    new_segment);
5472
5473
0
    if (! new_segment)
5474
0
      {
5475
0
        if ((hdr->flags & SEC_READONLY) == 0)
5476
0
    writable = true;
5477
0
        if ((hdr->flags & SEC_CODE) != 0)
5478
0
    executable = true;
5479
0
        last_hdr = hdr;
5480
        /* .tbss sections effectively have zero size.  */
5481
0
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5482
0
        continue;
5483
0
      }
5484
5485
    /* We need a new program segment.  We must create a new program
5486
       header holding all the sections from hdr_index until hdr.  */
5487
5488
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5489
0
    if (m == NULL)
5490
0
      goto error_return;
5491
5492
0
    *pm = m;
5493
0
    pm = &m->next;
5494
5495
0
    if ((hdr->flags & SEC_READONLY) == 0)
5496
0
      writable = true;
5497
0
    else
5498
0
      writable = false;
5499
5500
0
    if ((hdr->flags & SEC_CODE) == 0)
5501
0
      executable = false;
5502
0
    else
5503
0
      executable = true;
5504
5505
0
    last_hdr = hdr;
5506
    /* .tbss sections effectively have zero size.  */
5507
0
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5508
0
    hdr_index = i;
5509
0
    phdr_in_segment = false;
5510
0
  }
5511
5512
      /* Create a final PT_LOAD program segment, but not if it's just
5513
   for .tbss.  */
5514
0
      if (last_hdr != NULL
5515
0
    && (i - hdr_index != 1
5516
0
        || !IS_TBSS (last_hdr)))
5517
0
  {
5518
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5519
0
    if (m == NULL)
5520
0
      goto error_return;
5521
5522
0
    *pm = m;
5523
0
    pm = &m->next;
5524
0
  }
5525
5526
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5527
0
      if (dynsec != NULL)
5528
0
  {
5529
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5530
0
    if (m == NULL)
5531
0
      goto error_return;
5532
0
    *pm = m;
5533
0
    pm = &m->next;
5534
0
  }
5535
5536
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5537
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5538
   because if we link together nonloadable .note sections and
5539
   loadable .note sections, we will generate two .note sections
5540
   in the output file.  */
5541
0
      for (s = abfd->sections; s != NULL; s = s->next)
5542
0
  {
5543
0
    if ((s->flags & SEC_LOAD) != 0
5544
0
        && elf_section_type (s) == SHT_NOTE)
5545
0
      {
5546
0
        asection *s2;
5547
0
        unsigned int alignment_power = s->alignment_power;
5548
5549
0
        count = 1;
5550
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5551
0
    {
5552
0
      if (s2->next->alignment_power == alignment_power
5553
0
          && (s2->next->flags & SEC_LOAD) != 0
5554
0
          && elf_section_type (s2->next) == SHT_NOTE
5555
0
          && align_power (s2->lma + s2->size / opb,
5556
0
              alignment_power)
5557
0
          == s2->next->lma)
5558
0
        count++;
5559
0
      else
5560
0
        break;
5561
0
    }
5562
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5563
0
        amt += count * sizeof (asection *);
5564
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5565
0
        if (m == NULL)
5566
0
    goto error_return;
5567
0
        m->next = NULL;
5568
0
        m->p_type = PT_NOTE;
5569
0
        m->count = count;
5570
0
        while (count > 1)
5571
0
    {
5572
0
      m->sections[m->count - count--] = s;
5573
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5574
0
      s = s->next;
5575
0
    }
5576
0
        m->sections[m->count - 1] = s;
5577
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5578
0
        *pm = m;
5579
0
        pm = &m->next;
5580
0
      }
5581
0
    if (s->flags & SEC_THREAD_LOCAL)
5582
0
      {
5583
0
        if (! tls_count)
5584
0
    first_tls = s;
5585
0
        tls_count++;
5586
0
      }
5587
0
    if (first_mbind == NULL
5588
0
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5589
0
      first_mbind = s;
5590
0
  }
5591
5592
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5593
0
      if (tls_count > 0)
5594
0
  {
5595
0
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5596
0
    amt += tls_count * sizeof (asection *);
5597
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5598
0
    if (m == NULL)
5599
0
      goto error_return;
5600
0
    m->next = NULL;
5601
0
    m->p_type = PT_TLS;
5602
0
    m->count = tls_count;
5603
    /* Mandated PF_R.  */
5604
0
    m->p_flags = PF_R;
5605
0
    m->p_flags_valid = 1;
5606
0
    s = first_tls;
5607
0
    for (i = 0; i < tls_count; ++i)
5608
0
      {
5609
0
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5610
0
    {
5611
0
      _bfd_error_handler
5612
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5613
0
      s = first_tls;
5614
0
      i = 0;
5615
0
      while (i < tls_count)
5616
0
        {
5617
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5618
0
      {
5619
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5620
0
        i++;
5621
0
      }
5622
0
          else
5623
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5624
0
          s = s->next;
5625
0
        }
5626
0
      bfd_set_error (bfd_error_bad_value);
5627
0
      goto error_return;
5628
0
    }
5629
0
        m->sections[i] = s;
5630
0
        s = s->next;
5631
0
      }
5632
5633
0
    *pm = m;
5634
0
    pm = &m->next;
5635
0
  }
5636
5637
0
      if (first_mbind
5638
0
    && (abfd->flags & D_PAGED) != 0
5639
0
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5640
0
  for (s = first_mbind; s != NULL; s = s->next)
5641
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5642
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5643
0
      {
5644
        /* Mandated PF_R.  */
5645
0
        unsigned long p_flags = PF_R;
5646
0
        if ((s->flags & SEC_READONLY) == 0)
5647
0
    p_flags |= PF_W;
5648
0
        if ((s->flags & SEC_CODE) != 0)
5649
0
    p_flags |= PF_X;
5650
5651
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5652
0
        m = bfd_zalloc (abfd, amt);
5653
0
        if (m == NULL)
5654
0
    goto error_return;
5655
0
        m->next = NULL;
5656
0
        m->p_type = (PT_GNU_MBIND_LO
5657
0
         + elf_section_data (s)->this_hdr.sh_info);
5658
0
        m->count = 1;
5659
0
        m->p_flags_valid = 1;
5660
0
        m->sections[0] = s;
5661
0
        m->p_flags = p_flags;
5662
5663
0
        *pm = m;
5664
0
        pm = &m->next;
5665
0
      }
5666
5667
0
      s = bfd_get_section_by_name (abfd,
5668
0
           NOTE_GNU_PROPERTY_SECTION_NAME);
5669
0
      if (s != NULL && s->size != 0)
5670
0
  {
5671
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5672
0
    m = bfd_zalloc (abfd, amt);
5673
0
    if (m == NULL)
5674
0
      goto error_return;
5675
0
    m->next = NULL;
5676
0
    m->p_type = PT_GNU_PROPERTY;
5677
0
    m->count = 1;
5678
0
    m->p_flags_valid = 1;
5679
0
    m->sections[0] = s;
5680
0
    m->p_flags = PF_R;
5681
0
    *pm = m;
5682
0
    pm = &m->next;
5683
0
  }
5684
5685
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5686
   segment.  */
5687
0
      eh_frame_hdr = elf_eh_frame_hdr (info);
5688
0
      if (eh_frame_hdr != NULL
5689
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5690
0
  {
5691
0
    amt = sizeof (struct elf_segment_map);
5692
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5693
0
    if (m == NULL)
5694
0
      goto error_return;
5695
0
    m->next = NULL;
5696
0
    m->p_type = PT_GNU_EH_FRAME;
5697
0
    m->count = 1;
5698
0
    m->sections[0] = eh_frame_hdr->output_section;
5699
5700
0
    *pm = m;
5701
0
    pm = &m->next;
5702
0
  }
5703
5704
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5705
   segment.  */
5706
0
      sframe = elf_sframe (abfd);
5707
0
      if (sframe != NULL
5708
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5709
0
    && sframe->size != 0)
5710
0
  {
5711
0
    amt = sizeof (struct elf_segment_map);
5712
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5713
0
    if (m == NULL)
5714
0
      goto error_return;
5715
0
    m->next = NULL;
5716
0
    m->p_type = PT_GNU_SFRAME;
5717
0
    m->count = 1;
5718
0
    m->sections[0] = sframe->output_section;
5719
5720
0
    *pm = m;
5721
0
    pm = &m->next;
5722
0
  }
5723
5724
0
      if (elf_stack_flags (abfd))
5725
0
  {
5726
0
    amt = sizeof (struct elf_segment_map);
5727
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5728
0
    if (m == NULL)
5729
0
      goto error_return;
5730
0
    m->next = NULL;
5731
0
    m->p_type = PT_GNU_STACK;
5732
0
    m->p_flags = elf_stack_flags (abfd);
5733
0
    m->p_align = bed->stack_align;
5734
0
    m->p_flags_valid = 1;
5735
0
    m->p_align_valid = m->p_align != 0;
5736
0
    if (info->stacksize > 0)
5737
0
      {
5738
0
        m->p_size = info->stacksize;
5739
0
        m->p_size_valid = 1;
5740
0
      }
5741
5742
0
    *pm = m;
5743
0
    pm = &m->next;
5744
0
  }
5745
5746
0
      if (info != NULL && info->relro)
5747
0
  {
5748
0
    for (m = mfirst; m != NULL; m = m->next)
5749
0
      {
5750
0
        if (m->p_type == PT_LOAD
5751
0
      && m->count != 0
5752
0
      && m->sections[0]->vma >= info->relro_start
5753
0
      && m->sections[0]->vma < info->relro_end)
5754
0
    {
5755
0
      i = m->count;
5756
0
      while (--i != (unsigned) -1)
5757
0
        {
5758
0
          if (m->sections[i]->size > 0
5759
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5760
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5761
0
      break;
5762
0
        }
5763
5764
0
      if (i != (unsigned) -1)
5765
0
        break;
5766
0
    }
5767
0
      }
5768
5769
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5770
0
    if (m != NULL)
5771
0
      {
5772
0
        amt = sizeof (struct elf_segment_map);
5773
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5774
0
        if (m == NULL)
5775
0
    goto error_return;
5776
0
        m->next = NULL;
5777
0
        m->p_type = PT_GNU_RELRO;
5778
0
        *pm = m;
5779
0
        pm = &m->next;
5780
0
      }
5781
0
  }
5782
5783
0
      free (sections);
5784
0
      elf_seg_map (abfd) = mfirst;
5785
0
    }
5786
5787
0
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5788
0
    return false;
5789
5790
0
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5791
0
    ++count;
5792
0
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5793
5794
0
  return true;
5795
5796
0
 error_return:
5797
0
  free (sections);
5798
0
  return false;
5799
0
}
5800
5801
/* Sort sections by address.  */
5802
5803
static int
5804
elf_sort_sections (const void *arg1, const void *arg2)
5805
0
{
5806
0
  const asection *sec1 = *(const asection **) arg1;
5807
0
  const asection *sec2 = *(const asection **) arg2;
5808
0
  bfd_size_type size1, size2;
5809
5810
  /* Sort by LMA first, since this is the address used to
5811
     place the section into a segment.  */
5812
0
  if (sec1->lma < sec2->lma)
5813
0
    return -1;
5814
0
  else if (sec1->lma > sec2->lma)
5815
0
    return 1;
5816
5817
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5818
     the same, and this will do nothing.  */
5819
0
  if (sec1->vma < sec2->vma)
5820
0
    return -1;
5821
0
  else if (sec1->vma > sec2->vma)
5822
0
    return 1;
5823
5824
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5825
5826
0
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5827
0
      && (x)->size != 0)
5828
5829
0
  if (TOEND (sec1))
5830
0
    {
5831
0
      if (!TOEND (sec2))
5832
0
  return 1;
5833
0
    }
5834
0
  else if (TOEND (sec2))
5835
0
    return -1;
5836
5837
0
#undef TOEND
5838
5839
  /* Sort by size, to put zero sized sections
5840
     before others at the same address.  */
5841
5842
0
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5843
0
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5844
5845
0
  if (size1 < size2)
5846
0
    return -1;
5847
0
  if (size1 > size2)
5848
0
    return 1;
5849
5850
0
  return sec1->target_index - sec2->target_index;
5851
0
}
5852
5853
/* This qsort comparison functions sorts PT_LOAD segments first and
5854
   by p_paddr, for assign_file_positions_for_load_sections.  */
5855
5856
static int
5857
elf_sort_segments (const void *arg1, const void *arg2)
5858
0
{
5859
0
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5860
0
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5861
5862
0
  if (m1->p_type != m2->p_type)
5863
0
    {
5864
0
      if (m1->p_type == PT_NULL)
5865
0
  return 1;
5866
0
      if (m2->p_type == PT_NULL)
5867
0
  return -1;
5868
0
      return m1->p_type < m2->p_type ? -1 : 1;
5869
0
    }
5870
0
  if (m1->includes_filehdr != m2->includes_filehdr)
5871
0
    return m1->includes_filehdr ? -1 : 1;
5872
0
  if (m1->no_sort_lma != m2->no_sort_lma)
5873
0
    return m1->no_sort_lma ? -1 : 1;
5874
0
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5875
0
    {
5876
0
      bfd_vma lma1, lma2;  /* Octets.  */
5877
0
      lma1 = 0;
5878
0
      if (m1->p_paddr_valid)
5879
0
  lma1 = m1->p_paddr;
5880
0
      else if (m1->count != 0)
5881
0
  {
5882
0
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5883
0
              m1->sections[0]);
5884
0
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5885
0
  }
5886
0
      lma2 = 0;
5887
0
      if (m2->p_paddr_valid)
5888
0
  lma2 = m2->p_paddr;
5889
0
      else if (m2->count != 0)
5890
0
  {
5891
0
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5892
0
              m2->sections[0]);
5893
0
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5894
0
  }
5895
0
      if (lma1 != lma2)
5896
0
  return lma1 < lma2 ? -1 : 1;
5897
0
    }
5898
0
  if (m1->idx != m2->idx)
5899
0
    return m1->idx < m2->idx ? -1 : 1;
5900
0
  return 0;
5901
0
}
5902
5903
/* Ian Lance Taylor writes:
5904
5905
   We shouldn't be using % with a negative signed number.  That's just
5906
   not good.  We have to make sure either that the number is not
5907
   negative, or that the number has an unsigned type.  When the types
5908
   are all the same size they wind up as unsigned.  When file_ptr is a
5909
   larger signed type, the arithmetic winds up as signed long long,
5910
   which is wrong.
5911
5912
   What we're trying to say here is something like ``increase OFF by
5913
   the least amount that will cause it to be equal to the VMA modulo
5914
   the page size.''  */
5915
/* In other words, something like:
5916
5917
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5918
   off_offset = off % bed->maxpagesize;
5919
   if (vma_offset < off_offset)
5920
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5921
   else
5922
     adjustment = vma_offset - off_offset;
5923
5924
   which can be collapsed into the expression below.  */
5925
5926
static file_ptr
5927
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5928
0
{
5929
  /* PR binutils/16199: Handle an alignment of zero.  */
5930
0
  if (maxpagesize == 0)
5931
0
    maxpagesize = 1;
5932
0
  return ((vma - off) % maxpagesize);
5933
0
}
5934
5935
static void
5936
print_segment_map (const struct elf_segment_map *m)
5937
0
{
5938
0
  unsigned int j;
5939
0
  const char *pt = get_segment_type (m->p_type);
5940
0
  char buf[32];
5941
5942
0
  if (pt == NULL)
5943
0
    {
5944
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5945
0
  sprintf (buf, "LOPROC+%7.7x",
5946
0
     (unsigned int) (m->p_type - PT_LOPROC));
5947
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5948
0
  sprintf (buf, "LOOS+%7.7x",
5949
0
     (unsigned int) (m->p_type - PT_LOOS));
5950
0
      else
5951
0
  snprintf (buf, sizeof (buf), "%8.8x",
5952
0
      (unsigned int) m->p_type);
5953
0
      pt = buf;
5954
0
    }
5955
0
  fflush (stdout);
5956
0
  fprintf (stderr, "%s:", pt);
5957
0
  for (j = 0; j < m->count; j++)
5958
0
    fprintf (stderr, " %s", m->sections [j]->name);
5959
0
  putc ('\n',stderr);
5960
0
  fflush (stderr);
5961
0
}
5962
5963
/* Assign file positions to the sections based on the mapping from
5964
   sections to segments.  This function also sets up some fields in
5965
   the file header.  */
5966
5967
static bool
5968
assign_file_positions_for_load_sections (bfd *abfd,
5969
           struct bfd_link_info *link_info)
5970
0
{
5971
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5972
0
  struct elf_segment_map *m;
5973
0
  struct elf_segment_map *phdr_load_seg;
5974
0
  Elf_Internal_Phdr *phdrs;
5975
0
  Elf_Internal_Phdr *p;
5976
0
  file_ptr off;  /* Octets.  */
5977
0
  bfd_size_type maxpagesize;
5978
0
  unsigned int alloc, actual;
5979
0
  unsigned int i, j;
5980
0
  struct elf_segment_map **sorted_seg_map;
5981
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5982
5983
0
  if (link_info == NULL
5984
0
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5985
0
    return false;
5986
5987
0
  alloc = 0;
5988
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5989
0
    m->idx = alloc++;
5990
5991
0
  if (alloc)
5992
0
    {
5993
0
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5994
0
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5995
0
    }
5996
0
  else
5997
0
    {
5998
      /* PR binutils/12467.  */
5999
0
      elf_elfheader (abfd)->e_phoff = 0;
6000
0
      elf_elfheader (abfd)->e_phentsize = 0;
6001
0
    }
6002
6003
0
  elf_elfheader (abfd)->e_phnum = alloc;
6004
6005
0
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6006
0
    {
6007
0
      actual = alloc;
6008
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6009
0
    }
6010
0
  else
6011
0
    {
6012
0
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6013
0
      BFD_ASSERT (elf_program_header_size (abfd)
6014
0
      == actual * bed->s->sizeof_phdr);
6015
0
      BFD_ASSERT (actual >= alloc);
6016
0
    }
6017
6018
0
  if (alloc == 0)
6019
0
    {
6020
0
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6021
0
      return true;
6022
0
    }
6023
6024
  /* We're writing the size in elf_program_header_size (abfd),
6025
     see assign_file_positions_except_relocs, so make sure we have
6026
     that amount allocated, with trailing space cleared.
6027
     The variable alloc contains the computed need, while
6028
     elf_program_header_size (abfd) contains the size used for the
6029
     layout.
6030
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6031
     where the layout is forced to according to a larger size in the
6032
     last iterations for the testcase ld-elf/header.  */
6033
0
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6034
0
           + alloc * sizeof (*sorted_seg_map)));
6035
0
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6036
0
  elf_tdata (abfd)->phdr = phdrs;
6037
0
  if (phdrs == NULL)
6038
0
    return false;
6039
6040
0
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6041
0
    {
6042
0
      sorted_seg_map[j] = m;
6043
      /* If elf_segment_map is not from map_sections_to_segments, the
6044
   sections may not be correctly ordered.  NOTE: sorting should
6045
   not be done to the PT_NOTE section of a corefile, which may
6046
   contain several pseudo-sections artificially created by bfd.
6047
   Sorting these pseudo-sections breaks things badly.  */
6048
0
      if (m->count > 1
6049
0
    && !(elf_elfheader (abfd)->e_type == ET_CORE
6050
0
         && m->p_type == PT_NOTE))
6051
0
  {
6052
0
    for (i = 0; i < m->count; i++)
6053
0
      m->sections[i]->target_index = i;
6054
0
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
6055
0
     elf_sort_sections);
6056
0
  }
6057
0
    }
6058
0
  if (alloc > 1)
6059
0
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6060
0
     elf_sort_segments);
6061
6062
0
  maxpagesize = 1;
6063
0
  if ((abfd->flags & D_PAGED) != 0)
6064
0
    {
6065
0
      if (link_info != NULL)
6066
0
  maxpagesize = link_info->maxpagesize;
6067
0
      else
6068
0
  maxpagesize = bed->maxpagesize;
6069
0
    }
6070
6071
  /* Sections must map to file offsets past the ELF file header.  */
6072
0
  off = bed->s->sizeof_ehdr;
6073
  /* And if one of the PT_LOAD headers doesn't include the program
6074
     headers then we'll be mapping program headers in the usual
6075
     position after the ELF file header.  */
6076
0
  phdr_load_seg = NULL;
6077
0
  for (j = 0; j < alloc; j++)
6078
0
    {
6079
0
      m = sorted_seg_map[j];
6080
0
      if (m->p_type != PT_LOAD)
6081
0
  break;
6082
0
      if (m->includes_phdrs)
6083
0
  {
6084
0
    phdr_load_seg = m;
6085
0
    break;
6086
0
  }
6087
0
    }
6088
0
  if (phdr_load_seg == NULL)
6089
0
    off += actual * bed->s->sizeof_phdr;
6090
6091
0
  for (j = 0; j < alloc; j++)
6092
0
    {
6093
0
      asection **secpp;
6094
0
      bfd_vma off_adjust;  /* Octets.  */
6095
0
      bool no_contents;
6096
0
      bfd_size_type p_align;
6097
0
      bool p_align_p;
6098
6099
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6100
   number of sections with contents contributing to both p_filesz
6101
   and p_memsz, followed by a number of sections with no contents
6102
   that just contribute to p_memsz.  In this loop, OFF tracks next
6103
   available file offset for PT_LOAD and PT_NOTE segments.  */
6104
0
      m = sorted_seg_map[j];
6105
0
      p = phdrs + m->idx;
6106
0
      p->p_type = m->p_type;
6107
0
      p->p_flags = m->p_flags;
6108
0
      p_align = bed->p_align;
6109
0
      p_align_p = false;
6110
6111
0
      if (m->count == 0)
6112
0
  p->p_vaddr = m->p_vaddr_offset * opb;
6113
0
      else
6114
0
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6115
6116
0
      if (m->p_paddr_valid)
6117
0
  p->p_paddr = m->p_paddr;
6118
0
      else if (m->count == 0)
6119
0
  p->p_paddr = 0;
6120
0
      else
6121
0
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6122
6123
0
      if (p->p_type == PT_LOAD
6124
0
    && (abfd->flags & D_PAGED) != 0)
6125
0
  {
6126
    /* p_align in demand paged PT_LOAD segments effectively stores
6127
       the maximum page size.  When copying an executable with
6128
       objcopy, we set m->p_align from the input file.  Use this
6129
       value for maxpagesize rather than bed->maxpagesize, which
6130
       may be different.  Note that we use maxpagesize for PT_TLS
6131
       segment alignment later in this function, so we are relying
6132
       on at least one PT_LOAD segment appearing before a PT_TLS
6133
       segment.  */
6134
0
    if (m->p_align_valid)
6135
0
      maxpagesize = m->p_align;
6136
0
    else if (p_align != 0
6137
0
       && (link_info == NULL
6138
0
           || !link_info->maxpagesize_is_set))
6139
      /* Set p_align to the default p_align value while laying
6140
         out segments aligning to the maximum page size or the
6141
         largest section alignment.  The run-time loader can
6142
         align segments to the default p_align value or the
6143
         maximum page size, depending on system page size.  */
6144
0
      p_align_p = true;
6145
6146
0
    p->p_align = maxpagesize;
6147
0
  }
6148
0
      else if (m->p_align_valid)
6149
0
  p->p_align = m->p_align;
6150
0
      else if (m->count == 0)
6151
0
  p->p_align = 1 << bed->s->log_file_align;
6152
6153
0
      if (m == phdr_load_seg)
6154
0
  {
6155
0
    if (!m->includes_filehdr)
6156
0
      p->p_offset = off;
6157
0
    off += actual * bed->s->sizeof_phdr;
6158
0
  }
6159
6160
0
      no_contents = false;
6161
0
      off_adjust = 0;
6162
0
      if (p->p_type == PT_LOAD
6163
0
    && m->count > 0)
6164
0
  {
6165
0
    bfd_size_type align;  /* Bytes.  */
6166
0
    unsigned int align_power = 0;
6167
6168
0
    if (m->p_align_valid)
6169
0
      align = p->p_align;
6170
0
    else
6171
0
      {
6172
0
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6173
0
    {
6174
0
      unsigned int secalign;
6175
6176
0
      secalign = bfd_section_alignment (*secpp);
6177
0
      if (secalign > align_power)
6178
0
        align_power = secalign;
6179
0
    }
6180
0
        align = (bfd_size_type) 1 << align_power;
6181
0
        if (align < maxpagesize)
6182
0
    {
6183
      /* If a section requires alignment higher than the
6184
         default p_align value, don't set p_align to the
6185
         default p_align value.  */
6186
0
      if (align > p_align)
6187
0
        p_align_p = false;
6188
0
      align = maxpagesize;
6189
0
    }
6190
0
        else
6191
0
    {
6192
      /* If a section requires alignment higher than the
6193
         maximum page size, set p_align to the section
6194
         alignment.  */
6195
0
      p_align_p = true;
6196
0
      p_align = align;
6197
0
    }
6198
0
      }
6199
6200
0
    for (i = 0; i < m->count; i++)
6201
0
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6202
        /* If we aren't making room for this section, then
6203
     it must be SHT_NOBITS regardless of what we've
6204
     set via struct bfd_elf_special_section.  */
6205
0
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6206
6207
    /* Find out whether this segment contains any loadable
6208
       sections.  */
6209
0
    no_contents = true;
6210
0
    for (i = 0; i < m->count; i++)
6211
0
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6212
0
        {
6213
0
    no_contents = false;
6214
0
    break;
6215
0
        }
6216
6217
0
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6218
6219
    /* Broken hardware and/or kernel require that files do not
6220
       map the same page with different permissions on some hppa
6221
       processors.  */
6222
0
    if (j != 0
6223
0
        && (abfd->flags & D_PAGED) != 0
6224
0
        && bed->no_page_alias
6225
0
        && (off & (maxpagesize - 1)) != 0
6226
0
        && ((off & -maxpagesize)
6227
0
      == ((off + off_adjust) & -maxpagesize)))
6228
0
      off_adjust += maxpagesize;
6229
0
    off += off_adjust;
6230
0
    if (no_contents)
6231
0
      {
6232
        /* We shouldn't need to align the segment on disk since
6233
     the segment doesn't need file space, but the gABI
6234
     arguably requires the alignment and glibc ld.so
6235
     checks it.  So to comply with the alignment
6236
     requirement but not waste file space, we adjust
6237
     p_offset for just this segment.  (OFF_ADJUST is
6238
     subtracted from OFF later.)  This may put p_offset
6239
     past the end of file, but that shouldn't matter.  */
6240
0
      }
6241
0
    else
6242
0
      off_adjust = 0;
6243
0
  }
6244
      /* Make sure the .dynamic section is the first section in the
6245
   PT_DYNAMIC segment.  */
6246
0
      else if (p->p_type == PT_DYNAMIC
6247
0
         && m->count > 1
6248
0
         && strcmp (m->sections[0]->name, ".dynamic") != 0)
6249
0
  {
6250
0
    _bfd_error_handler
6251
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6252
0
         " is not the .dynamic section"),
6253
0
       abfd);
6254
0
    bfd_set_error (bfd_error_bad_value);
6255
0
    return false;
6256
0
  }
6257
      /* Set the note section type to SHT_NOTE.  */
6258
0
      else if (p->p_type == PT_NOTE)
6259
0
  for (i = 0; i < m->count; i++)
6260
0
    elf_section_type (m->sections[i]) = SHT_NOTE;
6261
6262
0
      if (m->includes_filehdr)
6263
0
  {
6264
0
    if (!m->p_flags_valid)
6265
0
      p->p_flags |= PF_R;
6266
0
    p->p_filesz = bed->s->sizeof_ehdr;
6267
0
    p->p_memsz = bed->s->sizeof_ehdr;
6268
0
    if (p->p_type == PT_LOAD)
6269
0
      {
6270
0
        if (m->count > 0)
6271
0
    {
6272
0
      if (p->p_vaddr < (bfd_vma) off
6273
0
          || (!m->p_paddr_valid
6274
0
        && p->p_paddr < (bfd_vma) off))
6275
0
        {
6276
0
          _bfd_error_handler
6277
0
      (_("%pB: not enough room for program headers,"
6278
0
         " try linking with -N"),
6279
0
       abfd);
6280
0
          bfd_set_error (bfd_error_bad_value);
6281
0
          return false;
6282
0
        }
6283
0
      p->p_vaddr -= off;
6284
0
      if (!m->p_paddr_valid)
6285
0
        p->p_paddr -= off;
6286
0
    }
6287
0
      }
6288
0
    else if (sorted_seg_map[0]->includes_filehdr)
6289
0
      {
6290
0
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6291
0
        p->p_vaddr = filehdr->p_vaddr;
6292
0
        if (!m->p_paddr_valid)
6293
0
    p->p_paddr = filehdr->p_paddr;
6294
0
      }
6295
0
  }
6296
6297
0
      if (m->includes_phdrs)
6298
0
  {
6299
0
    if (!m->p_flags_valid)
6300
0
      p->p_flags |= PF_R;
6301
0
    p->p_filesz += actual * bed->s->sizeof_phdr;
6302
0
    p->p_memsz += actual * bed->s->sizeof_phdr;
6303
0
    if (!m->includes_filehdr)
6304
0
      {
6305
0
        if (p->p_type == PT_LOAD)
6306
0
    {
6307
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6308
0
      if (m->count > 0)
6309
0
        {
6310
0
          p->p_vaddr -= off - p->p_offset;
6311
0
          if (!m->p_paddr_valid)
6312
0
      p->p_paddr -= off - p->p_offset;
6313
0
        }
6314
0
    }
6315
0
        else if (phdr_load_seg != NULL)
6316
0
    {
6317
0
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6318
0
      bfd_vma phdr_off = 0;  /* Octets.  */
6319
0
      if (phdr_load_seg->includes_filehdr)
6320
0
        phdr_off = bed->s->sizeof_ehdr;
6321
0
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6322
0
      if (!m->p_paddr_valid)
6323
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6324
0
      p->p_offset = phdr->p_offset + phdr_off;
6325
0
    }
6326
0
        else
6327
0
    p->p_offset = bed->s->sizeof_ehdr;
6328
0
      }
6329
0
  }
6330
6331
0
      if (p->p_type == PT_LOAD
6332
0
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6333
0
  {
6334
0
    if (!m->includes_filehdr && !m->includes_phdrs)
6335
0
      {
6336
0
        p->p_offset = off;
6337
0
        if (no_contents)
6338
0
    {
6339
      /* Put meaningless p_offset for PT_LOAD segments
6340
         without file contents somewhere within the first
6341
         page, in an attempt to not point past EOF.  */
6342
0
      bfd_size_type align = maxpagesize;
6343
0
      if (align < p->p_align)
6344
0
        align = p->p_align;
6345
0
      if (align < 1)
6346
0
        align = 1;
6347
0
      p->p_offset = off % align;
6348
0
    }
6349
0
      }
6350
0
    else
6351
0
      {
6352
0
        file_ptr adjust;  /* Octets.  */
6353
6354
0
        adjust = off - (p->p_offset + p->p_filesz);
6355
0
        if (!no_contents)
6356
0
    p->p_filesz += adjust;
6357
0
        p->p_memsz += adjust;
6358
0
      }
6359
0
  }
6360
6361
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6362
   maps.  Set filepos for sections in PT_LOAD segments, and in
6363
   core files, for sections in PT_NOTE segments.
6364
   assign_file_positions_for_non_load_sections will set filepos
6365
   for other sections and update p_filesz for other segments.  */
6366
0
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6367
0
  {
6368
0
    asection *sec;
6369
0
    bfd_size_type align;
6370
0
    Elf_Internal_Shdr *this_hdr;
6371
6372
0
    sec = *secpp;
6373
0
    this_hdr = &elf_section_data (sec)->this_hdr;
6374
0
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6375
6376
0
    if ((p->p_type == PT_LOAD
6377
0
         || p->p_type == PT_TLS)
6378
0
        && (this_hdr->sh_type != SHT_NOBITS
6379
0
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6380
0
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6381
0
        || p->p_type == PT_TLS))))
6382
0
      {
6383
0
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6384
0
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6385
0
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6386
0
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6387
6388
0
        if (adjust != 0
6389
0
      && (s_start < p_end
6390
0
          || p_end < p_start))
6391
0
    {
6392
0
      _bfd_error_handler
6393
        /* xgettext:c-format */
6394
0
        (_("%pB: section %pA lma %#" PRIx64
6395
0
           " adjusted to %#" PRIx64),
6396
0
         abfd, sec, (uint64_t) s_start / opb,
6397
0
         (uint64_t) p_end / opb);
6398
0
      adjust = 0;
6399
0
      sec->lma = p_end / opb;
6400
0
    }
6401
0
        p->p_memsz += adjust;
6402
6403
0
        if (p->p_type == PT_LOAD)
6404
0
    {
6405
0
      if (this_hdr->sh_type != SHT_NOBITS)
6406
0
        {
6407
0
          off_adjust = 0;
6408
0
          if (p->p_filesz + adjust < p->p_memsz)
6409
0
      {
6410
        /* We have a PROGBITS section following NOBITS ones.
6411
           Allocate file space for the NOBITS section(s).
6412
           We don't need to write out the zeros, posix
6413
           fseek past the end of data already written
6414
           followed by a write at that location is
6415
           guaranteed to result in zeros being read
6416
           from the gap.  */
6417
0
        adjust = p->p_memsz - p->p_filesz;
6418
0
      }
6419
0
        }
6420
      /* We only adjust sh_offset in SHT_NOBITS sections
6421
         as would seem proper for their address when the
6422
         section is first in the segment.  sh_offset
6423
         doesn't really have any significance for
6424
         SHT_NOBITS anyway, apart from a notional position
6425
         relative to other sections.  Historically we
6426
         didn't bother with adjusting sh_offset and some
6427
         programs depend on it not being adjusted.  See
6428
         pr12921 and pr25662.  */
6429
0
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6430
0
        {
6431
0
          off += adjust;
6432
0
          if (this_hdr->sh_type == SHT_NOBITS)
6433
0
      off_adjust += adjust;
6434
0
        }
6435
0
    }
6436
0
        if (this_hdr->sh_type != SHT_NOBITS)
6437
0
    p->p_filesz += adjust;
6438
0
      }
6439
6440
0
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6441
0
      {
6442
        /* The section at i == 0 is the one that actually contains
6443
     everything.  */
6444
0
        if (i == 0)
6445
0
    {
6446
0
      this_hdr->sh_offset = sec->filepos = off;
6447
0
      off += this_hdr->sh_size;
6448
0
      p->p_filesz = this_hdr->sh_size;
6449
0
      p->p_memsz = 0;
6450
0
      p->p_align = 1;
6451
0
    }
6452
0
        else
6453
0
    {
6454
      /* The rest are fake sections that shouldn't be written.  */
6455
0
      sec->filepos = 0;
6456
0
      sec->size = 0;
6457
0
      sec->flags = 0;
6458
0
      continue;
6459
0
    }
6460
0
      }
6461
0
    else
6462
0
      {
6463
0
        if (p->p_type == PT_LOAD)
6464
0
    {
6465
0
      this_hdr->sh_offset = sec->filepos = off;
6466
0
      if (this_hdr->sh_type != SHT_NOBITS)
6467
0
        off += this_hdr->sh_size;
6468
0
    }
6469
0
        else if (this_hdr->sh_type == SHT_NOBITS
6470
0
           && (this_hdr->sh_flags & SHF_TLS) != 0
6471
0
           && this_hdr->sh_offset == 0)
6472
0
    {
6473
      /* This is a .tbss section that didn't get a PT_LOAD.
6474
         (See _bfd_elf_map_sections_to_segments "Create a
6475
         final PT_LOAD".)  Set sh_offset to the value it
6476
         would have if we had created a zero p_filesz and
6477
         p_memsz PT_LOAD header for the section.  This
6478
         also makes the PT_TLS header have the same
6479
         p_offset value.  */
6480
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6481
0
                off, align);
6482
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
6483
0
    }
6484
6485
0
        if (this_hdr->sh_type != SHT_NOBITS)
6486
0
    {
6487
0
      p->p_filesz += this_hdr->sh_size;
6488
      /* A load section without SHF_ALLOC is something like
6489
         a note section in a PT_NOTE segment.  These take
6490
         file space but are not loaded into memory.  */
6491
0
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6492
0
        p->p_memsz += this_hdr->sh_size;
6493
0
    }
6494
0
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6495
0
    {
6496
0
      if (p->p_type == PT_TLS)
6497
0
        p->p_memsz += this_hdr->sh_size;
6498
6499
      /* .tbss is special.  It doesn't contribute to p_memsz of
6500
         normal segments.  */
6501
0
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6502
0
        p->p_memsz += this_hdr->sh_size;
6503
0
    }
6504
6505
0
        if (align > p->p_align
6506
0
      && !m->p_align_valid
6507
0
      && (p->p_type != PT_LOAD
6508
0
          || (abfd->flags & D_PAGED) == 0))
6509
0
    p->p_align = align;
6510
0
      }
6511
6512
0
    if (!m->p_flags_valid)
6513
0
      {
6514
0
        p->p_flags |= PF_R;
6515
0
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6516
0
    p->p_flags |= PF_X;
6517
0
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6518
0
    p->p_flags |= PF_W;
6519
0
      }
6520
0
  }
6521
6522
0
      off -= off_adjust;
6523
6524
      /* PR ld/20815 - Check that the program header segment, if
6525
   present, will be loaded into memory.  */
6526
0
      if (p->p_type == PT_PHDR
6527
0
    && phdr_load_seg == NULL
6528
0
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6529
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6530
0
  {
6531
    /* The fix for this error is usually to edit the linker script being
6532
       used and set up the program headers manually.  Either that or
6533
       leave room for the headers at the start of the SECTIONS.  */
6534
0
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6535
0
        " by LOAD segment"),
6536
0
            abfd);
6537
0
    if (link_info == NULL)
6538
0
      return false;
6539
    /* Arrange for the linker to exit with an error, deleting
6540
       the output file unless --noinhibit-exec is given.  */
6541
0
    link_info->callbacks->info ("%X");
6542
0
  }
6543
6544
      /* Check that all sections are in a PT_LOAD segment.
6545
   Don't check funky gdb generated core files.  */
6546
0
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6547
0
  {
6548
0
    bool check_vma = true;
6549
6550
0
    for (i = 1; i < m->count; i++)
6551
0
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6552
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6553
0
               ->this_hdr), p) != 0
6554
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6555
0
               ->this_hdr), p) != 0)
6556
0
        {
6557
    /* Looks like we have overlays packed into the segment.  */
6558
0
    check_vma = false;
6559
0
    break;
6560
0
        }
6561
6562
0
    for (i = 0; i < m->count; i++)
6563
0
      {
6564
0
        Elf_Internal_Shdr *this_hdr;
6565
0
        asection *sec;
6566
6567
0
        sec = m->sections[i];
6568
0
        this_hdr = &(elf_section_data(sec)->this_hdr);
6569
0
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6570
0
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6571
0
    {
6572
0
      _bfd_error_handler
6573
        /* xgettext:c-format */
6574
0
        (_("%pB: section `%pA' can't be allocated in segment %d"),
6575
0
         abfd, sec, j);
6576
0
      print_segment_map (m);
6577
0
    }
6578
0
      }
6579
6580
0
    if (p_align_p)
6581
0
      p->p_align = p_align;
6582
0
  }
6583
0
    }
6584
6585
0
  elf_next_file_pos (abfd) = off;
6586
6587
0
  if (link_info != NULL
6588
0
      && phdr_load_seg != NULL
6589
0
      && phdr_load_seg->includes_filehdr)
6590
0
    {
6591
      /* There is a segment that contains both the file headers and the
6592
   program headers, so provide a symbol __ehdr_start pointing there.
6593
   A program can use this to examine itself robustly.  */
6594
6595
0
      struct elf_link_hash_entry *hash
6596
0
  = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6597
0
        false, false, true);
6598
      /* If the symbol was referenced and not defined, define it.  */
6599
0
      if (hash != NULL
6600
0
    && (hash->root.type == bfd_link_hash_new
6601
0
        || hash->root.type == bfd_link_hash_undefined
6602
0
        || hash->root.type == bfd_link_hash_undefweak
6603
0
        || hash->root.type == bfd_link_hash_common))
6604
0
  {
6605
0
    asection *s = NULL;
6606
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6607
6608
0
    if (phdr_load_seg->count != 0)
6609
      /* The segment contains sections, so use the first one.  */
6610
0
      s = phdr_load_seg->sections[0];
6611
0
    else
6612
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6613
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6614
0
        if (m->p_type == PT_LOAD && m->count != 0)
6615
0
    {
6616
0
      s = m->sections[0];
6617
0
      break;
6618
0
    }
6619
6620
0
    if (s != NULL)
6621
0
      {
6622
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6623
0
        hash->root.u.def.section = s;
6624
0
      }
6625
0
    else
6626
0
      {
6627
0
        hash->root.u.def.value = filehdr_vaddr;
6628
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6629
0
      }
6630
6631
0
    hash->root.type = bfd_link_hash_defined;
6632
0
    hash->def_regular = 1;
6633
0
    hash->non_elf = 0;
6634
0
  }
6635
0
    }
6636
6637
0
  return true;
6638
0
}
6639
6640
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6641
   is no defined method for detecting such files, so we have to
6642
   use heuristics instead.  */
6643
6644
bool
6645
is_debuginfo_file (bfd *abfd)
6646
0
{
6647
0
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6648
0
    return false;
6649
6650
0
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6651
0
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6652
0
  Elf_Internal_Shdr **headerp;
6653
6654
0
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6655
0
    {
6656
0
      Elf_Internal_Shdr *header = * headerp;
6657
6658
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6659
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6660
0
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6661
0
    && header->sh_type != SHT_NOBITS
6662
0
    && header->sh_type != SHT_NOTE)
6663
0
  return false;
6664
0
    }
6665
6666
0
  return true;
6667
0
}
6668
6669
/* Assign file positions for other sections, except for compressed debug
6670
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6671
6672
static bool
6673
assign_file_positions_for_non_load_sections (bfd *abfd,
6674
               struct bfd_link_info *link_info)
6675
0
{
6676
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6677
0
  Elf_Internal_Shdr **i_shdrpp;
6678
0
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6679
0
  Elf_Internal_Phdr *phdrs;
6680
0
  Elf_Internal_Phdr *p;
6681
0
  struct elf_segment_map *m;
6682
0
  file_ptr off;
6683
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6684
0
  bfd_vma maxpagesize;
6685
6686
0
  if (link_info != NULL)
6687
0
    maxpagesize = link_info->maxpagesize;
6688
0
  else
6689
0
    maxpagesize = bed->maxpagesize;
6690
0
  i_shdrpp = elf_elfsections (abfd);
6691
0
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6692
0
  off = elf_next_file_pos (abfd);
6693
0
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6694
0
    {
6695
0
      Elf_Internal_Shdr *hdr;
6696
0
      bfd_vma align;
6697
6698
0
      hdr = *hdrpp;
6699
0
      if (hdr->bfd_section != NULL
6700
0
    && (hdr->bfd_section->filepos != 0
6701
0
        || (hdr->sh_type == SHT_NOBITS
6702
0
      && hdr->contents == NULL)))
6703
0
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6704
0
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6705
0
  {
6706
0
    if (hdr->sh_size != 0
6707
        /* PR 24717 - debuginfo files are known to be not strictly
6708
     compliant with the ELF standard.  In particular they often
6709
     have .note.gnu.property sections that are outside of any
6710
     loadable segment.  This is not a problem for such files,
6711
     so do not warn about them.  */
6712
0
        && ! is_debuginfo_file (abfd))
6713
0
      _bfd_error_handler
6714
        /* xgettext:c-format */
6715
0
        (_("%pB: warning: allocated section `%s' not in segment"),
6716
0
         abfd,
6717
0
         (hdr->bfd_section == NULL
6718
0
    ? "*unknown*"
6719
0
    : hdr->bfd_section->name));
6720
    /* We don't need to page align empty sections.  */
6721
0
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6722
0
      align = maxpagesize;
6723
0
    else
6724
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6725
0
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6726
0
    off = _bfd_elf_assign_file_position_for_section (hdr, off,
6727
0
                 false);
6728
0
  }
6729
0
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6730
0
    && hdr->bfd_section == NULL)
6731
         /* We don't know the offset of these sections yet:
6732
      their size has not been decided.  */
6733
0
         || (abfd->is_linker_output
6734
0
       && hdr->bfd_section != NULL
6735
0
       && (hdr->sh_name == -1u
6736
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6737
0
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6738
0
         || (elf_symtab_shndx_list (abfd) != NULL
6739
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6740
0
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6741
0
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6742
0
  hdr->sh_offset = -1;
6743
0
      else
6744
0
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6745
0
    }
6746
0
  elf_next_file_pos (abfd) = off;
6747
6748
  /* Now that we have set the section file positions, we can set up
6749
     the file positions for the non PT_LOAD segments.  */
6750
0
  phdrs = elf_tdata (abfd)->phdr;
6751
0
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6752
0
    {
6753
0
      if (p->p_type == PT_GNU_RELRO)
6754
0
  {
6755
0
    bfd_vma start, end;  /* Bytes.  */
6756
0
    bool ok;
6757
6758
0
    if (link_info != NULL)
6759
0
      {
6760
        /* During linking the range of the RELRO segment is passed
6761
     in link_info.  Note that there may be padding between
6762
     relro_start and the first RELRO section.  */
6763
0
        start = link_info->relro_start;
6764
0
        end = link_info->relro_end;
6765
0
      }
6766
0
    else if (m->count != 0)
6767
0
      {
6768
0
        if (!m->p_size_valid)
6769
0
    abort ();
6770
0
        start = m->sections[0]->vma;
6771
0
        end = start + m->p_size / opb;
6772
0
      }
6773
0
    else
6774
0
      {
6775
0
        start = 0;
6776
0
        end = 0;
6777
0
      }
6778
6779
0
    ok = false;
6780
0
    if (start < end)
6781
0
      {
6782
0
        struct elf_segment_map *lm;
6783
0
        const Elf_Internal_Phdr *lp;
6784
0
        unsigned int i;
6785
6786
        /* Find a LOAD segment containing a section in the RELRO
6787
     segment.  */
6788
0
        for (lm = elf_seg_map (abfd), lp = phdrs;
6789
0
       lm != NULL;
6790
0
       lm = lm->next, lp++)
6791
0
    {
6792
0
      if (lp->p_type == PT_LOAD
6793
0
          && lm->count != 0
6794
0
          && (lm->sections[lm->count - 1]->vma
6795
0
        + (!IS_TBSS (lm->sections[lm->count - 1])
6796
0
           ? lm->sections[lm->count - 1]->size / opb
6797
0
           : 0)) > start
6798
0
          && lm->sections[0]->vma < end)
6799
0
        break;
6800
0
    }
6801
6802
0
        if (lm != NULL)
6803
0
    {
6804
      /* Find the section starting the RELRO segment.  */
6805
0
      for (i = 0; i < lm->count; i++)
6806
0
        {
6807
0
          asection *s = lm->sections[i];
6808
0
          if (s->vma >= start
6809
0
        && s->vma < end
6810
0
        && s->size != 0)
6811
0
      break;
6812
0
        }
6813
6814
0
      if (i < lm->count)
6815
0
        {
6816
0
          p->p_vaddr = lm->sections[i]->vma * opb;
6817
0
          p->p_paddr = lm->sections[i]->lma * opb;
6818
0
          p->p_offset = lm->sections[i]->filepos;
6819
0
          p->p_memsz = end * opb - p->p_vaddr;
6820
0
          p->p_filesz = p->p_memsz;
6821
6822
          /* The RELRO segment typically ends a few bytes
6823
       into .got.plt but other layouts are possible.
6824
       In cases where the end does not match any
6825
       loaded section (for instance is in file
6826
       padding), trim p_filesz back to correspond to
6827
       the end of loaded section contents.  */
6828
0
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6829
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6830
6831
          /* Preserve the alignment and flags if they are
6832
       valid.  The gold linker generates RW/4 for
6833
       the PT_GNU_RELRO section.  It is better for
6834
       objcopy/strip to honor these attributes
6835
       otherwise gdb will choke when using separate
6836
       debug files.  */
6837
0
          if (!m->p_align_valid)
6838
0
      p->p_align = 1;
6839
0
          if (!m->p_flags_valid)
6840
0
      p->p_flags = PF_R;
6841
0
          ok = true;
6842
0
        }
6843
0
    }
6844
0
      }
6845
6846
0
    if (!ok)
6847
0
      {
6848
0
        if (link_info != NULL)
6849
0
    _bfd_error_handler
6850
0
      (_("%pB: warning: unable to allocate any sections"
6851
0
         " to PT_GNU_RELRO segment"),
6852
0
       abfd);
6853
0
        memset (p, 0, sizeof *p);
6854
0
      }
6855
0
  }
6856
0
      else if (p->p_type == PT_GNU_STACK)
6857
0
  {
6858
0
    if (m->p_size_valid)
6859
0
      p->p_memsz = m->p_size;
6860
0
  }
6861
0
      else if (m->count != 0)
6862
0
  {
6863
0
    unsigned int i;
6864
6865
0
    if (p->p_type != PT_LOAD
6866
0
        && (p->p_type != PT_NOTE
6867
0
      || bfd_get_format (abfd) != bfd_core))
6868
0
      {
6869
        /* A user specified segment layout may include a PHDR
6870
     segment that overlaps with a LOAD segment...  */
6871
0
        if (p->p_type == PT_PHDR)
6872
0
    {
6873
0
      m->count = 0;
6874
0
      continue;
6875
0
    }
6876
6877
0
        if (m->includes_filehdr || m->includes_phdrs)
6878
0
    {
6879
      /* PR 17512: file: 2195325e.  */
6880
0
      _bfd_error_handler
6881
0
        (_("%pB: error: non-load segment %d includes file header "
6882
0
           "and/or program header"),
6883
0
         abfd, (int) (p - phdrs));
6884
0
      return false;
6885
0
    }
6886
6887
0
        p->p_filesz = 0;
6888
0
        p->p_offset = m->sections[0]->filepos;
6889
0
        for (i = m->count; i-- != 0;)
6890
0
    {
6891
0
      asection *sect = m->sections[i];
6892
0
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6893
0
      if (hdr->sh_type != SHT_NOBITS)
6894
0
        {
6895
0
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6896
          /* NB: p_memsz of the loadable PT_NOTE segment
6897
       should be the same as p_filesz.  */
6898
0
          if (p->p_type == PT_NOTE
6899
0
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6900
0
      p->p_memsz = p->p_filesz;
6901
0
          break;
6902
0
        }
6903
0
    }
6904
0
      }
6905
0
  }
6906
0
    }
6907
6908
0
  return true;
6909
0
}
6910
6911
static elf_section_list *
6912
find_section_in_list (unsigned int i, elf_section_list * list)
6913
0
{
6914
0
  for (;list != NULL; list = list->next)
6915
0
    if (list->ndx == i)
6916
0
      break;
6917
0
  return list;
6918
0
}
6919
6920
/* Work out the file positions of all the sections.  This is called by
6921
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6922
   VMAs must be known before this is called.
6923
6924
   Reloc sections come in two flavours: Those processed specially as
6925
   "side-channel" data attached to a section to which they apply, and
6926
   those that bfd doesn't process as relocations.  The latter sort are
6927
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6928
   consider the former sort here, unless they form part of the loadable
6929
   image.  Reloc sections not assigned here (and compressed debugging
6930
   sections and CTF sections which nothing else in the file can rely
6931
   upon) will be handled later by assign_file_positions_for_relocs.
6932
6933
   We also don't set the positions of the .symtab and .strtab here.  */
6934
6935
static bool
6936
assign_file_positions_except_relocs (bfd *abfd,
6937
             struct bfd_link_info *link_info)
6938
0
{
6939
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6940
0
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6941
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6942
0
  unsigned int alloc;
6943
6944
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6945
0
      && bfd_get_format (abfd) != bfd_core)
6946
0
    {
6947
0
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6948
0
      unsigned int num_sec = elf_numsections (abfd);
6949
0
      Elf_Internal_Shdr **hdrpp;
6950
0
      unsigned int i;
6951
0
      file_ptr off;
6952
6953
      /* Start after the ELF header.  */
6954
0
      off = i_ehdrp->e_ehsize;
6955
6956
      /* We are not creating an executable, which means that we are
6957
   not creating a program header, and that the actual order of
6958
   the sections in the file is unimportant.  */
6959
0
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6960
0
  {
6961
0
    Elf_Internal_Shdr *hdr;
6962
6963
0
    hdr = *hdrpp;
6964
0
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6965
0
         && hdr->bfd_section == NULL)
6966
        /* Do not assign offsets for these sections yet: we don't know
6967
     their sizes.  */
6968
0
        || (abfd->is_linker_output
6969
0
      && hdr->bfd_section != NULL
6970
0
      && (hdr->sh_name == -1u
6971
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6972
0
        || i == elf_onesymtab (abfd)
6973
0
        || (elf_symtab_shndx_list (abfd) != NULL
6974
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6975
0
        || i == elf_strtab_sec (abfd)
6976
0
        || i == elf_shstrtab_sec (abfd))
6977
0
      {
6978
0
        hdr->sh_offset = -1;
6979
0
      }
6980
0
    else
6981
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6982
0
  }
6983
6984
0
      elf_next_file_pos (abfd) = off;
6985
0
      elf_program_header_size (abfd) = 0;
6986
0
    }
6987
0
  else
6988
0
    {
6989
      /* Assign file positions for the loaded sections based on the
6990
   assignment of sections to segments.  */
6991
0
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6992
0
  return false;
6993
6994
      /* And for non-load sections.  */
6995
0
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6996
0
  return false;
6997
0
    }
6998
6999
0
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
7000
0
    return false;
7001
7002
  /* Write out the program headers.  */
7003
0
  alloc = i_ehdrp->e_phnum;
7004
0
  if (alloc != 0)
7005
0
    {
7006
0
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7007
0
  {
7008
    /* Memory resident segments with non-zero size and RWX
7009
       permissions are a security risk, so we generate a warning
7010
       here if we are creating any.  */
7011
0
    unsigned int i;
7012
7013
0
    for (i = 0; i < alloc; i++)
7014
0
      {
7015
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7016
7017
0
        if (phdr->p_memsz == 0)
7018
0
    continue;
7019
7020
0
        if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
7021
0
    _bfd_error_handler (_("warning: %pB has a TLS segment"
7022
0
              " with execute permission"),
7023
0
            abfd);
7024
0
        else if (phdr->p_type == PT_LOAD
7025
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7026
0
         == (PF_R | PF_W | PF_X)))
7027
0
    _bfd_error_handler (_("warning: %pB has a LOAD segment"
7028
0
              " with RWX permissions"),
7029
0
            abfd);
7030
0
      }
7031
0
  }
7032
7033
0
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7034
0
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7035
0
  return false;
7036
0
    }
7037
7038
0
  return true;
7039
0
}
7040
7041
bool
7042
_bfd_elf_init_file_header (bfd *abfd,
7043
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7044
0
{
7045
0
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7046
0
  struct elf_strtab_hash *shstrtab;
7047
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7048
7049
0
  i_ehdrp = elf_elfheader (abfd);
7050
7051
0
  shstrtab = _bfd_elf_strtab_init ();
7052
0
  if (shstrtab == NULL)
7053
0
    return false;
7054
7055
0
  elf_shstrtab (abfd) = shstrtab;
7056
7057
0
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7058
0
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7059
0
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7060
0
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7061
7062
0
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7063
0
  i_ehdrp->e_ident[EI_DATA] =
7064
0
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7065
0
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7066
7067
0
  if ((abfd->flags & DYNAMIC) != 0)
7068
0
    i_ehdrp->e_type = ET_DYN;
7069
0
  else if ((abfd->flags & EXEC_P) != 0)
7070
0
    i_ehdrp->e_type = ET_EXEC;
7071
0
  else if (bfd_get_format (abfd) == bfd_core)
7072
0
    i_ehdrp->e_type = ET_CORE;
7073
0
  else
7074
0
    i_ehdrp->e_type = ET_REL;
7075
7076
0
  switch (bfd_get_arch (abfd))
7077
0
    {
7078
0
    case bfd_arch_unknown:
7079
0
      i_ehdrp->e_machine = EM_NONE;
7080
0
      break;
7081
7082
      /* There used to be a long list of cases here, each one setting
7083
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7084
   in the corresponding bfd definition.  To avoid duplication,
7085
   the switch was removed.  Machines that need special handling
7086
   can generally do it in elf_backend_final_write_processing(),
7087
   unless they need the information earlier than the final write.
7088
   Such need can generally be supplied by replacing the tests for
7089
   e_machine with the conditions used to determine it.  */
7090
0
    default:
7091
0
      i_ehdrp->e_machine = bed->elf_machine_code;
7092
0
    }
7093
7094
0
  i_ehdrp->e_version = bed->s->ev_current;
7095
0
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7096
7097
  /* No program header, for now.  */
7098
0
  i_ehdrp->e_phoff = 0;
7099
0
  i_ehdrp->e_phentsize = 0;
7100
0
  i_ehdrp->e_phnum = 0;
7101
7102
  /* Each bfd section is section header entry.  */
7103
0
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7104
0
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7105
7106
0
  elf_tdata (abfd)->symtab_hdr.sh_name =
7107
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7108
0
  elf_tdata (abfd)->strtab_hdr.sh_name =
7109
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7110
0
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7111
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7112
0
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7113
0
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7114
0
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7115
0
    return false;
7116
7117
0
  return true;
7118
0
}
7119
7120
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7121
7122
   FIXME: We used to have code here to sort the PT_LOAD segments into
7123
   ascending order, as per the ELF spec.  But this breaks some programs,
7124
   including the Linux kernel.  But really either the spec should be
7125
   changed or the programs updated.  */
7126
7127
bool
7128
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7129
0
{
7130
0
  if (link_info != NULL && bfd_link_pie (link_info))
7131
0
    {
7132
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7133
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7134
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7135
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7136
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7137
7138
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7139
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7140
0
      for (; segment < end_segment; segment++)
7141
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7142
0
    p_vaddr = segment->p_vaddr;
7143
7144
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7145
   segments is non-zero.  */
7146
0
      if (p_vaddr)
7147
0
  i_ehdrp->e_type = ET_EXEC;
7148
0
    }
7149
0
  return true;
7150
0
}
7151
7152
/* Assign file positions for all the reloc sections which are not part
7153
   of the loadable file image, and the file position of section headers.  */
7154
7155
static bool
7156
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7157
0
{
7158
0
  file_ptr off;
7159
0
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7160
0
  Elf_Internal_Shdr *shdrp;
7161
0
  Elf_Internal_Ehdr *i_ehdrp;
7162
0
  const struct elf_backend_data *bed;
7163
7164
  /* Skip non-load sections without section header.  */
7165
0
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7166
0
    return true;
7167
7168
0
  off = elf_next_file_pos (abfd);
7169
7170
0
  shdrpp = elf_elfsections (abfd);
7171
0
  end_shdrpp = shdrpp + elf_numsections (abfd);
7172
0
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7173
0
    {
7174
0
      shdrp = *shdrpp;
7175
0
      if (shdrp->sh_offset == -1)
7176
0
  {
7177
0
    asection *sec = shdrp->bfd_section;
7178
0
    if (sec == NULL
7179
0
        || shdrp->sh_type == SHT_REL
7180
0
        || shdrp->sh_type == SHT_RELA)
7181
0
      ;
7182
0
    else if (bfd_section_is_ctf (sec))
7183
0
      {
7184
        /* Update section size and contents.  */
7185
0
        shdrp->sh_size = sec->size;
7186
0
        shdrp->contents = sec->contents;
7187
0
      }
7188
0
    else if (shdrp->sh_name == -1u)
7189
0
      {
7190
0
        const char *name = sec->name;
7191
0
        struct bfd_elf_section_data *d;
7192
7193
        /* Compress DWARF debug sections.  */
7194
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7195
0
    return false;
7196
7197
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7198
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7199
0
      && name[1] == 'd')
7200
0
    {
7201
      /* If section is compressed with zlib-gnu, convert
7202
         section name from .debug_* to .zdebug_*.  */
7203
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7204
0
      if (new_name == NULL)
7205
0
        return false;
7206
0
      name = new_name;
7207
0
    }
7208
        /* Add section name to section name section.  */
7209
0
        shdrp->sh_name
7210
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7211
0
                  name, false);
7212
0
        d = elf_section_data (sec);
7213
7214
        /* Add reloc section name to section name section.  */
7215
0
        if (d->rel.hdr
7216
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7217
0
              name, false))
7218
0
    return false;
7219
0
        if (d->rela.hdr
7220
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7221
0
              name, true))
7222
0
    return false;
7223
7224
        /* Update section size and contents.  */
7225
0
        shdrp->sh_size = sec->size;
7226
0
        shdrp->contents = sec->contents;
7227
0
        sec->contents = NULL;
7228
0
      }
7229
7230
0
    off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7231
0
  }
7232
0
    }
7233
7234
  /* Place section name section after DWARF debug sections have been
7235
     compressed.  */
7236
0
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7237
0
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7238
0
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7239
0
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7240
7241
  /* Place the section headers.  */
7242
0
  i_ehdrp = elf_elfheader (abfd);
7243
0
  bed = get_elf_backend_data (abfd);
7244
0
  off = align_file_position (off, 1 << bed->s->log_file_align);
7245
0
  i_ehdrp->e_shoff = off;
7246
0
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7247
0
  elf_next_file_pos (abfd) = off;
7248
7249
0
  return true;
7250
0
}
7251
7252
bool
7253
_bfd_elf_write_object_contents (bfd *abfd)
7254
0
{
7255
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7256
0
  Elf_Internal_Shdr **i_shdrp;
7257
0
  bool failed;
7258
0
  unsigned int count, num_sec;
7259
0
  struct elf_obj_tdata *t;
7260
7261
0
  if (! abfd->output_has_begun
7262
0
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7263
0
    return false;
7264
  /* Do not rewrite ELF data when the BFD has been opened for update.
7265
     abfd->output_has_begun was set to TRUE on opening, so creation of
7266
     new sections, and modification of existing section sizes was
7267
     restricted.  This means the ELF header, program headers and
7268
     section headers can't have changed.  If the contents of any
7269
     sections has been modified, then those changes have already been
7270
     written to the BFD.  */
7271
0
  else if (abfd->direction == both_direction)
7272
0
    {
7273
0
      BFD_ASSERT (abfd->output_has_begun);
7274
0
      return true;
7275
0
    }
7276
7277
0
  i_shdrp = elf_elfsections (abfd);
7278
7279
0
  failed = false;
7280
0
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7281
0
  if (failed)
7282
0
    return false;
7283
7284
0
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7285
0
    return false;
7286
7287
  /* After writing the headers, we need to write the sections too...  */
7288
0
  num_sec = elf_numsections (abfd);
7289
0
  for (count = 1; count < num_sec; count++)
7290
0
    {
7291
      /* Don't set the sh_name field without section header.  */
7292
0
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7293
0
  i_shdrp[count]->sh_name
7294
0
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7295
0
            i_shdrp[count]->sh_name);
7296
0
      if (bed->elf_backend_section_processing)
7297
0
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7298
0
    return false;
7299
0
      if (i_shdrp[count]->contents)
7300
0
  {
7301
0
    bfd_size_type amt = i_shdrp[count]->sh_size;
7302
7303
0
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7304
0
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7305
0
      return false;
7306
0
  }
7307
0
    }
7308
7309
  /* Write out the section header names.  */
7310
0
  t = elf_tdata (abfd);
7311
0
  if (elf_shstrtab (abfd) != NULL
7312
0
      && t->shstrtab_hdr.sh_offset != -1
7313
0
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7314
0
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7315
0
    return false;
7316
7317
0
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7318
0
    return false;
7319
7320
0
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7321
0
    return false;
7322
7323
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7324
0
  if (t->o->build_id.after_write_object_contents != NULL
7325
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7326
0
    return false;
7327
0
  if (t->o->package_metadata.after_write_object_contents != NULL
7328
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7329
0
    return false;
7330
7331
0
  return true;
7332
0
}
7333
7334
bool
7335
_bfd_elf_write_corefile_contents (bfd *abfd)
7336
0
{
7337
  /* Hopefully this can be done just like an object file.  */
7338
0
  return _bfd_elf_write_object_contents (abfd);
7339
0
}
7340
7341
/* Given a section, search the header to find them.  */
7342
7343
unsigned int
7344
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7345
0
{
7346
0
  const struct elf_backend_data *bed;
7347
0
  unsigned int sec_index;
7348
7349
0
  if (elf_section_data (asect) != NULL
7350
0
      && elf_section_data (asect)->this_idx != 0)
7351
0
    return elf_section_data (asect)->this_idx;
7352
7353
0
  if (bfd_is_abs_section (asect))
7354
0
    sec_index = SHN_ABS;
7355
0
  else if (bfd_is_com_section (asect))
7356
0
    sec_index = SHN_COMMON;
7357
0
  else if (bfd_is_und_section (asect))
7358
0
    sec_index = SHN_UNDEF;
7359
0
  else
7360
0
    sec_index = SHN_BAD;
7361
7362
0
  bed = get_elf_backend_data (abfd);
7363
0
  if (bed->elf_backend_section_from_bfd_section)
7364
0
    {
7365
0
      int retval = sec_index;
7366
7367
0
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7368
0
  return retval;
7369
0
    }
7370
7371
0
  if (sec_index == SHN_BAD)
7372
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7373
7374
0
  return sec_index;
7375
0
}
7376
7377
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7378
   on error.  */
7379
7380
int
7381
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7382
0
{
7383
0
  asymbol *asym_ptr = *asym_ptr_ptr;
7384
0
  int idx;
7385
0
  flagword flags = asym_ptr->flags;
7386
7387
  /* When gas creates relocations against local labels, it creates its
7388
     own symbol for the section, but does put the symbol into the
7389
     symbol chain, so udata is 0.  When the linker is generating
7390
     relocatable output, this section symbol may be for one of the
7391
     input sections rather than the output section.  */
7392
0
  if (asym_ptr->udata.i == 0
7393
0
      && (flags & BSF_SECTION_SYM)
7394
0
      && asym_ptr->section)
7395
0
    {
7396
0
      asection *sec;
7397
7398
0
      sec = asym_ptr->section;
7399
0
      if (sec->owner != abfd && sec->output_section != NULL)
7400
0
  sec = sec->output_section;
7401
0
      if (sec->owner == abfd
7402
0
    && sec->index < elf_num_section_syms (abfd)
7403
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7404
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7405
0
    }
7406
7407
0
  idx = asym_ptr->udata.i;
7408
7409
0
  if (idx == 0)
7410
0
    {
7411
      /* This case can occur when using --strip-symbol on a symbol
7412
   which is used in a relocation entry.  */
7413
0
      _bfd_error_handler
7414
  /* xgettext:c-format */
7415
0
  (_("%pB: symbol `%s' required but not present"),
7416
0
   abfd, bfd_asymbol_name (asym_ptr));
7417
0
      bfd_set_error (bfd_error_no_symbols);
7418
0
      return -1;
7419
0
    }
7420
7421
#if DEBUG & 4
7422
  {
7423
    fprintf (stderr,
7424
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7425
       " flags = 0x%.8x\n",
7426
       (long) asym_ptr, asym_ptr->name, idx, flags);
7427
    fflush (stderr);
7428
  }
7429
#endif
7430
7431
0
  return idx;
7432
0
}
7433
7434
static inline bfd_vma
7435
segment_size (Elf_Internal_Phdr *segment)
7436
0
{
7437
0
  return (segment->p_memsz > segment->p_filesz
7438
0
    ? segment->p_memsz : segment->p_filesz);
7439
0
}
7440
7441
7442
/* Returns the end address of the segment + 1.  */
7443
static inline bfd_vma
7444
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7445
0
{
7446
0
  return start + segment_size (segment);
7447
0
}
7448
7449
static inline bfd_size_type
7450
section_size (asection *section, Elf_Internal_Phdr *segment)
7451
0
{
7452
0
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7453
0
      || (section->flags & SEC_THREAD_LOCAL) == 0
7454
0
      || segment->p_type == PT_TLS)
7455
0
    return section->size;
7456
0
  return 0;
7457
0
}
7458
7459
/* Returns TRUE if the given section is contained within the given
7460
   segment.  LMA addresses are compared against PADDR when
7461
   USE_VADDR is false, VMA against VADDR when true.  */
7462
static bool
7463
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7464
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7465
     bool use_vaddr)
7466
0
{
7467
0
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7468
0
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7469
0
  bfd_vma octet;
7470
0
  if (_bfd_mul_overflow (addr, opb, &octet))
7471
0
    return false;
7472
  /* The third and fourth lines below are testing that the section end
7473
     address is within the segment.  It's written this way to avoid
7474
     overflow.  Add seg_addr + section_size to both sides of the
7475
     inequality to make it obvious.  */
7476
0
  return (octet >= seg_addr
7477
0
    && segment_size (segment) >= section_size (section, segment)
7478
0
    && (octet - seg_addr
7479
0
        <= segment_size (segment) - section_size (section, segment)));
7480
0
}
7481
7482
/* Handle PT_NOTE segment.  */
7483
static bool
7484
is_note (asection *s, Elf_Internal_Phdr *p)
7485
0
{
7486
0
  return (p->p_type == PT_NOTE
7487
0
    && elf_section_type (s) == SHT_NOTE
7488
0
    && (ufile_ptr) s->filepos >= p->p_offset
7489
0
    && p->p_filesz >= s->size
7490
0
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7491
0
}
7492
7493
/* Rewrite program header information.  */
7494
7495
static bool
7496
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7497
0
{
7498
0
  Elf_Internal_Ehdr *iehdr;
7499
0
  struct elf_segment_map *map;
7500
0
  struct elf_segment_map *map_first;
7501
0
  struct elf_segment_map **pointer_to_map;
7502
0
  Elf_Internal_Phdr *segment;
7503
0
  asection *section;
7504
0
  unsigned int i;
7505
0
  unsigned int num_segments;
7506
0
  bool phdr_included = false;
7507
0
  bool p_paddr_valid;
7508
0
  struct elf_segment_map *phdr_adjust_seg = NULL;
7509
0
  unsigned int phdr_adjust_num = 0;
7510
0
  const struct elf_backend_data *bed;
7511
0
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7512
7513
0
  bed = get_elf_backend_data (ibfd);
7514
0
  iehdr = elf_elfheader (ibfd);
7515
7516
0
  map_first = NULL;
7517
0
  pointer_to_map = &map_first;
7518
7519
0
  num_segments = elf_elfheader (ibfd)->e_phnum;
7520
7521
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7522
     linker, which generates a PT_INTERP section with p_vaddr and
7523
     p_memsz set to 0.  */
7524
0
#define IS_SOLARIS_PT_INTERP(p, s)          \
7525
0
  (p->p_vaddr == 0              \
7526
0
   && p->p_paddr == 0              \
7527
0
   && p->p_memsz == 0              \
7528
0
   && p->p_filesz > 0              \
7529
0
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7530
0
   && s->size > 0              \
7531
0
   && (bfd_vma) s->filepos >= p->p_offset        \
7532
0
   && ((bfd_vma) s->filepos + s->size         \
7533
0
       <= p->p_offset + p->p_filesz))
7534
7535
  /* Decide if the given section should be included in the given segment.
7536
     A section will be included if:
7537
       1. It is within the address space of the segment -- we use the LMA
7538
    if that is set for the segment and the VMA otherwise,
7539
       2. It is an allocated section or a NOTE section in a PT_NOTE
7540
    segment.
7541
       3. There is an output section associated with it,
7542
       4. The section has not already been allocated to a previous segment.
7543
       5. PT_GNU_STACK segments do not include any sections.
7544
       6. PT_TLS segment includes only SHF_TLS sections.
7545
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7546
       8. PT_DYNAMIC should not contain empty sections at the beginning
7547
    (with the possible exception of .dynamic).  */
7548
0
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)   \
7549
0
  (((is_contained_by (section, segment, segment->p_paddr,    \
7550
0
          segment->p_vaddr, opb,        \
7551
0
          bed->want_p_paddr_set_to_zero)      \
7552
0
     && (section->flags & SEC_ALLOC) != 0)       \
7553
0
    || is_note (section, segment))         \
7554
0
   && segment->p_type != PT_GNU_STACK          \
7555
0
   && (segment->p_type != PT_TLS          \
7556
0
       || (section->flags & SEC_THREAD_LOCAL))        \
7557
0
   && (segment->p_type == PT_LOAD          \
7558
0
       || segment->p_type == PT_TLS          \
7559
0
       || (section->flags & SEC_THREAD_LOCAL) == 0)     \
7560
0
   && (segment->p_type != PT_DYNAMIC          \
7561
0
       || section_size (section, segment) > 0        \
7562
0
       || (segment->p_paddr            \
7563
0
     ? segment->p_paddr != section->lma * (opb)      \
7564
0
     : segment->p_vaddr != section->vma * (opb))     \
7565
0
       || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7566
0
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7567
7568
/* If the output section of a section in the input segment is NULL,
7569
   it is removed from the corresponding output segment.   */
7570
0
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)    \
7571
0
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)   \
7572
0
   && section->output_section != NULL)
7573
7574
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7575
0
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7576
0
  (seg1->field >= segment_end (seg2, seg2->field))
7577
7578
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7579
     their VMA address ranges and their LMA address ranges overlap.
7580
     It is possible to have overlapping VMA ranges without overlapping LMA
7581
     ranges.  RedBoot images for example can have both .data and .bss mapped
7582
     to the same VMA range, but with the .data section mapped to a different
7583
     LMA.  */
7584
0
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7585
0
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7586
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7587
0
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7588
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7589
7590
  /* Initialise the segment mark field, and discard stupid alignment.  */
7591
0
  for (section = ibfd->sections; section != NULL; section = section->next)
7592
0
    {
7593
0
      asection *o = section->output_section;
7594
0
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7595
0
  o->alignment_power = 0;
7596
0
      section->segment_mark = false;
7597
0
    }
7598
7599
  /* The Solaris linker creates program headers in which all the
7600
     p_paddr fields are zero.  When we try to objcopy or strip such a
7601
     file, we get confused.  Check for this case, and if we find it
7602
     don't set the p_paddr_valid fields.  */
7603
0
  p_paddr_valid = false;
7604
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7605
0
       i < num_segments;
7606
0
       i++, segment++)
7607
0
    if (segment->p_paddr != 0)
7608
0
      {
7609
0
  p_paddr_valid = true;
7610
0
  break;
7611
0
      }
7612
7613
  /* Scan through the segments specified in the program header
7614
     of the input BFD.  For this first scan we look for overlaps
7615
     in the loadable segments.  These can be created by weird
7616
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7617
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7618
0
       i < num_segments;
7619
0
       i++, segment++)
7620
0
    {
7621
0
      unsigned int j;
7622
0
      Elf_Internal_Phdr *segment2;
7623
7624
0
      if (segment->p_type == PT_INTERP)
7625
0
  for (section = ibfd->sections; section; section = section->next)
7626
0
    if (IS_SOLARIS_PT_INTERP (segment, section))
7627
0
      {
7628
        /* Mininal change so that the normal section to segment
7629
     assignment code will work.  */
7630
0
        segment->p_vaddr = section->vma * opb;
7631
0
        break;
7632
0
      }
7633
7634
0
      if (segment->p_type != PT_LOAD)
7635
0
  {
7636
    /* Remove PT_GNU_RELRO segment.  */
7637
0
    if (segment->p_type == PT_GNU_RELRO)
7638
0
      segment->p_type = PT_NULL;
7639
0
    continue;
7640
0
  }
7641
7642
      /* Determine if this segment overlaps any previous segments.  */
7643
0
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7644
0
  {
7645
0
    bfd_signed_vma extra_length;
7646
7647
0
    if (segment2->p_type != PT_LOAD
7648
0
        || !SEGMENT_OVERLAPS (segment, segment2))
7649
0
      continue;
7650
7651
    /* Merge the two segments together.  */
7652
0
    if (segment2->p_vaddr < segment->p_vaddr)
7653
0
      {
7654
        /* Extend SEGMENT2 to include SEGMENT and then delete
7655
     SEGMENT.  */
7656
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7657
0
            - segment_end (segment2, segment2->p_vaddr));
7658
7659
0
        if (extra_length > 0)
7660
0
    {
7661
0
      segment2->p_memsz += extra_length;
7662
0
      segment2->p_filesz += extra_length;
7663
0
    }
7664
7665
0
        segment->p_type = PT_NULL;
7666
7667
        /* Since we have deleted P we must restart the outer loop.  */
7668
0
        i = 0;
7669
0
        segment = elf_tdata (ibfd)->phdr;
7670
0
        break;
7671
0
      }
7672
0
    else
7673
0
      {
7674
        /* Extend SEGMENT to include SEGMENT2 and then delete
7675
     SEGMENT2.  */
7676
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7677
0
            - segment_end (segment, segment->p_vaddr));
7678
7679
0
        if (extra_length > 0)
7680
0
    {
7681
0
      segment->p_memsz += extra_length;
7682
0
      segment->p_filesz += extra_length;
7683
0
    }
7684
7685
0
        segment2->p_type = PT_NULL;
7686
0
      }
7687
0
  }
7688
0
    }
7689
7690
  /* The second scan attempts to assign sections to segments.  */
7691
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7692
0
       i < num_segments;
7693
0
       i++, segment++)
7694
0
    {
7695
0
      unsigned int section_count;
7696
0
      asection **sections;
7697
0
      asection *output_section;
7698
0
      unsigned int isec;
7699
0
      asection *matching_lma;
7700
0
      asection *suggested_lma;
7701
0
      unsigned int j;
7702
0
      size_t amt;
7703
0
      asection *first_section;
7704
7705
0
      if (segment->p_type == PT_NULL)
7706
0
  continue;
7707
7708
0
      first_section = NULL;
7709
      /* Compute how many sections might be placed into this segment.  */
7710
0
      for (section = ibfd->sections, section_count = 0;
7711
0
     section != NULL;
7712
0
     section = section->next)
7713
0
  {
7714
    /* Find the first section in the input segment, which may be
7715
       removed from the corresponding output segment.   */
7716
0
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7717
0
      {
7718
0
        if (first_section == NULL)
7719
0
    first_section = section;
7720
0
        if (section->output_section != NULL)
7721
0
    ++section_count;
7722
0
      }
7723
0
  }
7724
7725
      /* Allocate a segment map big enough to contain
7726
   all of the sections we have selected.  */
7727
0
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7728
0
      amt += section_count * sizeof (asection *);
7729
0
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7730
0
      if (map == NULL)
7731
0
  return false;
7732
7733
      /* Initialise the fields of the segment map.  Default to
7734
   using the physical address of the segment in the input BFD.  */
7735
0
      map->next = NULL;
7736
0
      map->p_type = segment->p_type;
7737
0
      map->p_flags = segment->p_flags;
7738
0
      map->p_flags_valid = 1;
7739
7740
0
      if (map->p_type == PT_LOAD
7741
0
    && (ibfd->flags & D_PAGED) != 0
7742
0
    && maxpagesize > 1
7743
0
    && segment->p_align > 1)
7744
0
  {
7745
0
    map->p_align = segment->p_align;
7746
0
    if (segment->p_align > maxpagesize)
7747
0
      map->p_align = maxpagesize;
7748
0
    map->p_align_valid = 1;
7749
0
  }
7750
7751
      /* If the first section in the input segment is removed, there is
7752
   no need to preserve segment physical address in the corresponding
7753
   output segment.  */
7754
0
      if (!first_section || first_section->output_section != NULL)
7755
0
  {
7756
0
    map->p_paddr = segment->p_paddr;
7757
0
    map->p_paddr_valid = p_paddr_valid;
7758
0
  }
7759
7760
      /* Determine if this segment contains the ELF file header
7761
   and if it contains the program headers themselves.  */
7762
0
      map->includes_filehdr = (segment->p_offset == 0
7763
0
             && segment->p_filesz >= iehdr->e_ehsize);
7764
0
      map->includes_phdrs = 0;
7765
7766
0
      if (!phdr_included || segment->p_type != PT_LOAD)
7767
0
  {
7768
0
    map->includes_phdrs =
7769
0
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7770
0
       && (segment->p_offset + segment->p_filesz
7771
0
     >= ((bfd_vma) iehdr->e_phoff
7772
0
         + iehdr->e_phnum * iehdr->e_phentsize)));
7773
7774
0
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7775
0
      phdr_included = true;
7776
0
  }
7777
7778
0
      if (section_count == 0)
7779
0
  {
7780
    /* Special segments, such as the PT_PHDR segment, may contain
7781
       no sections, but ordinary, loadable segments should contain
7782
       something.  They are allowed by the ELF spec however, so only
7783
       a warning is produced.
7784
       Don't warn if an empty PT_LOAD contains the program headers.
7785
       There is however the valid use case of embedded systems which
7786
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7787
       flash memory with zeros.  No warning is shown for that case.  */
7788
0
    if (segment->p_type == PT_LOAD
7789
0
        && !map->includes_phdrs
7790
0
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7791
      /* xgettext:c-format */
7792
0
      _bfd_error_handler
7793
0
        (_("%pB: warning: empty loadable segment detected"
7794
0
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7795
0
         ibfd, (uint64_t) segment->p_vaddr);
7796
7797
0
    map->p_vaddr_offset = segment->p_vaddr / opb;
7798
0
    map->count = 0;
7799
0
    *pointer_to_map = map;
7800
0
    pointer_to_map = &map->next;
7801
7802
0
    continue;
7803
0
  }
7804
7805
      /* Now scan the sections in the input BFD again and attempt
7806
   to add their corresponding output sections to the segment map.
7807
   The problem here is how to handle an output section which has
7808
   been moved (ie had its LMA changed).  There are four possibilities:
7809
7810
   1. None of the sections have been moved.
7811
      In this case we can continue to use the segment LMA from the
7812
      input BFD.
7813
7814
   2. All of the sections have been moved by the same amount.
7815
      In this case we can change the segment's LMA to match the LMA
7816
      of the first section.
7817
7818
   3. Some of the sections have been moved, others have not.
7819
      In this case those sections which have not been moved can be
7820
      placed in the current segment which will have to have its size,
7821
      and possibly its LMA changed, and a new segment or segments will
7822
      have to be created to contain the other sections.
7823
7824
   4. The sections have been moved, but not by the same amount.
7825
      In this case we can change the segment's LMA to match the LMA
7826
      of the first section and we will have to create a new segment
7827
      or segments to contain the other sections.
7828
7829
   In order to save time, we allocate an array to hold the section
7830
   pointers that we are interested in.  As these sections get assigned
7831
   to a segment, they are removed from this array.  */
7832
7833
0
      amt = section_count * sizeof (asection *);
7834
0
      sections = (asection **) bfd_malloc (amt);
7835
0
      if (sections == NULL)
7836
0
  return false;
7837
7838
      /* Step One: Scan for segment vs section LMA conflicts.
7839
   Also add the sections to the section array allocated above.
7840
   Also add the sections to the current segment.  In the common
7841
   case, where the sections have not been moved, this means that
7842
   we have completely filled the segment, and there is nothing
7843
   more to do.  */
7844
0
      isec = 0;
7845
0
      matching_lma = NULL;
7846
0
      suggested_lma = NULL;
7847
7848
0
      for (section = first_section, j = 0;
7849
0
     section != NULL;
7850
0
     section = section->next)
7851
0
  {
7852
0
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7853
0
      {
7854
0
        output_section = section->output_section;
7855
7856
0
        sections[j++] = section;
7857
7858
        /* The Solaris native linker always sets p_paddr to 0.
7859
     We try to catch that case here, and set it to the
7860
     correct value.  Note - some backends require that
7861
     p_paddr be left as zero.  */
7862
0
        if (!p_paddr_valid
7863
0
      && segment->p_vaddr != 0
7864
0
      && !bed->want_p_paddr_set_to_zero
7865
0
      && isec == 0
7866
0
      && output_section->lma != 0
7867
0
      && (align_power (segment->p_vaddr
7868
0
           + (map->includes_filehdr
7869
0
              ? iehdr->e_ehsize : 0)
7870
0
           + (map->includes_phdrs
7871
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7872
0
              : 0),
7873
0
           output_section->alignment_power * opb)
7874
0
          == (output_section->vma * opb)))
7875
0
    map->p_paddr = segment->p_vaddr;
7876
7877
        /* Match up the physical address of the segment with the
7878
     LMA address of the output section.  */
7879
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7880
0
           0, opb, false)
7881
0
      || is_note (section, segment))
7882
0
    {
7883
0
      if (matching_lma == NULL
7884
0
          || output_section->lma < matching_lma->lma)
7885
0
        matching_lma = output_section;
7886
7887
      /* We assume that if the section fits within the segment
7888
         then it does not overlap any other section within that
7889
         segment.  */
7890
0
      map->sections[isec++] = output_section;
7891
0
    }
7892
0
        else if (suggested_lma == NULL)
7893
0
    suggested_lma = output_section;
7894
7895
0
        if (j == section_count)
7896
0
    break;
7897
0
      }
7898
0
  }
7899
7900
0
      BFD_ASSERT (j == section_count);
7901
7902
      /* Step Two: Adjust the physical address of the current segment,
7903
   if necessary.  */
7904
0
      if (isec == section_count)
7905
0
  {
7906
    /* All of the sections fitted within the segment as currently
7907
       specified.  This is the default case.  Add the segment to
7908
       the list of built segments and carry on to process the next
7909
       program header in the input BFD.  */
7910
0
    map->count = section_count;
7911
0
    *pointer_to_map = map;
7912
0
    pointer_to_map = &map->next;
7913
7914
0
    if (p_paddr_valid
7915
0
        && !bed->want_p_paddr_set_to_zero)
7916
0
      {
7917
0
        bfd_vma hdr_size = 0;
7918
0
        if (map->includes_filehdr)
7919
0
    hdr_size = iehdr->e_ehsize;
7920
0
        if (map->includes_phdrs)
7921
0
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7922
7923
        /* Account for padding before the first section in the
7924
     segment.  */
7925
0
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7926
0
             - matching_lma->lma);
7927
0
      }
7928
7929
0
    free (sections);
7930
0
    continue;
7931
0
  }
7932
0
      else
7933
0
  {
7934
    /* Change the current segment's physical address to match
7935
       the LMA of the first section that fitted, or if no
7936
       section fitted, the first section.  */
7937
0
    if (matching_lma == NULL)
7938
0
      matching_lma = suggested_lma;
7939
7940
0
    map->p_paddr = matching_lma->lma * opb;
7941
7942
    /* Offset the segment physical address from the lma
7943
       to allow for space taken up by elf headers.  */
7944
0
    if (map->includes_phdrs)
7945
0
      {
7946
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7947
7948
        /* iehdr->e_phnum is just an estimate of the number
7949
     of program headers that we will need.  Make a note
7950
     here of the number we used and the segment we chose
7951
     to hold these headers, so that we can adjust the
7952
     offset when we know the correct value.  */
7953
0
        phdr_adjust_num = iehdr->e_phnum;
7954
0
        phdr_adjust_seg = map;
7955
0
      }
7956
7957
0
    if (map->includes_filehdr)
7958
0
      {
7959
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7960
0
        map->p_paddr -= iehdr->e_ehsize;
7961
        /* We've subtracted off the size of headers from the
7962
     first section lma, but there may have been some
7963
     alignment padding before that section too.  Try to
7964
     account for that by adjusting the segment lma down to
7965
     the same alignment.  */
7966
0
        if (segment->p_align != 0 && segment->p_align < align)
7967
0
    align = segment->p_align;
7968
0
        map->p_paddr &= -(align * opb);
7969
0
      }
7970
0
  }
7971
7972
      /* Step Three: Loop over the sections again, this time assigning
7973
   those that fit to the current segment and removing them from the
7974
   sections array; but making sure not to leave large gaps.  Once all
7975
   possible sections have been assigned to the current segment it is
7976
   added to the list of built segments and if sections still remain
7977
   to be assigned, a new segment is constructed before repeating
7978
   the loop.  */
7979
0
      isec = 0;
7980
0
      do
7981
0
  {
7982
0
    map->count = 0;
7983
0
    suggested_lma = NULL;
7984
7985
    /* Fill the current segment with sections that fit.  */
7986
0
    for (j = 0; j < section_count; j++)
7987
0
      {
7988
0
        section = sections[j];
7989
7990
0
        if (section == NULL)
7991
0
    continue;
7992
7993
0
        output_section = section->output_section;
7994
7995
0
        BFD_ASSERT (output_section != NULL);
7996
7997
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7998
0
           0, opb, false)
7999
0
      || is_note (section, segment))
8000
0
    {
8001
0
      if (map->count == 0)
8002
0
        {
8003
          /* If the first section in a segment does not start at
8004
       the beginning of the segment, then something is
8005
       wrong.  */
8006
0
          if (align_power (map->p_paddr
8007
0
               + (map->includes_filehdr
8008
0
            ? iehdr->e_ehsize : 0)
8009
0
               + (map->includes_phdrs
8010
0
            ? iehdr->e_phnum * iehdr->e_phentsize
8011
0
            : 0),
8012
0
               output_section->alignment_power * opb)
8013
0
        != output_section->lma * opb)
8014
0
      goto sorry;
8015
0
        }
8016
0
      else
8017
0
        {
8018
0
          asection *prev_sec;
8019
8020
0
          prev_sec = map->sections[map->count - 1];
8021
8022
          /* If the gap between the end of the previous section
8023
       and the start of this section is more than
8024
       maxpagesize then we need to start a new segment.  */
8025
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8026
0
              maxpagesize)
8027
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
8028
0
        || (prev_sec->lma + prev_sec->size
8029
0
            > output_section->lma))
8030
0
      {
8031
0
        if (suggested_lma == NULL)
8032
0
          suggested_lma = output_section;
8033
8034
0
        continue;
8035
0
      }
8036
0
        }
8037
8038
0
      map->sections[map->count++] = output_section;
8039
0
      ++isec;
8040
0
      sections[j] = NULL;
8041
0
      if (segment->p_type == PT_LOAD)
8042
0
        section->segment_mark = true;
8043
0
    }
8044
0
        else if (suggested_lma == NULL)
8045
0
    suggested_lma = output_section;
8046
0
      }
8047
8048
    /* PR 23932.  A corrupt input file may contain sections that cannot
8049
       be assigned to any segment - because for example they have a
8050
       negative size - or segments that do not contain any sections.
8051
       But there are also valid reasons why a segment can be empty.
8052
       So allow a count of zero.  */
8053
8054
    /* Add the current segment to the list of built segments.  */
8055
0
    *pointer_to_map = map;
8056
0
    pointer_to_map = &map->next;
8057
8058
0
    if (isec < section_count)
8059
0
      {
8060
        /* We still have not allocated all of the sections to
8061
     segments.  Create a new segment here, initialise it
8062
     and carry on looping.  */
8063
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8064
0
        amt += section_count * sizeof (asection *);
8065
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8066
0
        if (map == NULL)
8067
0
    {
8068
0
      free (sections);
8069
0
      return false;
8070
0
    }
8071
8072
        /* Initialise the fields of the segment map.  Set the physical
8073
     physical address to the LMA of the first section that has
8074
     not yet been assigned.  */
8075
0
        map->next = NULL;
8076
0
        map->p_type = segment->p_type;
8077
0
        map->p_flags = segment->p_flags;
8078
0
        map->p_flags_valid = 1;
8079
0
        map->p_paddr = suggested_lma->lma * opb;
8080
0
        map->p_paddr_valid = p_paddr_valid;
8081
0
        map->includes_filehdr = 0;
8082
0
        map->includes_phdrs = 0;
8083
0
      }
8084
8085
0
    continue;
8086
0
  sorry:
8087
0
    bfd_set_error (bfd_error_sorry);
8088
0
    free (sections);
8089
0
    return false;
8090
0
  }
8091
0
      while (isec < section_count);
8092
8093
0
      free (sections);
8094
0
    }
8095
8096
0
  elf_seg_map (obfd) = map_first;
8097
8098
  /* If we had to estimate the number of program headers that were
8099
     going to be needed, then check our estimate now and adjust
8100
     the offset if necessary.  */
8101
0
  if (phdr_adjust_seg != NULL)
8102
0
    {
8103
0
      unsigned int count;
8104
8105
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8106
0
  count++;
8107
8108
0
      if (count > phdr_adjust_num)
8109
0
  phdr_adjust_seg->p_paddr
8110
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8111
8112
0
      for (map = map_first; map != NULL; map = map->next)
8113
0
  if (map->p_type == PT_PHDR)
8114
0
    {
8115
0
      bfd_vma adjust
8116
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8117
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8118
0
      break;
8119
0
    }
8120
0
    }
8121
8122
0
#undef IS_SOLARIS_PT_INTERP
8123
0
#undef IS_SECTION_IN_INPUT_SEGMENT
8124
0
#undef INCLUDE_SECTION_IN_SEGMENT
8125
0
#undef SEGMENT_AFTER_SEGMENT
8126
0
#undef SEGMENT_OVERLAPS
8127
0
  return true;
8128
0
}
8129
8130
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8131
8132
static bool
8133
elf_is_p_align_valid (bfd *abfd)
8134
0
{
8135
0
  unsigned int i;
8136
0
  Elf_Internal_Phdr *segment;
8137
0
  unsigned int num_segments;
8138
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8139
0
  bfd_size_type maxpagesize = bed->maxpagesize;
8140
0
  bfd_size_type p_align = bed->p_align;
8141
8142
  /* Return true if the default p_align value isn't set or the maximum
8143
     page size is the same as the minimum page size.  */
8144
0
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8145
0
    return true;
8146
8147
  /* When the default p_align value is set, p_align may be set to the
8148
     default p_align value while segments are aligned to the maximum
8149
     page size.  In this case, the input p_align will be ignored and
8150
     the maximum page size will be used to align the output segments.  */
8151
0
  segment = elf_tdata (abfd)->phdr;
8152
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8153
0
  for (i = 0; i < num_segments; i++, segment++)
8154
0
    if (segment->p_type == PT_LOAD
8155
0
  && (segment->p_align != p_align
8156
0
      || vma_page_aligned_bias (segment->p_vaddr,
8157
0
              segment->p_offset,
8158
0
              maxpagesize) != 0))
8159
0
      return true;
8160
8161
0
  return false;
8162
0
}
8163
8164
/* Copy ELF program header information.  */
8165
8166
static bool
8167
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8168
0
{
8169
0
  Elf_Internal_Ehdr *iehdr;
8170
0
  struct elf_segment_map *map;
8171
0
  struct elf_segment_map *map_first;
8172
0
  struct elf_segment_map **pointer_to_map;
8173
0
  Elf_Internal_Phdr *segment;
8174
0
  unsigned int i;
8175
0
  unsigned int num_segments;
8176
0
  bool phdr_included = false;
8177
0
  bool p_paddr_valid;
8178
0
  bool p_palign_valid;
8179
0
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8180
8181
0
  iehdr = elf_elfheader (ibfd);
8182
8183
0
  map_first = NULL;
8184
0
  pointer_to_map = &map_first;
8185
8186
  /* If all the segment p_paddr fields are zero, don't set
8187
     map->p_paddr_valid.  */
8188
0
  p_paddr_valid = false;
8189
0
  num_segments = elf_elfheader (ibfd)->e_phnum;
8190
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8191
0
       i < num_segments;
8192
0
       i++, segment++)
8193
0
    if (segment->p_paddr != 0)
8194
0
      {
8195
0
  p_paddr_valid = true;
8196
0
  break;
8197
0
      }
8198
8199
0
  p_palign_valid = elf_is_p_align_valid (ibfd);
8200
8201
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8202
0
       i < num_segments;
8203
0
       i++, segment++)
8204
0
    {
8205
0
      asection *section;
8206
0
      unsigned int section_count;
8207
0
      size_t amt;
8208
0
      Elf_Internal_Shdr *this_hdr;
8209
0
      asection *first_section = NULL;
8210
0
      asection *lowest_section;
8211
8212
      /* Compute how many sections are in this segment.  */
8213
0
      for (section = ibfd->sections, section_count = 0;
8214
0
     section != NULL;
8215
0
     section = section->next)
8216
0
  {
8217
0
    this_hdr = &(elf_section_data(section)->this_hdr);
8218
0
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8219
0
      {
8220
0
        if (first_section == NULL)
8221
0
    first_section = section;
8222
0
        section_count++;
8223
0
      }
8224
0
  }
8225
8226
      /* Allocate a segment map big enough to contain
8227
   all of the sections we have selected.  */
8228
0
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8229
0
      amt += section_count * sizeof (asection *);
8230
0
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8231
0
      if (map == NULL)
8232
0
  return false;
8233
8234
      /* Initialize the fields of the output segment map with the
8235
   input segment.  */
8236
0
      map->next = NULL;
8237
0
      map->p_type = segment->p_type;
8238
0
      map->p_flags = segment->p_flags;
8239
0
      map->p_flags_valid = 1;
8240
0
      map->p_paddr = segment->p_paddr;
8241
0
      map->p_paddr_valid = p_paddr_valid;
8242
0
      map->p_align = segment->p_align;
8243
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8244
0
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8245
0
          || p_palign_valid);
8246
0
      map->p_vaddr_offset = 0;
8247
8248
0
      if (map->p_type == PT_GNU_RELRO
8249
0
    || map->p_type == PT_GNU_STACK)
8250
0
  {
8251
    /* The PT_GNU_RELRO segment may contain the first a few
8252
       bytes in the .got.plt section even if the whole .got.plt
8253
       section isn't in the PT_GNU_RELRO segment.  We won't
8254
       change the size of the PT_GNU_RELRO segment.
8255
       Similarly, PT_GNU_STACK size is significant on uclinux
8256
       systems.    */
8257
0
    map->p_size = segment->p_memsz;
8258
0
    map->p_size_valid = 1;
8259
0
  }
8260
8261
      /* Determine if this segment contains the ELF file header
8262
   and if it contains the program headers themselves.  */
8263
0
      map->includes_filehdr = (segment->p_offset == 0
8264
0
             && segment->p_filesz >= iehdr->e_ehsize);
8265
8266
0
      map->includes_phdrs = 0;
8267
0
      if (! phdr_included || segment->p_type != PT_LOAD)
8268
0
  {
8269
0
    map->includes_phdrs =
8270
0
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8271
0
       && (segment->p_offset + segment->p_filesz
8272
0
     >= ((bfd_vma) iehdr->e_phoff
8273
0
         + iehdr->e_phnum * iehdr->e_phentsize)));
8274
8275
0
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8276
0
      phdr_included = true;
8277
0
  }
8278
8279
0
      lowest_section = NULL;
8280
0
      if (section_count != 0)
8281
0
  {
8282
0
    unsigned int isec = 0;
8283
8284
0
    for (section = first_section;
8285
0
         section != NULL;
8286
0
         section = section->next)
8287
0
      {
8288
0
        this_hdr = &(elf_section_data(section)->this_hdr);
8289
0
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8290
0
    {
8291
0
      map->sections[isec++] = section->output_section;
8292
0
      if ((section->flags & SEC_ALLOC) != 0)
8293
0
        {
8294
0
          bfd_vma seg_off;
8295
8296
0
          if (lowest_section == NULL
8297
0
        || section->lma < lowest_section->lma)
8298
0
      lowest_section = section;
8299
8300
          /* Section lmas are set up from PT_LOAD header
8301
       p_paddr in _bfd_elf_make_section_from_shdr.
8302
       If this header has a p_paddr that disagrees
8303
       with the section lma, flag the p_paddr as
8304
       invalid.  */
8305
0
          if ((section->flags & SEC_LOAD) != 0)
8306
0
      seg_off = this_hdr->sh_offset - segment->p_offset;
8307
0
          else
8308
0
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8309
0
          if (section->lma * opb - segment->p_paddr != seg_off)
8310
0
      map->p_paddr_valid = false;
8311
0
        }
8312
0
      if (isec == section_count)
8313
0
        break;
8314
0
    }
8315
0
      }
8316
0
  }
8317
8318
0
      if (section_count == 0)
8319
0
  map->p_vaddr_offset = segment->p_vaddr / opb;
8320
0
      else if (map->p_paddr_valid)
8321
0
  {
8322
    /* Account for padding before the first section in the segment.  */
8323
0
    bfd_vma hdr_size = 0;
8324
0
    if (map->includes_filehdr)
8325
0
      hdr_size = iehdr->e_ehsize;
8326
0
    if (map->includes_phdrs)
8327
0
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8328
8329
0
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8330
0
         - (lowest_section ? lowest_section->lma : 0));
8331
0
  }
8332
8333
0
      map->count = section_count;
8334
0
      *pointer_to_map = map;
8335
0
      pointer_to_map = &map->next;
8336
0
    }
8337
8338
0
  elf_seg_map (obfd) = map_first;
8339
0
  return true;
8340
0
}
8341
8342
/* Copy private BFD data.  This copies or rewrites ELF program header
8343
   information.  */
8344
8345
static bool
8346
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8347
0
{
8348
0
  bfd_vma maxpagesize;
8349
8350
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8351
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8352
0
    return true;
8353
8354
0
  if (elf_tdata (ibfd)->phdr == NULL)
8355
0
    return true;
8356
8357
0
  if (ibfd->xvec == obfd->xvec)
8358
0
    {
8359
      /* Check to see if any sections in the input BFD
8360
   covered by ELF program header have changed.  */
8361
0
      Elf_Internal_Phdr *segment;
8362
0
      asection *section, *osec;
8363
0
      unsigned int i, num_segments;
8364
0
      Elf_Internal_Shdr *this_hdr;
8365
0
      const struct elf_backend_data *bed;
8366
8367
0
      bed = get_elf_backend_data (ibfd);
8368
8369
      /* Regenerate the segment map if p_paddr is set to 0.  */
8370
0
      if (bed->want_p_paddr_set_to_zero)
8371
0
  goto rewrite;
8372
8373
      /* Initialize the segment mark field.  */
8374
0
      for (section = obfd->sections; section != NULL;
8375
0
     section = section->next)
8376
0
  section->segment_mark = false;
8377
8378
0
      num_segments = elf_elfheader (ibfd)->e_phnum;
8379
0
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8380
0
     i < num_segments;
8381
0
     i++, segment++)
8382
0
  {
8383
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8384
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8385
       which severly confuses things, so always regenerate the segment
8386
       map in this case.  */
8387
0
    if (segment->p_paddr == 0
8388
0
        && segment->p_memsz == 0
8389
0
        && (segment->p_type == PT_INTERP
8390
0
      || segment->p_type == PT_DYNAMIC))
8391
0
      goto rewrite;
8392
8393
0
    for (section = ibfd->sections;
8394
0
         section != NULL; section = section->next)
8395
0
      {
8396
        /* We mark the output section so that we know it comes
8397
     from the input BFD.  */
8398
0
        osec = section->output_section;
8399
0
        if (osec)
8400
0
    osec->segment_mark = true;
8401
8402
        /* Check if this section is covered by the segment.  */
8403
0
        this_hdr = &(elf_section_data(section)->this_hdr);
8404
0
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8405
0
    {
8406
      /* FIXME: Check if its output section is changed or
8407
         removed.  What else do we need to check?  */
8408
0
      if (osec == NULL
8409
0
          || section->flags != osec->flags
8410
0
          || section->lma != osec->lma
8411
0
          || section->vma != osec->vma
8412
0
          || section->size != osec->size
8413
0
          || section->rawsize != osec->rawsize
8414
0
          || section->alignment_power != osec->alignment_power)
8415
0
        goto rewrite;
8416
0
    }
8417
0
      }
8418
0
  }
8419
8420
      /* Check to see if any output section do not come from the
8421
   input BFD.  */
8422
0
      for (section = obfd->sections; section != NULL;
8423
0
     section = section->next)
8424
0
  {
8425
0
    if (!section->segment_mark)
8426
0
      goto rewrite;
8427
0
    else
8428
0
      section->segment_mark = false;
8429
0
  }
8430
8431
0
      return copy_elf_program_header (ibfd, obfd);
8432
0
    }
8433
8434
0
 rewrite:
8435
0
  maxpagesize = 0;
8436
0
  if (ibfd->xvec == obfd->xvec)
8437
0
    {
8438
      /* When rewriting program header, set the output maxpagesize to
8439
   the maximum alignment of input PT_LOAD segments.  */
8440
0
      Elf_Internal_Phdr *segment;
8441
0
      unsigned int i;
8442
0
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8443
8444
0
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8445
0
     i < num_segments;
8446
0
     i++, segment++)
8447
0
  if (segment->p_type == PT_LOAD
8448
0
      && maxpagesize < segment->p_align)
8449
0
    {
8450
      /* PR 17512: file: f17299af.  */
8451
0
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8452
        /* xgettext:c-format */
8453
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8454
0
            PRIx64 " is too large"),
8455
0
          ibfd, (uint64_t) segment->p_align);
8456
0
      else
8457
0
        maxpagesize = segment->p_align;
8458
0
    }
8459
0
    }
8460
0
  if (maxpagesize == 0)
8461
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8462
8463
0
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8464
0
}
8465
8466
/* Initialize private output section information from input section.  */
8467
8468
bool
8469
_bfd_elf_init_private_section_data (bfd *ibfd,
8470
            asection *isec,
8471
            bfd *obfd,
8472
            asection *osec,
8473
            struct bfd_link_info *link_info)
8474
8475
0
{
8476
0
  Elf_Internal_Shdr *ihdr, *ohdr;
8477
0
  bool final_link = (link_info != NULL
8478
0
         && !bfd_link_relocatable (link_info));
8479
8480
0
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8481
0
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8482
0
    return true;
8483
8484
0
  BFD_ASSERT (elf_section_data (osec) != NULL);
8485
8486
  /* If this is a known ABI section, ELF section type and flags may
8487
     have been set up when OSEC was created.  For normal sections we
8488
     allow the user to override the type and flags other than
8489
     SHF_MASKOS and SHF_MASKPROC.  */
8490
0
  if (elf_section_type (osec) == SHT_PROGBITS
8491
0
      || elf_section_type (osec) == SHT_NOTE
8492
0
      || elf_section_type (osec) == SHT_NOBITS)
8493
0
    elf_section_type (osec) = SHT_NULL;
8494
  /* For objcopy and relocatable link, copy the ELF section type from
8495
     the input file if the BFD section flags are the same.  (If they
8496
     are different the user may be doing something like
8497
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8498
     link allow some flags that the linker clears to differ.  */
8499
0
  if (elf_section_type (osec) == SHT_NULL
8500
0
      && (osec->flags == isec->flags
8501
0
    || (final_link
8502
0
        && ((osec->flags ^ isec->flags)
8503
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8504
0
    elf_section_type (osec) = elf_section_type (isec);
8505
8506
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
8507
0
  elf_section_flags (osec) = (elf_section_flags (isec)
8508
0
            & (SHF_MASKOS | SHF_MASKPROC));
8509
8510
  /* Copy sh_info from input for mbind section.  */
8511
0
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8512
0
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8513
0
    elf_section_data (osec)->this_hdr.sh_info
8514
0
      = elf_section_data (isec)->this_hdr.sh_info;
8515
8516
  /* Set things up for objcopy and relocatable link.  The output
8517
     SHT_GROUP section will have its elf_next_in_group pointing back
8518
     to the input group members.  Ignore linker created group section.
8519
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8520
0
  if ((link_info == NULL
8521
0
       || !link_info->resolve_section_groups)
8522
0
      && (elf_sec_group (isec) == NULL
8523
0
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8524
0
    {
8525
0
      if (elf_section_flags (isec) & SHF_GROUP)
8526
0
  elf_section_flags (osec) |= SHF_GROUP;
8527
0
      elf_next_in_group (osec) = elf_next_in_group (isec);
8528
0
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8529
0
    }
8530
8531
  /* If not decompress, preserve SHF_COMPRESSED.  */
8532
0
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8533
0
    elf_section_flags (osec) |= (elf_section_flags (isec)
8534
0
         & SHF_COMPRESSED);
8535
8536
0
  ihdr = &elf_section_data (isec)->this_hdr;
8537
8538
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8539
     don't use the output section of the linked-to section since it
8540
     may be NULL at this point.  */
8541
0
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8542
0
    {
8543
0
      ohdr = &elf_section_data (osec)->this_hdr;
8544
0
      ohdr->sh_flags |= SHF_LINK_ORDER;
8545
0
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8546
0
    }
8547
8548
0
  osec->use_rela_p = isec->use_rela_p;
8549
8550
0
  return true;
8551
0
}
8552
8553
/* Copy private section information.  This copies over the entsize
8554
   field, and sometimes the info field.  */
8555
8556
bool
8557
_bfd_elf_copy_private_section_data (bfd *ibfd,
8558
            asection *isec,
8559
            bfd *obfd,
8560
            asection *osec)
8561
0
{
8562
0
  Elf_Internal_Shdr *ihdr, *ohdr;
8563
8564
0
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
8565
0
      || obfd->xvec->flavour != bfd_target_elf_flavour)
8566
0
    return true;
8567
8568
0
  ihdr = &elf_section_data (isec)->this_hdr;
8569
0
  ohdr = &elf_section_data (osec)->this_hdr;
8570
8571
0
  ohdr->sh_entsize = ihdr->sh_entsize;
8572
8573
0
  if (ihdr->sh_type == SHT_SYMTAB
8574
0
      || ihdr->sh_type == SHT_DYNSYM
8575
0
      || ihdr->sh_type == SHT_GNU_verneed
8576
0
      || ihdr->sh_type == SHT_GNU_verdef)
8577
0
    ohdr->sh_info = ihdr->sh_info;
8578
8579
0
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8580
0
               NULL);
8581
0
}
8582
8583
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8584
   necessary if we are removing either the SHT_GROUP section or any of
8585
   the group member sections.  DISCARDED is the value that a section's
8586
   output_section has if the section will be discarded, NULL when this
8587
   function is called from objcopy, bfd_abs_section_ptr when called
8588
   from the linker.  */
8589
8590
bool
8591
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8592
0
{
8593
0
  asection *isec;
8594
8595
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8596
0
    if (elf_section_type (isec) == SHT_GROUP)
8597
0
      {
8598
0
  asection *first = elf_next_in_group (isec);
8599
0
  asection *s = first;
8600
0
  bfd_size_type removed = 0;
8601
8602
0
  while (s != NULL)
8603
0
    {
8604
      /* If this member section is being output but the
8605
         SHT_GROUP section is not, then clear the group info
8606
         set up by _bfd_elf_copy_private_section_data.  */
8607
0
      if (s->output_section != discarded
8608
0
    && isec->output_section == discarded)
8609
0
        {
8610
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8611
0
    elf_group_name (s->output_section) = NULL;
8612
0
        }
8613
0
      else
8614
0
        {
8615
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8616
0
    if (s->output_section == discarded
8617
0
        && isec->output_section != discarded)
8618
0
      {
8619
        /* Conversely, if the member section is not being
8620
           output but the SHT_GROUP section is, then adjust
8621
           its size.  */
8622
0
        removed += 4;
8623
0
        if (elf_sec->rel.hdr != NULL
8624
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8625
0
          removed += 4;
8626
0
        if (elf_sec->rela.hdr != NULL
8627
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8628
0
          removed += 4;
8629
0
      }
8630
0
    else
8631
0
      {
8632
        /* Also adjust for zero-sized relocation member
8633
           section.  */
8634
0
        if (elf_sec->rel.hdr != NULL
8635
0
      && elf_sec->rel.hdr->sh_size == 0)
8636
0
          removed += 4;
8637
0
        if (elf_sec->rela.hdr != NULL
8638
0
      && elf_sec->rela.hdr->sh_size == 0)
8639
0
          removed += 4;
8640
0
      }
8641
0
        }
8642
0
      s = elf_next_in_group (s);
8643
0
      if (s == first)
8644
0
        break;
8645
0
    }
8646
0
  if (removed != 0)
8647
0
    {
8648
0
      if (discarded != NULL)
8649
0
        {
8650
    /* If we've been called for ld -r, then we need to
8651
       adjust the input section size.  */
8652
0
    if (isec->rawsize == 0)
8653
0
      isec->rawsize = isec->size;
8654
0
    isec->size = isec->rawsize - removed;
8655
0
    if (isec->size <= 4)
8656
0
      {
8657
0
        isec->size = 0;
8658
0
        isec->flags |= SEC_EXCLUDE;
8659
0
      }
8660
0
        }
8661
0
      else if (isec->output_section != NULL)
8662
0
        {
8663
    /* Adjust the output section size when called from
8664
       objcopy. */
8665
0
    isec->output_section->size -= removed;
8666
0
    if (isec->output_section->size <= 4)
8667
0
      {
8668
0
        isec->output_section->size = 0;
8669
0
        isec->output_section->flags |= SEC_EXCLUDE;
8670
0
      }
8671
0
        }
8672
0
    }
8673
0
      }
8674
8675
0
  return true;
8676
0
}
8677
8678
/* Copy private header information.  */
8679
8680
bool
8681
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8682
0
{
8683
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8684
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8685
0
    return true;
8686
8687
  /* Copy over private BFD data if it has not already been copied.
8688
     This must be done here, rather than in the copy_private_bfd_data
8689
     entry point, because the latter is called after the section
8690
     contents have been set, which means that the program headers have
8691
     already been worked out.  */
8692
0
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8693
0
    {
8694
0
      if (! copy_private_bfd_data (ibfd, obfd))
8695
0
  return false;
8696
0
    }
8697
8698
0
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8699
0
}
8700
8701
/* Copy private symbol information.  If this symbol is in a section
8702
   which we did not map into a BFD section, try to map the section
8703
   index correctly.  We use special macro definitions for the mapped
8704
   section indices; these definitions are interpreted by the
8705
   swap_out_syms function.  */
8706
8707
0
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8708
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8709
0
#define MAP_STRTAB    (SHN_HIOS + 3)
8710
0
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8711
0
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8712
8713
bool
8714
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8715
           asymbol *isymarg,
8716
           bfd *obfd,
8717
           asymbol *osymarg)
8718
0
{
8719
0
  elf_symbol_type *isym, *osym;
8720
8721
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8722
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8723
0
    return true;
8724
8725
0
  isym = elf_symbol_from (isymarg);
8726
0
  osym = elf_symbol_from (osymarg);
8727
8728
0
  if (isym != NULL
8729
0
      && isym->internal_elf_sym.st_shndx != 0
8730
0
      && osym != NULL
8731
0
      && bfd_is_abs_section (isym->symbol.section))
8732
0
    {
8733
0
      unsigned int shndx;
8734
8735
0
      shndx = isym->internal_elf_sym.st_shndx;
8736
0
      if (shndx == elf_onesymtab (ibfd))
8737
0
  shndx = MAP_ONESYMTAB;
8738
0
      else if (shndx == elf_dynsymtab (ibfd))
8739
0
  shndx = MAP_DYNSYMTAB;
8740
0
      else if (shndx == elf_strtab_sec (ibfd))
8741
0
  shndx = MAP_STRTAB;
8742
0
      else if (shndx == elf_shstrtab_sec (ibfd))
8743
0
  shndx = MAP_SHSTRTAB;
8744
0
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8745
0
  shndx = MAP_SYM_SHNDX;
8746
0
      osym->internal_elf_sym.st_shndx = shndx;
8747
0
    }
8748
8749
0
  return true;
8750
0
}
8751
8752
/* Swap out the symbols.  */
8753
8754
static bool
8755
swap_out_syms (bfd *abfd,
8756
         struct elf_strtab_hash **sttp,
8757
         int relocatable_p,
8758
         struct bfd_link_info *info)
8759
0
{
8760
0
  const struct elf_backend_data *bed;
8761
0
  unsigned int symcount;
8762
0
  asymbol **syms;
8763
0
  struct elf_strtab_hash *stt;
8764
0
  Elf_Internal_Shdr *symtab_hdr;
8765
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
8766
0
  Elf_Internal_Shdr *symstrtab_hdr;
8767
0
  struct elf_sym_strtab *symstrtab;
8768
0
  bfd_byte *outbound_syms;
8769
0
  bfd_byte *outbound_shndx;
8770
0
  unsigned long outbound_syms_index;
8771
0
  unsigned int idx;
8772
0
  unsigned int num_locals;
8773
0
  size_t amt;
8774
0
  bool name_local_sections;
8775
8776
0
  if (!elf_map_symbols (abfd, &num_locals))
8777
0
    return false;
8778
8779
  /* Dump out the symtabs.  */
8780
0
  stt = _bfd_elf_strtab_init ();
8781
0
  if (stt == NULL)
8782
0
    return false;
8783
8784
0
  bed = get_elf_backend_data (abfd);
8785
0
  symcount = bfd_get_symcount (abfd);
8786
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8787
0
  symtab_hdr->sh_type = SHT_SYMTAB;
8788
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8789
0
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8790
0
  symtab_hdr->sh_info = num_locals + 1;
8791
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8792
8793
0
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8794
0
  symstrtab_hdr->sh_type = SHT_STRTAB;
8795
8796
  /* Allocate buffer to swap out the .strtab section.  */
8797
0
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8798
0
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8799
0
    {
8800
0
      bfd_set_error (bfd_error_no_memory);
8801
0
      _bfd_elf_strtab_free (stt);
8802
0
      return false;
8803
0
    }
8804
8805
0
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8806
0
      || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8807
0
    {
8808
0
    error_no_mem:
8809
0
      bfd_set_error (bfd_error_no_memory);
8810
0
    error_return:
8811
0
      free (symstrtab);
8812
0
      _bfd_elf_strtab_free (stt);
8813
0
      return false;
8814
0
    }
8815
0
  symtab_hdr->contents = outbound_syms;
8816
0
  outbound_syms_index = 0;
8817
8818
0
  outbound_shndx = NULL;
8819
8820
0
  if (elf_symtab_shndx_list (abfd))
8821
0
    {
8822
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8823
0
      if (symtab_shndx_hdr->sh_name != 0)
8824
0
  {
8825
0
    if (_bfd_mul_overflow (symcount + 1,
8826
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8827
0
      goto error_no_mem;
8828
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8829
0
    if (outbound_shndx == NULL)
8830
0
      goto error_return;
8831
8832
0
    symtab_shndx_hdr->contents = outbound_shndx;
8833
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8834
0
    symtab_shndx_hdr->sh_size = amt;
8835
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8836
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8837
0
  }
8838
      /* FIXME: What about any other headers in the list ?  */
8839
0
    }
8840
8841
  /* Now generate the data (for "contents").  */
8842
0
  {
8843
    /* Fill in zeroth symbol and swap it out.  */
8844
0
    Elf_Internal_Sym sym;
8845
0
    sym.st_name = 0;
8846
0
    sym.st_value = 0;
8847
0
    sym.st_size = 0;
8848
0
    sym.st_info = 0;
8849
0
    sym.st_other = 0;
8850
0
    sym.st_shndx = SHN_UNDEF;
8851
0
    sym.st_target_internal = 0;
8852
0
    symstrtab[0].sym = sym;
8853
0
    symstrtab[0].dest_index = outbound_syms_index;
8854
0
    outbound_syms_index++;
8855
0
  }
8856
8857
0
  name_local_sections
8858
0
    = (bed->elf_backend_name_local_section_symbols
8859
0
       && bed->elf_backend_name_local_section_symbols (abfd));
8860
8861
0
  syms = bfd_get_outsymbols (abfd);
8862
0
  for (idx = 0; idx < symcount;)
8863
0
    {
8864
0
      Elf_Internal_Sym sym;
8865
0
      bfd_vma value = syms[idx]->value;
8866
0
      elf_symbol_type *type_ptr;
8867
0
      flagword flags = syms[idx]->flags;
8868
0
      int type;
8869
8870
0
      if (!name_local_sections
8871
0
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8872
0
  {
8873
    /* Local section symbols have no name.  */
8874
0
    sym.st_name = (unsigned long) -1;
8875
0
  }
8876
0
      else
8877
0
  {
8878
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8879
       to get the final offset for st_name.  */
8880
0
    sym.st_name
8881
0
      = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8882
0
               false);
8883
0
    if (sym.st_name == (unsigned long) -1)
8884
0
      goto error_return;
8885
0
  }
8886
8887
0
      type_ptr = elf_symbol_from (syms[idx]);
8888
8889
0
      if ((flags & BSF_SECTION_SYM) == 0
8890
0
    && bfd_is_com_section (syms[idx]->section))
8891
0
  {
8892
    /* ELF common symbols put the alignment into the `value' field,
8893
       and the size into the `size' field.  This is backwards from
8894
       how BFD handles it, so reverse it here.  */
8895
0
    sym.st_size = value;
8896
0
    if (type_ptr == NULL
8897
0
        || type_ptr->internal_elf_sym.st_value == 0)
8898
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8899
0
    else
8900
0
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8901
0
    sym.st_shndx = _bfd_elf_section_from_bfd_section
8902
0
      (abfd, syms[idx]->section);
8903
0
  }
8904
0
      else
8905
0
  {
8906
0
    asection *sec = syms[idx]->section;
8907
0
    unsigned int shndx;
8908
8909
0
    if (sec->output_section)
8910
0
      {
8911
0
        value += sec->output_offset;
8912
0
        sec = sec->output_section;
8913
0
      }
8914
8915
    /* Don't add in the section vma for relocatable output.  */
8916
0
    if (! relocatable_p)
8917
0
      value += sec->vma;
8918
0
    sym.st_value = value;
8919
0
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8920
8921
0
    if (bfd_is_abs_section (sec)
8922
0
        && type_ptr != NULL
8923
0
        && type_ptr->internal_elf_sym.st_shndx != 0)
8924
0
      {
8925
        /* This symbol is in a real ELF section which we did
8926
     not create as a BFD section.  Undo the mapping done
8927
     by copy_private_symbol_data.  */
8928
0
        shndx = type_ptr->internal_elf_sym.st_shndx;
8929
0
        switch (shndx)
8930
0
    {
8931
0
    case MAP_ONESYMTAB:
8932
0
      shndx = elf_onesymtab (abfd);
8933
0
      break;
8934
0
    case MAP_DYNSYMTAB:
8935
0
      shndx = elf_dynsymtab (abfd);
8936
0
      break;
8937
0
    case MAP_STRTAB:
8938
0
      shndx = elf_strtab_sec (abfd);
8939
0
      break;
8940
0
    case MAP_SHSTRTAB:
8941
0
      shndx = elf_shstrtab_sec (abfd);
8942
0
      break;
8943
0
    case MAP_SYM_SHNDX:
8944
0
      if (elf_symtab_shndx_list (abfd))
8945
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8946
0
      break;
8947
0
    case SHN_COMMON:
8948
0
    case SHN_ABS:
8949
0
      shndx = SHN_ABS;
8950
0
      break;
8951
0
    default:
8952
0
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8953
0
        {
8954
0
          if (bed->symbol_section_index)
8955
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8956
          /* Otherwise just leave the index alone.  */
8957
0
        }
8958
0
      else
8959
0
        {
8960
0
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8961
0
      _bfd_error_handler (_("%pB: \
8962
0
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8963
0
            abfd, shndx);
8964
0
          shndx = SHN_ABS;
8965
0
        }
8966
0
      break;
8967
0
    }
8968
0
      }
8969
0
    else
8970
0
      {
8971
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8972
8973
0
        if (shndx == SHN_BAD)
8974
0
    {
8975
0
      asection *sec2;
8976
8977
      /* Writing this would be a hell of a lot easier if
8978
         we had some decent documentation on bfd, and
8979
         knew what to expect of the library, and what to
8980
         demand of applications.  For example, it
8981
         appears that `objcopy' might not set the
8982
         section of a symbol to be a section that is
8983
         actually in the output file.  */
8984
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8985
0
      if (sec2 != NULL)
8986
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8987
0
      if (shndx == SHN_BAD)
8988
0
        {
8989
          /* xgettext:c-format */
8990
0
          _bfd_error_handler
8991
0
      (_("unable to find equivalent output section"
8992
0
         " for symbol '%s' from section '%s'"),
8993
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8994
0
       sec->name);
8995
0
          bfd_set_error (bfd_error_invalid_operation);
8996
0
          goto error_return;
8997
0
        }
8998
0
    }
8999
0
      }
9000
9001
0
    sym.st_shndx = shndx;
9002
0
  }
9003
9004
0
      if ((flags & BSF_THREAD_LOCAL) != 0)
9005
0
  type = STT_TLS;
9006
0
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9007
0
  type = STT_GNU_IFUNC;
9008
0
      else if ((flags & BSF_FUNCTION) != 0)
9009
0
  type = STT_FUNC;
9010
0
      else if ((flags & BSF_OBJECT) != 0)
9011
0
  type = STT_OBJECT;
9012
0
      else if ((flags & BSF_RELC) != 0)
9013
0
  type = STT_RELC;
9014
0
      else if ((flags & BSF_SRELC) != 0)
9015
0
  type = STT_SRELC;
9016
0
      else
9017
0
  type = STT_NOTYPE;
9018
9019
0
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9020
0
  type = STT_TLS;
9021
9022
      /* Processor-specific types.  */
9023
0
      if (type_ptr != NULL
9024
0
    && bed->elf_backend_get_symbol_type)
9025
0
  type = ((*bed->elf_backend_get_symbol_type)
9026
0
    (&type_ptr->internal_elf_sym, type));
9027
9028
0
      if (flags & BSF_SECTION_SYM)
9029
0
  {
9030
0
    if (flags & BSF_GLOBAL)
9031
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9032
0
    else
9033
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9034
0
  }
9035
0
      else if (bfd_is_com_section (syms[idx]->section))
9036
0
  {
9037
0
    if (type != STT_TLS)
9038
0
      {
9039
0
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9040
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9041
0
      ? STT_COMMON : STT_OBJECT);
9042
0
        else
9043
0
    type = ((flags & BSF_ELF_COMMON) != 0
9044
0
      ? STT_COMMON : STT_OBJECT);
9045
0
      }
9046
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9047
0
  }
9048
0
      else if (bfd_is_und_section (syms[idx]->section))
9049
0
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9050
0
            ? STB_WEAK
9051
0
            : STB_GLOBAL),
9052
0
           type);
9053
0
      else if (flags & BSF_FILE)
9054
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9055
0
      else
9056
0
  {
9057
0
    int bind = STB_LOCAL;
9058
9059
0
    if (flags & BSF_LOCAL)
9060
0
      bind = STB_LOCAL;
9061
0
    else if (flags & BSF_GNU_UNIQUE)
9062
0
      bind = STB_GNU_UNIQUE;
9063
0
    else if (flags & BSF_WEAK)
9064
0
      bind = STB_WEAK;
9065
0
    else if (flags & BSF_GLOBAL)
9066
0
      bind = STB_GLOBAL;
9067
9068
0
    sym.st_info = ELF_ST_INFO (bind, type);
9069
0
  }
9070
9071
0
      if (type_ptr != NULL)
9072
0
  {
9073
0
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9074
0
    sym.st_target_internal
9075
0
      = type_ptr->internal_elf_sym.st_target_internal;
9076
0
  }
9077
0
      else
9078
0
  {
9079
0
    sym.st_other = 0;
9080
0
    sym.st_target_internal = 0;
9081
0
  }
9082
9083
0
      idx++;
9084
0
      symstrtab[idx].sym = sym;
9085
0
      symstrtab[idx].dest_index = outbound_syms_index;
9086
9087
0
      outbound_syms_index++;
9088
0
    }
9089
9090
  /* Finalize the .strtab section.  */
9091
0
  _bfd_elf_strtab_finalize (stt);
9092
9093
  /* Swap out the .strtab section.  */
9094
0
  for (idx = 0; idx <= symcount; idx++)
9095
0
    {
9096
0
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9097
0
      if (elfsym->sym.st_name == (unsigned long) -1)
9098
0
  elfsym->sym.st_name = 0;
9099
0
      else
9100
0
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9101
0
                  elfsym->sym.st_name);
9102
0
      if (info && info->callbacks->ctf_new_symbol)
9103
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9104
0
           &elfsym->sym);
9105
9106
      /* Inform the linker of the addition of this symbol.  */
9107
9108
0
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9109
0
             (outbound_syms
9110
0
        + (elfsym->dest_index
9111
0
           * bed->s->sizeof_sym)),
9112
0
             NPTR_ADD (outbound_shndx,
9113
0
           (elfsym->dest_index
9114
0
            * sizeof (Elf_External_Sym_Shndx))));
9115
0
    }
9116
0
  free (symstrtab);
9117
9118
0
  *sttp = stt;
9119
0
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9120
0
  symstrtab_hdr->sh_type = SHT_STRTAB;
9121
0
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9122
0
  symstrtab_hdr->sh_addr = 0;
9123
0
  symstrtab_hdr->sh_entsize = 0;
9124
0
  symstrtab_hdr->sh_link = 0;
9125
0
  symstrtab_hdr->sh_info = 0;
9126
0
  symstrtab_hdr->sh_addralign = 1;
9127
9128
0
  return true;
9129
0
}
9130
9131
/* Return the number of bytes required to hold the symtab vector.
9132
9133
   Note that we base it on the count plus 1, since we will null terminate
9134
   the vector allocated based on this size.  However, the ELF symbol table
9135
   always has a dummy entry as symbol #0, so it ends up even.  */
9136
9137
long
9138
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9139
0
{
9140
0
  bfd_size_type symcount;
9141
0
  long symtab_size;
9142
0
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9143
9144
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9145
0
  if (symcount > LONG_MAX / sizeof (asymbol *))
9146
0
    {
9147
0
      bfd_set_error (bfd_error_file_too_big);
9148
0
      return -1;
9149
0
    }
9150
0
  symtab_size = symcount * (sizeof (asymbol *));
9151
0
  if (symcount == 0)
9152
0
    symtab_size = sizeof (asymbol *);
9153
0
  else if (!bfd_write_p (abfd))
9154
0
    {
9155
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
9156
9157
0
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9158
0
  {
9159
0
    bfd_set_error (bfd_error_file_truncated);
9160
0
    return -1;
9161
0
  }
9162
0
    }
9163
9164
0
  return symtab_size;
9165
0
}
9166
9167
long
9168
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9169
0
{
9170
0
  bfd_size_type symcount;
9171
0
  long symtab_size;
9172
0
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9173
9174
0
  if (elf_dynsymtab (abfd) == 0)
9175
0
    {
9176
      /* Check if there is dynamic symbol table.  */
9177
0
      symcount = elf_tdata (abfd)->dt_symtab_count;
9178
0
      if (symcount)
9179
0
  goto compute_symtab_size;
9180
9181
0
      bfd_set_error (bfd_error_invalid_operation);
9182
0
      return -1;
9183
0
    }
9184
9185
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9186
0
  if (symcount > LONG_MAX / sizeof (asymbol *))
9187
0
    {
9188
0
      bfd_set_error (bfd_error_file_too_big);
9189
0
      return -1;
9190
0
    }
9191
9192
0
 compute_symtab_size:
9193
0
  symtab_size = symcount * (sizeof (asymbol *));
9194
0
  if (symcount == 0)
9195
0
    symtab_size = sizeof (asymbol *);
9196
0
  else if (!bfd_write_p (abfd))
9197
0
    {
9198
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
9199
9200
0
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9201
0
  {
9202
0
    bfd_set_error (bfd_error_file_truncated);
9203
0
    return -1;
9204
0
  }
9205
0
    }
9206
9207
0
  return symtab_size;
9208
0
}
9209
9210
long
9211
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9212
0
{
9213
0
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9214
0
    {
9215
      /* Sanity check reloc section size.  */
9216
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
9217
9218
0
      if (filesize != 0)
9219
0
  {
9220
0
    struct bfd_elf_section_data *d = elf_section_data (asect);
9221
0
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9222
0
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9223
9224
0
    if (rel_size + rela_size > filesize
9225
0
        || rel_size + rela_size < rel_size)
9226
0
      {
9227
0
        bfd_set_error (bfd_error_file_truncated);
9228
0
        return -1;
9229
0
      }
9230
0
  }
9231
0
    }
9232
9233
#if SIZEOF_LONG == SIZEOF_INT
9234
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9235
    {
9236
      bfd_set_error (bfd_error_file_too_big);
9237
      return -1;
9238
    }
9239
#endif
9240
0
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9241
0
}
9242
9243
/* Canonicalize the relocs.  */
9244
9245
long
9246
_bfd_elf_canonicalize_reloc (bfd *abfd,
9247
           sec_ptr section,
9248
           arelent **relptr,
9249
           asymbol **symbols)
9250
0
{
9251
0
  arelent *tblptr;
9252
0
  unsigned int i;
9253
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9254
9255
0
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9256
0
    return -1;
9257
9258
0
  tblptr = section->relocation;
9259
0
  for (i = 0; i < section->reloc_count; i++)
9260
0
    *relptr++ = tblptr++;
9261
9262
0
  *relptr = NULL;
9263
9264
0
  return section->reloc_count;
9265
0
}
9266
9267
long
9268
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9269
0
{
9270
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9271
0
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9272
9273
0
  if (symcount >= 0)
9274
0
    abfd->symcount = symcount;
9275
0
  return symcount;
9276
0
}
9277
9278
long
9279
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9280
              asymbol **allocation)
9281
0
{
9282
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9283
0
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9284
9285
0
  if (symcount >= 0)
9286
0
    abfd->dynsymcount = symcount;
9287
0
  return symcount;
9288
0
}
9289
9290
/* Return the size required for the dynamic reloc entries.  Any loadable
9291
   section that was actually installed in the BFD, and has type SHT_REL
9292
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9293
   dynamic reloc section.  */
9294
9295
long
9296
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9297
0
{
9298
0
  bfd_size_type count, ext_rel_size;
9299
0
  asection *s;
9300
9301
0
  if (elf_dynsymtab (abfd) == 0)
9302
0
    {
9303
0
      bfd_set_error (bfd_error_invalid_operation);
9304
0
      return -1;
9305
0
    }
9306
9307
0
  count = 1;
9308
0
  ext_rel_size = 0;
9309
0
  for (s = abfd->sections; s != NULL; s = s->next)
9310
0
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9311
0
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9312
0
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9313
0
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9314
0
      {
9315
0
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9316
0
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9317
0
    {
9318
0
      bfd_set_error (bfd_error_file_truncated);
9319
0
      return -1;
9320
0
    }
9321
0
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9322
0
  if (count > LONG_MAX / sizeof (arelent *))
9323
0
    {
9324
0
      bfd_set_error (bfd_error_file_too_big);
9325
0
      return -1;
9326
0
    }
9327
0
      }
9328
0
  if (count > 1 && !bfd_write_p (abfd))
9329
0
    {
9330
      /* Sanity check reloc section sizes.  */
9331
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
9332
0
      if (filesize != 0 && ext_rel_size > filesize)
9333
0
  {
9334
0
    bfd_set_error (bfd_error_file_truncated);
9335
0
    return -1;
9336
0
  }
9337
0
    }
9338
0
  return count * sizeof (arelent *);
9339
0
}
9340
9341
/* Canonicalize the dynamic relocation entries.  Note that we return the
9342
   dynamic relocations as a single block, although they are actually
9343
   associated with particular sections; the interface, which was
9344
   designed for SunOS style shared libraries, expects that there is only
9345
   one set of dynamic relocs.  Any loadable section that was actually
9346
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9347
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9348
9349
long
9350
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9351
             arelent **storage,
9352
             asymbol **syms)
9353
0
{
9354
0
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9355
0
  asection *s;
9356
0
  long ret;
9357
9358
0
  if (elf_dynsymtab (abfd) == 0)
9359
0
    {
9360
0
      bfd_set_error (bfd_error_invalid_operation);
9361
0
      return -1;
9362
0
    }
9363
9364
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9365
0
  ret = 0;
9366
0
  for (s = abfd->sections; s != NULL; s = s->next)
9367
0
    {
9368
0
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9369
0
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9370
0
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9371
0
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9372
0
  {
9373
0
    arelent *p;
9374
0
    long count, i;
9375
9376
0
    if (! (*slurp_relocs) (abfd, s, syms, true))
9377
0
      return -1;
9378
0
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9379
0
    p = s->relocation;
9380
0
    for (i = 0; i < count; i++)
9381
0
      *storage++ = p++;
9382
0
    ret += count;
9383
0
  }
9384
0
    }
9385
9386
0
  *storage = NULL;
9387
9388
0
  return ret;
9389
0
}
9390

9391
/* Read in the version information.  */
9392
9393
bool
9394
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9395
0
{
9396
0
  bfd_byte *contents = NULL;
9397
0
  unsigned int freeidx = 0;
9398
0
  size_t amt;
9399
9400
0
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9401
0
    {
9402
0
      Elf_Internal_Shdr *hdr;
9403
0
      Elf_External_Verneed *everneed;
9404
0
      Elf_Internal_Verneed *iverneed;
9405
0
      unsigned int i;
9406
0
      bfd_byte *contents_end;
9407
0
      size_t verneed_count;
9408
0
      size_t verneed_size;
9409
9410
0
      if (elf_tdata (abfd)->dt_verneed != NULL)
9411
0
  {
9412
0
    hdr = NULL;
9413
0
    contents = elf_tdata (abfd)->dt_verneed;
9414
0
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9415
0
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9416
0
  }
9417
0
      else
9418
0
  {
9419
0
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9420
9421
0
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9422
0
      {
9423
0
      error_return_bad_verref:
9424
0
        _bfd_error_handler
9425
0
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9426
0
        bfd_set_error (bfd_error_bad_value);
9427
0
      error_return_verref:
9428
0
        elf_tdata (abfd)->verref = NULL;
9429
0
        elf_tdata (abfd)->cverrefs = 0;
9430
0
        goto error_return;
9431
0
      }
9432
9433
0
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9434
0
      goto error_return_verref;
9435
0
    contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9436
0
    if (contents == NULL)
9437
0
      goto error_return_verref;
9438
9439
0
    verneed_size = hdr->sh_size;
9440
0
    verneed_count = hdr->sh_info;
9441
0
  }
9442
9443
0
      if (_bfd_mul_overflow (verneed_count,
9444
0
           sizeof (Elf_Internal_Verneed), &amt))
9445
0
  {
9446
0
    bfd_set_error (bfd_error_file_too_big);
9447
0
    goto error_return_verref;
9448
0
  }
9449
0
      if (amt == 0)
9450
0
  goto error_return_verref;
9451
0
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9452
0
      if (elf_tdata (abfd)->verref == NULL)
9453
0
  goto error_return_verref;
9454
9455
0
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9456
0
      == sizeof (Elf_External_Vernaux));
9457
0
      contents_end = (contents + verneed_size
9458
0
          - sizeof (Elf_External_Verneed));
9459
0
      everneed = (Elf_External_Verneed *) contents;
9460
0
      iverneed = elf_tdata (abfd)->verref;
9461
0
      for (i = 0; i < verneed_count; i++, iverneed++)
9462
0
  {
9463
0
    Elf_External_Vernaux *evernaux;
9464
0
    Elf_Internal_Vernaux *ivernaux;
9465
0
    unsigned int j;
9466
9467
0
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9468
9469
0
    iverneed->vn_bfd = abfd;
9470
9471
0
    if (elf_use_dt_symtab_p (abfd))
9472
0
      iverneed->vn_filename
9473
0
        = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9474
0
    else
9475
0
      iverneed->vn_filename
9476
0
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9477
0
             iverneed->vn_file);
9478
0
    if (iverneed->vn_filename == NULL)
9479
0
      goto error_return_bad_verref;
9480
9481
0
    if (iverneed->vn_cnt == 0)
9482
0
      iverneed->vn_auxptr = NULL;
9483
0
    else
9484
0
      {
9485
0
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9486
0
             sizeof (Elf_Internal_Vernaux), &amt))
9487
0
    {
9488
0
      bfd_set_error (bfd_error_file_too_big);
9489
0
      goto error_return_verref;
9490
0
    }
9491
0
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9492
0
    bfd_alloc (abfd, amt);
9493
0
        if (iverneed->vn_auxptr == NULL)
9494
0
    goto error_return_verref;
9495
0
      }
9496
9497
0
    if (iverneed->vn_aux
9498
0
        > (size_t) (contents_end - (bfd_byte *) everneed))
9499
0
      goto error_return_bad_verref;
9500
9501
0
    evernaux = ((Elf_External_Vernaux *)
9502
0
          ((bfd_byte *) everneed + iverneed->vn_aux));
9503
0
    ivernaux = iverneed->vn_auxptr;
9504
0
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9505
0
      {
9506
0
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9507
9508
0
        if (elf_use_dt_symtab_p (abfd))
9509
0
    ivernaux->vna_nodename
9510
0
      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9511
0
        else
9512
0
    ivernaux->vna_nodename
9513
0
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9514
0
                 ivernaux->vna_name);
9515
0
        if (ivernaux->vna_nodename == NULL)
9516
0
    goto error_return_bad_verref;
9517
9518
0
        if (ivernaux->vna_other > freeidx)
9519
0
    freeidx = ivernaux->vna_other;
9520
9521
0
        ivernaux->vna_nextptr = NULL;
9522
0
        if (ivernaux->vna_next == 0)
9523
0
    {
9524
0
      iverneed->vn_cnt = j + 1;
9525
0
      break;
9526
0
    }
9527
0
        if (j + 1 < iverneed->vn_cnt)
9528
0
    ivernaux->vna_nextptr = ivernaux + 1;
9529
9530
0
        if (ivernaux->vna_next
9531
0
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9532
0
    goto error_return_bad_verref;
9533
9534
0
        evernaux = ((Elf_External_Vernaux *)
9535
0
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9536
0
      }
9537
9538
0
    iverneed->vn_nextref = NULL;
9539
0
    if (iverneed->vn_next == 0)
9540
0
      break;
9541
0
    if (i + 1 < hdr->sh_info)
9542
0
      iverneed->vn_nextref = iverneed + 1;
9543
9544
0
    if (iverneed->vn_next
9545
0
        > (size_t) (contents_end - (bfd_byte *) everneed))
9546
0
      goto error_return_bad_verref;
9547
9548
0
    everneed = ((Elf_External_Verneed *)
9549
0
          ((bfd_byte *) everneed + iverneed->vn_next));
9550
0
  }
9551
0
      elf_tdata (abfd)->cverrefs = i;
9552
9553
0
      if (elf_tdata (abfd)->dt_verneed == NULL)
9554
0
  free (contents);
9555
0
      contents = NULL;
9556
0
    }
9557
9558
0
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9559
0
    {
9560
0
      Elf_Internal_Shdr *hdr;
9561
0
      Elf_External_Verdef *everdef;
9562
0
      Elf_Internal_Verdef *iverdef;
9563
0
      Elf_Internal_Verdef *iverdefarr;
9564
0
      Elf_Internal_Verdef iverdefmem;
9565
0
      unsigned int i;
9566
0
      unsigned int maxidx;
9567
0
      bfd_byte *contents_end_def, *contents_end_aux;
9568
0
      size_t verdef_count;
9569
0
      size_t verdef_size;
9570
9571
0
      if (elf_tdata (abfd)->dt_verdef != NULL)
9572
0
  {
9573
0
    hdr = NULL;
9574
0
    contents = elf_tdata (abfd)->dt_verdef;
9575
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9576
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9577
0
  }
9578
0
      else
9579
0
  {
9580
0
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9581
9582
0
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9583
0
      {
9584
0
      error_return_bad_verdef:
9585
0
        _bfd_error_handler
9586
0
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9587
0
        bfd_set_error (bfd_error_bad_value);
9588
0
      error_return_verdef:
9589
0
        elf_tdata (abfd)->verdef = NULL;
9590
0
        elf_tdata (abfd)->cverdefs = 0;
9591
0
        goto error_return;
9592
0
      }
9593
9594
0
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9595
0
      goto error_return_verdef;
9596
0
    contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9597
0
    if (contents == NULL)
9598
0
      goto error_return_verdef;
9599
9600
0
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9601
0
          >= sizeof (Elf_External_Verdaux));
9602
9603
0
    verdef_count = hdr->sh_info;
9604
0
    verdef_size = hdr->sh_size;
9605
0
  }
9606
9607
0
      contents_end_def = (contents + verdef_size
9608
0
        - sizeof (Elf_External_Verdef));
9609
0
      contents_end_aux = (contents + verdef_size
9610
0
        - sizeof (Elf_External_Verdaux));
9611
9612
      /* We know the number of entries in the section but not the maximum
9613
   index.  Therefore we have to run through all entries and find
9614
   the maximum.  */
9615
0
      everdef = (Elf_External_Verdef *) contents;
9616
0
      maxidx = 0;
9617
0
      for (i = 0; i < verdef_count; ++i)
9618
0
  {
9619
0
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9620
9621
0
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9622
0
      goto error_return_bad_verdef;
9623
0
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9624
0
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9625
9626
0
    if (iverdefmem.vd_next == 0)
9627
0
      break;
9628
9629
0
    if (iverdefmem.vd_next
9630
0
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9631
0
      goto error_return_bad_verdef;
9632
9633
0
    everdef = ((Elf_External_Verdef *)
9634
0
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9635
0
  }
9636
9637
0
      if (default_imported_symver)
9638
0
  {
9639
0
    if (freeidx > maxidx)
9640
0
      maxidx = ++freeidx;
9641
0
    else
9642
0
      freeidx = ++maxidx;
9643
0
  }
9644
0
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9645
0
  {
9646
0
    bfd_set_error (bfd_error_file_too_big);
9647
0
    goto error_return_verdef;
9648
0
  }
9649
9650
0
      if (amt == 0)
9651
0
  goto error_return_verdef;
9652
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9653
0
      if (elf_tdata (abfd)->verdef == NULL)
9654
0
  goto error_return_verdef;
9655
9656
0
      elf_tdata (abfd)->cverdefs = maxidx;
9657
9658
0
      everdef = (Elf_External_Verdef *) contents;
9659
0
      iverdefarr = elf_tdata (abfd)->verdef;
9660
0
      for (i = 0; i < verdef_count; ++i)
9661
0
  {
9662
0
    Elf_External_Verdaux *everdaux;
9663
0
    Elf_Internal_Verdaux *iverdaux;
9664
0
    unsigned int j;
9665
9666
0
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9667
9668
0
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9669
0
      goto error_return_bad_verdef;
9670
9671
0
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9672
0
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9673
9674
0
    iverdef->vd_bfd = abfd;
9675
9676
0
    if (iverdef->vd_cnt == 0)
9677
0
      iverdef->vd_auxptr = NULL;
9678
0
    else
9679
0
      {
9680
0
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9681
0
             sizeof (Elf_Internal_Verdaux), &amt))
9682
0
    {
9683
0
      bfd_set_error (bfd_error_file_too_big);
9684
0
      goto error_return_verdef;
9685
0
    }
9686
0
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9687
0
    bfd_alloc (abfd, amt);
9688
0
        if (iverdef->vd_auxptr == NULL)
9689
0
    goto error_return_verdef;
9690
0
      }
9691
9692
0
    if (iverdef->vd_aux
9693
0
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9694
0
      goto error_return_bad_verdef;
9695
9696
0
    everdaux = ((Elf_External_Verdaux *)
9697
0
          ((bfd_byte *) everdef + iverdef->vd_aux));
9698
0
    iverdaux = iverdef->vd_auxptr;
9699
0
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9700
0
      {
9701
0
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9702
9703
0
        if (elf_use_dt_symtab_p (abfd))
9704
0
    iverdaux->vda_nodename
9705
0
      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9706
0
        else
9707
0
    iverdaux->vda_nodename
9708
0
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9709
0
                 iverdaux->vda_name);
9710
0
        if (iverdaux->vda_nodename == NULL)
9711
0
    goto error_return_bad_verdef;
9712
9713
0
        iverdaux->vda_nextptr = NULL;
9714
0
        if (iverdaux->vda_next == 0)
9715
0
    {
9716
0
      iverdef->vd_cnt = j + 1;
9717
0
      break;
9718
0
    }
9719
0
        if (j + 1 < iverdef->vd_cnt)
9720
0
    iverdaux->vda_nextptr = iverdaux + 1;
9721
9722
0
        if (iverdaux->vda_next
9723
0
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9724
0
    goto error_return_bad_verdef;
9725
9726
0
        everdaux = ((Elf_External_Verdaux *)
9727
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9728
0
      }
9729
9730
0
    iverdef->vd_nodename = NULL;
9731
0
    if (iverdef->vd_cnt)
9732
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9733
9734
0
    iverdef->vd_nextdef = NULL;
9735
0
    if (iverdef->vd_next == 0)
9736
0
      break;
9737
0
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9738
0
      iverdef->vd_nextdef = iverdef + 1;
9739
9740
0
    everdef = ((Elf_External_Verdef *)
9741
0
         ((bfd_byte *) everdef + iverdef->vd_next));
9742
0
  }
9743
9744
0
      if (elf_tdata (abfd)->dt_verdef == NULL)
9745
0
  free (contents);
9746
0
      contents = NULL;
9747
0
    }
9748
0
  else if (default_imported_symver)
9749
0
    {
9750
0
      if (freeidx < 3)
9751
0
  freeidx = 3;
9752
0
      else
9753
0
  freeidx++;
9754
9755
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9756
0
  {
9757
0
    bfd_set_error (bfd_error_file_too_big);
9758
0
    goto error_return;
9759
0
  }
9760
0
      if (amt == 0)
9761
0
  goto error_return;
9762
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9763
0
      if (elf_tdata (abfd)->verdef == NULL)
9764
0
  goto error_return;
9765
9766
0
      elf_tdata (abfd)->cverdefs = freeidx;
9767
0
    }
9768
9769
  /* Create a default version based on the soname.  */
9770
0
  if (default_imported_symver)
9771
0
    {
9772
0
      Elf_Internal_Verdef *iverdef;
9773
0
      Elf_Internal_Verdaux *iverdaux;
9774
9775
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9776
9777
0
      iverdef->vd_version = VER_DEF_CURRENT;
9778
0
      iverdef->vd_flags = 0;
9779
0
      iverdef->vd_ndx = freeidx;
9780
0
      iverdef->vd_cnt = 1;
9781
9782
0
      iverdef->vd_bfd = abfd;
9783
9784
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9785
0
      if (iverdef->vd_nodename == NULL)
9786
0
  goto error_return_verdef;
9787
0
      iverdef->vd_nextdef = NULL;
9788
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9789
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9790
0
      if (iverdef->vd_auxptr == NULL)
9791
0
  goto error_return_verdef;
9792
9793
0
      iverdaux = iverdef->vd_auxptr;
9794
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9795
0
    }
9796
9797
0
  return true;
9798
9799
0
 error_return:
9800
0
  free (contents);
9801
0
  return false;
9802
0
}
9803

9804
asymbol *
9805
_bfd_elf_make_empty_symbol (bfd *abfd)
9806
0
{
9807
0
  elf_symbol_type *newsym;
9808
9809
0
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9810
0
  if (!newsym)
9811
0
    return NULL;
9812
0
  newsym->symbol.the_bfd = abfd;
9813
0
  return &newsym->symbol;
9814
0
}
9815
9816
void
9817
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9818
        asymbol *symbol,
9819
        symbol_info *ret)
9820
0
{
9821
0
  bfd_symbol_info (symbol, ret);
9822
0
}
9823
9824
/* Return whether a symbol name implies a local symbol.  Most targets
9825
   use this function for the is_local_label_name entry point, but some
9826
   override it.  */
9827
9828
bool
9829
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9830
            const char *name)
9831
0
{
9832
  /* Normal local symbols start with ``.L''.  */
9833
0
  if (name[0] == '.' && name[1] == 'L')
9834
0
    return true;
9835
9836
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9837
     DWARF debugging symbols starting with ``..''.  */
9838
0
  if (name[0] == '.' && name[1] == '.')
9839
0
    return true;
9840
9841
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9842
     emitting DWARF debugging output.  I suspect this is actually a
9843
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9844
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9845
     underscore to be emitted on some ELF targets).  For ease of use,
9846
     we treat such symbols as local.  */
9847
0
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9848
0
    return true;
9849
9850
  /* Treat assembler generated fake symbols, dollar local labels and
9851
     forward-backward labels (aka local labels) as locals.
9852
     These labels have the form:
9853
9854
       L0^A.*              (fake symbols)
9855
9856
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9857
9858
     Versions which start with .L will have already been matched above,
9859
     so we only need to match the rest.  */
9860
0
  if (name[0] == 'L' && ISDIGIT (name[1]))
9861
0
    {
9862
0
      bool ret = false;
9863
0
      const char * p;
9864
0
      char c;
9865
9866
0
      for (p = name + 2; (c = *p); p++)
9867
0
  {
9868
0
    if (c == 1 || c == 2)
9869
0
      {
9870
0
        if (c == 1 && p == name + 2)
9871
    /* A fake symbol.  */
9872
0
    return true;
9873
9874
        /* FIXME: We are being paranoid here and treating symbols like
9875
     L0^Bfoo as if there were non-local, on the grounds that the
9876
     assembler will never generate them.  But can any symbol
9877
     containing an ASCII value in the range 1-31 ever be anything
9878
     other than some kind of local ?  */
9879
0
        ret = true;
9880
0
      }
9881
9882
0
    if (! ISDIGIT (c))
9883
0
      {
9884
0
        ret = false;
9885
0
        break;
9886
0
      }
9887
0
  }
9888
0
      return ret;
9889
0
    }
9890
9891
0
  return false;
9892
0
}
9893
9894
alent *
9895
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9896
         asymbol *symbol ATTRIBUTE_UNUSED)
9897
0
{
9898
0
  abort ();
9899
0
  return NULL;
9900
0
}
9901
9902
bool
9903
_bfd_elf_set_arch_mach (bfd *abfd,
9904
      enum bfd_architecture arch,
9905
      unsigned long machine)
9906
0
{
9907
  /* If this isn't the right architecture for this backend, and this
9908
     isn't the generic backend, fail.  */
9909
0
  if (arch != get_elf_backend_data (abfd)->arch
9910
0
      && arch != bfd_arch_unknown
9911
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9912
0
    return false;
9913
9914
0
  return bfd_default_set_arch_mach (abfd, arch, machine);
9915
0
}
9916
9917
/* Find the nearest line to a particular section and offset,
9918
   for error reporting.  */
9919
9920
bool
9921
_bfd_elf_find_nearest_line (bfd *abfd,
9922
          asymbol **symbols,
9923
          asection *section,
9924
          bfd_vma offset,
9925
          const char **filename_ptr,
9926
          const char **functionname_ptr,
9927
          unsigned int *line_ptr,
9928
          unsigned int *discriminator_ptr)
9929
0
{
9930
0
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9931
0
                offset, filename_ptr,
9932
0
                functionname_ptr, line_ptr,
9933
0
                discriminator_ptr);
9934
0
}
9935
9936
/* Find the nearest line to a particular section and offset,
9937
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9938
   can be optionally specified.  */
9939
9940
bool
9941
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9942
             const char *alt_filename,
9943
             asymbol **symbols,
9944
             asection *section,
9945
             bfd_vma offset,
9946
             const char **filename_ptr,
9947
             const char **functionname_ptr,
9948
             unsigned int *line_ptr,
9949
             unsigned int *discriminator_ptr)
9950
0
{
9951
0
  bool found;
9952
9953
0
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9954
0
                section, offset, filename_ptr,
9955
0
                functionname_ptr, line_ptr,
9956
0
                discriminator_ptr,
9957
0
                dwarf_debug_sections,
9958
0
                &elf_tdata (abfd)->dwarf2_find_line_info))
9959
0
    return true;
9960
9961
0
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9962
0
             filename_ptr, functionname_ptr, line_ptr))
9963
0
    {
9964
0
      if (!*functionname_ptr)
9965
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9966
0
        *filename_ptr ? NULL : filename_ptr,
9967
0
        functionname_ptr);
9968
0
      return true;
9969
0
    }
9970
9971
0
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9972
0
               &found, filename_ptr,
9973
0
               functionname_ptr, line_ptr,
9974
0
               &elf_tdata (abfd)->line_info))
9975
0
    return false;
9976
0
  if (found && (*functionname_ptr || *line_ptr))
9977
0
    return true;
9978
9979
0
  if (symbols == NULL)
9980
0
    return false;
9981
9982
0
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9983
0
        filename_ptr, functionname_ptr))
9984
0
    return false;
9985
9986
0
  *line_ptr = 0;
9987
0
  return true;
9988
0
}
9989
9990
/* Find the line for a symbol.  */
9991
9992
bool
9993
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9994
        const char **filename_ptr, unsigned int *line_ptr)
9995
0
{
9996
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9997
0
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9998
0
          filename_ptr, NULL, line_ptr, NULL,
9999
0
          dwarf_debug_sections,
10000
0
          &tdata->dwarf2_find_line_info);
10001
0
}
10002
10003
/* After a call to bfd_find_nearest_line, successive calls to
10004
   bfd_find_inliner_info can be used to get source information about
10005
   each level of function inlining that terminated at the address
10006
   passed to bfd_find_nearest_line.  Currently this is only supported
10007
   for DWARF2 with appropriate DWARF3 extensions. */
10008
10009
bool
10010
_bfd_elf_find_inliner_info (bfd *abfd,
10011
          const char **filename_ptr,
10012
          const char **functionname_ptr,
10013
          unsigned int *line_ptr)
10014
0
{
10015
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
10016
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10017
0
          functionname_ptr, line_ptr,
10018
0
          &tdata->dwarf2_find_line_info);
10019
0
}
10020
10021
int
10022
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10023
0
{
10024
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10025
0
  int ret = bed->s->sizeof_ehdr;
10026
10027
0
  if (!bfd_link_relocatable (info))
10028
0
    {
10029
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10030
10031
0
      if (phdr_size == (bfd_size_type) -1)
10032
0
  {
10033
0
    struct elf_segment_map *m;
10034
10035
0
    phdr_size = 0;
10036
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10037
0
      phdr_size += bed->s->sizeof_phdr;
10038
10039
0
    if (phdr_size == 0)
10040
0
      phdr_size = get_program_header_size (abfd, info);
10041
0
  }
10042
10043
0
      elf_program_header_size (abfd) = phdr_size;
10044
0
      ret += phdr_size;
10045
0
    }
10046
10047
0
  return ret;
10048
0
}
10049
10050
bool
10051
_bfd_elf_set_section_contents (bfd *abfd,
10052
             sec_ptr section,
10053
             const void *location,
10054
             file_ptr offset,
10055
             bfd_size_type count)
10056
0
{
10057
0
  Elf_Internal_Shdr *hdr;
10058
10059
0
  if (! abfd->output_has_begun
10060
0
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10061
0
    return false;
10062
10063
0
  if (!count)
10064
0
    return true;
10065
10066
0
  hdr = &elf_section_data (section)->this_hdr;
10067
0
  if (hdr->sh_offset == (file_ptr) -1)
10068
0
    {
10069
0
      unsigned char *contents;
10070
10071
0
      if (bfd_section_is_ctf (section))
10072
  /* Nothing to do with this section: the contents are generated
10073
     later.  */
10074
0
  return true;
10075
10076
0
      if ((offset + count) > hdr->sh_size)
10077
0
  {
10078
0
    _bfd_error_handler
10079
0
      (_("%pB:%pA: error: attempting to write"
10080
0
         " over the end of the section"),
10081
0
       abfd, section);
10082
10083
0
    bfd_set_error (bfd_error_invalid_operation);
10084
0
    return false;
10085
0
  }
10086
10087
0
      contents = hdr->contents;
10088
0
      if (contents == NULL)
10089
0
  {
10090
0
    _bfd_error_handler
10091
0
      (_("%pB:%pA: error: attempting to write"
10092
0
         " section into an empty buffer"),
10093
0
       abfd, section);
10094
10095
0
    bfd_set_error (bfd_error_invalid_operation);
10096
0
    return false;
10097
0
  }
10098
10099
0
      memcpy (contents + offset, location, count);
10100
0
      return true;
10101
0
    }
10102
10103
0
  return _bfd_generic_set_section_contents (abfd, section,
10104
0
              location, offset, count);
10105
0
}
10106
10107
bool
10108
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10109
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10110
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10111
0
{
10112
0
  abort ();
10113
0
  return false;
10114
0
}
10115
10116
/* Try to convert a non-ELF reloc into an ELF one.  */
10117
10118
bool
10119
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10120
0
{
10121
  /* Check whether we really have an ELF howto.  */
10122
10123
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10124
0
    {
10125
0
      bfd_reloc_code_real_type code;
10126
0
      reloc_howto_type *howto;
10127
10128
      /* Alien reloc: Try to determine its type to replace it with an
10129
   equivalent ELF reloc.  */
10130
10131
0
      if (areloc->howto->pc_relative)
10132
0
  {
10133
0
    switch (areloc->howto->bitsize)
10134
0
      {
10135
0
      case 8:
10136
0
        code = BFD_RELOC_8_PCREL;
10137
0
        break;
10138
0
      case 12:
10139
0
        code = BFD_RELOC_12_PCREL;
10140
0
        break;
10141
0
      case 16:
10142
0
        code = BFD_RELOC_16_PCREL;
10143
0
        break;
10144
0
      case 24:
10145
0
        code = BFD_RELOC_24_PCREL;
10146
0
        break;
10147
0
      case 32:
10148
0
        code = BFD_RELOC_32_PCREL;
10149
0
        break;
10150
0
      case 64:
10151
0
        code = BFD_RELOC_64_PCREL;
10152
0
        break;
10153
0
      default:
10154
0
        goto fail;
10155
0
      }
10156
10157
0
    howto = bfd_reloc_type_lookup (abfd, code);
10158
10159
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10160
0
      {
10161
0
        if (howto->pcrel_offset)
10162
0
    areloc->addend += areloc->address;
10163
0
        else
10164
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10165
0
      }
10166
0
  }
10167
0
      else
10168
0
  {
10169
0
    switch (areloc->howto->bitsize)
10170
0
      {
10171
0
      case 8:
10172
0
        code = BFD_RELOC_8;
10173
0
        break;
10174
0
      case 14:
10175
0
        code = BFD_RELOC_14;
10176
0
        break;
10177
0
      case 16:
10178
0
        code = BFD_RELOC_16;
10179
0
        break;
10180
0
      case 26:
10181
0
        code = BFD_RELOC_26;
10182
0
        break;
10183
0
      case 32:
10184
0
        code = BFD_RELOC_32;
10185
0
        break;
10186
0
      case 64:
10187
0
        code = BFD_RELOC_64;
10188
0
        break;
10189
0
      default:
10190
0
        goto fail;
10191
0
      }
10192
10193
0
    howto = bfd_reloc_type_lookup (abfd, code);
10194
0
  }
10195
10196
0
      if (howto)
10197
0
  areloc->howto = howto;
10198
0
      else
10199
0
  goto fail;
10200
0
    }
10201
10202
0
  return true;
10203
10204
0
 fail:
10205
  /* xgettext:c-format */
10206
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10207
0
          abfd, areloc->howto->name);
10208
0
  bfd_set_error (bfd_error_sorry);
10209
0
  return false;
10210
0
}
10211
10212
bool
10213
_bfd_elf_free_cached_info (bfd *abfd)
10214
0
{
10215
0
  struct elf_obj_tdata *tdata;
10216
10217
0
  if ((bfd_get_format (abfd) == bfd_object
10218
0
       || bfd_get_format (abfd) == bfd_core)
10219
0
      && (tdata = elf_tdata (abfd)) != NULL)
10220
0
    {
10221
0
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10222
0
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10223
0
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10224
0
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10225
0
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10226
0
    }
10227
10228
0
  return _bfd_generic_bfd_free_cached_info (abfd);
10229
0
}
10230
10231
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10232
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10233
   range-checking to interfere.  There is nothing else to do in processing
10234
   this reloc.  */
10235
10236
bfd_reloc_status_type
10237
_bfd_elf_rel_vtable_reloc_fn
10238
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10239
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10240
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10241
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10242
0
{
10243
0
  return bfd_reloc_ok;
10244
0
}
10245

10246
/* Elf core file support.  Much of this only works on native
10247
   toolchains, since we rely on knowing the
10248
   machine-dependent procfs structure in order to pick
10249
   out details about the corefile.  */
10250
10251
#ifdef HAVE_SYS_PROCFS_H
10252
# include <sys/procfs.h>
10253
#endif
10254
10255
/* Return a PID that identifies a "thread" for threaded cores, or the
10256
   PID of the main process for non-threaded cores.  */
10257
10258
static int
10259
elfcore_make_pid (bfd *abfd)
10260
0
{
10261
0
  int pid;
10262
10263
0
  pid = elf_tdata (abfd)->core->lwpid;
10264
0
  if (pid == 0)
10265
0
    pid = elf_tdata (abfd)->core->pid;
10266
10267
0
  return pid;
10268
0
}
10269
10270
/* If there isn't a section called NAME, make one, using data from
10271
   SECT.  Note, this function will generate a reference to NAME, so
10272
   you shouldn't deallocate or overwrite it.  */
10273
10274
static bool
10275
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10276
0
{
10277
0
  asection *sect2;
10278
10279
0
  if (bfd_get_section_by_name (abfd, name) != NULL)
10280
0
    return true;
10281
10282
0
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10283
0
  if (sect2 == NULL)
10284
0
    return false;
10285
10286
0
  sect2->size = sect->size;
10287
0
  sect2->filepos = sect->filepos;
10288
0
  sect2->alignment_power = sect->alignment_power;
10289
0
  return true;
10290
0
}
10291
10292
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10293
   actually creates up to two pseudosections:
10294
   - For the single-threaded case, a section named NAME, unless
10295
     such a section already exists.
10296
   - For the multi-threaded case, a section named "NAME/PID", where
10297
     PID is elfcore_make_pid (abfd).
10298
   Both pseudosections have identical contents.  */
10299
bool
10300
_bfd_elfcore_make_pseudosection (bfd *abfd,
10301
         char *name,
10302
         size_t size,
10303
         ufile_ptr filepos)
10304
0
{
10305
0
  char buf[100];
10306
0
  char *threaded_name;
10307
0
  size_t len;
10308
0
  asection *sect;
10309
10310
  /* Build the section name.  */
10311
10312
0
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10313
0
  len = strlen (buf) + 1;
10314
0
  threaded_name = (char *) bfd_alloc (abfd, len);
10315
0
  if (threaded_name == NULL)
10316
0
    return false;
10317
0
  memcpy (threaded_name, buf, len);
10318
10319
0
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10320
0
               SEC_HAS_CONTENTS);
10321
0
  if (sect == NULL)
10322
0
    return false;
10323
0
  sect->size = size;
10324
0
  sect->filepos = filepos;
10325
0
  sect->alignment_power = 2;
10326
10327
0
  return elfcore_maybe_make_sect (abfd, name, sect);
10328
0
}
10329
10330
static bool
10331
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10332
        size_t offs)
10333
0
{
10334
0
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10335
0
                   SEC_HAS_CONTENTS);
10336
10337
0
  if (sect == NULL)
10338
0
    return false;
10339
10340
0
  sect->size = note->descsz - offs;
10341
0
  sect->filepos = note->descpos + offs;
10342
0
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10343
10344
0
  return true;
10345
0
}
10346
10347
/* prstatus_t exists on:
10348
     solaris 2.5+
10349
     linux 2.[01] + glibc
10350
     unixware 4.2
10351
*/
10352
10353
#if defined (HAVE_PRSTATUS_T)
10354
10355
static bool
10356
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10357
0
{
10358
0
  size_t size;
10359
0
  int offset;
10360
10361
0
  if (note->descsz == sizeof (prstatus_t))
10362
0
    {
10363
0
      prstatus_t prstat;
10364
10365
0
      size = sizeof (prstat.pr_reg);
10366
0
      offset   = offsetof (prstatus_t, pr_reg);
10367
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10368
10369
      /* Do not overwrite the core signal if it
10370
   has already been set by another thread.  */
10371
0
      if (elf_tdata (abfd)->core->signal == 0)
10372
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10373
0
      if (elf_tdata (abfd)->core->pid == 0)
10374
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10375
10376
      /* pr_who exists on:
10377
   solaris 2.5+
10378
   unixware 4.2
10379
   pr_who doesn't exist on:
10380
   linux 2.[01]
10381
   */
10382
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10383
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10384
#else
10385
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10386
0
#endif
10387
0
    }
10388
0
#if defined (HAVE_PRSTATUS32_T)
10389
0
  else if (note->descsz == sizeof (prstatus32_t))
10390
0
    {
10391
      /* 64-bit host, 32-bit corefile */
10392
0
      prstatus32_t prstat;
10393
10394
0
      size = sizeof (prstat.pr_reg);
10395
0
      offset   = offsetof (prstatus32_t, pr_reg);
10396
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10397
10398
      /* Do not overwrite the core signal if it
10399
   has already been set by another thread.  */
10400
0
      if (elf_tdata (abfd)->core->signal == 0)
10401
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10402
0
      if (elf_tdata (abfd)->core->pid == 0)
10403
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10404
10405
      /* pr_who exists on:
10406
   solaris 2.5+
10407
   unixware 4.2
10408
   pr_who doesn't exist on:
10409
   linux 2.[01]
10410
   */
10411
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10412
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10413
#else
10414
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10415
0
#endif
10416
0
    }
10417
0
#endif /* HAVE_PRSTATUS32_T */
10418
0
  else
10419
0
    {
10420
      /* Fail - we don't know how to handle any other
10421
   note size (ie. data object type).  */
10422
0
      return true;
10423
0
    }
10424
10425
  /* Make a ".reg/999" section and a ".reg" section.  */
10426
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10427
0
            size, note->descpos + offset);
10428
0
}
10429
#endif /* defined (HAVE_PRSTATUS_T) */
10430
10431
/* Create a pseudosection containing the exact contents of NOTE.  */
10432
static bool
10433
elfcore_make_note_pseudosection (bfd *abfd,
10434
         char *name,
10435
         Elf_Internal_Note *note)
10436
0
{
10437
0
  return _bfd_elfcore_make_pseudosection (abfd, name,
10438
0
            note->descsz, note->descpos);
10439
0
}
10440
10441
/* There isn't a consistent prfpregset_t across platforms,
10442
   but it doesn't matter, because we don't have to pick this
10443
   data structure apart.  */
10444
10445
static bool
10446
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10447
0
{
10448
0
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10449
0
}
10450
10451
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10452
   type of NT_PRXFPREG.  Just include the whole note's contents
10453
   literally.  */
10454
10455
static bool
10456
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10457
0
{
10458
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10459
0
}
10460
10461
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10462
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10463
   contents literally.  */
10464
10465
static bool
10466
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10467
0
{
10468
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10469
0
}
10470
10471
static bool
10472
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10473
0
{
10474
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10475
0
}
10476
10477
static bool
10478
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10479
0
{
10480
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10481
0
}
10482
10483
static bool
10484
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10485
0
{
10486
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10487
0
}
10488
10489
static bool
10490
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10491
0
{
10492
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10493
0
}
10494
10495
static bool
10496
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10497
0
{
10498
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10499
0
}
10500
10501
static bool
10502
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10503
0
{
10504
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10505
0
}
10506
10507
static bool
10508
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10509
0
{
10510
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10511
0
}
10512
10513
static bool
10514
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10515
0
{
10516
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10517
0
}
10518
10519
static bool
10520
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10521
0
{
10522
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10523
0
}
10524
10525
static bool
10526
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10527
0
{
10528
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10529
0
}
10530
10531
static bool
10532
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10533
0
{
10534
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10535
0
}
10536
10537
static bool
10538
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10539
0
{
10540
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10541
0
}
10542
10543
static bool
10544
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10545
0
{
10546
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10547
0
}
10548
10549
static bool
10550
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10551
0
{
10552
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10553
0
}
10554
10555
static bool
10556
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10557
0
{
10558
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10559
0
}
10560
10561
static bool
10562
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10563
0
{
10564
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10565
0
}
10566
10567
static bool
10568
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10569
0
{
10570
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10571
0
}
10572
10573
static bool
10574
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10575
0
{
10576
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10577
0
}
10578
10579
static bool
10580
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10581
0
{
10582
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10583
0
}
10584
10585
static bool
10586
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10587
0
{
10588
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10589
0
}
10590
10591
static bool
10592
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10593
0
{
10594
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10595
0
}
10596
10597
static bool
10598
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10599
0
{
10600
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10601
0
}
10602
10603
static bool
10604
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10605
0
{
10606
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10607
0
}
10608
10609
static bool
10610
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10611
0
{
10612
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10613
0
}
10614
10615
static bool
10616
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10617
0
{
10618
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10619
0
}
10620
10621
static bool
10622
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10623
0
{
10624
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10625
0
}
10626
10627
static bool
10628
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10629
0
{
10630
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10631
0
}
10632
10633
static bool
10634
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10635
0
{
10636
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10637
0
}
10638
10639
static bool
10640
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10641
0
{
10642
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10643
0
}
10644
10645
static bool
10646
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10647
0
{
10648
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10649
0
}
10650
10651
static bool
10652
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10653
0
{
10654
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10655
0
}
10656
10657
static bool
10658
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10659
0
{
10660
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10661
0
}
10662
10663
static bool
10664
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10665
0
{
10666
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10667
0
}
10668
10669
static bool
10670
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10671
0
{
10672
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10673
0
}
10674
10675
static bool
10676
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10677
0
{
10678
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10679
0
            note);
10680
0
}
10681
10682
static bool
10683
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10684
0
{
10685
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10686
0
}
10687
10688
static bool
10689
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10690
0
{
10691
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10692
0
}
10693
10694
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10695
   successful, otherwise return FALSE.  */
10696
10697
static bool
10698
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10699
0
{
10700
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10701
0
}
10702
10703
static bool
10704
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10705
0
{
10706
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10707
0
}
10708
10709
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10710
   successful otherwise, return FALSE.  */
10711
10712
static bool
10713
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10714
0
{
10715
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10716
0
}
10717
10718
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10719
   successful otherwise, return FALSE.  */
10720
10721
static bool
10722
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10723
0
{
10724
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10725
0
}
10726
10727
static bool
10728
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10729
0
{
10730
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10731
0
}
10732
10733
static bool
10734
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10735
0
{
10736
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10737
0
}
10738
10739
static bool
10740
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10741
0
{
10742
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10743
0
}
10744
10745
static bool
10746
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10747
0
{
10748
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10749
0
}
10750
10751
#if defined (HAVE_PRPSINFO_T)
10752
typedef prpsinfo_t   elfcore_psinfo_t;
10753
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10754
typedef prpsinfo32_t elfcore_psinfo32_t;
10755
#endif
10756
#endif
10757
10758
#if defined (HAVE_PSINFO_T)
10759
typedef psinfo_t   elfcore_psinfo_t;
10760
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10761
typedef psinfo32_t elfcore_psinfo32_t;
10762
#endif
10763
#endif
10764
10765
/* return a malloc'ed copy of a string at START which is at
10766
   most MAX bytes long, possibly without a terminating '\0'.
10767
   the copy will always have a terminating '\0'.  */
10768
10769
char *
10770
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10771
0
{
10772
0
  char *dups;
10773
0
  char *end = (char *) memchr (start, '\0', max);
10774
0
  size_t len;
10775
10776
0
  if (end == NULL)
10777
0
    len = max;
10778
0
  else
10779
0
    len = end - start;
10780
10781
0
  dups = (char *) bfd_alloc (abfd, len + 1);
10782
0
  if (dups == NULL)
10783
0
    return NULL;
10784
10785
0
  memcpy (dups, start, len);
10786
0
  dups[len] = '\0';
10787
10788
0
  return dups;
10789
0
}
10790
10791
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10792
static bool
10793
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10794
0
{
10795
0
  if (note->descsz == sizeof (elfcore_psinfo_t))
10796
0
    {
10797
0
      elfcore_psinfo_t psinfo;
10798
10799
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10800
10801
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10802
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10803
0
#endif
10804
0
      elf_tdata (abfd)->core->program
10805
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10806
0
        sizeof (psinfo.pr_fname));
10807
10808
0
      elf_tdata (abfd)->core->command
10809
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10810
0
        sizeof (psinfo.pr_psargs));
10811
0
    }
10812
0
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10813
0
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10814
0
    {
10815
      /* 64-bit host, 32-bit corefile */
10816
0
      elfcore_psinfo32_t psinfo;
10817
10818
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10819
10820
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10821
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10822
0
#endif
10823
0
      elf_tdata (abfd)->core->program
10824
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10825
0
        sizeof (psinfo.pr_fname));
10826
10827
0
      elf_tdata (abfd)->core->command
10828
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10829
0
        sizeof (psinfo.pr_psargs));
10830
0
    }
10831
0
#endif
10832
10833
0
  else
10834
0
    {
10835
      /* Fail - we don't know how to handle any other
10836
   note size (ie. data object type).  */
10837
0
      return true;
10838
0
    }
10839
10840
  /* Note that for some reason, a spurious space is tacked
10841
     onto the end of the args in some (at least one anyway)
10842
     implementations, so strip it off if it exists.  */
10843
10844
0
  {
10845
0
    char *command = elf_tdata (abfd)->core->command;
10846
0
    int n = strlen (command);
10847
10848
0
    if (0 < n && command[n - 1] == ' ')
10849
0
      command[n - 1] = '\0';
10850
0
  }
10851
10852
0
  return true;
10853
0
}
10854
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10855
10856
#if defined (HAVE_PSTATUS_T)
10857
static bool
10858
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10859
{
10860
  if (note->descsz == sizeof (pstatus_t)
10861
#if defined (HAVE_PXSTATUS_T)
10862
      || note->descsz == sizeof (pxstatus_t)
10863
#endif
10864
      )
10865
    {
10866
      pstatus_t pstat;
10867
10868
      memcpy (&pstat, note->descdata, sizeof (pstat));
10869
10870
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10871
    }
10872
#if defined (HAVE_PSTATUS32_T)
10873
  else if (note->descsz == sizeof (pstatus32_t))
10874
    {
10875
      /* 64-bit host, 32-bit corefile */
10876
      pstatus32_t pstat;
10877
10878
      memcpy (&pstat, note->descdata, sizeof (pstat));
10879
10880
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10881
    }
10882
#endif
10883
  /* Could grab some more details from the "representative"
10884
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10885
     NT_LWPSTATUS note, presumably.  */
10886
10887
  return true;
10888
}
10889
#endif /* defined (HAVE_PSTATUS_T) */
10890
10891
#if defined (HAVE_LWPSTATUS_T)
10892
static bool
10893
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10894
{
10895
  lwpstatus_t lwpstat;
10896
  char buf[100];
10897
  char *name;
10898
  size_t len;
10899
  asection *sect;
10900
10901
  if (note->descsz != sizeof (lwpstat)
10902
#if defined (HAVE_LWPXSTATUS_T)
10903
      && note->descsz != sizeof (lwpxstatus_t)
10904
#endif
10905
      )
10906
    return true;
10907
10908
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10909
10910
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10911
  /* Do not overwrite the core signal if it has already been set by
10912
     another thread.  */
10913
  if (elf_tdata (abfd)->core->signal == 0)
10914
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10915
10916
  /* Make a ".reg/999" section.  */
10917
10918
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10919
  len = strlen (buf) + 1;
10920
  name = bfd_alloc (abfd, len);
10921
  if (name == NULL)
10922
    return false;
10923
  memcpy (name, buf, len);
10924
10925
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10926
  if (sect == NULL)
10927
    return false;
10928
10929
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10930
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10931
  sect->filepos = note->descpos
10932
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10933
#endif
10934
10935
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10936
  sect->size = sizeof (lwpstat.pr_reg);
10937
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10938
#endif
10939
10940
  sect->alignment_power = 2;
10941
10942
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10943
    return false;
10944
10945
  /* Make a ".reg2/999" section */
10946
10947
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10948
  len = strlen (buf) + 1;
10949
  name = bfd_alloc (abfd, len);
10950
  if (name == NULL)
10951
    return false;
10952
  memcpy (name, buf, len);
10953
10954
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10955
  if (sect == NULL)
10956
    return false;
10957
10958
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10959
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10960
  sect->filepos = note->descpos
10961
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10962
#endif
10963
10964
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10965
  sect->size = sizeof (lwpstat.pr_fpreg);
10966
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10967
#endif
10968
10969
  sect->alignment_power = 2;
10970
10971
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10972
}
10973
#endif /* defined (HAVE_LWPSTATUS_T) */
10974
10975
/* These constants, and the structure offsets used below, are defined by
10976
   Cygwin's core_dump.h */
10977
0
#define NOTE_INFO_PROCESS  1
10978
0
#define NOTE_INFO_THREAD   2
10979
0
#define NOTE_INFO_MODULE   3
10980
0
#define NOTE_INFO_MODULE64 4
10981
10982
static bool
10983
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10984
0
{
10985
0
  char buf[30];
10986
0
  char *name;
10987
0
  size_t len;
10988
0
  unsigned int name_size;
10989
0
  asection *sect;
10990
0
  unsigned int type;
10991
0
  int is_active_thread;
10992
0
  bfd_vma base_addr;
10993
10994
0
  if (note->descsz < 4)
10995
0
    return true;
10996
10997
0
  if (! startswith (note->namedata, "win32"))
10998
0
    return true;
10999
11000
0
  type = bfd_get_32 (abfd, note->descdata);
11001
11002
0
  struct
11003
0
  {
11004
0
    const char *type_name;
11005
0
    unsigned long min_size;
11006
0
  } size_check[] =
11007
0
      {
11008
0
       { "NOTE_INFO_PROCESS", 12 },
11009
0
       { "NOTE_INFO_THREAD", 12 },
11010
0
       { "NOTE_INFO_MODULE", 12 },
11011
0
       { "NOTE_INFO_MODULE64", 16 },
11012
0
      };
11013
11014
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11015
0
      return true;
11016
11017
0
  if (note->descsz < size_check[type - 1].min_size)
11018
0
    {
11019
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11020
0
          " is too small"),
11021
0
        abfd, size_check[type - 1].type_name, note->descsz);
11022
0
      return true;
11023
0
    }
11024
11025
0
  switch (type)
11026
0
    {
11027
0
    case NOTE_INFO_PROCESS:
11028
      /* FIXME: need to add ->core->command.  */
11029
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11030
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11031
0
      break;
11032
11033
0
    case NOTE_INFO_THREAD:
11034
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11035
   structure. */
11036
      /* thread_info.tid */
11037
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11038
11039
0
      len = strlen (buf) + 1;
11040
0
      name = (char *) bfd_alloc (abfd, len);
11041
0
      if (name == NULL)
11042
0
  return false;
11043
11044
0
      memcpy (name, buf, len);
11045
11046
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11047
0
      if (sect == NULL)
11048
0
  return false;
11049
11050
      /* sizeof (thread_info.thread_context) */
11051
0
      sect->size = note->descsz - 12;
11052
      /* offsetof (thread_info.thread_context) */
11053
0
      sect->filepos = note->descpos + 12;
11054
0
      sect->alignment_power = 2;
11055
11056
      /* thread_info.is_active_thread */
11057
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11058
11059
0
      if (is_active_thread)
11060
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11061
0
    return false;
11062
0
      break;
11063
11064
0
    case NOTE_INFO_MODULE:
11065
0
    case NOTE_INFO_MODULE64:
11066
      /* Make a ".module/xxxxxxxx" section.  */
11067
0
      if (type == NOTE_INFO_MODULE)
11068
0
  {
11069
    /* module_info.base_address */
11070
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11071
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11072
    /* module_info.module_name_size */
11073
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11074
0
  }
11075
0
      else /* NOTE_INFO_MODULE64 */
11076
0
  {
11077
    /* module_info.base_address */
11078
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11079
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11080
    /* module_info.module_name_size */
11081
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11082
0
  }
11083
11084
0
      len = strlen (buf) + 1;
11085
0
      name = (char *) bfd_alloc (abfd, len);
11086
0
      if (name == NULL)
11087
0
  return false;
11088
11089
0
      memcpy (name, buf, len);
11090
11091
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11092
11093
0
      if (sect == NULL)
11094
0
  return false;
11095
11096
0
      if (note->descsz < 12 + name_size)
11097
0
  {
11098
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11099
0
        " is too small to contain a name of size %u"),
11100
0
            abfd, note->descsz, name_size);
11101
0
    return true;
11102
0
  }
11103
11104
0
      sect->size = note->descsz;
11105
0
      sect->filepos = note->descpos;
11106
0
      sect->alignment_power = 2;
11107
0
      break;
11108
11109
0
    default:
11110
0
      return true;
11111
0
    }
11112
11113
0
  return true;
11114
0
}
11115
11116
static bool
11117
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11118
0
{
11119
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11120
11121
0
  switch (note->type)
11122
0
    {
11123
0
    default:
11124
0
      return true;
11125
11126
0
    case NT_PRSTATUS:
11127
0
      if (bed->elf_backend_grok_prstatus)
11128
0
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11129
0
    return true;
11130
0
#if defined (HAVE_PRSTATUS_T)
11131
0
      return elfcore_grok_prstatus (abfd, note);
11132
#else
11133
      return true;
11134
#endif
11135
11136
#if defined (HAVE_PSTATUS_T)
11137
    case NT_PSTATUS:
11138
      return elfcore_grok_pstatus (abfd, note);
11139
#endif
11140
11141
#if defined (HAVE_LWPSTATUS_T)
11142
    case NT_LWPSTATUS:
11143
      return elfcore_grok_lwpstatus (abfd, note);
11144
#endif
11145
11146
0
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
11147
0
      return elfcore_grok_prfpreg (abfd, note);
11148
11149
0
    case NT_WIN32PSTATUS:
11150
0
      return elfcore_grok_win32pstatus (abfd, note);
11151
11152
0
    case NT_PRXFPREG:   /* Linux SSE extension */
11153
0
      if (note->namesz == 6
11154
0
    && strcmp (note->namedata, "LINUX") == 0)
11155
0
  return elfcore_grok_prxfpreg (abfd, note);
11156
0
      else
11157
0
  return true;
11158
11159
0
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
11160
0
      if (note->namesz == 6
11161
0
    && strcmp (note->namedata, "LINUX") == 0)
11162
0
  return elfcore_grok_xstatereg (abfd, note);
11163
0
      else
11164
0
  return true;
11165
11166
0
    case NT_PPC_VMX:
11167
0
      if (note->namesz == 6
11168
0
    && strcmp (note->namedata, "LINUX") == 0)
11169
0
  return elfcore_grok_ppc_vmx (abfd, note);
11170
0
      else
11171
0
  return true;
11172
11173
0
    case NT_PPC_VSX:
11174
0
      if (note->namesz == 6
11175
0
    && strcmp (note->namedata, "LINUX") == 0)
11176
0
  return elfcore_grok_ppc_vsx (abfd, note);
11177
0
      else
11178
0
  return true;
11179
11180
0
    case NT_PPC_TAR:
11181
0
      if (note->namesz == 6
11182
0
    && strcmp (note->namedata, "LINUX") == 0)
11183
0
  return elfcore_grok_ppc_tar (abfd, note);
11184
0
      else
11185
0
  return true;
11186
11187
0
    case NT_PPC_PPR:
11188
0
      if (note->namesz == 6
11189
0
    && strcmp (note->namedata, "LINUX") == 0)
11190
0
  return elfcore_grok_ppc_ppr (abfd, note);
11191
0
      else
11192
0
  return true;
11193
11194
0
    case NT_PPC_DSCR:
11195
0
      if (note->namesz == 6
11196
0
    && strcmp (note->namedata, "LINUX") == 0)
11197
0
  return elfcore_grok_ppc_dscr (abfd, note);
11198
0
      else
11199
0
  return true;
11200
11201
0
    case NT_PPC_EBB:
11202
0
      if (note->namesz == 6
11203
0
    && strcmp (note->namedata, "LINUX") == 0)
11204
0
  return elfcore_grok_ppc_ebb (abfd, note);
11205
0
      else
11206
0
  return true;
11207
11208
0
    case NT_PPC_PMU:
11209
0
      if (note->namesz == 6
11210
0
    && strcmp (note->namedata, "LINUX") == 0)
11211
0
  return elfcore_grok_ppc_pmu (abfd, note);
11212
0
      else
11213
0
  return true;
11214
11215
0
    case NT_PPC_TM_CGPR:
11216
0
      if (note->namesz == 6
11217
0
    && strcmp (note->namedata, "LINUX") == 0)
11218
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
11219
0
      else
11220
0
  return true;
11221
11222
0
    case NT_PPC_TM_CFPR:
11223
0
      if (note->namesz == 6
11224
0
    && strcmp (note->namedata, "LINUX") == 0)
11225
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
11226
0
      else
11227
0
  return true;
11228
11229
0
    case NT_PPC_TM_CVMX:
11230
0
      if (note->namesz == 6
11231
0
    && strcmp (note->namedata, "LINUX") == 0)
11232
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
11233
0
      else
11234
0
  return true;
11235
11236
0
    case NT_PPC_TM_CVSX:
11237
0
      if (note->namesz == 6
11238
0
    && strcmp (note->namedata, "LINUX") == 0)
11239
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
11240
0
      else
11241
0
  return true;
11242
11243
0
    case NT_PPC_TM_SPR:
11244
0
      if (note->namesz == 6
11245
0
    && strcmp (note->namedata, "LINUX") == 0)
11246
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
11247
0
      else
11248
0
  return true;
11249
11250
0
    case NT_PPC_TM_CTAR:
11251
0
      if (note->namesz == 6
11252
0
    && strcmp (note->namedata, "LINUX") == 0)
11253
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
11254
0
      else
11255
0
  return true;
11256
11257
0
    case NT_PPC_TM_CPPR:
11258
0
      if (note->namesz == 6
11259
0
    && strcmp (note->namedata, "LINUX") == 0)
11260
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
11261
0
      else
11262
0
  return true;
11263
11264
0
    case NT_PPC_TM_CDSCR:
11265
0
      if (note->namesz == 6
11266
0
    && strcmp (note->namedata, "LINUX") == 0)
11267
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
11268
0
      else
11269
0
  return true;
11270
11271
0
    case NT_S390_HIGH_GPRS:
11272
0
      if (note->namesz == 6
11273
0
    && strcmp (note->namedata, "LINUX") == 0)
11274
0
  return elfcore_grok_s390_high_gprs (abfd, note);
11275
0
      else
11276
0
  return true;
11277
11278
0
    case NT_S390_TIMER:
11279
0
      if (note->namesz == 6
11280
0
    && strcmp (note->namedata, "LINUX") == 0)
11281
0
  return elfcore_grok_s390_timer (abfd, note);
11282
0
      else
11283
0
  return true;
11284
11285
0
    case NT_S390_TODCMP:
11286
0
      if (note->namesz == 6
11287
0
    && strcmp (note->namedata, "LINUX") == 0)
11288
0
  return elfcore_grok_s390_todcmp (abfd, note);
11289
0
      else
11290
0
  return true;
11291
11292
0
    case NT_S390_TODPREG:
11293
0
      if (note->namesz == 6
11294
0
    && strcmp (note->namedata, "LINUX") == 0)
11295
0
  return elfcore_grok_s390_todpreg (abfd, note);
11296
0
      else
11297
0
  return true;
11298
11299
0
    case NT_S390_CTRS:
11300
0
      if (note->namesz == 6
11301
0
    && strcmp (note->namedata, "LINUX") == 0)
11302
0
  return elfcore_grok_s390_ctrs (abfd, note);
11303
0
      else
11304
0
  return true;
11305
11306
0
    case NT_S390_PREFIX:
11307
0
      if (note->namesz == 6
11308
0
    && strcmp (note->namedata, "LINUX") == 0)
11309
0
  return elfcore_grok_s390_prefix (abfd, note);
11310
0
      else
11311
0
  return true;
11312
11313
0
    case NT_S390_LAST_BREAK:
11314
0
      if (note->namesz == 6
11315
0
    && strcmp (note->namedata, "LINUX") == 0)
11316
0
  return elfcore_grok_s390_last_break (abfd, note);
11317
0
      else
11318
0
  return true;
11319
11320
0
    case NT_S390_SYSTEM_CALL:
11321
0
      if (note->namesz == 6
11322
0
    && strcmp (note->namedata, "LINUX") == 0)
11323
0
  return elfcore_grok_s390_system_call (abfd, note);
11324
0
      else
11325
0
  return true;
11326
11327
0
    case NT_S390_TDB:
11328
0
      if (note->namesz == 6
11329
0
    && strcmp (note->namedata, "LINUX") == 0)
11330
0
  return elfcore_grok_s390_tdb (abfd, note);
11331
0
      else
11332
0
  return true;
11333
11334
0
    case NT_S390_VXRS_LOW:
11335
0
      if (note->namesz == 6
11336
0
    && strcmp (note->namedata, "LINUX") == 0)
11337
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
11338
0
      else
11339
0
  return true;
11340
11341
0
    case NT_S390_VXRS_HIGH:
11342
0
      if (note->namesz == 6
11343
0
    && strcmp (note->namedata, "LINUX") == 0)
11344
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
11345
0
      else
11346
0
  return true;
11347
11348
0
    case NT_S390_GS_CB:
11349
0
      if (note->namesz == 6
11350
0
    && strcmp (note->namedata, "LINUX") == 0)
11351
0
  return elfcore_grok_s390_gs_cb (abfd, note);
11352
0
      else
11353
0
  return true;
11354
11355
0
    case NT_S390_GS_BC:
11356
0
      if (note->namesz == 6
11357
0
    && strcmp (note->namedata, "LINUX") == 0)
11358
0
  return elfcore_grok_s390_gs_bc (abfd, note);
11359
0
      else
11360
0
  return true;
11361
11362
0
    case NT_ARC_V2:
11363
0
      if (note->namesz == 6
11364
0
    && strcmp (note->namedata, "LINUX") == 0)
11365
0
  return elfcore_grok_arc_v2 (abfd, note);
11366
0
      else
11367
0
  return true;
11368
11369
0
    case NT_ARM_VFP:
11370
0
      if (note->namesz == 6
11371
0
    && strcmp (note->namedata, "LINUX") == 0)
11372
0
  return elfcore_grok_arm_vfp (abfd, note);
11373
0
      else
11374
0
  return true;
11375
11376
0
    case NT_ARM_TLS:
11377
0
      if (note->namesz == 6
11378
0
    && strcmp (note->namedata, "LINUX") == 0)
11379
0
  return elfcore_grok_aarch_tls (abfd, note);
11380
0
      else
11381
0
  return true;
11382
11383
0
    case NT_ARM_HW_BREAK:
11384
0
      if (note->namesz == 6
11385
0
    && strcmp (note->namedata, "LINUX") == 0)
11386
0
  return elfcore_grok_aarch_hw_break (abfd, note);
11387
0
      else
11388
0
  return true;
11389
11390
0
    case NT_ARM_HW_WATCH:
11391
0
      if (note->namesz == 6
11392
0
    && strcmp (note->namedata, "LINUX") == 0)
11393
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
11394
0
      else
11395
0
  return true;
11396
11397
0
    case NT_ARM_SVE:
11398
0
      if (note->namesz == 6
11399
0
    && strcmp (note->namedata, "LINUX") == 0)
11400
0
  return elfcore_grok_aarch_sve (abfd, note);
11401
0
      else
11402
0
  return true;
11403
11404
0
    case NT_ARM_PAC_MASK:
11405
0
      if (note->namesz == 6
11406
0
    && strcmp (note->namedata, "LINUX") == 0)
11407
0
  return elfcore_grok_aarch_pauth (abfd, note);
11408
0
      else
11409
0
  return true;
11410
11411
0
    case NT_ARM_TAGGED_ADDR_CTRL:
11412
0
      if (note->namesz == 6
11413
0
    && strcmp (note->namedata, "LINUX") == 0)
11414
0
  return elfcore_grok_aarch_mte (abfd, note);
11415
0
      else
11416
0
  return true;
11417
11418
0
    case NT_ARM_SSVE:
11419
0
      if (note->namesz == 6
11420
0
    && strcmp (note->namedata, "LINUX") == 0)
11421
0
  return elfcore_grok_aarch_ssve (abfd, note);
11422
0
      else
11423
0
  return true;
11424
11425
0
    case NT_ARM_ZA:
11426
0
      if (note->namesz == 6
11427
0
    && strcmp (note->namedata, "LINUX") == 0)
11428
0
  return elfcore_grok_aarch_za (abfd, note);
11429
0
      else
11430
0
  return true;
11431
11432
0
    case NT_ARM_ZT:
11433
0
      if (note->namesz == 6
11434
0
    && strcmp (note->namedata, "LINUX") == 0)
11435
0
  return elfcore_grok_aarch_zt (abfd, note);
11436
0
      else
11437
0
  return true;
11438
11439
0
    case NT_GDB_TDESC:
11440
0
      if (note->namesz == 4
11441
0
    && strcmp (note->namedata, "GDB") == 0)
11442
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11443
0
      else
11444
0
  return true;
11445
11446
0
    case NT_RISCV_CSR:
11447
0
      if (note->namesz == 4
11448
0
    && strcmp (note->namedata, "GDB") == 0)
11449
0
  return elfcore_grok_riscv_csr (abfd, note);
11450
0
      else
11451
0
  return true;
11452
11453
0
    case NT_LARCH_CPUCFG:
11454
0
      if (note->namesz == 6
11455
0
    && strcmp (note->namedata, "LINUX") == 0)
11456
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
11457
0
      else
11458
0
  return true;
11459
11460
0
    case NT_LARCH_LBT:
11461
0
      if (note->namesz == 6
11462
0
    && strcmp (note->namedata, "LINUX") == 0)
11463
0
  return elfcore_grok_loongarch_lbt (abfd, note);
11464
0
      else
11465
0
  return true;
11466
11467
0
    case NT_LARCH_LSX:
11468
0
      if (note->namesz == 6
11469
0
    && strcmp (note->namedata, "LINUX") == 0)
11470
0
  return elfcore_grok_loongarch_lsx (abfd, note);
11471
0
      else
11472
0
  return true;
11473
11474
0
    case NT_LARCH_LASX:
11475
0
      if (note->namesz == 6
11476
0
    && strcmp (note->namedata, "LINUX") == 0)
11477
0
  return elfcore_grok_loongarch_lasx (abfd, note);
11478
0
      else
11479
0
  return true;
11480
11481
0
    case NT_PRPSINFO:
11482
0
    case NT_PSINFO:
11483
0
      if (bed->elf_backend_grok_psinfo)
11484
0
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11485
0
    return true;
11486
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11487
0
      return elfcore_grok_psinfo (abfd, note);
11488
#else
11489
      return true;
11490
#endif
11491
11492
0
    case NT_AUXV:
11493
0
      return elfcore_make_auxv_note_section (abfd, note, 0);
11494
11495
0
    case NT_FILE:
11496
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11497
0
                note);
11498
11499
0
    case NT_SIGINFO:
11500
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11501
0
                note);
11502
11503
0
    }
11504
0
}
11505
11506
static bool
11507
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11508
0
{
11509
0
  struct bfd_build_id* build_id;
11510
11511
0
  if (note->descsz == 0)
11512
0
    return false;
11513
11514
0
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11515
0
  if (build_id == NULL)
11516
0
    return false;
11517
11518
0
  build_id->size = note->descsz;
11519
0
  memcpy (build_id->data, note->descdata, note->descsz);
11520
0
  abfd->build_id = build_id;
11521
11522
0
  return true;
11523
0
}
11524
11525
static bool
11526
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11527
0
{
11528
0
  switch (note->type)
11529
0
    {
11530
0
    default:
11531
0
      return true;
11532
11533
0
    case NT_GNU_PROPERTY_TYPE_0:
11534
0
      return _bfd_elf_parse_gnu_properties (abfd, note);
11535
11536
0
    case NT_GNU_BUILD_ID:
11537
0
      return elfobj_grok_gnu_build_id (abfd, note);
11538
0
    }
11539
0
}
11540
11541
static bool
11542
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11543
0
{
11544
0
  struct sdt_note *cur =
11545
0
    (struct sdt_note *) bfd_alloc (abfd,
11546
0
           sizeof (struct sdt_note) + note->descsz);
11547
11548
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11549
0
  cur->size = (bfd_size_type) note->descsz;
11550
0
  memcpy (cur->data, note->descdata, note->descsz);
11551
11552
0
  elf_tdata (abfd)->sdt_note_head = cur;
11553
11554
0
  return true;
11555
0
}
11556
11557
static bool
11558
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11559
0
{
11560
0
  switch (note->type)
11561
0
    {
11562
0
    case NT_STAPSDT:
11563
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11564
11565
0
    default:
11566
0
      return true;
11567
0
    }
11568
0
}
11569
11570
static bool
11571
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11572
0
{
11573
0
  size_t offset;
11574
11575
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11576
0
    {
11577
0
    case ELFCLASS32:
11578
0
      if (note->descsz < 108)
11579
0
  return false;
11580
0
      break;
11581
11582
0
    case ELFCLASS64:
11583
0
      if (note->descsz < 120)
11584
0
  return false;
11585
0
      break;
11586
11587
0
    default:
11588
0
      return false;
11589
0
    }
11590
11591
  /* Check for version 1 in pr_version.  */
11592
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11593
0
    return false;
11594
11595
0
  offset = 4;
11596
11597
  /* Skip over pr_psinfosz. */
11598
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11599
0
    offset += 4;
11600
0
  else
11601
0
    {
11602
0
      offset += 4;  /* Padding before pr_psinfosz. */
11603
0
      offset += 8;
11604
0
    }
11605
11606
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11607
0
  elf_tdata (abfd)->core->program
11608
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11609
0
  offset += 17;
11610
11611
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11612
0
  elf_tdata (abfd)->core->command
11613
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11614
0
  offset += 81;
11615
11616
  /* Padding before pr_pid.  */
11617
0
  offset += 2;
11618
11619
  /* The pr_pid field was added in version "1a".  */
11620
0
  if (note->descsz < offset + 4)
11621
0
    return true;
11622
11623
0
  elf_tdata (abfd)->core->pid
11624
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11625
11626
0
  return true;
11627
0
}
11628
11629
static bool
11630
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11631
0
{
11632
0
  size_t offset;
11633
0
  size_t size;
11634
0
  size_t min_size;
11635
11636
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11637
     Also compute minimum size of this note.  */
11638
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11639
0
    {
11640
0
    case ELFCLASS32:
11641
0
      offset = 4 + 4;
11642
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11643
0
      break;
11644
11645
0
    case ELFCLASS64:
11646
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11647
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11648
0
      break;
11649
11650
0
    default:
11651
0
      return false;
11652
0
    }
11653
11654
0
  if (note->descsz < min_size)
11655
0
    return false;
11656
11657
  /* Check for version 1 in pr_version.  */
11658
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11659
0
    return false;
11660
11661
  /* Extract size of pr_reg from pr_gregsetsz.  */
11662
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11663
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11664
0
    {
11665
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11666
0
      offset += 4 * 2;
11667
0
    }
11668
0
  else
11669
0
    {
11670
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11671
0
      offset += 8 * 2;
11672
0
    }
11673
11674
  /* Skip over pr_osreldate.  */
11675
0
  offset += 4;
11676
11677
  /* Read signal from pr_cursig.  */
11678
0
  if (elf_tdata (abfd)->core->signal == 0)
11679
0
    elf_tdata (abfd)->core->signal
11680
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11681
0
  offset += 4;
11682
11683
  /* Read TID from pr_pid.  */
11684
0
  elf_tdata (abfd)->core->lwpid
11685
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11686
0
  offset += 4;
11687
11688
  /* Padding before pr_reg.  */
11689
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11690
0
    offset += 4;
11691
11692
  /* Make sure that there is enough data remaining in the note.  */
11693
0
  if ((note->descsz - offset) < size)
11694
0
    return false;
11695
11696
  /* Make a ".reg/999" section and a ".reg" section.  */
11697
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11698
0
            size, note->descpos + offset);
11699
0
}
11700
11701
static bool
11702
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11703
0
{
11704
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11705
11706
0
  switch (note->type)
11707
0
    {
11708
0
    case NT_PRSTATUS:
11709
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11710
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11711
0
    return true;
11712
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11713
11714
0
    case NT_FPREGSET:
11715
0
      return elfcore_grok_prfpreg (abfd, note);
11716
11717
0
    case NT_PRPSINFO:
11718
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11719
11720
0
    case NT_FREEBSD_THRMISC:
11721
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11722
11723
0
    case NT_FREEBSD_PROCSTAT_PROC:
11724
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11725
0
                note);
11726
11727
0
    case NT_FREEBSD_PROCSTAT_FILES:
11728
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11729
0
                note);
11730
11731
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11732
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11733
0
                note);
11734
11735
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11736
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11737
11738
0
    case NT_FREEBSD_X86_SEGBASES:
11739
0
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11740
11741
0
    case NT_X86_XSTATE:
11742
0
      return elfcore_grok_xstatereg (abfd, note);
11743
11744
0
    case NT_FREEBSD_PTLWPINFO:
11745
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11746
0
                note);
11747
11748
0
    case NT_ARM_TLS:
11749
0
      return elfcore_grok_aarch_tls (abfd, note);
11750
11751
0
    case NT_ARM_VFP:
11752
0
      return elfcore_grok_arm_vfp (abfd, note);
11753
11754
0
    default:
11755
0
      return true;
11756
0
    }
11757
0
}
11758
11759
static bool
11760
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11761
0
{
11762
0
  char *cp;
11763
11764
0
  cp = strchr (note->namedata, '@');
11765
0
  if (cp != NULL)
11766
0
    {
11767
0
      *lwpidp = atoi(cp + 1);
11768
0
      return true;
11769
0
    }
11770
0
  return false;
11771
0
}
11772
11773
static bool
11774
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11775
0
{
11776
0
  if (note->descsz <= 0x7c + 31)
11777
0
    return false;
11778
11779
  /* Signal number at offset 0x08. */
11780
0
  elf_tdata (abfd)->core->signal
11781
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11782
11783
  /* Process ID at offset 0x50. */
11784
0
  elf_tdata (abfd)->core->pid
11785
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11786
11787
  /* Command name at 0x7c (max 32 bytes, including nul). */
11788
0
  elf_tdata (abfd)->core->command
11789
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11790
11791
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11792
0
            note);
11793
0
}
11794
11795
static bool
11796
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11797
0
{
11798
0
  int lwp;
11799
11800
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11801
0
    elf_tdata (abfd)->core->lwpid = lwp;
11802
11803
0
  switch (note->type)
11804
0
    {
11805
0
    case NT_NETBSDCORE_PROCINFO:
11806
      /* NetBSD-specific core "procinfo".  Note that we expect to
11807
   find this note before any of the others, which is fine,
11808
   since the kernel writes this note out first when it
11809
   creates a core file.  */
11810
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11811
0
    case NT_NETBSDCORE_AUXV:
11812
      /* NetBSD-specific Elf Auxiliary Vector data. */
11813
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11814
0
    case NT_NETBSDCORE_LWPSTATUS:
11815
0
      return elfcore_make_note_pseudosection (abfd,
11816
0
                ".note.netbsdcore.lwpstatus",
11817
0
                note);
11818
0
    default:
11819
0
      break;
11820
0
    }
11821
11822
  /* As of March 2020 there are no other machine-independent notes
11823
     defined for NetBSD core files.  If the note type is less
11824
     than the start of the machine-dependent note types, we don't
11825
     understand it.  */
11826
11827
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11828
0
    return true;
11829
11830
11831
0
  switch (bfd_get_arch (abfd))
11832
0
    {
11833
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11834
   PT_GETFPREGS == mach+2.  */
11835
11836
0
    case bfd_arch_aarch64:
11837
0
    case bfd_arch_alpha:
11838
0
    case bfd_arch_sparc:
11839
0
      switch (note->type)
11840
0
  {
11841
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11842
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11843
11844
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11845
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11846
11847
0
  default:
11848
0
    return true;
11849
0
  }
11850
11851
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11852
   There's also old PT___GETREGS40 == mach + 1 for old reg
11853
   structure which lacks GBR.  */
11854
11855
0
    case bfd_arch_sh:
11856
0
      switch (note->type)
11857
0
  {
11858
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11859
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11860
11861
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11862
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11863
11864
0
  default:
11865
0
    return true;
11866
0
  }
11867
11868
      /* On all other arch's, PT_GETREGS == mach+1 and
11869
   PT_GETFPREGS == mach+3.  */
11870
11871
0
    default:
11872
0
      switch (note->type)
11873
0
  {
11874
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11875
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11876
11877
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11878
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11879
11880
0
  default:
11881
0
    return true;
11882
0
  }
11883
0
    }
11884
    /* NOTREACHED */
11885
0
}
11886
11887
static bool
11888
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11889
0
{
11890
0
  if (note->descsz <= 0x48 + 31)
11891
0
    return false;
11892
11893
  /* Signal number at offset 0x08. */
11894
0
  elf_tdata (abfd)->core->signal
11895
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11896
11897
  /* Process ID at offset 0x20. */
11898
0
  elf_tdata (abfd)->core->pid
11899
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11900
11901
  /* Command name at 0x48 (max 32 bytes, including nul). */
11902
0
  elf_tdata (abfd)->core->command
11903
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11904
11905
0
  return true;
11906
0
}
11907
11908
/* Processes Solaris's process status note.
11909
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11910
   pid_off ~ offsetof(prstatus_t, pr_pid)
11911
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11912
   gregset_size ~ sizeof(gregset_t)
11913
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11914
11915
static bool
11916
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11917
             int pid_off, int lwpid_off, size_t gregset_size,
11918
             size_t gregset_offset)
11919
0
{
11920
0
  asection *sect = NULL;
11921
0
  elf_tdata (abfd)->core->signal
11922
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11923
0
  elf_tdata (abfd)->core->pid
11924
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11925
0
  elf_tdata (abfd)->core->lwpid
11926
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11927
11928
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11929
0
  if (sect != NULL)
11930
0
    sect->size = gregset_size;
11931
11932
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11933
0
            note->descpos + gregset_offset);
11934
0
}
11935
11936
/* Gets program and arguments from a core.
11937
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11938
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11939
11940
static bool
11941
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11942
        int prog_off, int comm_off)
11943
0
{
11944
0
  elf_tdata (abfd)->core->program
11945
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11946
0
  elf_tdata (abfd)->core->command
11947
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11948
11949
0
  return true;
11950
0
}
11951
11952
/* Processes Solaris's LWP status note.
11953
   gregset_size ~ sizeof(gregset_t)
11954
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11955
   fpregset_size ~ sizeof(fpregset_t)
11956
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11957
11958
static bool
11959
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11960
        size_t gregset_size, int gregset_off,
11961
        size_t fpregset_size, int fpregset_off)
11962
0
{
11963
0
  asection *sect = NULL;
11964
0
  char reg2_section_name[16] = { 0 };
11965
11966
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11967
0
       elf_tdata (abfd)->core->lwpid);
11968
11969
  /* offsetof(lwpstatus_t, pr_lwpid) */
11970
0
  elf_tdata (abfd)->core->lwpid
11971
0
    = bfd_get_32 (abfd, note->descdata + 4);
11972
  /* offsetof(lwpstatus_t, pr_cursig) */
11973
0
  elf_tdata (abfd)->core->signal
11974
0
    = bfd_get_16 (abfd, note->descdata + 12);
11975
11976
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11977
0
  if (sect != NULL)
11978
0
    sect->size = gregset_size;
11979
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11980
0
               note->descpos + gregset_off))
11981
0
    return false;
11982
11983
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11984
0
  if (sect != NULL)
11985
0
    {
11986
0
      sect->size = fpregset_size;
11987
0
      sect->filepos = note->descpos + fpregset_off;
11988
0
      sect->alignment_power = 2;
11989
0
    }
11990
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11991
0
               note->descpos + fpregset_off))
11992
0
    return false;
11993
11994
0
  return true;
11995
0
}
11996
11997
static bool
11998
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11999
0
{
12000
0
  if (note == NULL)
12001
0
    return false;
12002
12003
  /* core files are identified as 32- or 64-bit, SPARC or x86,
12004
     by the size of the descsz which matches the sizeof()
12005
     the type appropriate for that note type (e.g., prstatus_t for
12006
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
12007
     on Solaris. The core file bitness may differ from the bitness of
12008
     gdb itself, so fixed values are used instead of sizeof().
12009
     Appropriate fixed offsets are also used to obtain data from
12010
     the note.  */
12011
12012
0
  switch ((int) note->type)
12013
0
    {
12014
0
    case SOLARIS_NT_PRSTATUS:
12015
0
      switch (note->descsz)
12016
0
  {
12017
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12018
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12019
0
                 136, 216, 308, 152, 356);
12020
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12021
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12022
0
                 264, 360, 520, 304, 600);
12023
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
12024
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12025
0
                 136, 216, 308, 76, 356);
12026
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
12027
0
    return elfcore_grok_solaris_prstatus(abfd, note,
12028
0
                 264, 360, 520, 224, 600);
12029
0
  default:
12030
0
    return true;
12031
0
  }
12032
12033
0
    case SOLARIS_NT_PSINFO:
12034
0
    case SOLARIS_NT_PRPSINFO:
12035
0
      switch (note->descsz)
12036
0
  {
12037
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12038
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
12039
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12040
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
12041
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12042
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
12043
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12044
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
12045
0
  default:
12046
0
    return true;
12047
0
  }
12048
12049
0
    case SOLARIS_NT_LWPSTATUS:
12050
0
      switch (note->descsz)
12051
0
  {
12052
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12053
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12054
0
            152, 344, 400, 496);
12055
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12056
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12057
0
            304, 544, 544, 848);
12058
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12059
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12060
0
            76, 344, 380, 420);
12061
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12062
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
12063
0
            224, 544, 528, 768);
12064
0
  default:
12065
0
    return true;
12066
0
  }
12067
12068
0
    case SOLARIS_NT_LWPSINFO:
12069
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12070
0
      if (note->descsz == 128 || note->descsz == 152)
12071
0
  elf_tdata (abfd)->core->lwpid =
12072
0
    bfd_get_32 (abfd, note->descdata + 4);
12073
0
      break;
12074
12075
0
    default:
12076
0
      break;
12077
0
    }
12078
12079
0
  return true;
12080
0
}
12081
12082
/* For name starting with "CORE" this may be either a Solaris
12083
   core file or a gdb-generated core file.  Do Solaris-specific
12084
   processing on selected note types first with
12085
   elfcore_grok_solaris_note(), then process the note
12086
   in elfcore_grok_note().  */
12087
12088
static bool
12089
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12090
0
{
12091
0
  if (!elfcore_grok_solaris_note_impl (abfd, note))
12092
0
    return false;
12093
12094
0
  return elfcore_grok_note (abfd, note);
12095
0
}
12096
12097
static bool
12098
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12099
0
{
12100
0
  if (note->type == NT_OPENBSD_PROCINFO)
12101
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
12102
12103
0
  if (note->type == NT_OPENBSD_REGS)
12104
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
12105
12106
0
  if (note->type == NT_OPENBSD_FPREGS)
12107
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12108
12109
0
  if (note->type == NT_OPENBSD_XFPREGS)
12110
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12111
12112
0
  if (note->type == NT_OPENBSD_AUXV)
12113
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
12114
12115
0
  if (note->type == NT_OPENBSD_WCOOKIE)
12116
0
    {
12117
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12118
0
                 SEC_HAS_CONTENTS);
12119
12120
0
      if (sect == NULL)
12121
0
  return false;
12122
0
      sect->size = note->descsz;
12123
0
      sect->filepos = note->descpos;
12124
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12125
12126
0
      return true;
12127
0
    }
12128
12129
0
  return true;
12130
0
}
12131
12132
static bool
12133
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12134
0
{
12135
0
  void *ddata = note->descdata;
12136
0
  char buf[100];
12137
0
  char *name;
12138
0
  asection *sect;
12139
0
  short sig;
12140
0
  unsigned flags;
12141
12142
0
  if (note->descsz < 16)
12143
0
    return false;
12144
12145
  /* nto_procfs_status 'pid' field is at offset 0.  */
12146
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12147
12148
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
12149
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12150
12151
  /* nto_procfs_status 'flags' field is at offset 8.  */
12152
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12153
12154
  /* nto_procfs_status 'what' field is at offset 14.  */
12155
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12156
0
    {
12157
0
      elf_tdata (abfd)->core->signal = sig;
12158
0
      elf_tdata (abfd)->core->lwpid = *tid;
12159
0
    }
12160
12161
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
12162
     do not come from signals so we make sure we set the current
12163
     thread just in case.  */
12164
0
  if (flags & 0x00000080)
12165
0
    elf_tdata (abfd)->core->lwpid = *tid;
12166
12167
  /* Make a ".qnx_core_status/%d" section.  */
12168
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
12169
12170
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12171
0
  if (name == NULL)
12172
0
    return false;
12173
0
  strcpy (name, buf);
12174
12175
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12176
0
  if (sect == NULL)
12177
0
    return false;
12178
12179
0
  sect->size    = note->descsz;
12180
0
  sect->filepos   = note->descpos;
12181
0
  sect->alignment_power = 2;
12182
12183
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12184
0
}
12185
12186
static bool
12187
elfcore_grok_nto_regs (bfd *abfd,
12188
           Elf_Internal_Note *note,
12189
           long tid,
12190
           char *base)
12191
0
{
12192
0
  char buf[100];
12193
0
  char *name;
12194
0
  asection *sect;
12195
12196
  /* Make a "(base)/%d" section.  */
12197
0
  sprintf (buf, "%s/%ld", base, tid);
12198
12199
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12200
0
  if (name == NULL)
12201
0
    return false;
12202
0
  strcpy (name, buf);
12203
12204
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12205
0
  if (sect == NULL)
12206
0
    return false;
12207
12208
0
  sect->size    = note->descsz;
12209
0
  sect->filepos   = note->descpos;
12210
0
  sect->alignment_power = 2;
12211
12212
  /* This is the current thread.  */
12213
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12214
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12215
12216
0
  return true;
12217
0
}
12218
12219
static bool
12220
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12221
0
{
12222
  /* Every GREG section has a STATUS section before it.  Store the
12223
     tid from the previous call to pass down to the next gregs
12224
     function.  */
12225
0
  static long tid = 1;
12226
12227
0
  switch (note->type)
12228
0
    {
12229
0
    case QNT_CORE_INFO:
12230
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12231
0
    case QNT_CORE_STATUS:
12232
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12233
0
    case QNT_CORE_GREG:
12234
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12235
0
    case QNT_CORE_FPREG:
12236
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12237
0
    default:
12238
0
      return true;
12239
0
    }
12240
0
}
12241
12242
static bool
12243
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12244
0
{
12245
0
  char *name;
12246
0
  asection *sect;
12247
0
  size_t len;
12248
12249
  /* Use note name as section name.  */
12250
0
  len = note->namesz;
12251
0
  name = (char *) bfd_alloc (abfd, len);
12252
0
  if (name == NULL)
12253
0
    return false;
12254
0
  memcpy (name, note->namedata, len);
12255
0
  name[len - 1] = '\0';
12256
12257
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12258
0
  if (sect == NULL)
12259
0
    return false;
12260
12261
0
  sect->size    = note->descsz;
12262
0
  sect->filepos   = note->descpos;
12263
0
  sect->alignment_power = 1;
12264
12265
0
  return true;
12266
0
}
12267
12268
/* Function: elfcore_write_note
12269
12270
   Inputs:
12271
     buffer to hold note, and current size of buffer
12272
     name of note
12273
     type of note
12274
     data for note
12275
     size of data for note
12276
12277
   Writes note to end of buffer.  ELF64 notes are written exactly as
12278
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12279
   that they ought to have 8-byte namesz and descsz field, and have
12280
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12281
12282
   Return:
12283
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12284
12285
char *
12286
elfcore_write_note (bfd *abfd,
12287
        char *buf,
12288
        int *bufsiz,
12289
        const char *name,
12290
        int type,
12291
        const void *input,
12292
        int size)
12293
0
{
12294
0
  Elf_External_Note *xnp;
12295
0
  size_t namesz;
12296
0
  size_t newspace;
12297
0
  char *dest;
12298
12299
0
  namesz = 0;
12300
0
  if (name != NULL)
12301
0
    namesz = strlen (name) + 1;
12302
12303
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12304
12305
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12306
0
  if (buf == NULL)
12307
0
    return buf;
12308
0
  dest = buf + *bufsiz;
12309
0
  *bufsiz += newspace;
12310
0
  xnp = (Elf_External_Note *) dest;
12311
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12312
0
  H_PUT_32 (abfd, size, xnp->descsz);
12313
0
  H_PUT_32 (abfd, type, xnp->type);
12314
0
  dest = xnp->name;
12315
0
  if (name != NULL)
12316
0
    {
12317
0
      memcpy (dest, name, namesz);
12318
0
      dest += namesz;
12319
0
      while (namesz & 3)
12320
0
  {
12321
0
    *dest++ = '\0';
12322
0
    ++namesz;
12323
0
  }
12324
0
    }
12325
0
  memcpy (dest, input, size);
12326
0
  dest += size;
12327
0
  while (size & 3)
12328
0
    {
12329
0
      *dest++ = '\0';
12330
0
      ++size;
12331
0
    }
12332
0
  return buf;
12333
0
}
12334
12335
/* gcc-8 warns (*) on all the strncpy calls in this function about
12336
   possible string truncation.  The "truncation" is not a bug.  We
12337
   have an external representation of structs with fields that are not
12338
   necessarily NULL terminated and corresponding internal
12339
   representation fields that are one larger so that they can always
12340
   be NULL terminated.
12341
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12342
   diagnostics inside functions, giving a hard error if you try to use
12343
   the finer control available with later versions.
12344
   gcc prior to 4.2 warns about diagnostic push and pop.
12345
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12346
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12347
   (*) Depending on your system header files!  */
12348
#if GCC_VERSION >= 8000
12349
# pragma GCC diagnostic push
12350
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12351
#endif
12352
char *
12353
elfcore_write_prpsinfo (bfd  *abfd,
12354
      char *buf,
12355
      int  *bufsiz,
12356
      const char *fname,
12357
      const char *psargs)
12358
0
{
12359
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12360
12361
0
  if (bed->elf_backend_write_core_note != NULL)
12362
0
    {
12363
0
      char *ret;
12364
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12365
0
             NT_PRPSINFO, fname, psargs);
12366
0
      if (ret != NULL)
12367
0
  return ret;
12368
0
    }
12369
12370
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12371
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12372
0
  if (bed->s->elfclass == ELFCLASS32)
12373
0
    {
12374
#  if defined (HAVE_PSINFO32_T)
12375
      psinfo32_t data;
12376
      int note_type = NT_PSINFO;
12377
#  else
12378
0
      prpsinfo32_t data;
12379
0
      int note_type = NT_PRPSINFO;
12380
0
#  endif
12381
12382
0
      memset (&data, 0, sizeof (data));
12383
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12384
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12385
0
      return elfcore_write_note (abfd, buf, bufsiz,
12386
0
         "CORE", note_type, &data, sizeof (data));
12387
0
    }
12388
0
  else
12389
0
# endif
12390
0
    {
12391
# if defined (HAVE_PSINFO_T)
12392
      psinfo_t data;
12393
      int note_type = NT_PSINFO;
12394
# else
12395
0
      prpsinfo_t data;
12396
0
      int note_type = NT_PRPSINFO;
12397
0
# endif
12398
12399
0
      memset (&data, 0, sizeof (data));
12400
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12401
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12402
0
      return elfcore_write_note (abfd, buf, bufsiz,
12403
0
         "CORE", note_type, &data, sizeof (data));
12404
0
    }
12405
0
#endif  /* PSINFO_T or PRPSINFO_T */
12406
12407
0
  free (buf);
12408
0
  return NULL;
12409
0
}
12410
#if GCC_VERSION >= 8000
12411
# pragma GCC diagnostic pop
12412
#endif
12413
12414
char *
12415
elfcore_write_linux_prpsinfo32
12416
  (bfd *abfd, char *buf, int *bufsiz,
12417
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12418
0
{
12419
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12420
0
    {
12421
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12422
12423
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12424
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12425
0
         &data, sizeof (data));
12426
0
    }
12427
0
  else
12428
0
    {
12429
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12430
12431
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12432
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12433
0
         &data, sizeof (data));
12434
0
    }
12435
0
}
12436
12437
char *
12438
elfcore_write_linux_prpsinfo64
12439
  (bfd *abfd, char *buf, int *bufsiz,
12440
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12441
0
{
12442
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12443
0
    {
12444
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12445
12446
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12447
0
      return elfcore_write_note (abfd, buf, bufsiz,
12448
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12449
0
    }
12450
0
  else
12451
0
    {
12452
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12453
12454
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12455
0
      return elfcore_write_note (abfd, buf, bufsiz,
12456
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
12457
0
    }
12458
0
}
12459
12460
char *
12461
elfcore_write_prstatus (bfd *abfd,
12462
      char *buf,
12463
      int *bufsiz,
12464
      long pid,
12465
      int cursig,
12466
      const void *gregs)
12467
0
{
12468
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12469
12470
0
  if (bed->elf_backend_write_core_note != NULL)
12471
0
    {
12472
0
      char *ret;
12473
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12474
0
             NT_PRSTATUS,
12475
0
             pid, cursig, gregs);
12476
0
      if (ret != NULL)
12477
0
  return ret;
12478
0
    }
12479
12480
0
#if defined (HAVE_PRSTATUS_T)
12481
0
#if defined (HAVE_PRSTATUS32_T)
12482
0
  if (bed->s->elfclass == ELFCLASS32)
12483
0
    {
12484
0
      prstatus32_t prstat;
12485
12486
0
      memset (&prstat, 0, sizeof (prstat));
12487
0
      prstat.pr_pid = pid;
12488
0
      prstat.pr_cursig = cursig;
12489
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12490
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12491
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12492
0
    }
12493
0
  else
12494
0
#endif
12495
0
    {
12496
0
      prstatus_t prstat;
12497
12498
0
      memset (&prstat, 0, sizeof (prstat));
12499
0
      prstat.pr_pid = pid;
12500
0
      prstat.pr_cursig = cursig;
12501
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12502
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12503
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12504
0
    }
12505
0
#endif /* HAVE_PRSTATUS_T */
12506
12507
0
  free (buf);
12508
0
  return NULL;
12509
0
}
12510
12511
#if defined (HAVE_LWPSTATUS_T)
12512
char *
12513
elfcore_write_lwpstatus (bfd *abfd,
12514
       char *buf,
12515
       int *bufsiz,
12516
       long pid,
12517
       int cursig,
12518
       const void *gregs)
12519
{
12520
  lwpstatus_t lwpstat;
12521
  const char *note_name = "CORE";
12522
12523
  memset (&lwpstat, 0, sizeof (lwpstat));
12524
  lwpstat.pr_lwpid  = pid >> 16;
12525
  lwpstat.pr_cursig = cursig;
12526
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12527
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12528
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12529
#if !defined(gregs)
12530
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12531
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12532
#else
12533
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12534
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12535
#endif
12536
#endif
12537
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
12538
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12539
}
12540
#endif /* HAVE_LWPSTATUS_T */
12541
12542
#if defined (HAVE_PSTATUS_T)
12543
char *
12544
elfcore_write_pstatus (bfd *abfd,
12545
           char *buf,
12546
           int *bufsiz,
12547
           long pid,
12548
           int cursig ATTRIBUTE_UNUSED,
12549
           const void *gregs ATTRIBUTE_UNUSED)
12550
{
12551
  const char *note_name = "CORE";
12552
#if defined (HAVE_PSTATUS32_T)
12553
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12554
12555
  if (bed->s->elfclass == ELFCLASS32)
12556
    {
12557
      pstatus32_t pstat;
12558
12559
      memset (&pstat, 0, sizeof (pstat));
12560
      pstat.pr_pid = pid & 0xffff;
12561
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12562
        NT_PSTATUS, &pstat, sizeof (pstat));
12563
      return buf;
12564
    }
12565
  else
12566
#endif
12567
    {
12568
      pstatus_t pstat;
12569
12570
      memset (&pstat, 0, sizeof (pstat));
12571
      pstat.pr_pid = pid & 0xffff;
12572
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12573
        NT_PSTATUS, &pstat, sizeof (pstat));
12574
      return buf;
12575
    }
12576
}
12577
#endif /* HAVE_PSTATUS_T */
12578
12579
char *
12580
elfcore_write_prfpreg (bfd *abfd,
12581
           char *buf,
12582
           int *bufsiz,
12583
           const void *fpregs,
12584
           int size)
12585
0
{
12586
0
  const char *note_name = "CORE";
12587
0
  return elfcore_write_note (abfd, buf, bufsiz,
12588
0
           note_name, NT_FPREGSET, fpregs, size);
12589
0
}
12590
12591
char *
12592
elfcore_write_prxfpreg (bfd *abfd,
12593
      char *buf,
12594
      int *bufsiz,
12595
      const void *xfpregs,
12596
      int size)
12597
0
{
12598
0
  char *note_name = "LINUX";
12599
0
  return elfcore_write_note (abfd, buf, bufsiz,
12600
0
           note_name, NT_PRXFPREG, xfpregs, size);
12601
0
}
12602
12603
char *
12604
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12605
       const void *xfpregs, int size)
12606
0
{
12607
0
  char *note_name;
12608
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12609
0
    note_name = "FreeBSD";
12610
0
  else
12611
0
    note_name = "LINUX";
12612
0
  return elfcore_write_note (abfd, buf, bufsiz,
12613
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12614
0
}
12615
12616
char *
12617
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12618
          const void *regs, int size)
12619
0
{
12620
0
  char *note_name = "FreeBSD";
12621
0
  return elfcore_write_note (abfd, buf, bufsiz,
12622
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12623
0
}
12624
12625
char *
12626
elfcore_write_ppc_vmx (bfd *abfd,
12627
           char *buf,
12628
           int *bufsiz,
12629
           const void *ppc_vmx,
12630
           int size)
12631
0
{
12632
0
  char *note_name = "LINUX";
12633
0
  return elfcore_write_note (abfd, buf, bufsiz,
12634
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
12635
0
}
12636
12637
char *
12638
elfcore_write_ppc_vsx (bfd *abfd,
12639
           char *buf,
12640
           int *bufsiz,
12641
           const void *ppc_vsx,
12642
           int size)
12643
0
{
12644
0
  char *note_name = "LINUX";
12645
0
  return elfcore_write_note (abfd, buf, bufsiz,
12646
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
12647
0
}
12648
12649
char *
12650
elfcore_write_ppc_tar (bfd *abfd,
12651
           char *buf,
12652
           int *bufsiz,
12653
           const void *ppc_tar,
12654
           int size)
12655
0
{
12656
0
  char *note_name = "LINUX";
12657
0
  return elfcore_write_note (abfd, buf, bufsiz,
12658
0
           note_name, NT_PPC_TAR, ppc_tar, size);
12659
0
}
12660
12661
char *
12662
elfcore_write_ppc_ppr (bfd *abfd,
12663
           char *buf,
12664
           int *bufsiz,
12665
           const void *ppc_ppr,
12666
           int size)
12667
0
{
12668
0
  char *note_name = "LINUX";
12669
0
  return elfcore_write_note (abfd, buf, bufsiz,
12670
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12671
0
}
12672
12673
char *
12674
elfcore_write_ppc_dscr (bfd *abfd,
12675
      char *buf,
12676
      int *bufsiz,
12677
      const void *ppc_dscr,
12678
      int size)
12679
0
{
12680
0
  char *note_name = "LINUX";
12681
0
  return elfcore_write_note (abfd, buf, bufsiz,
12682
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12683
0
}
12684
12685
char *
12686
elfcore_write_ppc_ebb (bfd *abfd,
12687
           char *buf,
12688
           int *bufsiz,
12689
           const void *ppc_ebb,
12690
           int size)
12691
0
{
12692
0
  char *note_name = "LINUX";
12693
0
  return elfcore_write_note (abfd, buf, bufsiz,
12694
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12695
0
}
12696
12697
char *
12698
elfcore_write_ppc_pmu (bfd *abfd,
12699
           char *buf,
12700
           int *bufsiz,
12701
           const void *ppc_pmu,
12702
           int size)
12703
0
{
12704
0
  char *note_name = "LINUX";
12705
0
  return elfcore_write_note (abfd, buf, bufsiz,
12706
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12707
0
}
12708
12709
char *
12710
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12711
         char *buf,
12712
         int *bufsiz,
12713
         const void *ppc_tm_cgpr,
12714
         int size)
12715
0
{
12716
0
  char *note_name = "LINUX";
12717
0
  return elfcore_write_note (abfd, buf, bufsiz,
12718
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12719
0
}
12720
12721
char *
12722
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12723
         char *buf,
12724
         int *bufsiz,
12725
         const void *ppc_tm_cfpr,
12726
         int size)
12727
0
{
12728
0
  char *note_name = "LINUX";
12729
0
  return elfcore_write_note (abfd, buf, bufsiz,
12730
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12731
0
}
12732
12733
char *
12734
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12735
         char *buf,
12736
         int *bufsiz,
12737
         const void *ppc_tm_cvmx,
12738
         int size)
12739
0
{
12740
0
  char *note_name = "LINUX";
12741
0
  return elfcore_write_note (abfd, buf, bufsiz,
12742
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12743
0
}
12744
12745
char *
12746
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12747
         char *buf,
12748
         int *bufsiz,
12749
         const void *ppc_tm_cvsx,
12750
         int size)
12751
0
{
12752
0
  char *note_name = "LINUX";
12753
0
  return elfcore_write_note (abfd, buf, bufsiz,
12754
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12755
0
}
12756
12757
char *
12758
elfcore_write_ppc_tm_spr (bfd *abfd,
12759
        char *buf,
12760
        int *bufsiz,
12761
        const void *ppc_tm_spr,
12762
        int size)
12763
0
{
12764
0
  char *note_name = "LINUX";
12765
0
  return elfcore_write_note (abfd, buf, bufsiz,
12766
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12767
0
}
12768
12769
char *
12770
elfcore_write_ppc_tm_ctar (bfd *abfd,
12771
         char *buf,
12772
         int *bufsiz,
12773
         const void *ppc_tm_ctar,
12774
         int size)
12775
0
{
12776
0
  char *note_name = "LINUX";
12777
0
  return elfcore_write_note (abfd, buf, bufsiz,
12778
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12779
0
}
12780
12781
char *
12782
elfcore_write_ppc_tm_cppr (bfd *abfd,
12783
         char *buf,
12784
         int *bufsiz,
12785
         const void *ppc_tm_cppr,
12786
         int size)
12787
0
{
12788
0
  char *note_name = "LINUX";
12789
0
  return elfcore_write_note (abfd, buf, bufsiz,
12790
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12791
0
}
12792
12793
char *
12794
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12795
          char *buf,
12796
          int *bufsiz,
12797
          const void *ppc_tm_cdscr,
12798
          int size)
12799
0
{
12800
0
  char *note_name = "LINUX";
12801
0
  return elfcore_write_note (abfd, buf, bufsiz,
12802
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12803
0
}
12804
12805
static char *
12806
elfcore_write_s390_high_gprs (bfd *abfd,
12807
            char *buf,
12808
            int *bufsiz,
12809
            const void *s390_high_gprs,
12810
            int size)
12811
0
{
12812
0
  char *note_name = "LINUX";
12813
0
  return elfcore_write_note (abfd, buf, bufsiz,
12814
0
           note_name, NT_S390_HIGH_GPRS,
12815
0
           s390_high_gprs, size);
12816
0
}
12817
12818
char *
12819
elfcore_write_s390_timer (bfd *abfd,
12820
        char *buf,
12821
        int *bufsiz,
12822
        const void *s390_timer,
12823
        int size)
12824
0
{
12825
0
  char *note_name = "LINUX";
12826
0
  return elfcore_write_note (abfd, buf, bufsiz,
12827
0
           note_name, NT_S390_TIMER, s390_timer, size);
12828
0
}
12829
12830
char *
12831
elfcore_write_s390_todcmp (bfd *abfd,
12832
         char *buf,
12833
         int *bufsiz,
12834
         const void *s390_todcmp,
12835
         int size)
12836
0
{
12837
0
  char *note_name = "LINUX";
12838
0
  return elfcore_write_note (abfd, buf, bufsiz,
12839
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12840
0
}
12841
12842
char *
12843
elfcore_write_s390_todpreg (bfd *abfd,
12844
          char *buf,
12845
          int *bufsiz,
12846
          const void *s390_todpreg,
12847
          int size)
12848
0
{
12849
0
  char *note_name = "LINUX";
12850
0
  return elfcore_write_note (abfd, buf, bufsiz,
12851
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12852
0
}
12853
12854
char *
12855
elfcore_write_s390_ctrs (bfd *abfd,
12856
       char *buf,
12857
       int *bufsiz,
12858
       const void *s390_ctrs,
12859
       int size)
12860
0
{
12861
0
  char *note_name = "LINUX";
12862
0
  return elfcore_write_note (abfd, buf, bufsiz,
12863
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12864
0
}
12865
12866
char *
12867
elfcore_write_s390_prefix (bfd *abfd,
12868
         char *buf,
12869
         int *bufsiz,
12870
         const void *s390_prefix,
12871
         int size)
12872
0
{
12873
0
  char *note_name = "LINUX";
12874
0
  return elfcore_write_note (abfd, buf, bufsiz,
12875
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12876
0
}
12877
12878
char *
12879
elfcore_write_s390_last_break (bfd *abfd,
12880
             char *buf,
12881
             int *bufsiz,
12882
             const void *s390_last_break,
12883
             int size)
12884
0
{
12885
0
  char *note_name = "LINUX";
12886
0
  return elfcore_write_note (abfd, buf, bufsiz,
12887
0
           note_name, NT_S390_LAST_BREAK,
12888
0
           s390_last_break, size);
12889
0
}
12890
12891
char *
12892
elfcore_write_s390_system_call (bfd *abfd,
12893
        char *buf,
12894
        int *bufsiz,
12895
        const void *s390_system_call,
12896
        int size)
12897
0
{
12898
0
  char *note_name = "LINUX";
12899
0
  return elfcore_write_note (abfd, buf, bufsiz,
12900
0
           note_name, NT_S390_SYSTEM_CALL,
12901
0
           s390_system_call, size);
12902
0
}
12903
12904
char *
12905
elfcore_write_s390_tdb (bfd *abfd,
12906
      char *buf,
12907
      int *bufsiz,
12908
      const void *s390_tdb,
12909
      int size)
12910
0
{
12911
0
  char *note_name = "LINUX";
12912
0
  return elfcore_write_note (abfd, buf, bufsiz,
12913
0
           note_name, NT_S390_TDB, s390_tdb, size);
12914
0
}
12915
12916
char *
12917
elfcore_write_s390_vxrs_low (bfd *abfd,
12918
           char *buf,
12919
           int *bufsiz,
12920
           const void *s390_vxrs_low,
12921
           int size)
12922
0
{
12923
0
  char *note_name = "LINUX";
12924
0
  return elfcore_write_note (abfd, buf, bufsiz,
12925
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12926
0
}
12927
12928
char *
12929
elfcore_write_s390_vxrs_high (bfd *abfd,
12930
           char *buf,
12931
           int *bufsiz,
12932
           const void *s390_vxrs_high,
12933
           int size)
12934
0
{
12935
0
  char *note_name = "LINUX";
12936
0
  return elfcore_write_note (abfd, buf, bufsiz,
12937
0
           note_name, NT_S390_VXRS_HIGH,
12938
0
           s390_vxrs_high, size);
12939
0
}
12940
12941
char *
12942
elfcore_write_s390_gs_cb (bfd *abfd,
12943
        char *buf,
12944
        int *bufsiz,
12945
        const void *s390_gs_cb,
12946
        int size)
12947
0
{
12948
0
  char *note_name = "LINUX";
12949
0
  return elfcore_write_note (abfd, buf, bufsiz,
12950
0
           note_name, NT_S390_GS_CB,
12951
0
           s390_gs_cb, size);
12952
0
}
12953
12954
char *
12955
elfcore_write_s390_gs_bc (bfd *abfd,
12956
        char *buf,
12957
        int *bufsiz,
12958
        const void *s390_gs_bc,
12959
        int size)
12960
0
{
12961
0
  char *note_name = "LINUX";
12962
0
  return elfcore_write_note (abfd, buf, bufsiz,
12963
0
           note_name, NT_S390_GS_BC,
12964
0
           s390_gs_bc, size);
12965
0
}
12966
12967
char *
12968
elfcore_write_arm_vfp (bfd *abfd,
12969
           char *buf,
12970
           int *bufsiz,
12971
           const void *arm_vfp,
12972
           int size)
12973
0
{
12974
0
  char *note_name = "LINUX";
12975
0
  return elfcore_write_note (abfd, buf, bufsiz,
12976
0
           note_name, NT_ARM_VFP, arm_vfp, size);
12977
0
}
12978
12979
char *
12980
elfcore_write_aarch_tls (bfd *abfd,
12981
           char *buf,
12982
           int *bufsiz,
12983
           const void *aarch_tls,
12984
           int size)
12985
0
{
12986
0
  char *note_name = "LINUX";
12987
0
  return elfcore_write_note (abfd, buf, bufsiz,
12988
0
           note_name, NT_ARM_TLS, aarch_tls, size);
12989
0
}
12990
12991
char *
12992
elfcore_write_aarch_hw_break (bfd *abfd,
12993
          char *buf,
12994
          int *bufsiz,
12995
          const void *aarch_hw_break,
12996
          int size)
12997
0
{
12998
0
  char *note_name = "LINUX";
12999
0
  return elfcore_write_note (abfd, buf, bufsiz,
13000
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13001
0
}
13002
13003
char *
13004
elfcore_write_aarch_hw_watch (bfd *abfd,
13005
          char *buf,
13006
          int *bufsiz,
13007
          const void *aarch_hw_watch,
13008
          int size)
13009
0
{
13010
0
  char *note_name = "LINUX";
13011
0
  return elfcore_write_note (abfd, buf, bufsiz,
13012
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13013
0
}
13014
13015
char *
13016
elfcore_write_aarch_sve (bfd *abfd,
13017
       char *buf,
13018
       int *bufsiz,
13019
       const void *aarch_sve,
13020
       int size)
13021
0
{
13022
0
  char *note_name = "LINUX";
13023
0
  return elfcore_write_note (abfd, buf, bufsiz,
13024
0
           note_name, NT_ARM_SVE, aarch_sve, size);
13025
0
}
13026
13027
char *
13028
elfcore_write_aarch_pauth (bfd *abfd,
13029
         char *buf,
13030
         int *bufsiz,
13031
         const void *aarch_pauth,
13032
         int size)
13033
0
{
13034
0
  char *note_name = "LINUX";
13035
0
  return elfcore_write_note (abfd, buf, bufsiz,
13036
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13037
0
}
13038
13039
char *
13040
elfcore_write_aarch_mte (bfd *abfd,
13041
              char *buf,
13042
              int *bufsiz,
13043
              const void *aarch_mte,
13044
              int size)
13045
0
{
13046
0
  char *note_name = "LINUX";
13047
0
  return elfcore_write_note (abfd, buf, bufsiz,
13048
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
13049
0
           aarch_mte,
13050
0
           size);
13051
0
}
13052
13053
char *
13054
elfcore_write_aarch_ssve (bfd *abfd,
13055
        char *buf,
13056
        int *bufsiz,
13057
        const void *aarch_ssve,
13058
        int size)
13059
0
{
13060
0
  char *note_name = "LINUX";
13061
0
  return elfcore_write_note (abfd, buf, bufsiz,
13062
0
           note_name, NT_ARM_SSVE,
13063
0
           aarch_ssve,
13064
0
           size);
13065
0
}
13066
13067
char *
13068
elfcore_write_aarch_za (bfd *abfd,
13069
      char *buf,
13070
      int *bufsiz,
13071
      const void *aarch_za,
13072
      int size)
13073
0
{
13074
0
  char *note_name = "LINUX";
13075
0
  return elfcore_write_note (abfd, buf, bufsiz,
13076
0
           note_name, NT_ARM_ZA,
13077
0
           aarch_za,
13078
0
           size);
13079
0
}
13080
13081
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
13082
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13083
   written into.  Return a pointer to the new start of the note buffer, to
13084
   replace BUF which may no longer be valid.  */
13085
13086
char *
13087
elfcore_write_aarch_zt (bfd *abfd,
13088
      char *buf,
13089
      int *bufsiz,
13090
      const void *aarch_zt,
13091
      int size)
13092
0
{
13093
0
  char *note_name = "LINUX";
13094
0
  return elfcore_write_note (abfd, buf, bufsiz,
13095
0
           note_name, NT_ARM_ZT,
13096
0
           aarch_zt,
13097
0
           size);
13098
0
}
13099
13100
char *
13101
elfcore_write_arc_v2 (bfd *abfd,
13102
          char *buf,
13103
          int *bufsiz,
13104
          const void *arc_v2,
13105
          int size)
13106
0
{
13107
0
  char *note_name = "LINUX";
13108
0
  return elfcore_write_note (abfd, buf, bufsiz,
13109
0
           note_name, NT_ARC_V2, arc_v2, size);
13110
0
}
13111
13112
char *
13113
elfcore_write_loongarch_cpucfg (bfd *abfd,
13114
        char *buf,
13115
        int *bufsiz,
13116
        const void *loongarch_cpucfg,
13117
        int size)
13118
0
{
13119
0
  char *note_name = "LINUX";
13120
0
  return elfcore_write_note (abfd, buf, bufsiz,
13121
0
           note_name, NT_LARCH_CPUCFG,
13122
0
           loongarch_cpucfg, size);
13123
0
}
13124
13125
char *
13126
elfcore_write_loongarch_lbt (bfd *abfd,
13127
           char *buf,
13128
           int *bufsiz,
13129
           const void *loongarch_lbt,
13130
           int size)
13131
0
{
13132
0
  char *note_name = "LINUX";
13133
0
  return elfcore_write_note (abfd, buf, bufsiz,
13134
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
13135
0
}
13136
13137
char *
13138
elfcore_write_loongarch_lsx (bfd *abfd,
13139
           char *buf,
13140
           int *bufsiz,
13141
           const void *loongarch_lsx,
13142
           int size)
13143
0
{
13144
0
  char *note_name = "LINUX";
13145
0
  return elfcore_write_note (abfd, buf, bufsiz,
13146
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
13147
0
}
13148
13149
char *
13150
elfcore_write_loongarch_lasx (bfd *abfd,
13151
            char *buf,
13152
            int *bufsiz,
13153
            const void *loongarch_lasx,
13154
            int size)
13155
0
{
13156
0
  char *note_name = "LINUX";
13157
0
  return elfcore_write_note (abfd, buf, bufsiz,
13158
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
13159
0
}
13160
13161
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13162
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13163
   written into.  Return a pointer to the new start of the note buffer, to
13164
   replace BUF which may no longer be valid.  */
13165
13166
char *
13167
elfcore_write_riscv_csr (bfd *abfd,
13168
       char *buf,
13169
       int *bufsiz,
13170
       const void *csrs,
13171
       int size)
13172
0
{
13173
0
  const char *note_name = "GDB";
13174
0
  return elfcore_write_note (abfd, buf, bufsiz,
13175
0
           note_name, NT_RISCV_CSR, csrs, size);
13176
0
}
13177
13178
/* Write the target description (a string) pointed to by TDESC, length
13179
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13180
   note is being written into.  Return a pointer to the new start of the
13181
   note buffer, to replace BUF which may no longer be valid.  */
13182
13183
char *
13184
elfcore_write_gdb_tdesc (bfd *abfd,
13185
       char *buf,
13186
       int *bufsiz,
13187
       const void *tdesc,
13188
       int size)
13189
0
{
13190
0
  const char *note_name = "GDB";
13191
0
  return elfcore_write_note (abfd, buf, bufsiz,
13192
0
           note_name, NT_GDB_TDESC, tdesc, size);
13193
0
}
13194
13195
char *
13196
elfcore_write_register_note (bfd *abfd,
13197
           char *buf,
13198
           int *bufsiz,
13199
           const char *section,
13200
           const void *data,
13201
           int size)
13202
0
{
13203
0
  if (strcmp (section, ".reg2") == 0)
13204
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13205
0
  if (strcmp (section, ".reg-xfp") == 0)
13206
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13207
0
  if (strcmp (section, ".reg-xstate") == 0)
13208
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13209
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
13210
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13211
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
13212
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13213
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
13214
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13215
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
13216
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13217
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
13218
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13219
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
13220
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13221
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
13222
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13223
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
13224
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13225
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13226
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13227
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13228
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13229
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13230
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13231
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13232
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13233
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13234
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13235
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13236
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13237
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13238
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13239
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13240
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13241
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
13242
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13243
0
  if (strcmp (section, ".reg-s390-timer") == 0)
13244
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13245
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
13246
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13247
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
13248
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13249
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
13250
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13251
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
13252
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13253
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
13254
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13255
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
13256
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13257
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
13258
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13259
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13260
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13261
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13262
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13263
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
13264
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13265
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
13266
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13267
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
13268
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13269
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
13270
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13271
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
13272
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13273
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13274
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13275
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
13276
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13277
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
13278
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13279
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
13280
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13281
0
  if (strcmp (section, ".reg-aarch-ssve") == 0)
13282
0
    return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13283
0
  if (strcmp (section, ".reg-aarch-za") == 0)
13284
0
    return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13285
0
  if (strcmp (section, ".reg-aarch-zt") == 0)
13286
0
    return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13287
0
  if (strcmp (section, ".reg-arc-v2") == 0)
13288
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13289
0
  if (strcmp (section, ".gdb-tdesc") == 0)
13290
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13291
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
13292
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13293
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13294
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13295
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
13296
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13297
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
13298
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13299
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
13300
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13301
0
  return NULL;
13302
0
}
13303
13304
char *
13305
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13306
       const void *buf, int bufsiz)
13307
0
{
13308
0
  return elfcore_write_note (obfd, note_data, note_size,
13309
0
           "CORE", NT_FILE, buf, bufsiz);
13310
0
}
13311
13312
static bool
13313
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13314
     size_t align)
13315
0
{
13316
0
  char *p;
13317
13318
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13319
     gABI specifies that PT_NOTE alignment should be aligned to 4
13320
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13321
     align is less than 4, we use 4 byte alignment.   */
13322
0
  if (align < 4)
13323
0
    align = 4;
13324
0
  if (align != 4 && align != 8)
13325
0
    return false;
13326
13327
0
  p = buf;
13328
0
  while (p < buf + size)
13329
0
    {
13330
0
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13331
0
      Elf_Internal_Note in;
13332
13333
0
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13334
0
  return false;
13335
13336
0
      in.type = H_GET_32 (abfd, xnp->type);
13337
13338
0
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13339
0
      in.namedata = xnp->name;
13340
0
      if (in.namesz > buf - in.namedata + size)
13341
0
  return false;
13342
13343
0
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13344
0
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13345
0
      in.descpos = offset + (in.descdata - buf);
13346
0
      if (in.descsz != 0
13347
0
    && (in.descdata >= buf + size
13348
0
        || in.descsz > buf - in.descdata + size))
13349
0
  return false;
13350
13351
0
      switch (bfd_get_format (abfd))
13352
0
  {
13353
0
  default:
13354
0
    return true;
13355
13356
0
  case bfd_core:
13357
0
    {
13358
0
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13359
0
      struct
13360
0
      {
13361
0
        const char * string;
13362
0
        size_t len;
13363
0
        bool (*func) (bfd *, Elf_Internal_Note *);
13364
0
      }
13365
0
      grokers[] =
13366
0
      {
13367
0
        GROKER_ELEMENT ("", elfcore_grok_note),
13368
0
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13369
0
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13370
0
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13371
0
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13372
0
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13373
0
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13374
0
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13375
0
      };
13376
0
#undef GROKER_ELEMENT
13377
0
      int i;
13378
13379
0
      for (i = ARRAY_SIZE (grokers); i--;)
13380
0
        {
13381
0
    if (in.namesz >= grokers[i].len
13382
0
        && strncmp (in.namedata, grokers[i].string,
13383
0
        grokers[i].len) == 0)
13384
0
      {
13385
0
        if (! grokers[i].func (abfd, & in))
13386
0
          return false;
13387
0
        break;
13388
0
      }
13389
0
        }
13390
0
      break;
13391
0
    }
13392
13393
0
  case bfd_object:
13394
0
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13395
0
      {
13396
0
        if (! elfobj_grok_gnu_note (abfd, &in))
13397
0
    return false;
13398
0
      }
13399
0
    else if (in.namesz == sizeof "stapsdt"
13400
0
       && strcmp (in.namedata, "stapsdt") == 0)
13401
0
      {
13402
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13403
0
    return false;
13404
0
      }
13405
0
    break;
13406
0
  }
13407
13408
0
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13409
0
    }
13410
13411
0
  return true;
13412
0
}
13413
13414
bool
13415
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13416
    size_t align)
13417
0
{
13418
0
  char *buf;
13419
13420
0
  if (size == 0 || (size + 1) == 0)
13421
0
    return true;
13422
13423
0
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13424
0
    return false;
13425
13426
0
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13427
0
  if (buf == NULL)
13428
0
    return false;
13429
13430
  /* PR 17512: file: ec08f814
13431
     0-termintate the buffer so that string searches will not overflow.  */
13432
0
  buf[size] = 0;
13433
13434
0
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13435
0
    {
13436
0
      free (buf);
13437
0
      return false;
13438
0
    }
13439
13440
0
  free (buf);
13441
0
  return true;
13442
0
}
13443

13444
/* Providing external access to the ELF program header table.  */
13445
13446
/* Return an upper bound on the number of bytes required to store a
13447
   copy of ABFD's program header table entries.  Return -1 if an error
13448
   occurs; bfd_get_error will return an appropriate code.  */
13449
13450
long
13451
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13452
0
{
13453
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13454
0
    {
13455
0
      bfd_set_error (bfd_error_wrong_format);
13456
0
      return -1;
13457
0
    }
13458
13459
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13460
0
}
13461
13462
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13463
   will be stored as an array of Elf_Internal_Phdr structures, as
13464
   defined in include/elf/internal.h.  To find out how large the
13465
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13466
13467
   Return the number of program header table entries read, or -1 if an
13468
   error occurs; bfd_get_error will return an appropriate code.  */
13469
13470
int
13471
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13472
0
{
13473
0
  int num_phdrs;
13474
13475
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13476
0
    {
13477
0
      bfd_set_error (bfd_error_wrong_format);
13478
0
      return -1;
13479
0
    }
13480
13481
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13482
0
  if (num_phdrs != 0)
13483
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13484
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13485
13486
0
  return num_phdrs;
13487
0
}
13488
13489
enum elf_reloc_type_class
13490
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13491
         const asection *rel_sec ATTRIBUTE_UNUSED,
13492
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13493
0
{
13494
0
  return reloc_class_normal;
13495
0
}
13496
13497
/* For RELA architectures, return the relocation value for a
13498
   relocation against a local symbol.  */
13499
13500
bfd_vma
13501
_bfd_elf_rela_local_sym (bfd *abfd,
13502
       Elf_Internal_Sym *sym,
13503
       asection **psec,
13504
       Elf_Internal_Rela *rel)
13505
0
{
13506
0
  asection *sec = *psec;
13507
0
  bfd_vma relocation;
13508
13509
0
  relocation = (sec->output_section->vma
13510
0
    + sec->output_offset
13511
0
    + sym->st_value);
13512
0
  if ((sec->flags & SEC_MERGE)
13513
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13514
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13515
0
    {
13516
0
      rel->r_addend =
13517
0
  _bfd_merged_section_offset (abfd, psec,
13518
0
            elf_section_data (sec)->sec_info,
13519
0
            sym->st_value + rel->r_addend);
13520
0
      if (sec != *psec)
13521
0
  {
13522
    /* If we have changed the section, and our original section is
13523
       marked with SEC_EXCLUDE, it means that the original
13524
       SEC_MERGE section has been completely subsumed in some
13525
       other SEC_MERGE section.  In this case, we need to leave
13526
       some info around for --emit-relocs.  */
13527
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13528
0
      sec->kept_section = *psec;
13529
0
    sec = *psec;
13530
0
  }
13531
0
      rel->r_addend -= relocation;
13532
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13533
0
    }
13534
0
  return relocation;
13535
0
}
13536
13537
bfd_vma
13538
_bfd_elf_rel_local_sym (bfd *abfd,
13539
      Elf_Internal_Sym *sym,
13540
      asection **psec,
13541
      bfd_vma addend)
13542
0
{
13543
0
  asection *sec = *psec;
13544
13545
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13546
0
    return sym->st_value + addend;
13547
13548
0
  return _bfd_merged_section_offset (abfd, psec,
13549
0
             elf_section_data (sec)->sec_info,
13550
0
             sym->st_value + addend);
13551
0
}
13552
13553
/* Adjust an address within a section.  Given OFFSET within SEC, return
13554
   the new offset within the section, based upon changes made to the
13555
   section.  Returns -1 if the offset is now invalid.
13556
   The offset (in abnd out) is in target sized bytes, however big a
13557
   byte may be.  */
13558
13559
bfd_vma
13560
_bfd_elf_section_offset (bfd *abfd,
13561
       struct bfd_link_info *info,
13562
       asection *sec,
13563
       bfd_vma offset)
13564
0
{
13565
0
  switch (sec->sec_info_type)
13566
0
    {
13567
0
    case SEC_INFO_TYPE_STABS:
13568
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13569
0
               offset);
13570
0
    case SEC_INFO_TYPE_EH_FRAME:
13571
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13572
13573
0
    default:
13574
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13575
0
  {
13576
    /* Reverse the offset.  */
13577
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13578
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13579
13580
    /* address_size and sec->size are in octets.  Convert
13581
       to bytes before subtracting the original offset.  */
13582
0
    offset = ((sec->size - address_size)
13583
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13584
0
  }
13585
0
      return offset;
13586
0
    }
13587
0
}
13588

13589
long
13590
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13591
             long symcount ATTRIBUTE_UNUSED,
13592
             asymbol **syms ATTRIBUTE_UNUSED,
13593
             long dynsymcount,
13594
             asymbol **dynsyms,
13595
             asymbol **ret)
13596
0
{
13597
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13598
0
  asection *relplt;
13599
0
  asymbol *s;
13600
0
  const char *relplt_name;
13601
0
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13602
0
  arelent *p;
13603
0
  long count, i, n;
13604
0
  size_t size;
13605
0
  Elf_Internal_Shdr *hdr;
13606
0
  char *names;
13607
0
  asection *plt;
13608
13609
0
  *ret = NULL;
13610
13611
0
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13612
0
    return 0;
13613
13614
0
  if (dynsymcount <= 0)
13615
0
    return 0;
13616
13617
0
  if (!bed->plt_sym_val)
13618
0
    return 0;
13619
13620
0
  relplt_name = bed->relplt_name;
13621
0
  if (relplt_name == NULL)
13622
0
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13623
0
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13624
0
  if (relplt == NULL)
13625
0
    return 0;
13626
13627
0
  hdr = &elf_section_data (relplt)->this_hdr;
13628
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13629
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13630
0
    return 0;
13631
13632
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13633
0
  if (plt == NULL)
13634
0
    return 0;
13635
13636
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13637
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13638
0
    return -1;
13639
13640
0
  count = NUM_SHDR_ENTRIES (hdr);
13641
0
  size = count * sizeof (asymbol);
13642
0
  p = relplt->relocation;
13643
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13644
0
    {
13645
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13646
0
      if (p->addend != 0)
13647
0
  {
13648
0
#ifdef BFD64
13649
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13650
#else
13651
    size += sizeof ("+0x") - 1 + 8;
13652
#endif
13653
0
  }
13654
0
    }
13655
13656
0
  s = *ret = (asymbol *) bfd_malloc (size);
13657
0
  if (s == NULL)
13658
0
    return -1;
13659
13660
0
  names = (char *) (s + count);
13661
0
  p = relplt->relocation;
13662
0
  n = 0;
13663
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13664
0
    {
13665
0
      size_t len;
13666
0
      bfd_vma addr;
13667
13668
0
      addr = bed->plt_sym_val (i, plt, p);
13669
0
      if (addr == (bfd_vma) -1)
13670
0
  continue;
13671
13672
0
      *s = **p->sym_ptr_ptr;
13673
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13674
   we are defining a symbol, ensure one of them is set.  */
13675
0
      if ((s->flags & BSF_LOCAL) == 0)
13676
0
  s->flags |= BSF_GLOBAL;
13677
0
      s->flags |= BSF_SYNTHETIC;
13678
0
      s->section = plt;
13679
0
      s->value = addr - plt->vma;
13680
0
      s->name = names;
13681
0
      s->udata.p = NULL;
13682
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13683
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13684
0
      names += len;
13685
0
      if (p->addend != 0)
13686
0
  {
13687
0
    char buf[30], *a;
13688
13689
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13690
0
    names += sizeof ("+0x") - 1;
13691
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13692
0
    for (a = buf; *a == '0'; ++a)
13693
0
      ;
13694
0
    len = strlen (a);
13695
0
    memcpy (names, a, len);
13696
0
    names += len;
13697
0
  }
13698
0
      memcpy (names, "@plt", sizeof ("@plt"));
13699
0
      names += sizeof ("@plt");
13700
0
      ++s, ++n;
13701
0
    }
13702
13703
0
  return n;
13704
0
}
13705
13706
/* It is only used by x86-64 so far.
13707
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
13708
   but current usage would allow all of _bfd_std_section to be zero.  */
13709
static const asymbol lcomm_sym
13710
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13711
asection _bfd_elf_large_com_section
13712
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13713
          "LARGE_COMMON", 0, SEC_IS_COMMON);
13714
13715
bool
13716
_bfd_elf_final_write_processing (bfd *abfd)
13717
0
{
13718
0
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13719
13720
0
  i_ehdrp = elf_elfheader (abfd);
13721
13722
0
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13723
0
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13724
13725
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13726
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13727
     or STB_GNU_UNIQUE binding.  */
13728
0
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13729
0
    {
13730
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13731
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13732
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13733
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13734
0
  {
13735
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13736
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13737
0
          "and FreeBSD targets"));
13738
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13739
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13740
0
          "only by GNU and FreeBSD targets"));
13741
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13742
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13743
0
          "only by GNU and FreeBSD targets"));
13744
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13745
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13746
0
          "only by GNU and FreeBSD targets"));
13747
0
    bfd_set_error (bfd_error_sorry);
13748
0
    return false;
13749
0
  }
13750
0
    }
13751
0
  return true;
13752
0
}
13753
13754
13755
/* Return TRUE for ELF symbol types that represent functions.
13756
   This is the default version of this function, which is sufficient for
13757
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13758
13759
bool
13760
_bfd_elf_is_function_type (unsigned int type)
13761
0
{
13762
0
  return (type == STT_FUNC
13763
0
    || type == STT_GNU_IFUNC);
13764
0
}
13765
13766
/* If the ELF symbol SYM might be a function in SEC, return the
13767
   function size and set *CODE_OFF to the function's entry point,
13768
   otherwise return zero.  */
13769
13770
bfd_size_type
13771
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13772
           bfd_vma *code_off)
13773
0
{
13774
0
  bfd_size_type size;
13775
0
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13776
13777
0
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13778
0
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13779
0
      || sym->section != sec)
13780
0
    return 0;
13781
13782
0
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13783
13784
  /* In theory we should check that the symbol's type satisfies
13785
     _bfd_elf_is_function_type(), but there are some function-like
13786
     symbols which would fail this test.  (eg _start).  Instead
13787
     we check for hidden, local, notype symbols with zero size.
13788
     This type of symbol is generated by the annobin plugin for gcc
13789
     and clang, and should not be considered to be a function symbol.  */
13790
0
  if (size == 0
13791
0
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13792
0
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13793
0
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13794
0
    return 0;
13795
13796
0
  *code_off = sym->value;
13797
  /* Do not return 0 for the function's size.  */
13798
0
  return size ? size : 1;
13799
0
}
13800
13801
/* Set to non-zero to enable some debug messages.  */
13802
#define DEBUG_SECONDARY_RELOCS   0
13803
13804
/* An internal-to-the-bfd-library only section type
13805
   used to indicate a cached secondary reloc section.  */
13806
0
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13807
13808
/* Create a BFD section to hold a secondary reloc section.  */
13809
13810
bool
13811
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13812
               Elf_Internal_Shdr *hdr,
13813
               const char * name,
13814
               unsigned int shindex)
13815
0
{
13816
  /* We only support RELA secondary relocs.  */
13817
0
  if (hdr->sh_type != SHT_RELA)
13818
0
    return false;
13819
13820
#if DEBUG_SECONDARY_RELOCS
13821
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13822
#endif
13823
0
  hdr->sh_type = SHT_SECONDARY_RELOC;
13824
0
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13825
0
}
13826
13827
/* Read in any secondary relocs associated with SEC.  */
13828
13829
bool
13830
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13831
          asection *  sec,
13832
          asymbol **  symbols,
13833
          bool dynamic)
13834
0
{
13835
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13836
0
  asection * relsec;
13837
0
  bool result = true;
13838
0
  bfd_vma (*r_sym) (bfd_vma);
13839
0
  ufile_ptr filesize;
13840
13841
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13842
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13843
0
    r_sym = elf64_r_sym;
13844
0
  else
13845
0
#endif
13846
0
    r_sym = elf32_r_sym;
13847
13848
0
  if (!elf_section_data (sec)->has_secondary_relocs)
13849
0
    return true;
13850
13851
  /* Discover if there are any secondary reloc sections
13852
     associated with SEC.  */
13853
0
  filesize = bfd_get_file_size (abfd);
13854
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13855
0
    {
13856
0
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13857
13858
0
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13859
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13860
0
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13861
0
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13862
0
  {
13863
0
    bfd_byte * native_relocs;
13864
0
    bfd_byte * native_reloc;
13865
0
    arelent * internal_relocs;
13866
0
    arelent * internal_reloc;
13867
0
    size_t i;
13868
0
    unsigned int entsize;
13869
0
    unsigned int symcount;
13870
0
    bfd_size_type reloc_count;
13871
0
    size_t amt;
13872
13873
0
    if (ebd->elf_info_to_howto == NULL)
13874
0
      return false;
13875
13876
#if DEBUG_SECONDARY_RELOCS
13877
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13878
       sec->name, relsec->name);
13879
#endif
13880
0
    entsize = hdr->sh_entsize;
13881
13882
0
    if (filesize != 0
13883
0
        && ((ufile_ptr) hdr->sh_offset > filesize
13884
0
      || hdr->sh_size > filesize - hdr->sh_offset))
13885
0
      {
13886
0
        bfd_set_error (bfd_error_file_truncated);
13887
0
        result = false;
13888
0
        continue;
13889
0
      }
13890
13891
0
    native_relocs = bfd_malloc (hdr->sh_size);
13892
0
    if (native_relocs == NULL)
13893
0
      {
13894
0
        result = false;
13895
0
        continue;
13896
0
      }
13897
13898
0
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13899
0
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13900
0
      {
13901
0
        free (native_relocs);
13902
0
        bfd_set_error (bfd_error_file_too_big);
13903
0
        result = false;
13904
0
        continue;
13905
0
      }
13906
13907
0
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13908
0
    if (internal_relocs == NULL)
13909
0
      {
13910
0
        free (native_relocs);
13911
0
        result = false;
13912
0
        continue;
13913
0
      }
13914
13915
0
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13916
0
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13917
0
      {
13918
0
        free (native_relocs);
13919
        /* The internal_relocs will be freed when
13920
     the memory for the bfd is released.  */
13921
0
        result = false;
13922
0
        continue;
13923
0
      }
13924
13925
0
    if (dynamic)
13926
0
      symcount = bfd_get_dynamic_symcount (abfd);
13927
0
    else
13928
0
      symcount = bfd_get_symcount (abfd);
13929
13930
0
    for (i = 0, internal_reloc = internal_relocs,
13931
0
     native_reloc = native_relocs;
13932
0
         i < reloc_count;
13933
0
         i++, internal_reloc++, native_reloc += entsize)
13934
0
      {
13935
0
        bool res;
13936
0
        Elf_Internal_Rela rela;
13937
13938
0
        if (entsize == ebd->s->sizeof_rel)
13939
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13940
0
        else /* entsize == ebd->s->sizeof_rela */
13941
0
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13942
13943
        /* The address of an ELF reloc is section relative for an object
13944
     file, and absolute for an executable file or shared library.
13945
     The address of a normal BFD reloc is always section relative,
13946
     and the address of a dynamic reloc is absolute..  */
13947
0
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13948
0
    internal_reloc->address = rela.r_offset;
13949
0
        else
13950
0
    internal_reloc->address = rela.r_offset - sec->vma;
13951
13952
0
        if (r_sym (rela.r_info) == STN_UNDEF)
13953
0
    {
13954
      /* FIXME: This and the error case below mean that we
13955
         have a symbol on relocs that is not elf_symbol_type.  */
13956
0
      internal_reloc->sym_ptr_ptr =
13957
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
13958
0
    }
13959
0
        else if (r_sym (rela.r_info) > symcount)
13960
0
    {
13961
0
      _bfd_error_handler
13962
        /* xgettext:c-format */
13963
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13964
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13965
0
      bfd_set_error (bfd_error_bad_value);
13966
0
      internal_reloc->sym_ptr_ptr =
13967
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
13968
0
      result = false;
13969
0
    }
13970
0
        else
13971
0
    {
13972
0
      asymbol **ps;
13973
13974
0
      ps = symbols + r_sym (rela.r_info) - 1;
13975
0
      internal_reloc->sym_ptr_ptr = ps;
13976
      /* Make sure that this symbol is not removed by strip.  */
13977
0
      (*ps)->flags |= BSF_KEEP;
13978
0
    }
13979
13980
0
        internal_reloc->addend = rela.r_addend;
13981
13982
0
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13983
0
        if (! res || internal_reloc->howto == NULL)
13984
0
    {
13985
#if DEBUG_SECONDARY_RELOCS
13986
      fprintf (stderr,
13987
         "there is no howto associated with reloc %lx\n",
13988
         rela.r_info);
13989
#endif
13990
0
      result = false;
13991
0
    }
13992
0
      }
13993
13994
0
    free (native_relocs);
13995
    /* Store the internal relocs.  */
13996
0
    elf_section_data (relsec)->sec_info = internal_relocs;
13997
0
  }
13998
0
    }
13999
14000
0
  return result;
14001
0
}
14002
14003
/* Set the ELF section header fields of an output secondary reloc section.  */
14004
14005
bool
14006
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14007
              bfd *obfd ATTRIBUTE_UNUSED,
14008
              const Elf_Internal_Shdr *isection,
14009
              Elf_Internal_Shdr *osection)
14010
0
{
14011
0
  asection * isec;
14012
0
  asection * osec;
14013
0
  struct bfd_elf_section_data * esd;
14014
14015
0
  if (isection == NULL)
14016
0
    return false;
14017
14018
0
  if (isection->sh_type != SHT_SECONDARY_RELOC)
14019
0
    return true;
14020
14021
0
  isec = isection->bfd_section;
14022
0
  if (isec == NULL)
14023
0
    return false;
14024
14025
0
  osec = osection->bfd_section;
14026
0
  if (osec == NULL)
14027
0
    return false;
14028
14029
0
  esd = elf_section_data (osec);
14030
0
  BFD_ASSERT (esd->sec_info == NULL);
14031
0
  esd->sec_info = elf_section_data (isec)->sec_info;
14032
0
  osection->sh_type = SHT_RELA;
14033
0
  osection->sh_link = elf_onesymtab (obfd);
14034
0
  if (osection->sh_link == 0)
14035
0
    {
14036
      /* There is no symbol table - we are hosed...  */
14037
0
      _bfd_error_handler
14038
  /* xgettext:c-format */
14039
0
  (_("%pB(%pA): link section cannot be set"
14040
0
     " because the output file does not have a symbol table"),
14041
0
  obfd, osec);
14042
0
      bfd_set_error (bfd_error_bad_value);
14043
0
      return false;
14044
0
    }
14045
14046
  /* Find the output section that corresponds to the isection's
14047
     sh_info link.  */
14048
0
  if (isection->sh_info == 0
14049
0
      || isection->sh_info >= elf_numsections (ibfd))
14050
0
    {
14051
0
      _bfd_error_handler
14052
  /* xgettext:c-format */
14053
0
  (_("%pB(%pA): info section index is invalid"),
14054
0
  obfd, osec);
14055
0
      bfd_set_error (bfd_error_bad_value);
14056
0
      return false;
14057
0
    }
14058
14059
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
14060
14061
0
  if (isection == NULL
14062
0
      || isection->bfd_section == NULL
14063
0
      || isection->bfd_section->output_section == NULL)
14064
0
    {
14065
0
      _bfd_error_handler
14066
  /* xgettext:c-format */
14067
0
  (_("%pB(%pA): info section index cannot be set"
14068
0
     " because the section is not in the output"),
14069
0
  obfd, osec);
14070
0
      bfd_set_error (bfd_error_bad_value);
14071
0
      return false;
14072
0
    }
14073
14074
0
  esd = elf_section_data (isection->bfd_section->output_section);
14075
0
  BFD_ASSERT (esd != NULL);
14076
0
  osection->sh_info = esd->this_idx;
14077
0
  esd->has_secondary_relocs = true;
14078
#if DEBUG_SECONDARY_RELOCS
14079
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14080
     osec->name, osection->sh_link, osection->sh_info);
14081
  fprintf (stderr, "mark section %s as having secondary relocs\n",
14082
     bfd_section_name (isection->bfd_section->output_section));
14083
#endif
14084
14085
0
  return true;
14086
0
}
14087
14088
/* Write out a secondary reloc section.
14089
14090
   FIXME: Currently this function can result in a serious performance penalty
14091
   for files with secondary relocs and lots of sections.  The proper way to
14092
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14093
   relocs together and then to have this function just walk that chain.  */
14094
14095
bool
14096
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14097
0
{
14098
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14099
0
  bfd_vma addr_offset;
14100
0
  asection * relsec;
14101
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
14102
0
  bool result = true;
14103
14104
0
  if (sec == NULL)
14105
0
    return false;
14106
14107
0
#if BFD_DEFAULT_TARGET_SIZE > 32
14108
0
  if (bfd_arch_bits_per_address (abfd) != 32)
14109
0
    r_info = elf64_r_info;
14110
0
  else
14111
0
#endif
14112
0
    r_info = elf32_r_info;
14113
14114
  /* The address of an ELF reloc is section relative for an object
14115
     file, and absolute for an executable file or shared library.
14116
     The address of a BFD reloc is always section relative.  */
14117
0
  addr_offset = 0;
14118
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14119
0
    addr_offset = sec->vma;
14120
14121
  /* Discover if there are any secondary reloc sections
14122
     associated with SEC.  */
14123
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14124
0
    {
14125
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14126
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14127
14128
0
      if (hdr->sh_type == SHT_RELA
14129
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14130
0
  {
14131
0
    asymbol *    last_sym;
14132
0
    int          last_sym_idx;
14133
0
    size_t       reloc_count;
14134
0
    size_t       idx;
14135
0
    bfd_size_type entsize;
14136
0
    arelent *    src_irel;
14137
0
    bfd_byte *   dst_rela;
14138
14139
0
    if (hdr->contents != NULL)
14140
0
      {
14141
0
        _bfd_error_handler
14142
    /* xgettext:c-format */
14143
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
14144
0
     abfd, relsec);
14145
0
        bfd_set_error (bfd_error_bad_value);
14146
0
        result = false;
14147
0
        continue;
14148
0
      }
14149
14150
0
    entsize = hdr->sh_entsize;
14151
0
    if (entsize == 0)
14152
0
      {
14153
0
        _bfd_error_handler
14154
    /* xgettext:c-format */
14155
0
    (_("%pB(%pA): error: secondary reloc section"
14156
0
       " has zero sized entries"),
14157
0
     abfd, relsec);
14158
0
        bfd_set_error (bfd_error_bad_value);
14159
0
        result = false;
14160
0
        continue;
14161
0
      }
14162
0
    else if (entsize != ebd->s->sizeof_rel
14163
0
       && entsize != ebd->s->sizeof_rela)
14164
0
      {
14165
0
        _bfd_error_handler
14166
    /* xgettext:c-format */
14167
0
    (_("%pB(%pA): error: secondary reloc section"
14168
0
       " has non-standard sized entries"),
14169
0
     abfd, relsec);
14170
0
        bfd_set_error (bfd_error_bad_value);
14171
0
        result = false;
14172
0
        continue;
14173
0
      }
14174
14175
0
    reloc_count = hdr->sh_size / entsize;
14176
0
    hdr->sh_size = entsize * reloc_count;
14177
0
    if (reloc_count == 0)
14178
0
      {
14179
0
        _bfd_error_handler
14180
    /* xgettext:c-format */
14181
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
14182
0
     abfd, relsec);
14183
0
        bfd_set_error (bfd_error_bad_value);
14184
0
        result = false;
14185
0
        continue;
14186
0
      }
14187
14188
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14189
0
    if (hdr->contents == NULL)
14190
0
      continue;
14191
14192
#if DEBUG_SECONDARY_RELOCS
14193
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14194
       reloc_count, sec->name, relsec->name);
14195
#endif
14196
0
    last_sym = NULL;
14197
0
    last_sym_idx = 0;
14198
0
    dst_rela = hdr->contents;
14199
0
    src_irel = (arelent *) esd->sec_info;
14200
0
    if (src_irel == NULL)
14201
0
      {
14202
0
        _bfd_error_handler
14203
    /* xgettext:c-format */
14204
0
    (_("%pB(%pA): error: internal relocs missing"
14205
0
       " for secondary reloc section"),
14206
0
     abfd, relsec);
14207
0
        bfd_set_error (bfd_error_bad_value);
14208
0
        result = false;
14209
0
        continue;
14210
0
      }
14211
14212
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14213
0
      {
14214
0
        Elf_Internal_Rela src_rela;
14215
0
        arelent *ptr;
14216
0
        asymbol *sym;
14217
0
        int n;
14218
14219
0
        ptr = src_irel + idx;
14220
0
        if (ptr == NULL)
14221
0
    {
14222
0
      _bfd_error_handler
14223
        /* xgettext:c-format */
14224
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14225
0
         abfd, relsec, idx);
14226
0
      bfd_set_error (bfd_error_bad_value);
14227
0
      result = false;
14228
0
      break;
14229
0
    }
14230
14231
0
        if (ptr->sym_ptr_ptr == NULL)
14232
0
    {
14233
      /* FIXME: Is this an error ? */
14234
0
      n = 0;
14235
0
    }
14236
0
        else
14237
0
    {
14238
0
      sym = *ptr->sym_ptr_ptr;
14239
14240
0
      if (sym == last_sym)
14241
0
        n = last_sym_idx;
14242
0
      else
14243
0
        {
14244
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14245
0
          if (n < 0)
14246
0
      {
14247
0
        _bfd_error_handler
14248
          /* xgettext:c-format */
14249
0
          (_("%pB(%pA): error: secondary reloc %zu"
14250
0
             " references a missing symbol"),
14251
0
           abfd, relsec, idx);
14252
0
        bfd_set_error (bfd_error_bad_value);
14253
0
        result = false;
14254
0
        n = 0;
14255
0
      }
14256
14257
0
          last_sym = sym;
14258
0
          last_sym_idx = n;
14259
0
        }
14260
14261
0
      if (sym->the_bfd != NULL
14262
0
          && sym->the_bfd->xvec != abfd->xvec
14263
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14264
0
        {
14265
0
          _bfd_error_handler
14266
      /* xgettext:c-format */
14267
0
      (_("%pB(%pA): error: secondary reloc %zu"
14268
0
         " references a deleted symbol"),
14269
0
       abfd, relsec, idx);
14270
0
          bfd_set_error (bfd_error_bad_value);
14271
0
          result = false;
14272
0
          n = 0;
14273
0
        }
14274
0
    }
14275
14276
0
        src_rela.r_offset = ptr->address + addr_offset;
14277
0
        if (ptr->howto == NULL)
14278
0
    {
14279
0
      _bfd_error_handler
14280
        /* xgettext:c-format */
14281
0
        (_("%pB(%pA): error: secondary reloc %zu"
14282
0
           " is of an unknown type"),
14283
0
         abfd, relsec, idx);
14284
0
      bfd_set_error (bfd_error_bad_value);
14285
0
      result = false;
14286
0
      src_rela.r_info = r_info (0, 0);
14287
0
    }
14288
0
        else
14289
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14290
0
        src_rela.r_addend = ptr->addend;
14291
14292
0
        if (entsize == ebd->s->sizeof_rel)
14293
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14294
0
        else /* entsize == ebd->s->sizeof_rela */
14295
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14296
0
      }
14297
0
  }
14298
0
    }
14299
14300
0
  return result;
14301
0
}