Coverage Report

Created: 2023-06-29 07:13

/src/binutils-gdb/bfd/elf.c
Line
Count
Source (jump to first uncovered line)
1
/* ELF executable support for BFD.
2
3
   Copyright (C) 1993-2023 Free Software Foundation, Inc.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
23
/*
24
SECTION
25
  ELF backends
26
27
  BFD support for ELF formats is being worked on.
28
  Currently, the best supported back ends are for sparc and i386
29
  (running svr4 or Solaris 2).
30
31
  Documentation of the internals of the support code still needs
32
  to be written.  The code is changing quickly enough that we
33
  haven't bothered yet.  */
34
35
/* For sparc64-cross-sparc32.  */
36
#define _SYSCALL32
37
#include "sysdep.h"
38
#include <limits.h>
39
#include "bfd.h"
40
#include "bfdlink.h"
41
#include "libbfd.h"
42
#define ARCH_SIZE 0
43
#include "elf-bfd.h"
44
#include "libiberty.h"
45
#include "safe-ctype.h"
46
#include "elf-linux-core.h"
47
48
#ifdef CORE_HEADER
49
#include CORE_HEADER
50
#endif
51
52
static int elf_sort_sections (const void *, const void *);
53
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55
         struct bfd_link_info *);
56
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57
           file_ptr offset, size_t align);
58
59
/* Swap version information in and out.  The version information is
60
   currently size independent.  If that ever changes, this code will
61
   need to move into elfcode.h.  */
62
63
/* Swap in a Verdef structure.  */
64
65
void
66
_bfd_elf_swap_verdef_in (bfd *abfd,
67
       const Elf_External_Verdef *src,
68
       Elf_Internal_Verdef *dst)
69
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
5.00M
{
231
5.00M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232
5.00M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
233
5.00M
  if (abfd->tdata.any == NULL)
234
0
    return false;
235
236
5.00M
  elf_object_id (abfd) = object_id;
237
5.00M
  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
5.00M
  return true;
246
5.00M
}
247
248
249
bool
250
bfd_elf_make_object (bfd *abfd)
251
2.34M
{
252
2.34M
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253
2.34M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254
2.34M
          bed->target_id);
255
2.34M
}
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
110k
{
270
110k
  Elf_Internal_Shdr **i_shdrp;
271
110k
  bfd_byte *shstrtab = NULL;
272
110k
  file_ptr offset;
273
110k
  bfd_size_type shstrtabsize;
274
275
110k
  i_shdrp = elf_elfsections (abfd);
276
110k
  if (i_shdrp == 0
277
110k
      || shindex >= elf_numsections (abfd)
278
110k
      || i_shdrp[shindex] == 0)
279
0
    return NULL;
280
281
110k
  shstrtab = i_shdrp[shindex]->contents;
282
110k
  if (shstrtab == NULL)
283
110k
    {
284
      /* No cached one, attempt to read, and cache what we read.  */
285
110k
      offset = i_shdrp[shindex]->sh_offset;
286
110k
      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
110k
      if (shstrtabsize + 1 <= 1
291
110k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
292
110k
    || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293
85.5k
                shstrtabsize)) == NULL)
294
27.8k
  {
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
27.8k
    i_shdrp[shindex]->sh_size = 0;
299
27.8k
  }
300
83.0k
      else
301
83.0k
  shstrtab[shstrtabsize] = '\0';
302
110k
      i_shdrp[shindex]->contents = shstrtab;
303
110k
    }
304
110k
  return (char *) shstrtab;
305
110k
}
306
307
char *
308
bfd_elf_string_from_elf_section (bfd *abfd,
309
         unsigned int shindex,
310
         unsigned int strindex)
311
790k
{
312
790k
  Elf_Internal_Shdr *hdr;
313
314
790k
  if (strindex == 0)
315
575k
    return "";
316
317
215k
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318
0
    return NULL;
319
320
215k
  hdr = elf_elfsections (abfd)[shindex];
321
322
215k
  if (hdr->contents == NULL)
323
112k
    {
324
112k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325
1.84k
  {
326
    /* PR 17512: file: f057ec89.  */
327
    /* xgettext:c-format */
328
1.84k
    _bfd_error_handler (_("%pB: attempt to load strings from"
329
1.84k
        " a non-string section (number %d)"),
330
1.84k
            abfd, shindex);
331
1.84k
    return NULL;
332
1.84k
  }
333
334
110k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335
27.8k
  return NULL;
336
110k
    }
337
102k
  else
338
102k
    {
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
102k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345
1.94k
  return NULL;
346
102k
    }
347
348
183k
  if (strindex >= hdr->sh_size)
349
3.06k
    {
350
3.06k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351
3.06k
      _bfd_error_handler
352
  /* xgettext:c-format */
353
3.06k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354
3.06k
   abfd, strindex, (uint64_t) hdr->sh_size,
355
3.06k
   (shindex == shstrndx && strindex == hdr->sh_name
356
3.06k
    ? ".shstrtab"
357
3.06k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
3.06k
      return NULL;
359
3.06k
    }
360
361
180k
  return ((char *) hdr->contents) + strindex;
362
183k
}
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
5.28k
{
381
5.28k
  Elf_Internal_Shdr *shndx_hdr;
382
5.28k
  void *alloc_ext;
383
5.28k
  const bfd_byte *esym;
384
5.28k
  Elf_External_Sym_Shndx *alloc_extshndx;
385
5.28k
  Elf_External_Sym_Shndx *shndx;
386
5.28k
  Elf_Internal_Sym *alloc_intsym;
387
5.28k
  Elf_Internal_Sym *isym;
388
5.28k
  Elf_Internal_Sym *isymend;
389
5.28k
  const struct elf_backend_data *bed;
390
5.28k
  size_t extsym_size;
391
5.28k
  size_t amt;
392
5.28k
  file_ptr pos;
393
394
5.28k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
0
    abort ();
396
397
5.28k
  if (symcount == 0)
398
0
    return intsym_buf;
399
400
  /* Normal syms might have section extension entries.  */
401
5.28k
  shndx_hdr = NULL;
402
5.28k
  if (elf_symtab_shndx_list (ibfd) != NULL)
403
28
    {
404
28
      elf_section_list * entry;
405
28
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
406
407
      /* Find an index section that is linked to this symtab section.  */
408
56
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
409
28
  {
410
    /* PR 20063.  */
411
28
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
412
0
      continue;
413
414
28
    if (sections[entry->hdr.sh_link] == symtab_hdr)
415
0
      {
416
0
        shndx_hdr = & entry->hdr;
417
0
        break;
418
28
      };
419
28
  }
420
421
28
      if (shndx_hdr == NULL)
422
28
  {
423
28
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
424
      /* Not really accurate, but this was how the old code used
425
         to work.  */
426
28
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
427
    /* Otherwise we do nothing.  The assumption is that
428
       the index table will not be needed.  */
429
28
  }
430
28
    }
431
432
  /* Read the symbols.  */
433
5.28k
  alloc_ext = NULL;
434
5.28k
  alloc_extshndx = NULL;
435
5.28k
  alloc_intsym = NULL;
436
5.28k
  bed = get_elf_backend_data (ibfd);
437
5.28k
  extsym_size = bed->s->sizeof_sym;
438
5.28k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
439
0
    {
440
0
      bfd_set_error (bfd_error_file_too_big);
441
0
      intsym_buf = NULL;
442
0
      goto out;
443
0
    }
444
5.28k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445
5.28k
  if (extsym_buf == NULL)
446
0
    {
447
0
      alloc_ext = bfd_malloc (amt);
448
0
      extsym_buf = alloc_ext;
449
0
    }
450
5.28k
  if (extsym_buf == NULL
451
5.28k
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
452
5.28k
      || bfd_bread (extsym_buf, amt, ibfd) != amt)
453
1.85k
    {
454
1.85k
      intsym_buf = NULL;
455
1.85k
      goto out;
456
1.85k
    }
457
458
3.43k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
459
3.42k
    extshndx_buf = NULL;
460
15
  else
461
15
    {
462
15
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
463
0
  {
464
0
    bfd_set_error (bfd_error_file_too_big);
465
0
    intsym_buf = NULL;
466
0
    goto out;
467
0
  }
468
15
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
469
15
      if (extshndx_buf == NULL)
470
0
  {
471
0
    alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
472
0
    extshndx_buf = alloc_extshndx;
473
0
  }
474
15
      if (extshndx_buf == NULL
475
15
    || bfd_seek (ibfd, pos, SEEK_SET) != 0
476
15
    || bfd_bread (extshndx_buf, amt, ibfd) != amt)
477
9
  {
478
9
    intsym_buf = NULL;
479
9
    goto out;
480
9
  }
481
15
    }
482
483
3.42k
  if (intsym_buf == NULL)
484
0
    {
485
0
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
486
0
  {
487
0
    bfd_set_error (bfd_error_file_too_big);
488
0
    goto out;
489
0
  }
490
0
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
491
0
      intsym_buf = alloc_intsym;
492
0
      if (intsym_buf == NULL)
493
0
  goto out;
494
0
    }
495
496
  /* Convert the symbols to internal form.  */
497
3.42k
  isymend = intsym_buf + symcount;
498
3.42k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
499
3.42k
     shndx = extshndx_buf;
500
6.01k
       isym < isymend;
501
3.42k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
502
3.42k
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
503
836
      {
504
836
  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
505
  /* xgettext:c-format */
506
836
  _bfd_error_handler (_("%pB symbol number %lu references"
507
836
            " nonexistent SHT_SYMTAB_SHNDX section"),
508
836
          ibfd, (unsigned long) symoffset);
509
836
  free (alloc_intsym);
510
836
  intsym_buf = NULL;
511
836
  goto out;
512
836
      }
513
514
5.28k
 out:
515
5.28k
  free (alloc_ext);
516
5.28k
  free (alloc_extshndx);
517
518
5.28k
  return intsym_buf;
519
3.42k
}
520
521
/* Look up a symbol name.  */
522
const char *
523
bfd_elf_sym_name (bfd *abfd,
524
      Elf_Internal_Shdr *symtab_hdr,
525
      Elf_Internal_Sym *isym,
526
      asection *sym_sec)
527
2.59k
{
528
2.59k
  const char *name;
529
2.59k
  unsigned int iname = isym->st_name;
530
2.59k
  unsigned int shindex = symtab_hdr->sh_link;
531
532
2.59k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
533
      /* Check for a bogus st_shndx to avoid crashing.  */
534
2.59k
      && isym->st_shndx < elf_numsections (abfd))
535
8
    {
536
8
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
537
8
      shindex = elf_elfheader (abfd)->e_shstrndx;
538
8
    }
539
540
2.59k
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
541
2.59k
  if (name == NULL)
542
1.84k
    name = "(null)";
543
750
  else if (sym_sec && *name == '\0')
544
0
    name = bfd_section_name (sym_sec);
545
546
2.59k
  return name;
547
2.59k
}
548
549
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
550
   sections.  The first element is the flags, the rest are section
551
   pointers.  */
552
553
typedef union elf_internal_group {
554
  Elf_Internal_Shdr *shdr;
555
  unsigned int flags;
556
} Elf_Internal_Group;
557
558
/* Return the name of the group signature symbol.  Why isn't the
559
   signature just a string?  */
560
561
static const char *
562
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
563
7.40k
{
564
7.40k
  Elf_Internal_Shdr *hdr;
565
7.40k
  unsigned char esym[sizeof (Elf64_External_Sym)];
566
7.40k
  Elf_External_Sym_Shndx eshndx;
567
7.40k
  Elf_Internal_Sym isym;
568
569
  /* First we need to ensure the symbol table is available.  Make sure
570
     that it is a symbol table section.  */
571
7.40k
  if (ghdr->sh_link >= elf_numsections (abfd))
572
0
    return NULL;
573
7.40k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
574
7.40k
  if (hdr->sh_type != SHT_SYMTAB
575
7.40k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
576
2.11k
    return NULL;
577
578
  /* Go read the symbol.  */
579
5.28k
  hdr = &elf_tdata (abfd)->symtab_hdr;
580
5.28k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
581
5.28k
          &isym, esym, &eshndx) == NULL)
582
2.69k
    return NULL;
583
584
2.59k
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
585
5.28k
}
586
587
/* Set next_in_group list pointer, and group name for NEWSECT.  */
588
589
static bool
590
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
591
64.9k
{
592
64.9k
  unsigned int num_group = elf_tdata (abfd)->num_group;
593
594
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
595
     is set to -1 if there are no SHT_GROUP sections.  */
596
64.9k
  if (num_group == 0)
597
46.6k
    {
598
46.6k
      unsigned int i, shnum;
599
600
      /* First count the number of groups.  If we have a SHT_GROUP
601
   section with just a flag word (ie. sh_size is 4), ignore it.  */
602
46.6k
      shnum = elf_numsections (abfd);
603
46.6k
      num_group = 0;
604
605
46.6k
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)  \
606
331k
  (   (shdr)->sh_type == SHT_GROUP    \
607
331k
   && (shdr)->sh_size >= minsize      \
608
331k
   && (shdr)->sh_entsize == GRP_ENTRY_SIZE  \
609
331k
   && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
610
611
289k
      for (i = 0; i < shnum; i++)
612
243k
  {
613
243k
    Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
614
615
243k
    if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
616
11.8k
      num_group += 1;
617
243k
  }
618
619
46.6k
      if (num_group == 0)
620
35.7k
  {
621
35.7k
    num_group = (unsigned) -1;
622
35.7k
    elf_tdata (abfd)->num_group = num_group;
623
35.7k
    elf_tdata (abfd)->group_sect_ptr = NULL;
624
35.7k
  }
625
10.8k
      else
626
10.8k
  {
627
    /* We keep a list of elf section headers for group sections,
628
       so we can find them quickly.  */
629
10.8k
    size_t amt;
630
631
10.8k
    elf_tdata (abfd)->num_group = num_group;
632
10.8k
    amt = num_group * sizeof (Elf_Internal_Shdr *);
633
10.8k
    elf_tdata (abfd)->group_sect_ptr
634
10.8k
      = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
635
10.8k
    if (elf_tdata (abfd)->group_sect_ptr == NULL)
636
0
      return false;
637
10.8k
    num_group = 0;
638
639
75.2k
    for (i = 0; i < shnum; i++)
640
65.1k
      {
641
65.1k
        Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
642
643
65.1k
        if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
644
11.8k
    {
645
11.8k
      unsigned char *src;
646
11.8k
      Elf_Internal_Group *dest;
647
648
      /* Make sure the group section has a BFD section
649
         attached to it.  */
650
11.8k
      if (!bfd_section_from_shdr (abfd, i))
651
835
        return false;
652
653
      /* Add to list of sections.  */
654
11.0k
      elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
655
11.0k
      num_group += 1;
656
657
      /* Read the raw contents.  */
658
11.0k
      BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
659
11.0k
      shdr->contents = NULL;
660
11.0k
      if (_bfd_mul_overflow (shdr->sh_size,
661
11.0k
           sizeof (*dest) / 4, &amt)
662
11.0k
          || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
663
11.0k
          || !(shdr->contents
664
9.76k
         = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
665
4.89k
        {
666
4.89k
          _bfd_error_handler
667
      /* xgettext:c-format */
668
4.89k
      (_("%pB: invalid size field in group section"
669
4.89k
         " header: %#" PRIx64 ""),
670
4.89k
       abfd, (uint64_t) shdr->sh_size);
671
4.89k
          bfd_set_error (bfd_error_bad_value);
672
4.89k
          -- num_group;
673
4.89k
          continue;
674
4.89k
        }
675
676
      /* Translate raw contents, a flag word followed by an
677
         array of elf section indices all in target byte order,
678
         to the flag word followed by an array of elf section
679
         pointers.  */
680
6.13k
      src = shdr->contents + shdr->sh_size;
681
6.13k
      dest = (Elf_Internal_Group *) (shdr->contents + amt);
682
683
1.57M
      while (1)
684
1.57M
        {
685
1.57M
          unsigned int idx;
686
687
1.57M
          src -= 4;
688
1.57M
          --dest;
689
1.57M
          idx = H_GET_32 (abfd, src);
690
1.57M
          if (src == shdr->contents)
691
6.13k
      {
692
6.13k
        dest->shdr = NULL;
693
6.13k
        dest->flags = idx;
694
6.13k
        if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
695
1.04k
          shdr->bfd_section->flags
696
1.04k
            |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
697
6.13k
        break;
698
6.13k
      }
699
1.56M
          if (idx < shnum)
700
373k
      {
701
373k
        dest->shdr = elf_elfsections (abfd)[idx];
702
        /* PR binutils/23199: All sections in a
703
           section group should be marked with
704
           SHF_GROUP.  But some tools generate
705
           broken objects without SHF_GROUP.  Fix
706
           them up here.  */
707
373k
        dest->shdr->sh_flags |= SHF_GROUP;
708
373k
      }
709
1.56M
          if (idx >= shnum
710
1.56M
        || dest->shdr->sh_type == SHT_GROUP)
711
1.19M
      {
712
1.19M
        _bfd_error_handler
713
1.19M
          (_("%pB: invalid entry in SHT_GROUP section [%u]"),
714
1.19M
             abfd, i);
715
1.19M
        dest->shdr = NULL;
716
1.19M
      }
717
1.56M
        }
718
6.13k
    }
719
65.1k
      }
720
721
    /* PR 17510: Corrupt binaries might contain invalid groups.  */
722
10.0k
    if (num_group != (unsigned) elf_tdata (abfd)->num_group)
723
4.56k
      {
724
4.56k
        elf_tdata (abfd)->num_group = num_group;
725
726
        /* If all groups are invalid then fail.  */
727
4.56k
        if (num_group == 0)
728
3.92k
    {
729
3.92k
      elf_tdata (abfd)->group_sect_ptr = NULL;
730
3.92k
      elf_tdata (abfd)->num_group = num_group = -1;
731
3.92k
      _bfd_error_handler
732
3.92k
        (_("%pB: no valid group sections found"), abfd);
733
3.92k
      bfd_set_error (bfd_error_bad_value);
734
3.92k
    }
735
4.56k
      }
736
10.0k
  }
737
46.6k
    }
738
739
64.0k
  if (num_group != (unsigned) -1)
740
13.5k
    {
741
13.5k
      unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
742
13.5k
      unsigned int j;
743
744
22.0k
      for (j = 0; j < num_group; j++)
745
13.5k
  {
746
    /* Begin search from previous found group.  */
747
13.5k
    unsigned i = (j + search_offset) % num_group;
748
749
13.5k
    Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
750
13.5k
    Elf_Internal_Group *idx;
751
13.5k
    bfd_size_type n_elt;
752
753
13.5k
    if (shdr == NULL)
754
2.50k
      continue;
755
756
11.0k
    idx = (Elf_Internal_Group *) shdr->contents;
757
11.0k
    if (idx == NULL || shdr->sh_size < 4)
758
214
      {
759
        /* See PR 21957 for a reproducer.  */
760
        /* xgettext:c-format */
761
214
        _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
762
214
          abfd, shdr->bfd_section);
763
214
        elf_tdata (abfd)->group_sect_ptr[i] = NULL;
764
214
        bfd_set_error (bfd_error_bad_value);
765
214
        return false;
766
214
      }
767
10.8k
    n_elt = shdr->sh_size / 4;
768
769
    /* Look through this group's sections to see if current
770
       section is a member.  */
771
1.37M
    while (--n_elt != 0)
772
1.37M
      if ((++idx)->shdr == hdr)
773
9.06k
        {
774
9.06k
    asection *s = NULL;
775
776
    /* We are a member of this group.  Go looking through
777
       other members to see if any others are linked via
778
       next_in_group.  */
779
9.06k
    idx = (Elf_Internal_Group *) shdr->contents;
780
9.06k
    n_elt = shdr->sh_size / 4;
781
2.03M
    while (--n_elt != 0)
782
2.03M
      if ((++idx)->shdr != NULL
783
2.03M
          && (s = idx->shdr->bfd_section) != NULL
784
2.03M
          && elf_next_in_group (s) != NULL)
785
1.66k
        break;
786
9.06k
    if (n_elt != 0)
787
1.66k
      {
788
        /* Snarf the group name from other member, and
789
           insert current section in circular list.  */
790
1.66k
        elf_group_name (newsect) = elf_group_name (s);
791
1.66k
        elf_next_in_group (newsect) = elf_next_in_group (s);
792
1.66k
        elf_next_in_group (s) = newsect;
793
1.66k
      }
794
7.40k
    else
795
7.40k
      {
796
7.40k
        const char *gname;
797
798
7.40k
        gname = group_signature (abfd, shdr);
799
7.40k
        if (gname == NULL)
800
4.81k
          return false;
801
2.59k
        elf_group_name (newsect) = gname;
802
803
        /* Start a circular list with one element.  */
804
2.59k
        elf_next_in_group (newsect) = newsect;
805
2.59k
      }
806
807
    /* If the group section has been created, point to the
808
       new member.  */
809
4.25k
    if (shdr->bfd_section != NULL)
810
4.25k
      elf_next_in_group (shdr->bfd_section) = newsect;
811
812
4.25k
    elf_tdata (abfd)->group_search_offset = i;
813
4.25k
    j = num_group - 1;
814
4.25k
    break;
815
9.06k
        }
816
10.8k
  }
817
13.5k
    }
818
819
59.0k
  if (elf_group_name (newsect) == NULL)
820
54.8k
    {
821
      /* xgettext:c-format */
822
54.8k
      _bfd_error_handler (_("%pB: no group info for section '%pA'"),
823
54.8k
        abfd, newsect);
824
      /* PR 29532: Return true here, even though the group info has not been
825
   read.  Separate debug info files can have empty group sections, but
826
   we do not want this to prevent them from being loaded as otherwise
827
   GDB will not be able to use them.  */
828
54.8k
      return true;
829
54.8k
    }
830
4.25k
  return true;
831
59.0k
}
832
833
bool
834
_bfd_elf_setup_sections (bfd *abfd)
835
107k
{
836
107k
  unsigned int i;
837
107k
  unsigned int num_group = elf_tdata (abfd)->num_group;
838
107k
  bool result = true;
839
107k
  asection *s;
840
841
  /* Process SHF_LINK_ORDER.  */
842
350k
  for (s = abfd->sections; s != NULL; s = s->next)
843
243k
    {
844
243k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
845
243k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
846
57.7k
  {
847
57.7k
    unsigned int elfsec = this_hdr->sh_link;
848
    /* An sh_link value of 0 is now allowed.  It indicates that linked
849
       to section has already been discarded, but that the current
850
       section has been retained for some other reason.  This linking
851
       section is still a candidate for later garbage collection
852
       however.  */
853
57.7k
    if (elfsec == 0)
854
53.8k
      {
855
53.8k
        elf_linked_to_section (s) = NULL;
856
53.8k
      }
857
3.82k
    else
858
3.82k
      {
859
3.82k
        asection *linksec = NULL;
860
861
3.82k
        if (elfsec < elf_numsections (abfd))
862
3.82k
    {
863
3.82k
      this_hdr = elf_elfsections (abfd)[elfsec];
864
3.82k
      linksec = this_hdr->bfd_section;
865
3.82k
    }
866
867
        /* PR 1991, 2008:
868
     Some strip/objcopy may leave an incorrect value in
869
     sh_link.  We don't want to proceed.  */
870
3.82k
        if (linksec == NULL)
871
1.83k
    {
872
1.83k
      _bfd_error_handler
873
        /* xgettext:c-format */
874
1.83k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
875
1.83k
         s->owner, elfsec, s);
876
1.83k
      result = false;
877
1.83k
    }
878
879
3.82k
        elf_linked_to_section (s) = linksec;
880
3.82k
      }
881
57.7k
  }
882
185k
      else if (this_hdr->sh_type == SHT_GROUP
883
185k
         && elf_next_in_group (s) == NULL)
884
3.14k
  {
885
3.14k
    _bfd_error_handler
886
      /* xgettext:c-format */
887
3.14k
      (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
888
3.14k
       abfd, elf_section_data (s)->this_idx);
889
3.14k
    result = false;
890
3.14k
  }
891
243k
    }
892
893
  /* Process section groups.  */
894
107k
  if (num_group == (unsigned) -1)
895
32.2k
    return result;
896
897
79.3k
  for (i = 0; i < num_group; i++)
898
4.12k
    {
899
4.12k
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
900
4.12k
      Elf_Internal_Group *idx;
901
4.12k
      unsigned int n_elt;
902
903
      /* PR binutils/18758: Beware of corrupt binaries with invalid
904
   group data.  */
905
4.12k
      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
906
22
  {
907
22
    _bfd_error_handler
908
      /* xgettext:c-format */
909
22
      (_("%pB: section group entry number %u is corrupt"),
910
22
       abfd, i);
911
22
    result = false;
912
22
    continue;
913
22
  }
914
915
4.10k
      idx = (Elf_Internal_Group *) shdr->contents;
916
4.10k
      n_elt = shdr->sh_size / 4;
917
918
1.05M
      while (--n_elt != 0)
919
1.04M
  {
920
1.04M
    ++ idx;
921
922
1.04M
    if (idx->shdr == NULL)
923
823k
      continue;
924
222k
    else if (idx->shdr->bfd_section)
925
11.3k
      elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
926
211k
    else if (idx->shdr->sh_type != SHT_RELA
927
211k
       && idx->shdr->sh_type != SHT_REL)
928
164k
      {
929
        /* There are some unknown sections in the group.  */
930
164k
        _bfd_error_handler
931
    /* xgettext:c-format */
932
164k
    (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
933
164k
     abfd,
934
164k
     idx->shdr->sh_type,
935
164k
     bfd_elf_string_from_elf_section (abfd,
936
164k
              (elf_elfheader (abfd)
937
164k
               ->e_shstrndx),
938
164k
              idx->shdr->sh_name),
939
164k
     shdr->bfd_section);
940
164k
        result = false;
941
164k
      }
942
1.04M
  }
943
4.10k
    }
944
945
75.2k
  return result;
946
107k
}
947
948
bool
949
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
950
0
{
951
0
  return elf_next_in_group (sec) != NULL;
952
0
}
953
954
const char *
955
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
956
0
{
957
0
  if (elf_sec_group (sec) != NULL)
958
0
    return elf_group_name (sec);
959
0
  return NULL;
960
0
}
961
962
/* This a copy of lto_section defined in GCC (lto-streamer.h).  */
963
964
struct lto_section
965
{
966
  int16_t major_version;
967
  int16_t minor_version;
968
  unsigned char slim_object;
969
970
  /* Flags is a private field that is not defined publicly.  */
971
  uint16_t flags;
972
};
973
974
/* Make a BFD section from an ELF section.  We store a pointer to the
975
   BFD section in the bfd_section field of the header.  */
976
977
bool
978
_bfd_elf_make_section_from_shdr (bfd *abfd,
979
         Elf_Internal_Shdr *hdr,
980
         const char *name,
981
         int shindex)
982
331k
{
983
331k
  asection *newsect;
984
331k
  flagword flags;
985
331k
  const struct elf_backend_data *bed;
986
331k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
987
988
331k
  if (hdr->bfd_section != NULL)
989
15.6k
    return true;
990
991
315k
  newsect = bfd_make_section_anyway (abfd, name);
992
315k
  if (newsect == NULL)
993
0
    return false;
994
995
315k
  hdr->bfd_section = newsect;
996
315k
  elf_section_data (newsect)->this_hdr = *hdr;
997
315k
  elf_section_data (newsect)->this_idx = shindex;
998
999
  /* Always use the real type/flags.  */
1000
315k
  elf_section_type (newsect) = hdr->sh_type;
1001
315k
  elf_section_flags (newsect) = hdr->sh_flags;
1002
1003
315k
  newsect->filepos = hdr->sh_offset;
1004
1005
315k
  flags = SEC_NO_FLAGS;
1006
315k
  if (hdr->sh_type != SHT_NOBITS)
1007
292k
    flags |= SEC_HAS_CONTENTS;
1008
315k
  if (hdr->sh_type == SHT_GROUP)
1009
11.5k
    flags |= SEC_GROUP;
1010
315k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
1011
98.6k
    {
1012
98.6k
      flags |= SEC_ALLOC;
1013
98.6k
      if (hdr->sh_type != SHT_NOBITS)
1014
78.2k
  flags |= SEC_LOAD;
1015
98.6k
    }
1016
315k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
1017
221k
    flags |= SEC_READONLY;
1018
315k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1019
118k
    flags |= SEC_CODE;
1020
197k
  else if ((flags & SEC_LOAD) != 0)
1021
25.0k
    flags |= SEC_DATA;
1022
315k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
1023
81.8k
    {
1024
81.8k
      flags |= SEC_MERGE;
1025
81.8k
      newsect->entsize = hdr->sh_entsize;
1026
81.8k
    }
1027
315k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
1028
94.3k
    flags |= SEC_STRINGS;
1029
315k
  if (hdr->sh_flags & SHF_GROUP)
1030
64.9k
    if (!setup_group (abfd, hdr, newsect))
1031
5.86k
      return false;
1032
309k
  if ((hdr->sh_flags & SHF_TLS) != 0)
1033
60.3k
    flags |= SEC_THREAD_LOCAL;
1034
309k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1035
115k
    flags |= SEC_EXCLUDE;
1036
1037
309k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1038
309k
    {
1039
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1040
   but binutils as of 2019-07-23 did not set the EI_OSABI header
1041
   byte.  */
1042
26.0k
    case ELFOSABI_GNU:
1043
62.0k
    case ELFOSABI_FREEBSD:
1044
62.0k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1045
33.4k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1046
      /* Fall through */
1047
91.8k
    case ELFOSABI_NONE:
1048
91.8k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1049
38.4k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1050
91.8k
      break;
1051
309k
    }
1052
1053
309k
  if ((flags & SEC_ALLOC) == 0)
1054
212k
    {
1055
      /* The debugging sections appear to be recognized only by name,
1056
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
1057
212k
      if (name [0] == '.')
1058
38.7k
  {
1059
38.7k
    if (startswith (name, ".debug")
1060
38.7k
        || startswith (name, ".gnu.debuglto_.debug_")
1061
38.7k
        || startswith (name, ".gnu.linkonce.wi.")
1062
38.7k
        || startswith (name, ".zdebug"))
1063
9.47k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1064
29.2k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1065
29.2k
       || startswith (name, ".note.gnu"))
1066
1.34k
      {
1067
1.34k
        flags |= SEC_ELF_OCTETS;
1068
1.34k
        opb = 1;
1069
1.34k
      }
1070
27.9k
    else if (startswith (name, ".line")
1071
27.9k
       || startswith (name, ".stab")
1072
27.9k
       || strcmp (name, ".gdb_index") == 0)
1073
6.77k
      flags |= SEC_DEBUGGING;
1074
38.7k
  }
1075
212k
    }
1076
1077
309k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1078
309k
      || !bfd_set_section_size (newsect, hdr->sh_size)
1079
309k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1080
309k
              & -hdr->sh_addralign)))
1081
3.62k
    return false;
1082
1083
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
1084
     only link a single copy of the section.  This is used to support
1085
     g++.  g++ will emit each template expansion in its own section.
1086
     The symbols will be defined as weak, so that multiple definitions
1087
     are permitted.  The GNU linker extension is to actually discard
1088
     all but one of the sections.  */
1089
306k
  if (startswith (name, ".gnu.linkonce")
1090
306k
      && elf_next_in_group (newsect) == NULL)
1091
10.9k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1092
1093
306k
  if (!bfd_set_section_flags (newsect, flags))
1094
0
    return false;
1095
1096
306k
  bed = get_elf_backend_data (abfd);
1097
306k
  if (bed->elf_backend_section_flags)
1098
68.5k
    if (!bed->elf_backend_section_flags (hdr))
1099
0
      return false;
1100
1101
  /* We do not parse the PT_NOTE segments as we are interested even in the
1102
     separate debug info files which may have the segments offsets corrupted.
1103
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1104
306k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1105
21.9k
    {
1106
21.9k
      bfd_byte *contents;
1107
1108
21.9k
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1109
5.66k
  return false;
1110
1111
16.3k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1112
16.3k
           hdr->sh_offset, hdr->sh_addralign);
1113
16.3k
      free (contents);
1114
16.3k
    }
1115
1116
300k
  if ((newsect->flags & SEC_ALLOC) != 0)
1117
97.3k
    {
1118
97.3k
      Elf_Internal_Phdr *phdr;
1119
97.3k
      unsigned int i, nload;
1120
1121
      /* Some ELF linkers produce binaries with all the program header
1122
   p_paddr fields zero.  If we have such a binary with more than
1123
   one PT_LOAD header, then leave the section lma equal to vma
1124
   so that we don't create sections with overlapping lma.  */
1125
97.3k
      phdr = elf_tdata (abfd)->phdr;
1126
106k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1127
64.9k
  if (phdr->p_paddr != 0)
1128
55.5k
    break;
1129
9.41k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1130
409
    ++nload;
1131
97.3k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1132
0
  return true;
1133
1134
97.3k
      phdr = elf_tdata (abfd)->phdr;
1135
1.91M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1136
1.82M
  {
1137
1.82M
    if (((phdr->p_type == PT_LOAD
1138
1.82M
    && (hdr->sh_flags & SHF_TLS) == 0)
1139
1.82M
         || phdr->p_type == PT_TLS)
1140
1.82M
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1141
7.44k
      {
1142
7.44k
        if ((newsect->flags & SEC_LOAD) == 0)
1143
7.05k
    newsect->lma = (phdr->p_paddr
1144
7.05k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1145
382
        else
1146
    /* We used to use the same adjustment for SEC_LOAD
1147
       sections, but that doesn't work if the segment
1148
       is packed with code from multiple VMAs.
1149
       Instead we calculate the section LMA based on
1150
       the segment LMA.  It is assumed that the
1151
       segment will contain sections with contiguous
1152
       LMAs, even if the VMAs are not.  */
1153
382
    newsect->lma = (phdr->p_paddr
1154
382
        + hdr->sh_offset - phdr->p_offset) / opb;
1155
1156
        /* With contiguous segments, we can't tell from file
1157
     offsets whether a section with zero size should
1158
     be placed at the end of one segment or the
1159
     beginning of the next.  Decide based on vaddr.  */
1160
7.44k
        if (hdr->sh_addr >= phdr->p_vaddr
1161
7.44k
      && (hdr->sh_addr + hdr->sh_size
1162
7.44k
          <= phdr->p_vaddr + phdr->p_memsz))
1163
3.91k
    break;
1164
7.44k
      }
1165
1.82M
  }
1166
97.3k
    }
1167
1168
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1169
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1170
300k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1171
300k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1172
300k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1173
8.52k
    {
1174
8.52k
      enum { nothing, compress, decompress } action = nothing;
1175
8.52k
      int compression_header_size;
1176
8.52k
      bfd_size_type uncompressed_size;
1177
8.52k
      unsigned int uncompressed_align_power;
1178
8.52k
      enum compression_type ch_type = ch_none;
1179
8.52k
      bool compressed
1180
8.52k
  = bfd_is_section_compressed_info (abfd, newsect,
1181
8.52k
            &compression_header_size,
1182
8.52k
            &uncompressed_size,
1183
8.52k
            &uncompressed_align_power,
1184
8.52k
            &ch_type);
1185
1186
      /* Should we decompress?  */
1187
8.52k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1188
0
  action = decompress;
1189
1190
      /* Should we compress?  Or convert to a different compression?  */
1191
8.52k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1192
8.52k
         && newsect->size != 0
1193
8.52k
         && compression_header_size >= 0
1194
8.52k
         && uncompressed_size > 0)
1195
0
  {
1196
0
    if (!compressed)
1197
0
      action = compress;
1198
0
    else
1199
0
      {
1200
0
        enum compression_type new_ch_type = ch_none;
1201
0
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1202
0
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1203
0
             ? ch_compress_zstd : ch_compress_zlib);
1204
0
        if (new_ch_type != ch_type)
1205
0
    action = compress;
1206
0
      }
1207
0
  }
1208
1209
8.52k
      if (action == compress)
1210
0
  {
1211
0
    if (!bfd_init_section_compress_status (abfd, newsect))
1212
0
      {
1213
0
        _bfd_error_handler
1214
    /* xgettext:c-format */
1215
0
    (_("%pB: unable to compress section %s"), abfd, name);
1216
0
        return false;
1217
0
      }
1218
0
  }
1219
8.52k
      else if (action == decompress)
1220
0
  {
1221
0
    if (!bfd_init_section_decompress_status (abfd, newsect))
1222
0
      {
1223
0
        _bfd_error_handler
1224
    /* xgettext:c-format */
1225
0
    (_("%pB: unable to decompress section %s"), abfd, name);
1226
0
        return false;
1227
0
      }
1228
0
#ifndef HAVE_ZSTD
1229
0
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1230
0
      {
1231
0
        _bfd_error_handler
1232
      /* xgettext:c-format */
1233
0
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1234
0
          "is not built with zstd support"),
1235
0
       abfd, name);
1236
0
        newsect->compress_status = COMPRESS_SECTION_NONE;
1237
0
        return false;
1238
0
      }
1239
0
#endif
1240
0
    if (abfd->is_linker_input
1241
0
        && name[1] == 'z')
1242
0
      {
1243
        /* Rename section from .zdebug_* to .debug_* so that ld
1244
     scripts will see this section as a debug section.  */
1245
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1246
0
        if (new_name == NULL)
1247
0
    return false;
1248
0
        bfd_rename_section (newsect, new_name);
1249
0
      }
1250
0
  }
1251
8.52k
    }
1252
1253
  /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1254
     section.  */
1255
300k
  if (startswith (name, ".gnu.lto_.lto."))
1256
2.60k
    {
1257
2.60k
      struct lto_section lsection;
1258
2.60k
      if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1259
2.60k
            sizeof (struct lto_section)))
1260
1.88k
  abfd->lto_slim_object = lsection.slim_object;
1261
2.60k
    }
1262
1263
300k
  return true;
1264
300k
}
1265
1266
const char *const bfd_elf_section_type_names[] =
1267
{
1268
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1269
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1270
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1271
};
1272
1273
/* ELF relocs are against symbols.  If we are producing relocatable
1274
   output, and the reloc is against an external symbol, and nothing
1275
   has given us any additional addend, the resulting reloc will also
1276
   be against the same symbol.  In such a case, we don't want to
1277
   change anything about the way the reloc is handled, since it will
1278
   all be done at final link time.  Rather than put special case code
1279
   into bfd_perform_relocation, all the reloc types use this howto
1280
   function, or should call this function for relocatable output.  */
1281
1282
bfd_reloc_status_type
1283
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1284
           arelent *reloc_entry,
1285
           asymbol *symbol,
1286
           void *data ATTRIBUTE_UNUSED,
1287
           asection *input_section,
1288
           bfd *output_bfd,
1289
           char **error_message ATTRIBUTE_UNUSED)
1290
0
{
1291
0
  if (output_bfd != NULL
1292
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1293
0
      && (! reloc_entry->howto->partial_inplace
1294
0
    || reloc_entry->addend == 0))
1295
0
    {
1296
0
      reloc_entry->address += input_section->output_offset;
1297
0
      return bfd_reloc_ok;
1298
0
    }
1299
1300
  /* In some cases the relocation should be treated as output section
1301
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1302
     targets lack section relative relocations and instead use
1303
     ordinary absolute relocations for references between DWARF
1304
     sections.  That is arguably a bug in those targets but it happens
1305
     to work for the usual case of linking to non-loaded ELF debug
1306
     sections with VMAs forced to zero.  PE COFF on the other hand
1307
     doesn't allow a section VMA of zero.  */
1308
0
  if (output_bfd == NULL
1309
0
      && !reloc_entry->howto->pc_relative
1310
0
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1311
0
      && (input_section->flags & SEC_DEBUGGING) != 0)
1312
0
    reloc_entry->addend -= symbol->section->output_section->vma;
1313
1314
0
  return bfd_reloc_continue;
1315
0
}
1316

1317
/* Returns TRUE if section A matches section B.
1318
   Names, addresses and links may be different, but everything else
1319
   should be the same.  */
1320
1321
static bool
1322
section_match (const Elf_Internal_Shdr * a,
1323
         const Elf_Internal_Shdr * b)
1324
0
{
1325
0
  if (a->sh_type != b->sh_type
1326
0
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1327
0
      || a->sh_addralign != b->sh_addralign
1328
0
      || a->sh_entsize != b->sh_entsize)
1329
0
    return false;
1330
0
  if (a->sh_type == SHT_SYMTAB
1331
0
      || a->sh_type == SHT_STRTAB)
1332
0
    return true;
1333
0
  return a->sh_size == b->sh_size;
1334
0
}
1335
1336
/* Find a section in OBFD that has the same characteristics
1337
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1338
   none can be found.  Check's section HINT first, as this is likely
1339
   to be the correct section.  */
1340
1341
static unsigned int
1342
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1343
     const unsigned int hint)
1344
0
{
1345
0
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1346
0
  unsigned int i;
1347
1348
0
  BFD_ASSERT (iheader != NULL);
1349
1350
  /* See PR 20922 for a reproducer of the NULL test.  */
1351
0
  if (hint < elf_numsections (obfd)
1352
0
      && oheaders[hint] != NULL
1353
0
      && section_match (oheaders[hint], iheader))
1354
0
    return hint;
1355
1356
0
  for (i = 1; i < elf_numsections (obfd); i++)
1357
0
    {
1358
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1359
1360
0
      if (oheader == NULL)
1361
0
  continue;
1362
0
      if (section_match (oheader, iheader))
1363
  /* FIXME: Do we care if there is a potential for
1364
     multiple matches ?  */
1365
0
  return i;
1366
0
    }
1367
1368
0
  return SHN_UNDEF;
1369
0
}
1370
1371
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1372
   Processor specific section, based upon a matching input section.
1373
   Returns TRUE upon success, FALSE otherwise.  */
1374
1375
static bool
1376
copy_special_section_fields (const bfd *ibfd,
1377
           bfd *obfd,
1378
           const Elf_Internal_Shdr *iheader,
1379
           Elf_Internal_Shdr *oheader,
1380
           const unsigned int secnum)
1381
0
{
1382
0
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1383
0
  const Elf_Internal_Shdr **iheaders
1384
0
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1385
0
  bool changed = false;
1386
0
  unsigned int sh_link;
1387
1388
0
  if (oheader->sh_type == SHT_NOBITS)
1389
0
    {
1390
      /* This is a feature for objcopy --only-keep-debug:
1391
   When a section's type is changed to NOBITS, we preserve
1392
   the sh_link and sh_info fields so that they can be
1393
   matched up with the original.
1394
1395
   Note: Strictly speaking these assignments are wrong.
1396
   The sh_link and sh_info fields should point to the
1397
   relevent sections in the output BFD, which may not be in
1398
   the same location as they were in the input BFD.  But
1399
   the whole point of this action is to preserve the
1400
   original values of the sh_link and sh_info fields, so
1401
   that they can be matched up with the section headers in
1402
   the original file.  So strictly speaking we may be
1403
   creating an invalid ELF file, but it is only for a file
1404
   that just contains debug info and only for sections
1405
   without any contents.  */
1406
0
      if (oheader->sh_link == 0)
1407
0
  oheader->sh_link = iheader->sh_link;
1408
0
      if (oheader->sh_info == 0)
1409
0
  oheader->sh_info = iheader->sh_info;
1410
0
      return true;
1411
0
    }
1412
1413
  /* Allow the target a chance to decide how these fields should be set.  */
1414
0
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1415
0
                iheader, oheader))
1416
0
    return true;
1417
1418
  /* We have an iheader which might match oheader, and which has non-zero
1419
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1420
     the section in the output bfd which corresponds to the linked section
1421
     in the input bfd.  */
1422
0
  if (iheader->sh_link != SHN_UNDEF)
1423
0
    {
1424
      /* See PR 20931 for a reproducer.  */
1425
0
      if (iheader->sh_link >= elf_numsections (ibfd))
1426
0
  {
1427
0
    _bfd_error_handler
1428
      /* xgettext:c-format */
1429
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1430
0
       ibfd, iheader->sh_link, secnum);
1431
0
    return false;
1432
0
  }
1433
1434
0
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1435
0
      if (sh_link != SHN_UNDEF)
1436
0
  {
1437
0
    oheader->sh_link = sh_link;
1438
0
    changed = true;
1439
0
  }
1440
0
      else
1441
  /* FIXME: Should we install iheader->sh_link
1442
     if we could not find a match ?  */
1443
0
  _bfd_error_handler
1444
    /* xgettext:c-format */
1445
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1446
0
    }
1447
1448
0
  if (iheader->sh_info)
1449
0
    {
1450
      /* The sh_info field can hold arbitrary information, but if the
1451
   SHF_LINK_INFO flag is set then it should be interpreted as a
1452
   section index.  */
1453
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1454
0
  {
1455
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1456
0
             iheader->sh_info);
1457
0
    if (sh_link != SHN_UNDEF)
1458
0
      oheader->sh_flags |= SHF_INFO_LINK;
1459
0
  }
1460
0
      else
1461
  /* No idea what it means - just copy it.  */
1462
0
  sh_link = iheader->sh_info;
1463
1464
0
      if (sh_link != SHN_UNDEF)
1465
0
  {
1466
0
    oheader->sh_info = sh_link;
1467
0
    changed = true;
1468
0
  }
1469
0
      else
1470
0
  _bfd_error_handler
1471
    /* xgettext:c-format */
1472
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1473
0
    }
1474
1475
0
  return changed;
1476
0
}
1477
1478
/* Copy the program header and other data from one object module to
1479
   another.  */
1480
1481
bool
1482
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1483
0
{
1484
0
  const Elf_Internal_Shdr **iheaders
1485
0
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1486
0
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1487
0
  const struct elf_backend_data *bed;
1488
0
  unsigned int i;
1489
1490
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1491
0
    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1492
0
    return true;
1493
1494
0
  if (!elf_flags_init (obfd))
1495
0
    {
1496
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1497
0
      elf_flags_init (obfd) = true;
1498
0
    }
1499
1500
0
  elf_gp (obfd) = elf_gp (ibfd);
1501
1502
  /* Also copy the EI_OSABI field.  */
1503
0
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1504
0
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1505
1506
  /* If set, copy the EI_ABIVERSION field.  */
1507
0
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1508
0
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1509
0
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1510
1511
  /* Copy object attributes.  */
1512
0
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1513
1514
0
  if (iheaders == NULL || oheaders == NULL)
1515
0
    return true;
1516
1517
0
  bed = get_elf_backend_data (obfd);
1518
1519
  /* Possibly copy other fields in the section header.  */
1520
0
  for (i = 1; i < elf_numsections (obfd); i++)
1521
0
    {
1522
0
      unsigned int j;
1523
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1524
1525
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1526
   because of a special case need for generating separate debug info
1527
   files.  See below for more details.  */
1528
0
      if (oheader == NULL
1529
0
    || (oheader->sh_type != SHT_NOBITS
1530
0
        && oheader->sh_type < SHT_LOOS))
1531
0
  continue;
1532
1533
      /* Ignore empty sections, and sections whose
1534
   fields have already been initialised.  */
1535
0
      if (oheader->sh_size == 0
1536
0
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1537
0
  continue;
1538
1539
      /* Scan for the matching section in the input bfd.
1540
   First we try for a direct mapping between the input and
1541
   output sections.  */
1542
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1543
0
  {
1544
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1545
1546
0
    if (iheader == NULL)
1547
0
      continue;
1548
1549
0
    if (oheader->bfd_section != NULL
1550
0
        && iheader->bfd_section != NULL
1551
0
        && iheader->bfd_section->output_section != NULL
1552
0
        && iheader->bfd_section->output_section == oheader->bfd_section)
1553
0
      {
1554
        /* We have found a connection from the input section to
1555
     the output section.  Attempt to copy the header fields.
1556
     If this fails then do not try any further sections -
1557
     there should only be a one-to-one mapping between
1558
     input and output.  */
1559
0
        if (!copy_special_section_fields (ibfd, obfd,
1560
0
            iheader, oheader, i))
1561
0
    j = elf_numsections (ibfd);
1562
0
        break;
1563
0
      }
1564
0
  }
1565
1566
0
      if (j < elf_numsections (ibfd))
1567
0
  continue;
1568
1569
      /* That failed.  So try to deduce the corresponding input section.
1570
   Unfortunately we cannot compare names as the output string table
1571
   is empty, so instead we check size, address and type.  */
1572
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1573
0
  {
1574
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1575
1576
0
    if (iheader == NULL)
1577
0
      continue;
1578
1579
    /* Try matching fields in the input section's header.
1580
       Since --only-keep-debug turns all non-debug sections into
1581
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1582
       input type.  */
1583
0
    if ((oheader->sh_type == SHT_NOBITS
1584
0
         || iheader->sh_type == oheader->sh_type)
1585
0
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1586
0
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1587
0
        && iheader->sh_addralign == oheader->sh_addralign
1588
0
        && iheader->sh_entsize == oheader->sh_entsize
1589
0
        && iheader->sh_size == oheader->sh_size
1590
0
        && iheader->sh_addr == oheader->sh_addr
1591
0
        && (iheader->sh_info != oheader->sh_info
1592
0
      || iheader->sh_link != oheader->sh_link))
1593
0
      {
1594
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1595
0
    break;
1596
0
      }
1597
0
  }
1598
1599
0
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1600
0
  {
1601
    /* Final attempt.  Call the backend copy function
1602
       with a NULL input section.  */
1603
0
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1604
0
                     NULL, oheader);
1605
0
  }
1606
0
    }
1607
1608
0
  return true;
1609
0
}
1610
1611
static const char *
1612
get_segment_type (unsigned int p_type)
1613
0
{
1614
0
  const char *pt;
1615
0
  switch (p_type)
1616
0
    {
1617
0
    case PT_NULL: pt = "NULL"; break;
1618
0
    case PT_LOAD: pt = "LOAD"; break;
1619
0
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1620
0
    case PT_INTERP: pt = "INTERP"; break;
1621
0
    case PT_NOTE: pt = "NOTE"; break;
1622
0
    case PT_SHLIB: pt = "SHLIB"; break;
1623
0
    case PT_PHDR: pt = "PHDR"; break;
1624
0
    case PT_TLS: pt = "TLS"; break;
1625
0
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1626
0
    case PT_GNU_STACK: pt = "STACK"; break;
1627
0
    case PT_GNU_RELRO: pt = "RELRO"; break;
1628
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1629
0
    default: pt = NULL; break;
1630
0
    }
1631
0
  return pt;
1632
0
}
1633
1634
/* Print out the program headers.  */
1635
1636
bool
1637
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1638
0
{
1639
0
  FILE *f = (FILE *) farg;
1640
0
  Elf_Internal_Phdr *p;
1641
0
  asection *s;
1642
0
  bfd_byte *dynbuf = NULL;
1643
1644
0
  p = elf_tdata (abfd)->phdr;
1645
0
  if (p != NULL)
1646
0
    {
1647
0
      unsigned int i, c;
1648
1649
0
      fprintf (f, _("\nProgram Header:\n"));
1650
0
      c = elf_elfheader (abfd)->e_phnum;
1651
0
      for (i = 0; i < c; i++, p++)
1652
0
  {
1653
0
    const char *pt = get_segment_type (p->p_type);
1654
0
    char buf[20];
1655
1656
0
    if (pt == NULL)
1657
0
      {
1658
0
        sprintf (buf, "0x%lx", p->p_type);
1659
0
        pt = buf;
1660
0
      }
1661
0
    fprintf (f, "%8s off    0x", pt);
1662
0
    bfd_fprintf_vma (abfd, f, p->p_offset);
1663
0
    fprintf (f, " vaddr 0x");
1664
0
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1665
0
    fprintf (f, " paddr 0x");
1666
0
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1667
0
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1668
0
    fprintf (f, "         filesz 0x");
1669
0
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1670
0
    fprintf (f, " memsz 0x");
1671
0
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1672
0
    fprintf (f, " flags %c%c%c",
1673
0
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1674
0
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1675
0
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1676
0
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1677
0
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1678
0
    fprintf (f, "\n");
1679
0
  }
1680
0
    }
1681
1682
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
1683
0
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1684
0
    {
1685
0
      unsigned int elfsec;
1686
0
      unsigned long shlink;
1687
0
      bfd_byte *extdyn, *extdynend;
1688
0
      size_t extdynsize;
1689
0
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1690
1691
0
      fprintf (f, _("\nDynamic Section:\n"));
1692
1693
0
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1694
0
  goto error_return;
1695
1696
0
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1697
0
      if (elfsec == SHN_BAD)
1698
0
  goto error_return;
1699
0
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1700
1701
0
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1702
0
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1703
1704
0
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1705
0
     (size_t) (extdynend - extdyn) >= extdynsize;
1706
0
     extdyn += extdynsize)
1707
0
  {
1708
0
    Elf_Internal_Dyn dyn;
1709
0
    const char *name = "";
1710
0
    char ab[20];
1711
0
    bool stringp;
1712
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1713
1714
0
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1715
1716
0
    if (dyn.d_tag == DT_NULL)
1717
0
      break;
1718
1719
0
    stringp = false;
1720
0
    switch (dyn.d_tag)
1721
0
      {
1722
0
      default:
1723
0
        if (bed->elf_backend_get_target_dtag)
1724
0
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1725
1726
0
        if (!strcmp (name, ""))
1727
0
    {
1728
0
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1729
0
      name = ab;
1730
0
    }
1731
0
        break;
1732
1733
0
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1734
0
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1735
0
      case DT_PLTGOT: name = "PLTGOT"; break;
1736
0
      case DT_HASH: name = "HASH"; break;
1737
0
      case DT_STRTAB: name = "STRTAB"; break;
1738
0
      case DT_SYMTAB: name = "SYMTAB"; break;
1739
0
      case DT_RELA: name = "RELA"; break;
1740
0
      case DT_RELASZ: name = "RELASZ"; break;
1741
0
      case DT_RELAENT: name = "RELAENT"; break;
1742
0
      case DT_STRSZ: name = "STRSZ"; break;
1743
0
      case DT_SYMENT: name = "SYMENT"; break;
1744
0
      case DT_INIT: name = "INIT"; break;
1745
0
      case DT_FINI: name = "FINI"; break;
1746
0
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1747
0
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1748
0
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1749
0
      case DT_REL: name = "REL"; break;
1750
0
      case DT_RELSZ: name = "RELSZ"; break;
1751
0
      case DT_RELENT: name = "RELENT"; break;
1752
0
      case DT_RELR: name = "RELR"; break;
1753
0
      case DT_RELRSZ: name = "RELRSZ"; break;
1754
0
      case DT_RELRENT: name = "RELRENT"; break;
1755
0
      case DT_PLTREL: name = "PLTREL"; break;
1756
0
      case DT_DEBUG: name = "DEBUG"; break;
1757
0
      case DT_TEXTREL: name = "TEXTREL"; break;
1758
0
      case DT_JMPREL: name = "JMPREL"; break;
1759
0
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1760
0
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1761
0
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1762
0
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1763
0
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1764
0
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1765
0
      case DT_FLAGS: name = "FLAGS"; break;
1766
0
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1767
0
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1768
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1769
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1770
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1771
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1772
0
      case DT_FEATURE: name = "FEATURE"; break;
1773
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1774
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1775
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1776
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1777
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1778
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1779
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1780
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1781
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1782
0
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1783
0
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1784
0
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1785
0
      case DT_VERSYM: name = "VERSYM"; break;
1786
0
      case DT_VERDEF: name = "VERDEF"; break;
1787
0
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1788
0
      case DT_VERNEED: name = "VERNEED"; break;
1789
0
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1790
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1791
0
      case DT_USED: name = "USED"; break;
1792
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1793
0
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1794
0
      }
1795
1796
0
    fprintf (f, "  %-20s ", name);
1797
0
    if (! stringp)
1798
0
      {
1799
0
        fprintf (f, "0x");
1800
0
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1801
0
      }
1802
0
    else
1803
0
      {
1804
0
        const char *string;
1805
0
        unsigned int tagv = dyn.d_un.d_val;
1806
1807
0
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1808
0
        if (string == NULL)
1809
0
    goto error_return;
1810
0
        fprintf (f, "%s", string);
1811
0
      }
1812
0
    fprintf (f, "\n");
1813
0
  }
1814
1815
0
      free (dynbuf);
1816
0
      dynbuf = NULL;
1817
0
    }
1818
1819
0
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1820
0
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1821
0
    {
1822
0
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1823
0
  return false;
1824
0
    }
1825
1826
0
  if (elf_dynverdef (abfd) != 0)
1827
0
    {
1828
0
      Elf_Internal_Verdef *t;
1829
1830
0
      fprintf (f, _("\nVersion definitions:\n"));
1831
0
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1832
0
  {
1833
0
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1834
0
       t->vd_flags, t->vd_hash,
1835
0
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1836
0
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1837
0
      {
1838
0
        Elf_Internal_Verdaux *a;
1839
1840
0
        fprintf (f, "\t");
1841
0
        for (a = t->vd_auxptr->vda_nextptr;
1842
0
       a != NULL;
1843
0
       a = a->vda_nextptr)
1844
0
    fprintf (f, "%s ",
1845
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1846
0
        fprintf (f, "\n");
1847
0
      }
1848
0
  }
1849
0
    }
1850
1851
0
  if (elf_dynverref (abfd) != 0)
1852
0
    {
1853
0
      Elf_Internal_Verneed *t;
1854
1855
0
      fprintf (f, _("\nVersion References:\n"));
1856
0
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1857
0
  {
1858
0
    Elf_Internal_Vernaux *a;
1859
1860
0
    fprintf (f, _("  required from %s:\n"),
1861
0
       t->vn_filename ? t->vn_filename : "<corrupt>");
1862
0
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863
0
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1864
0
         a->vna_flags, a->vna_other,
1865
0
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1866
0
  }
1867
0
    }
1868
1869
0
  return true;
1870
1871
0
 error_return:
1872
0
  free (dynbuf);
1873
0
  return false;
1874
0
}
1875
1876
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1877
   and return symbol version for symbol version itself.   */
1878
1879
const char *
1880
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1881
            bool base_p,
1882
            bool *hidden)
1883
0
{
1884
0
  const char *version_string = NULL;
1885
0
  if (elf_dynversym (abfd) != 0
1886
0
      && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1887
0
    {
1888
0
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1889
1890
0
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
1891
0
      vernum &= VERSYM_VERSION;
1892
1893
0
      if (vernum == 0)
1894
0
  version_string = "";
1895
0
      else if (vernum == 1
1896
0
         && (vernum > elf_tdata (abfd)->cverdefs
1897
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
1898
0
           == VER_FLG_BASE)))
1899
0
  version_string = base_p ? "Base" : "";
1900
0
      else if (vernum <= elf_tdata (abfd)->cverdefs)
1901
0
  {
1902
0
    const char *nodename
1903
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1904
0
    version_string = "";
1905
0
    if (base_p
1906
0
        || nodename == NULL
1907
0
        || symbol->name == NULL
1908
0
        || strcmp (symbol->name, nodename) != 0)
1909
0
      version_string = nodename;
1910
0
  }
1911
0
      else
1912
0
  {
1913
0
    Elf_Internal_Verneed *t;
1914
1915
0
    version_string = _("<corrupt>");
1916
0
    for (t = elf_tdata (abfd)->verref;
1917
0
         t != NULL;
1918
0
         t = t->vn_nextref)
1919
0
      {
1920
0
        Elf_Internal_Vernaux *a;
1921
1922
0
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1923
0
    {
1924
0
      if (a->vna_other == vernum)
1925
0
        {
1926
0
          *hidden = true;
1927
0
          version_string = a->vna_nodename;
1928
0
          break;
1929
0
        }
1930
0
    }
1931
0
      }
1932
0
  }
1933
0
    }
1934
0
  return version_string;
1935
0
}
1936
1937
/* Display ELF-specific fields of a symbol.  */
1938
1939
void
1940
bfd_elf_print_symbol (bfd *abfd,
1941
          void *filep,
1942
          asymbol *symbol,
1943
          bfd_print_symbol_type how)
1944
0
{
1945
0
  FILE *file = (FILE *) filep;
1946
0
  switch (how)
1947
0
    {
1948
0
    case bfd_print_symbol_name:
1949
0
      fprintf (file, "%s", symbol->name);
1950
0
      break;
1951
0
    case bfd_print_symbol_more:
1952
0
      fprintf (file, "elf ");
1953
0
      bfd_fprintf_vma (abfd, file, symbol->value);
1954
0
      fprintf (file, " %x", symbol->flags);
1955
0
      break;
1956
0
    case bfd_print_symbol_all:
1957
0
      {
1958
0
  const char *section_name;
1959
0
  const char *name = NULL;
1960
0
  const struct elf_backend_data *bed;
1961
0
  unsigned char st_other;
1962
0
  bfd_vma val;
1963
0
  const char *version_string;
1964
0
  bool hidden;
1965
1966
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
1967
1968
0
  bed = get_elf_backend_data (abfd);
1969
0
  if (bed->elf_backend_print_symbol_all)
1970
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1971
1972
0
  if (name == NULL)
1973
0
    {
1974
0
      name = symbol->name;
1975
0
      bfd_print_symbol_vandf (abfd, file, symbol);
1976
0
    }
1977
1978
0
  fprintf (file, " %s\t", section_name);
1979
  /* Print the "other" value for a symbol.  For common symbols,
1980
     we've already printed the size; now print the alignment.
1981
     For other symbols, we have no specified alignment, and
1982
     we've printed the address; now print the size.  */
1983
0
  if (symbol->section && bfd_is_com_section (symbol->section))
1984
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1985
0
  else
1986
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1987
0
  bfd_fprintf_vma (abfd, file, val);
1988
1989
  /* If we have version information, print it.  */
1990
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
1991
0
                   symbol,
1992
0
                   true,
1993
0
                   &hidden);
1994
0
  if (version_string)
1995
0
    {
1996
0
      if (!hidden)
1997
0
        fprintf (file, "  %-11s", version_string);
1998
0
      else
1999
0
        {
2000
0
    int i;
2001
2002
0
    fprintf (file, " (%s)", version_string);
2003
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2004
0
      putc (' ', file);
2005
0
        }
2006
0
    }
2007
2008
  /* If the st_other field is not zero, print it.  */
2009
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2010
2011
0
  switch (st_other)
2012
0
    {
2013
0
    case 0: break;
2014
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2015
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2016
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2017
0
    default:
2018
      /* Some other non-defined flags are also present, so print
2019
         everything hex.  */
2020
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2021
0
    }
2022
2023
0
  fprintf (file, " %s", name);
2024
0
      }
2025
0
      break;
2026
0
    }
2027
0
}
2028

2029
/* ELF .o/exec file reading */
2030
2031
/* Create a new bfd section from an ELF section header.  */
2032
2033
bool
2034
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2035
621k
{
2036
621k
  Elf_Internal_Shdr *hdr;
2037
621k
  Elf_Internal_Ehdr *ehdr;
2038
621k
  const struct elf_backend_data *bed;
2039
621k
  const char *name;
2040
621k
  bool ret = true;
2041
2042
621k
  if (shindex >= elf_numsections (abfd))
2043
0
    return false;
2044
2045
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2046
     sh_link or sh_info.  Detect this here, by refusing to load a
2047
     section that we are already in the process of loading.  */
2048
621k
  if (elf_tdata (abfd)->being_created[shindex])
2049
322
    {
2050
322
      _bfd_error_handler
2051
322
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2052
322
      return false;
2053
322
    }
2054
620k
  elf_tdata (abfd)->being_created[shindex] = true;
2055
2056
620k
  hdr = elf_elfsections (abfd)[shindex];
2057
620k
  ehdr = elf_elfheader (abfd);
2058
620k
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2059
620k
            hdr->sh_name);
2060
620k
  if (name == NULL)
2061
7.52k
    goto fail;
2062
2063
613k
  bed = get_elf_backend_data (abfd);
2064
613k
  switch (hdr->sh_type)
2065
613k
    {
2066
89.2k
    case SHT_NULL:
2067
      /* Inactive section. Throw it away.  */
2068
89.2k
      goto success;
2069
2070
4.81k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2071
27.9k
    case SHT_NOBITS:   /* .bss section.  */
2072
30.1k
    case SHT_HASH:   /* .hash section.  */
2073
55.8k
    case SHT_NOTE:   /* .note section.  */
2074
63.0k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2075
63.4k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2076
66.3k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2077
66.5k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2078
68.1k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2079
68.1k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2080
68.1k
      goto success;
2081
2082
1.52k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2083
1.52k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2084
0
  goto fail;
2085
2086
1.52k
      if (hdr->sh_link > elf_numsections (abfd))
2087
0
  {
2088
    /* PR 10478: Accept Solaris binaries with a sh_link field
2089
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2090
0
    switch (bfd_get_arch (abfd))
2091
0
      {
2092
0
      case bfd_arch_i386:
2093
0
      case bfd_arch_sparc:
2094
0
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2095
0
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2096
0
    break;
2097
        /* Otherwise fall through.  */
2098
0
      default:
2099
0
        goto fail;
2100
0
      }
2101
0
  }
2102
1.52k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2103
0
  goto fail;
2104
1.52k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2105
1.52k
  {
2106
1.52k
    Elf_Internal_Shdr *dynsymhdr;
2107
2108
    /* The shared libraries distributed with hpux11 have a bogus
2109
       sh_link field for the ".dynamic" section.  Find the
2110
       string table for the ".dynsym" section instead.  */
2111
1.52k
    if (elf_dynsymtab (abfd) != 0)
2112
652
      {
2113
652
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2114
652
        hdr->sh_link = dynsymhdr->sh_link;
2115
652
      }
2116
870
    else
2117
870
      {
2118
870
        unsigned int i, num_sec;
2119
2120
870
        num_sec = elf_numsections (abfd);
2121
5.52k
        for (i = 1; i < num_sec; i++)
2122
4.78k
    {
2123
4.78k
      dynsymhdr = elf_elfsections (abfd)[i];
2124
4.78k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2125
132
        {
2126
132
          hdr->sh_link = dynsymhdr->sh_link;
2127
132
          break;
2128
132
        }
2129
4.78k
    }
2130
870
      }
2131
1.52k
  }
2132
1.52k
      goto success;
2133
2134
6.41k
    case SHT_SYMTAB:   /* A symbol table.  */
2135
6.41k
      if (elf_onesymtab (abfd) == shindex)
2136
5.28k
  goto success;
2137
2138
1.13k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2139
136
  goto fail;
2140
2141
996
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2142
242
  {
2143
242
    if (hdr->sh_size != 0)
2144
3
      goto fail;
2145
    /* Some assemblers erroneously set sh_info to one with a
2146
       zero sh_size.  ld sees this as a global symbol count
2147
       of (unsigned) -1.  Fix it here.  */
2148
239
    hdr->sh_info = 0;
2149
239
    goto success;
2150
242
  }
2151
2152
      /* PR 18854: A binary might contain more than one symbol table.
2153
   Unusual, but possible.  Warn, but continue.  */
2154
754
      if (elf_onesymtab (abfd) != 0)
2155
0
  {
2156
0
    _bfd_error_handler
2157
      /* xgettext:c-format */
2158
0
      (_("%pB: warning: multiple symbol tables detected"
2159
0
         " - ignoring the table in section %u"),
2160
0
       abfd, shindex);
2161
0
    goto success;
2162
0
  }
2163
754
      elf_onesymtab (abfd) = shindex;
2164
754
      elf_symtab_hdr (abfd) = *hdr;
2165
754
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2166
754
      abfd->flags |= HAS_SYMS;
2167
2168
      /* Sometimes a shared object will map in the symbol table.  If
2169
   SHF_ALLOC is set, and this is a shared object, then we also
2170
   treat this section as a BFD section.  We can not base the
2171
   decision purely on SHF_ALLOC, because that flag is sometimes
2172
   set in a relocatable object file, which would confuse the
2173
   linker.  */
2174
754
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2175
754
    && (abfd->flags & DYNAMIC) != 0
2176
754
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2177
133
            shindex))
2178
0
  goto fail;
2179
2180
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2181
   can't read symbols without that section loaded as well.  It
2182
   is most likely specified by the next section header.  */
2183
754
      {
2184
754
  elf_section_list * entry;
2185
754
  unsigned int i, num_sec;
2186
2187
993
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2188
239
    if (entry->hdr.sh_link == shindex)
2189
0
      goto success;
2190
2191
754
  num_sec = elf_numsections (abfd);
2192
1.28k
  for (i = shindex + 1; i < num_sec; i++)
2193
532
    {
2194
532
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2195
2196
532
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2197
532
    && hdr2->sh_link == shindex)
2198
0
        break;
2199
532
    }
2200
2201
754
  if (i == num_sec)
2202
7.12k
    for (i = 1; i < shindex; i++)
2203
6.37k
      {
2204
6.37k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2205
2206
6.37k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2207
6.37k
      && hdr2->sh_link == shindex)
2208
0
    break;
2209
6.37k
      }
2210
2211
754
  if (i != shindex)
2212
0
    ret = bfd_section_from_shdr (abfd, i);
2213
  /* else FIXME: we have failed to find the symbol table.
2214
     Should we issue an error?  */
2215
754
  goto success;
2216
754
      }
2217
2218
1.86k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2219
1.86k
      if (elf_dynsymtab (abfd) == shindex)
2220
0
  goto success;
2221
2222
1.86k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2223
130
  goto fail;
2224
2225
1.73k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2226
143
  {
2227
143
    if (hdr->sh_size != 0)
2228
3
      goto fail;
2229
2230
    /* Some linkers erroneously set sh_info to one with a
2231
       zero sh_size.  ld sees this as a global symbol count
2232
       of (unsigned) -1.  Fix it here.  */
2233
140
    hdr->sh_info = 0;
2234
140
    goto success;
2235
143
  }
2236
2237
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2238
   Unusual, but possible.  Warn, but continue.  */
2239
1.59k
      if (elf_dynsymtab (abfd) != 0)
2240
0
  {
2241
0
    _bfd_error_handler
2242
      /* xgettext:c-format */
2243
0
      (_("%pB: warning: multiple dynamic symbol tables detected"
2244
0
         " - ignoring the table in section %u"),
2245
0
       abfd, shindex);
2246
0
    goto success;
2247
0
  }
2248
1.59k
      elf_dynsymtab (abfd) = shindex;
2249
1.59k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2250
1.59k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2251
1.59k
      abfd->flags |= HAS_SYMS;
2252
2253
      /* Besides being a symbol table, we also treat this as a regular
2254
   section, so that objcopy can handle it.  */
2255
1.59k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2256
1.59k
      goto success;
2257
2258
2.11k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2259
2.11k
      {
2260
2.11k
  elf_section_list * entry;
2261
2262
2.62k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2263
506
    if (entry->ndx == shindex)
2264
0
      goto success;
2265
2266
2.11k
  entry = bfd_alloc (abfd, sizeof (*entry));
2267
2.11k
  if (entry == NULL)
2268
0
    goto fail;
2269
2.11k
  entry->ndx = shindex;
2270
2.11k
  entry->hdr = * hdr;
2271
2.11k
  entry->next = elf_symtab_shndx_list (abfd);
2272
2.11k
  elf_symtab_shndx_list (abfd) = entry;
2273
2.11k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2274
2.11k
  goto success;
2275
2.11k
      }
2276
2277
159k
    case SHT_STRTAB:   /* A string table.  */
2278
159k
      if (hdr->bfd_section != NULL)
2279
1
  goto success;
2280
2281
159k
      if (ehdr->e_shstrndx == shindex)
2282
155k
  {
2283
155k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2284
155k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2285
155k
    goto success;
2286
155k
  }
2287
2288
4.28k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2289
10
  {
2290
10
  symtab_strtab:
2291
10
    elf_tdata (abfd)->strtab_hdr = *hdr;
2292
10
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2293
10
    goto success;
2294
10
  }
2295
2296
4.27k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2297
0
  {
2298
0
  dynsymtab_strtab:
2299
0
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2300
0
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2301
0
    elf_elfsections (abfd)[shindex] = hdr;
2302
    /* We also treat this as a regular section, so that objcopy
2303
       can handle it.  */
2304
0
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2305
0
             shindex);
2306
0
    goto success;
2307
0
  }
2308
2309
      /* If the string table isn't one of the above, then treat it as a
2310
   regular section.  We need to scan all the headers to be sure,
2311
   just in case this strtab section appeared before the above.  */
2312
4.27k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2313
4.27k
  {
2314
4.27k
    unsigned int i, num_sec;
2315
2316
4.27k
    num_sec = elf_numsections (abfd);
2317
44.3k
    for (i = 1; i < num_sec; i++)
2318
40.1k
      {
2319
40.1k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2320
40.1k
        if (hdr2->sh_link == shindex)
2321
756
    {
2322
      /* Prevent endless recursion on broken objects.  */
2323
756
      if (i == shindex)
2324
5
        goto fail;
2325
751
      if (! bfd_section_from_shdr (abfd, i))
2326
47
        goto fail;
2327
704
      if (elf_onesymtab (abfd) == i)
2328
0
        goto symtab_strtab;
2329
704
      if (elf_dynsymtab (abfd) == i)
2330
0
        goto dynsymtab_strtab;
2331
704
    }
2332
40.1k
      }
2333
4.27k
  }
2334
4.21k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2335
4.21k
      goto success;
2336
2337
1.19k
    case SHT_REL:
2338
4.71k
    case SHT_RELA:
2339
4.82k
    case SHT_RELR:
2340
      /* *These* do a lot of work -- but build no sections!  */
2341
4.82k
      {
2342
4.82k
  asection *target_sect;
2343
4.82k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2344
4.82k
  unsigned int num_sec = elf_numsections (abfd);
2345
4.82k
  struct bfd_elf_section_data *esdt;
2346
4.82k
  bfd_size_type size;
2347
2348
4.82k
  if (hdr->sh_type == SHT_REL)
2349
1.19k
    size = bed->s->sizeof_rel;
2350
3.63k
  else if (hdr->sh_type == SHT_RELA)
2351
3.52k
    size = bed->s->sizeof_rela;
2352
112
  else
2353
112
    size = bed->s->arch_size / 8;
2354
4.82k
  if (hdr->sh_entsize != size)
2355
738
    goto fail;
2356
2357
  /* Check for a bogus link to avoid crashing.  */
2358
4.08k
  if (hdr->sh_link >= num_sec)
2359
2
    {
2360
2
      _bfd_error_handler
2361
        /* xgettext:c-format */
2362
2
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2363
2
         abfd, hdr->sh_link, name, shindex);
2364
2
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2365
2
      goto success;
2366
2
    }
2367
2368
  /* Get the symbol table.  */
2369
4.08k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2370
4.08k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2371
4.08k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2372
17
    goto fail;
2373
2374
  /* If this is an alloc section in an executable or shared
2375
     library, or the reloc section does not use the main symbol
2376
     table we don't treat it as a reloc section.  BFD can't
2377
     adequately represent such a section, so at least for now,
2378
     we don't try.  We just present it as a normal section.  We
2379
     also can't use it as a reloc section if it points to the
2380
     null section, an invalid section, another reloc section, or
2381
     its sh_link points to the null section.  */
2382
4.06k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2383
4.06k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2384
4.06k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2385
4.06k
      || hdr->sh_type == SHT_RELR
2386
4.06k
      || hdr->sh_link == SHN_UNDEF
2387
4.06k
      || hdr->sh_link != elf_onesymtab (abfd)
2388
4.06k
      || hdr->sh_info == SHN_UNDEF
2389
4.06k
      || hdr->sh_info >= num_sec
2390
4.06k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2391
4.06k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2392
4.06k
    {
2393
4.06k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2394
4.06k
      goto success;
2395
4.06k
    }
2396
2397
6
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2398
5
    goto fail;
2399
2400
1
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2401
1
  if (target_sect == NULL)
2402
0
    goto fail;
2403
2404
1
  esdt = elf_section_data (target_sect);
2405
1
  if (hdr->sh_type == SHT_RELA)
2406
1
    p_hdr = &esdt->rela.hdr;
2407
0
  else
2408
0
    p_hdr = &esdt->rel.hdr;
2409
2410
  /* PR 17512: file: 0b4f81b7.
2411
     Also see PR 24456, for a file which deliberately has two reloc
2412
     sections.  */
2413
1
  if (*p_hdr != NULL)
2414
0
    {
2415
0
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2416
0
        {
2417
0
    _bfd_error_handler
2418
      /* xgettext:c-format */
2419
0
      (_("%pB: warning: secondary relocation section '%s' "
2420
0
         "for section %pA found - ignoring"),
2421
0
       abfd, name, target_sect);
2422
0
        }
2423
0
      else
2424
0
        esdt->has_secondary_relocs = true;
2425
0
      goto success;
2426
0
    }
2427
2428
1
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2429
1
  if (hdr2 == NULL)
2430
0
    goto fail;
2431
1
  *hdr2 = *hdr;
2432
1
  *p_hdr = hdr2;
2433
1
  elf_elfsections (abfd)[shindex] = hdr2;
2434
1
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2435
1
             * bed->s->int_rels_per_ext_rel);
2436
1
  target_sect->flags |= SEC_RELOC;
2437
1
  target_sect->relocation = NULL;
2438
1
  target_sect->rel_filepos = hdr->sh_offset;
2439
  /* In the section to which the relocations apply, mark whether
2440
     its relocations are of the REL or RELA variety.  */
2441
1
  if (hdr->sh_size != 0)
2442
1
    {
2443
1
      if (hdr->sh_type == SHT_RELA)
2444
1
        target_sect->use_rela_p = 1;
2445
1
    }
2446
1
  abfd->flags |= HAS_RELOC;
2447
1
  goto success;
2448
1
      }
2449
2450
263
    case SHT_GNU_verdef:
2451
263
      if (hdr->sh_info != 0)
2452
16
  elf_dynverdef (abfd) = shindex;
2453
263
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2454
263
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2455
263
      goto success;
2456
2457
3
    case SHT_GNU_versym:
2458
3
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2459
3
  goto fail;
2460
2461
0
      elf_dynversym (abfd) = shindex;
2462
0
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2463
0
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2464
0
      goto success;
2465
2466
5.68k
    case SHT_GNU_verneed:
2467
5.68k
      if (hdr->sh_info != 0)
2468
4.72k
  elf_dynverref (abfd) = shindex;
2469
5.68k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2470
5.68k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2471
5.68k
      goto success;
2472
2473
1.80k
    case SHT_SHLIB:
2474
1.80k
      goto success;
2475
2476
22.8k
    case SHT_GROUP:
2477
22.8k
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2478
2.61k
  goto fail;
2479
2480
20.2k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2481
520
  goto fail;
2482
2483
19.7k
      goto success;
2484
2485
248k
    default:
2486
      /* Possibly an attributes section.  */
2487
248k
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2488
248k
    || hdr->sh_type == bed->obj_attrs_section_type)
2489
32.2k
  {
2490
32.2k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2491
0
      goto fail;
2492
32.2k
    _bfd_elf_parse_attributes (abfd, hdr);
2493
32.2k
    goto success;
2494
32.2k
  }
2495
2496
      /* Check for any processor-specific section types.  */
2497
216k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2498
149k
  goto success;
2499
2500
67.2k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2501
31.7k
  {
2502
31.7k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2503
      /* FIXME: How to properly handle allocated section reserved
2504
         for applications?  */
2505
6.78k
      _bfd_error_handler
2506
        /* xgettext:c-format */
2507
6.78k
        (_("%pB: unknown type [%#x] section `%s'"),
2508
6.78k
         abfd, hdr->sh_type, name);
2509
25.0k
    else
2510
25.0k
      {
2511
        /* Allow sections reserved for applications.  */
2512
25.0k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2513
25.0k
        goto success;
2514
25.0k
      }
2515
31.7k
  }
2516
35.4k
      else if (hdr->sh_type >= SHT_LOPROC
2517
35.4k
         && hdr->sh_type <= SHT_HIPROC)
2518
  /* FIXME: We should handle this section.  */
2519
9.33k
  _bfd_error_handler
2520
    /* xgettext:c-format */
2521
9.33k
    (_("%pB: unknown type [%#x] section `%s'"),
2522
9.33k
     abfd, hdr->sh_type, name);
2523
26.1k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2524
12.0k
  {
2525
    /* Unrecognised OS-specific sections.  */
2526
12.0k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2527
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2528
         required to correctly process the section and the file should
2529
         be rejected with an error message.  */
2530
950
      _bfd_error_handler
2531
        /* xgettext:c-format */
2532
950
        (_("%pB: unknown type [%#x] section `%s'"),
2533
950
         abfd, hdr->sh_type, name);
2534
11.1k
    else
2535
11.1k
      {
2536
        /* Otherwise it should be processed.  */
2537
11.1k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2538
11.1k
        goto success;
2539
11.1k
      }
2540
12.0k
  }
2541
14.0k
      else
2542
  /* FIXME: We should handle this section.  */
2543
14.0k
  _bfd_error_handler
2544
    /* xgettext:c-format */
2545
14.0k
    (_("%pB: unknown type [%#x] section `%s'"),
2546
14.0k
     abfd, hdr->sh_type, name);
2547
2548
31.1k
      goto fail;
2549
613k
    }
2550
2551
42.8k
 fail:
2552
42.8k
  ret = false;
2553
620k
 success:
2554
620k
  elf_tdata (abfd)->being_created[shindex] = false;
2555
620k
  return ret;
2556
42.8k
}
2557
2558
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
2559
2560
Elf_Internal_Sym *
2561
bfd_sym_from_r_symndx (struct sym_cache *cache,
2562
           bfd *abfd,
2563
           unsigned long r_symndx)
2564
0
{
2565
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2566
2567
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2568
0
    {
2569
0
      Elf_Internal_Shdr *symtab_hdr;
2570
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
2571
0
      Elf_External_Sym_Shndx eshndx;
2572
2573
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2574
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2575
0
        &cache->sym[ent], esym, &eshndx) == NULL)
2576
0
  return NULL;
2577
2578
0
      if (cache->abfd != abfd)
2579
0
  {
2580
0
    memset (cache->indx, -1, sizeof (cache->indx));
2581
0
    cache->abfd = abfd;
2582
0
  }
2583
0
      cache->indx[ent] = r_symndx;
2584
0
    }
2585
2586
0
  return &cache->sym[ent];
2587
0
}
2588
2589
/* Given an ELF section number, retrieve the corresponding BFD
2590
   section.  */
2591
2592
asection *
2593
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2594
1.18k
{
2595
1.18k
  if (sec_index >= elf_numsections (abfd))
2596
0
    return NULL;
2597
1.18k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2598
1.18k
}
2599
2600
static const struct bfd_elf_special_section special_sections_b[] =
2601
{
2602
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2603
  { NULL,       0,  0, 0,    0 }
2604
};
2605
2606
static const struct bfd_elf_special_section special_sections_c[] =
2607
{
2608
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2609
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
2610
  { NULL,     0, 0, 0,      0 }
2611
};
2612
2613
static const struct bfd_elf_special_section special_sections_d[] =
2614
{
2615
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2616
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2617
  /* There are more DWARF sections than these, but they needn't be added here
2618
     unless you have to cope with broken compilers that don't emit section
2619
     attributes or you want to help the user writing assembler.  */
2620
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
2621
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
2622
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
2623
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
2624
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2625
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
2626
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
2627
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
2628
  { NULL,          0,  0, 0,      0 }
2629
};
2630
2631
static const struct bfd_elf_special_section special_sections_f[] =
2632
{
2633
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2634
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2635
  { NULL,        0 , 0, 0,      0 }
2636
};
2637
2638
static const struct bfd_elf_special_section special_sections_g[] =
2639
{
2640
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2641
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2642
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2643
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
2644
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2645
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2646
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2647
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2648
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2649
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2650
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
2651
  { NULL,      0,    0, 0,         0 }
2652
};
2653
2654
static const struct bfd_elf_special_section special_sections_h[] =
2655
{
2656
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
2657
  { NULL,        0, 0, 0,    0 }
2658
};
2659
2660
static const struct bfd_elf_special_section special_sections_i[] =
2661
{
2662
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2663
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2664
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
2665
  { NULL,          0,      0, 0,      0 }
2666
};
2667
2668
static const struct bfd_elf_special_section special_sections_l[] =
2669
{
2670
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2671
  { NULL,        0, 0, 0,    0 }
2672
};
2673
2674
static const struct bfd_elf_special_section special_sections_n[] =
2675
{
2676
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2677
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2678
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
2679
  { NULL,        0,     0, 0,      0 }
2680
};
2681
2682
static const struct bfd_elf_special_section special_sections_p[] =
2683
{
2684
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
2685
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
2686
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2687
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
2688
  { NULL,       0,      0, 0,     0 }
2689
};
2690
2691
static const struct bfd_elf_special_section special_sections_r[] =
2692
{
2693
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2694
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2695
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2696
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2697
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2698
  { NULL,       0,     0, 0,      0 }
2699
};
2700
2701
static const struct bfd_elf_special_section special_sections_s[] =
2702
{
2703
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2704
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2705
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2706
  /* See struct bfd_elf_special_section declaration for the semantics of
2707
     this special case where .prefix_length != strlen (.prefix).  */
2708
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
2709
  { NULL,     0,  0, 0,    0 }
2710
};
2711
2712
static const struct bfd_elf_special_section special_sections_t[] =
2713
{
2714
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2715
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2716
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2717
  { NULL,         0,  0, 0,      0 }
2718
};
2719
2720
static const struct bfd_elf_special_section special_sections_z[] =
2721
{
2722
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2723
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2724
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2725
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2726
  { NULL,         0,  0, 0,      0 }
2727
};
2728
2729
static const struct bfd_elf_special_section * const special_sections[] =
2730
{
2731
  special_sections_b,   /* 'b' */
2732
  special_sections_c,   /* 'c' */
2733
  special_sections_d,   /* 'd' */
2734
  NULL,       /* 'e' */
2735
  special_sections_f,   /* 'f' */
2736
  special_sections_g,   /* 'g' */
2737
  special_sections_h,   /* 'h' */
2738
  special_sections_i,   /* 'i' */
2739
  NULL,       /* 'j' */
2740
  NULL,       /* 'k' */
2741
  special_sections_l,   /* 'l' */
2742
  NULL,       /* 'm' */
2743
  special_sections_n,   /* 'n' */
2744
  NULL,       /* 'o' */
2745
  special_sections_p,   /* 'p' */
2746
  NULL,       /* 'q' */
2747
  special_sections_r,   /* 'r' */
2748
  special_sections_s,   /* 's' */
2749
  special_sections_t,   /* 't' */
2750
  NULL,       /* 'u' */
2751
  NULL,       /* 'v' */
2752
  NULL,       /* 'w' */
2753
  NULL,       /* 'x' */
2754
  NULL,       /* 'y' */
2755
  special_sections_z    /* 'z' */
2756
};
2757
2758
const struct bfd_elf_special_section *
2759
_bfd_elf_get_special_section (const char *name,
2760
            const struct bfd_elf_special_section *spec,
2761
            unsigned int rela)
2762
214k
{
2763
214k
  int i;
2764
214k
  int len;
2765
2766
214k
  len = strlen (name);
2767
2768
1.24M
  for (i = 0; spec[i].prefix != NULL; i++)
2769
1.03M
    {
2770
1.03M
      int suffix_len;
2771
1.03M
      int prefix_len = spec[i].prefix_length;
2772
2773
1.03M
      if (len < prefix_len)
2774
603k
  continue;
2775
435k
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2776
412k
  continue;
2777
2778
23.7k
      suffix_len = spec[i].suffix_length;
2779
23.7k
      if (suffix_len <= 0)
2780
17.2k
  {
2781
17.2k
    if (name[prefix_len] != 0)
2782
15.5k
      {
2783
15.5k
        if (suffix_len == 0)
2784
3.59k
    continue;
2785
11.9k
        if (name[prefix_len] != '.'
2786
11.9k
      && (suffix_len == -2
2787
8.42k
          || (rela && spec[i].type == SHT_REL)))
2788
7.12k
    continue;
2789
11.9k
      }
2790
17.2k
  }
2791
6.55k
      else
2792
6.55k
  {
2793
6.55k
    if (len < prefix_len + suffix_len)
2794
1.91k
      continue;
2795
4.64k
    if (memcmp (name + len - suffix_len,
2796
4.64k
          spec[i].prefix + prefix_len,
2797
4.64k
          suffix_len) != 0)
2798
2.31k
      continue;
2799
4.64k
  }
2800
8.83k
      return &spec[i];
2801
23.7k
    }
2802
2803
205k
  return NULL;
2804
214k
}
2805
2806
const struct bfd_elf_special_section *
2807
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2808
323k
{
2809
323k
  int i;
2810
323k
  const struct bfd_elf_special_section *spec;
2811
323k
  const struct elf_backend_data *bed;
2812
2813
  /* See if this is one of the special sections.  */
2814
323k
  if (sec->name == NULL)
2815
0
    return NULL;
2816
2817
323k
  bed = get_elf_backend_data (abfd);
2818
323k
  spec = bed->special_sections;
2819
323k
  if (spec)
2820
166k
    {
2821
166k
      spec = _bfd_elf_get_special_section (sec->name,
2822
166k
             bed->special_sections,
2823
166k
             sec->use_rela_p);
2824
166k
      if (spec != NULL)
2825
489
  return spec;
2826
166k
    }
2827
2828
323k
  if (sec->name[0] != '.')
2829
266k
    return NULL;
2830
2831
56.1k
  i = sec->name[1] - 'b';
2832
56.1k
  if (i < 0 || i > 'z' - 'b')
2833
9.63k
    return NULL;
2834
2835
46.5k
  spec = special_sections[i];
2836
2837
46.5k
  if (spec == NULL)
2838
317
    return NULL;
2839
2840
46.2k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2841
46.5k
}
2842
2843
bool
2844
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2845
323k
{
2846
323k
  struct bfd_elf_section_data *sdata;
2847
323k
  const struct elf_backend_data *bed;
2848
323k
  const struct bfd_elf_special_section *ssect;
2849
2850
323k
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2851
323k
  if (sdata == NULL)
2852
267k
    {
2853
267k
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2854
267k
                sizeof (*sdata));
2855
267k
      if (sdata == NULL)
2856
0
  return false;
2857
267k
      sec->used_by_bfd = sdata;
2858
267k
    }
2859
2860
  /* Indicate whether or not this section should use RELA relocations.  */
2861
323k
  bed = get_elf_backend_data (abfd);
2862
323k
  sec->use_rela_p = bed->default_use_rela_p;
2863
2864
  /* Set up ELF section type and flags for newly created sections, if
2865
     there is an ABI mandated section.  */
2866
323k
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
2867
323k
  if (ssect != NULL)
2868
8.83k
    {
2869
8.83k
      elf_section_type (sec) = ssect->type;
2870
8.83k
      elf_section_flags (sec) = ssect->attr;
2871
8.83k
    }
2872
2873
323k
  return _bfd_generic_new_section_hook (abfd, sec);
2874
323k
}
2875
2876
/* Create a new bfd section from an ELF program header.
2877
2878
   Since program segments have no names, we generate a synthetic name
2879
   of the form segment<NUM>, where NUM is generally the index in the
2880
   program header table.  For segments that are split (see below) we
2881
   generate the names segment<NUM>a and segment<NUM>b.
2882
2883
   Note that some program segments may have a file size that is different than
2884
   (less than) the memory size.  All this means is that at execution the
2885
   system must allocate the amount of memory specified by the memory size,
2886
   but only initialize it with the first "file size" bytes read from the
2887
   file.  This would occur for example, with program segments consisting
2888
   of combined data+bss.
2889
2890
   To handle the above situation, this routine generates TWO bfd sections
2891
   for the single program segment.  The first has the length specified by
2892
   the file size of the segment, and the second has the length specified
2893
   by the difference between the two sizes.  In effect, the segment is split
2894
   into its initialized and uninitialized parts.  */
2895
2896
bool
2897
_bfd_elf_make_section_from_phdr (bfd *abfd,
2898
         Elf_Internal_Phdr *hdr,
2899
         int hdr_index,
2900
         const char *type_name)
2901
0
{
2902
0
  asection *newsect;
2903
0
  char *name;
2904
0
  char namebuf[64];
2905
0
  size_t len;
2906
0
  int split;
2907
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2908
2909
0
  split = ((hdr->p_memsz > 0)
2910
0
      && (hdr->p_filesz > 0)
2911
0
      && (hdr->p_memsz > hdr->p_filesz));
2912
2913
0
  if (hdr->p_filesz > 0)
2914
0
    {
2915
0
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2916
0
      len = strlen (namebuf) + 1;
2917
0
      name = (char *) bfd_alloc (abfd, len);
2918
0
      if (!name)
2919
0
  return false;
2920
0
      memcpy (name, namebuf, len);
2921
0
      newsect = bfd_make_section (abfd, name);
2922
0
      if (newsect == NULL)
2923
0
  return false;
2924
0
      newsect->vma = hdr->p_vaddr / opb;
2925
0
      newsect->lma = hdr->p_paddr / opb;
2926
0
      newsect->size = hdr->p_filesz;
2927
0
      newsect->filepos = hdr->p_offset;
2928
0
      newsect->flags |= SEC_HAS_CONTENTS;
2929
0
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2930
0
      if (hdr->p_type == PT_LOAD)
2931
0
  {
2932
0
    newsect->flags |= SEC_ALLOC;
2933
0
    newsect->flags |= SEC_LOAD;
2934
0
    if (hdr->p_flags & PF_X)
2935
0
      {
2936
        /* FIXME: all we known is that it has execute PERMISSION,
2937
     may be data.  */
2938
0
        newsect->flags |= SEC_CODE;
2939
0
      }
2940
0
  }
2941
0
      if (!(hdr->p_flags & PF_W))
2942
0
  {
2943
0
    newsect->flags |= SEC_READONLY;
2944
0
  }
2945
0
    }
2946
2947
0
  if (hdr->p_memsz > hdr->p_filesz)
2948
0
    {
2949
0
      bfd_vma align;
2950
2951
0
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2952
0
      len = strlen (namebuf) + 1;
2953
0
      name = (char *) bfd_alloc (abfd, len);
2954
0
      if (!name)
2955
0
  return false;
2956
0
      memcpy (name, namebuf, len);
2957
0
      newsect = bfd_make_section (abfd, name);
2958
0
      if (newsect == NULL)
2959
0
  return false;
2960
0
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2961
0
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
2962
0
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2963
0
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2964
0
      align = newsect->vma & -newsect->vma;
2965
0
      if (align == 0 || align > hdr->p_align)
2966
0
  align = hdr->p_align;
2967
0
      newsect->alignment_power = bfd_log2 (align);
2968
0
      if (hdr->p_type == PT_LOAD)
2969
0
  {
2970
0
    newsect->flags |= SEC_ALLOC;
2971
0
    if (hdr->p_flags & PF_X)
2972
0
      newsect->flags |= SEC_CODE;
2973
0
  }
2974
0
      if (!(hdr->p_flags & PF_W))
2975
0
  newsect->flags |= SEC_READONLY;
2976
0
    }
2977
2978
0
  return true;
2979
0
}
2980
2981
static bool
2982
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
2983
0
{
2984
  /* The return value is ignored.  Build-ids are considered optional.  */
2985
0
  if (templ->xvec->flavour == bfd_target_elf_flavour)
2986
0
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
2987
0
      (templ, offset);
2988
0
  return false;
2989
0
}
2990
2991
bool
2992
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2993
0
{
2994
0
  const struct elf_backend_data *bed;
2995
2996
0
  switch (hdr->p_type)
2997
0
    {
2998
0
    case PT_NULL:
2999
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3000
3001
0
    case PT_LOAD:
3002
0
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3003
0
  return false;
3004
0
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3005
0
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3006
0
      return true;
3007
3008
0
    case PT_DYNAMIC:
3009
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3010
3011
0
    case PT_INTERP:
3012
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3013
3014
0
    case PT_NOTE:
3015
0
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3016
0
  return false;
3017
0
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3018
0
          hdr->p_align))
3019
0
  return false;
3020
0
      return true;
3021
3022
0
    case PT_SHLIB:
3023
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3024
3025
0
    case PT_PHDR:
3026
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3027
3028
0
    case PT_GNU_EH_FRAME:
3029
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3030
0
                "eh_frame_hdr");
3031
3032
0
    case PT_GNU_STACK:
3033
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3034
3035
0
    case PT_GNU_RELRO:
3036
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3037
3038
0
    case PT_GNU_SFRAME:
3039
0
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040
0
                "sframe");
3041
3042
0
    default:
3043
      /* Check for any processor-specific program segment types.  */
3044
0
      bed = get_elf_backend_data (abfd);
3045
0
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3046
0
    }
3047
0
}
3048
3049
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3050
   REL or RELA.  */
3051
3052
Elf_Internal_Shdr *
3053
_bfd_elf_single_rel_hdr (asection *sec)
3054
0
{
3055
0
  if (elf_section_data (sec)->rel.hdr)
3056
0
    {
3057
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3058
0
      return elf_section_data (sec)->rel.hdr;
3059
0
    }
3060
0
  else
3061
0
    return elf_section_data (sec)->rela.hdr;
3062
0
}
3063
3064
static bool
3065
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3066
          Elf_Internal_Shdr *rel_hdr,
3067
          const char *sec_name,
3068
          bool use_rela_p)
3069
0
{
3070
0
  char *name = (char *) bfd_alloc (abfd,
3071
0
           sizeof ".rela" + strlen (sec_name));
3072
0
  if (name == NULL)
3073
0
    return false;
3074
3075
0
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3076
0
  rel_hdr->sh_name =
3077
0
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3078
0
          false);
3079
0
  if (rel_hdr->sh_name == (unsigned int) -1)
3080
0
    return false;
3081
3082
0
  return true;
3083
0
}
3084
3085
/* Allocate and initialize a section-header for a new reloc section,
3086
   containing relocations against ASECT.  It is stored in RELDATA.  If
3087
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3088
   relocations.  */
3089
3090
static bool
3091
_bfd_elf_init_reloc_shdr (bfd *abfd,
3092
        struct bfd_elf_section_reloc_data *reldata,
3093
        const char *sec_name,
3094
        bool use_rela_p,
3095
        bool delay_st_name_p)
3096
0
{
3097
0
  Elf_Internal_Shdr *rel_hdr;
3098
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3099
3100
0
  BFD_ASSERT (reldata->hdr == NULL);
3101
0
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3102
0
  reldata->hdr = rel_hdr;
3103
3104
0
  if (delay_st_name_p)
3105
0
    rel_hdr->sh_name = (unsigned int) -1;
3106
0
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3107
0
          use_rela_p))
3108
0
    return false;
3109
0
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3110
0
  rel_hdr->sh_entsize = (use_rela_p
3111
0
       ? bed->s->sizeof_rela
3112
0
       : bed->s->sizeof_rel);
3113
0
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3114
0
  rel_hdr->sh_flags = 0;
3115
0
  rel_hdr->sh_addr = 0;
3116
0
  rel_hdr->sh_size = 0;
3117
0
  rel_hdr->sh_offset = 0;
3118
3119
0
  return true;
3120
0
}
3121
3122
/* Return the default section type based on the passed in section flags.  */
3123
3124
int
3125
bfd_elf_get_default_section_type (flagword flags)
3126
0
{
3127
0
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3128
0
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3129
0
    return SHT_NOBITS;
3130
0
  return SHT_PROGBITS;
3131
0
}
3132
3133
struct fake_section_arg
3134
{
3135
  struct bfd_link_info *link_info;
3136
  bool failed;
3137
};
3138
3139
/* Set up an ELF internal section header for a section.  */
3140
3141
static void
3142
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3143
0
{
3144
0
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3145
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3146
0
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3147
0
  Elf_Internal_Shdr *this_hdr;
3148
0
  unsigned int sh_type;
3149
0
  const char *name = asect->name;
3150
0
  bool delay_st_name_p = false;
3151
0
  bfd_vma mask;
3152
3153
0
  if (arg->failed)
3154
0
    {
3155
      /* We already failed; just get out of the bfd_map_over_sections
3156
   loop.  */
3157
0
      return;
3158
0
    }
3159
3160
0
  this_hdr = &esd->this_hdr;
3161
3162
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3163
0
  if (arg->link_info
3164
0
      && (abfd->flags & BFD_COMPRESS) != 0
3165
0
      && (asect->flags & SEC_DEBUGGING) != 0
3166
0
      && name[1] == 'd'
3167
0
      && name[6] == '_')
3168
0
    {
3169
      /* If this section will be compressed, delay adding section
3170
   name to section name section after it is compressed in
3171
   _bfd_elf_assign_file_positions_for_non_load.  */
3172
0
      delay_st_name_p = true;
3173
0
    }
3174
3175
0
  if (delay_st_name_p)
3176
0
    this_hdr->sh_name = (unsigned int) -1;
3177
0
  else
3178
0
    {
3179
0
      this_hdr->sh_name
3180
0
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3181
0
                name, false);
3182
0
      if (this_hdr->sh_name == (unsigned int) -1)
3183
0
  {
3184
0
    arg->failed = true;
3185
0
    return;
3186
0
  }
3187
0
    }
3188
3189
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3190
3191
0
  if ((asect->flags & SEC_ALLOC) != 0
3192
0
      || asect->user_set_vma)
3193
0
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3194
0
  else
3195
0
    this_hdr->sh_addr = 0;
3196
3197
0
  this_hdr->sh_offset = 0;
3198
0
  this_hdr->sh_size = asect->size;
3199
0
  this_hdr->sh_link = 0;
3200
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3201
0
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3202
0
    {
3203
0
      _bfd_error_handler
3204
  /* xgettext:c-format */
3205
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3206
0
   abfd, asect->alignment_power, asect);
3207
0
      arg->failed = true;
3208
0
      return;
3209
0
    }
3210
  /* Set sh_addralign to the highest power of two given by alignment
3211
     consistent with the section VMA.  Linker scripts can force VMA.  */
3212
0
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3213
0
  this_hdr->sh_addralign = mask & -mask;
3214
  /* The sh_entsize and sh_info fields may have been set already by
3215
     copy_private_section_data.  */
3216
3217
0
  this_hdr->bfd_section = asect;
3218
0
  this_hdr->contents = NULL;
3219
3220
  /* If the section type is unspecified, we set it based on
3221
     asect->flags.  */
3222
0
  if (asect->type != 0)
3223
0
    sh_type = asect->type;
3224
0
  else if ((asect->flags & SEC_GROUP) != 0)
3225
0
    sh_type = SHT_GROUP;
3226
0
  else
3227
0
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3228
3229
0
  if (this_hdr->sh_type == SHT_NULL)
3230
0
    this_hdr->sh_type = sh_type;
3231
0
  else if (this_hdr->sh_type == SHT_NOBITS
3232
0
     && sh_type == SHT_PROGBITS
3233
0
     && (asect->flags & SEC_ALLOC) != 0)
3234
0
    {
3235
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3236
   allow the link to proceed.  This can happen when users link
3237
   non-bss input sections to bss output sections, or emit data
3238
   to a bss output section via a linker script.  */
3239
0
      _bfd_error_handler
3240
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3241
0
      this_hdr->sh_type = sh_type;
3242
0
    }
3243
3244
0
  switch (this_hdr->sh_type)
3245
0
    {
3246
0
    default:
3247
0
      break;
3248
3249
0
    case SHT_STRTAB:
3250
0
    case SHT_NOTE:
3251
0
    case SHT_NOBITS:
3252
0
    case SHT_PROGBITS:
3253
0
      break;
3254
3255
0
    case SHT_INIT_ARRAY:
3256
0
    case SHT_FINI_ARRAY:
3257
0
    case SHT_PREINIT_ARRAY:
3258
0
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3259
0
      break;
3260
3261
0
    case SHT_HASH:
3262
0
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3263
0
      break;
3264
3265
0
    case SHT_DYNSYM:
3266
0
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3267
0
      break;
3268
3269
0
    case SHT_DYNAMIC:
3270
0
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3271
0
      break;
3272
3273
0
    case SHT_RELA:
3274
0
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3275
0
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3276
0
      break;
3277
3278
0
     case SHT_REL:
3279
0
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3280
0
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3281
0
      break;
3282
3283
0
     case SHT_GNU_versym:
3284
0
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3285
0
      break;
3286
3287
0
     case SHT_GNU_verdef:
3288
0
      this_hdr->sh_entsize = 0;
3289
      /* objcopy or strip will copy over sh_info, but may not set
3290
   cverdefs.  The linker will set cverdefs, but sh_info will be
3291
   zero.  */
3292
0
      if (this_hdr->sh_info == 0)
3293
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3294
0
      else
3295
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3296
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3297
0
      break;
3298
3299
0
    case SHT_GNU_verneed:
3300
0
      this_hdr->sh_entsize = 0;
3301
      /* objcopy or strip will copy over sh_info, but may not set
3302
   cverrefs.  The linker will set cverrefs, but sh_info will be
3303
   zero.  */
3304
0
      if (this_hdr->sh_info == 0)
3305
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3306
0
      else
3307
0
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3308
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3309
0
      break;
3310
3311
0
    case SHT_GROUP:
3312
0
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3313
0
      break;
3314
3315
0
    case SHT_GNU_HASH:
3316
0
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3317
0
      break;
3318
0
    }
3319
3320
0
  if ((asect->flags & SEC_ALLOC) != 0)
3321
0
    this_hdr->sh_flags |= SHF_ALLOC;
3322
0
  if ((asect->flags & SEC_READONLY) == 0)
3323
0
    this_hdr->sh_flags |= SHF_WRITE;
3324
0
  if ((asect->flags & SEC_CODE) != 0)
3325
0
    this_hdr->sh_flags |= SHF_EXECINSTR;
3326
0
  if ((asect->flags & SEC_MERGE) != 0)
3327
0
    {
3328
0
      this_hdr->sh_flags |= SHF_MERGE;
3329
0
      this_hdr->sh_entsize = asect->entsize;
3330
0
    }
3331
0
  if ((asect->flags & SEC_STRINGS) != 0)
3332
0
    this_hdr->sh_flags |= SHF_STRINGS;
3333
0
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3334
0
    this_hdr->sh_flags |= SHF_GROUP;
3335
0
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3336
0
    {
3337
0
      this_hdr->sh_flags |= SHF_TLS;
3338
0
      if (asect->size == 0
3339
0
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3340
0
  {
3341
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3342
3343
0
    this_hdr->sh_size = 0;
3344
0
    if (o != NULL)
3345
0
      {
3346
0
        this_hdr->sh_size = o->offset + o->size;
3347
0
        if (this_hdr->sh_size != 0)
3348
0
    this_hdr->sh_type = SHT_NOBITS;
3349
0
      }
3350
0
  }
3351
0
    }
3352
0
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3353
0
    this_hdr->sh_flags |= SHF_EXCLUDE;
3354
3355
  /* If the section has relocs, set up a section header for the
3356
     SHT_REL[A] section.  If two relocation sections are required for
3357
     this section, it is up to the processor-specific back-end to
3358
     create the other.  */
3359
0
  if ((asect->flags & SEC_RELOC) != 0)
3360
0
    {
3361
      /* When doing a relocatable link, create both REL and RELA sections if
3362
   needed.  */
3363
0
      if (arg->link_info
3364
    /* Do the normal setup if we wouldn't create any sections here.  */
3365
0
    && esd->rel.count + esd->rela.count > 0
3366
0
    && (bfd_link_relocatable (arg->link_info)
3367
0
        || arg->link_info->emitrelocations))
3368
0
  {
3369
0
    if (esd->rel.count && esd->rel.hdr == NULL
3370
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3371
0
              false, delay_st_name_p))
3372
0
      {
3373
0
        arg->failed = true;
3374
0
        return;
3375
0
      }
3376
0
    if (esd->rela.count && esd->rela.hdr == NULL
3377
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3378
0
              true, delay_st_name_p))
3379
0
      {
3380
0
        arg->failed = true;
3381
0
        return;
3382
0
      }
3383
0
  }
3384
0
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3385
0
            (asect->use_rela_p
3386
0
             ? &esd->rela : &esd->rel),
3387
0
            name,
3388
0
            asect->use_rela_p,
3389
0
            delay_st_name_p))
3390
0
  {
3391
0
    arg->failed = true;
3392
0
    return;
3393
0
  }
3394
0
    }
3395
3396
  /* Check for processor-specific section types.  */
3397
0
  sh_type = this_hdr->sh_type;
3398
0
  if (bed->elf_backend_fake_sections
3399
0
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3400
0
    {
3401
0
      arg->failed = true;
3402
0
      return;
3403
0
    }
3404
3405
0
  if (sh_type == SHT_NOBITS && asect->size != 0)
3406
0
    {
3407
      /* Don't change the header type from NOBITS if we are being
3408
   called for objcopy --only-keep-debug.  */
3409
0
      this_hdr->sh_type = sh_type;
3410
0
    }
3411
0
}
3412
3413
/* Fill in the contents of a SHT_GROUP section.  Called from
3414
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3415
   when ELF targets use the generic linker, ld.  Called for ld -r
3416
   from bfd_elf_final_link.  */
3417
3418
void
3419
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3420
0
{
3421
0
  bool *failedptr = (bool *) failedptrarg;
3422
0
  asection *elt, *first;
3423
0
  unsigned char *loc;
3424
0
  bool gas;
3425
3426
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3427
     elfxx-ia64.c.  */
3428
0
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3429
0
      || sec->size == 0
3430
0
      || *failedptr)
3431
0
    return;
3432
3433
0
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3434
0
    {
3435
0
      unsigned long symindx = 0;
3436
3437
      /* elf_group_id will have been set up by objcopy and the
3438
   generic linker.  */
3439
0
      if (elf_group_id (sec) != NULL)
3440
0
  symindx = elf_group_id (sec)->udata.i;
3441
3442
0
      if (symindx == 0)
3443
0
  {
3444
    /* If called from the assembler, swap_out_syms will have set up
3445
       elf_section_syms.
3446
       PR 25699: A corrupt input file could contain bogus group info.  */
3447
0
    if (sec->index >= elf_num_section_syms (abfd)
3448
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3449
0
      {
3450
0
        *failedptr = true;
3451
0
        return;
3452
0
      }
3453
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3454
0
  }
3455
0
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3456
0
    }
3457
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3458
0
    {
3459
      /* The ELF backend linker sets sh_info to -2 when the group
3460
   signature symbol is global, and thus the index can't be
3461
   set until all local symbols are output.  */
3462
0
      asection *igroup;
3463
0
      struct bfd_elf_section_data *sec_data;
3464
0
      unsigned long symndx;
3465
0
      unsigned long extsymoff;
3466
0
      struct elf_link_hash_entry *h;
3467
3468
      /* The point of this little dance to the first SHF_GROUP section
3469
   then back to the SHT_GROUP section is that this gets us to
3470
   the SHT_GROUP in the input object.  */
3471
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3472
0
      sec_data = elf_section_data (igroup);
3473
0
      symndx = sec_data->this_hdr.sh_info;
3474
0
      extsymoff = 0;
3475
0
      if (!elf_bad_symtab (igroup->owner))
3476
0
  {
3477
0
    Elf_Internal_Shdr *symtab_hdr;
3478
3479
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3480
0
    extsymoff = symtab_hdr->sh_info;
3481
0
  }
3482
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3483
0
      while (h->root.type == bfd_link_hash_indirect
3484
0
       || h->root.type == bfd_link_hash_warning)
3485
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3486
3487
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3488
0
    }
3489
3490
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3491
0
  gas = true;
3492
0
  if (sec->contents == NULL)
3493
0
    {
3494
0
      gas = false;
3495
0
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3496
3497
      /* Arrange for the section to be written out.  */
3498
0
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3499
0
      if (sec->contents == NULL)
3500
0
  {
3501
0
    *failedptr = true;
3502
0
    return;
3503
0
  }
3504
0
    }
3505
3506
0
  loc = sec->contents + sec->size;
3507
3508
  /* Get the pointer to the first section in the group that gas
3509
     squirreled away here.  objcopy arranges for this to be set to the
3510
     start of the input section group.  */
3511
0
  first = elt = elf_next_in_group (sec);
3512
3513
  /* First element is a flag word.  Rest of section is elf section
3514
     indices for all the sections of the group.  Write them backwards
3515
     just to keep the group in the same order as given in .section
3516
     directives, not that it matters.  */
3517
0
  while (elt != NULL)
3518
0
    {
3519
0
      asection *s;
3520
3521
0
      s = elt;
3522
0
      if (!gas)
3523
0
  s = s->output_section;
3524
0
      if (s != NULL
3525
0
    && !bfd_is_abs_section (s))
3526
0
  {
3527
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3528
0
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3529
3530
0
    if (elf_sec->rel.hdr != NULL
3531
0
        && (gas
3532
0
      || (input_elf_sec->rel.hdr != NULL
3533
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3534
0
      {
3535
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3536
0
        loc -= 4;
3537
0
        if (loc == sec->contents)
3538
0
    break;
3539
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3540
0
      }
3541
0
    if (elf_sec->rela.hdr != NULL
3542
0
        && (gas
3543
0
      || (input_elf_sec->rela.hdr != NULL
3544
0
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3545
0
      {
3546
0
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3547
0
        loc -= 4;
3548
0
        if (loc == sec->contents)
3549
0
    break;
3550
0
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3551
0
      }
3552
0
    loc -= 4;
3553
0
    if (loc == sec->contents)
3554
0
      break;
3555
0
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
3556
0
  }
3557
0
      elt = elf_next_in_group (elt);
3558
0
      if (elt == first)
3559
0
  break;
3560
0
    }
3561
3562
  /* We should always get here with loc == sec->contents + 4, but it is
3563
     possible to craft bogus SHT_GROUP sections that will cause segfaults
3564
     in objcopy without checking loc here and in the loop above.  */
3565
0
  if (loc == sec->contents)
3566
0
    BFD_ASSERT (0);
3567
0
  else
3568
0
    {
3569
0
      loc -= 4;
3570
0
      if (loc != sec->contents)
3571
0
  {
3572
0
    BFD_ASSERT (0);
3573
0
    memset (sec->contents + 4, 0, loc - sec->contents);
3574
0
    loc = sec->contents;
3575
0
  }
3576
0
    }
3577
3578
0
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3579
0
}
3580
3581
/* Given NAME, the name of a relocation section stripped of its
3582
   .rel/.rela prefix, return the section in ABFD to which the
3583
   relocations apply.  */
3584
3585
asection *
3586
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3587
0
{
3588
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3589
     section likely apply to .got.plt or .got section.  */
3590
0
  if (get_elf_backend_data (abfd)->want_got_plt
3591
0
      && strcmp (name, ".plt") == 0)
3592
0
    {
3593
0
      asection *sec;
3594
3595
0
      name = ".got.plt";
3596
0
      sec = bfd_get_section_by_name (abfd, name);
3597
0
      if (sec != NULL)
3598
0
  return sec;
3599
0
      name = ".got";
3600
0
    }
3601
3602
0
  return bfd_get_section_by_name (abfd, name);
3603
0
}
3604
3605
/* Return the section to which RELOC_SEC applies.  */
3606
3607
static asection *
3608
elf_get_reloc_section (asection *reloc_sec)
3609
0
{
3610
0
  const char *name;
3611
0
  unsigned int type;
3612
0
  bfd *abfd;
3613
0
  const struct elf_backend_data *bed;
3614
3615
0
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3616
0
  if (type != SHT_REL && type != SHT_RELA)
3617
0
    return NULL;
3618
3619
  /* We look up the section the relocs apply to by name.  */
3620
0
  name = reloc_sec->name;
3621
0
  if (!startswith (name, ".rel"))
3622
0
    return NULL;
3623
0
  name += 4;
3624
0
  if (type == SHT_RELA && *name++ != 'a')
3625
0
    return NULL;
3626
3627
0
  abfd = reloc_sec->owner;
3628
0
  bed = get_elf_backend_data (abfd);
3629
0
  return bed->get_reloc_section (abfd, name);
3630
0
}
3631
3632
/* Assign all ELF section numbers.  The dummy first section is handled here
3633
   too.  The link/info pointers for the standard section types are filled
3634
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
3635
   here for gas, objcopy, and when using the generic ELF linker.  */
3636
3637
static bool
3638
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3639
0
{
3640
0
  struct elf_obj_tdata *t = elf_tdata (abfd);
3641
0
  asection *sec;
3642
0
  unsigned int section_number;
3643
0
  Elf_Internal_Shdr **i_shdrp;
3644
0
  struct bfd_elf_section_data *d;
3645
0
  bool need_symtab;
3646
0
  size_t amt;
3647
3648
0
  section_number = 1;
3649
3650
0
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3651
3652
  /* SHT_GROUP sections are in relocatable files only.  */
3653
0
  if (link_info == NULL || !link_info->resolve_section_groups)
3654
0
    {
3655
0
      size_t reloc_count = 0;
3656
3657
      /* Put SHT_GROUP sections first.  */
3658
0
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
3659
0
  {
3660
0
    d = elf_section_data (sec);
3661
3662
0
    if (d->this_hdr.sh_type == SHT_GROUP)
3663
0
      {
3664
0
        if (sec->flags & SEC_LINKER_CREATED)
3665
0
    {
3666
      /* Remove the linker created SHT_GROUP sections.  */
3667
0
      bfd_section_list_remove (abfd, sec);
3668
0
      abfd->section_count--;
3669
0
    }
3670
0
        else
3671
0
    d->this_idx = section_number++;
3672
0
      }
3673
3674
    /* Count relocations.  */
3675
0
    reloc_count += sec->reloc_count;
3676
0
  }
3677
3678
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
3679
0
      if (reloc_count == 0)
3680
0
  abfd->flags &= ~HAS_RELOC;
3681
0
      else
3682
0
  abfd->flags |= HAS_RELOC;
3683
0
    }
3684
3685
0
  for (sec = abfd->sections; sec; sec = sec->next)
3686
0
    {
3687
0
      d = elf_section_data (sec);
3688
3689
0
      if (d->this_hdr.sh_type != SHT_GROUP)
3690
0
  d->this_idx = section_number++;
3691
0
      if (d->this_hdr.sh_name != (unsigned int) -1)
3692
0
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3693
0
      if (d->rel.hdr)
3694
0
  {
3695
0
    d->rel.idx = section_number++;
3696
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
3697
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3698
0
  }
3699
0
      else
3700
0
  d->rel.idx = 0;
3701
3702
0
      if (d->rela.hdr)
3703
0
  {
3704
0
    d->rela.idx = section_number++;
3705
0
    if (d->rela.hdr->sh_name != (unsigned int) -1)
3706
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3707
0
  }
3708
0
      else
3709
0
  d->rela.idx = 0;
3710
0
    }
3711
3712
0
  need_symtab = (bfd_get_symcount (abfd) > 0
3713
0
     || (link_info == NULL
3714
0
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3715
0
       == HAS_RELOC)));
3716
0
  if (need_symtab)
3717
0
    {
3718
0
      elf_onesymtab (abfd) = section_number++;
3719
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3720
0
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3721
0
  {
3722
0
    elf_section_list *entry;
3723
3724
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3725
3726
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
3727
0
    entry->ndx = section_number++;
3728
0
    elf_symtab_shndx_list (abfd) = entry;
3729
0
    entry->hdr.sh_name
3730
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3731
0
              ".symtab_shndx", false);
3732
0
    if (entry->hdr.sh_name == (unsigned int) -1)
3733
0
      return false;
3734
0
  }
3735
0
      elf_strtab_sec (abfd) = section_number++;
3736
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3737
0
    }
3738
3739
0
  elf_shstrtab_sec (abfd) = section_number++;
3740
0
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3741
0
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3742
3743
0
  if (section_number >= SHN_LORESERVE)
3744
0
    {
3745
      /* xgettext:c-format */
3746
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
3747
0
        abfd, section_number);
3748
0
      return false;
3749
0
    }
3750
3751
0
  elf_numsections (abfd) = section_number;
3752
0
  elf_elfheader (abfd)->e_shnum = section_number;
3753
3754
  /* Set up the list of section header pointers, in agreement with the
3755
     indices.  */
3756
0
  amt = section_number * sizeof (Elf_Internal_Shdr *);
3757
0
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3758
0
  if (i_shdrp == NULL)
3759
0
    return false;
3760
3761
0
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3762
0
             sizeof (Elf_Internal_Shdr));
3763
0
  if (i_shdrp[0] == NULL)
3764
0
    {
3765
0
      bfd_release (abfd, i_shdrp);
3766
0
      return false;
3767
0
    }
3768
3769
0
  elf_elfsections (abfd) = i_shdrp;
3770
3771
0
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3772
0
  if (need_symtab)
3773
0
    {
3774
0
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3775
0
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3776
0
  {
3777
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
3778
0
    BFD_ASSERT (entry != NULL);
3779
0
    i_shdrp[entry->ndx] = & entry->hdr;
3780
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
3781
0
  }
3782
0
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3783
0
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3784
0
    }
3785
3786
0
  for (sec = abfd->sections; sec; sec = sec->next)
3787
0
    {
3788
0
      asection *s;
3789
3790
0
      d = elf_section_data (sec);
3791
3792
0
      i_shdrp[d->this_idx] = &d->this_hdr;
3793
0
      if (d->rel.idx != 0)
3794
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
3795
0
      if (d->rela.idx != 0)
3796
0
  i_shdrp[d->rela.idx] = d->rela.hdr;
3797
3798
      /* Fill in the sh_link and sh_info fields while we're at it.  */
3799
3800
      /* sh_link of a reloc section is the section index of the symbol
3801
   table.  sh_info is the section index of the section to which
3802
   the relocation entries apply.  */
3803
0
      if (d->rel.idx != 0)
3804
0
  {
3805
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
3806
0
    d->rel.hdr->sh_info = d->this_idx;
3807
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3808
0
  }
3809
0
      if (d->rela.idx != 0)
3810
0
  {
3811
0
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
3812
0
    d->rela.hdr->sh_info = d->this_idx;
3813
0
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3814
0
  }
3815
3816
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
3817
0
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3818
0
  {
3819
0
    s = elf_linked_to_section (sec);
3820
    /* We can now have a NULL linked section pointer.
3821
       This happens when the sh_link field is 0, which is done
3822
       when a linked to section is discarded but the linking
3823
       section has been retained for some reason.  */
3824
0
    if (s)
3825
0
      {
3826
        /* Check discarded linkonce section.  */
3827
0
        if (discarded_section (s))
3828
0
    {
3829
0
      asection *kept;
3830
0
      _bfd_error_handler
3831
        /* xgettext:c-format */
3832
0
        (_("%pB: sh_link of section `%pA' points to"
3833
0
           " discarded section `%pA' of `%pB'"),
3834
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
3835
      /* Point to the kept section if it has the same
3836
         size as the discarded one.  */
3837
0
      kept = _bfd_elf_check_kept_section (s, link_info);
3838
0
      if (kept == NULL)
3839
0
        {
3840
0
          bfd_set_error (bfd_error_bad_value);
3841
0
          return false;
3842
0
        }
3843
0
      s = kept;
3844
0
    }
3845
        /* Handle objcopy. */
3846
0
        else if (s->output_section == NULL)
3847
0
    {
3848
0
      _bfd_error_handler
3849
        /* xgettext:c-format */
3850
0
        (_("%pB: sh_link of section `%pA' points to"
3851
0
           " removed section `%pA' of `%pB'"),
3852
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
3853
0
      bfd_set_error (bfd_error_bad_value);
3854
0
      return false;
3855
0
    }
3856
0
        s = s->output_section;
3857
0
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3858
0
      }
3859
0
  }
3860
3861
0
      switch (d->this_hdr.sh_type)
3862
0
  {
3863
0
  case SHT_REL:
3864
0
  case SHT_RELA:
3865
    /* sh_link is the section index of the symbol table.
3866
       sh_info is the section index of the section to which the
3867
       relocation entries apply.  */
3868
0
    if (d->this_hdr.sh_link == 0)
3869
0
      {
3870
        /* FIXME maybe: If this is a reloc section which we are
3871
     treating as a normal section then we likely should
3872
     not be assuming its sh_link is .dynsym or .symtab.  */
3873
0
        if ((sec->flags & SEC_ALLOC) != 0)
3874
0
    {
3875
0
      s = bfd_get_section_by_name (abfd, ".dynsym");
3876
0
      if (s != NULL)
3877
0
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3878
0
    }
3879
0
        else
3880
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
3881
0
      }
3882
3883
0
    s = elf_get_reloc_section (sec);
3884
0
    if (s != NULL)
3885
0
      {
3886
0
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3887
0
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
3888
0
      }
3889
0
    break;
3890
3891
0
  case SHT_STRTAB:
3892
    /* We assume that a section named .stab*str is a stabs
3893
       string section.  We look for a section with the same name
3894
       but without the trailing ``str'', and set its sh_link
3895
       field to point to this section.  */
3896
0
    if (startswith (sec->name, ".stab")
3897
0
        && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3898
0
      {
3899
0
        size_t len;
3900
0
        char *alc;
3901
3902
0
        len = strlen (sec->name);
3903
0
        alc = (char *) bfd_malloc (len - 2);
3904
0
        if (alc == NULL)
3905
0
    return false;
3906
0
        memcpy (alc, sec->name, len - 3);
3907
0
        alc[len - 3] = '\0';
3908
0
        s = bfd_get_section_by_name (abfd, alc);
3909
0
        free (alc);
3910
0
        if (s != NULL)
3911
0
    {
3912
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3913
3914
      /* This is a .stab section.  */
3915
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
3916
0
    }
3917
0
      }
3918
0
    break;
3919
3920
0
  case SHT_DYNAMIC:
3921
0
  case SHT_DYNSYM:
3922
0
  case SHT_GNU_verneed:
3923
0
  case SHT_GNU_verdef:
3924
    /* sh_link is the section header index of the string table
3925
       used for the dynamic entries, or the symbol table, or the
3926
       version strings.  */
3927
0
    s = bfd_get_section_by_name (abfd, ".dynstr");
3928
0
    if (s != NULL)
3929
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3930
0
    break;
3931
3932
0
  case SHT_GNU_LIBLIST:
3933
    /* sh_link is the section header index of the prelink library
3934
       list used for the dynamic entries, or the symbol table, or
3935
       the version strings.  */
3936
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3937
0
                ? ".dynstr" : ".gnu.libstr"));
3938
0
    if (s != NULL)
3939
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3940
0
    break;
3941
3942
0
  case SHT_HASH:
3943
0
  case SHT_GNU_HASH:
3944
0
  case SHT_GNU_versym:
3945
    /* sh_link is the section header index of the symbol table
3946
       this hash table or version table is for.  */
3947
0
    s = bfd_get_section_by_name (abfd, ".dynsym");
3948
0
    if (s != NULL)
3949
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3950
0
    break;
3951
3952
0
  case SHT_GROUP:
3953
0
    d->this_hdr.sh_link = elf_onesymtab (abfd);
3954
0
  }
3955
0
    }
3956
3957
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3958
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3959
     debug section name from .debug_* to .zdebug_* if needed.  */
3960
3961
0
  return true;
3962
0
}
3963
3964
static bool
3965
sym_is_global (bfd *abfd, asymbol *sym)
3966
0
{
3967
  /* If the backend has a special mapping, use it.  */
3968
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3969
0
  if (bed->elf_backend_sym_is_global)
3970
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3971
3972
0
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3973
0
    || bfd_is_und_section (bfd_asymbol_section (sym))
3974
0
    || bfd_is_com_section (bfd_asymbol_section (sym)));
3975
0
}
3976
3977
/* Filter global symbols of ABFD to include in the import library.  All
3978
   SYMCOUNT symbols of ABFD can be examined from their pointers in
3979
   SYMS.  Pointers of symbols to keep should be stored contiguously at
3980
   the beginning of that array.
3981
3982
   Returns the number of symbols to keep.  */
3983
3984
unsigned int
3985
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3986
        asymbol **syms, long symcount)
3987
0
{
3988
0
  long src_count, dst_count = 0;
3989
3990
0
  for (src_count = 0; src_count < symcount; src_count++)
3991
0
    {
3992
0
      asymbol *sym = syms[src_count];
3993
0
      char *name = (char *) bfd_asymbol_name (sym);
3994
0
      struct bfd_link_hash_entry *h;
3995
3996
0
      if (!sym_is_global (abfd, sym))
3997
0
  continue;
3998
3999
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4000
0
      if (h == NULL)
4001
0
  continue;
4002
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4003
0
  continue;
4004
0
      if (h->linker_def || h->ldscript_def)
4005
0
  continue;
4006
4007
0
      syms[dst_count++] = sym;
4008
0
    }
4009
4010
0
  syms[dst_count] = NULL;
4011
4012
0
  return dst_count;
4013
0
}
4014
4015
/* Don't output section symbols for sections that are not going to be
4016
   output, that are duplicates or there is no BFD section.  */
4017
4018
static bool
4019
ignore_section_sym (bfd *abfd, asymbol *sym)
4020
0
{
4021
0
  elf_symbol_type *type_ptr;
4022
4023
0
  if (sym == NULL)
4024
0
    return false;
4025
4026
0
  if ((sym->flags & BSF_SECTION_SYM) == 0)
4027
0
    return false;
4028
4029
  /* Ignore the section symbol if it isn't used.  */
4030
0
  if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4031
0
    return true;
4032
4033
0
  if (sym->section == NULL)
4034
0
    return true;
4035
4036
0
  type_ptr = elf_symbol_from (sym);
4037
0
  return ((type_ptr != NULL
4038
0
     && type_ptr->internal_elf_sym.st_shndx != 0
4039
0
     && bfd_is_abs_section (sym->section))
4040
0
    || !(sym->section->owner == abfd
4041
0
         || (sym->section->output_section != NULL
4042
0
       && sym->section->output_section->owner == abfd
4043
0
       && sym->section->output_offset == 0)
4044
0
         || bfd_is_abs_section (sym->section)));
4045
0
}
4046
4047
/* Map symbol from it's internal number to the external number, moving
4048
   all local symbols to be at the head of the list.  */
4049
4050
static bool
4051
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4052
0
{
4053
0
  unsigned int symcount = bfd_get_symcount (abfd);
4054
0
  asymbol **syms = bfd_get_outsymbols (abfd);
4055
0
  asymbol **sect_syms;
4056
0
  unsigned int num_locals = 0;
4057
0
  unsigned int num_globals = 0;
4058
0
  unsigned int num_locals2 = 0;
4059
0
  unsigned int num_globals2 = 0;
4060
0
  unsigned int max_index = 0;
4061
0
  unsigned int idx;
4062
0
  asection *asect;
4063
0
  asymbol **new_syms;
4064
0
  size_t amt;
4065
4066
#ifdef DEBUG
4067
  fprintf (stderr, "elf_map_symbols\n");
4068
  fflush (stderr);
4069
#endif
4070
4071
0
  for (asect = abfd->sections; asect; asect = asect->next)
4072
0
    {
4073
0
      if (max_index < asect->index)
4074
0
  max_index = asect->index;
4075
0
    }
4076
4077
0
  max_index++;
4078
0
  amt = max_index * sizeof (asymbol *);
4079
0
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4080
0
  if (sect_syms == NULL)
4081
0
    return false;
4082
0
  elf_section_syms (abfd) = sect_syms;
4083
0
  elf_num_section_syms (abfd) = max_index;
4084
4085
  /* Init sect_syms entries for any section symbols we have already
4086
     decided to output.  */
4087
0
  for (idx = 0; idx < symcount; idx++)
4088
0
    {
4089
0
      asymbol *sym = syms[idx];
4090
4091
0
      if ((sym->flags & BSF_SECTION_SYM) != 0
4092
0
    && sym->value == 0
4093
0
    && !ignore_section_sym (abfd, sym)
4094
0
    && !bfd_is_abs_section (sym->section))
4095
0
  {
4096
0
    asection *sec = sym->section;
4097
4098
0
    if (sec->owner != abfd)
4099
0
      sec = sec->output_section;
4100
4101
0
    sect_syms[sec->index] = syms[idx];
4102
0
  }
4103
0
    }
4104
4105
  /* Classify all of the symbols.  */
4106
0
  for (idx = 0; idx < symcount; idx++)
4107
0
    {
4108
0
      if (sym_is_global (abfd, syms[idx]))
4109
0
  num_globals++;
4110
0
      else if (!ignore_section_sym (abfd, syms[idx]))
4111
0
  num_locals++;
4112
0
    }
4113
4114
  /* We will be adding a section symbol for each normal BFD section.  Most
4115
     sections will already have a section symbol in outsymbols, but
4116
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4117
     at least in that case.  */
4118
0
  for (asect = abfd->sections; asect; asect = asect->next)
4119
0
    {
4120
0
      asymbol *sym = asect->symbol;
4121
      /* Don't include ignored section symbols.  */
4122
0
      if (!ignore_section_sym (abfd, sym)
4123
0
    && sect_syms[asect->index] == NULL)
4124
0
  {
4125
0
    if (!sym_is_global (abfd, asect->symbol))
4126
0
      num_locals++;
4127
0
    else
4128
0
      num_globals++;
4129
0
  }
4130
0
    }
4131
4132
  /* Now sort the symbols so the local symbols are first.  */
4133
0
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4134
0
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4135
0
  if (new_syms == NULL)
4136
0
    return false;
4137
4138
0
  for (idx = 0; idx < symcount; idx++)
4139
0
    {
4140
0
      asymbol *sym = syms[idx];
4141
0
      unsigned int i;
4142
4143
0
      if (sym_is_global (abfd, sym))
4144
0
  i = num_locals + num_globals2++;
4145
      /* Don't include ignored section symbols.  */
4146
0
      else if (!ignore_section_sym (abfd, sym))
4147
0
  i = num_locals2++;
4148
0
      else
4149
0
  continue;
4150
0
      new_syms[i] = sym;
4151
0
      sym->udata.i = i + 1;
4152
0
    }
4153
0
  for (asect = abfd->sections; asect; asect = asect->next)
4154
0
    {
4155
0
      asymbol *sym = asect->symbol;
4156
0
      if (!ignore_section_sym (abfd, sym)
4157
0
    && sect_syms[asect->index] == NULL)
4158
0
  {
4159
0
    unsigned int i;
4160
4161
0
    sect_syms[asect->index] = sym;
4162
0
    if (!sym_is_global (abfd, sym))
4163
0
      i = num_locals2++;
4164
0
    else
4165
0
      i = num_locals + num_globals2++;
4166
0
    new_syms[i] = sym;
4167
0
    sym->udata.i = i + 1;
4168
0
  }
4169
0
    }
4170
4171
0
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4172
4173
0
  *pnum_locals = num_locals;
4174
0
  return true;
4175
0
}
4176
4177
/* Align to the maximum file alignment that could be required for any
4178
   ELF data structure.  */
4179
4180
static inline file_ptr
4181
align_file_position (file_ptr off, int align)
4182
0
{
4183
0
  return (off + align - 1) & ~(align - 1);
4184
0
}
4185
4186
/* Assign a file position to a section, optionally aligning to the
4187
   required section alignment.  */
4188
4189
file_ptr
4190
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4191
             file_ptr offset,
4192
             bool align)
4193
0
{
4194
0
  if (align && i_shdrp->sh_addralign > 1)
4195
0
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4196
0
  i_shdrp->sh_offset = offset;
4197
0
  if (i_shdrp->bfd_section != NULL)
4198
0
    i_shdrp->bfd_section->filepos = offset;
4199
0
  if (i_shdrp->sh_type != SHT_NOBITS)
4200
0
    offset += i_shdrp->sh_size;
4201
0
  return offset;
4202
0
}
4203
4204
/* Compute the file positions we are going to put the sections at, and
4205
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4206
   is not NULL, this is being called by the ELF backend linker.  */
4207
4208
bool
4209
_bfd_elf_compute_section_file_positions (bfd *abfd,
4210
           struct bfd_link_info *link_info)
4211
0
{
4212
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4213
0
  struct fake_section_arg fsargs;
4214
0
  bool failed;
4215
0
  struct elf_strtab_hash *strtab = NULL;
4216
0
  Elf_Internal_Shdr *shstrtab_hdr;
4217
0
  bool need_symtab;
4218
4219
0
  if (abfd->output_has_begun)
4220
0
    return true;
4221
4222
  /* Do any elf backend specific processing first.  */
4223
0
  if (bed->elf_backend_begin_write_processing)
4224
0
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4225
4226
0
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4227
0
    return false;
4228
4229
0
  fsargs.failed = false;
4230
0
  fsargs.link_info = link_info;
4231
0
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4232
0
  if (fsargs.failed)
4233
0
    return false;
4234
4235
0
  if (!assign_section_numbers (abfd, link_info))
4236
0
    return false;
4237
4238
  /* The backend linker builds symbol table information itself.  */
4239
0
  need_symtab = (link_info == NULL
4240
0
     && (bfd_get_symcount (abfd) > 0
4241
0
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4242
0
       == HAS_RELOC)));
4243
0
  if (need_symtab)
4244
0
    {
4245
      /* Non-zero if doing a relocatable link.  */
4246
0
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4247
4248
0
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4249
0
  return false;
4250
0
    }
4251
4252
0
  failed = false;
4253
0
  if (link_info == NULL)
4254
0
    {
4255
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4256
0
      if (failed)
4257
0
  goto err_free_strtab;
4258
0
    }
4259
4260
0
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4261
  /* sh_name was set in init_file_header.  */
4262
0
  shstrtab_hdr->sh_type = SHT_STRTAB;
4263
0
  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4264
0
  shstrtab_hdr->sh_addr = 0;
4265
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4266
0
  shstrtab_hdr->sh_entsize = 0;
4267
0
  shstrtab_hdr->sh_link = 0;
4268
0
  shstrtab_hdr->sh_info = 0;
4269
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4270
0
  shstrtab_hdr->sh_addralign = 1;
4271
4272
0
  if (!assign_file_positions_except_relocs (abfd, link_info))
4273
0
    goto err_free_strtab;
4274
4275
0
  if (strtab != NULL)
4276
0
    {
4277
0
      file_ptr off;
4278
0
      Elf_Internal_Shdr *hdr;
4279
4280
0
      off = elf_next_file_pos (abfd);
4281
4282
0
      hdr = & elf_symtab_hdr (abfd);
4283
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4284
4285
0
      if (elf_symtab_shndx_list (abfd) != NULL)
4286
0
  {
4287
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4288
0
    if (hdr->sh_size != 0)
4289
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4290
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4291
0
  }
4292
4293
0
      hdr = &elf_tdata (abfd)->strtab_hdr;
4294
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4295
4296
0
      elf_next_file_pos (abfd) = off;
4297
4298
      /* Now that we know where the .strtab section goes, write it
4299
   out.  */
4300
0
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4301
0
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4302
0
  goto err_free_strtab;
4303
0
      _bfd_elf_strtab_free (strtab);
4304
0
    }
4305
4306
0
  abfd->output_has_begun = true;
4307
0
  return true;
4308
4309
0
 err_free_strtab:
4310
0
  if (strtab != NULL)
4311
0
    _bfd_elf_strtab_free (strtab);
4312
0
  return false;
4313
0
}
4314
4315
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4316
   function effectively returns whether --eh-frame-hdr is given on the
4317
   command line.  After size_dynamic_sections the result reflects
4318
   whether .eh_frame_hdr will actually be output (sizing isn't done
4319
   until ldemul_after_allocation).  */
4320
4321
static asection *
4322
elf_eh_frame_hdr (const struct bfd_link_info *info)
4323
0
{
4324
0
  if (info != NULL && is_elf_hash_table (info->hash))
4325
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4326
0
  return NULL;
4327
0
}
4328
4329
/* Make an initial estimate of the size of the program header.  If we
4330
   get the number wrong here, we'll redo section placement.  */
4331
4332
static bfd_size_type
4333
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4334
0
{
4335
0
  size_t segs;
4336
0
  asection *s;
4337
0
  const struct elf_backend_data *bed;
4338
4339
  /* Assume we will need exactly two PT_LOAD segments: one for text
4340
     and one for data.  */
4341
0
  segs = 2;
4342
4343
0
  s = bfd_get_section_by_name (abfd, ".interp");
4344
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4345
0
    {
4346
      /* If we have a loadable interpreter section, we need a
4347
   PT_INTERP segment.  In this case, assume we also need a
4348
   PT_PHDR segment, although that may not be true for all
4349
   targets.  */
4350
0
      segs += 2;
4351
0
    }
4352
4353
0
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4354
0
    {
4355
      /* We need a PT_DYNAMIC segment.  */
4356
0
      ++segs;
4357
0
    }
4358
4359
0
  if (info != NULL && info->relro)
4360
0
    {
4361
      /* We need a PT_GNU_RELRO segment.  */
4362
0
      ++segs;
4363
0
    }
4364
4365
0
  if (elf_eh_frame_hdr (info))
4366
0
    {
4367
      /* We need a PT_GNU_EH_FRAME segment.  */
4368
0
      ++segs;
4369
0
    }
4370
4371
0
  if (elf_stack_flags (abfd))
4372
0
    {
4373
      /* We need a PT_GNU_STACK segment.  */
4374
0
      ++segs;
4375
0
    }
4376
4377
0
  if (elf_sframe (abfd))
4378
0
    {
4379
      /* We need a PT_GNU_SFRAME segment.  */
4380
0
      ++segs;
4381
0
    }
4382
4383
0
  s = bfd_get_section_by_name (abfd,
4384
0
             NOTE_GNU_PROPERTY_SECTION_NAME);
4385
0
  if (s != NULL && s->size != 0)
4386
0
    {
4387
      /* We need a PT_GNU_PROPERTY segment.  */
4388
0
      ++segs;
4389
0
    }
4390
4391
0
  for (s = abfd->sections; s != NULL; s = s->next)
4392
0
    {
4393
0
      if ((s->flags & SEC_LOAD) != 0
4394
0
    && elf_section_type (s) == SHT_NOTE)
4395
0
  {
4396
0
    unsigned int alignment_power;
4397
    /* We need a PT_NOTE segment.  */
4398
0
    ++segs;
4399
    /* Try to create just one PT_NOTE segment for all adjacent
4400
       loadable SHT_NOTE sections.  gABI requires that within a
4401
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4402
       each note should have the same alignment.  So we check
4403
       whether the sections are correctly aligned.  */
4404
0
    alignment_power = s->alignment_power;
4405
0
    while (s->next != NULL
4406
0
     && s->next->alignment_power == alignment_power
4407
0
     && (s->next->flags & SEC_LOAD) != 0
4408
0
     && elf_section_type (s->next) == SHT_NOTE)
4409
0
      s = s->next;
4410
0
  }
4411
0
    }
4412
4413
0
  for (s = abfd->sections; s != NULL; s = s->next)
4414
0
    {
4415
0
      if (s->flags & SEC_THREAD_LOCAL)
4416
0
  {
4417
    /* We need a PT_TLS segment.  */
4418
0
    ++segs;
4419
0
    break;
4420
0
  }
4421
0
    }
4422
4423
0
  bed = get_elf_backend_data (abfd);
4424
4425
0
  if ((abfd->flags & D_PAGED) != 0
4426
0
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4427
0
    {
4428
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4429
0
      bfd_vma commonpagesize;
4430
0
      unsigned int page_align_power;
4431
4432
0
      if (info != NULL)
4433
0
  commonpagesize = info->commonpagesize;
4434
0
      else
4435
0
  commonpagesize = bed->commonpagesize;
4436
0
      page_align_power = bfd_log2 (commonpagesize);
4437
0
      for (s = abfd->sections; s != NULL; s = s->next)
4438
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4439
0
    {
4440
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4441
0
        {
4442
0
    _bfd_error_handler
4443
      /* xgettext:c-format */
4444
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4445
0
         "sh_info field: %d"),
4446
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4447
0
    continue;
4448
0
        }
4449
      /* Align mbind section to page size.  */
4450
0
      if (s->alignment_power < page_align_power)
4451
0
        s->alignment_power = page_align_power;
4452
0
      segs ++;
4453
0
    }
4454
0
    }
4455
4456
  /* Let the backend count up any program headers it might need.  */
4457
0
  if (bed->elf_backend_additional_program_headers)
4458
0
    {
4459
0
      int a;
4460
4461
0
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4462
0
      if (a == -1)
4463
0
  abort ();
4464
0
      segs += a;
4465
0
    }
4466
4467
0
  return segs * bed->s->sizeof_phdr;
4468
0
}
4469
4470
/* Find the segment that contains the output_section of section.  */
4471
4472
Elf_Internal_Phdr *
4473
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4474
0
{
4475
0
  struct elf_segment_map *m;
4476
0
  Elf_Internal_Phdr *p;
4477
4478
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4479
0
       m != NULL;
4480
0
       m = m->next, p++)
4481
0
    {
4482
0
      int i;
4483
4484
0
      for (i = m->count - 1; i >= 0; i--)
4485
0
  if (m->sections[i] == section)
4486
0
    return p;
4487
0
    }
4488
4489
0
  return NULL;
4490
0
}
4491
4492
/* Create a mapping from a set of sections to a program segment.  */
4493
4494
static struct elf_segment_map *
4495
make_mapping (bfd *abfd,
4496
        asection **sections,
4497
        unsigned int from,
4498
        unsigned int to,
4499
        bool phdr)
4500
0
{
4501
0
  struct elf_segment_map *m;
4502
0
  unsigned int i;
4503
0
  asection **hdrpp;
4504
0
  size_t amt;
4505
4506
0
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4507
0
  amt += (to - from) * sizeof (asection *);
4508
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4509
0
  if (m == NULL)
4510
0
    return NULL;
4511
0
  m->next = NULL;
4512
0
  m->p_type = PT_LOAD;
4513
0
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4514
0
    m->sections[i - from] = *hdrpp;
4515
0
  m->count = to - from;
4516
4517
0
  if (from == 0 && phdr)
4518
0
    {
4519
      /* Include the headers in the first PT_LOAD segment.  */
4520
0
      m->includes_filehdr = 1;
4521
0
      m->includes_phdrs = 1;
4522
0
    }
4523
4524
0
  return m;
4525
0
}
4526
4527
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
4528
   on failure.  */
4529
4530
struct elf_segment_map *
4531
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4532
0
{
4533
0
  struct elf_segment_map *m;
4534
4535
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
4536
0
               sizeof (struct elf_segment_map));
4537
0
  if (m == NULL)
4538
0
    return NULL;
4539
0
  m->next = NULL;
4540
0
  m->p_type = PT_DYNAMIC;
4541
0
  m->count = 1;
4542
0
  m->sections[0] = dynsec;
4543
4544
0
  return m;
4545
0
}
4546
4547
/* Possibly add or remove segments from the segment map.  */
4548
4549
static bool
4550
elf_modify_segment_map (bfd *abfd,
4551
      struct bfd_link_info *info,
4552
      bool remove_empty_load)
4553
0
{
4554
0
  struct elf_segment_map **m;
4555
0
  const struct elf_backend_data *bed;
4556
4557
  /* The placement algorithm assumes that non allocated sections are
4558
     not in PT_LOAD segments.  We ensure this here by removing such
4559
     sections from the segment map.  We also remove excluded
4560
     sections.  Finally, any PT_LOAD segment without sections is
4561
     removed.  */
4562
0
  m = &elf_seg_map (abfd);
4563
0
  while (*m)
4564
0
    {
4565
0
      unsigned int i, new_count;
4566
4567
0
      for (new_count = 0, i = 0; i < (*m)->count; i++)
4568
0
  {
4569
0
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4570
0
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4571
0
      || (*m)->p_type != PT_LOAD))
4572
0
      {
4573
0
        (*m)->sections[new_count] = (*m)->sections[i];
4574
0
        new_count++;
4575
0
      }
4576
0
  }
4577
0
      (*m)->count = new_count;
4578
4579
0
      if (remove_empty_load
4580
0
    && (*m)->p_type == PT_LOAD
4581
0
    && (*m)->count == 0
4582
0
    && !(*m)->includes_phdrs)
4583
0
  *m = (*m)->next;
4584
0
      else
4585
0
  m = &(*m)->next;
4586
0
    }
4587
4588
0
  bed = get_elf_backend_data (abfd);
4589
0
  if (bed->elf_backend_modify_segment_map != NULL)
4590
0
    {
4591
0
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4592
0
  return false;
4593
0
    }
4594
4595
0
  return true;
4596
0
}
4597
4598
#define IS_TBSS(s) \
4599
0
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4600
4601
/* Set up a mapping from BFD sections to program segments.  Update
4602
   NEED_LAYOUT if the section layout is changed.  */
4603
4604
bool
4605
_bfd_elf_map_sections_to_segments (bfd *abfd,
4606
           struct bfd_link_info *info,
4607
           bool *need_layout)
4608
0
{
4609
0
  unsigned int count;
4610
0
  struct elf_segment_map *m;
4611
0
  asection **sections = NULL;
4612
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4613
0
  bool no_user_phdrs;
4614
4615
0
  no_user_phdrs = elf_seg_map (abfd) == NULL;
4616
4617
0
  if (info != NULL)
4618
0
    {
4619
0
      info->user_phdrs = !no_user_phdrs;
4620
4621
      /* Size the relative relocations if DT_RELR is enabled.  */
4622
0
      if (info->enable_dt_relr
4623
0
    && need_layout != NULL
4624
0
    && bed->size_relative_relocs
4625
0
    && !bed->size_relative_relocs (info, need_layout))
4626
0
  info->callbacks->einfo
4627
0
    (_("%F%P: failed to size relative relocations\n"));
4628
0
    }
4629
4630
0
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4631
0
    {
4632
0
      asection *s;
4633
0
      unsigned int i;
4634
0
      struct elf_segment_map *mfirst;
4635
0
      struct elf_segment_map **pm;
4636
0
      asection *last_hdr;
4637
0
      bfd_vma last_size;
4638
0
      unsigned int hdr_index;
4639
0
      bfd_vma maxpagesize;
4640
0
      asection **hdrpp;
4641
0
      bool phdr_in_segment;
4642
0
      bool writable;
4643
0
      bool executable;
4644
0
      unsigned int tls_count = 0;
4645
0
      asection *first_tls = NULL;
4646
0
      asection *first_mbind = NULL;
4647
0
      asection *dynsec, *eh_frame_hdr;
4648
0
      asection *sframe;
4649
0
      size_t amt;
4650
0
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
4651
0
      bfd_size_type phdr_size;  /* Octets/bytes.  */
4652
0
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4653
4654
      /* Select the allocated sections, and sort them.  */
4655
4656
0
      amt = bfd_count_sections (abfd) * sizeof (asection *);
4657
0
      sections = (asection **) bfd_malloc (amt);
4658
0
      if (sections == NULL)
4659
0
  goto error_return;
4660
4661
      /* Calculate top address, avoiding undefined behaviour of shift
4662
   left operator when shift count is equal to size of type
4663
   being shifted.  */
4664
0
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4665
0
      addr_mask = (addr_mask << 1) + 1;
4666
4667
0
      i = 0;
4668
0
      for (s = abfd->sections; s != NULL; s = s->next)
4669
0
  {
4670
0
    if ((s->flags & SEC_ALLOC) != 0)
4671
0
      {
4672
        /* target_index is unused until bfd_elf_final_link
4673
     starts output of section symbols.  Use it to make
4674
     qsort stable.  */
4675
0
        s->target_index = i;
4676
0
        sections[i] = s;
4677
0
        ++i;
4678
        /* A wrapping section potentially clashes with header.  */
4679
0
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4680
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
4681
0
      }
4682
0
  }
4683
0
      BFD_ASSERT (i <= bfd_count_sections (abfd));
4684
0
      count = i;
4685
4686
0
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4687
4688
0
      phdr_size = elf_program_header_size (abfd);
4689
0
      if (phdr_size == (bfd_size_type) -1)
4690
0
  phdr_size = get_program_header_size (abfd, info);
4691
0
      phdr_size += bed->s->sizeof_ehdr;
4692
      /* phdr_size is compared to LMA values which are in bytes.  */
4693
0
      phdr_size /= opb;
4694
0
      if (info != NULL)
4695
0
  maxpagesize = info->maxpagesize;
4696
0
      else
4697
0
  maxpagesize = bed->maxpagesize;
4698
0
      if (maxpagesize == 0)
4699
0
  maxpagesize = 1;
4700
0
      phdr_in_segment = info != NULL && info->load_phdrs;
4701
0
      if (count != 0
4702
0
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4703
0
        >= (phdr_size & (maxpagesize - 1))))
4704
  /* For compatibility with old scripts that may not be using
4705
     SIZEOF_HEADERS, add headers when it looks like space has
4706
     been left for them.  */
4707
0
  phdr_in_segment = true;
4708
4709
      /* Build the mapping.  */
4710
0
      mfirst = NULL;
4711
0
      pm = &mfirst;
4712
4713
      /* If we have a .interp section, then create a PT_PHDR segment for
4714
   the program headers and a PT_INTERP segment for the .interp
4715
   section.  */
4716
0
      s = bfd_get_section_by_name (abfd, ".interp");
4717
0
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4718
0
  {
4719
0
    amt = sizeof (struct elf_segment_map);
4720
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4721
0
    if (m == NULL)
4722
0
      goto error_return;
4723
0
    m->next = NULL;
4724
0
    m->p_type = PT_PHDR;
4725
0
    m->p_flags = PF_R;
4726
0
    m->p_flags_valid = 1;
4727
0
    m->includes_phdrs = 1;
4728
0
    phdr_in_segment = true;
4729
0
    *pm = m;
4730
0
    pm = &m->next;
4731
4732
0
    amt = sizeof (struct elf_segment_map);
4733
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4734
0
    if (m == NULL)
4735
0
      goto error_return;
4736
0
    m->next = NULL;
4737
0
    m->p_type = PT_INTERP;
4738
0
    m->count = 1;
4739
0
    m->sections[0] = s;
4740
4741
0
    *pm = m;
4742
0
    pm = &m->next;
4743
0
  }
4744
4745
      /* Look through the sections.  We put sections in the same program
4746
   segment when the start of the second section can be placed within
4747
   a few bytes of the end of the first section.  */
4748
0
      last_hdr = NULL;
4749
0
      last_size = 0;
4750
0
      hdr_index = 0;
4751
0
      writable = false;
4752
0
      executable = false;
4753
0
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4754
0
      if (dynsec != NULL
4755
0
    && (dynsec->flags & SEC_LOAD) == 0)
4756
0
  dynsec = NULL;
4757
4758
0
      if ((abfd->flags & D_PAGED) == 0)
4759
0
  phdr_in_segment = false;
4760
4761
      /* Deal with -Ttext or something similar such that the first section
4762
   is not adjacent to the program headers.  This is an
4763
   approximation, since at this point we don't know exactly how many
4764
   program headers we will need.  */
4765
0
      if (phdr_in_segment && count > 0)
4766
0
  {
4767
0
    bfd_vma phdr_lma;  /* Bytes.  */
4768
0
    bool separate_phdr = false;
4769
4770
0
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4771
0
    if (info != NULL
4772
0
        && info->separate_code
4773
0
        && (sections[0]->flags & SEC_CODE) != 0)
4774
0
      {
4775
        /* If data sections should be separate from code and
4776
     thus not executable, and the first section is
4777
     executable then put the file and program headers in
4778
     their own PT_LOAD.  */
4779
0
        separate_phdr = true;
4780
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4781
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
4782
0
    {
4783
      /* The file and program headers are currently on the
4784
         same page as the first section.  Put them on the
4785
         previous page if we can.  */
4786
0
      if (phdr_lma >= maxpagesize)
4787
0
        phdr_lma -= maxpagesize;
4788
0
      else
4789
0
        separate_phdr = false;
4790
0
    }
4791
0
      }
4792
0
    if ((sections[0]->lma & addr_mask) < phdr_lma
4793
0
        || (sections[0]->lma & addr_mask) < phdr_size)
4794
      /* If file and program headers would be placed at the end
4795
         of memory then it's probably better to omit them.  */
4796
0
      phdr_in_segment = false;
4797
0
    else if (phdr_lma < wrap_to)
4798
      /* If a section wraps around to where we'll be placing
4799
         file and program headers, then the headers will be
4800
         overwritten.  */
4801
0
      phdr_in_segment = false;
4802
0
    else if (separate_phdr)
4803
0
      {
4804
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4805
0
        if (m == NULL)
4806
0
    goto error_return;
4807
0
        m->p_paddr = phdr_lma * opb;
4808
0
        m->p_vaddr_offset
4809
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4810
0
        m->p_paddr_valid = 1;
4811
0
        *pm = m;
4812
0
        pm = &m->next;
4813
0
        phdr_in_segment = false;
4814
0
      }
4815
0
  }
4816
4817
0
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4818
0
  {
4819
0
    asection *hdr;
4820
0
    bool new_segment;
4821
4822
0
    hdr = *hdrpp;
4823
4824
    /* See if this section and the last one will fit in the same
4825
       segment.  */
4826
4827
0
    if (last_hdr == NULL)
4828
0
      {
4829
        /* If we don't have a segment yet, then we don't need a new
4830
     one (we build the last one after this loop).  */
4831
0
        new_segment = false;
4832
0
      }
4833
0
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4834
0
      {
4835
        /* If this section has a different relation between the
4836
     virtual address and the load address, then we need a new
4837
     segment.  */
4838
0
        new_segment = true;
4839
0
      }
4840
0
    else if (hdr->lma < last_hdr->lma + last_size
4841
0
       || last_hdr->lma + last_size < last_hdr->lma)
4842
0
      {
4843
        /* If this section has a load address that makes it overlap
4844
     the previous section, then we need a new segment.  */
4845
0
        new_segment = true;
4846
0
      }
4847
0
    else if ((abfd->flags & D_PAGED) != 0
4848
0
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4849
0
           == (hdr->lma & -maxpagesize)))
4850
0
      {
4851
        /* If we are demand paged then we can't map two disk
4852
     pages onto the same memory page.  */
4853
0
        new_segment = false;
4854
0
      }
4855
    /* In the next test we have to be careful when last_hdr->lma is close
4856
       to the end of the address space.  If the aligned address wraps
4857
       around to the start of the address space, then there are no more
4858
       pages left in memory and it is OK to assume that the current
4859
       section can be included in the current segment.  */
4860
0
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4861
0
        + maxpagesize > last_hdr->lma)
4862
0
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4863
0
           + maxpagesize <= hdr->lma))
4864
0
      {
4865
        /* If putting this section in this segment would force us to
4866
     skip a page in the segment, then we need a new segment.  */
4867
0
        new_segment = true;
4868
0
      }
4869
0
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4870
0
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4871
0
      {
4872
        /* We don't want to put a loaded section after a
4873
     nonloaded (ie. bss style) section in the same segment
4874
     as that will force the non-loaded section to be loaded.
4875
     Consider .tbss sections as loaded for this purpose.  */
4876
0
        new_segment = true;
4877
0
      }
4878
0
    else if ((abfd->flags & D_PAGED) == 0)
4879
0
      {
4880
        /* If the file is not demand paged, which means that we
4881
     don't require the sections to be correctly aligned in the
4882
     file, then there is no other reason for a new segment.  */
4883
0
        new_segment = false;
4884
0
      }
4885
0
    else if (info != NULL
4886
0
       && info->separate_code
4887
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
4888
0
      {
4889
0
        new_segment = true;
4890
0
      }
4891
0
    else if (! writable
4892
0
       && (hdr->flags & SEC_READONLY) == 0)
4893
0
      {
4894
        /* We don't want to put a writable section in a read only
4895
     segment.  */
4896
0
        new_segment = true;
4897
0
      }
4898
0
    else
4899
0
      {
4900
        /* Otherwise, we can use the same segment.  */
4901
0
        new_segment = false;
4902
0
      }
4903
4904
    /* Allow interested parties a chance to override our decision.  */
4905
0
    if (last_hdr != NULL
4906
0
        && info != NULL
4907
0
        && info->callbacks->override_segment_assignment != NULL)
4908
0
      new_segment
4909
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
4910
0
                    last_hdr,
4911
0
                    new_segment);
4912
4913
0
    if (! new_segment)
4914
0
      {
4915
0
        if ((hdr->flags & SEC_READONLY) == 0)
4916
0
    writable = true;
4917
0
        if ((hdr->flags & SEC_CODE) != 0)
4918
0
    executable = true;
4919
0
        last_hdr = hdr;
4920
        /* .tbss sections effectively have zero size.  */
4921
0
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4922
0
        continue;
4923
0
      }
4924
4925
    /* We need a new program segment.  We must create a new program
4926
       header holding all the sections from hdr_index until hdr.  */
4927
4928
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4929
0
    if (m == NULL)
4930
0
      goto error_return;
4931
4932
0
    *pm = m;
4933
0
    pm = &m->next;
4934
4935
0
    if ((hdr->flags & SEC_READONLY) == 0)
4936
0
      writable = true;
4937
0
    else
4938
0
      writable = false;
4939
4940
0
    if ((hdr->flags & SEC_CODE) == 0)
4941
0
      executable = false;
4942
0
    else
4943
0
      executable = true;
4944
4945
0
    last_hdr = hdr;
4946
    /* .tbss sections effectively have zero size.  */
4947
0
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4948
0
    hdr_index = i;
4949
0
    phdr_in_segment = false;
4950
0
  }
4951
4952
      /* Create a final PT_LOAD program segment, but not if it's just
4953
   for .tbss.  */
4954
0
      if (last_hdr != NULL
4955
0
    && (i - hdr_index != 1
4956
0
        || !IS_TBSS (last_hdr)))
4957
0
  {
4958
0
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4959
0
    if (m == NULL)
4960
0
      goto error_return;
4961
4962
0
    *pm = m;
4963
0
    pm = &m->next;
4964
0
  }
4965
4966
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
4967
0
      if (dynsec != NULL)
4968
0
  {
4969
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4970
0
    if (m == NULL)
4971
0
      goto error_return;
4972
0
    *pm = m;
4973
0
    pm = &m->next;
4974
0
  }
4975
4976
      /* For each batch of consecutive loadable SHT_NOTE  sections,
4977
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
4978
   because if we link together nonloadable .note sections and
4979
   loadable .note sections, we will generate two .note sections
4980
   in the output file.  */
4981
0
      for (s = abfd->sections; s != NULL; s = s->next)
4982
0
  {
4983
0
    if ((s->flags & SEC_LOAD) != 0
4984
0
        && elf_section_type (s) == SHT_NOTE)
4985
0
      {
4986
0
        asection *s2;
4987
0
        unsigned int alignment_power = s->alignment_power;
4988
4989
0
        count = 1;
4990
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
4991
0
    {
4992
0
      if (s2->next->alignment_power == alignment_power
4993
0
          && (s2->next->flags & SEC_LOAD) != 0
4994
0
          && elf_section_type (s2->next) == SHT_NOTE
4995
0
          && align_power (s2->lma + s2->size / opb,
4996
0
              alignment_power)
4997
0
          == s2->next->lma)
4998
0
        count++;
4999
0
      else
5000
0
        break;
5001
0
    }
5002
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5003
0
        amt += count * sizeof (asection *);
5004
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5005
0
        if (m == NULL)
5006
0
    goto error_return;
5007
0
        m->next = NULL;
5008
0
        m->p_type = PT_NOTE;
5009
0
        m->count = count;
5010
0
        while (count > 1)
5011
0
    {
5012
0
      m->sections[m->count - count--] = s;
5013
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5014
0
      s = s->next;
5015
0
    }
5016
0
        m->sections[m->count - 1] = s;
5017
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5018
0
        *pm = m;
5019
0
        pm = &m->next;
5020
0
      }
5021
0
    if (s->flags & SEC_THREAD_LOCAL)
5022
0
      {
5023
0
        if (! tls_count)
5024
0
    first_tls = s;
5025
0
        tls_count++;
5026
0
      }
5027
0
    if (first_mbind == NULL
5028
0
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5029
0
      first_mbind = s;
5030
0
  }
5031
5032
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5033
0
      if (tls_count > 0)
5034
0
  {
5035
0
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5036
0
    amt += tls_count * sizeof (asection *);
5037
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5038
0
    if (m == NULL)
5039
0
      goto error_return;
5040
0
    m->next = NULL;
5041
0
    m->p_type = PT_TLS;
5042
0
    m->count = tls_count;
5043
    /* Mandated PF_R.  */
5044
0
    m->p_flags = PF_R;
5045
0
    m->p_flags_valid = 1;
5046
0
    s = first_tls;
5047
0
    for (i = 0; i < tls_count; ++i)
5048
0
      {
5049
0
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5050
0
    {
5051
0
      _bfd_error_handler
5052
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5053
0
      s = first_tls;
5054
0
      i = 0;
5055
0
      while (i < tls_count)
5056
0
        {
5057
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5058
0
      {
5059
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5060
0
        i++;
5061
0
      }
5062
0
          else
5063
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5064
0
          s = s->next;
5065
0
        }
5066
0
      bfd_set_error (bfd_error_bad_value);
5067
0
      goto error_return;
5068
0
    }
5069
0
        m->sections[i] = s;
5070
0
        s = s->next;
5071
0
      }
5072
5073
0
    *pm = m;
5074
0
    pm = &m->next;
5075
0
  }
5076
5077
0
      if (first_mbind
5078
0
    && (abfd->flags & D_PAGED) != 0
5079
0
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5080
0
  for (s = first_mbind; s != NULL; s = s->next)
5081
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5082
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5083
0
      {
5084
        /* Mandated PF_R.  */
5085
0
        unsigned long p_flags = PF_R;
5086
0
        if ((s->flags & SEC_READONLY) == 0)
5087
0
    p_flags |= PF_W;
5088
0
        if ((s->flags & SEC_CODE) != 0)
5089
0
    p_flags |= PF_X;
5090
5091
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5092
0
        m = bfd_zalloc (abfd, amt);
5093
0
        if (m == NULL)
5094
0
    goto error_return;
5095
0
        m->next = NULL;
5096
0
        m->p_type = (PT_GNU_MBIND_LO
5097
0
         + elf_section_data (s)->this_hdr.sh_info);
5098
0
        m->count = 1;
5099
0
        m->p_flags_valid = 1;
5100
0
        m->sections[0] = s;
5101
0
        m->p_flags = p_flags;
5102
5103
0
        *pm = m;
5104
0
        pm = &m->next;
5105
0
      }
5106
5107
0
      s = bfd_get_section_by_name (abfd,
5108
0
           NOTE_GNU_PROPERTY_SECTION_NAME);
5109
0
      if (s != NULL && s->size != 0)
5110
0
  {
5111
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5112
0
    m = bfd_zalloc (abfd, amt);
5113
0
    if (m == NULL)
5114
0
      goto error_return;
5115
0
    m->next = NULL;
5116
0
    m->p_type = PT_GNU_PROPERTY;
5117
0
    m->count = 1;
5118
0
    m->p_flags_valid = 1;
5119
0
    m->sections[0] = s;
5120
0
    m->p_flags = PF_R;
5121
0
    *pm = m;
5122
0
    pm = &m->next;
5123
0
  }
5124
5125
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5126
   segment.  */
5127
0
      eh_frame_hdr = elf_eh_frame_hdr (info);
5128
0
      if (eh_frame_hdr != NULL
5129
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5130
0
  {
5131
0
    amt = sizeof (struct elf_segment_map);
5132
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5133
0
    if (m == NULL)
5134
0
      goto error_return;
5135
0
    m->next = NULL;
5136
0
    m->p_type = PT_GNU_EH_FRAME;
5137
0
    m->count = 1;
5138
0
    m->sections[0] = eh_frame_hdr->output_section;
5139
5140
0
    *pm = m;
5141
0
    pm = &m->next;
5142
0
  }
5143
5144
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5145
   segment.  */
5146
0
      sframe = elf_sframe (abfd);
5147
0
      if (sframe != NULL
5148
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5149
0
    && sframe->size != 0)
5150
0
  {
5151
0
    amt = sizeof (struct elf_segment_map);
5152
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5153
0
    if (m == NULL)
5154
0
      goto error_return;
5155
0
    m->next = NULL;
5156
0
    m->p_type = PT_GNU_SFRAME;
5157
0
    m->count = 1;
5158
0
    m->sections[0] = sframe->output_section;
5159
5160
0
    *pm = m;
5161
0
    pm = &m->next;
5162
0
  }
5163
5164
0
      if (elf_stack_flags (abfd))
5165
0
  {
5166
0
    amt = sizeof (struct elf_segment_map);
5167
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5168
0
    if (m == NULL)
5169
0
      goto error_return;
5170
0
    m->next = NULL;
5171
0
    m->p_type = PT_GNU_STACK;
5172
0
    m->p_flags = elf_stack_flags (abfd);
5173
0
    m->p_align = bed->stack_align;
5174
0
    m->p_flags_valid = 1;
5175
0
    m->p_align_valid = m->p_align != 0;
5176
0
    if (info->stacksize > 0)
5177
0
      {
5178
0
        m->p_size = info->stacksize;
5179
0
        m->p_size_valid = 1;
5180
0
      }
5181
5182
0
    *pm = m;
5183
0
    pm = &m->next;
5184
0
  }
5185
5186
0
      if (info != NULL && info->relro)
5187
0
  {
5188
0
    for (m = mfirst; m != NULL; m = m->next)
5189
0
      {
5190
0
        if (m->p_type == PT_LOAD
5191
0
      && m->count != 0
5192
0
      && m->sections[0]->vma >= info->relro_start
5193
0
      && m->sections[0]->vma < info->relro_end)
5194
0
    {
5195
0
      i = m->count;
5196
0
      while (--i != (unsigned) -1)
5197
0
        {
5198
0
          if (m->sections[i]->size > 0
5199
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5200
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5201
0
      break;
5202
0
        }
5203
5204
0
      if (i != (unsigned) -1)
5205
0
        break;
5206
0
    }
5207
0
      }
5208
5209
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5210
0
    if (m != NULL)
5211
0
      {
5212
0
        amt = sizeof (struct elf_segment_map);
5213
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5214
0
        if (m == NULL)
5215
0
    goto error_return;
5216
0
        m->next = NULL;
5217
0
        m->p_type = PT_GNU_RELRO;
5218
0
        *pm = m;
5219
0
        pm = &m->next;
5220
0
      }
5221
0
  }
5222
5223
0
      free (sections);
5224
0
      elf_seg_map (abfd) = mfirst;
5225
0
    }
5226
5227
0
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5228
0
    return false;
5229
5230
0
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5231
0
    ++count;
5232
0
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5233
5234
0
  return true;
5235
5236
0
 error_return:
5237
0
  free (sections);
5238
0
  return false;
5239
0
}
5240
5241
/* Sort sections by address.  */
5242
5243
static int
5244
elf_sort_sections (const void *arg1, const void *arg2)
5245
0
{
5246
0
  const asection *sec1 = *(const asection **) arg1;
5247
0
  const asection *sec2 = *(const asection **) arg2;
5248
0
  bfd_size_type size1, size2;
5249
5250
  /* Sort by LMA first, since this is the address used to
5251
     place the section into a segment.  */
5252
0
  if (sec1->lma < sec2->lma)
5253
0
    return -1;
5254
0
  else if (sec1->lma > sec2->lma)
5255
0
    return 1;
5256
5257
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5258
     the same, and this will do nothing.  */
5259
0
  if (sec1->vma < sec2->vma)
5260
0
    return -1;
5261
0
  else if (sec1->vma > sec2->vma)
5262
0
    return 1;
5263
5264
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5265
5266
0
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5267
0
      && (x)->size != 0)
5268
5269
0
  if (TOEND (sec1))
5270
0
    {
5271
0
      if (!TOEND (sec2))
5272
0
  return 1;
5273
0
    }
5274
0
  else if (TOEND (sec2))
5275
0
    return -1;
5276
5277
0
#undef TOEND
5278
5279
  /* Sort by size, to put zero sized sections
5280
     before others at the same address.  */
5281
5282
0
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5283
0
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5284
5285
0
  if (size1 < size2)
5286
0
    return -1;
5287
0
  if (size1 > size2)
5288
0
    return 1;
5289
5290
0
  return sec1->target_index - sec2->target_index;
5291
0
}
5292
5293
/* This qsort comparison functions sorts PT_LOAD segments first and
5294
   by p_paddr, for assign_file_positions_for_load_sections.  */
5295
5296
static int
5297
elf_sort_segments (const void *arg1, const void *arg2)
5298
0
{
5299
0
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5300
0
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5301
5302
0
  if (m1->p_type != m2->p_type)
5303
0
    {
5304
0
      if (m1->p_type == PT_NULL)
5305
0
  return 1;
5306
0
      if (m2->p_type == PT_NULL)
5307
0
  return -1;
5308
0
      return m1->p_type < m2->p_type ? -1 : 1;
5309
0
    }
5310
0
  if (m1->includes_filehdr != m2->includes_filehdr)
5311
0
    return m1->includes_filehdr ? -1 : 1;
5312
0
  if (m1->no_sort_lma != m2->no_sort_lma)
5313
0
    return m1->no_sort_lma ? -1 : 1;
5314
0
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5315
0
    {
5316
0
      bfd_vma lma1, lma2;  /* Octets.  */
5317
0
      lma1 = 0;
5318
0
      if (m1->p_paddr_valid)
5319
0
  lma1 = m1->p_paddr;
5320
0
      else if (m1->count != 0)
5321
0
  {
5322
0
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5323
0
              m1->sections[0]);
5324
0
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5325
0
  }
5326
0
      lma2 = 0;
5327
0
      if (m2->p_paddr_valid)
5328
0
  lma2 = m2->p_paddr;
5329
0
      else if (m2->count != 0)
5330
0
  {
5331
0
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5332
0
              m2->sections[0]);
5333
0
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5334
0
  }
5335
0
      if (lma1 != lma2)
5336
0
  return lma1 < lma2 ? -1 : 1;
5337
0
    }
5338
0
  if (m1->idx != m2->idx)
5339
0
    return m1->idx < m2->idx ? -1 : 1;
5340
0
  return 0;
5341
0
}
5342
5343
/* Ian Lance Taylor writes:
5344
5345
   We shouldn't be using % with a negative signed number.  That's just
5346
   not good.  We have to make sure either that the number is not
5347
   negative, or that the number has an unsigned type.  When the types
5348
   are all the same size they wind up as unsigned.  When file_ptr is a
5349
   larger signed type, the arithmetic winds up as signed long long,
5350
   which is wrong.
5351
5352
   What we're trying to say here is something like ``increase OFF by
5353
   the least amount that will cause it to be equal to the VMA modulo
5354
   the page size.''  */
5355
/* In other words, something like:
5356
5357
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5358
   off_offset = off % bed->maxpagesize;
5359
   if (vma_offset < off_offset)
5360
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5361
   else
5362
     adjustment = vma_offset - off_offset;
5363
5364
   which can be collapsed into the expression below.  */
5365
5366
static file_ptr
5367
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5368
0
{
5369
  /* PR binutils/16199: Handle an alignment of zero.  */
5370
0
  if (maxpagesize == 0)
5371
0
    maxpagesize = 1;
5372
0
  return ((vma - off) % maxpagesize);
5373
0
}
5374
5375
static void
5376
print_segment_map (const struct elf_segment_map *m)
5377
0
{
5378
0
  unsigned int j;
5379
0
  const char *pt = get_segment_type (m->p_type);
5380
0
  char buf[32];
5381
5382
0
  if (pt == NULL)
5383
0
    {
5384
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5385
0
  sprintf (buf, "LOPROC+%7.7x",
5386
0
     (unsigned int) (m->p_type - PT_LOPROC));
5387
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5388
0
  sprintf (buf, "LOOS+%7.7x",
5389
0
     (unsigned int) (m->p_type - PT_LOOS));
5390
0
      else
5391
0
  snprintf (buf, sizeof (buf), "%8.8x",
5392
0
      (unsigned int) m->p_type);
5393
0
      pt = buf;
5394
0
    }
5395
0
  fflush (stdout);
5396
0
  fprintf (stderr, "%s:", pt);
5397
0
  for (j = 0; j < m->count; j++)
5398
0
    fprintf (stderr, " %s", m->sections [j]->name);
5399
0
  putc ('\n',stderr);
5400
0
  fflush (stderr);
5401
0
}
5402
5403
/* Assign file positions to the sections based on the mapping from
5404
   sections to segments.  This function also sets up some fields in
5405
   the file header.  */
5406
5407
static bool
5408
assign_file_positions_for_load_sections (bfd *abfd,
5409
           struct bfd_link_info *link_info)
5410
0
{
5411
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5412
0
  struct elf_segment_map *m;
5413
0
  struct elf_segment_map *phdr_load_seg;
5414
0
  Elf_Internal_Phdr *phdrs;
5415
0
  Elf_Internal_Phdr *p;
5416
0
  file_ptr off;  /* Octets.  */
5417
0
  bfd_size_type maxpagesize;
5418
0
  unsigned int alloc, actual;
5419
0
  unsigned int i, j;
5420
0
  struct elf_segment_map **sorted_seg_map;
5421
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5422
5423
0
  if (link_info == NULL
5424
0
      && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5425
0
    return false;
5426
5427
0
  alloc = 0;
5428
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5429
0
    m->idx = alloc++;
5430
5431
0
  if (alloc)
5432
0
    {
5433
0
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5434
0
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5435
0
    }
5436
0
  else
5437
0
    {
5438
      /* PR binutils/12467.  */
5439
0
      elf_elfheader (abfd)->e_phoff = 0;
5440
0
      elf_elfheader (abfd)->e_phentsize = 0;
5441
0
    }
5442
5443
0
  elf_elfheader (abfd)->e_phnum = alloc;
5444
5445
0
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5446
0
    {
5447
0
      actual = alloc;
5448
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5449
0
    }
5450
0
  else
5451
0
    {
5452
0
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5453
0
      BFD_ASSERT (elf_program_header_size (abfd)
5454
0
      == actual * bed->s->sizeof_phdr);
5455
0
      BFD_ASSERT (actual >= alloc);
5456
0
    }
5457
5458
0
  if (alloc == 0)
5459
0
    {
5460
0
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5461
0
      return true;
5462
0
    }
5463
5464
  /* We're writing the size in elf_program_header_size (abfd),
5465
     see assign_file_positions_except_relocs, so make sure we have
5466
     that amount allocated, with trailing space cleared.
5467
     The variable alloc contains the computed need, while
5468
     elf_program_header_size (abfd) contains the size used for the
5469
     layout.
5470
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5471
     where the layout is forced to according to a larger size in the
5472
     last iterations for the testcase ld-elf/header.  */
5473
0
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5474
0
           + alloc * sizeof (*sorted_seg_map)));
5475
0
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5476
0
  elf_tdata (abfd)->phdr = phdrs;
5477
0
  if (phdrs == NULL)
5478
0
    return false;
5479
5480
0
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5481
0
    {
5482
0
      sorted_seg_map[j] = m;
5483
      /* If elf_segment_map is not from map_sections_to_segments, the
5484
   sections may not be correctly ordered.  NOTE: sorting should
5485
   not be done to the PT_NOTE section of a corefile, which may
5486
   contain several pseudo-sections artificially created by bfd.
5487
   Sorting these pseudo-sections breaks things badly.  */
5488
0
      if (m->count > 1
5489
0
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5490
0
         && m->p_type == PT_NOTE))
5491
0
  {
5492
0
    for (i = 0; i < m->count; i++)
5493
0
      m->sections[i]->target_index = i;
5494
0
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5495
0
     elf_sort_sections);
5496
0
  }
5497
0
    }
5498
0
  if (alloc > 1)
5499
0
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5500
0
     elf_sort_segments);
5501
5502
0
  maxpagesize = 1;
5503
0
  if ((abfd->flags & D_PAGED) != 0)
5504
0
    {
5505
0
      if (link_info != NULL)
5506
0
  maxpagesize = link_info->maxpagesize;
5507
0
      else
5508
0
  maxpagesize = bed->maxpagesize;
5509
0
    }
5510
5511
  /* Sections must map to file offsets past the ELF file header.  */
5512
0
  off = bed->s->sizeof_ehdr;
5513
  /* And if one of the PT_LOAD headers doesn't include the program
5514
     headers then we'll be mapping program headers in the usual
5515
     position after the ELF file header.  */
5516
0
  phdr_load_seg = NULL;
5517
0
  for (j = 0; j < alloc; j++)
5518
0
    {
5519
0
      m = sorted_seg_map[j];
5520
0
      if (m->p_type != PT_LOAD)
5521
0
  break;
5522
0
      if (m->includes_phdrs)
5523
0
  {
5524
0
    phdr_load_seg = m;
5525
0
    break;
5526
0
  }
5527
0
    }
5528
0
  if (phdr_load_seg == NULL)
5529
0
    off += actual * bed->s->sizeof_phdr;
5530
5531
0
  for (j = 0; j < alloc; j++)
5532
0
    {
5533
0
      asection **secpp;
5534
0
      bfd_vma off_adjust;  /* Octets.  */
5535
0
      bool no_contents;
5536
0
      bfd_size_type p_align;
5537
0
      bool p_align_p;
5538
5539
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5540
   number of sections with contents contributing to both p_filesz
5541
   and p_memsz, followed by a number of sections with no contents
5542
   that just contribute to p_memsz.  In this loop, OFF tracks next
5543
   available file offset for PT_LOAD and PT_NOTE segments.  */
5544
0
      m = sorted_seg_map[j];
5545
0
      p = phdrs + m->idx;
5546
0
      p->p_type = m->p_type;
5547
0
      p->p_flags = m->p_flags;
5548
0
      p_align = bed->p_align;
5549
0
      p_align_p = false;
5550
5551
0
      if (m->count == 0)
5552
0
  p->p_vaddr = m->p_vaddr_offset * opb;
5553
0
      else
5554
0
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5555
5556
0
      if (m->p_paddr_valid)
5557
0
  p->p_paddr = m->p_paddr;
5558
0
      else if (m->count == 0)
5559
0
  p->p_paddr = 0;
5560
0
      else
5561
0
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5562
5563
0
      if (p->p_type == PT_LOAD
5564
0
    && (abfd->flags & D_PAGED) != 0)
5565
0
  {
5566
    /* p_align in demand paged PT_LOAD segments effectively stores
5567
       the maximum page size.  When copying an executable with
5568
       objcopy, we set m->p_align from the input file.  Use this
5569
       value for maxpagesize rather than bed->maxpagesize, which
5570
       may be different.  Note that we use maxpagesize for PT_TLS
5571
       segment alignment later in this function, so we are relying
5572
       on at least one PT_LOAD segment appearing before a PT_TLS
5573
       segment.  */
5574
0
    if (m->p_align_valid)
5575
0
      maxpagesize = m->p_align;
5576
0
    else if (p_align != 0
5577
0
       && (link_info == NULL
5578
0
           || !link_info->maxpagesize_is_set))
5579
      /* Set p_align to the default p_align value while laying
5580
         out segments aligning to the maximum page size or the
5581
         largest section alignment.  The run-time loader can
5582
         align segments to the default p_align value or the
5583
         maximum page size, depending on system page size.  */
5584
0
      p_align_p = true;
5585
5586
0
    p->p_align = maxpagesize;
5587
0
  }
5588
0
      else if (m->p_align_valid)
5589
0
  p->p_align = m->p_align;
5590
0
      else if (m->count == 0)
5591
0
  p->p_align = 1 << bed->s->log_file_align;
5592
5593
0
      if (m == phdr_load_seg)
5594
0
  {
5595
0
    if (!m->includes_filehdr)
5596
0
      p->p_offset = off;
5597
0
    off += actual * bed->s->sizeof_phdr;
5598
0
  }
5599
5600
0
      no_contents = false;
5601
0
      off_adjust = 0;
5602
0
      if (p->p_type == PT_LOAD
5603
0
    && m->count > 0)
5604
0
  {
5605
0
    bfd_size_type align;  /* Bytes.  */
5606
0
    unsigned int align_power = 0;
5607
5608
0
    if (m->p_align_valid)
5609
0
      align = p->p_align;
5610
0
    else
5611
0
      {
5612
0
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5613
0
    {
5614
0
      unsigned int secalign;
5615
5616
0
      secalign = bfd_section_alignment (*secpp);
5617
0
      if (secalign > align_power)
5618
0
        align_power = secalign;
5619
0
    }
5620
0
        align = (bfd_size_type) 1 << align_power;
5621
0
        if (align < maxpagesize)
5622
0
    {
5623
      /* If a section requires alignment higher than the
5624
         default p_align value, don't set p_align to the
5625
         default p_align value.  */
5626
0
      if (align > p_align)
5627
0
        p_align_p = false;
5628
0
      align = maxpagesize;
5629
0
    }
5630
0
        else
5631
0
    {
5632
      /* If a section requires alignment higher than the
5633
         maximum page size, set p_align to the section
5634
         alignment.  */
5635
0
      p_align_p = true;
5636
0
      p_align = align;
5637
0
    }
5638
0
      }
5639
5640
0
    for (i = 0; i < m->count; i++)
5641
0
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5642
        /* If we aren't making room for this section, then
5643
     it must be SHT_NOBITS regardless of what we've
5644
     set via struct bfd_elf_special_section.  */
5645
0
        elf_section_type (m->sections[i]) = SHT_NOBITS;
5646
5647
    /* Find out whether this segment contains any loadable
5648
       sections.  */
5649
0
    no_contents = true;
5650
0
    for (i = 0; i < m->count; i++)
5651
0
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5652
0
        {
5653
0
    no_contents = false;
5654
0
    break;
5655
0
        }
5656
5657
0
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5658
5659
    /* Broken hardware and/or kernel require that files do not
5660
       map the same page with different permissions on some hppa
5661
       processors.  */
5662
0
    if (j != 0
5663
0
        && (abfd->flags & D_PAGED) != 0
5664
0
        && bed->no_page_alias
5665
0
        && (off & (maxpagesize - 1)) != 0
5666
0
        && ((off & -maxpagesize)
5667
0
      == ((off + off_adjust) & -maxpagesize)))
5668
0
      off_adjust += maxpagesize;
5669
0
    off += off_adjust;
5670
0
    if (no_contents)
5671
0
      {
5672
        /* We shouldn't need to align the segment on disk since
5673
     the segment doesn't need file space, but the gABI
5674
     arguably requires the alignment and glibc ld.so
5675
     checks it.  So to comply with the alignment
5676
     requirement but not waste file space, we adjust
5677
     p_offset for just this segment.  (OFF_ADJUST is
5678
     subtracted from OFF later.)  This may put p_offset
5679
     past the end of file, but that shouldn't matter.  */
5680
0
      }
5681
0
    else
5682
0
      off_adjust = 0;
5683
0
  }
5684
      /* Make sure the .dynamic section is the first section in the
5685
   PT_DYNAMIC segment.  */
5686
0
      else if (p->p_type == PT_DYNAMIC
5687
0
         && m->count > 1
5688
0
         && strcmp (m->sections[0]->name, ".dynamic") != 0)
5689
0
  {
5690
0
    _bfd_error_handler
5691
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
5692
0
         " is not the .dynamic section"),
5693
0
       abfd);
5694
0
    bfd_set_error (bfd_error_bad_value);
5695
0
    return false;
5696
0
  }
5697
      /* Set the note section type to SHT_NOTE.  */
5698
0
      else if (p->p_type == PT_NOTE)
5699
0
  for (i = 0; i < m->count; i++)
5700
0
    elf_section_type (m->sections[i]) = SHT_NOTE;
5701
5702
0
      if (m->includes_filehdr)
5703
0
  {
5704
0
    if (!m->p_flags_valid)
5705
0
      p->p_flags |= PF_R;
5706
0
    p->p_filesz = bed->s->sizeof_ehdr;
5707
0
    p->p_memsz = bed->s->sizeof_ehdr;
5708
0
    if (p->p_type == PT_LOAD)
5709
0
      {
5710
0
        if (m->count > 0)
5711
0
    {
5712
0
      if (p->p_vaddr < (bfd_vma) off
5713
0
          || (!m->p_paddr_valid
5714
0
        && p->p_paddr < (bfd_vma) off))
5715
0
        {
5716
0
          _bfd_error_handler
5717
0
      (_("%pB: not enough room for program headers,"
5718
0
         " try linking with -N"),
5719
0
       abfd);
5720
0
          bfd_set_error (bfd_error_bad_value);
5721
0
          return false;
5722
0
        }
5723
0
      p->p_vaddr -= off;
5724
0
      if (!m->p_paddr_valid)
5725
0
        p->p_paddr -= off;
5726
0
    }
5727
0
      }
5728
0
    else if (sorted_seg_map[0]->includes_filehdr)
5729
0
      {
5730
0
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5731
0
        p->p_vaddr = filehdr->p_vaddr;
5732
0
        if (!m->p_paddr_valid)
5733
0
    p->p_paddr = filehdr->p_paddr;
5734
0
      }
5735
0
  }
5736
5737
0
      if (m->includes_phdrs)
5738
0
  {
5739
0
    if (!m->p_flags_valid)
5740
0
      p->p_flags |= PF_R;
5741
0
    p->p_filesz += actual * bed->s->sizeof_phdr;
5742
0
    p->p_memsz += actual * bed->s->sizeof_phdr;
5743
0
    if (!m->includes_filehdr)
5744
0
      {
5745
0
        if (p->p_type == PT_LOAD)
5746
0
    {
5747
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
5748
0
      if (m->count > 0)
5749
0
        {
5750
0
          p->p_vaddr -= off - p->p_offset;
5751
0
          if (!m->p_paddr_valid)
5752
0
      p->p_paddr -= off - p->p_offset;
5753
0
        }
5754
0
    }
5755
0
        else if (phdr_load_seg != NULL)
5756
0
    {
5757
0
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5758
0
      bfd_vma phdr_off = 0;  /* Octets.  */
5759
0
      if (phdr_load_seg->includes_filehdr)
5760
0
        phdr_off = bed->s->sizeof_ehdr;
5761
0
      p->p_vaddr = phdr->p_vaddr + phdr_off;
5762
0
      if (!m->p_paddr_valid)
5763
0
        p->p_paddr = phdr->p_paddr + phdr_off;
5764
0
      p->p_offset = phdr->p_offset + phdr_off;
5765
0
    }
5766
0
        else
5767
0
    p->p_offset = bed->s->sizeof_ehdr;
5768
0
      }
5769
0
  }
5770
5771
0
      if (p->p_type == PT_LOAD
5772
0
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5773
0
  {
5774
0
    if (!m->includes_filehdr && !m->includes_phdrs)
5775
0
      {
5776
0
        p->p_offset = off;
5777
0
        if (no_contents)
5778
0
    {
5779
      /* Put meaningless p_offset for PT_LOAD segments
5780
         without file contents somewhere within the first
5781
         page, in an attempt to not point past EOF.  */
5782
0
      bfd_size_type align = maxpagesize;
5783
0
      if (align < p->p_align)
5784
0
        align = p->p_align;
5785
0
      if (align < 1)
5786
0
        align = 1;
5787
0
      p->p_offset = off % align;
5788
0
    }
5789
0
      }
5790
0
    else
5791
0
      {
5792
0
        file_ptr adjust;  /* Octets.  */
5793
5794
0
        adjust = off - (p->p_offset + p->p_filesz);
5795
0
        if (!no_contents)
5796
0
    p->p_filesz += adjust;
5797
0
        p->p_memsz += adjust;
5798
0
      }
5799
0
  }
5800
5801
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5802
   maps.  Set filepos for sections in PT_LOAD segments, and in
5803
   core files, for sections in PT_NOTE segments.
5804
   assign_file_positions_for_non_load_sections will set filepos
5805
   for other sections and update p_filesz for other segments.  */
5806
0
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5807
0
  {
5808
0
    asection *sec;
5809
0
    bfd_size_type align;
5810
0
    Elf_Internal_Shdr *this_hdr;
5811
5812
0
    sec = *secpp;
5813
0
    this_hdr = &elf_section_data (sec)->this_hdr;
5814
0
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5815
5816
0
    if ((p->p_type == PT_LOAD
5817
0
         || p->p_type == PT_TLS)
5818
0
        && (this_hdr->sh_type != SHT_NOBITS
5819
0
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5820
0
          && ((this_hdr->sh_flags & SHF_TLS) == 0
5821
0
        || p->p_type == PT_TLS))))
5822
0
      {
5823
0
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
5824
0
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
5825
0
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
5826
0
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
5827
5828
0
        if (adjust != 0
5829
0
      && (s_start < p_end
5830
0
          || p_end < p_start))
5831
0
    {
5832
0
      _bfd_error_handler
5833
        /* xgettext:c-format */
5834
0
        (_("%pB: section %pA lma %#" PRIx64
5835
0
           " adjusted to %#" PRIx64),
5836
0
         abfd, sec, (uint64_t) s_start / opb,
5837
0
         (uint64_t) p_end / opb);
5838
0
      adjust = 0;
5839
0
      sec->lma = p_end / opb;
5840
0
    }
5841
0
        p->p_memsz += adjust;
5842
5843
0
        if (p->p_type == PT_LOAD)
5844
0
    {
5845
0
      if (this_hdr->sh_type != SHT_NOBITS)
5846
0
        {
5847
0
          off_adjust = 0;
5848
0
          if (p->p_filesz + adjust < p->p_memsz)
5849
0
      {
5850
        /* We have a PROGBITS section following NOBITS ones.
5851
           Allocate file space for the NOBITS section(s).
5852
           We don't need to write out the zeros, posix
5853
           fseek past the end of data already written
5854
           followed by a write at that location is
5855
           guaranteed to result in zeros being read
5856
           from the gap.  */
5857
0
        adjust = p->p_memsz - p->p_filesz;
5858
0
      }
5859
0
        }
5860
      /* We only adjust sh_offset in SHT_NOBITS sections
5861
         as would seem proper for their address when the
5862
         section is first in the segment.  sh_offset
5863
         doesn't really have any significance for
5864
         SHT_NOBITS anyway, apart from a notional position
5865
         relative to other sections.  Historically we
5866
         didn't bother with adjusting sh_offset and some
5867
         programs depend on it not being adjusted.  See
5868
         pr12921 and pr25662.  */
5869
0
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5870
0
        {
5871
0
          off += adjust;
5872
0
          if (this_hdr->sh_type == SHT_NOBITS)
5873
0
      off_adjust += adjust;
5874
0
        }
5875
0
    }
5876
0
        if (this_hdr->sh_type != SHT_NOBITS)
5877
0
    p->p_filesz += adjust;
5878
0
      }
5879
5880
0
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5881
0
      {
5882
        /* The section at i == 0 is the one that actually contains
5883
     everything.  */
5884
0
        if (i == 0)
5885
0
    {
5886
0
      this_hdr->sh_offset = sec->filepos = off;
5887
0
      off += this_hdr->sh_size;
5888
0
      p->p_filesz = this_hdr->sh_size;
5889
0
      p->p_memsz = 0;
5890
0
      p->p_align = 1;
5891
0
    }
5892
0
        else
5893
0
    {
5894
      /* The rest are fake sections that shouldn't be written.  */
5895
0
      sec->filepos = 0;
5896
0
      sec->size = 0;
5897
0
      sec->flags = 0;
5898
0
      continue;
5899
0
    }
5900
0
      }
5901
0
    else
5902
0
      {
5903
0
        if (p->p_type == PT_LOAD)
5904
0
    {
5905
0
      this_hdr->sh_offset = sec->filepos = off;
5906
0
      if (this_hdr->sh_type != SHT_NOBITS)
5907
0
        off += this_hdr->sh_size;
5908
0
    }
5909
0
        else if (this_hdr->sh_type == SHT_NOBITS
5910
0
           && (this_hdr->sh_flags & SHF_TLS) != 0
5911
0
           && this_hdr->sh_offset == 0)
5912
0
    {
5913
      /* This is a .tbss section that didn't get a PT_LOAD.
5914
         (See _bfd_elf_map_sections_to_segments "Create a
5915
         final PT_LOAD".)  Set sh_offset to the value it
5916
         would have if we had created a zero p_filesz and
5917
         p_memsz PT_LOAD header for the section.  This
5918
         also makes the PT_TLS header have the same
5919
         p_offset value.  */
5920
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5921
0
                off, align);
5922
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
5923
0
    }
5924
5925
0
        if (this_hdr->sh_type != SHT_NOBITS)
5926
0
    {
5927
0
      p->p_filesz += this_hdr->sh_size;
5928
      /* A load section without SHF_ALLOC is something like
5929
         a note section in a PT_NOTE segment.  These take
5930
         file space but are not loaded into memory.  */
5931
0
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5932
0
        p->p_memsz += this_hdr->sh_size;
5933
0
    }
5934
0
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5935
0
    {
5936
0
      if (p->p_type == PT_TLS)
5937
0
        p->p_memsz += this_hdr->sh_size;
5938
5939
      /* .tbss is special.  It doesn't contribute to p_memsz of
5940
         normal segments.  */
5941
0
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5942
0
        p->p_memsz += this_hdr->sh_size;
5943
0
    }
5944
5945
0
        if (align > p->p_align
5946
0
      && !m->p_align_valid
5947
0
      && (p->p_type != PT_LOAD
5948
0
          || (abfd->flags & D_PAGED) == 0))
5949
0
    p->p_align = align;
5950
0
      }
5951
5952
0
    if (!m->p_flags_valid)
5953
0
      {
5954
0
        p->p_flags |= PF_R;
5955
0
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5956
0
    p->p_flags |= PF_X;
5957
0
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5958
0
    p->p_flags |= PF_W;
5959
0
      }
5960
0
  }
5961
5962
0
      off -= off_adjust;
5963
5964
      /* PR ld/20815 - Check that the program header segment, if
5965
   present, will be loaded into memory.  */
5966
0
      if (p->p_type == PT_PHDR
5967
0
    && phdr_load_seg == NULL
5968
0
    && !(bed->elf_backend_allow_non_load_phdr != NULL
5969
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5970
0
  {
5971
    /* The fix for this error is usually to edit the linker script being
5972
       used and set up the program headers manually.  Either that or
5973
       leave room for the headers at the start of the SECTIONS.  */
5974
0
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5975
0
        " by LOAD segment"),
5976
0
            abfd);
5977
0
    if (link_info == NULL)
5978
0
      return false;
5979
    /* Arrange for the linker to exit with an error, deleting
5980
       the output file unless --noinhibit-exec is given.  */
5981
0
    link_info->callbacks->info ("%X");
5982
0
  }
5983
5984
      /* Check that all sections are in a PT_LOAD segment.
5985
   Don't check funky gdb generated core files.  */
5986
0
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5987
0
  {
5988
0
    bool check_vma = true;
5989
5990
0
    for (i = 1; i < m->count; i++)
5991
0
      if (m->sections[i]->vma == m->sections[i - 1]->vma
5992
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5993
0
               ->this_hdr), p) != 0
5994
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5995
0
               ->this_hdr), p) != 0)
5996
0
        {
5997
    /* Looks like we have overlays packed into the segment.  */
5998
0
    check_vma = false;
5999
0
    break;
6000
0
        }
6001
6002
0
    for (i = 0; i < m->count; i++)
6003
0
      {
6004
0
        Elf_Internal_Shdr *this_hdr;
6005
0
        asection *sec;
6006
6007
0
        sec = m->sections[i];
6008
0
        this_hdr = &(elf_section_data(sec)->this_hdr);
6009
0
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6010
0
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6011
0
    {
6012
0
      _bfd_error_handler
6013
        /* xgettext:c-format */
6014
0
        (_("%pB: section `%pA' can't be allocated in segment %d"),
6015
0
         abfd, sec, j);
6016
0
      print_segment_map (m);
6017
0
    }
6018
0
      }
6019
6020
0
    if (p_align_p)
6021
0
      p->p_align = p_align;
6022
0
  }
6023
0
    }
6024
6025
0
  elf_next_file_pos (abfd) = off;
6026
6027
0
  if (link_info != NULL
6028
0
      && phdr_load_seg != NULL
6029
0
      && phdr_load_seg->includes_filehdr)
6030
0
    {
6031
      /* There is a segment that contains both the file headers and the
6032
   program headers, so provide a symbol __ehdr_start pointing there.
6033
   A program can use this to examine itself robustly.  */
6034
6035
0
      struct elf_link_hash_entry *hash
6036
0
  = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6037
0
        false, false, true);
6038
      /* If the symbol was referenced and not defined, define it.  */
6039
0
      if (hash != NULL
6040
0
    && (hash->root.type == bfd_link_hash_new
6041
0
        || hash->root.type == bfd_link_hash_undefined
6042
0
        || hash->root.type == bfd_link_hash_undefweak
6043
0
        || hash->root.type == bfd_link_hash_common))
6044
0
  {
6045
0
    asection *s = NULL;
6046
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6047
6048
0
    if (phdr_load_seg->count != 0)
6049
      /* The segment contains sections, so use the first one.  */
6050
0
      s = phdr_load_seg->sections[0];
6051
0
    else
6052
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6053
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6054
0
        if (m->p_type == PT_LOAD && m->count != 0)
6055
0
    {
6056
0
      s = m->sections[0];
6057
0
      break;
6058
0
    }
6059
6060
0
    if (s != NULL)
6061
0
      {
6062
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6063
0
        hash->root.u.def.section = s;
6064
0
      }
6065
0
    else
6066
0
      {
6067
0
        hash->root.u.def.value = filehdr_vaddr;
6068
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6069
0
      }
6070
6071
0
    hash->root.type = bfd_link_hash_defined;
6072
0
    hash->def_regular = 1;
6073
0
    hash->non_elf = 0;
6074
0
  }
6075
0
    }
6076
6077
0
  return true;
6078
0
}
6079
6080
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6081
   is no defined method for detecting such files, so we have to
6082
   use heuristics instead.  */
6083
6084
bool
6085
is_debuginfo_file (bfd *abfd)
6086
0
{
6087
0
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6088
0
    return false;
6089
6090
0
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6091
0
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6092
0
  Elf_Internal_Shdr **headerp;
6093
6094
0
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6095
0
    {
6096
0
      Elf_Internal_Shdr *header = * headerp;
6097
6098
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6099
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6100
0
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6101
0
    && header->sh_type != SHT_NOBITS
6102
0
    && header->sh_type != SHT_NOTE)
6103
0
  return false;
6104
0
    }
6105
6106
0
  return true;
6107
0
}
6108
6109
/* Assign file positions for other sections, except for compressed debug
6110
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6111
6112
static bool
6113
assign_file_positions_for_non_load_sections (bfd *abfd,
6114
               struct bfd_link_info *link_info)
6115
0
{
6116
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6117
0
  Elf_Internal_Shdr **i_shdrpp;
6118
0
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6119
0
  Elf_Internal_Phdr *phdrs;
6120
0
  Elf_Internal_Phdr *p;
6121
0
  struct elf_segment_map *m;
6122
0
  file_ptr off;
6123
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6124
0
  bfd_vma maxpagesize;
6125
6126
0
  if (link_info != NULL)
6127
0
    maxpagesize = link_info->maxpagesize;
6128
0
  else
6129
0
    maxpagesize = bed->maxpagesize;
6130
0
  i_shdrpp = elf_elfsections (abfd);
6131
0
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6132
0
  off = elf_next_file_pos (abfd);
6133
0
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6134
0
    {
6135
0
      Elf_Internal_Shdr *hdr;
6136
0
      bfd_vma align;
6137
6138
0
      hdr = *hdrpp;
6139
0
      if (hdr->bfd_section != NULL
6140
0
    && (hdr->bfd_section->filepos != 0
6141
0
        || (hdr->sh_type == SHT_NOBITS
6142
0
      && hdr->contents == NULL)))
6143
0
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6144
0
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6145
0
  {
6146
0
    if (hdr->sh_size != 0
6147
        /* PR 24717 - debuginfo files are known to be not strictly
6148
     compliant with the ELF standard.  In particular they often
6149
     have .note.gnu.property sections that are outside of any
6150
     loadable segment.  This is not a problem for such files,
6151
     so do not warn about them.  */
6152
0
        && ! is_debuginfo_file (abfd))
6153
0
      _bfd_error_handler
6154
        /* xgettext:c-format */
6155
0
        (_("%pB: warning: allocated section `%s' not in segment"),
6156
0
         abfd,
6157
0
         (hdr->bfd_section == NULL
6158
0
    ? "*unknown*"
6159
0
    : hdr->bfd_section->name));
6160
    /* We don't need to page align empty sections.  */
6161
0
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6162
0
      align = maxpagesize;
6163
0
    else
6164
0
      align = hdr->sh_addralign & -hdr->sh_addralign;
6165
0
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6166
0
    off = _bfd_elf_assign_file_position_for_section (hdr, off,
6167
0
                 false);
6168
0
  }
6169
0
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6170
0
    && hdr->bfd_section == NULL)
6171
         /* We don't know the offset of these sections yet:
6172
      their size has not been decided.  */
6173
0
         || (abfd->is_linker_output
6174
0
       && hdr->bfd_section != NULL
6175
0
       && (hdr->sh_name == -1u
6176
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6177
0
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6178
0
         || (elf_symtab_shndx_list (abfd) != NULL
6179
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6180
0
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6181
0
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6182
0
  hdr->sh_offset = -1;
6183
0
      else
6184
0
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6185
0
    }
6186
0
  elf_next_file_pos (abfd) = off;
6187
6188
  /* Now that we have set the section file positions, we can set up
6189
     the file positions for the non PT_LOAD segments.  */
6190
0
  phdrs = elf_tdata (abfd)->phdr;
6191
0
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6192
0
    {
6193
0
      if (p->p_type == PT_GNU_RELRO)
6194
0
  {
6195
0
    bfd_vma start, end;  /* Bytes.  */
6196
0
    bool ok;
6197
6198
0
    if (link_info != NULL)
6199
0
      {
6200
        /* During linking the range of the RELRO segment is passed
6201
     in link_info.  Note that there may be padding between
6202
     relro_start and the first RELRO section.  */
6203
0
        start = link_info->relro_start;
6204
0
        end = link_info->relro_end;
6205
0
      }
6206
0
    else if (m->count != 0)
6207
0
      {
6208
0
        if (!m->p_size_valid)
6209
0
    abort ();
6210
0
        start = m->sections[0]->vma;
6211
0
        end = start + m->p_size / opb;
6212
0
      }
6213
0
    else
6214
0
      {
6215
0
        start = 0;
6216
0
        end = 0;
6217
0
      }
6218
6219
0
    ok = false;
6220
0
    if (start < end)
6221
0
      {
6222
0
        struct elf_segment_map *lm;
6223
0
        const Elf_Internal_Phdr *lp;
6224
0
        unsigned int i;
6225
6226
        /* Find a LOAD segment containing a section in the RELRO
6227
     segment.  */
6228
0
        for (lm = elf_seg_map (abfd), lp = phdrs;
6229
0
       lm != NULL;
6230
0
       lm = lm->next, lp++)
6231
0
    {
6232
0
      if (lp->p_type == PT_LOAD
6233
0
          && lm->count != 0
6234
0
          && (lm->sections[lm->count - 1]->vma
6235
0
        + (!IS_TBSS (lm->sections[lm->count - 1])
6236
0
           ? lm->sections[lm->count - 1]->size / opb
6237
0
           : 0)) > start
6238
0
          && lm->sections[0]->vma < end)
6239
0
        break;
6240
0
    }
6241
6242
0
        if (lm != NULL)
6243
0
    {
6244
      /* Find the section starting the RELRO segment.  */
6245
0
      for (i = 0; i < lm->count; i++)
6246
0
        {
6247
0
          asection *s = lm->sections[i];
6248
0
          if (s->vma >= start
6249
0
        && s->vma < end
6250
0
        && s->size != 0)
6251
0
      break;
6252
0
        }
6253
6254
0
      if (i < lm->count)
6255
0
        {
6256
0
          p->p_vaddr = lm->sections[i]->vma * opb;
6257
0
          p->p_paddr = lm->sections[i]->lma * opb;
6258
0
          p->p_offset = lm->sections[i]->filepos;
6259
0
          p->p_memsz = end * opb - p->p_vaddr;
6260
0
          p->p_filesz = p->p_memsz;
6261
6262
          /* The RELRO segment typically ends a few bytes
6263
       into .got.plt but other layouts are possible.
6264
       In cases where the end does not match any
6265
       loaded section (for instance is in file
6266
       padding), trim p_filesz back to correspond to
6267
       the end of loaded section contents.  */
6268
0
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6269
0
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6270
6271
          /* Preserve the alignment and flags if they are
6272
       valid.  The gold linker generates RW/4 for
6273
       the PT_GNU_RELRO section.  It is better for
6274
       objcopy/strip to honor these attributes
6275
       otherwise gdb will choke when using separate
6276
       debug files.  */
6277
0
          if (!m->p_align_valid)
6278
0
      p->p_align = 1;
6279
0
          if (!m->p_flags_valid)
6280
0
      p->p_flags = PF_R;
6281
0
          ok = true;
6282
0
        }
6283
0
    }
6284
0
      }
6285
6286
0
    if (!ok)
6287
0
      {
6288
0
        if (link_info != NULL)
6289
0
    _bfd_error_handler
6290
0
      (_("%pB: warning: unable to allocate any sections"
6291
0
         " to PT_GNU_RELRO segment"),
6292
0
       abfd);
6293
0
        memset (p, 0, sizeof *p);
6294
0
      }
6295
0
  }
6296
0
      else if (p->p_type == PT_GNU_STACK)
6297
0
  {
6298
0
    if (m->p_size_valid)
6299
0
      p->p_memsz = m->p_size;
6300
0
  }
6301
0
      else if (m->count != 0)
6302
0
  {
6303
0
    unsigned int i;
6304
6305
0
    if (p->p_type != PT_LOAD
6306
0
        && (p->p_type != PT_NOTE
6307
0
      || bfd_get_format (abfd) != bfd_core))
6308
0
      {
6309
        /* A user specified segment layout may include a PHDR
6310
     segment that overlaps with a LOAD segment...  */
6311
0
        if (p->p_type == PT_PHDR)
6312
0
    {
6313
0
      m->count = 0;
6314
0
      continue;
6315
0
    }
6316
6317
0
        if (m->includes_filehdr || m->includes_phdrs)
6318
0
    {
6319
      /* PR 17512: file: 2195325e.  */
6320
0
      _bfd_error_handler
6321
0
        (_("%pB: error: non-load segment %d includes file header "
6322
0
           "and/or program header"),
6323
0
         abfd, (int) (p - phdrs));
6324
0
      return false;
6325
0
    }
6326
6327
0
        p->p_filesz = 0;
6328
0
        p->p_offset = m->sections[0]->filepos;
6329
0
        for (i = m->count; i-- != 0;)
6330
0
    {
6331
0
      asection *sect = m->sections[i];
6332
0
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6333
0
      if (hdr->sh_type != SHT_NOBITS)
6334
0
        {
6335
0
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6336
          /* NB: p_memsz of the loadable PT_NOTE segment
6337
       should be the same as p_filesz.  */
6338
0
          if (p->p_type == PT_NOTE
6339
0
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6340
0
      p->p_memsz = p->p_filesz;
6341
0
          break;
6342
0
        }
6343
0
    }
6344
0
      }
6345
0
  }
6346
0
    }
6347
6348
0
  return true;
6349
0
}
6350
6351
static elf_section_list *
6352
find_section_in_list (unsigned int i, elf_section_list * list)
6353
0
{
6354
0
  for (;list != NULL; list = list->next)
6355
0
    if (list->ndx == i)
6356
0
      break;
6357
0
  return list;
6358
0
}
6359
6360
/* Work out the file positions of all the sections.  This is called by
6361
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6362
   VMAs must be known before this is called.
6363
6364
   Reloc sections come in two flavours: Those processed specially as
6365
   "side-channel" data attached to a section to which they apply, and
6366
   those that bfd doesn't process as relocations.  The latter sort are
6367
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6368
   consider the former sort here, unless they form part of the loadable
6369
   image.  Reloc sections not assigned here (and compressed debugging
6370
   sections and CTF sections which nothing else in the file can rely
6371
   upon) will be handled later by assign_file_positions_for_relocs.
6372
6373
   We also don't set the positions of the .symtab and .strtab here.  */
6374
6375
static bool
6376
assign_file_positions_except_relocs (bfd *abfd,
6377
             struct bfd_link_info *link_info)
6378
0
{
6379
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6380
0
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6381
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6382
0
  unsigned int alloc;
6383
6384
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6385
0
      && bfd_get_format (abfd) != bfd_core)
6386
0
    {
6387
0
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6388
0
      unsigned int num_sec = elf_numsections (abfd);
6389
0
      Elf_Internal_Shdr **hdrpp;
6390
0
      unsigned int i;
6391
0
      file_ptr off;
6392
6393
      /* Start after the ELF header.  */
6394
0
      off = i_ehdrp->e_ehsize;
6395
6396
      /* We are not creating an executable, which means that we are
6397
   not creating a program header, and that the actual order of
6398
   the sections in the file is unimportant.  */
6399
0
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6400
0
  {
6401
0
    Elf_Internal_Shdr *hdr;
6402
6403
0
    hdr = *hdrpp;
6404
0
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6405
0
         && hdr->bfd_section == NULL)
6406
        /* Do not assign offsets for these sections yet: we don't know
6407
     their sizes.  */
6408
0
        || (abfd->is_linker_output
6409
0
      && hdr->bfd_section != NULL
6410
0
      && (hdr->sh_name == -1u
6411
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6412
0
        || i == elf_onesymtab (abfd)
6413
0
        || (elf_symtab_shndx_list (abfd) != NULL
6414
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6415
0
        || i == elf_strtab_sec (abfd)
6416
0
        || i == elf_shstrtab_sec (abfd))
6417
0
      {
6418
0
        hdr->sh_offset = -1;
6419
0
      }
6420
0
    else
6421
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6422
0
  }
6423
6424
0
      elf_next_file_pos (abfd) = off;
6425
0
      elf_program_header_size (abfd) = 0;
6426
0
    }
6427
0
  else
6428
0
    {
6429
      /* Assign file positions for the loaded sections based on the
6430
   assignment of sections to segments.  */
6431
0
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6432
0
  return false;
6433
6434
      /* And for non-load sections.  */
6435
0
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6436
0
  return false;
6437
0
    }
6438
6439
0
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6440
0
    return false;
6441
6442
  /* Write out the program headers.  */
6443
0
  alloc = i_ehdrp->e_phnum;
6444
0
  if (alloc != 0)
6445
0
    {
6446
0
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6447
0
  {
6448
    /* Memory resident segments with non-zero size and RWX
6449
       permissions are a security risk, so we generate a warning
6450
       here if we are creating any.  */
6451
0
    unsigned int i;
6452
6453
0
    for (i = 0; i < alloc; i++)
6454
0
      {
6455
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6456
6457
0
        if (phdr->p_memsz == 0)
6458
0
    continue;
6459
6460
0
        if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6461
0
    _bfd_error_handler (_("warning: %pB has a TLS segment"
6462
0
              " with execute permission"),
6463
0
            abfd);
6464
0
        else if (phdr->p_type == PT_LOAD
6465
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6466
0
         == (PF_R | PF_W | PF_X)))
6467
0
    _bfd_error_handler (_("warning: %pB has a LOAD segment"
6468
0
              " with RWX permissions"),
6469
0
            abfd);
6470
0
      }
6471
0
  }
6472
6473
0
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6474
0
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6475
0
  return false;
6476
0
    }
6477
6478
0
  return true;
6479
0
}
6480
6481
bool
6482
_bfd_elf_init_file_header (bfd *abfd,
6483
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6484
0
{
6485
0
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
6486
0
  struct elf_strtab_hash *shstrtab;
6487
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6488
6489
0
  i_ehdrp = elf_elfheader (abfd);
6490
6491
0
  shstrtab = _bfd_elf_strtab_init ();
6492
0
  if (shstrtab == NULL)
6493
0
    return false;
6494
6495
0
  elf_shstrtab (abfd) = shstrtab;
6496
6497
0
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6498
0
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6499
0
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6500
0
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6501
6502
0
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6503
0
  i_ehdrp->e_ident[EI_DATA] =
6504
0
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6505
0
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6506
6507
0
  if ((abfd->flags & DYNAMIC) != 0)
6508
0
    i_ehdrp->e_type = ET_DYN;
6509
0
  else if ((abfd->flags & EXEC_P) != 0)
6510
0
    i_ehdrp->e_type = ET_EXEC;
6511
0
  else if (bfd_get_format (abfd) == bfd_core)
6512
0
    i_ehdrp->e_type = ET_CORE;
6513
0
  else
6514
0
    i_ehdrp->e_type = ET_REL;
6515
6516
0
  switch (bfd_get_arch (abfd))
6517
0
    {
6518
0
    case bfd_arch_unknown:
6519
0
      i_ehdrp->e_machine = EM_NONE;
6520
0
      break;
6521
6522
      /* There used to be a long list of cases here, each one setting
6523
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6524
   in the corresponding bfd definition.  To avoid duplication,
6525
   the switch was removed.  Machines that need special handling
6526
   can generally do it in elf_backend_final_write_processing(),
6527
   unless they need the information earlier than the final write.
6528
   Such need can generally be supplied by replacing the tests for
6529
   e_machine with the conditions used to determine it.  */
6530
0
    default:
6531
0
      i_ehdrp->e_machine = bed->elf_machine_code;
6532
0
    }
6533
6534
0
  i_ehdrp->e_version = bed->s->ev_current;
6535
0
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6536
6537
  /* No program header, for now.  */
6538
0
  i_ehdrp->e_phoff = 0;
6539
0
  i_ehdrp->e_phentsize = 0;
6540
0
  i_ehdrp->e_phnum = 0;
6541
6542
  /* Each bfd section is section header entry.  */
6543
0
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
6544
0
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6545
6546
0
  elf_tdata (abfd)->symtab_hdr.sh_name =
6547
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6548
0
  elf_tdata (abfd)->strtab_hdr.sh_name =
6549
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6550
0
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
6551
0
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6552
0
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6553
0
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6554
0
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6555
0
    return false;
6556
6557
0
  return true;
6558
0
}
6559
6560
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6561
6562
   FIXME: We used to have code here to sort the PT_LOAD segments into
6563
   ascending order, as per the ELF spec.  But this breaks some programs,
6564
   including the Linux kernel.  But really either the spec should be
6565
   changed or the programs updated.  */
6566
6567
bool
6568
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6569
0
{
6570
0
  if (link_info != NULL && bfd_link_pie (link_info))
6571
0
    {
6572
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6573
0
      unsigned int num_segments = i_ehdrp->e_phnum;
6574
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
6575
0
      Elf_Internal_Phdr *segment = tdata->phdr;
6576
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
6577
6578
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
6579
0
      bfd_vma p_vaddr = (bfd_vma) -1;
6580
0
      for (; segment < end_segment; segment++)
6581
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6582
0
    p_vaddr = segment->p_vaddr;
6583
6584
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6585
   segments is non-zero.  */
6586
0
      if (p_vaddr)
6587
0
  i_ehdrp->e_type = ET_EXEC;
6588
0
    }
6589
0
  return true;
6590
0
}
6591
6592
/* Assign file positions for all the reloc sections which are not part
6593
   of the loadable file image, and the file position of section headers.  */
6594
6595
static bool
6596
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6597
0
{
6598
0
  file_ptr off;
6599
0
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6600
0
  Elf_Internal_Shdr *shdrp;
6601
0
  Elf_Internal_Ehdr *i_ehdrp;
6602
0
  const struct elf_backend_data *bed;
6603
6604
0
  off = elf_next_file_pos (abfd);
6605
6606
0
  shdrpp = elf_elfsections (abfd);
6607
0
  end_shdrpp = shdrpp + elf_numsections (abfd);
6608
0
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6609
0
    {
6610
0
      shdrp = *shdrpp;
6611
0
      if (shdrp->sh_offset == -1)
6612
0
  {
6613
0
    asection *sec = shdrp->bfd_section;
6614
0
    if (sec == NULL
6615
0
        || shdrp->sh_type == SHT_REL
6616
0
        || shdrp->sh_type == SHT_RELA)
6617
0
      ;
6618
0
    else if (bfd_section_is_ctf (sec))
6619
0
      {
6620
        /* Update section size and contents.  */
6621
0
        shdrp->sh_size = sec->size;
6622
0
        shdrp->contents = sec->contents;
6623
0
      }
6624
0
    else if (shdrp->sh_name == -1u)
6625
0
      {
6626
0
        const char *name = sec->name;
6627
0
        struct bfd_elf_section_data *d;
6628
6629
        /* Compress DWARF debug sections.  */
6630
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
6631
0
    return false;
6632
6633
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
6634
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
6635
0
      && name[1] == 'd')
6636
0
    {
6637
      /* If section is compressed with zlib-gnu, convert
6638
         section name from .debug_* to .zdebug_*.  */
6639
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
6640
0
      if (new_name == NULL)
6641
0
        return false;
6642
0
      name = new_name;
6643
0
    }
6644
        /* Add section name to section name section.  */
6645
0
        shdrp->sh_name
6646
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6647
0
                  name, false);
6648
0
        d = elf_section_data (sec);
6649
6650
        /* Add reloc section name to section name section.  */
6651
0
        if (d->rel.hdr
6652
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6653
0
              name, false))
6654
0
    return false;
6655
0
        if (d->rela.hdr
6656
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6657
0
              name, true))
6658
0
    return false;
6659
6660
        /* Update section size and contents.  */
6661
0
        shdrp->sh_size = sec->size;
6662
0
        shdrp->contents = sec->contents;
6663
0
        sec->contents = NULL;
6664
0
      }
6665
6666
0
    off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6667
0
  }
6668
0
    }
6669
6670
  /* Place section name section after DWARF debug sections have been
6671
     compressed.  */
6672
0
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6673
0
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6674
0
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6675
0
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6676
6677
  /* Place the section headers.  */
6678
0
  i_ehdrp = elf_elfheader (abfd);
6679
0
  bed = get_elf_backend_data (abfd);
6680
0
  off = align_file_position (off, 1 << bed->s->log_file_align);
6681
0
  i_ehdrp->e_shoff = off;
6682
0
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6683
0
  elf_next_file_pos (abfd) = off;
6684
6685
0
  return true;
6686
0
}
6687
6688
bool
6689
_bfd_elf_write_object_contents (bfd *abfd)
6690
0
{
6691
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6692
0
  Elf_Internal_Shdr **i_shdrp;
6693
0
  bool failed;
6694
0
  unsigned int count, num_sec;
6695
0
  struct elf_obj_tdata *t;
6696
6697
0
  if (! abfd->output_has_begun
6698
0
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6699
0
    return false;
6700
  /* Do not rewrite ELF data when the BFD has been opened for update.
6701
     abfd->output_has_begun was set to TRUE on opening, so creation of
6702
     new sections, and modification of existing section sizes was
6703
     restricted.  This means the ELF header, program headers and
6704
     section headers can't have changed.  If the contents of any
6705
     sections has been modified, then those changes have already been
6706
     written to the BFD.  */
6707
0
  else if (abfd->direction == both_direction)
6708
0
    {
6709
0
      BFD_ASSERT (abfd->output_has_begun);
6710
0
      return true;
6711
0
    }
6712
6713
0
  i_shdrp = elf_elfsections (abfd);
6714
6715
0
  failed = false;
6716
0
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6717
0
  if (failed)
6718
0
    return false;
6719
6720
0
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6721
0
    return false;
6722
6723
  /* After writing the headers, we need to write the sections too...  */
6724
0
  num_sec = elf_numsections (abfd);
6725
0
  for (count = 1; count < num_sec; count++)
6726
0
    {
6727
0
      i_shdrp[count]->sh_name
6728
0
  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6729
0
          i_shdrp[count]->sh_name);
6730
0
      if (bed->elf_backend_section_processing)
6731
0
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6732
0
    return false;
6733
0
      if (i_shdrp[count]->contents)
6734
0
  {
6735
0
    bfd_size_type amt = i_shdrp[count]->sh_size;
6736
6737
0
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6738
0
        || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6739
0
      return false;
6740
0
  }
6741
0
    }
6742
6743
  /* Write out the section header names.  */
6744
0
  t = elf_tdata (abfd);
6745
0
  if (elf_shstrtab (abfd) != NULL
6746
0
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6747
0
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6748
0
    return false;
6749
6750
0
  if (!(*bed->elf_backend_final_write_processing) (abfd))
6751
0
    return false;
6752
6753
0
  if (!bed->s->write_shdrs_and_ehdr (abfd))
6754
0
    return false;
6755
6756
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
6757
0
  if (t->o->build_id.after_write_object_contents != NULL
6758
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
6759
0
    return false;
6760
0
  if (t->o->package_metadata.after_write_object_contents != NULL
6761
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6762
0
    return false;
6763
6764
0
  return true;
6765
0
}
6766
6767
bool
6768
_bfd_elf_write_corefile_contents (bfd *abfd)
6769
0
{
6770
  /* Hopefully this can be done just like an object file.  */
6771
0
  return _bfd_elf_write_object_contents (abfd);
6772
0
}
6773
6774
/* Given a section, search the header to find them.  */
6775
6776
unsigned int
6777
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6778
0
{
6779
0
  const struct elf_backend_data *bed;
6780
0
  unsigned int sec_index;
6781
6782
0
  if (elf_section_data (asect) != NULL
6783
0
      && elf_section_data (asect)->this_idx != 0)
6784
0
    return elf_section_data (asect)->this_idx;
6785
6786
0
  if (bfd_is_abs_section (asect))
6787
0
    sec_index = SHN_ABS;
6788
0
  else if (bfd_is_com_section (asect))
6789
0
    sec_index = SHN_COMMON;
6790
0
  else if (bfd_is_und_section (asect))
6791
0
    sec_index = SHN_UNDEF;
6792
0
  else
6793
0
    sec_index = SHN_BAD;
6794
6795
0
  bed = get_elf_backend_data (abfd);
6796
0
  if (bed->elf_backend_section_from_bfd_section)
6797
0
    {
6798
0
      int retval = sec_index;
6799
6800
0
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6801
0
  return retval;
6802
0
    }
6803
6804
0
  if (sec_index == SHN_BAD)
6805
0
    bfd_set_error (bfd_error_nonrepresentable_section);
6806
6807
0
  return sec_index;
6808
0
}
6809
6810
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6811
   on error.  */
6812
6813
int
6814
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6815
0
{
6816
0
  asymbol *asym_ptr = *asym_ptr_ptr;
6817
0
  int idx;
6818
0
  flagword flags = asym_ptr->flags;
6819
6820
  /* When gas creates relocations against local labels, it creates its
6821
     own symbol for the section, but does put the symbol into the
6822
     symbol chain, so udata is 0.  When the linker is generating
6823
     relocatable output, this section symbol may be for one of the
6824
     input sections rather than the output section.  */
6825
0
  if (asym_ptr->udata.i == 0
6826
0
      && (flags & BSF_SECTION_SYM)
6827
0
      && asym_ptr->section)
6828
0
    {
6829
0
      asection *sec;
6830
6831
0
      sec = asym_ptr->section;
6832
0
      if (sec->owner != abfd && sec->output_section != NULL)
6833
0
  sec = sec->output_section;
6834
0
      if (sec->owner == abfd
6835
0
    && sec->index < elf_num_section_syms (abfd)
6836
0
    && elf_section_syms (abfd)[sec->index] != NULL)
6837
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6838
0
    }
6839
6840
0
  idx = asym_ptr->udata.i;
6841
6842
0
  if (idx == 0)
6843
0
    {
6844
      /* This case can occur when using --strip-symbol on a symbol
6845
   which is used in a relocation entry.  */
6846
0
      _bfd_error_handler
6847
  /* xgettext:c-format */
6848
0
  (_("%pB: symbol `%s' required but not present"),
6849
0
   abfd, bfd_asymbol_name (asym_ptr));
6850
0
      bfd_set_error (bfd_error_no_symbols);
6851
0
      return -1;
6852
0
    }
6853
6854
#if DEBUG & 4
6855
  {
6856
    fprintf (stderr,
6857
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6858
       " flags = 0x%.8x\n",
6859
       (long) asym_ptr, asym_ptr->name, idx, flags);
6860
    fflush (stderr);
6861
  }
6862
#endif
6863
6864
0
  return idx;
6865
0
}
6866
6867
static inline bfd_vma
6868
segment_size (Elf_Internal_Phdr *segment)
6869
0
{
6870
0
  return (segment->p_memsz > segment->p_filesz
6871
0
    ? segment->p_memsz : segment->p_filesz);
6872
0
}
6873
6874
6875
/* Returns the end address of the segment + 1.  */
6876
static inline bfd_vma
6877
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6878
0
{
6879
0
  return start + segment_size (segment);
6880
0
}
6881
6882
static inline bfd_size_type
6883
section_size (asection *section, Elf_Internal_Phdr *segment)
6884
0
{
6885
0
  if ((section->flags & SEC_HAS_CONTENTS) != 0
6886
0
      || (section->flags & SEC_THREAD_LOCAL) == 0
6887
0
      || segment->p_type == PT_TLS)
6888
0
    return section->size;
6889
0
  return 0;
6890
0
}
6891
6892
/* Returns TRUE if the given section is contained within the given
6893
   segment.  LMA addresses are compared against PADDR when
6894
   USE_VADDR is false, VMA against VADDR when true.  */
6895
static bool
6896
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6897
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6898
     bool use_vaddr)
6899
0
{
6900
0
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
6901
0
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
6902
0
  bfd_vma octet;
6903
0
  if (_bfd_mul_overflow (addr, opb, &octet))
6904
0
    return false;
6905
  /* The third and fourth lines below are testing that the section end
6906
     address is within the segment.  It's written this way to avoid
6907
     overflow.  Add seg_addr + section_size to both sides of the
6908
     inequality to make it obvious.  */
6909
0
  return (octet >= seg_addr
6910
0
    && segment_size (segment) >= section_size (section, segment)
6911
0
    && (octet - seg_addr
6912
0
        <= segment_size (segment) - section_size (section, segment)));
6913
0
}
6914
6915
/* Handle PT_NOTE segment.  */
6916
static bool
6917
is_note (asection *s, Elf_Internal_Phdr *p)
6918
0
{
6919
0
  return (p->p_type == PT_NOTE
6920
0
    && elf_section_type (s) == SHT_NOTE
6921
0
    && (ufile_ptr) s->filepos >= p->p_offset
6922
0
    && p->p_filesz >= s->size
6923
0
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
6924
0
}
6925
6926
/* Rewrite program header information.  */
6927
6928
static bool
6929
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6930
0
{
6931
0
  Elf_Internal_Ehdr *iehdr;
6932
0
  struct elf_segment_map *map;
6933
0
  struct elf_segment_map *map_first;
6934
0
  struct elf_segment_map **pointer_to_map;
6935
0
  Elf_Internal_Phdr *segment;
6936
0
  asection *section;
6937
0
  unsigned int i;
6938
0
  unsigned int num_segments;
6939
0
  bool phdr_included = false;
6940
0
  bool p_paddr_valid;
6941
0
  struct elf_segment_map *phdr_adjust_seg = NULL;
6942
0
  unsigned int phdr_adjust_num = 0;
6943
0
  const struct elf_backend_data *bed;
6944
0
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6945
6946
0
  bed = get_elf_backend_data (ibfd);
6947
0
  iehdr = elf_elfheader (ibfd);
6948
6949
0
  map_first = NULL;
6950
0
  pointer_to_map = &map_first;
6951
6952
0
  num_segments = elf_elfheader (ibfd)->e_phnum;
6953
6954
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
6955
     linker, which generates a PT_INTERP section with p_vaddr and
6956
     p_memsz set to 0.  */
6957
0
#define IS_SOLARIS_PT_INTERP(p, s)          \
6958
0
  (p->p_vaddr == 0              \
6959
0
   && p->p_paddr == 0              \
6960
0
   && p->p_memsz == 0              \
6961
0
   && p->p_filesz > 0              \
6962
0
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
6963
0
   && s->size > 0              \
6964
0
   && (bfd_vma) s->filepos >= p->p_offset        \
6965
0
   && ((bfd_vma) s->filepos + s->size         \
6966
0
       <= p->p_offset + p->p_filesz))
6967
6968
  /* Decide if the given section should be included in the given segment.
6969
     A section will be included if:
6970
       1. It is within the address space of the segment -- we use the LMA
6971
    if that is set for the segment and the VMA otherwise,
6972
       2. It is an allocated section or a NOTE section in a PT_NOTE
6973
    segment.
6974
       3. There is an output section associated with it,
6975
       4. The section has not already been allocated to a previous segment.
6976
       5. PT_GNU_STACK segments do not include any sections.
6977
       6. PT_TLS segment includes only SHF_TLS sections.
6978
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6979
       8. PT_DYNAMIC should not contain empty sections at the beginning
6980
    (with the possible exception of .dynamic).  */
6981
0
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)   \
6982
0
  (((is_contained_by (section, segment, segment->p_paddr,    \
6983
0
          segment->p_vaddr, opb,        \
6984
0
          bed->want_p_paddr_set_to_zero)      \
6985
0
     && (section->flags & SEC_ALLOC) != 0)       \
6986
0
    || is_note (section, segment))         \
6987
0
   && segment->p_type != PT_GNU_STACK          \
6988
0
   && (segment->p_type != PT_TLS          \
6989
0
       || (section->flags & SEC_THREAD_LOCAL))        \
6990
0
   && (segment->p_type == PT_LOAD          \
6991
0
       || segment->p_type == PT_TLS          \
6992
0
       || (section->flags & SEC_THREAD_LOCAL) == 0)     \
6993
0
   && (segment->p_type != PT_DYNAMIC          \
6994
0
       || section_size (section, segment) > 0        \
6995
0
       || (segment->p_paddr            \
6996
0
     ? segment->p_paddr != section->lma * (opb)      \
6997
0
     : segment->p_vaddr != section->vma * (opb))     \
6998
0
       || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6999
0
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7000
7001
/* If the output section of a section in the input segment is NULL,
7002
   it is removed from the corresponding output segment.   */
7003
0
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)    \
7004
0
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)   \
7005
0
   && section->output_section != NULL)
7006
7007
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7008
0
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7009
0
  (seg1->field >= segment_end (seg2, seg2->field))
7010
7011
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7012
     their VMA address ranges and their LMA address ranges overlap.
7013
     It is possible to have overlapping VMA ranges without overlapping LMA
7014
     ranges.  RedBoot images for example can have both .data and .bss mapped
7015
     to the same VMA range, but with the .data section mapped to a different
7016
     LMA.  */
7017
0
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7018
0
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7019
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7020
0
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7021
0
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7022
7023
  /* Initialise the segment mark field, and discard stupid alignment.  */
7024
0
  for (section = ibfd->sections; section != NULL; section = section->next)
7025
0
    {
7026
0
      asection *o = section->output_section;
7027
0
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7028
0
  o->alignment_power = 0;
7029
0
      section->segment_mark = false;
7030
0
    }
7031
7032
  /* The Solaris linker creates program headers in which all the
7033
     p_paddr fields are zero.  When we try to objcopy or strip such a
7034
     file, we get confused.  Check for this case, and if we find it
7035
     don't set the p_paddr_valid fields.  */
7036
0
  p_paddr_valid = false;
7037
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7038
0
       i < num_segments;
7039
0
       i++, segment++)
7040
0
    if (segment->p_paddr != 0)
7041
0
      {
7042
0
  p_paddr_valid = true;
7043
0
  break;
7044
0
      }
7045
7046
  /* Scan through the segments specified in the program header
7047
     of the input BFD.  For this first scan we look for overlaps
7048
     in the loadable segments.  These can be created by weird
7049
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7050
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7051
0
       i < num_segments;
7052
0
       i++, segment++)
7053
0
    {
7054
0
      unsigned int j;
7055
0
      Elf_Internal_Phdr *segment2;
7056
7057
0
      if (segment->p_type == PT_INTERP)
7058
0
  for (section = ibfd->sections; section; section = section->next)
7059
0
    if (IS_SOLARIS_PT_INTERP (segment, section))
7060
0
      {
7061
        /* Mininal change so that the normal section to segment
7062
     assignment code will work.  */
7063
0
        segment->p_vaddr = section->vma * opb;
7064
0
        break;
7065
0
      }
7066
7067
0
      if (segment->p_type != PT_LOAD)
7068
0
  {
7069
    /* Remove PT_GNU_RELRO segment.  */
7070
0
    if (segment->p_type == PT_GNU_RELRO)
7071
0
      segment->p_type = PT_NULL;
7072
0
    continue;
7073
0
  }
7074
7075
      /* Determine if this segment overlaps any previous segments.  */
7076
0
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7077
0
  {
7078
0
    bfd_signed_vma extra_length;
7079
7080
0
    if (segment2->p_type != PT_LOAD
7081
0
        || !SEGMENT_OVERLAPS (segment, segment2))
7082
0
      continue;
7083
7084
    /* Merge the two segments together.  */
7085
0
    if (segment2->p_vaddr < segment->p_vaddr)
7086
0
      {
7087
        /* Extend SEGMENT2 to include SEGMENT and then delete
7088
     SEGMENT.  */
7089
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7090
0
            - segment_end (segment2, segment2->p_vaddr));
7091
7092
0
        if (extra_length > 0)
7093
0
    {
7094
0
      segment2->p_memsz += extra_length;
7095
0
      segment2->p_filesz += extra_length;
7096
0
    }
7097
7098
0
        segment->p_type = PT_NULL;
7099
7100
        /* Since we have deleted P we must restart the outer loop.  */
7101
0
        i = 0;
7102
0
        segment = elf_tdata (ibfd)->phdr;
7103
0
        break;
7104
0
      }
7105
0
    else
7106
0
      {
7107
        /* Extend SEGMENT to include SEGMENT2 and then delete
7108
     SEGMENT2.  */
7109
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7110
0
            - segment_end (segment, segment->p_vaddr));
7111
7112
0
        if (extra_length > 0)
7113
0
    {
7114
0
      segment->p_memsz += extra_length;
7115
0
      segment->p_filesz += extra_length;
7116
0
    }
7117
7118
0
        segment2->p_type = PT_NULL;
7119
0
      }
7120
0
  }
7121
0
    }
7122
7123
  /* The second scan attempts to assign sections to segments.  */
7124
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7125
0
       i < num_segments;
7126
0
       i++, segment++)
7127
0
    {
7128
0
      unsigned int section_count;
7129
0
      asection **sections;
7130
0
      asection *output_section;
7131
0
      unsigned int isec;
7132
0
      asection *matching_lma;
7133
0
      asection *suggested_lma;
7134
0
      unsigned int j;
7135
0
      size_t amt;
7136
0
      asection *first_section;
7137
7138
0
      if (segment->p_type == PT_NULL)
7139
0
  continue;
7140
7141
0
      first_section = NULL;
7142
      /* Compute how many sections might be placed into this segment.  */
7143
0
      for (section = ibfd->sections, section_count = 0;
7144
0
     section != NULL;
7145
0
     section = section->next)
7146
0
  {
7147
    /* Find the first section in the input segment, which may be
7148
       removed from the corresponding output segment.   */
7149
0
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7150
0
      {
7151
0
        if (first_section == NULL)
7152
0
    first_section = section;
7153
0
        if (section->output_section != NULL)
7154
0
    ++section_count;
7155
0
      }
7156
0
  }
7157
7158
      /* Allocate a segment map big enough to contain
7159
   all of the sections we have selected.  */
7160
0
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7161
0
      amt += section_count * sizeof (asection *);
7162
0
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7163
0
      if (map == NULL)
7164
0
  return false;
7165
7166
      /* Initialise the fields of the segment map.  Default to
7167
   using the physical address of the segment in the input BFD.  */
7168
0
      map->next = NULL;
7169
0
      map->p_type = segment->p_type;
7170
0
      map->p_flags = segment->p_flags;
7171
0
      map->p_flags_valid = 1;
7172
7173
0
      if (map->p_type == PT_LOAD
7174
0
    && (ibfd->flags & D_PAGED) != 0
7175
0
    && maxpagesize > 1
7176
0
    && segment->p_align > 1)
7177
0
  {
7178
0
    map->p_align = segment->p_align;
7179
0
    if (segment->p_align > maxpagesize)
7180
0
      map->p_align = maxpagesize;
7181
0
    map->p_align_valid = 1;
7182
0
  }
7183
7184
      /* If the first section in the input segment is removed, there is
7185
   no need to preserve segment physical address in the corresponding
7186
   output segment.  */
7187
0
      if (!first_section || first_section->output_section != NULL)
7188
0
  {
7189
0
    map->p_paddr = segment->p_paddr;
7190
0
    map->p_paddr_valid = p_paddr_valid;
7191
0
  }
7192
7193
      /* Determine if this segment contains the ELF file header
7194
   and if it contains the program headers themselves.  */
7195
0
      map->includes_filehdr = (segment->p_offset == 0
7196
0
             && segment->p_filesz >= iehdr->e_ehsize);
7197
0
      map->includes_phdrs = 0;
7198
7199
0
      if (!phdr_included || segment->p_type != PT_LOAD)
7200
0
  {
7201
0
    map->includes_phdrs =
7202
0
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7203
0
       && (segment->p_offset + segment->p_filesz
7204
0
     >= ((bfd_vma) iehdr->e_phoff
7205
0
         + iehdr->e_phnum * iehdr->e_phentsize)));
7206
7207
0
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7208
0
      phdr_included = true;
7209
0
  }
7210
7211
0
      if (section_count == 0)
7212
0
  {
7213
    /* Special segments, such as the PT_PHDR segment, may contain
7214
       no sections, but ordinary, loadable segments should contain
7215
       something.  They are allowed by the ELF spec however, so only
7216
       a warning is produced.
7217
       Don't warn if an empty PT_LOAD contains the program headers.
7218
       There is however the valid use case of embedded systems which
7219
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7220
       flash memory with zeros.  No warning is shown for that case.  */
7221
0
    if (segment->p_type == PT_LOAD
7222
0
        && !map->includes_phdrs
7223
0
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7224
      /* xgettext:c-format */
7225
0
      _bfd_error_handler
7226
0
        (_("%pB: warning: empty loadable segment detected"
7227
0
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7228
0
         ibfd, (uint64_t) segment->p_vaddr);
7229
7230
0
    map->p_vaddr_offset = segment->p_vaddr / opb;
7231
0
    map->count = 0;
7232
0
    *pointer_to_map = map;
7233
0
    pointer_to_map = &map->next;
7234
7235
0
    continue;
7236
0
  }
7237
7238
      /* Now scan the sections in the input BFD again and attempt
7239
   to add their corresponding output sections to the segment map.
7240
   The problem here is how to handle an output section which has
7241
   been moved (ie had its LMA changed).  There are four possibilities:
7242
7243
   1. None of the sections have been moved.
7244
      In this case we can continue to use the segment LMA from the
7245
      input BFD.
7246
7247
   2. All of the sections have been moved by the same amount.
7248
      In this case we can change the segment's LMA to match the LMA
7249
      of the first section.
7250
7251
   3. Some of the sections have been moved, others have not.
7252
      In this case those sections which have not been moved can be
7253
      placed in the current segment which will have to have its size,
7254
      and possibly its LMA changed, and a new segment or segments will
7255
      have to be created to contain the other sections.
7256
7257
   4. The sections have been moved, but not by the same amount.
7258
      In this case we can change the segment's LMA to match the LMA
7259
      of the first section and we will have to create a new segment
7260
      or segments to contain the other sections.
7261
7262
   In order to save time, we allocate an array to hold the section
7263
   pointers that we are interested in.  As these sections get assigned
7264
   to a segment, they are removed from this array.  */
7265
7266
0
      amt = section_count * sizeof (asection *);
7267
0
      sections = (asection **) bfd_malloc (amt);
7268
0
      if (sections == NULL)
7269
0
  return false;
7270
7271
      /* Step One: Scan for segment vs section LMA conflicts.
7272
   Also add the sections to the section array allocated above.
7273
   Also add the sections to the current segment.  In the common
7274
   case, where the sections have not been moved, this means that
7275
   we have completely filled the segment, and there is nothing
7276
   more to do.  */
7277
0
      isec = 0;
7278
0
      matching_lma = NULL;
7279
0
      suggested_lma = NULL;
7280
7281
0
      for (section = first_section, j = 0;
7282
0
     section != NULL;
7283
0
     section = section->next)
7284
0
  {
7285
0
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7286
0
      {
7287
0
        output_section = section->output_section;
7288
7289
0
        sections[j++] = section;
7290
7291
        /* The Solaris native linker always sets p_paddr to 0.
7292
     We try to catch that case here, and set it to the
7293
     correct value.  Note - some backends require that
7294
     p_paddr be left as zero.  */
7295
0
        if (!p_paddr_valid
7296
0
      && segment->p_vaddr != 0
7297
0
      && !bed->want_p_paddr_set_to_zero
7298
0
      && isec == 0
7299
0
      && output_section->lma != 0
7300
0
      && (align_power (segment->p_vaddr
7301
0
           + (map->includes_filehdr
7302
0
              ? iehdr->e_ehsize : 0)
7303
0
           + (map->includes_phdrs
7304
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7305
0
              : 0),
7306
0
           output_section->alignment_power * opb)
7307
0
          == (output_section->vma * opb)))
7308
0
    map->p_paddr = segment->p_vaddr;
7309
7310
        /* Match up the physical address of the segment with the
7311
     LMA address of the output section.  */
7312
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7313
0
           0, opb, false)
7314
0
      || is_note (section, segment))
7315
0
    {
7316
0
      if (matching_lma == NULL
7317
0
          || output_section->lma < matching_lma->lma)
7318
0
        matching_lma = output_section;
7319
7320
      /* We assume that if the section fits within the segment
7321
         then it does not overlap any other section within that
7322
         segment.  */
7323
0
      map->sections[isec++] = output_section;
7324
0
    }
7325
0
        else if (suggested_lma == NULL)
7326
0
    suggested_lma = output_section;
7327
7328
0
        if (j == section_count)
7329
0
    break;
7330
0
      }
7331
0
  }
7332
7333
0
      BFD_ASSERT (j == section_count);
7334
7335
      /* Step Two: Adjust the physical address of the current segment,
7336
   if necessary.  */
7337
0
      if (isec == section_count)
7338
0
  {
7339
    /* All of the sections fitted within the segment as currently
7340
       specified.  This is the default case.  Add the segment to
7341
       the list of built segments and carry on to process the next
7342
       program header in the input BFD.  */
7343
0
    map->count = section_count;
7344
0
    *pointer_to_map = map;
7345
0
    pointer_to_map = &map->next;
7346
7347
0
    if (p_paddr_valid
7348
0
        && !bed->want_p_paddr_set_to_zero)
7349
0
      {
7350
0
        bfd_vma hdr_size = 0;
7351
0
        if (map->includes_filehdr)
7352
0
    hdr_size = iehdr->e_ehsize;
7353
0
        if (map->includes_phdrs)
7354
0
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7355
7356
        /* Account for padding before the first section in the
7357
     segment.  */
7358
0
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7359
0
             - matching_lma->lma);
7360
0
      }
7361
7362
0
    free (sections);
7363
0
    continue;
7364
0
  }
7365
0
      else
7366
0
  {
7367
    /* Change the current segment's physical address to match
7368
       the LMA of the first section that fitted, or if no
7369
       section fitted, the first section.  */
7370
0
    if (matching_lma == NULL)
7371
0
      matching_lma = suggested_lma;
7372
7373
0
    map->p_paddr = matching_lma->lma * opb;
7374
7375
    /* Offset the segment physical address from the lma
7376
       to allow for space taken up by elf headers.  */
7377
0
    if (map->includes_phdrs)
7378
0
      {
7379
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7380
7381
        /* iehdr->e_phnum is just an estimate of the number
7382
     of program headers that we will need.  Make a note
7383
     here of the number we used and the segment we chose
7384
     to hold these headers, so that we can adjust the
7385
     offset when we know the correct value.  */
7386
0
        phdr_adjust_num = iehdr->e_phnum;
7387
0
        phdr_adjust_seg = map;
7388
0
      }
7389
7390
0
    if (map->includes_filehdr)
7391
0
      {
7392
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7393
0
        map->p_paddr -= iehdr->e_ehsize;
7394
        /* We've subtracted off the size of headers from the
7395
     first section lma, but there may have been some
7396
     alignment padding before that section too.  Try to
7397
     account for that by adjusting the segment lma down to
7398
     the same alignment.  */
7399
0
        if (segment->p_align != 0 && segment->p_align < align)
7400
0
    align = segment->p_align;
7401
0
        map->p_paddr &= -(align * opb);
7402
0
      }
7403
0
  }
7404
7405
      /* Step Three: Loop over the sections again, this time assigning
7406
   those that fit to the current segment and removing them from the
7407
   sections array; but making sure not to leave large gaps.  Once all
7408
   possible sections have been assigned to the current segment it is
7409
   added to the list of built segments and if sections still remain
7410
   to be assigned, a new segment is constructed before repeating
7411
   the loop.  */
7412
0
      isec = 0;
7413
0
      do
7414
0
  {
7415
0
    map->count = 0;
7416
0
    suggested_lma = NULL;
7417
7418
    /* Fill the current segment with sections that fit.  */
7419
0
    for (j = 0; j < section_count; j++)
7420
0
      {
7421
0
        section = sections[j];
7422
7423
0
        if (section == NULL)
7424
0
    continue;
7425
7426
0
        output_section = section->output_section;
7427
7428
0
        BFD_ASSERT (output_section != NULL);
7429
7430
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7431
0
           0, opb, false)
7432
0
      || is_note (section, segment))
7433
0
    {
7434
0
      if (map->count == 0)
7435
0
        {
7436
          /* If the first section in a segment does not start at
7437
       the beginning of the segment, then something is
7438
       wrong.  */
7439
0
          if (align_power (map->p_paddr
7440
0
               + (map->includes_filehdr
7441
0
            ? iehdr->e_ehsize : 0)
7442
0
               + (map->includes_phdrs
7443
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7444
0
            : 0),
7445
0
               output_section->alignment_power * opb)
7446
0
        != output_section->lma * opb)
7447
0
      goto sorry;
7448
0
        }
7449
0
      else
7450
0
        {
7451
0
          asection *prev_sec;
7452
7453
0
          prev_sec = map->sections[map->count - 1];
7454
7455
          /* If the gap between the end of the previous section
7456
       and the start of this section is more than
7457
       maxpagesize then we need to start a new segment.  */
7458
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7459
0
              maxpagesize)
7460
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7461
0
        || (prev_sec->lma + prev_sec->size
7462
0
            > output_section->lma))
7463
0
      {
7464
0
        if (suggested_lma == NULL)
7465
0
          suggested_lma = output_section;
7466
7467
0
        continue;
7468
0
      }
7469
0
        }
7470
7471
0
      map->sections[map->count++] = output_section;
7472
0
      ++isec;
7473
0
      sections[j] = NULL;
7474
0
      if (segment->p_type == PT_LOAD)
7475
0
        section->segment_mark = true;
7476
0
    }
7477
0
        else if (suggested_lma == NULL)
7478
0
    suggested_lma = output_section;
7479
0
      }
7480
7481
    /* PR 23932.  A corrupt input file may contain sections that cannot
7482
       be assigned to any segment - because for example they have a
7483
       negative size - or segments that do not contain any sections.
7484
       But there are also valid reasons why a segment can be empty.
7485
       So allow a count of zero.  */
7486
7487
    /* Add the current segment to the list of built segments.  */
7488
0
    *pointer_to_map = map;
7489
0
    pointer_to_map = &map->next;
7490
7491
0
    if (isec < section_count)
7492
0
      {
7493
        /* We still have not allocated all of the sections to
7494
     segments.  Create a new segment here, initialise it
7495
     and carry on looping.  */
7496
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7497
0
        amt += section_count * sizeof (asection *);
7498
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7499
0
        if (map == NULL)
7500
0
    {
7501
0
      free (sections);
7502
0
      return false;
7503
0
    }
7504
7505
        /* Initialise the fields of the segment map.  Set the physical
7506
     physical address to the LMA of the first section that has
7507
     not yet been assigned.  */
7508
0
        map->next = NULL;
7509
0
        map->p_type = segment->p_type;
7510
0
        map->p_flags = segment->p_flags;
7511
0
        map->p_flags_valid = 1;
7512
0
        map->p_paddr = suggested_lma->lma * opb;
7513
0
        map->p_paddr_valid = p_paddr_valid;
7514
0
        map->includes_filehdr = 0;
7515
0
        map->includes_phdrs = 0;
7516
0
      }
7517
7518
0
    continue;
7519
0
  sorry:
7520
0
    bfd_set_error (bfd_error_sorry);
7521
0
    free (sections);
7522
0
    return false;
7523
0
  }
7524
0
      while (isec < section_count);
7525
7526
0
      free (sections);
7527
0
    }
7528
7529
0
  elf_seg_map (obfd) = map_first;
7530
7531
  /* If we had to estimate the number of program headers that were
7532
     going to be needed, then check our estimate now and adjust
7533
     the offset if necessary.  */
7534
0
  if (phdr_adjust_seg != NULL)
7535
0
    {
7536
0
      unsigned int count;
7537
7538
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
7539
0
  count++;
7540
7541
0
      if (count > phdr_adjust_num)
7542
0
  phdr_adjust_seg->p_paddr
7543
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7544
7545
0
      for (map = map_first; map != NULL; map = map->next)
7546
0
  if (map->p_type == PT_PHDR)
7547
0
    {
7548
0
      bfd_vma adjust
7549
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7550
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7551
0
      break;
7552
0
    }
7553
0
    }
7554
7555
0
#undef IS_SOLARIS_PT_INTERP
7556
0
#undef IS_SECTION_IN_INPUT_SEGMENT
7557
0
#undef INCLUDE_SECTION_IN_SEGMENT
7558
0
#undef SEGMENT_AFTER_SEGMENT
7559
0
#undef SEGMENT_OVERLAPS
7560
0
  return true;
7561
0
}
7562
7563
/* Return true if p_align in the ELF program header in ABFD is valid.  */
7564
7565
static bool
7566
elf_is_p_align_valid (bfd *abfd)
7567
0
{
7568
0
  unsigned int i;
7569
0
  Elf_Internal_Phdr *segment;
7570
0
  unsigned int num_segments;
7571
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7572
0
  bfd_size_type maxpagesize = bed->maxpagesize;
7573
0
  bfd_size_type p_align = bed->p_align;
7574
7575
  /* Return true if the default p_align value isn't set or the maximum
7576
     page size is the same as the minimum page size.  */
7577
0
  if (p_align == 0 || maxpagesize == bed->minpagesize)
7578
0
    return true;
7579
7580
  /* When the default p_align value is set, p_align may be set to the
7581
     default p_align value while segments are aligned to the maximum
7582
     page size.  In this case, the input p_align will be ignored and
7583
     the maximum page size will be used to align the output segments.  */
7584
0
  segment = elf_tdata (abfd)->phdr;
7585
0
  num_segments = elf_elfheader (abfd)->e_phnum;
7586
0
  for (i = 0; i < num_segments; i++, segment++)
7587
0
    if (segment->p_type == PT_LOAD
7588
0
  && (segment->p_align != p_align
7589
0
      || vma_page_aligned_bias (segment->p_vaddr,
7590
0
              segment->p_offset,
7591
0
              maxpagesize) != 0))
7592
0
      return true;
7593
7594
0
  return false;
7595
0
}
7596
7597
/* Copy ELF program header information.  */
7598
7599
static bool
7600
copy_elf_program_header (bfd *ibfd, bfd *obfd)
7601
0
{
7602
0
  Elf_Internal_Ehdr *iehdr;
7603
0
  struct elf_segment_map *map;
7604
0
  struct elf_segment_map *map_first;
7605
0
  struct elf_segment_map **pointer_to_map;
7606
0
  Elf_Internal_Phdr *segment;
7607
0
  unsigned int i;
7608
0
  unsigned int num_segments;
7609
0
  bool phdr_included = false;
7610
0
  bool p_paddr_valid;
7611
0
  bool p_palign_valid;
7612
0
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7613
7614
0
  iehdr = elf_elfheader (ibfd);
7615
7616
0
  map_first = NULL;
7617
0
  pointer_to_map = &map_first;
7618
7619
  /* If all the segment p_paddr fields are zero, don't set
7620
     map->p_paddr_valid.  */
7621
0
  p_paddr_valid = false;
7622
0
  num_segments = elf_elfheader (ibfd)->e_phnum;
7623
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7624
0
       i < num_segments;
7625
0
       i++, segment++)
7626
0
    if (segment->p_paddr != 0)
7627
0
      {
7628
0
  p_paddr_valid = true;
7629
0
  break;
7630
0
      }
7631
7632
0
  p_palign_valid = elf_is_p_align_valid (ibfd);
7633
7634
0
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7635
0
       i < num_segments;
7636
0
       i++, segment++)
7637
0
    {
7638
0
      asection *section;
7639
0
      unsigned int section_count;
7640
0
      size_t amt;
7641
0
      Elf_Internal_Shdr *this_hdr;
7642
0
      asection *first_section = NULL;
7643
0
      asection *lowest_section;
7644
7645
      /* Compute how many sections are in this segment.  */
7646
0
      for (section = ibfd->sections, section_count = 0;
7647
0
     section != NULL;
7648
0
     section = section->next)
7649
0
  {
7650
0
    this_hdr = &(elf_section_data(section)->this_hdr);
7651
0
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7652
0
      {
7653
0
        if (first_section == NULL)
7654
0
    first_section = section;
7655
0
        section_count++;
7656
0
      }
7657
0
  }
7658
7659
      /* Allocate a segment map big enough to contain
7660
   all of the sections we have selected.  */
7661
0
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7662
0
      amt += section_count * sizeof (asection *);
7663
0
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7664
0
      if (map == NULL)
7665
0
  return false;
7666
7667
      /* Initialize the fields of the output segment map with the
7668
   input segment.  */
7669
0
      map->next = NULL;
7670
0
      map->p_type = segment->p_type;
7671
0
      map->p_flags = segment->p_flags;
7672
0
      map->p_flags_valid = 1;
7673
0
      map->p_paddr = segment->p_paddr;
7674
0
      map->p_paddr_valid = p_paddr_valid;
7675
0
      map->p_align = segment->p_align;
7676
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
7677
0
      map->p_align_valid = (map->p_type == PT_GNU_STACK
7678
0
          || p_palign_valid);
7679
0
      map->p_vaddr_offset = 0;
7680
7681
0
      if (map->p_type == PT_GNU_RELRO
7682
0
    || map->p_type == PT_GNU_STACK)
7683
0
  {
7684
    /* The PT_GNU_RELRO segment may contain the first a few
7685
       bytes in the .got.plt section even if the whole .got.plt
7686
       section isn't in the PT_GNU_RELRO segment.  We won't
7687
       change the size of the PT_GNU_RELRO segment.
7688
       Similarly, PT_GNU_STACK size is significant on uclinux
7689
       systems.    */
7690
0
    map->p_size = segment->p_memsz;
7691
0
    map->p_size_valid = 1;
7692
0
  }
7693
7694
      /* Determine if this segment contains the ELF file header
7695
   and if it contains the program headers themselves.  */
7696
0
      map->includes_filehdr = (segment->p_offset == 0
7697
0
             && segment->p_filesz >= iehdr->e_ehsize);
7698
7699
0
      map->includes_phdrs = 0;
7700
0
      if (! phdr_included || segment->p_type != PT_LOAD)
7701
0
  {
7702
0
    map->includes_phdrs =
7703
0
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7704
0
       && (segment->p_offset + segment->p_filesz
7705
0
     >= ((bfd_vma) iehdr->e_phoff
7706
0
         + iehdr->e_phnum * iehdr->e_phentsize)));
7707
7708
0
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7709
0
      phdr_included = true;
7710
0
  }
7711
7712
0
      lowest_section = NULL;
7713
0
      if (section_count != 0)
7714
0
  {
7715
0
    unsigned int isec = 0;
7716
7717
0
    for (section = first_section;
7718
0
         section != NULL;
7719
0
         section = section->next)
7720
0
      {
7721
0
        this_hdr = &(elf_section_data(section)->this_hdr);
7722
0
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7723
0
    {
7724
0
      map->sections[isec++] = section->output_section;
7725
0
      if ((section->flags & SEC_ALLOC) != 0)
7726
0
        {
7727
0
          bfd_vma seg_off;
7728
7729
0
          if (lowest_section == NULL
7730
0
        || section->lma < lowest_section->lma)
7731
0
      lowest_section = section;
7732
7733
          /* Section lmas are set up from PT_LOAD header
7734
       p_paddr in _bfd_elf_make_section_from_shdr.
7735
       If this header has a p_paddr that disagrees
7736
       with the section lma, flag the p_paddr as
7737
       invalid.  */
7738
0
          if ((section->flags & SEC_LOAD) != 0)
7739
0
      seg_off = this_hdr->sh_offset - segment->p_offset;
7740
0
          else
7741
0
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
7742
0
          if (section->lma * opb - segment->p_paddr != seg_off)
7743
0
      map->p_paddr_valid = false;
7744
0
        }
7745
0
      if (isec == section_count)
7746
0
        break;
7747
0
    }
7748
0
      }
7749
0
  }
7750
7751
0
      if (section_count == 0)
7752
0
  map->p_vaddr_offset = segment->p_vaddr / opb;
7753
0
      else if (map->p_paddr_valid)
7754
0
  {
7755
    /* Account for padding before the first section in the segment.  */
7756
0
    bfd_vma hdr_size = 0;
7757
0
    if (map->includes_filehdr)
7758
0
      hdr_size = iehdr->e_ehsize;
7759
0
    if (map->includes_phdrs)
7760
0
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7761
7762
0
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7763
0
         - (lowest_section ? lowest_section->lma : 0));
7764
0
  }
7765
7766
0
      map->count = section_count;
7767
0
      *pointer_to_map = map;
7768
0
      pointer_to_map = &map->next;
7769
0
    }
7770
7771
0
  elf_seg_map (obfd) = map_first;
7772
0
  return true;
7773
0
}
7774
7775
/* Copy private BFD data.  This copies or rewrites ELF program header
7776
   information.  */
7777
7778
static bool
7779
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7780
0
{
7781
0
  bfd_vma maxpagesize;
7782
7783
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7784
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7785
0
    return true;
7786
7787
0
  if (elf_tdata (ibfd)->phdr == NULL)
7788
0
    return true;
7789
7790
0
  if (ibfd->xvec == obfd->xvec)
7791
0
    {
7792
      /* Check to see if any sections in the input BFD
7793
   covered by ELF program header have changed.  */
7794
0
      Elf_Internal_Phdr *segment;
7795
0
      asection *section, *osec;
7796
0
      unsigned int i, num_segments;
7797
0
      Elf_Internal_Shdr *this_hdr;
7798
0
      const struct elf_backend_data *bed;
7799
7800
0
      bed = get_elf_backend_data (ibfd);
7801
7802
      /* Regenerate the segment map if p_paddr is set to 0.  */
7803
0
      if (bed->want_p_paddr_set_to_zero)
7804
0
  goto rewrite;
7805
7806
      /* Initialize the segment mark field.  */
7807
0
      for (section = obfd->sections; section != NULL;
7808
0
     section = section->next)
7809
0
  section->segment_mark = false;
7810
7811
0
      num_segments = elf_elfheader (ibfd)->e_phnum;
7812
0
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
7813
0
     i < num_segments;
7814
0
     i++, segment++)
7815
0
  {
7816
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
7817
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7818
       which severly confuses things, so always regenerate the segment
7819
       map in this case.  */
7820
0
    if (segment->p_paddr == 0
7821
0
        && segment->p_memsz == 0
7822
0
        && (segment->p_type == PT_INTERP
7823
0
      || segment->p_type == PT_DYNAMIC))
7824
0
      goto rewrite;
7825
7826
0
    for (section = ibfd->sections;
7827
0
         section != NULL; section = section->next)
7828
0
      {
7829
        /* We mark the output section so that we know it comes
7830
     from the input BFD.  */
7831
0
        osec = section->output_section;
7832
0
        if (osec)
7833
0
    osec->segment_mark = true;
7834
7835
        /* Check if this section is covered by the segment.  */
7836
0
        this_hdr = &(elf_section_data(section)->this_hdr);
7837
0
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7838
0
    {
7839
      /* FIXME: Check if its output section is changed or
7840
         removed.  What else do we need to check?  */
7841
0
      if (osec == NULL
7842
0
          || section->flags != osec->flags
7843
0
          || section->lma != osec->lma
7844
0
          || section->vma != osec->vma
7845
0
          || section->size != osec->size
7846
0
          || section->rawsize != osec->rawsize
7847
0
          || section->alignment_power != osec->alignment_power)
7848
0
        goto rewrite;
7849
0
    }
7850
0
      }
7851
0
  }
7852
7853
      /* Check to see if any output section do not come from the
7854
   input BFD.  */
7855
0
      for (section = obfd->sections; section != NULL;
7856
0
     section = section->next)
7857
0
  {
7858
0
    if (!section->segment_mark)
7859
0
      goto rewrite;
7860
0
    else
7861
0
      section->segment_mark = false;
7862
0
  }
7863
7864
0
      return copy_elf_program_header (ibfd, obfd);
7865
0
    }
7866
7867
0
 rewrite:
7868
0
  maxpagesize = 0;
7869
0
  if (ibfd->xvec == obfd->xvec)
7870
0
    {
7871
      /* When rewriting program header, set the output maxpagesize to
7872
   the maximum alignment of input PT_LOAD segments.  */
7873
0
      Elf_Internal_Phdr *segment;
7874
0
      unsigned int i;
7875
0
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7876
7877
0
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
7878
0
     i < num_segments;
7879
0
     i++, segment++)
7880
0
  if (segment->p_type == PT_LOAD
7881
0
      && maxpagesize < segment->p_align)
7882
0
    {
7883
      /* PR 17512: file: f17299af.  */
7884
0
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7885
        /* xgettext:c-format */
7886
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7887
0
            PRIx64 " is too large"),
7888
0
          ibfd, (uint64_t) segment->p_align);
7889
0
      else
7890
0
        maxpagesize = segment->p_align;
7891
0
    }
7892
0
    }
7893
0
  if (maxpagesize == 0)
7894
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7895
7896
0
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7897
0
}
7898
7899
/* Initialize private output section information from input section.  */
7900
7901
bool
7902
_bfd_elf_init_private_section_data (bfd *ibfd,
7903
            asection *isec,
7904
            bfd *obfd,
7905
            asection *osec,
7906
            struct bfd_link_info *link_info)
7907
7908
0
{
7909
0
  Elf_Internal_Shdr *ihdr, *ohdr;
7910
0
  bool final_link = (link_info != NULL
7911
0
         && !bfd_link_relocatable (link_info));
7912
7913
0
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
7914
0
      || obfd->xvec->flavour != bfd_target_elf_flavour)
7915
0
    return true;
7916
7917
0
  BFD_ASSERT (elf_section_data (osec) != NULL);
7918
7919
  /* If this is a known ABI section, ELF section type and flags may
7920
     have been set up when OSEC was created.  For normal sections we
7921
     allow the user to override the type and flags other than
7922
     SHF_MASKOS and SHF_MASKPROC.  */
7923
0
  if (elf_section_type (osec) == SHT_PROGBITS
7924
0
      || elf_section_type (osec) == SHT_NOTE
7925
0
      || elf_section_type (osec) == SHT_NOBITS)
7926
0
    elf_section_type (osec) = SHT_NULL;
7927
  /* For objcopy and relocatable link, copy the ELF section type from
7928
     the input file if the BFD section flags are the same.  (If they
7929
     are different the user may be doing something like
7930
     "objcopy --set-section-flags .text=alloc,data".)  For a final
7931
     link allow some flags that the linker clears to differ.  */
7932
0
  if (elf_section_type (osec) == SHT_NULL
7933
0
      && (osec->flags == isec->flags
7934
0
    || (final_link
7935
0
        && ((osec->flags ^ isec->flags)
7936
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7937
0
    elf_section_type (osec) = elf_section_type (isec);
7938
7939
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
7940
0
  elf_section_flags (osec) = (elf_section_flags (isec)
7941
0
            & (SHF_MASKOS | SHF_MASKPROC));
7942
7943
  /* Copy sh_info from input for mbind section.  */
7944
0
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7945
0
      && elf_section_flags (isec) & SHF_GNU_MBIND)
7946
0
    elf_section_data (osec)->this_hdr.sh_info
7947
0
      = elf_section_data (isec)->this_hdr.sh_info;
7948
7949
  /* Set things up for objcopy and relocatable link.  The output
7950
     SHT_GROUP section will have its elf_next_in_group pointing back
7951
     to the input group members.  Ignore linker created group section.
7952
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
7953
0
  if ((link_info == NULL
7954
0
       || !link_info->resolve_section_groups)
7955
0
      && (elf_sec_group (isec) == NULL
7956
0
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7957
0
    {
7958
0
      if (elf_section_flags (isec) & SHF_GROUP)
7959
0
  elf_section_flags (osec) |= SHF_GROUP;
7960
0
      elf_next_in_group (osec) = elf_next_in_group (isec);
7961
0
      elf_section_data (osec)->group = elf_section_data (isec)->group;
7962
0
    }
7963
7964
  /* If not decompress, preserve SHF_COMPRESSED.  */
7965
0
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7966
0
    elf_section_flags (osec) |= (elf_section_flags (isec)
7967
0
         & SHF_COMPRESSED);
7968
7969
0
  ihdr = &elf_section_data (isec)->this_hdr;
7970
7971
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7972
     don't use the output section of the linked-to section since it
7973
     may be NULL at this point.  */
7974
0
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7975
0
    {
7976
0
      ohdr = &elf_section_data (osec)->this_hdr;
7977
0
      ohdr->sh_flags |= SHF_LINK_ORDER;
7978
0
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
7979
0
    }
7980
7981
0
  osec->use_rela_p = isec->use_rela_p;
7982
7983
0
  return true;
7984
0
}
7985
7986
/* Copy private section information.  This copies over the entsize
7987
   field, and sometimes the info field.  */
7988
7989
bool
7990
_bfd_elf_copy_private_section_data (bfd *ibfd,
7991
            asection *isec,
7992
            bfd *obfd,
7993
            asection *osec)
7994
0
{
7995
0
  Elf_Internal_Shdr *ihdr, *ohdr;
7996
7997
0
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
7998
0
      || obfd->xvec->flavour != bfd_target_elf_flavour)
7999
0
    return true;
8000
8001
0
  ihdr = &elf_section_data (isec)->this_hdr;
8002
0
  ohdr = &elf_section_data (osec)->this_hdr;
8003
8004
0
  ohdr->sh_entsize = ihdr->sh_entsize;
8005
8006
0
  if (ihdr->sh_type == SHT_SYMTAB
8007
0
      || ihdr->sh_type == SHT_DYNSYM
8008
0
      || ihdr->sh_type == SHT_GNU_verneed
8009
0
      || ihdr->sh_type == SHT_GNU_verdef)
8010
0
    ohdr->sh_info = ihdr->sh_info;
8011
8012
0
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8013
0
               NULL);
8014
0
}
8015
8016
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8017
   necessary if we are removing either the SHT_GROUP section or any of
8018
   the group member sections.  DISCARDED is the value that a section's
8019
   output_section has if the section will be discarded, NULL when this
8020
   function is called from objcopy, bfd_abs_section_ptr when called
8021
   from the linker.  */
8022
8023
bool
8024
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8025
0
{
8026
0
  asection *isec;
8027
8028
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8029
0
    if (elf_section_type (isec) == SHT_GROUP)
8030
0
      {
8031
0
  asection *first = elf_next_in_group (isec);
8032
0
  asection *s = first;
8033
0
  bfd_size_type removed = 0;
8034
8035
0
  while (s != NULL)
8036
0
    {
8037
      /* If this member section is being output but the
8038
         SHT_GROUP section is not, then clear the group info
8039
         set up by _bfd_elf_copy_private_section_data.  */
8040
0
      if (s->output_section != discarded
8041
0
    && isec->output_section == discarded)
8042
0
        {
8043
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8044
0
    elf_group_name (s->output_section) = NULL;
8045
0
        }
8046
0
      else
8047
0
        {
8048
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8049
0
    if (s->output_section == discarded
8050
0
        && isec->output_section != discarded)
8051
0
      {
8052
        /* Conversely, if the member section is not being
8053
           output but the SHT_GROUP section is, then adjust
8054
           its size.  */
8055
0
        removed += 4;
8056
0
        if (elf_sec->rel.hdr != NULL
8057
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8058
0
          removed += 4;
8059
0
        if (elf_sec->rela.hdr != NULL
8060
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8061
0
          removed += 4;
8062
0
      }
8063
0
    else
8064
0
      {
8065
        /* Also adjust for zero-sized relocation member
8066
           section.  */
8067
0
        if (elf_sec->rel.hdr != NULL
8068
0
      && elf_sec->rel.hdr->sh_size == 0)
8069
0
          removed += 4;
8070
0
        if (elf_sec->rela.hdr != NULL
8071
0
      && elf_sec->rela.hdr->sh_size == 0)
8072
0
          removed += 4;
8073
0
      }
8074
0
        }
8075
0
      s = elf_next_in_group (s);
8076
0
      if (s == first)
8077
0
        break;
8078
0
    }
8079
0
  if (removed != 0)
8080
0
    {
8081
0
      if (discarded != NULL)
8082
0
        {
8083
    /* If we've been called for ld -r, then we need to
8084
       adjust the input section size.  */
8085
0
    if (isec->rawsize == 0)
8086
0
      isec->rawsize = isec->size;
8087
0
    isec->size = isec->rawsize - removed;
8088
0
    if (isec->size <= 4)
8089
0
      {
8090
0
        isec->size = 0;
8091
0
        isec->flags |= SEC_EXCLUDE;
8092
0
      }
8093
0
        }
8094
0
      else if (isec->output_section != NULL)
8095
0
        {
8096
    /* Adjust the output section size when called from
8097
       objcopy. */
8098
0
    isec->output_section->size -= removed;
8099
0
    if (isec->output_section->size <= 4)
8100
0
      {
8101
0
        isec->output_section->size = 0;
8102
0
        isec->output_section->flags |= SEC_EXCLUDE;
8103
0
      }
8104
0
        }
8105
0
    }
8106
0
      }
8107
8108
0
  return true;
8109
0
}
8110
8111
/* Copy private header information.  */
8112
8113
bool
8114
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8115
0
{
8116
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8117
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8118
0
    return true;
8119
8120
  /* Copy over private BFD data if it has not already been copied.
8121
     This must be done here, rather than in the copy_private_bfd_data
8122
     entry point, because the latter is called after the section
8123
     contents have been set, which means that the program headers have
8124
     already been worked out.  */
8125
0
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8126
0
    {
8127
0
      if (! copy_private_bfd_data (ibfd, obfd))
8128
0
  return false;
8129
0
    }
8130
8131
0
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8132
0
}
8133
8134
/* Copy private symbol information.  If this symbol is in a section
8135
   which we did not map into a BFD section, try to map the section
8136
   index correctly.  We use special macro definitions for the mapped
8137
   section indices; these definitions are interpreted by the
8138
   swap_out_syms function.  */
8139
8140
0
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8141
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8142
0
#define MAP_STRTAB    (SHN_HIOS + 3)
8143
0
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8144
0
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8145
8146
bool
8147
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8148
           asymbol *isymarg,
8149
           bfd *obfd,
8150
           asymbol *osymarg)
8151
0
{
8152
0
  elf_symbol_type *isym, *osym;
8153
8154
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8155
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8156
0
    return true;
8157
8158
0
  isym = elf_symbol_from (isymarg);
8159
0
  osym = elf_symbol_from (osymarg);
8160
8161
0
  if (isym != NULL
8162
0
      && isym->internal_elf_sym.st_shndx != 0
8163
0
      && osym != NULL
8164
0
      && bfd_is_abs_section (isym->symbol.section))
8165
0
    {
8166
0
      unsigned int shndx;
8167
8168
0
      shndx = isym->internal_elf_sym.st_shndx;
8169
0
      if (shndx == elf_onesymtab (ibfd))
8170
0
  shndx = MAP_ONESYMTAB;
8171
0
      else if (shndx == elf_dynsymtab (ibfd))
8172
0
  shndx = MAP_DYNSYMTAB;
8173
0
      else if (shndx == elf_strtab_sec (ibfd))
8174
0
  shndx = MAP_STRTAB;
8175
0
      else if (shndx == elf_shstrtab_sec (ibfd))
8176
0
  shndx = MAP_SHSTRTAB;
8177
0
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8178
0
  shndx = MAP_SYM_SHNDX;
8179
0
      osym->internal_elf_sym.st_shndx = shndx;
8180
0
    }
8181
8182
0
  return true;
8183
0
}
8184
8185
/* Swap out the symbols.  */
8186
8187
static bool
8188
swap_out_syms (bfd *abfd,
8189
         struct elf_strtab_hash **sttp,
8190
         int relocatable_p,
8191
         struct bfd_link_info *info)
8192
0
{
8193
0
  const struct elf_backend_data *bed;
8194
0
  unsigned int symcount;
8195
0
  asymbol **syms;
8196
0
  struct elf_strtab_hash *stt;
8197
0
  Elf_Internal_Shdr *symtab_hdr;
8198
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
8199
0
  Elf_Internal_Shdr *symstrtab_hdr;
8200
0
  struct elf_sym_strtab *symstrtab;
8201
0
  bfd_byte *outbound_syms;
8202
0
  bfd_byte *outbound_shndx;
8203
0
  unsigned long outbound_syms_index;
8204
0
  unsigned int idx;
8205
0
  unsigned int num_locals;
8206
0
  size_t amt;
8207
0
  bool name_local_sections;
8208
8209
0
  if (!elf_map_symbols (abfd, &num_locals))
8210
0
    return false;
8211
8212
  /* Dump out the symtabs.  */
8213
0
  stt = _bfd_elf_strtab_init ();
8214
0
  if (stt == NULL)
8215
0
    return false;
8216
8217
0
  bed = get_elf_backend_data (abfd);
8218
0
  symcount = bfd_get_symcount (abfd);
8219
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8220
0
  symtab_hdr->sh_type = SHT_SYMTAB;
8221
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8222
0
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8223
0
  symtab_hdr->sh_info = num_locals + 1;
8224
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8225
8226
0
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8227
0
  symstrtab_hdr->sh_type = SHT_STRTAB;
8228
8229
  /* Allocate buffer to swap out the .strtab section.  */
8230
0
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8231
0
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8232
0
    {
8233
0
      bfd_set_error (bfd_error_no_memory);
8234
0
      _bfd_elf_strtab_free (stt);
8235
0
      return false;
8236
0
    }
8237
8238
0
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8239
0
      || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8240
0
    {
8241
0
    error_no_mem:
8242
0
      bfd_set_error (bfd_error_no_memory);
8243
0
    error_return:
8244
0
      free (symstrtab);
8245
0
      _bfd_elf_strtab_free (stt);
8246
0
      return false;
8247
0
    }
8248
0
  symtab_hdr->contents = outbound_syms;
8249
0
  outbound_syms_index = 0;
8250
8251
0
  outbound_shndx = NULL;
8252
8253
0
  if (elf_symtab_shndx_list (abfd))
8254
0
    {
8255
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8256
0
      if (symtab_shndx_hdr->sh_name != 0)
8257
0
  {
8258
0
    if (_bfd_mul_overflow (symcount + 1,
8259
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8260
0
      goto error_no_mem;
8261
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8262
0
    if (outbound_shndx == NULL)
8263
0
      goto error_return;
8264
8265
0
    symtab_shndx_hdr->contents = outbound_shndx;
8266
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8267
0
    symtab_shndx_hdr->sh_size = amt;
8268
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8269
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8270
0
  }
8271
      /* FIXME: What about any other headers in the list ?  */
8272
0
    }
8273
8274
  /* Now generate the data (for "contents").  */
8275
0
  {
8276
    /* Fill in zeroth symbol and swap it out.  */
8277
0
    Elf_Internal_Sym sym;
8278
0
    sym.st_name = 0;
8279
0
    sym.st_value = 0;
8280
0
    sym.st_size = 0;
8281
0
    sym.st_info = 0;
8282
0
    sym.st_other = 0;
8283
0
    sym.st_shndx = SHN_UNDEF;
8284
0
    sym.st_target_internal = 0;
8285
0
    symstrtab[0].sym = sym;
8286
0
    symstrtab[0].dest_index = outbound_syms_index;
8287
0
    outbound_syms_index++;
8288
0
  }
8289
8290
0
  name_local_sections
8291
0
    = (bed->elf_backend_name_local_section_symbols
8292
0
       && bed->elf_backend_name_local_section_symbols (abfd));
8293
8294
0
  syms = bfd_get_outsymbols (abfd);
8295
0
  for (idx = 0; idx < symcount;)
8296
0
    {
8297
0
      Elf_Internal_Sym sym;
8298
0
      bfd_vma value = syms[idx]->value;
8299
0
      elf_symbol_type *type_ptr;
8300
0
      flagword flags = syms[idx]->flags;
8301
0
      int type;
8302
8303
0
      if (!name_local_sections
8304
0
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8305
0
  {
8306
    /* Local section symbols have no name.  */
8307
0
    sym.st_name = (unsigned long) -1;
8308
0
  }
8309
0
      else
8310
0
  {
8311
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8312
       to get the final offset for st_name.  */
8313
0
    sym.st_name
8314
0
      = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8315
0
               false);
8316
0
    if (sym.st_name == (unsigned long) -1)
8317
0
      goto error_return;
8318
0
  }
8319
8320
0
      type_ptr = elf_symbol_from (syms[idx]);
8321
8322
0
      if ((flags & BSF_SECTION_SYM) == 0
8323
0
    && bfd_is_com_section (syms[idx]->section))
8324
0
  {
8325
    /* ELF common symbols put the alignment into the `value' field,
8326
       and the size into the `size' field.  This is backwards from
8327
       how BFD handles it, so reverse it here.  */
8328
0
    sym.st_size = value;
8329
0
    if (type_ptr == NULL
8330
0
        || type_ptr->internal_elf_sym.st_value == 0)
8331
0
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8332
0
    else
8333
0
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8334
0
    sym.st_shndx = _bfd_elf_section_from_bfd_section
8335
0
      (abfd, syms[idx]->section);
8336
0
  }
8337
0
      else
8338
0
  {
8339
0
    asection *sec = syms[idx]->section;
8340
0
    unsigned int shndx;
8341
8342
0
    if (sec->output_section)
8343
0
      {
8344
0
        value += sec->output_offset;
8345
0
        sec = sec->output_section;
8346
0
      }
8347
8348
    /* Don't add in the section vma for relocatable output.  */
8349
0
    if (! relocatable_p)
8350
0
      value += sec->vma;
8351
0
    sym.st_value = value;
8352
0
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8353
8354
0
    if (bfd_is_abs_section (sec)
8355
0
        && type_ptr != NULL
8356
0
        && type_ptr->internal_elf_sym.st_shndx != 0)
8357
0
      {
8358
        /* This symbol is in a real ELF section which we did
8359
     not create as a BFD section.  Undo the mapping done
8360
     by copy_private_symbol_data.  */
8361
0
        shndx = type_ptr->internal_elf_sym.st_shndx;
8362
0
        switch (shndx)
8363
0
    {
8364
0
    case MAP_ONESYMTAB:
8365
0
      shndx = elf_onesymtab (abfd);
8366
0
      break;
8367
0
    case MAP_DYNSYMTAB:
8368
0
      shndx = elf_dynsymtab (abfd);
8369
0
      break;
8370
0
    case MAP_STRTAB:
8371
0
      shndx = elf_strtab_sec (abfd);
8372
0
      break;
8373
0
    case MAP_SHSTRTAB:
8374
0
      shndx = elf_shstrtab_sec (abfd);
8375
0
      break;
8376
0
    case MAP_SYM_SHNDX:
8377
0
      if (elf_symtab_shndx_list (abfd))
8378
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8379
0
      break;
8380
0
    case SHN_COMMON:
8381
0
    case SHN_ABS:
8382
0
      shndx = SHN_ABS;
8383
0
      break;
8384
0
    default:
8385
0
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8386
0
        {
8387
0
          if (bed->symbol_section_index)
8388
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8389
          /* Otherwise just leave the index alone.  */
8390
0
        }
8391
0
      else
8392
0
        {
8393
0
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8394
0
      _bfd_error_handler (_("%pB: \
8395
0
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8396
0
            abfd, shndx);
8397
0
          shndx = SHN_ABS;
8398
0
        }
8399
0
      break;
8400
0
    }
8401
0
      }
8402
0
    else
8403
0
      {
8404
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8405
8406
0
        if (shndx == SHN_BAD)
8407
0
    {
8408
0
      asection *sec2;
8409
8410
      /* Writing this would be a hell of a lot easier if
8411
         we had some decent documentation on bfd, and
8412
         knew what to expect of the library, and what to
8413
         demand of applications.  For example, it
8414
         appears that `objcopy' might not set the
8415
         section of a symbol to be a section that is
8416
         actually in the output file.  */
8417
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8418
0
      if (sec2 != NULL)
8419
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8420
0
      if (shndx == SHN_BAD)
8421
0
        {
8422
          /* xgettext:c-format */
8423
0
          _bfd_error_handler
8424
0
      (_("unable to find equivalent output section"
8425
0
         " for symbol '%s' from section '%s'"),
8426
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8427
0
       sec->name);
8428
0
          bfd_set_error (bfd_error_invalid_operation);
8429
0
          goto error_return;
8430
0
        }
8431
0
    }
8432
0
      }
8433
8434
0
    sym.st_shndx = shndx;
8435
0
  }
8436
8437
0
      if ((flags & BSF_THREAD_LOCAL) != 0)
8438
0
  type = STT_TLS;
8439
0
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8440
0
  type = STT_GNU_IFUNC;
8441
0
      else if ((flags & BSF_FUNCTION) != 0)
8442
0
  type = STT_FUNC;
8443
0
      else if ((flags & BSF_OBJECT) != 0)
8444
0
  type = STT_OBJECT;
8445
0
      else if ((flags & BSF_RELC) != 0)
8446
0
  type = STT_RELC;
8447
0
      else if ((flags & BSF_SRELC) != 0)
8448
0
  type = STT_SRELC;
8449
0
      else
8450
0
  type = STT_NOTYPE;
8451
8452
0
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8453
0
  type = STT_TLS;
8454
8455
      /* Processor-specific types.  */
8456
0
      if (type_ptr != NULL
8457
0
    && bed->elf_backend_get_symbol_type)
8458
0
  type = ((*bed->elf_backend_get_symbol_type)
8459
0
    (&type_ptr->internal_elf_sym, type));
8460
8461
0
      if (flags & BSF_SECTION_SYM)
8462
0
  {
8463
0
    if (flags & BSF_GLOBAL)
8464
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8465
0
    else
8466
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8467
0
  }
8468
0
      else if (bfd_is_com_section (syms[idx]->section))
8469
0
  {
8470
0
    if (type != STT_TLS)
8471
0
      {
8472
0
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8473
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8474
0
      ? STT_COMMON : STT_OBJECT);
8475
0
        else
8476
0
    type = ((flags & BSF_ELF_COMMON) != 0
8477
0
      ? STT_COMMON : STT_OBJECT);
8478
0
      }
8479
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8480
0
  }
8481
0
      else if (bfd_is_und_section (syms[idx]->section))
8482
0
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8483
0
            ? STB_WEAK
8484
0
            : STB_GLOBAL),
8485
0
           type);
8486
0
      else if (flags & BSF_FILE)
8487
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8488
0
      else
8489
0
  {
8490
0
    int bind = STB_LOCAL;
8491
8492
0
    if (flags & BSF_LOCAL)
8493
0
      bind = STB_LOCAL;
8494
0
    else if (flags & BSF_GNU_UNIQUE)
8495
0
      bind = STB_GNU_UNIQUE;
8496
0
    else if (flags & BSF_WEAK)
8497
0
      bind = STB_WEAK;
8498
0
    else if (flags & BSF_GLOBAL)
8499
0
      bind = STB_GLOBAL;
8500
8501
0
    sym.st_info = ELF_ST_INFO (bind, type);
8502
0
  }
8503
8504
0
      if (type_ptr != NULL)
8505
0
  {
8506
0
    sym.st_other = type_ptr->internal_elf_sym.st_other;
8507
0
    sym.st_target_internal
8508
0
      = type_ptr->internal_elf_sym.st_target_internal;
8509
0
  }
8510
0
      else
8511
0
  {
8512
0
    sym.st_other = 0;
8513
0
    sym.st_target_internal = 0;
8514
0
  }
8515
8516
0
      idx++;
8517
0
      symstrtab[idx].sym = sym;
8518
0
      symstrtab[idx].dest_index = outbound_syms_index;
8519
8520
0
      outbound_syms_index++;
8521
0
    }
8522
8523
  /* Finalize the .strtab section.  */
8524
0
  _bfd_elf_strtab_finalize (stt);
8525
8526
  /* Swap out the .strtab section.  */
8527
0
  for (idx = 0; idx <= symcount; idx++)
8528
0
    {
8529
0
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
8530
0
      if (elfsym->sym.st_name == (unsigned long) -1)
8531
0
  elfsym->sym.st_name = 0;
8532
0
      else
8533
0
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8534
0
                  elfsym->sym.st_name);
8535
0
      if (info && info->callbacks->ctf_new_symbol)
8536
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
8537
0
           &elfsym->sym);
8538
8539
      /* Inform the linker of the addition of this symbol.  */
8540
8541
0
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
8542
0
             (outbound_syms
8543
0
        + (elfsym->dest_index
8544
0
           * bed->s->sizeof_sym)),
8545
0
             NPTR_ADD (outbound_shndx,
8546
0
           (elfsym->dest_index
8547
0
            * sizeof (Elf_External_Sym_Shndx))));
8548
0
    }
8549
0
  free (symstrtab);
8550
8551
0
  *sttp = stt;
8552
0
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8553
0
  symstrtab_hdr->sh_type = SHT_STRTAB;
8554
0
  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8555
0
  symstrtab_hdr->sh_addr = 0;
8556
0
  symstrtab_hdr->sh_entsize = 0;
8557
0
  symstrtab_hdr->sh_link = 0;
8558
0
  symstrtab_hdr->sh_info = 0;
8559
0
  symstrtab_hdr->sh_addralign = 1;
8560
8561
0
  return true;
8562
0
}
8563
8564
/* Return the number of bytes required to hold the symtab vector.
8565
8566
   Note that we base it on the count plus 1, since we will null terminate
8567
   the vector allocated based on this size.  However, the ELF symbol table
8568
   always has a dummy entry as symbol #0, so it ends up even.  */
8569
8570
long
8571
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
8572
0
{
8573
0
  bfd_size_type symcount;
8574
0
  long symtab_size;
8575
0
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8576
8577
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8578
0
  if (symcount > LONG_MAX / sizeof (asymbol *))
8579
0
    {
8580
0
      bfd_set_error (bfd_error_file_too_big);
8581
0
      return -1;
8582
0
    }
8583
0
  symtab_size = symcount * (sizeof (asymbol *));
8584
0
  if (symcount == 0)
8585
0
    symtab_size = sizeof (asymbol *);
8586
0
  else if (!bfd_write_p (abfd))
8587
0
    {
8588
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
8589
8590
0
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
8591
0
  {
8592
0
    bfd_set_error (bfd_error_file_truncated);
8593
0
    return -1;
8594
0
  }
8595
0
    }
8596
8597
0
  return symtab_size;
8598
0
}
8599
8600
long
8601
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8602
0
{
8603
0
  bfd_size_type symcount;
8604
0
  long symtab_size;
8605
0
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8606
8607
0
  if (elf_dynsymtab (abfd) == 0)
8608
0
    {
8609
0
      bfd_set_error (bfd_error_invalid_operation);
8610
0
      return -1;
8611
0
    }
8612
8613
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8614
0
  if (symcount > LONG_MAX / sizeof (asymbol *))
8615
0
    {
8616
0
      bfd_set_error (bfd_error_file_too_big);
8617
0
      return -1;
8618
0
    }
8619
0
  symtab_size = symcount * (sizeof (asymbol *));
8620
0
  if (symcount == 0)
8621
0
    symtab_size = sizeof (asymbol *);
8622
0
  else if (!bfd_write_p (abfd))
8623
0
    {
8624
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
8625
8626
0
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
8627
0
  {
8628
0
    bfd_set_error (bfd_error_file_truncated);
8629
0
    return -1;
8630
0
  }
8631
0
    }
8632
8633
0
  return symtab_size;
8634
0
}
8635
8636
long
8637
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8638
0
{
8639
0
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8640
0
    {
8641
      /* Sanity check reloc section size.  */
8642
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
8643
8644
0
      if (filesize != 0)
8645
0
  {
8646
0
    struct bfd_elf_section_data *d = elf_section_data (asect);
8647
0
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8648
0
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8649
8650
0
    if (rel_size + rela_size > filesize
8651
0
        || rel_size + rela_size < rel_size)
8652
0
      {
8653
0
        bfd_set_error (bfd_error_file_truncated);
8654
0
        return -1;
8655
0
      }
8656
0
  }
8657
0
    }
8658
8659
#if SIZEOF_LONG == SIZEOF_INT
8660
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8661
    {
8662
      bfd_set_error (bfd_error_file_too_big);
8663
      return -1;
8664
    }
8665
#endif
8666
0
  return (asect->reloc_count + 1L) * sizeof (arelent *);
8667
0
}
8668
8669
/* Canonicalize the relocs.  */
8670
8671
long
8672
_bfd_elf_canonicalize_reloc (bfd *abfd,
8673
           sec_ptr section,
8674
           arelent **relptr,
8675
           asymbol **symbols)
8676
0
{
8677
0
  arelent *tblptr;
8678
0
  unsigned int i;
8679
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8680
8681
0
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8682
0
    return -1;
8683
8684
0
  tblptr = section->relocation;
8685
0
  for (i = 0; i < section->reloc_count; i++)
8686
0
    *relptr++ = tblptr++;
8687
8688
0
  *relptr = NULL;
8689
8690
0
  return section->reloc_count;
8691
0
}
8692
8693
long
8694
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8695
0
{
8696
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8697
0
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8698
8699
0
  if (symcount >= 0)
8700
0
    abfd->symcount = symcount;
8701
0
  return symcount;
8702
0
}
8703
8704
long
8705
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8706
              asymbol **allocation)
8707
0
{
8708
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8709
0
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8710
8711
0
  if (symcount >= 0)
8712
0
    abfd->dynsymcount = symcount;
8713
0
  return symcount;
8714
0
}
8715
8716
/* Return the size required for the dynamic reloc entries.  Any loadable
8717
   section that was actually installed in the BFD, and has type SHT_REL
8718
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8719
   dynamic reloc section.  */
8720
8721
long
8722
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8723
0
{
8724
0
  bfd_size_type count, ext_rel_size;
8725
0
  asection *s;
8726
8727
0
  if (elf_dynsymtab (abfd) == 0)
8728
0
    {
8729
0
      bfd_set_error (bfd_error_invalid_operation);
8730
0
      return -1;
8731
0
    }
8732
8733
0
  count = 1;
8734
0
  ext_rel_size = 0;
8735
0
  for (s = abfd->sections; s != NULL; s = s->next)
8736
0
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8737
0
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8738
0
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8739
0
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8740
0
      {
8741
0
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
8742
0
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
8743
0
    {
8744
0
      bfd_set_error (bfd_error_file_truncated);
8745
0
      return -1;
8746
0
    }
8747
0
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8748
0
  if (count > LONG_MAX / sizeof (arelent *))
8749
0
    {
8750
0
      bfd_set_error (bfd_error_file_too_big);
8751
0
      return -1;
8752
0
    }
8753
0
      }
8754
0
  if (count > 1 && !bfd_write_p (abfd))
8755
0
    {
8756
      /* Sanity check reloc section sizes.  */
8757
0
      ufile_ptr filesize = bfd_get_file_size (abfd);
8758
0
      if (filesize != 0 && ext_rel_size > filesize)
8759
0
  {
8760
0
    bfd_set_error (bfd_error_file_truncated);
8761
0
    return -1;
8762
0
  }
8763
0
    }
8764
0
  return count * sizeof (arelent *);
8765
0
}
8766
8767
/* Canonicalize the dynamic relocation entries.  Note that we return the
8768
   dynamic relocations as a single block, although they are actually
8769
   associated with particular sections; the interface, which was
8770
   designed for SunOS style shared libraries, expects that there is only
8771
   one set of dynamic relocs.  Any loadable section that was actually
8772
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8773
   dynamic symbol table, is considered to be a dynamic reloc section.  */
8774
8775
long
8776
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8777
             arelent **storage,
8778
             asymbol **syms)
8779
0
{
8780
0
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8781
0
  asection *s;
8782
0
  long ret;
8783
8784
0
  if (elf_dynsymtab (abfd) == 0)
8785
0
    {
8786
0
      bfd_set_error (bfd_error_invalid_operation);
8787
0
      return -1;
8788
0
    }
8789
8790
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8791
0
  ret = 0;
8792
0
  for (s = abfd->sections; s != NULL; s = s->next)
8793
0
    {
8794
0
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8795
0
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8796
0
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8797
0
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8798
0
  {
8799
0
    arelent *p;
8800
0
    long count, i;
8801
8802
0
    if (! (*slurp_relocs) (abfd, s, syms, true))
8803
0
      return -1;
8804
0
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8805
0
    p = s->relocation;
8806
0
    for (i = 0; i < count; i++)
8807
0
      *storage++ = p++;
8808
0
    ret += count;
8809
0
  }
8810
0
    }
8811
8812
0
  *storage = NULL;
8813
8814
0
  return ret;
8815
0
}
8816

8817
/* Read in the version information.  */
8818
8819
bool
8820
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8821
0
{
8822
0
  bfd_byte *contents = NULL;
8823
0
  unsigned int freeidx = 0;
8824
0
  size_t amt;
8825
8826
0
  if (elf_dynverref (abfd) != 0)
8827
0
    {
8828
0
      Elf_Internal_Shdr *hdr;
8829
0
      Elf_External_Verneed *everneed;
8830
0
      Elf_Internal_Verneed *iverneed;
8831
0
      unsigned int i;
8832
0
      bfd_byte *contents_end;
8833
8834
0
      hdr = &elf_tdata (abfd)->dynverref_hdr;
8835
8836
0
      if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8837
0
  {
8838
0
  error_return_bad_verref:
8839
0
    _bfd_error_handler
8840
0
      (_("%pB: .gnu.version_r invalid entry"), abfd);
8841
0
    bfd_set_error (bfd_error_bad_value);
8842
0
  error_return_verref:
8843
0
    elf_tdata (abfd)->verref = NULL;
8844
0
    elf_tdata (abfd)->cverrefs = 0;
8845
0
    goto error_return;
8846
0
  }
8847
8848
0
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8849
0
  goto error_return_verref;
8850
0
      contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8851
0
      if (contents == NULL)
8852
0
  goto error_return_verref;
8853
8854
0
      if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8855
0
  {
8856
0
    bfd_set_error (bfd_error_file_too_big);
8857
0
    goto error_return_verref;
8858
0
  }
8859
0
      if (amt == 0)
8860
0
  goto error_return_verref;
8861
0
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
8862
0
      if (elf_tdata (abfd)->verref == NULL)
8863
0
  goto error_return_verref;
8864
8865
0
      BFD_ASSERT (sizeof (Elf_External_Verneed)
8866
0
      == sizeof (Elf_External_Vernaux));
8867
0
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8868
0
      everneed = (Elf_External_Verneed *) contents;
8869
0
      iverneed = elf_tdata (abfd)->verref;
8870
0
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
8871
0
  {
8872
0
    Elf_External_Vernaux *evernaux;
8873
0
    Elf_Internal_Vernaux *ivernaux;
8874
0
    unsigned int j;
8875
8876
0
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8877
8878
0
    iverneed->vn_bfd = abfd;
8879
8880
0
    iverneed->vn_filename =
8881
0
      bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8882
0
               iverneed->vn_file);
8883
0
    if (iverneed->vn_filename == NULL)
8884
0
      goto error_return_bad_verref;
8885
8886
0
    if (iverneed->vn_cnt == 0)
8887
0
      iverneed->vn_auxptr = NULL;
8888
0
    else
8889
0
      {
8890
0
        if (_bfd_mul_overflow (iverneed->vn_cnt,
8891
0
             sizeof (Elf_Internal_Vernaux), &amt))
8892
0
    {
8893
0
      bfd_set_error (bfd_error_file_too_big);
8894
0
      goto error_return_verref;
8895
0
    }
8896
0
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8897
0
    bfd_alloc (abfd, amt);
8898
0
        if (iverneed->vn_auxptr == NULL)
8899
0
    goto error_return_verref;
8900
0
      }
8901
8902
0
    if (iverneed->vn_aux
8903
0
        > (size_t) (contents_end - (bfd_byte *) everneed))
8904
0
      goto error_return_bad_verref;
8905
8906
0
    evernaux = ((Elf_External_Vernaux *)
8907
0
          ((bfd_byte *) everneed + iverneed->vn_aux));
8908
0
    ivernaux = iverneed->vn_auxptr;
8909
0
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8910
0
      {
8911
0
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8912
8913
0
        ivernaux->vna_nodename =
8914
0
    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8915
0
             ivernaux->vna_name);
8916
0
        if (ivernaux->vna_nodename == NULL)
8917
0
    goto error_return_bad_verref;
8918
8919
0
        if (ivernaux->vna_other > freeidx)
8920
0
    freeidx = ivernaux->vna_other;
8921
8922
0
        ivernaux->vna_nextptr = NULL;
8923
0
        if (ivernaux->vna_next == 0)
8924
0
    {
8925
0
      iverneed->vn_cnt = j + 1;
8926
0
      break;
8927
0
    }
8928
0
        if (j + 1 < iverneed->vn_cnt)
8929
0
    ivernaux->vna_nextptr = ivernaux + 1;
8930
8931
0
        if (ivernaux->vna_next
8932
0
      > (size_t) (contents_end - (bfd_byte *) evernaux))
8933
0
    goto error_return_bad_verref;
8934
8935
0
        evernaux = ((Elf_External_Vernaux *)
8936
0
        ((bfd_byte *) evernaux + ivernaux->vna_next));
8937
0
      }
8938
8939
0
    iverneed->vn_nextref = NULL;
8940
0
    if (iverneed->vn_next == 0)
8941
0
      break;
8942
0
    if (i + 1 < hdr->sh_info)
8943
0
      iverneed->vn_nextref = iverneed + 1;
8944
8945
0
    if (iverneed->vn_next
8946
0
        > (size_t) (contents_end - (bfd_byte *) everneed))
8947
0
      goto error_return_bad_verref;
8948
8949
0
    everneed = ((Elf_External_Verneed *)
8950
0
          ((bfd_byte *) everneed + iverneed->vn_next));
8951
0
  }
8952
0
      elf_tdata (abfd)->cverrefs = i;
8953
8954
0
      free (contents);
8955
0
      contents = NULL;
8956
0
    }
8957
8958
0
  if (elf_dynverdef (abfd) != 0)
8959
0
    {
8960
0
      Elf_Internal_Shdr *hdr;
8961
0
      Elf_External_Verdef *everdef;
8962
0
      Elf_Internal_Verdef *iverdef;
8963
0
      Elf_Internal_Verdef *iverdefarr;
8964
0
      Elf_Internal_Verdef iverdefmem;
8965
0
      unsigned int i;
8966
0
      unsigned int maxidx;
8967
0
      bfd_byte *contents_end_def, *contents_end_aux;
8968
8969
0
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
8970
8971
0
      if (hdr->sh_size < sizeof (Elf_External_Verdef))
8972
0
  {
8973
0
  error_return_bad_verdef:
8974
0
    _bfd_error_handler
8975
0
      (_("%pB: .gnu.version_d invalid entry"), abfd);
8976
0
    bfd_set_error (bfd_error_bad_value);
8977
0
  error_return_verdef:
8978
0
    elf_tdata (abfd)->verdef = NULL;
8979
0
    elf_tdata (abfd)->cverdefs = 0;
8980
0
    goto error_return;
8981
0
  }
8982
8983
0
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8984
0
  goto error_return_verdef;
8985
0
      contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8986
0
      if (contents == NULL)
8987
0
  goto error_return_verdef;
8988
8989
0
      BFD_ASSERT (sizeof (Elf_External_Verdef)
8990
0
      >= sizeof (Elf_External_Verdaux));
8991
0
      contents_end_def = contents + hdr->sh_size
8992
0
       - sizeof (Elf_External_Verdef);
8993
0
      contents_end_aux = contents + hdr->sh_size
8994
0
       - sizeof (Elf_External_Verdaux);
8995
8996
      /* We know the number of entries in the section but not the maximum
8997
   index.  Therefore we have to run through all entries and find
8998
   the maximum.  */
8999
0
      everdef = (Elf_External_Verdef *) contents;
9000
0
      maxidx = 0;
9001
0
      for (i = 0; i < hdr->sh_info; ++i)
9002
0
  {
9003
0
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9004
9005
0
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9006
0
      goto error_return_bad_verdef;
9007
0
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9008
0
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9009
9010
0
    if (iverdefmem.vd_next == 0)
9011
0
      break;
9012
9013
0
    if (iverdefmem.vd_next
9014
0
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9015
0
      goto error_return_bad_verdef;
9016
9017
0
    everdef = ((Elf_External_Verdef *)
9018
0
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9019
0
  }
9020
9021
0
      if (default_imported_symver)
9022
0
  {
9023
0
    if (freeidx > maxidx)
9024
0
      maxidx = ++freeidx;
9025
0
    else
9026
0
      freeidx = ++maxidx;
9027
0
  }
9028
0
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9029
0
  {
9030
0
    bfd_set_error (bfd_error_file_too_big);
9031
0
    goto error_return_verdef;
9032
0
  }
9033
9034
0
      if (amt == 0)
9035
0
  goto error_return_verdef;
9036
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9037
0
      if (elf_tdata (abfd)->verdef == NULL)
9038
0
  goto error_return_verdef;
9039
9040
0
      elf_tdata (abfd)->cverdefs = maxidx;
9041
9042
0
      everdef = (Elf_External_Verdef *) contents;
9043
0
      iverdefarr = elf_tdata (abfd)->verdef;
9044
0
      for (i = 0; i < hdr->sh_info; i++)
9045
0
  {
9046
0
    Elf_External_Verdaux *everdaux;
9047
0
    Elf_Internal_Verdaux *iverdaux;
9048
0
    unsigned int j;
9049
9050
0
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9051
9052
0
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9053
0
      goto error_return_bad_verdef;
9054
9055
0
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9056
0
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9057
9058
0
    iverdef->vd_bfd = abfd;
9059
9060
0
    if (iverdef->vd_cnt == 0)
9061
0
      iverdef->vd_auxptr = NULL;
9062
0
    else
9063
0
      {
9064
0
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9065
0
             sizeof (Elf_Internal_Verdaux), &amt))
9066
0
    {
9067
0
      bfd_set_error (bfd_error_file_too_big);
9068
0
      goto error_return_verdef;
9069
0
    }
9070
0
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9071
0
    bfd_alloc (abfd, amt);
9072
0
        if (iverdef->vd_auxptr == NULL)
9073
0
    goto error_return_verdef;
9074
0
      }
9075
9076
0
    if (iverdef->vd_aux
9077
0
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9078
0
      goto error_return_bad_verdef;
9079
9080
0
    everdaux = ((Elf_External_Verdaux *)
9081
0
          ((bfd_byte *) everdef + iverdef->vd_aux));
9082
0
    iverdaux = iverdef->vd_auxptr;
9083
0
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9084
0
      {
9085
0
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9086
9087
0
        iverdaux->vda_nodename =
9088
0
    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9089
0
             iverdaux->vda_name);
9090
0
        if (iverdaux->vda_nodename == NULL)
9091
0
    goto error_return_bad_verdef;
9092
9093
0
        iverdaux->vda_nextptr = NULL;
9094
0
        if (iverdaux->vda_next == 0)
9095
0
    {
9096
0
      iverdef->vd_cnt = j + 1;
9097
0
      break;
9098
0
    }
9099
0
        if (j + 1 < iverdef->vd_cnt)
9100
0
    iverdaux->vda_nextptr = iverdaux + 1;
9101
9102
0
        if (iverdaux->vda_next
9103
0
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9104
0
    goto error_return_bad_verdef;
9105
9106
0
        everdaux = ((Elf_External_Verdaux *)
9107
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9108
0
      }
9109
9110
0
    iverdef->vd_nodename = NULL;
9111
0
    if (iverdef->vd_cnt)
9112
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9113
9114
0
    iverdef->vd_nextdef = NULL;
9115
0
    if (iverdef->vd_next == 0)
9116
0
      break;
9117
0
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9118
0
      iverdef->vd_nextdef = iverdef + 1;
9119
9120
0
    everdef = ((Elf_External_Verdef *)
9121
0
         ((bfd_byte *) everdef + iverdef->vd_next));
9122
0
  }
9123
9124
0
      free (contents);
9125
0
      contents = NULL;
9126
0
    }
9127
0
  else if (default_imported_symver)
9128
0
    {
9129
0
      if (freeidx < 3)
9130
0
  freeidx = 3;
9131
0
      else
9132
0
  freeidx++;
9133
9134
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9135
0
  {
9136
0
    bfd_set_error (bfd_error_file_too_big);
9137
0
    goto error_return;
9138
0
  }
9139
0
      if (amt == 0)
9140
0
  goto error_return;
9141
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9142
0
      if (elf_tdata (abfd)->verdef == NULL)
9143
0
  goto error_return;
9144
9145
0
      elf_tdata (abfd)->cverdefs = freeidx;
9146
0
    }
9147
9148
  /* Create a default version based on the soname.  */
9149
0
  if (default_imported_symver)
9150
0
    {
9151
0
      Elf_Internal_Verdef *iverdef;
9152
0
      Elf_Internal_Verdaux *iverdaux;
9153
9154
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9155
9156
0
      iverdef->vd_version = VER_DEF_CURRENT;
9157
0
      iverdef->vd_flags = 0;
9158
0
      iverdef->vd_ndx = freeidx;
9159
0
      iverdef->vd_cnt = 1;
9160
9161
0
      iverdef->vd_bfd = abfd;
9162
9163
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9164
0
      if (iverdef->vd_nodename == NULL)
9165
0
  goto error_return_verdef;
9166
0
      iverdef->vd_nextdef = NULL;
9167
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9168
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9169
0
      if (iverdef->vd_auxptr == NULL)
9170
0
  goto error_return_verdef;
9171
9172
0
      iverdaux = iverdef->vd_auxptr;
9173
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9174
0
    }
9175
9176
0
  return true;
9177
9178
0
 error_return:
9179
0
  free (contents);
9180
0
  return false;
9181
0
}
9182

9183
asymbol *
9184
_bfd_elf_make_empty_symbol (bfd *abfd)
9185
323k
{
9186
323k
  elf_symbol_type *newsym;
9187
9188
323k
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9189
323k
  if (!newsym)
9190
0
    return NULL;
9191
323k
  newsym->symbol.the_bfd = abfd;
9192
323k
  return &newsym->symbol;
9193
323k
}
9194
9195
void
9196
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9197
        asymbol *symbol,
9198
        symbol_info *ret)
9199
0
{
9200
0
  bfd_symbol_info (symbol, ret);
9201
0
}
9202
9203
/* Return whether a symbol name implies a local symbol.  Most targets
9204
   use this function for the is_local_label_name entry point, but some
9205
   override it.  */
9206
9207
bool
9208
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9209
            const char *name)
9210
0
{
9211
  /* Normal local symbols start with ``.L''.  */
9212
0
  if (name[0] == '.' && name[1] == 'L')
9213
0
    return true;
9214
9215
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9216
     DWARF debugging symbols starting with ``..''.  */
9217
0
  if (name[0] == '.' && name[1] == '.')
9218
0
    return true;
9219
9220
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9221
     emitting DWARF debugging output.  I suspect this is actually a
9222
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9223
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9224
     underscore to be emitted on some ELF targets).  For ease of use,
9225
     we treat such symbols as local.  */
9226
0
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9227
0
    return true;
9228
9229
  /* Treat assembler generated fake symbols, dollar local labels and
9230
     forward-backward labels (aka local labels) as locals.
9231
     These labels have the form:
9232
9233
       L0^A.*              (fake symbols)
9234
9235
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9236
9237
     Versions which start with .L will have already been matched above,
9238
     so we only need to match the rest.  */
9239
0
  if (name[0] == 'L' && ISDIGIT (name[1]))
9240
0
    {
9241
0
      bool ret = false;
9242
0
      const char * p;
9243
0
      char c;
9244
9245
0
      for (p = name + 2; (c = *p); p++)
9246
0
  {
9247
0
    if (c == 1 || c == 2)
9248
0
      {
9249
0
        if (c == 1 && p == name + 2)
9250
    /* A fake symbol.  */
9251
0
    return true;
9252
9253
        /* FIXME: We are being paranoid here and treating symbols like
9254
     L0^Bfoo as if there were non-local, on the grounds that the
9255
     assembler will never generate them.  But can any symbol
9256
     containing an ASCII value in the range 1-31 ever be anything
9257
     other than some kind of local ?  */
9258
0
        ret = true;
9259
0
      }
9260
9261
0
    if (! ISDIGIT (c))
9262
0
      {
9263
0
        ret = false;
9264
0
        break;
9265
0
      }
9266
0
  }
9267
0
      return ret;
9268
0
    }
9269
9270
0
  return false;
9271
0
}
9272
9273
alent *
9274
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9275
         asymbol *symbol ATTRIBUTE_UNUSED)
9276
0
{
9277
0
  abort ();
9278
0
  return NULL;
9279
0
}
9280
9281
bool
9282
_bfd_elf_set_arch_mach (bfd *abfd,
9283
      enum bfd_architecture arch,
9284
      unsigned long machine)
9285
0
{
9286
  /* If this isn't the right architecture for this backend, and this
9287
     isn't the generic backend, fail.  */
9288
0
  if (arch != get_elf_backend_data (abfd)->arch
9289
0
      && arch != bfd_arch_unknown
9290
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9291
0
    return false;
9292
9293
0
  return bfd_default_set_arch_mach (abfd, arch, machine);
9294
0
}
9295
9296
/* Find the nearest line to a particular section and offset,
9297
   for error reporting.  */
9298
9299
bool
9300
_bfd_elf_find_nearest_line (bfd *abfd,
9301
          asymbol **symbols,
9302
          asection *section,
9303
          bfd_vma offset,
9304
          const char **filename_ptr,
9305
          const char **functionname_ptr,
9306
          unsigned int *line_ptr,
9307
          unsigned int *discriminator_ptr)
9308
0
{
9309
0
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9310
0
                offset, filename_ptr,
9311
0
                functionname_ptr, line_ptr,
9312
0
                discriminator_ptr);
9313
0
}
9314
9315
/* Find the nearest line to a particular section and offset,
9316
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9317
   can be optionally specified.  */
9318
9319
bool
9320
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9321
             const char *alt_filename,
9322
             asymbol **symbols,
9323
             asection *section,
9324
             bfd_vma offset,
9325
             const char **filename_ptr,
9326
             const char **functionname_ptr,
9327
             unsigned int *line_ptr,
9328
             unsigned int *discriminator_ptr)
9329
0
{
9330
0
  bool found;
9331
9332
0
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9333
0
                section, offset, filename_ptr,
9334
0
                functionname_ptr, line_ptr,
9335
0
                discriminator_ptr,
9336
0
                dwarf_debug_sections,
9337
0
                &elf_tdata (abfd)->dwarf2_find_line_info))
9338
0
    return true;
9339
9340
0
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9341
0
             filename_ptr, functionname_ptr, line_ptr))
9342
0
    {
9343
0
      if (!*functionname_ptr)
9344
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9345
0
        *filename_ptr ? NULL : filename_ptr,
9346
0
        functionname_ptr);
9347
0
      return true;
9348
0
    }
9349
9350
0
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9351
0
               &found, filename_ptr,
9352
0
               functionname_ptr, line_ptr,
9353
0
               &elf_tdata (abfd)->line_info))
9354
0
    return false;
9355
0
  if (found && (*functionname_ptr || *line_ptr))
9356
0
    return true;
9357
9358
0
  if (symbols == NULL)
9359
0
    return false;
9360
9361
0
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9362
0
        filename_ptr, functionname_ptr))
9363
0
    return false;
9364
9365
0
  *line_ptr = 0;
9366
0
  return true;
9367
0
}
9368
9369
/* Find the line for a symbol.  */
9370
9371
bool
9372
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9373
        const char **filename_ptr, unsigned int *line_ptr)
9374
0
{
9375
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9376
0
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9377
0
          filename_ptr, NULL, line_ptr, NULL,
9378
0
          dwarf_debug_sections,
9379
0
          &tdata->dwarf2_find_line_info);
9380
0
}
9381
9382
/* After a call to bfd_find_nearest_line, successive calls to
9383
   bfd_find_inliner_info can be used to get source information about
9384
   each level of function inlining that terminated at the address
9385
   passed to bfd_find_nearest_line.  Currently this is only supported
9386
   for DWARF2 with appropriate DWARF3 extensions. */
9387
9388
bool
9389
_bfd_elf_find_inliner_info (bfd *abfd,
9390
          const char **filename_ptr,
9391
          const char **functionname_ptr,
9392
          unsigned int *line_ptr)
9393
0
{
9394
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9395
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9396
0
          functionname_ptr, line_ptr,
9397
0
          &tdata->dwarf2_find_line_info);
9398
0
}
9399
9400
int
9401
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9402
0
{
9403
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9404
0
  int ret = bed->s->sizeof_ehdr;
9405
9406
0
  if (!bfd_link_relocatable (info))
9407
0
    {
9408
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
9409
9410
0
      if (phdr_size == (bfd_size_type) -1)
9411
0
  {
9412
0
    struct elf_segment_map *m;
9413
9414
0
    phdr_size = 0;
9415
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9416
0
      phdr_size += bed->s->sizeof_phdr;
9417
9418
0
    if (phdr_size == 0)
9419
0
      phdr_size = get_program_header_size (abfd, info);
9420
0
  }
9421
9422
0
      elf_program_header_size (abfd) = phdr_size;
9423
0
      ret += phdr_size;
9424
0
    }
9425
9426
0
  return ret;
9427
0
}
9428
9429
bool
9430
_bfd_elf_set_section_contents (bfd *abfd,
9431
             sec_ptr section,
9432
             const void *location,
9433
             file_ptr offset,
9434
             bfd_size_type count)
9435
0
{
9436
0
  Elf_Internal_Shdr *hdr;
9437
9438
0
  if (! abfd->output_has_begun
9439
0
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9440
0
    return false;
9441
9442
0
  if (!count)
9443
0
    return true;
9444
9445
0
  hdr = &elf_section_data (section)->this_hdr;
9446
0
  if (hdr->sh_offset == (file_ptr) -1)
9447
0
    {
9448
0
      unsigned char *contents;
9449
9450
0
      if (bfd_section_is_ctf (section))
9451
  /* Nothing to do with this section: the contents are generated
9452
     later.  */
9453
0
  return true;
9454
9455
0
      if ((offset + count) > hdr->sh_size)
9456
0
  {
9457
0
    _bfd_error_handler
9458
0
      (_("%pB:%pA: error: attempting to write"
9459
0
         " over the end of the section"),
9460
0
       abfd, section);
9461
9462
0
    bfd_set_error (bfd_error_invalid_operation);
9463
0
    return false;
9464
0
  }
9465
9466
0
      contents = hdr->contents;
9467
0
      if (contents == NULL)
9468
0
  {
9469
0
    _bfd_error_handler
9470
0
      (_("%pB:%pA: error: attempting to write"
9471
0
         " section into an empty buffer"),
9472
0
       abfd, section);
9473
9474
0
    bfd_set_error (bfd_error_invalid_operation);
9475
0
    return false;
9476
0
  }
9477
9478
0
      memcpy (contents + offset, location, count);
9479
0
      return true;
9480
0
    }
9481
9482
0
  return _bfd_generic_set_section_contents (abfd, section,
9483
0
              location, offset, count);
9484
0
}
9485
9486
bool
9487
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9488
         arelent *cache_ptr ATTRIBUTE_UNUSED,
9489
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9490
0
{
9491
0
  abort ();
9492
0
  return false;
9493
0
}
9494
9495
/* Try to convert a non-ELF reloc into an ELF one.  */
9496
9497
bool
9498
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9499
0
{
9500
  /* Check whether we really have an ELF howto.  */
9501
9502
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9503
0
    {
9504
0
      bfd_reloc_code_real_type code;
9505
0
      reloc_howto_type *howto;
9506
9507
      /* Alien reloc: Try to determine its type to replace it with an
9508
   equivalent ELF reloc.  */
9509
9510
0
      if (areloc->howto->pc_relative)
9511
0
  {
9512
0
    switch (areloc->howto->bitsize)
9513
0
      {
9514
0
      case 8:
9515
0
        code = BFD_RELOC_8_PCREL;
9516
0
        break;
9517
0
      case 12:
9518
0
        code = BFD_RELOC_12_PCREL;
9519
0
        break;
9520
0
      case 16:
9521
0
        code = BFD_RELOC_16_PCREL;
9522
0
        break;
9523
0
      case 24:
9524
0
        code = BFD_RELOC_24_PCREL;
9525
0
        break;
9526
0
      case 32:
9527
0
        code = BFD_RELOC_32_PCREL;
9528
0
        break;
9529
0
      case 64:
9530
0
        code = BFD_RELOC_64_PCREL;
9531
0
        break;
9532
0
      default:
9533
0
        goto fail;
9534
0
      }
9535
9536
0
    howto = bfd_reloc_type_lookup (abfd, code);
9537
9538
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9539
0
      {
9540
0
        if (howto->pcrel_offset)
9541
0
    areloc->addend += areloc->address;
9542
0
        else
9543
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
9544
0
      }
9545
0
  }
9546
0
      else
9547
0
  {
9548
0
    switch (areloc->howto->bitsize)
9549
0
      {
9550
0
      case 8:
9551
0
        code = BFD_RELOC_8;
9552
0
        break;
9553
0
      case 14:
9554
0
        code = BFD_RELOC_14;
9555
0
        break;
9556
0
      case 16:
9557
0
        code = BFD_RELOC_16;
9558
0
        break;
9559
0
      case 26:
9560
0
        code = BFD_RELOC_26;
9561
0
        break;
9562
0
      case 32:
9563
0
        code = BFD_RELOC_32;
9564
0
        break;
9565
0
      case 64:
9566
0
        code = BFD_RELOC_64;
9567
0
        break;
9568
0
      default:
9569
0
        goto fail;
9570
0
      }
9571
9572
0
    howto = bfd_reloc_type_lookup (abfd, code);
9573
0
  }
9574
9575
0
      if (howto)
9576
0
  areloc->howto = howto;
9577
0
      else
9578
0
  goto fail;
9579
0
    }
9580
9581
0
  return true;
9582
9583
0
 fail:
9584
  /* xgettext:c-format */
9585
0
  _bfd_error_handler (_("%pB: %s unsupported"),
9586
0
          abfd, areloc->howto->name);
9587
0
  bfd_set_error (bfd_error_sorry);
9588
0
  return false;
9589
0
}
9590
9591
bool
9592
_bfd_elf_free_cached_info (bfd *abfd)
9593
227k
{
9594
227k
  struct elf_obj_tdata *tdata;
9595
9596
227k
  if ((bfd_get_format (abfd) == bfd_object
9597
227k
       || bfd_get_format (abfd) == bfd_core)
9598
227k
      && (tdata = elf_tdata (abfd)) != NULL)
9599
92.7k
    {
9600
92.7k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
9601
0
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
9602
92.7k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9603
92.7k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
9604
92.7k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
9605
92.7k
    }
9606
9607
227k
  return _bfd_generic_bfd_free_cached_info (abfd);
9608
227k
}
9609
9610
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9611
   in the relocation's offset.  Thus we cannot allow any sort of sanity
9612
   range-checking to interfere.  There is nothing else to do in processing
9613
   this reloc.  */
9614
9615
bfd_reloc_status_type
9616
_bfd_elf_rel_vtable_reloc_fn
9617
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9618
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9619
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9620
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9621
0
{
9622
0
  return bfd_reloc_ok;
9623
0
}
9624

9625
/* Elf core file support.  Much of this only works on native
9626
   toolchains, since we rely on knowing the
9627
   machine-dependent procfs structure in order to pick
9628
   out details about the corefile.  */
9629
9630
#ifdef HAVE_SYS_PROCFS_H
9631
# include <sys/procfs.h>
9632
#endif
9633
9634
/* Return a PID that identifies a "thread" for threaded cores, or the
9635
   PID of the main process for non-threaded cores.  */
9636
9637
static int
9638
elfcore_make_pid (bfd *abfd)
9639
0
{
9640
0
  int pid;
9641
9642
0
  pid = elf_tdata (abfd)->core->lwpid;
9643
0
  if (pid == 0)
9644
0
    pid = elf_tdata (abfd)->core->pid;
9645
9646
0
  return pid;
9647
0
}
9648
9649
/* If there isn't a section called NAME, make one, using data from
9650
   SECT.  Note, this function will generate a reference to NAME, so
9651
   you shouldn't deallocate or overwrite it.  */
9652
9653
static bool
9654
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9655
0
{
9656
0
  asection *sect2;
9657
9658
0
  if (bfd_get_section_by_name (abfd, name) != NULL)
9659
0
    return true;
9660
9661
0
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9662
0
  if (sect2 == NULL)
9663
0
    return false;
9664
9665
0
  sect2->size = sect->size;
9666
0
  sect2->filepos = sect->filepos;
9667
0
  sect2->alignment_power = sect->alignment_power;
9668
0
  return true;
9669
0
}
9670
9671
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
9672
   actually creates up to two pseudosections:
9673
   - For the single-threaded case, a section named NAME, unless
9674
     such a section already exists.
9675
   - For the multi-threaded case, a section named "NAME/PID", where
9676
     PID is elfcore_make_pid (abfd).
9677
   Both pseudosections have identical contents.  */
9678
bool
9679
_bfd_elfcore_make_pseudosection (bfd *abfd,
9680
         char *name,
9681
         size_t size,
9682
         ufile_ptr filepos)
9683
0
{
9684
0
  char buf[100];
9685
0
  char *threaded_name;
9686
0
  size_t len;
9687
0
  asection *sect;
9688
9689
  /* Build the section name.  */
9690
9691
0
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9692
0
  len = strlen (buf) + 1;
9693
0
  threaded_name = (char *) bfd_alloc (abfd, len);
9694
0
  if (threaded_name == NULL)
9695
0
    return false;
9696
0
  memcpy (threaded_name, buf, len);
9697
9698
0
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9699
0
               SEC_HAS_CONTENTS);
9700
0
  if (sect == NULL)
9701
0
    return false;
9702
0
  sect->size = size;
9703
0
  sect->filepos = filepos;
9704
0
  sect->alignment_power = 2;
9705
9706
0
  return elfcore_maybe_make_sect (abfd, name, sect);
9707
0
}
9708
9709
static bool
9710
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9711
        size_t offs)
9712
0
{
9713
0
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9714
0
                   SEC_HAS_CONTENTS);
9715
9716
0
  if (sect == NULL)
9717
0
    return false;
9718
9719
0
  sect->size = note->descsz - offs;
9720
0
  sect->filepos = note->descpos + offs;
9721
0
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9722
9723
0
  return true;
9724
0
}
9725
9726
/* prstatus_t exists on:
9727
     solaris 2.5+
9728
     linux 2.[01] + glibc
9729
     unixware 4.2
9730
*/
9731
9732
#if defined (HAVE_PRSTATUS_T)
9733
9734
static bool
9735
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9736
0
{
9737
0
  size_t size;
9738
0
  int offset;
9739
9740
0
  if (note->descsz == sizeof (prstatus_t))
9741
0
    {
9742
0
      prstatus_t prstat;
9743
9744
0
      size = sizeof (prstat.pr_reg);
9745
0
      offset   = offsetof (prstatus_t, pr_reg);
9746
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
9747
9748
      /* Do not overwrite the core signal if it
9749
   has already been set by another thread.  */
9750
0
      if (elf_tdata (abfd)->core->signal == 0)
9751
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9752
0
      if (elf_tdata (abfd)->core->pid == 0)
9753
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
9754
9755
      /* pr_who exists on:
9756
   solaris 2.5+
9757
   unixware 4.2
9758
   pr_who doesn't exist on:
9759
   linux 2.[01]
9760
   */
9761
#if defined (HAVE_PRSTATUS_T_PR_WHO)
9762
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9763
#else
9764
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9765
0
#endif
9766
0
    }
9767
0
#if defined (HAVE_PRSTATUS32_T)
9768
0
  else if (note->descsz == sizeof (prstatus32_t))
9769
0
    {
9770
      /* 64-bit host, 32-bit corefile */
9771
0
      prstatus32_t prstat;
9772
9773
0
      size = sizeof (prstat.pr_reg);
9774
0
      offset   = offsetof (prstatus32_t, pr_reg);
9775
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
9776
9777
      /* Do not overwrite the core signal if it
9778
   has already been set by another thread.  */
9779
0
      if (elf_tdata (abfd)->core->signal == 0)
9780
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9781
0
      if (elf_tdata (abfd)->core->pid == 0)
9782
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
9783
9784
      /* pr_who exists on:
9785
   solaris 2.5+
9786
   unixware 4.2
9787
   pr_who doesn't exist on:
9788
   linux 2.[01]
9789
   */
9790
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
9791
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9792
#else
9793
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9794
0
#endif
9795
0
    }
9796
0
#endif /* HAVE_PRSTATUS32_T */
9797
0
  else
9798
0
    {
9799
      /* Fail - we don't know how to handle any other
9800
   note size (ie. data object type).  */
9801
0
      return true;
9802
0
    }
9803
9804
  /* Make a ".reg/999" section and a ".reg" section.  */
9805
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9806
0
            size, note->descpos + offset);
9807
0
}
9808
#endif /* defined (HAVE_PRSTATUS_T) */
9809
9810
/* Create a pseudosection containing the exact contents of NOTE.  */
9811
static bool
9812
elfcore_make_note_pseudosection (bfd *abfd,
9813
         char *name,
9814
         Elf_Internal_Note *note)
9815
0
{
9816
0
  return _bfd_elfcore_make_pseudosection (abfd, name,
9817
0
            note->descsz, note->descpos);
9818
0
}
9819
9820
/* There isn't a consistent prfpregset_t across platforms,
9821
   but it doesn't matter, because we don't have to pick this
9822
   data structure apart.  */
9823
9824
static bool
9825
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9826
0
{
9827
0
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9828
0
}
9829
9830
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9831
   type of NT_PRXFPREG.  Just include the whole note's contents
9832
   literally.  */
9833
9834
static bool
9835
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9836
0
{
9837
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9838
0
}
9839
9840
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9841
   with a note type of NT_X86_XSTATE.  Just include the whole note's
9842
   contents literally.  */
9843
9844
static bool
9845
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9846
0
{
9847
0
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9848
0
}
9849
9850
static bool
9851
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9852
0
{
9853
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9854
0
}
9855
9856
static bool
9857
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9858
0
{
9859
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9860
0
}
9861
9862
static bool
9863
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9864
0
{
9865
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9866
0
}
9867
9868
static bool
9869
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9870
0
{
9871
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9872
0
}
9873
9874
static bool
9875
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9876
0
{
9877
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9878
0
}
9879
9880
static bool
9881
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9882
0
{
9883
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9884
0
}
9885
9886
static bool
9887
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9888
0
{
9889
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9890
0
}
9891
9892
static bool
9893
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9894
0
{
9895
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9896
0
}
9897
9898
static bool
9899
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9900
0
{
9901
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9902
0
}
9903
9904
static bool
9905
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9906
0
{
9907
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9908
0
}
9909
9910
static bool
9911
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9912
0
{
9913
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9914
0
}
9915
9916
static bool
9917
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9918
0
{
9919
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9920
0
}
9921
9922
static bool
9923
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9924
0
{
9925
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9926
0
}
9927
9928
static bool
9929
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9930
0
{
9931
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9932
0
}
9933
9934
static bool
9935
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9936
0
{
9937
0
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9938
0
}
9939
9940
static bool
9941
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9942
0
{
9943
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9944
0
}
9945
9946
static bool
9947
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9948
0
{
9949
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9950
0
}
9951
9952
static bool
9953
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9954
0
{
9955
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9956
0
}
9957
9958
static bool
9959
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9960
0
{
9961
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9962
0
}
9963
9964
static bool
9965
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9966
0
{
9967
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9968
0
}
9969
9970
static bool
9971
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9972
0
{
9973
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9974
0
}
9975
9976
static bool
9977
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9978
0
{
9979
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9980
0
}
9981
9982
static bool
9983
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9984
0
{
9985
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9986
0
}
9987
9988
static bool
9989
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9990
0
{
9991
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9992
0
}
9993
9994
static bool
9995
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9996
0
{
9997
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9998
0
}
9999
10000
static bool
10001
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10002
0
{
10003
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10004
0
}
10005
10006
static bool
10007
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10008
0
{
10009
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10010
0
}
10011
10012
static bool
10013
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10014
0
{
10015
0
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10016
0
}
10017
10018
static bool
10019
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10020
0
{
10021
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10022
0
}
10023
10024
static bool
10025
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10026
0
{
10027
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10028
0
}
10029
10030
static bool
10031
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10032
0
{
10033
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10034
0
}
10035
10036
static bool
10037
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10038
0
{
10039
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10040
0
}
10041
10042
static bool
10043
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10044
0
{
10045
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10046
0
}
10047
10048
static bool
10049
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10050
0
{
10051
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10052
0
}
10053
10054
static bool
10055
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10056
0
{
10057
0
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10058
0
            note);
10059
0
}
10060
10061
static bool
10062
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10063
0
{
10064
0
  return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10065
0
}
10066
10067
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10068
   successful otherwise, return FALSE.  */
10069
10070
static bool
10071
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10072
0
{
10073
0
  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10074
0
}
10075
10076
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10077
   successful otherwise, return FALSE.  */
10078
10079
static bool
10080
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10081
0
{
10082
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10083
0
}
10084
10085
static bool
10086
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10087
0
{
10088
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10089
0
}
10090
10091
static bool
10092
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10093
0
{
10094
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10095
0
}
10096
10097
static bool
10098
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10099
0
{
10100
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10101
0
}
10102
10103
static bool
10104
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10105
0
{
10106
0
  return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10107
0
}
10108
10109
#if defined (HAVE_PRPSINFO_T)
10110
typedef prpsinfo_t   elfcore_psinfo_t;
10111
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10112
typedef prpsinfo32_t elfcore_psinfo32_t;
10113
#endif
10114
#endif
10115
10116
#if defined (HAVE_PSINFO_T)
10117
typedef psinfo_t   elfcore_psinfo_t;
10118
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10119
typedef psinfo32_t elfcore_psinfo32_t;
10120
#endif
10121
#endif
10122
10123
/* return a malloc'ed copy of a string at START which is at
10124
   most MAX bytes long, possibly without a terminating '\0'.
10125
   the copy will always have a terminating '\0'.  */
10126
10127
char *
10128
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10129
0
{
10130
0
  char *dups;
10131
0
  char *end = (char *) memchr (start, '\0', max);
10132
0
  size_t len;
10133
10134
0
  if (end == NULL)
10135
0
    len = max;
10136
0
  else
10137
0
    len = end - start;
10138
10139
0
  dups = (char *) bfd_alloc (abfd, len + 1);
10140
0
  if (dups == NULL)
10141
0
    return NULL;
10142
10143
0
  memcpy (dups, start, len);
10144
0
  dups[len] = '\0';
10145
10146
0
  return dups;
10147
0
}
10148
10149
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10150
static bool
10151
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10152
0
{
10153
0
  if (note->descsz == sizeof (elfcore_psinfo_t))
10154
0
    {
10155
0
      elfcore_psinfo_t psinfo;
10156
10157
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10158
10159
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10160
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10161
0
#endif
10162
0
      elf_tdata (abfd)->core->program
10163
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10164
0
        sizeof (psinfo.pr_fname));
10165
10166
0
      elf_tdata (abfd)->core->command
10167
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10168
0
        sizeof (psinfo.pr_psargs));
10169
0
    }
10170
0
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10171
0
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10172
0
    {
10173
      /* 64-bit host, 32-bit corefile */
10174
0
      elfcore_psinfo32_t psinfo;
10175
10176
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10177
10178
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10179
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10180
0
#endif
10181
0
      elf_tdata (abfd)->core->program
10182
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10183
0
        sizeof (psinfo.pr_fname));
10184
10185
0
      elf_tdata (abfd)->core->command
10186
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10187
0
        sizeof (psinfo.pr_psargs));
10188
0
    }
10189
0
#endif
10190
10191
0
  else
10192
0
    {
10193
      /* Fail - we don't know how to handle any other
10194
   note size (ie. data object type).  */
10195
0
      return true;
10196
0
    }
10197
10198
  /* Note that for some reason, a spurious space is tacked
10199
     onto the end of the args in some (at least one anyway)
10200
     implementations, so strip it off if it exists.  */
10201
10202
0
  {
10203
0
    char *command = elf_tdata (abfd)->core->command;
10204
0
    int n = strlen (command);
10205
10206
0
    if (0 < n && command[n - 1] == ' ')
10207
0
      command[n - 1] = '\0';
10208
0
  }
10209
10210
0
  return true;
10211
0
}
10212
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10213
10214
#if defined (HAVE_PSTATUS_T)
10215
static bool
10216
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10217
{
10218
  if (note->descsz == sizeof (pstatus_t)
10219
#if defined (HAVE_PXSTATUS_T)
10220
      || note->descsz == sizeof (pxstatus_t)
10221
#endif
10222
      )
10223
    {
10224
      pstatus_t pstat;
10225
10226
      memcpy (&pstat, note->descdata, sizeof (pstat));
10227
10228
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10229
    }
10230
#if defined (HAVE_PSTATUS32_T)
10231
  else if (note->descsz == sizeof (pstatus32_t))
10232
    {
10233
      /* 64-bit host, 32-bit corefile */
10234
      pstatus32_t pstat;
10235
10236
      memcpy (&pstat, note->descdata, sizeof (pstat));
10237
10238
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10239
    }
10240
#endif
10241
  /* Could grab some more details from the "representative"
10242
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10243
     NT_LWPSTATUS note, presumably.  */
10244
10245
  return true;
10246
}
10247
#endif /* defined (HAVE_PSTATUS_T) */
10248
10249
#if defined (HAVE_LWPSTATUS_T)
10250
static bool
10251
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10252
{
10253
  lwpstatus_t lwpstat;
10254
  char buf[100];
10255
  char *name;
10256
  size_t len;
10257
  asection *sect;
10258
10259
  if (note->descsz != sizeof (lwpstat)
10260
#if defined (HAVE_LWPXSTATUS_T)
10261
      && note->descsz != sizeof (lwpxstatus_t)
10262
#endif
10263
      )
10264
    return true;
10265
10266
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10267
10268
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10269
  /* Do not overwrite the core signal if it has already been set by
10270
     another thread.  */
10271
  if (elf_tdata (abfd)->core->signal == 0)
10272
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10273
10274
  /* Make a ".reg/999" section.  */
10275
10276
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10277
  len = strlen (buf) + 1;
10278
  name = bfd_alloc (abfd, len);
10279
  if (name == NULL)
10280
    return false;
10281
  memcpy (name, buf, len);
10282
10283
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10284
  if (sect == NULL)
10285
    return false;
10286
10287
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10288
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10289
  sect->filepos = note->descpos
10290
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10291
#endif
10292
10293
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10294
  sect->size = sizeof (lwpstat.pr_reg);
10295
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10296
#endif
10297
10298
  sect->alignment_power = 2;
10299
10300
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10301
    return false;
10302
10303
  /* Make a ".reg2/999" section */
10304
10305
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10306
  len = strlen (buf) + 1;
10307
  name = bfd_alloc (abfd, len);
10308
  if (name == NULL)
10309
    return false;
10310
  memcpy (name, buf, len);
10311
10312
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10313
  if (sect == NULL)
10314
    return false;
10315
10316
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10317
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10318
  sect->filepos = note->descpos
10319
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10320
#endif
10321
10322
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10323
  sect->size = sizeof (lwpstat.pr_fpreg);
10324
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10325
#endif
10326
10327
  sect->alignment_power = 2;
10328
10329
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10330
}
10331
#endif /* defined (HAVE_LWPSTATUS_T) */
10332
10333
/* These constants, and the structure offsets used below, are defined by
10334
   Cygwin's core_dump.h */
10335
0
#define NOTE_INFO_PROCESS  1
10336
0
#define NOTE_INFO_THREAD   2
10337
0
#define NOTE_INFO_MODULE   3
10338
0
#define NOTE_INFO_MODULE64 4
10339
10340
static bool
10341
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10342
0
{
10343
0
  char buf[30];
10344
0
  char *name;
10345
0
  size_t len;
10346
0
  unsigned int name_size;
10347
0
  asection *sect;
10348
0
  unsigned int type;
10349
0
  int is_active_thread;
10350
0
  bfd_vma base_addr;
10351
10352
0
  if (note->descsz < 4)
10353
0
    return true;
10354
10355
0
  if (! startswith (note->namedata, "win32"))
10356
0
    return true;
10357
10358
0
  type = bfd_get_32 (abfd, note->descdata);
10359
10360
0
  struct
10361
0
  {
10362
0
    const char *type_name;
10363
0
    unsigned long min_size;
10364
0
  } size_check[] =
10365
0
      {
10366
0
       { "NOTE_INFO_PROCESS", 12 },
10367
0
       { "NOTE_INFO_THREAD", 12 },
10368
0
       { "NOTE_INFO_MODULE", 12 },
10369
0
       { "NOTE_INFO_MODULE64", 16 },
10370
0
      };
10371
10372
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10373
0
      return true;
10374
10375
0
  if (note->descsz < size_check[type - 1].min_size)
10376
0
    {
10377
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10378
0
          " is too small"),
10379
0
        abfd, size_check[type - 1].type_name, note->descsz);
10380
0
      return true;
10381
0
    }
10382
10383
0
  switch (type)
10384
0
    {
10385
0
    case NOTE_INFO_PROCESS:
10386
      /* FIXME: need to add ->core->command.  */
10387
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10388
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10389
0
      break;
10390
10391
0
    case NOTE_INFO_THREAD:
10392
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10393
   structure. */
10394
      /* thread_info.tid */
10395
0
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10396
10397
0
      len = strlen (buf) + 1;
10398
0
      name = (char *) bfd_alloc (abfd, len);
10399
0
      if (name == NULL)
10400
0
  return false;
10401
10402
0
      memcpy (name, buf, len);
10403
10404
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10405
0
      if (sect == NULL)
10406
0
  return false;
10407
10408
      /* sizeof (thread_info.thread_context) */
10409
0
      sect->size = note->descsz - 12;
10410
      /* offsetof (thread_info.thread_context) */
10411
0
      sect->filepos = note->descpos + 12;
10412
0
      sect->alignment_power = 2;
10413
10414
      /* thread_info.is_active_thread */
10415
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10416
10417
0
      if (is_active_thread)
10418
0
  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10419
0
    return false;
10420
0
      break;
10421
10422
0
    case NOTE_INFO_MODULE:
10423
0
    case NOTE_INFO_MODULE64:
10424
      /* Make a ".module/xxxxxxxx" section.  */
10425
0
      if (type == NOTE_INFO_MODULE)
10426
0
  {
10427
    /* module_info.base_address */
10428
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
10429
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10430
    /* module_info.module_name_size */
10431
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
10432
0
  }
10433
0
      else /* NOTE_INFO_MODULE64 */
10434
0
  {
10435
    /* module_info.base_address */
10436
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
10437
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10438
    /* module_info.module_name_size */
10439
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
10440
0
  }
10441
10442
0
      len = strlen (buf) + 1;
10443
0
      name = (char *) bfd_alloc (abfd, len);
10444
0
      if (name == NULL)
10445
0
  return false;
10446
10447
0
      memcpy (name, buf, len);
10448
10449
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10450
10451
0
      if (sect == NULL)
10452
0
  return false;
10453
10454
0
      if (note->descsz < 12 + name_size)
10455
0
  {
10456
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10457
0
        " is too small to contain a name of size %u"),
10458
0
            abfd, note->descsz, name_size);
10459
0
    return true;
10460
0
  }
10461
10462
0
      sect->size = note->descsz;
10463
0
      sect->filepos = note->descpos;
10464
0
      sect->alignment_power = 2;
10465
0
      break;
10466
10467
0
    default:
10468
0
      return true;
10469
0
    }
10470
10471
0
  return true;
10472
0
}
10473
10474
static bool
10475
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10476
0
{
10477
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10478
10479
0
  switch (note->type)
10480
0
    {
10481
0
    default:
10482
0
      return true;
10483
10484
0
    case NT_PRSTATUS:
10485
0
      if (bed->elf_backend_grok_prstatus)
10486
0
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10487
0
    return true;
10488
0
#if defined (HAVE_PRSTATUS_T)
10489
0
      return elfcore_grok_prstatus (abfd, note);
10490
#else
10491
      return true;
10492
#endif
10493
10494
#if defined (HAVE_PSTATUS_T)
10495
    case NT_PSTATUS:
10496
      return elfcore_grok_pstatus (abfd, note);
10497
#endif
10498
10499
#if defined (HAVE_LWPSTATUS_T)
10500
    case NT_LWPSTATUS:
10501
      return elfcore_grok_lwpstatus (abfd, note);
10502
#endif
10503
10504
0
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG */
10505
0
      return elfcore_grok_prfpreg (abfd, note);
10506
10507
0
    case NT_WIN32PSTATUS:
10508
0
      return elfcore_grok_win32pstatus (abfd, note);
10509
10510
0
    case NT_PRXFPREG:   /* Linux SSE extension */
10511
0
      if (note->namesz == 6
10512
0
    && strcmp (note->namedata, "LINUX") == 0)
10513
0
  return elfcore_grok_prxfpreg (abfd, note);
10514
0
      else
10515
0
  return true;
10516
10517
0
    case NT_X86_XSTATE:   /* Linux XSAVE extension */
10518
0
      if (note->namesz == 6
10519
0
    && strcmp (note->namedata, "LINUX") == 0)
10520
0
  return elfcore_grok_xstatereg (abfd, note);
10521
0
      else
10522
0
  return true;
10523
10524
0
    case NT_PPC_VMX:
10525
0
      if (note->namesz == 6
10526
0
    && strcmp (note->namedata, "LINUX") == 0)
10527
0
  return elfcore_grok_ppc_vmx (abfd, note);
10528
0
      else
10529
0
  return true;
10530
10531
0
    case NT_PPC_VSX:
10532
0
      if (note->namesz == 6
10533
0
    && strcmp (note->namedata, "LINUX") == 0)
10534
0
  return elfcore_grok_ppc_vsx (abfd, note);
10535
0
      else
10536
0
  return true;
10537
10538
0
    case NT_PPC_TAR:
10539
0
      if (note->namesz == 6
10540
0
    && strcmp (note->namedata, "LINUX") == 0)
10541
0
  return elfcore_grok_ppc_tar (abfd, note);
10542
0
      else
10543
0
  return true;
10544
10545
0
    case NT_PPC_PPR:
10546
0
      if (note->namesz == 6
10547
0
    && strcmp (note->namedata, "LINUX") == 0)
10548
0
  return elfcore_grok_ppc_ppr (abfd, note);
10549
0
      else
10550
0
  return true;
10551
10552
0
    case NT_PPC_DSCR:
10553
0
      if (note->namesz == 6
10554
0
    && strcmp (note->namedata, "LINUX") == 0)
10555
0
  return elfcore_grok_ppc_dscr (abfd, note);
10556
0
      else
10557
0
  return true;
10558
10559
0
    case NT_PPC_EBB:
10560
0
      if (note->namesz == 6
10561
0
    && strcmp (note->namedata, "LINUX") == 0)
10562
0
  return elfcore_grok_ppc_ebb (abfd, note);
10563
0
      else
10564
0
  return true;
10565
10566
0
    case NT_PPC_PMU:
10567
0
      if (note->namesz == 6
10568
0
    && strcmp (note->namedata, "LINUX") == 0)
10569
0
  return elfcore_grok_ppc_pmu (abfd, note);
10570
0
      else
10571
0
  return true;
10572
10573
0
    case NT_PPC_TM_CGPR:
10574
0
      if (note->namesz == 6
10575
0
    && strcmp (note->namedata, "LINUX") == 0)
10576
0
  return elfcore_grok_ppc_tm_cgpr (abfd, note);
10577
0
      else
10578
0
  return true;
10579
10580
0
    case NT_PPC_TM_CFPR:
10581
0
      if (note->namesz == 6
10582
0
    && strcmp (note->namedata, "LINUX") == 0)
10583
0
  return elfcore_grok_ppc_tm_cfpr (abfd, note);
10584
0
      else
10585
0
  return true;
10586
10587
0
    case NT_PPC_TM_CVMX:
10588
0
      if (note->namesz == 6
10589
0
    && strcmp (note->namedata, "LINUX") == 0)
10590
0
  return elfcore_grok_ppc_tm_cvmx (abfd, note);
10591
0
      else
10592
0
  return true;
10593
10594
0
    case NT_PPC_TM_CVSX:
10595
0
      if (note->namesz == 6
10596
0
    && strcmp (note->namedata, "LINUX") == 0)
10597
0
  return elfcore_grok_ppc_tm_cvsx (abfd, note);
10598
0
      else
10599
0
  return true;
10600
10601
0
    case NT_PPC_TM_SPR:
10602
0
      if (note->namesz == 6
10603
0
    && strcmp (note->namedata, "LINUX") == 0)
10604
0
  return elfcore_grok_ppc_tm_spr (abfd, note);
10605
0
      else
10606
0
  return true;
10607
10608
0
    case NT_PPC_TM_CTAR:
10609
0
      if (note->namesz == 6
10610
0
    && strcmp (note->namedata, "LINUX") == 0)
10611
0
  return elfcore_grok_ppc_tm_ctar (abfd, note);
10612
0
      else
10613
0
  return true;
10614
10615
0
    case NT_PPC_TM_CPPR:
10616
0
      if (note->namesz == 6
10617
0
    && strcmp (note->namedata, "LINUX") == 0)
10618
0
  return elfcore_grok_ppc_tm_cppr (abfd, note);
10619
0
      else
10620
0
  return true;
10621
10622
0
    case NT_PPC_TM_CDSCR:
10623
0
      if (note->namesz == 6
10624
0
    && strcmp (note->namedata, "LINUX") == 0)
10625
0
  return elfcore_grok_ppc_tm_cdscr (abfd, note);
10626
0
      else
10627
0
  return true;
10628
10629
0
    case NT_S390_HIGH_GPRS:
10630
0
      if (note->namesz == 6
10631
0
    && strcmp (note->namedata, "LINUX") == 0)
10632
0
  return elfcore_grok_s390_high_gprs (abfd, note);
10633
0
      else
10634
0
  return true;
10635
10636
0
    case NT_S390_TIMER:
10637
0
      if (note->namesz == 6
10638
0
    && strcmp (note->namedata, "LINUX") == 0)
10639
0
  return elfcore_grok_s390_timer (abfd, note);
10640
0
      else
10641
0
  return true;
10642
10643
0
    case NT_S390_TODCMP:
10644
0
      if (note->namesz == 6
10645
0
    && strcmp (note->namedata, "LINUX") == 0)
10646
0
  return elfcore_grok_s390_todcmp (abfd, note);
10647
0
      else
10648
0
  return true;
10649
10650
0
    case NT_S390_TODPREG:
10651
0
      if (note->namesz == 6
10652
0
    && strcmp (note->namedata, "LINUX") == 0)
10653
0
  return elfcore_grok_s390_todpreg (abfd, note);
10654
0
      else
10655
0
  return true;
10656
10657
0
    case NT_S390_CTRS:
10658
0
      if (note->namesz == 6
10659
0
    && strcmp (note->namedata, "LINUX") == 0)
10660
0
  return elfcore_grok_s390_ctrs (abfd, note);
10661
0
      else
10662
0
  return true;
10663
10664
0
    case NT_S390_PREFIX:
10665
0
      if (note->namesz == 6
10666
0
    && strcmp (note->namedata, "LINUX") == 0)
10667
0
  return elfcore_grok_s390_prefix (abfd, note);
10668
0
      else
10669
0
  return true;
10670
10671
0
    case NT_S390_LAST_BREAK:
10672
0
      if (note->namesz == 6
10673
0
    && strcmp (note->namedata, "LINUX") == 0)
10674
0
  return elfcore_grok_s390_last_break (abfd, note);
10675
0
      else
10676
0
  return true;
10677
10678
0
    case NT_S390_SYSTEM_CALL:
10679
0
      if (note->namesz == 6
10680
0
    && strcmp (note->namedata, "LINUX") == 0)
10681
0
  return elfcore_grok_s390_system_call (abfd, note);
10682
0
      else
10683
0
  return true;
10684
10685
0
    case NT_S390_TDB:
10686
0
      if (note->namesz == 6
10687
0
    && strcmp (note->namedata, "LINUX") == 0)
10688
0
  return elfcore_grok_s390_tdb (abfd, note);
10689
0
      else
10690
0
  return true;
10691
10692
0
    case NT_S390_VXRS_LOW:
10693
0
      if (note->namesz == 6
10694
0
    && strcmp (note->namedata, "LINUX") == 0)
10695
0
  return elfcore_grok_s390_vxrs_low (abfd, note);
10696
0
      else
10697
0
  return true;
10698
10699
0
    case NT_S390_VXRS_HIGH:
10700
0
      if (note->namesz == 6
10701
0
    && strcmp (note->namedata, "LINUX") == 0)
10702
0
  return elfcore_grok_s390_vxrs_high (abfd, note);
10703
0
      else
10704
0
  return true;
10705
10706
0
    case NT_S390_GS_CB:
10707
0
      if (note->namesz == 6
10708
0
    && strcmp (note->namedata, "LINUX") == 0)
10709
0
  return elfcore_grok_s390_gs_cb (abfd, note);
10710
0
      else
10711
0
  return true;
10712
10713
0
    case NT_S390_GS_BC:
10714
0
      if (note->namesz == 6
10715
0
    && strcmp (note->namedata, "LINUX") == 0)
10716
0
  return elfcore_grok_s390_gs_bc (abfd, note);
10717
0
      else
10718
0
  return true;
10719
10720
0
    case NT_ARC_V2:
10721
0
      if (note->namesz == 6
10722
0
    && strcmp (note->namedata, "LINUX") == 0)
10723
0
  return elfcore_grok_arc_v2 (abfd, note);
10724
0
      else
10725
0
  return true;
10726
10727
0
    case NT_ARM_VFP:
10728
0
      if (note->namesz == 6
10729
0
    && strcmp (note->namedata, "LINUX") == 0)
10730
0
  return elfcore_grok_arm_vfp (abfd, note);
10731
0
      else
10732
0
  return true;
10733
10734
0
    case NT_ARM_TLS:
10735
0
      if (note->namesz == 6
10736
0
    && strcmp (note->namedata, "LINUX") == 0)
10737
0
  return elfcore_grok_aarch_tls (abfd, note);
10738
0
      else
10739
0
  return true;
10740
10741
0
    case NT_ARM_HW_BREAK:
10742
0
      if (note->namesz == 6
10743
0
    && strcmp (note->namedata, "LINUX") == 0)
10744
0
  return elfcore_grok_aarch_hw_break (abfd, note);
10745
0
      else
10746
0
  return true;
10747
10748
0
    case NT_ARM_HW_WATCH:
10749
0
      if (note->namesz == 6
10750
0
    && strcmp (note->namedata, "LINUX") == 0)
10751
0
  return elfcore_grok_aarch_hw_watch (abfd, note);
10752
0
      else
10753
0
  return true;
10754
10755
0
    case NT_ARM_SVE:
10756
0
      if (note->namesz == 6
10757
0
    && strcmp (note->namedata, "LINUX") == 0)
10758
0
  return elfcore_grok_aarch_sve (abfd, note);
10759
0
      else
10760
0
  return true;
10761
10762
0
    case NT_ARM_PAC_MASK:
10763
0
      if (note->namesz == 6
10764
0
    && strcmp (note->namedata, "LINUX") == 0)
10765
0
  return elfcore_grok_aarch_pauth (abfd, note);
10766
0
      else
10767
0
  return true;
10768
10769
0
    case NT_ARM_TAGGED_ADDR_CTRL:
10770
0
      if (note->namesz == 6
10771
0
    && strcmp (note->namedata, "LINUX") == 0)
10772
0
  return elfcore_grok_aarch_mte (abfd, note);
10773
0
      else
10774
0
  return true;
10775
10776
0
    case NT_GDB_TDESC:
10777
0
      if (note->namesz == 4
10778
0
    && strcmp (note->namedata, "GDB") == 0)
10779
0
  return elfcore_grok_gdb_tdesc (abfd, note);
10780
0
      else
10781
0
  return true;
10782
10783
0
    case NT_RISCV_CSR:
10784
0
      if (note->namesz == 4
10785
0
    && strcmp (note->namedata, "GDB") == 0)
10786
0
  return elfcore_grok_riscv_csr (abfd, note);
10787
0
      else
10788
0
  return true;
10789
10790
0
    case NT_LARCH_CPUCFG:
10791
0
      if (note->namesz == 6
10792
0
    && strcmp (note->namedata, "LINUX") == 0)
10793
0
  return elfcore_grok_loongarch_cpucfg (abfd, note);
10794
0
      else
10795
0
  return true;
10796
10797
0
    case NT_LARCH_LBT:
10798
0
      if (note->namesz == 6
10799
0
    && strcmp (note->namedata, "LINUX") == 0)
10800
0
  return elfcore_grok_loongarch_lbt (abfd, note);
10801
0
      else
10802
0
  return true;
10803
10804
0
    case NT_LARCH_LSX:
10805
0
      if (note->namesz == 6
10806
0
    && strcmp (note->namedata, "LINUX") == 0)
10807
0
  return elfcore_grok_loongarch_lsx (abfd, note);
10808
0
      else
10809
0
  return true;
10810
10811
0
    case NT_LARCH_LASX:
10812
0
      if (note->namesz == 6
10813
0
    && strcmp (note->namedata, "LINUX") == 0)
10814
0
  return elfcore_grok_loongarch_lasx (abfd, note);
10815
0
      else
10816
0
  return true;
10817
10818
0
    case NT_PRPSINFO:
10819
0
    case NT_PSINFO:
10820
0
      if (bed->elf_backend_grok_psinfo)
10821
0
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10822
0
    return true;
10823
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10824
0
      return elfcore_grok_psinfo (abfd, note);
10825
#else
10826
      return true;
10827
#endif
10828
10829
0
    case NT_AUXV:
10830
0
      return elfcore_make_auxv_note_section (abfd, note, 0);
10831
10832
0
    case NT_FILE:
10833
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10834
0
                note);
10835
10836
0
    case NT_SIGINFO:
10837
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10838
0
                note);
10839
10840
0
    }
10841
0
}
10842
10843
static bool
10844
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10845
0
{
10846
0
  struct bfd_build_id* build_id;
10847
10848
0
  if (note->descsz == 0)
10849
0
    return false;
10850
10851
0
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10852
0
  if (build_id == NULL)
10853
0
    return false;
10854
10855
0
  build_id->size = note->descsz;
10856
0
  memcpy (build_id->data, note->descdata, note->descsz);
10857
0
  abfd->build_id = build_id;
10858
10859
0
  return true;
10860
0
}
10861
10862
static bool
10863
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10864
0
{
10865
0
  switch (note->type)
10866
0
    {
10867
0
    default:
10868
0
      return true;
10869
10870
0
    case NT_GNU_PROPERTY_TYPE_0:
10871
0
      return _bfd_elf_parse_gnu_properties (abfd, note);
10872
10873
0
    case NT_GNU_BUILD_ID:
10874
0
      return elfobj_grok_gnu_build_id (abfd, note);
10875
0
    }
10876
0
}
10877
10878
static bool
10879
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10880
0
{
10881
0
  struct sdt_note *cur =
10882
0
    (struct sdt_note *) bfd_alloc (abfd,
10883
0
           sizeof (struct sdt_note) + note->descsz);
10884
10885
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10886
0
  cur->size = (bfd_size_type) note->descsz;
10887
0
  memcpy (cur->data, note->descdata, note->descsz);
10888
10889
0
  elf_tdata (abfd)->sdt_note_head = cur;
10890
10891
0
  return true;
10892
0
}
10893
10894
static bool
10895
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10896
0
{
10897
0
  switch (note->type)
10898
0
    {
10899
0
    case NT_STAPSDT:
10900
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
10901
10902
0
    default:
10903
0
      return true;
10904
0
    }
10905
0
}
10906
10907
static bool
10908
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10909
0
{
10910
0
  size_t offset;
10911
10912
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10913
0
    {
10914
0
    case ELFCLASS32:
10915
0
      if (note->descsz < 108)
10916
0
  return false;
10917
0
      break;
10918
10919
0
    case ELFCLASS64:
10920
0
      if (note->descsz < 120)
10921
0
  return false;
10922
0
      break;
10923
10924
0
    default:
10925
0
      return false;
10926
0
    }
10927
10928
  /* Check for version 1 in pr_version.  */
10929
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10930
0
    return false;
10931
10932
0
  offset = 4;
10933
10934
  /* Skip over pr_psinfosz. */
10935
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10936
0
    offset += 4;
10937
0
  else
10938
0
    {
10939
0
      offset += 4;  /* Padding before pr_psinfosz. */
10940
0
      offset += 8;
10941
0
    }
10942
10943
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
10944
0
  elf_tdata (abfd)->core->program
10945
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10946
0
  offset += 17;
10947
10948
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
10949
0
  elf_tdata (abfd)->core->command
10950
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10951
0
  offset += 81;
10952
10953
  /* Padding before pr_pid.  */
10954
0
  offset += 2;
10955
10956
  /* The pr_pid field was added in version "1a".  */
10957
0
  if (note->descsz < offset + 4)
10958
0
    return true;
10959
10960
0
  elf_tdata (abfd)->core->pid
10961
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10962
10963
0
  return true;
10964
0
}
10965
10966
static bool
10967
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10968
0
{
10969
0
  size_t offset;
10970
0
  size_t size;
10971
0
  size_t min_size;
10972
10973
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
10974
     Also compute minimum size of this note.  */
10975
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10976
0
    {
10977
0
    case ELFCLASS32:
10978
0
      offset = 4 + 4;
10979
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
10980
0
      break;
10981
10982
0
    case ELFCLASS64:
10983
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
10984
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10985
0
      break;
10986
10987
0
    default:
10988
0
      return false;
10989
0
    }
10990
10991
0
  if (note->descsz < min_size)
10992
0
    return false;
10993
10994
  /* Check for version 1 in pr_version.  */
10995
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10996
0
    return false;
10997
10998
  /* Extract size of pr_reg from pr_gregsetsz.  */
10999
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11000
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11001
0
    {
11002
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11003
0
      offset += 4 * 2;
11004
0
    }
11005
0
  else
11006
0
    {
11007
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11008
0
      offset += 8 * 2;
11009
0
    }
11010
11011
  /* Skip over pr_osreldate.  */
11012
0
  offset += 4;
11013
11014
  /* Read signal from pr_cursig.  */
11015
0
  if (elf_tdata (abfd)->core->signal == 0)
11016
0
    elf_tdata (abfd)->core->signal
11017
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11018
0
  offset += 4;
11019
11020
  /* Read TID from pr_pid.  */
11021
0
  elf_tdata (abfd)->core->lwpid
11022
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11023
0
  offset += 4;
11024
11025
  /* Padding before pr_reg.  */
11026
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11027
0
    offset += 4;
11028
11029
  /* Make sure that there is enough data remaining in the note.  */
11030
0
  if ((note->descsz - offset) < size)
11031
0
    return false;
11032
11033
  /* Make a ".reg/999" section and a ".reg" section.  */
11034
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11035
0
            size, note->descpos + offset);
11036
0
}
11037
11038
static bool
11039
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11040
0
{
11041
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11042
11043
0
  switch (note->type)
11044
0
    {
11045
0
    case NT_PRSTATUS:
11046
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11047
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11048
0
    return true;
11049
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11050
11051
0
    case NT_FPREGSET:
11052
0
      return elfcore_grok_prfpreg (abfd, note);
11053
11054
0
    case NT_PRPSINFO:
11055
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11056
11057
0
    case NT_FREEBSD_THRMISC:
11058
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11059
11060
0
    case NT_FREEBSD_PROCSTAT_PROC:
11061
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11062
0
                note);
11063
11064
0
    case NT_FREEBSD_PROCSTAT_FILES:
11065
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11066
0
                note);
11067
11068
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11069
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11070
0
                note);
11071
11072
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11073
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11074
11075
0
    case NT_FREEBSD_X86_SEGBASES:
11076
0
      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11077
11078
0
    case NT_X86_XSTATE:
11079
0
      return elfcore_grok_xstatereg (abfd, note);
11080
11081
0
    case NT_FREEBSD_PTLWPINFO:
11082
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11083
0
                note);
11084
11085
0
    case NT_ARM_TLS:
11086
0
      return elfcore_grok_aarch_tls (abfd, note);
11087
11088
0
    case NT_ARM_VFP:
11089
0
      return elfcore_grok_arm_vfp (abfd, note);
11090
11091
0
    default:
11092
0
      return true;
11093
0
    }
11094
0
}
11095
11096
static bool
11097
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11098
0
{
11099
0
  char *cp;
11100
11101
0
  cp = strchr (note->namedata, '@');
11102
0
  if (cp != NULL)
11103
0
    {
11104
0
      *lwpidp = atoi(cp + 1);
11105
0
      return true;
11106
0
    }
11107
0
  return false;
11108
0
}
11109
11110
static bool
11111
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11112
0
{
11113
0
  if (note->descsz <= 0x7c + 31)
11114
0
    return false;
11115
11116
  /* Signal number at offset 0x08. */
11117
0
  elf_tdata (abfd)->core->signal
11118
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11119
11120
  /* Process ID at offset 0x50. */
11121
0
  elf_tdata (abfd)->core->pid
11122
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11123
11124
  /* Command name at 0x7c (max 32 bytes, including nul). */
11125
0
  elf_tdata (abfd)->core->command
11126
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11127
11128
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11129
0
            note);
11130
0
}
11131
11132
static bool
11133
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11134
0
{
11135
0
  int lwp;
11136
11137
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11138
0
    elf_tdata (abfd)->core->lwpid = lwp;
11139
11140
0
  switch (note->type)
11141
0
    {
11142
0
    case NT_NETBSDCORE_PROCINFO:
11143
      /* NetBSD-specific core "procinfo".  Note that we expect to
11144
   find this note before any of the others, which is fine,
11145
   since the kernel writes this note out first when it
11146
   creates a core file.  */
11147
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11148
0
    case NT_NETBSDCORE_AUXV:
11149
      /* NetBSD-specific Elf Auxiliary Vector data. */
11150
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11151
0
    case NT_NETBSDCORE_LWPSTATUS:
11152
0
      return elfcore_make_note_pseudosection (abfd,
11153
0
                ".note.netbsdcore.lwpstatus",
11154
0
                note);
11155
0
    default:
11156
0
      break;
11157
0
    }
11158
11159
  /* As of March 2020 there are no other machine-independent notes
11160
     defined for NetBSD core files.  If the note type is less
11161
     than the start of the machine-dependent note types, we don't
11162
     understand it.  */
11163
11164
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11165
0
    return true;
11166
11167
11168
0
  switch (bfd_get_arch (abfd))
11169
0
    {
11170
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11171
   PT_GETFPREGS == mach+2.  */
11172
11173
0
    case bfd_arch_aarch64:
11174
0
    case bfd_arch_alpha:
11175
0
    case bfd_arch_sparc:
11176
0
      switch (note->type)
11177
0
  {
11178
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11179
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11180
11181
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11182
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11183
11184
0
  default:
11185
0
    return true;
11186
0
  }
11187
11188
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11189
   There's also old PT___GETREGS40 == mach + 1 for old reg
11190
   structure which lacks GBR.  */
11191
11192
0
    case bfd_arch_sh:
11193
0
      switch (note->type)
11194
0
  {
11195
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11196
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11197
11198
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11199
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11200
11201
0
  default:
11202
0
    return true;
11203
0
  }
11204
11205
      /* On all other arch's, PT_GETREGS == mach+1 and
11206
   PT_GETFPREGS == mach+3.  */
11207
11208
0
    default:
11209
0
      switch (note->type)
11210
0
  {
11211
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11212
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11213
11214
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11215
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11216
11217
0
  default:
11218
0
    return true;
11219
0
  }
11220
0
    }
11221
    /* NOTREACHED */
11222
0
}
11223
11224
static bool
11225
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11226
0
{
11227
0
  if (note->descsz <= 0x48 + 31)
11228
0
    return false;
11229
11230
  /* Signal number at offset 0x08. */
11231
0
  elf_tdata (abfd)->core->signal
11232
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11233
11234
  /* Process ID at offset 0x20. */
11235
0
  elf_tdata (abfd)->core->pid
11236
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11237
11238
  /* Command name at 0x48 (max 32 bytes, including nul). */
11239
0
  elf_tdata (abfd)->core->command
11240
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11241
11242
0
  return true;
11243
0
}
11244
11245
/* Processes Solaris's process status note.
11246
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11247
   pid_off ~ offsetof(prstatus_t, pr_pid)
11248
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11249
   gregset_size ~ sizeof(gregset_t)
11250
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11251
11252
static bool
11253
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11254
             int pid_off, int lwpid_off, size_t gregset_size,
11255
             size_t gregset_offset)
11256
0
{
11257
0
  asection *sect = NULL;
11258
0
  elf_tdata (abfd)->core->signal
11259
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11260
0
  elf_tdata (abfd)->core->pid
11261
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11262
0
  elf_tdata (abfd)->core->lwpid
11263
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11264
11265
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11266
0
  if (sect != NULL)
11267
0
    sect->size = gregset_size;
11268
11269
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11270
0
            note->descpos + gregset_offset);
11271
0
}
11272
11273
/* Gets program and arguments from a core.
11274
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11275
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11276
11277
static bool
11278
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11279
        int prog_off, int comm_off)
11280
0
{
11281
0
  elf_tdata (abfd)->core->program
11282
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11283
0
  elf_tdata (abfd)->core->command
11284
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11285
11286
0
  return true;
11287
0
}
11288
11289
/* Processes Solaris's LWP status note.
11290
   gregset_size ~ sizeof(gregset_t)
11291
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11292
   fpregset_size ~ sizeof(fpregset_t)
11293
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11294
11295
static bool
11296
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11297
        size_t gregset_size, int gregset_off,
11298
        size_t fpregset_size, int fpregset_off)
11299
0
{
11300
0
  asection *sect = NULL;
11301
0
  char reg2_section_name[16] = { 0 };
11302
11303
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11304
0
       elf_tdata (abfd)->core->lwpid);
11305
11306
  /* offsetof(lwpstatus_t, pr_lwpid) */
11307
0
  elf_tdata (abfd)->core->lwpid
11308
0
    = bfd_get_32 (abfd, note->descdata + 4);
11309
  /* offsetof(lwpstatus_t, pr_cursig) */
11310
0
  elf_tdata (abfd)->core->signal
11311
0
    = bfd_get_16 (abfd, note->descdata + 12);
11312
11313
0
  sect = bfd_get_section_by_name (abfd, ".reg");
11314
0
  if (sect != NULL)
11315
0
    sect->size = gregset_size;
11316
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11317
0
               note->descpos + gregset_off))
11318
0
    return false;
11319
11320
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11321
0
  if (sect != NULL)
11322
0
    {
11323
0
      sect->size = fpregset_size;
11324
0
      sect->filepos = note->descpos + fpregset_off;
11325
0
      sect->alignment_power = 2;
11326
0
    }
11327
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11328
0
               note->descpos + fpregset_off))
11329
0
    return false;
11330
11331
0
  return true;
11332
0
}
11333
11334
static bool
11335
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11336
0
{
11337
0
  if (note == NULL)
11338
0
    return false;
11339
11340
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11341
     by the size of the descsz which matches the sizeof()
11342
     the type appropriate for that note type (e.g., prstatus_t for
11343
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11344
     on Solaris. The core file bitness may differ from the bitness of
11345
     gdb itself, so fixed values are used instead of sizeof().
11346
     Appropriate fixed offsets are also used to obtain data from
11347
     the note.  */
11348
11349
0
  switch ((int) note->type)
11350
0
    {
11351
0
    case SOLARIS_NT_PRSTATUS:
11352
0
      switch (note->descsz)
11353
0
  {
11354
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11355
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11356
0
                 136, 216, 308, 152, 356);
11357
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11358
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11359
0
                 264, 360, 520, 304, 600);
11360
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11361
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11362
0
                 136, 216, 308, 76, 356);
11363
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11364
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11365
0
                 264, 360, 520, 224, 600);
11366
0
  default:
11367
0
    return true;
11368
0
  }
11369
11370
0
    case SOLARIS_NT_PSINFO:
11371
0
    case SOLARIS_NT_PRPSINFO:
11372
0
      switch (note->descsz)
11373
0
  {
11374
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11375
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11376
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11377
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11378
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11379
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11380
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11381
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11382
0
  default:
11383
0
    return true;
11384
0
  }
11385
11386
0
    case SOLARIS_NT_LWPSTATUS:
11387
0
      switch (note->descsz)
11388
0
  {
11389
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11390
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11391
0
            152, 344, 400, 496);
11392
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11393
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11394
0
            304, 544, 544, 848);
11395
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11396
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11397
0
            76, 344, 380, 420);
11398
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11399
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11400
0
            224, 544, 528, 768);
11401
0
  default:
11402
0
    return true;
11403
0
  }
11404
11405
0
    case SOLARIS_NT_LWPSINFO:
11406
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11407
0
      if (note->descsz == 128 || note->descsz == 152)
11408
0
  elf_tdata (abfd)->core->lwpid =
11409
0
    bfd_get_32 (abfd, note->descdata + 4);
11410
0
      break;
11411
11412
0
    default:
11413
0
      break;
11414
0
    }
11415
11416
0
  return true;
11417
0
}
11418
11419
/* For name starting with "CORE" this may be either a Solaris
11420
   core file or a gdb-generated core file.  Do Solaris-specific
11421
   processing on selected note types first with
11422
   elfcore_grok_solaris_note(), then process the note
11423
   in elfcore_grok_note().  */
11424
11425
static bool
11426
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11427
0
{
11428
0
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11429
0
    return false;
11430
11431
0
  return elfcore_grok_note (abfd, note);
11432
0
}
11433
11434
static bool
11435
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11436
0
{
11437
0
  if (note->type == NT_OPENBSD_PROCINFO)
11438
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11439
11440
0
  if (note->type == NT_OPENBSD_REGS)
11441
0
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
11442
11443
0
  if (note->type == NT_OPENBSD_FPREGS)
11444
0
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11445
11446
0
  if (note->type == NT_OPENBSD_XFPREGS)
11447
0
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11448
11449
0
  if (note->type == NT_OPENBSD_AUXV)
11450
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11451
11452
0
  if (note->type == NT_OPENBSD_WCOOKIE)
11453
0
    {
11454
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11455
0
                 SEC_HAS_CONTENTS);
11456
11457
0
      if (sect == NULL)
11458
0
  return false;
11459
0
      sect->size = note->descsz;
11460
0
      sect->filepos = note->descpos;
11461
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11462
11463
0
      return true;
11464
0
    }
11465
11466
0
  return true;
11467
0
}
11468
11469
static bool
11470
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11471
0
{
11472
0
  void *ddata = note->descdata;
11473
0
  char buf[100];
11474
0
  char *name;
11475
0
  asection *sect;
11476
0
  short sig;
11477
0
  unsigned flags;
11478
11479
0
  if (note->descsz < 16)
11480
0
    return false;
11481
11482
  /* nto_procfs_status 'pid' field is at offset 0.  */
11483
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11484
11485
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11486
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11487
11488
  /* nto_procfs_status 'flags' field is at offset 8.  */
11489
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11490
11491
  /* nto_procfs_status 'what' field is at offset 14.  */
11492
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11493
0
    {
11494
0
      elf_tdata (abfd)->core->signal = sig;
11495
0
      elf_tdata (abfd)->core->lwpid = *tid;
11496
0
    }
11497
11498
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11499
     do not come from signals so we make sure we set the current
11500
     thread just in case.  */
11501
0
  if (flags & 0x00000080)
11502
0
    elf_tdata (abfd)->core->lwpid = *tid;
11503
11504
  /* Make a ".qnx_core_status/%d" section.  */
11505
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11506
11507
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11508
0
  if (name == NULL)
11509
0
    return false;
11510
0
  strcpy (name, buf);
11511
11512
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11513
0
  if (sect == NULL)
11514
0
    return false;
11515
11516
0
  sect->size    = note->descsz;
11517
0
  sect->filepos   = note->descpos;
11518
0
  sect->alignment_power = 2;
11519
11520
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11521
0
}
11522
11523
static bool
11524
elfcore_grok_nto_regs (bfd *abfd,
11525
           Elf_Internal_Note *note,
11526
           long tid,
11527
           char *base)
11528
0
{
11529
0
  char buf[100];
11530
0
  char *name;
11531
0
  asection *sect;
11532
11533
  /* Make a "(base)/%d" section.  */
11534
0
  sprintf (buf, "%s/%ld", base, tid);
11535
11536
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11537
0
  if (name == NULL)
11538
0
    return false;
11539
0
  strcpy (name, buf);
11540
11541
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11542
0
  if (sect == NULL)
11543
0
    return false;
11544
11545
0
  sect->size    = note->descsz;
11546
0
  sect->filepos   = note->descpos;
11547
0
  sect->alignment_power = 2;
11548
11549
  /* This is the current thread.  */
11550
0
  if (elf_tdata (abfd)->core->lwpid == tid)
11551
0
    return elfcore_maybe_make_sect (abfd, base, sect);
11552
11553
0
  return true;
11554
0
}
11555
11556
static bool
11557
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11558
0
{
11559
  /* Every GREG section has a STATUS section before it.  Store the
11560
     tid from the previous call to pass down to the next gregs
11561
     function.  */
11562
0
  static long tid = 1;
11563
11564
0
  switch (note->type)
11565
0
    {
11566
0
    case QNT_CORE_INFO:
11567
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11568
0
    case QNT_CORE_STATUS:
11569
0
      return elfcore_grok_nto_status (abfd, note, &tid);
11570
0
    case QNT_CORE_GREG:
11571
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11572
0
    case QNT_CORE_FPREG:
11573
0
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11574
0
    default:
11575
0
      return true;
11576
0
    }
11577
0
}
11578
11579
static bool
11580
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11581
0
{
11582
0
  char *name;
11583
0
  asection *sect;
11584
0
  size_t len;
11585
11586
  /* Use note name as section name.  */
11587
0
  len = note->namesz;
11588
0
  name = (char *) bfd_alloc (abfd, len);
11589
0
  if (name == NULL)
11590
0
    return false;
11591
0
  memcpy (name, note->namedata, len);
11592
0
  name[len - 1] = '\0';
11593
11594
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11595
0
  if (sect == NULL)
11596
0
    return false;
11597
11598
0
  sect->size    = note->descsz;
11599
0
  sect->filepos   = note->descpos;
11600
0
  sect->alignment_power = 1;
11601
11602
0
  return true;
11603
0
}
11604
11605
/* Function: elfcore_write_note
11606
11607
   Inputs:
11608
     buffer to hold note, and current size of buffer
11609
     name of note
11610
     type of note
11611
     data for note
11612
     size of data for note
11613
11614
   Writes note to end of buffer.  ELF64 notes are written exactly as
11615
   for ELF32, despite the current (as of 2006) ELF gabi specifying
11616
   that they ought to have 8-byte namesz and descsz field, and have
11617
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
11618
11619
   Return:
11620
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
11621
11622
char *
11623
elfcore_write_note (bfd *abfd,
11624
        char *buf,
11625
        int *bufsiz,
11626
        const char *name,
11627
        int type,
11628
        const void *input,
11629
        int size)
11630
0
{
11631
0
  Elf_External_Note *xnp;
11632
0
  size_t namesz;
11633
0
  size_t newspace;
11634
0
  char *dest;
11635
11636
0
  namesz = 0;
11637
0
  if (name != NULL)
11638
0
    namesz = strlen (name) + 1;
11639
11640
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11641
11642
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
11643
0
  if (buf == NULL)
11644
0
    return buf;
11645
0
  dest = buf + *bufsiz;
11646
0
  *bufsiz += newspace;
11647
0
  xnp = (Elf_External_Note *) dest;
11648
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
11649
0
  H_PUT_32 (abfd, size, xnp->descsz);
11650
0
  H_PUT_32 (abfd, type, xnp->type);
11651
0
  dest = xnp->name;
11652
0
  if (name != NULL)
11653
0
    {
11654
0
      memcpy (dest, name, namesz);
11655
0
      dest += namesz;
11656
0
      while (namesz & 3)
11657
0
  {
11658
0
    *dest++ = '\0';
11659
0
    ++namesz;
11660
0
  }
11661
0
    }
11662
0
  memcpy (dest, input, size);
11663
0
  dest += size;
11664
0
  while (size & 3)
11665
0
    {
11666
0
      *dest++ = '\0';
11667
0
      ++size;
11668
0
    }
11669
0
  return buf;
11670
0
}
11671
11672
/* gcc-8 warns (*) on all the strncpy calls in this function about
11673
   possible string truncation.  The "truncation" is not a bug.  We
11674
   have an external representation of structs with fields that are not
11675
   necessarily NULL terminated and corresponding internal
11676
   representation fields that are one larger so that they can always
11677
   be NULL terminated.
11678
   gcc versions between 4.2 and 4.6 do not allow pragma control of
11679
   diagnostics inside functions, giving a hard error if you try to use
11680
   the finer control available with later versions.
11681
   gcc prior to 4.2 warns about diagnostic push and pop.
11682
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11683
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11684
   (*) Depending on your system header files!  */
11685
#if GCC_VERSION >= 8000
11686
# pragma GCC diagnostic push
11687
# pragma GCC diagnostic ignored "-Wstringop-truncation"
11688
#endif
11689
char *
11690
elfcore_write_prpsinfo (bfd  *abfd,
11691
      char *buf,
11692
      int  *bufsiz,
11693
      const char *fname,
11694
      const char *psargs)
11695
0
{
11696
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11697
11698
0
  if (bed->elf_backend_write_core_note != NULL)
11699
0
    {
11700
0
      char *ret;
11701
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11702
0
             NT_PRPSINFO, fname, psargs);
11703
0
      if (ret != NULL)
11704
0
  return ret;
11705
0
    }
11706
11707
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11708
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11709
0
  if (bed->s->elfclass == ELFCLASS32)
11710
0
    {
11711
#  if defined (HAVE_PSINFO32_T)
11712
      psinfo32_t data;
11713
      int note_type = NT_PSINFO;
11714
#  else
11715
0
      prpsinfo32_t data;
11716
0
      int note_type = NT_PRPSINFO;
11717
0
#  endif
11718
11719
0
      memset (&data, 0, sizeof (data));
11720
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11721
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11722
0
      return elfcore_write_note (abfd, buf, bufsiz,
11723
0
         "CORE", note_type, &data, sizeof (data));
11724
0
    }
11725
0
  else
11726
0
# endif
11727
0
    {
11728
# if defined (HAVE_PSINFO_T)
11729
      psinfo_t data;
11730
      int note_type = NT_PSINFO;
11731
# else
11732
0
      prpsinfo_t data;
11733
0
      int note_type = NT_PRPSINFO;
11734
0
# endif
11735
11736
0
      memset (&data, 0, sizeof (data));
11737
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11738
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11739
0
      return elfcore_write_note (abfd, buf, bufsiz,
11740
0
         "CORE", note_type, &data, sizeof (data));
11741
0
    }
11742
0
#endif  /* PSINFO_T or PRPSINFO_T */
11743
11744
0
  free (buf);
11745
0
  return NULL;
11746
0
}
11747
#if GCC_VERSION >= 8000
11748
# pragma GCC diagnostic pop
11749
#endif
11750
11751
char *
11752
elfcore_write_linux_prpsinfo32
11753
  (bfd *abfd, char *buf, int *bufsiz,
11754
   const struct elf_internal_linux_prpsinfo *prpsinfo)
11755
0
{
11756
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11757
0
    {
11758
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
11759
11760
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11761
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11762
0
         &data, sizeof (data));
11763
0
    }
11764
0
  else
11765
0
    {
11766
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
11767
11768
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11769
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11770
0
         &data, sizeof (data));
11771
0
    }
11772
0
}
11773
11774
char *
11775
elfcore_write_linux_prpsinfo64
11776
  (bfd *abfd, char *buf, int *bufsiz,
11777
   const struct elf_internal_linux_prpsinfo *prpsinfo)
11778
0
{
11779
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11780
0
    {
11781
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
11782
11783
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11784
0
      return elfcore_write_note (abfd, buf, bufsiz,
11785
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
11786
0
    }
11787
0
  else
11788
0
    {
11789
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
11790
11791
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11792
0
      return elfcore_write_note (abfd, buf, bufsiz,
11793
0
         "CORE", NT_PRPSINFO, &data, sizeof (data));
11794
0
    }
11795
0
}
11796
11797
char *
11798
elfcore_write_prstatus (bfd *abfd,
11799
      char *buf,
11800
      int *bufsiz,
11801
      long pid,
11802
      int cursig,
11803
      const void *gregs)
11804
0
{
11805
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11806
11807
0
  if (bed->elf_backend_write_core_note != NULL)
11808
0
    {
11809
0
      char *ret;
11810
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11811
0
             NT_PRSTATUS,
11812
0
             pid, cursig, gregs);
11813
0
      if (ret != NULL)
11814
0
  return ret;
11815
0
    }
11816
11817
0
#if defined (HAVE_PRSTATUS_T)
11818
0
#if defined (HAVE_PRSTATUS32_T)
11819
0
  if (bed->s->elfclass == ELFCLASS32)
11820
0
    {
11821
0
      prstatus32_t prstat;
11822
11823
0
      memset (&prstat, 0, sizeof (prstat));
11824
0
      prstat.pr_pid = pid;
11825
0
      prstat.pr_cursig = cursig;
11826
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11827
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11828
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
11829
0
    }
11830
0
  else
11831
0
#endif
11832
0
    {
11833
0
      prstatus_t prstat;
11834
11835
0
      memset (&prstat, 0, sizeof (prstat));
11836
0
      prstat.pr_pid = pid;
11837
0
      prstat.pr_cursig = cursig;
11838
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11839
0
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11840
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
11841
0
    }
11842
0
#endif /* HAVE_PRSTATUS_T */
11843
11844
0
  free (buf);
11845
0
  return NULL;
11846
0
}
11847
11848
#if defined (HAVE_LWPSTATUS_T)
11849
char *
11850
elfcore_write_lwpstatus (bfd *abfd,
11851
       char *buf,
11852
       int *bufsiz,
11853
       long pid,
11854
       int cursig,
11855
       const void *gregs)
11856
{
11857
  lwpstatus_t lwpstat;
11858
  const char *note_name = "CORE";
11859
11860
  memset (&lwpstat, 0, sizeof (lwpstat));
11861
  lwpstat.pr_lwpid  = pid >> 16;
11862
  lwpstat.pr_cursig = cursig;
11863
#if defined (HAVE_LWPSTATUS_T_PR_REG)
11864
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11865
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11866
#if !defined(gregs)
11867
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11868
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11869
#else
11870
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11871
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11872
#endif
11873
#endif
11874
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
11875
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11876
}
11877
#endif /* HAVE_LWPSTATUS_T */
11878
11879
#if defined (HAVE_PSTATUS_T)
11880
char *
11881
elfcore_write_pstatus (bfd *abfd,
11882
           char *buf,
11883
           int *bufsiz,
11884
           long pid,
11885
           int cursig ATTRIBUTE_UNUSED,
11886
           const void *gregs ATTRIBUTE_UNUSED)
11887
{
11888
  const char *note_name = "CORE";
11889
#if defined (HAVE_PSTATUS32_T)
11890
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11891
11892
  if (bed->s->elfclass == ELFCLASS32)
11893
    {
11894
      pstatus32_t pstat;
11895
11896
      memset (&pstat, 0, sizeof (pstat));
11897
      pstat.pr_pid = pid & 0xffff;
11898
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11899
        NT_PSTATUS, &pstat, sizeof (pstat));
11900
      return buf;
11901
    }
11902
  else
11903
#endif
11904
    {
11905
      pstatus_t pstat;
11906
11907
      memset (&pstat, 0, sizeof (pstat));
11908
      pstat.pr_pid = pid & 0xffff;
11909
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11910
        NT_PSTATUS, &pstat, sizeof (pstat));
11911
      return buf;
11912
    }
11913
}
11914
#endif /* HAVE_PSTATUS_T */
11915
11916
char *
11917
elfcore_write_prfpreg (bfd *abfd,
11918
           char *buf,
11919
           int *bufsiz,
11920
           const void *fpregs,
11921
           int size)
11922
0
{
11923
0
  const char *note_name = "CORE";
11924
0
  return elfcore_write_note (abfd, buf, bufsiz,
11925
0
           note_name, NT_FPREGSET, fpregs, size);
11926
0
}
11927
11928
char *
11929
elfcore_write_prxfpreg (bfd *abfd,
11930
      char *buf,
11931
      int *bufsiz,
11932
      const void *xfpregs,
11933
      int size)
11934
0
{
11935
0
  char *note_name = "LINUX";
11936
0
  return elfcore_write_note (abfd, buf, bufsiz,
11937
0
           note_name, NT_PRXFPREG, xfpregs, size);
11938
0
}
11939
11940
char *
11941
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11942
       const void *xfpregs, int size)
11943
0
{
11944
0
  char *note_name;
11945
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11946
0
    note_name = "FreeBSD";
11947
0
  else
11948
0
    note_name = "LINUX";
11949
0
  return elfcore_write_note (abfd, buf, bufsiz,
11950
0
           note_name, NT_X86_XSTATE, xfpregs, size);
11951
0
}
11952
11953
char *
11954
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11955
          const void *regs, int size)
11956
0
{
11957
0
  char *note_name = "FreeBSD";
11958
0
  return elfcore_write_note (abfd, buf, bufsiz,
11959
0
           note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11960
0
}
11961
11962
char *
11963
elfcore_write_ppc_vmx (bfd *abfd,
11964
           char *buf,
11965
           int *bufsiz,
11966
           const void *ppc_vmx,
11967
           int size)
11968
0
{
11969
0
  char *note_name = "LINUX";
11970
0
  return elfcore_write_note (abfd, buf, bufsiz,
11971
0
           note_name, NT_PPC_VMX, ppc_vmx, size);
11972
0
}
11973
11974
char *
11975
elfcore_write_ppc_vsx (bfd *abfd,
11976
           char *buf,
11977
           int *bufsiz,
11978
           const void *ppc_vsx,
11979
           int size)
11980
0
{
11981
0
  char *note_name = "LINUX";
11982
0
  return elfcore_write_note (abfd, buf, bufsiz,
11983
0
           note_name, NT_PPC_VSX, ppc_vsx, size);
11984
0
}
11985
11986
char *
11987
elfcore_write_ppc_tar (bfd *abfd,
11988
           char *buf,
11989
           int *bufsiz,
11990
           const void *ppc_tar,
11991
           int size)
11992
0
{
11993
0
  char *note_name = "LINUX";
11994
0
  return elfcore_write_note (abfd, buf, bufsiz,
11995
0
           note_name, NT_PPC_TAR, ppc_tar, size);
11996
0
}
11997
11998
char *
11999
elfcore_write_ppc_ppr (bfd *abfd,
12000
           char *buf,
12001
           int *bufsiz,
12002
           const void *ppc_ppr,
12003
           int size)
12004
0
{
12005
0
  char *note_name = "LINUX";
12006
0
  return elfcore_write_note (abfd, buf, bufsiz,
12007
0
           note_name, NT_PPC_PPR, ppc_ppr, size);
12008
0
}
12009
12010
char *
12011
elfcore_write_ppc_dscr (bfd *abfd,
12012
      char *buf,
12013
      int *bufsiz,
12014
      const void *ppc_dscr,
12015
      int size)
12016
0
{
12017
0
  char *note_name = "LINUX";
12018
0
  return elfcore_write_note (abfd, buf, bufsiz,
12019
0
           note_name, NT_PPC_DSCR, ppc_dscr, size);
12020
0
}
12021
12022
char *
12023
elfcore_write_ppc_ebb (bfd *abfd,
12024
           char *buf,
12025
           int *bufsiz,
12026
           const void *ppc_ebb,
12027
           int size)
12028
0
{
12029
0
  char *note_name = "LINUX";
12030
0
  return elfcore_write_note (abfd, buf, bufsiz,
12031
0
           note_name, NT_PPC_EBB, ppc_ebb, size);
12032
0
}
12033
12034
char *
12035
elfcore_write_ppc_pmu (bfd *abfd,
12036
           char *buf,
12037
           int *bufsiz,
12038
           const void *ppc_pmu,
12039
           int size)
12040
0
{
12041
0
  char *note_name = "LINUX";
12042
0
  return elfcore_write_note (abfd, buf, bufsiz,
12043
0
           note_name, NT_PPC_PMU, ppc_pmu, size);
12044
0
}
12045
12046
char *
12047
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12048
         char *buf,
12049
         int *bufsiz,
12050
         const void *ppc_tm_cgpr,
12051
         int size)
12052
0
{
12053
0
  char *note_name = "LINUX";
12054
0
  return elfcore_write_note (abfd, buf, bufsiz,
12055
0
           note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12056
0
}
12057
12058
char *
12059
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12060
         char *buf,
12061
         int *bufsiz,
12062
         const void *ppc_tm_cfpr,
12063
         int size)
12064
0
{
12065
0
  char *note_name = "LINUX";
12066
0
  return elfcore_write_note (abfd, buf, bufsiz,
12067
0
           note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12068
0
}
12069
12070
char *
12071
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12072
         char *buf,
12073
         int *bufsiz,
12074
         const void *ppc_tm_cvmx,
12075
         int size)
12076
0
{
12077
0
  char *note_name = "LINUX";
12078
0
  return elfcore_write_note (abfd, buf, bufsiz,
12079
0
           note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12080
0
}
12081
12082
char *
12083
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12084
         char *buf,
12085
         int *bufsiz,
12086
         const void *ppc_tm_cvsx,
12087
         int size)
12088
0
{
12089
0
  char *note_name = "LINUX";
12090
0
  return elfcore_write_note (abfd, buf, bufsiz,
12091
0
           note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12092
0
}
12093
12094
char *
12095
elfcore_write_ppc_tm_spr (bfd *abfd,
12096
        char *buf,
12097
        int *bufsiz,
12098
        const void *ppc_tm_spr,
12099
        int size)
12100
0
{
12101
0
  char *note_name = "LINUX";
12102
0
  return elfcore_write_note (abfd, buf, bufsiz,
12103
0
           note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12104
0
}
12105
12106
char *
12107
elfcore_write_ppc_tm_ctar (bfd *abfd,
12108
         char *buf,
12109
         int *bufsiz,
12110
         const void *ppc_tm_ctar,
12111
         int size)
12112
0
{
12113
0
  char *note_name = "LINUX";
12114
0
  return elfcore_write_note (abfd, buf, bufsiz,
12115
0
           note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12116
0
}
12117
12118
char *
12119
elfcore_write_ppc_tm_cppr (bfd *abfd,
12120
         char *buf,
12121
         int *bufsiz,
12122
         const void *ppc_tm_cppr,
12123
         int size)
12124
0
{
12125
0
  char *note_name = "LINUX";
12126
0
  return elfcore_write_note (abfd, buf, bufsiz,
12127
0
           note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12128
0
}
12129
12130
char *
12131
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12132
          char *buf,
12133
          int *bufsiz,
12134
          const void *ppc_tm_cdscr,
12135
          int size)
12136
0
{
12137
0
  char *note_name = "LINUX";
12138
0
  return elfcore_write_note (abfd, buf, bufsiz,
12139
0
           note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12140
0
}
12141
12142
static char *
12143
elfcore_write_s390_high_gprs (bfd *abfd,
12144
            char *buf,
12145
            int *bufsiz,
12146
            const void *s390_high_gprs,
12147
            int size)
12148
0
{
12149
0
  char *note_name = "LINUX";
12150
0
  return elfcore_write_note (abfd, buf, bufsiz,
12151
0
           note_name, NT_S390_HIGH_GPRS,
12152
0
           s390_high_gprs, size);
12153
0
}
12154
12155
char *
12156
elfcore_write_s390_timer (bfd *abfd,
12157
        char *buf,
12158
        int *bufsiz,
12159
        const void *s390_timer,
12160
        int size)
12161
0
{
12162
0
  char *note_name = "LINUX";
12163
0
  return elfcore_write_note (abfd, buf, bufsiz,
12164
0
           note_name, NT_S390_TIMER, s390_timer, size);
12165
0
}
12166
12167
char *
12168
elfcore_write_s390_todcmp (bfd *abfd,
12169
         char *buf,
12170
         int *bufsiz,
12171
         const void *s390_todcmp,
12172
         int size)
12173
0
{
12174
0
  char *note_name = "LINUX";
12175
0
  return elfcore_write_note (abfd, buf, bufsiz,
12176
0
           note_name, NT_S390_TODCMP, s390_todcmp, size);
12177
0
}
12178
12179
char *
12180
elfcore_write_s390_todpreg (bfd *abfd,
12181
          char *buf,
12182
          int *bufsiz,
12183
          const void *s390_todpreg,
12184
          int size)
12185
0
{
12186
0
  char *note_name = "LINUX";
12187
0
  return elfcore_write_note (abfd, buf, bufsiz,
12188
0
           note_name, NT_S390_TODPREG, s390_todpreg, size);
12189
0
}
12190
12191
char *
12192
elfcore_write_s390_ctrs (bfd *abfd,
12193
       char *buf,
12194
       int *bufsiz,
12195
       const void *s390_ctrs,
12196
       int size)
12197
0
{
12198
0
  char *note_name = "LINUX";
12199
0
  return elfcore_write_note (abfd, buf, bufsiz,
12200
0
           note_name, NT_S390_CTRS, s390_ctrs, size);
12201
0
}
12202
12203
char *
12204
elfcore_write_s390_prefix (bfd *abfd,
12205
         char *buf,
12206
         int *bufsiz,
12207
         const void *s390_prefix,
12208
         int size)
12209
0
{
12210
0
  char *note_name = "LINUX";
12211
0
  return elfcore_write_note (abfd, buf, bufsiz,
12212
0
           note_name, NT_S390_PREFIX, s390_prefix, size);
12213
0
}
12214
12215
char *
12216
elfcore_write_s390_last_break (bfd *abfd,
12217
             char *buf,
12218
             int *bufsiz,
12219
             const void *s390_last_break,
12220
             int size)
12221
0
{
12222
0
  char *note_name = "LINUX";
12223
0
  return elfcore_write_note (abfd, buf, bufsiz,
12224
0
           note_name, NT_S390_LAST_BREAK,
12225
0
           s390_last_break, size);
12226
0
}
12227
12228
char *
12229
elfcore_write_s390_system_call (bfd *abfd,
12230
        char *buf,
12231
        int *bufsiz,
12232
        const void *s390_system_call,
12233
        int size)
12234
0
{
12235
0
  char *note_name = "LINUX";
12236
0
  return elfcore_write_note (abfd, buf, bufsiz,
12237
0
           note_name, NT_S390_SYSTEM_CALL,
12238
0
           s390_system_call, size);
12239
0
}
12240
12241
char *
12242
elfcore_write_s390_tdb (bfd *abfd,
12243
      char *buf,
12244
      int *bufsiz,
12245
      const void *s390_tdb,
12246
      int size)
12247
0
{
12248
0
  char *note_name = "LINUX";
12249
0
  return elfcore_write_note (abfd, buf, bufsiz,
12250
0
           note_name, NT_S390_TDB, s390_tdb, size);
12251
0
}
12252
12253
char *
12254
elfcore_write_s390_vxrs_low (bfd *abfd,
12255
           char *buf,
12256
           int *bufsiz,
12257
           const void *s390_vxrs_low,
12258
           int size)
12259
0
{
12260
0
  char *note_name = "LINUX";
12261
0
  return elfcore_write_note (abfd, buf, bufsiz,
12262
0
           note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12263
0
}
12264
12265
char *
12266
elfcore_write_s390_vxrs_high (bfd *abfd,
12267
           char *buf,
12268
           int *bufsiz,
12269
           const void *s390_vxrs_high,
12270
           int size)
12271
0
{
12272
0
  char *note_name = "LINUX";
12273
0
  return elfcore_write_note (abfd, buf, bufsiz,
12274
0
           note_name, NT_S390_VXRS_HIGH,
12275
0
           s390_vxrs_high, size);
12276
0
}
12277
12278
char *
12279
elfcore_write_s390_gs_cb (bfd *abfd,
12280
        char *buf,
12281
        int *bufsiz,
12282
        const void *s390_gs_cb,
12283
        int size)
12284
0
{
12285
0
  char *note_name = "LINUX";
12286
0
  return elfcore_write_note (abfd, buf, bufsiz,
12287
0
           note_name, NT_S390_GS_CB,
12288
0
           s390_gs_cb, size);
12289
0
}
12290
12291
char *
12292
elfcore_write_s390_gs_bc (bfd *abfd,
12293
        char *buf,
12294
        int *bufsiz,
12295
        const void *s390_gs_bc,
12296
        int size)
12297
0
{
12298
0
  char *note_name = "LINUX";
12299
0
  return elfcore_write_note (abfd, buf, bufsiz,
12300
0
           note_name, NT_S390_GS_BC,
12301
0
           s390_gs_bc, size);
12302
0
}
12303
12304
char *
12305
elfcore_write_arm_vfp (bfd *abfd,
12306
           char *buf,
12307
           int *bufsiz,
12308
           const void *arm_vfp,
12309
           int size)
12310
0
{
12311
0
  char *note_name = "LINUX";
12312
0
  return elfcore_write_note (abfd, buf, bufsiz,
12313
0
           note_name, NT_ARM_VFP, arm_vfp, size);
12314
0
}
12315
12316
char *
12317
elfcore_write_aarch_tls (bfd *abfd,
12318
           char *buf,
12319
           int *bufsiz,
12320
           const void *aarch_tls,
12321
           int size)
12322
0
{
12323
0
  char *note_name = "LINUX";
12324
0
  return elfcore_write_note (abfd, buf, bufsiz,
12325
0
           note_name, NT_ARM_TLS, aarch_tls, size);
12326
0
}
12327
12328
char *
12329
elfcore_write_aarch_hw_break (bfd *abfd,
12330
          char *buf,
12331
          int *bufsiz,
12332
          const void *aarch_hw_break,
12333
          int size)
12334
0
{
12335
0
  char *note_name = "LINUX";
12336
0
  return elfcore_write_note (abfd, buf, bufsiz,
12337
0
           note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12338
0
}
12339
12340
char *
12341
elfcore_write_aarch_hw_watch (bfd *abfd,
12342
          char *buf,
12343
          int *bufsiz,
12344
          const void *aarch_hw_watch,
12345
          int size)
12346
0
{
12347
0
  char *note_name = "LINUX";
12348
0
  return elfcore_write_note (abfd, buf, bufsiz,
12349
0
           note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12350
0
}
12351
12352
char *
12353
elfcore_write_aarch_sve (bfd *abfd,
12354
       char *buf,
12355
       int *bufsiz,
12356
       const void *aarch_sve,
12357
       int size)
12358
0
{
12359
0
  char *note_name = "LINUX";
12360
0
  return elfcore_write_note (abfd, buf, bufsiz,
12361
0
           note_name, NT_ARM_SVE, aarch_sve, size);
12362
0
}
12363
12364
char *
12365
elfcore_write_aarch_pauth (bfd *abfd,
12366
         char *buf,
12367
         int *bufsiz,
12368
         const void *aarch_pauth,
12369
         int size)
12370
0
{
12371
0
  char *note_name = "LINUX";
12372
0
  return elfcore_write_note (abfd, buf, bufsiz,
12373
0
           note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12374
0
}
12375
12376
char *
12377
elfcore_write_aarch_mte (bfd *abfd,
12378
              char *buf,
12379
              int *bufsiz,
12380
              const void *aarch_mte,
12381
              int size)
12382
0
{
12383
0
  char *note_name = "LINUX";
12384
0
  return elfcore_write_note (abfd, buf, bufsiz,
12385
0
           note_name, NT_ARM_TAGGED_ADDR_CTRL,
12386
0
           aarch_mte,
12387
0
           size);
12388
0
}
12389
12390
char *
12391
elfcore_write_arc_v2 (bfd *abfd,
12392
          char *buf,
12393
          int *bufsiz,
12394
          const void *arc_v2,
12395
          int size)
12396
0
{
12397
0
  char *note_name = "LINUX";
12398
0
  return elfcore_write_note (abfd, buf, bufsiz,
12399
0
           note_name, NT_ARC_V2, arc_v2, size);
12400
0
}
12401
12402
char *
12403
elfcore_write_loongarch_cpucfg (bfd *abfd,
12404
        char *buf,
12405
        int *bufsiz,
12406
        const void *loongarch_cpucfg,
12407
        int size)
12408
0
{
12409
0
  char *note_name = "LINUX";
12410
0
  return elfcore_write_note (abfd, buf, bufsiz,
12411
0
           note_name, NT_LARCH_CPUCFG,
12412
0
           loongarch_cpucfg, size);
12413
0
}
12414
12415
char *
12416
elfcore_write_loongarch_lbt (bfd *abfd,
12417
           char *buf,
12418
           int *bufsiz,
12419
           const void *loongarch_lbt,
12420
           int size)
12421
0
{
12422
0
  char *note_name = "LINUX";
12423
0
  return elfcore_write_note (abfd, buf, bufsiz,
12424
0
           note_name, NT_LARCH_LBT, loongarch_lbt, size);
12425
0
}
12426
12427
char *
12428
elfcore_write_loongarch_lsx (bfd *abfd,
12429
           char *buf,
12430
           int *bufsiz,
12431
           const void *loongarch_lsx,
12432
           int size)
12433
0
{
12434
0
  char *note_name = "LINUX";
12435
0
  return elfcore_write_note (abfd, buf, bufsiz,
12436
0
           note_name, NT_LARCH_LSX, loongarch_lsx, size);
12437
0
}
12438
12439
char *
12440
elfcore_write_loongarch_lasx (bfd *abfd,
12441
            char *buf,
12442
            int *bufsiz,
12443
            const void *loongarch_lasx,
12444
            int size)
12445
0
{
12446
0
  char *note_name = "LINUX";
12447
0
  return elfcore_write_note (abfd, buf, bufsiz,
12448
0
           note_name, NT_LARCH_LASX, loongarch_lasx, size);
12449
0
}
12450
12451
/* Write the buffer of csr values in CSRS (length SIZE) into the note
12452
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12453
   written into.  Return a pointer to the new start of the note buffer, to
12454
   replace BUF which may no longer be valid.  */
12455
12456
char *
12457
elfcore_write_riscv_csr (bfd *abfd,
12458
       char *buf,
12459
       int *bufsiz,
12460
       const void *csrs,
12461
       int size)
12462
0
{
12463
0
  const char *note_name = "GDB";
12464
0
  return elfcore_write_note (abfd, buf, bufsiz,
12465
0
           note_name, NT_RISCV_CSR, csrs, size);
12466
0
}
12467
12468
/* Write the target description (a string) pointed to by TDESC, length
12469
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
12470
   note is being written into.  Return a pointer to the new start of the
12471
   note buffer, to replace BUF which may no longer be valid.  */
12472
12473
char *
12474
elfcore_write_gdb_tdesc (bfd *abfd,
12475
       char *buf,
12476
       int *bufsiz,
12477
       const void *tdesc,
12478
       int size)
12479
0
{
12480
0
  const char *note_name = "GDB";
12481
0
  return elfcore_write_note (abfd, buf, bufsiz,
12482
0
           note_name, NT_GDB_TDESC, tdesc, size);
12483
0
}
12484
12485
char *
12486
elfcore_write_register_note (bfd *abfd,
12487
           char *buf,
12488
           int *bufsiz,
12489
           const char *section,
12490
           const void *data,
12491
           int size)
12492
0
{
12493
0
  if (strcmp (section, ".reg2") == 0)
12494
0
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12495
0
  if (strcmp (section, ".reg-xfp") == 0)
12496
0
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12497
0
  if (strcmp (section, ".reg-xstate") == 0)
12498
0
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12499
0
  if (strcmp (section, ".reg-x86-segbases") == 0)
12500
0
    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12501
0
  if (strcmp (section, ".reg-ppc-vmx") == 0)
12502
0
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12503
0
  if (strcmp (section, ".reg-ppc-vsx") == 0)
12504
0
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12505
0
  if (strcmp (section, ".reg-ppc-tar") == 0)
12506
0
    return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12507
0
  if (strcmp (section, ".reg-ppc-ppr") == 0)
12508
0
    return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12509
0
  if (strcmp (section, ".reg-ppc-dscr") == 0)
12510
0
    return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12511
0
  if (strcmp (section, ".reg-ppc-ebb") == 0)
12512
0
    return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12513
0
  if (strcmp (section, ".reg-ppc-pmu") == 0)
12514
0
    return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12515
0
  if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12516
0
    return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12517
0
  if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12518
0
    return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12519
0
  if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12520
0
    return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12521
0
  if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12522
0
    return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12523
0
  if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12524
0
    return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12525
0
  if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12526
0
    return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12527
0
  if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12528
0
    return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12529
0
  if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12530
0
    return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12531
0
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
12532
0
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12533
0
  if (strcmp (section, ".reg-s390-timer") == 0)
12534
0
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12535
0
  if (strcmp (section, ".reg-s390-todcmp") == 0)
12536
0
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12537
0
  if (strcmp (section, ".reg-s390-todpreg") == 0)
12538
0
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12539
0
  if (strcmp (section, ".reg-s390-ctrs") == 0)
12540
0
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12541
0
  if (strcmp (section, ".reg-s390-prefix") == 0)
12542
0
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12543
0
  if (strcmp (section, ".reg-s390-last-break") == 0)
12544
0
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12545
0
  if (strcmp (section, ".reg-s390-system-call") == 0)
12546
0
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12547
0
  if (strcmp (section, ".reg-s390-tdb") == 0)
12548
0
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12549
0
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12550
0
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12551
0
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12552
0
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12553
0
  if (strcmp (section, ".reg-s390-gs-cb") == 0)
12554
0
    return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12555
0
  if (strcmp (section, ".reg-s390-gs-bc") == 0)
12556
0
    return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12557
0
  if (strcmp (section, ".reg-arm-vfp") == 0)
12558
0
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12559
0
  if (strcmp (section, ".reg-aarch-tls") == 0)
12560
0
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12561
0
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
12562
0
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12563
0
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12564
0
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12565
0
  if (strcmp (section, ".reg-aarch-sve") == 0)
12566
0
    return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12567
0
  if (strcmp (section, ".reg-aarch-pauth") == 0)
12568
0
    return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12569
0
  if (strcmp (section, ".reg-aarch-mte") == 0)
12570
0
    return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12571
0
  if (strcmp (section, ".reg-arc-v2") == 0)
12572
0
    return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12573
0
  if (strcmp (section, ".gdb-tdesc") == 0)
12574
0
    return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12575
0
  if (strcmp (section, ".reg-riscv-csr") == 0)
12576
0
    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12577
0
  if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12578
0
    return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12579
0
  if (strcmp (section, ".reg-loongarch-lbt") == 0)
12580
0
    return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12581
0
  if (strcmp (section, ".reg-loongarch-lsx") == 0)
12582
0
    return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12583
0
  if (strcmp (section, ".reg-loongarch-lasx") == 0)
12584
0
    return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12585
0
  return NULL;
12586
0
}
12587
12588
char *
12589
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12590
       const void *buf, int bufsiz)
12591
0
{
12592
0
  return elfcore_write_note (obfd, note_data, note_size,
12593
0
           "CORE", NT_FILE, buf, bufsiz);
12594
0
}
12595
12596
static bool
12597
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12598
     size_t align)
12599
16.3k
{
12600
16.3k
  char *p;
12601
12602
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12603
     gABI specifies that PT_NOTE alignment should be aligned to 4
12604
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
12605
     align is less than 4, we use 4 byte alignment.   */
12606
16.3k
  if (align < 4)
12607
2.72k
    align = 4;
12608
16.3k
  if (align != 4 && align != 8)
12609
7.44k
    return false;
12610
12611
8.87k
  p = buf;
12612
15.2k
  while (p < buf + size)
12613
13.2k
    {
12614
13.2k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
12615
13.2k
      Elf_Internal_Note in;
12616
12617
13.2k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
12618
1.56k
  return false;
12619
12620
11.7k
      in.type = H_GET_32 (abfd, xnp->type);
12621
12622
11.7k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
12623
11.7k
      in.namedata = xnp->name;
12624
11.7k
      if (in.namesz > buf - in.namedata + size)
12625
3.18k
  return false;
12626
12627
8.52k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
12628
8.52k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12629
8.52k
      in.descpos = offset + (in.descdata - buf);
12630
8.52k
      if (in.descsz != 0
12631
8.52k
    && (in.descdata >= buf + size
12632
2.99k
        || in.descsz > buf - in.descdata + size))
12633
2.14k
  return false;
12634
12635
6.38k
      switch (bfd_get_format (abfd))
12636
6.38k
  {
12637
0
  default:
12638
0
    return true;
12639
12640
0
  case bfd_core:
12641
0
    {
12642
0
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12643
0
      struct
12644
0
      {
12645
0
        const char * string;
12646
0
        size_t len;
12647
0
        bool (*func) (bfd *, Elf_Internal_Note *);
12648
0
      }
12649
0
      grokers[] =
12650
0
      {
12651
0
        GROKER_ELEMENT ("", elfcore_grok_note),
12652
0
        GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12653
0
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12654
0
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12655
0
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12656
0
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12657
0
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12658
0
        GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12659
0
      };
12660
0
#undef GROKER_ELEMENT
12661
0
      int i;
12662
12663
0
      for (i = ARRAY_SIZE (grokers); i--;)
12664
0
        {
12665
0
    if (in.namesz >= grokers[i].len
12666
0
        && strncmp (in.namedata, grokers[i].string,
12667
0
        grokers[i].len) == 0)
12668
0
      {
12669
0
        if (! grokers[i].func (abfd, & in))
12670
0
          return false;
12671
0
        break;
12672
0
      }
12673
0
        }
12674
0
      break;
12675
0
    }
12676
12677
6.38k
  case bfd_object:
12678
6.38k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12679
0
      {
12680
0
        if (! elfobj_grok_gnu_note (abfd, &in))
12681
0
    return false;
12682
0
      }
12683
6.38k
    else if (in.namesz == sizeof "stapsdt"
12684
6.38k
       && strcmp (in.namedata, "stapsdt") == 0)
12685
0
      {
12686
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
12687
0
    return false;
12688
0
      }
12689
6.38k
    break;
12690
6.38k
  }
12691
12692
6.38k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12693
6.38k
    }
12694
12695
1.98k
  return true;
12696
8.87k
}
12697
12698
bool
12699
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12700
    size_t align)
12701
0
{
12702
0
  char *buf;
12703
12704
0
  if (size == 0 || (size + 1) == 0)
12705
0
    return true;
12706
12707
0
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12708
0
    return false;
12709
12710
0
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12711
0
  if (buf == NULL)
12712
0
    return false;
12713
12714
  /* PR 17512: file: ec08f814
12715
     0-termintate the buffer so that string searches will not overflow.  */
12716
0
  buf[size] = 0;
12717
12718
0
  if (!elf_parse_notes (abfd, buf, size, offset, align))
12719
0
    {
12720
0
      free (buf);
12721
0
      return false;
12722
0
    }
12723
12724
0
  free (buf);
12725
0
  return true;
12726
0
}
12727

12728
/* Providing external access to the ELF program header table.  */
12729
12730
/* Return an upper bound on the number of bytes required to store a
12731
   copy of ABFD's program header table entries.  Return -1 if an error
12732
   occurs; bfd_get_error will return an appropriate code.  */
12733
12734
long
12735
bfd_get_elf_phdr_upper_bound (bfd *abfd)
12736
0
{
12737
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
12738
0
    {
12739
0
      bfd_set_error (bfd_error_wrong_format);
12740
0
      return -1;
12741
0
    }
12742
12743
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12744
0
}
12745
12746
/* Copy ABFD's program header table entries to *PHDRS.  The entries
12747
   will be stored as an array of Elf_Internal_Phdr structures, as
12748
   defined in include/elf/internal.h.  To find out how large the
12749
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12750
12751
   Return the number of program header table entries read, or -1 if an
12752
   error occurs; bfd_get_error will return an appropriate code.  */
12753
12754
int
12755
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12756
0
{
12757
0
  int num_phdrs;
12758
12759
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
12760
0
    {
12761
0
      bfd_set_error (bfd_error_wrong_format);
12762
0
      return -1;
12763
0
    }
12764
12765
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
12766
0
  if (num_phdrs != 0)
12767
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
12768
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
12769
12770
0
  return num_phdrs;
12771
0
}
12772
12773
enum elf_reloc_type_class
12774
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12775
         const asection *rel_sec ATTRIBUTE_UNUSED,
12776
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12777
0
{
12778
0
  return reloc_class_normal;
12779
0
}
12780
12781
/* For RELA architectures, return the relocation value for a
12782
   relocation against a local symbol.  */
12783
12784
bfd_vma
12785
_bfd_elf_rela_local_sym (bfd *abfd,
12786
       Elf_Internal_Sym *sym,
12787
       asection **psec,
12788
       Elf_Internal_Rela *rel)
12789
0
{
12790
0
  asection *sec = *psec;
12791
0
  bfd_vma relocation;
12792
12793
0
  relocation = (sec->output_section->vma
12794
0
    + sec->output_offset
12795
0
    + sym->st_value);
12796
0
  if ((sec->flags & SEC_MERGE)
12797
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12798
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12799
0
    {
12800
0
      rel->r_addend =
12801
0
  _bfd_merged_section_offset (abfd, psec,
12802
0
            elf_section_data (sec)->sec_info,
12803
0
            sym->st_value + rel->r_addend);
12804
0
      if (sec != *psec)
12805
0
  {
12806
    /* If we have changed the section, and our original section is
12807
       marked with SEC_EXCLUDE, it means that the original
12808
       SEC_MERGE section has been completely subsumed in some
12809
       other SEC_MERGE section.  In this case, we need to leave
12810
       some info around for --emit-relocs.  */
12811
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
12812
0
      sec->kept_section = *psec;
12813
0
    sec = *psec;
12814
0
  }
12815
0
      rel->r_addend -= relocation;
12816
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
12817
0
    }
12818
0
  return relocation;
12819
0
}
12820
12821
bfd_vma
12822
_bfd_elf_rel_local_sym (bfd *abfd,
12823
      Elf_Internal_Sym *sym,
12824
      asection **psec,
12825
      bfd_vma addend)
12826
0
{
12827
0
  asection *sec = *psec;
12828
12829
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12830
0
    return sym->st_value + addend;
12831
12832
0
  return _bfd_merged_section_offset (abfd, psec,
12833
0
             elf_section_data (sec)->sec_info,
12834
0
             sym->st_value + addend);
12835
0
}
12836
12837
/* Adjust an address within a section.  Given OFFSET within SEC, return
12838
   the new offset within the section, based upon changes made to the
12839
   section.  Returns -1 if the offset is now invalid.
12840
   The offset (in abnd out) is in target sized bytes, however big a
12841
   byte may be.  */
12842
12843
bfd_vma
12844
_bfd_elf_section_offset (bfd *abfd,
12845
       struct bfd_link_info *info,
12846
       asection *sec,
12847
       bfd_vma offset)
12848
0
{
12849
0
  switch (sec->sec_info_type)
12850
0
    {
12851
0
    case SEC_INFO_TYPE_STABS:
12852
0
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12853
0
               offset);
12854
0
    case SEC_INFO_TYPE_EH_FRAME:
12855
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12856
12857
0
    default:
12858
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12859
0
  {
12860
    /* Reverse the offset.  */
12861
0
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12862
0
    bfd_size_type address_size = bed->s->arch_size / 8;
12863
12864
    /* address_size and sec->size are in octets.  Convert
12865
       to bytes before subtracting the original offset.  */
12866
0
    offset = ((sec->size - address_size)
12867
0
        / bfd_octets_per_byte (abfd, sec) - offset);
12868
0
  }
12869
0
      return offset;
12870
0
    }
12871
0
}
12872

12873
long
12874
_bfd_elf_get_synthetic_symtab (bfd *abfd,
12875
             long symcount ATTRIBUTE_UNUSED,
12876
             asymbol **syms ATTRIBUTE_UNUSED,
12877
             long dynsymcount,
12878
             asymbol **dynsyms,
12879
             asymbol **ret)
12880
0
{
12881
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12882
0
  asection *relplt;
12883
0
  asymbol *s;
12884
0
  const char *relplt_name;
12885
0
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12886
0
  arelent *p;
12887
0
  long count, i, n;
12888
0
  size_t size;
12889
0
  Elf_Internal_Shdr *hdr;
12890
0
  char *names;
12891
0
  asection *plt;
12892
12893
0
  *ret = NULL;
12894
12895
0
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12896
0
    return 0;
12897
12898
0
  if (dynsymcount <= 0)
12899
0
    return 0;
12900
12901
0
  if (!bed->plt_sym_val)
12902
0
    return 0;
12903
12904
0
  relplt_name = bed->relplt_name;
12905
0
  if (relplt_name == NULL)
12906
0
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12907
0
  relplt = bfd_get_section_by_name (abfd, relplt_name);
12908
0
  if (relplt == NULL)
12909
0
    return 0;
12910
12911
0
  hdr = &elf_section_data (relplt)->this_hdr;
12912
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
12913
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12914
0
    return 0;
12915
12916
0
  plt = bfd_get_section_by_name (abfd, ".plt");
12917
0
  if (plt == NULL)
12918
0
    return 0;
12919
12920
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12921
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12922
0
    return -1;
12923
12924
0
  count = NUM_SHDR_ENTRIES (hdr);
12925
0
  size = count * sizeof (asymbol);
12926
0
  p = relplt->relocation;
12927
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12928
0
    {
12929
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12930
0
      if (p->addend != 0)
12931
0
  {
12932
0
#ifdef BFD64
12933
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12934
#else
12935
    size += sizeof ("+0x") - 1 + 8;
12936
#endif
12937
0
  }
12938
0
    }
12939
12940
0
  s = *ret = (asymbol *) bfd_malloc (size);
12941
0
  if (s == NULL)
12942
0
    return -1;
12943
12944
0
  names = (char *) (s + count);
12945
0
  p = relplt->relocation;
12946
0
  n = 0;
12947
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12948
0
    {
12949
0
      size_t len;
12950
0
      bfd_vma addr;
12951
12952
0
      addr = bed->plt_sym_val (i, plt, p);
12953
0
      if (addr == (bfd_vma) -1)
12954
0
  continue;
12955
12956
0
      *s = **p->sym_ptr_ptr;
12957
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
12958
   we are defining a symbol, ensure one of them is set.  */
12959
0
      if ((s->flags & BSF_LOCAL) == 0)
12960
0
  s->flags |= BSF_GLOBAL;
12961
0
      s->flags |= BSF_SYNTHETIC;
12962
0
      s->section = plt;
12963
0
      s->value = addr - plt->vma;
12964
0
      s->name = names;
12965
0
      s->udata.p = NULL;
12966
0
      len = strlen ((*p->sym_ptr_ptr)->name);
12967
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
12968
0
      names += len;
12969
0
      if (p->addend != 0)
12970
0
  {
12971
0
    char buf[30], *a;
12972
12973
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
12974
0
    names += sizeof ("+0x") - 1;
12975
0
    bfd_sprintf_vma (abfd, buf, p->addend);
12976
0
    for (a = buf; *a == '0'; ++a)
12977
0
      ;
12978
0
    len = strlen (a);
12979
0
    memcpy (names, a, len);
12980
0
    names += len;
12981
0
  }
12982
0
      memcpy (names, "@plt", sizeof ("@plt"));
12983
0
      names += sizeof ("@plt");
12984
0
      ++s, ++n;
12985
0
    }
12986
12987
0
  return n;
12988
0
}
12989
12990
/* It is only used by x86-64 so far.
12991
   ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
12992
   but current usage would allow all of _bfd_std_section to be zero.  */
12993
static const asymbol lcomm_sym
12994
  = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12995
asection _bfd_elf_large_com_section
12996
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12997
          "LARGE_COMMON", 0, SEC_IS_COMMON);
12998
12999
bool
13000
_bfd_elf_final_write_processing (bfd *abfd)
13001
0
{
13002
0
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
13003
13004
0
  i_ehdrp = elf_elfheader (abfd);
13005
13006
0
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13007
0
    i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13008
13009
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13010
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13011
     or STB_GNU_UNIQUE binding.  */
13012
0
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13013
0
    {
13014
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13015
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13016
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13017
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13018
0
  {
13019
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13020
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13021
0
          "and FreeBSD targets"));
13022
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13023
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13024
0
          "only by GNU and FreeBSD targets"));
13025
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13026
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13027
0
          "only by GNU and FreeBSD targets"));
13028
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13029
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13030
0
          "only by GNU and FreeBSD targets"));
13031
0
    bfd_set_error (bfd_error_sorry);
13032
0
    return false;
13033
0
  }
13034
0
    }
13035
0
  return true;
13036
0
}
13037
13038
13039
/* Return TRUE for ELF symbol types that represent functions.
13040
   This is the default version of this function, which is sufficient for
13041
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13042
13043
bool
13044
_bfd_elf_is_function_type (unsigned int type)
13045
0
{
13046
0
  return (type == STT_FUNC
13047
0
    || type == STT_GNU_IFUNC);
13048
0
}
13049
13050
/* If the ELF symbol SYM might be a function in SEC, return the
13051
   function size and set *CODE_OFF to the function's entry point,
13052
   otherwise return zero.  */
13053
13054
bfd_size_type
13055
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13056
           bfd_vma *code_off)
13057
0
{
13058
0
  bfd_size_type size;
13059
0
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13060
13061
0
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13062
0
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13063
0
      || sym->section != sec)
13064
0
    return 0;
13065
13066
0
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13067
13068
  /* In theory we should check that the symbol's type satisfies
13069
     _bfd_elf_is_function_type(), but there are some function-like
13070
     symbols which would fail this test.  (eg _start).  Instead
13071
     we check for hidden, local, notype symbols with zero size.
13072
     This type of symbol is generated by the annobin plugin for gcc
13073
     and clang, and should not be considered to be a function symbol.  */
13074
0
  if (size == 0
13075
0
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13076
0
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13077
0
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13078
0
    return 0;
13079
13080
0
  *code_off = sym->value;
13081
  /* Do not return 0 for the function's size.  */
13082
0
  return size ? size : 1;
13083
0
}
13084
13085
/* Set to non-zero to enable some debug messages.  */
13086
#define DEBUG_SECONDARY_RELOCS   0
13087
13088
/* An internal-to-the-bfd-library only section type
13089
   used to indicate a cached secondary reloc section.  */
13090
0
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13091
13092
/* Create a BFD section to hold a secondary reloc section.  */
13093
13094
bool
13095
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13096
               Elf_Internal_Shdr *hdr,
13097
               const char * name,
13098
               unsigned int shindex)
13099
0
{
13100
  /* We only support RELA secondary relocs.  */
13101
0
  if (hdr->sh_type != SHT_RELA)
13102
0
    return false;
13103
13104
#if DEBUG_SECONDARY_RELOCS
13105
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13106
#endif
13107
0
  hdr->sh_type = SHT_SECONDARY_RELOC;
13108
0
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13109
0
}
13110
13111
/* Read in any secondary relocs associated with SEC.  */
13112
13113
bool
13114
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13115
          asection *  sec,
13116
          asymbol **  symbols,
13117
          bool dynamic)
13118
0
{
13119
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13120
0
  asection * relsec;
13121
0
  bool result = true;
13122
0
  bfd_vma (*r_sym) (bfd_vma);
13123
0
  ufile_ptr filesize;
13124
13125
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13126
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13127
0
    r_sym = elf64_r_sym;
13128
0
  else
13129
0
#endif
13130
0
    r_sym = elf32_r_sym;
13131
13132
0
  if (!elf_section_data (sec)->has_secondary_relocs)
13133
0
    return true;
13134
13135
  /* Discover if there are any secondary reloc sections
13136
     associated with SEC.  */
13137
0
  filesize = bfd_get_file_size (abfd);
13138
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13139
0
    {
13140
0
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13141
13142
0
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13143
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13144
0
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13145
0
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13146
0
  {
13147
0
    bfd_byte * native_relocs;
13148
0
    bfd_byte * native_reloc;
13149
0
    arelent * internal_relocs;
13150
0
    arelent * internal_reloc;
13151
0
    size_t i;
13152
0
    unsigned int entsize;
13153
0
    unsigned int symcount;
13154
0
    bfd_size_type reloc_count;
13155
0
    size_t amt;
13156
13157
0
    if (ebd->elf_info_to_howto == NULL)
13158
0
      return false;
13159
13160
#if DEBUG_SECONDARY_RELOCS
13161
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13162
       sec->name, relsec->name);
13163
#endif
13164
0
    entsize = hdr->sh_entsize;
13165
13166
0
    if (filesize != 0
13167
0
        && ((ufile_ptr) hdr->sh_offset > filesize
13168
0
      || hdr->sh_size > filesize - hdr->sh_offset))
13169
0
      {
13170
0
        bfd_set_error (bfd_error_file_truncated);
13171
0
        result = false;
13172
0
        continue;
13173
0
      }
13174
13175
0
    native_relocs = bfd_malloc (hdr->sh_size);
13176
0
    if (native_relocs == NULL)
13177
0
      {
13178
0
        result = false;
13179
0
        continue;
13180
0
      }
13181
13182
0
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13183
0
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13184
0
      {
13185
0
        free (native_relocs);
13186
0
        bfd_set_error (bfd_error_file_too_big);
13187
0
        result = false;
13188
0
        continue;
13189
0
      }
13190
13191
0
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13192
0
    if (internal_relocs == NULL)
13193
0
      {
13194
0
        free (native_relocs);
13195
0
        result = false;
13196
0
        continue;
13197
0
      }
13198
13199
0
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13200
0
        || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13201
0
      != hdr->sh_size))
13202
0
      {
13203
0
        free (native_relocs);
13204
        /* The internal_relocs will be freed when
13205
     the memory for the bfd is released.  */
13206
0
        result = false;
13207
0
        continue;
13208
0
      }
13209
13210
0
    if (dynamic)
13211
0
      symcount = bfd_get_dynamic_symcount (abfd);
13212
0
    else
13213
0
      symcount = bfd_get_symcount (abfd);
13214
13215
0
    for (i = 0, internal_reloc = internal_relocs,
13216
0
     native_reloc = native_relocs;
13217
0
         i < reloc_count;
13218
0
         i++, internal_reloc++, native_reloc += entsize)
13219
0
      {
13220
0
        bool res;
13221
0
        Elf_Internal_Rela rela;
13222
13223
0
        if (entsize == ebd->s->sizeof_rel)
13224
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13225
0
        else /* entsize == ebd->s->sizeof_rela */
13226
0
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13227
13228
        /* The address of an ELF reloc is section relative for an object
13229
     file, and absolute for an executable file or shared library.
13230
     The address of a normal BFD reloc is always section relative,
13231
     and the address of a dynamic reloc is absolute..  */
13232
0
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13233
0
    internal_reloc->address = rela.r_offset;
13234
0
        else
13235
0
    internal_reloc->address = rela.r_offset - sec->vma;
13236
13237
0
        if (r_sym (rela.r_info) == STN_UNDEF)
13238
0
    {
13239
      /* FIXME: This and the error case below mean that we
13240
         have a symbol on relocs that is not elf_symbol_type.  */
13241
0
      internal_reloc->sym_ptr_ptr =
13242
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
13243
0
    }
13244
0
        else if (r_sym (rela.r_info) > symcount)
13245
0
    {
13246
0
      _bfd_error_handler
13247
        /* xgettext:c-format */
13248
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13249
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13250
0
      bfd_set_error (bfd_error_bad_value);
13251
0
      internal_reloc->sym_ptr_ptr =
13252
0
        bfd_abs_section_ptr->symbol_ptr_ptr;
13253
0
      result = false;
13254
0
    }
13255
0
        else
13256
0
    {
13257
0
      asymbol **ps;
13258
13259
0
      ps = symbols + r_sym (rela.r_info) - 1;
13260
0
      internal_reloc->sym_ptr_ptr = ps;
13261
      /* Make sure that this symbol is not removed by strip.  */
13262
0
      (*ps)->flags |= BSF_KEEP;
13263
0
    }
13264
13265
0
        internal_reloc->addend = rela.r_addend;
13266
13267
0
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13268
0
        if (! res || internal_reloc->howto == NULL)
13269
0
    {
13270
#if DEBUG_SECONDARY_RELOCS
13271
      fprintf (stderr,
13272
         "there is no howto associated with reloc %lx\n",
13273
         rela.r_info);
13274
#endif
13275
0
      result = false;
13276
0
    }
13277
0
      }
13278
13279
0
    free (native_relocs);
13280
    /* Store the internal relocs.  */
13281
0
    elf_section_data (relsec)->sec_info = internal_relocs;
13282
0
  }
13283
0
    }
13284
13285
0
  return result;
13286
0
}
13287
13288
/* Set the ELF section header fields of an output secondary reloc section.  */
13289
13290
bool
13291
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13292
              bfd *obfd ATTRIBUTE_UNUSED,
13293
              const Elf_Internal_Shdr *isection,
13294
              Elf_Internal_Shdr *osection)
13295
0
{
13296
0
  asection * isec;
13297
0
  asection * osec;
13298
0
  struct bfd_elf_section_data * esd;
13299
13300
0
  if (isection == NULL)
13301
0
    return false;
13302
13303
0
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13304
0
    return true;
13305
13306
0
  isec = isection->bfd_section;
13307
0
  if (isec == NULL)
13308
0
    return false;
13309
13310
0
  osec = osection->bfd_section;
13311
0
  if (osec == NULL)
13312
0
    return false;
13313
13314
0
  esd = elf_section_data (osec);
13315
0
  BFD_ASSERT (esd->sec_info == NULL);
13316
0
  esd->sec_info = elf_section_data (isec)->sec_info;
13317
0
  osection->sh_type = SHT_RELA;
13318
0
  osection->sh_link = elf_onesymtab (obfd);
13319
0
  if (osection->sh_link == 0)
13320
0
    {
13321
      /* There is no symbol table - we are hosed...  */
13322
0
      _bfd_error_handler
13323
  /* xgettext:c-format */
13324
0
  (_("%pB(%pA): link section cannot be set"
13325
0
     " because the output file does not have a symbol table"),
13326
0
  obfd, osec);
13327
0
      bfd_set_error (bfd_error_bad_value);
13328
0
      return false;
13329
0
    }
13330
13331
  /* Find the output section that corresponds to the isection's
13332
     sh_info link.  */
13333
0
  if (isection->sh_info == 0
13334
0
      || isection->sh_info >= elf_numsections (ibfd))
13335
0
    {
13336
0
      _bfd_error_handler
13337
  /* xgettext:c-format */
13338
0
  (_("%pB(%pA): info section index is invalid"),
13339
0
  obfd, osec);
13340
0
      bfd_set_error (bfd_error_bad_value);
13341
0
      return false;
13342
0
    }
13343
13344
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
13345
13346
0
  if (isection == NULL
13347
0
      || isection->bfd_section == NULL
13348
0
      || isection->bfd_section->output_section == NULL)
13349
0
    {
13350
0
      _bfd_error_handler
13351
  /* xgettext:c-format */
13352
0
  (_("%pB(%pA): info section index cannot be set"
13353
0
     " because the section is not in the output"),
13354
0
  obfd, osec);
13355
0
      bfd_set_error (bfd_error_bad_value);
13356
0
      return false;
13357
0
    }
13358
13359
0
  esd = elf_section_data (isection->bfd_section->output_section);
13360
0
  BFD_ASSERT (esd != NULL);
13361
0
  osection->sh_info = esd->this_idx;
13362
0
  esd->has_secondary_relocs = true;
13363
#if DEBUG_SECONDARY_RELOCS
13364
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13365
     osec->name, osection->sh_link, osection->sh_info);
13366
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13367
     bfd_section_name (isection->bfd_section->output_section));
13368
#endif
13369
13370
0
  return true;
13371
0
}
13372
13373
/* Write out a secondary reloc section.
13374
13375
   FIXME: Currently this function can result in a serious performance penalty
13376
   for files with secondary relocs and lots of sections.  The proper way to
13377
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13378
   relocs together and then to have this function just walk that chain.  */
13379
13380
bool
13381
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13382
0
{
13383
0
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13384
0
  bfd_vma addr_offset;
13385
0
  asection * relsec;
13386
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13387
0
  bool result = true;
13388
13389
0
  if (sec == NULL)
13390
0
    return false;
13391
13392
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13393
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13394
0
    r_info = elf64_r_info;
13395
0
  else
13396
0
#endif
13397
0
    r_info = elf32_r_info;
13398
13399
  /* The address of an ELF reloc is section relative for an object
13400
     file, and absolute for an executable file or shared library.
13401
     The address of a BFD reloc is always section relative.  */
13402
0
  addr_offset = 0;
13403
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13404
0
    addr_offset = sec->vma;
13405
13406
  /* Discover if there are any secondary reloc sections
13407
     associated with SEC.  */
13408
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13409
0
    {
13410
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13411
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13412
13413
0
      if (hdr->sh_type == SHT_RELA
13414
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13415
0
  {
13416
0
    asymbol *    last_sym;
13417
0
    int          last_sym_idx;
13418
0
    size_t       reloc_count;
13419
0
    size_t       idx;
13420
0
    bfd_size_type entsize;
13421
0
    arelent *    src_irel;
13422
0
    bfd_byte *   dst_rela;
13423
13424
0
    if (hdr->contents != NULL)
13425
0
      {
13426
0
        _bfd_error_handler
13427
    /* xgettext:c-format */
13428
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13429
0
     abfd, relsec);
13430
0
        bfd_set_error (bfd_error_bad_value);
13431
0
        result = false;
13432
0
        continue;
13433
0
      }
13434
13435
0
    entsize = hdr->sh_entsize;
13436
0
    if (entsize == 0)
13437
0
      {
13438
0
        _bfd_error_handler
13439
    /* xgettext:c-format */
13440
0
    (_("%pB(%pA): error: secondary reloc section"
13441
0
       " has zero sized entries"),
13442
0
     abfd, relsec);
13443
0
        bfd_set_error (bfd_error_bad_value);
13444
0
        result = false;
13445
0
        continue;
13446
0
      }
13447
0
    else if (entsize != ebd->s->sizeof_rel
13448
0
       && entsize != ebd->s->sizeof_rela)
13449
0
      {
13450
0
        _bfd_error_handler
13451
    /* xgettext:c-format */
13452
0
    (_("%pB(%pA): error: secondary reloc section"
13453
0
       " has non-standard sized entries"),
13454
0
     abfd, relsec);
13455
0
        bfd_set_error (bfd_error_bad_value);
13456
0
        result = false;
13457
0
        continue;
13458
0
      }
13459
13460
0
    reloc_count = hdr->sh_size / entsize;
13461
0
    hdr->sh_size = entsize * reloc_count;
13462
0
    if (reloc_count == 0)
13463
0
      {
13464
0
        _bfd_error_handler
13465
    /* xgettext:c-format */
13466
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13467
0
     abfd, relsec);
13468
0
        bfd_set_error (bfd_error_bad_value);
13469
0
        result = false;
13470
0
        continue;
13471
0
      }
13472
13473
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13474
0
    if (hdr->contents == NULL)
13475
0
      continue;
13476
13477
#if DEBUG_SECONDARY_RELOCS
13478
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13479
       reloc_count, sec->name, relsec->name);
13480
#endif
13481
0
    last_sym = NULL;
13482
0
    last_sym_idx = 0;
13483
0
    dst_rela = hdr->contents;
13484
0
    src_irel = (arelent *) esd->sec_info;
13485
0
    if (src_irel == NULL)
13486
0
      {
13487
0
        _bfd_error_handler
13488
    /* xgettext:c-format */
13489
0
    (_("%pB(%pA): error: internal relocs missing"
13490
0
       " for secondary reloc section"),
13491
0
     abfd, relsec);
13492
0
        bfd_set_error (bfd_error_bad_value);
13493
0
        result = false;
13494
0
        continue;
13495
0
      }
13496
13497
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13498
0
      {
13499
0
        Elf_Internal_Rela src_rela;
13500
0
        arelent *ptr;
13501
0
        asymbol *sym;
13502
0
        int n;
13503
13504
0
        ptr = src_irel + idx;
13505
0
        if (ptr == NULL)
13506
0
    {
13507
0
      _bfd_error_handler
13508
        /* xgettext:c-format */
13509
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
13510
0
         abfd, relsec, idx);
13511
0
      bfd_set_error (bfd_error_bad_value);
13512
0
      result = false;
13513
0
      break;
13514
0
    }
13515
13516
0
        if (ptr->sym_ptr_ptr == NULL)
13517
0
    {
13518
      /* FIXME: Is this an error ? */
13519
0
      n = 0;
13520
0
    }
13521
0
        else
13522
0
    {
13523
0
      sym = *ptr->sym_ptr_ptr;
13524
13525
0
      if (sym == last_sym)
13526
0
        n = last_sym_idx;
13527
0
      else
13528
0
        {
13529
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13530
0
          if (n < 0)
13531
0
      {
13532
0
        _bfd_error_handler
13533
          /* xgettext:c-format */
13534
0
          (_("%pB(%pA): error: secondary reloc %zu"
13535
0
             " references a missing symbol"),
13536
0
           abfd, relsec, idx);
13537
0
        bfd_set_error (bfd_error_bad_value);
13538
0
        result = false;
13539
0
        n = 0;
13540
0
      }
13541
13542
0
          last_sym = sym;
13543
0
          last_sym_idx = n;
13544
0
        }
13545
13546
0
      if (sym->the_bfd != NULL
13547
0
          && sym->the_bfd->xvec != abfd->xvec
13548
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
13549
0
        {
13550
0
          _bfd_error_handler
13551
      /* xgettext:c-format */
13552
0
      (_("%pB(%pA): error: secondary reloc %zu"
13553
0
         " references a deleted symbol"),
13554
0
       abfd, relsec, idx);
13555
0
          bfd_set_error (bfd_error_bad_value);
13556
0
          result = false;
13557
0
          n = 0;
13558
0
        }
13559
0
    }
13560
13561
0
        src_rela.r_offset = ptr->address + addr_offset;
13562
0
        if (ptr->howto == NULL)
13563
0
    {
13564
0
      _bfd_error_handler
13565
        /* xgettext:c-format */
13566
0
        (_("%pB(%pA): error: secondary reloc %zu"
13567
0
           " is of an unknown type"),
13568
0
         abfd, relsec, idx);
13569
0
      bfd_set_error (bfd_error_bad_value);
13570
0
      result = false;
13571
0
      src_rela.r_info = r_info (0, 0);
13572
0
    }
13573
0
        else
13574
0
    src_rela.r_info = r_info (n, ptr->howto->type);
13575
0
        src_rela.r_addend = ptr->addend;
13576
13577
0
        if (entsize == ebd->s->sizeof_rel)
13578
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13579
0
        else /* entsize == ebd->s->sizeof_rela */
13580
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13581
0
      }
13582
0
  }
13583
0
    }
13584
13585
0
  return result;
13586
0
}