Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
250d07de 3 Copyright (C) 1993-2021 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
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.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
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
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3a551c7a 38#include <limits.h>
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
de64ce13 46#include "elf-linux-core.h"
252b5132 47
8bc7f138
L
48#ifdef CORE_HEADER
49#include CORE_HEADER
50#endif
51
217aa764 52static int elf_sort_sections (const void *, const void *);
0a1b45a2
AM
53static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
50b2bdb7 58
252b5132
RH
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
65void
217aa764
AM
66_bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
252b5132 69{
dc810e39
AM
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
77}
78
79/* Swap out a Verdef structure. */
80
81void
217aa764
AM
82_bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
252b5132 85{
dc810e39
AM
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
93}
94
95/* Swap in a Verdaux structure. */
96
97void
217aa764
AM
98_bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
252b5132 101{
dc810e39
AM
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
104}
105
106/* Swap out a Verdaux structure. */
107
108void
217aa764
AM
109_bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
252b5132 112{
dc810e39
AM
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
115}
116
117/* Swap in a Verneed structure. */
118
119void
217aa764
AM
120_bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
252b5132 123{
dc810e39
AM
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
129}
130
131/* Swap out a Verneed structure. */
132
133void
217aa764
AM
134_bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
252b5132 137{
dc810e39
AM
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
143}
144
145/* Swap in a Vernaux structure. */
146
147void
217aa764
AM
148_bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
252b5132 151{
dc810e39
AM
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
157}
158
159/* Swap out a Vernaux structure. */
160
161void
217aa764
AM
162_bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
252b5132 165{
dc810e39
AM
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
171}
172
173/* Swap in a Versym structure. */
174
175void
217aa764
AM
176_bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
252b5132 179{
dc810e39 180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
181}
182
183/* Swap out a Versym structure. */
184
185void
217aa764
AM
186_bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
252b5132 189{
dc810e39 190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
191}
192
193/* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
3a99b017 195
252b5132 196unsigned long
217aa764 197bfd_elf_hash (const char *namearg)
252b5132 198{
3a99b017 199 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
32dfa85d 215 return h & 0xffffffff;
252b5132
RH
216}
217
fdc90cb4
JJ
218/* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221unsigned long
222bfd_elf_gnu_hash (const char *namearg)
223{
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231}
232
0c8d6e5c
AM
233/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
0a1b45a2 235bool
0c8d6e5c 236bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 237 size_t object_size,
4dfe6ac6 238 enum elf_target_id object_id)
252b5132 239{
0ffa91dd
NC
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
0a1b45a2 243 return false;
252b5132 244
0ffa91dd 245 elf_object_id (abfd) = object_id;
c0355132
AM
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
0a1b45a2 250 return false;
c0355132
AM
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
0a1b45a2 254 return true;
252b5132
RH
255}
256
0ffa91dd 257
0a1b45a2 258bool
ae95ffa6 259bfd_elf_make_object (bfd *abfd)
0ffa91dd 260{
ae95ffa6 261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 263 bed->target_id);
0ffa91dd
NC
264}
265
0a1b45a2 266bool
217aa764 267bfd_elf_mkcorefile (bfd *abfd)
252b5132 268{
c044fabd 269 /* I think this can be done just like an object file. */
228e534f 270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
0a1b45a2 271 return false;
228e534f
AM
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
252b5132
RH
274}
275
6d5944fc 276char *
217aa764 277bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
278{
279 Elf_Internal_Shdr **i_shdrp;
f075ee0c 280 bfd_byte *shstrtab = NULL;
dc810e39
AM
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
252b5132
RH
283
284 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
f075ee0c 288 return NULL;
252b5132 289
f075ee0c 290 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
291 if (shstrtab == NULL)
292 {
c044fabd 293 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
3471d59d 299 if (shstrtabsize + 1 <= 1
06614111 300 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
301 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 shstrtabsize)) == NULL)
303 {
3471d59d
CC
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
308 }
309 else
310 shstrtab[shstrtabsize] = '\0';
217aa764 311 i_shdrp[shindex]->contents = shstrtab;
252b5132 312 }
f075ee0c 313 return (char *) shstrtab;
252b5132
RH
314}
315
316char *
217aa764
AM
317bfd_elf_string_from_elf_section (bfd *abfd,
318 unsigned int shindex,
319 unsigned int strindex)
252b5132
RH
320{
321 Elf_Internal_Shdr *hdr;
322
323 if (strindex == 0)
324 return "";
325
74f2e02b
AM
326 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 return NULL;
328
252b5132
RH
329 hdr = elf_elfsections (abfd)[shindex];
330
06614111
NC
331 if (hdr->contents == NULL)
332 {
333 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 {
335 /* PR 17512: file: f057ec89. */
695344c0 336 /* xgettext:c-format */
871b3ab2 337 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 338 " a non-string section (number %d)"),
06614111
NC
339 abfd, shindex);
340 return NULL;
341 }
b1fa9dd6 342
06614111
NC
343 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 return NULL;
345 }
eed5def8
NC
346 else
347 {
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 return NULL;
355 }
252b5132
RH
356
357 if (strindex >= hdr->sh_size)
358 {
1b3a8575 359 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 360 _bfd_error_handler
695344c0 361 /* xgettext:c-format */
2dcf00ce
AM
362 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 364 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 365 ? ".shstrtab"
1b3a8575 366 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 367 return NULL;
252b5132
RH
368 }
369
370 return ((char *) hdr->contents) + strindex;
371}
372
6cdc0ccc
AM
373/* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
380
381Elf_Internal_Sym *
217aa764
AM
382bfd_elf_get_elf_syms (bfd *ibfd,
383 Elf_Internal_Shdr *symtab_hdr,
384 size_t symcount,
385 size_t symoffset,
386 Elf_Internal_Sym *intsym_buf,
387 void *extsym_buf,
388 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
389{
390 Elf_Internal_Shdr *shndx_hdr;
217aa764 391 void *alloc_ext;
df622259 392 const bfd_byte *esym;
6cdc0ccc
AM
393 Elf_External_Sym_Shndx *alloc_extshndx;
394 Elf_External_Sym_Shndx *shndx;
4dd07732 395 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
396 Elf_Internal_Sym *isym;
397 Elf_Internal_Sym *isymend;
9c5bfbb7 398 const struct elf_backend_data *bed;
6cdc0ccc 399 size_t extsym_size;
1f4361a7 400 size_t amt;
6cdc0ccc
AM
401 file_ptr pos;
402
e44a2c9c
AM
403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 abort ();
405
6cdc0ccc
AM
406 if (symcount == 0)
407 return intsym_buf;
408
409 /* Normal syms might have section extension entries. */
410 shndx_hdr = NULL;
6a40cf0c
NC
411 if (elf_symtab_shndx_list (ibfd) != NULL)
412 {
413 elf_section_list * entry;
414 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415
416 /* Find an index section that is linked to this symtab section. */
417 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
418 {
419 /* PR 20063. */
420 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 continue;
422
423 if (sections[entry->hdr.sh_link] == symtab_hdr)
424 {
425 shndx_hdr = & entry->hdr;
426 break;
427 };
428 }
6a40cf0c
NC
429
430 if (shndx_hdr == NULL)
431 {
432 if (symtab_hdr == & elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used to work. */
434 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
435 /* Otherwise we do nothing. The assumption is that
436 the index table will not be needed. */
437 }
438 }
6cdc0ccc
AM
439
440 /* Read the symbols. */
441 alloc_ext = NULL;
442 alloc_extshndx = NULL;
4dd07732 443 alloc_intsym = NULL;
6cdc0ccc
AM
444 bed = get_elf_backend_data (ibfd);
445 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
446 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
447 {
448 bfd_set_error (bfd_error_file_too_big);
449 intsym_buf = NULL;
450 goto out;
451 }
6cdc0ccc
AM
452 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
453 if (extsym_buf == NULL)
454 {
1f4361a7 455 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
456 extsym_buf = alloc_ext;
457 }
458 if (extsym_buf == NULL
459 || bfd_seek (ibfd, pos, SEEK_SET) != 0
460 || bfd_bread (extsym_buf, amt, ibfd) != amt)
461 {
462 intsym_buf = NULL;
463 goto out;
464 }
465
466 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
467 extshndx_buf = NULL;
468 else
469 {
1f4361a7
AM
470 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
471 {
472 bfd_set_error (bfd_error_file_too_big);
473 intsym_buf = NULL;
474 goto out;
475 }
6cdc0ccc
AM
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (extshndx_buf == NULL)
478 {
1f4361a7 479 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
480 extshndx_buf = alloc_extshndx;
481 }
482 if (extshndx_buf == NULL
483 || bfd_seek (ibfd, pos, SEEK_SET) != 0
484 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
485 {
486 intsym_buf = NULL;
487 goto out;
488 }
489 }
490
491 if (intsym_buf == NULL)
492 {
1f4361a7
AM
493 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
494 {
495 bfd_set_error (bfd_error_file_too_big);
496 goto out;
497 }
498 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 499 intsym_buf = alloc_intsym;
6cdc0ccc
AM
500 if (intsym_buf == NULL)
501 goto out;
502 }
503
504 /* Convert the symbols to internal form. */
505 isymend = intsym_buf + symcount;
a50b1753 506 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 507 shndx = extshndx_buf;
6cdc0ccc
AM
508 isym < isymend;
509 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
510 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
511 {
512 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 513 /* xgettext:c-format */
871b3ab2 514 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 515 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 516 ibfd, (unsigned long) symoffset);
c9594989 517 free (alloc_intsym);
8384fb8f
AM
518 intsym_buf = NULL;
519 goto out;
520 }
6cdc0ccc
AM
521
522 out:
c9594989
AM
523 free (alloc_ext);
524 free (alloc_extshndx);
6cdc0ccc
AM
525
526 return intsym_buf;
527}
528
5cab59f6
AM
529/* Look up a symbol name. */
530const char *
be8dd2ca
AM
531bfd_elf_sym_name (bfd *abfd,
532 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
533 Elf_Internal_Sym *isym,
534 asection *sym_sec)
5cab59f6 535{
26c61ae5 536 const char *name;
5cab59f6 537 unsigned int iname = isym->st_name;
be8dd2ca 538 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 539
138f35cc
JJ
540 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
541 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 542 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
543 {
544 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
545 shindex = elf_elfheader (abfd)->e_shstrndx;
546 }
547
26c61ae5
L
548 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 if (name == NULL)
550 name = "(null)";
551 else if (sym_sec && *name == '\0')
fd361982 552 name = bfd_section_name (sym_sec);
26c61ae5
L
553
554 return name;
5cab59f6
AM
555}
556
dbb410c3
AM
557/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
558 sections. The first element is the flags, the rest are section
559 pointers. */
560
561typedef union elf_internal_group {
562 Elf_Internal_Shdr *shdr;
563 unsigned int flags;
564} Elf_Internal_Group;
565
b885599b
AM
566/* Return the name of the group signature symbol. Why isn't the
567 signature just a string? */
568
569static const char *
217aa764 570group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 571{
9dce4196 572 Elf_Internal_Shdr *hdr;
9dce4196
AM
573 unsigned char esym[sizeof (Elf64_External_Sym)];
574 Elf_External_Sym_Shndx eshndx;
575 Elf_Internal_Sym isym;
b885599b 576
13792e9d
L
577 /* First we need to ensure the symbol table is available. Make sure
578 that it is a symbol table section. */
4fbb74a6
AM
579 if (ghdr->sh_link >= elf_numsections (abfd))
580 return NULL;
13792e9d
L
581 hdr = elf_elfsections (abfd) [ghdr->sh_link];
582 if (hdr->sh_type != SHT_SYMTAB
583 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
584 return NULL;
585
9dce4196
AM
586 /* Go read the symbol. */
587 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
588 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
589 &isym, esym, &eshndx) == NULL)
b885599b 590 return NULL;
9dce4196 591
26c61ae5 592 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
593}
594
dbb410c3
AM
595/* Set next_in_group list pointer, and group name for NEWSECT. */
596
0a1b45a2 597static bool
217aa764 598setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
599{
600 unsigned int num_group = elf_tdata (abfd)->num_group;
601
602 /* If num_group is zero, read in all SHT_GROUP sections. The count
603 is set to -1 if there are no SHT_GROUP sections. */
604 if (num_group == 0)
605 {
606 unsigned int i, shnum;
607
608 /* First count the number of groups. If we have a SHT_GROUP
609 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 610 shnum = elf_numsections (abfd);
dbb410c3 611 num_group = 0;
08a40648 612
44534af3 613#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 614 ( (shdr)->sh_type == SHT_GROUP \
44534af3 615 && (shdr)->sh_size >= minsize \
1783205a
NC
616 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
617 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 618
dbb410c3
AM
619 for (i = 0; i < shnum; i++)
620 {
621 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 622
44534af3 623 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
624 num_group += 1;
625 }
626
627 if (num_group == 0)
20dbb49d
L
628 {
629 num_group = (unsigned) -1;
630 elf_tdata (abfd)->num_group = num_group;
ce497010 631 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
632 }
633 else
dbb410c3
AM
634 {
635 /* We keep a list of elf section headers for group sections,
636 so we can find them quickly. */
1f4361a7 637 size_t amt;
d0fb9a8d 638
20dbb49d 639 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
640 amt = num_group * sizeof (Elf_Internal_Shdr *);
641 elf_tdata (abfd)->group_sect_ptr
642 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 643 if (elf_tdata (abfd)->group_sect_ptr == NULL)
0a1b45a2 644 return false;
dbb410c3 645 num_group = 0;
ce497010 646
dbb410c3
AM
647 for (i = 0; i < shnum; i++)
648 {
649 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 650
44534af3 651 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 652 {
973ffd63 653 unsigned char *src;
dbb410c3
AM
654 Elf_Internal_Group *dest;
655
07d6d2b8
AM
656 /* Make sure the group section has a BFD section
657 attached to it. */
658 if (!bfd_section_from_shdr (abfd, i))
0a1b45a2 659 return false;
07d6d2b8 660
dbb410c3
AM
661 /* Add to list of sections. */
662 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
663 num_group += 1;
664
665 /* Read the raw contents. */
1f4361a7
AM
666 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
667 shdr->contents = NULL;
668 if (_bfd_mul_overflow (shdr->sh_size,
669 sizeof (*dest) / 4, &amt)
1f4361a7 670 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
671 || !(shdr->contents
672 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
673 {
674 _bfd_error_handler
695344c0 675 /* xgettext:c-format */
871b3ab2 676 (_("%pB: invalid size field in group section"
2dcf00ce
AM
677 " header: %#" PRIx64 ""),
678 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
679 bfd_set_error (bfd_error_bad_value);
680 -- num_group;
493a3386
NC
681 continue;
682 }
708d7d0d 683
dbb410c3
AM
684 /* Translate raw contents, a flag word followed by an
685 array of elf section indices all in target byte order,
686 to the flag word followed by an array of elf section
687 pointers. */
688 src = shdr->contents + shdr->sh_size;
689 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 690
dbb410c3
AM
691 while (1)
692 {
693 unsigned int idx;
694
695 src -= 4;
696 --dest;
697 idx = H_GET_32 (abfd, src);
698 if (src == shdr->contents)
699 {
327301a4 700 dest->shdr = NULL;
dbb410c3 701 dest->flags = idx;
b885599b
AM
702 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
703 shdr->bfd_section->flags
704 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
705 break;
706 }
4bba0fb1 707 if (idx < shnum)
bae363f1
L
708 {
709 dest->shdr = elf_elfsections (abfd)[idx];
710 /* PR binutils/23199: All sections in a
711 section group should be marked with
712 SHF_GROUP. But some tools generate
713 broken objects without SHF_GROUP. Fix
714 them up here. */
715 dest->shdr->sh_flags |= SHF_GROUP;
716 }
4bba0fb1
AM
717 if (idx >= shnum
718 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 719 {
4eca0228 720 _bfd_error_handler
4bba0fb1
AM
721 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
722 abfd, i);
723 dest->shdr = NULL;
dbb410c3 724 }
dbb410c3
AM
725 }
726 }
727 }
493a3386
NC
728
729 /* PR 17510: Corrupt binaries might contain invalid groups. */
730 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
731 {
732 elf_tdata (abfd)->num_group = num_group;
733
734 /* If all groups are invalid then fail. */
735 if (num_group == 0)
736 {
737 elf_tdata (abfd)->group_sect_ptr = NULL;
738 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 739 _bfd_error_handler
871b3ab2 740 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
741 bfd_set_error (bfd_error_bad_value);
742 }
743 }
dbb410c3
AM
744 }
745 }
746
747 if (num_group != (unsigned) -1)
748 {
564e11c9
JW
749 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
750 unsigned int j;
dbb410c3 751
564e11c9 752 for (j = 0; j < num_group; j++)
dbb410c3 753 {
564e11c9
JW
754 /* Begin search from previous found group. */
755 unsigned i = (j + search_offset) % num_group;
756
dbb410c3 757 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 758 Elf_Internal_Group *idx;
0c54f692 759 bfd_size_type n_elt;
ce497010
NC
760
761 if (shdr == NULL)
762 continue;
763
764 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
765 if (idx == NULL || shdr->sh_size < 4)
766 {
767 /* See PR 21957 for a reproducer. */
768 /* xgettext:c-format */
871b3ab2 769 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
770 abfd, shdr->bfd_section);
771 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
772 bfd_set_error (bfd_error_bad_value);
0a1b45a2 773 return false;
0c54f692 774 }
ce497010 775 n_elt = shdr->sh_size / 4;
dbb410c3
AM
776
777 /* Look through this group's sections to see if current
778 section is a member. */
779 while (--n_elt != 0)
780 if ((++idx)->shdr == hdr)
781 {
e0e8c97f 782 asection *s = NULL;
dbb410c3
AM
783
784 /* We are a member of this group. Go looking through
785 other members to see if any others are linked via
786 next_in_group. */
787 idx = (Elf_Internal_Group *) shdr->contents;
788 n_elt = shdr->sh_size / 4;
789 while (--n_elt != 0)
4bba0fb1
AM
790 if ((++idx)->shdr != NULL
791 && (s = idx->shdr->bfd_section) != NULL
945906ff 792 && elf_next_in_group (s) != NULL)
dbb410c3
AM
793 break;
794 if (n_elt != 0)
795 {
dbb410c3
AM
796 /* Snarf the group name from other member, and
797 insert current section in circular list. */
945906ff
AM
798 elf_group_name (newsect) = elf_group_name (s);
799 elf_next_in_group (newsect) = elf_next_in_group (s);
800 elf_next_in_group (s) = newsect;
dbb410c3
AM
801 }
802 else
803 {
dbb410c3
AM
804 const char *gname;
805
b885599b
AM
806 gname = group_signature (abfd, shdr);
807 if (gname == NULL)
0a1b45a2 808 return false;
945906ff 809 elf_group_name (newsect) = gname;
dbb410c3
AM
810
811 /* Start a circular list with one element. */
945906ff 812 elf_next_in_group (newsect) = newsect;
dbb410c3 813 }
b885599b 814
9dce4196
AM
815 /* If the group section has been created, point to the
816 new member. */
dbb410c3 817 if (shdr->bfd_section != NULL)
945906ff 818 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 819
564e11c9
JW
820 elf_tdata (abfd)->group_search_offset = i;
821 j = num_group - 1;
dbb410c3
AM
822 break;
823 }
824 }
825 }
826
945906ff 827 if (elf_group_name (newsect) == NULL)
dbb410c3 828 {
695344c0 829 /* xgettext:c-format */
871b3ab2 830 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 831 abfd, newsect);
0a1b45a2 832 return false;
dbb410c3 833 }
0a1b45a2 834 return true;
dbb410c3
AM
835}
836
0a1b45a2 837bool
dd863624 838_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
839{
840 unsigned int i;
841 unsigned int num_group = elf_tdata (abfd)->num_group;
0a1b45a2 842 bool result = true;
dd863624
L
843 asection *s;
844
845 /* Process SHF_LINK_ORDER. */
846 for (s = abfd->sections; s != NULL; s = s->next)
847 {
848 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
849 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
850 {
851 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
852 /* An sh_link value of 0 is now allowed. It indicates that linked
853 to section has already been discarded, but that the current
854 section has been retained for some other reason. This linking
855 section is still a candidate for later garbage collection
856 however. */
dd863624
L
857 if (elfsec == 0)
858 {
b71702f1 859 elf_linked_to_section (s) = NULL;
dd863624
L
860 }
861 else
862 {
91d6fa6a 863 asection *linksec = NULL;
25bbc984 864
4fbb74a6
AM
865 if (elfsec < elf_numsections (abfd))
866 {
867 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 868 linksec = this_hdr->bfd_section;
4fbb74a6 869 }
25bbc984
L
870
871 /* PR 1991, 2008:
872 Some strip/objcopy may leave an incorrect value in
873 sh_link. We don't want to proceed. */
91d6fa6a 874 if (linksec == NULL)
25bbc984 875 {
4eca0228 876 _bfd_error_handler
695344c0 877 /* xgettext:c-format */
871b3ab2 878 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 879 s->owner, elfsec, s);
0a1b45a2 880 result = false;
25bbc984
L
881 }
882
91d6fa6a 883 elf_linked_to_section (s) = linksec;
dd863624
L
884 }
885 }
53720c49
AM
886 else if (this_hdr->sh_type == SHT_GROUP
887 && elf_next_in_group (s) == NULL)
888 {
4eca0228 889 _bfd_error_handler
695344c0 890 /* xgettext:c-format */
871b3ab2 891 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49 892 abfd, elf_section_data (s)->this_idx);
0a1b45a2 893 result = false;
53720c49 894 }
dd863624 895 }
3d7f7666 896
dd863624 897 /* Process section groups. */
3d7f7666
L
898 if (num_group == (unsigned) -1)
899 return result;
900
901 for (i = 0; i < num_group; i++)
902 {
903 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
904 Elf_Internal_Group *idx;
905 unsigned int n_elt;
3d7f7666 906
4b0e8a5f
NC
907 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
908 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
909 {
4eca0228 910 _bfd_error_handler
695344c0 911 /* xgettext:c-format */
871b3ab2 912 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f 913 abfd, i);
0a1b45a2 914 result = false;
4b0e8a5f
NC
915 continue;
916 }
917
918 idx = (Elf_Internal_Group *) shdr->contents;
919 n_elt = shdr->sh_size / 4;
1b786873 920
3d7f7666 921 while (--n_elt != 0)
24d3e51b
NC
922 {
923 ++ idx;
924
925 if (idx->shdr == NULL)
926 continue;
927 else if (idx->shdr->bfd_section)
928 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
929 else if (idx->shdr->sh_type != SHT_RELA
930 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
931 {
932 /* There are some unknown sections in the group. */
933 _bfd_error_handler
934 /* xgettext:c-format */
871b3ab2 935 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
936 abfd,
937 idx->shdr->sh_type,
938 bfd_elf_string_from_elf_section (abfd,
939 (elf_elfheader (abfd)
940 ->e_shstrndx),
941 idx->shdr->sh_name),
942 shdr->bfd_section);
0a1b45a2 943 result = false;
24d3e51b
NC
944 }
945 }
3d7f7666 946 }
24d3e51b 947
3d7f7666
L
948 return result;
949}
950
0a1b45a2 951bool
72adc230
AM
952bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
953{
954 return elf_next_in_group (sec) != NULL;
955}
956
cb7f4b29
AM
957const char *
958bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959{
960 if (elf_sec_group (sec) != NULL)
961 return elf_group_name (sec);
962 return NULL;
963}
964
f6fe1ccd
L
965static char *
966convert_debug_to_zdebug (bfd *abfd, const char *name)
967{
968 unsigned int len = strlen (name);
969 char *new_name = bfd_alloc (abfd, len + 2);
970 if (new_name == NULL)
971 return NULL;
972 new_name[0] = '.';
973 new_name[1] = 'z';
974 memcpy (new_name + 2, name + 1, len);
975 return new_name;
976}
977
978static char *
979convert_zdebug_to_debug (bfd *abfd, const char *name)
980{
981 unsigned int len = strlen (name);
982 char *new_name = bfd_alloc (abfd, len);
983 if (new_name == NULL)
984 return NULL;
985 new_name[0] = '.';
986 memcpy (new_name + 1, name + 2, len - 1);
987 return new_name;
988}
989
cc5277b1
ML
990/* This a copy of lto_section defined in GCC (lto-streamer.h). */
991
992struct lto_section
993{
994 int16_t major_version;
995 int16_t minor_version;
996 unsigned char slim_object;
997
998 /* Flags is a private field that is not defined publicly. */
999 uint16_t flags;
1000};
1001
252b5132
RH
1002/* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1004
0a1b45a2 1005bool
217aa764
AM
1006_bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
6dc132d9
L
1008 const char *name,
1009 int shindex)
252b5132
RH
1010{
1011 asection *newsect;
1012 flagword flags;
9c5bfbb7 1013 const struct elf_backend_data *bed;
502794d4 1014 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1015
1016 if (hdr->bfd_section != NULL)
0a1b45a2 1017 return true;
252b5132
RH
1018
1019 newsect = bfd_make_section_anyway (abfd, name);
1020 if (newsect == NULL)
0a1b45a2 1021 return false;
252b5132 1022
1829f4b2
AM
1023 hdr->bfd_section = newsect;
1024 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1025 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1026
2f89ff8d
L
1027 /* Always use the real type/flags. */
1028 elf_section_type (newsect) = hdr->sh_type;
1029 elf_section_flags (newsect) = hdr->sh_flags;
1030
252b5132
RH
1031 newsect->filepos = hdr->sh_offset;
1032
252b5132
RH
1033 flags = SEC_NO_FLAGS;
1034 if (hdr->sh_type != SHT_NOBITS)
1035 flags |= SEC_HAS_CONTENTS;
dbb410c3 1036 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1037 flags |= SEC_GROUP;
252b5132
RH
1038 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1039 {
1040 flags |= SEC_ALLOC;
1041 if (hdr->sh_type != SHT_NOBITS)
1042 flags |= SEC_LOAD;
1043 }
1044 if ((hdr->sh_flags & SHF_WRITE) == 0)
1045 flags |= SEC_READONLY;
1046 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1047 flags |= SEC_CODE;
1048 else if ((flags & SEC_LOAD) != 0)
1049 flags |= SEC_DATA;
f5fa8ca2
JJ
1050 if ((hdr->sh_flags & SHF_MERGE) != 0)
1051 {
1052 flags |= SEC_MERGE;
1053 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1054 }
84865015
NC
1055 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1056 flags |= SEC_STRINGS;
dbb410c3
AM
1057 if (hdr->sh_flags & SHF_GROUP)
1058 if (!setup_group (abfd, hdr, newsect))
0a1b45a2 1059 return false;
13ae64f3
JJ
1060 if ((hdr->sh_flags & SHF_TLS) != 0)
1061 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1062 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1063 flags |= SEC_EXCLUDE;
252b5132 1064
df3a023b
AM
1065 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1066 {
1067 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1068 but binutils as of 2019-07-23 did not set the EI_OSABI header
1069 byte. */
df3a023b
AM
1070 case ELFOSABI_GNU:
1071 case ELFOSABI_FREEBSD:
99fabbc9
JL
1072 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1073 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1074 /* Fall through */
1075 case ELFOSABI_NONE:
df3a023b
AM
1076 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1077 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1078 break;
1079 }
1080
3d2b39cf 1081 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1082 {
3d2b39cf
L
1083 /* The debugging sections appear to be recognized only by name,
1084 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1085 if (name [0] == '.')
1086 {
3f3328b8
ML
1087 if (startswith (name, ".debug")
1088 || startswith (name, ".gnu.debuglto_.debug_")
1089 || startswith (name, ".gnu.linkonce.wi.")
1090 || startswith (name, ".zdebug"))
bb294208 1091 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
3f3328b8
ML
1092 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1093 || startswith (name, ".note.gnu"))
502794d4
CE
1094 {
1095 flags |= SEC_ELF_OCTETS;
1096 opb = 1;
1097 }
3f3328b8
ML
1098 else if (startswith (name, ".line")
1099 || startswith (name, ".stab")
bb294208 1100 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1101 flags |= SEC_DEBUGGING;
1102 }
1103 }
252b5132 1104
502794d4
CE
1105 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1106 || !bfd_set_section_size (newsect, hdr->sh_size)
1107 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
0a1b45a2 1108 return false;
502794d4 1109
252b5132
RH
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
08dedd66 1116 if (startswith (name, ".gnu.linkonce")
b885599b 1117 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1118 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1119
8c803a2d 1120 if (!bfd_set_section_flags (newsect, flags))
0a1b45a2 1121 return false;
8c803a2d 1122
fa152c49
JW
1123 bed = get_elf_backend_data (abfd);
1124 if (bed->elf_backend_section_flags)
8c803a2d 1125 if (!bed->elf_backend_section_flags (hdr))
0a1b45a2 1126 return false;
fa152c49 1127
718175fa
JK
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr->sh_type == SHT_NOTE)
1132 {
baea7ef1 1133 bfd_byte *contents;
718175fa 1134
baea7ef1 1135 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
0a1b45a2 1136 return false;
718175fa 1137
276da9b3
L
1138 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1139 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1140 free (contents);
1141 }
1142
8c803a2d 1143 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1144 {
1145 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1146 unsigned int i, nload;
1147
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr = elf_tdata (abfd)->phdr;
1153 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1154 if (phdr->p_paddr != 0)
1155 break;
1156 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1157 ++nload;
1158 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
0a1b45a2 1159 return true;
252b5132 1160
252b5132
RH
1161 phdr = elf_tdata (abfd)->phdr;
1162 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1163 {
86b2281f
AM
1164 if (((phdr->p_type == PT_LOAD
1165 && (hdr->sh_flags & SHF_TLS) == 0)
1166 || phdr->p_type == PT_TLS)
9a83a553 1167 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1168 {
8c803a2d 1169 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1170 newsect->lma = (phdr->p_paddr
502794d4 1171 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1172 else
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect->lma = (phdr->p_paddr
502794d4 1181 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1182
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr->sh_addr >= phdr->p_vaddr
1188 && (hdr->sh_addr + hdr->sh_size
1189 <= phdr->p_vaddr + phdr->p_memsz))
1190 break;
252b5132
RH
1191 }
1192 }
1193 }
1194
4a114e3e
L
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
8c803a2d 1197 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1198 && ((name[1] == 'd' && name[6] == '_')
1199 || (name[1] == 'z' && name[7] == '_')))
1200 {
1201 enum { nothing, compress, decompress } action = nothing;
151411f8 1202 int compression_header_size;
dab394de 1203 bfd_size_type uncompressed_size;
4207142d 1204 unsigned int uncompressed_align_power;
0a1b45a2 1205 bool compressed
151411f8 1206 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1207 &compression_header_size,
4207142d
MW
1208 &uncompressed_size,
1209 &uncompressed_align_power);
151411f8 1210 if (compressed)
4a114e3e
L
1211 {
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd->flags & BFD_DECOMPRESS))
1214 action = decompress;
1215 }
151411f8
L
1216
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action == nothing)
4a114e3e 1220 {
151411f8
L
1221 if (newsect->size != 0
1222 && (abfd->flags & BFD_COMPRESS)
1223 && compression_header_size >= 0
dab394de 1224 && uncompressed_size > 0
151411f8
L
1225 && (!compressed
1226 || ((compression_header_size > 0)
1227 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1228 action = compress;
151411f8 1229 else
0a1b45a2 1230 return true;
4a114e3e
L
1231 }
1232
151411f8 1233 if (action == compress)
4a114e3e 1234 {
4a114e3e
L
1235 if (!bfd_init_section_compress_status (abfd, newsect))
1236 {
4eca0228 1237 _bfd_error_handler
695344c0 1238 /* xgettext:c-format */
871b3ab2 1239 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e 1240 abfd, name);
0a1b45a2 1241 return false;
4a114e3e 1242 }
151411f8
L
1243 }
1244 else
1245 {
4a114e3e
L
1246 if (!bfd_init_section_decompress_status (abfd, newsect))
1247 {
4eca0228 1248 _bfd_error_handler
695344c0 1249 /* xgettext:c-format */
871b3ab2 1250 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e 1251 abfd, name);
0a1b45a2 1252 return false;
4a114e3e 1253 }
151411f8
L
1254 }
1255
f6fe1ccd 1256 if (abfd->is_linker_input)
151411f8 1257 {
f6fe1ccd
L
1258 if (name[1] == 'z'
1259 && (action == decompress
1260 || (action == compress
1261 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1262 {
f6fe1ccd
L
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1265 section. */
1266 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8 1267 if (new_name == NULL)
0a1b45a2 1268 return false;
fd361982 1269 bfd_rename_section (newsect, new_name);
151411f8 1270 }
4a114e3e 1271 }
f6fe1ccd
L
1272 else
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1276 }
1277
cc5277b1
ML
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1279 section. */
3f3328b8 1280 if (startswith (name, ".gnu.lto_.lto."))
cc5277b1
ML
1281 {
1282 struct lto_section lsection;
1283 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1284 sizeof (struct lto_section)))
1285 abfd->lto_slim_object = lsection.slim_object;
1286 }
1287
0a1b45a2 1288 return true;
252b5132
RH
1289}
1290
84865015
NC
1291const char *const bfd_elf_section_type_names[] =
1292{
252b5132
RH
1293 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1294 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1295 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1296};
1297
1049f94e 1298/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1299 output, and the reloc is against an external symbol, and nothing
1300 has given us any additional addend, the resulting reloc will also
1301 be against the same symbol. In such a case, we don't want to
1302 change anything about the way the reloc is handled, since it will
1303 all be done at final link time. Rather than put special case code
1304 into bfd_perform_relocation, all the reloc types use this howto
2dfa8341 1305 function, or should call this function for relocatable output. */
252b5132 1306
252b5132 1307bfd_reloc_status_type
217aa764
AM
1308bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1309 arelent *reloc_entry,
1310 asymbol *symbol,
1311 void *data ATTRIBUTE_UNUSED,
1312 asection *input_section,
1313 bfd *output_bfd,
1314 char **error_message ATTRIBUTE_UNUSED)
1315{
1316 if (output_bfd != NULL
252b5132
RH
1317 && (symbol->flags & BSF_SECTION_SYM) == 0
1318 && (! reloc_entry->howto->partial_inplace
1319 || reloc_entry->addend == 0))
1320 {
1321 reloc_entry->address += input_section->output_offset;
1322 return bfd_reloc_ok;
1323 }
1324
2dfa8341
AM
1325 /* In some cases the relocation should be treated as output section
1326 relative, as when linking ELF DWARF into PE COFF. Many ELF
1327 targets lack section relative relocations and instead use
1328 ordinary absolute relocations for references between DWARF
1329 sections. That is arguably a bug in those targets but it happens
1330 to work for the usual case of linking to non-loaded ELF debug
1331 sections with VMAs forced to zero. PE COFF on the other hand
1332 doesn't allow a section VMA of zero. */
1333 if (output_bfd == NULL
1334 && !reloc_entry->howto->pc_relative
1335 && (symbol->section->flags & SEC_DEBUGGING) != 0
1336 && (input_section->flags & SEC_DEBUGGING) != 0)
1337 reloc_entry->addend -= symbol->section->output_section->vma;
1338
252b5132
RH
1339 return bfd_reloc_continue;
1340}
1341\f
84865015
NC
1342/* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1345
0a1b45a2 1346static bool
5522f910
NC
1347section_match (const Elf_Internal_Shdr * a,
1348 const Elf_Internal_Shdr * b)
84865015 1349{
ac85e67c
AM
1350 if (a->sh_type != b->sh_type
1351 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1352 || a->sh_addralign != b->sh_addralign
1353 || a->sh_entsize != b->sh_entsize)
0a1b45a2 1354 return false;
ac85e67c
AM
1355 if (a->sh_type == SHT_SYMTAB
1356 || a->sh_type == SHT_STRTAB)
0a1b45a2 1357 return true;
ac85e67c 1358 return a->sh_size == b->sh_size;
84865015
NC
1359}
1360
1361/* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1365
1366static unsigned int
5cc4ca83
ST
1367find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1368 const unsigned int hint)
84865015
NC
1369{
1370 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1371 unsigned int i;
1372
a55c9876
NC
1373 BFD_ASSERT (iheader != NULL);
1374
1375 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1376 if (hint < elf_numsections (obfd)
1377 && oheaders[hint] != NULL
a55c9876 1378 && section_match (oheaders[hint], iheader))
84865015
NC
1379 return hint;
1380
1381 for (i = 1; i < elf_numsections (obfd); i++)
1382 {
1383 Elf_Internal_Shdr * oheader = oheaders[i];
1384
a55c9876
NC
1385 if (oheader == NULL)
1386 continue;
84865015
NC
1387 if (section_match (oheader, iheader))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1390 return i;
1391 }
1392
1393 return SHN_UNDEF;
1394}
1395
5522f910
NC
1396/* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1399
0a1b45a2 1400static bool
5522f910
NC
1401copy_special_section_fields (const bfd *ibfd,
1402 bfd *obfd,
1403 const Elf_Internal_Shdr *iheader,
1404 Elf_Internal_Shdr *oheader,
1405 const unsigned int secnum)
1406{
1407 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1408 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
0a1b45a2 1409 bool changed = false;
5522f910
NC
1410 unsigned int sh_link;
1411
1412 if (oheader->sh_type == SHT_NOBITS)
1413 {
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1418
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader->sh_link == 0)
1431 oheader->sh_link = iheader->sh_link;
1432 if (oheader->sh_info == 0)
1433 oheader->sh_info = iheader->sh_info;
0a1b45a2 1434 return true;
5522f910
NC
1435 }
1436
1437 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1438 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1439 iheader, oheader))
0a1b45a2 1440 return true;
5522f910
NC
1441
1442 /* We have an iheader which might match oheader, and which has non-zero
1443 sh_info and/or sh_link fields. Attempt to follow those links and find
1444 the section in the output bfd which corresponds to the linked section
1445 in the input bfd. */
1446 if (iheader->sh_link != SHN_UNDEF)
1447 {
4f3ca05b
NC
1448 /* See PR 20931 for a reproducer. */
1449 if (iheader->sh_link >= elf_numsections (ibfd))
1450 {
76cfced5 1451 _bfd_error_handler
4f3ca05b 1452 /* xgettext:c-format */
9793eb77 1453 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b 1454 ibfd, iheader->sh_link, secnum);
0a1b45a2 1455 return false;
4f3ca05b
NC
1456 }
1457
5522f910
NC
1458 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1459 if (sh_link != SHN_UNDEF)
1460 {
1461 oheader->sh_link = sh_link;
0a1b45a2 1462 changed = true;
5522f910
NC
1463 }
1464 else
1465 /* FIXME: Should we install iheader->sh_link
1466 if we could not find a match ? */
76cfced5 1467 _bfd_error_handler
695344c0 1468 /* xgettext:c-format */
9793eb77 1469 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1470 }
1471
1472 if (iheader->sh_info)
1473 {
1474 /* The sh_info field can hold arbitrary information, but if the
1475 SHF_LINK_INFO flag is set then it should be interpreted as a
1476 section index. */
1477 if (iheader->sh_flags & SHF_INFO_LINK)
1478 {
1479 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1480 iheader->sh_info);
1481 if (sh_link != SHN_UNDEF)
1482 oheader->sh_flags |= SHF_INFO_LINK;
1483 }
1484 else
1485 /* No idea what it means - just copy it. */
1486 sh_link = iheader->sh_info;
1487
1488 if (sh_link != SHN_UNDEF)
1489 {
1490 oheader->sh_info = sh_link;
0a1b45a2 1491 changed = true;
5522f910
NC
1492 }
1493 else
76cfced5 1494 _bfd_error_handler
695344c0 1495 /* xgettext:c-format */
9793eb77 1496 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1497 }
1498
1499 return changed;
1500}
07d6d2b8 1501
0ac4564e
L
1502/* Copy the program header and other data from one object module to
1503 another. */
252b5132 1504
0a1b45a2 1505bool
217aa764 1506_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1507{
5522f910
NC
1508 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1509 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1510 const struct elf_backend_data *bed;
84865015
NC
1511 unsigned int i;
1512
2d502050 1513 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1514 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 1515 return true;
2d502050 1516
57b828ef
L
1517 if (!elf_flags_init (obfd))
1518 {
1519 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
0a1b45a2 1520 elf_flags_init (obfd) = true;
57b828ef 1521 }
2d502050 1522
0ac4564e 1523 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1524
1525 /* Also copy the EI_OSABI field. */
1526 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1527 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1528
5522f910
NC
1529 /* If set, copy the EI_ABIVERSION field. */
1530 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1531 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1532 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1533
104d59d1
JM
1534 /* Copy object attributes. */
1535 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1536
84865015 1537 if (iheaders == NULL || oheaders == NULL)
0a1b45a2 1538 return true;
63b9bbb7 1539
5522f910
NC
1540 bed = get_elf_backend_data (obfd);
1541
1542 /* Possibly copy other fields in the section header. */
84865015 1543 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1544 {
84865015
NC
1545 unsigned int j;
1546 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1547
5522f910
NC
1548 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1549 because of a special case need for generating separate debug info
1550 files. See below for more details. */
84865015
NC
1551 if (oheader == NULL
1552 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1553 && oheader->sh_type < SHT_LOOS))
1554 continue;
1555
1556 /* Ignore empty sections, and sections whose
1557 fields have already been initialised. */
1558 if (oheader->sh_size == 0
84865015
NC
1559 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1560 continue;
63b9bbb7 1561
84865015 1562 /* Scan for the matching section in the input bfd.
5522f910
NC
1563 First we try for a direct mapping between the input and output sections. */
1564 for (j = 1; j < elf_numsections (ibfd); j++)
1565 {
1566 const Elf_Internal_Shdr * iheader = iheaders[j];
1567
1568 if (iheader == NULL)
1569 continue;
1570
1571 if (oheader->bfd_section != NULL
1572 && iheader->bfd_section != NULL
1573 && iheader->bfd_section->output_section != NULL
1574 && iheader->bfd_section->output_section == oheader->bfd_section)
1575 {
1576 /* We have found a connection from the input section to the
1577 output section. Attempt to copy the header fields. If
1578 this fails then do not try any further sections - there
1579 should only be a one-to-one mapping between input and output. */
1580 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1581 j = elf_numsections (ibfd);
1582 break;
1583 }
1584 }
1585
1586 if (j < elf_numsections (ibfd))
1587 continue;
1588
1589 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1590 Unfortunately we cannot compare names as the output string table
1591 is empty, so instead we check size, address and type. */
1592 for (j = 1; j < elf_numsections (ibfd); j++)
1593 {
5522f910 1594 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1595
5522f910
NC
1596 if (iheader == NULL)
1597 continue;
1598
1599 /* Try matching fields in the input section's header.
1600 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1601 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1602 input type. */
1603 if ((oheader->sh_type == SHT_NOBITS
1604 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1605 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1606 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1607 && iheader->sh_addralign == oheader->sh_addralign
1608 && iheader->sh_entsize == oheader->sh_entsize
1609 && iheader->sh_size == oheader->sh_size
1610 && iheader->sh_addr == oheader->sh_addr
1611 && (iheader->sh_info != oheader->sh_info
1612 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1613 {
5522f910
NC
1614 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1615 break;
63b9bbb7
NC
1616 }
1617 }
5522f910
NC
1618
1619 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1620 {
1621 /* Final attempt. Call the backend copy function
1622 with a NULL input section. */
a859124d
AM
1623 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1624 NULL, oheader);
5522f910 1625 }
63b9bbb7
NC
1626 }
1627
0a1b45a2 1628 return true;
2d502050
L
1629}
1630
cedc298e
L
1631static const char *
1632get_segment_type (unsigned int p_type)
1633{
1634 const char *pt;
1635 switch (p_type)
1636 {
1637 case PT_NULL: pt = "NULL"; break;
1638 case PT_LOAD: pt = "LOAD"; break;
1639 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1640 case PT_INTERP: pt = "INTERP"; break;
1641 case PT_NOTE: pt = "NOTE"; break;
1642 case PT_SHLIB: pt = "SHLIB"; break;
1643 case PT_PHDR: pt = "PHDR"; break;
1644 case PT_TLS: pt = "TLS"; break;
1645 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1646 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1647 case PT_GNU_RELRO: pt = "RELRO"; break;
1648 default: pt = NULL; break;
1649 }
1650 return pt;
1651}
1652
f0b79d91
L
1653/* Print out the program headers. */
1654
0a1b45a2 1655bool
217aa764 1656_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1657{
a50b1753 1658 FILE *f = (FILE *) farg;
252b5132
RH
1659 Elf_Internal_Phdr *p;
1660 asection *s;
1661 bfd_byte *dynbuf = NULL;
1662
1663 p = elf_tdata (abfd)->phdr;
1664 if (p != NULL)
1665 {
1666 unsigned int i, c;
1667
1668 fprintf (f, _("\nProgram Header:\n"));
1669 c = elf_elfheader (abfd)->e_phnum;
1670 for (i = 0; i < c; i++, p++)
1671 {
cedc298e 1672 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1673 char buf[20];
1674
cedc298e 1675 if (pt == NULL)
252b5132 1676 {
cedc298e
L
1677 sprintf (buf, "0x%lx", p->p_type);
1678 pt = buf;
252b5132 1679 }
dc810e39 1680 fprintf (f, "%8s off 0x", pt);
60b89a18 1681 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1682 fprintf (f, " vaddr 0x");
60b89a18 1683 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1684 fprintf (f, " paddr 0x");
60b89a18 1685 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1686 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1687 fprintf (f, " filesz 0x");
60b89a18 1688 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1689 fprintf (f, " memsz 0x");
60b89a18 1690 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1691 fprintf (f, " flags %c%c%c",
1692 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1693 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1694 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1695 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1696 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1697 fprintf (f, "\n");
1698 }
1699 }
1700
1701 s = bfd_get_section_by_name (abfd, ".dynamic");
1702 if (s != NULL)
1703 {
cb33740c 1704 unsigned int elfsec;
dc810e39 1705 unsigned long shlink;
252b5132
RH
1706 bfd_byte *extdyn, *extdynend;
1707 size_t extdynsize;
217aa764 1708 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1709
1710 fprintf (f, _("\nDynamic Section:\n"));
1711
eea6121a 1712 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1713 goto error_return;
1714
1715 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1716 if (elfsec == SHN_BAD)
252b5132 1717 goto error_return;
dc810e39 1718 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1719
1720 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1721 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1722
1723 extdyn = dynbuf;
06614111
NC
1724 /* PR 17512: file: 6f427532. */
1725 if (s->size < extdynsize)
1726 goto error_return;
eea6121a 1727 extdynend = extdyn + s->size;
1036838a 1728 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1729 Fix range check. */
1036838a 1730 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1731 {
1732 Elf_Internal_Dyn dyn;
ad9563d6 1733 const char *name = "";
252b5132 1734 char ab[20];
0a1b45a2 1735 bool stringp;
ad9563d6 1736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1737
217aa764 1738 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1739
1740 if (dyn.d_tag == DT_NULL)
1741 break;
1742
0a1b45a2 1743 stringp = false;
252b5132
RH
1744 switch (dyn.d_tag)
1745 {
1746 default:
ad9563d6
CM
1747 if (bed->elf_backend_get_target_dtag)
1748 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1749
1750 if (!strcmp (name, ""))
1751 {
cd9af601 1752 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1753 name = ab;
1754 }
252b5132
RH
1755 break;
1756
0a1b45a2 1757 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
252b5132
RH
1758 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1759 case DT_PLTGOT: name = "PLTGOT"; break;
1760 case DT_HASH: name = "HASH"; break;
1761 case DT_STRTAB: name = "STRTAB"; break;
1762 case DT_SYMTAB: name = "SYMTAB"; break;
1763 case DT_RELA: name = "RELA"; break;
1764 case DT_RELASZ: name = "RELASZ"; break;
1765 case DT_RELAENT: name = "RELAENT"; break;
1766 case DT_STRSZ: name = "STRSZ"; break;
1767 case DT_SYMENT: name = "SYMENT"; break;
1768 case DT_INIT: name = "INIT"; break;
1769 case DT_FINI: name = "FINI"; break;
0a1b45a2
AM
1770 case DT_SONAME: name = "SONAME"; stringp = true; break;
1771 case DT_RPATH: name = "RPATH"; stringp = true; break;
252b5132
RH
1772 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1773 case DT_REL: name = "REL"; break;
1774 case DT_RELSZ: name = "RELSZ"; break;
1775 case DT_RELENT: name = "RELENT"; break;
1776 case DT_PLTREL: name = "PLTREL"; break;
1777 case DT_DEBUG: name = "DEBUG"; break;
1778 case DT_TEXTREL: name = "TEXTREL"; break;
1779 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1780 case DT_BIND_NOW: name = "BIND_NOW"; break;
1781 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1782 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1783 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1784 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
0a1b45a2 1785 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
94558834
L
1786 case DT_FLAGS: name = "FLAGS"; break;
1787 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1788 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1789 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1790 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1791 case DT_MOVEENT: name = "MOVEENT"; break;
1792 case DT_MOVESZ: name = "MOVESZ"; break;
1793 case DT_FEATURE: name = "FEATURE"; break;
1794 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1795 case DT_SYMINSZ: name = "SYMINSZ"; break;
1796 case DT_SYMINENT: name = "SYMINENT"; break;
0a1b45a2
AM
1797 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1798 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1799 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1800 case DT_PLTPAD: name = "PLTPAD"; break;
1801 case DT_MOVETAB: name = "MOVETAB"; break;
1802 case DT_SYMINFO: name = "SYMINFO"; break;
1803 case DT_RELACOUNT: name = "RELACOUNT"; break;
1804 case DT_RELCOUNT: name = "RELCOUNT"; break;
1805 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1806 case DT_VERSYM: name = "VERSYM"; break;
1807 case DT_VERDEF: name = "VERDEF"; break;
1808 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1809 case DT_VERNEED: name = "VERNEED"; break;
1810 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
0a1b45a2 1811 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
94558834 1812 case DT_USED: name = "USED"; break;
0a1b45a2 1813 case DT_FILTER: name = "FILTER"; stringp = true; break;
fdc90cb4 1814 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1815 }
1816
ad9563d6 1817 fprintf (f, " %-20s ", name);
252b5132 1818 if (! stringp)
a1f3c56e
AN
1819 {
1820 fprintf (f, "0x");
1821 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1822 }
252b5132
RH
1823 else
1824 {
1825 const char *string;
dc810e39 1826 unsigned int tagv = dyn.d_un.d_val;
252b5132 1827
dc810e39 1828 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1829 if (string == NULL)
1830 goto error_return;
1831 fprintf (f, "%s", string);
1832 }
1833 fprintf (f, "\n");
1834 }
1835
1836 free (dynbuf);
1837 dynbuf = NULL;
1838 }
1839
1840 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1841 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1842 {
0a1b45a2
AM
1843 if (! _bfd_elf_slurp_version_tables (abfd, false))
1844 return false;
252b5132
RH
1845 }
1846
1847 if (elf_dynverdef (abfd) != 0)
1848 {
1849 Elf_Internal_Verdef *t;
1850
1851 fprintf (f, _("\nVersion definitions:\n"));
1852 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1853 {
1854 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1855 t->vd_flags, t->vd_hash,
1856 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1857 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1858 {
1859 Elf_Internal_Verdaux *a;
1860
1861 fprintf (f, "\t");
1862 for (a = t->vd_auxptr->vda_nextptr;
1863 a != NULL;
1864 a = a->vda_nextptr)
d0fb9a8d
JJ
1865 fprintf (f, "%s ",
1866 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1867 fprintf (f, "\n");
1868 }
1869 }
1870 }
1871
1872 if (elf_dynverref (abfd) != 0)
1873 {
1874 Elf_Internal_Verneed *t;
1875
1876 fprintf (f, _("\nVersion References:\n"));
1877 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1878 {
1879 Elf_Internal_Vernaux *a;
1880
d0fb9a8d
JJ
1881 fprintf (f, _(" required from %s:\n"),
1882 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1883 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1884 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1885 a->vna_flags, a->vna_other,
1886 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1887 }
1888 }
1889
0a1b45a2 1890 return true;
252b5132
RH
1891
1892 error_return:
c9594989 1893 free (dynbuf);
0a1b45a2 1894 return false;
252b5132
RH
1895}
1896
7e6e972f
L
1897/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1898 and return symbol version for symbol version itself. */
bb4d2ac2
L
1899
1900const char *
1081065c 1901_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
0a1b45a2
AM
1902 bool base_p,
1903 bool *hidden)
bb4d2ac2
L
1904{
1905 const char *version_string = NULL;
1906 if (elf_dynversym (abfd) != 0
1907 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1908 {
1909 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1910
1911 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1912 vernum &= VERSYM_VERSION;
1913
1914 if (vernum == 0)
1915 version_string = "";
1f6f5dba
L
1916 else if (vernum == 1
1917 && (vernum > elf_tdata (abfd)->cverdefs
1918 || (elf_tdata (abfd)->verdef[0].vd_flags
1919 == VER_FLG_BASE)))
7e6e972f 1920 version_string = base_p ? "Base" : "";
bb4d2ac2 1921 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1922 {
1923 const char *nodename
1924 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1925 version_string = "";
1926 if (base_p
1927 || nodename == NULL
1928 || symbol->name == NULL
1929 || strcmp (symbol->name, nodename) != 0)
1930 version_string = nodename;
7e6e972f 1931 }
bb4d2ac2
L
1932 else
1933 {
1934 Elf_Internal_Verneed *t;
1935
7a815dd5 1936 version_string = _("<corrupt>");
bb4d2ac2
L
1937 for (t = elf_tdata (abfd)->verref;
1938 t != NULL;
1939 t = t->vn_nextref)
1940 {
1941 Elf_Internal_Vernaux *a;
1942
1943 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1944 {
1945 if (a->vna_other == vernum)
1946 {
1947 version_string = a->vna_nodename;
1948 break;
1949 }
1950 }
1951 }
1952 }
1953 }
1954 return version_string;
1955}
1956
252b5132
RH
1957/* Display ELF-specific fields of a symbol. */
1958
1959void
217aa764
AM
1960bfd_elf_print_symbol (bfd *abfd,
1961 void *filep,
1962 asymbol *symbol,
1963 bfd_print_symbol_type how)
252b5132 1964{
a50b1753 1965 FILE *file = (FILE *) filep;
252b5132
RH
1966 switch (how)
1967 {
1968 case bfd_print_symbol_name:
1969 fprintf (file, "%s", symbol->name);
1970 break;
1971 case bfd_print_symbol_more:
1972 fprintf (file, "elf ");
60b89a18 1973 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1974 fprintf (file, " %x", symbol->flags);
252b5132
RH
1975 break;
1976 case bfd_print_symbol_all:
1977 {
4e8a9624
AM
1978 const char *section_name;
1979 const char *name = NULL;
9c5bfbb7 1980 const struct elf_backend_data *bed;
7a13edea 1981 unsigned char st_other;
dbb410c3 1982 bfd_vma val;
bb4d2ac2 1983 const char *version_string;
0a1b45a2 1984 bool hidden;
c044fabd 1985
252b5132 1986 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1987
1988 bed = get_elf_backend_data (abfd);
1989 if (bed->elf_backend_print_symbol_all)
c044fabd 1990 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1991
1992 if (name == NULL)
1993 {
7ee38065 1994 name = symbol->name;
217aa764 1995 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1996 }
1997
252b5132
RH
1998 fprintf (file, " %s\t", section_name);
1999 /* Print the "other" value for a symbol. For common symbols,
2000 we've already printed the size; now print the alignment.
2001 For other symbols, we have no specified alignment, and
2002 we've printed the address; now print the size. */
dcf6c779 2003 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
2004 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2005 else
2006 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2007 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
2008
2009 /* If we have version information, print it. */
60bb06bc
L
2010 version_string = _bfd_elf_get_symbol_version_string (abfd,
2011 symbol,
0a1b45a2 2012 true,
60bb06bc 2013 &hidden);
bb4d2ac2 2014 if (version_string)
252b5132 2015 {
bb4d2ac2 2016 if (!hidden)
252b5132
RH
2017 fprintf (file, " %-11s", version_string);
2018 else
2019 {
2020 int i;
2021
2022 fprintf (file, " (%s)", version_string);
2023 for (i = 10 - strlen (version_string); i > 0; --i)
2024 putc (' ', file);
2025 }
2026 }
2027
2028 /* If the st_other field is not zero, print it. */
7a13edea 2029 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2030
7a13edea
NC
2031 switch (st_other)
2032 {
2033 case 0: break;
2034 case STV_INTERNAL: fprintf (file, " .internal"); break;
2035 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2036 case STV_PROTECTED: fprintf (file, " .protected"); break;
2037 default:
2038 /* Some other non-defined flags are also present, so print
2039 everything hex. */
2040 fprintf (file, " 0x%02x", (unsigned int) st_other);
2041 }
252b5132 2042
587ff49e 2043 fprintf (file, " %s", name);
252b5132
RH
2044 }
2045 break;
2046 }
2047}
252b5132
RH
2048\f
2049/* ELF .o/exec file reading */
2050
c044fabd 2051/* Create a new bfd section from an ELF section header. */
252b5132 2052
0a1b45a2 2053bool
217aa764 2054bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2055{
4fbb74a6
AM
2056 Elf_Internal_Shdr *hdr;
2057 Elf_Internal_Ehdr *ehdr;
2058 const struct elf_backend_data *bed;
90937f86 2059 const char *name;
0a1b45a2 2060 bool ret = true;
252b5132 2061
4fbb74a6 2062 if (shindex >= elf_numsections (abfd))
0a1b45a2 2063 return false;
4fbb74a6 2064
a86c6c19
AM
2065 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2066 sh_link or sh_info. Detect this here, by refusing to load a
2067 section that we are already in the process of loading. */
2068 if (elf_tdata (abfd)->being_created[shindex])
bf67003b 2069 {
a86c6c19
AM
2070 _bfd_error_handler
2071 (_("%pB: warning: loop in section dependencies detected"), abfd);
0a1b45a2 2072 return false;
bf67003b 2073 }
0a1b45a2 2074 elf_tdata (abfd)->being_created[shindex] = true;
bf67003b 2075
4fbb74a6
AM
2076 hdr = elf_elfsections (abfd)[shindex];
2077 ehdr = elf_elfheader (abfd);
2078 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2079 hdr->sh_name);
933d961a 2080 if (name == NULL)
bf67003b 2081 goto fail;
252b5132 2082
4fbb74a6 2083 bed = get_elf_backend_data (abfd);
252b5132
RH
2084 switch (hdr->sh_type)
2085 {
2086 case SHT_NULL:
2087 /* Inactive section. Throw it away. */
bf67003b 2088 goto success;
252b5132 2089
bf67003b
NC
2090 case SHT_PROGBITS: /* Normal section with contents. */
2091 case SHT_NOBITS: /* .bss section. */
2092 case SHT_HASH: /* .hash section. */
2093 case SHT_NOTE: /* .note section. */
25e27870
L
2094 case SHT_INIT_ARRAY: /* .init_array section. */
2095 case SHT_FINI_ARRAY: /* .fini_array section. */
2096 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2097 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2098 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2099 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2100 goto success;
252b5132 2101
797fc050 2102 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2103 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2104 goto fail;
2105
cfcac11d
NC
2106 if (hdr->sh_link > elf_numsections (abfd))
2107 {
caa83f8b 2108 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2109 field set to SHN_BEFORE or SHN_AFTER. */
2110 switch (bfd_get_arch (abfd))
2111 {
caa83f8b 2112 case bfd_arch_i386:
cfcac11d
NC
2113 case bfd_arch_sparc:
2114 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2115 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2116 break;
2117 /* Otherwise fall through. */
2118 default:
bf67003b 2119 goto fail;
cfcac11d
NC
2120 }
2121 }
2122 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2123 goto fail;
cfcac11d 2124 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2125 {
2126 Elf_Internal_Shdr *dynsymhdr;
2127
2128 /* The shared libraries distributed with hpux11 have a bogus
2129 sh_link field for the ".dynamic" section. Find the
2130 string table for the ".dynsym" section instead. */
2131 if (elf_dynsymtab (abfd) != 0)
2132 {
2133 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2134 hdr->sh_link = dynsymhdr->sh_link;
2135 }
2136 else
2137 {
2138 unsigned int i, num_sec;
2139
2140 num_sec = elf_numsections (abfd);
2141 for (i = 1; i < num_sec; i++)
2142 {
2143 dynsymhdr = elf_elfsections (abfd)[i];
2144 if (dynsymhdr->sh_type == SHT_DYNSYM)
2145 {
2146 hdr->sh_link = dynsymhdr->sh_link;
2147 break;
2148 }
2149 }
2150 }
2151 }
bf67003b 2152 goto success;
797fc050 2153
bf67003b 2154 case SHT_SYMTAB: /* A symbol table. */
252b5132 2155 if (elf_onesymtab (abfd) == shindex)
bf67003b 2156 goto success;
252b5132 2157
a50b2160 2158 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2159 goto fail;
2160
3337c1e5 2161 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2162 {
2163 if (hdr->sh_size != 0)
bf67003b 2164 goto fail;
eee3b786
AM
2165 /* Some assemblers erroneously set sh_info to one with a
2166 zero sh_size. ld sees this as a global symbol count
2167 of (unsigned) -1. Fix it here. */
2168 hdr->sh_info = 0;
bf67003b 2169 goto success;
eee3b786 2170 }
bf67003b 2171
16ad13ec
NC
2172 /* PR 18854: A binary might contain more than one symbol table.
2173 Unusual, but possible. Warn, but continue. */
2174 if (elf_onesymtab (abfd) != 0)
2175 {
4eca0228 2176 _bfd_error_handler
695344c0 2177 /* xgettext:c-format */
871b3ab2 2178 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2179 " - ignoring the table in section %u"),
16ad13ec
NC
2180 abfd, shindex);
2181 goto success;
2182 }
252b5132 2183 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2184 elf_symtab_hdr (abfd) = *hdr;
2185 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2186 abfd->flags |= HAS_SYMS;
2187
2188 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2189 SHF_ALLOC is set, and this is a shared object, then we also
2190 treat this section as a BFD section. We can not base the
2191 decision purely on SHF_ALLOC, because that flag is sometimes
2192 set in a relocatable object file, which would confuse the
2193 linker. */
252b5132
RH
2194 if ((hdr->sh_flags & SHF_ALLOC) != 0
2195 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2196 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2197 shindex))
bf67003b 2198 goto fail;
252b5132 2199
1b3a8575
AM
2200 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2201 can't read symbols without that section loaded as well. It
2202 is most likely specified by the next section header. */
6a40cf0c
NC
2203 {
2204 elf_section_list * entry;
2205 unsigned int i, num_sec;
1b3a8575 2206
6a40cf0c
NC
2207 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2208 if (entry->hdr.sh_link == shindex)
2209 goto success;
2210
2211 num_sec = elf_numsections (abfd);
2212 for (i = shindex + 1; i < num_sec; i++)
2213 {
2214 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2215
2216 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2217 && hdr2->sh_link == shindex)
2218 break;
2219 }
2220
2221 if (i == num_sec)
2222 for (i = 1; i < shindex; i++)
1b3a8575
AM
2223 {
2224 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2225
1b3a8575
AM
2226 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2227 && hdr2->sh_link == shindex)
2228 break;
2229 }
6a40cf0c
NC
2230
2231 if (i != shindex)
2232 ret = bfd_section_from_shdr (abfd, i);
2233 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2234 goto success;
2235 }
252b5132 2236
bf67003b 2237 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2238 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2239 goto success;
252b5132 2240
a50b2160 2241 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2242 goto fail;
2243
eee3b786
AM
2244 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2245 {
2246 if (hdr->sh_size != 0)
bf67003b
NC
2247 goto fail;
2248
eee3b786
AM
2249 /* Some linkers erroneously set sh_info to one with a
2250 zero sh_size. ld sees this as a global symbol count
2251 of (unsigned) -1. Fix it here. */
2252 hdr->sh_info = 0;
bf67003b 2253 goto success;
eee3b786 2254 }
bf67003b 2255
16ad13ec
NC
2256 /* PR 18854: A binary might contain more than one dynamic symbol table.
2257 Unusual, but possible. Warn, but continue. */
2258 if (elf_dynsymtab (abfd) != 0)
2259 {
4eca0228 2260 _bfd_error_handler
695344c0 2261 /* xgettext:c-format */
871b3ab2 2262 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2263 " - ignoring the table in section %u"),
16ad13ec
NC
2264 abfd, shindex);
2265 goto success;
2266 }
252b5132
RH
2267 elf_dynsymtab (abfd) = shindex;
2268 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2269 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2270 abfd->flags |= HAS_SYMS;
2271
2272 /* Besides being a symbol table, we also treat this as a regular
2273 section, so that objcopy can handle it. */
bf67003b
NC
2274 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2275 goto success;
252b5132 2276
bf67003b 2277 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2278 {
2279 elf_section_list * entry;
9ad5cbcf 2280
6a40cf0c
NC
2281 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2282 if (entry->ndx == shindex)
2283 goto success;
07d6d2b8 2284
7a6e0d89 2285 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2286 if (entry == NULL)
2287 goto fail;
2288 entry->ndx = shindex;
2289 entry->hdr = * hdr;
2290 entry->next = elf_symtab_shndx_list (abfd);
2291 elf_symtab_shndx_list (abfd) = entry;
2292 elf_elfsections (abfd)[shindex] = & entry->hdr;
2293 goto success;
2294 }
9ad5cbcf 2295
bf67003b 2296 case SHT_STRTAB: /* A string table. */
252b5132 2297 if (hdr->bfd_section != NULL)
bf67003b
NC
2298 goto success;
2299
252b5132
RH
2300 if (ehdr->e_shstrndx == shindex)
2301 {
2302 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2303 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2304 goto success;
252b5132 2305 }
bf67003b 2306
1b3a8575
AM
2307 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2308 {
2309 symtab_strtab:
2310 elf_tdata (abfd)->strtab_hdr = *hdr;
2311 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2312 goto success;
1b3a8575 2313 }
bf67003b 2314
1b3a8575
AM
2315 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2316 {
2317 dynsymtab_strtab:
2318 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2319 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2320 elf_elfsections (abfd)[shindex] = hdr;
2321 /* We also treat this as a regular section, so that objcopy
2322 can handle it. */
bf67003b
NC
2323 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2324 shindex);
2325 goto success;
1b3a8575 2326 }
252b5132 2327
1b3a8575
AM
2328 /* If the string table isn't one of the above, then treat it as a
2329 regular section. We need to scan all the headers to be sure,
2330 just in case this strtab section appeared before the above. */
2331 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2332 {
2333 unsigned int i, num_sec;
252b5132 2334
1b3a8575
AM
2335 num_sec = elf_numsections (abfd);
2336 for (i = 1; i < num_sec; i++)
2337 {
2338 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2339 if (hdr2->sh_link == shindex)
2340 {
933d961a
JJ
2341 /* Prevent endless recursion on broken objects. */
2342 if (i == shindex)
bf67003b 2343 goto fail;
1b3a8575 2344 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2345 goto fail;
1b3a8575
AM
2346 if (elf_onesymtab (abfd) == i)
2347 goto symtab_strtab;
2348 if (elf_dynsymtab (abfd) == i)
2349 goto dynsymtab_strtab;
2350 }
2351 }
2352 }
bf67003b
NC
2353 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2354 goto success;
252b5132
RH
2355
2356 case SHT_REL:
2357 case SHT_RELA:
2358 /* *These* do a lot of work -- but build no sections! */
2359 {
2360 asection *target_sect;
d4730f92 2361 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2362 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2363 struct bfd_elf_section_data *esdt;
252b5132 2364
aa2ca951
JJ
2365 if (hdr->sh_entsize
2366 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2367 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2368 goto fail;
a50b2160 2369
03ae5f59 2370 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2371 if (hdr->sh_link >= num_sec)
03ae5f59 2372 {
4eca0228 2373 _bfd_error_handler
695344c0 2374 /* xgettext:c-format */
871b3ab2 2375 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2376 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2377 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2378 shindex);
2379 goto success;
03ae5f59
ILT
2380 }
2381
252b5132
RH
2382 /* For some incomprehensible reason Oracle distributes
2383 libraries for Solaris in which some of the objects have
2384 bogus sh_link fields. It would be nice if we could just
2385 reject them, but, unfortunately, some people need to use
2386 them. We scan through the section headers; if we find only
2387 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2388 to it. I hope this doesn't break anything.
2389
2390 Don't do it on executable nor shared library. */
2391 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2392 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2393 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2394 {
9ad5cbcf 2395 unsigned int scan;
252b5132
RH
2396 int found;
2397
2398 found = 0;
9ad5cbcf 2399 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2400 {
2401 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2402 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2403 {
2404 if (found != 0)
2405 {
2406 found = 0;
2407 break;
2408 }
2409 found = scan;
2410 }
2411 }
2412 if (found != 0)
2413 hdr->sh_link = found;
2414 }
2415
2416 /* Get the symbol table. */
1b3a8575
AM
2417 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2418 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2419 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2420 goto fail;
252b5132 2421
a4bcd733
AM
2422 /* If this is an alloc section in an executable or shared
2423 library, or the reloc section does not use the main symbol
2424 table we don't treat it as a reloc section. BFD can't
2425 adequately represent such a section, so at least for now,
2426 we don't try. We just present it as a normal section. We
2427 also can't use it as a reloc section if it points to the
2428 null section, an invalid section, another reloc section, or
2429 its sh_link points to the null section. */
2430 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2431 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2432 || hdr->sh_link == SHN_UNDEF
a4bcd733 2433 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2434 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2435 || hdr->sh_info >= num_sec
2436 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2437 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2438 {
2439 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2440 shindex);
2441 goto success;
2442 }
252b5132
RH
2443
2444 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2445 goto fail;
2446
252b5132
RH
2447 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2448 if (target_sect == NULL)
bf67003b 2449 goto fail;
252b5132 2450
d4730f92
BS
2451 esdt = elf_section_data (target_sect);
2452 if (hdr->sh_type == SHT_RELA)
2453 p_hdr = &esdt->rela.hdr;
252b5132 2454 else
d4730f92
BS
2455 p_hdr = &esdt->rel.hdr;
2456
a7ba3896
NC
2457 /* PR 17512: file: 0b4f81b7.
2458 Also see PR 24456, for a file which deliberately has two reloc
2459 sections. */
06614111 2460 if (*p_hdr != NULL)
a7ba3896 2461 {
a859124d 2462 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2463 {
2464 _bfd_error_handler
2465 /* xgettext:c-format */
a859124d
AM
2466 (_("%pB: warning: secondary relocation section '%s' "
2467 "for section %pA found - ignoring"),
a8e14f4c
NC
2468 abfd, name, target_sect);
2469 }
956ea65c
MM
2470 else
2471 esdt->has_secondary_relocs = true;
a7ba3896
NC
2472 goto success;
2473 }
a8e14f4c 2474
ef53be89 2475 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2476 if (hdr2 == NULL)
bf67003b 2477 goto fail;
252b5132 2478 *hdr2 = *hdr;
d4730f92 2479 *p_hdr = hdr2;
252b5132 2480 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2481 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2482 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2483 target_sect->flags |= SEC_RELOC;
2484 target_sect->relocation = NULL;
2485 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2486 /* In the section to which the relocations apply, mark whether
2487 its relocations are of the REL or RELA variety. */
72730e0c 2488 if (hdr->sh_size != 0)
d4730f92
BS
2489 {
2490 if (hdr->sh_type == SHT_RELA)
2491 target_sect->use_rela_p = 1;
2492 }
252b5132 2493 abfd->flags |= HAS_RELOC;
bf67003b 2494 goto success;
252b5132 2495 }
252b5132
RH
2496
2497 case SHT_GNU_verdef:
2498 elf_dynverdef (abfd) = shindex;
2499 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2500 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2501 goto success;
252b5132
RH
2502
2503 case SHT_GNU_versym:
a50b2160 2504 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2505 goto fail;
2506
252b5132
RH
2507 elf_dynversym (abfd) = shindex;
2508 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2509 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2510 goto success;
252b5132
RH
2511
2512 case SHT_GNU_verneed:
2513 elf_dynverref (abfd) = shindex;
2514 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2516 goto success;
252b5132
RH
2517
2518 case SHT_SHLIB:
bf67003b 2519 goto success;
252b5132 2520
dbb410c3 2521 case SHT_GROUP:
44534af3 2522 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2523 goto fail;
2524
6dc132d9 2525 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2526 goto fail;
2527
bf67003b 2528 goto success;
dbb410c3 2529
252b5132 2530 default:
104d59d1
JM
2531 /* Possibly an attributes section. */
2532 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2533 || hdr->sh_type == bed->obj_attrs_section_type)
2534 {
2535 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2536 goto fail;
104d59d1 2537 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2538 goto success;
104d59d1
JM
2539 }
2540
252b5132 2541 /* Check for any processor-specific section types. */
3eb70a79 2542 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2543 goto success;
3eb70a79
L
2544
2545 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2546 {
2547 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2548 /* FIXME: How to properly handle allocated section reserved
2549 for applications? */
4eca0228 2550 _bfd_error_handler
695344c0 2551 /* xgettext:c-format */
871b3ab2 2552 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2553 abfd, hdr->sh_type, name);
3eb70a79 2554 else
bf67003b
NC
2555 {
2556 /* Allow sections reserved for applications. */
2557 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2558 shindex);
2559 goto success;
2560 }
3eb70a79
L
2561 }
2562 else if (hdr->sh_type >= SHT_LOPROC
2563 && hdr->sh_type <= SHT_HIPROC)
2564 /* FIXME: We should handle this section. */
4eca0228 2565 _bfd_error_handler
695344c0 2566 /* xgettext:c-format */
871b3ab2 2567 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2568 abfd, hdr->sh_type, name);
3eb70a79 2569 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2570 {
2571 /* Unrecognised OS-specific sections. */
2572 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2573 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2574 required to correctly process the section and the file should
ff15b240 2575 be rejected with an error message. */
4eca0228 2576 _bfd_error_handler
695344c0 2577 /* xgettext:c-format */
871b3ab2 2578 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2579 abfd, hdr->sh_type, name);
ff15b240 2580 else
bf67003b
NC
2581 {
2582 /* Otherwise it should be processed. */
2583 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2584 goto success;
2585 }
ff15b240 2586 }
3eb70a79
L
2587 else
2588 /* FIXME: We should handle this section. */
4eca0228 2589 _bfd_error_handler
695344c0 2590 /* xgettext:c-format */
871b3ab2 2591 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2592 abfd, hdr->sh_type, name);
3eb70a79 2593
bf67003b 2594 goto fail;
252b5132
RH
2595 }
2596
bf67003b 2597 fail:
0a1b45a2 2598 ret = false;
bf67003b 2599 success:
0a1b45a2 2600 elf_tdata (abfd)->being_created[shindex] = false;
bf67003b 2601 return ret;
252b5132
RH
2602}
2603
87d72d41 2604/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2605
87d72d41
AM
2606Elf_Internal_Sym *
2607bfd_sym_from_r_symndx (struct sym_cache *cache,
2608 bfd *abfd,
2609 unsigned long r_symndx)
ec338859 2610{
ec338859
AM
2611 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2612
a5d1b3b5
AM
2613 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2614 {
2615 Elf_Internal_Shdr *symtab_hdr;
2616 unsigned char esym[sizeof (Elf64_External_Sym)];
2617 Elf_External_Sym_Shndx eshndx;
ec338859 2618
a5d1b3b5
AM
2619 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2620 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2621 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2622 return NULL;
9ad5cbcf 2623
a5d1b3b5
AM
2624 if (cache->abfd != abfd)
2625 {
2626 memset (cache->indx, -1, sizeof (cache->indx));
2627 cache->abfd = abfd;
2628 }
2629 cache->indx[ent] = r_symndx;
ec338859 2630 }
a5d1b3b5 2631
87d72d41 2632 return &cache->sym[ent];
ec338859
AM
2633}
2634
252b5132
RH
2635/* Given an ELF section number, retrieve the corresponding BFD
2636 section. */
2637
2638asection *
91d6fa6a 2639bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2640{
91d6fa6a 2641 if (sec_index >= elf_numsections (abfd))
252b5132 2642 return NULL;
91d6fa6a 2643 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2644}
2645
b35d266b 2646static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2647{
0112cd26 2648 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2649 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2650};
2651
b35d266b 2652static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2653{
0112cd26 2654 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2655 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2656 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2657};
2658
b35d266b 2659static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2660{
07d6d2b8
AM
2661 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2662 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2663 /* There are more DWARF sections than these, but they needn't be added here
2664 unless you have to cope with broken compilers that don't emit section
2665 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2666 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2667 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2668 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2669 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2670 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2671 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2672 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2673 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2674 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2675};
2676
b35d266b 2677static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2678{
07d6d2b8 2679 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2680 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2681 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2682};
2683
b35d266b 2684static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2685{
0112cd26 2686 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2687 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2688 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2689 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2690 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2691 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2692 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2693 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2694 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2695 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2696 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2697 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2698};
2699
b35d266b 2700static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2701{
07d6d2b8
AM
2702 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2703 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2704};
2705
b35d266b 2706static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2707{
07d6d2b8 2708 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2709 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2710 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2711 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2712};
2713
b35d266b 2714static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2715{
0112cd26 2716 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2717 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2718};
2719
b35d266b 2720static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2721{
2c6f3e56 2722 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2723 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2724 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2725 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2726};
2727
b35d266b 2728static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2729{
f5e98b7d 2730 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56 2731 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2732 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2733 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2734 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2735};
2736
b35d266b 2737static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2738{
0112cd26
NC
2739 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2740 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2741 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2742 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2743 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2744};
2745
b35d266b 2746static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2747{
0112cd26
NC
2748 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2749 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2750 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2751 /* See struct bfd_elf_special_section declaration for the semantics of
2752 this special case where .prefix_length != strlen (.prefix). */
2753 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2754 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2755};
2756
b35d266b 2757static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2758{
07d6d2b8
AM
2759 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2760 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2761 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2762 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2763};
2764
1b315056
CS
2765static const struct bfd_elf_special_section special_sections_z[] =
2766{
07d6d2b8
AM
2767 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2768 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2769 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2770 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2771 { NULL, 0, 0, 0, 0 }
1b315056
CS
2772};
2773
e4c93b56 2774static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2775{
7f4d3958 2776 special_sections_b, /* 'b' */
98ece1b3 2777 special_sections_c, /* 'c' */
7f4d3958
L
2778 special_sections_d, /* 'd' */
2779 NULL, /* 'e' */
2780 special_sections_f, /* 'f' */
2781 special_sections_g, /* 'g' */
2782 special_sections_h, /* 'h' */
2783 special_sections_i, /* 'i' */
2784 NULL, /* 'j' */
2785 NULL, /* 'k' */
2786 special_sections_l, /* 'l' */
2787 NULL, /* 'm' */
2788 special_sections_n, /* 'n' */
2789 NULL, /* 'o' */
2790 special_sections_p, /* 'p' */
2791 NULL, /* 'q' */
2792 special_sections_r, /* 'r' */
2793 special_sections_s, /* 's' */
2794 special_sections_t, /* 't' */
1b315056
CS
2795 NULL, /* 'u' */
2796 NULL, /* 'v' */
2797 NULL, /* 'w' */
2798 NULL, /* 'x' */
2799 NULL, /* 'y' */
2800 special_sections_z /* 'z' */
7f4d3958
L
2801};
2802
551b43fd
AM
2803const struct bfd_elf_special_section *
2804_bfd_elf_get_special_section (const char *name,
2805 const struct bfd_elf_special_section *spec,
2806 unsigned int rela)
2f89ff8d
L
2807{
2808 int i;
7f4d3958 2809 int len;
7f4d3958 2810
551b43fd 2811 len = strlen (name);
7f4d3958 2812
551b43fd 2813 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2814 {
2815 int suffix_len;
551b43fd 2816 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2817
2818 if (len < prefix_len)
2819 continue;
551b43fd 2820 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2821 continue;
2822
551b43fd 2823 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2824 if (suffix_len <= 0)
2825 {
2826 if (name[prefix_len] != 0)
2827 {
2828 if (suffix_len == 0)
2829 continue;
2830 if (name[prefix_len] != '.'
2831 && (suffix_len == -2
551b43fd 2832 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2833 continue;
2834 }
2835 }
2836 else
2837 {
2838 if (len < prefix_len + suffix_len)
2839 continue;
2840 if (memcmp (name + len - suffix_len,
551b43fd 2841 spec[i].prefix + prefix_len,
7dcb9820
AM
2842 suffix_len) != 0)
2843 continue;
2844 }
551b43fd 2845 return &spec[i];
7dcb9820 2846 }
2f89ff8d
L
2847
2848 return NULL;
2849}
2850
7dcb9820 2851const struct bfd_elf_special_section *
29ef7005 2852_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2853{
551b43fd
AM
2854 int i;
2855 const struct bfd_elf_special_section *spec;
29ef7005 2856 const struct elf_backend_data *bed;
2f89ff8d
L
2857
2858 /* See if this is one of the special sections. */
551b43fd
AM
2859 if (sec->name == NULL)
2860 return NULL;
2f89ff8d 2861
29ef7005
L
2862 bed = get_elf_backend_data (abfd);
2863 spec = bed->special_sections;
2864 if (spec)
2865 {
2866 spec = _bfd_elf_get_special_section (sec->name,
2867 bed->special_sections,
2868 sec->use_rela_p);
2869 if (spec != NULL)
2870 return spec;
2871 }
2872
551b43fd
AM
2873 if (sec->name[0] != '.')
2874 return NULL;
2f89ff8d 2875
551b43fd 2876 i = sec->name[1] - 'b';
1b315056 2877 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2878 return NULL;
2879
2880 spec = special_sections[i];
2f89ff8d 2881
551b43fd
AM
2882 if (spec == NULL)
2883 return NULL;
2884
2885 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2886}
2887
0a1b45a2 2888bool
217aa764 2889_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2890{
2891 struct bfd_elf_section_data *sdata;
551b43fd 2892 const struct elf_backend_data *bed;
7dcb9820 2893 const struct bfd_elf_special_section *ssect;
252b5132 2894
f0abc2a1
AM
2895 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2896 if (sdata == NULL)
2897 {
a50b1753 2898 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2899 sizeof (*sdata));
f0abc2a1 2900 if (sdata == NULL)
0a1b45a2 2901 return false;
217aa764 2902 sec->used_by_bfd = sdata;
f0abc2a1 2903 }
bf572ba0 2904
551b43fd
AM
2905 /* Indicate whether or not this section should use RELA relocations. */
2906 bed = get_elf_backend_data (abfd);
2907 sec->use_rela_p = bed->default_use_rela_p;
2908
8c803a2d
AM
2909 /* Set up ELF section type and flags for newly created sections, if
2910 there is an ABI mandated section. */
2911 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2912 if (ssect != NULL)
2f89ff8d 2913 {
8c803a2d
AM
2914 elf_section_type (sec) = ssect->type;
2915 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2916 }
2917
f592407e 2918 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2919}
2920
2921/* Create a new bfd section from an ELF program header.
2922
2923 Since program segments have no names, we generate a synthetic name
2924 of the form segment<NUM>, where NUM is generally the index in the
2925 program header table. For segments that are split (see below) we
2926 generate the names segment<NUM>a and segment<NUM>b.
2927
2928 Note that some program segments may have a file size that is different than
2929 (less than) the memory size. All this means is that at execution the
2930 system must allocate the amount of memory specified by the memory size,
2931 but only initialize it with the first "file size" bytes read from the
2932 file. This would occur for example, with program segments consisting
2933 of combined data+bss.
2934
2935 To handle the above situation, this routine generates TWO bfd sections
2936 for the single program segment. The first has the length specified by
2937 the file size of the segment, and the second has the length specified
2938 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2939 into its initialized and uninitialized parts.
252b5132
RH
2940
2941 */
2942
0a1b45a2 2943bool
217aa764
AM
2944_bfd_elf_make_section_from_phdr (bfd *abfd,
2945 Elf_Internal_Phdr *hdr,
91d6fa6a 2946 int hdr_index,
a50b1753 2947 const char *type_name)
252b5132
RH
2948{
2949 asection *newsect;
2950 char *name;
2951 char namebuf[64];
d4c88bbb 2952 size_t len;
252b5132 2953 int split;
502794d4 2954 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2955
2956 split = ((hdr->p_memsz > 0)
2957 && (hdr->p_filesz > 0)
2958 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2959
2960 if (hdr->p_filesz > 0)
252b5132 2961 {
91d6fa6a 2962 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2963 len = strlen (namebuf) + 1;
a50b1753 2964 name = (char *) bfd_alloc (abfd, len);
d5191d0c 2965 if (!name)
0a1b45a2 2966 return false;
d5191d0c
AM
2967 memcpy (name, namebuf, len);
2968 newsect = bfd_make_section (abfd, name);
2969 if (newsect == NULL)
0a1b45a2 2970 return false;
502794d4
CE
2971 newsect->vma = hdr->p_vaddr / opb;
2972 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2973 newsect->size = hdr->p_filesz;
2974 newsect->filepos = hdr->p_offset;
2975 newsect->flags |= SEC_HAS_CONTENTS;
2976 newsect->alignment_power = bfd_log2 (hdr->p_align);
2977 if (hdr->p_type == PT_LOAD)
252b5132 2978 {
d5191d0c
AM
2979 newsect->flags |= SEC_ALLOC;
2980 newsect->flags |= SEC_LOAD;
2981 if (hdr->p_flags & PF_X)
2982 {
2983 /* FIXME: all we known is that it has execute PERMISSION,
2984 may be data. */
2985 newsect->flags |= SEC_CODE;
2986 }
2987 }
2988 if (!(hdr->p_flags & PF_W))
2989 {
2990 newsect->flags |= SEC_READONLY;
252b5132 2991 }
252b5132
RH
2992 }
2993
d5191d0c 2994 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2995 {
d5191d0c
AM
2996 bfd_vma align;
2997
91d6fa6a 2998 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2999 len = strlen (namebuf) + 1;
a50b1753 3000 name = (char *) bfd_alloc (abfd, len);
252b5132 3001 if (!name)
0a1b45a2 3002 return false;
d4c88bbb 3003 memcpy (name, namebuf, len);
252b5132
RH
3004 newsect = bfd_make_section (abfd, name);
3005 if (newsect == NULL)
0a1b45a2 3006 return false;
502794d4
CE
3007 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3008 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 3009 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3010 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3011 align = newsect->vma & -newsect->vma;
3012 if (align == 0 || align > hdr->p_align)
3013 align = hdr->p_align;
3014 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3015 if (hdr->p_type == PT_LOAD)
3016 {
3017 newsect->flags |= SEC_ALLOC;
3018 if (hdr->p_flags & PF_X)
3019 newsect->flags |= SEC_CODE;
3020 }
3021 if (!(hdr->p_flags & PF_W))
3022 newsect->flags |= SEC_READONLY;
3023 }
3024
0a1b45a2 3025 return true;
252b5132
RH
3026}
3027
0a1b45a2 3028static bool
864619bb
KS
3029_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3030{
3031 /* The return value is ignored. Build-ids are considered optional. */
3032 if (templ->xvec->flavour == bfd_target_elf_flavour)
3033 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3034 (templ, offset);
0a1b45a2 3035 return false;
864619bb
KS
3036}
3037
0a1b45a2 3038bool
91d6fa6a 3039bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3040{
9c5bfbb7 3041 const struct elf_backend_data *bed;
20cfcaae
NC
3042
3043 switch (hdr->p_type)
3044 {
3045 case PT_NULL:
91d6fa6a 3046 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3047
3048 case PT_LOAD:
864619bb 3049 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
0a1b45a2 3050 return false;
864619bb
KS
3051 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3052 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
0a1b45a2 3053 return true;
20cfcaae
NC
3054
3055 case PT_DYNAMIC:
91d6fa6a 3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3057
3058 case PT_INTERP:
91d6fa6a 3059 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3060
3061 case PT_NOTE:
91d6fa6a 3062 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
0a1b45a2 3063 return false;
276da9b3
L
3064 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3065 hdr->p_align))
0a1b45a2
AM
3066 return false;
3067 return true;
20cfcaae
NC
3068
3069 case PT_SHLIB:
91d6fa6a 3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3071
3072 case PT_PHDR:
91d6fa6a 3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3074
811072d8 3075 case PT_GNU_EH_FRAME:
91d6fa6a 3076 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3077 "eh_frame_hdr");
3078
2b05f1b7 3079 case PT_GNU_STACK:
91d6fa6a 3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3081
8c37241b 3082 case PT_GNU_RELRO:
91d6fa6a 3083 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3084
20cfcaae 3085 default:
8c1acd09 3086 /* Check for any processor-specific program segment types. */
20cfcaae 3087 bed = get_elf_backend_data (abfd);
91d6fa6a 3088 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3089 }
3090}
3091
d4730f92
BS
3092/* Return the REL_HDR for SEC, assuming there is only a single one, either
3093 REL or RELA. */
3094
3095Elf_Internal_Shdr *
3096_bfd_elf_single_rel_hdr (asection *sec)
3097{
3098 if (elf_section_data (sec)->rel.hdr)
3099 {
3100 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3101 return elf_section_data (sec)->rel.hdr;
3102 }
3103 else
3104 return elf_section_data (sec)->rela.hdr;
3105}
3106
0a1b45a2 3107static bool
3e19fb8f
L
3108_bfd_elf_set_reloc_sh_name (bfd *abfd,
3109 Elf_Internal_Shdr *rel_hdr,
3110 const char *sec_name,
0a1b45a2 3111 bool use_rela_p)
3e19fb8f
L
3112{
3113 char *name = (char *) bfd_alloc (abfd,
3114 sizeof ".rela" + strlen (sec_name));
3115 if (name == NULL)
0a1b45a2 3116 return false;
3e19fb8f
L
3117
3118 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3119 rel_hdr->sh_name =
3120 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
0a1b45a2 3121 false);
3e19fb8f 3122 if (rel_hdr->sh_name == (unsigned int) -1)
0a1b45a2 3123 return false;
3e19fb8f 3124
0a1b45a2 3125 return true;
3e19fb8f
L
3126}
3127
d4730f92
BS
3128/* Allocate and initialize a section-header for a new reloc section,
3129 containing relocations against ASECT. It is stored in RELDATA. If
3130 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3131 relocations. */
23bc299b 3132
0a1b45a2 3133static bool
217aa764 3134_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3135 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3136 const char *sec_name,
0a1b45a2
AM
3137 bool use_rela_p,
3138 bool delay_st_name_p)
23bc299b 3139{
d4730f92 3140 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3142
d4730f92 3143 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3144 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3145 reldata->hdr = rel_hdr;
23bc299b 3146
3e19fb8f
L
3147 if (delay_st_name_p)
3148 rel_hdr->sh_name = (unsigned int) -1;
3149 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3150 use_rela_p))
0a1b45a2 3151 return false;
23bc299b
MM
3152 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3153 rel_hdr->sh_entsize = (use_rela_p
3154 ? bed->s->sizeof_rela
3155 : bed->s->sizeof_rel);
72de5009 3156 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3157 rel_hdr->sh_flags = 0;
23bc299b
MM
3158 rel_hdr->sh_addr = 0;
3159 rel_hdr->sh_size = 0;
3160 rel_hdr->sh_offset = 0;
3161
0a1b45a2 3162 return true;
23bc299b
MM
3163}
3164
94be91de
JB
3165/* Return the default section type based on the passed in section flags. */
3166
3167int
3168bfd_elf_get_default_section_type (flagword flags)
3169{
0e41bebb 3170 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3171 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3172 return SHT_NOBITS;
3173 return SHT_PROGBITS;
3174}
3175
d4730f92
BS
3176struct fake_section_arg
3177{
3178 struct bfd_link_info *link_info;
0a1b45a2 3179 bool failed;
d4730f92
BS
3180};
3181
252b5132
RH
3182/* Set up an ELF internal section header for a section. */
3183
252b5132 3184static void
d4730f92 3185elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3186{
d4730f92 3187 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3189 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3190 Elf_Internal_Shdr *this_hdr;
0414f35b 3191 unsigned int sh_type;
0ce398f1 3192 const char *name = asect->name;
0a1b45a2 3193 bool delay_st_name_p = false;
233bf4f8 3194 bfd_vma mask;
252b5132 3195
d4730f92 3196 if (arg->failed)
252b5132
RH
3197 {
3198 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3199 loop. */
252b5132
RH
3200 return;
3201 }
3202
d4730f92 3203 this_hdr = &esd->this_hdr;
252b5132 3204
f6fe1ccd 3205 if (arg->link_info)
0ce398f1 3206 {
f6fe1ccd
L
3207 /* ld: compress DWARF debug sections with names: .debug_*. */
3208 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3209 && (asect->flags & SEC_DEBUGGING)
3210 && name[1] == 'd'
3211 && name[6] == '_')
3212 {
3213 /* Set SEC_ELF_COMPRESS to indicate this section should be
3214 compressed. */
3215 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3216 /* If this section will be compressed, delay adding section
3e19fb8f
L
3217 name to section name section after it is compressed in
3218 _bfd_elf_assign_file_positions_for_non_load. */
0a1b45a2 3219 delay_st_name_p = true;
f6fe1ccd
L
3220 }
3221 }
3222 else if ((asect->flags & SEC_ELF_RENAME))
3223 {
3224 /* objcopy: rename output DWARF debug section. */
3225 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3226 {
3227 /* When we decompress or compress with SHF_COMPRESSED,
3228 convert section name from .zdebug_* to .debug_* if
3229 needed. */
3230 if (name[1] == 'z')
3231 {
3232 char *new_name = convert_zdebug_to_debug (abfd, name);
3233 if (new_name == NULL)
3234 {
0a1b45a2 3235 arg->failed = true;
f6fe1ccd
L
3236 return;
3237 }
3238 name = new_name;
3239 }
3240 }
3241 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3242 {
f6fe1ccd
L
3243 /* PR binutils/18087: Compression does not always make a
3244 section smaller. So only rename the section when
3245 compression has actually taken place. If input section
3246 name is .zdebug_*, we should never compress it again. */
3247 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3248 if (new_name == NULL)
3249 {
0a1b45a2 3250 arg->failed = true;
0ce398f1
L
3251 return;
3252 }
f6fe1ccd
L
3253 BFD_ASSERT (name[1] != 'z');
3254 name = new_name;
0ce398f1
L
3255 }
3256 }
3257
3e19fb8f
L
3258 if (delay_st_name_p)
3259 this_hdr->sh_name = (unsigned int) -1;
3260 else
252b5132 3261 {
3e19fb8f
L
3262 this_hdr->sh_name
3263 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3264 name, false);
3e19fb8f
L
3265 if (this_hdr->sh_name == (unsigned int) -1)
3266 {
0a1b45a2 3267 arg->failed = true;
3e19fb8f
L
3268 return;
3269 }
252b5132
RH
3270 }
3271
a4d8e49b 3272 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3273
3274 if ((asect->flags & SEC_ALLOC) != 0
3275 || asect->user_set_vma)
502794d4 3276 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3277 else
3278 this_hdr->sh_addr = 0;
3279
3280 this_hdr->sh_offset = 0;
eea6121a 3281 this_hdr->sh_size = asect->size;
252b5132 3282 this_hdr->sh_link = 0;
c86934ce
NC
3283 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3284 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3285 {
4eca0228 3286 _bfd_error_handler
695344c0 3287 /* xgettext:c-format */
9793eb77 3288 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3289 abfd, asect->alignment_power, asect);
0a1b45a2 3290 arg->failed = true;
c86934ce
NC
3291 return;
3292 }
233bf4f8
AM
3293 /* Set sh_addralign to the highest power of two given by alignment
3294 consistent with the section VMA. Linker scripts can force VMA. */
3295 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3296 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3297 /* The sh_entsize and sh_info fields may have been set already by
3298 copy_private_section_data. */
3299
3300 this_hdr->bfd_section = asect;
3301 this_hdr->contents = NULL;
3302
3cddba1e
L
3303 /* If the section type is unspecified, we set it based on
3304 asect->flags. */
98ece1b3
AM
3305 if ((asect->flags & SEC_GROUP) != 0)
3306 sh_type = SHT_GROUP;
98ece1b3 3307 else
94be91de 3308 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3309
3cddba1e 3310 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3311 this_hdr->sh_type = sh_type;
3312 else if (this_hdr->sh_type == SHT_NOBITS
3313 && sh_type == SHT_PROGBITS
3314 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3315 {
98ece1b3
AM
3316 /* Warn if we are changing a NOBITS section to PROGBITS, but
3317 allow the link to proceed. This can happen when users link
3318 non-bss input sections to bss output sections, or emit data
3319 to a bss output section via a linker script. */
4eca0228 3320 _bfd_error_handler
871b3ab2 3321 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3322 this_hdr->sh_type = sh_type;
3cddba1e
L
3323 }
3324
2f89ff8d 3325 switch (this_hdr->sh_type)
252b5132 3326 {
2f89ff8d 3327 default:
2f89ff8d
L
3328 break;
3329
3330 case SHT_STRTAB:
2f89ff8d
L
3331 case SHT_NOTE:
3332 case SHT_NOBITS:
3333 case SHT_PROGBITS:
3334 break;
606851fb
AM
3335
3336 case SHT_INIT_ARRAY:
3337 case SHT_FINI_ARRAY:
3338 case SHT_PREINIT_ARRAY:
3339 this_hdr->sh_entsize = bed->s->arch_size / 8;
3340 break;
2f89ff8d
L
3341
3342 case SHT_HASH:
c7ac6ff8 3343 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3344 break;
5de3bf90 3345
2f89ff8d 3346 case SHT_DYNSYM:
252b5132 3347 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3348 break;
3349
3350 case SHT_DYNAMIC:
252b5132 3351 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3352 break;
3353
3354 case SHT_RELA:
3355 if (get_elf_backend_data (abfd)->may_use_rela_p)
3356 this_hdr->sh_entsize = bed->s->sizeof_rela;
3357 break;
3358
3359 case SHT_REL:
3360 if (get_elf_backend_data (abfd)->may_use_rel_p)
3361 this_hdr->sh_entsize = bed->s->sizeof_rel;
3362 break;
3363
3364 case SHT_GNU_versym:
252b5132 3365 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3366 break;
3367
3368 case SHT_GNU_verdef:
252b5132
RH
3369 this_hdr->sh_entsize = 0;
3370 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3371 cverdefs. The linker will set cverdefs, but sh_info will be
3372 zero. */
252b5132
RH
3373 if (this_hdr->sh_info == 0)
3374 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3375 else
3376 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3377 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3378 break;
3379
3380 case SHT_GNU_verneed:
252b5132
RH
3381 this_hdr->sh_entsize = 0;
3382 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3383 cverrefs. The linker will set cverrefs, but sh_info will be
3384 zero. */
252b5132
RH
3385 if (this_hdr->sh_info == 0)
3386 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3387 else
3388 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3389 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3390 break;
3391
3392 case SHT_GROUP:
1783205a 3393 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3394 break;
fdc90cb4
JJ
3395
3396 case SHT_GNU_HASH:
3397 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3398 break;
dbb410c3 3399 }
252b5132
RH
3400
3401 if ((asect->flags & SEC_ALLOC) != 0)
3402 this_hdr->sh_flags |= SHF_ALLOC;
3403 if ((asect->flags & SEC_READONLY) == 0)
3404 this_hdr->sh_flags |= SHF_WRITE;
3405 if ((asect->flags & SEC_CODE) != 0)
3406 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3407 if ((asect->flags & SEC_MERGE) != 0)
3408 {
3409 this_hdr->sh_flags |= SHF_MERGE;
3410 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3411 }
84865015
NC
3412 if ((asect->flags & SEC_STRINGS) != 0)
3413 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3414 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3415 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3416 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3417 {
3418 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3419 if (asect->size == 0
3420 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3421 {
3a800eb9 3422 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3423
704afa60 3424 this_hdr->sh_size = 0;
3a800eb9
AM
3425 if (o != NULL)
3426 {
704afa60 3427 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3428 if (this_hdr->sh_size != 0)
3429 this_hdr->sh_type = SHT_NOBITS;
3430 }
704afa60
JJ
3431 }
3432 }
18ae9cc1
L
3433 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3434 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3435
d4730f92
BS
3436 /* If the section has relocs, set up a section header for the
3437 SHT_REL[A] section. If two relocation sections are required for
3438 this section, it is up to the processor-specific back-end to
3439 create the other. */
3440 if ((asect->flags & SEC_RELOC) != 0)
3441 {
3442 /* When doing a relocatable link, create both REL and RELA sections if
3443 needed. */
3444 if (arg->link_info
3445 /* Do the normal setup if we wouldn't create any sections here. */
3446 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3447 && (bfd_link_relocatable (arg->link_info)
3448 || arg->link_info->emitrelocations))
d4730f92
BS
3449 {
3450 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3451 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
0a1b45a2 3452 false, delay_st_name_p))
d4730f92 3453 {
0a1b45a2 3454 arg->failed = true;
d4730f92
BS
3455 return;
3456 }
3457 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3458 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
0a1b45a2 3459 true, delay_st_name_p))
d4730f92 3460 {
0a1b45a2 3461 arg->failed = true;
d4730f92
BS
3462 return;
3463 }
3464 }
3465 else if (!_bfd_elf_init_reloc_shdr (abfd,
3466 (asect->use_rela_p
3467 ? &esd->rela : &esd->rel),
f6fe1ccd 3468 name,
3e19fb8f
L
3469 asect->use_rela_p,
3470 delay_st_name_p))
db4677b8 3471 {
0a1b45a2 3472 arg->failed = true;
db4677b8
AM
3473 return;
3474 }
d4730f92
BS
3475 }
3476
252b5132 3477 /* Check for processor-specific section types. */
0414f35b 3478 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3479 if (bed->elf_backend_fake_sections
3480 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8 3481 {
0a1b45a2 3482 arg->failed = true;
db4677b8
AM
3483 return;
3484 }
252b5132 3485
42bb2e33 3486 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3487 {
3488 /* Don't change the header type from NOBITS if we are being
42bb2e33 3489 called for objcopy --only-keep-debug. */
0414f35b
AM
3490 this_hdr->sh_type = sh_type;
3491 }
252b5132
RH
3492}
3493
bcacc0f5
AM
3494/* Fill in the contents of a SHT_GROUP section. Called from
3495 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3496 when ELF targets use the generic linker, ld. Called for ld -r
3497 from bfd_elf_final_link. */
dbb410c3 3498
1126897b 3499void
217aa764 3500bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3501{
0a1b45a2 3502 bool *failedptr = (bool *) failedptrarg;
9dce4196 3503 asection *elt, *first;
dbb410c3 3504 unsigned char *loc;
0a1b45a2 3505 bool gas;
dbb410c3 3506
7e4111ad
L
3507 /* Ignore linker created group section. See elfNN_ia64_object_p in
3508 elfxx-ia64.c. */
ce5aecf8
AM
3509 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3510 || sec->size == 0
dbb410c3
AM
3511 || *failedptr)
3512 return;
3513
bcacc0f5
AM
3514 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3515 {
3516 unsigned long symindx = 0;
3517
3518 /* elf_group_id will have been set up by objcopy and the
3519 generic linker. */
3520 if (elf_group_id (sec) != NULL)
3521 symindx = elf_group_id (sec)->udata.i;
1126897b 3522
bcacc0f5
AM
3523 if (symindx == 0)
3524 {
3525 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3526 elf_section_syms.
3527 PR 25699: A corrupt input file could contain bogus group info. */
3528 if (elf_section_syms (abfd) == NULL)
3529 {
0a1b45a2 3530 *failedptr = true;
6a541707
NC
3531 return;
3532 }
bcacc0f5
AM
3533 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3534 }
3535 elf_section_data (sec)->this_hdr.sh_info = symindx;
3536 }
3537 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3538 {
bcacc0f5
AM
3539 /* The ELF backend linker sets sh_info to -2 when the group
3540 signature symbol is global, and thus the index can't be
3541 set until all local symbols are output. */
53720c49
AM
3542 asection *igroup;
3543 struct bfd_elf_section_data *sec_data;
3544 unsigned long symndx;
3545 unsigned long extsymoff;
bcacc0f5
AM
3546 struct elf_link_hash_entry *h;
3547
53720c49
AM
3548 /* The point of this little dance to the first SHF_GROUP section
3549 then back to the SHT_GROUP section is that this gets us to
3550 the SHT_GROUP in the input object. */
3551 igroup = elf_sec_group (elf_next_in_group (sec));
3552 sec_data = elf_section_data (igroup);
3553 symndx = sec_data->this_hdr.sh_info;
3554 extsymoff = 0;
bcacc0f5
AM
3555 if (!elf_bad_symtab (igroup->owner))
3556 {
3557 Elf_Internal_Shdr *symtab_hdr;
3558
3559 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3560 extsymoff = symtab_hdr->sh_info;
3561 }
3562 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3563 while (h->root.type == bfd_link_hash_indirect
3564 || h->root.type == bfd_link_hash_warning)
3565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3566
3567 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3568 }
dbb410c3 3569
1126897b 3570 /* The contents won't be allocated for "ld -r" or objcopy. */
0a1b45a2 3571 gas = true;
dbb410c3
AM
3572 if (sec->contents == NULL)
3573 {
0a1b45a2 3574 gas = false;
a50b1753 3575 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3576
3577 /* Arrange for the section to be written out. */
3578 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3579 if (sec->contents == NULL)
3580 {
0a1b45a2 3581 *failedptr = true;
dbb410c3
AM
3582 return;
3583 }
3584 }
3585
eea6121a 3586 loc = sec->contents + sec->size;
dbb410c3 3587
9dce4196
AM
3588 /* Get the pointer to the first section in the group that gas
3589 squirreled away here. objcopy arranges for this to be set to the
3590 start of the input section group. */
3591 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3592
3593 /* First element is a flag word. Rest of section is elf section
3594 indices for all the sections of the group. Write them backwards
3595 just to keep the group in the same order as given in .section
3596 directives, not that it matters. */
3597 while (elt != NULL)
3598 {
9dce4196 3599 asection *s;
9dce4196 3600
9dce4196 3601 s = elt;
415f38a6
AM
3602 if (!gas)
3603 s = s->output_section;
3604 if (s != NULL
3605 && !bfd_is_abs_section (s))
01e1a5bc 3606 {
db4677b8 3607 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3608 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3609
3610 if (elf_sec->rel.hdr != NULL
3611 && (gas
3612 || (input_elf_sec->rel.hdr != NULL
3613 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3614 {
28e07a05 3615 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3616 loc -= 4;
3617 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3618 }
28e07a05
AM
3619 if (elf_sec->rela.hdr != NULL
3620 && (gas
3621 || (input_elf_sec->rela.hdr != NULL
3622 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3623 {
28e07a05 3624 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3625 loc -= 4;
3626 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3627 }
01e1a5bc 3628 loc -= 4;
db4677b8 3629 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3630 }
945906ff 3631 elt = elf_next_in_group (elt);
9dce4196
AM
3632 if (elt == first)
3633 break;
dbb410c3
AM
3634 }
3635
7bdf4127
AB
3636 loc -= 4;
3637 BFD_ASSERT (loc == sec->contents);
dbb410c3 3638
9dce4196 3639 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3640}
3641
bce964aa
AM
3642/* Given NAME, the name of a relocation section stripped of its
3643 .rel/.rela prefix, return the section in ABFD to which the
3644 relocations apply. */
bd53a53a
L
3645
3646asection *
bce964aa
AM
3647_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3648{
3649 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3650 section likely apply to .got.plt or .got section. */
3651 if (get_elf_backend_data (abfd)->want_got_plt
3652 && strcmp (name, ".plt") == 0)
3653 {
3654 asection *sec;
3655
3656 name = ".got.plt";
3657 sec = bfd_get_section_by_name (abfd, name);
3658 if (sec != NULL)
3659 return sec;
3660 name = ".got";
3661 }
3662
3663 return bfd_get_section_by_name (abfd, name);
3664}
3665
3666/* Return the section to which RELOC_SEC applies. */
3667
3668static asection *
3669elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3670{
3671 const char *name;
3672 unsigned int type;
3673 bfd *abfd;
bce964aa 3674 const struct elf_backend_data *bed;
bd53a53a
L
3675
3676 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3677 if (type != SHT_REL && type != SHT_RELA)
3678 return NULL;
3679
3680 /* We look up the section the relocs apply to by name. */
3681 name = reloc_sec->name;
3f3328b8 3682 if (!startswith (name, ".rel"))
bce964aa
AM
3683 return NULL;
3684 name += 4;
3685 if (type == SHT_RELA && *name++ != 'a')
3686 return NULL;
bd53a53a 3687
bd53a53a 3688 abfd = reloc_sec->owner;
bce964aa
AM
3689 bed = get_elf_backend_data (abfd);
3690 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3691}
3692
252b5132
RH
3693/* Assign all ELF section numbers. The dummy first section is handled here
3694 too. The link/info pointers for the standard section types are filled
67411cbf
AM
3695 in here too, while we're at it. LINK_INFO will be 0 when arriving
3696 here for objcopy, and when using the generic ELF linker. */
252b5132 3697
0a1b45a2 3698static bool
da9f89d4 3699assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3700{
3701 struct elf_obj_tdata *t = elf_tdata (abfd);
3702 asection *sec;
3e19fb8f 3703 unsigned int section_number;
252b5132 3704 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3705 struct bfd_elf_section_data *d;
0a1b45a2 3706 bool need_symtab;
446f7ed5 3707 size_t amt;
252b5132
RH
3708
3709 section_number = 1;
3710
2b0f7ef9
JJ
3711 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3712
da9f89d4 3713 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3714 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3715 {
ef53be89 3716 size_t reloc_count = 0;
14f2c699 3717
da9f89d4 3718 /* Put SHT_GROUP sections first. */
04dd1667 3719 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3720 {
5daa8fe7 3721 d = elf_section_data (sec);
da9f89d4
L
3722
3723 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3724 {
5daa8fe7 3725 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3726 {
3727 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3728 bfd_section_list_remove (abfd, sec);
da9f89d4 3729 abfd->section_count--;
da9f89d4 3730 }
08a40648 3731 else
4fbb74a6 3732 d->this_idx = section_number++;
da9f89d4 3733 }
14f2c699
L
3734
3735 /* Count relocations. */
3736 reloc_count += sec->reloc_count;
47cc2cf5 3737 }
14f2c699
L
3738
3739 /* Clear HAS_RELOC if there are no relocations. */
3740 if (reloc_count == 0)
3741 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3742 }
3743
3744 for (sec = abfd->sections; sec; sec = sec->next)
3745 {
3746 d = elf_section_data (sec);
3747
3748 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3749 d->this_idx = section_number++;
3e19fb8f
L
3750 if (d->this_hdr.sh_name != (unsigned int) -1)
3751 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3752 if (d->rel.hdr)
2b0f7ef9 3753 {
d4730f92 3754 d->rel.idx = section_number++;
3e19fb8f
L
3755 if (d->rel.hdr->sh_name != (unsigned int) -1)
3756 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3757 }
d4730f92
BS
3758 else
3759 d->rel.idx = 0;
23bc299b 3760
d4730f92 3761 if (d->rela.hdr)
2b0f7ef9 3762 {
d4730f92 3763 d->rela.idx = section_number++;
3e19fb8f
L
3764 if (d->rela.hdr->sh_name != (unsigned int) -1)
3765 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3766 }
23bc299b 3767 else
d4730f92 3768 d->rela.idx = 0;
252b5132
RH
3769 }
3770
3516e984
L
3771 need_symtab = (bfd_get_symcount (abfd) > 0
3772 || (link_info == NULL
3773 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3774 == HAS_RELOC)));
3775 if (need_symtab)
252b5132 3776 {
12bd6957 3777 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3778 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3779 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3780 {
7a6e0d89 3781 elf_section_list *entry;
6a40cf0c
NC
3782
3783 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3784
7a6e0d89 3785 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3786 entry->ndx = section_number++;
3787 elf_symtab_shndx_list (abfd) = entry;
3788 entry->hdr.sh_name
9ad5cbcf 3789 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3790 ".symtab_shndx", false);
6a40cf0c 3791 if (entry->hdr.sh_name == (unsigned int) -1)
0a1b45a2 3792 return false;
9ad5cbcf 3793 }
12bd6957 3794 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3795 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3796 }
3797
dd905818
NC
3798 elf_shstrtab_sec (abfd) = section_number++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3800 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3801
1c52a645
L
3802 if (section_number >= SHN_LORESERVE)
3803 {
695344c0 3804 /* xgettext:c-format */
871b3ab2 3805 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645 3806 abfd, section_number);
0a1b45a2 3807 return false;
1c52a645
L
3808 }
3809
9ad5cbcf 3810 elf_numsections (abfd) = section_number;
252b5132
RH
3811 elf_elfheader (abfd)->e_shnum = section_number;
3812
3813 /* Set up the list of section header pointers, in agreement with the
3814 indices. */
446f7ed5
AM
3815 amt = section_number * sizeof (Elf_Internal_Shdr *);
3816 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3817 if (i_shdrp == NULL)
0a1b45a2 3818 return false;
252b5132 3819
a50b1753 3820 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3821 sizeof (Elf_Internal_Shdr));
252b5132
RH
3822 if (i_shdrp[0] == NULL)
3823 {
3824 bfd_release (abfd, i_shdrp);
0a1b45a2 3825 return false;
252b5132 3826 }
252b5132
RH
3827
3828 elf_elfsections (abfd) = i_shdrp;
3829
12bd6957 3830 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3831 if (need_symtab)
252b5132 3832 {
12bd6957 3833 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3834 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3835 {
6a40cf0c
NC
3836 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3837 BFD_ASSERT (entry != NULL);
3838 i_shdrp[entry->ndx] = & entry->hdr;
3839 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3840 }
12bd6957
AM
3841 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3842 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3843 }
38ce5b11 3844
252b5132
RH
3845 for (sec = abfd->sections; sec; sec = sec->next)
3846 {
252b5132 3847 asection *s;
252b5132 3848
91d6fa6a
NC
3849 d = elf_section_data (sec);
3850
252b5132 3851 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3852 if (d->rel.idx != 0)
3853 i_shdrp[d->rel.idx] = d->rel.hdr;
3854 if (d->rela.idx != 0)
3855 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3856
3857 /* Fill in the sh_link and sh_info fields while we're at it. */
3858
3859 /* sh_link of a reloc section is the section index of the symbol
3860 table. sh_info is the section index of the section to which
3861 the relocation entries apply. */
d4730f92 3862 if (d->rel.idx != 0)
252b5132 3863 {
12bd6957 3864 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3865 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3866 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3867 }
d4730f92 3868 if (d->rela.idx != 0)
23bc299b 3869 {
12bd6957 3870 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3871 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3872 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3873 }
252b5132 3874
38ce5b11
L
3875 /* We need to set up sh_link for SHF_LINK_ORDER. */
3876 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3877 {
3878 s = elf_linked_to_section (sec);
b71702f1
NC
3879 /* We can now have a NULL linked section pointer.
3880 This happens when the sh_link field is 0, which is done
3881 when a linked to section is discarded but the linking
3882 section has been retained for some reason. */
38ce5b11 3883 if (s)
38ce5b11 3884 {
67411cbf
AM
3885 /* Check discarded linkonce section. */
3886 if (discarded_section (s))
38ce5b11 3887 {
67411cbf
AM
3888 asection *kept;
3889 _bfd_error_handler
3890 /* xgettext:c-format */
3891 (_("%pB: sh_link of section `%pA' points to"
3892 " discarded section `%pA' of `%pB'"),
3893 abfd, d->this_hdr.bfd_section, s, s->owner);
3894 /* Point to the kept section if it has the same
3895 size as the discarded one. */
3896 kept = _bfd_elf_check_kept_section (s, link_info);
3897 if (kept == NULL)
f2876037 3898 {
f2876037 3899 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3900 return false;
f2876037 3901 }
67411cbf
AM
3902 s = kept;
3903 }
3904 /* Handle objcopy. */
3905 else if (s->output_section == NULL)
3906 {
3907 _bfd_error_handler
3908 /* xgettext:c-format */
3909 (_("%pB: sh_link of section `%pA' points to"
3910 " removed section `%pA' of `%pB'"),
3911 abfd, d->this_hdr.bfd_section, s, s->owner);
3912 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3913 return false;
f2876037 3914 }
67411cbf 3915 s = s->output_section;
ccd2ec6a
L
3916 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3917 }
38ce5b11
L
3918 }
3919
252b5132
RH
3920 switch (d->this_hdr.sh_type)
3921 {
3922 case SHT_REL:
3923 case SHT_RELA:
3924 /* A reloc section which we are treating as a normal BFD
3925 section. sh_link is the section index of the symbol
3926 table. sh_info is the section index of the section to
3927 which the relocation entries apply. We assume that an
3928 allocated reloc section uses the dynamic symbol table.
3929 FIXME: How can we be sure? */
3930 s = bfd_get_section_by_name (abfd, ".dynsym");
3931 if (s != NULL)
3932 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3933
bce964aa 3934 s = elf_get_reloc_section (sec);
252b5132 3935 if (s != NULL)
9ef5d938
L
3936 {
3937 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3938 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3939 }
252b5132
RH
3940 break;
3941
3942 case SHT_STRTAB:
3943 /* We assume that a section named .stab*str is a stabs
3944 string section. We look for a section with the same name
3945 but without the trailing ``str'', and set its sh_link
3946 field to point to this section. */
08dedd66 3947 if (startswith (sec->name, ".stab")
252b5132
RH
3948 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3949 {
3950 size_t len;
3951 char *alc;
3952
3953 len = strlen (sec->name);
a50b1753 3954 alc = (char *) bfd_malloc (len - 2);
252b5132 3955 if (alc == NULL)
0a1b45a2 3956 return false;
d4c88bbb 3957 memcpy (alc, sec->name, len - 3);
252b5132
RH
3958 alc[len - 3] = '\0';
3959 s = bfd_get_section_by_name (abfd, alc);
3960 free (alc);
3961 if (s != NULL)
3962 {
3963 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3964
3965 /* This is a .stab section. */
34ca5531 3966 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3967 }
3968 }
3969 break;
3970
3971 case SHT_DYNAMIC:
3972 case SHT_DYNSYM:
3973 case SHT_GNU_verneed:
3974 case SHT_GNU_verdef:
3975 /* sh_link is the section header index of the string table
3976 used for the dynamic entries, or the symbol table, or the
3977 version strings. */
3978 s = bfd_get_section_by_name (abfd, ".dynstr");
3979 if (s != NULL)
3980 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3981 break;
3982
7f1204bb
JJ
3983 case SHT_GNU_LIBLIST:
3984 /* sh_link is the section header index of the prelink library
08a40648
AM
3985 list used for the dynamic entries, or the symbol table, or
3986 the version strings. */
7f1204bb
JJ
3987 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3988 ? ".dynstr" : ".gnu.libstr");
3989 if (s != NULL)
3990 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3991 break;
3992
252b5132 3993 case SHT_HASH:
fdc90cb4 3994 case SHT_GNU_HASH:
252b5132
RH
3995 case SHT_GNU_versym:
3996 /* sh_link is the section header index of the symbol table
3997 this hash table or version table is for. */
3998 s = bfd_get_section_by_name (abfd, ".dynsym");
3999 if (s != NULL)
4000 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4001 break;
dbb410c3
AM
4002
4003 case SHT_GROUP:
12bd6957 4004 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4005 }
4006 }
4007
3e19fb8f
L
4008 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4009 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4010 debug section name from .debug_* to .zdebug_* if needed. */
4011
0a1b45a2 4012 return true;
252b5132
RH
4013}
4014
0a1b45a2 4015static bool
217aa764 4016sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4017{
4018 /* If the backend has a special mapping, use it. */
9c5bfbb7 4019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4020 if (bed->elf_backend_sym_is_global)
4021 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4022
e47bf690 4023 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4024 || bfd_is_und_section (bfd_asymbol_section (sym))
4025 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4026}
4027
76359541
TP
4028/* Filter global symbols of ABFD to include in the import library. All
4029 SYMCOUNT symbols of ABFD can be examined from their pointers in
4030 SYMS. Pointers of symbols to keep should be stored contiguously at
4031 the beginning of that array.
4032
4033 Returns the number of symbols to keep. */
4034
4035unsigned int
4036_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4037 asymbol **syms, long symcount)
4038{
4039 long src_count, dst_count = 0;
4040
4041 for (src_count = 0; src_count < symcount; src_count++)
4042 {
4043 asymbol *sym = syms[src_count];
4044 char *name = (char *) bfd_asymbol_name (sym);
4045 struct bfd_link_hash_entry *h;
4046
4047 if (!sym_is_global (abfd, sym))
4048 continue;
4049
0a1b45a2 4050 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
5df1bc57
AM
4051 if (h == NULL)
4052 continue;
76359541
TP
4053 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4054 continue;
76359541
TP
4055 if (h->linker_def || h->ldscript_def)
4056 continue;
4057
4058 syms[dst_count++] = sym;
4059 }
4060
4061 syms[dst_count] = NULL;
4062
4063 return dst_count;
4064}
4065
5372391b 4066/* Don't output section symbols for sections that are not going to be
c6d8cab4 4067 output, that are duplicates or there is no BFD section. */
5372391b 4068
0a1b45a2 4069static bool
5372391b
AM
4070ignore_section_sym (bfd *abfd, asymbol *sym)
4071{
c6d8cab4
L
4072 elf_symbol_type *type_ptr;
4073
db0c309f 4074 if (sym == NULL)
0a1b45a2 4075 return false;
db0c309f 4076
c6d8cab4 4077 if ((sym->flags & BSF_SECTION_SYM) == 0)
0a1b45a2 4078 return false;
c6d8cab4 4079
d1bcae83
L
4080 /* Ignore the section symbol if it isn't used. */
4081 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
0a1b45a2 4082 return true;
d1bcae83 4083
db0c309f 4084 if (sym->section == NULL)
0a1b45a2 4085 return true;
db0c309f 4086
c1229f84 4087 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4088 return ((type_ptr != NULL
4089 && type_ptr->internal_elf_sym.st_shndx != 0
4090 && bfd_is_abs_section (sym->section))
4091 || !(sym->section->owner == abfd
db0c309f
NC
4092 || (sym->section->output_section != NULL
4093 && sym->section->output_section->owner == abfd
2633a79c
AM
4094 && sym->section->output_offset == 0)
4095 || bfd_is_abs_section (sym->section)));
5372391b
AM
4096}
4097
2633a79c
AM
4098/* Map symbol from it's internal number to the external number, moving
4099 all local symbols to be at the head of the list. */
4100
0a1b45a2 4101static bool
12bd6957 4102elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4103{
dc810e39 4104 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4105 asymbol **syms = bfd_get_outsymbols (abfd);
4106 asymbol **sect_syms;
dc810e39
AM
4107 unsigned int num_locals = 0;
4108 unsigned int num_globals = 0;
4109 unsigned int num_locals2 = 0;
4110 unsigned int num_globals2 = 0;
7292b3ac 4111 unsigned int max_index = 0;
dc810e39 4112 unsigned int idx;
252b5132
RH
4113 asection *asect;
4114 asymbol **new_syms;
446f7ed5 4115 size_t amt;
252b5132
RH
4116
4117#ifdef DEBUG
4118 fprintf (stderr, "elf_map_symbols\n");
4119 fflush (stderr);
4120#endif
4121
252b5132
RH
4122 for (asect = abfd->sections; asect; asect = asect->next)
4123 {
4124 if (max_index < asect->index)
4125 max_index = asect->index;
4126 }
4127
4128 max_index++;
446f7ed5
AM
4129 amt = max_index * sizeof (asymbol *);
4130 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4131 if (sect_syms == NULL)
0a1b45a2 4132 return false;
252b5132 4133 elf_section_syms (abfd) = sect_syms;
4e89ac30 4134 elf_num_section_syms (abfd) = max_index;
252b5132 4135
079e9a2f
AM
4136 /* Init sect_syms entries for any section symbols we have already
4137 decided to output. */
252b5132
RH
4138 for (idx = 0; idx < symcount; idx++)
4139 {
dc810e39 4140 asymbol *sym = syms[idx];
c044fabd 4141
252b5132 4142 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4143 && sym->value == 0
2633a79c
AM
4144 && !ignore_section_sym (abfd, sym)
4145 && !bfd_is_abs_section (sym->section))
252b5132 4146 {
5372391b 4147 asection *sec = sym->section;
252b5132 4148
5372391b
AM
4149 if (sec->owner != abfd)
4150 sec = sec->output_section;
252b5132 4151
5372391b 4152 sect_syms[sec->index] = syms[idx];
252b5132
RH
4153 }
4154 }
4155
252b5132
RH
4156 /* Classify all of the symbols. */
4157 for (idx = 0; idx < symcount; idx++)
4158 {
2633a79c 4159 if (sym_is_global (abfd, syms[idx]))
252b5132 4160 num_globals++;
2633a79c
AM
4161 else if (!ignore_section_sym (abfd, syms[idx]))
4162 num_locals++;
252b5132 4163 }
079e9a2f 4164
5372391b 4165 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4166 sections will already have a section symbol in outsymbols, but
4167 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4168 at least in that case. */
252b5132
RH
4169 for (asect = abfd->sections; asect; asect = asect->next)
4170 {
d1bcae83
L
4171 asymbol *sym = asect->symbol;
4172 /* Don't include ignored section symbols. */
4173 if (!ignore_section_sym (abfd, sym)
4174 && sect_syms[asect->index] == NULL)
252b5132 4175 {
079e9a2f 4176 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4177 num_locals++;
4178 else
4179 num_globals++;
252b5132
RH
4180 }
4181 }
4182
4183 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4184 amt = (num_locals + num_globals) * sizeof (asymbol *);
4185 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4186 if (new_syms == NULL)
0a1b45a2 4187 return false;
252b5132
RH
4188
4189 for (idx = 0; idx < symcount; idx++)
4190 {
4191 asymbol *sym = syms[idx];
dc810e39 4192 unsigned int i;
252b5132 4193
2633a79c
AM
4194 if (sym_is_global (abfd, sym))
4195 i = num_locals + num_globals2++;
d1bcae83 4196 /* Don't include ignored section symbols. */
2633a79c 4197 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4198 i = num_locals2++;
4199 else
2633a79c 4200 continue;
252b5132
RH
4201 new_syms[i] = sym;
4202 sym->udata.i = i + 1;
4203 }
4204 for (asect = abfd->sections; asect; asect = asect->next)
4205 {
d1bcae83
L
4206 asymbol *sym = asect->symbol;
4207 if (!ignore_section_sym (abfd, sym)
4208 && sect_syms[asect->index] == NULL)
252b5132 4209 {
dc810e39 4210 unsigned int i;
252b5132 4211
079e9a2f 4212 sect_syms[asect->index] = sym;
252b5132
RH
4213 if (!sym_is_global (abfd, sym))
4214 i = num_locals2++;
4215 else
4216 i = num_locals + num_globals2++;
4217 new_syms[i] = sym;
4218 sym->udata.i = i + 1;
4219 }
4220 }
4221
4222 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4223
12bd6957 4224 *pnum_locals = num_locals;
0a1b45a2 4225 return true;
252b5132
RH
4226}
4227
4228/* Align to the maximum file alignment that could be required for any
4229 ELF data structure. */
4230
268b6b39 4231static inline file_ptr
217aa764 4232align_file_position (file_ptr off, int align)
252b5132
RH
4233{
4234 return (off + align - 1) & ~(align - 1);
4235}
4236
4237/* Assign a file position to a section, optionally aligning to the
4238 required section alignment. */
4239
217aa764
AM
4240file_ptr
4241_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4242 file_ptr offset,
0a1b45a2 4243 bool align)
252b5132 4244{
72de5009
AM
4245 if (align && i_shdrp->sh_addralign > 1)
4246 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4247 i_shdrp->sh_offset = offset;
4248 if (i_shdrp->bfd_section != NULL)
4249 i_shdrp->bfd_section->filepos = offset;
4250 if (i_shdrp->sh_type != SHT_NOBITS)
4251 offset += i_shdrp->sh_size;
4252 return offset;
4253}
4254
4255/* Compute the file positions we are going to put the sections at, and
4256 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4257 is not NULL, this is being called by the ELF backend linker. */
4258
0a1b45a2 4259bool
217aa764
AM
4260_bfd_elf_compute_section_file_positions (bfd *abfd,
4261 struct bfd_link_info *link_info)
252b5132 4262{
9c5bfbb7 4263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4264 struct fake_section_arg fsargs;
0a1b45a2 4265 bool failed;
ef10c3ac 4266 struct elf_strtab_hash *strtab = NULL;
252b5132 4267 Elf_Internal_Shdr *shstrtab_hdr;
0a1b45a2 4268 bool need_symtab;
252b5132
RH
4269
4270 if (abfd->output_has_begun)
0a1b45a2 4271 return true;
252b5132
RH
4272
4273 /* Do any elf backend specific processing first. */
4274 if (bed->elf_backend_begin_write_processing)
4275 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4276
ed7e9d0b 4277 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
0a1b45a2 4278 return false;
252b5132 4279
0a1b45a2 4280 fsargs.failed = false;
d4730f92
BS
4281 fsargs.link_info = link_info;
4282 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4283 if (fsargs.failed)
0a1b45a2 4284 return false;
252b5132 4285
da9f89d4 4286 if (!assign_section_numbers (abfd, link_info))
0a1b45a2 4287 return false;
252b5132
RH
4288
4289 /* The backend linker builds symbol table information itself. */
3516e984
L
4290 need_symtab = (link_info == NULL
4291 && (bfd_get_symcount (abfd) > 0
4292 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4293 == HAS_RELOC)));
4294 if (need_symtab)
252b5132
RH
4295 {
4296 /* Non-zero if doing a relocatable link. */
4297 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4298
3d16b64e 4299 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
0a1b45a2 4300 return false;
252b5132
RH
4301 }
4302
0a1b45a2 4303 failed = false;
1126897b 4304 if (link_info == NULL)
dbb410c3 4305 {
1126897b 4306 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4307 if (failed)
0a1b45a2 4308 return false;
dbb410c3
AM
4309 }
4310
252b5132 4311 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4312 /* sh_name was set in init_file_header. */
252b5132 4313 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4314 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4315 shstrtab_hdr->sh_addr = 0;
946748d5 4316 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4317 shstrtab_hdr->sh_entsize = 0;
4318 shstrtab_hdr->sh_link = 0;
4319 shstrtab_hdr->sh_info = 0;
3e19fb8f 4320 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4321 shstrtab_hdr->sh_addralign = 1;
4322
c84fca4d 4323 if (!assign_file_positions_except_relocs (abfd, link_info))
0a1b45a2 4324 return false;
252b5132 4325
3516e984 4326 if (need_symtab)
252b5132
RH
4327 {
4328 file_ptr off;
4329 Elf_Internal_Shdr *hdr;
4330
12bd6957 4331 off = elf_next_file_pos (abfd);
252b5132 4332
6a40cf0c 4333 hdr = & elf_symtab_hdr (abfd);
0a1b45a2 4334 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4335
6a40cf0c
NC
4336 if (elf_symtab_shndx_list (abfd) != NULL)
4337 {
4338 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4339 if (hdr->sh_size != 0)
0a1b45a2 4340 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6a40cf0c
NC
4341 /* FIXME: What about other symtab_shndx sections in the list ? */
4342 }
9ad5cbcf 4343
252b5132 4344 hdr = &elf_tdata (abfd)->strtab_hdr;
0a1b45a2 4345 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4346
12bd6957 4347 elf_next_file_pos (abfd) = off;
252b5132
RH
4348
4349 /* Now that we know where the .strtab section goes, write it
08a40648 4350 out. */
252b5132 4351 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4352 || ! _bfd_elf_strtab_emit (abfd, strtab))
0a1b45a2 4353 return false;
ef10c3ac 4354 _bfd_elf_strtab_free (strtab);
252b5132
RH
4355 }
4356
0a1b45a2 4357 abfd->output_has_begun = true;
252b5132 4358
0a1b45a2 4359 return true;
252b5132
RH
4360}
4361
8ded5a0f
AM
4362/* Make an initial estimate of the size of the program header. If we
4363 get the number wrong here, we'll redo section placement. */
4364
4365static bfd_size_type
4366get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4367{
4368 size_t segs;
4369 asection *s;
2b05f1b7 4370 const struct elf_backend_data *bed;
8ded5a0f
AM
4371
4372 /* Assume we will need exactly two PT_LOAD segments: one for text
4373 and one for data. */
4374 segs = 2;
4375
4376 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4377 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4378 {
4379 /* If we have a loadable interpreter section, we need a
4380 PT_INTERP segment. In this case, assume we also need a
4381 PT_PHDR segment, although that may not be true for all
4382 targets. */
e9a38e0f 4383 segs += 2;
8ded5a0f
AM
4384 }
4385
4386 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4387 {
4388 /* We need a PT_DYNAMIC segment. */
4389 ++segs;
f210dcff 4390 }
08a40648 4391
ceae84aa 4392 if (info != NULL && info->relro)
f210dcff
L
4393 {
4394 /* We need a PT_GNU_RELRO segment. */
4395 ++segs;
8ded5a0f
AM
4396 }
4397
12bd6957 4398 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4399 {
4400 /* We need a PT_GNU_EH_FRAME segment. */
4401 ++segs;
4402 }
4403
12bd6957 4404 if (elf_stack_flags (abfd))
8ded5a0f 4405 {
2b05f1b7
L
4406 /* We need a PT_GNU_STACK segment. */
4407 ++segs;
4408 }
94b11780 4409
0a59decb
L
4410 s = bfd_get_section_by_name (abfd,
4411 NOTE_GNU_PROPERTY_SECTION_NAME);
4412 if (s != NULL && s->size != 0)
4413 {
4414 /* We need a PT_GNU_PROPERTY segment. */
4415 ++segs;
4416 }
4417
2b05f1b7
L
4418 for (s = abfd->sections; s != NULL; s = s->next)
4419 {
8ded5a0f 4420 if ((s->flags & SEC_LOAD) != 0
23e463ed 4421 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4422 {
23e463ed 4423 unsigned int alignment_power;
8ded5a0f
AM
4424 /* We need a PT_NOTE segment. */
4425 ++segs;
23e463ed
L
4426 /* Try to create just one PT_NOTE segment for all adjacent
4427 loadable SHT_NOTE sections. gABI requires that within a
4428 PT_NOTE segment (and also inside of each SHT_NOTE section)
4429 each note should have the same alignment. So we check
4430 whether the sections are correctly aligned. */
4431 alignment_power = s->alignment_power;
4432 while (s->next != NULL
4433 && s->next->alignment_power == alignment_power
4434 && (s->next->flags & SEC_LOAD) != 0
4435 && elf_section_type (s->next) == SHT_NOTE)
4436 s = s->next;
8ded5a0f
AM
4437 }
4438 }
4439
4440 for (s = abfd->sections; s != NULL; s = s->next)
4441 {
4442 if (s->flags & SEC_THREAD_LOCAL)
4443 {
4444 /* We need a PT_TLS segment. */
4445 ++segs;
4446 break;
4447 }
4448 }
4449
2b05f1b7 4450 bed = get_elf_backend_data (abfd);
a91e1603 4451
df3a023b
AM
4452 if ((abfd->flags & D_PAGED) != 0
4453 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4454 {
4455 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4456 bfd_vma commonpagesize;
4457 unsigned int page_align_power;
4458
4459 if (info != NULL)
4460 commonpagesize = info->commonpagesize;
4461 else
4462 commonpagesize = bed->commonpagesize;
4463 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4464 for (s = abfd->sections; s != NULL; s = s->next)
4465 if (elf_section_flags (s) & SHF_GNU_MBIND)
4466 {
4467 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4468 {
4469 _bfd_error_handler
4470 /* xgettext:c-format */
4471 (_("%pB: GNU_MBIND section `%pA' has invalid "
4472 "sh_info field: %d"),
4473 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4474 continue;
4475 }
4476 /* Align mbind section to page size. */
4477 if (s->alignment_power < page_align_power)
4478 s->alignment_power = page_align_power;
4479 segs ++;
4480 }
4481 }
4482
4483 /* Let the backend count up any program headers it might need. */
4484 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4485 {
4486 int a;
4487
4488 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4489 if (a == -1)
4490 abort ();
4491 segs += a;
4492 }
4493
4494 return segs * bed->s->sizeof_phdr;
4495}
4496
2ea37f1c
NC
4497/* Find the segment that contains the output_section of section. */
4498
4499Elf_Internal_Phdr *
4500_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4501{
4502 struct elf_segment_map *m;
4503 Elf_Internal_Phdr *p;
4504
12bd6957 4505 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4506 m != NULL;
4507 m = m->next, p++)
4508 {
4509 int i;
4510
4511 for (i = m->count - 1; i >= 0; i--)
4512 if (m->sections[i] == section)
4513 return p;
4514 }
4515
4516 return NULL;
4517}
4518
252b5132
RH
4519/* Create a mapping from a set of sections to a program segment. */
4520
217aa764
AM
4521static struct elf_segment_map *
4522make_mapping (bfd *abfd,
4523 asection **sections,
4524 unsigned int from,
4525 unsigned int to,
0a1b45a2 4526 bool phdr)
252b5132
RH
4527{
4528 struct elf_segment_map *m;
4529 unsigned int i;
4530 asection **hdrpp;
986f0783 4531 size_t amt;
252b5132 4532
00bee008
AM
4533 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4534 amt += (to - from) * sizeof (asection *);
a50b1753 4535 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4536 if (m == NULL)
4537 return NULL;
4538 m->next = NULL;
4539 m->p_type = PT_LOAD;
4540 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4541 m->sections[i - from] = *hdrpp;
4542 m->count = to - from;
4543
4544 if (from == 0 && phdr)
4545 {
4546 /* Include the headers in the first PT_LOAD segment. */
4547 m->includes_filehdr = 1;
4548 m->includes_phdrs = 1;
4549 }
4550
4551 return m;
4552}
4553
229fcec5
MM
4554/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4555 on failure. */
4556
4557struct elf_segment_map *
4558_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4559{
4560 struct elf_segment_map *m;
4561
a50b1753 4562 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4563 sizeof (struct elf_segment_map));
229fcec5
MM
4564 if (m == NULL)
4565 return NULL;
4566 m->next = NULL;
4567 m->p_type = PT_DYNAMIC;
4568 m->count = 1;
4569 m->sections[0] = dynsec;
08a40648 4570
229fcec5
MM
4571 return m;
4572}
4573
8ded5a0f 4574/* Possibly add or remove segments from the segment map. */
252b5132 4575
0a1b45a2 4576static bool
3dea8fca
AM
4577elf_modify_segment_map (bfd *abfd,
4578 struct bfd_link_info *info,
0a1b45a2 4579 bool remove_empty_load)
252b5132 4580{
252e386e 4581 struct elf_segment_map **m;
8ded5a0f 4582 const struct elf_backend_data *bed;
252b5132 4583
8ded5a0f
AM
4584 /* The placement algorithm assumes that non allocated sections are
4585 not in PT_LOAD segments. We ensure this here by removing such
4586 sections from the segment map. We also remove excluded
252e386e
AM
4587 sections. Finally, any PT_LOAD segment without sections is
4588 removed. */
12bd6957 4589 m = &elf_seg_map (abfd);
252e386e 4590 while (*m)
8ded5a0f
AM
4591 {
4592 unsigned int i, new_count;
252b5132 4593
252e386e 4594 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4595 {
252e386e
AM
4596 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4597 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4598 || (*m)->p_type != PT_LOAD))
8ded5a0f 4599 {
252e386e
AM
4600 (*m)->sections[new_count] = (*m)->sections[i];
4601 new_count++;
8ded5a0f
AM
4602 }
4603 }
252e386e 4604 (*m)->count = new_count;
252b5132 4605
1a9ccd70
NC
4606 if (remove_empty_load
4607 && (*m)->p_type == PT_LOAD
4608 && (*m)->count == 0
4609 && !(*m)->includes_phdrs)
252e386e
AM
4610 *m = (*m)->next;
4611 else
4612 m = &(*m)->next;
8ded5a0f 4613 }
252b5132 4614
8ded5a0f
AM
4615 bed = get_elf_backend_data (abfd);
4616 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4617 {
252e386e 4618 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
0a1b45a2 4619 return false;
252b5132 4620 }
252b5132 4621
0a1b45a2 4622 return true;
8ded5a0f 4623}
252b5132 4624
dbc88fc1
AM
4625#define IS_TBSS(s) \
4626 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4627
8ded5a0f 4628/* Set up a mapping from BFD sections to program segments. */
252b5132 4629
0a1b45a2 4630bool
8ded5a0f
AM
4631_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4632{
4633 unsigned int count;
4634 struct elf_segment_map *m;
4635 asection **sections = NULL;
4636 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 4637 bool no_user_phdrs;
252b5132 4638
12bd6957 4639 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4640
4641 if (info != NULL)
4642 info->user_phdrs = !no_user_phdrs;
4643
3dea8fca 4644 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4645 {
8ded5a0f
AM
4646 asection *s;
4647 unsigned int i;
4648 struct elf_segment_map *mfirst;
4649 struct elf_segment_map **pm;
4650 asection *last_hdr;
4651 bfd_vma last_size;
00bee008 4652 unsigned int hdr_index;
8ded5a0f
AM
4653 bfd_vma maxpagesize;
4654 asection **hdrpp;
0a1b45a2
AM
4655 bool phdr_in_segment;
4656 bool writable;
4657 bool executable;
446f7ed5 4658 unsigned int tls_count = 0;
8ded5a0f 4659 asection *first_tls = NULL;
a91e1603 4660 asection *first_mbind = NULL;
8ded5a0f 4661 asection *dynsec, *eh_frame_hdr;
446f7ed5 4662 size_t amt;
66631823
CE
4663 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4664 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4665 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4666
8ded5a0f 4667 /* Select the allocated sections, and sort them. */
252b5132 4668
446f7ed5
AM
4669 amt = bfd_count_sections (abfd) * sizeof (asection *);
4670 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4671 if (sections == NULL)
252b5132 4672 goto error_return;
252b5132 4673
8d06853e
AM
4674 /* Calculate top address, avoiding undefined behaviour of shift
4675 left operator when shift count is equal to size of type
4676 being shifted. */
4677 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4678 addr_mask = (addr_mask << 1) + 1;
4679
8ded5a0f
AM
4680 i = 0;
4681 for (s = abfd->sections; s != NULL; s = s->next)
4682 {
4683 if ((s->flags & SEC_ALLOC) != 0)
4684 {
48db3297
AM
4685 /* target_index is unused until bfd_elf_final_link
4686 starts output of section symbols. Use it to make
4687 qsort stable. */
4688 s->target_index = i;
8ded5a0f
AM
4689 sections[i] = s;
4690 ++i;
8d06853e 4691 /* A wrapping section potentially clashes with header. */
66631823
CE
4692 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4693 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4694 }
4695 }
4696 BFD_ASSERT (i <= bfd_count_sections (abfd));
4697 count = i;
252b5132 4698
8ded5a0f 4699 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4700
64029e93
AM
4701 phdr_size = elf_program_header_size (abfd);
4702 if (phdr_size == (bfd_size_type) -1)
4703 phdr_size = get_program_header_size (abfd, info);
4704 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4705 /* phdr_size is compared to LMA values which are in bytes. */
4706 phdr_size /= opb;
c410035d
AM
4707 if (info != NULL)
4708 maxpagesize = info->maxpagesize;
4709 else
4710 maxpagesize = bed->maxpagesize;
64029e93
AM
4711 if (maxpagesize == 0)
4712 maxpagesize = 1;
4713 phdr_in_segment = info != NULL && info->load_phdrs;
4714 if (count != 0
4715 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4716 >= (phdr_size & (maxpagesize - 1))))
4717 /* For compatibility with old scripts that may not be using
4718 SIZEOF_HEADERS, add headers when it looks like space has
4719 been left for them. */
0a1b45a2 4720 phdr_in_segment = true;
252b5132 4721
64029e93 4722 /* Build the mapping. */
8ded5a0f
AM
4723 mfirst = NULL;
4724 pm = &mfirst;
252b5132 4725
8ded5a0f
AM
4726 /* If we have a .interp section, then create a PT_PHDR segment for
4727 the program headers and a PT_INTERP segment for the .interp
4728 section. */
4729 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4730 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4731 {
4732 amt = sizeof (struct elf_segment_map);
a50b1753 4733 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4734 if (m == NULL)
4735 goto error_return;
4736 m->next = NULL;
4737 m->p_type = PT_PHDR;
f882209d 4738 m->p_flags = PF_R;
8ded5a0f
AM
4739 m->p_flags_valid = 1;
4740 m->includes_phdrs = 1;
0a1b45a2 4741 phdr_in_segment = true;
8ded5a0f
AM
4742 *pm = m;
4743 pm = &m->next;
252b5132 4744
8ded5a0f 4745 amt = sizeof (struct elf_segment_map);
a50b1753 4746 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4747 if (m == NULL)
4748 goto error_return;
4749 m->next = NULL;
4750 m->p_type = PT_INTERP;
4751 m->count = 1;
4752 m->sections[0] = s;
4753
4754 *pm = m;
4755 pm = &m->next;
252b5132 4756 }
8ded5a0f
AM
4757
4758 /* Look through the sections. We put sections in the same program
4759 segment when the start of the second section can be placed within
4760 a few bytes of the end of the first section. */
4761 last_hdr = NULL;
4762 last_size = 0;
00bee008 4763 hdr_index = 0;
0a1b45a2
AM
4764 writable = false;
4765 executable = false;
8ded5a0f
AM
4766 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4767 if (dynsec != NULL
4768 && (dynsec->flags & SEC_LOAD) == 0)
4769 dynsec = NULL;
4770
64029e93 4771 if ((abfd->flags & D_PAGED) == 0)
0a1b45a2 4772 phdr_in_segment = false;
64029e93 4773
8ded5a0f
AM
4774 /* Deal with -Ttext or something similar such that the first section
4775 is not adjacent to the program headers. This is an
4776 approximation, since at this point we don't know exactly how many
4777 program headers we will need. */
64029e93 4778 if (phdr_in_segment && count > 0)
252b5132 4779 {
66631823 4780 bfd_vma phdr_lma; /* Bytes. */
0a1b45a2 4781 bool separate_phdr = false;
64029e93
AM
4782
4783 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4784 if (info != NULL
4785 && info->separate_code
4786 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4787 {
64029e93
AM
4788 /* If data sections should be separate from code and
4789 thus not executable, and the first section is
4790 executable then put the file and program headers in
4791 their own PT_LOAD. */
0a1b45a2 4792 separate_phdr = true;
64029e93
AM
4793 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4794 == (sections[0]->lma & addr_mask & -maxpagesize)))
4795 {
4796 /* The file and program headers are currently on the
4797 same page as the first section. Put them on the
4798 previous page if we can. */
4799 if (phdr_lma >= maxpagesize)
4800 phdr_lma -= maxpagesize;
4801 else
0a1b45a2 4802 separate_phdr = false;
64029e93
AM
4803 }
4804 }
4805 if ((sections[0]->lma & addr_mask) < phdr_lma
4806 || (sections[0]->lma & addr_mask) < phdr_size)
4807 /* If file and program headers would be placed at the end
4808 of memory then it's probably better to omit them. */
0a1b45a2 4809 phdr_in_segment = false;
64029e93
AM
4810 else if (phdr_lma < wrap_to)
4811 /* If a section wraps around to where we'll be placing
4812 file and program headers, then the headers will be
4813 overwritten. */
0a1b45a2 4814 phdr_in_segment = false;
64029e93
AM
4815 else if (separate_phdr)
4816 {
4817 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4818 if (m == NULL)
4819 goto error_return;
66631823 4820 m->p_paddr = phdr_lma * opb;
64029e93
AM
4821 m->p_vaddr_offset
4822 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4823 m->p_paddr_valid = 1;
4824 *pm = m;
4825 pm = &m->next;
0a1b45a2 4826 phdr_in_segment = false;
1a9ccd70 4827 }
252b5132
RH
4828 }
4829
8ded5a0f 4830 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4831 {
8ded5a0f 4832 asection *hdr;
0a1b45a2 4833 bool new_segment;
8ded5a0f
AM
4834
4835 hdr = *hdrpp;
4836
4837 /* See if this section and the last one will fit in the same
4838 segment. */
4839
4840 if (last_hdr == NULL)
4841 {
4842 /* If we don't have a segment yet, then we don't need a new
4843 one (we build the last one after this loop). */
0a1b45a2 4844 new_segment = false;
8ded5a0f
AM
4845 }
4846 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4847 {
4848 /* If this section has a different relation between the
4849 virtual address and the load address, then we need a new
4850 segment. */
0a1b45a2 4851 new_segment = true;
8ded5a0f 4852 }
b5599592
AM
4853 else if (hdr->lma < last_hdr->lma + last_size
4854 || last_hdr->lma + last_size < last_hdr->lma)
4855 {
4856 /* If this section has a load address that makes it overlap
4857 the previous section, then we need a new segment. */
0a1b45a2 4858 new_segment = true;
b5599592 4859 }
76cb3a89
AM
4860 else if ((abfd->flags & D_PAGED) != 0
4861 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4862 == (hdr->lma & -maxpagesize)))
4863 {
4864 /* If we are demand paged then we can't map two disk
4865 pages onto the same memory page. */
0a1b45a2 4866 new_segment = false;
76cb3a89 4867 }
39948a60
NC
4868 /* In the next test we have to be careful when last_hdr->lma is close
4869 to the end of the address space. If the aligned address wraps
4870 around to the start of the address space, then there are no more
4871 pages left in memory and it is OK to assume that the current
4872 section can be included in the current segment. */
76cb3a89
AM
4873 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4874 + maxpagesize > last_hdr->lma)
4875 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4876 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4877 {
4878 /* If putting this section in this segment would force us to
4879 skip a page in the segment, then we need a new segment. */
0a1b45a2 4880 new_segment = true;
8ded5a0f
AM
4881 }
4882 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4883 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4884 {
e5654c0f
AM
4885 /* We don't want to put a loaded section after a
4886 nonloaded (ie. bss style) section in the same segment
4887 as that will force the non-loaded section to be loaded.
76cb3a89 4888 Consider .tbss sections as loaded for this purpose. */
0a1b45a2 4889 new_segment = true;
8ded5a0f
AM
4890 }
4891 else if ((abfd->flags & D_PAGED) == 0)
4892 {
4893 /* If the file is not demand paged, which means that we
4894 don't require the sections to be correctly aligned in the
4895 file, then there is no other reason for a new segment. */
0a1b45a2 4896 new_segment = false;
8ded5a0f 4897 }
2888249f
L
4898 else if (info != NULL
4899 && info->separate_code
4900 && executable != ((hdr->flags & SEC_CODE) != 0))
4901 {
0a1b45a2 4902 new_segment = true;
2888249f 4903 }
8ded5a0f 4904 else if (! writable
76cb3a89 4905 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4906 {
4907 /* We don't want to put a writable section in a read only
76cb3a89 4908 segment. */
0a1b45a2 4909 new_segment = true;
8ded5a0f
AM
4910 }
4911 else
4912 {
4913 /* Otherwise, we can use the same segment. */
0a1b45a2 4914 new_segment = false;
8ded5a0f
AM
4915 }
4916
2889e75b 4917 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4918 if (last_hdr != NULL
4919 && info != NULL
4920 && info->callbacks->override_segment_assignment != NULL)
4921 new_segment
4922 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4923 last_hdr,
4924 new_segment);
2889e75b 4925
8ded5a0f
AM
4926 if (! new_segment)
4927 {
4928 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4929 writable = true;
2888249f 4930 if ((hdr->flags & SEC_CODE) != 0)
0a1b45a2 4931 executable = true;
8ded5a0f
AM
4932 last_hdr = hdr;
4933 /* .tbss sections effectively have zero size. */
502794d4 4934 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4935 continue;
4936 }
4937
4938 /* We need a new program segment. We must create a new program
00bee008 4939 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4940
00bee008 4941 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4942 if (m == NULL)
4943 goto error_return;
4944
4945 *pm = m;
4946 pm = &m->next;
4947
252b5132 4948 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4949 writable = true;
8ded5a0f 4950 else
0a1b45a2 4951 writable = false;
8ded5a0f 4952
2888249f 4953 if ((hdr->flags & SEC_CODE) == 0)
0a1b45a2 4954 executable = false;
2888249f 4955 else
0a1b45a2 4956 executable = true;
2888249f 4957
baaff79e
JJ
4958 last_hdr = hdr;
4959 /* .tbss sections effectively have zero size. */
502794d4 4960 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4961 hdr_index = i;
0a1b45a2 4962 phdr_in_segment = false;
252b5132
RH
4963 }
4964
86b2281f
AM
4965 /* Create a final PT_LOAD program segment, but not if it's just
4966 for .tbss. */
4967 if (last_hdr != NULL
00bee008 4968 && (i - hdr_index != 1
dbc88fc1 4969 || !IS_TBSS (last_hdr)))
8ded5a0f 4970 {
00bee008 4971 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4972 if (m == NULL)
4973 goto error_return;
252b5132 4974
8ded5a0f
AM
4975 *pm = m;
4976 pm = &m->next;
4977 }
252b5132 4978
8ded5a0f
AM
4979 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4980 if (dynsec != NULL)
4981 {
4982 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4983 if (m == NULL)
4984 goto error_return;
4985 *pm = m;
4986 pm = &m->next;
4987 }
252b5132 4988
23e463ed 4989 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4990 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4991 because if we link together nonloadable .note sections and
4992 loadable .note sections, we will generate two .note sections
23e463ed 4993 in the output file. */
8ded5a0f
AM
4994 for (s = abfd->sections; s != NULL; s = s->next)
4995 {
4996 if ((s->flags & SEC_LOAD) != 0
23e463ed 4997 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4998 {
1c5265b5 4999 asection *s2;
23e463ed 5000 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5001
5002 count = 1;
23e463ed
L
5003 for (s2 = s; s2->next != NULL; s2 = s2->next)
5004 {
5005 if (s2->next->alignment_power == alignment_power
5006 && (s2->next->flags & SEC_LOAD) != 0
5007 && elf_section_type (s2->next) == SHT_NOTE
66631823 5008 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5009 alignment_power)
5010 == s2->next->lma)
5011 count++;
5012 else
5013 break;
5014 }
00bee008
AM
5015 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5016 amt += count * sizeof (asection *);
a50b1753 5017 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5018 if (m == NULL)
5019 goto error_return;
5020 m->next = NULL;
5021 m->p_type = PT_NOTE;
1c5265b5
JJ
5022 m->count = count;
5023 while (count > 1)
5024 {
5025 m->sections[m->count - count--] = s;
5026 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5027 s = s->next;
5028 }
5029 m->sections[m->count - 1] = s;
5030 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5031 *pm = m;
5032 pm = &m->next;
5033 }
5034 if (s->flags & SEC_THREAD_LOCAL)
5035 {
5036 if (! tls_count)
5037 first_tls = s;
5038 tls_count++;
5039 }
a91e1603
L
5040 if (first_mbind == NULL
5041 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5042 first_mbind = s;
8ded5a0f 5043 }
252b5132 5044
8ded5a0f
AM
5045 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5046 if (tls_count > 0)
5047 {
00bee008
AM
5048 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5049 amt += tls_count * sizeof (asection *);
a50b1753 5050 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5051 if (m == NULL)
5052 goto error_return;
5053 m->next = NULL;
5054 m->p_type = PT_TLS;
5055 m->count = tls_count;
5056 /* Mandated PF_R. */
5057 m->p_flags = PF_R;
5058 m->p_flags_valid = 1;
d923cae0 5059 s = first_tls;
446f7ed5 5060 for (i = 0; i < tls_count; ++i)
8ded5a0f 5061 {
d923cae0
L
5062 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5063 {
5064 _bfd_error_handler
871b3ab2 5065 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5066 s = first_tls;
5067 i = 0;
446f7ed5 5068 while (i < tls_count)
d923cae0
L
5069 {
5070 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5071 {
871b3ab2 5072 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5073 i++;
5074 }
5075 else
871b3ab2 5076 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5077 s = s->next;
5078 }
5079 bfd_set_error (bfd_error_bad_value);
5080 goto error_return;
5081 }
5082 m->sections[i] = s;
5083 s = s->next;
8ded5a0f 5084 }
252b5132 5085
8ded5a0f
AM
5086 *pm = m;
5087 pm = &m->next;
5088 }
252b5132 5089
df3a023b
AM
5090 if (first_mbind
5091 && (abfd->flags & D_PAGED) != 0
5092 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5093 for (s = first_mbind; s != NULL; s = s->next)
5094 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5095 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5096 {
5097 /* Mandated PF_R. */
5098 unsigned long p_flags = PF_R;
5099 if ((s->flags & SEC_READONLY) == 0)
5100 p_flags |= PF_W;
5101 if ((s->flags & SEC_CODE) != 0)
5102 p_flags |= PF_X;
5103
5104 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5105 m = bfd_zalloc (abfd, amt);
5106 if (m == NULL)
5107 goto error_return;
5108 m->next = NULL;
5109 m->p_type = (PT_GNU_MBIND_LO
5110 + elf_section_data (s)->this_hdr.sh_info);
5111 m->count = 1;
5112 m->p_flags_valid = 1;
5113 m->sections[0] = s;
5114 m->p_flags = p_flags;
5115
5116 *pm = m;
5117 pm = &m->next;
5118 }
5119
0a59decb
L
5120 s = bfd_get_section_by_name (abfd,
5121 NOTE_GNU_PROPERTY_SECTION_NAME);
5122 if (s != NULL && s->size != 0)
5123 {
5124 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5125 m = bfd_zalloc (abfd, amt);
5126 if (m == NULL)
5127 goto error_return;
5128 m->next = NULL;
5129 m->p_type = PT_GNU_PROPERTY;
5130 m->count = 1;
5131 m->p_flags_valid = 1;
5132 m->sections[0] = s;
5133 m->p_flags = PF_R;
5134 *pm = m;
5135 pm = &m->next;
5136 }
5137
8ded5a0f
AM
5138 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5139 segment. */
12bd6957 5140 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5141 if (eh_frame_hdr != NULL
5142 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5143 {
dc810e39 5144 amt = sizeof (struct elf_segment_map);
a50b1753 5145 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5146 if (m == NULL)
5147 goto error_return;
5148 m->next = NULL;
8ded5a0f 5149 m->p_type = PT_GNU_EH_FRAME;
252b5132 5150 m->count = 1;
8ded5a0f 5151 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5152
5153 *pm = m;
5154 pm = &m->next;
5155 }
13ae64f3 5156
12bd6957 5157 if (elf_stack_flags (abfd))
13ae64f3 5158 {
8ded5a0f 5159 amt = sizeof (struct elf_segment_map);
a50b1753 5160 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5161 if (m == NULL)
5162 goto error_return;
5163 m->next = NULL;
2b05f1b7 5164 m->p_type = PT_GNU_STACK;
12bd6957 5165 m->p_flags = elf_stack_flags (abfd);
04c3a755 5166 m->p_align = bed->stack_align;
8ded5a0f 5167 m->p_flags_valid = 1;
04c3a755
NS
5168 m->p_align_valid = m->p_align != 0;
5169 if (info->stacksize > 0)
5170 {
5171 m->p_size = info->stacksize;
5172 m->p_size_valid = 1;
5173 }
252b5132 5174
8ded5a0f
AM
5175 *pm = m;
5176 pm = &m->next;
5177 }
65765700 5178
ceae84aa 5179 if (info != NULL && info->relro)
8ded5a0f 5180 {
f210dcff
L
5181 for (m = mfirst; m != NULL; m = m->next)
5182 {
3832a4d8
AM
5183 if (m->p_type == PT_LOAD
5184 && m->count != 0
5185 && m->sections[0]->vma >= info->relro_start
5186 && m->sections[0]->vma < info->relro_end)
f210dcff 5187 {
3832a4d8
AM
5188 i = m->count;
5189 while (--i != (unsigned) -1)
ec2e748a
NC
5190 {
5191 if (m->sections[i]->size > 0
5192 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5193 == (SEC_LOAD | SEC_HAS_CONTENTS))
5194 break;
5195 }
3832a4d8 5196
43a8475c 5197 if (i != (unsigned) -1)
f210dcff
L
5198 break;
5199 }
be01b344 5200 }
f210dcff
L
5201
5202 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5203 if (m != NULL)
5204 {
5205 amt = sizeof (struct elf_segment_map);
a50b1753 5206 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5207 if (m == NULL)
5208 goto error_return;
5209 m->next = NULL;
5210 m->p_type = PT_GNU_RELRO;
f210dcff
L
5211 *pm = m;
5212 pm = &m->next;
5213 }
8ded5a0f 5214 }
9ee5e499 5215
8ded5a0f 5216 free (sections);
12bd6957 5217 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5218 }
5219
3dea8fca 5220 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
0a1b45a2 5221 return false;
8c37241b 5222
12bd6957 5223 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5224 ++count;
12bd6957 5225 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5226
0a1b45a2 5227 return true;
252b5132
RH
5228
5229 error_return:
c9594989 5230 free (sections);
0a1b45a2 5231 return false;
252b5132
RH
5232}
5233
5234/* Sort sections by address. */
5235
5236static int
217aa764 5237elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5238{
5239 const asection *sec1 = *(const asection **) arg1;
5240 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5241 bfd_size_type size1, size2;
252b5132
RH
5242
5243 /* Sort by LMA first, since this is the address used to
5244 place the section into a segment. */
5245 if (sec1->lma < sec2->lma)
5246 return -1;
5247 else if (sec1->lma > sec2->lma)
5248 return 1;
5249
5250 /* Then sort by VMA. Normally the LMA and the VMA will be
5251 the same, and this will do nothing. */
5252 if (sec1->vma < sec2->vma)
5253 return -1;
5254 else if (sec1->vma > sec2->vma)
5255 return 1;
5256
5257 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5258
8d748d1d
AM
5259#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5260 && (x)->size != 0)
252b5132
RH
5261
5262 if (TOEND (sec1))
5263 {
48db3297 5264 if (!TOEND (sec2))
252b5132
RH
5265 return 1;
5266 }
00a7cdc5 5267 else if (TOEND (sec2))
252b5132
RH
5268 return -1;
5269
5270#undef TOEND
5271
00a7cdc5
NC
5272 /* Sort by size, to put zero sized sections
5273 before others at the same address. */
252b5132 5274
eea6121a
AM
5275 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5276 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5277
5278 if (size1 < size2)
252b5132 5279 return -1;
eecdbe52 5280 if (size1 > size2)
252b5132
RH
5281 return 1;
5282
5283 return sec1->target_index - sec2->target_index;
5284}
5285
30fe1832
AM
5286/* This qsort comparison functions sorts PT_LOAD segments first and
5287 by p_paddr, for assign_file_positions_for_load_sections. */
5288
5289static int
5290elf_sort_segments (const void *arg1, const void *arg2)
5291{
5292 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5293 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5294
5295 if (m1->p_type != m2->p_type)
5296 {
5297 if (m1->p_type == PT_NULL)
5298 return 1;
5299 if (m2->p_type == PT_NULL)
5300 return -1;
5301 return m1->p_type < m2->p_type ? -1 : 1;
5302 }
5303 if (m1->includes_filehdr != m2->includes_filehdr)
5304 return m1->includes_filehdr ? -1 : 1;
5305 if (m1->no_sort_lma != m2->no_sort_lma)
5306 return m1->no_sort_lma ? -1 : 1;
5307 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5308 {
4b3ecb3b 5309 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5310 lma1 = 0;
5311 if (m1->p_paddr_valid)
4b3ecb3b 5312 lma1 = m1->p_paddr;
30fe1832 5313 else if (m1->count != 0)
4b3ecb3b
AM
5314 {
5315 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5316 m1->sections[0]);
5317 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5318 }
30fe1832
AM
5319 lma2 = 0;
5320 if (m2->p_paddr_valid)
4b3ecb3b 5321 lma2 = m2->p_paddr;
30fe1832 5322 else if (m2->count != 0)
4b3ecb3b
AM
5323 {
5324 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5325 m2->sections[0]);
5326 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5327 }
30fe1832
AM
5328 if (lma1 != lma2)
5329 return lma1 < lma2 ? -1 : 1;
5330 }
5331 if (m1->idx != m2->idx)
5332 return m1->idx < m2->idx ? -1 : 1;
5333 return 0;
5334}
5335
340b6d91
AC
5336/* Ian Lance Taylor writes:
5337
5338 We shouldn't be using % with a negative signed number. That's just
5339 not good. We have to make sure either that the number is not
5340 negative, or that the number has an unsigned type. When the types
5341 are all the same size they wind up as unsigned. When file_ptr is a
5342 larger signed type, the arithmetic winds up as signed long long,
5343 which is wrong.
5344
5345 What we're trying to say here is something like ``increase OFF by
5346 the least amount that will cause it to be equal to the VMA modulo
5347 the page size.'' */
5348/* In other words, something like:
5349
5350 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5351 off_offset = off % bed->maxpagesize;
5352 if (vma_offset < off_offset)
5353 adjustment = vma_offset + bed->maxpagesize - off_offset;
5354 else
5355 adjustment = vma_offset - off_offset;
08a40648 5356
de194d85 5357 which can be collapsed into the expression below. */
340b6d91
AC
5358
5359static file_ptr
5360vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5361{
dc9155b2
NC
5362 /* PR binutils/16199: Handle an alignment of zero. */
5363 if (maxpagesize == 0)
5364 maxpagesize = 1;
340b6d91
AC
5365 return ((vma - off) % maxpagesize);
5366}
5367
6d33f217
L
5368static void
5369print_segment_map (const struct elf_segment_map *m)
5370{
5371 unsigned int j;
5372 const char *pt = get_segment_type (m->p_type);
5373 char buf[32];
5374
5375 if (pt == NULL)
5376 {
5377 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5378 sprintf (buf, "LOPROC+%7.7x",
5379 (unsigned int) (m->p_type - PT_LOPROC));
5380 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5381 sprintf (buf, "LOOS+%7.7x",
5382 (unsigned int) (m->p_type - PT_LOOS));
5383 else
5384 snprintf (buf, sizeof (buf), "%8.8x",
5385 (unsigned int) m->p_type);
5386 pt = buf;
5387 }
4a97a0e5 5388 fflush (stdout);
6d33f217
L
5389 fprintf (stderr, "%s:", pt);
5390 for (j = 0; j < m->count; j++)
5391 fprintf (stderr, " %s", m->sections [j]->name);
5392 putc ('\n',stderr);
4a97a0e5 5393 fflush (stderr);
6d33f217
L
5394}
5395
0a1b45a2 5396static bool
32812159
AM
5397write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5398{
5399 void *buf;
0a1b45a2 5400 bool ret;
32812159
AM
5401
5402 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
0a1b45a2 5403 return false;
32812159
AM
5404 buf = bfd_zmalloc (len);
5405 if (buf == NULL)
0a1b45a2 5406 return false;
32812159
AM
5407 ret = bfd_bwrite (buf, len, abfd) == len;
5408 free (buf);
5409 return ret;
5410}
5411
252b5132
RH
5412/* Assign file positions to the sections based on the mapping from
5413 sections to segments. This function also sets up some fields in
f3520d2f 5414 the file header. */
252b5132 5415
0a1b45a2 5416static bool
f3520d2f
AM
5417assign_file_positions_for_load_sections (bfd *abfd,
5418 struct bfd_link_info *link_info)
252b5132
RH
5419{
5420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5421 struct elf_segment_map *m;
30fe1832 5422 struct elf_segment_map *phdr_load_seg;
252b5132 5423 Elf_Internal_Phdr *phdrs;
252b5132 5424 Elf_Internal_Phdr *p;
502794d4 5425 file_ptr off; /* Octets. */
3f570048 5426 bfd_size_type maxpagesize;
30fe1832 5427 unsigned int alloc, actual;
0920dee7 5428 unsigned int i, j;
30fe1832 5429 struct elf_segment_map **sorted_seg_map;
502794d4 5430 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5431
e36284ab 5432 if (link_info == NULL
ceae84aa 5433 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
0a1b45a2 5434 return false;
252b5132 5435
8ded5a0f 5436 alloc = 0;
12bd6957 5437 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5438 m->idx = alloc++;
252b5132 5439
82f2dbf7
NC
5440 if (alloc)
5441 {
5442 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5443 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5444 }
5445 else
5446 {
5447 /* PR binutils/12467. */
5448 elf_elfheader (abfd)->e_phoff = 0;
5449 elf_elfheader (abfd)->e_phentsize = 0;
5450 }
d324f6d6 5451
8ded5a0f 5452 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5453
12bd6957 5454 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5455 {
5456 actual = alloc;
5457 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5458 }
8ded5a0f 5459 else
30fe1832
AM
5460 {
5461 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5462 BFD_ASSERT (elf_program_header_size (abfd)
5463 == actual * bed->s->sizeof_phdr);
5464 BFD_ASSERT (actual >= alloc);
5465 }
252b5132
RH
5466
5467 if (alloc == 0)
f3520d2f 5468 {
12bd6957 5469 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
0a1b45a2 5470 return true;
f3520d2f 5471 }
252b5132 5472
12bd6957 5473 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5474 see assign_file_positions_except_relocs, so make sure we have
5475 that amount allocated, with trailing space cleared.
12bd6957
AM
5476 The variable alloc contains the computed need, while
5477 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5478 layout.
5479 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5480 where the layout is forced to according to a larger size in the
5481 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5482 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5483 + alloc * sizeof (*sorted_seg_map)));
5484 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5485 elf_tdata (abfd)->phdr = phdrs;
252b5132 5486 if (phdrs == NULL)
0a1b45a2 5487 return false;
252b5132 5488
30fe1832 5489 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5490 {
30fe1832 5491 sorted_seg_map[j] = m;
252b5132 5492 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5493 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5494 not be done to the PT_NOTE section of a corefile, which may
5495 contain several pseudo-sections artificially created by bfd.
5496 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5497 if (m->count > 1
5498 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5499 && m->p_type == PT_NOTE))
48db3297
AM
5500 {
5501 for (i = 0; i < m->count; i++)
5502 m->sections[i]->target_index = i;
5503 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5504 elf_sort_sections);
5505 }
30fe1832
AM
5506 }
5507 if (alloc > 1)
5508 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5509 elf_sort_segments);
5510
5511 maxpagesize = 1;
5512 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5513 {
5514 if (link_info != NULL)
5515 maxpagesize = link_info->maxpagesize;
5516 else
5517 maxpagesize = bed->maxpagesize;
5518 }
30fe1832
AM
5519
5520 /* Sections must map to file offsets past the ELF file header. */
5521 off = bed->s->sizeof_ehdr;
5522 /* And if one of the PT_LOAD headers doesn't include the program
5523 headers then we'll be mapping program headers in the usual
5524 position after the ELF file header. */
5525 phdr_load_seg = NULL;
5526 for (j = 0; j < alloc; j++)
5527 {
5528 m = sorted_seg_map[j];
5529 if (m->p_type != PT_LOAD)
5530 break;
5531 if (m->includes_phdrs)
5532 {
5533 phdr_load_seg = m;
5534 break;
5535 }
5536 }
5537 if (phdr_load_seg == NULL)
5538 off += actual * bed->s->sizeof_phdr;
5539
5540 for (j = 0; j < alloc; j++)
5541 {
5542 asection **secpp;
502794d4 5543 bfd_vma off_adjust; /* Octets. */
0a1b45a2 5544 bool no_contents;
252b5132 5545
b301b248
AM
5546 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5547 number of sections with contents contributing to both p_filesz
5548 and p_memsz, followed by a number of sections with no contents
5549 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5550 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5551 m = sorted_seg_map[j];
5552 p = phdrs + m->idx;
252b5132 5553 p->p_type = m->p_type;
28a7f3e7 5554 p->p_flags = m->p_flags;
252b5132 5555
3f570048 5556 if (m->count == 0)
502794d4 5557 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5558 else
502794d4 5559 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5560
5561 if (m->p_paddr_valid)
5562 p->p_paddr = m->p_paddr;
5563 else if (m->count == 0)
5564 p->p_paddr = 0;
5565 else
502794d4 5566 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5567
5568 if (p->p_type == PT_LOAD
5569 && (abfd->flags & D_PAGED) != 0)
5570 {
5571 /* p_align in demand paged PT_LOAD segments effectively stores
5572 the maximum page size. When copying an executable with
5573 objcopy, we set m->p_align from the input file. Use this
5574 value for maxpagesize rather than bed->maxpagesize, which
5575 may be different. Note that we use maxpagesize for PT_TLS
5576 segment alignment later in this function, so we are relying
5577 on at least one PT_LOAD segment appearing before a PT_TLS
5578 segment. */
5579 if (m->p_align_valid)
5580 maxpagesize = m->p_align;
5581
5582 p->p_align = maxpagesize;
5583 }
3271a814
NS
5584 else if (m->p_align_valid)
5585 p->p_align = m->p_align;
e970b90a
DJ
5586 else if (m->count == 0)
5587 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5588
5589 if (m == phdr_load_seg)
5590 {
5591 if (!m->includes_filehdr)
5592 p->p_offset = off;
5593 off += actual * bed->s->sizeof_phdr;
5594 }
3f570048 5595
0a1b45a2 5596 no_contents = false;
bf988460 5597 off_adjust = 0;
252b5132 5598 if (p->p_type == PT_LOAD
b301b248 5599 && m->count > 0)
252b5132 5600 {
66631823 5601 bfd_size_type align; /* Bytes. */
a49e53ed 5602 unsigned int align_power = 0;
b301b248 5603
3271a814
NS
5604 if (m->p_align_valid)
5605 align = p->p_align;
5606 else
252b5132 5607 {
3271a814
NS
5608 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5609 {
5610 unsigned int secalign;
08a40648 5611
fd361982 5612 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5613 if (secalign > align_power)
5614 align_power = secalign;
5615 }
5616 align = (bfd_size_type) 1 << align_power;
5617 if (align < maxpagesize)
5618 align = maxpagesize;
b301b248 5619 }
252b5132 5620
02bf8d82
AM
5621 for (i = 0; i < m->count; i++)
5622 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5623 /* If we aren't making room for this section, then
5624 it must be SHT_NOBITS regardless of what we've
5625 set via struct bfd_elf_special_section. */
5626 elf_section_type (m->sections[i]) = SHT_NOBITS;
5627
bf988460 5628 /* Find out whether this segment contains any loadable
aea274d3 5629 sections. */
0a1b45a2 5630 no_contents = true;
aea274d3
AM
5631 for (i = 0; i < m->count; i++)
5632 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5633 {
0a1b45a2 5634 no_contents = false;
aea274d3
AM
5635 break;
5636 }
bf988460 5637
66631823 5638 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5639
5640 /* Broken hardware and/or kernel require that files do not
5641 map the same page with different permissions on some hppa
5642 processors. */
30fe1832
AM
5643 if (j != 0
5644 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5645 && bed->no_page_alias
5646 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5647 && ((off & -maxpagesize)
5648 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5649 off_adjust += maxpagesize;
bf988460
AM
5650 off += off_adjust;
5651 if (no_contents)
5652 {
5653 /* We shouldn't need to align the segment on disk since
5654 the segment doesn't need file space, but the gABI
5655 arguably requires the alignment and glibc ld.so
5656 checks it. So to comply with the alignment
5657 requirement but not waste file space, we adjust
5658 p_offset for just this segment. (OFF_ADJUST is
5659 subtracted from OFF later.) This may put p_offset
5660 past the end of file, but that shouldn't matter. */
5661 }
5662 else
5663 off_adjust = 0;
252b5132 5664 }
b1a6d0b1
NC
5665 /* Make sure the .dynamic section is the first section in the
5666 PT_DYNAMIC segment. */
5667 else if (p->p_type == PT_DYNAMIC
5668 && m->count > 1
5669 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5670 {
5671 _bfd_error_handler
871b3ab2 5672 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5673 " is not the .dynamic section"),
b301b248 5674 abfd);
b1a6d0b1 5675 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5676 return false;
b1a6d0b1 5677 }
3f001e84
JK
5678 /* Set the note section type to SHT_NOTE. */
5679 else if (p->p_type == PT_NOTE)
5680 for (i = 0; i < m->count; i++)
5681 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5682
252b5132
RH
5683 if (m->includes_filehdr)
5684 {
bf988460 5685 if (!m->p_flags_valid)
252b5132 5686 p->p_flags |= PF_R;
252b5132
RH
5687 p->p_filesz = bed->s->sizeof_ehdr;
5688 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5689 if (p->p_type == PT_LOAD)
252b5132 5690 {
30fe1832 5691 if (m->count > 0)
252b5132 5692 {
30fe1832
AM
5693 if (p->p_vaddr < (bfd_vma) off
5694 || (!m->p_paddr_valid
5695 && p->p_paddr < (bfd_vma) off))
5696 {
5697 _bfd_error_handler
5698 (_("%pB: not enough room for program headers,"
5699 " try linking with -N"),
5700 abfd);
5701 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5702 return false;
30fe1832
AM
5703 }
5704 p->p_vaddr -= off;
5705 if (!m->p_paddr_valid)
5706 p->p_paddr -= off;
252b5132 5707 }
30fe1832
AM
5708 }
5709 else if (sorted_seg_map[0]->includes_filehdr)
5710 {
5711 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5712 p->p_vaddr = filehdr->p_vaddr;
bf988460 5713 if (!m->p_paddr_valid)
30fe1832 5714 p->p_paddr = filehdr->p_paddr;
252b5132 5715 }
252b5132
RH
5716 }
5717
5718 if (m->includes_phdrs)
5719 {
bf988460 5720 if (!m->p_flags_valid)
252b5132 5721 p->p_flags |= PF_R;
30fe1832
AM
5722 p->p_filesz += actual * bed->s->sizeof_phdr;
5723 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5724 if (!m->includes_filehdr)
252b5132 5725 {
30fe1832 5726 if (p->p_type == PT_LOAD)
252b5132 5727 {
30fe1832
AM
5728 elf_elfheader (abfd)->e_phoff = p->p_offset;
5729 if (m->count > 0)
5730 {
5731 p->p_vaddr -= off - p->p_offset;
5732 if (!m->p_paddr_valid)
5733 p->p_paddr -= off - p->p_offset;
5734 }
5735 }
5736 else if (phdr_load_seg != NULL)
5737 {
5738 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5739 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5740 if (phdr_load_seg->includes_filehdr)
5741 phdr_off = bed->s->sizeof_ehdr;
5742 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5743 if (!m->p_paddr_valid)
30fe1832
AM
5744 p->p_paddr = phdr->p_paddr + phdr_off;
5745 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5746 }
30fe1832
AM
5747 else
5748 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5749 }
252b5132
RH
5750 }
5751
5752 if (p->p_type == PT_LOAD
5753 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5754 {
bf988460 5755 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5756 {
5757 p->p_offset = off;
5758 if (no_contents)
67641dd3
AM
5759 {
5760 /* Put meaningless p_offset for PT_LOAD segments
5761 without file contents somewhere within the first
5762 page, in an attempt to not point past EOF. */
5763 bfd_size_type align = maxpagesize;
5764 if (align < p->p_align)
5765 align = p->p_align;
5766 if (align < 1)
5767 align = 1;
5768 p->p_offset = off % align;
5769 }
0bc3450e 5770 }
252b5132
RH
5771 else
5772 {
502794d4 5773 file_ptr adjust; /* Octets. */
252b5132
RH
5774
5775 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5776 if (!no_contents)
5777 p->p_filesz += adjust;
252b5132
RH
5778 p->p_memsz += adjust;
5779 }
5780 }
5781
1ea63fd2
AM
5782 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5783 maps. Set filepos for sections in PT_LOAD segments, and in
5784 core files, for sections in PT_NOTE segments.
5785 assign_file_positions_for_non_load_sections will set filepos
5786 for other sections and update p_filesz for other segments. */
252b5132
RH
5787 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5788 {
5789 asection *sec;
252b5132 5790 bfd_size_type align;
627b32bc 5791 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5792
5793 sec = *secpp;
02bf8d82 5794 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5795 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5796
88967714
AM
5797 if ((p->p_type == PT_LOAD
5798 || p->p_type == PT_TLS)
5799 && (this_hdr->sh_type != SHT_NOBITS
5800 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5801 && ((this_hdr->sh_flags & SHF_TLS) == 0
5802 || p->p_type == PT_TLS))))
252b5132 5803 {
502794d4
CE
5804 bfd_vma p_start = p->p_paddr; /* Octets. */
5805 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5806 bfd_vma s_start = sec->lma * opb; /* Octets. */
5807 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5808
a2d1e028
L
5809 if (adjust != 0
5810 && (s_start < p_end
5811 || p_end < p_start))
252b5132 5812 {
4eca0228 5813 _bfd_error_handler
695344c0 5814 /* xgettext:c-format */
2dcf00ce 5815 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5816 abfd, sec, (uint64_t) s_start / opb,
5817 (uint64_t) p_end / opb);
88967714 5818 adjust = 0;
502794d4 5819 sec->lma = p_end / opb;
1cfb7d1e 5820 }
3ac9b6c9 5821 p->p_memsz += adjust;
1cfb7d1e 5822
d16e3d2e 5823 if (p->p_type == PT_LOAD)
88967714 5824 {
d16e3d2e 5825 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5826 {
d16e3d2e 5827 off_adjust = 0;
30fe1832
AM
5828 if (p->p_filesz + adjust < p->p_memsz)
5829 {
5830 /* We have a PROGBITS section following NOBITS ones.
5831 Allocate file space for the NOBITS section(s) and
5832 zero it. */
5833 adjust = p->p_memsz - p->p_filesz;
5834 if (!write_zeros (abfd, off, adjust))
0a1b45a2 5835 return false;
30fe1832 5836 }
d16e3d2e
AM
5837 }
5838 /* We only adjust sh_offset in SHT_NOBITS sections
5839 as would seem proper for their address when the
5840 section is first in the segment. sh_offset
5841 doesn't really have any significance for
5842 SHT_NOBITS anyway, apart from a notional position
5843 relative to other sections. Historically we
5844 didn't bother with adjusting sh_offset and some
5845 programs depend on it not being adjusted. See
5846 pr12921 and pr25662. */
5847 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5848 {
30fe1832 5849 off += adjust;
d16e3d2e
AM
5850 if (this_hdr->sh_type == SHT_NOBITS)
5851 off_adjust += adjust;
32812159 5852 }
252b5132 5853 }
d16e3d2e
AM
5854 if (this_hdr->sh_type != SHT_NOBITS)
5855 p->p_filesz += adjust;
252b5132
RH
5856 }
5857
5858 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5859 {
b301b248
AM
5860 /* The section at i == 0 is the one that actually contains
5861 everything. */
4a938328
MS
5862 if (i == 0)
5863 {
627b32bc 5864 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5865 off += this_hdr->sh_size;
5866 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5867 p->p_memsz = 0;
5868 p->p_align = 1;
252b5132 5869 }
4a938328 5870 else
252b5132 5871 {
b301b248 5872 /* The rest are fake sections that shouldn't be written. */
252b5132 5873 sec->filepos = 0;
eea6121a 5874 sec->size = 0;
b301b248
AM
5875 sec->flags = 0;
5876 continue;
252b5132 5877 }
252b5132
RH
5878 }
5879 else
5880 {
1e951488 5881 if (p->p_type == PT_LOAD)
b301b248 5882 {
1e951488
AM
5883 this_hdr->sh_offset = sec->filepos = off;
5884 if (this_hdr->sh_type != SHT_NOBITS)
5885 off += this_hdr->sh_size;
5886 }
5887 else if (this_hdr->sh_type == SHT_NOBITS
5888 && (this_hdr->sh_flags & SHF_TLS) != 0
5889 && this_hdr->sh_offset == 0)
5890 {
5891 /* This is a .tbss section that didn't get a PT_LOAD.
5892 (See _bfd_elf_map_sections_to_segments "Create a
5893 final PT_LOAD".) Set sh_offset to the value it
5894 would have if we had created a zero p_filesz and
5895 p_memsz PT_LOAD header for the section. This
5896 also makes the PT_TLS header have the same
5897 p_offset value. */
5898 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5899 off, align);
5900 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5901 }
252b5132 5902
02bf8d82 5903 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5904 {
6a3cd2b4 5905 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5906 /* A load section without SHF_ALLOC is something like
5907 a note section in a PT_NOTE segment. These take
5908 file space but are not loaded into memory. */
5909 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5910 p->p_memsz += this_hdr->sh_size;
b301b248 5911 }
6a3cd2b4 5912 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5913 {
6a3cd2b4
AM
5914 if (p->p_type == PT_TLS)
5915 p->p_memsz += this_hdr->sh_size;
5916
5917 /* .tbss is special. It doesn't contribute to p_memsz of
5918 normal segments. */
5919 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5920 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5921 }
5922
b10a8ae0
L
5923 if (align > p->p_align
5924 && !m->p_align_valid
5925 && (p->p_type != PT_LOAD
5926 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5927 p->p_align = align;
5928 }
5929
bf988460 5930 if (!m->p_flags_valid)
252b5132
RH
5931 {
5932 p->p_flags |= PF_R;
02bf8d82 5933 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5934 p->p_flags |= PF_X;
02bf8d82 5935 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5936 p->p_flags |= PF_W;
5937 }
5938 }
43a8475c 5939
bf988460 5940 off -= off_adjust;
0920dee7 5941
30fe1832
AM
5942 /* PR ld/20815 - Check that the program header segment, if
5943 present, will be loaded into memory. */
5944 if (p->p_type == PT_PHDR
5945 && phdr_load_seg == NULL
5946 && !(bed->elf_backend_allow_non_load_phdr != NULL
5947 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5948 {
5949 /* The fix for this error is usually to edit the linker script being
5950 used and set up the program headers manually. Either that or
5951 leave room for the headers at the start of the SECTIONS. */
5952 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5953 " by LOAD segment"),
5954 abfd);
7b3c2715 5955 if (link_info == NULL)
0a1b45a2 5956 return false;
7b3c2715
AM
5957 /* Arrange for the linker to exit with an error, deleting
5958 the output file unless --noinhibit-exec is given. */
5959 link_info->callbacks->info ("%X");
30fe1832
AM
5960 }
5961
7c928300
AM
5962 /* Check that all sections are in a PT_LOAD segment.
5963 Don't check funky gdb generated core files. */
5964 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553 5965 {
0a1b45a2 5966 bool check_vma = true;
9a83a553
AM
5967
5968 for (i = 1; i < m->count; i++)
5969 if (m->sections[i]->vma == m->sections[i - 1]->vma
5970 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5971 ->this_hdr), p) != 0
5972 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5973 ->this_hdr), p) != 0)
0920dee7 5974 {
9a83a553 5975 /* Looks like we have overlays packed into the segment. */
0a1b45a2 5976 check_vma = false;
9a83a553 5977 break;
0920dee7 5978 }
9a83a553
AM
5979
5980 for (i = 0; i < m->count; i++)
5981 {
5982 Elf_Internal_Shdr *this_hdr;
5983 asection *sec;
5984
5985 sec = m->sections[i];
5986 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5987 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5988 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5989 {
4eca0228 5990 _bfd_error_handler
695344c0 5991 /* xgettext:c-format */
871b3ab2 5992 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5993 abfd, sec, j);
5994 print_segment_map (m);
5995 }
5996 }
5997 }
252b5132
RH
5998 }
5999
12bd6957 6000 elf_next_file_pos (abfd) = off;
30fe1832
AM
6001
6002 if (link_info != NULL
6003 && phdr_load_seg != NULL
6004 && phdr_load_seg->includes_filehdr)
6005 {
6006 /* There is a segment that contains both the file headers and the
6007 program headers, so provide a symbol __ehdr_start pointing there.
6008 A program can use this to examine itself robustly. */
6009
6010 struct elf_link_hash_entry *hash
6011 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
0a1b45a2 6012 false, false, true);
30fe1832
AM
6013 /* If the symbol was referenced and not defined, define it. */
6014 if (hash != NULL
6015 && (hash->root.type == bfd_link_hash_new
6016 || hash->root.type == bfd_link_hash_undefined
6017 || hash->root.type == bfd_link_hash_undefweak
6018 || hash->root.type == bfd_link_hash_common))
6019 {
6020 asection *s = NULL;
66631823 6021 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6022
6023 if (phdr_load_seg->count != 0)
6024 /* The segment contains sections, so use the first one. */
6025 s = phdr_load_seg->sections[0];
6026 else
6027 /* Use the first (i.e. lowest-addressed) section in any segment. */
6028 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6029 if (m->p_type == PT_LOAD && m->count != 0)
6030 {
6031 s = m->sections[0];
6032 break;
6033 }
6034
6035 if (s != NULL)
6036 {
6037 hash->root.u.def.value = filehdr_vaddr - s->vma;
6038 hash->root.u.def.section = s;
6039 }
6040 else
6041 {
6042 hash->root.u.def.value = filehdr_vaddr;
6043 hash->root.u.def.section = bfd_abs_section_ptr;
6044 }
6045
6046 hash->root.type = bfd_link_hash_defined;
6047 hash->def_regular = 1;
6048 hash->non_elf = 0;
6049 }
6050 }
6051
0a1b45a2 6052 return true;
f3520d2f
AM
6053}
6054
1faa385f
NC
6055/* Determine if a bfd is a debuginfo file. Unfortunately there
6056 is no defined method for detecting such files, so we have to
6057 use heuristics instead. */
6058
0a1b45a2 6059bool
1faa385f
NC
6060is_debuginfo_file (bfd *abfd)
6061{
6062 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
0a1b45a2 6063 return false;
1faa385f
NC
6064
6065 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6066 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6067 Elf_Internal_Shdr **headerp;
6068
6069 for (headerp = start_headers; headerp < end_headers; headerp ++)
6070 {
6071 Elf_Internal_Shdr *header = * headerp;
6072
6073 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6074 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6075 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6076 && header->sh_type != SHT_NOBITS
6077 && header->sh_type != SHT_NOTE)
0a1b45a2 6078 return false;
1faa385f
NC
6079 }
6080
0a1b45a2 6081 return true;
1faa385f
NC
6082}
6083
1ff6de03
NA
6084/* Assign file positions for the other sections, except for compressed debugging
6085 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f 6086
0a1b45a2 6087static bool
f3520d2f
AM
6088assign_file_positions_for_non_load_sections (bfd *abfd,
6089 struct bfd_link_info *link_info)
6090{
6091 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6092 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6093 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6094 Elf_Internal_Phdr *phdrs;
6095 Elf_Internal_Phdr *p;
6096 struct elf_segment_map *m;
f3520d2f 6097 file_ptr off;
66631823 6098 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6099 bfd_vma maxpagesize;
f3520d2f 6100
c410035d
AM
6101 if (link_info != NULL)
6102 maxpagesize = link_info->maxpagesize;
6103 else
6104 maxpagesize = bed->maxpagesize;
5c182d5f 6105 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6106 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6107 off = elf_next_file_pos (abfd);
e06efbf1 6108 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6109 {
5c182d5f
AM
6110 Elf_Internal_Shdr *hdr;
6111
6112 hdr = *hdrpp;
6113 if (hdr->bfd_section != NULL
252e386e
AM
6114 && (hdr->bfd_section->filepos != 0
6115 || (hdr->sh_type == SHT_NOBITS
6116 && hdr->contents == NULL)))
627b32bc 6117 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6118 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6119 {
1faa385f
NC
6120 if (hdr->sh_size != 0
6121 /* PR 24717 - debuginfo files are known to be not strictly
6122 compliant with the ELF standard. In particular they often
6123 have .note.gnu.property sections that are outside of any
6124 loadable segment. This is not a problem for such files,
6125 so do not warn about them. */
6126 && ! is_debuginfo_file (abfd))
4eca0228 6127 _bfd_error_handler
695344c0 6128 /* xgettext:c-format */
871b3ab2 6129 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6130 abfd,
6131 (hdr->bfd_section == NULL
6132 ? "*unknown*"
6133 : hdr->bfd_section->name));
3ba71138
L
6134 /* We don't need to page align empty sections. */
6135 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f 6136 off += vma_page_aligned_bias (hdr->sh_addr, off,
c410035d 6137 maxpagesize);
5c182d5f
AM
6138 else
6139 off += vma_page_aligned_bias (hdr->sh_addr, off,
6140 hdr->sh_addralign);
6141 off = _bfd_elf_assign_file_position_for_section (hdr, off,
0a1b45a2 6142 false);
5c182d5f
AM
6143 }
6144 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6145 && hdr->bfd_section == NULL)
1ff6de03
NA
6146 /* We don't know the offset of these sections yet: their size has
6147 not been decided. */
0ce398f1 6148 || (hdr->bfd_section != NULL
1ff6de03
NA
6149 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6150 || (bfd_section_is_ctf (hdr->bfd_section)
6151 && abfd->is_linker_output)))
12bd6957 6152 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6153 || (elf_symtab_shndx_list (abfd) != NULL
6154 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6155 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6156 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6157 hdr->sh_offset = -1;
6158 else
0a1b45a2 6159 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
5c182d5f 6160 }
30fe1832 6161 elf_next_file_pos (abfd) = off;
5c182d5f 6162
252b5132
RH
6163 /* Now that we have set the section file positions, we can set up
6164 the file positions for the non PT_LOAD segments. */
f3520d2f 6165 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6166 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6167 {
129af99f 6168 if (p->p_type == PT_GNU_RELRO)
252b5132 6169 {
66631823 6170 bfd_vma start, end; /* Bytes. */
0a1b45a2 6171 bool ok;
1ea63fd2 6172
129af99f 6173 if (link_info != NULL)
8c37241b 6174 {
129af99f 6175 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6176 in link_info. Note that there may be padding between
6177 relro_start and the first RELRO section. */
6178 start = link_info->relro_start;
6179 end = link_info->relro_end;
6180 }
6181 else if (m->count != 0)
6182 {
6183 if (!m->p_size_valid)
6184 abort ();
6185 start = m->sections[0]->vma;
66631823 6186 end = start + m->p_size / opb;
f2731e0c
AM
6187 }
6188 else
6189 {
6190 start = 0;
6191 end = 0;
6192 }
6193
0a1b45a2 6194 ok = false;
f2731e0c
AM
6195 if (start < end)
6196 {
6197 struct elf_segment_map *lm;
6198 const Elf_Internal_Phdr *lp;
6199 unsigned int i;
6200
6201 /* Find a LOAD segment containing a section in the RELRO
6202 segment. */
12bd6957 6203 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6204 lm != NULL;
6205 lm = lm->next, lp++)
8c37241b
JJ
6206 {
6207 if (lp->p_type == PT_LOAD
3146fac4 6208 && lm->count != 0
dbc88fc1
AM
6209 && (lm->sections[lm->count - 1]->vma
6210 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6211 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6212 : 0)) > start
f2731e0c 6213 && lm->sections[0]->vma < end)
8c37241b
JJ
6214 break;
6215 }
f2731e0c 6216
01f7e10c 6217 if (lm != NULL)
129af99f 6218 {
01f7e10c
AM
6219 /* Find the section starting the RELRO segment. */
6220 for (i = 0; i < lm->count; i++)
6221 {
6222 asection *s = lm->sections[i];
6223 if (s->vma >= start
6224 && s->vma < end
6225 && s->size != 0)
6226 break;
6227 }
6228
6229 if (i < lm->count)
6230 {
502794d4
CE
6231 p->p_vaddr = lm->sections[i]->vma * opb;
6232 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6233 p->p_offset = lm->sections[i]->filepos;
66631823 6234 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6235 p->p_filesz = p->p_memsz;
6236
6237 /* The RELRO segment typically ends a few bytes
6238 into .got.plt but other layouts are possible.
6239 In cases where the end does not match any
6240 loaded section (for instance is in file
6241 padding), trim p_filesz back to correspond to
6242 the end of loaded section contents. */
6243 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6244 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6245
6246 /* Preserve the alignment and flags if they are
6247 valid. The gold linker generates RW/4 for
6248 the PT_GNU_RELRO section. It is better for
6249 objcopy/strip to honor these attributes
6250 otherwise gdb will choke when using separate
6251 debug files. */
6252 if (!m->p_align_valid)
6253 p->p_align = 1;
6254 if (!m->p_flags_valid)
6255 p->p_flags = PF_R;
0a1b45a2 6256 ok = true;
01f7e10c 6257 }
129af99f 6258 }
b84a33b5 6259 }
01f7e10c
AM
6260 if (link_info != NULL)
6261 BFD_ASSERT (ok);
6262 if (!ok)
6263 memset (p, 0, sizeof *p);
129af99f 6264 }
04c3a755
NS
6265 else if (p->p_type == PT_GNU_STACK)
6266 {
6267 if (m->p_size_valid)
6268 p->p_memsz = m->p_size;
6269 }
129af99f
AS
6270 else if (m->count != 0)
6271 {
e06efbf1 6272 unsigned int i;
1a9ccd70 6273
129af99f
AS
6274 if (p->p_type != PT_LOAD
6275 && (p->p_type != PT_NOTE
6276 || bfd_get_format (abfd) != bfd_core))
6277 {
1a9ccd70
NC
6278 /* A user specified segment layout may include a PHDR
6279 segment that overlaps with a LOAD segment... */
6280 if (p->p_type == PT_PHDR)
6281 {
6282 m->count = 0;
6283 continue;
6284 }
6285
c86934ce
NC
6286 if (m->includes_filehdr || m->includes_phdrs)
6287 {
b1fa9dd6 6288 /* PR 17512: file: 2195325e. */
4eca0228 6289 _bfd_error_handler
871b3ab2 6290 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6291 "and/or program header"),
6292 abfd, (int) (p - phdrs));
0a1b45a2 6293 return false;
c86934ce 6294 }
129af99f 6295
86b2281f 6296 p->p_filesz = 0;
129af99f 6297 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6298 for (i = m->count; i-- != 0;)
6299 {
6300 asection *sect = m->sections[i];
6301 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6302 if (hdr->sh_type != SHT_NOBITS)
6303 {
6304 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6305 + hdr->sh_size);
9917b559
L
6306 /* NB: p_memsz of the loadable PT_NOTE segment
6307 should be the same as p_filesz. */
6308 if (p->p_type == PT_NOTE
6309 && (hdr->sh_flags & SHF_ALLOC) != 0)
6310 p->p_memsz = p->p_filesz;
86b2281f
AM
6311 break;
6312 }
6313 }
129af99f
AS
6314 }
6315 }
252b5132
RH
6316 }
6317
0a1b45a2 6318 return true;
252b5132
RH
6319}
6320
6a40cf0c
NC
6321static elf_section_list *
6322find_section_in_list (unsigned int i, elf_section_list * list)
6323{
6324 for (;list != NULL; list = list->next)
6325 if (list->ndx == i)
6326 break;
6327 return list;
6328}
6329
252b5132
RH
6330/* Work out the file positions of all the sections. This is called by
6331 _bfd_elf_compute_section_file_positions. All the section sizes and
6332 VMAs must be known before this is called.
6333
e0638f70 6334 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6335 "side-channel" data attached to a section to which they apply, and those that
6336 bfd doesn't process as relocations. The latter sort are stored in a normal
6337 bfd section by bfd_section_from_shdr. We don't consider the former sort
6338 here, unless they form part of the loadable image. Reloc sections not
6339 assigned here (and compressed debugging sections and CTF sections which
6340 nothing else in the file can rely upon) will be handled later by
e0638f70 6341 assign_file_positions_for_relocs.
252b5132
RH
6342
6343 We also don't set the positions of the .symtab and .strtab here. */
6344
0a1b45a2 6345static bool
c84fca4d
AO
6346assign_file_positions_except_relocs (bfd *abfd,
6347 struct bfd_link_info *link_info)
252b5132 6348{
5c182d5f
AM
6349 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6350 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6352 unsigned int alloc;
252b5132
RH
6353
6354 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6355 && bfd_get_format (abfd) != bfd_core)
6356 {
5c182d5f
AM
6357 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6358 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6359 Elf_Internal_Shdr **hdrpp;
6360 unsigned int i;
a485e98e 6361 file_ptr off;
252b5132
RH
6362
6363 /* Start after the ELF header. */
6364 off = i_ehdrp->e_ehsize;
6365
6366 /* We are not creating an executable, which means that we are
6367 not creating a program header, and that the actual order of
6368 the sections in the file is unimportant. */
9ad5cbcf 6369 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6370 {
6371 Elf_Internal_Shdr *hdr;
6372
6373 hdr = *hdrpp;
e0638f70
AM
6374 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6375 && hdr->bfd_section == NULL)
1ff6de03
NA
6376 /* Do not assign offsets for these sections yet: we don't know
6377 their sizes. */
0ce398f1 6378 || (hdr->bfd_section != NULL
1ff6de03
NA
6379 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6380 || (bfd_section_is_ctf (hdr->bfd_section)
6381 && abfd->is_linker_output)))
12bd6957 6382 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6383 || (elf_symtab_shndx_list (abfd) != NULL
6384 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6385 || i == elf_strtab_sec (abfd)
6386 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6387 {
6388 hdr->sh_offset = -1;
252b5132 6389 }
9ad5cbcf 6390 else
0a1b45a2 6391 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 6392 }
a485e98e
AM
6393
6394 elf_next_file_pos (abfd) = off;
6d6c25c8 6395 elf_program_header_size (abfd) = 0;
252b5132
RH
6396 }
6397 else
6398 {
252b5132 6399 /* Assign file positions for the loaded sections based on the
08a40648 6400 assignment of sections to segments. */
f3520d2f 6401 if (!assign_file_positions_for_load_sections (abfd, link_info))
0a1b45a2 6402 return false;
f3520d2f
AM
6403
6404 /* And for non-load sections. */
6405 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
0a1b45a2 6406 return false;
6d6c25c8 6407 }
f3520d2f 6408
6d6c25c8 6409 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
0a1b45a2 6410 return false;
1a9ccd70 6411
6d6c25c8
AM
6412 /* Write out the program headers. */
6413 alloc = i_ehdrp->e_phnum;
6414 if (alloc != 0)
6415 {
30fe1832 6416 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857 6417 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
0a1b45a2 6418 return false;
252b5132
RH
6419 }
6420
0a1b45a2 6421 return true;
252b5132
RH
6422}
6423
0a1b45a2 6424bool
ed7e9d0b
AM
6425_bfd_elf_init_file_header (bfd *abfd,
6426 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6427{
3d540e93 6428 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6429 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6430 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6431
6432 i_ehdrp = elf_elfheader (abfd);
252b5132 6433
2b0f7ef9 6434 shstrtab = _bfd_elf_strtab_init ();
252b5132 6435 if (shstrtab == NULL)
0a1b45a2 6436 return false;
252b5132
RH
6437
6438 elf_shstrtab (abfd) = shstrtab;
6439
6440 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6441 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6442 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6443 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6444
6445 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6446 i_ehdrp->e_ident[EI_DATA] =
6447 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6448 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6449
252b5132
RH
6450 if ((abfd->flags & DYNAMIC) != 0)
6451 i_ehdrp->e_type = ET_DYN;
6452 else if ((abfd->flags & EXEC_P) != 0)
6453 i_ehdrp->e_type = ET_EXEC;
6454 else if (bfd_get_format (abfd) == bfd_core)
6455 i_ehdrp->e_type = ET_CORE;
6456 else
6457 i_ehdrp->e_type = ET_REL;
6458
6459 switch (bfd_get_arch (abfd))
6460 {
6461 case bfd_arch_unknown:
6462 i_ehdrp->e_machine = EM_NONE;
6463 break;
aa4f99bb
AO
6464
6465 /* There used to be a long list of cases here, each one setting
6466 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6467 in the corresponding bfd definition. To avoid duplication,
6468 the switch was removed. Machines that need special handling
6469 can generally do it in elf_backend_final_write_processing(),
6470 unless they need the information earlier than the final write.
6471 Such need can generally be supplied by replacing the tests for
6472 e_machine with the conditions used to determine it. */
252b5132 6473 default:
9c5bfbb7
AM
6474 i_ehdrp->e_machine = bed->elf_machine_code;
6475 }
aa4f99bb 6476
252b5132
RH
6477 i_ehdrp->e_version = bed->s->ev_current;
6478 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6479
c044fabd 6480 /* No program header, for now. */
252b5132
RH
6481 i_ehdrp->e_phoff = 0;
6482 i_ehdrp->e_phentsize = 0;
6483 i_ehdrp->e_phnum = 0;
6484
c044fabd 6485 /* Each bfd section is section header entry. */
252b5132
RH
6486 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6487 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6488
252b5132 6489 elf_tdata (abfd)->symtab_hdr.sh_name =
0a1b45a2 6490 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 6491 elf_tdata (abfd)->strtab_hdr.sh_name =
0a1b45a2 6492 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 6493 elf_tdata (abfd)->shstrtab_hdr.sh_name =
0a1b45a2 6494 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132 6495 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6496 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6497 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
0a1b45a2 6498 return false;
252b5132 6499
0a1b45a2 6500 return true;
252b5132
RH
6501}
6502
6d6c25c8
AM
6503/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6504
6505 FIXME: We used to have code here to sort the PT_LOAD segments into
6506 ascending order, as per the ELF spec. But this breaks some programs,
6507 including the Linux kernel. But really either the spec should be
6508 changed or the programs updated. */
6509
0a1b45a2 6510bool
6d6c25c8
AM
6511_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6512{
6513 if (link_info != NULL && bfd_link_pie (link_info))
6514 {
6515 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6516 unsigned int num_segments = i_ehdrp->e_phnum;
6517 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6518 Elf_Internal_Phdr *segment = tdata->phdr;
6519 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6520
6521 /* Find the lowest p_vaddr in PT_LOAD segments. */
6522 bfd_vma p_vaddr = (bfd_vma) -1;
6523 for (; segment < end_segment; segment++)
6524 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6525 p_vaddr = segment->p_vaddr;
6526
6527 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6528 segments is non-zero. */
6529 if (p_vaddr)
6530 i_ehdrp->e_type = ET_EXEC;
6531 }
0a1b45a2 6532 return true;
6d6c25c8
AM
6533}
6534
252b5132 6535/* Assign file positions for all the reloc sections which are not part
a485e98e 6536 of the loadable file image, and the file position of section headers. */
252b5132 6537
0a1b45a2 6538static bool
0ce398f1 6539_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6540{
6541 file_ptr off;
e06efbf1 6542 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6543 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6544 Elf_Internal_Ehdr *i_ehdrp;
6545 const struct elf_backend_data *bed;
252b5132 6546
12bd6957 6547 off = elf_next_file_pos (abfd);
252b5132 6548
e06efbf1
L
6549 shdrpp = elf_elfsections (abfd);
6550 end_shdrpp = shdrpp + elf_numsections (abfd);
6551 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6552 {
252b5132 6553 shdrp = *shdrpp;
0ce398f1
L
6554 if (shdrp->sh_offset == -1)
6555 {
3e19fb8f 6556 asection *sec = shdrp->bfd_section;
0a1b45a2
AM
6557 bool is_rel = (shdrp->sh_type == SHT_REL
6558 || shdrp->sh_type == SHT_RELA);
6559 bool is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6560 if (is_rel
1ff6de03 6561 || is_ctf
3e19fb8f 6562 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6563 {
1ff6de03 6564 if (!is_rel && !is_ctf)
0ce398f1 6565 {
3e19fb8f
L
6566 const char *name = sec->name;
6567 struct bfd_elf_section_data *d;
6568
0ce398f1 6569 /* Compress DWARF debug sections. */
3e19fb8f 6570 if (!bfd_compress_section (abfd, sec,
0ce398f1 6571 shdrp->contents))
0a1b45a2 6572 return false;
3e19fb8f
L
6573
6574 if (sec->compress_status == COMPRESS_SECTION_DONE
6575 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6576 {
6577 /* If section is compressed with zlib-gnu, convert
6578 section name from .debug_* to .zdebug_*. */
6579 char *new_name
6580 = convert_debug_to_zdebug (abfd, name);
6581 if (new_name == NULL)
0a1b45a2 6582 return false;
3e19fb8f
L
6583 name = new_name;
6584 }
dd905818 6585 /* Add section name to section name section. */
3e19fb8f
L
6586 if (shdrp->sh_name != (unsigned int) -1)
6587 abort ();
6588 shdrp->sh_name
6589 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 6590 name, false);
3e19fb8f
L
6591 d = elf_section_data (sec);
6592
dd905818 6593 /* Add reloc section name to section name section. */
3e19fb8f
L
6594 if (d->rel.hdr
6595 && !_bfd_elf_set_reloc_sh_name (abfd,
6596 d->rel.hdr,
0a1b45a2
AM
6597 name, false))
6598 return false;
3e19fb8f
L
6599 if (d->rela.hdr
6600 && !_bfd_elf_set_reloc_sh_name (abfd,
6601 d->rela.hdr,
0a1b45a2
AM
6602 name, true))
6603 return false;
3e19fb8f 6604
0ce398f1 6605 /* Update section size and contents. */
3e19fb8f
L
6606 shdrp->sh_size = sec->size;
6607 shdrp->contents = sec->contents;
0ce398f1
L
6608 shdrp->bfd_section->contents = NULL;
6609 }
1ff6de03
NA
6610 else if (is_ctf)
6611 {
6612 /* Update section size and contents. */
6613 shdrp->sh_size = sec->size;
6614 shdrp->contents = sec->contents;
6615 }
6616
0ce398f1
L
6617 off = _bfd_elf_assign_file_position_for_section (shdrp,
6618 off,
0a1b45a2 6619 true);
0ce398f1
L
6620 }
6621 }
252b5132
RH
6622 }
6623
3e19fb8f
L
6624 /* Place section name section after DWARF debug sections have been
6625 compressed. */
6626 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6627 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6628 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
0a1b45a2 6629 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3e19fb8f
L
6630
6631 /* Place the section headers. */
a485e98e
AM
6632 i_ehdrp = elf_elfheader (abfd);
6633 bed = get_elf_backend_data (abfd);
6634 off = align_file_position (off, 1 << bed->s->log_file_align);
6635 i_ehdrp->e_shoff = off;
6636 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6637 elf_next_file_pos (abfd) = off;
0ce398f1 6638
0a1b45a2 6639 return true;
252b5132
RH
6640}
6641
0a1b45a2 6642bool
217aa764 6643_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6644{
9c5bfbb7 6645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6646 Elf_Internal_Shdr **i_shdrp;
0a1b45a2 6647 bool failed;
9ad5cbcf 6648 unsigned int count, num_sec;
30e8ee25 6649 struct elf_obj_tdata *t;
252b5132
RH
6650
6651 if (! abfd->output_has_begun
217aa764 6652 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 6653 return false;
db727370
JL
6654 /* Do not rewrite ELF data when the BFD has been opened for update.
6655 abfd->output_has_begun was set to TRUE on opening, so creation of new
6656 sections, and modification of existing section sizes was restricted.
6657 This means the ELF header, program headers and section headers can't have
6658 changed.
6659 If the contents of any sections has been modified, then those changes have
6660 already been written to the BFD. */
6661 else if (abfd->direction == both_direction)
6662 {
6663 BFD_ASSERT (abfd->output_has_begun);
0a1b45a2 6664 return true;
db727370 6665 }
252b5132
RH
6666
6667 i_shdrp = elf_elfsections (abfd);
252b5132 6668
0a1b45a2 6669 failed = false;
252b5132
RH
6670 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6671 if (failed)
0a1b45a2 6672 return false;
252b5132 6673
0ce398f1 6674 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
0a1b45a2 6675 return false;
252b5132 6676
c044fabd 6677 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6678 num_sec = elf_numsections (abfd);
6679 for (count = 1; count < num_sec; count++)
252b5132 6680 {
3e19fb8f
L
6681 i_shdrp[count]->sh_name
6682 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6683 i_shdrp[count]->sh_name);
252b5132 6684 if (bed->elf_backend_section_processing)
75506100 6685 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
0a1b45a2 6686 return false;
252b5132
RH
6687 if (i_shdrp[count]->contents)
6688 {
dc810e39
AM
6689 bfd_size_type amt = i_shdrp[count]->sh_size;
6690
252b5132 6691 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6692 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
0a1b45a2 6693 return false;
252b5132
RH
6694 }
6695 }
6696
6697 /* Write out the section header names. */
30e8ee25 6698 t = elf_tdata (abfd);
26ae6d5e 6699 if (elf_shstrtab (abfd) != NULL
30e8ee25 6700 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6701 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
0a1b45a2 6702 return false;
252b5132 6703
cc364be6 6704 if (!(*bed->elf_backend_final_write_processing) (abfd))
0a1b45a2 6705 return false;
252b5132 6706
ff59fc36 6707 if (!bed->s->write_shdrs_and_ehdr (abfd))
0a1b45a2 6708 return false;
ff59fc36
RM
6709
6710 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6711 if (t->o->build_id.after_write_object_contents != NULL)
6712 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36 6713
0a1b45a2 6714 return true;
252b5132
RH
6715}
6716
0a1b45a2 6717bool
217aa764 6718_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6719{
c044fabd 6720 /* Hopefully this can be done just like an object file. */
252b5132
RH
6721 return _bfd_elf_write_object_contents (abfd);
6722}
c044fabd
KH
6723
6724/* Given a section, search the header to find them. */
6725
cb33740c 6726unsigned int
198beae2 6727_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6728{
9c5bfbb7 6729 const struct elf_backend_data *bed;
91d6fa6a 6730 unsigned int sec_index;
252b5132 6731
9ad5cbcf
AM
6732 if (elf_section_data (asect) != NULL
6733 && elf_section_data (asect)->this_idx != 0)
6734 return elf_section_data (asect)->this_idx;
6735
6736 if (bfd_is_abs_section (asect))
91d6fa6a 6737 sec_index = SHN_ABS;
af746e92 6738 else if (bfd_is_com_section (asect))
91d6fa6a 6739 sec_index = SHN_COMMON;
af746e92 6740 else if (bfd_is_und_section (asect))
91d6fa6a 6741 sec_index = SHN_UNDEF;
af746e92 6742 else
91d6fa6a 6743 sec_index = SHN_BAD;
252b5132 6744
af746e92 6745 bed = get_elf_backend_data (abfd);
252b5132
RH
6746 if (bed->elf_backend_section_from_bfd_section)
6747 {
91d6fa6a 6748 int retval = sec_index;
9ad5cbcf 6749
af746e92
AM
6750 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6751 return retval;
252b5132
RH
6752 }
6753
91d6fa6a 6754 if (sec_index == SHN_BAD)
af746e92 6755 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6756
91d6fa6a 6757 return sec_index;
252b5132
RH
6758}
6759
6760/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6761 on error. */
6762
6763int
217aa764 6764_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6765{
6766 asymbol *asym_ptr = *asym_ptr_ptr;
6767 int idx;
6768 flagword flags = asym_ptr->flags;
6769
6770 /* When gas creates relocations against local labels, it creates its
6771 own symbol for the section, but does put the symbol into the
6772 symbol chain, so udata is 0. When the linker is generating
6773 relocatable output, this section symbol may be for one of the
6774 input sections rather than the output section. */
6775 if (asym_ptr->udata.i == 0
6776 && (flags & BSF_SECTION_SYM)
6777 && asym_ptr->section)
6778 {
5372391b 6779 asection *sec;
252b5132
RH
6780 int indx;
6781
5372391b
AM
6782 sec = asym_ptr->section;
6783 if (sec->owner != abfd && sec->output_section != NULL)
6784 sec = sec->output_section;
6785 if (sec->owner == abfd
6786 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6787 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6788 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6789 }
6790
6791 idx = asym_ptr->udata.i;
6792
6793 if (idx == 0)
6794 {
6795 /* This case can occur when using --strip-symbol on a symbol
08a40648 6796 which is used in a relocation entry. */
4eca0228 6797 _bfd_error_handler
695344c0 6798 /* xgettext:c-format */
871b3ab2 6799 (_("%pB: symbol `%s' required but not present"),
d003868e 6800 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6801 bfd_set_error (bfd_error_no_symbols);
6802 return -1;
6803 }
6804
6805#if DEBUG & 4
6806 {
6807 fprintf (stderr,
cd9af601
AM
6808 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6809 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6810 fflush (stderr);
6811 }
6812#endif
6813
6814 return idx;
6815}
6816
84d1d650 6817/* Rewrite program header information. */
252b5132 6818
0a1b45a2 6819static bool
c410035d 6820rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6821{
b34976b6
AM
6822 Elf_Internal_Ehdr *iehdr;
6823 struct elf_segment_map *map;
6824 struct elf_segment_map *map_first;
6825 struct elf_segment_map **pointer_to_map;
6826 Elf_Internal_Phdr *segment;
6827 asection *section;
6828 unsigned int i;
6829 unsigned int num_segments;
0a1b45a2
AM
6830 bool phdr_included = false;
6831 bool p_paddr_valid;
b34976b6
AM
6832 struct elf_segment_map *phdr_adjust_seg = NULL;
6833 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6834 const struct elf_backend_data *bed;
502794d4 6835 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6836
caf47ea6 6837 bed = get_elf_backend_data (ibfd);
252b5132
RH
6838 iehdr = elf_elfheader (ibfd);
6839
bc67d8a6 6840 map_first = NULL;
c044fabd 6841 pointer_to_map = &map_first;
252b5132
RH
6842
6843 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6844
6845 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6846#define SEGMENT_END(segment, start) \
6847 (start + (segment->p_memsz > segment->p_filesz \
6848 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6849
eecdbe52
JJ
6850#define SECTION_SIZE(section, segment) \
6851 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6852 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6853 ? section->size : 0)
eecdbe52 6854
b34976b6 6855 /* Returns TRUE if the given section is contained within
bc67d8a6 6856 the given segment. VMA addresses are compared. */
502794d4
CE
6857#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6858 (section->vma * (opb) >= segment->p_vaddr \
6859 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6860 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6861
b34976b6 6862 /* Returns TRUE if the given section is contained within
bc67d8a6 6863 the given segment. LMA addresses are compared. */
502794d4
CE
6864#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6865 (section->lma * (opb) >= base \
6866 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6867 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6868 <= SEGMENT_END (segment, base)))
252b5132 6869
0efc80c8
L
6870 /* Handle PT_NOTE segment. */
6871#define IS_NOTE(p, s) \
aecc8f8a 6872 (p->p_type == PT_NOTE \
0efc80c8 6873 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6874 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6875 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6876 <= p->p_offset + p->p_filesz))
252b5132 6877
0efc80c8
L
6878 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6879 etc. */
6880#define IS_COREFILE_NOTE(p, s) \
6881 (IS_NOTE (p, s) \
6882 && bfd_get_format (ibfd) == bfd_core \
6883 && s->vma == 0 \
6884 && s->lma == 0)
6885
252b5132
RH
6886 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6887 linker, which generates a PT_INTERP section with p_vaddr and
6888 p_memsz set to 0. */
aecc8f8a
AM
6889#define IS_SOLARIS_PT_INTERP(p, s) \
6890 (p->p_vaddr == 0 \
6891 && p->p_paddr == 0 \
6892 && p->p_memsz == 0 \
6893 && p->p_filesz > 0 \
6894 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6895 && s->size > 0 \
aecc8f8a 6896 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6897 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6898 <= p->p_offset + p->p_filesz))
5c440b1e 6899
bc67d8a6
NC
6900 /* Decide if the given section should be included in the given segment.
6901 A section will be included if:
f5ffc919 6902 1. It is within the address space of the segment -- we use the LMA
08a40648 6903 if that is set for the segment and the VMA otherwise,
0efc80c8 6904 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6905 segment.
bc67d8a6 6906 3. There is an output section associated with it,
eecdbe52 6907 4. The section has not already been allocated to a previous segment.
2b05f1b7 6908 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6909 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6910 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6911 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6912 (with the possible exception of .dynamic). */
502794d4 6913#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6914 ((((segment->p_paddr \
502794d4
CE
6915 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6916 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6917 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6918 || IS_NOTE (segment, section)) \
2b05f1b7
L
6919 && segment->p_type != PT_GNU_STACK \
6920 && (segment->p_type != PT_TLS \
6921 || (section->flags & SEC_THREAD_LOCAL)) \
6922 && (segment->p_type == PT_LOAD \
6923 || segment->p_type == PT_TLS \
6924 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6925 && (segment->p_type != PT_DYNAMIC \
6926 || SECTION_SIZE (section, segment) > 0 \
6927 || (segment->p_paddr \
502794d4
CE
6928 ? segment->p_paddr != section->lma * (opb) \
6929 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6930 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6931 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6932
9f17e2a6
L
6933/* If the output section of a section in the input segment is NULL,
6934 it is removed from the corresponding output segment. */
502794d4
CE
6935#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6936 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6937 && section->output_section != NULL)
6938
b34976b6 6939 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6940#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6941 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6942
6943 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6944 their VMA address ranges and their LMA address ranges overlap.
6945 It is possible to have overlapping VMA ranges without overlapping LMA
6946 ranges. RedBoot images for example can have both .data and .bss mapped
6947 to the same VMA range, but with the .data section mapped to a different
6948 LMA. */
aecc8f8a 6949#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6950 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6951 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6952 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6953 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6954
6955 /* Initialise the segment mark field. */
6956 for (section = ibfd->sections; section != NULL; section = section->next)
0a1b45a2 6957 section->segment_mark = false;
bc67d8a6 6958
5c44b38e
AM
6959 /* The Solaris linker creates program headers in which all the
6960 p_paddr fields are zero. When we try to objcopy or strip such a
6961 file, we get confused. Check for this case, and if we find it
6962 don't set the p_paddr_valid fields. */
0a1b45a2 6963 p_paddr_valid = false;
5c44b38e
AM
6964 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6965 i < num_segments;
6966 i++, segment++)
6967 if (segment->p_paddr != 0)
6968 {
0a1b45a2 6969 p_paddr_valid = true;
5c44b38e
AM
6970 break;
6971 }
6972
252b5132 6973 /* Scan through the segments specified in the program header
bc67d8a6 6974 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6975 in the loadable segments. These can be created by weird
aecc8f8a 6976 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6977 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6978 i < num_segments;
c044fabd 6979 i++, segment++)
252b5132 6980 {
252b5132 6981 unsigned int j;
c044fabd 6982 Elf_Internal_Phdr *segment2;
252b5132 6983
aecc8f8a
AM
6984 if (segment->p_type == PT_INTERP)
6985 for (section = ibfd->sections; section; section = section->next)
6986 if (IS_SOLARIS_PT_INTERP (segment, section))
6987 {
6988 /* Mininal change so that the normal section to segment
4cc11e76 6989 assignment code will work. */
502794d4 6990 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
6991 break;
6992 }
6993
bc67d8a6 6994 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6995 {
6996 /* Remove PT_GNU_RELRO segment. */
6997 if (segment->p_type == PT_GNU_RELRO)
6998 segment->p_type = PT_NULL;
6999 continue;
7000 }
c044fabd 7001
bc67d8a6 7002 /* Determine if this segment overlaps any previous segments. */
0067a569 7003 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7004 {
7005 bfd_signed_vma extra_length;
c044fabd 7006
bc67d8a6 7007 if (segment2->p_type != PT_LOAD
0067a569 7008 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7009 continue;
c044fabd 7010
bc67d8a6
NC
7011 /* Merge the two segments together. */
7012 if (segment2->p_vaddr < segment->p_vaddr)
7013 {
c044fabd 7014 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7015 SEGMENT. */
0067a569
AM
7016 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7017 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 7018
bc67d8a6
NC
7019 if (extra_length > 0)
7020 {
0067a569 7021 segment2->p_memsz += extra_length;
bc67d8a6
NC
7022 segment2->p_filesz += extra_length;
7023 }
c044fabd 7024
bc67d8a6 7025 segment->p_type = PT_NULL;
c044fabd 7026
bc67d8a6
NC
7027 /* Since we have deleted P we must restart the outer loop. */
7028 i = 0;
7029 segment = elf_tdata (ibfd)->phdr;
7030 break;
7031 }
7032 else
7033 {
c044fabd 7034 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7035 SEGMENT2. */
0067a569
AM
7036 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7037 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7038
bc67d8a6
NC
7039 if (extra_length > 0)
7040 {
0067a569 7041 segment->p_memsz += extra_length;
bc67d8a6
NC
7042 segment->p_filesz += extra_length;
7043 }
c044fabd 7044
bc67d8a6
NC
7045 segment2->p_type = PT_NULL;
7046 }
7047 }
7048 }
c044fabd 7049
bc67d8a6
NC
7050 /* The second scan attempts to assign sections to segments. */
7051 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7052 i < num_segments;
0067a569 7053 i++, segment++)
bc67d8a6 7054 {
0067a569
AM
7055 unsigned int section_count;
7056 asection **sections;
7057 asection *output_section;
7058 unsigned int isec;
9933dc52
AM
7059 asection *matching_lma;
7060 asection *suggested_lma;
0067a569 7061 unsigned int j;
446f7ed5 7062 size_t amt;
0067a569 7063 asection *first_section;
bc67d8a6
NC
7064
7065 if (segment->p_type == PT_NULL)
7066 continue;
c044fabd 7067
9f17e2a6 7068 first_section = NULL;
bc67d8a6 7069 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7070 for (section = ibfd->sections, section_count = 0;
7071 section != NULL;
7072 section = section->next)
9f17e2a6
L
7073 {
7074 /* Find the first section in the input segment, which may be
7075 removed from the corresponding output segment. */
502794d4 7076 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7077 {
7078 if (first_section == NULL)
7079 first_section = section;
7080 if (section->output_section != NULL)
7081 ++section_count;
7082 }
7083 }
811072d8 7084
b5f852ea
NC
7085 /* Allocate a segment map big enough to contain
7086 all of the sections we have selected. */
00bee008 7087 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7088 amt += section_count * sizeof (asection *);
a50b1753 7089 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7090 if (map == NULL)
0a1b45a2 7091 return false;
252b5132
RH
7092
7093 /* Initialise the fields of the segment map. Default to
7094 using the physical address of the segment in the input BFD. */
0067a569
AM
7095 map->next = NULL;
7096 map->p_type = segment->p_type;
7097 map->p_flags = segment->p_flags;
bc67d8a6 7098 map->p_flags_valid = 1;
55d55ac7 7099
c410035d
AM
7100 if (map->p_type == PT_LOAD
7101 && (ibfd->flags & D_PAGED) != 0
7102 && maxpagesize > 1
7103 && segment->p_align > 1)
7104 {
7105 map->p_align = segment->p_align;
7106 if (segment->p_align > maxpagesize)
7107 map->p_align = maxpagesize;
7108 map->p_align_valid = 1;
7109 }
7110
9f17e2a6
L
7111 /* If the first section in the input segment is removed, there is
7112 no need to preserve segment physical address in the corresponding
7113 output segment. */
945c025a 7114 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7115 {
7116 map->p_paddr = segment->p_paddr;
5c44b38e 7117 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7118 }
252b5132
RH
7119
7120 /* Determine if this segment contains the ELF file header
7121 and if it contains the program headers themselves. */
bc67d8a6
NC
7122 map->includes_filehdr = (segment->p_offset == 0
7123 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7124 map->includes_phdrs = 0;
252b5132 7125
0067a569 7126 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7127 {
bc67d8a6
NC
7128 map->includes_phdrs =
7129 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7130 && (segment->p_offset + segment->p_filesz
252b5132
RH
7131 >= ((bfd_vma) iehdr->e_phoff
7132 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7133
bc67d8a6 7134 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7135 phdr_included = true;
252b5132
RH
7136 }
7137
bc67d8a6 7138 if (section_count == 0)
252b5132
RH
7139 {
7140 /* Special segments, such as the PT_PHDR segment, may contain
7141 no sections, but ordinary, loadable segments should contain
1ed89aa9 7142 something. They are allowed by the ELF spec however, so only
07d6d2b8 7143 a warning is produced.
f98450c6
NC
7144 There is however the valid use case of embedded systems which
7145 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7146 flash memory with zeros. No warning is shown for that case. */
7147 if (segment->p_type == PT_LOAD
7148 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7149 /* xgettext:c-format */
9793eb77
AM
7150 _bfd_error_handler
7151 (_("%pB: warning: empty loadable segment detected"
7152 " at vaddr=%#" PRIx64 ", is this intentional?"),
7153 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7154
502794d4 7155 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7156 map->count = 0;
c044fabd
KH
7157 *pointer_to_map = map;
7158 pointer_to_map = &map->next;
252b5132
RH
7159
7160 continue;
7161 }
7162
7163 /* Now scan the sections in the input BFD again and attempt
7164 to add their corresponding output sections to the segment map.
7165 The problem here is how to handle an output section which has
7166 been moved (ie had its LMA changed). There are four possibilities:
7167
7168 1. None of the sections have been moved.
7169 In this case we can continue to use the segment LMA from the
7170 input BFD.
7171
7172 2. All of the sections have been moved by the same amount.
7173 In this case we can change the segment's LMA to match the LMA
7174 of the first section.
7175
7176 3. Some of the sections have been moved, others have not.
7177 In this case those sections which have not been moved can be
7178 placed in the current segment which will have to have its size,
7179 and possibly its LMA changed, and a new segment or segments will
7180 have to be created to contain the other sections.
7181
b5f852ea 7182 4. The sections have been moved, but not by the same amount.
252b5132
RH
7183 In this case we can change the segment's LMA to match the LMA
7184 of the first section and we will have to create a new segment
7185 or segments to contain the other sections.
7186
7187 In order to save time, we allocate an array to hold the section
7188 pointers that we are interested in. As these sections get assigned
7189 to a segment, they are removed from this array. */
7190
446f7ed5
AM
7191 amt = section_count * sizeof (asection *);
7192 sections = (asection **) bfd_malloc (amt);
252b5132 7193 if (sections == NULL)
0a1b45a2 7194 return false;
252b5132
RH
7195
7196 /* Step One: Scan for segment vs section LMA conflicts.
7197 Also add the sections to the section array allocated above.
7198 Also add the sections to the current segment. In the common
7199 case, where the sections have not been moved, this means that
7200 we have completely filled the segment, and there is nothing
7201 more to do. */
252b5132 7202 isec = 0;
9933dc52
AM
7203 matching_lma = NULL;
7204 suggested_lma = NULL;
252b5132 7205
461c4b2e 7206 for (section = first_section, j = 0;
bc67d8a6
NC
7207 section != NULL;
7208 section = section->next)
252b5132 7209 {
502794d4 7210 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7211 {
bc67d8a6
NC
7212 output_section = section->output_section;
7213
0067a569 7214 sections[j++] = section;
252b5132
RH
7215
7216 /* The Solaris native linker always sets p_paddr to 0.
7217 We try to catch that case here, and set it to the
5e8d7549
NC
7218 correct value. Note - some backends require that
7219 p_paddr be left as zero. */
5c44b38e 7220 if (!p_paddr_valid
4455705d 7221 && segment->p_vaddr != 0
0067a569 7222 && !bed->want_p_paddr_set_to_zero
252b5132 7223 && isec == 0
bc67d8a6 7224 && output_section->lma != 0
9933dc52
AM
7225 && (align_power (segment->p_vaddr
7226 + (map->includes_filehdr
7227 ? iehdr->e_ehsize : 0)
7228 + (map->includes_phdrs
7229 ? iehdr->e_phnum * iehdr->e_phentsize
7230 : 0),
66631823
CE
7231 output_section->alignment_power * opb)
7232 == (output_section->vma * opb)))
bc67d8a6 7233 map->p_paddr = segment->p_vaddr;
252b5132
RH
7234
7235 /* Match up the physical address of the segment with the
7236 LMA address of the output section. */
502794d4
CE
7237 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7238 opb)
5e8d7549 7239 || IS_COREFILE_NOTE (segment, section)
0067a569 7240 || (bed->want_p_paddr_set_to_zero
502794d4 7241 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7242 {
9933dc52
AM
7243 if (matching_lma == NULL
7244 || output_section->lma < matching_lma->lma)
7245 matching_lma = output_section;
252b5132
RH
7246
7247 /* We assume that if the section fits within the segment
bc67d8a6 7248 then it does not overlap any other section within that
252b5132 7249 segment. */
0067a569
AM
7250 map->sections[isec++] = output_section;
7251 }
9933dc52
AM
7252 else if (suggested_lma == NULL)
7253 suggested_lma = output_section;
147d51c2
L
7254
7255 if (j == section_count)
7256 break;
252b5132
RH
7257 }
7258 }
7259
bc67d8a6 7260 BFD_ASSERT (j == section_count);
252b5132
RH
7261
7262 /* Step Two: Adjust the physical address of the current segment,
7263 if necessary. */
bc67d8a6 7264 if (isec == section_count)
252b5132
RH
7265 {
7266 /* All of the sections fitted within the segment as currently
7267 specified. This is the default case. Add the segment to
7268 the list of built segments and carry on to process the next
7269 program header in the input BFD. */
bc67d8a6 7270 map->count = section_count;
c044fabd
KH
7271 *pointer_to_map = map;
7272 pointer_to_map = &map->next;
08a40648 7273
5c44b38e 7274 if (p_paddr_valid
30fe1832
AM
7275 && !bed->want_p_paddr_set_to_zero)
7276 {
7277 bfd_vma hdr_size = 0;
7278 if (map->includes_filehdr)
7279 hdr_size = iehdr->e_ehsize;
7280 if (map->includes_phdrs)
7281 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7282
7283 /* Account for padding before the first section in the
7284 segment. */
502794d4
CE
7285 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7286 - matching_lma->lma);
30fe1832 7287 }
08a40648 7288
252b5132
RH
7289 free (sections);
7290 continue;
7291 }
252b5132
RH
7292 else
7293 {
9933dc52
AM
7294 /* Change the current segment's physical address to match
7295 the LMA of the first section that fitted, or if no
7296 section fitted, the first section. */
7297 if (matching_lma == NULL)
7298 matching_lma = suggested_lma;
7299
66631823 7300 map->p_paddr = matching_lma->lma * opb;
72730e0c 7301
bc67d8a6
NC
7302 /* Offset the segment physical address from the lma
7303 to allow for space taken up by elf headers. */
9933dc52 7304 if (map->includes_phdrs)
010c8431 7305 {
9933dc52
AM
7306 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7307
7308 /* iehdr->e_phnum is just an estimate of the number
7309 of program headers that we will need. Make a note
7310 here of the number we used and the segment we chose
7311 to hold these headers, so that we can adjust the
7312 offset when we know the correct value. */
7313 phdr_adjust_num = iehdr->e_phnum;
7314 phdr_adjust_seg = map;
010c8431 7315 }
252b5132 7316
9933dc52 7317 if (map->includes_filehdr)
bc67d8a6 7318 {
9933dc52
AM
7319 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7320 map->p_paddr -= iehdr->e_ehsize;
7321 /* We've subtracted off the size of headers from the
7322 first section lma, but there may have been some
7323 alignment padding before that section too. Try to
7324 account for that by adjusting the segment lma down to
7325 the same alignment. */
7326 if (segment->p_align != 0 && segment->p_align < align)
7327 align = segment->p_align;
66631823 7328 map->p_paddr &= -(align * opb);
bc67d8a6 7329 }
252b5132
RH
7330 }
7331
7332 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7333 those that fit to the current segment and removing them from the
252b5132
RH
7334 sections array; but making sure not to leave large gaps. Once all
7335 possible sections have been assigned to the current segment it is
7336 added to the list of built segments and if sections still remain
7337 to be assigned, a new segment is constructed before repeating
7338 the loop. */
7339 isec = 0;
7340 do
7341 {
bc67d8a6 7342 map->count = 0;
9933dc52 7343 suggested_lma = NULL;
252b5132
RH
7344
7345 /* Fill the current segment with sections that fit. */
bc67d8a6 7346 for (j = 0; j < section_count; j++)
252b5132 7347 {
bc67d8a6 7348 section = sections[j];
252b5132 7349
bc67d8a6 7350 if (section == NULL)
252b5132
RH
7351 continue;
7352
bc67d8a6 7353 output_section = section->output_section;
252b5132 7354
bc67d8a6 7355 BFD_ASSERT (output_section != NULL);
c044fabd 7356
502794d4
CE
7357 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7358 opb)
bc67d8a6 7359 || IS_COREFILE_NOTE (segment, section))
252b5132 7360 {
bc67d8a6 7361 if (map->count == 0)
252b5132
RH
7362 {
7363 /* If the first section in a segment does not start at
bc67d8a6
NC
7364 the beginning of the segment, then something is
7365 wrong. */
9933dc52
AM
7366 if (align_power (map->p_paddr
7367 + (map->includes_filehdr
7368 ? iehdr->e_ehsize : 0)
7369 + (map->includes_phdrs
7370 ? iehdr->e_phnum * iehdr->e_phentsize
7371 : 0),
66631823
CE
7372 output_section->alignment_power * opb)
7373 != output_section->lma * opb)
9aea1e31 7374 goto sorry;
252b5132
RH
7375 }
7376 else
7377 {
0067a569 7378 asection *prev_sec;
252b5132 7379
bc67d8a6 7380 prev_sec = map->sections[map->count - 1];
252b5132
RH
7381
7382 /* If the gap between the end of the previous section
bc67d8a6
NC
7383 and the start of this section is more than
7384 maxpagesize then we need to start a new segment. */
eea6121a 7385 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7386 maxpagesize)
caf47ea6 7387 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7388 || (prev_sec->lma + prev_sec->size
079e9a2f 7389 > output_section->lma))
252b5132 7390 {
9933dc52
AM
7391 if (suggested_lma == NULL)
7392 suggested_lma = output_section;
252b5132
RH
7393
7394 continue;
7395 }
7396 }
7397
bc67d8a6 7398 map->sections[map->count++] = output_section;
252b5132
RH
7399 ++isec;
7400 sections[j] = NULL;
9933dc52 7401 if (segment->p_type == PT_LOAD)
0a1b45a2 7402 section->segment_mark = true;
0067a569 7403 }
9933dc52
AM
7404 else if (suggested_lma == NULL)
7405 suggested_lma = output_section;
252b5132
RH
7406 }
7407
beab4532
NC
7408 /* PR 23932. A corrupt input file may contain sections that cannot
7409 be assigned to any segment - because for example they have a
9984857c
NC
7410 negative size - or segments that do not contain any sections.
7411 But there are also valid reasons why a segment can be empty.
7412 So allow a count of zero. */
252b5132
RH
7413
7414 /* Add the current segment to the list of built segments. */
c044fabd
KH
7415 *pointer_to_map = map;
7416 pointer_to_map = &map->next;
252b5132 7417
bc67d8a6 7418 if (isec < section_count)
252b5132
RH
7419 {
7420 /* We still have not allocated all of the sections to
7421 segments. Create a new segment here, initialise it
7422 and carry on looping. */
00bee008 7423 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7424 amt += section_count * sizeof (asection *);
5964fc3a 7425 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7426 if (map == NULL)
5ed6aba4
NC
7427 {
7428 free (sections);
0a1b45a2 7429 return false;
5ed6aba4 7430 }
252b5132
RH
7431
7432 /* Initialise the fields of the segment map. Set the physical
7433 physical address to the LMA of the first section that has
7434 not yet been assigned. */
0067a569
AM
7435 map->next = NULL;
7436 map->p_type = segment->p_type;
7437 map->p_flags = segment->p_flags;
7438 map->p_flags_valid = 1;
66631823 7439 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7440 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7441 map->includes_filehdr = 0;
0067a569 7442 map->includes_phdrs = 0;
252b5132 7443 }
9984857c
NC
7444
7445 continue;
7446 sorry:
7447 bfd_set_error (bfd_error_sorry);
7448 free (sections);
0a1b45a2 7449 return false;
252b5132 7450 }
bc67d8a6 7451 while (isec < section_count);
252b5132
RH
7452
7453 free (sections);
7454 }
7455
12bd6957 7456 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7457
7458 /* If we had to estimate the number of program headers that were
9ad5cbcf 7459 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7460 the offset if necessary. */
7461 if (phdr_adjust_seg != NULL)
7462 {
7463 unsigned int count;
c044fabd 7464
bc67d8a6 7465 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7466 count++;
252b5132 7467
bc67d8a6
NC
7468 if (count > phdr_adjust_num)
7469 phdr_adjust_seg->p_paddr
7470 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7471
7472 for (map = map_first; map != NULL; map = map->next)
7473 if (map->p_type == PT_PHDR)
7474 {
7475 bfd_vma adjust
7476 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7477 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7478 break;
7479 }
bc67d8a6 7480 }
c044fabd 7481
bc67d8a6 7482#undef SEGMENT_END
eecdbe52 7483#undef SECTION_SIZE
bc67d8a6
NC
7484#undef IS_CONTAINED_BY_VMA
7485#undef IS_CONTAINED_BY_LMA
0efc80c8 7486#undef IS_NOTE
252b5132 7487#undef IS_COREFILE_NOTE
bc67d8a6 7488#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7489#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7490#undef INCLUDE_SECTION_IN_SEGMENT
7491#undef SEGMENT_AFTER_SEGMENT
7492#undef SEGMENT_OVERLAPS
0a1b45a2 7493 return true;
252b5132
RH
7494}
7495
84d1d650
L
7496/* Copy ELF program header information. */
7497
0a1b45a2 7498static bool
84d1d650
L
7499copy_elf_program_header (bfd *ibfd, bfd *obfd)
7500{
7501 Elf_Internal_Ehdr *iehdr;
7502 struct elf_segment_map *map;
7503 struct elf_segment_map *map_first;
7504 struct elf_segment_map **pointer_to_map;
7505 Elf_Internal_Phdr *segment;
7506 unsigned int i;
7507 unsigned int num_segments;
0a1b45a2
AM
7508 bool phdr_included = false;
7509 bool p_paddr_valid;
502794d4 7510 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7511
7512 iehdr = elf_elfheader (ibfd);
7513
7514 map_first = NULL;
7515 pointer_to_map = &map_first;
7516
88967714
AM
7517 /* If all the segment p_paddr fields are zero, don't set
7518 map->p_paddr_valid. */
0a1b45a2 7519 p_paddr_valid = false;
84d1d650 7520 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7521 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7522 i < num_segments;
7523 i++, segment++)
7524 if (segment->p_paddr != 0)
7525 {
0a1b45a2 7526 p_paddr_valid = true;
88967714
AM
7527 break;
7528 }
7529
84d1d650
L
7530 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7531 i < num_segments;
7532 i++, segment++)
7533 {
7534 asection *section;
7535 unsigned int section_count;
986f0783 7536 size_t amt;
84d1d650 7537 Elf_Internal_Shdr *this_hdr;
53020534 7538 asection *first_section = NULL;
a76e6f2f 7539 asection *lowest_section;
84d1d650 7540
84d1d650
L
7541 /* Compute how many sections are in this segment. */
7542 for (section = ibfd->sections, section_count = 0;
7543 section != NULL;
7544 section = section->next)
7545 {
7546 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7547 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7548 {
a76e6f2f
AM
7549 if (first_section == NULL)
7550 first_section = section;
3271a814
NS
7551 section_count++;
7552 }
84d1d650
L
7553 }
7554
7555 /* Allocate a segment map big enough to contain
7556 all of the sections we have selected. */
00bee008 7557 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7558 amt += section_count * sizeof (asection *);
a50b1753 7559 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650 7560 if (map == NULL)
0a1b45a2 7561 return false;
84d1d650
L
7562
7563 /* Initialize the fields of the output segment map with the
7564 input segment. */
7565 map->next = NULL;
7566 map->p_type = segment->p_type;
7567 map->p_flags = segment->p_flags;
7568 map->p_flags_valid = 1;
7569 map->p_paddr = segment->p_paddr;
88967714 7570 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7571 map->p_align = segment->p_align;
7572 map->p_align_valid = 1;
3271a814 7573 map->p_vaddr_offset = 0;
84d1d650 7574
04c3a755
NS
7575 if (map->p_type == PT_GNU_RELRO
7576 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7577 {
7578 /* The PT_GNU_RELRO segment may contain the first a few
7579 bytes in the .got.plt section even if the whole .got.plt
7580 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7581 change the size of the PT_GNU_RELRO segment.
7582 Similarly, PT_GNU_STACK size is significant on uclinux
7583 systems. */
9433b9b1 7584 map->p_size = segment->p_memsz;
b10a8ae0
L
7585 map->p_size_valid = 1;
7586 }
7587
84d1d650
L
7588 /* Determine if this segment contains the ELF file header
7589 and if it contains the program headers themselves. */
7590 map->includes_filehdr = (segment->p_offset == 0
7591 && segment->p_filesz >= iehdr->e_ehsize);
7592
7593 map->includes_phdrs = 0;
7594 if (! phdr_included || segment->p_type != PT_LOAD)
7595 {
7596 map->includes_phdrs =
7597 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7598 && (segment->p_offset + segment->p_filesz
7599 >= ((bfd_vma) iehdr->e_phoff
7600 + iehdr->e_phnum * iehdr->e_phentsize)));
7601
7602 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7603 phdr_included = true;
84d1d650
L
7604 }
7605
bbefd0a9 7606 lowest_section = NULL;
84d1d650
L
7607 if (section_count != 0)
7608 {
7609 unsigned int isec = 0;
7610
53020534 7611 for (section = first_section;
84d1d650
L
7612 section != NULL;
7613 section = section->next)
7614 {
7615 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7616 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7617 {
7618 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7619 if ((section->flags & SEC_ALLOC) != 0)
7620 {
7621 bfd_vma seg_off;
7622
bbefd0a9
AM
7623 if (lowest_section == NULL
7624 || section->lma < lowest_section->lma)
fb8a5684
AM
7625 lowest_section = section;
7626
a76e6f2f
AM
7627 /* Section lmas are set up from PT_LOAD header
7628 p_paddr in _bfd_elf_make_section_from_shdr.
7629 If this header has a p_paddr that disagrees
7630 with the section lma, flag the p_paddr as
7631 invalid. */
7632 if ((section->flags & SEC_LOAD) != 0)
7633 seg_off = this_hdr->sh_offset - segment->p_offset;
7634 else
7635 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7636 if (section->lma * opb - segment->p_paddr != seg_off)
0a1b45a2 7637 map->p_paddr_valid = false;
a76e6f2f 7638 }
53020534
L
7639 if (isec == section_count)
7640 break;
7641 }
84d1d650
L
7642 }
7643 }
7644
5d695627 7645 if (section_count == 0)
502794d4 7646 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7647 else if (map->p_paddr_valid)
7648 {
7649 /* Account for padding before the first section in the segment. */
7650 bfd_vma hdr_size = 0;
7651 if (map->includes_filehdr)
7652 hdr_size = iehdr->e_ehsize;
7653 if (map->includes_phdrs)
7654 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7655
502794d4 7656 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7657 - (lowest_section ? lowest_section->lma : 0));
7658 }
a76e6f2f 7659
84d1d650
L
7660 map->count = section_count;
7661 *pointer_to_map = map;
7662 pointer_to_map = &map->next;
7663 }
7664
12bd6957 7665 elf_seg_map (obfd) = map_first;
0a1b45a2 7666 return true;
84d1d650
L
7667}
7668
7669/* Copy private BFD data. This copies or rewrites ELF program header
7670 information. */
7671
0a1b45a2 7672static bool
84d1d650
L
7673copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7674{
c410035d
AM
7675 bfd_vma maxpagesize;
7676
84d1d650
L
7677 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7678 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 7679 return true;
84d1d650
L
7680
7681 if (elf_tdata (ibfd)->phdr == NULL)
0a1b45a2 7682 return true;
84d1d650
L
7683
7684 if (ibfd->xvec == obfd->xvec)
7685 {
cb3ff1e5
NC
7686 /* Check to see if any sections in the input BFD
7687 covered by ELF program header have changed. */
d55ce4e2 7688 Elf_Internal_Phdr *segment;
84d1d650
L
7689 asection *section, *osec;
7690 unsigned int i, num_segments;
7691 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7692 const struct elf_backend_data *bed;
7693
7694 bed = get_elf_backend_data (ibfd);
7695
7696 /* Regenerate the segment map if p_paddr is set to 0. */
7697 if (bed->want_p_paddr_set_to_zero)
7698 goto rewrite;
84d1d650
L
7699
7700 /* Initialize the segment mark field. */
7701 for (section = obfd->sections; section != NULL;
7702 section = section->next)
0a1b45a2 7703 section->segment_mark = false;
84d1d650
L
7704
7705 num_segments = elf_elfheader (ibfd)->e_phnum;
7706 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7707 i < num_segments;
7708 i++, segment++)
7709 {
5f6999aa
NC
7710 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7711 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7712 which severly confuses things, so always regenerate the segment
7713 map in this case. */
7714 if (segment->p_paddr == 0
7715 && segment->p_memsz == 0
7716 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7717 goto rewrite;
5f6999aa 7718
84d1d650
L
7719 for (section = ibfd->sections;
7720 section != NULL; section = section->next)
7721 {
7722 /* We mark the output section so that we know it comes
7723 from the input BFD. */
7724 osec = section->output_section;
7725 if (osec)
0a1b45a2 7726 osec->segment_mark = true;
84d1d650
L
7727
7728 /* Check if this section is covered by the segment. */
7729 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7730 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7731 {
7732 /* FIXME: Check if its output section is changed or
7733 removed. What else do we need to check? */
7734 if (osec == NULL
7735 || section->flags != osec->flags
7736 || section->lma != osec->lma
7737 || section->vma != osec->vma
7738 || section->size != osec->size
7739 || section->rawsize != osec->rawsize
7740 || section->alignment_power != osec->alignment_power)
7741 goto rewrite;
7742 }
7743 }
7744 }
7745
cb3ff1e5 7746 /* Check to see if any output section do not come from the
84d1d650
L
7747 input BFD. */
7748 for (section = obfd->sections; section != NULL;
7749 section = section->next)
7750 {
535b785f 7751 if (!section->segment_mark)
84d1d650
L
7752 goto rewrite;
7753 else
0a1b45a2 7754 section->segment_mark = false;
84d1d650
L
7755 }
7756
7757 return copy_elf_program_header (ibfd, obfd);
7758 }
7759
dc1e8a47 7760 rewrite:
c410035d 7761 maxpagesize = 0;
f1d85785
L
7762 if (ibfd->xvec == obfd->xvec)
7763 {
7764 /* When rewriting program header, set the output maxpagesize to
7765 the maximum alignment of input PT_LOAD segments. */
7766 Elf_Internal_Phdr *segment;
7767 unsigned int i;
7768 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7769
7770 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7771 i < num_segments;
7772 i++, segment++)
7773 if (segment->p_type == PT_LOAD
7774 && maxpagesize < segment->p_align)
c86934ce
NC
7775 {
7776 /* PR 17512: file: f17299af. */
7777 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7778 /* xgettext:c-format */
2dcf00ce
AM
7779 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7780 PRIx64 " is too large"),
7781 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7782 else
7783 maxpagesize = segment->p_align;
7784 }
f1d85785 7785 }
c410035d
AM
7786 if (maxpagesize == 0)
7787 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7788
c410035d 7789 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7790}
7791
ccd2ec6a
L
7792/* Initialize private output section information from input section. */
7793
0a1b45a2 7794bool
ccd2ec6a
L
7795_bfd_elf_init_private_section_data (bfd *ibfd,
7796 asection *isec,
7797 bfd *obfd,
7798 asection *osec,
7799 struct bfd_link_info *link_info)
7800
7801{
7802 Elf_Internal_Shdr *ihdr, *ohdr;
0a1b45a2
AM
7803 bool final_link = (link_info != NULL
7804 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7805
7806 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7807 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7808 return true;
ccd2ec6a 7809
ba85c43e
NC
7810 BFD_ASSERT (elf_section_data (osec) != NULL);
7811
8c803a2d
AM
7812 /* If this is a known ABI section, ELF section type and flags may
7813 have been set up when OSEC was created. For normal sections we
7814 allow the user to override the type and flags other than
7815 SHF_MASKOS and SHF_MASKPROC. */
7816 if (elf_section_type (osec) == SHT_PROGBITS
7817 || elf_section_type (osec) == SHT_NOTE
7818 || elf_section_type (osec) == SHT_NOBITS)
7819 elf_section_type (osec) = SHT_NULL;
7820 /* For objcopy and relocatable link, copy the ELF section type from
7821 the input file if the BFD section flags are the same. (If they
7822 are different the user may be doing something like
7823 "objcopy --set-section-flags .text=alloc,data".) For a final
7824 link allow some flags that the linker clears to differ. */
42bb2e33 7825 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7826 && (osec->flags == isec->flags
7827 || (final_link
7828 && ((osec->flags ^ isec->flags)
0814be7d 7829 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7830 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7831
7832 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7833 elf_section_flags (osec) = (elf_section_flags (isec)
7834 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7835
a91e1603 7836 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7837 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7838 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7839 elf_section_data (osec)->this_hdr.sh_info
7840 = elf_section_data (isec)->this_hdr.sh_info;
7841
ccd2ec6a
L
7842 /* Set things up for objcopy and relocatable link. The output
7843 SHT_GROUP section will have its elf_next_in_group pointing back
7844 to the input group members. Ignore linker created group section.
7845 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7846 if ((link_info == NULL
7847 || !link_info->resolve_section_groups)
7848 && (elf_sec_group (isec) == NULL
7849 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7850 {
7bdf4127
AB
7851 if (elf_section_flags (isec) & SHF_GROUP)
7852 elf_section_flags (osec) |= SHF_GROUP;
7853 elf_next_in_group (osec) = elf_next_in_group (isec);
7854 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7855 }
7856
7bdf4127
AB
7857 /* If not decompress, preserve SHF_COMPRESSED. */
7858 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7859 elf_section_flags (osec) |= (elf_section_flags (isec)
7860 & SHF_COMPRESSED);
7861
ccd2ec6a
L
7862 ihdr = &elf_section_data (isec)->this_hdr;
7863
7864 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7865 don't use the output section of the linked-to section since it
7866 may be NULL at this point. */
7867 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7868 {
7869 ohdr = &elf_section_data (osec)->this_hdr;
7870 ohdr->sh_flags |= SHF_LINK_ORDER;
7871 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7872 }
7873
7874 osec->use_rela_p = isec->use_rela_p;
7875
0a1b45a2 7876 return true;
ccd2ec6a
L
7877}
7878
252b5132
RH
7879/* Copy private section information. This copies over the entsize
7880 field, and sometimes the info field. */
7881
0a1b45a2 7882bool
217aa764
AM
7883_bfd_elf_copy_private_section_data (bfd *ibfd,
7884 asection *isec,
7885 bfd *obfd,
7886 asection *osec)
252b5132
RH
7887{
7888 Elf_Internal_Shdr *ihdr, *ohdr;
7889
7890 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7891 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7892 return true;
252b5132 7893
252b5132
RH
7894 ihdr = &elf_section_data (isec)->this_hdr;
7895 ohdr = &elf_section_data (osec)->this_hdr;
7896
7897 ohdr->sh_entsize = ihdr->sh_entsize;
7898
7899 if (ihdr->sh_type == SHT_SYMTAB
7900 || ihdr->sh_type == SHT_DYNSYM
7901 || ihdr->sh_type == SHT_GNU_verneed
7902 || ihdr->sh_type == SHT_GNU_verdef)
7903 ohdr->sh_info = ihdr->sh_info;
7904
ccd2ec6a
L
7905 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7906 NULL);
252b5132
RH
7907}
7908
d0bf826b
AM
7909/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7910 necessary if we are removing either the SHT_GROUP section or any of
7911 the group member sections. DISCARDED is the value that a section's
7912 output_section has if the section will be discarded, NULL when this
7913 function is called from objcopy, bfd_abs_section_ptr when called
7914 from the linker. */
80fccad2 7915
0a1b45a2 7916bool
d0bf826b 7917_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7918{
30288845
AM
7919 asection *isec;
7920
30288845 7921 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7922 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7923 {
7924 asection *first = elf_next_in_group (isec);
7925 asection *s = first;
d0bf826b
AM
7926 bfd_size_type removed = 0;
7927
30288845
AM
7928 while (s != NULL)
7929 {
415f38a6
AM
7930 /* If this member section is being output but the
7931 SHT_GROUP section is not, then clear the group info
7932 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7933 if (s->output_section != discarded
7934 && isec->output_section == discarded)
30288845
AM
7935 {
7936 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7937 elf_group_name (s->output_section) = NULL;
7938 }
3349112e 7939 else
6e5e9d58
AM
7940 {
7941 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7942 if (s->output_section == discarded
7943 && isec->output_section != discarded)
7944 {
7945 /* Conversely, if the member section is not being
7946 output but the SHT_GROUP section is, then adjust
7947 its size. */
7948 removed += 4;
7949 if (elf_sec->rel.hdr != NULL
7950 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7951 removed += 4;
7952 if (elf_sec->rela.hdr != NULL
7953 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7954 removed += 4;
7955 }
7956 else
7957 {
7958 /* Also adjust for zero-sized relocation member
7959 section. */
7960 if (elf_sec->rel.hdr != NULL
7961 && elf_sec->rel.hdr->sh_size == 0)
7962 removed += 4;
7963 if (elf_sec->rela.hdr != NULL
7964 && elf_sec->rela.hdr->sh_size == 0)
7965 removed += 4;
7966 }
6e5e9d58 7967 }
30288845
AM
7968 s = elf_next_in_group (s);
7969 if (s == first)
7970 break;
7971 }
d0bf826b
AM
7972 if (removed != 0)
7973 {
7974 if (discarded != NULL)
7975 {
7976 /* If we've been called for ld -r, then we need to
6e5e9d58 7977 adjust the input section size. */
d0bf826b
AM
7978 if (isec->rawsize == 0)
7979 isec->rawsize = isec->size;
7980 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7981 if (isec->size <= 4)
7982 {
7983 isec->size = 0;
7984 isec->flags |= SEC_EXCLUDE;
7985 }
d0bf826b
AM
7986 }
7987 else
7988 {
7989 /* Adjust the output section size when called from
7990 objcopy. */
7991 isec->output_section->size -= removed;
6e5e9d58
AM
7992 if (isec->output_section->size <= 4)
7993 {
7994 isec->output_section->size = 0;
7995 isec->output_section->flags |= SEC_EXCLUDE;
7996 }
d0bf826b
AM
7997 }
7998 }
30288845
AM
7999 }
8000
0a1b45a2 8001 return true;
80fccad2
BW
8002}
8003
d0bf826b
AM
8004/* Copy private header information. */
8005
0a1b45a2 8006bool
d0bf826b
AM
8007_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8008{
8009 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8010 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8011 return true;
d0bf826b
AM
8012
8013 /* Copy over private BFD data if it has not already been copied.
8014 This must be done here, rather than in the copy_private_bfd_data
8015 entry point, because the latter is called after the section
8016 contents have been set, which means that the program headers have
8017 already been worked out. */
12bd6957 8018 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8019 {
8020 if (! copy_private_bfd_data (ibfd, obfd))
0a1b45a2 8021 return false;
d0bf826b
AM
8022 }
8023
8024 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8025}
8026
252b5132
RH
8027/* Copy private symbol information. If this symbol is in a section
8028 which we did not map into a BFD section, try to map the section
8029 index correctly. We use special macro definitions for the mapped
8030 section indices; these definitions are interpreted by the
8031 swap_out_syms function. */
8032
9ad5cbcf
AM
8033#define MAP_ONESYMTAB (SHN_HIOS + 1)
8034#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8035#define MAP_STRTAB (SHN_HIOS + 3)
8036#define MAP_SHSTRTAB (SHN_HIOS + 4)
8037#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8038
0a1b45a2 8039bool
217aa764
AM
8040_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8041 asymbol *isymarg,
8042 bfd *obfd,
8043 asymbol *osymarg)
252b5132
RH
8044{
8045 elf_symbol_type *isym, *osym;
8046
8047 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8048 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8049 return true;
252b5132 8050
c1229f84
AM
8051 isym = elf_symbol_from (isymarg);
8052 osym = elf_symbol_from (osymarg);
252b5132
RH
8053
8054 if (isym != NULL
8424d8f5 8055 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8056 && osym != NULL
8057 && bfd_is_abs_section (isym->symbol.section))
8058 {
8059 unsigned int shndx;
8060
8061 shndx = isym->internal_elf_sym.st_shndx;
8062 if (shndx == elf_onesymtab (ibfd))
8063 shndx = MAP_ONESYMTAB;
8064 else if (shndx == elf_dynsymtab (ibfd))
8065 shndx = MAP_DYNSYMTAB;
12bd6957 8066 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8067 shndx = MAP_STRTAB;
12bd6957 8068 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8069 shndx = MAP_SHSTRTAB;
6a40cf0c 8070 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8071 shndx = MAP_SYM_SHNDX;
252b5132
RH
8072 osym->internal_elf_sym.st_shndx = shndx;
8073 }
8074
0a1b45a2 8075 return true;
252b5132
RH
8076}
8077
8078/* Swap out the symbols. */
8079
0a1b45a2 8080static bool
217aa764 8081swap_out_syms (bfd *abfd,
ef10c3ac 8082 struct elf_strtab_hash **sttp,
3d16b64e
NA
8083 int relocatable_p,
8084 struct bfd_link_info *info)
252b5132 8085{
9c5bfbb7 8086 const struct elf_backend_data *bed;
1f4361a7 8087 unsigned int symcount;
079e9a2f 8088 asymbol **syms;
ef10c3ac 8089 struct elf_strtab_hash *stt;
079e9a2f 8090 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8091 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8092 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8093 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8094 bfd_byte *outbound_syms;
8095 bfd_byte *outbound_shndx;
ef10c3ac
L
8096 unsigned long outbound_syms_index;
8097 unsigned long outbound_shndx_index;
1f4361a7 8098 unsigned int idx;
12bd6957 8099 unsigned int num_locals;
1f4361a7 8100 size_t amt;
0a1b45a2 8101 bool name_local_sections;
252b5132 8102
12bd6957 8103 if (!elf_map_symbols (abfd, &num_locals))
0a1b45a2 8104 return false;
252b5132 8105
c044fabd 8106 /* Dump out the symtabs. */
ef10c3ac 8107 stt = _bfd_elf_strtab_init ();
079e9a2f 8108 if (stt == NULL)
0a1b45a2 8109 return false;
252b5132 8110
079e9a2f
AM
8111 bed = get_elf_backend_data (abfd);
8112 symcount = bfd_get_symcount (abfd);
8113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8114 symtab_hdr->sh_type = SHT_SYMTAB;
8115 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8116 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8117 symtab_hdr->sh_info = num_locals + 1;
72de5009 8118 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8119
8120 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8121 symstrtab_hdr->sh_type = SHT_STRTAB;
8122
ef10c3ac 8123 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8124 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8125 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8126 {
1f4361a7 8127 bfd_set_error (bfd_error_no_memory);
ef10c3ac 8128 _bfd_elf_strtab_free (stt);
0a1b45a2 8129 return false;
ef10c3ac
L
8130 }
8131
1f4361a7
AM
8132 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8133 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8134 {
1f4361a7
AM
8135 error_no_mem:
8136 bfd_set_error (bfd_error_no_memory);
8137 error_return:
ef10c3ac 8138 free (symstrtab);
1f4361a7 8139 _bfd_elf_strtab_free (stt);
0a1b45a2 8140 return false;
5ed6aba4 8141 }
217aa764 8142 symtab_hdr->contents = outbound_syms;
ef10c3ac 8143 outbound_syms_index = 0;
252b5132 8144
9ad5cbcf 8145 outbound_shndx = NULL;
ef10c3ac 8146 outbound_shndx_index = 0;
6a40cf0c
NC
8147
8148 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8149 {
6a40cf0c
NC
8150 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8151 if (symtab_shndx_hdr->sh_name != 0)
8152 {
1f4361a7
AM
8153 if (_bfd_mul_overflow (symcount + 1,
8154 sizeof (Elf_External_Sym_Shndx), &amt))
8155 goto error_no_mem;
8156 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8157 if (outbound_shndx == NULL)
8158 goto error_return;
5ed6aba4 8159
6a40cf0c
NC
8160 symtab_shndx_hdr->contents = outbound_shndx;
8161 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8162 symtab_shndx_hdr->sh_size = amt;
8163 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8164 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8165 }
8166 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8167 }
8168
589e6347 8169 /* Now generate the data (for "contents"). */
079e9a2f
AM
8170 {
8171 /* Fill in zeroth symbol and swap it out. */
8172 Elf_Internal_Sym sym;
8173 sym.st_name = 0;
8174 sym.st_value = 0;
8175 sym.st_size = 0;
8176 sym.st_info = 0;
8177 sym.st_other = 0;
8178 sym.st_shndx = SHN_UNDEF;
35fc36a8 8179 sym.st_target_internal = 0;
ef10c3ac
L
8180 symstrtab[0].sym = sym;
8181 symstrtab[0].dest_index = outbound_syms_index;
8182 symstrtab[0].destshndx_index = outbound_shndx_index;
8183 outbound_syms_index++;
9ad5cbcf 8184 if (outbound_shndx != NULL)
ef10c3ac 8185 outbound_shndx_index++;
079e9a2f 8186 }
252b5132 8187
174fd7f9
RS
8188 name_local_sections
8189 = (bed->elf_backend_name_local_section_symbols
8190 && bed->elf_backend_name_local_section_symbols (abfd));
8191
079e9a2f 8192 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8193 for (idx = 0; idx < symcount;)
252b5132 8194 {
252b5132 8195 Elf_Internal_Sym sym;
079e9a2f
AM
8196 bfd_vma value = syms[idx]->value;
8197 elf_symbol_type *type_ptr;
8198 flagword flags = syms[idx]->flags;
8199 int type;
252b5132 8200
174fd7f9
RS
8201 if (!name_local_sections
8202 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8203 {
8204 /* Local section symbols have no name. */
ef10c3ac 8205 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8206 }
8207 else
8208 {
ef10c3ac
L
8209 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8210 to get the final offset for st_name. */
8211 sym.st_name
8212 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
0a1b45a2 8213 false);
079e9a2f 8214 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8215 goto error_return;
079e9a2f 8216 }
252b5132 8217
c1229f84 8218 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8219
079e9a2f
AM
8220 if ((flags & BSF_SECTION_SYM) == 0
8221 && bfd_is_com_section (syms[idx]->section))
8222 {
8223 /* ELF common symbols put the alignment into the `value' field,
8224 and the size into the `size' field. This is backwards from
8225 how BFD handles it, so reverse it here. */
8226 sym.st_size = value;
8227 if (type_ptr == NULL
8228 || type_ptr->internal_elf_sym.st_value == 0)
8229 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8230 else
8231 sym.st_value = type_ptr->internal_elf_sym.st_value;
8232 sym.st_shndx = _bfd_elf_section_from_bfd_section
8233 (abfd, syms[idx]->section);
8234 }
8235 else
8236 {
8237 asection *sec = syms[idx]->section;
cb33740c 8238 unsigned int shndx;
252b5132 8239
079e9a2f
AM
8240 if (sec->output_section)
8241 {
8242 value += sec->output_offset;
8243 sec = sec->output_section;
8244 }
589e6347 8245
079e9a2f
AM
8246 /* Don't add in the section vma for relocatable output. */
8247 if (! relocatable_p)
8248 value += sec->vma;
8249 sym.st_value = value;
8250 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8251
8252 if (bfd_is_abs_section (sec)
8253 && type_ptr != NULL
8254 && type_ptr->internal_elf_sym.st_shndx != 0)
8255 {
8256 /* This symbol is in a real ELF section which we did
8257 not create as a BFD section. Undo the mapping done
8258 by copy_private_symbol_data. */
8259 shndx = type_ptr->internal_elf_sym.st_shndx;
8260 switch (shndx)
8261 {
8262 case MAP_ONESYMTAB:
8263 shndx = elf_onesymtab (abfd);
8264 break;
8265 case MAP_DYNSYMTAB:
8266 shndx = elf_dynsymtab (abfd);
8267 break;
8268 case MAP_STRTAB:
12bd6957 8269 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8270 break;
8271 case MAP_SHSTRTAB:
12bd6957 8272 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8273 break;
9ad5cbcf 8274 case MAP_SYM_SHNDX:
6a40cf0c
NC
8275 if (elf_symtab_shndx_list (abfd))
8276 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8277 break;
00e49dff
NC
8278 case SHN_COMMON:
8279 case SHN_ABS:
15bc576a 8280 shndx = SHN_ABS;
079e9a2f 8281 break;
00e49dff
NC
8282 default:
8283 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8284 {
8285 if (bed->symbol_section_index)
8286 shndx = bed->symbol_section_index (abfd, type_ptr);
8287 /* Otherwise just leave the index alone. */
8288 }
8289 else
8290 {
8291 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8292 _bfd_error_handler (_("%pB: \
8293Unable to handle section index %x in ELF symbol. Using ABS instead."),
8294 abfd, shndx);
8295 shndx = SHN_ABS;
8296 }
8297 break;
079e9a2f
AM
8298 }
8299 }
8300 else
8301 {
8302 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8303
cb33740c 8304 if (shndx == SHN_BAD)
079e9a2f
AM
8305 {
8306 asection *sec2;
8307
8308 /* Writing this would be a hell of a lot easier if
8309 we had some decent documentation on bfd, and
8310 knew what to expect of the library, and what to
8311 demand of applications. For example, it
8312 appears that `objcopy' might not set the
8313 section of a symbol to be a section that is
8314 actually in the output file. */
8315 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8316 if (sec2 != NULL)
8317 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8318 if (shndx == SHN_BAD)
589e6347 8319 {
695344c0 8320 /* xgettext:c-format */
9793eb77
AM
8321 _bfd_error_handler
8322 (_("unable to find equivalent output section"
8323 " for symbol '%s' from section '%s'"),
8324 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8325 sec->name);
811072d8 8326 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8327 goto error_return;
589e6347 8328 }
079e9a2f
AM
8329 }
8330 }
252b5132 8331
079e9a2f
AM
8332 sym.st_shndx = shndx;
8333 }
252b5132 8334
13ae64f3
JJ
8335 if ((flags & BSF_THREAD_LOCAL) != 0)
8336 type = STT_TLS;
d8045f23
NC
8337 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8338 type = STT_GNU_IFUNC;
13ae64f3 8339 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8340 type = STT_FUNC;
8341 else if ((flags & BSF_OBJECT) != 0)
8342 type = STT_OBJECT;
d9352518
DB
8343 else if ((flags & BSF_RELC) != 0)
8344 type = STT_RELC;
8345 else if ((flags & BSF_SRELC) != 0)
8346 type = STT_SRELC;
079e9a2f
AM
8347 else
8348 type = STT_NOTYPE;
252b5132 8349
13ae64f3
JJ
8350 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8351 type = STT_TLS;
8352
589e6347 8353 /* Processor-specific types. */
079e9a2f
AM
8354 if (type_ptr != NULL
8355 && bed->elf_backend_get_symbol_type)
8356 type = ((*bed->elf_backend_get_symbol_type)
8357 (&type_ptr->internal_elf_sym, type));
252b5132 8358
079e9a2f
AM
8359 if (flags & BSF_SECTION_SYM)
8360 {
8361 if (flags & BSF_GLOBAL)
8362 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8363 else
8364 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8365 }
8366 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8367 {
b8871f35
L
8368 if (type != STT_TLS)
8369 {
8370 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8371 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8372 ? STT_COMMON : STT_OBJECT);
8373 else
8374 type = ((flags & BSF_ELF_COMMON) != 0
8375 ? STT_COMMON : STT_OBJECT);
8376 }
8377 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8378 }
079e9a2f
AM
8379 else if (bfd_is_und_section (syms[idx]->section))
8380 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8381 ? STB_WEAK
8382 : STB_GLOBAL),
8383 type);
8384 else if (flags & BSF_FILE)
8385 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8386 else
8387 {
8388 int bind = STB_LOCAL;
252b5132 8389
079e9a2f
AM
8390 if (flags & BSF_LOCAL)
8391 bind = STB_LOCAL;
3e7a7d11
NC
8392 else if (flags & BSF_GNU_UNIQUE)
8393 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8394 else if (flags & BSF_WEAK)
8395 bind = STB_WEAK;
8396 else if (flags & BSF_GLOBAL)
8397 bind = STB_GLOBAL;
252b5132 8398
079e9a2f
AM
8399 sym.st_info = ELF_ST_INFO (bind, type);
8400 }
252b5132 8401
079e9a2f 8402 if (type_ptr != NULL)
35fc36a8
RS
8403 {
8404 sym.st_other = type_ptr->internal_elf_sym.st_other;
8405 sym.st_target_internal
8406 = type_ptr->internal_elf_sym.st_target_internal;
8407 }
079e9a2f 8408 else
35fc36a8
RS
8409 {
8410 sym.st_other = 0;
8411 sym.st_target_internal = 0;
8412 }
252b5132 8413
ef10c3ac
L
8414 idx++;
8415 symstrtab[idx].sym = sym;
8416 symstrtab[idx].dest_index = outbound_syms_index;
8417 symstrtab[idx].destshndx_index = outbound_shndx_index;
8418
8419 outbound_syms_index++;
9ad5cbcf 8420 if (outbound_shndx != NULL)
ef10c3ac
L
8421 outbound_shndx_index++;
8422 }
8423
8424 /* Finalize the .strtab section. */
8425 _bfd_elf_strtab_finalize (stt);
8426
8427 /* Swap out the .strtab section. */
8428 for (idx = 0; idx <= symcount; idx++)
8429 {
8430 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8431 if (elfsym->sym.st_name == (unsigned long) -1)
8432 elfsym->sym.st_name = 0;
8433 else
8434 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8435 elfsym->sym.st_name);
3d16b64e
NA
8436 if (info && info->callbacks->ctf_new_symbol)
8437 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8438 &elfsym->sym);
8439
8440 /* Inform the linker of the addition of this symbol. */
8441
ef10c3ac
L
8442 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8443 (outbound_syms
8444 + (elfsym->dest_index
8445 * bed->s->sizeof_sym)),
8446 (outbound_shndx
8447 + (elfsym->destshndx_index
8448 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8449 }
ef10c3ac 8450 free (symstrtab);
252b5132 8451
079e9a2f 8452 *sttp = stt;
ef10c3ac 8453 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8454 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8455 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8456 symstrtab_hdr->sh_addr = 0;
8457 symstrtab_hdr->sh_entsize = 0;
8458 symstrtab_hdr->sh_link = 0;
8459 symstrtab_hdr->sh_info = 0;
8460 symstrtab_hdr->sh_addralign = 1;
252b5132 8461
0a1b45a2 8462 return true;
252b5132
RH
8463}
8464
8465/* Return the number of bytes required to hold the symtab vector.
8466
8467 Note that we base it on the count plus 1, since we will null terminate
8468 the vector allocated based on this size. However, the ELF symbol table
8469 always has a dummy entry as symbol #0, so it ends up even. */
8470
8471long
217aa764 8472_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8473{
3a551c7a 8474 bfd_size_type symcount;
252b5132
RH
8475 long symtab_size;
8476 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8477
8478 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8479 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8480 {
8481 bfd_set_error (bfd_error_file_too_big);
8482 return -1;
8483 }
b5f386d5
AM
8484 symtab_size = symcount * (sizeof (asymbol *));
8485 if (symcount == 0)
8486 symtab_size = sizeof (asymbol *);
8487 else if (!bfd_write_p (abfd))
8488 {
8489 ufile_ptr filesize = bfd_get_file_size (abfd);
8490
8491 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8492 {
8493 bfd_set_error (bfd_error_file_truncated);
8494 return -1;
8495 }
8496 }
252b5132
RH
8497
8498 return symtab_size;
8499}
8500
8501long
217aa764 8502_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8503{
3a551c7a 8504 bfd_size_type symcount;
252b5132
RH
8505 long symtab_size;
8506 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8507
8508 if (elf_dynsymtab (abfd) == 0)
8509 {
8510 bfd_set_error (bfd_error_invalid_operation);
8511 return -1;
8512 }
8513
8514 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8515 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8516 {
8517 bfd_set_error (bfd_error_file_too_big);
8518 return -1;
8519 }
b5f386d5
AM
8520 symtab_size = symcount * (sizeof (asymbol *));
8521 if (symcount == 0)
8522 symtab_size = sizeof (asymbol *);
8523 else if (!bfd_write_p (abfd))
8524 {
8525 ufile_ptr filesize = bfd_get_file_size (abfd);
8526
8527 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8528 {
8529 bfd_set_error (bfd_error_file_truncated);
8530 return -1;
8531 }
8532 }
252b5132
RH
8533
8534 return symtab_size;
8535}
8536
8537long
3c568b8a 8538_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8539{
b5f386d5 8540 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8541 {
8542 /* Sanity check reloc section size. */
8543 struct bfd_elf_section_data *d = elf_section_data (asect);
8544 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8545 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8546 ufile_ptr filesize = bfd_get_file_size (abfd);
8547
8548 if (filesize != 0 && ext_rel_size > filesize)
8549 {
8550 bfd_set_error (bfd_error_file_truncated);
8551 return -1;
8552 }
8553 }
8554
242a1159 8555#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8556 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8557 {
8558 bfd_set_error (bfd_error_file_too_big);
8559 return -1;
8560 }
242a1159 8561#endif
252b5132
RH
8562 return (asect->reloc_count + 1) * sizeof (arelent *);
8563}
8564
8565/* Canonicalize the relocs. */
8566
8567long
217aa764
AM
8568_bfd_elf_canonicalize_reloc (bfd *abfd,
8569 sec_ptr section,
8570 arelent **relptr,
8571 asymbol **symbols)
252b5132
RH
8572{
8573 arelent *tblptr;
8574 unsigned int i;
9c5bfbb7 8575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8576
0a1b45a2 8577 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
8578 return -1;
8579
8580 tblptr = section->relocation;
8581 for (i = 0; i < section->reloc_count; i++)
8582 *relptr++ = tblptr++;
8583
8584 *relptr = NULL;
8585
8586 return section->reloc_count;
8587}
8588
8589long
6cee3f79 8590_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8591{
9c5bfbb7 8592 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8593 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
252b5132
RH
8594
8595 if (symcount >= 0)
ed48ec2e 8596 abfd->symcount = symcount;
252b5132
RH
8597 return symcount;
8598}
8599
8600long
217aa764
AM
8601_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8602 asymbol **allocation)
252b5132 8603{
9c5bfbb7 8604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8605 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
1f70368c
DJ
8606
8607 if (symcount >= 0)
ed48ec2e 8608 abfd->dynsymcount = symcount;
1f70368c 8609 return symcount;
252b5132
RH
8610}
8611
8615f3f2
AM
8612/* Return the size required for the dynamic reloc entries. Any loadable
8613 section that was actually installed in the BFD, and has type SHT_REL
8614 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8615 dynamic reloc section. */
252b5132
RH
8616
8617long
217aa764 8618_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8619{
3c568b8a 8620 bfd_size_type count, ext_rel_size;
252b5132
RH
8621 asection *s;
8622
8623 if (elf_dynsymtab (abfd) == 0)
8624 {
8625 bfd_set_error (bfd_error_invalid_operation);
8626 return -1;
8627 }
8628
3a551c7a 8629 count = 1;
3c568b8a 8630 ext_rel_size = 0;
252b5132 8631 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8632 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8633 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8634 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8635 {
3c568b8a
AM
8636 ext_rel_size += s->size;
8637 if (ext_rel_size < s->size)
8638 {
8639 bfd_set_error (bfd_error_file_truncated);
8640 return -1;
8641 }
3a551c7a
AM
8642 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8643 if (count > LONG_MAX / sizeof (arelent *))
8644 {
8645 bfd_set_error (bfd_error_file_too_big);
8646 return -1;
8647 }
8648 }
b5f386d5 8649 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8650 {
8651 /* Sanity check reloc section sizes. */
8652 ufile_ptr filesize = bfd_get_file_size (abfd);
8653 if (filesize != 0 && ext_rel_size > filesize)
8654 {
8655 bfd_set_error (bfd_error_file_truncated);
8656 return -1;
8657 }
8658 }
3a551c7a 8659 return count * sizeof (arelent *);
252b5132
RH
8660}
8661
8615f3f2
AM
8662/* Canonicalize the dynamic relocation entries. Note that we return the
8663 dynamic relocations as a single block, although they are actually
8664 associated with particular sections; the interface, which was
8665 designed for SunOS style shared libraries, expects that there is only
8666 one set of dynamic relocs. Any loadable section that was actually
8667 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8668 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8669
8670long
217aa764
AM
8671_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8672 arelent **storage,
8673 asymbol **syms)
252b5132 8674{
0a1b45a2 8675 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
252b5132
RH
8676 asection *s;
8677 long ret;
8678
8679 if (elf_dynsymtab (abfd) == 0)
8680 {
8681 bfd_set_error (bfd_error_invalid_operation);
8682 return -1;
8683 }
8684
8685 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8686 ret = 0;
8687 for (s = abfd->sections; s != NULL; s = s->next)
8688 {
266b05cf 8689 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8690 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8691 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8692 {
8693 arelent *p;
8694 long count, i;
8695
0a1b45a2 8696 if (! (*slurp_relocs) (abfd, s, syms, true))
252b5132 8697 return -1;
eea6121a 8698 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8699 p = s->relocation;
8700 for (i = 0; i < count; i++)
8701 *storage++ = p++;
8702 ret += count;
8703 }
8704 }
8705
8706 *storage = NULL;
8707
8708 return ret;
8709}
8710\f
8711/* Read in the version information. */
8712
0a1b45a2
AM
8713bool
8714_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
252b5132
RH
8715{
8716 bfd_byte *contents = NULL;
fc0e6df6 8717 unsigned int freeidx = 0;
1f4361a7 8718 size_t amt;
fc0e6df6
PB
8719
8720 if (elf_dynverref (abfd) != 0)
8721 {
8722 Elf_Internal_Shdr *hdr;
8723 Elf_External_Verneed *everneed;
8724 Elf_Internal_Verneed *iverneed;
8725 unsigned int i;
d0fb9a8d 8726 bfd_byte *contents_end;
fc0e6df6
PB
8727
8728 hdr = &elf_tdata (abfd)->dynverref_hdr;
8729
bd61e135
AM
8730 if (hdr->sh_info == 0
8731 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8732 {
dc1e8a47 8733 error_return_bad_verref:
4eca0228 8734 _bfd_error_handler
871b3ab2 8735 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8736 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8737 error_return_verref:
d0fb9a8d
JJ
8738 elf_tdata (abfd)->verref = NULL;
8739 elf_tdata (abfd)->cverrefs = 0;
8740 goto error_return;
8741 }
601a03ba 8742
2bb3687b
AM
8743 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8744 goto error_return_verref;
8745 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8746 if (contents == NULL)
d0fb9a8d 8747 goto error_return_verref;
fc0e6df6 8748
1f4361a7
AM
8749 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8750 {
8751 bfd_set_error (bfd_error_file_too_big);
8752 goto error_return_verref;
8753 }
8754 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8755 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8756 goto error_return_verref;
8757
8758 BFD_ASSERT (sizeof (Elf_External_Verneed)
8759 == sizeof (Elf_External_Vernaux));
8760 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8761 everneed = (Elf_External_Verneed *) contents;
8762 iverneed = elf_tdata (abfd)->verref;
8763 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8764 {
8765 Elf_External_Vernaux *evernaux;
8766 Elf_Internal_Vernaux *ivernaux;
8767 unsigned int j;
8768
8769 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8770
8771 iverneed->vn_bfd = abfd;
8772
8773 iverneed->vn_filename =
8774 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8775 iverneed->vn_file);
8776 if (iverneed->vn_filename == NULL)
601a03ba 8777 goto error_return_bad_verref;
fc0e6df6 8778
d0fb9a8d
JJ
8779 if (iverneed->vn_cnt == 0)
8780 iverneed->vn_auxptr = NULL;
8781 else
8782 {
1f4361a7
AM
8783 if (_bfd_mul_overflow (iverneed->vn_cnt,
8784 sizeof (Elf_Internal_Vernaux), &amt))
8785 {
8786 bfd_set_error (bfd_error_file_too_big);
8787 goto error_return_verref;
8788 }
a50b1753 8789 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8790 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8791 if (iverneed->vn_auxptr == NULL)
8792 goto error_return_verref;
8793 }
8794
8795 if (iverneed->vn_aux
8796 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8797 goto error_return_bad_verref;
fc0e6df6
PB
8798
8799 evernaux = ((Elf_External_Vernaux *)
8800 ((bfd_byte *) everneed + iverneed->vn_aux));
8801 ivernaux = iverneed->vn_auxptr;
8802 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8803 {
8804 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8805
8806 ivernaux->vna_nodename =
8807 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8808 ivernaux->vna_name);
8809 if (ivernaux->vna_nodename == NULL)
601a03ba 8810 goto error_return_bad_verref;
fc0e6df6 8811
25ff461f
AM
8812 if (ivernaux->vna_other > freeidx)
8813 freeidx = ivernaux->vna_other;
8814
8815 ivernaux->vna_nextptr = NULL;
8816 if (ivernaux->vna_next == 0)
8817 {
8818 iverneed->vn_cnt = j + 1;
8819 break;
8820 }
fc0e6df6
PB
8821 if (j + 1 < iverneed->vn_cnt)
8822 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8823
d0fb9a8d
JJ
8824 if (ivernaux->vna_next
8825 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8826 goto error_return_bad_verref;
d0fb9a8d 8827
fc0e6df6
PB
8828 evernaux = ((Elf_External_Vernaux *)
8829 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8830 }
8831
25ff461f
AM
8832 iverneed->vn_nextref = NULL;
8833 if (iverneed->vn_next == 0)
8834 break;
fc0e6df6
PB
8835 if (i + 1 < hdr->sh_info)
8836 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8837
d0fb9a8d
JJ
8838 if (iverneed->vn_next
8839 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8840 goto error_return_bad_verref;
d0fb9a8d 8841
fc0e6df6
PB
8842 everneed = ((Elf_External_Verneed *)
8843 ((bfd_byte *) everneed + iverneed->vn_next));
8844 }
25ff461f 8845 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8846
8847 free (contents);
8848 contents = NULL;
8849 }
252b5132
RH
8850
8851 if (elf_dynverdef (abfd) != 0)
8852 {
8853 Elf_Internal_Shdr *hdr;
8854 Elf_External_Verdef *everdef;
8855 Elf_Internal_Verdef *iverdef;
f631889e
UD
8856 Elf_Internal_Verdef *iverdefarr;
8857 Elf_Internal_Verdef iverdefmem;
252b5132 8858 unsigned int i;
062e2358 8859 unsigned int maxidx;
d0fb9a8d 8860 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8861
8862 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8863
601a03ba
AM
8864 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8865 {
8866 error_return_bad_verdef:
4eca0228 8867 _bfd_error_handler
871b3ab2 8868 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8869 bfd_set_error (bfd_error_bad_value);
8870 error_return_verdef:
8871 elf_tdata (abfd)->verdef = NULL;
8872 elf_tdata (abfd)->cverdefs = 0;
8873 goto error_return;
8874 }
8875
2bb3687b 8876 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8877 goto error_return_verdef;
2bb3687b
AM
8878 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8879 if (contents == NULL)
601a03ba 8880 goto error_return_verdef;
d0fb9a8d
JJ
8881
8882 BFD_ASSERT (sizeof (Elf_External_Verdef)
8883 >= sizeof (Elf_External_Verdaux));
8884 contents_end_def = contents + hdr->sh_size
8885 - sizeof (Elf_External_Verdef);
8886 contents_end_aux = contents + hdr->sh_size
8887 - sizeof (Elf_External_Verdaux);
8888
f631889e
UD
8889 /* We know the number of entries in the section but not the maximum
8890 index. Therefore we have to run through all entries and find
8891 the maximum. */
252b5132 8892 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8893 maxidx = 0;
8894 for (i = 0; i < hdr->sh_info; ++i)
8895 {
8896 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8897
601a03ba
AM
8898 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8899 goto error_return_bad_verdef;
062e2358
AM
8900 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8901 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8902
25ff461f
AM
8903 if (iverdefmem.vd_next == 0)
8904 break;
8905
d0fb9a8d
JJ
8906 if (iverdefmem.vd_next
8907 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8908 goto error_return_bad_verdef;
d0fb9a8d 8909
f631889e
UD
8910 everdef = ((Elf_External_Verdef *)
8911 ((bfd_byte *) everdef + iverdefmem.vd_next));
8912 }
8913
fc0e6df6
PB
8914 if (default_imported_symver)
8915 {
8916 if (freeidx > maxidx)
8917 maxidx = ++freeidx;
8918 else
8919 freeidx = ++maxidx;
8920 }
1f4361a7
AM
8921 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8922 {
8923 bfd_set_error (bfd_error_file_too_big);
8924 goto error_return_verdef;
8925 }
8926 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8927 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8928 goto error_return_verdef;
f631889e
UD
8929
8930 elf_tdata (abfd)->cverdefs = maxidx;
8931
8932 everdef = (Elf_External_Verdef *) contents;
8933 iverdefarr = elf_tdata (abfd)->verdef;
8934 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8935 {
8936 Elf_External_Verdaux *everdaux;
8937 Elf_Internal_Verdaux *iverdaux;
8938 unsigned int j;
8939
f631889e
UD
8940 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8941
d0fb9a8d 8942 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8943 goto error_return_bad_verdef;
d0fb9a8d 8944
f631889e 8945 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8946 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8947
8948 iverdef->vd_bfd = abfd;
8949
d0fb9a8d
JJ
8950 if (iverdef->vd_cnt == 0)
8951 iverdef->vd_auxptr = NULL;
8952 else
8953 {
1f4361a7
AM
8954 if (_bfd_mul_overflow (iverdef->vd_cnt,
8955 sizeof (Elf_Internal_Verdaux), &amt))
8956 {
8957 bfd_set_error (bfd_error_file_too_big);
8958 goto error_return_verdef;
8959 }
a50b1753 8960 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8961 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8962 if (iverdef->vd_auxptr == NULL)
8963 goto error_return_verdef;
8964 }
8965
8966 if (iverdef->vd_aux
8967 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8968 goto error_return_bad_verdef;
252b5132
RH
8969
8970 everdaux = ((Elf_External_Verdaux *)
8971 ((bfd_byte *) everdef + iverdef->vd_aux));
8972 iverdaux = iverdef->vd_auxptr;
8973 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8974 {
8975 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8976
8977 iverdaux->vda_nodename =
8978 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8979 iverdaux->vda_name);
8980 if (iverdaux->vda_nodename == NULL)
601a03ba 8981 goto error_return_bad_verdef;
252b5132 8982
25ff461f
AM
8983 iverdaux->vda_nextptr = NULL;
8984 if (iverdaux->vda_next == 0)
8985 {
8986 iverdef->vd_cnt = j + 1;
8987 break;
8988 }
252b5132
RH
8989 if (j + 1 < iverdef->vd_cnt)
8990 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8991
d0fb9a8d
JJ
8992 if (iverdaux->vda_next
8993 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8994 goto error_return_bad_verdef;
d0fb9a8d 8995
252b5132
RH
8996 everdaux = ((Elf_External_Verdaux *)
8997 ((bfd_byte *) everdaux + iverdaux->vda_next));
8998 }
8999
595bce75 9000 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
9001 if (iverdef->vd_cnt)
9002 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 9003
25ff461f
AM
9004 iverdef->vd_nextdef = NULL;
9005 if (iverdef->vd_next == 0)
9006 break;
d0fb9a8d 9007 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 9008 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
9009
9010 everdef = ((Elf_External_Verdef *)
9011 ((bfd_byte *) everdef + iverdef->vd_next));
9012 }
9013
9014 free (contents);
9015 contents = NULL;
9016 }
fc0e6df6 9017 else if (default_imported_symver)
252b5132 9018 {
fc0e6df6
PB
9019 if (freeidx < 3)
9020 freeidx = 3;
9021 else
9022 freeidx++;
252b5132 9023
1f4361a7
AM
9024 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9025 {
9026 bfd_set_error (bfd_error_file_too_big);
9027 goto error_return;
9028 }
9029 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9030 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9031 goto error_return;
9032
fc0e6df6
PB
9033 elf_tdata (abfd)->cverdefs = freeidx;
9034 }
252b5132 9035
fc0e6df6
PB
9036 /* Create a default version based on the soname. */
9037 if (default_imported_symver)
9038 {
9039 Elf_Internal_Verdef *iverdef;
9040 Elf_Internal_Verdaux *iverdaux;
252b5132 9041
5bb3703f 9042 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9043
fc0e6df6
PB
9044 iverdef->vd_version = VER_DEF_CURRENT;
9045 iverdef->vd_flags = 0;
9046 iverdef->vd_ndx = freeidx;
9047 iverdef->vd_cnt = 1;
252b5132 9048
fc0e6df6 9049 iverdef->vd_bfd = abfd;
252b5132 9050
fc0e6df6
PB
9051 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9052 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9053 goto error_return_verdef;
fc0e6df6 9054 iverdef->vd_nextdef = NULL;
601a03ba
AM
9055 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9056 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9057 if (iverdef->vd_auxptr == NULL)
9058 goto error_return_verdef;
252b5132 9059
fc0e6df6
PB
9060 iverdaux = iverdef->vd_auxptr;
9061 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9062 }
9063
0a1b45a2 9064 return true;
252b5132
RH
9065
9066 error_return:
c9594989 9067 free (contents);
0a1b45a2 9068 return false;
252b5132
RH
9069}
9070\f
9071asymbol *
217aa764 9072_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9073{
9074 elf_symbol_type *newsym;
9075
7a6e0d89 9076 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9077 if (!newsym)
9078 return NULL;
201159ec
NC
9079 newsym->symbol.the_bfd = abfd;
9080 return &newsym->symbol;
252b5132
RH
9081}
9082
9083void
217aa764
AM
9084_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9085 asymbol *symbol,
9086 symbol_info *ret)
252b5132
RH
9087{
9088 bfd_symbol_info (symbol, ret);
9089}
9090
9091/* Return whether a symbol name implies a local symbol. Most targets
9092 use this function for the is_local_label_name entry point, but some
9093 override it. */
9094
0a1b45a2 9095bool
217aa764
AM
9096_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9097 const char *name)
252b5132
RH
9098{
9099 /* Normal local symbols start with ``.L''. */
9100 if (name[0] == '.' && name[1] == 'L')
0a1b45a2 9101 return true;
252b5132
RH
9102
9103 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9104 DWARF debugging symbols starting with ``..''. */
9105 if (name[0] == '.' && name[1] == '.')
0a1b45a2 9106 return true;
252b5132
RH
9107
9108 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9109 emitting DWARF debugging output. I suspect this is actually a
9110 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9111 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9112 underscore to be emitted on some ELF targets). For ease of use,
9113 we treat such symbols as local. */
9114 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
0a1b45a2 9115 return true;
252b5132 9116
b1fa9dd6
NC
9117 /* Treat assembler generated fake symbols, dollar local labels and
9118 forward-backward labels (aka local labels) as locals.
9119 These labels have the form:
9120
07d6d2b8 9121 L0^A.* (fake symbols)
b1fa9dd6
NC
9122
9123 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9124
9125 Versions which start with .L will have already been matched above,
9126 so we only need to match the rest. */
9127 if (name[0] == 'L' && ISDIGIT (name[1]))
9128 {
0a1b45a2 9129 bool ret = false;
b1fa9dd6
NC
9130 const char * p;
9131 char c;
9132
9133 for (p = name + 2; (c = *p); p++)
9134 {
9135 if (c == 1 || c == 2)
9136 {
9137 if (c == 1 && p == name + 2)
9138 /* A fake symbol. */
0a1b45a2 9139 return true;
b1fa9dd6
NC
9140
9141 /* FIXME: We are being paranoid here and treating symbols like
9142 L0^Bfoo as if there were non-local, on the grounds that the
9143 assembler will never generate them. But can any symbol
9144 containing an ASCII value in the range 1-31 ever be anything
9145 other than some kind of local ? */
0a1b45a2 9146 ret = true;
b1fa9dd6
NC
9147 }
9148
9149 if (! ISDIGIT (c))
9150 {
0a1b45a2 9151 ret = false;
b1fa9dd6
NC
9152 break;
9153 }
9154 }
9155 return ret;
9156 }
ffa54770 9157
0a1b45a2 9158 return false;
252b5132
RH
9159}
9160
9161alent *
217aa764
AM
9162_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9163 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9164{
9165 abort ();
9166 return NULL;
9167}
9168
0a1b45a2 9169bool
217aa764
AM
9170_bfd_elf_set_arch_mach (bfd *abfd,
9171 enum bfd_architecture arch,
9172 unsigned long machine)
252b5132
RH
9173{
9174 /* If this isn't the right architecture for this backend, and this
9175 isn't the generic backend, fail. */
9176 if (arch != get_elf_backend_data (abfd)->arch
9177 && arch != bfd_arch_unknown
9178 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
0a1b45a2 9179 return false;
252b5132
RH
9180
9181 return bfd_default_set_arch_mach (abfd, arch, machine);
9182}
9183
d1fad7c6
NC
9184/* Find the nearest line to a particular section and offset,
9185 for error reporting. */
9186
0a1b45a2 9187bool
217aa764 9188_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9189 asymbol **symbols,
fb167eb2 9190 asection *section,
217aa764
AM
9191 bfd_vma offset,
9192 const char **filename_ptr,
9193 const char **functionname_ptr,
fb167eb2
AM
9194 unsigned int *line_ptr,
9195 unsigned int *discriminator_ptr)
d1fad7c6 9196{
0a1b45a2 9197 bool found;
d1fad7c6 9198
fb167eb2 9199 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9200 filename_ptr, functionname_ptr,
fb167eb2 9201 line_ptr, discriminator_ptr,
9defd221 9202 dwarf_debug_sections,
e7679060 9203 &elf_tdata (abfd)->dwarf2_find_line_info))
0a1b45a2 9204 return true;
e7679060
AM
9205
9206 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9207 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9208 {
9209 if (!*functionname_ptr)
e00e8198
AM
9210 _bfd_elf_find_function (abfd, symbols, section, offset,
9211 *filename_ptr ? NULL : filename_ptr,
9212 functionname_ptr);
0a1b45a2 9213 return true;
d1fad7c6
NC
9214 }
9215
9216 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9217 &found, filename_ptr,
9218 functionname_ptr, line_ptr,
9219 &elf_tdata (abfd)->line_info))
0a1b45a2 9220 return false;
dc43ada5 9221 if (found && (*functionname_ptr || *line_ptr))
0a1b45a2 9222 return true;
d1fad7c6
NC
9223
9224 if (symbols == NULL)
0a1b45a2 9225 return false;
d1fad7c6 9226
e00e8198
AM
9227 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9228 filename_ptr, functionname_ptr))
0a1b45a2 9229 return false;
d1fad7c6 9230
252b5132 9231 *line_ptr = 0;
0a1b45a2 9232 return true;
252b5132
RH
9233}
9234
5420f73d
L
9235/* Find the line for a symbol. */
9236
0a1b45a2 9237bool
5420f73d
L
9238_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9239 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9240{
fb167eb2
AM
9241 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9242 filename_ptr, NULL, line_ptr, NULL,
9defd221 9243 dwarf_debug_sections,
fb167eb2 9244 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9245}
9246
4ab527b0
FF
9247/* After a call to bfd_find_nearest_line, successive calls to
9248 bfd_find_inliner_info can be used to get source information about
9249 each level of function inlining that terminated at the address
9250 passed to bfd_find_nearest_line. Currently this is only supported
9251 for DWARF2 with appropriate DWARF3 extensions. */
9252
0a1b45a2 9253bool
4ab527b0
FF
9254_bfd_elf_find_inliner_info (bfd *abfd,
9255 const char **filename_ptr,
9256 const char **functionname_ptr,
9257 unsigned int *line_ptr)
9258{
0a1b45a2 9259 bool found;
4ab527b0
FF
9260 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9261 functionname_ptr, line_ptr,
9262 & elf_tdata (abfd)->dwarf2_find_line_info);
9263 return found;
9264}
9265
252b5132 9266int
a6b96beb 9267_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9268{
8ded5a0f
AM
9269 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9270 int ret = bed->s->sizeof_ehdr;
252b5132 9271
0e1862bb 9272 if (!bfd_link_relocatable (info))
8ded5a0f 9273 {
12bd6957 9274 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9275
62d7a5f6
AM
9276 if (phdr_size == (bfd_size_type) -1)
9277 {
9278 struct elf_segment_map *m;
9279
9280 phdr_size = 0;
12bd6957 9281 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9282 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9283
62d7a5f6
AM
9284 if (phdr_size == 0)
9285 phdr_size = get_program_header_size (abfd, info);
9286 }
8ded5a0f 9287
12bd6957 9288 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9289 ret += phdr_size;
9290 }
9291
252b5132
RH
9292 return ret;
9293}
9294
0a1b45a2 9295bool
217aa764
AM
9296_bfd_elf_set_section_contents (bfd *abfd,
9297 sec_ptr section,
0f867abe 9298 const void *location,
217aa764
AM
9299 file_ptr offset,
9300 bfd_size_type count)
252b5132
RH
9301{
9302 Elf_Internal_Shdr *hdr;
1b6aeedb 9303 file_ptr pos;
252b5132
RH
9304
9305 if (! abfd->output_has_begun
217aa764 9306 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 9307 return false;
252b5132 9308
0ce398f1 9309 if (!count)
0a1b45a2 9310 return true;
0ce398f1 9311
252b5132 9312 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9313 if (hdr->sh_offset == (file_ptr) -1)
9314 {
a0dcf297
NC
9315 unsigned char *contents;
9316
1ff6de03
NA
9317 if (bfd_section_is_ctf (section))
9318 /* Nothing to do with this section: the contents are generated
9319 later. */
0a1b45a2 9320 return true;
1ff6de03 9321
a0dcf297
NC
9322 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9323 {
9324 _bfd_error_handler
9325 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9326 abfd, section);
9327 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9328 return false;
a0dcf297
NC
9329 }
9330
9331 if ((offset + count) > hdr->sh_size)
9332 {
9333 _bfd_error_handler
9334 (_("%pB:%pA: error: attempting to write over the end of the section"),
9335 abfd, section);
9336
9337 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9338 return false;
a0dcf297
NC
9339 }
9340
9341 contents = hdr->contents;
9342 if (contents == NULL)
9343 {
9344 _bfd_error_handler
9345 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9346 abfd, section);
9347
9348 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9349 return false;
a0dcf297
NC
9350 }
9351
0ce398f1 9352 memcpy (contents + offset, location, count);
0a1b45a2 9353 return true;
0ce398f1 9354 }
a0dcf297 9355
dc810e39
AM
9356 pos = hdr->sh_offset + offset;
9357 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9358 || bfd_bwrite (location, count, abfd) != count)
0a1b45a2 9359 return false;
252b5132 9360
0a1b45a2 9361 return true;
252b5132
RH
9362}
9363
0a1b45a2 9364bool
217aa764
AM
9365_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9366 arelent *cache_ptr ATTRIBUTE_UNUSED,
9367 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9368{
9369 abort ();
0a1b45a2 9370 return false;
252b5132
RH
9371}
9372
252b5132
RH
9373/* Try to convert a non-ELF reloc into an ELF one. */
9374
0a1b45a2 9375bool
217aa764 9376_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9377{
c044fabd 9378 /* Check whether we really have an ELF howto. */
252b5132
RH
9379
9380 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9381 {
9382 bfd_reloc_code_real_type code;
9383 reloc_howto_type *howto;
9384
9385 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9386 equivalent ELF reloc. */
252b5132
RH
9387
9388 if (areloc->howto->pc_relative)
9389 {
9390 switch (areloc->howto->bitsize)
9391 {
9392 case 8:
9393 code = BFD_RELOC_8_PCREL;
9394 break;
9395 case 12:
9396 code = BFD_RELOC_12_PCREL;
9397 break;
9398 case 16:
9399 code = BFD_RELOC_16_PCREL;
9400 break;
9401 case 24:
9402 code = BFD_RELOC_24_PCREL;
9403 break;
9404 case 32:
9405 code = BFD_RELOC_32_PCREL;
9406 break;
9407 case 64:
9408 code = BFD_RELOC_64_PCREL;
9409 break;
9410 default:
9411 goto fail;
9412 }
9413
9414 howto = bfd_reloc_type_lookup (abfd, code);
9415
94698d01 9416 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9417 {
9418 if (howto->pcrel_offset)
9419 areloc->addend += areloc->address;
9420 else
9421 areloc->addend -= areloc->address; /* addend is unsigned!! */
9422 }
9423 }
9424 else
9425 {
9426 switch (areloc->howto->bitsize)
9427 {
9428 case 8:
9429 code = BFD_RELOC_8;
9430 break;
9431 case 14:
9432 code = BFD_RELOC_14;
9433 break;
9434 case 16:
9435 code = BFD_RELOC_16;
9436 break;
9437 case 26:
9438 code = BFD_RELOC_26;
9439 break;
9440 case 32:
9441 code = BFD_RELOC_32;
9442 break;
9443 case 64:
9444 code = BFD_RELOC_64;
9445 break;
9446 default:
9447 goto fail;
9448 }
9449
9450 howto = bfd_reloc_type_lookup (abfd, code);
9451 }
9452
9453 if (howto)
9454 areloc->howto = howto;
9455 else
9456 goto fail;
9457 }
9458
0a1b45a2 9459 return true;
252b5132
RH
9460
9461 fail:
0aa13fee
AM
9462 /* xgettext:c-format */
9463 _bfd_error_handler (_("%pB: %s unsupported"),
9464 abfd, areloc->howto->name);
9aea1e31 9465 bfd_set_error (bfd_error_sorry);
0a1b45a2 9466 return false;
252b5132
RH
9467}
9468
0a1b45a2 9469bool
217aa764 9470_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9471{
d9071b0c 9472 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9473 if (tdata != NULL
9474 && (bfd_get_format (abfd) == bfd_object
9475 || bfd_get_format (abfd) == bfd_core))
252b5132 9476 {
c0355132 9477 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9478 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9479 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9480 }
9481
9482 return _bfd_generic_close_and_cleanup (abfd);
9483}
9484
9485/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9486 in the relocation's offset. Thus we cannot allow any sort of sanity
9487 range-checking to interfere. There is nothing else to do in processing
9488 this reloc. */
9489
9490bfd_reloc_status_type
217aa764
AM
9491_bfd_elf_rel_vtable_reloc_fn
9492 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9493 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9494 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9495 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9496{
9497 return bfd_reloc_ok;
9498}
252b5132
RH
9499\f
9500/* Elf core file support. Much of this only works on native
9501 toolchains, since we rely on knowing the
9502 machine-dependent procfs structure in order to pick
c044fabd 9503 out details about the corefile. */
252b5132
RH
9504
9505#ifdef HAVE_SYS_PROCFS_H
9506# include <sys/procfs.h>
9507#endif
9508
261b8d08
PA
9509/* Return a PID that identifies a "thread" for threaded cores, or the
9510 PID of the main process for non-threaded cores. */
252b5132
RH
9511
9512static int
217aa764 9513elfcore_make_pid (bfd *abfd)
252b5132 9514{
261b8d08
PA
9515 int pid;
9516
228e534f 9517 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9518 if (pid == 0)
228e534f 9519 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9520
9521 return pid;
252b5132
RH
9522}
9523
252b5132
RH
9524/* If there isn't a section called NAME, make one, using
9525 data from SECT. Note, this function will generate a
9526 reference to NAME, so you shouldn't deallocate or
c044fabd 9527 overwrite it. */
252b5132 9528
0a1b45a2 9529static bool
217aa764 9530elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9531{
c044fabd 9532 asection *sect2;
252b5132
RH
9533
9534 if (bfd_get_section_by_name (abfd, name) != NULL)
0a1b45a2 9535 return true;
252b5132 9536
117ed4f8 9537 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9538 if (sect2 == NULL)
0a1b45a2 9539 return false;
252b5132 9540
eea6121a 9541 sect2->size = sect->size;
252b5132 9542 sect2->filepos = sect->filepos;
252b5132 9543 sect2->alignment_power = sect->alignment_power;
0a1b45a2 9544 return true;
252b5132
RH
9545}
9546
bb0082d6
AM
9547/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9548 actually creates up to two pseudosections:
9549 - For the single-threaded case, a section named NAME, unless
9550 such a section already exists.
9551 - For the multi-threaded case, a section named "NAME/PID", where
9552 PID is elfcore_make_pid (abfd).
24d3e51b 9553 Both pseudosections have identical contents. */
0a1b45a2 9554bool
217aa764
AM
9555_bfd_elfcore_make_pseudosection (bfd *abfd,
9556 char *name,
9557 size_t size,
9558 ufile_ptr filepos)
bb0082d6
AM
9559{
9560 char buf[100];
9561 char *threaded_name;
d4c88bbb 9562 size_t len;
bb0082d6
AM
9563 asection *sect;
9564
9565 /* Build the section name. */
9566
9567 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9568 len = strlen (buf) + 1;
a50b1753 9569 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9570 if (threaded_name == NULL)
0a1b45a2 9571 return false;
d4c88bbb 9572 memcpy (threaded_name, buf, len);
bb0082d6 9573
117ed4f8
AM
9574 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9575 SEC_HAS_CONTENTS);
bb0082d6 9576 if (sect == NULL)
0a1b45a2 9577 return false;
eea6121a 9578 sect->size = size;
bb0082d6 9579 sect->filepos = filepos;
bb0082d6
AM
9580 sect->alignment_power = 2;
9581
936e320b 9582 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9583}
9584
0a1b45a2 9585static bool
58e07198
CZ
9586elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9587 size_t offs)
9588{
9589 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9590 SEC_HAS_CONTENTS);
9591
9592 if (sect == NULL)
0a1b45a2 9593 return false;
58e07198
CZ
9594
9595 sect->size = note->descsz - offs;
9596 sect->filepos = note->descpos + offs;
9597 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9598
0a1b45a2 9599 return true;
58e07198
CZ
9600}
9601
252b5132 9602/* prstatus_t exists on:
4a938328 9603 solaris 2.5+
252b5132
RH
9604 linux 2.[01] + glibc
9605 unixware 4.2
9606*/
9607
9608#if defined (HAVE_PRSTATUS_T)
a7b97311 9609
0a1b45a2 9610static bool
217aa764 9611elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9612{
eea6121a 9613 size_t size;
7ee38065 9614 int offset;
252b5132 9615
4a938328
MS
9616 if (note->descsz == sizeof (prstatus_t))
9617 {
9618 prstatus_t prstat;
252b5132 9619
eea6121a 9620 size = sizeof (prstat.pr_reg);
7ee38065 9621 offset = offsetof (prstatus_t, pr_reg);
4a938328 9622 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9623
fa49d224
NC
9624 /* Do not overwrite the core signal if it
9625 has already been set by another thread. */
228e534f
AM
9626 if (elf_tdata (abfd)->core->signal == 0)
9627 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9628 if (elf_tdata (abfd)->core->pid == 0)
9629 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9630
4a938328
MS
9631 /* pr_who exists on:
9632 solaris 2.5+
9633 unixware 4.2
9634 pr_who doesn't exist on:
9635 linux 2.[01]
9636 */
252b5132 9637#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9638 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9639#else
228e534f 9640 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9641#endif
4a938328 9642 }
7ee38065 9643#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9644 else if (note->descsz == sizeof (prstatus32_t))
9645 {
9646 /* 64-bit host, 32-bit corefile */
9647 prstatus32_t prstat;
9648
eea6121a 9649 size = sizeof (prstat.pr_reg);
7ee38065 9650 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9651 memcpy (&prstat, note->descdata, sizeof (prstat));
9652
fa49d224
NC
9653 /* Do not overwrite the core signal if it
9654 has already been set by another thread. */
228e534f
AM
9655 if (elf_tdata (abfd)->core->signal == 0)
9656 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9657 if (elf_tdata (abfd)->core->pid == 0)
9658 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9659
9660 /* pr_who exists on:
9661 solaris 2.5+
9662 unixware 4.2
9663 pr_who doesn't exist on:
9664 linux 2.[01]
9665 */
7ee38065 9666#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9667 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9668#else
228e534f 9669 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9670#endif
9671 }
7ee38065 9672#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9673 else
9674 {
9675 /* Fail - we don't know how to handle any other
9676 note size (ie. data object type). */
0a1b45a2 9677 return true;
4a938328 9678 }
252b5132 9679
bb0082d6 9680 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9681 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9682 size, note->descpos + offset);
252b5132
RH
9683}
9684#endif /* defined (HAVE_PRSTATUS_T) */
9685
bb0082d6 9686/* Create a pseudosection containing the exact contents of NOTE. */
0a1b45a2 9687static bool
217aa764
AM
9688elfcore_make_note_pseudosection (bfd *abfd,
9689 char *name,
9690 Elf_Internal_Note *note)
252b5132 9691{
936e320b
AM
9692 return _bfd_elfcore_make_pseudosection (abfd, name,
9693 note->descsz, note->descpos);
252b5132
RH
9694}
9695
ff08c6bb
JB
9696/* There isn't a consistent prfpregset_t across platforms,
9697 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9698 data structure apart. */
9699
0a1b45a2 9700static bool
217aa764 9701elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9702{
9703 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9704}
9705
ff08c6bb 9706/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9707 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9708 literally. */
c044fabd 9709
0a1b45a2 9710static bool
217aa764 9711elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9712{
9713 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9714}
9715
4339cae0
L
9716/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9717 with a note type of NT_X86_XSTATE. Just include the whole note's
9718 contents literally. */
9719
0a1b45a2 9720static bool
4339cae0
L
9721elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9722{
9723 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9724}
9725
0a1b45a2 9726static bool
97753bd5
AM
9727elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9728{
9729 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9730}
9731
0a1b45a2 9732static bool
89eeb0bc
LM
9733elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9734{
9735 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9736}
97753bd5 9737
0a1b45a2 9738static bool
cb2366c1
EBM
9739elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9740{
9741 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9742}
9743
0a1b45a2 9744static bool
cb2366c1
EBM
9745elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9746{
9747 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9748}
9749
0a1b45a2 9750static bool
cb2366c1
EBM
9751elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9752{
9753 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9754}
9755
0a1b45a2 9756static bool
cb2366c1
EBM
9757elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9758{
9759 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9760}
9761
0a1b45a2 9762static bool
cb2366c1
EBM
9763elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9764{
9765 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9766}
9767
0a1b45a2 9768static bool
cb2366c1
EBM
9769elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9770{
9771 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9772}
9773
0a1b45a2 9774static bool
cb2366c1
EBM
9775elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9776{
9777 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9778}
9779
0a1b45a2 9780static bool
cb2366c1
EBM
9781elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9782{
9783 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9784}
9785
0a1b45a2 9786static bool
cb2366c1
EBM
9787elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9788{
9789 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9790}
9791
0a1b45a2 9792static bool
cb2366c1
EBM
9793elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9794{
9795 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9796}
9797
0a1b45a2 9798static bool
cb2366c1
EBM
9799elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9800{
9801 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9802}
9803
0a1b45a2 9804static bool
cb2366c1
EBM
9805elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9806{
9807 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9808}
9809
0a1b45a2 9810static bool
cb2366c1
EBM
9811elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9812{
9813 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9814}
9815
0a1b45a2 9816static bool
0675e188
UW
9817elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9818{
9819 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9820}
9821
0a1b45a2 9822static bool
d7eeb400
MS
9823elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9824{
9825 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9826}
9827
0a1b45a2 9828static bool
d7eeb400
MS
9829elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9830{
9831 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9832}
9833
0a1b45a2 9834static bool
d7eeb400
MS
9835elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9836{
9837 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9838}
9839
0a1b45a2 9840static bool
d7eeb400
MS
9841elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9842{
9843 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9844}
9845
0a1b45a2 9846static bool
d7eeb400
MS
9847elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9848{
9849 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9850}
9851
0a1b45a2 9852static bool
355b81d9
UW
9853elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9854{
9855 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9856}
9857
0a1b45a2 9858static bool
355b81d9
UW
9859elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9860{
9861 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9862}
9863
0a1b45a2 9864static bool
abb3f6cc
NC
9865elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9866{
9867 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9868}
9869
0a1b45a2 9870static bool
4ef9f41a
AA
9871elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9872{
9873 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9874}
9875
0a1b45a2 9876static bool
4ef9f41a
AA
9877elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9878{
9879 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9880}
9881
0a1b45a2 9882static bool
88ab90e8
AA
9883elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9884{
9885 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9886}
9887
0a1b45a2 9888static bool
88ab90e8
AA
9889elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9890{
9891 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9892}
9893
0a1b45a2 9894static bool
faa9a424
UW
9895elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9896{
9897 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9898}
9899
0a1b45a2 9900static bool
652451f8
YZ
9901elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9902{
9903 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9904}
9905
0a1b45a2 9906static bool
652451f8
YZ
9907elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9908{
9909 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9910}
9911
0a1b45a2 9912static bool
652451f8
YZ
9913elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9914{
9915 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9916}
9917
0a1b45a2 9918static bool
ad1cc4e4
AH
9919elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9920{
9921 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9922}
9923
0a1b45a2 9924static bool
e6c3b5bf
AH
9925elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9926{
9927 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9928}
9929
f0bbe8ba
LM
9930static bool
9931elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
9932{
9933 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
9934 note);
9935}
9936
0a1b45a2 9937static bool
27456742
AK
9938elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9939{
9940 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9941}
9942
db6092f3
AB
9943/* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
9944 successful otherwise, return FALSE. */
9945
0a1b45a2 9946static bool
db6092f3
AB
9947elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
9948{
9949 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
9950}
9951
b63a5e38
AB
9952/* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
9953 successful otherwise, return FALSE. */
9954
0a1b45a2 9955static bool
b63a5e38
AB
9956elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
9957{
9958 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
9959}
9960
252b5132 9961#if defined (HAVE_PRPSINFO_T)
4a938328 9962typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9963#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9964typedef prpsinfo32_t elfcore_psinfo32_t;
9965#endif
252b5132
RH
9966#endif
9967
9968#if defined (HAVE_PSINFO_T)
4a938328 9969typedef psinfo_t elfcore_psinfo_t;
7ee38065 9970#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9971typedef psinfo32_t elfcore_psinfo32_t;
9972#endif
252b5132
RH
9973#endif
9974
252b5132
RH
9975/* return a malloc'ed copy of a string at START which is at
9976 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9977 the copy will always have a terminating '\0'. */
252b5132 9978
936e320b 9979char *
217aa764 9980_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9981{
dc810e39 9982 char *dups;
a50b1753 9983 char *end = (char *) memchr (start, '\0', max);
dc810e39 9984 size_t len;
252b5132
RH
9985
9986 if (end == NULL)
9987 len = max;
9988 else
9989 len = end - start;
9990
a50b1753 9991 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9992 if (dups == NULL)
252b5132
RH
9993 return NULL;
9994
dc810e39
AM
9995 memcpy (dups, start, len);
9996 dups[len] = '\0';
252b5132 9997
dc810e39 9998 return dups;
252b5132
RH
9999}
10000
bb0082d6 10001#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
0a1b45a2 10002static bool
217aa764 10003elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 10004{
4a938328
MS
10005 if (note->descsz == sizeof (elfcore_psinfo_t))
10006 {
10007 elfcore_psinfo_t psinfo;
252b5132 10008
7ee38065 10009 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10010
335e41d4 10011#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 10012 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10013#endif
228e534f 10014 elf_tdata (abfd)->core->program
936e320b
AM
10015 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10016 sizeof (psinfo.pr_fname));
252b5132 10017
228e534f 10018 elf_tdata (abfd)->core->command
936e320b
AM
10019 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10020 sizeof (psinfo.pr_psargs));
4a938328 10021 }
7ee38065 10022#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
10023 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10024 {
10025 /* 64-bit host, 32-bit corefile */
10026 elfcore_psinfo32_t psinfo;
10027
7ee38065 10028 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10029
335e41d4 10030#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 10031 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10032#endif
228e534f 10033 elf_tdata (abfd)->core->program
936e320b
AM
10034 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10035 sizeof (psinfo.pr_fname));
4a938328 10036
228e534f 10037 elf_tdata (abfd)->core->command
936e320b
AM
10038 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10039 sizeof (psinfo.pr_psargs));
4a938328
MS
10040 }
10041#endif
10042
10043 else
10044 {
10045 /* Fail - we don't know how to handle any other
10046 note size (ie. data object type). */
0a1b45a2 10047 return true;
4a938328 10048 }
252b5132
RH
10049
10050 /* Note that for some reason, a spurious space is tacked
10051 onto the end of the args in some (at least one anyway)
c044fabd 10052 implementations, so strip it off if it exists. */
252b5132
RH
10053
10054 {
228e534f 10055 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10056 int n = strlen (command);
10057
10058 if (0 < n && command[n - 1] == ' ')
10059 command[n - 1] = '\0';
10060 }
10061
0a1b45a2 10062 return true;
252b5132
RH
10063}
10064#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10065
252b5132 10066#if defined (HAVE_PSTATUS_T)
0a1b45a2 10067static bool
217aa764 10068elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10069{
f572a39d
AM
10070 if (note->descsz == sizeof (pstatus_t)
10071#if defined (HAVE_PXSTATUS_T)
10072 || note->descsz == sizeof (pxstatus_t)
10073#endif
10074 )
4a938328
MS
10075 {
10076 pstatus_t pstat;
252b5132 10077
4a938328 10078 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10079
228e534f 10080 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10081 }
7ee38065 10082#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10083 else if (note->descsz == sizeof (pstatus32_t))
10084 {
10085 /* 64-bit host, 32-bit corefile */
10086 pstatus32_t pstat;
252b5132 10087
4a938328 10088 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10089
228e534f 10090 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10091 }
10092#endif
252b5132
RH
10093 /* Could grab some more details from the "representative"
10094 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10095 NT_LWPSTATUS note, presumably. */
252b5132 10096
0a1b45a2 10097 return true;
252b5132
RH
10098}
10099#endif /* defined (HAVE_PSTATUS_T) */
10100
252b5132 10101#if defined (HAVE_LWPSTATUS_T)
0a1b45a2 10102static bool
217aa764 10103elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10104{
10105 lwpstatus_t lwpstat;
10106 char buf[100];
c044fabd 10107 char *name;
d4c88bbb 10108 size_t len;
c044fabd 10109 asection *sect;
252b5132 10110
f572a39d
AM
10111 if (note->descsz != sizeof (lwpstat)
10112#if defined (HAVE_LWPXSTATUS_T)
10113 && note->descsz != sizeof (lwpxstatus_t)
10114#endif
10115 )
0a1b45a2 10116 return true;
252b5132
RH
10117
10118 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10119
228e534f 10120 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10121 /* Do not overwrite the core signal if it has already been set by
10122 another thread. */
228e534f
AM
10123 if (elf_tdata (abfd)->core->signal == 0)
10124 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10125
c044fabd 10126 /* Make a ".reg/999" section. */
252b5132
RH
10127
10128 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10129 len = strlen (buf) + 1;
217aa764 10130 name = bfd_alloc (abfd, len);
252b5132 10131 if (name == NULL)
0a1b45a2 10132 return false;
d4c88bbb 10133 memcpy (name, buf, len);
252b5132 10134
117ed4f8 10135 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10136 if (sect == NULL)
0a1b45a2 10137 return false;
252b5132
RH
10138
10139#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10140 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10141 sect->filepos = note->descpos
10142 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10143#endif
10144
10145#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10146 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10147 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10148#endif
10149
252b5132
RH
10150 sect->alignment_power = 2;
10151
10152 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10153 return false;
252b5132
RH
10154
10155 /* Make a ".reg2/999" section */
10156
10157 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10158 len = strlen (buf) + 1;
217aa764 10159 name = bfd_alloc (abfd, len);
252b5132 10160 if (name == NULL)
0a1b45a2 10161 return false;
d4c88bbb 10162 memcpy (name, buf, len);
252b5132 10163
117ed4f8 10164 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10165 if (sect == NULL)
0a1b45a2 10166 return false;
252b5132
RH
10167
10168#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10169 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10170 sect->filepos = note->descpos
10171 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10172#endif
10173
10174#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10175 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10176 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10177#endif
10178
252b5132
RH
10179 sect->alignment_power = 2;
10180
936e320b 10181 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10182}
10183#endif /* defined (HAVE_LWPSTATUS_T) */
10184
8fbac78b
JT
10185/* These constants, and the structure offsets used below, are defined by
10186 Cygwin's core_dump.h */
10187#define NOTE_INFO_PROCESS 1
10188#define NOTE_INFO_THREAD 2
10189#define NOTE_INFO_MODULE 3
d61f3d03 10190#define NOTE_INFO_MODULE64 4
8fbac78b 10191
0a1b45a2 10192static bool
217aa764 10193elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10194{
10195 char buf[30];
c044fabd 10196 char *name;
d4c88bbb 10197 size_t len;
3cdad084 10198 unsigned int name_size;
c044fabd 10199 asection *sect;
2fef9373 10200 unsigned int type;
4a6636fb
PA
10201 int is_active_thread;
10202 bfd_vma base_addr;
16e9c715 10203
04ec0fa2 10204 if (note->descsz < 4)
0a1b45a2 10205 return true;
16e9c715 10206
08dedd66 10207 if (! startswith (note->namedata, "win32"))
0a1b45a2 10208 return true;
4a6636fb
PA
10209
10210 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10211
7e0d77ef
NC
10212 struct
10213 {
404ec933
JT
10214 const char *type_name;
10215 unsigned long min_size;
10216 } size_check[] =
10217 {
10218 { "NOTE_INFO_PROCESS", 12 },
10219 { "NOTE_INFO_THREAD", 12 },
10220 { "NOTE_INFO_MODULE", 12 },
10221 { "NOTE_INFO_MODULE64", 16 },
10222 };
10223
7e0d77ef 10224 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
0a1b45a2 10225 return true;
404ec933
JT
10226
10227 if (note->descsz < size_check[type - 1].min_size)
10228 {
10229 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10230 abfd, size_check[type - 1].type_name, note->descsz);
0a1b45a2 10231 return true;
404ec933
JT
10232 }
10233
4a6636fb 10234 switch (type)
16e9c715 10235 {
8fbac78b 10236 case NOTE_INFO_PROCESS:
228e534f 10237 /* FIXME: need to add ->core->command. */
ff2084b9 10238 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10239 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10240 break;
16e9c715 10241
8fbac78b 10242 case NOTE_INFO_THREAD:
ff2084b9
JT
10243 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10244 structure. */
4a6636fb 10245 /* thread_info.tid */
ff2084b9 10246 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10247
d4c88bbb 10248 len = strlen (buf) + 1;
a50b1753 10249 name = (char *) bfd_alloc (abfd, len);
16e9c715 10250 if (name == NULL)
0a1b45a2 10251 return false;
c044fabd 10252
d4c88bbb 10253 memcpy (name, buf, len);
16e9c715 10254
117ed4f8 10255 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10256 if (sect == NULL)
0a1b45a2 10257 return false;
c044fabd 10258
4a6636fb 10259 /* sizeof (thread_info.thread_context) */
03c29a6f 10260 sect->size = note->descsz - 12;
4a6636fb
PA
10261 /* offsetof (thread_info.thread_context) */
10262 sect->filepos = note->descpos + 12;
16e9c715
NC
10263 sect->alignment_power = 2;
10264
4a6636fb
PA
10265 /* thread_info.is_active_thread */
10266 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10267
10268 if (is_active_thread)
16e9c715 10269 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10270 return false;
16e9c715
NC
10271 break;
10272
8fbac78b 10273 case NOTE_INFO_MODULE:
d61f3d03 10274 case NOTE_INFO_MODULE64:
16e9c715 10275 /* Make a ".module/xxxxxxxx" section. */
d61f3d03
JT
10276 if (type == NOTE_INFO_MODULE)
10277 {
d61f3d03
JT
10278 /* module_info.base_address */
10279 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10280 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10281 /* module_info.module_name_size */
10282 name_size = bfd_get_32 (abfd, note->descdata + 8);
10283 }
10284 else /* NOTE_INFO_MODULE64 */
10285 {
d61f3d03
JT
10286 /* module_info.base_address */
10287 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10288 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10289 /* module_info.module_name_size */
10290 name_size = bfd_get_32 (abfd, note->descdata + 12);
10291 }
c044fabd 10292
d4c88bbb 10293 len = strlen (buf) + 1;
a50b1753 10294 name = (char *) bfd_alloc (abfd, len);
16e9c715 10295 if (name == NULL)
0a1b45a2 10296 return false;
c044fabd 10297
d4c88bbb 10298 memcpy (name, buf, len);
252b5132 10299
117ed4f8 10300 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10301
16e9c715 10302 if (sect == NULL)
0a1b45a2 10303 return false;
c044fabd 10304
04ec0fa2 10305 if (note->descsz < 12 + name_size)
404ec933 10306 {
3cdad084 10307 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
404ec933 10308 abfd, note->descsz, name_size);
0a1b45a2 10309 return true;
404ec933 10310 }
04ec0fa2 10311
eea6121a 10312 sect->size = note->descsz;
16e9c715 10313 sect->filepos = note->descpos;
16e9c715
NC
10314 sect->alignment_power = 2;
10315 break;
10316
10317 default:
0a1b45a2 10318 return true;
16e9c715
NC
10319 }
10320
0a1b45a2 10321 return true;
16e9c715 10322}
252b5132 10323
0a1b45a2 10324static bool
217aa764 10325elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10326{
9c5bfbb7 10327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10328
252b5132
RH
10329 switch (note->type)
10330 {
10331 default:
0a1b45a2 10332 return true;
252b5132 10333
252b5132 10334 case NT_PRSTATUS:
bb0082d6
AM
10335 if (bed->elf_backend_grok_prstatus)
10336 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
0a1b45a2 10337 return true;
bb0082d6 10338#if defined (HAVE_PRSTATUS_T)
252b5132 10339 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10340#else
0a1b45a2 10341 return true;
252b5132
RH
10342#endif
10343
10344#if defined (HAVE_PSTATUS_T)
10345 case NT_PSTATUS:
10346 return elfcore_grok_pstatus (abfd, note);
10347#endif
10348
10349#if defined (HAVE_LWPSTATUS_T)
10350 case NT_LWPSTATUS:
10351 return elfcore_grok_lwpstatus (abfd, note);
10352#endif
10353
10354 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10355 return elfcore_grok_prfpreg (abfd, note);
10356
c044fabd 10357 case NT_WIN32PSTATUS:
16e9c715 10358 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10359
c044fabd 10360 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10361 if (note->namesz == 6
10362 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10363 return elfcore_grok_prxfpreg (abfd, note);
10364 else
0a1b45a2 10365 return true;
ff08c6bb 10366
4339cae0
L
10367 case NT_X86_XSTATE: /* Linux XSAVE extension */
10368 if (note->namesz == 6
10369 && strcmp (note->namedata, "LINUX") == 0)
10370 return elfcore_grok_xstatereg (abfd, note);
10371 else
0a1b45a2 10372 return true;
4339cae0 10373
97753bd5
AM
10374 case NT_PPC_VMX:
10375 if (note->namesz == 6
10376 && strcmp (note->namedata, "LINUX") == 0)
10377 return elfcore_grok_ppc_vmx (abfd, note);
10378 else
0a1b45a2 10379 return true;
97753bd5 10380
89eeb0bc
LM
10381 case NT_PPC_VSX:
10382 if (note->namesz == 6
07d6d2b8
AM
10383 && strcmp (note->namedata, "LINUX") == 0)
10384 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10385 else
0a1b45a2 10386 return true;
89eeb0bc 10387
cb2366c1
EBM
10388 case NT_PPC_TAR:
10389 if (note->namesz == 6
4b24dd1a
AM
10390 && strcmp (note->namedata, "LINUX") == 0)
10391 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10392 else
0a1b45a2 10393 return true;
cb2366c1
EBM
10394
10395 case NT_PPC_PPR:
10396 if (note->namesz == 6
4b24dd1a
AM
10397 && strcmp (note->namedata, "LINUX") == 0)
10398 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10399 else
0a1b45a2 10400 return true;
cb2366c1
EBM
10401
10402 case NT_PPC_DSCR:
10403 if (note->namesz == 6
4b24dd1a
AM
10404 && strcmp (note->namedata, "LINUX") == 0)
10405 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10406 else
0a1b45a2 10407 return true;
cb2366c1
EBM
10408
10409 case NT_PPC_EBB:
10410 if (note->namesz == 6
4b24dd1a
AM
10411 && strcmp (note->namedata, "LINUX") == 0)
10412 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10413 else
0a1b45a2 10414 return true;
cb2366c1
EBM
10415
10416 case NT_PPC_PMU:
10417 if (note->namesz == 6
4b24dd1a
AM
10418 && strcmp (note->namedata, "LINUX") == 0)
10419 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10420 else
0a1b45a2 10421 return true;
cb2366c1
EBM
10422
10423 case NT_PPC_TM_CGPR:
10424 if (note->namesz == 6
4b24dd1a
AM
10425 && strcmp (note->namedata, "LINUX") == 0)
10426 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10427 else
0a1b45a2 10428 return true;
cb2366c1
EBM
10429
10430 case NT_PPC_TM_CFPR:
10431 if (note->namesz == 6
4b24dd1a
AM
10432 && strcmp (note->namedata, "LINUX") == 0)
10433 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10434 else
0a1b45a2 10435 return true;
cb2366c1
EBM
10436
10437 case NT_PPC_TM_CVMX:
10438 if (note->namesz == 6
4b24dd1a
AM
10439 && strcmp (note->namedata, "LINUX") == 0)
10440 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10441 else
0a1b45a2 10442 return true;
cb2366c1
EBM
10443
10444 case NT_PPC_TM_CVSX:
10445 if (note->namesz == 6
4b24dd1a
AM
10446 && strcmp (note->namedata, "LINUX") == 0)
10447 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10448 else
0a1b45a2 10449 return true;
cb2366c1
EBM
10450
10451 case NT_PPC_TM_SPR:
10452 if (note->namesz == 6
4b24dd1a
AM
10453 && strcmp (note->namedata, "LINUX") == 0)
10454 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10455 else
0a1b45a2 10456 return true;
cb2366c1
EBM
10457
10458 case NT_PPC_TM_CTAR:
10459 if (note->namesz == 6
4b24dd1a
AM
10460 && strcmp (note->namedata, "LINUX") == 0)
10461 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10462 else
0a1b45a2 10463 return true;
cb2366c1
EBM
10464
10465 case NT_PPC_TM_CPPR:
10466 if (note->namesz == 6
4b24dd1a
AM
10467 && strcmp (note->namedata, "LINUX") == 0)
10468 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10469 else
0a1b45a2 10470 return true;
cb2366c1
EBM
10471
10472 case NT_PPC_TM_CDSCR:
10473 if (note->namesz == 6
4b24dd1a
AM
10474 && strcmp (note->namedata, "LINUX") == 0)
10475 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10476 else
0a1b45a2 10477 return true;
cb2366c1 10478
0675e188
UW
10479 case NT_S390_HIGH_GPRS:
10480 if (note->namesz == 6
07d6d2b8
AM
10481 && strcmp (note->namedata, "LINUX") == 0)
10482 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10483 else
0a1b45a2 10484 return true;
0675e188 10485
d7eeb400
MS
10486 case NT_S390_TIMER:
10487 if (note->namesz == 6
07d6d2b8
AM
10488 && strcmp (note->namedata, "LINUX") == 0)
10489 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10490 else
0a1b45a2 10491 return true;
d7eeb400
MS
10492
10493 case NT_S390_TODCMP:
10494 if (note->namesz == 6
07d6d2b8
AM
10495 && strcmp (note->namedata, "LINUX") == 0)
10496 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10497 else
0a1b45a2 10498 return true;
d7eeb400
MS
10499
10500 case NT_S390_TODPREG:
10501 if (note->namesz == 6
07d6d2b8
AM
10502 && strcmp (note->namedata, "LINUX") == 0)
10503 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10504 else
0a1b45a2 10505 return true;
d7eeb400
MS
10506
10507 case NT_S390_CTRS:
10508 if (note->namesz == 6
07d6d2b8
AM
10509 && strcmp (note->namedata, "LINUX") == 0)
10510 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10511 else
0a1b45a2 10512 return true;
d7eeb400
MS
10513
10514 case NT_S390_PREFIX:
10515 if (note->namesz == 6
07d6d2b8
AM
10516 && strcmp (note->namedata, "LINUX") == 0)
10517 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10518 else
0a1b45a2 10519 return true;
d7eeb400 10520
355b81d9
UW
10521 case NT_S390_LAST_BREAK:
10522 if (note->namesz == 6
07d6d2b8
AM
10523 && strcmp (note->namedata, "LINUX") == 0)
10524 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10525 else
0a1b45a2 10526 return true;
355b81d9
UW
10527
10528 case NT_S390_SYSTEM_CALL:
10529 if (note->namesz == 6
07d6d2b8
AM
10530 && strcmp (note->namedata, "LINUX") == 0)
10531 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10532 else
0a1b45a2 10533 return true;
355b81d9 10534
abb3f6cc
NC
10535 case NT_S390_TDB:
10536 if (note->namesz == 6
07d6d2b8
AM
10537 && strcmp (note->namedata, "LINUX") == 0)
10538 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10539 else
0a1b45a2 10540 return true;
abb3f6cc 10541
4ef9f41a
AA
10542 case NT_S390_VXRS_LOW:
10543 if (note->namesz == 6
10544 && strcmp (note->namedata, "LINUX") == 0)
10545 return elfcore_grok_s390_vxrs_low (abfd, note);
10546 else
0a1b45a2 10547 return true;
4ef9f41a
AA
10548
10549 case NT_S390_VXRS_HIGH:
10550 if (note->namesz == 6
10551 && strcmp (note->namedata, "LINUX") == 0)
10552 return elfcore_grok_s390_vxrs_high (abfd, note);
10553 else
0a1b45a2 10554 return true;
4ef9f41a 10555
88ab90e8
AA
10556 case NT_S390_GS_CB:
10557 if (note->namesz == 6
10558 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10559 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8 10560 else
0a1b45a2 10561 return true;
88ab90e8
AA
10562
10563 case NT_S390_GS_BC:
10564 if (note->namesz == 6
10565 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10566 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8 10567 else
0a1b45a2 10568 return true;
88ab90e8 10569
27456742
AK
10570 case NT_ARC_V2:
10571 if (note->namesz == 6
10572 && strcmp (note->namedata, "LINUX") == 0)
10573 return elfcore_grok_arc_v2 (abfd, note);
10574 else
0a1b45a2 10575 return true;
27456742 10576
faa9a424
UW
10577 case NT_ARM_VFP:
10578 if (note->namesz == 6
10579 && strcmp (note->namedata, "LINUX") == 0)
10580 return elfcore_grok_arm_vfp (abfd, note);
10581 else
0a1b45a2 10582 return true;
faa9a424 10583
652451f8
YZ
10584 case NT_ARM_TLS:
10585 if (note->namesz == 6
10586 && strcmp (note->namedata, "LINUX") == 0)
10587 return elfcore_grok_aarch_tls (abfd, note);
10588 else
0a1b45a2 10589 return true;
652451f8
YZ
10590
10591 case NT_ARM_HW_BREAK:
10592 if (note->namesz == 6
10593 && strcmp (note->namedata, "LINUX") == 0)
10594 return elfcore_grok_aarch_hw_break (abfd, note);
10595 else
0a1b45a2 10596 return true;
652451f8
YZ
10597
10598 case NT_ARM_HW_WATCH:
10599 if (note->namesz == 6
10600 && strcmp (note->namedata, "LINUX") == 0)
10601 return elfcore_grok_aarch_hw_watch (abfd, note);
10602 else
0a1b45a2 10603 return true;
652451f8 10604
ad1cc4e4
AH
10605 case NT_ARM_SVE:
10606 if (note->namesz == 6
10607 && strcmp (note->namedata, "LINUX") == 0)
10608 return elfcore_grok_aarch_sve (abfd, note);
10609 else
0a1b45a2 10610 return true;
ad1cc4e4 10611
e6c3b5bf
AH
10612 case NT_ARM_PAC_MASK:
10613 if (note->namesz == 6
10614 && strcmp (note->namedata, "LINUX") == 0)
10615 return elfcore_grok_aarch_pauth (abfd, note);
10616 else
0a1b45a2 10617 return true;
e6c3b5bf 10618
f0bbe8ba
LM
10619 case NT_ARM_TAGGED_ADDR_CTRL:
10620 if (note->namesz == 6
10621 && strcmp (note->namedata, "LINUX") == 0)
10622 return elfcore_grok_aarch_mte (abfd, note);
10623 else
10624 return true;
10625
b63a5e38
AB
10626 case NT_GDB_TDESC:
10627 if (note->namesz == 4
10628 && strcmp (note->namedata, "GDB") == 0)
10629 return elfcore_grok_gdb_tdesc (abfd, note);
10630 else
0a1b45a2 10631 return true;
b63a5e38 10632
db6092f3
AB
10633 case NT_RISCV_CSR:
10634 if (note->namesz == 4
10635 && strcmp (note->namedata, "GDB") == 0)
10636 return elfcore_grok_riscv_csr (abfd, note);
10637 else
0a1b45a2 10638 return true;
db6092f3 10639
252b5132
RH
10640 case NT_PRPSINFO:
10641 case NT_PSINFO:
bb0082d6
AM
10642 if (bed->elf_backend_grok_psinfo)
10643 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
0a1b45a2 10644 return true;
bb0082d6 10645#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10646 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10647#else
0a1b45a2 10648 return true;
252b5132 10649#endif
3333a7c3
RM
10650
10651 case NT_AUXV:
58e07198 10652 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10653
451b7c33
TT
10654 case NT_FILE:
10655 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10656 note);
10657
9015683b
TT
10658 case NT_SIGINFO:
10659 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10660 note);
5b2c414d 10661
252b5132
RH
10662 }
10663}
10664
0a1b45a2 10665static bool
718175fa
JK
10666elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10667{
c74f7d1c 10668 struct bfd_build_id* build_id;
30e8ee25
AM
10669
10670 if (note->descsz == 0)
0a1b45a2 10671 return false;
30e8ee25 10672
c74f7d1c
JT
10673 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10674 if (build_id == NULL)
0a1b45a2 10675 return false;
718175fa 10676
c74f7d1c
JT
10677 build_id->size = note->descsz;
10678 memcpy (build_id->data, note->descdata, note->descsz);
10679 abfd->build_id = build_id;
718175fa 10680
0a1b45a2 10681 return true;
718175fa
JK
10682}
10683
0a1b45a2 10684static bool
718175fa
JK
10685elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10686{
10687 switch (note->type)
10688 {
10689 default:
0a1b45a2 10690 return true;
718175fa 10691
46bed679
L
10692 case NT_GNU_PROPERTY_TYPE_0:
10693 return _bfd_elf_parse_gnu_properties (abfd, note);
10694
718175fa
JK
10695 case NT_GNU_BUILD_ID:
10696 return elfobj_grok_gnu_build_id (abfd, note);
10697 }
10698}
10699
0a1b45a2 10700static bool
e21e5835
NC
10701elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10702{
10703 struct sdt_note *cur =
7a6e0d89
AM
10704 (struct sdt_note *) bfd_alloc (abfd,
10705 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10706
10707 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10708 cur->size = (bfd_size_type) note->descsz;
10709 memcpy (cur->data, note->descdata, note->descsz);
10710
10711 elf_tdata (abfd)->sdt_note_head = cur;
10712
0a1b45a2 10713 return true;
e21e5835
NC
10714}
10715
0a1b45a2 10716static bool
e21e5835
NC
10717elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10718{
10719 switch (note->type)
10720 {
10721 case NT_STAPSDT:
10722 return elfobj_grok_stapsdt_note_1 (abfd, note);
10723
10724 default:
0a1b45a2 10725 return true;
e21e5835
NC
10726 }
10727}
10728
0a1b45a2 10729static bool
aa1ed4a9
JB
10730elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10731{
10732 size_t offset;
10733
b5430a3c 10734 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10735 {
b5430a3c 10736 case ELFCLASS32:
0064d223 10737 if (note->descsz < 108)
0a1b45a2 10738 return false;
aa1ed4a9
JB
10739 break;
10740
b5430a3c 10741 case ELFCLASS64:
0064d223 10742 if (note->descsz < 120)
0a1b45a2 10743 return false;
aa1ed4a9
JB
10744 break;
10745
10746 default:
0a1b45a2 10747 return false;
aa1ed4a9
JB
10748 }
10749
0064d223
JB
10750 /* Check for version 1 in pr_version. */
10751 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10752 return false;
80a04378 10753
0064d223
JB
10754 offset = 4;
10755
10756 /* Skip over pr_psinfosz. */
b5430a3c 10757 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10758 offset += 4;
10759 else
10760 {
10761 offset += 4; /* Padding before pr_psinfosz. */
10762 offset += 8;
10763 }
10764
aa1ed4a9
JB
10765 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10766 elf_tdata (abfd)->core->program
10767 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10768 offset += 17;
10769
10770 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10771 elf_tdata (abfd)->core->command
10772 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10773 offset += 81;
10774
10775 /* Padding before pr_pid. */
10776 offset += 2;
10777
10778 /* The pr_pid field was added in version "1a". */
10779 if (note->descsz < offset + 4)
0a1b45a2 10780 return true;
0064d223
JB
10781
10782 elf_tdata (abfd)->core->pid
10783 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9 10784
0a1b45a2 10785 return true;
aa1ed4a9
JB
10786}
10787
0a1b45a2 10788static bool
aa1ed4a9
JB
10789elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10790{
10791 size_t offset;
10792 size_t size;
24d3e51b 10793 size_t min_size;
aa1ed4a9 10794
24d3e51b
NC
10795 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10796 Also compute minimum size of this note. */
b5430a3c 10797 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10798 {
b5430a3c 10799 case ELFCLASS32:
24d3e51b
NC
10800 offset = 4 + 4;
10801 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10802 break;
10803
b5430a3c 10804 case ELFCLASS64:
24d3e51b
NC
10805 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10806 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10807 break;
10808
10809 default:
0a1b45a2 10810 return false;
aa1ed4a9
JB
10811 }
10812
24d3e51b 10813 if (note->descsz < min_size)
0a1b45a2 10814 return false;
24d3e51b
NC
10815
10816 /* Check for version 1 in pr_version. */
10817 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10818 return false;
aa1ed4a9 10819
24d3e51b
NC
10820 /* Extract size of pr_reg from pr_gregsetsz. */
10821 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10822 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10823 {
10824 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10825 offset += 4 * 2;
10826 }
b5430a3c 10827 else
24d3e51b
NC
10828 {
10829 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10830 offset += 8 * 2;
10831 }
aa1ed4a9 10832
24d3e51b 10833 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10834 offset += 4;
10835
24d3e51b 10836 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10837 if (elf_tdata (abfd)->core->signal == 0)
10838 elf_tdata (abfd)->core->signal
10839 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10840 offset += 4;
10841
24d3e51b 10842 /* Read TID from pr_pid. */
aa1ed4a9
JB
10843 elf_tdata (abfd)->core->lwpid
10844 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10845 offset += 4;
10846
24d3e51b 10847 /* Padding before pr_reg. */
b5430a3c 10848 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10849 offset += 4;
10850
24d3e51b
NC
10851 /* Make sure that there is enough data remaining in the note. */
10852 if ((note->descsz - offset) < size)
0a1b45a2 10853 return false;
24d3e51b 10854
aa1ed4a9
JB
10855 /* Make a ".reg/999" section and a ".reg" section. */
10856 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10857 size, note->descpos + offset);
10858}
10859
0a1b45a2 10860static bool
aa1ed4a9
JB
10861elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10862{
544c67cd
JB
10863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10864
aa1ed4a9
JB
10865 switch (note->type)
10866 {
10867 case NT_PRSTATUS:
544c67cd
JB
10868 if (bed->elf_backend_grok_freebsd_prstatus)
10869 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
0a1b45a2 10870 return true;
aa1ed4a9
JB
10871 return elfcore_grok_freebsd_prstatus (abfd, note);
10872
10873 case NT_FPREGSET:
10874 return elfcore_grok_prfpreg (abfd, note);
10875
10876 case NT_PRPSINFO:
10877 return elfcore_grok_freebsd_psinfo (abfd, note);
10878
10879 case NT_FREEBSD_THRMISC:
10880 if (note->namesz == 8)
10881 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10882 else
0a1b45a2 10883 return true;
aa1ed4a9 10884
ddb2bbcf
JB
10885 case NT_FREEBSD_PROCSTAT_PROC:
10886 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10887 note);
10888
10889 case NT_FREEBSD_PROCSTAT_FILES:
10890 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10891 note);
10892
10893 case NT_FREEBSD_PROCSTAT_VMMAP:
10894 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10895 note);
10896
3350c5f5 10897 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10898 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10899
aa1ed4a9
JB
10900 case NT_X86_XSTATE:
10901 if (note->namesz == 8)
10902 return elfcore_grok_xstatereg (abfd, note);
10903 else
0a1b45a2 10904 return true;
aa1ed4a9 10905
e6f3b9c3
JB
10906 case NT_FREEBSD_PTLWPINFO:
10907 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10908 note);
10909
6d5be5d6
JB
10910 case NT_ARM_VFP:
10911 return elfcore_grok_arm_vfp (abfd, note);
10912
aa1ed4a9 10913 default:
0a1b45a2 10914 return true;
aa1ed4a9
JB
10915 }
10916}
10917
0a1b45a2 10918static bool
217aa764 10919elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10920{
10921 char *cp;
10922
10923 cp = strchr (note->namedata, '@');
10924 if (cp != NULL)
10925 {
d2b64500 10926 *lwpidp = atoi(cp + 1);
0a1b45a2 10927 return true;
50b2bdb7 10928 }
0a1b45a2 10929 return false;
50b2bdb7
AM
10930}
10931
0a1b45a2 10932static bool
217aa764 10933elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10934{
80a04378 10935 if (note->descsz <= 0x7c + 31)
0a1b45a2 10936 return false;
80a04378 10937
50b2bdb7 10938 /* Signal number at offset 0x08. */
228e534f 10939 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10940 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10941
10942 /* Process ID at offset 0x50. */
228e534f 10943 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10944 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10945
10946 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10947 elf_tdata (abfd)->core->command
50b2bdb7
AM
10948 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10949
7720ba9f
MK
10950 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10951 note);
50b2bdb7
AM
10952}
10953
0a1b45a2 10954static bool
217aa764 10955elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10956{
10957 int lwp;
10958
10959 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10960 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10961
58e07198 10962 switch (note->type)
50b2bdb7 10963 {
58e07198 10964 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10965 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10966 find this note before any of the others, which is fine,
10967 since the kernel writes this note out first when it
10968 creates a core file. */
50b2bdb7 10969 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10970 case NT_NETBSDCORE_AUXV:
10971 /* NetBSD-specific Elf Auxiliary Vector data. */
10972 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10973 case NT_NETBSDCORE_LWPSTATUS:
10974 return elfcore_make_note_pseudosection (abfd,
10975 ".note.netbsdcore.lwpstatus",
10976 note);
58e07198
CZ
10977 default:
10978 break;
50b2bdb7
AM
10979 }
10980
06d949ec 10981 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10982 defined for NetBSD core files. If the note type is less
10983 than the start of the machine-dependent note types, we don't
10984 understand it. */
47d9a591 10985
b4db1224 10986 if (note->type < NT_NETBSDCORE_FIRSTMACH)
0a1b45a2 10987 return true;
50b2bdb7
AM
10988
10989
10990 switch (bfd_get_arch (abfd))
10991 {
08a40648
AM
10992 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10993 PT_GETFPREGS == mach+2. */
50b2bdb7 10994
015ec493 10995 case bfd_arch_aarch64:
50b2bdb7
AM
10996 case bfd_arch_alpha:
10997 case bfd_arch_sparc:
10998 switch (note->type)
08a40648
AM
10999 {
11000 case NT_NETBSDCORE_FIRSTMACH+0:
11001 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11002
08a40648
AM
11003 case NT_NETBSDCORE_FIRSTMACH+2:
11004 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11005
08a40648 11006 default:
0a1b45a2 11007 return true;
08a40648 11008 }
50b2bdb7 11009
58e07198
CZ
11010 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11011 There's also old PT___GETREGS40 == mach + 1 for old reg
11012 structure which lacks GBR. */
11013
11014 case bfd_arch_sh:
11015 switch (note->type)
11016 {
11017 case NT_NETBSDCORE_FIRSTMACH+3:
11018 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11019
11020 case NT_NETBSDCORE_FIRSTMACH+5:
11021 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11022
11023 default:
0a1b45a2 11024 return true;
58e07198
CZ
11025 }
11026
08a40648
AM
11027 /* On all other arch's, PT_GETREGS == mach+1 and
11028 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
11029
11030 default:
11031 switch (note->type)
08a40648
AM
11032 {
11033 case NT_NETBSDCORE_FIRSTMACH+1:
11034 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11035
08a40648
AM
11036 case NT_NETBSDCORE_FIRSTMACH+3:
11037 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11038
08a40648 11039 default:
0a1b45a2 11040 return true;
08a40648 11041 }
50b2bdb7
AM
11042 }
11043 /* NOTREACHED */
11044}
11045
0a1b45a2 11046static bool
67cc5033
MK
11047elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11048{
80a04378 11049 if (note->descsz <= 0x48 + 31)
0a1b45a2 11050 return false;
80a04378 11051
67cc5033 11052 /* Signal number at offset 0x08. */
228e534f 11053 elf_tdata (abfd)->core->signal
67cc5033
MK
11054 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11055
11056 /* Process ID at offset 0x20. */
228e534f 11057 elf_tdata (abfd)->core->pid
67cc5033
MK
11058 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11059
11060 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 11061 elf_tdata (abfd)->core->command
67cc5033
MK
11062 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11063
0a1b45a2 11064 return true;
67cc5033
MK
11065}
11066
0a1b45a2 11067static bool
67cc5033
MK
11068elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11069{
11070 if (note->type == NT_OPENBSD_PROCINFO)
11071 return elfcore_grok_openbsd_procinfo (abfd, note);
11072
11073 if (note->type == NT_OPENBSD_REGS)
11074 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11075
11076 if (note->type == NT_OPENBSD_FPREGS)
11077 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11078
11079 if (note->type == NT_OPENBSD_XFPREGS)
11080 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11081
11082 if (note->type == NT_OPENBSD_AUXV)
58e07198 11083 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11084
11085 if (note->type == NT_OPENBSD_WCOOKIE)
11086 {
11087 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11088 SEC_HAS_CONTENTS);
11089
11090 if (sect == NULL)
0a1b45a2 11091 return false;
67cc5033
MK
11092 sect->size = note->descsz;
11093 sect->filepos = note->descpos;
11094 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11095
0a1b45a2 11096 return true;
67cc5033
MK
11097 }
11098
0a1b45a2 11099 return true;
67cc5033
MK
11100}
11101
0a1b45a2 11102static bool
d3fd4074 11103elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11104{
11105 void *ddata = note->descdata;
11106 char buf[100];
11107 char *name;
11108 asection *sect;
f8843e87
AM
11109 short sig;
11110 unsigned flags;
07c6e936 11111
80a04378 11112 if (note->descsz < 16)
0a1b45a2 11113 return false;
80a04378 11114
07c6e936 11115 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11116 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11117
f8843e87
AM
11118 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11119 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11120
11121 /* nto_procfs_status 'flags' field is at offset 8. */
11122 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11123
11124 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11125 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11126 {
228e534f
AM
11127 elf_tdata (abfd)->core->signal = sig;
11128 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11129 }
07c6e936 11130
f8843e87
AM
11131 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11132 do not come from signals so we make sure we set the current
11133 thread just in case. */
11134 if (flags & 0x00000080)
228e534f 11135 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11136
11137 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11138 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11139
a50b1753 11140 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11141 if (name == NULL)
0a1b45a2 11142 return false;
07c6e936
NC
11143 strcpy (name, buf);
11144
117ed4f8 11145 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11146 if (sect == NULL)
0a1b45a2 11147 return false;
07c6e936 11148
07d6d2b8
AM
11149 sect->size = note->descsz;
11150 sect->filepos = note->descpos;
07c6e936
NC
11151 sect->alignment_power = 2;
11152
11153 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11154}
11155
0a1b45a2 11156static bool
d69f560c
KW
11157elfcore_grok_nto_regs (bfd *abfd,
11158 Elf_Internal_Note *note,
d3fd4074 11159 long tid,
d69f560c 11160 char *base)
07c6e936
NC
11161{
11162 char buf[100];
11163 char *name;
11164 asection *sect;
11165
d69f560c 11166 /* Make a "(base)/%d" section. */
d3fd4074 11167 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11168
a50b1753 11169 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11170 if (name == NULL)
0a1b45a2 11171 return false;
07c6e936
NC
11172 strcpy (name, buf);
11173
117ed4f8 11174 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11175 if (sect == NULL)
0a1b45a2 11176 return false;
07c6e936 11177
07d6d2b8
AM
11178 sect->size = note->descsz;
11179 sect->filepos = note->descpos;
07c6e936
NC
11180 sect->alignment_power = 2;
11181
f8843e87 11182 /* This is the current thread. */
228e534f 11183 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11184 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87 11185
0a1b45a2 11186 return true;
07c6e936
NC
11187}
11188
11189#define BFD_QNT_CORE_INFO 7
11190#define BFD_QNT_CORE_STATUS 8
11191#define BFD_QNT_CORE_GREG 9
11192#define BFD_QNT_CORE_FPREG 10
11193
0a1b45a2 11194static bool
217aa764 11195elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11196{
11197 /* Every GREG section has a STATUS section before it. Store the
811072d8 11198 tid from the previous call to pass down to the next gregs
07c6e936 11199 function. */
d3fd4074 11200 static long tid = 1;
07c6e936
NC
11201
11202 switch (note->type)
11203 {
d69f560c
KW
11204 case BFD_QNT_CORE_INFO:
11205 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11206 case BFD_QNT_CORE_STATUS:
11207 return elfcore_grok_nto_status (abfd, note, &tid);
11208 case BFD_QNT_CORE_GREG:
11209 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11210 case BFD_QNT_CORE_FPREG:
11211 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11212 default:
0a1b45a2 11213 return true;
07c6e936
NC
11214 }
11215}
11216
0a1b45a2 11217static bool
b15fa79e
AM
11218elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11219{
11220 char *name;
11221 asection *sect;
11222 size_t len;
11223
11224 /* Use note name as section name. */
11225 len = note->namesz;
a50b1753 11226 name = (char *) bfd_alloc (abfd, len);
b15fa79e 11227 if (name == NULL)
0a1b45a2 11228 return false;
b15fa79e
AM
11229 memcpy (name, note->namedata, len);
11230 name[len - 1] = '\0';
11231
11232 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11233 if (sect == NULL)
0a1b45a2 11234 return false;
b15fa79e 11235
07d6d2b8
AM
11236 sect->size = note->descsz;
11237 sect->filepos = note->descpos;
b15fa79e
AM
11238 sect->alignment_power = 1;
11239
0a1b45a2 11240 return true;
b15fa79e
AM
11241}
11242
7c76fa91
MS
11243/* Function: elfcore_write_note
11244
47d9a591 11245 Inputs:
a39f3346 11246 buffer to hold note, and current size of buffer
7c76fa91
MS
11247 name of note
11248 type of note
11249 data for note
11250 size of data for note
11251
a39f3346
AM
11252 Writes note to end of buffer. ELF64 notes are written exactly as
11253 for ELF32, despite the current (as of 2006) ELF gabi specifying
11254 that they ought to have 8-byte namesz and descsz field, and have
11255 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11256
7c76fa91 11257 Return:
a39f3346 11258 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11259
11260char *
a39f3346 11261elfcore_write_note (bfd *abfd,
217aa764 11262 char *buf,
a39f3346 11263 int *bufsiz,
217aa764 11264 const char *name,
a39f3346 11265 int type,
217aa764 11266 const void *input,
a39f3346 11267 int size)
7c76fa91
MS
11268{
11269 Elf_External_Note *xnp;
d4c88bbb 11270 size_t namesz;
d4c88bbb 11271 size_t newspace;
a39f3346 11272 char *dest;
7c76fa91 11273
d4c88bbb 11274 namesz = 0;
d4c88bbb 11275 if (name != NULL)
a39f3346 11276 namesz = strlen (name) + 1;
d4c88bbb 11277
a39f3346 11278 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11279
a50b1753 11280 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11281 if (buf == NULL)
11282 return buf;
a39f3346 11283 dest = buf + *bufsiz;
7c76fa91
MS
11284 *bufsiz += newspace;
11285 xnp = (Elf_External_Note *) dest;
11286 H_PUT_32 (abfd, namesz, xnp->namesz);
11287 H_PUT_32 (abfd, size, xnp->descsz);
11288 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11289 dest = xnp->name;
11290 if (name != NULL)
11291 {
11292 memcpy (dest, name, namesz);
11293 dest += namesz;
a39f3346 11294 while (namesz & 3)
d4c88bbb
AM
11295 {
11296 *dest++ = '\0';
a39f3346 11297 ++namesz;
d4c88bbb
AM
11298 }
11299 }
11300 memcpy (dest, input, size);
a39f3346
AM
11301 dest += size;
11302 while (size & 3)
11303 {
11304 *dest++ = '\0';
11305 ++size;
11306 }
11307 return buf;
7c76fa91
MS
11308}
11309
602f1657
AM
11310/* gcc-8 warns (*) on all the strncpy calls in this function about
11311 possible string truncation. The "truncation" is not a bug. We
11312 have an external representation of structs with fields that are not
11313 necessarily NULL terminated and corresponding internal
11314 representation fields that are one larger so that they can always
11315 be NULL terminated.
11316 gcc versions between 4.2 and 4.6 do not allow pragma control of
11317 diagnostics inside functions, giving a hard error if you try to use
11318 the finer control available with later versions.
11319 gcc prior to 4.2 warns about diagnostic push and pop.
11320 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11321 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11322 (*) Depending on your system header files! */
d99b4b92 11323#if GCC_VERSION >= 8000
602f1657
AM
11324# pragma GCC diagnostic push
11325# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11326#endif
7c76fa91 11327char *
217aa764
AM
11328elfcore_write_prpsinfo (bfd *abfd,
11329 char *buf,
11330 int *bufsiz,
11331 const char *fname,
11332 const char *psargs)
7c76fa91 11333{
183e98be
AM
11334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11335
11336 if (bed->elf_backend_write_core_note != NULL)
11337 {
11338 char *ret;
11339 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11340 NT_PRPSINFO, fname, psargs);
11341 if (ret != NULL)
11342 return ret;
11343 }
7c76fa91 11344
1f20dca5 11345#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11346# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11347 if (bed->s->elfclass == ELFCLASS32)
11348 {
602f1657 11349# if defined (HAVE_PSINFO32_T)
183e98be
AM
11350 psinfo32_t data;
11351 int note_type = NT_PSINFO;
602f1657 11352# else
183e98be
AM
11353 prpsinfo32_t data;
11354 int note_type = NT_PRPSINFO;
602f1657 11355# endif
183e98be
AM
11356
11357 memset (&data, 0, sizeof (data));
11358 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11359 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11360 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11361 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11362 }
11363 else
602f1657 11364# endif
183e98be 11365 {
602f1657 11366# if defined (HAVE_PSINFO_T)
183e98be
AM
11367 psinfo_t data;
11368 int note_type = NT_PSINFO;
602f1657 11369# else
183e98be
AM
11370 prpsinfo_t data;
11371 int note_type = NT_PRPSINFO;
602f1657 11372# endif
7c76fa91 11373
183e98be
AM
11374 memset (&data, 0, sizeof (data));
11375 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11376 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11377 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11378 "CORE", note_type, &data, sizeof (data));
183e98be 11379 }
7c76fa91
MS
11380#endif /* PSINFO_T or PRPSINFO_T */
11381
1f20dca5
UW
11382 free (buf);
11383 return NULL;
11384}
d99b4b92 11385#if GCC_VERSION >= 8000
602f1657 11386# pragma GCC diagnostic pop
d99b4b92 11387#endif
1f20dca5 11388
70a38d42
SDJ
11389char *
11390elfcore_write_linux_prpsinfo32
11391 (bfd *abfd, char *buf, int *bufsiz,
11392 const struct elf_internal_linux_prpsinfo *prpsinfo)
11393{
a2f63b2e
MR
11394 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11395 {
11396 struct elf_external_linux_prpsinfo32_ugid16 data;
11397
11398 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11399 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11400 &data, sizeof (data));
11401 }
11402 else
11403 {
11404 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11405
a2f63b2e
MR
11406 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11407 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11408 &data, sizeof (data));
11409 }
70a38d42
SDJ
11410}
11411
11412char *
11413elfcore_write_linux_prpsinfo64
11414 (bfd *abfd, char *buf, int *bufsiz,
11415 const struct elf_internal_linux_prpsinfo *prpsinfo)
11416{
3c9a7b0d
MR
11417 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11418 {
11419 struct elf_external_linux_prpsinfo64_ugid16 data;
11420
11421 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11422 return elfcore_write_note (abfd, buf, bufsiz,
11423 "CORE", NT_PRPSINFO, &data, sizeof (data));
11424 }
11425 else
11426 {
11427 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11428
3c9a7b0d
MR
11429 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11430 return elfcore_write_note (abfd, buf, bufsiz,
11431 "CORE", NT_PRPSINFO, &data, sizeof (data));
11432 }
70a38d42
SDJ
11433}
11434
7c76fa91 11435char *
217aa764
AM
11436elfcore_write_prstatus (bfd *abfd,
11437 char *buf,
11438 int *bufsiz,
11439 long pid,
11440 int cursig,
11441 const void *gregs)
7c76fa91 11442{
183e98be 11443 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11444
183e98be
AM
11445 if (bed->elf_backend_write_core_note != NULL)
11446 {
11447 char *ret;
11448 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11449 NT_PRSTATUS,
11450 pid, cursig, gregs);
11451 if (ret != NULL)
11452 return ret;
11453 }
11454
1f20dca5 11455#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11456#if defined (HAVE_PRSTATUS32_T)
11457 if (bed->s->elfclass == ELFCLASS32)
11458 {
11459 prstatus32_t prstat;
11460
11461 memset (&prstat, 0, sizeof (prstat));
11462 prstat.pr_pid = pid;
11463 prstat.pr_cursig = cursig;
11464 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11465 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11466 NT_PRSTATUS, &prstat, sizeof (prstat));
11467 }
11468 else
11469#endif
11470 {
11471 prstatus_t prstat;
11472
11473 memset (&prstat, 0, sizeof (prstat));
11474 prstat.pr_pid = pid;
11475 prstat.pr_cursig = cursig;
11476 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11477 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11478 NT_PRSTATUS, &prstat, sizeof (prstat));
11479 }
7c76fa91
MS
11480#endif /* HAVE_PRSTATUS_T */
11481
1f20dca5
UW
11482 free (buf);
11483 return NULL;
11484}
11485
51316059
MS
11486#if defined (HAVE_LWPSTATUS_T)
11487char *
217aa764
AM
11488elfcore_write_lwpstatus (bfd *abfd,
11489 char *buf,
11490 int *bufsiz,
11491 long pid,
11492 int cursig,
11493 const void *gregs)
51316059
MS
11494{
11495 lwpstatus_t lwpstat;
183e98be 11496 const char *note_name = "CORE";
51316059
MS
11497
11498 memset (&lwpstat, 0, sizeof (lwpstat));
11499 lwpstat.pr_lwpid = pid >> 16;
11500 lwpstat.pr_cursig = cursig;
11501#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11502 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11503#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11504#if !defined(gregs)
11505 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11506 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11507#else
11508 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11509 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11510#endif
11511#endif
47d9a591 11512 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11513 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11514}
11515#endif /* HAVE_LWPSTATUS_T */
11516
7c76fa91
MS
11517#if defined (HAVE_PSTATUS_T)
11518char *
217aa764
AM
11519elfcore_write_pstatus (bfd *abfd,
11520 char *buf,
11521 int *bufsiz,
11522 long pid,
6c10990d
NC
11523 int cursig ATTRIBUTE_UNUSED,
11524 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11525{
183e98be
AM
11526 const char *note_name = "CORE";
11527#if defined (HAVE_PSTATUS32_T)
11528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11529
183e98be
AM
11530 if (bed->s->elfclass == ELFCLASS32)
11531 {
11532 pstatus32_t pstat;
11533
11534 memset (&pstat, 0, sizeof (pstat));
11535 pstat.pr_pid = pid & 0xffff;
11536 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11537 NT_PSTATUS, &pstat, sizeof (pstat));
11538 return buf;
11539 }
11540 else
11541#endif
11542 {
11543 pstatus_t pstat;
11544
11545 memset (&pstat, 0, sizeof (pstat));
11546 pstat.pr_pid = pid & 0xffff;
11547 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11548 NT_PSTATUS, &pstat, sizeof (pstat));
11549 return buf;
11550 }
7c76fa91
MS
11551}
11552#endif /* HAVE_PSTATUS_T */
11553
11554char *
217aa764
AM
11555elfcore_write_prfpreg (bfd *abfd,
11556 char *buf,
11557 int *bufsiz,
11558 const void *fpregs,
11559 int size)
7c76fa91 11560{
183e98be 11561 const char *note_name = "CORE";
47d9a591 11562 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11563 note_name, NT_FPREGSET, fpregs, size);
11564}
11565
11566char *
217aa764
AM
11567elfcore_write_prxfpreg (bfd *abfd,
11568 char *buf,
11569 int *bufsiz,
11570 const void *xfpregs,
11571 int size)
7c76fa91
MS
11572{
11573 char *note_name = "LINUX";
47d9a591 11574 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11575 note_name, NT_PRXFPREG, xfpregs, size);
11576}
11577
4339cae0
L
11578char *
11579elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11580 const void *xfpregs, int size)
11581{
97de3545
JB
11582 char *note_name;
11583 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11584 note_name = "FreeBSD";
11585 else
11586 note_name = "LINUX";
4339cae0
L
11587 return elfcore_write_note (abfd, buf, bufsiz,
11588 note_name, NT_X86_XSTATE, xfpregs, size);
11589}
11590
97753bd5
AM
11591char *
11592elfcore_write_ppc_vmx (bfd *abfd,
11593 char *buf,
11594 int *bufsiz,
11595 const void *ppc_vmx,
11596 int size)
11597{
11598 char *note_name = "LINUX";
11599 return elfcore_write_note (abfd, buf, bufsiz,
11600 note_name, NT_PPC_VMX, ppc_vmx, size);
11601}
11602
89eeb0bc
LM
11603char *
11604elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11605 char *buf,
11606 int *bufsiz,
11607 const void *ppc_vsx,
11608 int size)
89eeb0bc
LM
11609{
11610 char *note_name = "LINUX";
11611 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11612 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11613}
11614
cb2366c1
EBM
11615char *
11616elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11617 char *buf,
11618 int *bufsiz,
11619 const void *ppc_tar,
11620 int size)
cb2366c1
EBM
11621{
11622 char *note_name = "LINUX";
11623 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11624 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11625}
11626
11627char *
11628elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11629 char *buf,
11630 int *bufsiz,
11631 const void *ppc_ppr,
11632 int size)
cb2366c1
EBM
11633{
11634 char *note_name = "LINUX";
11635 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11636 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11637}
11638
11639char *
11640elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11641 char *buf,
11642 int *bufsiz,
11643 const void *ppc_dscr,
11644 int size)
cb2366c1
EBM
11645{
11646 char *note_name = "LINUX";
11647 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11648 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11649}
11650
11651char *
11652elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11653 char *buf,
11654 int *bufsiz,
11655 const void *ppc_ebb,
11656 int size)
cb2366c1
EBM
11657{
11658 char *note_name = "LINUX";
11659 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11660 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11661}
11662
11663char *
11664elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11665 char *buf,
11666 int *bufsiz,
11667 const void *ppc_pmu,
11668 int size)
cb2366c1
EBM
11669{
11670 char *note_name = "LINUX";
11671 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11672 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11673}
11674
11675char *
11676elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11677 char *buf,
11678 int *bufsiz,
11679 const void *ppc_tm_cgpr,
11680 int size)
cb2366c1
EBM
11681{
11682 char *note_name = "LINUX";
11683 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11684 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11685}
11686
11687char *
11688elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11689 char *buf,
11690 int *bufsiz,
11691 const void *ppc_tm_cfpr,
11692 int size)
cb2366c1
EBM
11693{
11694 char *note_name = "LINUX";
11695 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11696 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11697}
11698
11699char *
11700elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11701 char *buf,
11702 int *bufsiz,
11703 const void *ppc_tm_cvmx,
11704 int size)
cb2366c1
EBM
11705{
11706 char *note_name = "LINUX";
11707 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11708 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11709}
11710
11711char *
11712elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11713 char *buf,
11714 int *bufsiz,
11715 const void *ppc_tm_cvsx,
11716 int size)
cb2366c1
EBM
11717{
11718 char *note_name = "LINUX";
11719 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11720 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11721}
11722
11723char *
11724elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11725 char *buf,
11726 int *bufsiz,
11727 const void *ppc_tm_spr,
11728 int size)
cb2366c1
EBM
11729{
11730 char *note_name = "LINUX";
11731 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11732 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11733}
11734
11735char *
11736elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11737 char *buf,
11738 int *bufsiz,
11739 const void *ppc_tm_ctar,
11740 int size)
cb2366c1
EBM
11741{
11742 char *note_name = "LINUX";
11743 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11744 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11745}
11746
11747char *
11748elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11749 char *buf,
11750 int *bufsiz,
11751 const void *ppc_tm_cppr,
11752 int size)
cb2366c1
EBM
11753{
11754 char *note_name = "LINUX";
11755 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11756 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11757}
11758
11759char *
11760elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11761 char *buf,
11762 int *bufsiz,
11763 const void *ppc_tm_cdscr,
11764 int size)
cb2366c1
EBM
11765{
11766 char *note_name = "LINUX";
11767 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11768 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11769}
11770
0675e188
UW
11771static char *
11772elfcore_write_s390_high_gprs (bfd *abfd,
11773 char *buf,
11774 int *bufsiz,
11775 const void *s390_high_gprs,
11776 int size)
11777{
11778 char *note_name = "LINUX";
11779 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11780 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11781 s390_high_gprs, size);
11782}
11783
d7eeb400
MS
11784char *
11785elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11786 char *buf,
11787 int *bufsiz,
11788 const void *s390_timer,
11789 int size)
d7eeb400
MS
11790{
11791 char *note_name = "LINUX";
11792 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11793 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11794}
11795
11796char *
11797elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11798 char *buf,
11799 int *bufsiz,
11800 const void *s390_todcmp,
11801 int size)
d7eeb400
MS
11802{
11803 char *note_name = "LINUX";
11804 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11805 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11806}
11807
11808char *
11809elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11810 char *buf,
11811 int *bufsiz,
11812 const void *s390_todpreg,
11813 int size)
d7eeb400
MS
11814{
11815 char *note_name = "LINUX";
11816 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11817 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11818}
11819
11820char *
11821elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11822 char *buf,
11823 int *bufsiz,
11824 const void *s390_ctrs,
11825 int size)
d7eeb400
MS
11826{
11827 char *note_name = "LINUX";
11828 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11829 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11830}
11831
11832char *
11833elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11834 char *buf,
11835 int *bufsiz,
11836 const void *s390_prefix,
11837 int size)
d7eeb400
MS
11838{
11839 char *note_name = "LINUX";
11840 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11841 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11842}
11843
355b81d9
UW
11844char *
11845elfcore_write_s390_last_break (bfd *abfd,
11846 char *buf,
11847 int *bufsiz,
11848 const void *s390_last_break,
11849 int size)
11850{
11851 char *note_name = "LINUX";
11852 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11853 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11854 s390_last_break, size);
11855}
11856
11857char *
11858elfcore_write_s390_system_call (bfd *abfd,
11859 char *buf,
11860 int *bufsiz,
11861 const void *s390_system_call,
11862 int size)
11863{
11864 char *note_name = "LINUX";
11865 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11866 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11867 s390_system_call, size);
11868}
11869
abb3f6cc
NC
11870char *
11871elfcore_write_s390_tdb (bfd *abfd,
11872 char *buf,
11873 int *bufsiz,
11874 const void *s390_tdb,
11875 int size)
11876{
11877 char *note_name = "LINUX";
11878 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11879 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11880}
11881
4ef9f41a
AA
11882char *
11883elfcore_write_s390_vxrs_low (bfd *abfd,
11884 char *buf,
11885 int *bufsiz,
11886 const void *s390_vxrs_low,
11887 int size)
11888{
11889 char *note_name = "LINUX";
11890 return elfcore_write_note (abfd, buf, bufsiz,
11891 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11892}
11893
11894char *
11895elfcore_write_s390_vxrs_high (bfd *abfd,
11896 char *buf,
11897 int *bufsiz,
11898 const void *s390_vxrs_high,
11899 int size)
11900{
11901 char *note_name = "LINUX";
11902 return elfcore_write_note (abfd, buf, bufsiz,
11903 note_name, NT_S390_VXRS_HIGH,
11904 s390_vxrs_high, size);
11905}
11906
88ab90e8
AA
11907char *
11908elfcore_write_s390_gs_cb (bfd *abfd,
11909 char *buf,
11910 int *bufsiz,
11911 const void *s390_gs_cb,
11912 int size)
11913{
11914 char *note_name = "LINUX";
11915 return elfcore_write_note (abfd, buf, bufsiz,
11916 note_name, NT_S390_GS_CB,
11917 s390_gs_cb, size);
11918}
11919
11920char *
11921elfcore_write_s390_gs_bc (bfd *abfd,
11922 char *buf,
11923 int *bufsiz,
11924 const void *s390_gs_bc,
11925 int size)
11926{
11927 char *note_name = "LINUX";
11928 return elfcore_write_note (abfd, buf, bufsiz,
11929 note_name, NT_S390_GS_BC,
11930 s390_gs_bc, size);
11931}
11932
faa9a424
UW
11933char *
11934elfcore_write_arm_vfp (bfd *abfd,
11935 char *buf,
11936 int *bufsiz,
11937 const void *arm_vfp,
11938 int size)
11939{
11940 char *note_name = "LINUX";
11941 return elfcore_write_note (abfd, buf, bufsiz,
11942 note_name, NT_ARM_VFP, arm_vfp, size);
11943}
11944
652451f8
YZ
11945char *
11946elfcore_write_aarch_tls (bfd *abfd,
11947 char *buf,
11948 int *bufsiz,
11949 const void *aarch_tls,
11950 int size)
11951{
11952 char *note_name = "LINUX";
11953 return elfcore_write_note (abfd, buf, bufsiz,
11954 note_name, NT_ARM_TLS, aarch_tls, size);
11955}
11956
11957char *
11958elfcore_write_aarch_hw_break (bfd *abfd,
11959 char *buf,
11960 int *bufsiz,
11961 const void *aarch_hw_break,
11962 int size)
11963{
11964 char *note_name = "LINUX";
11965 return elfcore_write_note (abfd, buf, bufsiz,
11966 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11967}
11968
11969char *
11970elfcore_write_aarch_hw_watch (bfd *abfd,
11971 char *buf,
11972 int *bufsiz,
11973 const void *aarch_hw_watch,
11974 int size)
11975{
11976 char *note_name = "LINUX";
11977 return elfcore_write_note (abfd, buf, bufsiz,
11978 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11979}
11980
ad1cc4e4
AH
11981char *
11982elfcore_write_aarch_sve (bfd *abfd,
11983 char *buf,
11984 int *bufsiz,
11985 const void *aarch_sve,
11986 int size)
11987{
11988 char *note_name = "LINUX";
11989 return elfcore_write_note (abfd, buf, bufsiz,
11990 note_name, NT_ARM_SVE, aarch_sve, size);
11991}
11992
e6c3b5bf
AH
11993char *
11994elfcore_write_aarch_pauth (bfd *abfd,
11995 char *buf,
11996 int *bufsiz,
11997 const void *aarch_pauth,
11998 int size)
11999{
12000 char *note_name = "LINUX";
12001 return elfcore_write_note (abfd, buf, bufsiz,
12002 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12003}
12004
f0bbe8ba
LM
12005char *
12006elfcore_write_aarch_mte (bfd *abfd,
12007 char *buf,
12008 int *bufsiz,
12009 const void *aarch_mte,
12010 int size)
12011{
12012 char *note_name = "LINUX";
12013 return elfcore_write_note (abfd, buf, bufsiz,
12014 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12015 aarch_mte,
12016 size);
12017}
12018
27456742
AK
12019char *
12020elfcore_write_arc_v2 (bfd *abfd,
12021 char *buf,
12022 int *bufsiz,
12023 const void *arc_v2,
12024 int size)
12025{
12026 char *note_name = "LINUX";
12027 return elfcore_write_note (abfd, buf, bufsiz,
12028 note_name, NT_ARC_V2, arc_v2, size);
12029}
12030
db6092f3
AB
12031/* Write the buffer of csr values in CSRS (length SIZE) into the note
12032 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12033 written into. Return a pointer to the new start of the note buffer, to
12034 replace BUF which may no longer be valid. */
12035
12036char *
12037elfcore_write_riscv_csr (bfd *abfd,
12038 char *buf,
12039 int *bufsiz,
12040 const void *csrs,
12041 int size)
12042{
12043 const char *note_name = "GDB";
12044 return elfcore_write_note (abfd, buf, bufsiz,
12045 note_name, NT_RISCV_CSR, csrs, size);
12046}
12047
b63a5e38
AB
12048/* Write the target description (a string) pointed to by TDESC, length
12049 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12050 note is being written into. Return a pointer to the new start of the
12051 note buffer, to replace BUF which may no longer be valid. */
12052
12053char *
12054elfcore_write_gdb_tdesc (bfd *abfd,
12055 char *buf,
12056 int *bufsiz,
12057 const void *tdesc,
12058 int size)
12059{
12060 const char *note_name = "GDB";
12061 return elfcore_write_note (abfd, buf, bufsiz,
12062 note_name, NT_GDB_TDESC, tdesc, size);
12063}
12064
bb864ac1
CES
12065char *
12066elfcore_write_register_note (bfd *abfd,
12067 char *buf,
12068 int *bufsiz,
12069 const char *section,
12070 const void *data,
12071 int size)
12072{
12073 if (strcmp (section, ".reg2") == 0)
12074 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12075 if (strcmp (section, ".reg-xfp") == 0)
12076 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
12077 if (strcmp (section, ".reg-xstate") == 0)
12078 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12079 if (strcmp (section, ".reg-ppc-vmx") == 0)
12080 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
12081 if (strcmp (section, ".reg-ppc-vsx") == 0)
12082 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
12083 if (strcmp (section, ".reg-ppc-tar") == 0)
12084 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12085 if (strcmp (section, ".reg-ppc-ppr") == 0)
12086 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12087 if (strcmp (section, ".reg-ppc-dscr") == 0)
12088 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12089 if (strcmp (section, ".reg-ppc-ebb") == 0)
12090 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12091 if (strcmp (section, ".reg-ppc-pmu") == 0)
12092 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12093 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12094 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12095 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12096 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12097 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12098 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12099 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12100 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12101 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12102 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12103 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12104 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12105 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12106 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12107 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12108 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
12109 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12110 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12111 if (strcmp (section, ".reg-s390-timer") == 0)
12112 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12113 if (strcmp (section, ".reg-s390-todcmp") == 0)
12114 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12115 if (strcmp (section, ".reg-s390-todpreg") == 0)
12116 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12117 if (strcmp (section, ".reg-s390-ctrs") == 0)
12118 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12119 if (strcmp (section, ".reg-s390-prefix") == 0)
12120 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12121 if (strcmp (section, ".reg-s390-last-break") == 0)
12122 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12123 if (strcmp (section, ".reg-s390-system-call") == 0)
12124 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12125 if (strcmp (section, ".reg-s390-tdb") == 0)
12126 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12127 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12128 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12129 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12130 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12131 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12132 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12133 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12134 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12135 if (strcmp (section, ".reg-arm-vfp") == 0)
12136 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12137 if (strcmp (section, ".reg-aarch-tls") == 0)
12138 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12139 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12140 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12141 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12142 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12143 if (strcmp (section, ".reg-aarch-sve") == 0)
12144 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12145 if (strcmp (section, ".reg-aarch-pauth") == 0)
12146 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
f0bbe8ba
LM
12147 if (strcmp (section, ".reg-aarch-mte") == 0)
12148 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
27456742
AK
12149 if (strcmp (section, ".reg-arc-v2") == 0)
12150 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
b63a5e38
AB
12151 if (strcmp (section, ".gdb-tdesc") == 0)
12152 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
db6092f3
AB
12153 if (strcmp (section, ".reg-riscv-csr") == 0)
12154 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12155 return NULL;
12156}
12157
4cb1265b
MS
12158char *
12159elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12160 const void *buf, int bufsiz)
12161{
12162 return elfcore_write_note (obfd, note_data, note_size,
12163 "CORE", NT_FILE, buf, bufsiz);
12164}
12165
0a1b45a2 12166static bool
276da9b3
L
12167elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12168 size_t align)
252b5132 12169{
c044fabd 12170 char *p;
252b5132 12171
276da9b3
L
12172 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12173 gABI specifies that PT_NOTE alignment should be aligned to 4
12174 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12175 align is less than 4, we use 4 byte alignment. */
12176 if (align < 4)
12177 align = 4;
ef135d43 12178 if (align != 4 && align != 8)
0a1b45a2 12179 return false;
276da9b3 12180
252b5132
RH
12181 p = buf;
12182 while (p < buf + size)
12183 {
c044fabd 12184 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12185 Elf_Internal_Note in;
12186
baea7ef1 12187 if (offsetof (Elf_External_Note, name) > buf - p + size)
0a1b45a2 12188 return false;
baea7ef1 12189
dc810e39 12190 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12191
dc810e39 12192 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12193 in.namedata = xnp->name;
baea7ef1 12194 if (in.namesz > buf - in.namedata + size)
0a1b45a2 12195 return false;
252b5132 12196
dc810e39 12197 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12198 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12199 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12200 if (in.descsz != 0
12201 && (in.descdata >= buf + size
12202 || in.descsz > buf - in.descdata + size))
0a1b45a2 12203 return false;
252b5132 12204
718175fa 12205 switch (bfd_get_format (abfd))
07d6d2b8 12206 {
718175fa 12207 default:
0a1b45a2 12208 return true;
718175fa
JK
12209
12210 case bfd_core:
f64e188b 12211 {
8acbedd6 12212#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12213 struct
718175fa 12214 {
f64e188b 12215 const char * string;
8acbedd6 12216 size_t len;
0a1b45a2 12217 bool (*func) (bfd *, Elf_Internal_Note *);
718175fa 12218 }
f64e188b 12219 grokers[] =
b15fa79e 12220 {
8acbedd6 12221 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12222 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
12223 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12224 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12225 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
12226 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12227 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 12228 };
8acbedd6 12229#undef GROKER_ELEMENT
f64e188b
NC
12230 int i;
12231
12232 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12233 {
12234 if (in.namesz >= grokers[i].len
12235 && strncmp (in.namedata, grokers[i].string,
12236 grokers[i].len) == 0)
12237 {
12238 if (! grokers[i].func (abfd, & in))
0a1b45a2 12239 return false;
8acbedd6
KS
12240 break;
12241 }
12242 }
f64e188b
NC
12243 break;
12244 }
718175fa
JK
12245
12246 case bfd_object:
12247 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12248 {
12249 if (! elfobj_grok_gnu_note (abfd, &in))
0a1b45a2 12250 return false;
718175fa 12251 }
e21e5835
NC
12252 else if (in.namesz == sizeof "stapsdt"
12253 && strcmp (in.namedata, "stapsdt") == 0)
12254 {
12255 if (! elfobj_grok_stapsdt_note (abfd, &in))
0a1b45a2 12256 return false;
e21e5835 12257 }
718175fa 12258 break;
08a40648 12259 }
252b5132 12260
276da9b3 12261 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12262 }
12263
0a1b45a2 12264 return true;
718175fa
JK
12265}
12266
0a1b45a2 12267bool
276da9b3
L
12268elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12269 size_t align)
718175fa
JK
12270{
12271 char *buf;
12272
957e1fc1 12273 if (size == 0 || (size + 1) == 0)
0a1b45a2 12274 return true;
718175fa
JK
12275
12276 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
0a1b45a2 12277 return false;
718175fa 12278
2bb3687b 12279 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa 12280 if (buf == NULL)
0a1b45a2 12281 return false;
718175fa 12282
f64e188b
NC
12283 /* PR 17512: file: ec08f814
12284 0-termintate the buffer so that string searches will not overflow. */
12285 buf[size] = 0;
12286
2bb3687b 12287 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12288 {
12289 free (buf);
0a1b45a2 12290 return false;
718175fa
JK
12291 }
12292
252b5132 12293 free (buf);
0a1b45a2 12294 return true;
252b5132 12295}
98d8431c
JB
12296\f
12297/* Providing external access to the ELF program header table. */
12298
12299/* Return an upper bound on the number of bytes required to store a
12300 copy of ABFD's program header table entries. Return -1 if an error
12301 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12302
98d8431c 12303long
217aa764 12304bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12305{
12306 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12307 {
12308 bfd_set_error (bfd_error_wrong_format);
12309 return -1;
12310 }
12311
936e320b 12312 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12313}
12314
98d8431c
JB
12315/* Copy ABFD's program header table entries to *PHDRS. The entries
12316 will be stored as an array of Elf_Internal_Phdr structures, as
12317 defined in include/elf/internal.h. To find out how large the
12318 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12319
12320 Return the number of program header table entries read, or -1 if an
12321 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12322
98d8431c 12323int
217aa764 12324bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12325{
12326 int num_phdrs;
12327
12328 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12329 {
12330 bfd_set_error (bfd_error_wrong_format);
12331 return -1;
12332 }
12333
12334 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12335 if (num_phdrs != 0)
12336 memcpy (phdrs, elf_tdata (abfd)->phdr,
12337 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12338
12339 return num_phdrs;
12340}
ae4221d7 12341
db6751f2 12342enum elf_reloc_type_class
7e612e98
AM
12343_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12344 const asection *rel_sec ATTRIBUTE_UNUSED,
12345 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12346{
12347 return reloc_class_normal;
12348}
f8df10f4 12349
47d9a591 12350/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12351 relocation against a local symbol. */
12352
12353bfd_vma
217aa764
AM
12354_bfd_elf_rela_local_sym (bfd *abfd,
12355 Elf_Internal_Sym *sym,
8517fae7 12356 asection **psec,
217aa764 12357 Elf_Internal_Rela *rel)
f8df10f4 12358{
8517fae7 12359 asection *sec = *psec;
f8df10f4
JJ
12360 bfd_vma relocation;
12361
6835821b
AM
12362 relocation = (sec->output_section->vma
12363 + sec->output_offset
12364 + sym->st_value);
f8df10f4 12365 if ((sec->flags & SEC_MERGE)
c629eae0 12366 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12367 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12368 {
f8df10f4 12369 rel->r_addend =
8517fae7 12370 _bfd_merged_section_offset (abfd, psec,
65765700 12371 elf_section_data (sec)->sec_info,
753731ee
AM
12372 sym->st_value + rel->r_addend);
12373 if (sec != *psec)
12374 {
12375 /* If we have changed the section, and our original section is
12376 marked with SEC_EXCLUDE, it means that the original
12377 SEC_MERGE section has been completely subsumed in some
12378 other SEC_MERGE section. In this case, we need to leave
12379 some info around for --emit-relocs. */
12380 if ((sec->flags & SEC_EXCLUDE) != 0)
12381 sec->kept_section = *psec;
12382 sec = *psec;
12383 }
8517fae7
AM
12384 rel->r_addend -= relocation;
12385 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12386 }
12387 return relocation;
12388}
c629eae0
JJ
12389
12390bfd_vma
217aa764
AM
12391_bfd_elf_rel_local_sym (bfd *abfd,
12392 Elf_Internal_Sym *sym,
12393 asection **psec,
12394 bfd_vma addend)
47d9a591 12395{
c629eae0
JJ
12396 asection *sec = *psec;
12397
6835821b 12398 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12399 return sym->st_value + addend;
12400
12401 return _bfd_merged_section_offset (abfd, psec,
65765700 12402 elf_section_data (sec)->sec_info,
753731ee 12403 sym->st_value + addend);
c629eae0
JJ
12404}
12405
37b01f6a
DG
12406/* Adjust an address within a section. Given OFFSET within SEC, return
12407 the new offset within the section, based upon changes made to the
12408 section. Returns -1 if the offset is now invalid.
12409 The offset (in abnd out) is in target sized bytes, however big a
12410 byte may be. */
12411
c629eae0 12412bfd_vma
217aa764 12413_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12414 struct bfd_link_info *info,
217aa764
AM
12415 asection *sec,
12416 bfd_vma offset)
c629eae0 12417{
68bfbfcc 12418 switch (sec->sec_info_type)
65765700 12419 {
dbaa2011 12420 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12421 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12422 offset);
dbaa2011 12423 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12424 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12425
65765700 12426 default:
310fd250
L
12427 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12428 {
37b01f6a 12429 /* Reverse the offset. */
310fd250
L
12430 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12431 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12432
12433 /* address_size and sec->size are in octets. Convert
12434 to bytes before subtracting the original offset. */
61826503 12435 offset = ((sec->size - address_size)
bb294208 12436 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12437 }
65765700
JJ
12438 return offset;
12439 }
c629eae0 12440}
3333a7c3
RM
12441\f
12442/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12443 reconstruct an ELF file by reading the segments out of remote memory
12444 based on the ELF file header at EHDR_VMA and the ELF program headers it
12445 points to. If not null, *LOADBASEP is filled in with the difference
12446 between the VMAs from which the segments were read, and the VMAs the
12447 file headers (and hence BFD's idea of each section's VMA) put them at.
12448
12449 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12450 remote memory at target address VMA into the local buffer at MYADDR; it
12451 should return zero on success or an `errno' code on failure. TEMPL must
12452 be a BFD for an ELF target with the word size and byte order found in
12453 the remote memory. */
12454
12455bfd *
217aa764
AM
12456bfd_elf_bfd_from_remote_memory
12457 (bfd *templ,
12458 bfd_vma ehdr_vma,
f0a5d95a 12459 bfd_size_type size,
217aa764 12460 bfd_vma *loadbasep,
fe78531d 12461 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12462{
12463 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12464 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12465}
4c45e5c9
JJ
12466\f
12467long
c9727e01
AM
12468_bfd_elf_get_synthetic_symtab (bfd *abfd,
12469 long symcount ATTRIBUTE_UNUSED,
12470 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12471 long dynsymcount,
c9727e01
AM
12472 asymbol **dynsyms,
12473 asymbol **ret)
4c45e5c9
JJ
12474{
12475 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12476 asection *relplt;
12477 asymbol *s;
12478 const char *relplt_name;
0a1b45a2 12479 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
4c45e5c9
JJ
12480 arelent *p;
12481 long count, i, n;
12482 size_t size;
12483 Elf_Internal_Shdr *hdr;
12484 char *names;
12485 asection *plt;
12486
8615f3f2
AM
12487 *ret = NULL;
12488
90e3cdf2
JJ
12489 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12490 return 0;
12491
8615f3f2
AM
12492 if (dynsymcount <= 0)
12493 return 0;
12494
4c45e5c9
JJ
12495 if (!bed->plt_sym_val)
12496 return 0;
12497
12498 relplt_name = bed->relplt_name;
12499 if (relplt_name == NULL)
d35fd659 12500 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12501 relplt = bfd_get_section_by_name (abfd, relplt_name);
12502 if (relplt == NULL)
12503 return 0;
12504
12505 hdr = &elf_section_data (relplt)->this_hdr;
12506 if (hdr->sh_link != elf_dynsymtab (abfd)
12507 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12508 return 0;
12509
12510 plt = bfd_get_section_by_name (abfd, ".plt");
12511 if (plt == NULL)
12512 return 0;
12513
12514 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 12515 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
4c45e5c9
JJ
12516 return -1;
12517
eea6121a 12518 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12519 size = count * sizeof (asymbol);
12520 p = relplt->relocation;
cb53bf42 12521 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12522 {
12523 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12524 if (p->addend != 0)
12525 {
12526#ifdef BFD64
12527 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12528#else
12529 size += sizeof ("+0x") - 1 + 8;
12530#endif
12531 }
12532 }
4c45e5c9 12533
a50b1753 12534 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12535 if (s == NULL)
12536 return -1;
12537
12538 names = (char *) (s + count);
12539 p = relplt->relocation;
12540 n = 0;
cb53bf42 12541 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12542 {
12543 size_t len;
12544 bfd_vma addr;
12545
12546 addr = bed->plt_sym_val (i, plt, p);
12547 if (addr == (bfd_vma) -1)
12548 continue;
12549
12550 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12551 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12552 we are defining a symbol, ensure one of them is set. */
12553 if ((s->flags & BSF_LOCAL) == 0)
12554 s->flags |= BSF_GLOBAL;
6ba2a415 12555 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12556 s->section = plt;
12557 s->value = addr - plt->vma;
12558 s->name = names;
8f39ba8e 12559 s->udata.p = NULL;
4c45e5c9
JJ
12560 len = strlen ((*p->sym_ptr_ptr)->name);
12561 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12562 names += len;
041de40d
AM
12563 if (p->addend != 0)
12564 {
1d770845 12565 char buf[30], *a;
d324f6d6 12566
041de40d
AM
12567 memcpy (names, "+0x", sizeof ("+0x") - 1);
12568 names += sizeof ("+0x") - 1;
1d770845
L
12569 bfd_sprintf_vma (abfd, buf, p->addend);
12570 for (a = buf; *a == '0'; ++a)
12571 ;
12572 len = strlen (a);
12573 memcpy (names, a, len);
12574 names += len;
041de40d 12575 }
4c45e5c9
JJ
12576 memcpy (names, "@plt", sizeof ("@plt"));
12577 names += sizeof ("@plt");
8f39ba8e 12578 ++s, ++n;
4c45e5c9
JJ
12579 }
12580
12581 return n;
12582}
3d7f7666 12583
821e6ff6
AM
12584/* It is only used by x86-64 so far.
12585 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12586 but current usage would allow all of _bfd_std_section to be zero. */
12587static const asymbol lcomm_sym
12588 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12589asection _bfd_elf_large_com_section
7eacd66b 12590 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12591 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12592
0a1b45a2 12593bool
cc364be6 12594_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12595{
12596 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12597
12598 i_ehdrp = elf_elfheader (abfd);
12599
06f44071
AM
12600 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12601 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12602
df3a023b 12603 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
12604 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12605 or STB_GNU_UNIQUE binding. */
cc364be6
AM
12606 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12607 {
12608 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12609 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12610 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12611 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12612 {
12613 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
12614 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12615 "and FreeBSD targets"));
cc364be6 12616 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
12617 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12618 "only by GNU and FreeBSD targets"));
cc364be6 12619 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
12620 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12621 "only by GNU and FreeBSD targets"));
12622 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
12623 _bfd_error_handler (_("GNU_RETAIN section is supported "
12624 "only by GNU and FreeBSD targets"));
9aea1e31 12625 bfd_set_error (bfd_error_sorry);
0a1b45a2 12626 return false;
cc364be6
AM
12627 }
12628 }
0a1b45a2 12629 return true;
d1036acb 12630}
fcb93ecf
PB
12631
12632
12633/* Return TRUE for ELF symbol types that represent functions.
12634 This is the default version of this function, which is sufficient for
d8045f23 12635 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf 12636
0a1b45a2 12637bool
fcb93ecf
PB
12638_bfd_elf_is_function_type (unsigned int type)
12639{
d8045f23
NC
12640 return (type == STT_FUNC
12641 || type == STT_GNU_IFUNC);
fcb93ecf 12642}
9f296da3 12643
aef36ac1
AM
12644/* If the ELF symbol SYM might be a function in SEC, return the
12645 function size and set *CODE_OFF to the function's entry point,
12646 otherwise return zero. */
9f296da3 12647
aef36ac1
AM
12648bfd_size_type
12649_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12650 bfd_vma *code_off)
9f296da3 12651{
aef36ac1 12652 bfd_size_type size;
24aebc79 12653 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1 12654
ff9e0f5b 12655 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12656 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12657 || sym->section != sec)
12658 return 0;
ff9e0f5b 12659
24aebc79
NC
12660 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
12661
12662 /* In theory we should check that the symbol's type satisfies
12663 _bfd_elf_is_function_type(), but there are some function-like
12664 symbols which would fail this test. (eg _start). Instead
12665 we check for hidden, local, notype symbols with zero size.
12666 This type of symbol is generated by the annobin plugin for gcc
12667 and clang, and should not be considered to be a function symbol. */
12668 if (size == 0
12669 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
12670 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
12671 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
12672 return 0;
12673
ff9e0f5b 12674 *code_off = sym->value;
24aebc79
NC
12675 /* Do not return 0 for the function's size. */
12676 return size ? size : 1;
9f296da3 12677}
a8e14f4c
NC
12678
12679/* Set to non-zero to enable some debug messages. */
12680#define DEBUG_SECONDARY_RELOCS 0
12681
12682/* An internal-to-the-bfd-library only section type
12683 used to indicate a cached secondary reloc section. */
12684#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12685
12686/* Create a BFD section to hold a secondary reloc section. */
12687
0a1b45a2 12688bool
a8e14f4c
NC
12689_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12690 Elf_Internal_Shdr *hdr,
12691 const char * name,
12692 unsigned int shindex)
12693{
12694 /* We only support RELA secondary relocs. */
12695 if (hdr->sh_type != SHT_RELA)
0a1b45a2 12696 return false;
a8e14f4c
NC
12697
12698#if DEBUG_SECONDARY_RELOCS
12699 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12700#endif
12701 hdr->sh_type = SHT_SECONDARY_RELOC;
12702 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12703}
12704
12705/* Read in any secondary relocs associated with SEC. */
12706
0a1b45a2 12707bool
f60742b2
NC
12708_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12709 asection * sec,
12710 asymbol ** symbols,
0a1b45a2 12711 bool dynamic)
a8e14f4c
NC
12712{
12713 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12714 asection * relsec;
0a1b45a2 12715 bool result = true;
a8e14f4c
NC
12716 bfd_vma (*r_sym) (bfd_vma);
12717
12718#if BFD_DEFAULT_TARGET_SIZE > 32
12719 if (bfd_arch_bits_per_address (abfd) != 32)
12720 r_sym = elf64_r_sym;
12721 else
12722#endif
12723 r_sym = elf32_r_sym;
12724
956ea65c
MM
12725 if (!elf_section_data (sec)->has_secondary_relocs)
12726 return true;
12727
a8e14f4c
NC
12728 /* Discover if there are any secondary reloc sections
12729 associated with SEC. */
12730 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12731 {
12732 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12733
12734 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
12735 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
12736 && (hdr->sh_entsize == ebd->s->sizeof_rel
12737 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
12738 {
12739 bfd_byte * native_relocs;
12740 bfd_byte * native_reloc;
12741 arelent * internal_relocs;
12742 arelent * internal_reloc;
12743 unsigned int i;
12744 unsigned int entsize;
12745 unsigned int symcount;
12746 unsigned int reloc_count;
12747 size_t amt;
12748
12749 if (ebd->elf_info_to_howto == NULL)
0a1b45a2 12750 return false;
a8e14f4c
NC
12751
12752#if DEBUG_SECONDARY_RELOCS
12753 fprintf (stderr, "read secondary relocs for %s from %s\n",
12754 sec->name, relsec->name);
12755#endif
12756 entsize = hdr->sh_entsize;
12757
12758 native_relocs = bfd_malloc (hdr->sh_size);
12759 if (native_relocs == NULL)
12760 {
0a1b45a2 12761 result = false;
a8e14f4c
NC
12762 continue;
12763 }
12764
12765 reloc_count = NUM_SHDR_ENTRIES (hdr);
12766 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12767 {
ecbbbdba 12768 free (native_relocs);
a8e14f4c 12769 bfd_set_error (bfd_error_file_too_big);
0a1b45a2 12770 result = false;
a8e14f4c
NC
12771 continue;
12772 }
12773
12774 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12775 if (internal_relocs == NULL)
12776 {
12777 free (native_relocs);
0a1b45a2 12778 result = false;
a8e14f4c
NC
12779 continue;
12780 }
12781
12782 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12783 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12784 != hdr->sh_size))
12785 {
12786 free (native_relocs);
ecbbbdba
NC
12787 /* The internal_relocs will be freed when
12788 the memory for the bfd is released. */
0a1b45a2 12789 result = false;
a8e14f4c
NC
12790 continue;
12791 }
12792
f60742b2
NC
12793 if (dynamic)
12794 symcount = bfd_get_dynamic_symcount (abfd);
12795 else
12796 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
12797
12798 for (i = 0, internal_reloc = internal_relocs,
12799 native_reloc = native_relocs;
12800 i < reloc_count;
12801 i++, internal_reloc++, native_reloc += entsize)
12802 {
0a1b45a2 12803 bool res;
a8e14f4c
NC
12804 Elf_Internal_Rela rela;
12805
8ee54925
NC
12806 if (entsize == ebd->s->sizeof_rel)
12807 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
12808 else /* entsize == ebd->s->sizeof_rela */
12809 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
12810
12811 /* The address of an ELF reloc is section relative for an object
12812 file, and absolute for an executable file or shared library.
12813 The address of a normal BFD reloc is always section relative,
12814 and the address of a dynamic reloc is absolute.. */
12815 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12816 internal_reloc->address = rela.r_offset;
12817 else
12818 internal_reloc->address = rela.r_offset - sec->vma;
12819
12820 if (r_sym (rela.r_info) == STN_UNDEF)
12821 {
12822 /* FIXME: This and the error case below mean that we
12823 have a symbol on relocs that is not elf_symbol_type. */
12824 internal_reloc->sym_ptr_ptr =
12825 bfd_abs_section_ptr->symbol_ptr_ptr;
12826 }
12827 else if (r_sym (rela.r_info) > symcount)
12828 {
12829 _bfd_error_handler
12830 /* xgettext:c-format */
12831 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12832 abfd, sec, i, (long) r_sym (rela.r_info));
12833 bfd_set_error (bfd_error_bad_value);
12834 internal_reloc->sym_ptr_ptr =
12835 bfd_abs_section_ptr->symbol_ptr_ptr;
0a1b45a2 12836 result = false;
a8e14f4c
NC
12837 }
12838 else
12839 {
12840 asymbol **ps;
12841
12842 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
12843 internal_reloc->sym_ptr_ptr = ps;
12844 /* Make sure that this symbol is not removed by strip. */
12845 (*ps)->flags |= BSF_KEEP;
12846 }
12847
12848 internal_reloc->addend = rela.r_addend;
12849
12850 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12851 if (! res || internal_reloc->howto == NULL)
12852 {
12853#if DEBUG_SECONDARY_RELOCS
12854 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12855 rela.r_info);
12856#endif
0a1b45a2 12857 result = false;
a8e14f4c
NC
12858 }
12859 }
12860
12861 free (native_relocs);
12862 /* Store the internal relocs. */
12863 elf_section_data (relsec)->sec_info = internal_relocs;
12864 }
12865 }
12866
12867 return result;
12868}
12869
12870/* Set the ELF section header fields of an output secondary reloc section. */
12871
0a1b45a2 12872bool
a8e14f4c
NC
12873_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12874 bfd * obfd ATTRIBUTE_UNUSED,
12875 const Elf_Internal_Shdr * isection,
12876 Elf_Internal_Shdr * osection)
12877{
12878 asection * isec;
12879 asection * osec;
44466e45 12880 struct bfd_elf_section_data * esd;
a8e14f4c
NC
12881
12882 if (isection == NULL)
0a1b45a2 12883 return false;
a8e14f4c
NC
12884
12885 if (isection->sh_type != SHT_SECONDARY_RELOC)
0a1b45a2 12886 return true;
a8e14f4c
NC
12887
12888 isec = isection->bfd_section;
12889 if (isec == NULL)
0a1b45a2 12890 return false;
a8e14f4c
NC
12891
12892 osec = osection->bfd_section;
12893 if (osec == NULL)
0a1b45a2 12894 return false;
a8e14f4c 12895
44466e45
NC
12896 esd = elf_section_data (osec);
12897 BFD_ASSERT (esd->sec_info == NULL);
12898 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
12899 osection->sh_type = SHT_RELA;
12900 osection->sh_link = elf_onesymtab (obfd);
12901 if (osection->sh_link == 0)
12902 {
12903 /* There is no symbol table - we are hosed... */
12904 _bfd_error_handler
12905 /* xgettext:c-format */
12906 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12907 obfd, osec);
12908 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12909 return false;
a8e14f4c
NC
12910 }
12911
12912 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12913 if (isection->sh_info == 0
12914 || isection->sh_info >= elf_numsections (ibfd))
12915 {
12916 _bfd_error_handler
12917 /* xgettext:c-format */
12918 (_("%pB(%pA): info section index is invalid"),
12919 obfd, osec);
12920 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12921 return false;
327ef784
NC
12922 }
12923
a8e14f4c
NC
12924 isection = elf_elfsections (ibfd)[isection->sh_info];
12925
327ef784
NC
12926 if (isection == NULL
12927 || isection->bfd_section == NULL
12928 || isection->bfd_section->output_section == NULL)
12929 {
12930 _bfd_error_handler
12931 /* xgettext:c-format */
12932 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12933 obfd, osec);
12934 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12935 return false;
327ef784
NC
12936 }
12937
44466e45
NC
12938 esd = elf_section_data (isection->bfd_section->output_section);
12939 BFD_ASSERT (esd != NULL);
12940 osection->sh_info = esd->this_idx;
0a1b45a2 12941 esd->has_secondary_relocs = true;
a8e14f4c
NC
12942#if DEBUG_SECONDARY_RELOCS
12943 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12944 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
12945 fprintf (stderr, "mark section %s as having secondary relocs\n",
12946 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
12947#endif
12948
0a1b45a2 12949 return true;
a8e14f4c
NC
12950}
12951
44466e45
NC
12952/* Write out a secondary reloc section.
12953
12954 FIXME: Currently this function can result in a serious performance penalty
12955 for files with secondary relocs and lots of sections. The proper way to
12956 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12957 relocs together and then to have this function just walk that chain. */
a8e14f4c 12958
0a1b45a2 12959bool
a8e14f4c
NC
12960_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12961{
12962 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12963 bfd_vma addr_offset;
12964 asection * relsec;
12965 bfd_vma (*r_info) (bfd_vma, bfd_vma);
0a1b45a2 12966 bool result = true;
ac4bf06c
NC
12967
12968 if (sec == NULL)
0a1b45a2 12969 return false;
a8e14f4c
NC
12970
12971#if BFD_DEFAULT_TARGET_SIZE > 32
12972 if (bfd_arch_bits_per_address (abfd) != 32)
12973 r_info = elf64_r_info;
12974 else
12975#endif
12976 r_info = elf32_r_info;
12977
a8e14f4c
NC
12978 /* The address of an ELF reloc is section relative for an object
12979 file, and absolute for an executable file or shared library.
12980 The address of a BFD reloc is always section relative. */
12981 addr_offset = 0;
12982 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12983 addr_offset = sec->vma;
12984
12985 /* Discover if there are any secondary reloc sections
12986 associated with SEC. */
12987 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12988 {
12989 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12990 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12991
12992 if (hdr->sh_type == SHT_RELA
12993 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12994 {
12995 asymbol * last_sym;
12996 int last_sym_idx;
12997 unsigned int reloc_count;
12998 unsigned int idx;
8ee54925 12999 unsigned int entsize;
a8e14f4c
NC
13000 arelent * src_irel;
13001 bfd_byte * dst_rela;
13002
ac4bf06c
NC
13003 if (hdr->contents != NULL)
13004 {
13005 _bfd_error_handler
13006 /* xgettext:c-format */
13007 (_("%pB(%pA): error: secondary reloc section processed twice"),
13008 abfd, relsec);
13009 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13010 result = false;
ac4bf06c
NC
13011 continue;
13012 }
a8e14f4c 13013
8ee54925
NC
13014 entsize = hdr->sh_entsize;
13015 if (entsize == 0)
ac267c75
NC
13016 {
13017 _bfd_error_handler
13018 /* xgettext:c-format */
13019 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
13020 abfd, relsec);
13021 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13022 result = false;
ac267c75
NC
13023 continue;
13024 }
8ee54925
NC
13025 else if (entsize != ebd->s->sizeof_rel
13026 && entsize != ebd->s->sizeof_rela)
13027 {
13028 _bfd_error_handler
13029 /* xgettext:c-format */
13030 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
13031 abfd, relsec);
13032 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13033 result = false;
8ee54925
NC
13034 continue;
13035 }
ac267c75 13036
8ee54925 13037 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
13038 if (reloc_count <= 0)
13039 {
13040 _bfd_error_handler
13041 /* xgettext:c-format */
13042 (_("%pB(%pA): error: secondary reloc section is empty!"),
13043 abfd, relsec);
13044 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13045 result = false;
ac4bf06c
NC
13046 continue;
13047 }
a8e14f4c
NC
13048
13049 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13050 if (hdr->contents == NULL)
13051 continue;
13052
13053#if DEBUG_SECONDARY_RELOCS
13054 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13055 reloc_count, sec->name, relsec->name);
13056#endif
13057 last_sym = NULL;
13058 last_sym_idx = 0;
13059 dst_rela = hdr->contents;
13060 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
13061 if (src_irel == NULL)
13062 {
13063 _bfd_error_handler
13064 /* xgettext:c-format */
13065 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
13066 abfd, relsec);
13067 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13068 result = false;
ac4bf06c
NC
13069 continue;
13070 }
a8e14f4c 13071
8ee54925 13072 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
13073 {
13074 Elf_Internal_Rela src_rela;
13075 arelent *ptr;
13076 asymbol *sym;
13077 int n;
13078
13079 ptr = src_irel + idx;
ac4bf06c
NC
13080 if (ptr == NULL)
13081 {
13082 _bfd_error_handler
13083 /* xgettext:c-format */
13084 (_("%pB(%pA): error: reloc table entry %u is empty"),
13085 abfd, relsec, idx);
13086 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13087 result = false;
ac4bf06c
NC
13088 break;
13089 }
a8e14f4c 13090
ac4bf06c
NC
13091 if (ptr->sym_ptr_ptr == NULL)
13092 {
13093 /* FIXME: Is this an error ? */
13094 n = 0;
13095 }
a8e14f4c
NC
13096 else
13097 {
ac4bf06c
NC
13098 sym = *ptr->sym_ptr_ptr;
13099
13100 if (sym == last_sym)
13101 n = last_sym_idx;
13102 else
a8e14f4c 13103 {
ac4bf06c
NC
13104 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13105 if (n < 0)
13106 {
13107 _bfd_error_handler
13108 /* xgettext:c-format */
13109 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
13110 abfd, relsec, idx);
13111 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13112 result = false;
ac4bf06c
NC
13113 n = 0;
13114 }
13115
13116 last_sym = sym;
13117 last_sym_idx = n;
a8e14f4c 13118 }
a8e14f4c 13119
ac4bf06c
NC
13120 if (sym->the_bfd != NULL
13121 && sym->the_bfd->xvec != abfd->xvec
13122 && ! _bfd_elf_validate_reloc (abfd, ptr))
13123 {
13124 _bfd_error_handler
13125 /* xgettext:c-format */
13126 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
13127 abfd, relsec, idx);
13128 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13129 result = false;
ac4bf06c
NC
13130 n = 0;
13131 }
a8e14f4c
NC
13132 }
13133
ac4bf06c 13134 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13135 if (ptr->howto == NULL)
13136 {
ac4bf06c
NC
13137 _bfd_error_handler
13138 /* xgettext:c-format */
13139 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13140 abfd, relsec, idx);
13141 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13142 result = false;
ac4bf06c 13143 src_rela.r_info = r_info (0, 0);
a8e14f4c 13144 }
ac4bf06c
NC
13145 else
13146 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13147 src_rela.r_addend = ptr->addend;
8ee54925
NC
13148
13149 if (entsize == ebd->s->sizeof_rel)
13150 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13151 else /* entsize == ebd->s->sizeof_rela */
13152 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13153 }
13154 }
13155 }
13156
ac4bf06c 13157 return result;
a8e14f4c 13158}
This page took 2.802891 seconds and 4 git commands to generate.