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 }
a7ba3896
NC
2470 goto success;
2471 }
a8e14f4c 2472
ef53be89 2473 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2474 if (hdr2 == NULL)
bf67003b 2475 goto fail;
252b5132 2476 *hdr2 = *hdr;
d4730f92 2477 *p_hdr = hdr2;
252b5132 2478 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2479 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2480 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2481 target_sect->flags |= SEC_RELOC;
2482 target_sect->relocation = NULL;
2483 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2484 /* In the section to which the relocations apply, mark whether
2485 its relocations are of the REL or RELA variety. */
72730e0c 2486 if (hdr->sh_size != 0)
d4730f92
BS
2487 {
2488 if (hdr->sh_type == SHT_RELA)
2489 target_sect->use_rela_p = 1;
2490 }
252b5132 2491 abfd->flags |= HAS_RELOC;
bf67003b 2492 goto success;
252b5132 2493 }
252b5132
RH
2494
2495 case SHT_GNU_verdef:
2496 elf_dynverdef (abfd) = shindex;
2497 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2498 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2499 goto success;
252b5132
RH
2500
2501 case SHT_GNU_versym:
a50b2160 2502 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2503 goto fail;
2504
252b5132
RH
2505 elf_dynversym (abfd) = shindex;
2506 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2507 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2508 goto success;
252b5132
RH
2509
2510 case SHT_GNU_verneed:
2511 elf_dynverref (abfd) = shindex;
2512 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2513 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2514 goto success;
252b5132
RH
2515
2516 case SHT_SHLIB:
bf67003b 2517 goto success;
252b5132 2518
dbb410c3 2519 case SHT_GROUP:
44534af3 2520 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2521 goto fail;
2522
6dc132d9 2523 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2524 goto fail;
2525
bf67003b 2526 goto success;
dbb410c3 2527
252b5132 2528 default:
104d59d1
JM
2529 /* Possibly an attributes section. */
2530 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2531 || hdr->sh_type == bed->obj_attrs_section_type)
2532 {
2533 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2534 goto fail;
104d59d1 2535 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2536 goto success;
104d59d1
JM
2537 }
2538
252b5132 2539 /* Check for any processor-specific section types. */
3eb70a79 2540 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2541 goto success;
3eb70a79
L
2542
2543 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2544 {
2545 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2546 /* FIXME: How to properly handle allocated section reserved
2547 for applications? */
4eca0228 2548 _bfd_error_handler
695344c0 2549 /* xgettext:c-format */
871b3ab2 2550 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2551 abfd, hdr->sh_type, name);
3eb70a79 2552 else
bf67003b
NC
2553 {
2554 /* Allow sections reserved for applications. */
2555 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2556 shindex);
2557 goto success;
2558 }
3eb70a79
L
2559 }
2560 else if (hdr->sh_type >= SHT_LOPROC
2561 && hdr->sh_type <= SHT_HIPROC)
2562 /* FIXME: We should handle this section. */
4eca0228 2563 _bfd_error_handler
695344c0 2564 /* xgettext:c-format */
871b3ab2 2565 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2566 abfd, hdr->sh_type, name);
3eb70a79 2567 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2568 {
2569 /* Unrecognised OS-specific sections. */
2570 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2571 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2572 required to correctly process the section and the file should
ff15b240 2573 be rejected with an error message. */
4eca0228 2574 _bfd_error_handler
695344c0 2575 /* xgettext:c-format */
871b3ab2 2576 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2577 abfd, hdr->sh_type, name);
ff15b240 2578 else
bf67003b
NC
2579 {
2580 /* Otherwise it should be processed. */
2581 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2582 goto success;
2583 }
ff15b240 2584 }
3eb70a79
L
2585 else
2586 /* FIXME: We should handle this section. */
4eca0228 2587 _bfd_error_handler
695344c0 2588 /* xgettext:c-format */
871b3ab2 2589 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2590 abfd, hdr->sh_type, name);
3eb70a79 2591
bf67003b 2592 goto fail;
252b5132
RH
2593 }
2594
bf67003b 2595 fail:
0a1b45a2 2596 ret = false;
bf67003b 2597 success:
0a1b45a2 2598 elf_tdata (abfd)->being_created[shindex] = false;
bf67003b 2599 return ret;
252b5132
RH
2600}
2601
87d72d41 2602/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2603
87d72d41
AM
2604Elf_Internal_Sym *
2605bfd_sym_from_r_symndx (struct sym_cache *cache,
2606 bfd *abfd,
2607 unsigned long r_symndx)
ec338859 2608{
ec338859
AM
2609 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2610
a5d1b3b5
AM
2611 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2612 {
2613 Elf_Internal_Shdr *symtab_hdr;
2614 unsigned char esym[sizeof (Elf64_External_Sym)];
2615 Elf_External_Sym_Shndx eshndx;
ec338859 2616
a5d1b3b5
AM
2617 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2618 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2619 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2620 return NULL;
9ad5cbcf 2621
a5d1b3b5
AM
2622 if (cache->abfd != abfd)
2623 {
2624 memset (cache->indx, -1, sizeof (cache->indx));
2625 cache->abfd = abfd;
2626 }
2627 cache->indx[ent] = r_symndx;
ec338859 2628 }
a5d1b3b5 2629
87d72d41 2630 return &cache->sym[ent];
ec338859
AM
2631}
2632
252b5132
RH
2633/* Given an ELF section number, retrieve the corresponding BFD
2634 section. */
2635
2636asection *
91d6fa6a 2637bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2638{
91d6fa6a 2639 if (sec_index >= elf_numsections (abfd))
252b5132 2640 return NULL;
91d6fa6a 2641 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2642}
2643
b35d266b 2644static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2645{
0112cd26 2646 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2647 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2648};
2649
b35d266b 2650static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2651{
0112cd26 2652 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2653 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2654 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2655};
2656
b35d266b 2657static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2658{
07d6d2b8
AM
2659 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2660 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2661 /* There are more DWARF sections than these, but they needn't be added here
2662 unless you have to cope with broken compilers that don't emit section
2663 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2664 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2665 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2666 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2667 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2668 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2669 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2670 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2671 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2672 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2673};
2674
b35d266b 2675static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2676{
07d6d2b8 2677 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2678 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2679 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2680};
2681
b35d266b 2682static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2683{
0112cd26 2684 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2685 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2686 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2687 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2688 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2689 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2690 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2691 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2692 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2693 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2694 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2695 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2696};
2697
b35d266b 2698static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2699{
07d6d2b8
AM
2700 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2701 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2702};
2703
b35d266b 2704static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2705{
07d6d2b8 2706 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2707 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2708 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2709 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2710};
2711
b35d266b 2712static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2713{
0112cd26 2714 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2715 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2716};
2717
b35d266b 2718static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2719{
2c6f3e56 2720 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2721 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2722 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2723 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2724};
2725
b35d266b 2726static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2727{
2c6f3e56 2728 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2729 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2730 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2731 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2732};
2733
b35d266b 2734static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2735{
0112cd26
NC
2736 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2737 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2738 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2739 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2740 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2741};
2742
b35d266b 2743static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2744{
0112cd26
NC
2745 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2746 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2747 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2748 /* See struct bfd_elf_special_section declaration for the semantics of
2749 this special case where .prefix_length != strlen (.prefix). */
2750 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2751 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2752};
2753
b35d266b 2754static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2755{
07d6d2b8
AM
2756 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2757 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2758 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2759 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2760};
2761
1b315056
CS
2762static const struct bfd_elf_special_section special_sections_z[] =
2763{
07d6d2b8
AM
2764 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2765 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2766 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2767 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2768 { NULL, 0, 0, 0, 0 }
1b315056
CS
2769};
2770
e4c93b56 2771static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2772{
7f4d3958 2773 special_sections_b, /* 'b' */
98ece1b3 2774 special_sections_c, /* 'c' */
7f4d3958
L
2775 special_sections_d, /* 'd' */
2776 NULL, /* 'e' */
2777 special_sections_f, /* 'f' */
2778 special_sections_g, /* 'g' */
2779 special_sections_h, /* 'h' */
2780 special_sections_i, /* 'i' */
2781 NULL, /* 'j' */
2782 NULL, /* 'k' */
2783 special_sections_l, /* 'l' */
2784 NULL, /* 'm' */
2785 special_sections_n, /* 'n' */
2786 NULL, /* 'o' */
2787 special_sections_p, /* 'p' */
2788 NULL, /* 'q' */
2789 special_sections_r, /* 'r' */
2790 special_sections_s, /* 's' */
2791 special_sections_t, /* 't' */
1b315056
CS
2792 NULL, /* 'u' */
2793 NULL, /* 'v' */
2794 NULL, /* 'w' */
2795 NULL, /* 'x' */
2796 NULL, /* 'y' */
2797 special_sections_z /* 'z' */
7f4d3958
L
2798};
2799
551b43fd
AM
2800const struct bfd_elf_special_section *
2801_bfd_elf_get_special_section (const char *name,
2802 const struct bfd_elf_special_section *spec,
2803 unsigned int rela)
2f89ff8d
L
2804{
2805 int i;
7f4d3958 2806 int len;
7f4d3958 2807
551b43fd 2808 len = strlen (name);
7f4d3958 2809
551b43fd 2810 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2811 {
2812 int suffix_len;
551b43fd 2813 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2814
2815 if (len < prefix_len)
2816 continue;
551b43fd 2817 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2818 continue;
2819
551b43fd 2820 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2821 if (suffix_len <= 0)
2822 {
2823 if (name[prefix_len] != 0)
2824 {
2825 if (suffix_len == 0)
2826 continue;
2827 if (name[prefix_len] != '.'
2828 && (suffix_len == -2
551b43fd 2829 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2830 continue;
2831 }
2832 }
2833 else
2834 {
2835 if (len < prefix_len + suffix_len)
2836 continue;
2837 if (memcmp (name + len - suffix_len,
551b43fd 2838 spec[i].prefix + prefix_len,
7dcb9820
AM
2839 suffix_len) != 0)
2840 continue;
2841 }
551b43fd 2842 return &spec[i];
7dcb9820 2843 }
2f89ff8d
L
2844
2845 return NULL;
2846}
2847
7dcb9820 2848const struct bfd_elf_special_section *
29ef7005 2849_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2850{
551b43fd
AM
2851 int i;
2852 const struct bfd_elf_special_section *spec;
29ef7005 2853 const struct elf_backend_data *bed;
2f89ff8d
L
2854
2855 /* See if this is one of the special sections. */
551b43fd
AM
2856 if (sec->name == NULL)
2857 return NULL;
2f89ff8d 2858
29ef7005
L
2859 bed = get_elf_backend_data (abfd);
2860 spec = bed->special_sections;
2861 if (spec)
2862 {
2863 spec = _bfd_elf_get_special_section (sec->name,
2864 bed->special_sections,
2865 sec->use_rela_p);
2866 if (spec != NULL)
2867 return spec;
2868 }
2869
551b43fd
AM
2870 if (sec->name[0] != '.')
2871 return NULL;
2f89ff8d 2872
551b43fd 2873 i = sec->name[1] - 'b';
1b315056 2874 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2875 return NULL;
2876
2877 spec = special_sections[i];
2f89ff8d 2878
551b43fd
AM
2879 if (spec == NULL)
2880 return NULL;
2881
2882 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2883}
2884
0a1b45a2 2885bool
217aa764 2886_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2887{
2888 struct bfd_elf_section_data *sdata;
551b43fd 2889 const struct elf_backend_data *bed;
7dcb9820 2890 const struct bfd_elf_special_section *ssect;
252b5132 2891
f0abc2a1
AM
2892 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2893 if (sdata == NULL)
2894 {
a50b1753 2895 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2896 sizeof (*sdata));
f0abc2a1 2897 if (sdata == NULL)
0a1b45a2 2898 return false;
217aa764 2899 sec->used_by_bfd = sdata;
f0abc2a1 2900 }
bf572ba0 2901
551b43fd
AM
2902 /* Indicate whether or not this section should use RELA relocations. */
2903 bed = get_elf_backend_data (abfd);
2904 sec->use_rela_p = bed->default_use_rela_p;
2905
8c803a2d
AM
2906 /* Set up ELF section type and flags for newly created sections, if
2907 there is an ABI mandated section. */
2908 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2909 if (ssect != NULL)
2f89ff8d 2910 {
8c803a2d
AM
2911 elf_section_type (sec) = ssect->type;
2912 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2913 }
2914
f592407e 2915 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2916}
2917
2918/* Create a new bfd section from an ELF program header.
2919
2920 Since program segments have no names, we generate a synthetic name
2921 of the form segment<NUM>, where NUM is generally the index in the
2922 program header table. For segments that are split (see below) we
2923 generate the names segment<NUM>a and segment<NUM>b.
2924
2925 Note that some program segments may have a file size that is different than
2926 (less than) the memory size. All this means is that at execution the
2927 system must allocate the amount of memory specified by the memory size,
2928 but only initialize it with the first "file size" bytes read from the
2929 file. This would occur for example, with program segments consisting
2930 of combined data+bss.
2931
2932 To handle the above situation, this routine generates TWO bfd sections
2933 for the single program segment. The first has the length specified by
2934 the file size of the segment, and the second has the length specified
2935 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2936 into its initialized and uninitialized parts.
252b5132
RH
2937
2938 */
2939
0a1b45a2 2940bool
217aa764
AM
2941_bfd_elf_make_section_from_phdr (bfd *abfd,
2942 Elf_Internal_Phdr *hdr,
91d6fa6a 2943 int hdr_index,
a50b1753 2944 const char *type_name)
252b5132
RH
2945{
2946 asection *newsect;
2947 char *name;
2948 char namebuf[64];
d4c88bbb 2949 size_t len;
252b5132 2950 int split;
502794d4 2951 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2952
2953 split = ((hdr->p_memsz > 0)
2954 && (hdr->p_filesz > 0)
2955 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2956
2957 if (hdr->p_filesz > 0)
252b5132 2958 {
91d6fa6a 2959 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2960 len = strlen (namebuf) + 1;
a50b1753 2961 name = (char *) bfd_alloc (abfd, len);
d5191d0c 2962 if (!name)
0a1b45a2 2963 return false;
d5191d0c
AM
2964 memcpy (name, namebuf, len);
2965 newsect = bfd_make_section (abfd, name);
2966 if (newsect == NULL)
0a1b45a2 2967 return false;
502794d4
CE
2968 newsect->vma = hdr->p_vaddr / opb;
2969 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2970 newsect->size = hdr->p_filesz;
2971 newsect->filepos = hdr->p_offset;
2972 newsect->flags |= SEC_HAS_CONTENTS;
2973 newsect->alignment_power = bfd_log2 (hdr->p_align);
2974 if (hdr->p_type == PT_LOAD)
252b5132 2975 {
d5191d0c
AM
2976 newsect->flags |= SEC_ALLOC;
2977 newsect->flags |= SEC_LOAD;
2978 if (hdr->p_flags & PF_X)
2979 {
2980 /* FIXME: all we known is that it has execute PERMISSION,
2981 may be data. */
2982 newsect->flags |= SEC_CODE;
2983 }
2984 }
2985 if (!(hdr->p_flags & PF_W))
2986 {
2987 newsect->flags |= SEC_READONLY;
252b5132 2988 }
252b5132
RH
2989 }
2990
d5191d0c 2991 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2992 {
d5191d0c
AM
2993 bfd_vma align;
2994
91d6fa6a 2995 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2996 len = strlen (namebuf) + 1;
a50b1753 2997 name = (char *) bfd_alloc (abfd, len);
252b5132 2998 if (!name)
0a1b45a2 2999 return false;
d4c88bbb 3000 memcpy (name, namebuf, len);
252b5132
RH
3001 newsect = bfd_make_section (abfd, name);
3002 if (newsect == NULL)
0a1b45a2 3003 return false;
502794d4
CE
3004 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3005 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 3006 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3007 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3008 align = newsect->vma & -newsect->vma;
3009 if (align == 0 || align > hdr->p_align)
3010 align = hdr->p_align;
3011 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3012 if (hdr->p_type == PT_LOAD)
3013 {
3014 newsect->flags |= SEC_ALLOC;
3015 if (hdr->p_flags & PF_X)
3016 newsect->flags |= SEC_CODE;
3017 }
3018 if (!(hdr->p_flags & PF_W))
3019 newsect->flags |= SEC_READONLY;
3020 }
3021
0a1b45a2 3022 return true;
252b5132
RH
3023}
3024
0a1b45a2 3025static bool
864619bb
KS
3026_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3027{
3028 /* The return value is ignored. Build-ids are considered optional. */
3029 if (templ->xvec->flavour == bfd_target_elf_flavour)
3030 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3031 (templ, offset);
0a1b45a2 3032 return false;
864619bb
KS
3033}
3034
0a1b45a2 3035bool
91d6fa6a 3036bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3037{
9c5bfbb7 3038 const struct elf_backend_data *bed;
20cfcaae
NC
3039
3040 switch (hdr->p_type)
3041 {
3042 case PT_NULL:
91d6fa6a 3043 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3044
3045 case PT_LOAD:
864619bb 3046 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
0a1b45a2 3047 return false;
864619bb
KS
3048 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3049 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
0a1b45a2 3050 return true;
20cfcaae
NC
3051
3052 case PT_DYNAMIC:
91d6fa6a 3053 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3054
3055 case PT_INTERP:
91d6fa6a 3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3057
3058 case PT_NOTE:
91d6fa6a 3059 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
0a1b45a2 3060 return false;
276da9b3
L
3061 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3062 hdr->p_align))
0a1b45a2
AM
3063 return false;
3064 return true;
20cfcaae
NC
3065
3066 case PT_SHLIB:
91d6fa6a 3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3068
3069 case PT_PHDR:
91d6fa6a 3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3071
811072d8 3072 case PT_GNU_EH_FRAME:
91d6fa6a 3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3074 "eh_frame_hdr");
3075
2b05f1b7 3076 case PT_GNU_STACK:
91d6fa6a 3077 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3078
8c37241b 3079 case PT_GNU_RELRO:
91d6fa6a 3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3081
20cfcaae 3082 default:
8c1acd09 3083 /* Check for any processor-specific program segment types. */
20cfcaae 3084 bed = get_elf_backend_data (abfd);
91d6fa6a 3085 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3086 }
3087}
3088
d4730f92
BS
3089/* Return the REL_HDR for SEC, assuming there is only a single one, either
3090 REL or RELA. */
3091
3092Elf_Internal_Shdr *
3093_bfd_elf_single_rel_hdr (asection *sec)
3094{
3095 if (elf_section_data (sec)->rel.hdr)
3096 {
3097 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3098 return elf_section_data (sec)->rel.hdr;
3099 }
3100 else
3101 return elf_section_data (sec)->rela.hdr;
3102}
3103
0a1b45a2 3104static bool
3e19fb8f
L
3105_bfd_elf_set_reloc_sh_name (bfd *abfd,
3106 Elf_Internal_Shdr *rel_hdr,
3107 const char *sec_name,
0a1b45a2 3108 bool use_rela_p)
3e19fb8f
L
3109{
3110 char *name = (char *) bfd_alloc (abfd,
3111 sizeof ".rela" + strlen (sec_name));
3112 if (name == NULL)
0a1b45a2 3113 return false;
3e19fb8f
L
3114
3115 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3116 rel_hdr->sh_name =
3117 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
0a1b45a2 3118 false);
3e19fb8f 3119 if (rel_hdr->sh_name == (unsigned int) -1)
0a1b45a2 3120 return false;
3e19fb8f 3121
0a1b45a2 3122 return true;
3e19fb8f
L
3123}
3124
d4730f92
BS
3125/* Allocate and initialize a section-header for a new reloc section,
3126 containing relocations against ASECT. It is stored in RELDATA. If
3127 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3128 relocations. */
23bc299b 3129
0a1b45a2 3130static bool
217aa764 3131_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3132 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3133 const char *sec_name,
0a1b45a2
AM
3134 bool use_rela_p,
3135 bool delay_st_name_p)
23bc299b 3136{
d4730f92 3137 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3139
d4730f92 3140 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3141 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3142 reldata->hdr = rel_hdr;
23bc299b 3143
3e19fb8f
L
3144 if (delay_st_name_p)
3145 rel_hdr->sh_name = (unsigned int) -1;
3146 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3147 use_rela_p))
0a1b45a2 3148 return false;
23bc299b
MM
3149 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3150 rel_hdr->sh_entsize = (use_rela_p
3151 ? bed->s->sizeof_rela
3152 : bed->s->sizeof_rel);
72de5009 3153 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3154 rel_hdr->sh_flags = 0;
23bc299b
MM
3155 rel_hdr->sh_addr = 0;
3156 rel_hdr->sh_size = 0;
3157 rel_hdr->sh_offset = 0;
3158
0a1b45a2 3159 return true;
23bc299b
MM
3160}
3161
94be91de
JB
3162/* Return the default section type based on the passed in section flags. */
3163
3164int
3165bfd_elf_get_default_section_type (flagword flags)
3166{
0e41bebb 3167 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3168 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3169 return SHT_NOBITS;
3170 return SHT_PROGBITS;
3171}
3172
d4730f92
BS
3173struct fake_section_arg
3174{
3175 struct bfd_link_info *link_info;
0a1b45a2 3176 bool failed;
d4730f92
BS
3177};
3178
252b5132
RH
3179/* Set up an ELF internal section header for a section. */
3180
252b5132 3181static void
d4730f92 3182elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3183{
d4730f92 3184 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3186 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3187 Elf_Internal_Shdr *this_hdr;
0414f35b 3188 unsigned int sh_type;
0ce398f1 3189 const char *name = asect->name;
0a1b45a2 3190 bool delay_st_name_p = false;
233bf4f8 3191 bfd_vma mask;
252b5132 3192
d4730f92 3193 if (arg->failed)
252b5132
RH
3194 {
3195 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3196 loop. */
252b5132
RH
3197 return;
3198 }
3199
d4730f92 3200 this_hdr = &esd->this_hdr;
252b5132 3201
f6fe1ccd 3202 if (arg->link_info)
0ce398f1 3203 {
f6fe1ccd
L
3204 /* ld: compress DWARF debug sections with names: .debug_*. */
3205 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3206 && (asect->flags & SEC_DEBUGGING)
3207 && name[1] == 'd'
3208 && name[6] == '_')
3209 {
3210 /* Set SEC_ELF_COMPRESS to indicate this section should be
3211 compressed. */
3212 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3213 /* If this section will be compressed, delay adding section
3e19fb8f
L
3214 name to section name section after it is compressed in
3215 _bfd_elf_assign_file_positions_for_non_load. */
0a1b45a2 3216 delay_st_name_p = true;
f6fe1ccd
L
3217 }
3218 }
3219 else if ((asect->flags & SEC_ELF_RENAME))
3220 {
3221 /* objcopy: rename output DWARF debug section. */
3222 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3223 {
3224 /* When we decompress or compress with SHF_COMPRESSED,
3225 convert section name from .zdebug_* to .debug_* if
3226 needed. */
3227 if (name[1] == 'z')
3228 {
3229 char *new_name = convert_zdebug_to_debug (abfd, name);
3230 if (new_name == NULL)
3231 {
0a1b45a2 3232 arg->failed = true;
f6fe1ccd
L
3233 return;
3234 }
3235 name = new_name;
3236 }
3237 }
3238 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3239 {
f6fe1ccd
L
3240 /* PR binutils/18087: Compression does not always make a
3241 section smaller. So only rename the section when
3242 compression has actually taken place. If input section
3243 name is .zdebug_*, we should never compress it again. */
3244 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3245 if (new_name == NULL)
3246 {
0a1b45a2 3247 arg->failed = true;
0ce398f1
L
3248 return;
3249 }
f6fe1ccd
L
3250 BFD_ASSERT (name[1] != 'z');
3251 name = new_name;
0ce398f1
L
3252 }
3253 }
3254
3e19fb8f
L
3255 if (delay_st_name_p)
3256 this_hdr->sh_name = (unsigned int) -1;
3257 else
252b5132 3258 {
3e19fb8f
L
3259 this_hdr->sh_name
3260 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3261 name, false);
3e19fb8f
L
3262 if (this_hdr->sh_name == (unsigned int) -1)
3263 {
0a1b45a2 3264 arg->failed = true;
3e19fb8f
L
3265 return;
3266 }
252b5132
RH
3267 }
3268
a4d8e49b 3269 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3270
3271 if ((asect->flags & SEC_ALLOC) != 0
3272 || asect->user_set_vma)
502794d4 3273 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3274 else
3275 this_hdr->sh_addr = 0;
3276
3277 this_hdr->sh_offset = 0;
eea6121a 3278 this_hdr->sh_size = asect->size;
252b5132 3279 this_hdr->sh_link = 0;
c86934ce
NC
3280 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3281 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3282 {
4eca0228 3283 _bfd_error_handler
695344c0 3284 /* xgettext:c-format */
9793eb77 3285 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3286 abfd, asect->alignment_power, asect);
0a1b45a2 3287 arg->failed = true;
c86934ce
NC
3288 return;
3289 }
233bf4f8
AM
3290 /* Set sh_addralign to the highest power of two given by alignment
3291 consistent with the section VMA. Linker scripts can force VMA. */
3292 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3293 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3294 /* The sh_entsize and sh_info fields may have been set already by
3295 copy_private_section_data. */
3296
3297 this_hdr->bfd_section = asect;
3298 this_hdr->contents = NULL;
3299
3cddba1e
L
3300 /* If the section type is unspecified, we set it based on
3301 asect->flags. */
98ece1b3
AM
3302 if ((asect->flags & SEC_GROUP) != 0)
3303 sh_type = SHT_GROUP;
98ece1b3 3304 else
94be91de 3305 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3306
3cddba1e 3307 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3308 this_hdr->sh_type = sh_type;
3309 else if (this_hdr->sh_type == SHT_NOBITS
3310 && sh_type == SHT_PROGBITS
3311 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3312 {
98ece1b3
AM
3313 /* Warn if we are changing a NOBITS section to PROGBITS, but
3314 allow the link to proceed. This can happen when users link
3315 non-bss input sections to bss output sections, or emit data
3316 to a bss output section via a linker script. */
4eca0228 3317 _bfd_error_handler
871b3ab2 3318 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3319 this_hdr->sh_type = sh_type;
3cddba1e
L
3320 }
3321
2f89ff8d 3322 switch (this_hdr->sh_type)
252b5132 3323 {
2f89ff8d 3324 default:
2f89ff8d
L
3325 break;
3326
3327 case SHT_STRTAB:
2f89ff8d
L
3328 case SHT_NOTE:
3329 case SHT_NOBITS:
3330 case SHT_PROGBITS:
3331 break;
606851fb
AM
3332
3333 case SHT_INIT_ARRAY:
3334 case SHT_FINI_ARRAY:
3335 case SHT_PREINIT_ARRAY:
3336 this_hdr->sh_entsize = bed->s->arch_size / 8;
3337 break;
2f89ff8d
L
3338
3339 case SHT_HASH:
c7ac6ff8 3340 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3341 break;
5de3bf90 3342
2f89ff8d 3343 case SHT_DYNSYM:
252b5132 3344 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3345 break;
3346
3347 case SHT_DYNAMIC:
252b5132 3348 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3349 break;
3350
3351 case SHT_RELA:
3352 if (get_elf_backend_data (abfd)->may_use_rela_p)
3353 this_hdr->sh_entsize = bed->s->sizeof_rela;
3354 break;
3355
3356 case SHT_REL:
3357 if (get_elf_backend_data (abfd)->may_use_rel_p)
3358 this_hdr->sh_entsize = bed->s->sizeof_rel;
3359 break;
3360
3361 case SHT_GNU_versym:
252b5132 3362 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3363 break;
3364
3365 case SHT_GNU_verdef:
252b5132
RH
3366 this_hdr->sh_entsize = 0;
3367 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3368 cverdefs. The linker will set cverdefs, but sh_info will be
3369 zero. */
252b5132
RH
3370 if (this_hdr->sh_info == 0)
3371 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3372 else
3373 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3374 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3375 break;
3376
3377 case SHT_GNU_verneed:
252b5132
RH
3378 this_hdr->sh_entsize = 0;
3379 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3380 cverrefs. The linker will set cverrefs, but sh_info will be
3381 zero. */
252b5132
RH
3382 if (this_hdr->sh_info == 0)
3383 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3384 else
3385 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3386 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3387 break;
3388
3389 case SHT_GROUP:
1783205a 3390 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3391 break;
fdc90cb4
JJ
3392
3393 case SHT_GNU_HASH:
3394 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3395 break;
dbb410c3 3396 }
252b5132
RH
3397
3398 if ((asect->flags & SEC_ALLOC) != 0)
3399 this_hdr->sh_flags |= SHF_ALLOC;
3400 if ((asect->flags & SEC_READONLY) == 0)
3401 this_hdr->sh_flags |= SHF_WRITE;
3402 if ((asect->flags & SEC_CODE) != 0)
3403 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3404 if ((asect->flags & SEC_MERGE) != 0)
3405 {
3406 this_hdr->sh_flags |= SHF_MERGE;
3407 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3408 }
84865015
NC
3409 if ((asect->flags & SEC_STRINGS) != 0)
3410 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3411 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3412 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3413 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3414 {
3415 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3416 if (asect->size == 0
3417 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3418 {
3a800eb9 3419 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3420
704afa60 3421 this_hdr->sh_size = 0;
3a800eb9
AM
3422 if (o != NULL)
3423 {
704afa60 3424 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3425 if (this_hdr->sh_size != 0)
3426 this_hdr->sh_type = SHT_NOBITS;
3427 }
704afa60
JJ
3428 }
3429 }
18ae9cc1
L
3430 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3431 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3432
d4730f92
BS
3433 /* If the section has relocs, set up a section header for the
3434 SHT_REL[A] section. If two relocation sections are required for
3435 this section, it is up to the processor-specific back-end to
3436 create the other. */
3437 if ((asect->flags & SEC_RELOC) != 0)
3438 {
3439 /* When doing a relocatable link, create both REL and RELA sections if
3440 needed. */
3441 if (arg->link_info
3442 /* Do the normal setup if we wouldn't create any sections here. */
3443 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3444 && (bfd_link_relocatable (arg->link_info)
3445 || arg->link_info->emitrelocations))
d4730f92
BS
3446 {
3447 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3448 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
0a1b45a2 3449 false, delay_st_name_p))
d4730f92 3450 {
0a1b45a2 3451 arg->failed = true;
d4730f92
BS
3452 return;
3453 }
3454 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3455 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
0a1b45a2 3456 true, delay_st_name_p))
d4730f92 3457 {
0a1b45a2 3458 arg->failed = true;
d4730f92
BS
3459 return;
3460 }
3461 }
3462 else if (!_bfd_elf_init_reloc_shdr (abfd,
3463 (asect->use_rela_p
3464 ? &esd->rela : &esd->rel),
f6fe1ccd 3465 name,
3e19fb8f
L
3466 asect->use_rela_p,
3467 delay_st_name_p))
db4677b8 3468 {
0a1b45a2 3469 arg->failed = true;
db4677b8
AM
3470 return;
3471 }
d4730f92
BS
3472 }
3473
252b5132 3474 /* Check for processor-specific section types. */
0414f35b 3475 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3476 if (bed->elf_backend_fake_sections
3477 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8 3478 {
0a1b45a2 3479 arg->failed = true;
db4677b8
AM
3480 return;
3481 }
252b5132 3482
42bb2e33 3483 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3484 {
3485 /* Don't change the header type from NOBITS if we are being
42bb2e33 3486 called for objcopy --only-keep-debug. */
0414f35b
AM
3487 this_hdr->sh_type = sh_type;
3488 }
252b5132
RH
3489}
3490
bcacc0f5
AM
3491/* Fill in the contents of a SHT_GROUP section. Called from
3492 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3493 when ELF targets use the generic linker, ld. Called for ld -r
3494 from bfd_elf_final_link. */
dbb410c3 3495
1126897b 3496void
217aa764 3497bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3498{
0a1b45a2 3499 bool *failedptr = (bool *) failedptrarg;
9dce4196 3500 asection *elt, *first;
dbb410c3 3501 unsigned char *loc;
0a1b45a2 3502 bool gas;
dbb410c3 3503
7e4111ad
L
3504 /* Ignore linker created group section. See elfNN_ia64_object_p in
3505 elfxx-ia64.c. */
ce5aecf8
AM
3506 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3507 || sec->size == 0
dbb410c3
AM
3508 || *failedptr)
3509 return;
3510
bcacc0f5
AM
3511 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3512 {
3513 unsigned long symindx = 0;
3514
3515 /* elf_group_id will have been set up by objcopy and the
3516 generic linker. */
3517 if (elf_group_id (sec) != NULL)
3518 symindx = elf_group_id (sec)->udata.i;
1126897b 3519
bcacc0f5
AM
3520 if (symindx == 0)
3521 {
3522 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3523 elf_section_syms.
3524 PR 25699: A corrupt input file could contain bogus group info. */
3525 if (elf_section_syms (abfd) == NULL)
3526 {
0a1b45a2 3527 *failedptr = true;
6a541707
NC
3528 return;
3529 }
bcacc0f5
AM
3530 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3531 }
3532 elf_section_data (sec)->this_hdr.sh_info = symindx;
3533 }
3534 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3535 {
bcacc0f5
AM
3536 /* The ELF backend linker sets sh_info to -2 when the group
3537 signature symbol is global, and thus the index can't be
3538 set until all local symbols are output. */
53720c49
AM
3539 asection *igroup;
3540 struct bfd_elf_section_data *sec_data;
3541 unsigned long symndx;
3542 unsigned long extsymoff;
bcacc0f5
AM
3543 struct elf_link_hash_entry *h;
3544
53720c49
AM
3545 /* The point of this little dance to the first SHF_GROUP section
3546 then back to the SHT_GROUP section is that this gets us to
3547 the SHT_GROUP in the input object. */
3548 igroup = elf_sec_group (elf_next_in_group (sec));
3549 sec_data = elf_section_data (igroup);
3550 symndx = sec_data->this_hdr.sh_info;
3551 extsymoff = 0;
bcacc0f5
AM
3552 if (!elf_bad_symtab (igroup->owner))
3553 {
3554 Elf_Internal_Shdr *symtab_hdr;
3555
3556 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3557 extsymoff = symtab_hdr->sh_info;
3558 }
3559 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3560 while (h->root.type == bfd_link_hash_indirect
3561 || h->root.type == bfd_link_hash_warning)
3562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3563
3564 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3565 }
dbb410c3 3566
1126897b 3567 /* The contents won't be allocated for "ld -r" or objcopy. */
0a1b45a2 3568 gas = true;
dbb410c3
AM
3569 if (sec->contents == NULL)
3570 {
0a1b45a2 3571 gas = false;
a50b1753 3572 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3573
3574 /* Arrange for the section to be written out. */
3575 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3576 if (sec->contents == NULL)
3577 {
0a1b45a2 3578 *failedptr = true;
dbb410c3
AM
3579 return;
3580 }
3581 }
3582
eea6121a 3583 loc = sec->contents + sec->size;
dbb410c3 3584
9dce4196
AM
3585 /* Get the pointer to the first section in the group that gas
3586 squirreled away here. objcopy arranges for this to be set to the
3587 start of the input section group. */
3588 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3589
3590 /* First element is a flag word. Rest of section is elf section
3591 indices for all the sections of the group. Write them backwards
3592 just to keep the group in the same order as given in .section
3593 directives, not that it matters. */
3594 while (elt != NULL)
3595 {
9dce4196 3596 asection *s;
9dce4196 3597
9dce4196 3598 s = elt;
415f38a6
AM
3599 if (!gas)
3600 s = s->output_section;
3601 if (s != NULL
3602 && !bfd_is_abs_section (s))
01e1a5bc 3603 {
db4677b8 3604 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3605 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3606
3607 if (elf_sec->rel.hdr != NULL
3608 && (gas
3609 || (input_elf_sec->rel.hdr != NULL
3610 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3611 {
28e07a05 3612 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3613 loc -= 4;
3614 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3615 }
28e07a05
AM
3616 if (elf_sec->rela.hdr != NULL
3617 && (gas
3618 || (input_elf_sec->rela.hdr != NULL
3619 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3620 {
28e07a05 3621 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3622 loc -= 4;
3623 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3624 }
01e1a5bc 3625 loc -= 4;
db4677b8 3626 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3627 }
945906ff 3628 elt = elf_next_in_group (elt);
9dce4196
AM
3629 if (elt == first)
3630 break;
dbb410c3
AM
3631 }
3632
7bdf4127
AB
3633 loc -= 4;
3634 BFD_ASSERT (loc == sec->contents);
dbb410c3 3635
9dce4196 3636 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3637}
3638
bce964aa
AM
3639/* Given NAME, the name of a relocation section stripped of its
3640 .rel/.rela prefix, return the section in ABFD to which the
3641 relocations apply. */
bd53a53a
L
3642
3643asection *
bce964aa
AM
3644_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3645{
3646 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3647 section likely apply to .got.plt or .got section. */
3648 if (get_elf_backend_data (abfd)->want_got_plt
3649 && strcmp (name, ".plt") == 0)
3650 {
3651 asection *sec;
3652
3653 name = ".got.plt";
3654 sec = bfd_get_section_by_name (abfd, name);
3655 if (sec != NULL)
3656 return sec;
3657 name = ".got";
3658 }
3659
3660 return bfd_get_section_by_name (abfd, name);
3661}
3662
3663/* Return the section to which RELOC_SEC applies. */
3664
3665static asection *
3666elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3667{
3668 const char *name;
3669 unsigned int type;
3670 bfd *abfd;
bce964aa 3671 const struct elf_backend_data *bed;
bd53a53a
L
3672
3673 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3674 if (type != SHT_REL && type != SHT_RELA)
3675 return NULL;
3676
3677 /* We look up the section the relocs apply to by name. */
3678 name = reloc_sec->name;
3f3328b8 3679 if (!startswith (name, ".rel"))
bce964aa
AM
3680 return NULL;
3681 name += 4;
3682 if (type == SHT_RELA && *name++ != 'a')
3683 return NULL;
bd53a53a 3684
bd53a53a 3685 abfd = reloc_sec->owner;
bce964aa
AM
3686 bed = get_elf_backend_data (abfd);
3687 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3688}
3689
252b5132
RH
3690/* Assign all ELF section numbers. The dummy first section is handled here
3691 too. The link/info pointers for the standard section types are filled
67411cbf
AM
3692 in here too, while we're at it. LINK_INFO will be 0 when arriving
3693 here for objcopy, and when using the generic ELF linker. */
252b5132 3694
0a1b45a2 3695static bool
da9f89d4 3696assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3697{
3698 struct elf_obj_tdata *t = elf_tdata (abfd);
3699 asection *sec;
3e19fb8f 3700 unsigned int section_number;
252b5132 3701 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3702 struct bfd_elf_section_data *d;
0a1b45a2 3703 bool need_symtab;
446f7ed5 3704 size_t amt;
252b5132
RH
3705
3706 section_number = 1;
3707
2b0f7ef9
JJ
3708 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3709
da9f89d4 3710 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3711 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3712 {
ef53be89 3713 size_t reloc_count = 0;
14f2c699 3714
da9f89d4 3715 /* Put SHT_GROUP sections first. */
04dd1667 3716 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3717 {
5daa8fe7 3718 d = elf_section_data (sec);
da9f89d4
L
3719
3720 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3721 {
5daa8fe7 3722 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3723 {
3724 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3725 bfd_section_list_remove (abfd, sec);
da9f89d4 3726 abfd->section_count--;
da9f89d4 3727 }
08a40648 3728 else
4fbb74a6 3729 d->this_idx = section_number++;
da9f89d4 3730 }
14f2c699
L
3731
3732 /* Count relocations. */
3733 reloc_count += sec->reloc_count;
47cc2cf5 3734 }
14f2c699
L
3735
3736 /* Clear HAS_RELOC if there are no relocations. */
3737 if (reloc_count == 0)
3738 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3739 }
3740
3741 for (sec = abfd->sections; sec; sec = sec->next)
3742 {
3743 d = elf_section_data (sec);
3744
3745 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3746 d->this_idx = section_number++;
3e19fb8f
L
3747 if (d->this_hdr.sh_name != (unsigned int) -1)
3748 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3749 if (d->rel.hdr)
2b0f7ef9 3750 {
d4730f92 3751 d->rel.idx = section_number++;
3e19fb8f
L
3752 if (d->rel.hdr->sh_name != (unsigned int) -1)
3753 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3754 }
d4730f92
BS
3755 else
3756 d->rel.idx = 0;
23bc299b 3757
d4730f92 3758 if (d->rela.hdr)
2b0f7ef9 3759 {
d4730f92 3760 d->rela.idx = section_number++;
3e19fb8f
L
3761 if (d->rela.hdr->sh_name != (unsigned int) -1)
3762 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3763 }
23bc299b 3764 else
d4730f92 3765 d->rela.idx = 0;
252b5132
RH
3766 }
3767
3516e984
L
3768 need_symtab = (bfd_get_symcount (abfd) > 0
3769 || (link_info == NULL
3770 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3771 == HAS_RELOC)));
3772 if (need_symtab)
252b5132 3773 {
12bd6957 3774 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3775 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3776 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3777 {
7a6e0d89 3778 elf_section_list *entry;
6a40cf0c
NC
3779
3780 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3781
7a6e0d89 3782 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3783 entry->ndx = section_number++;
3784 elf_symtab_shndx_list (abfd) = entry;
3785 entry->hdr.sh_name
9ad5cbcf 3786 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3787 ".symtab_shndx", false);
6a40cf0c 3788 if (entry->hdr.sh_name == (unsigned int) -1)
0a1b45a2 3789 return false;
9ad5cbcf 3790 }
12bd6957 3791 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3792 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3793 }
3794
dd905818
NC
3795 elf_shstrtab_sec (abfd) = section_number++;
3796 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3797 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3798
1c52a645
L
3799 if (section_number >= SHN_LORESERVE)
3800 {
695344c0 3801 /* xgettext:c-format */
871b3ab2 3802 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645 3803 abfd, section_number);
0a1b45a2 3804 return false;
1c52a645
L
3805 }
3806
9ad5cbcf 3807 elf_numsections (abfd) = section_number;
252b5132
RH
3808 elf_elfheader (abfd)->e_shnum = section_number;
3809
3810 /* Set up the list of section header pointers, in agreement with the
3811 indices. */
446f7ed5
AM
3812 amt = section_number * sizeof (Elf_Internal_Shdr *);
3813 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3814 if (i_shdrp == NULL)
0a1b45a2 3815 return false;
252b5132 3816
a50b1753 3817 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3818 sizeof (Elf_Internal_Shdr));
252b5132
RH
3819 if (i_shdrp[0] == NULL)
3820 {
3821 bfd_release (abfd, i_shdrp);
0a1b45a2 3822 return false;
252b5132 3823 }
252b5132
RH
3824
3825 elf_elfsections (abfd) = i_shdrp;
3826
12bd6957 3827 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3828 if (need_symtab)
252b5132 3829 {
12bd6957 3830 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3831 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3832 {
6a40cf0c
NC
3833 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3834 BFD_ASSERT (entry != NULL);
3835 i_shdrp[entry->ndx] = & entry->hdr;
3836 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3837 }
12bd6957
AM
3838 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3839 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3840 }
38ce5b11 3841
252b5132
RH
3842 for (sec = abfd->sections; sec; sec = sec->next)
3843 {
252b5132 3844 asection *s;
252b5132 3845
91d6fa6a
NC
3846 d = elf_section_data (sec);
3847
252b5132 3848 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3849 if (d->rel.idx != 0)
3850 i_shdrp[d->rel.idx] = d->rel.hdr;
3851 if (d->rela.idx != 0)
3852 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3853
3854 /* Fill in the sh_link and sh_info fields while we're at it. */
3855
3856 /* sh_link of a reloc section is the section index of the symbol
3857 table. sh_info is the section index of the section to which
3858 the relocation entries apply. */
d4730f92 3859 if (d->rel.idx != 0)
252b5132 3860 {
12bd6957 3861 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3862 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3863 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3864 }
d4730f92 3865 if (d->rela.idx != 0)
23bc299b 3866 {
12bd6957 3867 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3868 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3869 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3870 }
252b5132 3871
38ce5b11
L
3872 /* We need to set up sh_link for SHF_LINK_ORDER. */
3873 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3874 {
3875 s = elf_linked_to_section (sec);
b71702f1
NC
3876 /* We can now have a NULL linked section pointer.
3877 This happens when the sh_link field is 0, which is done
3878 when a linked to section is discarded but the linking
3879 section has been retained for some reason. */
38ce5b11 3880 if (s)
38ce5b11 3881 {
67411cbf
AM
3882 /* Check discarded linkonce section. */
3883 if (discarded_section (s))
38ce5b11 3884 {
67411cbf
AM
3885 asection *kept;
3886 _bfd_error_handler
3887 /* xgettext:c-format */
3888 (_("%pB: sh_link of section `%pA' points to"
3889 " discarded section `%pA' of `%pB'"),
3890 abfd, d->this_hdr.bfd_section, s, s->owner);
3891 /* Point to the kept section if it has the same
3892 size as the discarded one. */
3893 kept = _bfd_elf_check_kept_section (s, link_info);
3894 if (kept == NULL)
f2876037 3895 {
f2876037 3896 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3897 return false;
f2876037 3898 }
67411cbf
AM
3899 s = kept;
3900 }
3901 /* Handle objcopy. */
3902 else if (s->output_section == NULL)
3903 {
3904 _bfd_error_handler
3905 /* xgettext:c-format */
3906 (_("%pB: sh_link of section `%pA' points to"
3907 " removed section `%pA' of `%pB'"),
3908 abfd, d->this_hdr.bfd_section, s, s->owner);
3909 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3910 return false;
f2876037 3911 }
67411cbf 3912 s = s->output_section;
ccd2ec6a
L
3913 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3914 }
38ce5b11
L
3915 }
3916
252b5132
RH
3917 switch (d->this_hdr.sh_type)
3918 {
3919 case SHT_REL:
3920 case SHT_RELA:
3921 /* A reloc section which we are treating as a normal BFD
3922 section. sh_link is the section index of the symbol
3923 table. sh_info is the section index of the section to
3924 which the relocation entries apply. We assume that an
3925 allocated reloc section uses the dynamic symbol table.
3926 FIXME: How can we be sure? */
3927 s = bfd_get_section_by_name (abfd, ".dynsym");
3928 if (s != NULL)
3929 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3930
bce964aa 3931 s = elf_get_reloc_section (sec);
252b5132 3932 if (s != NULL)
9ef5d938
L
3933 {
3934 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3935 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3936 }
252b5132
RH
3937 break;
3938
3939 case SHT_STRTAB:
3940 /* We assume that a section named .stab*str is a stabs
3941 string section. We look for a section with the same name
3942 but without the trailing ``str'', and set its sh_link
3943 field to point to this section. */
08dedd66 3944 if (startswith (sec->name, ".stab")
252b5132
RH
3945 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3946 {
3947 size_t len;
3948 char *alc;
3949
3950 len = strlen (sec->name);
a50b1753 3951 alc = (char *) bfd_malloc (len - 2);
252b5132 3952 if (alc == NULL)
0a1b45a2 3953 return false;
d4c88bbb 3954 memcpy (alc, sec->name, len - 3);
252b5132
RH
3955 alc[len - 3] = '\0';
3956 s = bfd_get_section_by_name (abfd, alc);
3957 free (alc);
3958 if (s != NULL)
3959 {
3960 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3961
3962 /* This is a .stab section. */
34ca5531 3963 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3964 }
3965 }
3966 break;
3967
3968 case SHT_DYNAMIC:
3969 case SHT_DYNSYM:
3970 case SHT_GNU_verneed:
3971 case SHT_GNU_verdef:
3972 /* sh_link is the section header index of the string table
3973 used for the dynamic entries, or the symbol table, or the
3974 version strings. */
3975 s = bfd_get_section_by_name (abfd, ".dynstr");
3976 if (s != NULL)
3977 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3978 break;
3979
7f1204bb
JJ
3980 case SHT_GNU_LIBLIST:
3981 /* sh_link is the section header index of the prelink library
08a40648
AM
3982 list used for the dynamic entries, or the symbol table, or
3983 the version strings. */
7f1204bb
JJ
3984 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3985 ? ".dynstr" : ".gnu.libstr");
3986 if (s != NULL)
3987 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3988 break;
3989
252b5132 3990 case SHT_HASH:
fdc90cb4 3991 case SHT_GNU_HASH:
252b5132
RH
3992 case SHT_GNU_versym:
3993 /* sh_link is the section header index of the symbol table
3994 this hash table or version table is for. */
3995 s = bfd_get_section_by_name (abfd, ".dynsym");
3996 if (s != NULL)
3997 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3998 break;
dbb410c3
AM
3999
4000 case SHT_GROUP:
12bd6957 4001 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4002 }
4003 }
4004
3e19fb8f
L
4005 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4006 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4007 debug section name from .debug_* to .zdebug_* if needed. */
4008
0a1b45a2 4009 return true;
252b5132
RH
4010}
4011
0a1b45a2 4012static bool
217aa764 4013sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4014{
4015 /* If the backend has a special mapping, use it. */
9c5bfbb7 4016 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4017 if (bed->elf_backend_sym_is_global)
4018 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4019
e47bf690 4020 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4021 || bfd_is_und_section (bfd_asymbol_section (sym))
4022 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4023}
4024
76359541
TP
4025/* Filter global symbols of ABFD to include in the import library. All
4026 SYMCOUNT symbols of ABFD can be examined from their pointers in
4027 SYMS. Pointers of symbols to keep should be stored contiguously at
4028 the beginning of that array.
4029
4030 Returns the number of symbols to keep. */
4031
4032unsigned int
4033_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4034 asymbol **syms, long symcount)
4035{
4036 long src_count, dst_count = 0;
4037
4038 for (src_count = 0; src_count < symcount; src_count++)
4039 {
4040 asymbol *sym = syms[src_count];
4041 char *name = (char *) bfd_asymbol_name (sym);
4042 struct bfd_link_hash_entry *h;
4043
4044 if (!sym_is_global (abfd, sym))
4045 continue;
4046
0a1b45a2 4047 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
5df1bc57
AM
4048 if (h == NULL)
4049 continue;
76359541
TP
4050 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4051 continue;
76359541
TP
4052 if (h->linker_def || h->ldscript_def)
4053 continue;
4054
4055 syms[dst_count++] = sym;
4056 }
4057
4058 syms[dst_count] = NULL;
4059
4060 return dst_count;
4061}
4062
5372391b 4063/* Don't output section symbols for sections that are not going to be
c6d8cab4 4064 output, that are duplicates or there is no BFD section. */
5372391b 4065
0a1b45a2 4066static bool
5372391b
AM
4067ignore_section_sym (bfd *abfd, asymbol *sym)
4068{
c6d8cab4
L
4069 elf_symbol_type *type_ptr;
4070
db0c309f 4071 if (sym == NULL)
0a1b45a2 4072 return false;
db0c309f 4073
c6d8cab4 4074 if ((sym->flags & BSF_SECTION_SYM) == 0)
0a1b45a2 4075 return false;
c6d8cab4 4076
d1bcae83
L
4077 /* Ignore the section symbol if it isn't used. */
4078 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
0a1b45a2 4079 return true;
d1bcae83 4080
db0c309f 4081 if (sym->section == NULL)
0a1b45a2 4082 return true;
db0c309f 4083
c1229f84 4084 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4085 return ((type_ptr != NULL
4086 && type_ptr->internal_elf_sym.st_shndx != 0
4087 && bfd_is_abs_section (sym->section))
4088 || !(sym->section->owner == abfd
db0c309f
NC
4089 || (sym->section->output_section != NULL
4090 && sym->section->output_section->owner == abfd
2633a79c
AM
4091 && sym->section->output_offset == 0)
4092 || bfd_is_abs_section (sym->section)));
5372391b
AM
4093}
4094
2633a79c
AM
4095/* Map symbol from it's internal number to the external number, moving
4096 all local symbols to be at the head of the list. */
4097
0a1b45a2 4098static bool
12bd6957 4099elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4100{
dc810e39 4101 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4102 asymbol **syms = bfd_get_outsymbols (abfd);
4103 asymbol **sect_syms;
dc810e39
AM
4104 unsigned int num_locals = 0;
4105 unsigned int num_globals = 0;
4106 unsigned int num_locals2 = 0;
4107 unsigned int num_globals2 = 0;
7292b3ac 4108 unsigned int max_index = 0;
dc810e39 4109 unsigned int idx;
252b5132
RH
4110 asection *asect;
4111 asymbol **new_syms;
446f7ed5 4112 size_t amt;
252b5132
RH
4113
4114#ifdef DEBUG
4115 fprintf (stderr, "elf_map_symbols\n");
4116 fflush (stderr);
4117#endif
4118
252b5132
RH
4119 for (asect = abfd->sections; asect; asect = asect->next)
4120 {
4121 if (max_index < asect->index)
4122 max_index = asect->index;
4123 }
4124
4125 max_index++;
446f7ed5
AM
4126 amt = max_index * sizeof (asymbol *);
4127 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4128 if (sect_syms == NULL)
0a1b45a2 4129 return false;
252b5132 4130 elf_section_syms (abfd) = sect_syms;
4e89ac30 4131 elf_num_section_syms (abfd) = max_index;
252b5132 4132
079e9a2f
AM
4133 /* Init sect_syms entries for any section symbols we have already
4134 decided to output. */
252b5132
RH
4135 for (idx = 0; idx < symcount; idx++)
4136 {
dc810e39 4137 asymbol *sym = syms[idx];
c044fabd 4138
252b5132 4139 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4140 && sym->value == 0
2633a79c
AM
4141 && !ignore_section_sym (abfd, sym)
4142 && !bfd_is_abs_section (sym->section))
252b5132 4143 {
5372391b 4144 asection *sec = sym->section;
252b5132 4145
5372391b
AM
4146 if (sec->owner != abfd)
4147 sec = sec->output_section;
252b5132 4148
5372391b 4149 sect_syms[sec->index] = syms[idx];
252b5132
RH
4150 }
4151 }
4152
252b5132
RH
4153 /* Classify all of the symbols. */
4154 for (idx = 0; idx < symcount; idx++)
4155 {
2633a79c 4156 if (sym_is_global (abfd, syms[idx]))
252b5132 4157 num_globals++;
2633a79c
AM
4158 else if (!ignore_section_sym (abfd, syms[idx]))
4159 num_locals++;
252b5132 4160 }
079e9a2f 4161
5372391b 4162 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4163 sections will already have a section symbol in outsymbols, but
4164 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4165 at least in that case. */
252b5132
RH
4166 for (asect = abfd->sections; asect; asect = asect->next)
4167 {
d1bcae83
L
4168 asymbol *sym = asect->symbol;
4169 /* Don't include ignored section symbols. */
4170 if (!ignore_section_sym (abfd, sym)
4171 && sect_syms[asect->index] == NULL)
252b5132 4172 {
079e9a2f 4173 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4174 num_locals++;
4175 else
4176 num_globals++;
252b5132
RH
4177 }
4178 }
4179
4180 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4181 amt = (num_locals + num_globals) * sizeof (asymbol *);
4182 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4183 if (new_syms == NULL)
0a1b45a2 4184 return false;
252b5132
RH
4185
4186 for (idx = 0; idx < symcount; idx++)
4187 {
4188 asymbol *sym = syms[idx];
dc810e39 4189 unsigned int i;
252b5132 4190
2633a79c
AM
4191 if (sym_is_global (abfd, sym))
4192 i = num_locals + num_globals2++;
d1bcae83 4193 /* Don't include ignored section symbols. */
2633a79c 4194 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4195 i = num_locals2++;
4196 else
2633a79c 4197 continue;
252b5132
RH
4198 new_syms[i] = sym;
4199 sym->udata.i = i + 1;
4200 }
4201 for (asect = abfd->sections; asect; asect = asect->next)
4202 {
d1bcae83
L
4203 asymbol *sym = asect->symbol;
4204 if (!ignore_section_sym (abfd, sym)
4205 && sect_syms[asect->index] == NULL)
252b5132 4206 {
dc810e39 4207 unsigned int i;
252b5132 4208
079e9a2f 4209 sect_syms[asect->index] = sym;
252b5132
RH
4210 if (!sym_is_global (abfd, sym))
4211 i = num_locals2++;
4212 else
4213 i = num_locals + num_globals2++;
4214 new_syms[i] = sym;
4215 sym->udata.i = i + 1;
4216 }
4217 }
4218
4219 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4220
12bd6957 4221 *pnum_locals = num_locals;
0a1b45a2 4222 return true;
252b5132
RH
4223}
4224
4225/* Align to the maximum file alignment that could be required for any
4226 ELF data structure. */
4227
268b6b39 4228static inline file_ptr
217aa764 4229align_file_position (file_ptr off, int align)
252b5132
RH
4230{
4231 return (off + align - 1) & ~(align - 1);
4232}
4233
4234/* Assign a file position to a section, optionally aligning to the
4235 required section alignment. */
4236
217aa764
AM
4237file_ptr
4238_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4239 file_ptr offset,
0a1b45a2 4240 bool align)
252b5132 4241{
72de5009
AM
4242 if (align && i_shdrp->sh_addralign > 1)
4243 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4244 i_shdrp->sh_offset = offset;
4245 if (i_shdrp->bfd_section != NULL)
4246 i_shdrp->bfd_section->filepos = offset;
4247 if (i_shdrp->sh_type != SHT_NOBITS)
4248 offset += i_shdrp->sh_size;
4249 return offset;
4250}
4251
4252/* Compute the file positions we are going to put the sections at, and
4253 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4254 is not NULL, this is being called by the ELF backend linker. */
4255
0a1b45a2 4256bool
217aa764
AM
4257_bfd_elf_compute_section_file_positions (bfd *abfd,
4258 struct bfd_link_info *link_info)
252b5132 4259{
9c5bfbb7 4260 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4261 struct fake_section_arg fsargs;
0a1b45a2 4262 bool failed;
ef10c3ac 4263 struct elf_strtab_hash *strtab = NULL;
252b5132 4264 Elf_Internal_Shdr *shstrtab_hdr;
0a1b45a2 4265 bool need_symtab;
252b5132
RH
4266
4267 if (abfd->output_has_begun)
0a1b45a2 4268 return true;
252b5132
RH
4269
4270 /* Do any elf backend specific processing first. */
4271 if (bed->elf_backend_begin_write_processing)
4272 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4273
ed7e9d0b 4274 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
0a1b45a2 4275 return false;
252b5132 4276
0a1b45a2 4277 fsargs.failed = false;
d4730f92
BS
4278 fsargs.link_info = link_info;
4279 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4280 if (fsargs.failed)
0a1b45a2 4281 return false;
252b5132 4282
da9f89d4 4283 if (!assign_section_numbers (abfd, link_info))
0a1b45a2 4284 return false;
252b5132
RH
4285
4286 /* The backend linker builds symbol table information itself. */
3516e984
L
4287 need_symtab = (link_info == NULL
4288 && (bfd_get_symcount (abfd) > 0
4289 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4290 == HAS_RELOC)));
4291 if (need_symtab)
252b5132
RH
4292 {
4293 /* Non-zero if doing a relocatable link. */
4294 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4295
3d16b64e 4296 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
0a1b45a2 4297 return false;
252b5132
RH
4298 }
4299
0a1b45a2 4300 failed = false;
1126897b 4301 if (link_info == NULL)
dbb410c3 4302 {
1126897b 4303 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4304 if (failed)
0a1b45a2 4305 return false;
dbb410c3
AM
4306 }
4307
252b5132 4308 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4309 /* sh_name was set in init_file_header. */
252b5132 4310 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4311 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4312 shstrtab_hdr->sh_addr = 0;
946748d5 4313 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4314 shstrtab_hdr->sh_entsize = 0;
4315 shstrtab_hdr->sh_link = 0;
4316 shstrtab_hdr->sh_info = 0;
3e19fb8f 4317 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4318 shstrtab_hdr->sh_addralign = 1;
4319
c84fca4d 4320 if (!assign_file_positions_except_relocs (abfd, link_info))
0a1b45a2 4321 return false;
252b5132 4322
3516e984 4323 if (need_symtab)
252b5132
RH
4324 {
4325 file_ptr off;
4326 Elf_Internal_Shdr *hdr;
4327
12bd6957 4328 off = elf_next_file_pos (abfd);
252b5132 4329
6a40cf0c 4330 hdr = & elf_symtab_hdr (abfd);
0a1b45a2 4331 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4332
6a40cf0c
NC
4333 if (elf_symtab_shndx_list (abfd) != NULL)
4334 {
4335 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4336 if (hdr->sh_size != 0)
0a1b45a2 4337 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6a40cf0c
NC
4338 /* FIXME: What about other symtab_shndx sections in the list ? */
4339 }
9ad5cbcf 4340
252b5132 4341 hdr = &elf_tdata (abfd)->strtab_hdr;
0a1b45a2 4342 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4343
12bd6957 4344 elf_next_file_pos (abfd) = off;
252b5132
RH
4345
4346 /* Now that we know where the .strtab section goes, write it
08a40648 4347 out. */
252b5132 4348 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4349 || ! _bfd_elf_strtab_emit (abfd, strtab))
0a1b45a2 4350 return false;
ef10c3ac 4351 _bfd_elf_strtab_free (strtab);
252b5132
RH
4352 }
4353
0a1b45a2 4354 abfd->output_has_begun = true;
252b5132 4355
0a1b45a2 4356 return true;
252b5132
RH
4357}
4358
8ded5a0f
AM
4359/* Make an initial estimate of the size of the program header. If we
4360 get the number wrong here, we'll redo section placement. */
4361
4362static bfd_size_type
4363get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4364{
4365 size_t segs;
4366 asection *s;
2b05f1b7 4367 const struct elf_backend_data *bed;
8ded5a0f
AM
4368
4369 /* Assume we will need exactly two PT_LOAD segments: one for text
4370 and one for data. */
4371 segs = 2;
4372
4373 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4374 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4375 {
4376 /* If we have a loadable interpreter section, we need a
4377 PT_INTERP segment. In this case, assume we also need a
4378 PT_PHDR segment, although that may not be true for all
4379 targets. */
e9a38e0f 4380 segs += 2;
8ded5a0f
AM
4381 }
4382
4383 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4384 {
4385 /* We need a PT_DYNAMIC segment. */
4386 ++segs;
f210dcff 4387 }
08a40648 4388
ceae84aa 4389 if (info != NULL && info->relro)
f210dcff
L
4390 {
4391 /* We need a PT_GNU_RELRO segment. */
4392 ++segs;
8ded5a0f
AM
4393 }
4394
12bd6957 4395 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4396 {
4397 /* We need a PT_GNU_EH_FRAME segment. */
4398 ++segs;
4399 }
4400
12bd6957 4401 if (elf_stack_flags (abfd))
8ded5a0f 4402 {
2b05f1b7
L
4403 /* We need a PT_GNU_STACK segment. */
4404 ++segs;
4405 }
94b11780 4406
0a59decb
L
4407 s = bfd_get_section_by_name (abfd,
4408 NOTE_GNU_PROPERTY_SECTION_NAME);
4409 if (s != NULL && s->size != 0)
4410 {
4411 /* We need a PT_GNU_PROPERTY segment. */
4412 ++segs;
4413 }
4414
2b05f1b7
L
4415 for (s = abfd->sections; s != NULL; s = s->next)
4416 {
8ded5a0f 4417 if ((s->flags & SEC_LOAD) != 0
23e463ed 4418 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4419 {
23e463ed 4420 unsigned int alignment_power;
8ded5a0f
AM
4421 /* We need a PT_NOTE segment. */
4422 ++segs;
23e463ed
L
4423 /* Try to create just one PT_NOTE segment for all adjacent
4424 loadable SHT_NOTE sections. gABI requires that within a
4425 PT_NOTE segment (and also inside of each SHT_NOTE section)
4426 each note should have the same alignment. So we check
4427 whether the sections are correctly aligned. */
4428 alignment_power = s->alignment_power;
4429 while (s->next != NULL
4430 && s->next->alignment_power == alignment_power
4431 && (s->next->flags & SEC_LOAD) != 0
4432 && elf_section_type (s->next) == SHT_NOTE)
4433 s = s->next;
8ded5a0f
AM
4434 }
4435 }
4436
4437 for (s = abfd->sections; s != NULL; s = s->next)
4438 {
4439 if (s->flags & SEC_THREAD_LOCAL)
4440 {
4441 /* We need a PT_TLS segment. */
4442 ++segs;
4443 break;
4444 }
4445 }
4446
2b05f1b7 4447 bed = get_elf_backend_data (abfd);
a91e1603 4448
df3a023b
AM
4449 if ((abfd->flags & D_PAGED) != 0
4450 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4451 {
4452 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4453 bfd_vma commonpagesize;
4454 unsigned int page_align_power;
4455
4456 if (info != NULL)
4457 commonpagesize = info->commonpagesize;
4458 else
4459 commonpagesize = bed->commonpagesize;
4460 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4461 for (s = abfd->sections; s != NULL; s = s->next)
4462 if (elf_section_flags (s) & SHF_GNU_MBIND)
4463 {
4464 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4465 {
4466 _bfd_error_handler
4467 /* xgettext:c-format */
4468 (_("%pB: GNU_MBIND section `%pA' has invalid "
4469 "sh_info field: %d"),
4470 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4471 continue;
4472 }
4473 /* Align mbind section to page size. */
4474 if (s->alignment_power < page_align_power)
4475 s->alignment_power = page_align_power;
4476 segs ++;
4477 }
4478 }
4479
4480 /* Let the backend count up any program headers it might need. */
4481 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4482 {
4483 int a;
4484
4485 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4486 if (a == -1)
4487 abort ();
4488 segs += a;
4489 }
4490
4491 return segs * bed->s->sizeof_phdr;
4492}
4493
2ea37f1c
NC
4494/* Find the segment that contains the output_section of section. */
4495
4496Elf_Internal_Phdr *
4497_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4498{
4499 struct elf_segment_map *m;
4500 Elf_Internal_Phdr *p;
4501
12bd6957 4502 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4503 m != NULL;
4504 m = m->next, p++)
4505 {
4506 int i;
4507
4508 for (i = m->count - 1; i >= 0; i--)
4509 if (m->sections[i] == section)
4510 return p;
4511 }
4512
4513 return NULL;
4514}
4515
252b5132
RH
4516/* Create a mapping from a set of sections to a program segment. */
4517
217aa764
AM
4518static struct elf_segment_map *
4519make_mapping (bfd *abfd,
4520 asection **sections,
4521 unsigned int from,
4522 unsigned int to,
0a1b45a2 4523 bool phdr)
252b5132
RH
4524{
4525 struct elf_segment_map *m;
4526 unsigned int i;
4527 asection **hdrpp;
986f0783 4528 size_t amt;
252b5132 4529
00bee008
AM
4530 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4531 amt += (to - from) * sizeof (asection *);
a50b1753 4532 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4533 if (m == NULL)
4534 return NULL;
4535 m->next = NULL;
4536 m->p_type = PT_LOAD;
4537 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4538 m->sections[i - from] = *hdrpp;
4539 m->count = to - from;
4540
4541 if (from == 0 && phdr)
4542 {
4543 /* Include the headers in the first PT_LOAD segment. */
4544 m->includes_filehdr = 1;
4545 m->includes_phdrs = 1;
4546 }
4547
4548 return m;
4549}
4550
229fcec5
MM
4551/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4552 on failure. */
4553
4554struct elf_segment_map *
4555_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4556{
4557 struct elf_segment_map *m;
4558
a50b1753 4559 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4560 sizeof (struct elf_segment_map));
229fcec5
MM
4561 if (m == NULL)
4562 return NULL;
4563 m->next = NULL;
4564 m->p_type = PT_DYNAMIC;
4565 m->count = 1;
4566 m->sections[0] = dynsec;
08a40648 4567
229fcec5
MM
4568 return m;
4569}
4570
8ded5a0f 4571/* Possibly add or remove segments from the segment map. */
252b5132 4572
0a1b45a2 4573static bool
3dea8fca
AM
4574elf_modify_segment_map (bfd *abfd,
4575 struct bfd_link_info *info,
0a1b45a2 4576 bool remove_empty_load)
252b5132 4577{
252e386e 4578 struct elf_segment_map **m;
8ded5a0f 4579 const struct elf_backend_data *bed;
252b5132 4580
8ded5a0f
AM
4581 /* The placement algorithm assumes that non allocated sections are
4582 not in PT_LOAD segments. We ensure this here by removing such
4583 sections from the segment map. We also remove excluded
252e386e
AM
4584 sections. Finally, any PT_LOAD segment without sections is
4585 removed. */
12bd6957 4586 m = &elf_seg_map (abfd);
252e386e 4587 while (*m)
8ded5a0f
AM
4588 {
4589 unsigned int i, new_count;
252b5132 4590
252e386e 4591 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4592 {
252e386e
AM
4593 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4594 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4595 || (*m)->p_type != PT_LOAD))
8ded5a0f 4596 {
252e386e
AM
4597 (*m)->sections[new_count] = (*m)->sections[i];
4598 new_count++;
8ded5a0f
AM
4599 }
4600 }
252e386e 4601 (*m)->count = new_count;
252b5132 4602
1a9ccd70
NC
4603 if (remove_empty_load
4604 && (*m)->p_type == PT_LOAD
4605 && (*m)->count == 0
4606 && !(*m)->includes_phdrs)
252e386e
AM
4607 *m = (*m)->next;
4608 else
4609 m = &(*m)->next;
8ded5a0f 4610 }
252b5132 4611
8ded5a0f
AM
4612 bed = get_elf_backend_data (abfd);
4613 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4614 {
252e386e 4615 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
0a1b45a2 4616 return false;
252b5132 4617 }
252b5132 4618
0a1b45a2 4619 return true;
8ded5a0f 4620}
252b5132 4621
dbc88fc1
AM
4622#define IS_TBSS(s) \
4623 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4624
8ded5a0f 4625/* Set up a mapping from BFD sections to program segments. */
252b5132 4626
0a1b45a2 4627bool
8ded5a0f
AM
4628_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4629{
4630 unsigned int count;
4631 struct elf_segment_map *m;
4632 asection **sections = NULL;
4633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 4634 bool no_user_phdrs;
252b5132 4635
12bd6957 4636 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4637
4638 if (info != NULL)
4639 info->user_phdrs = !no_user_phdrs;
4640
3dea8fca 4641 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4642 {
8ded5a0f
AM
4643 asection *s;
4644 unsigned int i;
4645 struct elf_segment_map *mfirst;
4646 struct elf_segment_map **pm;
4647 asection *last_hdr;
4648 bfd_vma last_size;
00bee008 4649 unsigned int hdr_index;
8ded5a0f
AM
4650 bfd_vma maxpagesize;
4651 asection **hdrpp;
0a1b45a2
AM
4652 bool phdr_in_segment;
4653 bool writable;
4654 bool executable;
446f7ed5 4655 unsigned int tls_count = 0;
8ded5a0f 4656 asection *first_tls = NULL;
a91e1603 4657 asection *first_mbind = NULL;
8ded5a0f 4658 asection *dynsec, *eh_frame_hdr;
446f7ed5 4659 size_t amt;
66631823
CE
4660 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4661 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4662 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4663
8ded5a0f 4664 /* Select the allocated sections, and sort them. */
252b5132 4665
446f7ed5
AM
4666 amt = bfd_count_sections (abfd) * sizeof (asection *);
4667 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4668 if (sections == NULL)
252b5132 4669 goto error_return;
252b5132 4670
8d06853e
AM
4671 /* Calculate top address, avoiding undefined behaviour of shift
4672 left operator when shift count is equal to size of type
4673 being shifted. */
4674 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4675 addr_mask = (addr_mask << 1) + 1;
4676
8ded5a0f
AM
4677 i = 0;
4678 for (s = abfd->sections; s != NULL; s = s->next)
4679 {
4680 if ((s->flags & SEC_ALLOC) != 0)
4681 {
48db3297
AM
4682 /* target_index is unused until bfd_elf_final_link
4683 starts output of section symbols. Use it to make
4684 qsort stable. */
4685 s->target_index = i;
8ded5a0f
AM
4686 sections[i] = s;
4687 ++i;
8d06853e 4688 /* A wrapping section potentially clashes with header. */
66631823
CE
4689 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4690 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4691 }
4692 }
4693 BFD_ASSERT (i <= bfd_count_sections (abfd));
4694 count = i;
252b5132 4695
8ded5a0f 4696 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4697
64029e93
AM
4698 phdr_size = elf_program_header_size (abfd);
4699 if (phdr_size == (bfd_size_type) -1)
4700 phdr_size = get_program_header_size (abfd, info);
4701 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4702 /* phdr_size is compared to LMA values which are in bytes. */
4703 phdr_size /= opb;
c410035d
AM
4704 if (info != NULL)
4705 maxpagesize = info->maxpagesize;
4706 else
4707 maxpagesize = bed->maxpagesize;
64029e93
AM
4708 if (maxpagesize == 0)
4709 maxpagesize = 1;
4710 phdr_in_segment = info != NULL && info->load_phdrs;
4711 if (count != 0
4712 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4713 >= (phdr_size & (maxpagesize - 1))))
4714 /* For compatibility with old scripts that may not be using
4715 SIZEOF_HEADERS, add headers when it looks like space has
4716 been left for them. */
0a1b45a2 4717 phdr_in_segment = true;
252b5132 4718
64029e93 4719 /* Build the mapping. */
8ded5a0f
AM
4720 mfirst = NULL;
4721 pm = &mfirst;
252b5132 4722
8ded5a0f
AM
4723 /* If we have a .interp section, then create a PT_PHDR segment for
4724 the program headers and a PT_INTERP segment for the .interp
4725 section. */
4726 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4727 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4728 {
4729 amt = sizeof (struct elf_segment_map);
a50b1753 4730 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4731 if (m == NULL)
4732 goto error_return;
4733 m->next = NULL;
4734 m->p_type = PT_PHDR;
f882209d 4735 m->p_flags = PF_R;
8ded5a0f
AM
4736 m->p_flags_valid = 1;
4737 m->includes_phdrs = 1;
0a1b45a2 4738 phdr_in_segment = true;
8ded5a0f
AM
4739 *pm = m;
4740 pm = &m->next;
252b5132 4741
8ded5a0f 4742 amt = sizeof (struct elf_segment_map);
a50b1753 4743 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4744 if (m == NULL)
4745 goto error_return;
4746 m->next = NULL;
4747 m->p_type = PT_INTERP;
4748 m->count = 1;
4749 m->sections[0] = s;
4750
4751 *pm = m;
4752 pm = &m->next;
252b5132 4753 }
8ded5a0f
AM
4754
4755 /* Look through the sections. We put sections in the same program
4756 segment when the start of the second section can be placed within
4757 a few bytes of the end of the first section. */
4758 last_hdr = NULL;
4759 last_size = 0;
00bee008 4760 hdr_index = 0;
0a1b45a2
AM
4761 writable = false;
4762 executable = false;
8ded5a0f
AM
4763 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4764 if (dynsec != NULL
4765 && (dynsec->flags & SEC_LOAD) == 0)
4766 dynsec = NULL;
4767
64029e93 4768 if ((abfd->flags & D_PAGED) == 0)
0a1b45a2 4769 phdr_in_segment = false;
64029e93 4770
8ded5a0f
AM
4771 /* Deal with -Ttext or something similar such that the first section
4772 is not adjacent to the program headers. This is an
4773 approximation, since at this point we don't know exactly how many
4774 program headers we will need. */
64029e93 4775 if (phdr_in_segment && count > 0)
252b5132 4776 {
66631823 4777 bfd_vma phdr_lma; /* Bytes. */
0a1b45a2 4778 bool separate_phdr = false;
64029e93
AM
4779
4780 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4781 if (info != NULL
4782 && info->separate_code
4783 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4784 {
64029e93
AM
4785 /* If data sections should be separate from code and
4786 thus not executable, and the first section is
4787 executable then put the file and program headers in
4788 their own PT_LOAD. */
0a1b45a2 4789 separate_phdr = true;
64029e93
AM
4790 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4791 == (sections[0]->lma & addr_mask & -maxpagesize)))
4792 {
4793 /* The file and program headers are currently on the
4794 same page as the first section. Put them on the
4795 previous page if we can. */
4796 if (phdr_lma >= maxpagesize)
4797 phdr_lma -= maxpagesize;
4798 else
0a1b45a2 4799 separate_phdr = false;
64029e93
AM
4800 }
4801 }
4802 if ((sections[0]->lma & addr_mask) < phdr_lma
4803 || (sections[0]->lma & addr_mask) < phdr_size)
4804 /* If file and program headers would be placed at the end
4805 of memory then it's probably better to omit them. */
0a1b45a2 4806 phdr_in_segment = false;
64029e93
AM
4807 else if (phdr_lma < wrap_to)
4808 /* If a section wraps around to where we'll be placing
4809 file and program headers, then the headers will be
4810 overwritten. */
0a1b45a2 4811 phdr_in_segment = false;
64029e93
AM
4812 else if (separate_phdr)
4813 {
4814 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4815 if (m == NULL)
4816 goto error_return;
66631823 4817 m->p_paddr = phdr_lma * opb;
64029e93
AM
4818 m->p_vaddr_offset
4819 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4820 m->p_paddr_valid = 1;
4821 *pm = m;
4822 pm = &m->next;
0a1b45a2 4823 phdr_in_segment = false;
1a9ccd70 4824 }
252b5132
RH
4825 }
4826
8ded5a0f 4827 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4828 {
8ded5a0f 4829 asection *hdr;
0a1b45a2 4830 bool new_segment;
8ded5a0f
AM
4831
4832 hdr = *hdrpp;
4833
4834 /* See if this section and the last one will fit in the same
4835 segment. */
4836
4837 if (last_hdr == NULL)
4838 {
4839 /* If we don't have a segment yet, then we don't need a new
4840 one (we build the last one after this loop). */
0a1b45a2 4841 new_segment = false;
8ded5a0f
AM
4842 }
4843 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4844 {
4845 /* If this section has a different relation between the
4846 virtual address and the load address, then we need a new
4847 segment. */
0a1b45a2 4848 new_segment = true;
8ded5a0f 4849 }
b5599592
AM
4850 else if (hdr->lma < last_hdr->lma + last_size
4851 || last_hdr->lma + last_size < last_hdr->lma)
4852 {
4853 /* If this section has a load address that makes it overlap
4854 the previous section, then we need a new segment. */
0a1b45a2 4855 new_segment = true;
b5599592 4856 }
76cb3a89
AM
4857 else if ((abfd->flags & D_PAGED) != 0
4858 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4859 == (hdr->lma & -maxpagesize)))
4860 {
4861 /* If we are demand paged then we can't map two disk
4862 pages onto the same memory page. */
0a1b45a2 4863 new_segment = false;
76cb3a89 4864 }
39948a60
NC
4865 /* In the next test we have to be careful when last_hdr->lma is close
4866 to the end of the address space. If the aligned address wraps
4867 around to the start of the address space, then there are no more
4868 pages left in memory and it is OK to assume that the current
4869 section can be included in the current segment. */
76cb3a89
AM
4870 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4871 + maxpagesize > last_hdr->lma)
4872 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4873 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4874 {
4875 /* If putting this section in this segment would force us to
4876 skip a page in the segment, then we need a new segment. */
0a1b45a2 4877 new_segment = true;
8ded5a0f
AM
4878 }
4879 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4880 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4881 {
e5654c0f
AM
4882 /* We don't want to put a loaded section after a
4883 nonloaded (ie. bss style) section in the same segment
4884 as that will force the non-loaded section to be loaded.
76cb3a89 4885 Consider .tbss sections as loaded for this purpose. */
0a1b45a2 4886 new_segment = true;
8ded5a0f
AM
4887 }
4888 else if ((abfd->flags & D_PAGED) == 0)
4889 {
4890 /* If the file is not demand paged, which means that we
4891 don't require the sections to be correctly aligned in the
4892 file, then there is no other reason for a new segment. */
0a1b45a2 4893 new_segment = false;
8ded5a0f 4894 }
2888249f
L
4895 else if (info != NULL
4896 && info->separate_code
4897 && executable != ((hdr->flags & SEC_CODE) != 0))
4898 {
0a1b45a2 4899 new_segment = true;
2888249f 4900 }
8ded5a0f 4901 else if (! writable
76cb3a89 4902 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4903 {
4904 /* We don't want to put a writable section in a read only
76cb3a89 4905 segment. */
0a1b45a2 4906 new_segment = true;
8ded5a0f
AM
4907 }
4908 else
4909 {
4910 /* Otherwise, we can use the same segment. */
0a1b45a2 4911 new_segment = false;
8ded5a0f
AM
4912 }
4913
2889e75b 4914 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4915 if (last_hdr != NULL
4916 && info != NULL
4917 && info->callbacks->override_segment_assignment != NULL)
4918 new_segment
4919 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4920 last_hdr,
4921 new_segment);
2889e75b 4922
8ded5a0f
AM
4923 if (! new_segment)
4924 {
4925 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4926 writable = true;
2888249f 4927 if ((hdr->flags & SEC_CODE) != 0)
0a1b45a2 4928 executable = true;
8ded5a0f
AM
4929 last_hdr = hdr;
4930 /* .tbss sections effectively have zero size. */
502794d4 4931 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4932 continue;
4933 }
4934
4935 /* We need a new program segment. We must create a new program
00bee008 4936 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4937
00bee008 4938 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4939 if (m == NULL)
4940 goto error_return;
4941
4942 *pm = m;
4943 pm = &m->next;
4944
252b5132 4945 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4946 writable = true;
8ded5a0f 4947 else
0a1b45a2 4948 writable = false;
8ded5a0f 4949
2888249f 4950 if ((hdr->flags & SEC_CODE) == 0)
0a1b45a2 4951 executable = false;
2888249f 4952 else
0a1b45a2 4953 executable = true;
2888249f 4954
baaff79e
JJ
4955 last_hdr = hdr;
4956 /* .tbss sections effectively have zero size. */
502794d4 4957 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4958 hdr_index = i;
0a1b45a2 4959 phdr_in_segment = false;
252b5132
RH
4960 }
4961
86b2281f
AM
4962 /* Create a final PT_LOAD program segment, but not if it's just
4963 for .tbss. */
4964 if (last_hdr != NULL
00bee008 4965 && (i - hdr_index != 1
dbc88fc1 4966 || !IS_TBSS (last_hdr)))
8ded5a0f 4967 {
00bee008 4968 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4969 if (m == NULL)
4970 goto error_return;
252b5132 4971
8ded5a0f
AM
4972 *pm = m;
4973 pm = &m->next;
4974 }
252b5132 4975
8ded5a0f
AM
4976 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4977 if (dynsec != NULL)
4978 {
4979 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4980 if (m == NULL)
4981 goto error_return;
4982 *pm = m;
4983 pm = &m->next;
4984 }
252b5132 4985
23e463ed 4986 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4987 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4988 because if we link together nonloadable .note sections and
4989 loadable .note sections, we will generate two .note sections
23e463ed 4990 in the output file. */
8ded5a0f
AM
4991 for (s = abfd->sections; s != NULL; s = s->next)
4992 {
4993 if ((s->flags & SEC_LOAD) != 0
23e463ed 4994 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4995 {
1c5265b5 4996 asection *s2;
23e463ed 4997 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
4998
4999 count = 1;
23e463ed
L
5000 for (s2 = s; s2->next != NULL; s2 = s2->next)
5001 {
5002 if (s2->next->alignment_power == alignment_power
5003 && (s2->next->flags & SEC_LOAD) != 0
5004 && elf_section_type (s2->next) == SHT_NOTE
66631823 5005 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5006 alignment_power)
5007 == s2->next->lma)
5008 count++;
5009 else
5010 break;
5011 }
00bee008
AM
5012 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5013 amt += count * sizeof (asection *);
a50b1753 5014 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5015 if (m == NULL)
5016 goto error_return;
5017 m->next = NULL;
5018 m->p_type = PT_NOTE;
1c5265b5
JJ
5019 m->count = count;
5020 while (count > 1)
5021 {
5022 m->sections[m->count - count--] = s;
5023 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5024 s = s->next;
5025 }
5026 m->sections[m->count - 1] = s;
5027 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5028 *pm = m;
5029 pm = &m->next;
5030 }
5031 if (s->flags & SEC_THREAD_LOCAL)
5032 {
5033 if (! tls_count)
5034 first_tls = s;
5035 tls_count++;
5036 }
a91e1603
L
5037 if (first_mbind == NULL
5038 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5039 first_mbind = s;
8ded5a0f 5040 }
252b5132 5041
8ded5a0f
AM
5042 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5043 if (tls_count > 0)
5044 {
00bee008
AM
5045 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5046 amt += tls_count * sizeof (asection *);
a50b1753 5047 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5048 if (m == NULL)
5049 goto error_return;
5050 m->next = NULL;
5051 m->p_type = PT_TLS;
5052 m->count = tls_count;
5053 /* Mandated PF_R. */
5054 m->p_flags = PF_R;
5055 m->p_flags_valid = 1;
d923cae0 5056 s = first_tls;
446f7ed5 5057 for (i = 0; i < tls_count; ++i)
8ded5a0f 5058 {
d923cae0
L
5059 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5060 {
5061 _bfd_error_handler
871b3ab2 5062 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5063 s = first_tls;
5064 i = 0;
446f7ed5 5065 while (i < tls_count)
d923cae0
L
5066 {
5067 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5068 {
871b3ab2 5069 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5070 i++;
5071 }
5072 else
871b3ab2 5073 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5074 s = s->next;
5075 }
5076 bfd_set_error (bfd_error_bad_value);
5077 goto error_return;
5078 }
5079 m->sections[i] = s;
5080 s = s->next;
8ded5a0f 5081 }
252b5132 5082
8ded5a0f
AM
5083 *pm = m;
5084 pm = &m->next;
5085 }
252b5132 5086
df3a023b
AM
5087 if (first_mbind
5088 && (abfd->flags & D_PAGED) != 0
5089 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5090 for (s = first_mbind; s != NULL; s = s->next)
5091 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5092 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5093 {
5094 /* Mandated PF_R. */
5095 unsigned long p_flags = PF_R;
5096 if ((s->flags & SEC_READONLY) == 0)
5097 p_flags |= PF_W;
5098 if ((s->flags & SEC_CODE) != 0)
5099 p_flags |= PF_X;
5100
5101 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5102 m = bfd_zalloc (abfd, amt);
5103 if (m == NULL)
5104 goto error_return;
5105 m->next = NULL;
5106 m->p_type = (PT_GNU_MBIND_LO
5107 + elf_section_data (s)->this_hdr.sh_info);
5108 m->count = 1;
5109 m->p_flags_valid = 1;
5110 m->sections[0] = s;
5111 m->p_flags = p_flags;
5112
5113 *pm = m;
5114 pm = &m->next;
5115 }
5116
0a59decb
L
5117 s = bfd_get_section_by_name (abfd,
5118 NOTE_GNU_PROPERTY_SECTION_NAME);
5119 if (s != NULL && s->size != 0)
5120 {
5121 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5122 m = bfd_zalloc (abfd, amt);
5123 if (m == NULL)
5124 goto error_return;
5125 m->next = NULL;
5126 m->p_type = PT_GNU_PROPERTY;
5127 m->count = 1;
5128 m->p_flags_valid = 1;
5129 m->sections[0] = s;
5130 m->p_flags = PF_R;
5131 *pm = m;
5132 pm = &m->next;
5133 }
5134
8ded5a0f
AM
5135 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5136 segment. */
12bd6957 5137 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5138 if (eh_frame_hdr != NULL
5139 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5140 {
dc810e39 5141 amt = sizeof (struct elf_segment_map);
a50b1753 5142 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5143 if (m == NULL)
5144 goto error_return;
5145 m->next = NULL;
8ded5a0f 5146 m->p_type = PT_GNU_EH_FRAME;
252b5132 5147 m->count = 1;
8ded5a0f 5148 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5149
5150 *pm = m;
5151 pm = &m->next;
5152 }
13ae64f3 5153
12bd6957 5154 if (elf_stack_flags (abfd))
13ae64f3 5155 {
8ded5a0f 5156 amt = sizeof (struct elf_segment_map);
a50b1753 5157 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5158 if (m == NULL)
5159 goto error_return;
5160 m->next = NULL;
2b05f1b7 5161 m->p_type = PT_GNU_STACK;
12bd6957 5162 m->p_flags = elf_stack_flags (abfd);
04c3a755 5163 m->p_align = bed->stack_align;
8ded5a0f 5164 m->p_flags_valid = 1;
04c3a755
NS
5165 m->p_align_valid = m->p_align != 0;
5166 if (info->stacksize > 0)
5167 {
5168 m->p_size = info->stacksize;
5169 m->p_size_valid = 1;
5170 }
252b5132 5171
8ded5a0f
AM
5172 *pm = m;
5173 pm = &m->next;
5174 }
65765700 5175
ceae84aa 5176 if (info != NULL && info->relro)
8ded5a0f 5177 {
f210dcff
L
5178 for (m = mfirst; m != NULL; m = m->next)
5179 {
3832a4d8
AM
5180 if (m->p_type == PT_LOAD
5181 && m->count != 0
5182 && m->sections[0]->vma >= info->relro_start
5183 && m->sections[0]->vma < info->relro_end)
f210dcff 5184 {
3832a4d8
AM
5185 i = m->count;
5186 while (--i != (unsigned) -1)
ec2e748a
NC
5187 {
5188 if (m->sections[i]->size > 0
5189 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5190 == (SEC_LOAD | SEC_HAS_CONTENTS))
5191 break;
5192 }
3832a4d8 5193
43a8475c 5194 if (i != (unsigned) -1)
f210dcff
L
5195 break;
5196 }
be01b344 5197 }
f210dcff
L
5198
5199 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5200 if (m != NULL)
5201 {
5202 amt = sizeof (struct elf_segment_map);
a50b1753 5203 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5204 if (m == NULL)
5205 goto error_return;
5206 m->next = NULL;
5207 m->p_type = PT_GNU_RELRO;
f210dcff
L
5208 *pm = m;
5209 pm = &m->next;
5210 }
8ded5a0f 5211 }
9ee5e499 5212
8ded5a0f 5213 free (sections);
12bd6957 5214 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5215 }
5216
3dea8fca 5217 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
0a1b45a2 5218 return false;
8c37241b 5219
12bd6957 5220 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5221 ++count;
12bd6957 5222 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5223
0a1b45a2 5224 return true;
252b5132
RH
5225
5226 error_return:
c9594989 5227 free (sections);
0a1b45a2 5228 return false;
252b5132
RH
5229}
5230
5231/* Sort sections by address. */
5232
5233static int
217aa764 5234elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5235{
5236 const asection *sec1 = *(const asection **) arg1;
5237 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5238 bfd_size_type size1, size2;
252b5132
RH
5239
5240 /* Sort by LMA first, since this is the address used to
5241 place the section into a segment. */
5242 if (sec1->lma < sec2->lma)
5243 return -1;
5244 else if (sec1->lma > sec2->lma)
5245 return 1;
5246
5247 /* Then sort by VMA. Normally the LMA and the VMA will be
5248 the same, and this will do nothing. */
5249 if (sec1->vma < sec2->vma)
5250 return -1;
5251 else if (sec1->vma > sec2->vma)
5252 return 1;
5253
5254 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5255
8d748d1d
AM
5256#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5257 && (x)->size != 0)
252b5132
RH
5258
5259 if (TOEND (sec1))
5260 {
48db3297 5261 if (!TOEND (sec2))
252b5132
RH
5262 return 1;
5263 }
00a7cdc5 5264 else if (TOEND (sec2))
252b5132
RH
5265 return -1;
5266
5267#undef TOEND
5268
00a7cdc5
NC
5269 /* Sort by size, to put zero sized sections
5270 before others at the same address. */
252b5132 5271
eea6121a
AM
5272 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5273 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5274
5275 if (size1 < size2)
252b5132 5276 return -1;
eecdbe52 5277 if (size1 > size2)
252b5132
RH
5278 return 1;
5279
5280 return sec1->target_index - sec2->target_index;
5281}
5282
30fe1832
AM
5283/* This qsort comparison functions sorts PT_LOAD segments first and
5284 by p_paddr, for assign_file_positions_for_load_sections. */
5285
5286static int
5287elf_sort_segments (const void *arg1, const void *arg2)
5288{
5289 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5290 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5291
5292 if (m1->p_type != m2->p_type)
5293 {
5294 if (m1->p_type == PT_NULL)
5295 return 1;
5296 if (m2->p_type == PT_NULL)
5297 return -1;
5298 return m1->p_type < m2->p_type ? -1 : 1;
5299 }
5300 if (m1->includes_filehdr != m2->includes_filehdr)
5301 return m1->includes_filehdr ? -1 : 1;
5302 if (m1->no_sort_lma != m2->no_sort_lma)
5303 return m1->no_sort_lma ? -1 : 1;
5304 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5305 {
4b3ecb3b 5306 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5307 lma1 = 0;
5308 if (m1->p_paddr_valid)
4b3ecb3b 5309 lma1 = m1->p_paddr;
30fe1832 5310 else if (m1->count != 0)
4b3ecb3b
AM
5311 {
5312 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5313 m1->sections[0]);
5314 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5315 }
30fe1832
AM
5316 lma2 = 0;
5317 if (m2->p_paddr_valid)
4b3ecb3b 5318 lma2 = m2->p_paddr;
30fe1832 5319 else if (m2->count != 0)
4b3ecb3b
AM
5320 {
5321 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5322 m2->sections[0]);
5323 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5324 }
30fe1832
AM
5325 if (lma1 != lma2)
5326 return lma1 < lma2 ? -1 : 1;
5327 }
5328 if (m1->idx != m2->idx)
5329 return m1->idx < m2->idx ? -1 : 1;
5330 return 0;
5331}
5332
340b6d91
AC
5333/* Ian Lance Taylor writes:
5334
5335 We shouldn't be using % with a negative signed number. That's just
5336 not good. We have to make sure either that the number is not
5337 negative, or that the number has an unsigned type. When the types
5338 are all the same size they wind up as unsigned. When file_ptr is a
5339 larger signed type, the arithmetic winds up as signed long long,
5340 which is wrong.
5341
5342 What we're trying to say here is something like ``increase OFF by
5343 the least amount that will cause it to be equal to the VMA modulo
5344 the page size.'' */
5345/* In other words, something like:
5346
5347 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5348 off_offset = off % bed->maxpagesize;
5349 if (vma_offset < off_offset)
5350 adjustment = vma_offset + bed->maxpagesize - off_offset;
5351 else
5352 adjustment = vma_offset - off_offset;
08a40648 5353
de194d85 5354 which can be collapsed into the expression below. */
340b6d91
AC
5355
5356static file_ptr
5357vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5358{
dc9155b2
NC
5359 /* PR binutils/16199: Handle an alignment of zero. */
5360 if (maxpagesize == 0)
5361 maxpagesize = 1;
340b6d91
AC
5362 return ((vma - off) % maxpagesize);
5363}
5364
6d33f217
L
5365static void
5366print_segment_map (const struct elf_segment_map *m)
5367{
5368 unsigned int j;
5369 const char *pt = get_segment_type (m->p_type);
5370 char buf[32];
5371
5372 if (pt == NULL)
5373 {
5374 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5375 sprintf (buf, "LOPROC+%7.7x",
5376 (unsigned int) (m->p_type - PT_LOPROC));
5377 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5378 sprintf (buf, "LOOS+%7.7x",
5379 (unsigned int) (m->p_type - PT_LOOS));
5380 else
5381 snprintf (buf, sizeof (buf), "%8.8x",
5382 (unsigned int) m->p_type);
5383 pt = buf;
5384 }
4a97a0e5 5385 fflush (stdout);
6d33f217
L
5386 fprintf (stderr, "%s:", pt);
5387 for (j = 0; j < m->count; j++)
5388 fprintf (stderr, " %s", m->sections [j]->name);
5389 putc ('\n',stderr);
4a97a0e5 5390 fflush (stderr);
6d33f217
L
5391}
5392
0a1b45a2 5393static bool
32812159
AM
5394write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5395{
5396 void *buf;
0a1b45a2 5397 bool ret;
32812159
AM
5398
5399 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
0a1b45a2 5400 return false;
32812159
AM
5401 buf = bfd_zmalloc (len);
5402 if (buf == NULL)
0a1b45a2 5403 return false;
32812159
AM
5404 ret = bfd_bwrite (buf, len, abfd) == len;
5405 free (buf);
5406 return ret;
5407}
5408
252b5132
RH
5409/* Assign file positions to the sections based on the mapping from
5410 sections to segments. This function also sets up some fields in
f3520d2f 5411 the file header. */
252b5132 5412
0a1b45a2 5413static bool
f3520d2f
AM
5414assign_file_positions_for_load_sections (bfd *abfd,
5415 struct bfd_link_info *link_info)
252b5132
RH
5416{
5417 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5418 struct elf_segment_map *m;
30fe1832 5419 struct elf_segment_map *phdr_load_seg;
252b5132 5420 Elf_Internal_Phdr *phdrs;
252b5132 5421 Elf_Internal_Phdr *p;
502794d4 5422 file_ptr off; /* Octets. */
3f570048 5423 bfd_size_type maxpagesize;
30fe1832 5424 unsigned int alloc, actual;
0920dee7 5425 unsigned int i, j;
30fe1832 5426 struct elf_segment_map **sorted_seg_map;
502794d4 5427 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5428
e36284ab 5429 if (link_info == NULL
ceae84aa 5430 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
0a1b45a2 5431 return false;
252b5132 5432
8ded5a0f 5433 alloc = 0;
12bd6957 5434 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5435 m->idx = alloc++;
252b5132 5436
82f2dbf7
NC
5437 if (alloc)
5438 {
5439 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5440 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5441 }
5442 else
5443 {
5444 /* PR binutils/12467. */
5445 elf_elfheader (abfd)->e_phoff = 0;
5446 elf_elfheader (abfd)->e_phentsize = 0;
5447 }
d324f6d6 5448
8ded5a0f 5449 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5450
12bd6957 5451 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5452 {
5453 actual = alloc;
5454 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5455 }
8ded5a0f 5456 else
30fe1832
AM
5457 {
5458 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5459 BFD_ASSERT (elf_program_header_size (abfd)
5460 == actual * bed->s->sizeof_phdr);
5461 BFD_ASSERT (actual >= alloc);
5462 }
252b5132
RH
5463
5464 if (alloc == 0)
f3520d2f 5465 {
12bd6957 5466 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
0a1b45a2 5467 return true;
f3520d2f 5468 }
252b5132 5469
12bd6957 5470 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5471 see assign_file_positions_except_relocs, so make sure we have
5472 that amount allocated, with trailing space cleared.
12bd6957
AM
5473 The variable alloc contains the computed need, while
5474 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5475 layout.
5476 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5477 where the layout is forced to according to a larger size in the
5478 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5479 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5480 + alloc * sizeof (*sorted_seg_map)));
5481 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5482 elf_tdata (abfd)->phdr = phdrs;
252b5132 5483 if (phdrs == NULL)
0a1b45a2 5484 return false;
252b5132 5485
30fe1832 5486 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5487 {
30fe1832 5488 sorted_seg_map[j] = m;
252b5132 5489 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5490 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5491 not be done to the PT_NOTE section of a corefile, which may
5492 contain several pseudo-sections artificially created by bfd.
5493 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5494 if (m->count > 1
5495 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5496 && m->p_type == PT_NOTE))
48db3297
AM
5497 {
5498 for (i = 0; i < m->count; i++)
5499 m->sections[i]->target_index = i;
5500 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5501 elf_sort_sections);
5502 }
30fe1832
AM
5503 }
5504 if (alloc > 1)
5505 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5506 elf_sort_segments);
5507
5508 maxpagesize = 1;
5509 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5510 {
5511 if (link_info != NULL)
5512 maxpagesize = link_info->maxpagesize;
5513 else
5514 maxpagesize = bed->maxpagesize;
5515 }
30fe1832
AM
5516
5517 /* Sections must map to file offsets past the ELF file header. */
5518 off = bed->s->sizeof_ehdr;
5519 /* And if one of the PT_LOAD headers doesn't include the program
5520 headers then we'll be mapping program headers in the usual
5521 position after the ELF file header. */
5522 phdr_load_seg = NULL;
5523 for (j = 0; j < alloc; j++)
5524 {
5525 m = sorted_seg_map[j];
5526 if (m->p_type != PT_LOAD)
5527 break;
5528 if (m->includes_phdrs)
5529 {
5530 phdr_load_seg = m;
5531 break;
5532 }
5533 }
5534 if (phdr_load_seg == NULL)
5535 off += actual * bed->s->sizeof_phdr;
5536
5537 for (j = 0; j < alloc; j++)
5538 {
5539 asection **secpp;
502794d4 5540 bfd_vma off_adjust; /* Octets. */
0a1b45a2 5541 bool no_contents;
252b5132 5542
b301b248
AM
5543 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5544 number of sections with contents contributing to both p_filesz
5545 and p_memsz, followed by a number of sections with no contents
5546 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5547 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5548 m = sorted_seg_map[j];
5549 p = phdrs + m->idx;
252b5132 5550 p->p_type = m->p_type;
28a7f3e7 5551 p->p_flags = m->p_flags;
252b5132 5552
3f570048 5553 if (m->count == 0)
502794d4 5554 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5555 else
502794d4 5556 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5557
5558 if (m->p_paddr_valid)
5559 p->p_paddr = m->p_paddr;
5560 else if (m->count == 0)
5561 p->p_paddr = 0;
5562 else
502794d4 5563 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5564
5565 if (p->p_type == PT_LOAD
5566 && (abfd->flags & D_PAGED) != 0)
5567 {
5568 /* p_align in demand paged PT_LOAD segments effectively stores
5569 the maximum page size. When copying an executable with
5570 objcopy, we set m->p_align from the input file. Use this
5571 value for maxpagesize rather than bed->maxpagesize, which
5572 may be different. Note that we use maxpagesize for PT_TLS
5573 segment alignment later in this function, so we are relying
5574 on at least one PT_LOAD segment appearing before a PT_TLS
5575 segment. */
5576 if (m->p_align_valid)
5577 maxpagesize = m->p_align;
5578
5579 p->p_align = maxpagesize;
5580 }
3271a814
NS
5581 else if (m->p_align_valid)
5582 p->p_align = m->p_align;
e970b90a
DJ
5583 else if (m->count == 0)
5584 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5585
5586 if (m == phdr_load_seg)
5587 {
5588 if (!m->includes_filehdr)
5589 p->p_offset = off;
5590 off += actual * bed->s->sizeof_phdr;
5591 }
3f570048 5592
0a1b45a2 5593 no_contents = false;
bf988460 5594 off_adjust = 0;
252b5132 5595 if (p->p_type == PT_LOAD
b301b248 5596 && m->count > 0)
252b5132 5597 {
66631823 5598 bfd_size_type align; /* Bytes. */
a49e53ed 5599 unsigned int align_power = 0;
b301b248 5600
3271a814
NS
5601 if (m->p_align_valid)
5602 align = p->p_align;
5603 else
252b5132 5604 {
3271a814
NS
5605 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5606 {
5607 unsigned int secalign;
08a40648 5608
fd361982 5609 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5610 if (secalign > align_power)
5611 align_power = secalign;
5612 }
5613 align = (bfd_size_type) 1 << align_power;
5614 if (align < maxpagesize)
5615 align = maxpagesize;
b301b248 5616 }
252b5132 5617
02bf8d82
AM
5618 for (i = 0; i < m->count; i++)
5619 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5620 /* If we aren't making room for this section, then
5621 it must be SHT_NOBITS regardless of what we've
5622 set via struct bfd_elf_special_section. */
5623 elf_section_type (m->sections[i]) = SHT_NOBITS;
5624
bf988460 5625 /* Find out whether this segment contains any loadable
aea274d3 5626 sections. */
0a1b45a2 5627 no_contents = true;
aea274d3
AM
5628 for (i = 0; i < m->count; i++)
5629 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5630 {
0a1b45a2 5631 no_contents = false;
aea274d3
AM
5632 break;
5633 }
bf988460 5634
66631823 5635 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5636
5637 /* Broken hardware and/or kernel require that files do not
5638 map the same page with different permissions on some hppa
5639 processors. */
30fe1832
AM
5640 if (j != 0
5641 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5642 && bed->no_page_alias
5643 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5644 && ((off & -maxpagesize)
5645 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5646 off_adjust += maxpagesize;
bf988460
AM
5647 off += off_adjust;
5648 if (no_contents)
5649 {
5650 /* We shouldn't need to align the segment on disk since
5651 the segment doesn't need file space, but the gABI
5652 arguably requires the alignment and glibc ld.so
5653 checks it. So to comply with the alignment
5654 requirement but not waste file space, we adjust
5655 p_offset for just this segment. (OFF_ADJUST is
5656 subtracted from OFF later.) This may put p_offset
5657 past the end of file, but that shouldn't matter. */
5658 }
5659 else
5660 off_adjust = 0;
252b5132 5661 }
b1a6d0b1
NC
5662 /* Make sure the .dynamic section is the first section in the
5663 PT_DYNAMIC segment. */
5664 else if (p->p_type == PT_DYNAMIC
5665 && m->count > 1
5666 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5667 {
5668 _bfd_error_handler
871b3ab2 5669 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5670 " is not the .dynamic section"),
b301b248 5671 abfd);
b1a6d0b1 5672 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5673 return false;
b1a6d0b1 5674 }
3f001e84
JK
5675 /* Set the note section type to SHT_NOTE. */
5676 else if (p->p_type == PT_NOTE)
5677 for (i = 0; i < m->count; i++)
5678 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5679
252b5132
RH
5680 if (m->includes_filehdr)
5681 {
bf988460 5682 if (!m->p_flags_valid)
252b5132 5683 p->p_flags |= PF_R;
252b5132
RH
5684 p->p_filesz = bed->s->sizeof_ehdr;
5685 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5686 if (p->p_type == PT_LOAD)
252b5132 5687 {
30fe1832 5688 if (m->count > 0)
252b5132 5689 {
30fe1832
AM
5690 if (p->p_vaddr < (bfd_vma) off
5691 || (!m->p_paddr_valid
5692 && p->p_paddr < (bfd_vma) off))
5693 {
5694 _bfd_error_handler
5695 (_("%pB: not enough room for program headers,"
5696 " try linking with -N"),
5697 abfd);
5698 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5699 return false;
30fe1832
AM
5700 }
5701 p->p_vaddr -= off;
5702 if (!m->p_paddr_valid)
5703 p->p_paddr -= off;
252b5132 5704 }
30fe1832
AM
5705 }
5706 else if (sorted_seg_map[0]->includes_filehdr)
5707 {
5708 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5709 p->p_vaddr = filehdr->p_vaddr;
bf988460 5710 if (!m->p_paddr_valid)
30fe1832 5711 p->p_paddr = filehdr->p_paddr;
252b5132 5712 }
252b5132
RH
5713 }
5714
5715 if (m->includes_phdrs)
5716 {
bf988460 5717 if (!m->p_flags_valid)
252b5132 5718 p->p_flags |= PF_R;
30fe1832
AM
5719 p->p_filesz += actual * bed->s->sizeof_phdr;
5720 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5721 if (!m->includes_filehdr)
252b5132 5722 {
30fe1832 5723 if (p->p_type == PT_LOAD)
252b5132 5724 {
30fe1832
AM
5725 elf_elfheader (abfd)->e_phoff = p->p_offset;
5726 if (m->count > 0)
5727 {
5728 p->p_vaddr -= off - p->p_offset;
5729 if (!m->p_paddr_valid)
5730 p->p_paddr -= off - p->p_offset;
5731 }
5732 }
5733 else if (phdr_load_seg != NULL)
5734 {
5735 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5736 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5737 if (phdr_load_seg->includes_filehdr)
5738 phdr_off = bed->s->sizeof_ehdr;
5739 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5740 if (!m->p_paddr_valid)
30fe1832
AM
5741 p->p_paddr = phdr->p_paddr + phdr_off;
5742 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5743 }
30fe1832
AM
5744 else
5745 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5746 }
252b5132
RH
5747 }
5748
5749 if (p->p_type == PT_LOAD
5750 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5751 {
bf988460 5752 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5753 {
5754 p->p_offset = off;
5755 if (no_contents)
67641dd3
AM
5756 {
5757 /* Put meaningless p_offset for PT_LOAD segments
5758 without file contents somewhere within the first
5759 page, in an attempt to not point past EOF. */
5760 bfd_size_type align = maxpagesize;
5761 if (align < p->p_align)
5762 align = p->p_align;
5763 if (align < 1)
5764 align = 1;
5765 p->p_offset = off % align;
5766 }
0bc3450e 5767 }
252b5132
RH
5768 else
5769 {
502794d4 5770 file_ptr adjust; /* Octets. */
252b5132
RH
5771
5772 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5773 if (!no_contents)
5774 p->p_filesz += adjust;
252b5132
RH
5775 p->p_memsz += adjust;
5776 }
5777 }
5778
1ea63fd2
AM
5779 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5780 maps. Set filepos for sections in PT_LOAD segments, and in
5781 core files, for sections in PT_NOTE segments.
5782 assign_file_positions_for_non_load_sections will set filepos
5783 for other sections and update p_filesz for other segments. */
252b5132
RH
5784 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5785 {
5786 asection *sec;
252b5132 5787 bfd_size_type align;
627b32bc 5788 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5789
5790 sec = *secpp;
02bf8d82 5791 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5792 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5793
88967714
AM
5794 if ((p->p_type == PT_LOAD
5795 || p->p_type == PT_TLS)
5796 && (this_hdr->sh_type != SHT_NOBITS
5797 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5798 && ((this_hdr->sh_flags & SHF_TLS) == 0
5799 || p->p_type == PT_TLS))))
252b5132 5800 {
502794d4
CE
5801 bfd_vma p_start = p->p_paddr; /* Octets. */
5802 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5803 bfd_vma s_start = sec->lma * opb; /* Octets. */
5804 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5805
a2d1e028
L
5806 if (adjust != 0
5807 && (s_start < p_end
5808 || p_end < p_start))
252b5132 5809 {
4eca0228 5810 _bfd_error_handler
695344c0 5811 /* xgettext:c-format */
2dcf00ce 5812 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5813 abfd, sec, (uint64_t) s_start / opb,
5814 (uint64_t) p_end / opb);
88967714 5815 adjust = 0;
502794d4 5816 sec->lma = p_end / opb;
1cfb7d1e 5817 }
3ac9b6c9 5818 p->p_memsz += adjust;
1cfb7d1e 5819
d16e3d2e 5820 if (p->p_type == PT_LOAD)
88967714 5821 {
d16e3d2e 5822 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5823 {
d16e3d2e 5824 off_adjust = 0;
30fe1832
AM
5825 if (p->p_filesz + adjust < p->p_memsz)
5826 {
5827 /* We have a PROGBITS section following NOBITS ones.
5828 Allocate file space for the NOBITS section(s) and
5829 zero it. */
5830 adjust = p->p_memsz - p->p_filesz;
5831 if (!write_zeros (abfd, off, adjust))
0a1b45a2 5832 return false;
30fe1832 5833 }
d16e3d2e
AM
5834 }
5835 /* We only adjust sh_offset in SHT_NOBITS sections
5836 as would seem proper for their address when the
5837 section is first in the segment. sh_offset
5838 doesn't really have any significance for
5839 SHT_NOBITS anyway, apart from a notional position
5840 relative to other sections. Historically we
5841 didn't bother with adjusting sh_offset and some
5842 programs depend on it not being adjusted. See
5843 pr12921 and pr25662. */
5844 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5845 {
30fe1832 5846 off += adjust;
d16e3d2e
AM
5847 if (this_hdr->sh_type == SHT_NOBITS)
5848 off_adjust += adjust;
32812159 5849 }
252b5132 5850 }
d16e3d2e
AM
5851 if (this_hdr->sh_type != SHT_NOBITS)
5852 p->p_filesz += adjust;
252b5132
RH
5853 }
5854
5855 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5856 {
b301b248
AM
5857 /* The section at i == 0 is the one that actually contains
5858 everything. */
4a938328
MS
5859 if (i == 0)
5860 {
627b32bc 5861 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5862 off += this_hdr->sh_size;
5863 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5864 p->p_memsz = 0;
5865 p->p_align = 1;
252b5132 5866 }
4a938328 5867 else
252b5132 5868 {
b301b248 5869 /* The rest are fake sections that shouldn't be written. */
252b5132 5870 sec->filepos = 0;
eea6121a 5871 sec->size = 0;
b301b248
AM
5872 sec->flags = 0;
5873 continue;
252b5132 5874 }
252b5132
RH
5875 }
5876 else
5877 {
1e951488 5878 if (p->p_type == PT_LOAD)
b301b248 5879 {
1e951488
AM
5880 this_hdr->sh_offset = sec->filepos = off;
5881 if (this_hdr->sh_type != SHT_NOBITS)
5882 off += this_hdr->sh_size;
5883 }
5884 else if (this_hdr->sh_type == SHT_NOBITS
5885 && (this_hdr->sh_flags & SHF_TLS) != 0
5886 && this_hdr->sh_offset == 0)
5887 {
5888 /* This is a .tbss section that didn't get a PT_LOAD.
5889 (See _bfd_elf_map_sections_to_segments "Create a
5890 final PT_LOAD".) Set sh_offset to the value it
5891 would have if we had created a zero p_filesz and
5892 p_memsz PT_LOAD header for the section. This
5893 also makes the PT_TLS header have the same
5894 p_offset value. */
5895 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5896 off, align);
5897 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5898 }
252b5132 5899
02bf8d82 5900 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5901 {
6a3cd2b4 5902 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5903 /* A load section without SHF_ALLOC is something like
5904 a note section in a PT_NOTE segment. These take
5905 file space but are not loaded into memory. */
5906 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5907 p->p_memsz += this_hdr->sh_size;
b301b248 5908 }
6a3cd2b4 5909 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5910 {
6a3cd2b4
AM
5911 if (p->p_type == PT_TLS)
5912 p->p_memsz += this_hdr->sh_size;
5913
5914 /* .tbss is special. It doesn't contribute to p_memsz of
5915 normal segments. */
5916 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5917 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5918 }
5919
b10a8ae0
L
5920 if (align > p->p_align
5921 && !m->p_align_valid
5922 && (p->p_type != PT_LOAD
5923 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5924 p->p_align = align;
5925 }
5926
bf988460 5927 if (!m->p_flags_valid)
252b5132
RH
5928 {
5929 p->p_flags |= PF_R;
02bf8d82 5930 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5931 p->p_flags |= PF_X;
02bf8d82 5932 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5933 p->p_flags |= PF_W;
5934 }
5935 }
43a8475c 5936
bf988460 5937 off -= off_adjust;
0920dee7 5938
30fe1832
AM
5939 /* PR ld/20815 - Check that the program header segment, if
5940 present, will be loaded into memory. */
5941 if (p->p_type == PT_PHDR
5942 && phdr_load_seg == NULL
5943 && !(bed->elf_backend_allow_non_load_phdr != NULL
5944 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5945 {
5946 /* The fix for this error is usually to edit the linker script being
5947 used and set up the program headers manually. Either that or
5948 leave room for the headers at the start of the SECTIONS. */
5949 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5950 " by LOAD segment"),
5951 abfd);
7b3c2715 5952 if (link_info == NULL)
0a1b45a2 5953 return false;
7b3c2715
AM
5954 /* Arrange for the linker to exit with an error, deleting
5955 the output file unless --noinhibit-exec is given. */
5956 link_info->callbacks->info ("%X");
30fe1832
AM
5957 }
5958
7c928300
AM
5959 /* Check that all sections are in a PT_LOAD segment.
5960 Don't check funky gdb generated core files. */
5961 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553 5962 {
0a1b45a2 5963 bool check_vma = true;
9a83a553
AM
5964
5965 for (i = 1; i < m->count; i++)
5966 if (m->sections[i]->vma == m->sections[i - 1]->vma
5967 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5968 ->this_hdr), p) != 0
5969 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5970 ->this_hdr), p) != 0)
0920dee7 5971 {
9a83a553 5972 /* Looks like we have overlays packed into the segment. */
0a1b45a2 5973 check_vma = false;
9a83a553 5974 break;
0920dee7 5975 }
9a83a553
AM
5976
5977 for (i = 0; i < m->count; i++)
5978 {
5979 Elf_Internal_Shdr *this_hdr;
5980 asection *sec;
5981
5982 sec = m->sections[i];
5983 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5984 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5985 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5986 {
4eca0228 5987 _bfd_error_handler
695344c0 5988 /* xgettext:c-format */
871b3ab2 5989 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5990 abfd, sec, j);
5991 print_segment_map (m);
5992 }
5993 }
5994 }
252b5132
RH
5995 }
5996
12bd6957 5997 elf_next_file_pos (abfd) = off;
30fe1832
AM
5998
5999 if (link_info != NULL
6000 && phdr_load_seg != NULL
6001 && phdr_load_seg->includes_filehdr)
6002 {
6003 /* There is a segment that contains both the file headers and the
6004 program headers, so provide a symbol __ehdr_start pointing there.
6005 A program can use this to examine itself robustly. */
6006
6007 struct elf_link_hash_entry *hash
6008 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
0a1b45a2 6009 false, false, true);
30fe1832
AM
6010 /* If the symbol was referenced and not defined, define it. */
6011 if (hash != NULL
6012 && (hash->root.type == bfd_link_hash_new
6013 || hash->root.type == bfd_link_hash_undefined
6014 || hash->root.type == bfd_link_hash_undefweak
6015 || hash->root.type == bfd_link_hash_common))
6016 {
6017 asection *s = NULL;
66631823 6018 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6019
6020 if (phdr_load_seg->count != 0)
6021 /* The segment contains sections, so use the first one. */
6022 s = phdr_load_seg->sections[0];
6023 else
6024 /* Use the first (i.e. lowest-addressed) section in any segment. */
6025 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6026 if (m->p_type == PT_LOAD && m->count != 0)
6027 {
6028 s = m->sections[0];
6029 break;
6030 }
6031
6032 if (s != NULL)
6033 {
6034 hash->root.u.def.value = filehdr_vaddr - s->vma;
6035 hash->root.u.def.section = s;
6036 }
6037 else
6038 {
6039 hash->root.u.def.value = filehdr_vaddr;
6040 hash->root.u.def.section = bfd_abs_section_ptr;
6041 }
6042
6043 hash->root.type = bfd_link_hash_defined;
6044 hash->def_regular = 1;
6045 hash->non_elf = 0;
6046 }
6047 }
6048
0a1b45a2 6049 return true;
f3520d2f
AM
6050}
6051
1faa385f
NC
6052/* Determine if a bfd is a debuginfo file. Unfortunately there
6053 is no defined method for detecting such files, so we have to
6054 use heuristics instead. */
6055
0a1b45a2 6056bool
1faa385f
NC
6057is_debuginfo_file (bfd *abfd)
6058{
6059 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
0a1b45a2 6060 return false;
1faa385f
NC
6061
6062 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6063 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6064 Elf_Internal_Shdr **headerp;
6065
6066 for (headerp = start_headers; headerp < end_headers; headerp ++)
6067 {
6068 Elf_Internal_Shdr *header = * headerp;
6069
6070 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6071 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6072 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6073 && header->sh_type != SHT_NOBITS
6074 && header->sh_type != SHT_NOTE)
0a1b45a2 6075 return false;
1faa385f
NC
6076 }
6077
0a1b45a2 6078 return true;
1faa385f
NC
6079}
6080
1ff6de03
NA
6081/* Assign file positions for the other sections, except for compressed debugging
6082 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f 6083
0a1b45a2 6084static bool
f3520d2f
AM
6085assign_file_positions_for_non_load_sections (bfd *abfd,
6086 struct bfd_link_info *link_info)
6087{
6088 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6089 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6090 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6091 Elf_Internal_Phdr *phdrs;
6092 Elf_Internal_Phdr *p;
6093 struct elf_segment_map *m;
f3520d2f 6094 file_ptr off;
66631823 6095 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6096 bfd_vma maxpagesize;
f3520d2f 6097
c410035d
AM
6098 if (link_info != NULL)
6099 maxpagesize = link_info->maxpagesize;
6100 else
6101 maxpagesize = bed->maxpagesize;
5c182d5f 6102 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6103 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6104 off = elf_next_file_pos (abfd);
e06efbf1 6105 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6106 {
5c182d5f
AM
6107 Elf_Internal_Shdr *hdr;
6108
6109 hdr = *hdrpp;
6110 if (hdr->bfd_section != NULL
252e386e
AM
6111 && (hdr->bfd_section->filepos != 0
6112 || (hdr->sh_type == SHT_NOBITS
6113 && hdr->contents == NULL)))
627b32bc 6114 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6115 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6116 {
1faa385f
NC
6117 if (hdr->sh_size != 0
6118 /* PR 24717 - debuginfo files are known to be not strictly
6119 compliant with the ELF standard. In particular they often
6120 have .note.gnu.property sections that are outside of any
6121 loadable segment. This is not a problem for such files,
6122 so do not warn about them. */
6123 && ! is_debuginfo_file (abfd))
4eca0228 6124 _bfd_error_handler
695344c0 6125 /* xgettext:c-format */
871b3ab2 6126 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6127 abfd,
6128 (hdr->bfd_section == NULL
6129 ? "*unknown*"
6130 : hdr->bfd_section->name));
3ba71138
L
6131 /* We don't need to page align empty sections. */
6132 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f 6133 off += vma_page_aligned_bias (hdr->sh_addr, off,
c410035d 6134 maxpagesize);
5c182d5f
AM
6135 else
6136 off += vma_page_aligned_bias (hdr->sh_addr, off,
6137 hdr->sh_addralign);
6138 off = _bfd_elf_assign_file_position_for_section (hdr, off,
0a1b45a2 6139 false);
5c182d5f
AM
6140 }
6141 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6142 && hdr->bfd_section == NULL)
1ff6de03
NA
6143 /* We don't know the offset of these sections yet: their size has
6144 not been decided. */
0ce398f1 6145 || (hdr->bfd_section != NULL
1ff6de03
NA
6146 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6147 || (bfd_section_is_ctf (hdr->bfd_section)
6148 && abfd->is_linker_output)))
12bd6957 6149 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6150 || (elf_symtab_shndx_list (abfd) != NULL
6151 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6152 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6153 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6154 hdr->sh_offset = -1;
6155 else
0a1b45a2 6156 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
5c182d5f 6157 }
30fe1832 6158 elf_next_file_pos (abfd) = off;
5c182d5f 6159
252b5132
RH
6160 /* Now that we have set the section file positions, we can set up
6161 the file positions for the non PT_LOAD segments. */
f3520d2f 6162 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6163 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6164 {
129af99f 6165 if (p->p_type == PT_GNU_RELRO)
252b5132 6166 {
66631823 6167 bfd_vma start, end; /* Bytes. */
0a1b45a2 6168 bool ok;
1ea63fd2 6169
129af99f 6170 if (link_info != NULL)
8c37241b 6171 {
129af99f 6172 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6173 in link_info. Note that there may be padding between
6174 relro_start and the first RELRO section. */
6175 start = link_info->relro_start;
6176 end = link_info->relro_end;
6177 }
6178 else if (m->count != 0)
6179 {
6180 if (!m->p_size_valid)
6181 abort ();
6182 start = m->sections[0]->vma;
66631823 6183 end = start + m->p_size / opb;
f2731e0c
AM
6184 }
6185 else
6186 {
6187 start = 0;
6188 end = 0;
6189 }
6190
0a1b45a2 6191 ok = false;
f2731e0c
AM
6192 if (start < end)
6193 {
6194 struct elf_segment_map *lm;
6195 const Elf_Internal_Phdr *lp;
6196 unsigned int i;
6197
6198 /* Find a LOAD segment containing a section in the RELRO
6199 segment. */
12bd6957 6200 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6201 lm != NULL;
6202 lm = lm->next, lp++)
8c37241b
JJ
6203 {
6204 if (lp->p_type == PT_LOAD
3146fac4 6205 && lm->count != 0
dbc88fc1
AM
6206 && (lm->sections[lm->count - 1]->vma
6207 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6208 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6209 : 0)) > start
f2731e0c 6210 && lm->sections[0]->vma < end)
8c37241b
JJ
6211 break;
6212 }
f2731e0c 6213
01f7e10c 6214 if (lm != NULL)
129af99f 6215 {
01f7e10c
AM
6216 /* Find the section starting the RELRO segment. */
6217 for (i = 0; i < lm->count; i++)
6218 {
6219 asection *s = lm->sections[i];
6220 if (s->vma >= start
6221 && s->vma < end
6222 && s->size != 0)
6223 break;
6224 }
6225
6226 if (i < lm->count)
6227 {
502794d4
CE
6228 p->p_vaddr = lm->sections[i]->vma * opb;
6229 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6230 p->p_offset = lm->sections[i]->filepos;
66631823 6231 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6232 p->p_filesz = p->p_memsz;
6233
6234 /* The RELRO segment typically ends a few bytes
6235 into .got.plt but other layouts are possible.
6236 In cases where the end does not match any
6237 loaded section (for instance is in file
6238 padding), trim p_filesz back to correspond to
6239 the end of loaded section contents. */
6240 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6241 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6242
6243 /* Preserve the alignment and flags if they are
6244 valid. The gold linker generates RW/4 for
6245 the PT_GNU_RELRO section. It is better for
6246 objcopy/strip to honor these attributes
6247 otherwise gdb will choke when using separate
6248 debug files. */
6249 if (!m->p_align_valid)
6250 p->p_align = 1;
6251 if (!m->p_flags_valid)
6252 p->p_flags = PF_R;
0a1b45a2 6253 ok = true;
01f7e10c 6254 }
129af99f 6255 }
b84a33b5 6256 }
01f7e10c
AM
6257 if (link_info != NULL)
6258 BFD_ASSERT (ok);
6259 if (!ok)
6260 memset (p, 0, sizeof *p);
129af99f 6261 }
04c3a755
NS
6262 else if (p->p_type == PT_GNU_STACK)
6263 {
6264 if (m->p_size_valid)
6265 p->p_memsz = m->p_size;
6266 }
129af99f
AS
6267 else if (m->count != 0)
6268 {
e06efbf1 6269 unsigned int i;
1a9ccd70 6270
129af99f
AS
6271 if (p->p_type != PT_LOAD
6272 && (p->p_type != PT_NOTE
6273 || bfd_get_format (abfd) != bfd_core))
6274 {
1a9ccd70
NC
6275 /* A user specified segment layout may include a PHDR
6276 segment that overlaps with a LOAD segment... */
6277 if (p->p_type == PT_PHDR)
6278 {
6279 m->count = 0;
6280 continue;
6281 }
6282
c86934ce
NC
6283 if (m->includes_filehdr || m->includes_phdrs)
6284 {
b1fa9dd6 6285 /* PR 17512: file: 2195325e. */
4eca0228 6286 _bfd_error_handler
871b3ab2 6287 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6288 "and/or program header"),
6289 abfd, (int) (p - phdrs));
0a1b45a2 6290 return false;
c86934ce 6291 }
129af99f 6292
86b2281f 6293 p->p_filesz = 0;
129af99f 6294 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6295 for (i = m->count; i-- != 0;)
6296 {
6297 asection *sect = m->sections[i];
6298 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6299 if (hdr->sh_type != SHT_NOBITS)
6300 {
6301 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6302 + hdr->sh_size);
9917b559
L
6303 /* NB: p_memsz of the loadable PT_NOTE segment
6304 should be the same as p_filesz. */
6305 if (p->p_type == PT_NOTE
6306 && (hdr->sh_flags & SHF_ALLOC) != 0)
6307 p->p_memsz = p->p_filesz;
86b2281f
AM
6308 break;
6309 }
6310 }
129af99f
AS
6311 }
6312 }
252b5132
RH
6313 }
6314
0a1b45a2 6315 return true;
252b5132
RH
6316}
6317
6a40cf0c
NC
6318static elf_section_list *
6319find_section_in_list (unsigned int i, elf_section_list * list)
6320{
6321 for (;list != NULL; list = list->next)
6322 if (list->ndx == i)
6323 break;
6324 return list;
6325}
6326
252b5132
RH
6327/* Work out the file positions of all the sections. This is called by
6328 _bfd_elf_compute_section_file_positions. All the section sizes and
6329 VMAs must be known before this is called.
6330
e0638f70 6331 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6332 "side-channel" data attached to a section to which they apply, and those that
6333 bfd doesn't process as relocations. The latter sort are stored in a normal
6334 bfd section by bfd_section_from_shdr. We don't consider the former sort
6335 here, unless they form part of the loadable image. Reloc sections not
6336 assigned here (and compressed debugging sections and CTF sections which
6337 nothing else in the file can rely upon) will be handled later by
e0638f70 6338 assign_file_positions_for_relocs.
252b5132
RH
6339
6340 We also don't set the positions of the .symtab and .strtab here. */
6341
0a1b45a2 6342static bool
c84fca4d
AO
6343assign_file_positions_except_relocs (bfd *abfd,
6344 struct bfd_link_info *link_info)
252b5132 6345{
5c182d5f
AM
6346 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6347 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6348 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6349 unsigned int alloc;
252b5132
RH
6350
6351 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6352 && bfd_get_format (abfd) != bfd_core)
6353 {
5c182d5f
AM
6354 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6355 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6356 Elf_Internal_Shdr **hdrpp;
6357 unsigned int i;
a485e98e 6358 file_ptr off;
252b5132
RH
6359
6360 /* Start after the ELF header. */
6361 off = i_ehdrp->e_ehsize;
6362
6363 /* We are not creating an executable, which means that we are
6364 not creating a program header, and that the actual order of
6365 the sections in the file is unimportant. */
9ad5cbcf 6366 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6367 {
6368 Elf_Internal_Shdr *hdr;
6369
6370 hdr = *hdrpp;
e0638f70
AM
6371 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6372 && hdr->bfd_section == NULL)
1ff6de03
NA
6373 /* Do not assign offsets for these sections yet: we don't know
6374 their sizes. */
0ce398f1 6375 || (hdr->bfd_section != NULL
1ff6de03
NA
6376 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6377 || (bfd_section_is_ctf (hdr->bfd_section)
6378 && abfd->is_linker_output)))
12bd6957 6379 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6380 || (elf_symtab_shndx_list (abfd) != NULL
6381 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6382 || i == elf_strtab_sec (abfd)
6383 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6384 {
6385 hdr->sh_offset = -1;
252b5132 6386 }
9ad5cbcf 6387 else
0a1b45a2 6388 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 6389 }
a485e98e
AM
6390
6391 elf_next_file_pos (abfd) = off;
6d6c25c8 6392 elf_program_header_size (abfd) = 0;
252b5132
RH
6393 }
6394 else
6395 {
252b5132 6396 /* Assign file positions for the loaded sections based on the
08a40648 6397 assignment of sections to segments. */
f3520d2f 6398 if (!assign_file_positions_for_load_sections (abfd, link_info))
0a1b45a2 6399 return false;
f3520d2f
AM
6400
6401 /* And for non-load sections. */
6402 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
0a1b45a2 6403 return false;
6d6c25c8 6404 }
f3520d2f 6405
6d6c25c8 6406 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
0a1b45a2 6407 return false;
1a9ccd70 6408
6d6c25c8
AM
6409 /* Write out the program headers. */
6410 alloc = i_ehdrp->e_phnum;
6411 if (alloc != 0)
6412 {
30fe1832 6413 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857 6414 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
0a1b45a2 6415 return false;
252b5132
RH
6416 }
6417
0a1b45a2 6418 return true;
252b5132
RH
6419}
6420
0a1b45a2 6421bool
ed7e9d0b
AM
6422_bfd_elf_init_file_header (bfd *abfd,
6423 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6424{
3d540e93 6425 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6426 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6427 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6428
6429 i_ehdrp = elf_elfheader (abfd);
252b5132 6430
2b0f7ef9 6431 shstrtab = _bfd_elf_strtab_init ();
252b5132 6432 if (shstrtab == NULL)
0a1b45a2 6433 return false;
252b5132
RH
6434
6435 elf_shstrtab (abfd) = shstrtab;
6436
6437 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6438 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6439 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6440 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6441
6442 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6443 i_ehdrp->e_ident[EI_DATA] =
6444 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6445 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6446
252b5132
RH
6447 if ((abfd->flags & DYNAMIC) != 0)
6448 i_ehdrp->e_type = ET_DYN;
6449 else if ((abfd->flags & EXEC_P) != 0)
6450 i_ehdrp->e_type = ET_EXEC;
6451 else if (bfd_get_format (abfd) == bfd_core)
6452 i_ehdrp->e_type = ET_CORE;
6453 else
6454 i_ehdrp->e_type = ET_REL;
6455
6456 switch (bfd_get_arch (abfd))
6457 {
6458 case bfd_arch_unknown:
6459 i_ehdrp->e_machine = EM_NONE;
6460 break;
aa4f99bb
AO
6461
6462 /* There used to be a long list of cases here, each one setting
6463 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6464 in the corresponding bfd definition. To avoid duplication,
6465 the switch was removed. Machines that need special handling
6466 can generally do it in elf_backend_final_write_processing(),
6467 unless they need the information earlier than the final write.
6468 Such need can generally be supplied by replacing the tests for
6469 e_machine with the conditions used to determine it. */
252b5132 6470 default:
9c5bfbb7
AM
6471 i_ehdrp->e_machine = bed->elf_machine_code;
6472 }
aa4f99bb 6473
252b5132
RH
6474 i_ehdrp->e_version = bed->s->ev_current;
6475 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6476
c044fabd 6477 /* No program header, for now. */
252b5132
RH
6478 i_ehdrp->e_phoff = 0;
6479 i_ehdrp->e_phentsize = 0;
6480 i_ehdrp->e_phnum = 0;
6481
c044fabd 6482 /* Each bfd section is section header entry. */
252b5132
RH
6483 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6484 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6485
252b5132 6486 elf_tdata (abfd)->symtab_hdr.sh_name =
0a1b45a2 6487 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 6488 elf_tdata (abfd)->strtab_hdr.sh_name =
0a1b45a2 6489 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 6490 elf_tdata (abfd)->shstrtab_hdr.sh_name =
0a1b45a2 6491 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132 6492 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6493 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6494 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
0a1b45a2 6495 return false;
252b5132 6496
0a1b45a2 6497 return true;
252b5132
RH
6498}
6499
6d6c25c8
AM
6500/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6501
6502 FIXME: We used to have code here to sort the PT_LOAD segments into
6503 ascending order, as per the ELF spec. But this breaks some programs,
6504 including the Linux kernel. But really either the spec should be
6505 changed or the programs updated. */
6506
0a1b45a2 6507bool
6d6c25c8
AM
6508_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6509{
6510 if (link_info != NULL && bfd_link_pie (link_info))
6511 {
6512 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6513 unsigned int num_segments = i_ehdrp->e_phnum;
6514 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6515 Elf_Internal_Phdr *segment = tdata->phdr;
6516 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6517
6518 /* Find the lowest p_vaddr in PT_LOAD segments. */
6519 bfd_vma p_vaddr = (bfd_vma) -1;
6520 for (; segment < end_segment; segment++)
6521 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6522 p_vaddr = segment->p_vaddr;
6523
6524 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6525 segments is non-zero. */
6526 if (p_vaddr)
6527 i_ehdrp->e_type = ET_EXEC;
6528 }
0a1b45a2 6529 return true;
6d6c25c8
AM
6530}
6531
252b5132 6532/* Assign file positions for all the reloc sections which are not part
a485e98e 6533 of the loadable file image, and the file position of section headers. */
252b5132 6534
0a1b45a2 6535static bool
0ce398f1 6536_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6537{
6538 file_ptr off;
e06efbf1 6539 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6540 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6541 Elf_Internal_Ehdr *i_ehdrp;
6542 const struct elf_backend_data *bed;
252b5132 6543
12bd6957 6544 off = elf_next_file_pos (abfd);
252b5132 6545
e06efbf1
L
6546 shdrpp = elf_elfsections (abfd);
6547 end_shdrpp = shdrpp + elf_numsections (abfd);
6548 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6549 {
252b5132 6550 shdrp = *shdrpp;
0ce398f1
L
6551 if (shdrp->sh_offset == -1)
6552 {
3e19fb8f 6553 asection *sec = shdrp->bfd_section;
0a1b45a2
AM
6554 bool is_rel = (shdrp->sh_type == SHT_REL
6555 || shdrp->sh_type == SHT_RELA);
6556 bool is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6557 if (is_rel
1ff6de03 6558 || is_ctf
3e19fb8f 6559 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6560 {
1ff6de03 6561 if (!is_rel && !is_ctf)
0ce398f1 6562 {
3e19fb8f
L
6563 const char *name = sec->name;
6564 struct bfd_elf_section_data *d;
6565
0ce398f1 6566 /* Compress DWARF debug sections. */
3e19fb8f 6567 if (!bfd_compress_section (abfd, sec,
0ce398f1 6568 shdrp->contents))
0a1b45a2 6569 return false;
3e19fb8f
L
6570
6571 if (sec->compress_status == COMPRESS_SECTION_DONE
6572 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6573 {
6574 /* If section is compressed with zlib-gnu, convert
6575 section name from .debug_* to .zdebug_*. */
6576 char *new_name
6577 = convert_debug_to_zdebug (abfd, name);
6578 if (new_name == NULL)
0a1b45a2 6579 return false;
3e19fb8f
L
6580 name = new_name;
6581 }
dd905818 6582 /* Add section name to section name section. */
3e19fb8f
L
6583 if (shdrp->sh_name != (unsigned int) -1)
6584 abort ();
6585 shdrp->sh_name
6586 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 6587 name, false);
3e19fb8f
L
6588 d = elf_section_data (sec);
6589
dd905818 6590 /* Add reloc section name to section name section. */
3e19fb8f
L
6591 if (d->rel.hdr
6592 && !_bfd_elf_set_reloc_sh_name (abfd,
6593 d->rel.hdr,
0a1b45a2
AM
6594 name, false))
6595 return false;
3e19fb8f
L
6596 if (d->rela.hdr
6597 && !_bfd_elf_set_reloc_sh_name (abfd,
6598 d->rela.hdr,
0a1b45a2
AM
6599 name, true))
6600 return false;
3e19fb8f 6601
0ce398f1 6602 /* Update section size and contents. */
3e19fb8f
L
6603 shdrp->sh_size = sec->size;
6604 shdrp->contents = sec->contents;
0ce398f1
L
6605 shdrp->bfd_section->contents = NULL;
6606 }
1ff6de03
NA
6607 else if (is_ctf)
6608 {
6609 /* Update section size and contents. */
6610 shdrp->sh_size = sec->size;
6611 shdrp->contents = sec->contents;
6612 }
6613
0ce398f1
L
6614 off = _bfd_elf_assign_file_position_for_section (shdrp,
6615 off,
0a1b45a2 6616 true);
0ce398f1
L
6617 }
6618 }
252b5132
RH
6619 }
6620
3e19fb8f
L
6621 /* Place section name section after DWARF debug sections have been
6622 compressed. */
6623 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6624 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6625 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
0a1b45a2 6626 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3e19fb8f
L
6627
6628 /* Place the section headers. */
a485e98e
AM
6629 i_ehdrp = elf_elfheader (abfd);
6630 bed = get_elf_backend_data (abfd);
6631 off = align_file_position (off, 1 << bed->s->log_file_align);
6632 i_ehdrp->e_shoff = off;
6633 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6634 elf_next_file_pos (abfd) = off;
0ce398f1 6635
0a1b45a2 6636 return true;
252b5132
RH
6637}
6638
0a1b45a2 6639bool
217aa764 6640_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6641{
9c5bfbb7 6642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6643 Elf_Internal_Shdr **i_shdrp;
0a1b45a2 6644 bool failed;
9ad5cbcf 6645 unsigned int count, num_sec;
30e8ee25 6646 struct elf_obj_tdata *t;
252b5132
RH
6647
6648 if (! abfd->output_has_begun
217aa764 6649 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 6650 return false;
db727370
JL
6651 /* Do not rewrite ELF data when the BFD has been opened for update.
6652 abfd->output_has_begun was set to TRUE on opening, so creation of new
6653 sections, and modification of existing section sizes was restricted.
6654 This means the ELF header, program headers and section headers can't have
6655 changed.
6656 If the contents of any sections has been modified, then those changes have
6657 already been written to the BFD. */
6658 else if (abfd->direction == both_direction)
6659 {
6660 BFD_ASSERT (abfd->output_has_begun);
0a1b45a2 6661 return true;
db727370 6662 }
252b5132
RH
6663
6664 i_shdrp = elf_elfsections (abfd);
252b5132 6665
0a1b45a2 6666 failed = false;
252b5132
RH
6667 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6668 if (failed)
0a1b45a2 6669 return false;
252b5132 6670
0ce398f1 6671 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
0a1b45a2 6672 return false;
252b5132 6673
c044fabd 6674 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6675 num_sec = elf_numsections (abfd);
6676 for (count = 1; count < num_sec; count++)
252b5132 6677 {
3e19fb8f
L
6678 i_shdrp[count]->sh_name
6679 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6680 i_shdrp[count]->sh_name);
252b5132 6681 if (bed->elf_backend_section_processing)
75506100 6682 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
0a1b45a2 6683 return false;
252b5132
RH
6684 if (i_shdrp[count]->contents)
6685 {
dc810e39
AM
6686 bfd_size_type amt = i_shdrp[count]->sh_size;
6687
252b5132 6688 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6689 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
0a1b45a2 6690 return false;
252b5132
RH
6691 }
6692 }
6693
6694 /* Write out the section header names. */
30e8ee25 6695 t = elf_tdata (abfd);
26ae6d5e 6696 if (elf_shstrtab (abfd) != NULL
30e8ee25 6697 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6698 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
0a1b45a2 6699 return false;
252b5132 6700
cc364be6 6701 if (!(*bed->elf_backend_final_write_processing) (abfd))
0a1b45a2 6702 return false;
252b5132 6703
ff59fc36 6704 if (!bed->s->write_shdrs_and_ehdr (abfd))
0a1b45a2 6705 return false;
ff59fc36
RM
6706
6707 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6708 if (t->o->build_id.after_write_object_contents != NULL)
6709 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36 6710
0a1b45a2 6711 return true;
252b5132
RH
6712}
6713
0a1b45a2 6714bool
217aa764 6715_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6716{
c044fabd 6717 /* Hopefully this can be done just like an object file. */
252b5132
RH
6718 return _bfd_elf_write_object_contents (abfd);
6719}
c044fabd
KH
6720
6721/* Given a section, search the header to find them. */
6722
cb33740c 6723unsigned int
198beae2 6724_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6725{
9c5bfbb7 6726 const struct elf_backend_data *bed;
91d6fa6a 6727 unsigned int sec_index;
252b5132 6728
9ad5cbcf
AM
6729 if (elf_section_data (asect) != NULL
6730 && elf_section_data (asect)->this_idx != 0)
6731 return elf_section_data (asect)->this_idx;
6732
6733 if (bfd_is_abs_section (asect))
91d6fa6a 6734 sec_index = SHN_ABS;
af746e92 6735 else if (bfd_is_com_section (asect))
91d6fa6a 6736 sec_index = SHN_COMMON;
af746e92 6737 else if (bfd_is_und_section (asect))
91d6fa6a 6738 sec_index = SHN_UNDEF;
af746e92 6739 else
91d6fa6a 6740 sec_index = SHN_BAD;
252b5132 6741
af746e92 6742 bed = get_elf_backend_data (abfd);
252b5132
RH
6743 if (bed->elf_backend_section_from_bfd_section)
6744 {
91d6fa6a 6745 int retval = sec_index;
9ad5cbcf 6746
af746e92
AM
6747 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6748 return retval;
252b5132
RH
6749 }
6750
91d6fa6a 6751 if (sec_index == SHN_BAD)
af746e92 6752 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6753
91d6fa6a 6754 return sec_index;
252b5132
RH
6755}
6756
6757/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6758 on error. */
6759
6760int
217aa764 6761_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6762{
6763 asymbol *asym_ptr = *asym_ptr_ptr;
6764 int idx;
6765 flagword flags = asym_ptr->flags;
6766
6767 /* When gas creates relocations against local labels, it creates its
6768 own symbol for the section, but does put the symbol into the
6769 symbol chain, so udata is 0. When the linker is generating
6770 relocatable output, this section symbol may be for one of the
6771 input sections rather than the output section. */
6772 if (asym_ptr->udata.i == 0
6773 && (flags & BSF_SECTION_SYM)
6774 && asym_ptr->section)
6775 {
5372391b 6776 asection *sec;
252b5132
RH
6777 int indx;
6778
5372391b
AM
6779 sec = asym_ptr->section;
6780 if (sec->owner != abfd && sec->output_section != NULL)
6781 sec = sec->output_section;
6782 if (sec->owner == abfd
6783 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6784 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6785 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6786 }
6787
6788 idx = asym_ptr->udata.i;
6789
6790 if (idx == 0)
6791 {
6792 /* This case can occur when using --strip-symbol on a symbol
08a40648 6793 which is used in a relocation entry. */
4eca0228 6794 _bfd_error_handler
695344c0 6795 /* xgettext:c-format */
871b3ab2 6796 (_("%pB: symbol `%s' required but not present"),
d003868e 6797 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6798 bfd_set_error (bfd_error_no_symbols);
6799 return -1;
6800 }
6801
6802#if DEBUG & 4
6803 {
6804 fprintf (stderr,
cd9af601
AM
6805 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6806 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6807 fflush (stderr);
6808 }
6809#endif
6810
6811 return idx;
6812}
6813
84d1d650 6814/* Rewrite program header information. */
252b5132 6815
0a1b45a2 6816static bool
c410035d 6817rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6818{
b34976b6
AM
6819 Elf_Internal_Ehdr *iehdr;
6820 struct elf_segment_map *map;
6821 struct elf_segment_map *map_first;
6822 struct elf_segment_map **pointer_to_map;
6823 Elf_Internal_Phdr *segment;
6824 asection *section;
6825 unsigned int i;
6826 unsigned int num_segments;
0a1b45a2
AM
6827 bool phdr_included = false;
6828 bool p_paddr_valid;
b34976b6
AM
6829 struct elf_segment_map *phdr_adjust_seg = NULL;
6830 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6831 const struct elf_backend_data *bed;
502794d4 6832 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6833
caf47ea6 6834 bed = get_elf_backend_data (ibfd);
252b5132
RH
6835 iehdr = elf_elfheader (ibfd);
6836
bc67d8a6 6837 map_first = NULL;
c044fabd 6838 pointer_to_map = &map_first;
252b5132
RH
6839
6840 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6841
6842 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6843#define SEGMENT_END(segment, start) \
6844 (start + (segment->p_memsz > segment->p_filesz \
6845 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6846
eecdbe52
JJ
6847#define SECTION_SIZE(section, segment) \
6848 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6849 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6850 ? section->size : 0)
eecdbe52 6851
b34976b6 6852 /* Returns TRUE if the given section is contained within
bc67d8a6 6853 the given segment. VMA addresses are compared. */
502794d4
CE
6854#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6855 (section->vma * (opb) >= segment->p_vaddr \
6856 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6857 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6858
b34976b6 6859 /* Returns TRUE if the given section is contained within
bc67d8a6 6860 the given segment. LMA addresses are compared. */
502794d4
CE
6861#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6862 (section->lma * (opb) >= base \
6863 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6864 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6865 <= SEGMENT_END (segment, base)))
252b5132 6866
0efc80c8
L
6867 /* Handle PT_NOTE segment. */
6868#define IS_NOTE(p, s) \
aecc8f8a 6869 (p->p_type == PT_NOTE \
0efc80c8 6870 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6871 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6872 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6873 <= p->p_offset + p->p_filesz))
252b5132 6874
0efc80c8
L
6875 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6876 etc. */
6877#define IS_COREFILE_NOTE(p, s) \
6878 (IS_NOTE (p, s) \
6879 && bfd_get_format (ibfd) == bfd_core \
6880 && s->vma == 0 \
6881 && s->lma == 0)
6882
252b5132
RH
6883 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6884 linker, which generates a PT_INTERP section with p_vaddr and
6885 p_memsz set to 0. */
aecc8f8a
AM
6886#define IS_SOLARIS_PT_INTERP(p, s) \
6887 (p->p_vaddr == 0 \
6888 && p->p_paddr == 0 \
6889 && p->p_memsz == 0 \
6890 && p->p_filesz > 0 \
6891 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6892 && s->size > 0 \
aecc8f8a 6893 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6894 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6895 <= p->p_offset + p->p_filesz))
5c440b1e 6896
bc67d8a6
NC
6897 /* Decide if the given section should be included in the given segment.
6898 A section will be included if:
f5ffc919 6899 1. It is within the address space of the segment -- we use the LMA
08a40648 6900 if that is set for the segment and the VMA otherwise,
0efc80c8 6901 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6902 segment.
bc67d8a6 6903 3. There is an output section associated with it,
eecdbe52 6904 4. The section has not already been allocated to a previous segment.
2b05f1b7 6905 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6906 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6907 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6908 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6909 (with the possible exception of .dynamic). */
502794d4 6910#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6911 ((((segment->p_paddr \
502794d4
CE
6912 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6913 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6914 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6915 || IS_NOTE (segment, section)) \
2b05f1b7
L
6916 && segment->p_type != PT_GNU_STACK \
6917 && (segment->p_type != PT_TLS \
6918 || (section->flags & SEC_THREAD_LOCAL)) \
6919 && (segment->p_type == PT_LOAD \
6920 || segment->p_type == PT_TLS \
6921 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6922 && (segment->p_type != PT_DYNAMIC \
6923 || SECTION_SIZE (section, segment) > 0 \
6924 || (segment->p_paddr \
502794d4
CE
6925 ? segment->p_paddr != section->lma * (opb) \
6926 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6927 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6928 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6929
9f17e2a6
L
6930/* If the output section of a section in the input segment is NULL,
6931 it is removed from the corresponding output segment. */
502794d4
CE
6932#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6933 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6934 && section->output_section != NULL)
6935
b34976b6 6936 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6937#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6938 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6939
6940 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6941 their VMA address ranges and their LMA address ranges overlap.
6942 It is possible to have overlapping VMA ranges without overlapping LMA
6943 ranges. RedBoot images for example can have both .data and .bss mapped
6944 to the same VMA range, but with the .data section mapped to a different
6945 LMA. */
aecc8f8a 6946#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6947 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6948 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6949 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6950 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6951
6952 /* Initialise the segment mark field. */
6953 for (section = ibfd->sections; section != NULL; section = section->next)
0a1b45a2 6954 section->segment_mark = false;
bc67d8a6 6955
5c44b38e
AM
6956 /* The Solaris linker creates program headers in which all the
6957 p_paddr fields are zero. When we try to objcopy or strip such a
6958 file, we get confused. Check for this case, and if we find it
6959 don't set the p_paddr_valid fields. */
0a1b45a2 6960 p_paddr_valid = false;
5c44b38e
AM
6961 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6962 i < num_segments;
6963 i++, segment++)
6964 if (segment->p_paddr != 0)
6965 {
0a1b45a2 6966 p_paddr_valid = true;
5c44b38e
AM
6967 break;
6968 }
6969
252b5132 6970 /* Scan through the segments specified in the program header
bc67d8a6 6971 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6972 in the loadable segments. These can be created by weird
aecc8f8a 6973 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6974 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6975 i < num_segments;
c044fabd 6976 i++, segment++)
252b5132 6977 {
252b5132 6978 unsigned int j;
c044fabd 6979 Elf_Internal_Phdr *segment2;
252b5132 6980
aecc8f8a
AM
6981 if (segment->p_type == PT_INTERP)
6982 for (section = ibfd->sections; section; section = section->next)
6983 if (IS_SOLARIS_PT_INTERP (segment, section))
6984 {
6985 /* Mininal change so that the normal section to segment
4cc11e76 6986 assignment code will work. */
502794d4 6987 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
6988 break;
6989 }
6990
bc67d8a6 6991 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6992 {
6993 /* Remove PT_GNU_RELRO segment. */
6994 if (segment->p_type == PT_GNU_RELRO)
6995 segment->p_type = PT_NULL;
6996 continue;
6997 }
c044fabd 6998
bc67d8a6 6999 /* Determine if this segment overlaps any previous segments. */
0067a569 7000 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7001 {
7002 bfd_signed_vma extra_length;
c044fabd 7003
bc67d8a6 7004 if (segment2->p_type != PT_LOAD
0067a569 7005 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7006 continue;
c044fabd 7007
bc67d8a6
NC
7008 /* Merge the two segments together. */
7009 if (segment2->p_vaddr < segment->p_vaddr)
7010 {
c044fabd 7011 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7012 SEGMENT. */
0067a569
AM
7013 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7014 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 7015
bc67d8a6
NC
7016 if (extra_length > 0)
7017 {
0067a569 7018 segment2->p_memsz += extra_length;
bc67d8a6
NC
7019 segment2->p_filesz += extra_length;
7020 }
c044fabd 7021
bc67d8a6 7022 segment->p_type = PT_NULL;
c044fabd 7023
bc67d8a6
NC
7024 /* Since we have deleted P we must restart the outer loop. */
7025 i = 0;
7026 segment = elf_tdata (ibfd)->phdr;
7027 break;
7028 }
7029 else
7030 {
c044fabd 7031 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7032 SEGMENT2. */
0067a569
AM
7033 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7034 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7035
bc67d8a6
NC
7036 if (extra_length > 0)
7037 {
0067a569 7038 segment->p_memsz += extra_length;
bc67d8a6
NC
7039 segment->p_filesz += extra_length;
7040 }
c044fabd 7041
bc67d8a6
NC
7042 segment2->p_type = PT_NULL;
7043 }
7044 }
7045 }
c044fabd 7046
bc67d8a6
NC
7047 /* The second scan attempts to assign sections to segments. */
7048 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7049 i < num_segments;
0067a569 7050 i++, segment++)
bc67d8a6 7051 {
0067a569
AM
7052 unsigned int section_count;
7053 asection **sections;
7054 asection *output_section;
7055 unsigned int isec;
9933dc52
AM
7056 asection *matching_lma;
7057 asection *suggested_lma;
0067a569 7058 unsigned int j;
446f7ed5 7059 size_t amt;
0067a569 7060 asection *first_section;
bc67d8a6
NC
7061
7062 if (segment->p_type == PT_NULL)
7063 continue;
c044fabd 7064
9f17e2a6 7065 first_section = NULL;
bc67d8a6 7066 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7067 for (section = ibfd->sections, section_count = 0;
7068 section != NULL;
7069 section = section->next)
9f17e2a6
L
7070 {
7071 /* Find the first section in the input segment, which may be
7072 removed from the corresponding output segment. */
502794d4 7073 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7074 {
7075 if (first_section == NULL)
7076 first_section = section;
7077 if (section->output_section != NULL)
7078 ++section_count;
7079 }
7080 }
811072d8 7081
b5f852ea
NC
7082 /* Allocate a segment map big enough to contain
7083 all of the sections we have selected. */
00bee008 7084 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7085 amt += section_count * sizeof (asection *);
a50b1753 7086 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7087 if (map == NULL)
0a1b45a2 7088 return false;
252b5132
RH
7089
7090 /* Initialise the fields of the segment map. Default to
7091 using the physical address of the segment in the input BFD. */
0067a569
AM
7092 map->next = NULL;
7093 map->p_type = segment->p_type;
7094 map->p_flags = segment->p_flags;
bc67d8a6 7095 map->p_flags_valid = 1;
55d55ac7 7096
c410035d
AM
7097 if (map->p_type == PT_LOAD
7098 && (ibfd->flags & D_PAGED) != 0
7099 && maxpagesize > 1
7100 && segment->p_align > 1)
7101 {
7102 map->p_align = segment->p_align;
7103 if (segment->p_align > maxpagesize)
7104 map->p_align = maxpagesize;
7105 map->p_align_valid = 1;
7106 }
7107
9f17e2a6
L
7108 /* If the first section in the input segment is removed, there is
7109 no need to preserve segment physical address in the corresponding
7110 output segment. */
945c025a 7111 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7112 {
7113 map->p_paddr = segment->p_paddr;
5c44b38e 7114 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7115 }
252b5132
RH
7116
7117 /* Determine if this segment contains the ELF file header
7118 and if it contains the program headers themselves. */
bc67d8a6
NC
7119 map->includes_filehdr = (segment->p_offset == 0
7120 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7121 map->includes_phdrs = 0;
252b5132 7122
0067a569 7123 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7124 {
bc67d8a6
NC
7125 map->includes_phdrs =
7126 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7127 && (segment->p_offset + segment->p_filesz
252b5132
RH
7128 >= ((bfd_vma) iehdr->e_phoff
7129 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7130
bc67d8a6 7131 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7132 phdr_included = true;
252b5132
RH
7133 }
7134
bc67d8a6 7135 if (section_count == 0)
252b5132
RH
7136 {
7137 /* Special segments, such as the PT_PHDR segment, may contain
7138 no sections, but ordinary, loadable segments should contain
1ed89aa9 7139 something. They are allowed by the ELF spec however, so only
07d6d2b8 7140 a warning is produced.
f98450c6
NC
7141 There is however the valid use case of embedded systems which
7142 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7143 flash memory with zeros. No warning is shown for that case. */
7144 if (segment->p_type == PT_LOAD
7145 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7146 /* xgettext:c-format */
9793eb77
AM
7147 _bfd_error_handler
7148 (_("%pB: warning: empty loadable segment detected"
7149 " at vaddr=%#" PRIx64 ", is this intentional?"),
7150 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7151
502794d4 7152 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7153 map->count = 0;
c044fabd
KH
7154 *pointer_to_map = map;
7155 pointer_to_map = &map->next;
252b5132
RH
7156
7157 continue;
7158 }
7159
7160 /* Now scan the sections in the input BFD again and attempt
7161 to add their corresponding output sections to the segment map.
7162 The problem here is how to handle an output section which has
7163 been moved (ie had its LMA changed). There are four possibilities:
7164
7165 1. None of the sections have been moved.
7166 In this case we can continue to use the segment LMA from the
7167 input BFD.
7168
7169 2. All of the sections have been moved by the same amount.
7170 In this case we can change the segment's LMA to match the LMA
7171 of the first section.
7172
7173 3. Some of the sections have been moved, others have not.
7174 In this case those sections which have not been moved can be
7175 placed in the current segment which will have to have its size,
7176 and possibly its LMA changed, and a new segment or segments will
7177 have to be created to contain the other sections.
7178
b5f852ea 7179 4. The sections have been moved, but not by the same amount.
252b5132
RH
7180 In this case we can change the segment's LMA to match the LMA
7181 of the first section and we will have to create a new segment
7182 or segments to contain the other sections.
7183
7184 In order to save time, we allocate an array to hold the section
7185 pointers that we are interested in. As these sections get assigned
7186 to a segment, they are removed from this array. */
7187
446f7ed5
AM
7188 amt = section_count * sizeof (asection *);
7189 sections = (asection **) bfd_malloc (amt);
252b5132 7190 if (sections == NULL)
0a1b45a2 7191 return false;
252b5132
RH
7192
7193 /* Step One: Scan for segment vs section LMA conflicts.
7194 Also add the sections to the section array allocated above.
7195 Also add the sections to the current segment. In the common
7196 case, where the sections have not been moved, this means that
7197 we have completely filled the segment, and there is nothing
7198 more to do. */
252b5132 7199 isec = 0;
9933dc52
AM
7200 matching_lma = NULL;
7201 suggested_lma = NULL;
252b5132 7202
461c4b2e 7203 for (section = first_section, j = 0;
bc67d8a6
NC
7204 section != NULL;
7205 section = section->next)
252b5132 7206 {
502794d4 7207 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7208 {
bc67d8a6
NC
7209 output_section = section->output_section;
7210
0067a569 7211 sections[j++] = section;
252b5132
RH
7212
7213 /* The Solaris native linker always sets p_paddr to 0.
7214 We try to catch that case here, and set it to the
5e8d7549
NC
7215 correct value. Note - some backends require that
7216 p_paddr be left as zero. */
5c44b38e 7217 if (!p_paddr_valid
4455705d 7218 && segment->p_vaddr != 0
0067a569 7219 && !bed->want_p_paddr_set_to_zero
252b5132 7220 && isec == 0
bc67d8a6 7221 && output_section->lma != 0
9933dc52
AM
7222 && (align_power (segment->p_vaddr
7223 + (map->includes_filehdr
7224 ? iehdr->e_ehsize : 0)
7225 + (map->includes_phdrs
7226 ? iehdr->e_phnum * iehdr->e_phentsize
7227 : 0),
66631823
CE
7228 output_section->alignment_power * opb)
7229 == (output_section->vma * opb)))
bc67d8a6 7230 map->p_paddr = segment->p_vaddr;
252b5132
RH
7231
7232 /* Match up the physical address of the segment with the
7233 LMA address of the output section. */
502794d4
CE
7234 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7235 opb)
5e8d7549 7236 || IS_COREFILE_NOTE (segment, section)
0067a569 7237 || (bed->want_p_paddr_set_to_zero
502794d4 7238 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7239 {
9933dc52
AM
7240 if (matching_lma == NULL
7241 || output_section->lma < matching_lma->lma)
7242 matching_lma = output_section;
252b5132
RH
7243
7244 /* We assume that if the section fits within the segment
bc67d8a6 7245 then it does not overlap any other section within that
252b5132 7246 segment. */
0067a569
AM
7247 map->sections[isec++] = output_section;
7248 }
9933dc52
AM
7249 else if (suggested_lma == NULL)
7250 suggested_lma = output_section;
147d51c2
L
7251
7252 if (j == section_count)
7253 break;
252b5132
RH
7254 }
7255 }
7256
bc67d8a6 7257 BFD_ASSERT (j == section_count);
252b5132
RH
7258
7259 /* Step Two: Adjust the physical address of the current segment,
7260 if necessary. */
bc67d8a6 7261 if (isec == section_count)
252b5132
RH
7262 {
7263 /* All of the sections fitted within the segment as currently
7264 specified. This is the default case. Add the segment to
7265 the list of built segments and carry on to process the next
7266 program header in the input BFD. */
bc67d8a6 7267 map->count = section_count;
c044fabd
KH
7268 *pointer_to_map = map;
7269 pointer_to_map = &map->next;
08a40648 7270
5c44b38e 7271 if (p_paddr_valid
30fe1832
AM
7272 && !bed->want_p_paddr_set_to_zero)
7273 {
7274 bfd_vma hdr_size = 0;
7275 if (map->includes_filehdr)
7276 hdr_size = iehdr->e_ehsize;
7277 if (map->includes_phdrs)
7278 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7279
7280 /* Account for padding before the first section in the
7281 segment. */
502794d4
CE
7282 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7283 - matching_lma->lma);
30fe1832 7284 }
08a40648 7285
252b5132
RH
7286 free (sections);
7287 continue;
7288 }
252b5132
RH
7289 else
7290 {
9933dc52
AM
7291 /* Change the current segment's physical address to match
7292 the LMA of the first section that fitted, or if no
7293 section fitted, the first section. */
7294 if (matching_lma == NULL)
7295 matching_lma = suggested_lma;
7296
66631823 7297 map->p_paddr = matching_lma->lma * opb;
72730e0c 7298
bc67d8a6
NC
7299 /* Offset the segment physical address from the lma
7300 to allow for space taken up by elf headers. */
9933dc52 7301 if (map->includes_phdrs)
010c8431 7302 {
9933dc52
AM
7303 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7304
7305 /* iehdr->e_phnum is just an estimate of the number
7306 of program headers that we will need. Make a note
7307 here of the number we used and the segment we chose
7308 to hold these headers, so that we can adjust the
7309 offset when we know the correct value. */
7310 phdr_adjust_num = iehdr->e_phnum;
7311 phdr_adjust_seg = map;
010c8431 7312 }
252b5132 7313
9933dc52 7314 if (map->includes_filehdr)
bc67d8a6 7315 {
9933dc52
AM
7316 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7317 map->p_paddr -= iehdr->e_ehsize;
7318 /* We've subtracted off the size of headers from the
7319 first section lma, but there may have been some
7320 alignment padding before that section too. Try to
7321 account for that by adjusting the segment lma down to
7322 the same alignment. */
7323 if (segment->p_align != 0 && segment->p_align < align)
7324 align = segment->p_align;
66631823 7325 map->p_paddr &= -(align * opb);
bc67d8a6 7326 }
252b5132
RH
7327 }
7328
7329 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7330 those that fit to the current segment and removing them from the
252b5132
RH
7331 sections array; but making sure not to leave large gaps. Once all
7332 possible sections have been assigned to the current segment it is
7333 added to the list of built segments and if sections still remain
7334 to be assigned, a new segment is constructed before repeating
7335 the loop. */
7336 isec = 0;
7337 do
7338 {
bc67d8a6 7339 map->count = 0;
9933dc52 7340 suggested_lma = NULL;
252b5132
RH
7341
7342 /* Fill the current segment with sections that fit. */
bc67d8a6 7343 for (j = 0; j < section_count; j++)
252b5132 7344 {
bc67d8a6 7345 section = sections[j];
252b5132 7346
bc67d8a6 7347 if (section == NULL)
252b5132
RH
7348 continue;
7349
bc67d8a6 7350 output_section = section->output_section;
252b5132 7351
bc67d8a6 7352 BFD_ASSERT (output_section != NULL);
c044fabd 7353
502794d4
CE
7354 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7355 opb)
bc67d8a6 7356 || IS_COREFILE_NOTE (segment, section))
252b5132 7357 {
bc67d8a6 7358 if (map->count == 0)
252b5132
RH
7359 {
7360 /* If the first section in a segment does not start at
bc67d8a6
NC
7361 the beginning of the segment, then something is
7362 wrong. */
9933dc52
AM
7363 if (align_power (map->p_paddr
7364 + (map->includes_filehdr
7365 ? iehdr->e_ehsize : 0)
7366 + (map->includes_phdrs
7367 ? iehdr->e_phnum * iehdr->e_phentsize
7368 : 0),
66631823
CE
7369 output_section->alignment_power * opb)
7370 != output_section->lma * opb)
9aea1e31 7371 goto sorry;
252b5132
RH
7372 }
7373 else
7374 {
0067a569 7375 asection *prev_sec;
252b5132 7376
bc67d8a6 7377 prev_sec = map->sections[map->count - 1];
252b5132
RH
7378
7379 /* If the gap between the end of the previous section
bc67d8a6
NC
7380 and the start of this section is more than
7381 maxpagesize then we need to start a new segment. */
eea6121a 7382 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7383 maxpagesize)
caf47ea6 7384 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7385 || (prev_sec->lma + prev_sec->size
079e9a2f 7386 > output_section->lma))
252b5132 7387 {
9933dc52
AM
7388 if (suggested_lma == NULL)
7389 suggested_lma = output_section;
252b5132
RH
7390
7391 continue;
7392 }
7393 }
7394
bc67d8a6 7395 map->sections[map->count++] = output_section;
252b5132
RH
7396 ++isec;
7397 sections[j] = NULL;
9933dc52 7398 if (segment->p_type == PT_LOAD)
0a1b45a2 7399 section->segment_mark = true;
0067a569 7400 }
9933dc52
AM
7401 else if (suggested_lma == NULL)
7402 suggested_lma = output_section;
252b5132
RH
7403 }
7404
beab4532
NC
7405 /* PR 23932. A corrupt input file may contain sections that cannot
7406 be assigned to any segment - because for example they have a
9984857c
NC
7407 negative size - or segments that do not contain any sections.
7408 But there are also valid reasons why a segment can be empty.
7409 So allow a count of zero. */
252b5132
RH
7410
7411 /* Add the current segment to the list of built segments. */
c044fabd
KH
7412 *pointer_to_map = map;
7413 pointer_to_map = &map->next;
252b5132 7414
bc67d8a6 7415 if (isec < section_count)
252b5132
RH
7416 {
7417 /* We still have not allocated all of the sections to
7418 segments. Create a new segment here, initialise it
7419 and carry on looping. */
00bee008 7420 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7421 amt += section_count * sizeof (asection *);
5964fc3a 7422 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7423 if (map == NULL)
5ed6aba4
NC
7424 {
7425 free (sections);
0a1b45a2 7426 return false;
5ed6aba4 7427 }
252b5132
RH
7428
7429 /* Initialise the fields of the segment map. Set the physical
7430 physical address to the LMA of the first section that has
7431 not yet been assigned. */
0067a569
AM
7432 map->next = NULL;
7433 map->p_type = segment->p_type;
7434 map->p_flags = segment->p_flags;
7435 map->p_flags_valid = 1;
66631823 7436 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7437 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7438 map->includes_filehdr = 0;
0067a569 7439 map->includes_phdrs = 0;
252b5132 7440 }
9984857c
NC
7441
7442 continue;
7443 sorry:
7444 bfd_set_error (bfd_error_sorry);
7445 free (sections);
0a1b45a2 7446 return false;
252b5132 7447 }
bc67d8a6 7448 while (isec < section_count);
252b5132
RH
7449
7450 free (sections);
7451 }
7452
12bd6957 7453 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7454
7455 /* If we had to estimate the number of program headers that were
9ad5cbcf 7456 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7457 the offset if necessary. */
7458 if (phdr_adjust_seg != NULL)
7459 {
7460 unsigned int count;
c044fabd 7461
bc67d8a6 7462 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7463 count++;
252b5132 7464
bc67d8a6
NC
7465 if (count > phdr_adjust_num)
7466 phdr_adjust_seg->p_paddr
7467 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7468
7469 for (map = map_first; map != NULL; map = map->next)
7470 if (map->p_type == PT_PHDR)
7471 {
7472 bfd_vma adjust
7473 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7474 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7475 break;
7476 }
bc67d8a6 7477 }
c044fabd 7478
bc67d8a6 7479#undef SEGMENT_END
eecdbe52 7480#undef SECTION_SIZE
bc67d8a6
NC
7481#undef IS_CONTAINED_BY_VMA
7482#undef IS_CONTAINED_BY_LMA
0efc80c8 7483#undef IS_NOTE
252b5132 7484#undef IS_COREFILE_NOTE
bc67d8a6 7485#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7486#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7487#undef INCLUDE_SECTION_IN_SEGMENT
7488#undef SEGMENT_AFTER_SEGMENT
7489#undef SEGMENT_OVERLAPS
0a1b45a2 7490 return true;
252b5132
RH
7491}
7492
84d1d650
L
7493/* Copy ELF program header information. */
7494
0a1b45a2 7495static bool
84d1d650
L
7496copy_elf_program_header (bfd *ibfd, bfd *obfd)
7497{
7498 Elf_Internal_Ehdr *iehdr;
7499 struct elf_segment_map *map;
7500 struct elf_segment_map *map_first;
7501 struct elf_segment_map **pointer_to_map;
7502 Elf_Internal_Phdr *segment;
7503 unsigned int i;
7504 unsigned int num_segments;
0a1b45a2
AM
7505 bool phdr_included = false;
7506 bool p_paddr_valid;
502794d4 7507 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7508
7509 iehdr = elf_elfheader (ibfd);
7510
7511 map_first = NULL;
7512 pointer_to_map = &map_first;
7513
88967714
AM
7514 /* If all the segment p_paddr fields are zero, don't set
7515 map->p_paddr_valid. */
0a1b45a2 7516 p_paddr_valid = false;
84d1d650 7517 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7518 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7519 i < num_segments;
7520 i++, segment++)
7521 if (segment->p_paddr != 0)
7522 {
0a1b45a2 7523 p_paddr_valid = true;
88967714
AM
7524 break;
7525 }
7526
84d1d650
L
7527 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7528 i < num_segments;
7529 i++, segment++)
7530 {
7531 asection *section;
7532 unsigned int section_count;
986f0783 7533 size_t amt;
84d1d650 7534 Elf_Internal_Shdr *this_hdr;
53020534 7535 asection *first_section = NULL;
a76e6f2f 7536 asection *lowest_section;
84d1d650 7537
84d1d650
L
7538 /* Compute how many sections are in this segment. */
7539 for (section = ibfd->sections, section_count = 0;
7540 section != NULL;
7541 section = section->next)
7542 {
7543 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7544 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7545 {
a76e6f2f
AM
7546 if (first_section == NULL)
7547 first_section = section;
3271a814
NS
7548 section_count++;
7549 }
84d1d650
L
7550 }
7551
7552 /* Allocate a segment map big enough to contain
7553 all of the sections we have selected. */
00bee008 7554 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7555 amt += section_count * sizeof (asection *);
a50b1753 7556 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650 7557 if (map == NULL)
0a1b45a2 7558 return false;
84d1d650
L
7559
7560 /* Initialize the fields of the output segment map with the
7561 input segment. */
7562 map->next = NULL;
7563 map->p_type = segment->p_type;
7564 map->p_flags = segment->p_flags;
7565 map->p_flags_valid = 1;
7566 map->p_paddr = segment->p_paddr;
88967714 7567 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7568 map->p_align = segment->p_align;
7569 map->p_align_valid = 1;
3271a814 7570 map->p_vaddr_offset = 0;
84d1d650 7571
04c3a755
NS
7572 if (map->p_type == PT_GNU_RELRO
7573 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7574 {
7575 /* The PT_GNU_RELRO segment may contain the first a few
7576 bytes in the .got.plt section even if the whole .got.plt
7577 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7578 change the size of the PT_GNU_RELRO segment.
7579 Similarly, PT_GNU_STACK size is significant on uclinux
7580 systems. */
9433b9b1 7581 map->p_size = segment->p_memsz;
b10a8ae0
L
7582 map->p_size_valid = 1;
7583 }
7584
84d1d650
L
7585 /* Determine if this segment contains the ELF file header
7586 and if it contains the program headers themselves. */
7587 map->includes_filehdr = (segment->p_offset == 0
7588 && segment->p_filesz >= iehdr->e_ehsize);
7589
7590 map->includes_phdrs = 0;
7591 if (! phdr_included || segment->p_type != PT_LOAD)
7592 {
7593 map->includes_phdrs =
7594 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7595 && (segment->p_offset + segment->p_filesz
7596 >= ((bfd_vma) iehdr->e_phoff
7597 + iehdr->e_phnum * iehdr->e_phentsize)));
7598
7599 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7600 phdr_included = true;
84d1d650
L
7601 }
7602
bbefd0a9 7603 lowest_section = NULL;
84d1d650
L
7604 if (section_count != 0)
7605 {
7606 unsigned int isec = 0;
7607
53020534 7608 for (section = first_section;
84d1d650
L
7609 section != NULL;
7610 section = section->next)
7611 {
7612 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7613 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7614 {
7615 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7616 if ((section->flags & SEC_ALLOC) != 0)
7617 {
7618 bfd_vma seg_off;
7619
bbefd0a9
AM
7620 if (lowest_section == NULL
7621 || section->lma < lowest_section->lma)
fb8a5684
AM
7622 lowest_section = section;
7623
a76e6f2f
AM
7624 /* Section lmas are set up from PT_LOAD header
7625 p_paddr in _bfd_elf_make_section_from_shdr.
7626 If this header has a p_paddr that disagrees
7627 with the section lma, flag the p_paddr as
7628 invalid. */
7629 if ((section->flags & SEC_LOAD) != 0)
7630 seg_off = this_hdr->sh_offset - segment->p_offset;
7631 else
7632 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7633 if (section->lma * opb - segment->p_paddr != seg_off)
0a1b45a2 7634 map->p_paddr_valid = false;
a76e6f2f 7635 }
53020534
L
7636 if (isec == section_count)
7637 break;
7638 }
84d1d650
L
7639 }
7640 }
7641
5d695627 7642 if (section_count == 0)
502794d4 7643 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7644 else if (map->p_paddr_valid)
7645 {
7646 /* Account for padding before the first section in the segment. */
7647 bfd_vma hdr_size = 0;
7648 if (map->includes_filehdr)
7649 hdr_size = iehdr->e_ehsize;
7650 if (map->includes_phdrs)
7651 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7652
502794d4 7653 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7654 - (lowest_section ? lowest_section->lma : 0));
7655 }
a76e6f2f 7656
84d1d650
L
7657 map->count = section_count;
7658 *pointer_to_map = map;
7659 pointer_to_map = &map->next;
7660 }
7661
12bd6957 7662 elf_seg_map (obfd) = map_first;
0a1b45a2 7663 return true;
84d1d650
L
7664}
7665
7666/* Copy private BFD data. This copies or rewrites ELF program header
7667 information. */
7668
0a1b45a2 7669static bool
84d1d650
L
7670copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7671{
c410035d
AM
7672 bfd_vma maxpagesize;
7673
84d1d650
L
7674 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7675 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 7676 return true;
84d1d650
L
7677
7678 if (elf_tdata (ibfd)->phdr == NULL)
0a1b45a2 7679 return true;
84d1d650
L
7680
7681 if (ibfd->xvec == obfd->xvec)
7682 {
cb3ff1e5
NC
7683 /* Check to see if any sections in the input BFD
7684 covered by ELF program header have changed. */
d55ce4e2 7685 Elf_Internal_Phdr *segment;
84d1d650
L
7686 asection *section, *osec;
7687 unsigned int i, num_segments;
7688 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7689 const struct elf_backend_data *bed;
7690
7691 bed = get_elf_backend_data (ibfd);
7692
7693 /* Regenerate the segment map if p_paddr is set to 0. */
7694 if (bed->want_p_paddr_set_to_zero)
7695 goto rewrite;
84d1d650
L
7696
7697 /* Initialize the segment mark field. */
7698 for (section = obfd->sections; section != NULL;
7699 section = section->next)
0a1b45a2 7700 section->segment_mark = false;
84d1d650
L
7701
7702 num_segments = elf_elfheader (ibfd)->e_phnum;
7703 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7704 i < num_segments;
7705 i++, segment++)
7706 {
5f6999aa
NC
7707 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7708 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7709 which severly confuses things, so always regenerate the segment
7710 map in this case. */
7711 if (segment->p_paddr == 0
7712 && segment->p_memsz == 0
7713 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7714 goto rewrite;
5f6999aa 7715
84d1d650
L
7716 for (section = ibfd->sections;
7717 section != NULL; section = section->next)
7718 {
7719 /* We mark the output section so that we know it comes
7720 from the input BFD. */
7721 osec = section->output_section;
7722 if (osec)
0a1b45a2 7723 osec->segment_mark = true;
84d1d650
L
7724
7725 /* Check if this section is covered by the segment. */
7726 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7727 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7728 {
7729 /* FIXME: Check if its output section is changed or
7730 removed. What else do we need to check? */
7731 if (osec == NULL
7732 || section->flags != osec->flags
7733 || section->lma != osec->lma
7734 || section->vma != osec->vma
7735 || section->size != osec->size
7736 || section->rawsize != osec->rawsize
7737 || section->alignment_power != osec->alignment_power)
7738 goto rewrite;
7739 }
7740 }
7741 }
7742
cb3ff1e5 7743 /* Check to see if any output section do not come from the
84d1d650
L
7744 input BFD. */
7745 for (section = obfd->sections; section != NULL;
7746 section = section->next)
7747 {
535b785f 7748 if (!section->segment_mark)
84d1d650
L
7749 goto rewrite;
7750 else
0a1b45a2 7751 section->segment_mark = false;
84d1d650
L
7752 }
7753
7754 return copy_elf_program_header (ibfd, obfd);
7755 }
7756
dc1e8a47 7757 rewrite:
c410035d 7758 maxpagesize = 0;
f1d85785
L
7759 if (ibfd->xvec == obfd->xvec)
7760 {
7761 /* When rewriting program header, set the output maxpagesize to
7762 the maximum alignment of input PT_LOAD segments. */
7763 Elf_Internal_Phdr *segment;
7764 unsigned int i;
7765 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7766
7767 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7768 i < num_segments;
7769 i++, segment++)
7770 if (segment->p_type == PT_LOAD
7771 && maxpagesize < segment->p_align)
c86934ce
NC
7772 {
7773 /* PR 17512: file: f17299af. */
7774 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7775 /* xgettext:c-format */
2dcf00ce
AM
7776 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7777 PRIx64 " is too large"),
7778 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7779 else
7780 maxpagesize = segment->p_align;
7781 }
f1d85785 7782 }
c410035d
AM
7783 if (maxpagesize == 0)
7784 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7785
c410035d 7786 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7787}
7788
ccd2ec6a
L
7789/* Initialize private output section information from input section. */
7790
0a1b45a2 7791bool
ccd2ec6a
L
7792_bfd_elf_init_private_section_data (bfd *ibfd,
7793 asection *isec,
7794 bfd *obfd,
7795 asection *osec,
7796 struct bfd_link_info *link_info)
7797
7798{
7799 Elf_Internal_Shdr *ihdr, *ohdr;
0a1b45a2
AM
7800 bool final_link = (link_info != NULL
7801 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7802
7803 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7804 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7805 return true;
ccd2ec6a 7806
ba85c43e
NC
7807 BFD_ASSERT (elf_section_data (osec) != NULL);
7808
8c803a2d
AM
7809 /* If this is a known ABI section, ELF section type and flags may
7810 have been set up when OSEC was created. For normal sections we
7811 allow the user to override the type and flags other than
7812 SHF_MASKOS and SHF_MASKPROC. */
7813 if (elf_section_type (osec) == SHT_PROGBITS
7814 || elf_section_type (osec) == SHT_NOTE
7815 || elf_section_type (osec) == SHT_NOBITS)
7816 elf_section_type (osec) = SHT_NULL;
7817 /* For objcopy and relocatable link, copy the ELF section type from
7818 the input file if the BFD section flags are the same. (If they
7819 are different the user may be doing something like
7820 "objcopy --set-section-flags .text=alloc,data".) For a final
7821 link allow some flags that the linker clears to differ. */
42bb2e33 7822 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7823 && (osec->flags == isec->flags
7824 || (final_link
7825 && ((osec->flags ^ isec->flags)
0814be7d 7826 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7827 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7828
7829 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7830 elf_section_flags (osec) = (elf_section_flags (isec)
7831 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7832
a91e1603 7833 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7834 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7835 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7836 elf_section_data (osec)->this_hdr.sh_info
7837 = elf_section_data (isec)->this_hdr.sh_info;
7838
ccd2ec6a
L
7839 /* Set things up for objcopy and relocatable link. The output
7840 SHT_GROUP section will have its elf_next_in_group pointing back
7841 to the input group members. Ignore linker created group section.
7842 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7843 if ((link_info == NULL
7844 || !link_info->resolve_section_groups)
7845 && (elf_sec_group (isec) == NULL
7846 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7847 {
7bdf4127
AB
7848 if (elf_section_flags (isec) & SHF_GROUP)
7849 elf_section_flags (osec) |= SHF_GROUP;
7850 elf_next_in_group (osec) = elf_next_in_group (isec);
7851 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7852 }
7853
7bdf4127
AB
7854 /* If not decompress, preserve SHF_COMPRESSED. */
7855 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7856 elf_section_flags (osec) |= (elf_section_flags (isec)
7857 & SHF_COMPRESSED);
7858
ccd2ec6a
L
7859 ihdr = &elf_section_data (isec)->this_hdr;
7860
7861 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7862 don't use the output section of the linked-to section since it
7863 may be NULL at this point. */
7864 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7865 {
7866 ohdr = &elf_section_data (osec)->this_hdr;
7867 ohdr->sh_flags |= SHF_LINK_ORDER;
7868 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7869 }
7870
7871 osec->use_rela_p = isec->use_rela_p;
7872
0a1b45a2 7873 return true;
ccd2ec6a
L
7874}
7875
252b5132
RH
7876/* Copy private section information. This copies over the entsize
7877 field, and sometimes the info field. */
7878
0a1b45a2 7879bool
217aa764
AM
7880_bfd_elf_copy_private_section_data (bfd *ibfd,
7881 asection *isec,
7882 bfd *obfd,
7883 asection *osec)
252b5132
RH
7884{
7885 Elf_Internal_Shdr *ihdr, *ohdr;
7886
7887 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7888 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7889 return true;
252b5132 7890
252b5132
RH
7891 ihdr = &elf_section_data (isec)->this_hdr;
7892 ohdr = &elf_section_data (osec)->this_hdr;
7893
7894 ohdr->sh_entsize = ihdr->sh_entsize;
7895
7896 if (ihdr->sh_type == SHT_SYMTAB
7897 || ihdr->sh_type == SHT_DYNSYM
7898 || ihdr->sh_type == SHT_GNU_verneed
7899 || ihdr->sh_type == SHT_GNU_verdef)
7900 ohdr->sh_info = ihdr->sh_info;
7901
ccd2ec6a
L
7902 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7903 NULL);
252b5132
RH
7904}
7905
d0bf826b
AM
7906/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7907 necessary if we are removing either the SHT_GROUP section or any of
7908 the group member sections. DISCARDED is the value that a section's
7909 output_section has if the section will be discarded, NULL when this
7910 function is called from objcopy, bfd_abs_section_ptr when called
7911 from the linker. */
80fccad2 7912
0a1b45a2 7913bool
d0bf826b 7914_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7915{
30288845
AM
7916 asection *isec;
7917
30288845 7918 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7919 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7920 {
7921 asection *first = elf_next_in_group (isec);
7922 asection *s = first;
d0bf826b
AM
7923 bfd_size_type removed = 0;
7924
30288845
AM
7925 while (s != NULL)
7926 {
415f38a6
AM
7927 /* If this member section is being output but the
7928 SHT_GROUP section is not, then clear the group info
7929 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7930 if (s->output_section != discarded
7931 && isec->output_section == discarded)
30288845
AM
7932 {
7933 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7934 elf_group_name (s->output_section) = NULL;
7935 }
3349112e 7936 else
6e5e9d58
AM
7937 {
7938 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
7939 if (s->output_section == discarded
7940 && isec->output_section != discarded)
7941 {
7942 /* Conversely, if the member section is not being
7943 output but the SHT_GROUP section is, then adjust
7944 its size. */
7945 removed += 4;
7946 if (elf_sec->rel.hdr != NULL
7947 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7948 removed += 4;
7949 if (elf_sec->rela.hdr != NULL
7950 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7951 removed += 4;
7952 }
7953 else
7954 {
7955 /* Also adjust for zero-sized relocation member
7956 section. */
7957 if (elf_sec->rel.hdr != NULL
7958 && elf_sec->rel.hdr->sh_size == 0)
7959 removed += 4;
7960 if (elf_sec->rela.hdr != NULL
7961 && elf_sec->rela.hdr->sh_size == 0)
7962 removed += 4;
7963 }
6e5e9d58 7964 }
30288845
AM
7965 s = elf_next_in_group (s);
7966 if (s == first)
7967 break;
7968 }
d0bf826b
AM
7969 if (removed != 0)
7970 {
7971 if (discarded != NULL)
7972 {
7973 /* If we've been called for ld -r, then we need to
6e5e9d58 7974 adjust the input section size. */
d0bf826b
AM
7975 if (isec->rawsize == 0)
7976 isec->rawsize = isec->size;
7977 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7978 if (isec->size <= 4)
7979 {
7980 isec->size = 0;
7981 isec->flags |= SEC_EXCLUDE;
7982 }
d0bf826b
AM
7983 }
7984 else
7985 {
7986 /* Adjust the output section size when called from
7987 objcopy. */
7988 isec->output_section->size -= removed;
6e5e9d58
AM
7989 if (isec->output_section->size <= 4)
7990 {
7991 isec->output_section->size = 0;
7992 isec->output_section->flags |= SEC_EXCLUDE;
7993 }
d0bf826b
AM
7994 }
7995 }
30288845
AM
7996 }
7997
0a1b45a2 7998 return true;
80fccad2
BW
7999}
8000
d0bf826b
AM
8001/* Copy private header information. */
8002
0a1b45a2 8003bool
d0bf826b
AM
8004_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8005{
8006 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8007 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8008 return true;
d0bf826b
AM
8009
8010 /* Copy over private BFD data if it has not already been copied.
8011 This must be done here, rather than in the copy_private_bfd_data
8012 entry point, because the latter is called after the section
8013 contents have been set, which means that the program headers have
8014 already been worked out. */
12bd6957 8015 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8016 {
8017 if (! copy_private_bfd_data (ibfd, obfd))
0a1b45a2 8018 return false;
d0bf826b
AM
8019 }
8020
8021 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8022}
8023
252b5132
RH
8024/* Copy private symbol information. If this symbol is in a section
8025 which we did not map into a BFD section, try to map the section
8026 index correctly. We use special macro definitions for the mapped
8027 section indices; these definitions are interpreted by the
8028 swap_out_syms function. */
8029
9ad5cbcf
AM
8030#define MAP_ONESYMTAB (SHN_HIOS + 1)
8031#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8032#define MAP_STRTAB (SHN_HIOS + 3)
8033#define MAP_SHSTRTAB (SHN_HIOS + 4)
8034#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8035
0a1b45a2 8036bool
217aa764
AM
8037_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8038 asymbol *isymarg,
8039 bfd *obfd,
8040 asymbol *osymarg)
252b5132
RH
8041{
8042 elf_symbol_type *isym, *osym;
8043
8044 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8045 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8046 return true;
252b5132 8047
c1229f84
AM
8048 isym = elf_symbol_from (isymarg);
8049 osym = elf_symbol_from (osymarg);
252b5132
RH
8050
8051 if (isym != NULL
8424d8f5 8052 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8053 && osym != NULL
8054 && bfd_is_abs_section (isym->symbol.section))
8055 {
8056 unsigned int shndx;
8057
8058 shndx = isym->internal_elf_sym.st_shndx;
8059 if (shndx == elf_onesymtab (ibfd))
8060 shndx = MAP_ONESYMTAB;
8061 else if (shndx == elf_dynsymtab (ibfd))
8062 shndx = MAP_DYNSYMTAB;
12bd6957 8063 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8064 shndx = MAP_STRTAB;
12bd6957 8065 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8066 shndx = MAP_SHSTRTAB;
6a40cf0c 8067 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8068 shndx = MAP_SYM_SHNDX;
252b5132
RH
8069 osym->internal_elf_sym.st_shndx = shndx;
8070 }
8071
0a1b45a2 8072 return true;
252b5132
RH
8073}
8074
8075/* Swap out the symbols. */
8076
0a1b45a2 8077static bool
217aa764 8078swap_out_syms (bfd *abfd,
ef10c3ac 8079 struct elf_strtab_hash **sttp,
3d16b64e
NA
8080 int relocatable_p,
8081 struct bfd_link_info *info)
252b5132 8082{
9c5bfbb7 8083 const struct elf_backend_data *bed;
1f4361a7 8084 unsigned int symcount;
079e9a2f 8085 asymbol **syms;
ef10c3ac 8086 struct elf_strtab_hash *stt;
079e9a2f 8087 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8088 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8089 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8090 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8091 bfd_byte *outbound_syms;
8092 bfd_byte *outbound_shndx;
ef10c3ac
L
8093 unsigned long outbound_syms_index;
8094 unsigned long outbound_shndx_index;
1f4361a7 8095 unsigned int idx;
12bd6957 8096 unsigned int num_locals;
1f4361a7 8097 size_t amt;
0a1b45a2 8098 bool name_local_sections;
252b5132 8099
12bd6957 8100 if (!elf_map_symbols (abfd, &num_locals))
0a1b45a2 8101 return false;
252b5132 8102
c044fabd 8103 /* Dump out the symtabs. */
ef10c3ac 8104 stt = _bfd_elf_strtab_init ();
079e9a2f 8105 if (stt == NULL)
0a1b45a2 8106 return false;
252b5132 8107
079e9a2f
AM
8108 bed = get_elf_backend_data (abfd);
8109 symcount = bfd_get_symcount (abfd);
8110 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8111 symtab_hdr->sh_type = SHT_SYMTAB;
8112 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8113 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8114 symtab_hdr->sh_info = num_locals + 1;
72de5009 8115 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8116
8117 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8118 symstrtab_hdr->sh_type = SHT_STRTAB;
8119
ef10c3ac 8120 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8121 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8122 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8123 {
1f4361a7 8124 bfd_set_error (bfd_error_no_memory);
ef10c3ac 8125 _bfd_elf_strtab_free (stt);
0a1b45a2 8126 return false;
ef10c3ac
L
8127 }
8128
1f4361a7
AM
8129 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8130 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8131 {
1f4361a7
AM
8132 error_no_mem:
8133 bfd_set_error (bfd_error_no_memory);
8134 error_return:
ef10c3ac 8135 free (symstrtab);
1f4361a7 8136 _bfd_elf_strtab_free (stt);
0a1b45a2 8137 return false;
5ed6aba4 8138 }
217aa764 8139 symtab_hdr->contents = outbound_syms;
ef10c3ac 8140 outbound_syms_index = 0;
252b5132 8141
9ad5cbcf 8142 outbound_shndx = NULL;
ef10c3ac 8143 outbound_shndx_index = 0;
6a40cf0c
NC
8144
8145 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8146 {
6a40cf0c
NC
8147 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8148 if (symtab_shndx_hdr->sh_name != 0)
8149 {
1f4361a7
AM
8150 if (_bfd_mul_overflow (symcount + 1,
8151 sizeof (Elf_External_Sym_Shndx), &amt))
8152 goto error_no_mem;
8153 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8154 if (outbound_shndx == NULL)
8155 goto error_return;
5ed6aba4 8156
6a40cf0c
NC
8157 symtab_shndx_hdr->contents = outbound_shndx;
8158 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8159 symtab_shndx_hdr->sh_size = amt;
8160 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8161 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8162 }
8163 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8164 }
8165
589e6347 8166 /* Now generate the data (for "contents"). */
079e9a2f
AM
8167 {
8168 /* Fill in zeroth symbol and swap it out. */
8169 Elf_Internal_Sym sym;
8170 sym.st_name = 0;
8171 sym.st_value = 0;
8172 sym.st_size = 0;
8173 sym.st_info = 0;
8174 sym.st_other = 0;
8175 sym.st_shndx = SHN_UNDEF;
35fc36a8 8176 sym.st_target_internal = 0;
ef10c3ac
L
8177 symstrtab[0].sym = sym;
8178 symstrtab[0].dest_index = outbound_syms_index;
8179 symstrtab[0].destshndx_index = outbound_shndx_index;
8180 outbound_syms_index++;
9ad5cbcf 8181 if (outbound_shndx != NULL)
ef10c3ac 8182 outbound_shndx_index++;
079e9a2f 8183 }
252b5132 8184
174fd7f9
RS
8185 name_local_sections
8186 = (bed->elf_backend_name_local_section_symbols
8187 && bed->elf_backend_name_local_section_symbols (abfd));
8188
079e9a2f 8189 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8190 for (idx = 0; idx < symcount;)
252b5132 8191 {
252b5132 8192 Elf_Internal_Sym sym;
079e9a2f
AM
8193 bfd_vma value = syms[idx]->value;
8194 elf_symbol_type *type_ptr;
8195 flagword flags = syms[idx]->flags;
8196 int type;
252b5132 8197
174fd7f9
RS
8198 if (!name_local_sections
8199 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8200 {
8201 /* Local section symbols have no name. */
ef10c3ac 8202 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8203 }
8204 else
8205 {
ef10c3ac
L
8206 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8207 to get the final offset for st_name. */
8208 sym.st_name
8209 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
0a1b45a2 8210 false);
079e9a2f 8211 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8212 goto error_return;
079e9a2f 8213 }
252b5132 8214
c1229f84 8215 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8216
079e9a2f
AM
8217 if ((flags & BSF_SECTION_SYM) == 0
8218 && bfd_is_com_section (syms[idx]->section))
8219 {
8220 /* ELF common symbols put the alignment into the `value' field,
8221 and the size into the `size' field. This is backwards from
8222 how BFD handles it, so reverse it here. */
8223 sym.st_size = value;
8224 if (type_ptr == NULL
8225 || type_ptr->internal_elf_sym.st_value == 0)
8226 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8227 else
8228 sym.st_value = type_ptr->internal_elf_sym.st_value;
8229 sym.st_shndx = _bfd_elf_section_from_bfd_section
8230 (abfd, syms[idx]->section);
8231 }
8232 else
8233 {
8234 asection *sec = syms[idx]->section;
cb33740c 8235 unsigned int shndx;
252b5132 8236
079e9a2f
AM
8237 if (sec->output_section)
8238 {
8239 value += sec->output_offset;
8240 sec = sec->output_section;
8241 }
589e6347 8242
079e9a2f
AM
8243 /* Don't add in the section vma for relocatable output. */
8244 if (! relocatable_p)
8245 value += sec->vma;
8246 sym.st_value = value;
8247 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8248
8249 if (bfd_is_abs_section (sec)
8250 && type_ptr != NULL
8251 && type_ptr->internal_elf_sym.st_shndx != 0)
8252 {
8253 /* This symbol is in a real ELF section which we did
8254 not create as a BFD section. Undo the mapping done
8255 by copy_private_symbol_data. */
8256 shndx = type_ptr->internal_elf_sym.st_shndx;
8257 switch (shndx)
8258 {
8259 case MAP_ONESYMTAB:
8260 shndx = elf_onesymtab (abfd);
8261 break;
8262 case MAP_DYNSYMTAB:
8263 shndx = elf_dynsymtab (abfd);
8264 break;
8265 case MAP_STRTAB:
12bd6957 8266 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8267 break;
8268 case MAP_SHSTRTAB:
12bd6957 8269 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8270 break;
9ad5cbcf 8271 case MAP_SYM_SHNDX:
6a40cf0c
NC
8272 if (elf_symtab_shndx_list (abfd))
8273 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8274 break;
00e49dff
NC
8275 case SHN_COMMON:
8276 case SHN_ABS:
15bc576a 8277 shndx = SHN_ABS;
079e9a2f 8278 break;
00e49dff
NC
8279 default:
8280 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8281 {
8282 if (bed->symbol_section_index)
8283 shndx = bed->symbol_section_index (abfd, type_ptr);
8284 /* Otherwise just leave the index alone. */
8285 }
8286 else
8287 {
8288 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8289 _bfd_error_handler (_("%pB: \
8290Unable to handle section index %x in ELF symbol. Using ABS instead."),
8291 abfd, shndx);
8292 shndx = SHN_ABS;
8293 }
8294 break;
079e9a2f
AM
8295 }
8296 }
8297 else
8298 {
8299 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8300
cb33740c 8301 if (shndx == SHN_BAD)
079e9a2f
AM
8302 {
8303 asection *sec2;
8304
8305 /* Writing this would be a hell of a lot easier if
8306 we had some decent documentation on bfd, and
8307 knew what to expect of the library, and what to
8308 demand of applications. For example, it
8309 appears that `objcopy' might not set the
8310 section of a symbol to be a section that is
8311 actually in the output file. */
8312 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8313 if (sec2 != NULL)
8314 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8315 if (shndx == SHN_BAD)
589e6347 8316 {
695344c0 8317 /* xgettext:c-format */
9793eb77
AM
8318 _bfd_error_handler
8319 (_("unable to find equivalent output section"
8320 " for symbol '%s' from section '%s'"),
8321 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8322 sec->name);
811072d8 8323 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8324 goto error_return;
589e6347 8325 }
079e9a2f
AM
8326 }
8327 }
252b5132 8328
079e9a2f
AM
8329 sym.st_shndx = shndx;
8330 }
252b5132 8331
13ae64f3
JJ
8332 if ((flags & BSF_THREAD_LOCAL) != 0)
8333 type = STT_TLS;
d8045f23
NC
8334 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8335 type = STT_GNU_IFUNC;
13ae64f3 8336 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8337 type = STT_FUNC;
8338 else if ((flags & BSF_OBJECT) != 0)
8339 type = STT_OBJECT;
d9352518
DB
8340 else if ((flags & BSF_RELC) != 0)
8341 type = STT_RELC;
8342 else if ((flags & BSF_SRELC) != 0)
8343 type = STT_SRELC;
079e9a2f
AM
8344 else
8345 type = STT_NOTYPE;
252b5132 8346
13ae64f3
JJ
8347 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8348 type = STT_TLS;
8349
589e6347 8350 /* Processor-specific types. */
079e9a2f
AM
8351 if (type_ptr != NULL
8352 && bed->elf_backend_get_symbol_type)
8353 type = ((*bed->elf_backend_get_symbol_type)
8354 (&type_ptr->internal_elf_sym, type));
252b5132 8355
079e9a2f
AM
8356 if (flags & BSF_SECTION_SYM)
8357 {
8358 if (flags & BSF_GLOBAL)
8359 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8360 else
8361 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8362 }
8363 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8364 {
b8871f35
L
8365 if (type != STT_TLS)
8366 {
8367 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8368 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8369 ? STT_COMMON : STT_OBJECT);
8370 else
8371 type = ((flags & BSF_ELF_COMMON) != 0
8372 ? STT_COMMON : STT_OBJECT);
8373 }
8374 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8375 }
079e9a2f
AM
8376 else if (bfd_is_und_section (syms[idx]->section))
8377 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8378 ? STB_WEAK
8379 : STB_GLOBAL),
8380 type);
8381 else if (flags & BSF_FILE)
8382 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8383 else
8384 {
8385 int bind = STB_LOCAL;
252b5132 8386
079e9a2f
AM
8387 if (flags & BSF_LOCAL)
8388 bind = STB_LOCAL;
3e7a7d11
NC
8389 else if (flags & BSF_GNU_UNIQUE)
8390 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8391 else if (flags & BSF_WEAK)
8392 bind = STB_WEAK;
8393 else if (flags & BSF_GLOBAL)
8394 bind = STB_GLOBAL;
252b5132 8395
079e9a2f
AM
8396 sym.st_info = ELF_ST_INFO (bind, type);
8397 }
252b5132 8398
079e9a2f 8399 if (type_ptr != NULL)
35fc36a8
RS
8400 {
8401 sym.st_other = type_ptr->internal_elf_sym.st_other;
8402 sym.st_target_internal
8403 = type_ptr->internal_elf_sym.st_target_internal;
8404 }
079e9a2f 8405 else
35fc36a8
RS
8406 {
8407 sym.st_other = 0;
8408 sym.st_target_internal = 0;
8409 }
252b5132 8410
ef10c3ac
L
8411 idx++;
8412 symstrtab[idx].sym = sym;
8413 symstrtab[idx].dest_index = outbound_syms_index;
8414 symstrtab[idx].destshndx_index = outbound_shndx_index;
8415
8416 outbound_syms_index++;
9ad5cbcf 8417 if (outbound_shndx != NULL)
ef10c3ac
L
8418 outbound_shndx_index++;
8419 }
8420
8421 /* Finalize the .strtab section. */
8422 _bfd_elf_strtab_finalize (stt);
8423
8424 /* Swap out the .strtab section. */
8425 for (idx = 0; idx <= symcount; idx++)
8426 {
8427 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8428 if (elfsym->sym.st_name == (unsigned long) -1)
8429 elfsym->sym.st_name = 0;
8430 else
8431 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8432 elfsym->sym.st_name);
3d16b64e
NA
8433 if (info && info->callbacks->ctf_new_symbol)
8434 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8435 &elfsym->sym);
8436
8437 /* Inform the linker of the addition of this symbol. */
8438
ef10c3ac
L
8439 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8440 (outbound_syms
8441 + (elfsym->dest_index
8442 * bed->s->sizeof_sym)),
8443 (outbound_shndx
8444 + (elfsym->destshndx_index
8445 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8446 }
ef10c3ac 8447 free (symstrtab);
252b5132 8448
079e9a2f 8449 *sttp = stt;
ef10c3ac 8450 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8451 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8452 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8453 symstrtab_hdr->sh_addr = 0;
8454 symstrtab_hdr->sh_entsize = 0;
8455 symstrtab_hdr->sh_link = 0;
8456 symstrtab_hdr->sh_info = 0;
8457 symstrtab_hdr->sh_addralign = 1;
252b5132 8458
0a1b45a2 8459 return true;
252b5132
RH
8460}
8461
8462/* Return the number of bytes required to hold the symtab vector.
8463
8464 Note that we base it on the count plus 1, since we will null terminate
8465 the vector allocated based on this size. However, the ELF symbol table
8466 always has a dummy entry as symbol #0, so it ends up even. */
8467
8468long
217aa764 8469_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8470{
3a551c7a 8471 bfd_size_type symcount;
252b5132
RH
8472 long symtab_size;
8473 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8474
8475 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8476 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8477 {
8478 bfd_set_error (bfd_error_file_too_big);
8479 return -1;
8480 }
b5f386d5
AM
8481 symtab_size = symcount * (sizeof (asymbol *));
8482 if (symcount == 0)
8483 symtab_size = sizeof (asymbol *);
8484 else if (!bfd_write_p (abfd))
8485 {
8486 ufile_ptr filesize = bfd_get_file_size (abfd);
8487
8488 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8489 {
8490 bfd_set_error (bfd_error_file_truncated);
8491 return -1;
8492 }
8493 }
252b5132
RH
8494
8495 return symtab_size;
8496}
8497
8498long
217aa764 8499_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8500{
3a551c7a 8501 bfd_size_type symcount;
252b5132
RH
8502 long symtab_size;
8503 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8504
8505 if (elf_dynsymtab (abfd) == 0)
8506 {
8507 bfd_set_error (bfd_error_invalid_operation);
8508 return -1;
8509 }
8510
8511 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8512 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8513 {
8514 bfd_set_error (bfd_error_file_too_big);
8515 return -1;
8516 }
b5f386d5
AM
8517 symtab_size = symcount * (sizeof (asymbol *));
8518 if (symcount == 0)
8519 symtab_size = sizeof (asymbol *);
8520 else if (!bfd_write_p (abfd))
8521 {
8522 ufile_ptr filesize = bfd_get_file_size (abfd);
8523
8524 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8525 {
8526 bfd_set_error (bfd_error_file_truncated);
8527 return -1;
8528 }
8529 }
252b5132
RH
8530
8531 return symtab_size;
8532}
8533
8534long
3c568b8a 8535_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8536{
b5f386d5 8537 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8538 {
8539 /* Sanity check reloc section size. */
8540 struct bfd_elf_section_data *d = elf_section_data (asect);
8541 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8542 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8543 ufile_ptr filesize = bfd_get_file_size (abfd);
8544
8545 if (filesize != 0 && ext_rel_size > filesize)
8546 {
8547 bfd_set_error (bfd_error_file_truncated);
8548 return -1;
8549 }
8550 }
8551
242a1159 8552#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8553 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8554 {
8555 bfd_set_error (bfd_error_file_too_big);
8556 return -1;
8557 }
242a1159 8558#endif
252b5132
RH
8559 return (asect->reloc_count + 1) * sizeof (arelent *);
8560}
8561
8562/* Canonicalize the relocs. */
8563
8564long
217aa764
AM
8565_bfd_elf_canonicalize_reloc (bfd *abfd,
8566 sec_ptr section,
8567 arelent **relptr,
8568 asymbol **symbols)
252b5132
RH
8569{
8570 arelent *tblptr;
8571 unsigned int i;
9c5bfbb7 8572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8573
0a1b45a2 8574 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
8575 return -1;
8576
8577 tblptr = section->relocation;
8578 for (i = 0; i < section->reloc_count; i++)
8579 *relptr++ = tblptr++;
8580
8581 *relptr = NULL;
8582
8583 return section->reloc_count;
8584}
8585
8586long
6cee3f79 8587_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8588{
9c5bfbb7 8589 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8590 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
252b5132
RH
8591
8592 if (symcount >= 0)
ed48ec2e 8593 abfd->symcount = symcount;
252b5132
RH
8594 return symcount;
8595}
8596
8597long
217aa764
AM
8598_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8599 asymbol **allocation)
252b5132 8600{
9c5bfbb7 8601 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8602 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
1f70368c
DJ
8603
8604 if (symcount >= 0)
ed48ec2e 8605 abfd->dynsymcount = symcount;
1f70368c 8606 return symcount;
252b5132
RH
8607}
8608
8615f3f2
AM
8609/* Return the size required for the dynamic reloc entries. Any loadable
8610 section that was actually installed in the BFD, and has type SHT_REL
8611 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8612 dynamic reloc section. */
252b5132
RH
8613
8614long
217aa764 8615_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8616{
3c568b8a 8617 bfd_size_type count, ext_rel_size;
252b5132
RH
8618 asection *s;
8619
8620 if (elf_dynsymtab (abfd) == 0)
8621 {
8622 bfd_set_error (bfd_error_invalid_operation);
8623 return -1;
8624 }
8625
3a551c7a 8626 count = 1;
3c568b8a 8627 ext_rel_size = 0;
252b5132 8628 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8629 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8630 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8631 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8632 {
3c568b8a
AM
8633 ext_rel_size += s->size;
8634 if (ext_rel_size < s->size)
8635 {
8636 bfd_set_error (bfd_error_file_truncated);
8637 return -1;
8638 }
3a551c7a
AM
8639 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8640 if (count > LONG_MAX / sizeof (arelent *))
8641 {
8642 bfd_set_error (bfd_error_file_too_big);
8643 return -1;
8644 }
8645 }
b5f386d5 8646 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8647 {
8648 /* Sanity check reloc section sizes. */
8649 ufile_ptr filesize = bfd_get_file_size (abfd);
8650 if (filesize != 0 && ext_rel_size > filesize)
8651 {
8652 bfd_set_error (bfd_error_file_truncated);
8653 return -1;
8654 }
8655 }
3a551c7a 8656 return count * sizeof (arelent *);
252b5132
RH
8657}
8658
8615f3f2
AM
8659/* Canonicalize the dynamic relocation entries. Note that we return the
8660 dynamic relocations as a single block, although they are actually
8661 associated with particular sections; the interface, which was
8662 designed for SunOS style shared libraries, expects that there is only
8663 one set of dynamic relocs. Any loadable section that was actually
8664 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8665 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8666
8667long
217aa764
AM
8668_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8669 arelent **storage,
8670 asymbol **syms)
252b5132 8671{
0a1b45a2 8672 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
252b5132
RH
8673 asection *s;
8674 long ret;
8675
8676 if (elf_dynsymtab (abfd) == 0)
8677 {
8678 bfd_set_error (bfd_error_invalid_operation);
8679 return -1;
8680 }
8681
8682 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8683 ret = 0;
8684 for (s = abfd->sections; s != NULL; s = s->next)
8685 {
266b05cf 8686 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8687 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8688 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8689 {
8690 arelent *p;
8691 long count, i;
8692
0a1b45a2 8693 if (! (*slurp_relocs) (abfd, s, syms, true))
252b5132 8694 return -1;
eea6121a 8695 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8696 p = s->relocation;
8697 for (i = 0; i < count; i++)
8698 *storage++ = p++;
8699 ret += count;
8700 }
8701 }
8702
8703 *storage = NULL;
8704
8705 return ret;
8706}
8707\f
8708/* Read in the version information. */
8709
0a1b45a2
AM
8710bool
8711_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
252b5132
RH
8712{
8713 bfd_byte *contents = NULL;
fc0e6df6 8714 unsigned int freeidx = 0;
1f4361a7 8715 size_t amt;
fc0e6df6
PB
8716
8717 if (elf_dynverref (abfd) != 0)
8718 {
8719 Elf_Internal_Shdr *hdr;
8720 Elf_External_Verneed *everneed;
8721 Elf_Internal_Verneed *iverneed;
8722 unsigned int i;
d0fb9a8d 8723 bfd_byte *contents_end;
fc0e6df6
PB
8724
8725 hdr = &elf_tdata (abfd)->dynverref_hdr;
8726
bd61e135
AM
8727 if (hdr->sh_info == 0
8728 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8729 {
dc1e8a47 8730 error_return_bad_verref:
4eca0228 8731 _bfd_error_handler
871b3ab2 8732 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8733 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8734 error_return_verref:
d0fb9a8d
JJ
8735 elf_tdata (abfd)->verref = NULL;
8736 elf_tdata (abfd)->cverrefs = 0;
8737 goto error_return;
8738 }
601a03ba 8739
2bb3687b
AM
8740 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8741 goto error_return_verref;
8742 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8743 if (contents == NULL)
d0fb9a8d 8744 goto error_return_verref;
fc0e6df6 8745
1f4361a7
AM
8746 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8747 {
8748 bfd_set_error (bfd_error_file_too_big);
8749 goto error_return_verref;
8750 }
8751 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8752 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8753 goto error_return_verref;
8754
8755 BFD_ASSERT (sizeof (Elf_External_Verneed)
8756 == sizeof (Elf_External_Vernaux));
8757 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8758 everneed = (Elf_External_Verneed *) contents;
8759 iverneed = elf_tdata (abfd)->verref;
8760 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8761 {
8762 Elf_External_Vernaux *evernaux;
8763 Elf_Internal_Vernaux *ivernaux;
8764 unsigned int j;
8765
8766 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8767
8768 iverneed->vn_bfd = abfd;
8769
8770 iverneed->vn_filename =
8771 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8772 iverneed->vn_file);
8773 if (iverneed->vn_filename == NULL)
601a03ba 8774 goto error_return_bad_verref;
fc0e6df6 8775
d0fb9a8d
JJ
8776 if (iverneed->vn_cnt == 0)
8777 iverneed->vn_auxptr = NULL;
8778 else
8779 {
1f4361a7
AM
8780 if (_bfd_mul_overflow (iverneed->vn_cnt,
8781 sizeof (Elf_Internal_Vernaux), &amt))
8782 {
8783 bfd_set_error (bfd_error_file_too_big);
8784 goto error_return_verref;
8785 }
a50b1753 8786 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8787 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8788 if (iverneed->vn_auxptr == NULL)
8789 goto error_return_verref;
8790 }
8791
8792 if (iverneed->vn_aux
8793 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8794 goto error_return_bad_verref;
fc0e6df6
PB
8795
8796 evernaux = ((Elf_External_Vernaux *)
8797 ((bfd_byte *) everneed + iverneed->vn_aux));
8798 ivernaux = iverneed->vn_auxptr;
8799 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8800 {
8801 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8802
8803 ivernaux->vna_nodename =
8804 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8805 ivernaux->vna_name);
8806 if (ivernaux->vna_nodename == NULL)
601a03ba 8807 goto error_return_bad_verref;
fc0e6df6 8808
25ff461f
AM
8809 if (ivernaux->vna_other > freeidx)
8810 freeidx = ivernaux->vna_other;
8811
8812 ivernaux->vna_nextptr = NULL;
8813 if (ivernaux->vna_next == 0)
8814 {
8815 iverneed->vn_cnt = j + 1;
8816 break;
8817 }
fc0e6df6
PB
8818 if (j + 1 < iverneed->vn_cnt)
8819 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8820
d0fb9a8d
JJ
8821 if (ivernaux->vna_next
8822 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8823 goto error_return_bad_verref;
d0fb9a8d 8824
fc0e6df6
PB
8825 evernaux = ((Elf_External_Vernaux *)
8826 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8827 }
8828
25ff461f
AM
8829 iverneed->vn_nextref = NULL;
8830 if (iverneed->vn_next == 0)
8831 break;
fc0e6df6
PB
8832 if (i + 1 < hdr->sh_info)
8833 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8834
d0fb9a8d
JJ
8835 if (iverneed->vn_next
8836 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8837 goto error_return_bad_verref;
d0fb9a8d 8838
fc0e6df6
PB
8839 everneed = ((Elf_External_Verneed *)
8840 ((bfd_byte *) everneed + iverneed->vn_next));
8841 }
25ff461f 8842 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8843
8844 free (contents);
8845 contents = NULL;
8846 }
252b5132
RH
8847
8848 if (elf_dynverdef (abfd) != 0)
8849 {
8850 Elf_Internal_Shdr *hdr;
8851 Elf_External_Verdef *everdef;
8852 Elf_Internal_Verdef *iverdef;
f631889e
UD
8853 Elf_Internal_Verdef *iverdefarr;
8854 Elf_Internal_Verdef iverdefmem;
252b5132 8855 unsigned int i;
062e2358 8856 unsigned int maxidx;
d0fb9a8d 8857 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8858
8859 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8860
601a03ba
AM
8861 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8862 {
8863 error_return_bad_verdef:
4eca0228 8864 _bfd_error_handler
871b3ab2 8865 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8866 bfd_set_error (bfd_error_bad_value);
8867 error_return_verdef:
8868 elf_tdata (abfd)->verdef = NULL;
8869 elf_tdata (abfd)->cverdefs = 0;
8870 goto error_return;
8871 }
8872
2bb3687b 8873 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8874 goto error_return_verdef;
2bb3687b
AM
8875 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8876 if (contents == NULL)
601a03ba 8877 goto error_return_verdef;
d0fb9a8d
JJ
8878
8879 BFD_ASSERT (sizeof (Elf_External_Verdef)
8880 >= sizeof (Elf_External_Verdaux));
8881 contents_end_def = contents + hdr->sh_size
8882 - sizeof (Elf_External_Verdef);
8883 contents_end_aux = contents + hdr->sh_size
8884 - sizeof (Elf_External_Verdaux);
8885
f631889e
UD
8886 /* We know the number of entries in the section but not the maximum
8887 index. Therefore we have to run through all entries and find
8888 the maximum. */
252b5132 8889 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8890 maxidx = 0;
8891 for (i = 0; i < hdr->sh_info; ++i)
8892 {
8893 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8894
601a03ba
AM
8895 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8896 goto error_return_bad_verdef;
062e2358
AM
8897 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8898 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8899
25ff461f
AM
8900 if (iverdefmem.vd_next == 0)
8901 break;
8902
d0fb9a8d
JJ
8903 if (iverdefmem.vd_next
8904 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8905 goto error_return_bad_verdef;
d0fb9a8d 8906
f631889e
UD
8907 everdef = ((Elf_External_Verdef *)
8908 ((bfd_byte *) everdef + iverdefmem.vd_next));
8909 }
8910
fc0e6df6
PB
8911 if (default_imported_symver)
8912 {
8913 if (freeidx > maxidx)
8914 maxidx = ++freeidx;
8915 else
8916 freeidx = ++maxidx;
8917 }
1f4361a7
AM
8918 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8919 {
8920 bfd_set_error (bfd_error_file_too_big);
8921 goto error_return_verdef;
8922 }
8923 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 8924 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8925 goto error_return_verdef;
f631889e
UD
8926
8927 elf_tdata (abfd)->cverdefs = maxidx;
8928
8929 everdef = (Elf_External_Verdef *) contents;
8930 iverdefarr = elf_tdata (abfd)->verdef;
8931 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8932 {
8933 Elf_External_Verdaux *everdaux;
8934 Elf_Internal_Verdaux *iverdaux;
8935 unsigned int j;
8936
f631889e
UD
8937 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8938
d0fb9a8d 8939 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8940 goto error_return_bad_verdef;
d0fb9a8d 8941
f631889e 8942 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8943 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8944
8945 iverdef->vd_bfd = abfd;
8946
d0fb9a8d
JJ
8947 if (iverdef->vd_cnt == 0)
8948 iverdef->vd_auxptr = NULL;
8949 else
8950 {
1f4361a7
AM
8951 if (_bfd_mul_overflow (iverdef->vd_cnt,
8952 sizeof (Elf_Internal_Verdaux), &amt))
8953 {
8954 bfd_set_error (bfd_error_file_too_big);
8955 goto error_return_verdef;
8956 }
a50b1753 8957 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 8958 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8959 if (iverdef->vd_auxptr == NULL)
8960 goto error_return_verdef;
8961 }
8962
8963 if (iverdef->vd_aux
8964 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8965 goto error_return_bad_verdef;
252b5132
RH
8966
8967 everdaux = ((Elf_External_Verdaux *)
8968 ((bfd_byte *) everdef + iverdef->vd_aux));
8969 iverdaux = iverdef->vd_auxptr;
8970 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8971 {
8972 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8973
8974 iverdaux->vda_nodename =
8975 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8976 iverdaux->vda_name);
8977 if (iverdaux->vda_nodename == NULL)
601a03ba 8978 goto error_return_bad_verdef;
252b5132 8979
25ff461f
AM
8980 iverdaux->vda_nextptr = NULL;
8981 if (iverdaux->vda_next == 0)
8982 {
8983 iverdef->vd_cnt = j + 1;
8984 break;
8985 }
252b5132
RH
8986 if (j + 1 < iverdef->vd_cnt)
8987 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8988
d0fb9a8d
JJ
8989 if (iverdaux->vda_next
8990 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8991 goto error_return_bad_verdef;
d0fb9a8d 8992
252b5132
RH
8993 everdaux = ((Elf_External_Verdaux *)
8994 ((bfd_byte *) everdaux + iverdaux->vda_next));
8995 }
8996
595bce75 8997 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8998 if (iverdef->vd_cnt)
8999 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 9000
25ff461f
AM
9001 iverdef->vd_nextdef = NULL;
9002 if (iverdef->vd_next == 0)
9003 break;
d0fb9a8d 9004 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 9005 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
9006
9007 everdef = ((Elf_External_Verdef *)
9008 ((bfd_byte *) everdef + iverdef->vd_next));
9009 }
9010
9011 free (contents);
9012 contents = NULL;
9013 }
fc0e6df6 9014 else if (default_imported_symver)
252b5132 9015 {
fc0e6df6
PB
9016 if (freeidx < 3)
9017 freeidx = 3;
9018 else
9019 freeidx++;
252b5132 9020
1f4361a7
AM
9021 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9022 {
9023 bfd_set_error (bfd_error_file_too_big);
9024 goto error_return;
9025 }
9026 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9027 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9028 goto error_return;
9029
fc0e6df6
PB
9030 elf_tdata (abfd)->cverdefs = freeidx;
9031 }
252b5132 9032
fc0e6df6
PB
9033 /* Create a default version based on the soname. */
9034 if (default_imported_symver)
9035 {
9036 Elf_Internal_Verdef *iverdef;
9037 Elf_Internal_Verdaux *iverdaux;
252b5132 9038
5bb3703f 9039 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9040
fc0e6df6
PB
9041 iverdef->vd_version = VER_DEF_CURRENT;
9042 iverdef->vd_flags = 0;
9043 iverdef->vd_ndx = freeidx;
9044 iverdef->vd_cnt = 1;
252b5132 9045
fc0e6df6 9046 iverdef->vd_bfd = abfd;
252b5132 9047
fc0e6df6
PB
9048 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9049 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9050 goto error_return_verdef;
fc0e6df6 9051 iverdef->vd_nextdef = NULL;
601a03ba
AM
9052 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9053 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9054 if (iverdef->vd_auxptr == NULL)
9055 goto error_return_verdef;
252b5132 9056
fc0e6df6
PB
9057 iverdaux = iverdef->vd_auxptr;
9058 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9059 }
9060
0a1b45a2 9061 return true;
252b5132
RH
9062
9063 error_return:
c9594989 9064 free (contents);
0a1b45a2 9065 return false;
252b5132
RH
9066}
9067\f
9068asymbol *
217aa764 9069_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9070{
9071 elf_symbol_type *newsym;
9072
7a6e0d89 9073 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9074 if (!newsym)
9075 return NULL;
201159ec
NC
9076 newsym->symbol.the_bfd = abfd;
9077 return &newsym->symbol;
252b5132
RH
9078}
9079
9080void
217aa764
AM
9081_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9082 asymbol *symbol,
9083 symbol_info *ret)
252b5132
RH
9084{
9085 bfd_symbol_info (symbol, ret);
9086}
9087
9088/* Return whether a symbol name implies a local symbol. Most targets
9089 use this function for the is_local_label_name entry point, but some
9090 override it. */
9091
0a1b45a2 9092bool
217aa764
AM
9093_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9094 const char *name)
252b5132
RH
9095{
9096 /* Normal local symbols start with ``.L''. */
9097 if (name[0] == '.' && name[1] == 'L')
0a1b45a2 9098 return true;
252b5132
RH
9099
9100 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9101 DWARF debugging symbols starting with ``..''. */
9102 if (name[0] == '.' && name[1] == '.')
0a1b45a2 9103 return true;
252b5132
RH
9104
9105 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9106 emitting DWARF debugging output. I suspect this is actually a
9107 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9108 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9109 underscore to be emitted on some ELF targets). For ease of use,
9110 we treat such symbols as local. */
9111 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
0a1b45a2 9112 return true;
252b5132 9113
b1fa9dd6
NC
9114 /* Treat assembler generated fake symbols, dollar local labels and
9115 forward-backward labels (aka local labels) as locals.
9116 These labels have the form:
9117
07d6d2b8 9118 L0^A.* (fake symbols)
b1fa9dd6
NC
9119
9120 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9121
9122 Versions which start with .L will have already been matched above,
9123 so we only need to match the rest. */
9124 if (name[0] == 'L' && ISDIGIT (name[1]))
9125 {
0a1b45a2 9126 bool ret = false;
b1fa9dd6
NC
9127 const char * p;
9128 char c;
9129
9130 for (p = name + 2; (c = *p); p++)
9131 {
9132 if (c == 1 || c == 2)
9133 {
9134 if (c == 1 && p == name + 2)
9135 /* A fake symbol. */
0a1b45a2 9136 return true;
b1fa9dd6
NC
9137
9138 /* FIXME: We are being paranoid here and treating symbols like
9139 L0^Bfoo as if there were non-local, on the grounds that the
9140 assembler will never generate them. But can any symbol
9141 containing an ASCII value in the range 1-31 ever be anything
9142 other than some kind of local ? */
0a1b45a2 9143 ret = true;
b1fa9dd6
NC
9144 }
9145
9146 if (! ISDIGIT (c))
9147 {
0a1b45a2 9148 ret = false;
b1fa9dd6
NC
9149 break;
9150 }
9151 }
9152 return ret;
9153 }
ffa54770 9154
0a1b45a2 9155 return false;
252b5132
RH
9156}
9157
9158alent *
217aa764
AM
9159_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9160 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9161{
9162 abort ();
9163 return NULL;
9164}
9165
0a1b45a2 9166bool
217aa764
AM
9167_bfd_elf_set_arch_mach (bfd *abfd,
9168 enum bfd_architecture arch,
9169 unsigned long machine)
252b5132
RH
9170{
9171 /* If this isn't the right architecture for this backend, and this
9172 isn't the generic backend, fail. */
9173 if (arch != get_elf_backend_data (abfd)->arch
9174 && arch != bfd_arch_unknown
9175 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
0a1b45a2 9176 return false;
252b5132
RH
9177
9178 return bfd_default_set_arch_mach (abfd, arch, machine);
9179}
9180
d1fad7c6
NC
9181/* Find the nearest line to a particular section and offset,
9182 for error reporting. */
9183
0a1b45a2 9184bool
217aa764 9185_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9186 asymbol **symbols,
fb167eb2 9187 asection *section,
217aa764
AM
9188 bfd_vma offset,
9189 const char **filename_ptr,
9190 const char **functionname_ptr,
fb167eb2
AM
9191 unsigned int *line_ptr,
9192 unsigned int *discriminator_ptr)
d1fad7c6 9193{
0a1b45a2 9194 bool found;
d1fad7c6 9195
fb167eb2 9196 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9197 filename_ptr, functionname_ptr,
fb167eb2 9198 line_ptr, discriminator_ptr,
9defd221 9199 dwarf_debug_sections,
e7679060 9200 &elf_tdata (abfd)->dwarf2_find_line_info))
0a1b45a2 9201 return true;
e7679060
AM
9202
9203 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9204 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9205 {
9206 if (!*functionname_ptr)
e00e8198
AM
9207 _bfd_elf_find_function (abfd, symbols, section, offset,
9208 *filename_ptr ? NULL : filename_ptr,
9209 functionname_ptr);
0a1b45a2 9210 return true;
d1fad7c6
NC
9211 }
9212
9213 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9214 &found, filename_ptr,
9215 functionname_ptr, line_ptr,
9216 &elf_tdata (abfd)->line_info))
0a1b45a2 9217 return false;
dc43ada5 9218 if (found && (*functionname_ptr || *line_ptr))
0a1b45a2 9219 return true;
d1fad7c6
NC
9220
9221 if (symbols == NULL)
0a1b45a2 9222 return false;
d1fad7c6 9223
e00e8198
AM
9224 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9225 filename_ptr, functionname_ptr))
0a1b45a2 9226 return false;
d1fad7c6 9227
252b5132 9228 *line_ptr = 0;
0a1b45a2 9229 return true;
252b5132
RH
9230}
9231
5420f73d
L
9232/* Find the line for a symbol. */
9233
0a1b45a2 9234bool
5420f73d
L
9235_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9236 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9237{
fb167eb2
AM
9238 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9239 filename_ptr, NULL, line_ptr, NULL,
9defd221 9240 dwarf_debug_sections,
fb167eb2 9241 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9242}
9243
4ab527b0
FF
9244/* After a call to bfd_find_nearest_line, successive calls to
9245 bfd_find_inliner_info can be used to get source information about
9246 each level of function inlining that terminated at the address
9247 passed to bfd_find_nearest_line. Currently this is only supported
9248 for DWARF2 with appropriate DWARF3 extensions. */
9249
0a1b45a2 9250bool
4ab527b0
FF
9251_bfd_elf_find_inliner_info (bfd *abfd,
9252 const char **filename_ptr,
9253 const char **functionname_ptr,
9254 unsigned int *line_ptr)
9255{
0a1b45a2 9256 bool found;
4ab527b0
FF
9257 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9258 functionname_ptr, line_ptr,
9259 & elf_tdata (abfd)->dwarf2_find_line_info);
9260 return found;
9261}
9262
252b5132 9263int
a6b96beb 9264_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9265{
8ded5a0f
AM
9266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9267 int ret = bed->s->sizeof_ehdr;
252b5132 9268
0e1862bb 9269 if (!bfd_link_relocatable (info))
8ded5a0f 9270 {
12bd6957 9271 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9272
62d7a5f6
AM
9273 if (phdr_size == (bfd_size_type) -1)
9274 {
9275 struct elf_segment_map *m;
9276
9277 phdr_size = 0;
12bd6957 9278 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9279 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9280
62d7a5f6
AM
9281 if (phdr_size == 0)
9282 phdr_size = get_program_header_size (abfd, info);
9283 }
8ded5a0f 9284
12bd6957 9285 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9286 ret += phdr_size;
9287 }
9288
252b5132
RH
9289 return ret;
9290}
9291
0a1b45a2 9292bool
217aa764
AM
9293_bfd_elf_set_section_contents (bfd *abfd,
9294 sec_ptr section,
0f867abe 9295 const void *location,
217aa764
AM
9296 file_ptr offset,
9297 bfd_size_type count)
252b5132
RH
9298{
9299 Elf_Internal_Shdr *hdr;
1b6aeedb 9300 file_ptr pos;
252b5132
RH
9301
9302 if (! abfd->output_has_begun
217aa764 9303 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 9304 return false;
252b5132 9305
0ce398f1 9306 if (!count)
0a1b45a2 9307 return true;
0ce398f1 9308
252b5132 9309 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9310 if (hdr->sh_offset == (file_ptr) -1)
9311 {
a0dcf297
NC
9312 unsigned char *contents;
9313
1ff6de03
NA
9314 if (bfd_section_is_ctf (section))
9315 /* Nothing to do with this section: the contents are generated
9316 later. */
0a1b45a2 9317 return true;
1ff6de03 9318
a0dcf297
NC
9319 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9320 {
9321 _bfd_error_handler
9322 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9323 abfd, section);
9324 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9325 return false;
a0dcf297
NC
9326 }
9327
9328 if ((offset + count) > hdr->sh_size)
9329 {
9330 _bfd_error_handler
9331 (_("%pB:%pA: error: attempting to write over the end of the section"),
9332 abfd, section);
9333
9334 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9335 return false;
a0dcf297
NC
9336 }
9337
9338 contents = hdr->contents;
9339 if (contents == NULL)
9340 {
9341 _bfd_error_handler
9342 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9343 abfd, section);
9344
9345 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9346 return false;
a0dcf297
NC
9347 }
9348
0ce398f1 9349 memcpy (contents + offset, location, count);
0a1b45a2 9350 return true;
0ce398f1 9351 }
a0dcf297 9352
dc810e39
AM
9353 pos = hdr->sh_offset + offset;
9354 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9355 || bfd_bwrite (location, count, abfd) != count)
0a1b45a2 9356 return false;
252b5132 9357
0a1b45a2 9358 return true;
252b5132
RH
9359}
9360
0a1b45a2 9361bool
217aa764
AM
9362_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9363 arelent *cache_ptr ATTRIBUTE_UNUSED,
9364 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9365{
9366 abort ();
0a1b45a2 9367 return false;
252b5132
RH
9368}
9369
252b5132
RH
9370/* Try to convert a non-ELF reloc into an ELF one. */
9371
0a1b45a2 9372bool
217aa764 9373_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9374{
c044fabd 9375 /* Check whether we really have an ELF howto. */
252b5132
RH
9376
9377 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9378 {
9379 bfd_reloc_code_real_type code;
9380 reloc_howto_type *howto;
9381
9382 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9383 equivalent ELF reloc. */
252b5132
RH
9384
9385 if (areloc->howto->pc_relative)
9386 {
9387 switch (areloc->howto->bitsize)
9388 {
9389 case 8:
9390 code = BFD_RELOC_8_PCREL;
9391 break;
9392 case 12:
9393 code = BFD_RELOC_12_PCREL;
9394 break;
9395 case 16:
9396 code = BFD_RELOC_16_PCREL;
9397 break;
9398 case 24:
9399 code = BFD_RELOC_24_PCREL;
9400 break;
9401 case 32:
9402 code = BFD_RELOC_32_PCREL;
9403 break;
9404 case 64:
9405 code = BFD_RELOC_64_PCREL;
9406 break;
9407 default:
9408 goto fail;
9409 }
9410
9411 howto = bfd_reloc_type_lookup (abfd, code);
9412
94698d01 9413 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9414 {
9415 if (howto->pcrel_offset)
9416 areloc->addend += areloc->address;
9417 else
9418 areloc->addend -= areloc->address; /* addend is unsigned!! */
9419 }
9420 }
9421 else
9422 {
9423 switch (areloc->howto->bitsize)
9424 {
9425 case 8:
9426 code = BFD_RELOC_8;
9427 break;
9428 case 14:
9429 code = BFD_RELOC_14;
9430 break;
9431 case 16:
9432 code = BFD_RELOC_16;
9433 break;
9434 case 26:
9435 code = BFD_RELOC_26;
9436 break;
9437 case 32:
9438 code = BFD_RELOC_32;
9439 break;
9440 case 64:
9441 code = BFD_RELOC_64;
9442 break;
9443 default:
9444 goto fail;
9445 }
9446
9447 howto = bfd_reloc_type_lookup (abfd, code);
9448 }
9449
9450 if (howto)
9451 areloc->howto = howto;
9452 else
9453 goto fail;
9454 }
9455
0a1b45a2 9456 return true;
252b5132
RH
9457
9458 fail:
0aa13fee
AM
9459 /* xgettext:c-format */
9460 _bfd_error_handler (_("%pB: %s unsupported"),
9461 abfd, areloc->howto->name);
9aea1e31 9462 bfd_set_error (bfd_error_sorry);
0a1b45a2 9463 return false;
252b5132
RH
9464}
9465
0a1b45a2 9466bool
217aa764 9467_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9468{
d9071b0c 9469 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9470 if (tdata != NULL
9471 && (bfd_get_format (abfd) == bfd_object
9472 || bfd_get_format (abfd) == bfd_core))
252b5132 9473 {
c0355132 9474 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9475 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9476 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9477 }
9478
9479 return _bfd_generic_close_and_cleanup (abfd);
9480}
9481
9482/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9483 in the relocation's offset. Thus we cannot allow any sort of sanity
9484 range-checking to interfere. There is nothing else to do in processing
9485 this reloc. */
9486
9487bfd_reloc_status_type
217aa764
AM
9488_bfd_elf_rel_vtable_reloc_fn
9489 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9490 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9491 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9492 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9493{
9494 return bfd_reloc_ok;
9495}
252b5132
RH
9496\f
9497/* Elf core file support. Much of this only works on native
9498 toolchains, since we rely on knowing the
9499 machine-dependent procfs structure in order to pick
c044fabd 9500 out details about the corefile. */
252b5132
RH
9501
9502#ifdef HAVE_SYS_PROCFS_H
9503# include <sys/procfs.h>
9504#endif
9505
261b8d08
PA
9506/* Return a PID that identifies a "thread" for threaded cores, or the
9507 PID of the main process for non-threaded cores. */
252b5132
RH
9508
9509static int
217aa764 9510elfcore_make_pid (bfd *abfd)
252b5132 9511{
261b8d08
PA
9512 int pid;
9513
228e534f 9514 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9515 if (pid == 0)
228e534f 9516 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9517
9518 return pid;
252b5132
RH
9519}
9520
252b5132
RH
9521/* If there isn't a section called NAME, make one, using
9522 data from SECT. Note, this function will generate a
9523 reference to NAME, so you shouldn't deallocate or
c044fabd 9524 overwrite it. */
252b5132 9525
0a1b45a2 9526static bool
217aa764 9527elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9528{
c044fabd 9529 asection *sect2;
252b5132
RH
9530
9531 if (bfd_get_section_by_name (abfd, name) != NULL)
0a1b45a2 9532 return true;
252b5132 9533
117ed4f8 9534 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9535 if (sect2 == NULL)
0a1b45a2 9536 return false;
252b5132 9537
eea6121a 9538 sect2->size = sect->size;
252b5132 9539 sect2->filepos = sect->filepos;
252b5132 9540 sect2->alignment_power = sect->alignment_power;
0a1b45a2 9541 return true;
252b5132
RH
9542}
9543
bb0082d6
AM
9544/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9545 actually creates up to two pseudosections:
9546 - For the single-threaded case, a section named NAME, unless
9547 such a section already exists.
9548 - For the multi-threaded case, a section named "NAME/PID", where
9549 PID is elfcore_make_pid (abfd).
24d3e51b 9550 Both pseudosections have identical contents. */
0a1b45a2 9551bool
217aa764
AM
9552_bfd_elfcore_make_pseudosection (bfd *abfd,
9553 char *name,
9554 size_t size,
9555 ufile_ptr filepos)
bb0082d6
AM
9556{
9557 char buf[100];
9558 char *threaded_name;
d4c88bbb 9559 size_t len;
bb0082d6
AM
9560 asection *sect;
9561
9562 /* Build the section name. */
9563
9564 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9565 len = strlen (buf) + 1;
a50b1753 9566 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9567 if (threaded_name == NULL)
0a1b45a2 9568 return false;
d4c88bbb 9569 memcpy (threaded_name, buf, len);
bb0082d6 9570
117ed4f8
AM
9571 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9572 SEC_HAS_CONTENTS);
bb0082d6 9573 if (sect == NULL)
0a1b45a2 9574 return false;
eea6121a 9575 sect->size = size;
bb0082d6 9576 sect->filepos = filepos;
bb0082d6
AM
9577 sect->alignment_power = 2;
9578
936e320b 9579 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9580}
9581
0a1b45a2 9582static bool
58e07198
CZ
9583elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9584 size_t offs)
9585{
9586 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9587 SEC_HAS_CONTENTS);
9588
9589 if (sect == NULL)
0a1b45a2 9590 return false;
58e07198
CZ
9591
9592 sect->size = note->descsz - offs;
9593 sect->filepos = note->descpos + offs;
9594 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9595
0a1b45a2 9596 return true;
58e07198
CZ
9597}
9598
252b5132 9599/* prstatus_t exists on:
4a938328 9600 solaris 2.5+
252b5132
RH
9601 linux 2.[01] + glibc
9602 unixware 4.2
9603*/
9604
9605#if defined (HAVE_PRSTATUS_T)
a7b97311 9606
0a1b45a2 9607static bool
217aa764 9608elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9609{
eea6121a 9610 size_t size;
7ee38065 9611 int offset;
252b5132 9612
4a938328
MS
9613 if (note->descsz == sizeof (prstatus_t))
9614 {
9615 prstatus_t prstat;
252b5132 9616
eea6121a 9617 size = sizeof (prstat.pr_reg);
7ee38065 9618 offset = offsetof (prstatus_t, pr_reg);
4a938328 9619 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9620
fa49d224
NC
9621 /* Do not overwrite the core signal if it
9622 has already been set by another thread. */
228e534f
AM
9623 if (elf_tdata (abfd)->core->signal == 0)
9624 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9625 if (elf_tdata (abfd)->core->pid == 0)
9626 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9627
4a938328
MS
9628 /* pr_who exists on:
9629 solaris 2.5+
9630 unixware 4.2
9631 pr_who doesn't exist on:
9632 linux 2.[01]
9633 */
252b5132 9634#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9635 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9636#else
228e534f 9637 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9638#endif
4a938328 9639 }
7ee38065 9640#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9641 else if (note->descsz == sizeof (prstatus32_t))
9642 {
9643 /* 64-bit host, 32-bit corefile */
9644 prstatus32_t prstat;
9645
eea6121a 9646 size = sizeof (prstat.pr_reg);
7ee38065 9647 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9648 memcpy (&prstat, note->descdata, sizeof (prstat));
9649
fa49d224
NC
9650 /* Do not overwrite the core signal if it
9651 has already been set by another thread. */
228e534f
AM
9652 if (elf_tdata (abfd)->core->signal == 0)
9653 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9654 if (elf_tdata (abfd)->core->pid == 0)
9655 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9656
9657 /* pr_who exists on:
9658 solaris 2.5+
9659 unixware 4.2
9660 pr_who doesn't exist on:
9661 linux 2.[01]
9662 */
7ee38065 9663#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9664 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9665#else
228e534f 9666 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9667#endif
9668 }
7ee38065 9669#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9670 else
9671 {
9672 /* Fail - we don't know how to handle any other
9673 note size (ie. data object type). */
0a1b45a2 9674 return true;
4a938328 9675 }
252b5132 9676
bb0082d6 9677 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9678 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9679 size, note->descpos + offset);
252b5132
RH
9680}
9681#endif /* defined (HAVE_PRSTATUS_T) */
9682
bb0082d6 9683/* Create a pseudosection containing the exact contents of NOTE. */
0a1b45a2 9684static bool
217aa764
AM
9685elfcore_make_note_pseudosection (bfd *abfd,
9686 char *name,
9687 Elf_Internal_Note *note)
252b5132 9688{
936e320b
AM
9689 return _bfd_elfcore_make_pseudosection (abfd, name,
9690 note->descsz, note->descpos);
252b5132
RH
9691}
9692
ff08c6bb
JB
9693/* There isn't a consistent prfpregset_t across platforms,
9694 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9695 data structure apart. */
9696
0a1b45a2 9697static bool
217aa764 9698elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9699{
9700 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9701}
9702
ff08c6bb 9703/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9704 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9705 literally. */
c044fabd 9706
0a1b45a2 9707static bool
217aa764 9708elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9709{
9710 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9711}
9712
4339cae0
L
9713/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9714 with a note type of NT_X86_XSTATE. Just include the whole note's
9715 contents literally. */
9716
0a1b45a2 9717static bool
4339cae0
L
9718elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9719{
9720 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9721}
9722
0a1b45a2 9723static bool
97753bd5
AM
9724elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9725{
9726 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9727}
9728
0a1b45a2 9729static bool
89eeb0bc
LM
9730elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9731{
9732 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9733}
97753bd5 9734
0a1b45a2 9735static bool
cb2366c1
EBM
9736elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9737{
9738 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9739}
9740
0a1b45a2 9741static bool
cb2366c1
EBM
9742elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9743{
9744 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9745}
9746
0a1b45a2 9747static bool
cb2366c1
EBM
9748elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9749{
9750 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9751}
9752
0a1b45a2 9753static bool
cb2366c1
EBM
9754elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9755{
9756 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9757}
9758
0a1b45a2 9759static bool
cb2366c1
EBM
9760elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9761{
9762 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9763}
9764
0a1b45a2 9765static bool
cb2366c1
EBM
9766elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9767{
9768 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9769}
9770
0a1b45a2 9771static bool
cb2366c1
EBM
9772elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9773{
9774 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9775}
9776
0a1b45a2 9777static bool
cb2366c1
EBM
9778elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9779{
9780 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9781}
9782
0a1b45a2 9783static bool
cb2366c1
EBM
9784elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9785{
9786 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9787}
9788
0a1b45a2 9789static bool
cb2366c1
EBM
9790elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9791{
9792 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9793}
9794
0a1b45a2 9795static bool
cb2366c1
EBM
9796elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9797{
9798 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9799}
9800
0a1b45a2 9801static bool
cb2366c1
EBM
9802elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9803{
9804 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9805}
9806
0a1b45a2 9807static bool
cb2366c1
EBM
9808elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9809{
9810 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9811}
9812
0a1b45a2 9813static bool
0675e188
UW
9814elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9815{
9816 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9817}
9818
0a1b45a2 9819static bool
d7eeb400
MS
9820elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9821{
9822 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9823}
9824
0a1b45a2 9825static bool
d7eeb400
MS
9826elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9827{
9828 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9829}
9830
0a1b45a2 9831static bool
d7eeb400
MS
9832elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9833{
9834 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9835}
9836
0a1b45a2 9837static bool
d7eeb400
MS
9838elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9839{
9840 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9841}
9842
0a1b45a2 9843static bool
d7eeb400
MS
9844elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9845{
9846 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9847}
9848
0a1b45a2 9849static bool
355b81d9
UW
9850elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9851{
9852 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9853}
9854
0a1b45a2 9855static bool
355b81d9
UW
9856elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9857{
9858 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9859}
9860
0a1b45a2 9861static bool
abb3f6cc
NC
9862elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9863{
9864 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9865}
9866
0a1b45a2 9867static bool
4ef9f41a
AA
9868elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9869{
9870 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9871}
9872
0a1b45a2 9873static bool
4ef9f41a
AA
9874elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9875{
9876 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9877}
9878
0a1b45a2 9879static bool
88ab90e8
AA
9880elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9881{
9882 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9883}
9884
0a1b45a2 9885static bool
88ab90e8
AA
9886elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9887{
9888 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9889}
9890
0a1b45a2 9891static bool
faa9a424
UW
9892elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9893{
9894 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9895}
9896
0a1b45a2 9897static bool
652451f8
YZ
9898elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9899{
9900 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9901}
9902
0a1b45a2 9903static bool
652451f8
YZ
9904elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9905{
9906 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9907}
9908
0a1b45a2 9909static bool
652451f8
YZ
9910elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9911{
9912 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9913}
9914
0a1b45a2 9915static bool
ad1cc4e4
AH
9916elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9917{
9918 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9919}
9920
0a1b45a2 9921static bool
e6c3b5bf
AH
9922elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9923{
9924 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9925}
9926
0a1b45a2 9927static bool
27456742
AK
9928elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9929{
9930 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9931}
9932
db6092f3
AB
9933/* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
9934 successful otherwise, return FALSE. */
9935
0a1b45a2 9936static bool
db6092f3
AB
9937elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
9938{
9939 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
9940}
9941
b63a5e38
AB
9942/* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
9943 successful otherwise, return FALSE. */
9944
0a1b45a2 9945static bool
b63a5e38
AB
9946elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
9947{
9948 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
9949}
9950
252b5132 9951#if defined (HAVE_PRPSINFO_T)
4a938328 9952typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9953#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9954typedef prpsinfo32_t elfcore_psinfo32_t;
9955#endif
252b5132
RH
9956#endif
9957
9958#if defined (HAVE_PSINFO_T)
4a938328 9959typedef psinfo_t elfcore_psinfo_t;
7ee38065 9960#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9961typedef psinfo32_t elfcore_psinfo32_t;
9962#endif
252b5132
RH
9963#endif
9964
252b5132
RH
9965/* return a malloc'ed copy of a string at START which is at
9966 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9967 the copy will always have a terminating '\0'. */
252b5132 9968
936e320b 9969char *
217aa764 9970_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9971{
dc810e39 9972 char *dups;
a50b1753 9973 char *end = (char *) memchr (start, '\0', max);
dc810e39 9974 size_t len;
252b5132
RH
9975
9976 if (end == NULL)
9977 len = max;
9978 else
9979 len = end - start;
9980
a50b1753 9981 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9982 if (dups == NULL)
252b5132
RH
9983 return NULL;
9984
dc810e39
AM
9985 memcpy (dups, start, len);
9986 dups[len] = '\0';
252b5132 9987
dc810e39 9988 return dups;
252b5132
RH
9989}
9990
bb0082d6 9991#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
0a1b45a2 9992static bool
217aa764 9993elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9994{
4a938328
MS
9995 if (note->descsz == sizeof (elfcore_psinfo_t))
9996 {
9997 elfcore_psinfo_t psinfo;
252b5132 9998
7ee38065 9999 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10000
335e41d4 10001#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 10002 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10003#endif
228e534f 10004 elf_tdata (abfd)->core->program
936e320b
AM
10005 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10006 sizeof (psinfo.pr_fname));
252b5132 10007
228e534f 10008 elf_tdata (abfd)->core->command
936e320b
AM
10009 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10010 sizeof (psinfo.pr_psargs));
4a938328 10011 }
7ee38065 10012#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
10013 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10014 {
10015 /* 64-bit host, 32-bit corefile */
10016 elfcore_psinfo32_t psinfo;
10017
7ee38065 10018 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10019
335e41d4 10020#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 10021 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10022#endif
228e534f 10023 elf_tdata (abfd)->core->program
936e320b
AM
10024 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10025 sizeof (psinfo.pr_fname));
4a938328 10026
228e534f 10027 elf_tdata (abfd)->core->command
936e320b
AM
10028 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10029 sizeof (psinfo.pr_psargs));
4a938328
MS
10030 }
10031#endif
10032
10033 else
10034 {
10035 /* Fail - we don't know how to handle any other
10036 note size (ie. data object type). */
0a1b45a2 10037 return true;
4a938328 10038 }
252b5132
RH
10039
10040 /* Note that for some reason, a spurious space is tacked
10041 onto the end of the args in some (at least one anyway)
c044fabd 10042 implementations, so strip it off if it exists. */
252b5132
RH
10043
10044 {
228e534f 10045 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10046 int n = strlen (command);
10047
10048 if (0 < n && command[n - 1] == ' ')
10049 command[n - 1] = '\0';
10050 }
10051
0a1b45a2 10052 return true;
252b5132
RH
10053}
10054#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10055
252b5132 10056#if defined (HAVE_PSTATUS_T)
0a1b45a2 10057static bool
217aa764 10058elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10059{
f572a39d
AM
10060 if (note->descsz == sizeof (pstatus_t)
10061#if defined (HAVE_PXSTATUS_T)
10062 || note->descsz == sizeof (pxstatus_t)
10063#endif
10064 )
4a938328
MS
10065 {
10066 pstatus_t pstat;
252b5132 10067
4a938328 10068 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10069
228e534f 10070 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10071 }
7ee38065 10072#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10073 else if (note->descsz == sizeof (pstatus32_t))
10074 {
10075 /* 64-bit host, 32-bit corefile */
10076 pstatus32_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
MS
10081 }
10082#endif
252b5132
RH
10083 /* Could grab some more details from the "representative"
10084 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10085 NT_LWPSTATUS note, presumably. */
252b5132 10086
0a1b45a2 10087 return true;
252b5132
RH
10088}
10089#endif /* defined (HAVE_PSTATUS_T) */
10090
252b5132 10091#if defined (HAVE_LWPSTATUS_T)
0a1b45a2 10092static bool
217aa764 10093elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10094{
10095 lwpstatus_t lwpstat;
10096 char buf[100];
c044fabd 10097 char *name;
d4c88bbb 10098 size_t len;
c044fabd 10099 asection *sect;
252b5132 10100
f572a39d
AM
10101 if (note->descsz != sizeof (lwpstat)
10102#if defined (HAVE_LWPXSTATUS_T)
10103 && note->descsz != sizeof (lwpxstatus_t)
10104#endif
10105 )
0a1b45a2 10106 return true;
252b5132
RH
10107
10108 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10109
228e534f 10110 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10111 /* Do not overwrite the core signal if it has already been set by
10112 another thread. */
228e534f
AM
10113 if (elf_tdata (abfd)->core->signal == 0)
10114 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10115
c044fabd 10116 /* Make a ".reg/999" section. */
252b5132
RH
10117
10118 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10119 len = strlen (buf) + 1;
217aa764 10120 name = bfd_alloc (abfd, len);
252b5132 10121 if (name == NULL)
0a1b45a2 10122 return false;
d4c88bbb 10123 memcpy (name, buf, len);
252b5132 10124
117ed4f8 10125 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10126 if (sect == NULL)
0a1b45a2 10127 return false;
252b5132
RH
10128
10129#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10130 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10131 sect->filepos = note->descpos
10132 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10133#endif
10134
10135#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10136 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10137 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10138#endif
10139
252b5132
RH
10140 sect->alignment_power = 2;
10141
10142 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10143 return false;
252b5132
RH
10144
10145 /* Make a ".reg2/999" section */
10146
10147 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10148 len = strlen (buf) + 1;
217aa764 10149 name = bfd_alloc (abfd, len);
252b5132 10150 if (name == NULL)
0a1b45a2 10151 return false;
d4c88bbb 10152 memcpy (name, buf, len);
252b5132 10153
117ed4f8 10154 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10155 if (sect == NULL)
0a1b45a2 10156 return false;
252b5132
RH
10157
10158#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10159 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10160 sect->filepos = note->descpos
10161 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10162#endif
10163
10164#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10165 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10166 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10167#endif
10168
252b5132
RH
10169 sect->alignment_power = 2;
10170
936e320b 10171 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10172}
10173#endif /* defined (HAVE_LWPSTATUS_T) */
10174
8fbac78b
JT
10175/* These constants, and the structure offsets used below, are defined by
10176 Cygwin's core_dump.h */
10177#define NOTE_INFO_PROCESS 1
10178#define NOTE_INFO_THREAD 2
10179#define NOTE_INFO_MODULE 3
d61f3d03 10180#define NOTE_INFO_MODULE64 4
8fbac78b 10181
0a1b45a2 10182static bool
217aa764 10183elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10184{
10185 char buf[30];
c044fabd 10186 char *name;
d4c88bbb 10187 size_t len;
3cdad084 10188 unsigned int name_size;
c044fabd 10189 asection *sect;
2fef9373 10190 unsigned int type;
4a6636fb
PA
10191 int is_active_thread;
10192 bfd_vma base_addr;
16e9c715 10193
04ec0fa2 10194 if (note->descsz < 4)
0a1b45a2 10195 return true;
16e9c715 10196
08dedd66 10197 if (! startswith (note->namedata, "win32"))
0a1b45a2 10198 return true;
4a6636fb
PA
10199
10200 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10201
7e0d77ef
NC
10202 struct
10203 {
404ec933
JT
10204 const char *type_name;
10205 unsigned long min_size;
10206 } size_check[] =
10207 {
10208 { "NOTE_INFO_PROCESS", 12 },
10209 { "NOTE_INFO_THREAD", 12 },
10210 { "NOTE_INFO_MODULE", 12 },
10211 { "NOTE_INFO_MODULE64", 16 },
10212 };
10213
7e0d77ef 10214 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
0a1b45a2 10215 return true;
404ec933
JT
10216
10217 if (note->descsz < size_check[type - 1].min_size)
10218 {
10219 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10220 abfd, size_check[type - 1].type_name, note->descsz);
0a1b45a2 10221 return true;
404ec933
JT
10222 }
10223
4a6636fb 10224 switch (type)
16e9c715 10225 {
8fbac78b 10226 case NOTE_INFO_PROCESS:
228e534f 10227 /* FIXME: need to add ->core->command. */
ff2084b9 10228 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10229 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10230 break;
16e9c715 10231
8fbac78b 10232 case NOTE_INFO_THREAD:
ff2084b9
JT
10233 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10234 structure. */
4a6636fb 10235 /* thread_info.tid */
ff2084b9 10236 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10237
d4c88bbb 10238 len = strlen (buf) + 1;
a50b1753 10239 name = (char *) bfd_alloc (abfd, len);
16e9c715 10240 if (name == NULL)
0a1b45a2 10241 return false;
c044fabd 10242
d4c88bbb 10243 memcpy (name, buf, len);
16e9c715 10244
117ed4f8 10245 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10246 if (sect == NULL)
0a1b45a2 10247 return false;
c044fabd 10248
4a6636fb 10249 /* sizeof (thread_info.thread_context) */
03c29a6f 10250 sect->size = note->descsz - 12;
4a6636fb
PA
10251 /* offsetof (thread_info.thread_context) */
10252 sect->filepos = note->descpos + 12;
16e9c715
NC
10253 sect->alignment_power = 2;
10254
4a6636fb
PA
10255 /* thread_info.is_active_thread */
10256 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10257
10258 if (is_active_thread)
16e9c715 10259 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10260 return false;
16e9c715
NC
10261 break;
10262
8fbac78b 10263 case NOTE_INFO_MODULE:
d61f3d03 10264 case NOTE_INFO_MODULE64:
16e9c715 10265 /* Make a ".module/xxxxxxxx" section. */
d61f3d03
JT
10266 if (type == NOTE_INFO_MODULE)
10267 {
d61f3d03
JT
10268 /* module_info.base_address */
10269 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10270 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10271 /* module_info.module_name_size */
10272 name_size = bfd_get_32 (abfd, note->descdata + 8);
10273 }
10274 else /* NOTE_INFO_MODULE64 */
10275 {
d61f3d03
JT
10276 /* module_info.base_address */
10277 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10278 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10279 /* module_info.module_name_size */
10280 name_size = bfd_get_32 (abfd, note->descdata + 12);
10281 }
c044fabd 10282
d4c88bbb 10283 len = strlen (buf) + 1;
a50b1753 10284 name = (char *) bfd_alloc (abfd, len);
16e9c715 10285 if (name == NULL)
0a1b45a2 10286 return false;
c044fabd 10287
d4c88bbb 10288 memcpy (name, buf, len);
252b5132 10289
117ed4f8 10290 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10291
16e9c715 10292 if (sect == NULL)
0a1b45a2 10293 return false;
c044fabd 10294
04ec0fa2 10295 if (note->descsz < 12 + name_size)
404ec933 10296 {
3cdad084 10297 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
404ec933 10298 abfd, note->descsz, name_size);
0a1b45a2 10299 return true;
404ec933 10300 }
04ec0fa2 10301
eea6121a 10302 sect->size = note->descsz;
16e9c715 10303 sect->filepos = note->descpos;
16e9c715
NC
10304 sect->alignment_power = 2;
10305 break;
10306
10307 default:
0a1b45a2 10308 return true;
16e9c715
NC
10309 }
10310
0a1b45a2 10311 return true;
16e9c715 10312}
252b5132 10313
0a1b45a2 10314static bool
217aa764 10315elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10316{
9c5bfbb7 10317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10318
252b5132
RH
10319 switch (note->type)
10320 {
10321 default:
0a1b45a2 10322 return true;
252b5132 10323
252b5132 10324 case NT_PRSTATUS:
bb0082d6
AM
10325 if (bed->elf_backend_grok_prstatus)
10326 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
0a1b45a2 10327 return true;
bb0082d6 10328#if defined (HAVE_PRSTATUS_T)
252b5132 10329 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10330#else
0a1b45a2 10331 return true;
252b5132
RH
10332#endif
10333
10334#if defined (HAVE_PSTATUS_T)
10335 case NT_PSTATUS:
10336 return elfcore_grok_pstatus (abfd, note);
10337#endif
10338
10339#if defined (HAVE_LWPSTATUS_T)
10340 case NT_LWPSTATUS:
10341 return elfcore_grok_lwpstatus (abfd, note);
10342#endif
10343
10344 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10345 return elfcore_grok_prfpreg (abfd, note);
10346
c044fabd 10347 case NT_WIN32PSTATUS:
16e9c715 10348 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10349
c044fabd 10350 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10351 if (note->namesz == 6
10352 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10353 return elfcore_grok_prxfpreg (abfd, note);
10354 else
0a1b45a2 10355 return true;
ff08c6bb 10356
4339cae0
L
10357 case NT_X86_XSTATE: /* Linux XSAVE extension */
10358 if (note->namesz == 6
10359 && strcmp (note->namedata, "LINUX") == 0)
10360 return elfcore_grok_xstatereg (abfd, note);
10361 else
0a1b45a2 10362 return true;
4339cae0 10363
97753bd5
AM
10364 case NT_PPC_VMX:
10365 if (note->namesz == 6
10366 && strcmp (note->namedata, "LINUX") == 0)
10367 return elfcore_grok_ppc_vmx (abfd, note);
10368 else
0a1b45a2 10369 return true;
97753bd5 10370
89eeb0bc
LM
10371 case NT_PPC_VSX:
10372 if (note->namesz == 6
07d6d2b8
AM
10373 && strcmp (note->namedata, "LINUX") == 0)
10374 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10375 else
0a1b45a2 10376 return true;
89eeb0bc 10377
cb2366c1
EBM
10378 case NT_PPC_TAR:
10379 if (note->namesz == 6
4b24dd1a
AM
10380 && strcmp (note->namedata, "LINUX") == 0)
10381 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10382 else
0a1b45a2 10383 return true;
cb2366c1
EBM
10384
10385 case NT_PPC_PPR:
10386 if (note->namesz == 6
4b24dd1a
AM
10387 && strcmp (note->namedata, "LINUX") == 0)
10388 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10389 else
0a1b45a2 10390 return true;
cb2366c1
EBM
10391
10392 case NT_PPC_DSCR:
10393 if (note->namesz == 6
4b24dd1a
AM
10394 && strcmp (note->namedata, "LINUX") == 0)
10395 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10396 else
0a1b45a2 10397 return true;
cb2366c1
EBM
10398
10399 case NT_PPC_EBB:
10400 if (note->namesz == 6
4b24dd1a
AM
10401 && strcmp (note->namedata, "LINUX") == 0)
10402 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10403 else
0a1b45a2 10404 return true;
cb2366c1
EBM
10405
10406 case NT_PPC_PMU:
10407 if (note->namesz == 6
4b24dd1a
AM
10408 && strcmp (note->namedata, "LINUX") == 0)
10409 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10410 else
0a1b45a2 10411 return true;
cb2366c1
EBM
10412
10413 case NT_PPC_TM_CGPR:
10414 if (note->namesz == 6
4b24dd1a
AM
10415 && strcmp (note->namedata, "LINUX") == 0)
10416 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10417 else
0a1b45a2 10418 return true;
cb2366c1
EBM
10419
10420 case NT_PPC_TM_CFPR:
10421 if (note->namesz == 6
4b24dd1a
AM
10422 && strcmp (note->namedata, "LINUX") == 0)
10423 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10424 else
0a1b45a2 10425 return true;
cb2366c1
EBM
10426
10427 case NT_PPC_TM_CVMX:
10428 if (note->namesz == 6
4b24dd1a
AM
10429 && strcmp (note->namedata, "LINUX") == 0)
10430 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10431 else
0a1b45a2 10432 return true;
cb2366c1
EBM
10433
10434 case NT_PPC_TM_CVSX:
10435 if (note->namesz == 6
4b24dd1a
AM
10436 && strcmp (note->namedata, "LINUX") == 0)
10437 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10438 else
0a1b45a2 10439 return true;
cb2366c1
EBM
10440
10441 case NT_PPC_TM_SPR:
10442 if (note->namesz == 6
4b24dd1a
AM
10443 && strcmp (note->namedata, "LINUX") == 0)
10444 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10445 else
0a1b45a2 10446 return true;
cb2366c1
EBM
10447
10448 case NT_PPC_TM_CTAR:
10449 if (note->namesz == 6
4b24dd1a
AM
10450 && strcmp (note->namedata, "LINUX") == 0)
10451 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10452 else
0a1b45a2 10453 return true;
cb2366c1
EBM
10454
10455 case NT_PPC_TM_CPPR:
10456 if (note->namesz == 6
4b24dd1a
AM
10457 && strcmp (note->namedata, "LINUX") == 0)
10458 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10459 else
0a1b45a2 10460 return true;
cb2366c1
EBM
10461
10462 case NT_PPC_TM_CDSCR:
10463 if (note->namesz == 6
4b24dd1a
AM
10464 && strcmp (note->namedata, "LINUX") == 0)
10465 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10466 else
0a1b45a2 10467 return true;
cb2366c1 10468
0675e188
UW
10469 case NT_S390_HIGH_GPRS:
10470 if (note->namesz == 6
07d6d2b8
AM
10471 && strcmp (note->namedata, "LINUX") == 0)
10472 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10473 else
0a1b45a2 10474 return true;
0675e188 10475
d7eeb400
MS
10476 case NT_S390_TIMER:
10477 if (note->namesz == 6
07d6d2b8
AM
10478 && strcmp (note->namedata, "LINUX") == 0)
10479 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10480 else
0a1b45a2 10481 return true;
d7eeb400
MS
10482
10483 case NT_S390_TODCMP:
10484 if (note->namesz == 6
07d6d2b8
AM
10485 && strcmp (note->namedata, "LINUX") == 0)
10486 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10487 else
0a1b45a2 10488 return true;
d7eeb400
MS
10489
10490 case NT_S390_TODPREG:
10491 if (note->namesz == 6
07d6d2b8
AM
10492 && strcmp (note->namedata, "LINUX") == 0)
10493 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10494 else
0a1b45a2 10495 return true;
d7eeb400
MS
10496
10497 case NT_S390_CTRS:
10498 if (note->namesz == 6
07d6d2b8
AM
10499 && strcmp (note->namedata, "LINUX") == 0)
10500 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10501 else
0a1b45a2 10502 return true;
d7eeb400
MS
10503
10504 case NT_S390_PREFIX:
10505 if (note->namesz == 6
07d6d2b8
AM
10506 && strcmp (note->namedata, "LINUX") == 0)
10507 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10508 else
0a1b45a2 10509 return true;
d7eeb400 10510
355b81d9
UW
10511 case NT_S390_LAST_BREAK:
10512 if (note->namesz == 6
07d6d2b8
AM
10513 && strcmp (note->namedata, "LINUX") == 0)
10514 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10515 else
0a1b45a2 10516 return true;
355b81d9
UW
10517
10518 case NT_S390_SYSTEM_CALL:
10519 if (note->namesz == 6
07d6d2b8
AM
10520 && strcmp (note->namedata, "LINUX") == 0)
10521 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10522 else
0a1b45a2 10523 return true;
355b81d9 10524
abb3f6cc
NC
10525 case NT_S390_TDB:
10526 if (note->namesz == 6
07d6d2b8
AM
10527 && strcmp (note->namedata, "LINUX") == 0)
10528 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10529 else
0a1b45a2 10530 return true;
abb3f6cc 10531
4ef9f41a
AA
10532 case NT_S390_VXRS_LOW:
10533 if (note->namesz == 6
10534 && strcmp (note->namedata, "LINUX") == 0)
10535 return elfcore_grok_s390_vxrs_low (abfd, note);
10536 else
0a1b45a2 10537 return true;
4ef9f41a
AA
10538
10539 case NT_S390_VXRS_HIGH:
10540 if (note->namesz == 6
10541 && strcmp (note->namedata, "LINUX") == 0)
10542 return elfcore_grok_s390_vxrs_high (abfd, note);
10543 else
0a1b45a2 10544 return true;
4ef9f41a 10545
88ab90e8
AA
10546 case NT_S390_GS_CB:
10547 if (note->namesz == 6
10548 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10549 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8 10550 else
0a1b45a2 10551 return true;
88ab90e8
AA
10552
10553 case NT_S390_GS_BC:
10554 if (note->namesz == 6
10555 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10556 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8 10557 else
0a1b45a2 10558 return true;
88ab90e8 10559
27456742
AK
10560 case NT_ARC_V2:
10561 if (note->namesz == 6
10562 && strcmp (note->namedata, "LINUX") == 0)
10563 return elfcore_grok_arc_v2 (abfd, note);
10564 else
0a1b45a2 10565 return true;
27456742 10566
faa9a424
UW
10567 case NT_ARM_VFP:
10568 if (note->namesz == 6
10569 && strcmp (note->namedata, "LINUX") == 0)
10570 return elfcore_grok_arm_vfp (abfd, note);
10571 else
0a1b45a2 10572 return true;
faa9a424 10573
652451f8
YZ
10574 case NT_ARM_TLS:
10575 if (note->namesz == 6
10576 && strcmp (note->namedata, "LINUX") == 0)
10577 return elfcore_grok_aarch_tls (abfd, note);
10578 else
0a1b45a2 10579 return true;
652451f8
YZ
10580
10581 case NT_ARM_HW_BREAK:
10582 if (note->namesz == 6
10583 && strcmp (note->namedata, "LINUX") == 0)
10584 return elfcore_grok_aarch_hw_break (abfd, note);
10585 else
0a1b45a2 10586 return true;
652451f8
YZ
10587
10588 case NT_ARM_HW_WATCH:
10589 if (note->namesz == 6
10590 && strcmp (note->namedata, "LINUX") == 0)
10591 return elfcore_grok_aarch_hw_watch (abfd, note);
10592 else
0a1b45a2 10593 return true;
652451f8 10594
ad1cc4e4
AH
10595 case NT_ARM_SVE:
10596 if (note->namesz == 6
10597 && strcmp (note->namedata, "LINUX") == 0)
10598 return elfcore_grok_aarch_sve (abfd, note);
10599 else
0a1b45a2 10600 return true;
ad1cc4e4 10601
e6c3b5bf
AH
10602 case NT_ARM_PAC_MASK:
10603 if (note->namesz == 6
10604 && strcmp (note->namedata, "LINUX") == 0)
10605 return elfcore_grok_aarch_pauth (abfd, note);
10606 else
0a1b45a2 10607 return true;
e6c3b5bf 10608
b63a5e38
AB
10609 case NT_GDB_TDESC:
10610 if (note->namesz == 4
10611 && strcmp (note->namedata, "GDB") == 0)
10612 return elfcore_grok_gdb_tdesc (abfd, note);
10613 else
0a1b45a2 10614 return true;
b63a5e38 10615
db6092f3
AB
10616 case NT_RISCV_CSR:
10617 if (note->namesz == 4
10618 && strcmp (note->namedata, "GDB") == 0)
10619 return elfcore_grok_riscv_csr (abfd, note);
10620 else
0a1b45a2 10621 return true;
db6092f3 10622
252b5132
RH
10623 case NT_PRPSINFO:
10624 case NT_PSINFO:
bb0082d6
AM
10625 if (bed->elf_backend_grok_psinfo)
10626 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
0a1b45a2 10627 return true;
bb0082d6 10628#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10629 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10630#else
0a1b45a2 10631 return true;
252b5132 10632#endif
3333a7c3
RM
10633
10634 case NT_AUXV:
58e07198 10635 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10636
451b7c33
TT
10637 case NT_FILE:
10638 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10639 note);
10640
9015683b
TT
10641 case NT_SIGINFO:
10642 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10643 note);
5b2c414d 10644
252b5132
RH
10645 }
10646}
10647
0a1b45a2 10648static bool
718175fa
JK
10649elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10650{
c74f7d1c 10651 struct bfd_build_id* build_id;
30e8ee25
AM
10652
10653 if (note->descsz == 0)
0a1b45a2 10654 return false;
30e8ee25 10655
c74f7d1c
JT
10656 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10657 if (build_id == NULL)
0a1b45a2 10658 return false;
718175fa 10659
c74f7d1c
JT
10660 build_id->size = note->descsz;
10661 memcpy (build_id->data, note->descdata, note->descsz);
10662 abfd->build_id = build_id;
718175fa 10663
0a1b45a2 10664 return true;
718175fa
JK
10665}
10666
0a1b45a2 10667static bool
718175fa
JK
10668elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10669{
10670 switch (note->type)
10671 {
10672 default:
0a1b45a2 10673 return true;
718175fa 10674
46bed679
L
10675 case NT_GNU_PROPERTY_TYPE_0:
10676 return _bfd_elf_parse_gnu_properties (abfd, note);
10677
718175fa
JK
10678 case NT_GNU_BUILD_ID:
10679 return elfobj_grok_gnu_build_id (abfd, note);
10680 }
10681}
10682
0a1b45a2 10683static bool
e21e5835
NC
10684elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10685{
10686 struct sdt_note *cur =
7a6e0d89
AM
10687 (struct sdt_note *) bfd_alloc (abfd,
10688 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10689
10690 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10691 cur->size = (bfd_size_type) note->descsz;
10692 memcpy (cur->data, note->descdata, note->descsz);
10693
10694 elf_tdata (abfd)->sdt_note_head = cur;
10695
0a1b45a2 10696 return true;
e21e5835
NC
10697}
10698
0a1b45a2 10699static bool
e21e5835
NC
10700elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10701{
10702 switch (note->type)
10703 {
10704 case NT_STAPSDT:
10705 return elfobj_grok_stapsdt_note_1 (abfd, note);
10706
10707 default:
0a1b45a2 10708 return true;
e21e5835
NC
10709 }
10710}
10711
0a1b45a2 10712static bool
aa1ed4a9
JB
10713elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10714{
10715 size_t offset;
10716
b5430a3c 10717 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10718 {
b5430a3c 10719 case ELFCLASS32:
0064d223 10720 if (note->descsz < 108)
0a1b45a2 10721 return false;
aa1ed4a9
JB
10722 break;
10723
b5430a3c 10724 case ELFCLASS64:
0064d223 10725 if (note->descsz < 120)
0a1b45a2 10726 return false;
aa1ed4a9
JB
10727 break;
10728
10729 default:
0a1b45a2 10730 return false;
aa1ed4a9
JB
10731 }
10732
0064d223
JB
10733 /* Check for version 1 in pr_version. */
10734 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10735 return false;
80a04378 10736
0064d223
JB
10737 offset = 4;
10738
10739 /* Skip over pr_psinfosz. */
b5430a3c 10740 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10741 offset += 4;
10742 else
10743 {
10744 offset += 4; /* Padding before pr_psinfosz. */
10745 offset += 8;
10746 }
10747
aa1ed4a9
JB
10748 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10749 elf_tdata (abfd)->core->program
10750 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10751 offset += 17;
10752
10753 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10754 elf_tdata (abfd)->core->command
10755 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10756 offset += 81;
10757
10758 /* Padding before pr_pid. */
10759 offset += 2;
10760
10761 /* The pr_pid field was added in version "1a". */
10762 if (note->descsz < offset + 4)
0a1b45a2 10763 return true;
0064d223
JB
10764
10765 elf_tdata (abfd)->core->pid
10766 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9 10767
0a1b45a2 10768 return true;
aa1ed4a9
JB
10769}
10770
0a1b45a2 10771static bool
aa1ed4a9
JB
10772elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10773{
10774 size_t offset;
10775 size_t size;
24d3e51b 10776 size_t min_size;
aa1ed4a9 10777
24d3e51b
NC
10778 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10779 Also compute minimum size of this note. */
b5430a3c 10780 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10781 {
b5430a3c 10782 case ELFCLASS32:
24d3e51b
NC
10783 offset = 4 + 4;
10784 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10785 break;
10786
b5430a3c 10787 case ELFCLASS64:
24d3e51b
NC
10788 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10789 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10790 break;
10791
10792 default:
0a1b45a2 10793 return false;
aa1ed4a9
JB
10794 }
10795
24d3e51b 10796 if (note->descsz < min_size)
0a1b45a2 10797 return false;
24d3e51b
NC
10798
10799 /* Check for version 1 in pr_version. */
10800 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10801 return false;
aa1ed4a9 10802
24d3e51b
NC
10803 /* Extract size of pr_reg from pr_gregsetsz. */
10804 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10805 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10806 {
10807 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10808 offset += 4 * 2;
10809 }
b5430a3c 10810 else
24d3e51b
NC
10811 {
10812 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10813 offset += 8 * 2;
10814 }
aa1ed4a9 10815
24d3e51b 10816 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10817 offset += 4;
10818
24d3e51b 10819 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10820 if (elf_tdata (abfd)->core->signal == 0)
10821 elf_tdata (abfd)->core->signal
10822 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10823 offset += 4;
10824
24d3e51b 10825 /* Read TID from pr_pid. */
aa1ed4a9
JB
10826 elf_tdata (abfd)->core->lwpid
10827 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10828 offset += 4;
10829
24d3e51b 10830 /* Padding before pr_reg. */
b5430a3c 10831 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10832 offset += 4;
10833
24d3e51b
NC
10834 /* Make sure that there is enough data remaining in the note. */
10835 if ((note->descsz - offset) < size)
0a1b45a2 10836 return false;
24d3e51b 10837
aa1ed4a9
JB
10838 /* Make a ".reg/999" section and a ".reg" section. */
10839 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10840 size, note->descpos + offset);
10841}
10842
0a1b45a2 10843static bool
aa1ed4a9
JB
10844elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10845{
544c67cd
JB
10846 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10847
aa1ed4a9
JB
10848 switch (note->type)
10849 {
10850 case NT_PRSTATUS:
544c67cd
JB
10851 if (bed->elf_backend_grok_freebsd_prstatus)
10852 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
0a1b45a2 10853 return true;
aa1ed4a9
JB
10854 return elfcore_grok_freebsd_prstatus (abfd, note);
10855
10856 case NT_FPREGSET:
10857 return elfcore_grok_prfpreg (abfd, note);
10858
10859 case NT_PRPSINFO:
10860 return elfcore_grok_freebsd_psinfo (abfd, note);
10861
10862 case NT_FREEBSD_THRMISC:
10863 if (note->namesz == 8)
10864 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10865 else
0a1b45a2 10866 return true;
aa1ed4a9 10867
ddb2bbcf
JB
10868 case NT_FREEBSD_PROCSTAT_PROC:
10869 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10870 note);
10871
10872 case NT_FREEBSD_PROCSTAT_FILES:
10873 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10874 note);
10875
10876 case NT_FREEBSD_PROCSTAT_VMMAP:
10877 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10878 note);
10879
3350c5f5 10880 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10881 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10882
aa1ed4a9
JB
10883 case NT_X86_XSTATE:
10884 if (note->namesz == 8)
10885 return elfcore_grok_xstatereg (abfd, note);
10886 else
0a1b45a2 10887 return true;
aa1ed4a9 10888
e6f3b9c3
JB
10889 case NT_FREEBSD_PTLWPINFO:
10890 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10891 note);
10892
6d5be5d6
JB
10893 case NT_ARM_VFP:
10894 return elfcore_grok_arm_vfp (abfd, note);
10895
aa1ed4a9 10896 default:
0a1b45a2 10897 return true;
aa1ed4a9
JB
10898 }
10899}
10900
0a1b45a2 10901static bool
217aa764 10902elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10903{
10904 char *cp;
10905
10906 cp = strchr (note->namedata, '@');
10907 if (cp != NULL)
10908 {
d2b64500 10909 *lwpidp = atoi(cp + 1);
0a1b45a2 10910 return true;
50b2bdb7 10911 }
0a1b45a2 10912 return false;
50b2bdb7
AM
10913}
10914
0a1b45a2 10915static bool
217aa764 10916elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10917{
80a04378 10918 if (note->descsz <= 0x7c + 31)
0a1b45a2 10919 return false;
80a04378 10920
50b2bdb7 10921 /* Signal number at offset 0x08. */
228e534f 10922 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10923 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10924
10925 /* Process ID at offset 0x50. */
228e534f 10926 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10927 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10928
10929 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10930 elf_tdata (abfd)->core->command
50b2bdb7
AM
10931 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10932
7720ba9f
MK
10933 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10934 note);
50b2bdb7
AM
10935}
10936
0a1b45a2 10937static bool
217aa764 10938elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10939{
10940 int lwp;
10941
10942 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10943 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10944
58e07198 10945 switch (note->type)
50b2bdb7 10946 {
58e07198 10947 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10948 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10949 find this note before any of the others, which is fine,
10950 since the kernel writes this note out first when it
10951 creates a core file. */
50b2bdb7 10952 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10953 case NT_NETBSDCORE_AUXV:
10954 /* NetBSD-specific Elf Auxiliary Vector data. */
10955 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
10956 case NT_NETBSDCORE_LWPSTATUS:
10957 return elfcore_make_note_pseudosection (abfd,
10958 ".note.netbsdcore.lwpstatus",
10959 note);
58e07198
CZ
10960 default:
10961 break;
50b2bdb7
AM
10962 }
10963
06d949ec 10964 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
10965 defined for NetBSD core files. If the note type is less
10966 than the start of the machine-dependent note types, we don't
10967 understand it. */
47d9a591 10968
b4db1224 10969 if (note->type < NT_NETBSDCORE_FIRSTMACH)
0a1b45a2 10970 return true;
50b2bdb7
AM
10971
10972
10973 switch (bfd_get_arch (abfd))
10974 {
08a40648
AM
10975 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10976 PT_GETFPREGS == mach+2. */
50b2bdb7 10977
015ec493 10978 case bfd_arch_aarch64:
50b2bdb7
AM
10979 case bfd_arch_alpha:
10980 case bfd_arch_sparc:
10981 switch (note->type)
08a40648
AM
10982 {
10983 case NT_NETBSDCORE_FIRSTMACH+0:
10984 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10985
08a40648
AM
10986 case NT_NETBSDCORE_FIRSTMACH+2:
10987 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10988
08a40648 10989 default:
0a1b45a2 10990 return true;
08a40648 10991 }
50b2bdb7 10992
58e07198
CZ
10993 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10994 There's also old PT___GETREGS40 == mach + 1 for old reg
10995 structure which lacks GBR. */
10996
10997 case bfd_arch_sh:
10998 switch (note->type)
10999 {
11000 case NT_NETBSDCORE_FIRSTMACH+3:
11001 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11002
11003 case NT_NETBSDCORE_FIRSTMACH+5:
11004 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11005
11006 default:
0a1b45a2 11007 return true;
58e07198
CZ
11008 }
11009
08a40648
AM
11010 /* On all other arch's, PT_GETREGS == mach+1 and
11011 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
11012
11013 default:
11014 switch (note->type)
08a40648
AM
11015 {
11016 case NT_NETBSDCORE_FIRSTMACH+1:
11017 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11018
08a40648
AM
11019 case NT_NETBSDCORE_FIRSTMACH+3:
11020 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11021
08a40648 11022 default:
0a1b45a2 11023 return true;
08a40648 11024 }
50b2bdb7
AM
11025 }
11026 /* NOTREACHED */
11027}
11028
0a1b45a2 11029static bool
67cc5033
MK
11030elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11031{
80a04378 11032 if (note->descsz <= 0x48 + 31)
0a1b45a2 11033 return false;
80a04378 11034
67cc5033 11035 /* Signal number at offset 0x08. */
228e534f 11036 elf_tdata (abfd)->core->signal
67cc5033
MK
11037 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11038
11039 /* Process ID at offset 0x20. */
228e534f 11040 elf_tdata (abfd)->core->pid
67cc5033
MK
11041 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11042
11043 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 11044 elf_tdata (abfd)->core->command
67cc5033
MK
11045 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11046
0a1b45a2 11047 return true;
67cc5033
MK
11048}
11049
0a1b45a2 11050static bool
67cc5033
MK
11051elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11052{
11053 if (note->type == NT_OPENBSD_PROCINFO)
11054 return elfcore_grok_openbsd_procinfo (abfd, note);
11055
11056 if (note->type == NT_OPENBSD_REGS)
11057 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11058
11059 if (note->type == NT_OPENBSD_FPREGS)
11060 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11061
11062 if (note->type == NT_OPENBSD_XFPREGS)
11063 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11064
11065 if (note->type == NT_OPENBSD_AUXV)
58e07198 11066 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11067
11068 if (note->type == NT_OPENBSD_WCOOKIE)
11069 {
11070 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11071 SEC_HAS_CONTENTS);
11072
11073 if (sect == NULL)
0a1b45a2 11074 return false;
67cc5033
MK
11075 sect->size = note->descsz;
11076 sect->filepos = note->descpos;
11077 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11078
0a1b45a2 11079 return true;
67cc5033
MK
11080 }
11081
0a1b45a2 11082 return true;
67cc5033
MK
11083}
11084
0a1b45a2 11085static bool
d3fd4074 11086elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11087{
11088 void *ddata = note->descdata;
11089 char buf[100];
11090 char *name;
11091 asection *sect;
f8843e87
AM
11092 short sig;
11093 unsigned flags;
07c6e936 11094
80a04378 11095 if (note->descsz < 16)
0a1b45a2 11096 return false;
80a04378 11097
07c6e936 11098 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11099 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11100
f8843e87
AM
11101 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11102 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11103
11104 /* nto_procfs_status 'flags' field is at offset 8. */
11105 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11106
11107 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11108 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11109 {
228e534f
AM
11110 elf_tdata (abfd)->core->signal = sig;
11111 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11112 }
07c6e936 11113
f8843e87
AM
11114 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11115 do not come from signals so we make sure we set the current
11116 thread just in case. */
11117 if (flags & 0x00000080)
228e534f 11118 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11119
11120 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11121 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11122
a50b1753 11123 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11124 if (name == NULL)
0a1b45a2 11125 return false;
07c6e936
NC
11126 strcpy (name, buf);
11127
117ed4f8 11128 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11129 if (sect == NULL)
0a1b45a2 11130 return false;
07c6e936 11131
07d6d2b8
AM
11132 sect->size = note->descsz;
11133 sect->filepos = note->descpos;
07c6e936
NC
11134 sect->alignment_power = 2;
11135
11136 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11137}
11138
0a1b45a2 11139static bool
d69f560c
KW
11140elfcore_grok_nto_regs (bfd *abfd,
11141 Elf_Internal_Note *note,
d3fd4074 11142 long tid,
d69f560c 11143 char *base)
07c6e936
NC
11144{
11145 char buf[100];
11146 char *name;
11147 asection *sect;
11148
d69f560c 11149 /* Make a "(base)/%d" section. */
d3fd4074 11150 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11151
a50b1753 11152 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11153 if (name == NULL)
0a1b45a2 11154 return false;
07c6e936
NC
11155 strcpy (name, buf);
11156
117ed4f8 11157 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11158 if (sect == NULL)
0a1b45a2 11159 return false;
07c6e936 11160
07d6d2b8
AM
11161 sect->size = note->descsz;
11162 sect->filepos = note->descpos;
07c6e936
NC
11163 sect->alignment_power = 2;
11164
f8843e87 11165 /* This is the current thread. */
228e534f 11166 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11167 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87 11168
0a1b45a2 11169 return true;
07c6e936
NC
11170}
11171
11172#define BFD_QNT_CORE_INFO 7
11173#define BFD_QNT_CORE_STATUS 8
11174#define BFD_QNT_CORE_GREG 9
11175#define BFD_QNT_CORE_FPREG 10
11176
0a1b45a2 11177static bool
217aa764 11178elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11179{
11180 /* Every GREG section has a STATUS section before it. Store the
811072d8 11181 tid from the previous call to pass down to the next gregs
07c6e936 11182 function. */
d3fd4074 11183 static long tid = 1;
07c6e936
NC
11184
11185 switch (note->type)
11186 {
d69f560c
KW
11187 case BFD_QNT_CORE_INFO:
11188 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11189 case BFD_QNT_CORE_STATUS:
11190 return elfcore_grok_nto_status (abfd, note, &tid);
11191 case BFD_QNT_CORE_GREG:
11192 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11193 case BFD_QNT_CORE_FPREG:
11194 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11195 default:
0a1b45a2 11196 return true;
07c6e936
NC
11197 }
11198}
11199
0a1b45a2 11200static bool
b15fa79e
AM
11201elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11202{
11203 char *name;
11204 asection *sect;
11205 size_t len;
11206
11207 /* Use note name as section name. */
11208 len = note->namesz;
a50b1753 11209 name = (char *) bfd_alloc (abfd, len);
b15fa79e 11210 if (name == NULL)
0a1b45a2 11211 return false;
b15fa79e
AM
11212 memcpy (name, note->namedata, len);
11213 name[len - 1] = '\0';
11214
11215 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11216 if (sect == NULL)
0a1b45a2 11217 return false;
b15fa79e 11218
07d6d2b8
AM
11219 sect->size = note->descsz;
11220 sect->filepos = note->descpos;
b15fa79e
AM
11221 sect->alignment_power = 1;
11222
0a1b45a2 11223 return true;
b15fa79e
AM
11224}
11225
7c76fa91
MS
11226/* Function: elfcore_write_note
11227
47d9a591 11228 Inputs:
a39f3346 11229 buffer to hold note, and current size of buffer
7c76fa91
MS
11230 name of note
11231 type of note
11232 data for note
11233 size of data for note
11234
a39f3346
AM
11235 Writes note to end of buffer. ELF64 notes are written exactly as
11236 for ELF32, despite the current (as of 2006) ELF gabi specifying
11237 that they ought to have 8-byte namesz and descsz field, and have
11238 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11239
7c76fa91 11240 Return:
a39f3346 11241 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11242
11243char *
a39f3346 11244elfcore_write_note (bfd *abfd,
217aa764 11245 char *buf,
a39f3346 11246 int *bufsiz,
217aa764 11247 const char *name,
a39f3346 11248 int type,
217aa764 11249 const void *input,
a39f3346 11250 int size)
7c76fa91
MS
11251{
11252 Elf_External_Note *xnp;
d4c88bbb 11253 size_t namesz;
d4c88bbb 11254 size_t newspace;
a39f3346 11255 char *dest;
7c76fa91 11256
d4c88bbb 11257 namesz = 0;
d4c88bbb 11258 if (name != NULL)
a39f3346 11259 namesz = strlen (name) + 1;
d4c88bbb 11260
a39f3346 11261 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11262
a50b1753 11263 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11264 if (buf == NULL)
11265 return buf;
a39f3346 11266 dest = buf + *bufsiz;
7c76fa91
MS
11267 *bufsiz += newspace;
11268 xnp = (Elf_External_Note *) dest;
11269 H_PUT_32 (abfd, namesz, xnp->namesz);
11270 H_PUT_32 (abfd, size, xnp->descsz);
11271 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11272 dest = xnp->name;
11273 if (name != NULL)
11274 {
11275 memcpy (dest, name, namesz);
11276 dest += namesz;
a39f3346 11277 while (namesz & 3)
d4c88bbb
AM
11278 {
11279 *dest++ = '\0';
a39f3346 11280 ++namesz;
d4c88bbb
AM
11281 }
11282 }
11283 memcpy (dest, input, size);
a39f3346
AM
11284 dest += size;
11285 while (size & 3)
11286 {
11287 *dest++ = '\0';
11288 ++size;
11289 }
11290 return buf;
7c76fa91
MS
11291}
11292
602f1657
AM
11293/* gcc-8 warns (*) on all the strncpy calls in this function about
11294 possible string truncation. The "truncation" is not a bug. We
11295 have an external representation of structs with fields that are not
11296 necessarily NULL terminated and corresponding internal
11297 representation fields that are one larger so that they can always
11298 be NULL terminated.
11299 gcc versions between 4.2 and 4.6 do not allow pragma control of
11300 diagnostics inside functions, giving a hard error if you try to use
11301 the finer control available with later versions.
11302 gcc prior to 4.2 warns about diagnostic push and pop.
11303 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11304 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11305 (*) Depending on your system header files! */
d99b4b92 11306#if GCC_VERSION >= 8000
602f1657
AM
11307# pragma GCC diagnostic push
11308# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11309#endif
7c76fa91 11310char *
217aa764
AM
11311elfcore_write_prpsinfo (bfd *abfd,
11312 char *buf,
11313 int *bufsiz,
11314 const char *fname,
11315 const char *psargs)
7c76fa91 11316{
183e98be
AM
11317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11318
11319 if (bed->elf_backend_write_core_note != NULL)
11320 {
11321 char *ret;
11322 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11323 NT_PRPSINFO, fname, psargs);
11324 if (ret != NULL)
11325 return ret;
11326 }
7c76fa91 11327
1f20dca5 11328#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11329# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11330 if (bed->s->elfclass == ELFCLASS32)
11331 {
602f1657 11332# if defined (HAVE_PSINFO32_T)
183e98be
AM
11333 psinfo32_t data;
11334 int note_type = NT_PSINFO;
602f1657 11335# else
183e98be
AM
11336 prpsinfo32_t data;
11337 int note_type = NT_PRPSINFO;
602f1657 11338# endif
183e98be
AM
11339
11340 memset (&data, 0, sizeof (data));
11341 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11342 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11343 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11344 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11345 }
11346 else
602f1657 11347# endif
183e98be 11348 {
602f1657 11349# if defined (HAVE_PSINFO_T)
183e98be
AM
11350 psinfo_t data;
11351 int note_type = NT_PSINFO;
602f1657 11352# else
183e98be
AM
11353 prpsinfo_t data;
11354 int note_type = NT_PRPSINFO;
602f1657 11355# endif
7c76fa91 11356
183e98be
AM
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 11362 }
7c76fa91
MS
11363#endif /* PSINFO_T or PRPSINFO_T */
11364
1f20dca5
UW
11365 free (buf);
11366 return NULL;
11367}
d99b4b92 11368#if GCC_VERSION >= 8000
602f1657 11369# pragma GCC diagnostic pop
d99b4b92 11370#endif
1f20dca5 11371
70a38d42
SDJ
11372char *
11373elfcore_write_linux_prpsinfo32
11374 (bfd *abfd, char *buf, int *bufsiz,
11375 const struct elf_internal_linux_prpsinfo *prpsinfo)
11376{
a2f63b2e
MR
11377 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11378 {
11379 struct elf_external_linux_prpsinfo32_ugid16 data;
11380
11381 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11382 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11383 &data, sizeof (data));
11384 }
11385 else
11386 {
11387 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11388
a2f63b2e
MR
11389 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11390 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11391 &data, sizeof (data));
11392 }
70a38d42
SDJ
11393}
11394
11395char *
11396elfcore_write_linux_prpsinfo64
11397 (bfd *abfd, char *buf, int *bufsiz,
11398 const struct elf_internal_linux_prpsinfo *prpsinfo)
11399{
3c9a7b0d
MR
11400 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11401 {
11402 struct elf_external_linux_prpsinfo64_ugid16 data;
11403
11404 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11405 return elfcore_write_note (abfd, buf, bufsiz,
11406 "CORE", NT_PRPSINFO, &data, sizeof (data));
11407 }
11408 else
11409 {
11410 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11411
3c9a7b0d
MR
11412 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11413 return elfcore_write_note (abfd, buf, bufsiz,
11414 "CORE", NT_PRPSINFO, &data, sizeof (data));
11415 }
70a38d42
SDJ
11416}
11417
7c76fa91 11418char *
217aa764
AM
11419elfcore_write_prstatus (bfd *abfd,
11420 char *buf,
11421 int *bufsiz,
11422 long pid,
11423 int cursig,
11424 const void *gregs)
7c76fa91 11425{
183e98be 11426 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11427
183e98be
AM
11428 if (bed->elf_backend_write_core_note != NULL)
11429 {
11430 char *ret;
11431 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11432 NT_PRSTATUS,
11433 pid, cursig, gregs);
11434 if (ret != NULL)
11435 return ret;
11436 }
11437
1f20dca5 11438#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11439#if defined (HAVE_PRSTATUS32_T)
11440 if (bed->s->elfclass == ELFCLASS32)
11441 {
11442 prstatus32_t prstat;
11443
11444 memset (&prstat, 0, sizeof (prstat));
11445 prstat.pr_pid = pid;
11446 prstat.pr_cursig = cursig;
11447 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11448 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11449 NT_PRSTATUS, &prstat, sizeof (prstat));
11450 }
11451 else
11452#endif
11453 {
11454 prstatus_t prstat;
11455
11456 memset (&prstat, 0, sizeof (prstat));
11457 prstat.pr_pid = pid;
11458 prstat.pr_cursig = cursig;
11459 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11460 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11461 NT_PRSTATUS, &prstat, sizeof (prstat));
11462 }
7c76fa91
MS
11463#endif /* HAVE_PRSTATUS_T */
11464
1f20dca5
UW
11465 free (buf);
11466 return NULL;
11467}
11468
51316059
MS
11469#if defined (HAVE_LWPSTATUS_T)
11470char *
217aa764
AM
11471elfcore_write_lwpstatus (bfd *abfd,
11472 char *buf,
11473 int *bufsiz,
11474 long pid,
11475 int cursig,
11476 const void *gregs)
51316059
MS
11477{
11478 lwpstatus_t lwpstat;
183e98be 11479 const char *note_name = "CORE";
51316059
MS
11480
11481 memset (&lwpstat, 0, sizeof (lwpstat));
11482 lwpstat.pr_lwpid = pid >> 16;
11483 lwpstat.pr_cursig = cursig;
11484#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11485 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11486#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11487#if !defined(gregs)
11488 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11489 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11490#else
11491 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11492 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11493#endif
11494#endif
47d9a591 11495 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11496 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11497}
11498#endif /* HAVE_LWPSTATUS_T */
11499
7c76fa91
MS
11500#if defined (HAVE_PSTATUS_T)
11501char *
217aa764
AM
11502elfcore_write_pstatus (bfd *abfd,
11503 char *buf,
11504 int *bufsiz,
11505 long pid,
6c10990d
NC
11506 int cursig ATTRIBUTE_UNUSED,
11507 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11508{
183e98be
AM
11509 const char *note_name = "CORE";
11510#if defined (HAVE_PSTATUS32_T)
11511 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11512
183e98be
AM
11513 if (bed->s->elfclass == ELFCLASS32)
11514 {
11515 pstatus32_t pstat;
11516
11517 memset (&pstat, 0, sizeof (pstat));
11518 pstat.pr_pid = pid & 0xffff;
11519 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11520 NT_PSTATUS, &pstat, sizeof (pstat));
11521 return buf;
11522 }
11523 else
11524#endif
11525 {
11526 pstatus_t pstat;
11527
11528 memset (&pstat, 0, sizeof (pstat));
11529 pstat.pr_pid = pid & 0xffff;
11530 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11531 NT_PSTATUS, &pstat, sizeof (pstat));
11532 return buf;
11533 }
7c76fa91
MS
11534}
11535#endif /* HAVE_PSTATUS_T */
11536
11537char *
217aa764
AM
11538elfcore_write_prfpreg (bfd *abfd,
11539 char *buf,
11540 int *bufsiz,
11541 const void *fpregs,
11542 int size)
7c76fa91 11543{
183e98be 11544 const char *note_name = "CORE";
47d9a591 11545 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11546 note_name, NT_FPREGSET, fpregs, size);
11547}
11548
11549char *
217aa764
AM
11550elfcore_write_prxfpreg (bfd *abfd,
11551 char *buf,
11552 int *bufsiz,
11553 const void *xfpregs,
11554 int size)
7c76fa91
MS
11555{
11556 char *note_name = "LINUX";
47d9a591 11557 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11558 note_name, NT_PRXFPREG, xfpregs, size);
11559}
11560
4339cae0
L
11561char *
11562elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11563 const void *xfpregs, int size)
11564{
97de3545
JB
11565 char *note_name;
11566 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11567 note_name = "FreeBSD";
11568 else
11569 note_name = "LINUX";
4339cae0
L
11570 return elfcore_write_note (abfd, buf, bufsiz,
11571 note_name, NT_X86_XSTATE, xfpregs, size);
11572}
11573
97753bd5
AM
11574char *
11575elfcore_write_ppc_vmx (bfd *abfd,
11576 char *buf,
11577 int *bufsiz,
11578 const void *ppc_vmx,
11579 int size)
11580{
11581 char *note_name = "LINUX";
11582 return elfcore_write_note (abfd, buf, bufsiz,
11583 note_name, NT_PPC_VMX, ppc_vmx, size);
11584}
11585
89eeb0bc
LM
11586char *
11587elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11588 char *buf,
11589 int *bufsiz,
11590 const void *ppc_vsx,
11591 int size)
89eeb0bc
LM
11592{
11593 char *note_name = "LINUX";
11594 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11595 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11596}
11597
cb2366c1
EBM
11598char *
11599elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11600 char *buf,
11601 int *bufsiz,
11602 const void *ppc_tar,
11603 int size)
cb2366c1
EBM
11604{
11605 char *note_name = "LINUX";
11606 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11607 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11608}
11609
11610char *
11611elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11612 char *buf,
11613 int *bufsiz,
11614 const void *ppc_ppr,
11615 int size)
cb2366c1
EBM
11616{
11617 char *note_name = "LINUX";
11618 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11619 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11620}
11621
11622char *
11623elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11624 char *buf,
11625 int *bufsiz,
11626 const void *ppc_dscr,
11627 int size)
cb2366c1
EBM
11628{
11629 char *note_name = "LINUX";
11630 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11631 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11632}
11633
11634char *
11635elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11636 char *buf,
11637 int *bufsiz,
11638 const void *ppc_ebb,
11639 int size)
cb2366c1
EBM
11640{
11641 char *note_name = "LINUX";
11642 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11643 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11644}
11645
11646char *
11647elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11648 char *buf,
11649 int *bufsiz,
11650 const void *ppc_pmu,
11651 int size)
cb2366c1
EBM
11652{
11653 char *note_name = "LINUX";
11654 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11655 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11656}
11657
11658char *
11659elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11660 char *buf,
11661 int *bufsiz,
11662 const void *ppc_tm_cgpr,
11663 int size)
cb2366c1
EBM
11664{
11665 char *note_name = "LINUX";
11666 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11667 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11668}
11669
11670char *
11671elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11672 char *buf,
11673 int *bufsiz,
11674 const void *ppc_tm_cfpr,
11675 int size)
cb2366c1
EBM
11676{
11677 char *note_name = "LINUX";
11678 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11679 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11680}
11681
11682char *
11683elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11684 char *buf,
11685 int *bufsiz,
11686 const void *ppc_tm_cvmx,
11687 int size)
cb2366c1
EBM
11688{
11689 char *note_name = "LINUX";
11690 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11691 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11692}
11693
11694char *
11695elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11696 char *buf,
11697 int *bufsiz,
11698 const void *ppc_tm_cvsx,
11699 int size)
cb2366c1
EBM
11700{
11701 char *note_name = "LINUX";
11702 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11703 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11704}
11705
11706char *
11707elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11708 char *buf,
11709 int *bufsiz,
11710 const void *ppc_tm_spr,
11711 int size)
cb2366c1
EBM
11712{
11713 char *note_name = "LINUX";
11714 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11715 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11716}
11717
11718char *
11719elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11720 char *buf,
11721 int *bufsiz,
11722 const void *ppc_tm_ctar,
11723 int size)
cb2366c1
EBM
11724{
11725 char *note_name = "LINUX";
11726 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11727 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11728}
11729
11730char *
11731elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11732 char *buf,
11733 int *bufsiz,
11734 const void *ppc_tm_cppr,
11735 int size)
cb2366c1
EBM
11736{
11737 char *note_name = "LINUX";
11738 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11739 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11740}
11741
11742char *
11743elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11744 char *buf,
11745 int *bufsiz,
11746 const void *ppc_tm_cdscr,
11747 int size)
cb2366c1
EBM
11748{
11749 char *note_name = "LINUX";
11750 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11751 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11752}
11753
0675e188
UW
11754static char *
11755elfcore_write_s390_high_gprs (bfd *abfd,
11756 char *buf,
11757 int *bufsiz,
11758 const void *s390_high_gprs,
11759 int size)
11760{
11761 char *note_name = "LINUX";
11762 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11763 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11764 s390_high_gprs, size);
11765}
11766
d7eeb400
MS
11767char *
11768elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11769 char *buf,
11770 int *bufsiz,
11771 const void *s390_timer,
11772 int size)
d7eeb400
MS
11773{
11774 char *note_name = "LINUX";
11775 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11776 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11777}
11778
11779char *
11780elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11781 char *buf,
11782 int *bufsiz,
11783 const void *s390_todcmp,
11784 int size)
d7eeb400
MS
11785{
11786 char *note_name = "LINUX";
11787 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11788 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11789}
11790
11791char *
11792elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11793 char *buf,
11794 int *bufsiz,
11795 const void *s390_todpreg,
11796 int size)
d7eeb400
MS
11797{
11798 char *note_name = "LINUX";
11799 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11800 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11801}
11802
11803char *
11804elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11805 char *buf,
11806 int *bufsiz,
11807 const void *s390_ctrs,
11808 int size)
d7eeb400
MS
11809{
11810 char *note_name = "LINUX";
11811 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11812 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11813}
11814
11815char *
11816elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11817 char *buf,
11818 int *bufsiz,
11819 const void *s390_prefix,
11820 int size)
d7eeb400
MS
11821{
11822 char *note_name = "LINUX";
11823 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11824 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11825}
11826
355b81d9
UW
11827char *
11828elfcore_write_s390_last_break (bfd *abfd,
11829 char *buf,
11830 int *bufsiz,
11831 const void *s390_last_break,
11832 int size)
11833{
11834 char *note_name = "LINUX";
11835 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11836 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11837 s390_last_break, size);
11838}
11839
11840char *
11841elfcore_write_s390_system_call (bfd *abfd,
11842 char *buf,
11843 int *bufsiz,
11844 const void *s390_system_call,
11845 int size)
11846{
11847 char *note_name = "LINUX";
11848 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11849 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11850 s390_system_call, size);
11851}
11852
abb3f6cc
NC
11853char *
11854elfcore_write_s390_tdb (bfd *abfd,
11855 char *buf,
11856 int *bufsiz,
11857 const void *s390_tdb,
11858 int size)
11859{
11860 char *note_name = "LINUX";
11861 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11862 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11863}
11864
4ef9f41a
AA
11865char *
11866elfcore_write_s390_vxrs_low (bfd *abfd,
11867 char *buf,
11868 int *bufsiz,
11869 const void *s390_vxrs_low,
11870 int size)
11871{
11872 char *note_name = "LINUX";
11873 return elfcore_write_note (abfd, buf, bufsiz,
11874 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11875}
11876
11877char *
11878elfcore_write_s390_vxrs_high (bfd *abfd,
11879 char *buf,
11880 int *bufsiz,
11881 const void *s390_vxrs_high,
11882 int size)
11883{
11884 char *note_name = "LINUX";
11885 return elfcore_write_note (abfd, buf, bufsiz,
11886 note_name, NT_S390_VXRS_HIGH,
11887 s390_vxrs_high, size);
11888}
11889
88ab90e8
AA
11890char *
11891elfcore_write_s390_gs_cb (bfd *abfd,
11892 char *buf,
11893 int *bufsiz,
11894 const void *s390_gs_cb,
11895 int size)
11896{
11897 char *note_name = "LINUX";
11898 return elfcore_write_note (abfd, buf, bufsiz,
11899 note_name, NT_S390_GS_CB,
11900 s390_gs_cb, size);
11901}
11902
11903char *
11904elfcore_write_s390_gs_bc (bfd *abfd,
11905 char *buf,
11906 int *bufsiz,
11907 const void *s390_gs_bc,
11908 int size)
11909{
11910 char *note_name = "LINUX";
11911 return elfcore_write_note (abfd, buf, bufsiz,
11912 note_name, NT_S390_GS_BC,
11913 s390_gs_bc, size);
11914}
11915
faa9a424
UW
11916char *
11917elfcore_write_arm_vfp (bfd *abfd,
11918 char *buf,
11919 int *bufsiz,
11920 const void *arm_vfp,
11921 int size)
11922{
11923 char *note_name = "LINUX";
11924 return elfcore_write_note (abfd, buf, bufsiz,
11925 note_name, NT_ARM_VFP, arm_vfp, size);
11926}
11927
652451f8
YZ
11928char *
11929elfcore_write_aarch_tls (bfd *abfd,
11930 char *buf,
11931 int *bufsiz,
11932 const void *aarch_tls,
11933 int size)
11934{
11935 char *note_name = "LINUX";
11936 return elfcore_write_note (abfd, buf, bufsiz,
11937 note_name, NT_ARM_TLS, aarch_tls, size);
11938}
11939
11940char *
11941elfcore_write_aarch_hw_break (bfd *abfd,
11942 char *buf,
11943 int *bufsiz,
11944 const void *aarch_hw_break,
11945 int size)
11946{
11947 char *note_name = "LINUX";
11948 return elfcore_write_note (abfd, buf, bufsiz,
11949 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11950}
11951
11952char *
11953elfcore_write_aarch_hw_watch (bfd *abfd,
11954 char *buf,
11955 int *bufsiz,
11956 const void *aarch_hw_watch,
11957 int size)
11958{
11959 char *note_name = "LINUX";
11960 return elfcore_write_note (abfd, buf, bufsiz,
11961 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11962}
11963
ad1cc4e4
AH
11964char *
11965elfcore_write_aarch_sve (bfd *abfd,
11966 char *buf,
11967 int *bufsiz,
11968 const void *aarch_sve,
11969 int size)
11970{
11971 char *note_name = "LINUX";
11972 return elfcore_write_note (abfd, buf, bufsiz,
11973 note_name, NT_ARM_SVE, aarch_sve, size);
11974}
11975
e6c3b5bf
AH
11976char *
11977elfcore_write_aarch_pauth (bfd *abfd,
11978 char *buf,
11979 int *bufsiz,
11980 const void *aarch_pauth,
11981 int size)
11982{
11983 char *note_name = "LINUX";
11984 return elfcore_write_note (abfd, buf, bufsiz,
11985 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11986}
11987
27456742
AK
11988char *
11989elfcore_write_arc_v2 (bfd *abfd,
11990 char *buf,
11991 int *bufsiz,
11992 const void *arc_v2,
11993 int size)
11994{
11995 char *note_name = "LINUX";
11996 return elfcore_write_note (abfd, buf, bufsiz,
11997 note_name, NT_ARC_V2, arc_v2, size);
11998}
11999
db6092f3
AB
12000/* Write the buffer of csr values in CSRS (length SIZE) into the note
12001 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12002 written into. Return a pointer to the new start of the note buffer, to
12003 replace BUF which may no longer be valid. */
12004
12005char *
12006elfcore_write_riscv_csr (bfd *abfd,
12007 char *buf,
12008 int *bufsiz,
12009 const void *csrs,
12010 int size)
12011{
12012 const char *note_name = "GDB";
12013 return elfcore_write_note (abfd, buf, bufsiz,
12014 note_name, NT_RISCV_CSR, csrs, size);
12015}
12016
b63a5e38
AB
12017/* Write the target description (a string) pointed to by TDESC, length
12018 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12019 note is being written into. Return a pointer to the new start of the
12020 note buffer, to replace BUF which may no longer be valid. */
12021
12022char *
12023elfcore_write_gdb_tdesc (bfd *abfd,
12024 char *buf,
12025 int *bufsiz,
12026 const void *tdesc,
12027 int size)
12028{
12029 const char *note_name = "GDB";
12030 return elfcore_write_note (abfd, buf, bufsiz,
12031 note_name, NT_GDB_TDESC, tdesc, size);
12032}
12033
bb864ac1
CES
12034char *
12035elfcore_write_register_note (bfd *abfd,
12036 char *buf,
12037 int *bufsiz,
12038 const char *section,
12039 const void *data,
12040 int size)
12041{
12042 if (strcmp (section, ".reg2") == 0)
12043 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12044 if (strcmp (section, ".reg-xfp") == 0)
12045 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
12046 if (strcmp (section, ".reg-xstate") == 0)
12047 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12048 if (strcmp (section, ".reg-ppc-vmx") == 0)
12049 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
12050 if (strcmp (section, ".reg-ppc-vsx") == 0)
12051 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
12052 if (strcmp (section, ".reg-ppc-tar") == 0)
12053 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12054 if (strcmp (section, ".reg-ppc-ppr") == 0)
12055 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12056 if (strcmp (section, ".reg-ppc-dscr") == 0)
12057 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12058 if (strcmp (section, ".reg-ppc-ebb") == 0)
12059 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12060 if (strcmp (section, ".reg-ppc-pmu") == 0)
12061 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12062 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12063 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12064 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12065 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12066 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12067 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12068 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12069 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12070 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12071 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12072 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12073 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12074 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12075 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12076 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12077 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
12078 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12079 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12080 if (strcmp (section, ".reg-s390-timer") == 0)
12081 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12082 if (strcmp (section, ".reg-s390-todcmp") == 0)
12083 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12084 if (strcmp (section, ".reg-s390-todpreg") == 0)
12085 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12086 if (strcmp (section, ".reg-s390-ctrs") == 0)
12087 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12088 if (strcmp (section, ".reg-s390-prefix") == 0)
12089 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12090 if (strcmp (section, ".reg-s390-last-break") == 0)
12091 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12092 if (strcmp (section, ".reg-s390-system-call") == 0)
12093 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12094 if (strcmp (section, ".reg-s390-tdb") == 0)
12095 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12096 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12097 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12098 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12099 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12100 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12101 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12102 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12103 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12104 if (strcmp (section, ".reg-arm-vfp") == 0)
12105 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12106 if (strcmp (section, ".reg-aarch-tls") == 0)
12107 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12108 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12109 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12110 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12111 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12112 if (strcmp (section, ".reg-aarch-sve") == 0)
12113 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12114 if (strcmp (section, ".reg-aarch-pauth") == 0)
12115 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
27456742
AK
12116 if (strcmp (section, ".reg-arc-v2") == 0)
12117 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
b63a5e38
AB
12118 if (strcmp (section, ".gdb-tdesc") == 0)
12119 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
db6092f3
AB
12120 if (strcmp (section, ".reg-riscv-csr") == 0)
12121 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12122 return NULL;
12123}
12124
4cb1265b
MS
12125char *
12126elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12127 const void *buf, int bufsiz)
12128{
12129 return elfcore_write_note (obfd, note_data, note_size,
12130 "CORE", NT_FILE, buf, bufsiz);
12131}
12132
0a1b45a2 12133static bool
276da9b3
L
12134elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12135 size_t align)
252b5132 12136{
c044fabd 12137 char *p;
252b5132 12138
276da9b3
L
12139 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12140 gABI specifies that PT_NOTE alignment should be aligned to 4
12141 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12142 align is less than 4, we use 4 byte alignment. */
12143 if (align < 4)
12144 align = 4;
ef135d43 12145 if (align != 4 && align != 8)
0a1b45a2 12146 return false;
276da9b3 12147
252b5132
RH
12148 p = buf;
12149 while (p < buf + size)
12150 {
c044fabd 12151 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12152 Elf_Internal_Note in;
12153
baea7ef1 12154 if (offsetof (Elf_External_Note, name) > buf - p + size)
0a1b45a2 12155 return false;
baea7ef1 12156
dc810e39 12157 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12158
dc810e39 12159 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12160 in.namedata = xnp->name;
baea7ef1 12161 if (in.namesz > buf - in.namedata + size)
0a1b45a2 12162 return false;
252b5132 12163
dc810e39 12164 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12165 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12166 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12167 if (in.descsz != 0
12168 && (in.descdata >= buf + size
12169 || in.descsz > buf - in.descdata + size))
0a1b45a2 12170 return false;
252b5132 12171
718175fa 12172 switch (bfd_get_format (abfd))
07d6d2b8 12173 {
718175fa 12174 default:
0a1b45a2 12175 return true;
718175fa
JK
12176
12177 case bfd_core:
f64e188b 12178 {
8acbedd6 12179#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12180 struct
718175fa 12181 {
f64e188b 12182 const char * string;
8acbedd6 12183 size_t len;
0a1b45a2 12184 bool (*func) (bfd *, Elf_Internal_Note *);
718175fa 12185 }
f64e188b 12186 grokers[] =
b15fa79e 12187 {
8acbedd6 12188 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12189 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
12190 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12191 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12192 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb
KS
12193 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12194 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
f64e188b 12195 };
8acbedd6 12196#undef GROKER_ELEMENT
f64e188b
NC
12197 int i;
12198
12199 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12200 {
12201 if (in.namesz >= grokers[i].len
12202 && strncmp (in.namedata, grokers[i].string,
12203 grokers[i].len) == 0)
12204 {
12205 if (! grokers[i].func (abfd, & in))
0a1b45a2 12206 return false;
8acbedd6
KS
12207 break;
12208 }
12209 }
f64e188b
NC
12210 break;
12211 }
718175fa
JK
12212
12213 case bfd_object:
12214 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12215 {
12216 if (! elfobj_grok_gnu_note (abfd, &in))
0a1b45a2 12217 return false;
718175fa 12218 }
e21e5835
NC
12219 else if (in.namesz == sizeof "stapsdt"
12220 && strcmp (in.namedata, "stapsdt") == 0)
12221 {
12222 if (! elfobj_grok_stapsdt_note (abfd, &in))
0a1b45a2 12223 return false;
e21e5835 12224 }
718175fa 12225 break;
08a40648 12226 }
252b5132 12227
276da9b3 12228 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12229 }
12230
0a1b45a2 12231 return true;
718175fa
JK
12232}
12233
0a1b45a2 12234bool
276da9b3
L
12235elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12236 size_t align)
718175fa
JK
12237{
12238 char *buf;
12239
957e1fc1 12240 if (size == 0 || (size + 1) == 0)
0a1b45a2 12241 return true;
718175fa
JK
12242
12243 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
0a1b45a2 12244 return false;
718175fa 12245
2bb3687b 12246 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa 12247 if (buf == NULL)
0a1b45a2 12248 return false;
718175fa 12249
f64e188b
NC
12250 /* PR 17512: file: ec08f814
12251 0-termintate the buffer so that string searches will not overflow. */
12252 buf[size] = 0;
12253
2bb3687b 12254 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12255 {
12256 free (buf);
0a1b45a2 12257 return false;
718175fa
JK
12258 }
12259
252b5132 12260 free (buf);
0a1b45a2 12261 return true;
252b5132 12262}
98d8431c
JB
12263\f
12264/* Providing external access to the ELF program header table. */
12265
12266/* Return an upper bound on the number of bytes required to store a
12267 copy of ABFD's program header table entries. Return -1 if an error
12268 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12269
98d8431c 12270long
217aa764 12271bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12272{
12273 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12274 {
12275 bfd_set_error (bfd_error_wrong_format);
12276 return -1;
12277 }
12278
936e320b 12279 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12280}
12281
98d8431c
JB
12282/* Copy ABFD's program header table entries to *PHDRS. The entries
12283 will be stored as an array of Elf_Internal_Phdr structures, as
12284 defined in include/elf/internal.h. To find out how large the
12285 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12286
12287 Return the number of program header table entries read, or -1 if an
12288 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12289
98d8431c 12290int
217aa764 12291bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12292{
12293 int num_phdrs;
12294
12295 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12296 {
12297 bfd_set_error (bfd_error_wrong_format);
12298 return -1;
12299 }
12300
12301 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12302 if (num_phdrs != 0)
12303 memcpy (phdrs, elf_tdata (abfd)->phdr,
12304 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12305
12306 return num_phdrs;
12307}
ae4221d7 12308
db6751f2 12309enum elf_reloc_type_class
7e612e98
AM
12310_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12311 const asection *rel_sec ATTRIBUTE_UNUSED,
12312 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12313{
12314 return reloc_class_normal;
12315}
f8df10f4 12316
47d9a591 12317/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12318 relocation against a local symbol. */
12319
12320bfd_vma
217aa764
AM
12321_bfd_elf_rela_local_sym (bfd *abfd,
12322 Elf_Internal_Sym *sym,
8517fae7 12323 asection **psec,
217aa764 12324 Elf_Internal_Rela *rel)
f8df10f4 12325{
8517fae7 12326 asection *sec = *psec;
f8df10f4
JJ
12327 bfd_vma relocation;
12328
6835821b
AM
12329 relocation = (sec->output_section->vma
12330 + sec->output_offset
12331 + sym->st_value);
f8df10f4 12332 if ((sec->flags & SEC_MERGE)
c629eae0 12333 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12334 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12335 {
f8df10f4 12336 rel->r_addend =
8517fae7 12337 _bfd_merged_section_offset (abfd, psec,
65765700 12338 elf_section_data (sec)->sec_info,
753731ee
AM
12339 sym->st_value + rel->r_addend);
12340 if (sec != *psec)
12341 {
12342 /* If we have changed the section, and our original section is
12343 marked with SEC_EXCLUDE, it means that the original
12344 SEC_MERGE section has been completely subsumed in some
12345 other SEC_MERGE section. In this case, we need to leave
12346 some info around for --emit-relocs. */
12347 if ((sec->flags & SEC_EXCLUDE) != 0)
12348 sec->kept_section = *psec;
12349 sec = *psec;
12350 }
8517fae7
AM
12351 rel->r_addend -= relocation;
12352 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12353 }
12354 return relocation;
12355}
c629eae0
JJ
12356
12357bfd_vma
217aa764
AM
12358_bfd_elf_rel_local_sym (bfd *abfd,
12359 Elf_Internal_Sym *sym,
12360 asection **psec,
12361 bfd_vma addend)
47d9a591 12362{
c629eae0
JJ
12363 asection *sec = *psec;
12364
6835821b 12365 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12366 return sym->st_value + addend;
12367
12368 return _bfd_merged_section_offset (abfd, psec,
65765700 12369 elf_section_data (sec)->sec_info,
753731ee 12370 sym->st_value + addend);
c629eae0
JJ
12371}
12372
37b01f6a
DG
12373/* Adjust an address within a section. Given OFFSET within SEC, return
12374 the new offset within the section, based upon changes made to the
12375 section. Returns -1 if the offset is now invalid.
12376 The offset (in abnd out) is in target sized bytes, however big a
12377 byte may be. */
12378
c629eae0 12379bfd_vma
217aa764 12380_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12381 struct bfd_link_info *info,
217aa764
AM
12382 asection *sec,
12383 bfd_vma offset)
c629eae0 12384{
68bfbfcc 12385 switch (sec->sec_info_type)
65765700 12386 {
dbaa2011 12387 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12388 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12389 offset);
dbaa2011 12390 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12391 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12392
65765700 12393 default:
310fd250
L
12394 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12395 {
37b01f6a 12396 /* Reverse the offset. */
310fd250
L
12397 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12398 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12399
12400 /* address_size and sec->size are in octets. Convert
12401 to bytes before subtracting the original offset. */
61826503 12402 offset = ((sec->size - address_size)
bb294208 12403 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12404 }
65765700
JJ
12405 return offset;
12406 }
c629eae0 12407}
3333a7c3
RM
12408\f
12409/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12410 reconstruct an ELF file by reading the segments out of remote memory
12411 based on the ELF file header at EHDR_VMA and the ELF program headers it
12412 points to. If not null, *LOADBASEP is filled in with the difference
12413 between the VMAs from which the segments were read, and the VMAs the
12414 file headers (and hence BFD's idea of each section's VMA) put them at.
12415
12416 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12417 remote memory at target address VMA into the local buffer at MYADDR; it
12418 should return zero on success or an `errno' code on failure. TEMPL must
12419 be a BFD for an ELF target with the word size and byte order found in
12420 the remote memory. */
12421
12422bfd *
217aa764
AM
12423bfd_elf_bfd_from_remote_memory
12424 (bfd *templ,
12425 bfd_vma ehdr_vma,
f0a5d95a 12426 bfd_size_type size,
217aa764 12427 bfd_vma *loadbasep,
fe78531d 12428 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12429{
12430 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12431 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12432}
4c45e5c9
JJ
12433\f
12434long
c9727e01
AM
12435_bfd_elf_get_synthetic_symtab (bfd *abfd,
12436 long symcount ATTRIBUTE_UNUSED,
12437 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12438 long dynsymcount,
c9727e01
AM
12439 asymbol **dynsyms,
12440 asymbol **ret)
4c45e5c9
JJ
12441{
12442 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12443 asection *relplt;
12444 asymbol *s;
12445 const char *relplt_name;
0a1b45a2 12446 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
4c45e5c9
JJ
12447 arelent *p;
12448 long count, i, n;
12449 size_t size;
12450 Elf_Internal_Shdr *hdr;
12451 char *names;
12452 asection *plt;
12453
8615f3f2
AM
12454 *ret = NULL;
12455
90e3cdf2
JJ
12456 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12457 return 0;
12458
8615f3f2
AM
12459 if (dynsymcount <= 0)
12460 return 0;
12461
4c45e5c9
JJ
12462 if (!bed->plt_sym_val)
12463 return 0;
12464
12465 relplt_name = bed->relplt_name;
12466 if (relplt_name == NULL)
d35fd659 12467 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12468 relplt = bfd_get_section_by_name (abfd, relplt_name);
12469 if (relplt == NULL)
12470 return 0;
12471
12472 hdr = &elf_section_data (relplt)->this_hdr;
12473 if (hdr->sh_link != elf_dynsymtab (abfd)
12474 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12475 return 0;
12476
12477 plt = bfd_get_section_by_name (abfd, ".plt");
12478 if (plt == NULL)
12479 return 0;
12480
12481 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 12482 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
4c45e5c9
JJ
12483 return -1;
12484
eea6121a 12485 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12486 size = count * sizeof (asymbol);
12487 p = relplt->relocation;
cb53bf42 12488 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12489 {
12490 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12491 if (p->addend != 0)
12492 {
12493#ifdef BFD64
12494 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12495#else
12496 size += sizeof ("+0x") - 1 + 8;
12497#endif
12498 }
12499 }
4c45e5c9 12500
a50b1753 12501 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12502 if (s == NULL)
12503 return -1;
12504
12505 names = (char *) (s + count);
12506 p = relplt->relocation;
12507 n = 0;
cb53bf42 12508 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12509 {
12510 size_t len;
12511 bfd_vma addr;
12512
12513 addr = bed->plt_sym_val (i, plt, p);
12514 if (addr == (bfd_vma) -1)
12515 continue;
12516
12517 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12518 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12519 we are defining a symbol, ensure one of them is set. */
12520 if ((s->flags & BSF_LOCAL) == 0)
12521 s->flags |= BSF_GLOBAL;
6ba2a415 12522 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12523 s->section = plt;
12524 s->value = addr - plt->vma;
12525 s->name = names;
8f39ba8e 12526 s->udata.p = NULL;
4c45e5c9
JJ
12527 len = strlen ((*p->sym_ptr_ptr)->name);
12528 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12529 names += len;
041de40d
AM
12530 if (p->addend != 0)
12531 {
1d770845 12532 char buf[30], *a;
d324f6d6 12533
041de40d
AM
12534 memcpy (names, "+0x", sizeof ("+0x") - 1);
12535 names += sizeof ("+0x") - 1;
1d770845
L
12536 bfd_sprintf_vma (abfd, buf, p->addend);
12537 for (a = buf; *a == '0'; ++a)
12538 ;
12539 len = strlen (a);
12540 memcpy (names, a, len);
12541 names += len;
041de40d 12542 }
4c45e5c9
JJ
12543 memcpy (names, "@plt", sizeof ("@plt"));
12544 names += sizeof ("@plt");
8f39ba8e 12545 ++s, ++n;
4c45e5c9
JJ
12546 }
12547
12548 return n;
12549}
3d7f7666 12550
821e6ff6
AM
12551/* It is only used by x86-64 so far.
12552 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12553 but current usage would allow all of _bfd_std_section to be zero. */
12554static const asymbol lcomm_sym
12555 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12556asection _bfd_elf_large_com_section
7eacd66b 12557 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12558 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12559
0a1b45a2 12560bool
cc364be6 12561_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12562{
12563 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12564
12565 i_ehdrp = elf_elfheader (abfd);
12566
06f44071
AM
12567 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12568 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12569
df3a023b 12570 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
12571 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12572 or STB_GNU_UNIQUE binding. */
cc364be6
AM
12573 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12574 {
12575 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12576 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12577 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12578 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12579 {
12580 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
12581 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12582 "and FreeBSD targets"));
cc364be6 12583 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
12584 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12585 "only by GNU and FreeBSD targets"));
cc364be6 12586 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
12587 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12588 "only by GNU and FreeBSD targets"));
12589 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
12590 _bfd_error_handler (_("GNU_RETAIN section is supported "
12591 "only by GNU and FreeBSD targets"));
9aea1e31 12592 bfd_set_error (bfd_error_sorry);
0a1b45a2 12593 return false;
cc364be6
AM
12594 }
12595 }
0a1b45a2 12596 return true;
d1036acb 12597}
fcb93ecf
PB
12598
12599
12600/* Return TRUE for ELF symbol types that represent functions.
12601 This is the default version of this function, which is sufficient for
d8045f23 12602 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf 12603
0a1b45a2 12604bool
fcb93ecf
PB
12605_bfd_elf_is_function_type (unsigned int type)
12606{
d8045f23
NC
12607 return (type == STT_FUNC
12608 || type == STT_GNU_IFUNC);
fcb93ecf 12609}
9f296da3 12610
aef36ac1
AM
12611/* If the ELF symbol SYM might be a function in SEC, return the
12612 function size and set *CODE_OFF to the function's entry point,
12613 otherwise return zero. */
9f296da3 12614
aef36ac1
AM
12615bfd_size_type
12616_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12617 bfd_vma *code_off)
9f296da3 12618{
aef36ac1
AM
12619 bfd_size_type size;
12620
ff9e0f5b 12621 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12622 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12623 || sym->section != sec)
12624 return 0;
ff9e0f5b 12625
ff9e0f5b 12626 *code_off = sym->value;
aef36ac1
AM
12627 size = 0;
12628 if (!(sym->flags & BSF_SYNTHETIC))
12629 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12630 if (size == 0)
12631 size = 1;
12632 return size;
9f296da3 12633}
a8e14f4c
NC
12634
12635/* Set to non-zero to enable some debug messages. */
12636#define DEBUG_SECONDARY_RELOCS 0
12637
12638/* An internal-to-the-bfd-library only section type
12639 used to indicate a cached secondary reloc section. */
12640#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12641
12642/* Create a BFD section to hold a secondary reloc section. */
12643
0a1b45a2 12644bool
a8e14f4c
NC
12645_bfd_elf_init_secondary_reloc_section (bfd * abfd,
12646 Elf_Internal_Shdr *hdr,
12647 const char * name,
12648 unsigned int shindex)
12649{
12650 /* We only support RELA secondary relocs. */
12651 if (hdr->sh_type != SHT_RELA)
0a1b45a2 12652 return false;
a8e14f4c
NC
12653
12654#if DEBUG_SECONDARY_RELOCS
12655 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12656#endif
12657 hdr->sh_type = SHT_SECONDARY_RELOC;
12658 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12659}
12660
12661/* Read in any secondary relocs associated with SEC. */
12662
0a1b45a2 12663bool
f60742b2
NC
12664_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12665 asection * sec,
12666 asymbol ** symbols,
0a1b45a2 12667 bool dynamic)
a8e14f4c
NC
12668{
12669 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12670 asection * relsec;
0a1b45a2 12671 bool result = true;
a8e14f4c
NC
12672 bfd_vma (*r_sym) (bfd_vma);
12673
12674#if BFD_DEFAULT_TARGET_SIZE > 32
12675 if (bfd_arch_bits_per_address (abfd) != 32)
12676 r_sym = elf64_r_sym;
12677 else
12678#endif
12679 r_sym = elf32_r_sym;
12680
12681 /* Discover if there are any secondary reloc sections
12682 associated with SEC. */
12683 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12684 {
12685 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12686
12687 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
12688 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
12689 && (hdr->sh_entsize == ebd->s->sizeof_rel
12690 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
12691 {
12692 bfd_byte * native_relocs;
12693 bfd_byte * native_reloc;
12694 arelent * internal_relocs;
12695 arelent * internal_reloc;
12696 unsigned int i;
12697 unsigned int entsize;
12698 unsigned int symcount;
12699 unsigned int reloc_count;
12700 size_t amt;
12701
12702 if (ebd->elf_info_to_howto == NULL)
0a1b45a2 12703 return false;
a8e14f4c
NC
12704
12705#if DEBUG_SECONDARY_RELOCS
12706 fprintf (stderr, "read secondary relocs for %s from %s\n",
12707 sec->name, relsec->name);
12708#endif
12709 entsize = hdr->sh_entsize;
12710
12711 native_relocs = bfd_malloc (hdr->sh_size);
12712 if (native_relocs == NULL)
12713 {
0a1b45a2 12714 result = false;
a8e14f4c
NC
12715 continue;
12716 }
12717
12718 reloc_count = NUM_SHDR_ENTRIES (hdr);
12719 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12720 {
ecbbbdba 12721 free (native_relocs);
a8e14f4c 12722 bfd_set_error (bfd_error_file_too_big);
0a1b45a2 12723 result = false;
a8e14f4c
NC
12724 continue;
12725 }
12726
12727 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12728 if (internal_relocs == NULL)
12729 {
12730 free (native_relocs);
0a1b45a2 12731 result = false;
a8e14f4c
NC
12732 continue;
12733 }
12734
12735 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12736 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12737 != hdr->sh_size))
12738 {
12739 free (native_relocs);
ecbbbdba
NC
12740 /* The internal_relocs will be freed when
12741 the memory for the bfd is released. */
0a1b45a2 12742 result = false;
a8e14f4c
NC
12743 continue;
12744 }
12745
f60742b2
NC
12746 if (dynamic)
12747 symcount = bfd_get_dynamic_symcount (abfd);
12748 else
12749 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
12750
12751 for (i = 0, internal_reloc = internal_relocs,
12752 native_reloc = native_relocs;
12753 i < reloc_count;
12754 i++, internal_reloc++, native_reloc += entsize)
12755 {
0a1b45a2 12756 bool res;
a8e14f4c
NC
12757 Elf_Internal_Rela rela;
12758
8ee54925
NC
12759 if (entsize == ebd->s->sizeof_rel)
12760 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
12761 else /* entsize == ebd->s->sizeof_rela */
12762 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
12763
12764 /* The address of an ELF reloc is section relative for an object
12765 file, and absolute for an executable file or shared library.
12766 The address of a normal BFD reloc is always section relative,
12767 and the address of a dynamic reloc is absolute.. */
12768 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12769 internal_reloc->address = rela.r_offset;
12770 else
12771 internal_reloc->address = rela.r_offset - sec->vma;
12772
12773 if (r_sym (rela.r_info) == STN_UNDEF)
12774 {
12775 /* FIXME: This and the error case below mean that we
12776 have a symbol on relocs that is not elf_symbol_type. */
12777 internal_reloc->sym_ptr_ptr =
12778 bfd_abs_section_ptr->symbol_ptr_ptr;
12779 }
12780 else if (r_sym (rela.r_info) > symcount)
12781 {
12782 _bfd_error_handler
12783 /* xgettext:c-format */
12784 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12785 abfd, sec, i, (long) r_sym (rela.r_info));
12786 bfd_set_error (bfd_error_bad_value);
12787 internal_reloc->sym_ptr_ptr =
12788 bfd_abs_section_ptr->symbol_ptr_ptr;
0a1b45a2 12789 result = false;
a8e14f4c
NC
12790 }
12791 else
12792 {
12793 asymbol **ps;
12794
12795 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
12796 internal_reloc->sym_ptr_ptr = ps;
12797 /* Make sure that this symbol is not removed by strip. */
12798 (*ps)->flags |= BSF_KEEP;
12799 }
12800
12801 internal_reloc->addend = rela.r_addend;
12802
12803 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12804 if (! res || internal_reloc->howto == NULL)
12805 {
12806#if DEBUG_SECONDARY_RELOCS
12807 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12808 rela.r_info);
12809#endif
0a1b45a2 12810 result = false;
a8e14f4c
NC
12811 }
12812 }
12813
12814 free (native_relocs);
12815 /* Store the internal relocs. */
12816 elf_section_data (relsec)->sec_info = internal_relocs;
12817 }
12818 }
12819
12820 return result;
12821}
12822
12823/* Set the ELF section header fields of an output secondary reloc section. */
12824
0a1b45a2 12825bool
a8e14f4c
NC
12826_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12827 bfd * obfd ATTRIBUTE_UNUSED,
12828 const Elf_Internal_Shdr * isection,
12829 Elf_Internal_Shdr * osection)
12830{
12831 asection * isec;
12832 asection * osec;
44466e45 12833 struct bfd_elf_section_data * esd;
a8e14f4c
NC
12834
12835 if (isection == NULL)
0a1b45a2 12836 return false;
a8e14f4c
NC
12837
12838 if (isection->sh_type != SHT_SECONDARY_RELOC)
0a1b45a2 12839 return true;
a8e14f4c
NC
12840
12841 isec = isection->bfd_section;
12842 if (isec == NULL)
0a1b45a2 12843 return false;
a8e14f4c
NC
12844
12845 osec = osection->bfd_section;
12846 if (osec == NULL)
0a1b45a2 12847 return false;
a8e14f4c 12848
44466e45
NC
12849 esd = elf_section_data (osec);
12850 BFD_ASSERT (esd->sec_info == NULL);
12851 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
12852 osection->sh_type = SHT_RELA;
12853 osection->sh_link = elf_onesymtab (obfd);
12854 if (osection->sh_link == 0)
12855 {
12856 /* There is no symbol table - we are hosed... */
12857 _bfd_error_handler
12858 /* xgettext:c-format */
12859 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12860 obfd, osec);
12861 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12862 return false;
a8e14f4c
NC
12863 }
12864
12865 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
12866 if (isection->sh_info == 0
12867 || isection->sh_info >= elf_numsections (ibfd))
12868 {
12869 _bfd_error_handler
12870 /* xgettext:c-format */
12871 (_("%pB(%pA): info section index is invalid"),
12872 obfd, osec);
12873 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12874 return false;
327ef784
NC
12875 }
12876
a8e14f4c
NC
12877 isection = elf_elfsections (ibfd)[isection->sh_info];
12878
327ef784
NC
12879 if (isection == NULL
12880 || isection->bfd_section == NULL
12881 || isection->bfd_section->output_section == NULL)
12882 {
12883 _bfd_error_handler
12884 /* xgettext:c-format */
12885 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12886 obfd, osec);
12887 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12888 return false;
327ef784
NC
12889 }
12890
44466e45
NC
12891 esd = elf_section_data (isection->bfd_section->output_section);
12892 BFD_ASSERT (esd != NULL);
12893 osection->sh_info = esd->this_idx;
0a1b45a2 12894 esd->has_secondary_relocs = true;
a8e14f4c
NC
12895#if DEBUG_SECONDARY_RELOCS
12896 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12897 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
12898 fprintf (stderr, "mark section %s as having secondary relocs\n",
12899 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
12900#endif
12901
0a1b45a2 12902 return true;
a8e14f4c
NC
12903}
12904
44466e45
NC
12905/* Write out a secondary reloc section.
12906
12907 FIXME: Currently this function can result in a serious performance penalty
12908 for files with secondary relocs and lots of sections. The proper way to
12909 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12910 relocs together and then to have this function just walk that chain. */
a8e14f4c 12911
0a1b45a2 12912bool
a8e14f4c
NC
12913_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12914{
12915 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12916 bfd_vma addr_offset;
12917 asection * relsec;
12918 bfd_vma (*r_info) (bfd_vma, bfd_vma);
0a1b45a2 12919 bool result = true;
ac4bf06c
NC
12920
12921 if (sec == NULL)
0a1b45a2 12922 return false;
a8e14f4c
NC
12923
12924#if BFD_DEFAULT_TARGET_SIZE > 32
12925 if (bfd_arch_bits_per_address (abfd) != 32)
12926 r_info = elf64_r_info;
12927 else
12928#endif
12929 r_info = elf32_r_info;
12930
a8e14f4c
NC
12931 /* The address of an ELF reloc is section relative for an object
12932 file, and absolute for an executable file or shared library.
12933 The address of a BFD reloc is always section relative. */
12934 addr_offset = 0;
12935 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
12936 addr_offset = sec->vma;
12937
12938 /* Discover if there are any secondary reloc sections
12939 associated with SEC. */
12940 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12941 {
12942 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
12943 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
12944
12945 if (hdr->sh_type == SHT_RELA
12946 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
12947 {
12948 asymbol * last_sym;
12949 int last_sym_idx;
12950 unsigned int reloc_count;
12951 unsigned int idx;
8ee54925 12952 unsigned int entsize;
a8e14f4c
NC
12953 arelent * src_irel;
12954 bfd_byte * dst_rela;
12955
ac4bf06c
NC
12956 if (hdr->contents != NULL)
12957 {
12958 _bfd_error_handler
12959 /* xgettext:c-format */
12960 (_("%pB(%pA): error: secondary reloc section processed twice"),
12961 abfd, relsec);
12962 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12963 result = false;
ac4bf06c
NC
12964 continue;
12965 }
a8e14f4c 12966
8ee54925
NC
12967 entsize = hdr->sh_entsize;
12968 if (entsize == 0)
ac267c75
NC
12969 {
12970 _bfd_error_handler
12971 /* xgettext:c-format */
12972 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
12973 abfd, relsec);
12974 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12975 result = false;
ac267c75
NC
12976 continue;
12977 }
8ee54925
NC
12978 else if (entsize != ebd->s->sizeof_rel
12979 && entsize != ebd->s->sizeof_rela)
12980 {
12981 _bfd_error_handler
12982 /* xgettext:c-format */
12983 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
12984 abfd, relsec);
12985 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12986 result = false;
8ee54925
NC
12987 continue;
12988 }
ac267c75 12989
8ee54925 12990 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
12991 if (reloc_count <= 0)
12992 {
12993 _bfd_error_handler
12994 /* xgettext:c-format */
12995 (_("%pB(%pA): error: secondary reloc section is empty!"),
12996 abfd, relsec);
12997 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12998 result = false;
ac4bf06c
NC
12999 continue;
13000 }
a8e14f4c
NC
13001
13002 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13003 if (hdr->contents == NULL)
13004 continue;
13005
13006#if DEBUG_SECONDARY_RELOCS
13007 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13008 reloc_count, sec->name, relsec->name);
13009#endif
13010 last_sym = NULL;
13011 last_sym_idx = 0;
13012 dst_rela = hdr->contents;
13013 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
13014 if (src_irel == NULL)
13015 {
13016 _bfd_error_handler
13017 /* xgettext:c-format */
13018 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
13019 abfd, relsec);
13020 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13021 result = false;
ac4bf06c
NC
13022 continue;
13023 }
a8e14f4c 13024
8ee54925 13025 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
13026 {
13027 Elf_Internal_Rela src_rela;
13028 arelent *ptr;
13029 asymbol *sym;
13030 int n;
13031
13032 ptr = src_irel + idx;
ac4bf06c
NC
13033 if (ptr == NULL)
13034 {
13035 _bfd_error_handler
13036 /* xgettext:c-format */
13037 (_("%pB(%pA): error: reloc table entry %u is empty"),
13038 abfd, relsec, idx);
13039 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13040 result = false;
ac4bf06c
NC
13041 break;
13042 }
a8e14f4c 13043
ac4bf06c
NC
13044 if (ptr->sym_ptr_ptr == NULL)
13045 {
13046 /* FIXME: Is this an error ? */
13047 n = 0;
13048 }
a8e14f4c
NC
13049 else
13050 {
ac4bf06c
NC
13051 sym = *ptr->sym_ptr_ptr;
13052
13053 if (sym == last_sym)
13054 n = last_sym_idx;
13055 else
a8e14f4c 13056 {
ac4bf06c
NC
13057 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13058 if (n < 0)
13059 {
13060 _bfd_error_handler
13061 /* xgettext:c-format */
13062 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
13063 abfd, relsec, idx);
13064 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13065 result = false;
ac4bf06c
NC
13066 n = 0;
13067 }
13068
13069 last_sym = sym;
13070 last_sym_idx = n;
a8e14f4c 13071 }
a8e14f4c 13072
ac4bf06c
NC
13073 if (sym->the_bfd != NULL
13074 && sym->the_bfd->xvec != abfd->xvec
13075 && ! _bfd_elf_validate_reloc (abfd, ptr))
13076 {
13077 _bfd_error_handler
13078 /* xgettext:c-format */
13079 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
13080 abfd, relsec, idx);
13081 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13082 result = false;
ac4bf06c
NC
13083 n = 0;
13084 }
a8e14f4c
NC
13085 }
13086
ac4bf06c 13087 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13088 if (ptr->howto == NULL)
13089 {
ac4bf06c
NC
13090 _bfd_error_handler
13091 /* xgettext:c-format */
13092 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13093 abfd, relsec, idx);
13094 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13095 result = false;
ac4bf06c 13096 src_rela.r_info = r_info (0, 0);
a8e14f4c 13097 }
ac4bf06c
NC
13098 else
13099 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13100 src_rela.r_addend = ptr->addend;
8ee54925
NC
13101
13102 if (entsize == ebd->s->sizeof_rel)
13103 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13104 else /* entsize == ebd->s->sizeof_rela */
13105 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13106 }
13107 }
13108 }
13109
ac4bf06c 13110 return result;
a8e14f4c 13111}
This page took 2.66183 seconds and 4 git commands to generate.