]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
bfd: Add bfd_find_nearest_line_with_alt
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
a2c58332 3 Copyright (C) 1993-2022 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 {
11c6a7c6
AM
432 if (symtab_hdr == &elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used
434 to work. */
435 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
6a40cf0c
NC
436 /* Otherwise we do nothing. The assumption is that
437 the index table will not be needed. */
438 }
439 }
6cdc0ccc
AM
440
441 /* Read the symbols. */
442 alloc_ext = NULL;
443 alloc_extshndx = NULL;
4dd07732 444 alloc_intsym = NULL;
6cdc0ccc
AM
445 bed = get_elf_backend_data (ibfd);
446 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
447 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
448 {
449 bfd_set_error (bfd_error_file_too_big);
450 intsym_buf = NULL;
451 goto out;
452 }
6cdc0ccc
AM
453 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
454 if (extsym_buf == NULL)
455 {
1f4361a7 456 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
457 extsym_buf = alloc_ext;
458 }
459 if (extsym_buf == NULL
460 || bfd_seek (ibfd, pos, SEEK_SET) != 0
461 || bfd_bread (extsym_buf, amt, ibfd) != amt)
462 {
463 intsym_buf = NULL;
464 goto out;
465 }
466
467 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
468 extshndx_buf = NULL;
469 else
470 {
1f4361a7
AM
471 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
472 {
473 bfd_set_error (bfd_error_file_too_big);
474 intsym_buf = NULL;
475 goto out;
476 }
6cdc0ccc
AM
477 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
478 if (extshndx_buf == NULL)
479 {
1f4361a7 480 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
481 extshndx_buf = alloc_extshndx;
482 }
483 if (extshndx_buf == NULL
484 || bfd_seek (ibfd, pos, SEEK_SET) != 0
485 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
486 {
487 intsym_buf = NULL;
488 goto out;
489 }
490 }
491
492 if (intsym_buf == NULL)
493 {
1f4361a7
AM
494 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
495 {
496 bfd_set_error (bfd_error_file_too_big);
497 goto out;
498 }
499 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 500 intsym_buf = alloc_intsym;
6cdc0ccc
AM
501 if (intsym_buf == NULL)
502 goto out;
503 }
504
505 /* Convert the symbols to internal form. */
506 isymend = intsym_buf + symcount;
a50b1753 507 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 508 shndx = extshndx_buf;
6cdc0ccc
AM
509 isym < isymend;
510 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
511 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
512 {
513 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 514 /* xgettext:c-format */
871b3ab2 515 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 516 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 517 ibfd, (unsigned long) symoffset);
c9594989 518 free (alloc_intsym);
8384fb8f
AM
519 intsym_buf = NULL;
520 goto out;
521 }
6cdc0ccc
AM
522
523 out:
c9594989
AM
524 free (alloc_ext);
525 free (alloc_extshndx);
6cdc0ccc
AM
526
527 return intsym_buf;
528}
529
5cab59f6
AM
530/* Look up a symbol name. */
531const char *
be8dd2ca
AM
532bfd_elf_sym_name (bfd *abfd,
533 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
534 Elf_Internal_Sym *isym,
535 asection *sym_sec)
5cab59f6 536{
26c61ae5 537 const char *name;
5cab59f6 538 unsigned int iname = isym->st_name;
be8dd2ca 539 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 540
138f35cc
JJ
541 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
542 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 543 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
544 {
545 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
546 shindex = elf_elfheader (abfd)->e_shstrndx;
547 }
548
26c61ae5
L
549 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
550 if (name == NULL)
551 name = "(null)";
552 else if (sym_sec && *name == '\0')
fd361982 553 name = bfd_section_name (sym_sec);
26c61ae5
L
554
555 return name;
5cab59f6
AM
556}
557
dbb410c3
AM
558/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
559 sections. The first element is the flags, the rest are section
560 pointers. */
561
562typedef union elf_internal_group {
563 Elf_Internal_Shdr *shdr;
564 unsigned int flags;
565} Elf_Internal_Group;
566
b885599b
AM
567/* Return the name of the group signature symbol. Why isn't the
568 signature just a string? */
569
570static const char *
217aa764 571group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 572{
9dce4196 573 Elf_Internal_Shdr *hdr;
9dce4196
AM
574 unsigned char esym[sizeof (Elf64_External_Sym)];
575 Elf_External_Sym_Shndx eshndx;
576 Elf_Internal_Sym isym;
b885599b 577
13792e9d
L
578 /* First we need to ensure the symbol table is available. Make sure
579 that it is a symbol table section. */
4fbb74a6
AM
580 if (ghdr->sh_link >= elf_numsections (abfd))
581 return NULL;
13792e9d
L
582 hdr = elf_elfsections (abfd) [ghdr->sh_link];
583 if (hdr->sh_type != SHT_SYMTAB
584 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
585 return NULL;
586
9dce4196
AM
587 /* Go read the symbol. */
588 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
589 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
590 &isym, esym, &eshndx) == NULL)
b885599b 591 return NULL;
9dce4196 592
26c61ae5 593 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
594}
595
dbb410c3
AM
596/* Set next_in_group list pointer, and group name for NEWSECT. */
597
0a1b45a2 598static bool
217aa764 599setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
600{
601 unsigned int num_group = elf_tdata (abfd)->num_group;
602
603 /* If num_group is zero, read in all SHT_GROUP sections. The count
604 is set to -1 if there are no SHT_GROUP sections. */
605 if (num_group == 0)
606 {
607 unsigned int i, shnum;
608
609 /* First count the number of groups. If we have a SHT_GROUP
610 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 611 shnum = elf_numsections (abfd);
dbb410c3 612 num_group = 0;
08a40648 613
44534af3 614#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 615 ( (shdr)->sh_type == SHT_GROUP \
44534af3 616 && (shdr)->sh_size >= minsize \
1783205a
NC
617 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
618 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 619
dbb410c3
AM
620 for (i = 0; i < shnum; i++)
621 {
622 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 623
44534af3 624 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
625 num_group += 1;
626 }
627
628 if (num_group == 0)
20dbb49d
L
629 {
630 num_group = (unsigned) -1;
631 elf_tdata (abfd)->num_group = num_group;
ce497010 632 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
633 }
634 else
dbb410c3
AM
635 {
636 /* We keep a list of elf section headers for group sections,
637 so we can find them quickly. */
1f4361a7 638 size_t amt;
d0fb9a8d 639
20dbb49d 640 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
641 amt = num_group * sizeof (Elf_Internal_Shdr *);
642 elf_tdata (abfd)->group_sect_ptr
643 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 644 if (elf_tdata (abfd)->group_sect_ptr == NULL)
0a1b45a2 645 return false;
dbb410c3 646 num_group = 0;
ce497010 647
dbb410c3
AM
648 for (i = 0; i < shnum; i++)
649 {
650 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 651
44534af3 652 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 653 {
973ffd63 654 unsigned char *src;
dbb410c3
AM
655 Elf_Internal_Group *dest;
656
07d6d2b8
AM
657 /* Make sure the group section has a BFD section
658 attached to it. */
659 if (!bfd_section_from_shdr (abfd, i))
0a1b45a2 660 return false;
07d6d2b8 661
dbb410c3
AM
662 /* Add to list of sections. */
663 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 num_group += 1;
665
666 /* Read the raw contents. */
1f4361a7
AM
667 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
668 shdr->contents = NULL;
669 if (_bfd_mul_overflow (shdr->sh_size,
670 sizeof (*dest) / 4, &amt)
1f4361a7 671 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
672 || !(shdr->contents
673 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
674 {
675 _bfd_error_handler
695344c0 676 /* xgettext:c-format */
871b3ab2 677 (_("%pB: invalid size field in group section"
2dcf00ce
AM
678 " header: %#" PRIx64 ""),
679 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
680 bfd_set_error (bfd_error_bad_value);
681 -- num_group;
493a3386
NC
682 continue;
683 }
708d7d0d 684
dbb410c3
AM
685 /* Translate raw contents, a flag word followed by an
686 array of elf section indices all in target byte order,
687 to the flag word followed by an array of elf section
688 pointers. */
689 src = shdr->contents + shdr->sh_size;
690 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 691
dbb410c3
AM
692 while (1)
693 {
694 unsigned int idx;
695
696 src -= 4;
697 --dest;
698 idx = H_GET_32 (abfd, src);
699 if (src == shdr->contents)
700 {
327301a4 701 dest->shdr = NULL;
dbb410c3 702 dest->flags = idx;
b885599b
AM
703 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
704 shdr->bfd_section->flags
705 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
706 break;
707 }
4bba0fb1 708 if (idx < shnum)
bae363f1
L
709 {
710 dest->shdr = elf_elfsections (abfd)[idx];
711 /* PR binutils/23199: All sections in a
712 section group should be marked with
713 SHF_GROUP. But some tools generate
714 broken objects without SHF_GROUP. Fix
715 them up here. */
716 dest->shdr->sh_flags |= SHF_GROUP;
717 }
4bba0fb1
AM
718 if (idx >= shnum
719 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 720 {
4eca0228 721 _bfd_error_handler
4bba0fb1
AM
722 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
723 abfd, i);
724 dest->shdr = NULL;
dbb410c3 725 }
dbb410c3
AM
726 }
727 }
728 }
493a3386
NC
729
730 /* PR 17510: Corrupt binaries might contain invalid groups. */
731 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
732 {
733 elf_tdata (abfd)->num_group = num_group;
734
735 /* If all groups are invalid then fail. */
736 if (num_group == 0)
737 {
738 elf_tdata (abfd)->group_sect_ptr = NULL;
739 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 740 _bfd_error_handler
871b3ab2 741 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
742 bfd_set_error (bfd_error_bad_value);
743 }
744 }
dbb410c3
AM
745 }
746 }
747
748 if (num_group != (unsigned) -1)
749 {
564e11c9
JW
750 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
751 unsigned int j;
dbb410c3 752
564e11c9 753 for (j = 0; j < num_group; j++)
dbb410c3 754 {
564e11c9
JW
755 /* Begin search from previous found group. */
756 unsigned i = (j + search_offset) % num_group;
757
dbb410c3 758 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 759 Elf_Internal_Group *idx;
0c54f692 760 bfd_size_type n_elt;
ce497010
NC
761
762 if (shdr == NULL)
763 continue;
764
765 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
766 if (idx == NULL || shdr->sh_size < 4)
767 {
768 /* See PR 21957 for a reproducer. */
769 /* xgettext:c-format */
871b3ab2 770 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
771 abfd, shdr->bfd_section);
772 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
773 bfd_set_error (bfd_error_bad_value);
0a1b45a2 774 return false;
0c54f692 775 }
ce497010 776 n_elt = shdr->sh_size / 4;
dbb410c3
AM
777
778 /* Look through this group's sections to see if current
779 section is a member. */
780 while (--n_elt != 0)
781 if ((++idx)->shdr == hdr)
782 {
e0e8c97f 783 asection *s = NULL;
dbb410c3
AM
784
785 /* We are a member of this group. Go looking through
786 other members to see if any others are linked via
787 next_in_group. */
788 idx = (Elf_Internal_Group *) shdr->contents;
789 n_elt = shdr->sh_size / 4;
790 while (--n_elt != 0)
4bba0fb1
AM
791 if ((++idx)->shdr != NULL
792 && (s = idx->shdr->bfd_section) != NULL
945906ff 793 && elf_next_in_group (s) != NULL)
dbb410c3
AM
794 break;
795 if (n_elt != 0)
796 {
dbb410c3
AM
797 /* Snarf the group name from other member, and
798 insert current section in circular list. */
945906ff
AM
799 elf_group_name (newsect) = elf_group_name (s);
800 elf_next_in_group (newsect) = elf_next_in_group (s);
801 elf_next_in_group (s) = newsect;
dbb410c3
AM
802 }
803 else
804 {
dbb410c3
AM
805 const char *gname;
806
b885599b
AM
807 gname = group_signature (abfd, shdr);
808 if (gname == NULL)
0a1b45a2 809 return false;
945906ff 810 elf_group_name (newsect) = gname;
dbb410c3
AM
811
812 /* Start a circular list with one element. */
945906ff 813 elf_next_in_group (newsect) = newsect;
dbb410c3 814 }
b885599b 815
9dce4196
AM
816 /* If the group section has been created, point to the
817 new member. */
dbb410c3 818 if (shdr->bfd_section != NULL)
945906ff 819 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 820
564e11c9
JW
821 elf_tdata (abfd)->group_search_offset = i;
822 j = num_group - 1;
dbb410c3
AM
823 break;
824 }
825 }
826 }
827
945906ff 828 if (elf_group_name (newsect) == NULL)
dbb410c3 829 {
695344c0 830 /* xgettext:c-format */
871b3ab2 831 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 832 abfd, newsect);
0a1b45a2 833 return false;
dbb410c3 834 }
0a1b45a2 835 return true;
dbb410c3
AM
836}
837
0a1b45a2 838bool
dd863624 839_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
840{
841 unsigned int i;
842 unsigned int num_group = elf_tdata (abfd)->num_group;
0a1b45a2 843 bool result = true;
dd863624
L
844 asection *s;
845
846 /* Process SHF_LINK_ORDER. */
847 for (s = abfd->sections; s != NULL; s = s->next)
848 {
849 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
850 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
851 {
852 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
853 /* An sh_link value of 0 is now allowed. It indicates that linked
854 to section has already been discarded, but that the current
855 section has been retained for some other reason. This linking
856 section is still a candidate for later garbage collection
857 however. */
dd863624
L
858 if (elfsec == 0)
859 {
b71702f1 860 elf_linked_to_section (s) = NULL;
dd863624
L
861 }
862 else
863 {
91d6fa6a 864 asection *linksec = NULL;
25bbc984 865
4fbb74a6
AM
866 if (elfsec < elf_numsections (abfd))
867 {
868 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 869 linksec = this_hdr->bfd_section;
4fbb74a6 870 }
25bbc984
L
871
872 /* PR 1991, 2008:
873 Some strip/objcopy may leave an incorrect value in
874 sh_link. We don't want to proceed. */
91d6fa6a 875 if (linksec == NULL)
25bbc984 876 {
4eca0228 877 _bfd_error_handler
695344c0 878 /* xgettext:c-format */
871b3ab2 879 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 880 s->owner, elfsec, s);
0a1b45a2 881 result = false;
25bbc984
L
882 }
883
91d6fa6a 884 elf_linked_to_section (s) = linksec;
dd863624
L
885 }
886 }
53720c49
AM
887 else if (this_hdr->sh_type == SHT_GROUP
888 && elf_next_in_group (s) == NULL)
889 {
4eca0228 890 _bfd_error_handler
695344c0 891 /* xgettext:c-format */
871b3ab2 892 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49 893 abfd, elf_section_data (s)->this_idx);
0a1b45a2 894 result = false;
53720c49 895 }
dd863624 896 }
3d7f7666 897
dd863624 898 /* Process section groups. */
3d7f7666
L
899 if (num_group == (unsigned) -1)
900 return result;
901
902 for (i = 0; i < num_group; i++)
903 {
904 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
905 Elf_Internal_Group *idx;
906 unsigned int n_elt;
3d7f7666 907
11c6a7c6
AM
908 /* PR binutils/18758: Beware of corrupt binaries with invalid
909 group data. */
4b0e8a5f
NC
910 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
911 {
4eca0228 912 _bfd_error_handler
695344c0 913 /* xgettext:c-format */
871b3ab2 914 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f 915 abfd, i);
0a1b45a2 916 result = false;
4b0e8a5f
NC
917 continue;
918 }
919
920 idx = (Elf_Internal_Group *) shdr->contents;
921 n_elt = shdr->sh_size / 4;
1b786873 922
3d7f7666 923 while (--n_elt != 0)
24d3e51b
NC
924 {
925 ++ idx;
926
927 if (idx->shdr == NULL)
928 continue;
929 else if (idx->shdr->bfd_section)
930 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
931 else if (idx->shdr->sh_type != SHT_RELA
932 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
933 {
934 /* There are some unknown sections in the group. */
935 _bfd_error_handler
936 /* xgettext:c-format */
871b3ab2 937 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
938 abfd,
939 idx->shdr->sh_type,
940 bfd_elf_string_from_elf_section (abfd,
941 (elf_elfheader (abfd)
942 ->e_shstrndx),
943 idx->shdr->sh_name),
944 shdr->bfd_section);
0a1b45a2 945 result = false;
24d3e51b
NC
946 }
947 }
3d7f7666 948 }
24d3e51b 949
3d7f7666
L
950 return result;
951}
952
0a1b45a2 953bool
72adc230
AM
954bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
955{
956 return elf_next_in_group (sec) != NULL;
957}
958
cb7f4b29
AM
959const char *
960bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
961{
962 if (elf_sec_group (sec) != NULL)
963 return elf_group_name (sec);
964 return NULL;
965}
966
f6fe1ccd
L
967static char *
968convert_debug_to_zdebug (bfd *abfd, const char *name)
969{
970 unsigned int len = strlen (name);
971 char *new_name = bfd_alloc (abfd, len + 2);
972 if (new_name == NULL)
973 return NULL;
974 new_name[0] = '.';
975 new_name[1] = 'z';
976 memcpy (new_name + 2, name + 1, len);
977 return new_name;
978}
979
980static char *
981convert_zdebug_to_debug (bfd *abfd, const char *name)
982{
983 unsigned int len = strlen (name);
984 char *new_name = bfd_alloc (abfd, len);
985 if (new_name == NULL)
986 return NULL;
987 new_name[0] = '.';
988 memcpy (new_name + 1, name + 2, len - 1);
989 return new_name;
990}
991
cc5277b1
ML
992/* This a copy of lto_section defined in GCC (lto-streamer.h). */
993
994struct lto_section
995{
996 int16_t major_version;
997 int16_t minor_version;
998 unsigned char slim_object;
999
1000 /* Flags is a private field that is not defined publicly. */
1001 uint16_t flags;
1002};
1003
252b5132
RH
1004/* Make a BFD section from an ELF section. We store a pointer to the
1005 BFD section in the bfd_section field of the header. */
1006
0a1b45a2 1007bool
217aa764
AM
1008_bfd_elf_make_section_from_shdr (bfd *abfd,
1009 Elf_Internal_Shdr *hdr,
6dc132d9
L
1010 const char *name,
1011 int shindex)
252b5132
RH
1012{
1013 asection *newsect;
1014 flagword flags;
9c5bfbb7 1015 const struct elf_backend_data *bed;
502794d4 1016 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1017
1018 if (hdr->bfd_section != NULL)
0a1b45a2 1019 return true;
252b5132
RH
1020
1021 newsect = bfd_make_section_anyway (abfd, name);
1022 if (newsect == NULL)
0a1b45a2 1023 return false;
252b5132 1024
1829f4b2
AM
1025 hdr->bfd_section = newsect;
1026 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1027 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1028
2f89ff8d
L
1029 /* Always use the real type/flags. */
1030 elf_section_type (newsect) = hdr->sh_type;
1031 elf_section_flags (newsect) = hdr->sh_flags;
1032
252b5132
RH
1033 newsect->filepos = hdr->sh_offset;
1034
252b5132
RH
1035 flags = SEC_NO_FLAGS;
1036 if (hdr->sh_type != SHT_NOBITS)
1037 flags |= SEC_HAS_CONTENTS;
dbb410c3 1038 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1039 flags |= SEC_GROUP;
252b5132
RH
1040 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1041 {
1042 flags |= SEC_ALLOC;
1043 if (hdr->sh_type != SHT_NOBITS)
1044 flags |= SEC_LOAD;
1045 }
1046 if ((hdr->sh_flags & SHF_WRITE) == 0)
1047 flags |= SEC_READONLY;
1048 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1049 flags |= SEC_CODE;
1050 else if ((flags & SEC_LOAD) != 0)
1051 flags |= SEC_DATA;
f5fa8ca2
JJ
1052 if ((hdr->sh_flags & SHF_MERGE) != 0)
1053 {
1054 flags |= SEC_MERGE;
1055 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1056 }
84865015
NC
1057 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1058 flags |= SEC_STRINGS;
dbb410c3
AM
1059 if (hdr->sh_flags & SHF_GROUP)
1060 if (!setup_group (abfd, hdr, newsect))
0a1b45a2 1061 return false;
13ae64f3
JJ
1062 if ((hdr->sh_flags & SHF_TLS) != 0)
1063 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1064 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1065 flags |= SEC_EXCLUDE;
252b5132 1066
df3a023b
AM
1067 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1068 {
1069 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1070 but binutils as of 2019-07-23 did not set the EI_OSABI header
1071 byte. */
df3a023b
AM
1072 case ELFOSABI_GNU:
1073 case ELFOSABI_FREEBSD:
99fabbc9
JL
1074 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1075 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1076 /* Fall through */
1077 case ELFOSABI_NONE:
df3a023b
AM
1078 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1079 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1080 break;
1081 }
1082
3d2b39cf 1083 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1084 {
3d2b39cf
L
1085 /* The debugging sections appear to be recognized only by name,
1086 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1087 if (name [0] == '.')
1088 {
3f3328b8
ML
1089 if (startswith (name, ".debug")
1090 || startswith (name, ".gnu.debuglto_.debug_")
1091 || startswith (name, ".gnu.linkonce.wi.")
1092 || startswith (name, ".zdebug"))
bb294208 1093 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
3f3328b8
ML
1094 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1095 || startswith (name, ".note.gnu"))
502794d4
CE
1096 {
1097 flags |= SEC_ELF_OCTETS;
1098 opb = 1;
1099 }
3f3328b8
ML
1100 else if (startswith (name, ".line")
1101 || startswith (name, ".stab")
bb294208 1102 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1103 flags |= SEC_DEBUGGING;
1104 }
1105 }
252b5132 1106
502794d4
CE
1107 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1108 || !bfd_set_section_size (newsect, hdr->sh_size)
1f9b1a84
AM
1109 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1110 & -hdr->sh_addralign)))
0a1b45a2 1111 return false;
502794d4 1112
252b5132
RH
1113 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1114 only link a single copy of the section. This is used to support
1115 g++. g++ will emit each template expansion in its own section.
1116 The symbols will be defined as weak, so that multiple definitions
1117 are permitted. The GNU linker extension is to actually discard
1118 all but one of the sections. */
08dedd66 1119 if (startswith (name, ".gnu.linkonce")
b885599b 1120 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1121 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1122
8c803a2d 1123 if (!bfd_set_section_flags (newsect, flags))
0a1b45a2 1124 return false;
8c803a2d 1125
fa152c49
JW
1126 bed = get_elf_backend_data (abfd);
1127 if (bed->elf_backend_section_flags)
8c803a2d 1128 if (!bed->elf_backend_section_flags (hdr))
0a1b45a2 1129 return false;
fa152c49 1130
718175fa
JK
1131 /* We do not parse the PT_NOTE segments as we are interested even in the
1132 separate debug info files which may have the segments offsets corrupted.
1133 PT_NOTEs from the core files are currently not parsed using BFD. */
1134 if (hdr->sh_type == SHT_NOTE)
1135 {
baea7ef1 1136 bfd_byte *contents;
718175fa 1137
baea7ef1 1138 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
0a1b45a2 1139 return false;
718175fa 1140
276da9b3
L
1141 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1142 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1143 free (contents);
1144 }
1145
8c803a2d 1146 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1147 {
1148 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1149 unsigned int i, nload;
1150
1151 /* Some ELF linkers produce binaries with all the program header
1152 p_paddr fields zero. If we have such a binary with more than
1153 one PT_LOAD header, then leave the section lma equal to vma
1154 so that we don't create sections with overlapping lma. */
1155 phdr = elf_tdata (abfd)->phdr;
1156 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1157 if (phdr->p_paddr != 0)
1158 break;
1159 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1160 ++nload;
1161 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
0a1b45a2 1162 return true;
252b5132 1163
252b5132
RH
1164 phdr = elf_tdata (abfd)->phdr;
1165 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1166 {
86b2281f
AM
1167 if (((phdr->p_type == PT_LOAD
1168 && (hdr->sh_flags & SHF_TLS) == 0)
1169 || phdr->p_type == PT_TLS)
9a83a553 1170 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1171 {
8c803a2d 1172 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1173 newsect->lma = (phdr->p_paddr
502794d4 1174 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1175 else
1176 /* We used to use the same adjustment for SEC_LOAD
1177 sections, but that doesn't work if the segment
1178 is packed with code from multiple VMAs.
1179 Instead we calculate the section LMA based on
1180 the segment LMA. It is assumed that the
1181 segment will contain sections with contiguous
1182 LMAs, even if the VMAs are not. */
1183 newsect->lma = (phdr->p_paddr
502794d4 1184 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1185
1186 /* With contiguous segments, we can't tell from file
1187 offsets whether a section with zero size should
1188 be placed at the end of one segment or the
1189 beginning of the next. Decide based on vaddr. */
1190 if (hdr->sh_addr >= phdr->p_vaddr
1191 && (hdr->sh_addr + hdr->sh_size
1192 <= phdr->p_vaddr + phdr->p_memsz))
1193 break;
252b5132
RH
1194 }
1195 }
1196 }
1197
4a114e3e
L
1198 /* Compress/decompress DWARF debug sections with names: .debug_* and
1199 .zdebug_*, after the section flags is set. */
8c803a2d 1200 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1201 && ((name[1] == 'd' && name[6] == '_')
1202 || (name[1] == 'z' && name[7] == '_')))
1203 {
1204 enum { nothing, compress, decompress } action = nothing;
151411f8 1205 int compression_header_size;
dab394de 1206 bfd_size_type uncompressed_size;
4207142d 1207 unsigned int uncompressed_align_power;
0a1b45a2 1208 bool compressed
151411f8 1209 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1210 &compression_header_size,
4207142d
MW
1211 &uncompressed_size,
1212 &uncompressed_align_power);
151411f8 1213 if (compressed)
4a114e3e
L
1214 {
1215 /* Compressed section. Check if we should decompress. */
1216 if ((abfd->flags & BFD_DECOMPRESS))
1217 action = decompress;
1218 }
151411f8
L
1219
1220 /* Compress the uncompressed section or convert from/to .zdebug*
1221 section. Check if we should compress. */
1222 if (action == nothing)
4a114e3e 1223 {
151411f8
L
1224 if (newsect->size != 0
1225 && (abfd->flags & BFD_COMPRESS)
1226 && compression_header_size >= 0
dab394de 1227 && uncompressed_size > 0
151411f8
L
1228 && (!compressed
1229 || ((compression_header_size > 0)
1230 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1231 action = compress;
151411f8 1232 else
0a1b45a2 1233 return true;
4a114e3e
L
1234 }
1235
151411f8 1236 if (action == compress)
4a114e3e 1237 {
4a114e3e
L
1238 if (!bfd_init_section_compress_status (abfd, newsect))
1239 {
4eca0228 1240 _bfd_error_handler
695344c0 1241 /* xgettext:c-format */
871b3ab2 1242 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e 1243 abfd, name);
0a1b45a2 1244 return false;
4a114e3e 1245 }
151411f8
L
1246 }
1247 else
1248 {
4a114e3e
L
1249 if (!bfd_init_section_decompress_status (abfd, newsect))
1250 {
4eca0228 1251 _bfd_error_handler
695344c0 1252 /* xgettext:c-format */
11c6a7c6
AM
1253 (_("%pB: unable to initialize decompress status"
1254 " for section %s"),
4a114e3e 1255 abfd, name);
0a1b45a2 1256 return false;
4a114e3e 1257 }
151411f8
L
1258 }
1259
f6fe1ccd 1260 if (abfd->is_linker_input)
151411f8 1261 {
f6fe1ccd
L
1262 if (name[1] == 'z'
1263 && (action == decompress
1264 || (action == compress
1265 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1266 {
f6fe1ccd
L
1267 /* Convert section name from .zdebug_* to .debug_* so
1268 that linker will consider this section as a debug
1269 section. */
1270 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8 1271 if (new_name == NULL)
0a1b45a2 1272 return false;
fd361982 1273 bfd_rename_section (newsect, new_name);
151411f8 1274 }
4a114e3e 1275 }
f6fe1ccd
L
1276 else
1277 /* For objdump, don't rename the section. For objcopy, delay
1278 section rename to elf_fake_sections. */
1279 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1280 }
1281
cc5277b1
ML
1282 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1283 section. */
3f3328b8 1284 if (startswith (name, ".gnu.lto_.lto."))
cc5277b1
ML
1285 {
1286 struct lto_section lsection;
1287 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1288 sizeof (struct lto_section)))
1289 abfd->lto_slim_object = lsection.slim_object;
1290 }
1291
0a1b45a2 1292 return true;
252b5132
RH
1293}
1294
84865015
NC
1295const char *const bfd_elf_section_type_names[] =
1296{
252b5132
RH
1297 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1298 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1299 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1300};
1301
1049f94e 1302/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1303 output, and the reloc is against an external symbol, and nothing
1304 has given us any additional addend, the resulting reloc will also
1305 be against the same symbol. In such a case, we don't want to
1306 change anything about the way the reloc is handled, since it will
1307 all be done at final link time. Rather than put special case code
1308 into bfd_perform_relocation, all the reloc types use this howto
2dfa8341 1309 function, or should call this function for relocatable output. */
252b5132 1310
252b5132 1311bfd_reloc_status_type
217aa764
AM
1312bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1313 arelent *reloc_entry,
1314 asymbol *symbol,
1315 void *data ATTRIBUTE_UNUSED,
1316 asection *input_section,
1317 bfd *output_bfd,
1318 char **error_message ATTRIBUTE_UNUSED)
1319{
1320 if (output_bfd != NULL
252b5132
RH
1321 && (symbol->flags & BSF_SECTION_SYM) == 0
1322 && (! reloc_entry->howto->partial_inplace
1323 || reloc_entry->addend == 0))
1324 {
1325 reloc_entry->address += input_section->output_offset;
1326 return bfd_reloc_ok;
1327 }
1328
2dfa8341
AM
1329 /* In some cases the relocation should be treated as output section
1330 relative, as when linking ELF DWARF into PE COFF. Many ELF
1331 targets lack section relative relocations and instead use
1332 ordinary absolute relocations for references between DWARF
1333 sections. That is arguably a bug in those targets but it happens
1334 to work for the usual case of linking to non-loaded ELF debug
1335 sections with VMAs forced to zero. PE COFF on the other hand
1336 doesn't allow a section VMA of zero. */
1337 if (output_bfd == NULL
1338 && !reloc_entry->howto->pc_relative
1339 && (symbol->section->flags & SEC_DEBUGGING) != 0
1340 && (input_section->flags & SEC_DEBUGGING) != 0)
1341 reloc_entry->addend -= symbol->section->output_section->vma;
1342
252b5132
RH
1343 return bfd_reloc_continue;
1344}
1345\f
84865015
NC
1346/* Returns TRUE if section A matches section B.
1347 Names, addresses and links may be different, but everything else
1348 should be the same. */
1349
0a1b45a2 1350static bool
5522f910
NC
1351section_match (const Elf_Internal_Shdr * a,
1352 const Elf_Internal_Shdr * b)
84865015 1353{
ac85e67c
AM
1354 if (a->sh_type != b->sh_type
1355 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1356 || a->sh_addralign != b->sh_addralign
1357 || a->sh_entsize != b->sh_entsize)
0a1b45a2 1358 return false;
ac85e67c
AM
1359 if (a->sh_type == SHT_SYMTAB
1360 || a->sh_type == SHT_STRTAB)
0a1b45a2 1361 return true;
ac85e67c 1362 return a->sh_size == b->sh_size;
84865015
NC
1363}
1364
1365/* Find a section in OBFD that has the same characteristics
1366 as IHEADER. Return the index of this section or SHN_UNDEF if
1367 none can be found. Check's section HINT first, as this is likely
1368 to be the correct section. */
1369
1370static unsigned int
5cc4ca83
ST
1371find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1372 const unsigned int hint)
84865015
NC
1373{
1374 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1375 unsigned int i;
1376
a55c9876
NC
1377 BFD_ASSERT (iheader != NULL);
1378
1379 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1380 if (hint < elf_numsections (obfd)
1381 && oheaders[hint] != NULL
a55c9876 1382 && section_match (oheaders[hint], iheader))
84865015
NC
1383 return hint;
1384
1385 for (i = 1; i < elf_numsections (obfd); i++)
1386 {
1387 Elf_Internal_Shdr * oheader = oheaders[i];
1388
a55c9876
NC
1389 if (oheader == NULL)
1390 continue;
84865015
NC
1391 if (section_match (oheader, iheader))
1392 /* FIXME: Do we care if there is a potential for
1393 multiple matches ? */
1394 return i;
1395 }
1396
1397 return SHN_UNDEF;
1398}
1399
5522f910
NC
1400/* PR 19938: Attempt to set the ELF section header fields of an OS or
1401 Processor specific section, based upon a matching input section.
1402 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1403
0a1b45a2 1404static bool
5522f910
NC
1405copy_special_section_fields (const bfd *ibfd,
1406 bfd *obfd,
1407 const Elf_Internal_Shdr *iheader,
1408 Elf_Internal_Shdr *oheader,
1409 const unsigned int secnum)
1410{
1411 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11c6a7c6
AM
1412 const Elf_Internal_Shdr **iheaders
1413 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
0a1b45a2 1414 bool changed = false;
5522f910
NC
1415 unsigned int sh_link;
1416
1417 if (oheader->sh_type == SHT_NOBITS)
1418 {
1419 /* This is a feature for objcopy --only-keep-debug:
1420 When a section's type is changed to NOBITS, we preserve
1421 the sh_link and sh_info fields so that they can be
1422 matched up with the original.
1423
1424 Note: Strictly speaking these assignments are wrong.
1425 The sh_link and sh_info fields should point to the
1426 relevent sections in the output BFD, which may not be in
1427 the same location as they were in the input BFD. But
1428 the whole point of this action is to preserve the
1429 original values of the sh_link and sh_info fields, so
1430 that they can be matched up with the section headers in
1431 the original file. So strictly speaking we may be
1432 creating an invalid ELF file, but it is only for a file
1433 that just contains debug info and only for sections
1434 without any contents. */
1435 if (oheader->sh_link == 0)
1436 oheader->sh_link = iheader->sh_link;
1437 if (oheader->sh_info == 0)
1438 oheader->sh_info = iheader->sh_info;
0a1b45a2 1439 return true;
5522f910
NC
1440 }
1441
1442 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1443 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1444 iheader, oheader))
0a1b45a2 1445 return true;
5522f910
NC
1446
1447 /* We have an iheader which might match oheader, and which has non-zero
1448 sh_info and/or sh_link fields. Attempt to follow those links and find
1449 the section in the output bfd which corresponds to the linked section
1450 in the input bfd. */
1451 if (iheader->sh_link != SHN_UNDEF)
1452 {
4f3ca05b
NC
1453 /* See PR 20931 for a reproducer. */
1454 if (iheader->sh_link >= elf_numsections (ibfd))
1455 {
76cfced5 1456 _bfd_error_handler
4f3ca05b 1457 /* xgettext:c-format */
9793eb77 1458 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b 1459 ibfd, iheader->sh_link, secnum);
0a1b45a2 1460 return false;
4f3ca05b
NC
1461 }
1462
5522f910
NC
1463 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1464 if (sh_link != SHN_UNDEF)
1465 {
1466 oheader->sh_link = sh_link;
0a1b45a2 1467 changed = true;
5522f910
NC
1468 }
1469 else
1470 /* FIXME: Should we install iheader->sh_link
1471 if we could not find a match ? */
76cfced5 1472 _bfd_error_handler
695344c0 1473 /* xgettext:c-format */
9793eb77 1474 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1475 }
1476
1477 if (iheader->sh_info)
1478 {
1479 /* The sh_info field can hold arbitrary information, but if the
1480 SHF_LINK_INFO flag is set then it should be interpreted as a
1481 section index. */
1482 if (iheader->sh_flags & SHF_INFO_LINK)
1483 {
1484 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1485 iheader->sh_info);
1486 if (sh_link != SHN_UNDEF)
1487 oheader->sh_flags |= SHF_INFO_LINK;
1488 }
1489 else
1490 /* No idea what it means - just copy it. */
1491 sh_link = iheader->sh_info;
1492
1493 if (sh_link != SHN_UNDEF)
1494 {
1495 oheader->sh_info = sh_link;
0a1b45a2 1496 changed = true;
5522f910
NC
1497 }
1498 else
76cfced5 1499 _bfd_error_handler
695344c0 1500 /* xgettext:c-format */
9793eb77 1501 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1502 }
1503
1504 return changed;
1505}
07d6d2b8 1506
0ac4564e
L
1507/* Copy the program header and other data from one object module to
1508 another. */
252b5132 1509
0a1b45a2 1510bool
217aa764 1511_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1512{
11c6a7c6
AM
1513 const Elf_Internal_Shdr **iheaders
1514 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
5522f910
NC
1515 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1516 const struct elf_backend_data *bed;
84865015
NC
1517 unsigned int i;
1518
2d502050 1519 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1520 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 1521 return true;
2d502050 1522
57b828ef
L
1523 if (!elf_flags_init (obfd))
1524 {
1525 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
0a1b45a2 1526 elf_flags_init (obfd) = true;
57b828ef 1527 }
2d502050 1528
0ac4564e 1529 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1530
1531 /* Also copy the EI_OSABI field. */
1532 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1533 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1534
5522f910
NC
1535 /* If set, copy the EI_ABIVERSION field. */
1536 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1537 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1538 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1539
104d59d1
JM
1540 /* Copy object attributes. */
1541 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1542
84865015 1543 if (iheaders == NULL || oheaders == NULL)
0a1b45a2 1544 return true;
63b9bbb7 1545
5522f910
NC
1546 bed = get_elf_backend_data (obfd);
1547
1548 /* Possibly copy other fields in the section header. */
84865015 1549 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1550 {
84865015
NC
1551 unsigned int j;
1552 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1553
5522f910
NC
1554 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1555 because of a special case need for generating separate debug info
1556 files. See below for more details. */
84865015
NC
1557 if (oheader == NULL
1558 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1559 && oheader->sh_type < SHT_LOOS))
1560 continue;
1561
1562 /* Ignore empty sections, and sections whose
1563 fields have already been initialised. */
1564 if (oheader->sh_size == 0
84865015
NC
1565 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1566 continue;
63b9bbb7 1567
84865015 1568 /* Scan for the matching section in the input bfd.
11c6a7c6
AM
1569 First we try for a direct mapping between the input and
1570 output sections. */
5522f910
NC
1571 for (j = 1; j < elf_numsections (ibfd); j++)
1572 {
1573 const Elf_Internal_Shdr * iheader = iheaders[j];
1574
1575 if (iheader == NULL)
1576 continue;
1577
1578 if (oheader->bfd_section != NULL
1579 && iheader->bfd_section != NULL
1580 && iheader->bfd_section->output_section != NULL
1581 && iheader->bfd_section->output_section == oheader->bfd_section)
1582 {
11c6a7c6
AM
1583 /* We have found a connection from the input section to
1584 the output section. Attempt to copy the header fields.
1585 If this fails then do not try any further sections -
1586 there should only be a one-to-one mapping between
1587 input and output. */
1588 if (!copy_special_section_fields (ibfd, obfd,
1589 iheader, oheader, i))
5522f910
NC
1590 j = elf_numsections (ibfd);
1591 break;
1592 }
1593 }
1594
1595 if (j < elf_numsections (ibfd))
1596 continue;
1597
1598 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1599 Unfortunately we cannot compare names as the output string table
1600 is empty, so instead we check size, address and type. */
1601 for (j = 1; j < elf_numsections (ibfd); j++)
1602 {
5522f910 1603 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1604
5522f910
NC
1605 if (iheader == NULL)
1606 continue;
1607
1608 /* Try matching fields in the input section's header.
1609 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1610 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1611 input type. */
1612 if ((oheader->sh_type == SHT_NOBITS
1613 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1614 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1615 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1616 && iheader->sh_addralign == oheader->sh_addralign
1617 && iheader->sh_entsize == oheader->sh_entsize
1618 && iheader->sh_size == oheader->sh_size
1619 && iheader->sh_addr == oheader->sh_addr
1620 && (iheader->sh_info != oheader->sh_info
1621 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1622 {
5522f910
NC
1623 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1624 break;
63b9bbb7
NC
1625 }
1626 }
5522f910
NC
1627
1628 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1629 {
1630 /* Final attempt. Call the backend copy function
1631 with a NULL input section. */
a859124d
AM
1632 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1633 NULL, oheader);
5522f910 1634 }
63b9bbb7
NC
1635 }
1636
0a1b45a2 1637 return true;
2d502050
L
1638}
1639
cedc298e
L
1640static const char *
1641get_segment_type (unsigned int p_type)
1642{
1643 const char *pt;
1644 switch (p_type)
1645 {
1646 case PT_NULL: pt = "NULL"; break;
1647 case PT_LOAD: pt = "LOAD"; break;
1648 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1649 case PT_INTERP: pt = "INTERP"; break;
1650 case PT_NOTE: pt = "NOTE"; break;
1651 case PT_SHLIB: pt = "SHLIB"; break;
1652 case PT_PHDR: pt = "PHDR"; break;
1653 case PT_TLS: pt = "TLS"; break;
1654 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1655 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1656 case PT_GNU_RELRO: pt = "RELRO"; break;
1657 default: pt = NULL; break;
1658 }
1659 return pt;
1660}
1661
f0b79d91
L
1662/* Print out the program headers. */
1663
0a1b45a2 1664bool
217aa764 1665_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1666{
a50b1753 1667 FILE *f = (FILE *) farg;
252b5132
RH
1668 Elf_Internal_Phdr *p;
1669 asection *s;
1670 bfd_byte *dynbuf = NULL;
1671
1672 p = elf_tdata (abfd)->phdr;
1673 if (p != NULL)
1674 {
1675 unsigned int i, c;
1676
1677 fprintf (f, _("\nProgram Header:\n"));
1678 c = elf_elfheader (abfd)->e_phnum;
1679 for (i = 0; i < c; i++, p++)
1680 {
cedc298e 1681 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1682 char buf[20];
1683
cedc298e 1684 if (pt == NULL)
252b5132 1685 {
cedc298e
L
1686 sprintf (buf, "0x%lx", p->p_type);
1687 pt = buf;
252b5132 1688 }
dc810e39 1689 fprintf (f, "%8s off 0x", pt);
60b89a18 1690 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1691 fprintf (f, " vaddr 0x");
60b89a18 1692 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1693 fprintf (f, " paddr 0x");
60b89a18 1694 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1695 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1696 fprintf (f, " filesz 0x");
60b89a18 1697 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1698 fprintf (f, " memsz 0x");
60b89a18 1699 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1700 fprintf (f, " flags %c%c%c",
1701 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1702 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1703 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1704 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1705 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1706 fprintf (f, "\n");
1707 }
1708 }
1709
1710 s = bfd_get_section_by_name (abfd, ".dynamic");
1711 if (s != NULL)
1712 {
cb33740c 1713 unsigned int elfsec;
dc810e39 1714 unsigned long shlink;
252b5132
RH
1715 bfd_byte *extdyn, *extdynend;
1716 size_t extdynsize;
217aa764 1717 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1718
1719 fprintf (f, _("\nDynamic Section:\n"));
1720
eea6121a 1721 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1722 goto error_return;
1723
1724 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1725 if (elfsec == SHN_BAD)
252b5132 1726 goto error_return;
dc810e39 1727 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1728
1729 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1730 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1731
37c59664
AM
1732 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1733 (size_t) (extdynend - extdyn) >= extdynsize;
1734 extdyn += extdynsize)
252b5132
RH
1735 {
1736 Elf_Internal_Dyn dyn;
ad9563d6 1737 const char *name = "";
252b5132 1738 char ab[20];
0a1b45a2 1739 bool stringp;
ad9563d6 1740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1741
217aa764 1742 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1743
1744 if (dyn.d_tag == DT_NULL)
1745 break;
1746
0a1b45a2 1747 stringp = false;
252b5132
RH
1748 switch (dyn.d_tag)
1749 {
1750 default:
ad9563d6
CM
1751 if (bed->elf_backend_get_target_dtag)
1752 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1753
1754 if (!strcmp (name, ""))
1755 {
b8281767 1756 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
ad9563d6
CM
1757 name = ab;
1758 }
252b5132
RH
1759 break;
1760
0a1b45a2 1761 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
252b5132
RH
1762 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1763 case DT_PLTGOT: name = "PLTGOT"; break;
1764 case DT_HASH: name = "HASH"; break;
1765 case DT_STRTAB: name = "STRTAB"; break;
1766 case DT_SYMTAB: name = "SYMTAB"; break;
1767 case DT_RELA: name = "RELA"; break;
1768 case DT_RELASZ: name = "RELASZ"; break;
1769 case DT_RELAENT: name = "RELAENT"; break;
1770 case DT_STRSZ: name = "STRSZ"; break;
1771 case DT_SYMENT: name = "SYMENT"; break;
1772 case DT_INIT: name = "INIT"; break;
1773 case DT_FINI: name = "FINI"; break;
0a1b45a2
AM
1774 case DT_SONAME: name = "SONAME"; stringp = true; break;
1775 case DT_RPATH: name = "RPATH"; stringp = true; break;
252b5132
RH
1776 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1777 case DT_REL: name = "REL"; break;
1778 case DT_RELSZ: name = "RELSZ"; break;
1779 case DT_RELENT: name = "RELENT"; break;
dd207c13
FS
1780 case DT_RELR: name = "RELR"; break;
1781 case DT_RELRSZ: name = "RELRSZ"; break;
1782 case DT_RELRENT: name = "RELRENT"; break;
252b5132
RH
1783 case DT_PLTREL: name = "PLTREL"; break;
1784 case DT_DEBUG: name = "DEBUG"; break;
1785 case DT_TEXTREL: name = "TEXTREL"; break;
1786 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1787 case DT_BIND_NOW: name = "BIND_NOW"; break;
1788 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1789 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1790 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1791 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
0a1b45a2 1792 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
94558834
L
1793 case DT_FLAGS: name = "FLAGS"; break;
1794 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1795 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1796 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1797 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1798 case DT_MOVEENT: name = "MOVEENT"; break;
1799 case DT_MOVESZ: name = "MOVESZ"; break;
1800 case DT_FEATURE: name = "FEATURE"; break;
1801 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1802 case DT_SYMINSZ: name = "SYMINSZ"; break;
1803 case DT_SYMINENT: name = "SYMINENT"; break;
0a1b45a2
AM
1804 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1805 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1806 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1807 case DT_PLTPAD: name = "PLTPAD"; break;
1808 case DT_MOVETAB: name = "MOVETAB"; break;
1809 case DT_SYMINFO: name = "SYMINFO"; break;
1810 case DT_RELACOUNT: name = "RELACOUNT"; break;
1811 case DT_RELCOUNT: name = "RELCOUNT"; break;
1812 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1813 case DT_VERSYM: name = "VERSYM"; break;
1814 case DT_VERDEF: name = "VERDEF"; break;
1815 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1816 case DT_VERNEED: name = "VERNEED"; break;
1817 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
0a1b45a2 1818 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
94558834 1819 case DT_USED: name = "USED"; break;
0a1b45a2 1820 case DT_FILTER: name = "FILTER"; stringp = true; break;
fdc90cb4 1821 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1822 }
1823
ad9563d6 1824 fprintf (f, " %-20s ", name);
252b5132 1825 if (! stringp)
a1f3c56e
AN
1826 {
1827 fprintf (f, "0x");
1828 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1829 }
252b5132
RH
1830 else
1831 {
1832 const char *string;
dc810e39 1833 unsigned int tagv = dyn.d_un.d_val;
252b5132 1834
dc810e39 1835 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1836 if (string == NULL)
1837 goto error_return;
1838 fprintf (f, "%s", string);
1839 }
1840 fprintf (f, "\n");
1841 }
1842
1843 free (dynbuf);
1844 dynbuf = NULL;
1845 }
1846
1847 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1848 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1849 {
0a1b45a2
AM
1850 if (! _bfd_elf_slurp_version_tables (abfd, false))
1851 return false;
252b5132
RH
1852 }
1853
1854 if (elf_dynverdef (abfd) != 0)
1855 {
1856 Elf_Internal_Verdef *t;
1857
1858 fprintf (f, _("\nVersion definitions:\n"));
1859 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1860 {
1861 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1862 t->vd_flags, t->vd_hash,
1863 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1864 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1865 {
1866 Elf_Internal_Verdaux *a;
1867
1868 fprintf (f, "\t");
1869 for (a = t->vd_auxptr->vda_nextptr;
1870 a != NULL;
1871 a = a->vda_nextptr)
d0fb9a8d
JJ
1872 fprintf (f, "%s ",
1873 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1874 fprintf (f, "\n");
1875 }
1876 }
1877 }
1878
1879 if (elf_dynverref (abfd) != 0)
1880 {
1881 Elf_Internal_Verneed *t;
1882
1883 fprintf (f, _("\nVersion References:\n"));
1884 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1885 {
1886 Elf_Internal_Vernaux *a;
1887
d0fb9a8d
JJ
1888 fprintf (f, _(" required from %s:\n"),
1889 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1890 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1891 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1892 a->vna_flags, a->vna_other,
1893 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1894 }
1895 }
1896
0a1b45a2 1897 return true;
252b5132
RH
1898
1899 error_return:
c9594989 1900 free (dynbuf);
0a1b45a2 1901 return false;
252b5132
RH
1902}
1903
7e6e972f
L
1904/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1905 and return symbol version for symbol version itself. */
bb4d2ac2
L
1906
1907const char *
1081065c 1908_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
0a1b45a2
AM
1909 bool base_p,
1910 bool *hidden)
bb4d2ac2
L
1911{
1912 const char *version_string = NULL;
1913 if (elf_dynversym (abfd) != 0
1914 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1915 {
1916 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1917
1918 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1919 vernum &= VERSYM_VERSION;
1920
1921 if (vernum == 0)
1922 version_string = "";
1f6f5dba
L
1923 else if (vernum == 1
1924 && (vernum > elf_tdata (abfd)->cverdefs
1925 || (elf_tdata (abfd)->verdef[0].vd_flags
1926 == VER_FLG_BASE)))
7e6e972f 1927 version_string = base_p ? "Base" : "";
bb4d2ac2 1928 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1929 {
1930 const char *nodename
1931 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1932 version_string = "";
1933 if (base_p
1934 || nodename == NULL
1935 || symbol->name == NULL
1936 || strcmp (symbol->name, nodename) != 0)
1937 version_string = nodename;
7e6e972f 1938 }
bb4d2ac2
L
1939 else
1940 {
1941 Elf_Internal_Verneed *t;
1942
7a815dd5 1943 version_string = _("<corrupt>");
bb4d2ac2
L
1944 for (t = elf_tdata (abfd)->verref;
1945 t != NULL;
1946 t = t->vn_nextref)
1947 {
1948 Elf_Internal_Vernaux *a;
1949
1950 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1951 {
1952 if (a->vna_other == vernum)
1953 {
f897689c 1954 *hidden = true;
bb4d2ac2
L
1955 version_string = a->vna_nodename;
1956 break;
1957 }
1958 }
1959 }
1960 }
1961 }
1962 return version_string;
1963}
1964
252b5132
RH
1965/* Display ELF-specific fields of a symbol. */
1966
1967void
217aa764
AM
1968bfd_elf_print_symbol (bfd *abfd,
1969 void *filep,
1970 asymbol *symbol,
1971 bfd_print_symbol_type how)
252b5132 1972{
a50b1753 1973 FILE *file = (FILE *) filep;
252b5132
RH
1974 switch (how)
1975 {
1976 case bfd_print_symbol_name:
1977 fprintf (file, "%s", symbol->name);
1978 break;
1979 case bfd_print_symbol_more:
1980 fprintf (file, "elf ");
60b89a18 1981 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1982 fprintf (file, " %x", symbol->flags);
252b5132
RH
1983 break;
1984 case bfd_print_symbol_all:
1985 {
4e8a9624
AM
1986 const char *section_name;
1987 const char *name = NULL;
9c5bfbb7 1988 const struct elf_backend_data *bed;
7a13edea 1989 unsigned char st_other;
dbb410c3 1990 bfd_vma val;
bb4d2ac2 1991 const char *version_string;
0a1b45a2 1992 bool hidden;
c044fabd 1993
252b5132 1994 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1995
1996 bed = get_elf_backend_data (abfd);
1997 if (bed->elf_backend_print_symbol_all)
c044fabd 1998 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1999
2000 if (name == NULL)
2001 {
7ee38065 2002 name = symbol->name;
217aa764 2003 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
2004 }
2005
252b5132
RH
2006 fprintf (file, " %s\t", section_name);
2007 /* Print the "other" value for a symbol. For common symbols,
2008 we've already printed the size; now print the alignment.
2009 For other symbols, we have no specified alignment, and
2010 we've printed the address; now print the size. */
dcf6c779 2011 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
2012 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2013 else
2014 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2015 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
2016
2017 /* If we have version information, print it. */
60bb06bc
L
2018 version_string = _bfd_elf_get_symbol_version_string (abfd,
2019 symbol,
0a1b45a2 2020 true,
60bb06bc 2021 &hidden);
bb4d2ac2 2022 if (version_string)
252b5132 2023 {
bb4d2ac2 2024 if (!hidden)
252b5132
RH
2025 fprintf (file, " %-11s", version_string);
2026 else
2027 {
2028 int i;
2029
2030 fprintf (file, " (%s)", version_string);
2031 for (i = 10 - strlen (version_string); i > 0; --i)
2032 putc (' ', file);
2033 }
2034 }
2035
2036 /* If the st_other field is not zero, print it. */
7a13edea 2037 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2038
7a13edea
NC
2039 switch (st_other)
2040 {
2041 case 0: break;
2042 case STV_INTERNAL: fprintf (file, " .internal"); break;
2043 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2044 case STV_PROTECTED: fprintf (file, " .protected"); break;
2045 default:
2046 /* Some other non-defined flags are also present, so print
2047 everything hex. */
2048 fprintf (file, " 0x%02x", (unsigned int) st_other);
2049 }
252b5132 2050
587ff49e 2051 fprintf (file, " %s", name);
252b5132
RH
2052 }
2053 break;
2054 }
2055}
252b5132
RH
2056\f
2057/* ELF .o/exec file reading */
2058
c044fabd 2059/* Create a new bfd section from an ELF section header. */
252b5132 2060
0a1b45a2 2061bool
217aa764 2062bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2063{
4fbb74a6
AM
2064 Elf_Internal_Shdr *hdr;
2065 Elf_Internal_Ehdr *ehdr;
2066 const struct elf_backend_data *bed;
90937f86 2067 const char *name;
0a1b45a2 2068 bool ret = true;
252b5132 2069
4fbb74a6 2070 if (shindex >= elf_numsections (abfd))
0a1b45a2 2071 return false;
4fbb74a6 2072
a86c6c19
AM
2073 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2074 sh_link or sh_info. Detect this here, by refusing to load a
2075 section that we are already in the process of loading. */
2076 if (elf_tdata (abfd)->being_created[shindex])
bf67003b 2077 {
a86c6c19
AM
2078 _bfd_error_handler
2079 (_("%pB: warning: loop in section dependencies detected"), abfd);
0a1b45a2 2080 return false;
bf67003b 2081 }
0a1b45a2 2082 elf_tdata (abfd)->being_created[shindex] = true;
bf67003b 2083
4fbb74a6
AM
2084 hdr = elf_elfsections (abfd)[shindex];
2085 ehdr = elf_elfheader (abfd);
2086 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2087 hdr->sh_name);
933d961a 2088 if (name == NULL)
bf67003b 2089 goto fail;
252b5132 2090
4fbb74a6 2091 bed = get_elf_backend_data (abfd);
252b5132
RH
2092 switch (hdr->sh_type)
2093 {
2094 case SHT_NULL:
2095 /* Inactive section. Throw it away. */
bf67003b 2096 goto success;
252b5132 2097
bf67003b
NC
2098 case SHT_PROGBITS: /* Normal section with contents. */
2099 case SHT_NOBITS: /* .bss section. */
2100 case SHT_HASH: /* .hash section. */
2101 case SHT_NOTE: /* .note section. */
25e27870
L
2102 case SHT_INIT_ARRAY: /* .init_array section. */
2103 case SHT_FINI_ARRAY: /* .fini_array section. */
2104 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2105 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2106 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2107 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2108 goto success;
252b5132 2109
797fc050 2110 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2111 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2112 goto fail;
2113
cfcac11d
NC
2114 if (hdr->sh_link > elf_numsections (abfd))
2115 {
11c6a7c6
AM
2116 /* PR 10478: Accept Solaris binaries with a sh_link field
2117 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
cfcac11d
NC
2118 switch (bfd_get_arch (abfd))
2119 {
caa83f8b 2120 case bfd_arch_i386:
cfcac11d 2121 case bfd_arch_sparc:
11c6a7c6
AM
2122 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2123 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
cfcac11d
NC
2124 break;
2125 /* Otherwise fall through. */
2126 default:
bf67003b 2127 goto fail;
cfcac11d
NC
2128 }
2129 }
2130 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2131 goto fail;
cfcac11d 2132 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2133 {
2134 Elf_Internal_Shdr *dynsymhdr;
2135
2136 /* The shared libraries distributed with hpux11 have a bogus
2137 sh_link field for the ".dynamic" section. Find the
2138 string table for the ".dynsym" section instead. */
2139 if (elf_dynsymtab (abfd) != 0)
2140 {
2141 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2142 hdr->sh_link = dynsymhdr->sh_link;
2143 }
2144 else
2145 {
2146 unsigned int i, num_sec;
2147
2148 num_sec = elf_numsections (abfd);
2149 for (i = 1; i < num_sec; i++)
2150 {
2151 dynsymhdr = elf_elfsections (abfd)[i];
2152 if (dynsymhdr->sh_type == SHT_DYNSYM)
2153 {
2154 hdr->sh_link = dynsymhdr->sh_link;
2155 break;
2156 }
2157 }
2158 }
2159 }
bf67003b 2160 goto success;
797fc050 2161
bf67003b 2162 case SHT_SYMTAB: /* A symbol table. */
252b5132 2163 if (elf_onesymtab (abfd) == shindex)
bf67003b 2164 goto success;
252b5132 2165
a50b2160 2166 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2167 goto fail;
2168
3337c1e5 2169 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2170 {
2171 if (hdr->sh_size != 0)
bf67003b 2172 goto fail;
eee3b786
AM
2173 /* Some assemblers erroneously set sh_info to one with a
2174 zero sh_size. ld sees this as a global symbol count
2175 of (unsigned) -1. Fix it here. */
2176 hdr->sh_info = 0;
bf67003b 2177 goto success;
eee3b786 2178 }
bf67003b 2179
16ad13ec
NC
2180 /* PR 18854: A binary might contain more than one symbol table.
2181 Unusual, but possible. Warn, but continue. */
2182 if (elf_onesymtab (abfd) != 0)
2183 {
4eca0228 2184 _bfd_error_handler
695344c0 2185 /* xgettext:c-format */
871b3ab2 2186 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2187 " - ignoring the table in section %u"),
16ad13ec
NC
2188 abfd, shindex);
2189 goto success;
2190 }
252b5132 2191 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2192 elf_symtab_hdr (abfd) = *hdr;
2193 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2194 abfd->flags |= HAS_SYMS;
2195
2196 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2197 SHF_ALLOC is set, and this is a shared object, then we also
2198 treat this section as a BFD section. We can not base the
2199 decision purely on SHF_ALLOC, because that flag is sometimes
2200 set in a relocatable object file, which would confuse the
2201 linker. */
252b5132
RH
2202 if ((hdr->sh_flags & SHF_ALLOC) != 0
2203 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2204 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2205 shindex))
bf67003b 2206 goto fail;
252b5132 2207
1b3a8575
AM
2208 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2209 can't read symbols without that section loaded as well. It
2210 is most likely specified by the next section header. */
6a40cf0c
NC
2211 {
2212 elf_section_list * entry;
2213 unsigned int i, num_sec;
1b3a8575 2214
11c6a7c6 2215 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
6a40cf0c
NC
2216 if (entry->hdr.sh_link == shindex)
2217 goto success;
2218
2219 num_sec = elf_numsections (abfd);
2220 for (i = shindex + 1; i < num_sec; i++)
2221 {
2222 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2223
2224 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2225 && hdr2->sh_link == shindex)
2226 break;
2227 }
2228
2229 if (i == num_sec)
2230 for (i = 1; i < shindex; i++)
1b3a8575
AM
2231 {
2232 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2233
1b3a8575
AM
2234 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2235 && hdr2->sh_link == shindex)
2236 break;
2237 }
6a40cf0c
NC
2238
2239 if (i != shindex)
2240 ret = bfd_section_from_shdr (abfd, i);
11c6a7c6
AM
2241 /* else FIXME: we have failed to find the symbol table.
2242 Should we issue an error? */
6a40cf0c
NC
2243 goto success;
2244 }
252b5132 2245
bf67003b 2246 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2247 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2248 goto success;
252b5132 2249
a50b2160 2250 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2251 goto fail;
2252
eee3b786
AM
2253 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2254 {
2255 if (hdr->sh_size != 0)
bf67003b
NC
2256 goto fail;
2257
eee3b786
AM
2258 /* Some linkers erroneously set sh_info to one with a
2259 zero sh_size. ld sees this as a global symbol count
2260 of (unsigned) -1. Fix it here. */
2261 hdr->sh_info = 0;
bf67003b 2262 goto success;
eee3b786 2263 }
bf67003b 2264
16ad13ec
NC
2265 /* PR 18854: A binary might contain more than one dynamic symbol table.
2266 Unusual, but possible. Warn, but continue. */
2267 if (elf_dynsymtab (abfd) != 0)
2268 {
4eca0228 2269 _bfd_error_handler
695344c0 2270 /* xgettext:c-format */
871b3ab2 2271 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2272 " - ignoring the table in section %u"),
16ad13ec
NC
2273 abfd, shindex);
2274 goto success;
2275 }
252b5132
RH
2276 elf_dynsymtab (abfd) = shindex;
2277 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2278 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2279 abfd->flags |= HAS_SYMS;
2280
2281 /* Besides being a symbol table, we also treat this as a regular
2282 section, so that objcopy can handle it. */
bf67003b
NC
2283 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2284 goto success;
252b5132 2285
11c6a7c6 2286 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2287 {
2288 elf_section_list * entry;
9ad5cbcf 2289
11c6a7c6 2290 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
6a40cf0c
NC
2291 if (entry->ndx == shindex)
2292 goto success;
07d6d2b8 2293
7a6e0d89 2294 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2295 if (entry == NULL)
2296 goto fail;
2297 entry->ndx = shindex;
2298 entry->hdr = * hdr;
2299 entry->next = elf_symtab_shndx_list (abfd);
2300 elf_symtab_shndx_list (abfd) = entry;
2301 elf_elfsections (abfd)[shindex] = & entry->hdr;
2302 goto success;
2303 }
9ad5cbcf 2304
bf67003b 2305 case SHT_STRTAB: /* A string table. */
252b5132 2306 if (hdr->bfd_section != NULL)
bf67003b
NC
2307 goto success;
2308
252b5132
RH
2309 if (ehdr->e_shstrndx == shindex)
2310 {
2311 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2312 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2313 goto success;
252b5132 2314 }
bf67003b 2315
1b3a8575
AM
2316 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2317 {
2318 symtab_strtab:
2319 elf_tdata (abfd)->strtab_hdr = *hdr;
2320 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2321 goto success;
1b3a8575 2322 }
bf67003b 2323
1b3a8575
AM
2324 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2325 {
2326 dynsymtab_strtab:
2327 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2328 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2329 elf_elfsections (abfd)[shindex] = hdr;
2330 /* We also treat this as a regular section, so that objcopy
2331 can handle it. */
bf67003b
NC
2332 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2333 shindex);
2334 goto success;
1b3a8575 2335 }
252b5132 2336
1b3a8575
AM
2337 /* If the string table isn't one of the above, then treat it as a
2338 regular section. We need to scan all the headers to be sure,
2339 just in case this strtab section appeared before the above. */
2340 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2341 {
2342 unsigned int i, num_sec;
252b5132 2343
1b3a8575
AM
2344 num_sec = elf_numsections (abfd);
2345 for (i = 1; i < num_sec; i++)
2346 {
2347 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2348 if (hdr2->sh_link == shindex)
2349 {
933d961a
JJ
2350 /* Prevent endless recursion on broken objects. */
2351 if (i == shindex)
bf67003b 2352 goto fail;
1b3a8575 2353 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2354 goto fail;
1b3a8575
AM
2355 if (elf_onesymtab (abfd) == i)
2356 goto symtab_strtab;
2357 if (elf_dynsymtab (abfd) == i)
2358 goto dynsymtab_strtab;
2359 }
2360 }
2361 }
bf67003b
NC
2362 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2363 goto success;
252b5132
RH
2364
2365 case SHT_REL:
2366 case SHT_RELA:
a619b587 2367 case SHT_RELR:
252b5132
RH
2368 /* *These* do a lot of work -- but build no sections! */
2369 {
2370 asection *target_sect;
d4730f92 2371 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2372 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2373 struct bfd_elf_section_data *esdt;
a619b587 2374 bfd_size_type size;
252b5132 2375
a619b587
FS
2376 if (hdr->sh_type == SHT_REL)
2377 size = bed->s->sizeof_rel;
2378 else if (hdr->sh_type == SHT_RELA)
2379 size = bed->s->sizeof_rela;
2380 else
2381 size = bed->s->arch_size / 8;
2382 if (hdr->sh_entsize != size)
bf67003b 2383 goto fail;
a50b2160 2384
03ae5f59 2385 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2386 if (hdr->sh_link >= num_sec)
03ae5f59 2387 {
4eca0228 2388 _bfd_error_handler
695344c0 2389 /* xgettext:c-format */
871b3ab2 2390 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2391 abfd, hdr->sh_link, name, shindex);
11c6a7c6 2392 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b 2393 goto success;
03ae5f59
ILT
2394 }
2395
252b5132 2396 /* Get the symbol table. */
1b3a8575
AM
2397 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2398 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2399 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2400 goto fail;
252b5132 2401
a4bcd733
AM
2402 /* If this is an alloc section in an executable or shared
2403 library, or the reloc section does not use the main symbol
2404 table we don't treat it as a reloc section. BFD can't
2405 adequately represent such a section, so at least for now,
2406 we don't try. We just present it as a normal section. We
2407 also can't use it as a reloc section if it points to the
2408 null section, an invalid section, another reloc section, or
2409 its sh_link points to the null section. */
2410 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2411 && (hdr->sh_flags & SHF_ALLOC) != 0)
37c49d0d 2412 || hdr->sh_type == SHT_RELR
83b89087 2413 || hdr->sh_link == SHN_UNDEF
a4bcd733 2414 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2415 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2416 || hdr->sh_info >= num_sec
2417 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2418 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b 2419 {
11c6a7c6 2420 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b
NC
2421 goto success;
2422 }
252b5132
RH
2423
2424 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2425 goto fail;
2426
252b5132
RH
2427 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2428 if (target_sect == NULL)
bf67003b 2429 goto fail;
252b5132 2430
d4730f92
BS
2431 esdt = elf_section_data (target_sect);
2432 if (hdr->sh_type == SHT_RELA)
2433 p_hdr = &esdt->rela.hdr;
252b5132 2434 else
d4730f92
BS
2435 p_hdr = &esdt->rel.hdr;
2436
a7ba3896
NC
2437 /* PR 17512: file: 0b4f81b7.
2438 Also see PR 24456, for a file which deliberately has two reloc
2439 sections. */
06614111 2440 if (*p_hdr != NULL)
a7ba3896 2441 {
a859124d 2442 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2443 {
2444 _bfd_error_handler
2445 /* xgettext:c-format */
a859124d
AM
2446 (_("%pB: warning: secondary relocation section '%s' "
2447 "for section %pA found - ignoring"),
a8e14f4c
NC
2448 abfd, name, target_sect);
2449 }
956ea65c
MM
2450 else
2451 esdt->has_secondary_relocs = true;
a7ba3896
NC
2452 goto success;
2453 }
a8e14f4c 2454
ef53be89 2455 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2456 if (hdr2 == NULL)
bf67003b 2457 goto fail;
252b5132 2458 *hdr2 = *hdr;
d4730f92 2459 *p_hdr = hdr2;
252b5132 2460 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2461 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2462 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2463 target_sect->flags |= SEC_RELOC;
2464 target_sect->relocation = NULL;
2465 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2466 /* In the section to which the relocations apply, mark whether
2467 its relocations are of the REL or RELA variety. */
72730e0c 2468 if (hdr->sh_size != 0)
d4730f92
BS
2469 {
2470 if (hdr->sh_type == SHT_RELA)
2471 target_sect->use_rela_p = 1;
2472 }
252b5132 2473 abfd->flags |= HAS_RELOC;
bf67003b 2474 goto success;
252b5132 2475 }
252b5132
RH
2476
2477 case SHT_GNU_verdef:
2478 elf_dynverdef (abfd) = shindex;
2479 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2480 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2481 goto success;
252b5132
RH
2482
2483 case SHT_GNU_versym:
a50b2160 2484 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2485 goto fail;
2486
252b5132
RH
2487 elf_dynversym (abfd) = shindex;
2488 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2489 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2490 goto success;
252b5132
RH
2491
2492 case SHT_GNU_verneed:
2493 elf_dynverref (abfd) = shindex;
2494 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2495 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2496 goto success;
252b5132
RH
2497
2498 case SHT_SHLIB:
bf67003b 2499 goto success;
252b5132 2500
dbb410c3 2501 case SHT_GROUP:
44534af3 2502 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2503 goto fail;
2504
6dc132d9 2505 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2506 goto fail;
2507
bf67003b 2508 goto success;
dbb410c3 2509
252b5132 2510 default:
104d59d1
JM
2511 /* Possibly an attributes section. */
2512 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2513 || hdr->sh_type == bed->obj_attrs_section_type)
2514 {
2515 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2516 goto fail;
104d59d1 2517 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2518 goto success;
104d59d1
JM
2519 }
2520
252b5132 2521 /* Check for any processor-specific section types. */
3eb70a79 2522 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2523 goto success;
3eb70a79
L
2524
2525 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2526 {
2527 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2528 /* FIXME: How to properly handle allocated section reserved
2529 for applications? */
4eca0228 2530 _bfd_error_handler
695344c0 2531 /* xgettext:c-format */
871b3ab2 2532 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2533 abfd, hdr->sh_type, name);
3eb70a79 2534 else
bf67003b
NC
2535 {
2536 /* Allow sections reserved for applications. */
11c6a7c6 2537 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b
NC
2538 goto success;
2539 }
3eb70a79
L
2540 }
2541 else if (hdr->sh_type >= SHT_LOPROC
2542 && hdr->sh_type <= SHT_HIPROC)
2543 /* FIXME: We should handle this section. */
4eca0228 2544 _bfd_error_handler
695344c0 2545 /* xgettext:c-format */
871b3ab2 2546 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2547 abfd, hdr->sh_type, name);
3eb70a79 2548 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2549 {
2550 /* Unrecognised OS-specific sections. */
2551 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2552 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2553 required to correctly process the section and the file should
ff15b240 2554 be rejected with an error message. */
4eca0228 2555 _bfd_error_handler
695344c0 2556 /* xgettext:c-format */
871b3ab2 2557 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2558 abfd, hdr->sh_type, name);
ff15b240 2559 else
bf67003b
NC
2560 {
2561 /* Otherwise it should be processed. */
2562 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2563 goto success;
2564 }
ff15b240 2565 }
3eb70a79
L
2566 else
2567 /* FIXME: We should handle this section. */
4eca0228 2568 _bfd_error_handler
695344c0 2569 /* xgettext:c-format */
871b3ab2 2570 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2571 abfd, hdr->sh_type, name);
3eb70a79 2572
bf67003b 2573 goto fail;
252b5132
RH
2574 }
2575
bf67003b 2576 fail:
0a1b45a2 2577 ret = false;
bf67003b 2578 success:
0a1b45a2 2579 elf_tdata (abfd)->being_created[shindex] = false;
bf67003b 2580 return ret;
252b5132
RH
2581}
2582
87d72d41 2583/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2584
87d72d41
AM
2585Elf_Internal_Sym *
2586bfd_sym_from_r_symndx (struct sym_cache *cache,
2587 bfd *abfd,
2588 unsigned long r_symndx)
ec338859 2589{
ec338859
AM
2590 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2591
a5d1b3b5
AM
2592 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2593 {
2594 Elf_Internal_Shdr *symtab_hdr;
2595 unsigned char esym[sizeof (Elf64_External_Sym)];
2596 Elf_External_Sym_Shndx eshndx;
ec338859 2597
a5d1b3b5
AM
2598 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2599 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2600 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2601 return NULL;
9ad5cbcf 2602
a5d1b3b5
AM
2603 if (cache->abfd != abfd)
2604 {
2605 memset (cache->indx, -1, sizeof (cache->indx));
2606 cache->abfd = abfd;
2607 }
2608 cache->indx[ent] = r_symndx;
ec338859 2609 }
a5d1b3b5 2610
87d72d41 2611 return &cache->sym[ent];
ec338859
AM
2612}
2613
252b5132
RH
2614/* Given an ELF section number, retrieve the corresponding BFD
2615 section. */
2616
2617asection *
91d6fa6a 2618bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2619{
91d6fa6a 2620 if (sec_index >= elf_numsections (abfd))
252b5132 2621 return NULL;
91d6fa6a 2622 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2623}
2624
b35d266b 2625static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2626{
0112cd26 2627 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2628 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2629};
2630
b35d266b 2631static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2632{
0112cd26 2633 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2634 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2635 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2636};
2637
b35d266b 2638static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2639{
07d6d2b8
AM
2640 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2641 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2642 /* There are more DWARF sections than these, but they needn't be added here
2643 unless you have to cope with broken compilers that don't emit section
2644 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2645 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2646 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2647 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2648 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2649 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2650 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2651 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2652 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2653 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2654};
2655
b35d266b 2656static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2657{
07d6d2b8 2658 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2659 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2660 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2661};
2662
b35d266b 2663static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2664{
0112cd26 2665 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2666 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2667 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2668 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2669 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2670 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2671 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2672 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2673 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2674 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2675 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2676 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2677};
2678
b35d266b 2679static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2680{
07d6d2b8
AM
2681 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2682 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2683};
2684
b35d266b 2685static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2686{
07d6d2b8 2687 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2688 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2689 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2690 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2691};
2692
b35d266b 2693static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2694{
0112cd26 2695 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2696 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2697};
2698
b35d266b 2699static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2700{
2c6f3e56 2701 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2702 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2703 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2704 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2705};
2706
b35d266b 2707static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2708{
f5e98b7d 2709 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56 2710 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2711 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2712 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2713 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2714};
2715
b35d266b 2716static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2717{
0112cd26
NC
2718 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2719 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
fc07cf22 2720 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
07d6d2b8
AM
2721 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2722 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2723 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2724};
2725
b35d266b 2726static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2727{
0112cd26
NC
2728 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2729 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2730 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2731 /* See struct bfd_elf_special_section declaration for the semantics of
2732 this special case where .prefix_length != strlen (.prefix). */
2733 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2734 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2735};
2736
b35d266b 2737static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2738{
07d6d2b8
AM
2739 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2740 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2741 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2742 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2743};
2744
1b315056
CS
2745static const struct bfd_elf_special_section special_sections_z[] =
2746{
07d6d2b8
AM
2747 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2748 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2749 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2750 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2751 { NULL, 0, 0, 0, 0 }
1b315056
CS
2752};
2753
e4c93b56 2754static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2755{
7f4d3958 2756 special_sections_b, /* 'b' */
98ece1b3 2757 special_sections_c, /* 'c' */
7f4d3958
L
2758 special_sections_d, /* 'd' */
2759 NULL, /* 'e' */
2760 special_sections_f, /* 'f' */
2761 special_sections_g, /* 'g' */
2762 special_sections_h, /* 'h' */
2763 special_sections_i, /* 'i' */
2764 NULL, /* 'j' */
2765 NULL, /* 'k' */
2766 special_sections_l, /* 'l' */
2767 NULL, /* 'm' */
2768 special_sections_n, /* 'n' */
2769 NULL, /* 'o' */
2770 special_sections_p, /* 'p' */
2771 NULL, /* 'q' */
2772 special_sections_r, /* 'r' */
2773 special_sections_s, /* 's' */
2774 special_sections_t, /* 't' */
1b315056
CS
2775 NULL, /* 'u' */
2776 NULL, /* 'v' */
2777 NULL, /* 'w' */
2778 NULL, /* 'x' */
2779 NULL, /* 'y' */
2780 special_sections_z /* 'z' */
7f4d3958
L
2781};
2782
551b43fd
AM
2783const struct bfd_elf_special_section *
2784_bfd_elf_get_special_section (const char *name,
2785 const struct bfd_elf_special_section *spec,
2786 unsigned int rela)
2f89ff8d
L
2787{
2788 int i;
7f4d3958 2789 int len;
7f4d3958 2790
551b43fd 2791 len = strlen (name);
7f4d3958 2792
551b43fd 2793 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2794 {
2795 int suffix_len;
551b43fd 2796 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2797
2798 if (len < prefix_len)
2799 continue;
551b43fd 2800 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2801 continue;
2802
551b43fd 2803 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2804 if (suffix_len <= 0)
2805 {
2806 if (name[prefix_len] != 0)
2807 {
2808 if (suffix_len == 0)
2809 continue;
2810 if (name[prefix_len] != '.'
2811 && (suffix_len == -2
551b43fd 2812 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2813 continue;
2814 }
2815 }
2816 else
2817 {
2818 if (len < prefix_len + suffix_len)
2819 continue;
2820 if (memcmp (name + len - suffix_len,
551b43fd 2821 spec[i].prefix + prefix_len,
7dcb9820
AM
2822 suffix_len) != 0)
2823 continue;
2824 }
551b43fd 2825 return &spec[i];
7dcb9820 2826 }
2f89ff8d
L
2827
2828 return NULL;
2829}
2830
7dcb9820 2831const struct bfd_elf_special_section *
29ef7005 2832_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2833{
551b43fd
AM
2834 int i;
2835 const struct bfd_elf_special_section *spec;
29ef7005 2836 const struct elf_backend_data *bed;
2f89ff8d
L
2837
2838 /* See if this is one of the special sections. */
551b43fd
AM
2839 if (sec->name == NULL)
2840 return NULL;
2f89ff8d 2841
29ef7005
L
2842 bed = get_elf_backend_data (abfd);
2843 spec = bed->special_sections;
2844 if (spec)
2845 {
2846 spec = _bfd_elf_get_special_section (sec->name,
2847 bed->special_sections,
2848 sec->use_rela_p);
2849 if (spec != NULL)
2850 return spec;
2851 }
2852
551b43fd
AM
2853 if (sec->name[0] != '.')
2854 return NULL;
2f89ff8d 2855
551b43fd 2856 i = sec->name[1] - 'b';
1b315056 2857 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2858 return NULL;
2859
2860 spec = special_sections[i];
2f89ff8d 2861
551b43fd
AM
2862 if (spec == NULL)
2863 return NULL;
2864
2865 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2866}
2867
0a1b45a2 2868bool
217aa764 2869_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2870{
2871 struct bfd_elf_section_data *sdata;
551b43fd 2872 const struct elf_backend_data *bed;
7dcb9820 2873 const struct bfd_elf_special_section *ssect;
252b5132 2874
f0abc2a1
AM
2875 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2876 if (sdata == NULL)
2877 {
a50b1753 2878 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2879 sizeof (*sdata));
f0abc2a1 2880 if (sdata == NULL)
0a1b45a2 2881 return false;
217aa764 2882 sec->used_by_bfd = sdata;
f0abc2a1 2883 }
bf572ba0 2884
551b43fd
AM
2885 /* Indicate whether or not this section should use RELA relocations. */
2886 bed = get_elf_backend_data (abfd);
2887 sec->use_rela_p = bed->default_use_rela_p;
2888
8c803a2d
AM
2889 /* Set up ELF section type and flags for newly created sections, if
2890 there is an ABI mandated section. */
2891 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2892 if (ssect != NULL)
2f89ff8d 2893 {
8c803a2d
AM
2894 elf_section_type (sec) = ssect->type;
2895 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2896 }
2897
f592407e 2898 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2899}
2900
2901/* Create a new bfd section from an ELF program header.
2902
2903 Since program segments have no names, we generate a synthetic name
2904 of the form segment<NUM>, where NUM is generally the index in the
2905 program header table. For segments that are split (see below) we
2906 generate the names segment<NUM>a and segment<NUM>b.
2907
2908 Note that some program segments may have a file size that is different than
2909 (less than) the memory size. All this means is that at execution the
2910 system must allocate the amount of memory specified by the memory size,
2911 but only initialize it with the first "file size" bytes read from the
2912 file. This would occur for example, with program segments consisting
2913 of combined data+bss.
2914
2915 To handle the above situation, this routine generates TWO bfd sections
2916 for the single program segment. The first has the length specified by
2917 the file size of the segment, and the second has the length specified
2918 by the difference between the two sizes. In effect, the segment is split
11c6a7c6 2919 into its initialized and uninitialized parts. */
252b5132 2920
0a1b45a2 2921bool
217aa764
AM
2922_bfd_elf_make_section_from_phdr (bfd *abfd,
2923 Elf_Internal_Phdr *hdr,
91d6fa6a 2924 int hdr_index,
a50b1753 2925 const char *type_name)
252b5132
RH
2926{
2927 asection *newsect;
2928 char *name;
2929 char namebuf[64];
d4c88bbb 2930 size_t len;
252b5132 2931 int split;
502794d4 2932 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2933
2934 split = ((hdr->p_memsz > 0)
2935 && (hdr->p_filesz > 0)
2936 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2937
2938 if (hdr->p_filesz > 0)
252b5132 2939 {
91d6fa6a 2940 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2941 len = strlen (namebuf) + 1;
a50b1753 2942 name = (char *) bfd_alloc (abfd, len);
d5191d0c 2943 if (!name)
0a1b45a2 2944 return false;
d5191d0c
AM
2945 memcpy (name, namebuf, len);
2946 newsect = bfd_make_section (abfd, name);
2947 if (newsect == NULL)
0a1b45a2 2948 return false;
502794d4
CE
2949 newsect->vma = hdr->p_vaddr / opb;
2950 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2951 newsect->size = hdr->p_filesz;
2952 newsect->filepos = hdr->p_offset;
2953 newsect->flags |= SEC_HAS_CONTENTS;
2954 newsect->alignment_power = bfd_log2 (hdr->p_align);
2955 if (hdr->p_type == PT_LOAD)
252b5132 2956 {
d5191d0c
AM
2957 newsect->flags |= SEC_ALLOC;
2958 newsect->flags |= SEC_LOAD;
2959 if (hdr->p_flags & PF_X)
2960 {
2961 /* FIXME: all we known is that it has execute PERMISSION,
2962 may be data. */
2963 newsect->flags |= SEC_CODE;
2964 }
2965 }
2966 if (!(hdr->p_flags & PF_W))
2967 {
2968 newsect->flags |= SEC_READONLY;
252b5132 2969 }
252b5132
RH
2970 }
2971
d5191d0c 2972 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2973 {
d5191d0c
AM
2974 bfd_vma align;
2975
91d6fa6a 2976 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2977 len = strlen (namebuf) + 1;
a50b1753 2978 name = (char *) bfd_alloc (abfd, len);
252b5132 2979 if (!name)
0a1b45a2 2980 return false;
d4c88bbb 2981 memcpy (name, namebuf, len);
252b5132
RH
2982 newsect = bfd_make_section (abfd, name);
2983 if (newsect == NULL)
0a1b45a2 2984 return false;
502794d4
CE
2985 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2986 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 2987 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2988 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2989 align = newsect->vma & -newsect->vma;
2990 if (align == 0 || align > hdr->p_align)
2991 align = hdr->p_align;
2992 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2993 if (hdr->p_type == PT_LOAD)
2994 {
2995 newsect->flags |= SEC_ALLOC;
2996 if (hdr->p_flags & PF_X)
2997 newsect->flags |= SEC_CODE;
2998 }
2999 if (!(hdr->p_flags & PF_W))
3000 newsect->flags |= SEC_READONLY;
3001 }
3002
0a1b45a2 3003 return true;
252b5132
RH
3004}
3005
0a1b45a2 3006static bool
864619bb
KS
3007_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3008{
3009 /* The return value is ignored. Build-ids are considered optional. */
3010 if (templ->xvec->flavour == bfd_target_elf_flavour)
3011 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3012 (templ, offset);
0a1b45a2 3013 return false;
864619bb
KS
3014}
3015
0a1b45a2 3016bool
91d6fa6a 3017bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3018{
9c5bfbb7 3019 const struct elf_backend_data *bed;
20cfcaae
NC
3020
3021 switch (hdr->p_type)
3022 {
3023 case PT_NULL:
91d6fa6a 3024 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3025
3026 case PT_LOAD:
864619bb 3027 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
0a1b45a2 3028 return false;
864619bb
KS
3029 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3030 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
0a1b45a2 3031 return true;
20cfcaae
NC
3032
3033 case PT_DYNAMIC:
91d6fa6a 3034 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3035
3036 case PT_INTERP:
91d6fa6a 3037 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3038
3039 case PT_NOTE:
91d6fa6a 3040 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
0a1b45a2 3041 return false;
276da9b3
L
3042 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3043 hdr->p_align))
0a1b45a2
AM
3044 return false;
3045 return true;
20cfcaae
NC
3046
3047 case PT_SHLIB:
91d6fa6a 3048 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3049
3050 case PT_PHDR:
91d6fa6a 3051 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3052
811072d8 3053 case PT_GNU_EH_FRAME:
91d6fa6a 3054 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3055 "eh_frame_hdr");
3056
2b05f1b7 3057 case PT_GNU_STACK:
91d6fa6a 3058 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3059
8c37241b 3060 case PT_GNU_RELRO:
91d6fa6a 3061 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3062
20cfcaae 3063 default:
8c1acd09 3064 /* Check for any processor-specific program segment types. */
20cfcaae 3065 bed = get_elf_backend_data (abfd);
91d6fa6a 3066 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3067 }
3068}
3069
d4730f92
BS
3070/* Return the REL_HDR for SEC, assuming there is only a single one, either
3071 REL or RELA. */
3072
3073Elf_Internal_Shdr *
3074_bfd_elf_single_rel_hdr (asection *sec)
3075{
3076 if (elf_section_data (sec)->rel.hdr)
3077 {
3078 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3079 return elf_section_data (sec)->rel.hdr;
3080 }
3081 else
3082 return elf_section_data (sec)->rela.hdr;
3083}
3084
0a1b45a2 3085static bool
3e19fb8f
L
3086_bfd_elf_set_reloc_sh_name (bfd *abfd,
3087 Elf_Internal_Shdr *rel_hdr,
3088 const char *sec_name,
0a1b45a2 3089 bool use_rela_p)
3e19fb8f
L
3090{
3091 char *name = (char *) bfd_alloc (abfd,
3092 sizeof ".rela" + strlen (sec_name));
3093 if (name == NULL)
0a1b45a2 3094 return false;
3e19fb8f
L
3095
3096 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3097 rel_hdr->sh_name =
3098 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
0a1b45a2 3099 false);
3e19fb8f 3100 if (rel_hdr->sh_name == (unsigned int) -1)
0a1b45a2 3101 return false;
3e19fb8f 3102
0a1b45a2 3103 return true;
3e19fb8f
L
3104}
3105
d4730f92
BS
3106/* Allocate and initialize a section-header for a new reloc section,
3107 containing relocations against ASECT. It is stored in RELDATA. If
3108 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3109 relocations. */
23bc299b 3110
0a1b45a2 3111static bool
217aa764 3112_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3113 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3114 const char *sec_name,
0a1b45a2
AM
3115 bool use_rela_p,
3116 bool delay_st_name_p)
23bc299b 3117{
d4730f92 3118 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3120
d4730f92 3121 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3122 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3123 reldata->hdr = rel_hdr;
23bc299b 3124
3e19fb8f
L
3125 if (delay_st_name_p)
3126 rel_hdr->sh_name = (unsigned int) -1;
3127 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3128 use_rela_p))
0a1b45a2 3129 return false;
23bc299b
MM
3130 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3131 rel_hdr->sh_entsize = (use_rela_p
3132 ? bed->s->sizeof_rela
3133 : bed->s->sizeof_rel);
72de5009 3134 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3135 rel_hdr->sh_flags = 0;
23bc299b
MM
3136 rel_hdr->sh_addr = 0;
3137 rel_hdr->sh_size = 0;
3138 rel_hdr->sh_offset = 0;
3139
0a1b45a2 3140 return true;
23bc299b
MM
3141}
3142
94be91de
JB
3143/* Return the default section type based on the passed in section flags. */
3144
3145int
3146bfd_elf_get_default_section_type (flagword flags)
3147{
0e41bebb 3148 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3149 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3150 return SHT_NOBITS;
3151 return SHT_PROGBITS;
3152}
3153
d4730f92
BS
3154struct fake_section_arg
3155{
3156 struct bfd_link_info *link_info;
0a1b45a2 3157 bool failed;
d4730f92
BS
3158};
3159
252b5132
RH
3160/* Set up an ELF internal section header for a section. */
3161
252b5132 3162static void
d4730f92 3163elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3164{
d4730f92 3165 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3166 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3167 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3168 Elf_Internal_Shdr *this_hdr;
0414f35b 3169 unsigned int sh_type;
0ce398f1 3170 const char *name = asect->name;
0a1b45a2 3171 bool delay_st_name_p = false;
233bf4f8 3172 bfd_vma mask;
252b5132 3173
d4730f92 3174 if (arg->failed)
252b5132
RH
3175 {
3176 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3177 loop. */
252b5132
RH
3178 return;
3179 }
3180
d4730f92 3181 this_hdr = &esd->this_hdr;
252b5132 3182
f6fe1ccd 3183 if (arg->link_info)
0ce398f1 3184 {
f6fe1ccd
L
3185 /* ld: compress DWARF debug sections with names: .debug_*. */
3186 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3187 && (asect->flags & SEC_DEBUGGING)
3188 && name[1] == 'd'
3189 && name[6] == '_')
3190 {
3191 /* Set SEC_ELF_COMPRESS to indicate this section should be
3192 compressed. */
3193 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3194 /* If this section will be compressed, delay adding section
3e19fb8f
L
3195 name to section name section after it is compressed in
3196 _bfd_elf_assign_file_positions_for_non_load. */
0a1b45a2 3197 delay_st_name_p = true;
f6fe1ccd
L
3198 }
3199 }
3200 else if ((asect->flags & SEC_ELF_RENAME))
3201 {
3202 /* objcopy: rename output DWARF debug section. */
3203 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3204 {
3205 /* When we decompress or compress with SHF_COMPRESSED,
3206 convert section name from .zdebug_* to .debug_* if
3207 needed. */
3208 if (name[1] == 'z')
3209 {
3210 char *new_name = convert_zdebug_to_debug (abfd, name);
3211 if (new_name == NULL)
3212 {
0a1b45a2 3213 arg->failed = true;
f6fe1ccd
L
3214 return;
3215 }
3216 name = new_name;
3217 }
3218 }
3219 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3220 {
f6fe1ccd
L
3221 /* PR binutils/18087: Compression does not always make a
3222 section smaller. So only rename the section when
3223 compression has actually taken place. If input section
3224 name is .zdebug_*, we should never compress it again. */
3225 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3226 if (new_name == NULL)
3227 {
0a1b45a2 3228 arg->failed = true;
0ce398f1
L
3229 return;
3230 }
f6fe1ccd
L
3231 BFD_ASSERT (name[1] != 'z');
3232 name = new_name;
0ce398f1
L
3233 }
3234 }
3235
3e19fb8f
L
3236 if (delay_st_name_p)
3237 this_hdr->sh_name = (unsigned int) -1;
3238 else
252b5132 3239 {
3e19fb8f
L
3240 this_hdr->sh_name
3241 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3242 name, false);
3e19fb8f
L
3243 if (this_hdr->sh_name == (unsigned int) -1)
3244 {
0a1b45a2 3245 arg->failed = true;
3e19fb8f
L
3246 return;
3247 }
252b5132
RH
3248 }
3249
a4d8e49b 3250 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3251
3252 if ((asect->flags & SEC_ALLOC) != 0
3253 || asect->user_set_vma)
502794d4 3254 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3255 else
3256 this_hdr->sh_addr = 0;
3257
3258 this_hdr->sh_offset = 0;
eea6121a 3259 this_hdr->sh_size = asect->size;
252b5132 3260 this_hdr->sh_link = 0;
c86934ce
NC
3261 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3262 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3263 {
4eca0228 3264 _bfd_error_handler
695344c0 3265 /* xgettext:c-format */
9793eb77 3266 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3267 abfd, asect->alignment_power, asect);
0a1b45a2 3268 arg->failed = true;
c86934ce
NC
3269 return;
3270 }
233bf4f8
AM
3271 /* Set sh_addralign to the highest power of two given by alignment
3272 consistent with the section VMA. Linker scripts can force VMA. */
3273 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3274 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3275 /* The sh_entsize and sh_info fields may have been set already by
3276 copy_private_section_data. */
3277
3278 this_hdr->bfd_section = asect;
3279 this_hdr->contents = NULL;
3280
3cddba1e
L
3281 /* If the section type is unspecified, we set it based on
3282 asect->flags. */
c212f39d
FS
3283 if (asect->type != 0)
3284 sh_type = asect->type;
3285 else if ((asect->flags & SEC_GROUP) != 0)
98ece1b3 3286 sh_type = SHT_GROUP;
98ece1b3 3287 else
94be91de 3288 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3289
3cddba1e 3290 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3291 this_hdr->sh_type = sh_type;
3292 else if (this_hdr->sh_type == SHT_NOBITS
3293 && sh_type == SHT_PROGBITS
3294 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3295 {
98ece1b3
AM
3296 /* Warn if we are changing a NOBITS section to PROGBITS, but
3297 allow the link to proceed. This can happen when users link
3298 non-bss input sections to bss output sections, or emit data
3299 to a bss output section via a linker script. */
4eca0228 3300 _bfd_error_handler
871b3ab2 3301 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3302 this_hdr->sh_type = sh_type;
3cddba1e
L
3303 }
3304
2f89ff8d 3305 switch (this_hdr->sh_type)
252b5132 3306 {
2f89ff8d 3307 default:
2f89ff8d
L
3308 break;
3309
3310 case SHT_STRTAB:
2f89ff8d
L
3311 case SHT_NOTE:
3312 case SHT_NOBITS:
3313 case SHT_PROGBITS:
3314 break;
606851fb
AM
3315
3316 case SHT_INIT_ARRAY:
3317 case SHT_FINI_ARRAY:
3318 case SHT_PREINIT_ARRAY:
3319 this_hdr->sh_entsize = bed->s->arch_size / 8;
3320 break;
2f89ff8d
L
3321
3322 case SHT_HASH:
c7ac6ff8 3323 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3324 break;
5de3bf90 3325
2f89ff8d 3326 case SHT_DYNSYM:
252b5132 3327 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3328 break;
3329
3330 case SHT_DYNAMIC:
252b5132 3331 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3332 break;
3333
3334 case SHT_RELA:
3335 if (get_elf_backend_data (abfd)->may_use_rela_p)
3336 this_hdr->sh_entsize = bed->s->sizeof_rela;
3337 break;
3338
3339 case SHT_REL:
3340 if (get_elf_backend_data (abfd)->may_use_rel_p)
3341 this_hdr->sh_entsize = bed->s->sizeof_rel;
3342 break;
3343
3344 case SHT_GNU_versym:
252b5132 3345 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3346 break;
3347
3348 case SHT_GNU_verdef:
252b5132
RH
3349 this_hdr->sh_entsize = 0;
3350 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3351 cverdefs. The linker will set cverdefs, but sh_info will be
3352 zero. */
252b5132
RH
3353 if (this_hdr->sh_info == 0)
3354 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3355 else
3356 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3357 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3358 break;
3359
3360 case SHT_GNU_verneed:
252b5132
RH
3361 this_hdr->sh_entsize = 0;
3362 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3363 cverrefs. The linker will set cverrefs, but sh_info will be
3364 zero. */
252b5132
RH
3365 if (this_hdr->sh_info == 0)
3366 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3367 else
3368 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3369 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3370 break;
3371
3372 case SHT_GROUP:
1783205a 3373 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3374 break;
fdc90cb4
JJ
3375
3376 case SHT_GNU_HASH:
3377 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3378 break;
dbb410c3 3379 }
252b5132
RH
3380
3381 if ((asect->flags & SEC_ALLOC) != 0)
3382 this_hdr->sh_flags |= SHF_ALLOC;
3383 if ((asect->flags & SEC_READONLY) == 0)
3384 this_hdr->sh_flags |= SHF_WRITE;
3385 if ((asect->flags & SEC_CODE) != 0)
3386 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3387 if ((asect->flags & SEC_MERGE) != 0)
3388 {
3389 this_hdr->sh_flags |= SHF_MERGE;
3390 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3391 }
84865015
NC
3392 if ((asect->flags & SEC_STRINGS) != 0)
3393 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3394 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3395 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3396 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3397 {
3398 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3399 if (asect->size == 0
3400 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3401 {
3a800eb9 3402 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3403
704afa60 3404 this_hdr->sh_size = 0;
3a800eb9
AM
3405 if (o != NULL)
3406 {
704afa60 3407 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3408 if (this_hdr->sh_size != 0)
3409 this_hdr->sh_type = SHT_NOBITS;
3410 }
704afa60
JJ
3411 }
3412 }
18ae9cc1
L
3413 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3414 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3415
d4730f92
BS
3416 /* If the section has relocs, set up a section header for the
3417 SHT_REL[A] section. If two relocation sections are required for
3418 this section, it is up to the processor-specific back-end to
3419 create the other. */
3420 if ((asect->flags & SEC_RELOC) != 0)
3421 {
3422 /* When doing a relocatable link, create both REL and RELA sections if
3423 needed. */
3424 if (arg->link_info
3425 /* Do the normal setup if we wouldn't create any sections here. */
3426 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3427 && (bfd_link_relocatable (arg->link_info)
3428 || arg->link_info->emitrelocations))
d4730f92
BS
3429 {
3430 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3431 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
0a1b45a2 3432 false, delay_st_name_p))
d4730f92 3433 {
0a1b45a2 3434 arg->failed = true;
d4730f92
BS
3435 return;
3436 }
3437 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3438 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
0a1b45a2 3439 true, delay_st_name_p))
d4730f92 3440 {
0a1b45a2 3441 arg->failed = true;
d4730f92
BS
3442 return;
3443 }
3444 }
3445 else if (!_bfd_elf_init_reloc_shdr (abfd,
3446 (asect->use_rela_p
3447 ? &esd->rela : &esd->rel),
f6fe1ccd 3448 name,
3e19fb8f
L
3449 asect->use_rela_p,
3450 delay_st_name_p))
db4677b8 3451 {
0a1b45a2 3452 arg->failed = true;
db4677b8
AM
3453 return;
3454 }
d4730f92
BS
3455 }
3456
252b5132 3457 /* Check for processor-specific section types. */
0414f35b 3458 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3459 if (bed->elf_backend_fake_sections
3460 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8 3461 {
0a1b45a2 3462 arg->failed = true;
db4677b8
AM
3463 return;
3464 }
252b5132 3465
42bb2e33 3466 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3467 {
3468 /* Don't change the header type from NOBITS if we are being
42bb2e33 3469 called for objcopy --only-keep-debug. */
0414f35b
AM
3470 this_hdr->sh_type = sh_type;
3471 }
252b5132
RH
3472}
3473
bcacc0f5
AM
3474/* Fill in the contents of a SHT_GROUP section. Called from
3475 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3476 when ELF targets use the generic linker, ld. Called for ld -r
3477 from bfd_elf_final_link. */
dbb410c3 3478
1126897b 3479void
217aa764 3480bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3481{
0a1b45a2 3482 bool *failedptr = (bool *) failedptrarg;
9dce4196 3483 asection *elt, *first;
dbb410c3 3484 unsigned char *loc;
0a1b45a2 3485 bool gas;
dbb410c3 3486
7e4111ad
L
3487 /* Ignore linker created group section. See elfNN_ia64_object_p in
3488 elfxx-ia64.c. */
ce5aecf8
AM
3489 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3490 || sec->size == 0
dbb410c3
AM
3491 || *failedptr)
3492 return;
3493
bcacc0f5
AM
3494 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3495 {
3496 unsigned long symindx = 0;
3497
3498 /* elf_group_id will have been set up by objcopy and the
3499 generic linker. */
3500 if (elf_group_id (sec) != NULL)
3501 symindx = elf_group_id (sec)->udata.i;
1126897b 3502
bcacc0f5
AM
3503 if (symindx == 0)
3504 {
3505 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3506 elf_section_syms.
3507 PR 25699: A corrupt input file could contain bogus group info. */
27e3da31
AM
3508 if (sec->index >= elf_num_section_syms (abfd)
3509 || elf_section_syms (abfd)[sec->index] == NULL)
6a541707 3510 {
0a1b45a2 3511 *failedptr = true;
6a541707
NC
3512 return;
3513 }
bcacc0f5
AM
3514 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3515 }
3516 elf_section_data (sec)->this_hdr.sh_info = symindx;
3517 }
3518 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3519 {
bcacc0f5
AM
3520 /* The ELF backend linker sets sh_info to -2 when the group
3521 signature symbol is global, and thus the index can't be
3522 set until all local symbols are output. */
53720c49
AM
3523 asection *igroup;
3524 struct bfd_elf_section_data *sec_data;
3525 unsigned long symndx;
3526 unsigned long extsymoff;
bcacc0f5
AM
3527 struct elf_link_hash_entry *h;
3528
53720c49
AM
3529 /* The point of this little dance to the first SHF_GROUP section
3530 then back to the SHT_GROUP section is that this gets us to
3531 the SHT_GROUP in the input object. */
3532 igroup = elf_sec_group (elf_next_in_group (sec));
3533 sec_data = elf_section_data (igroup);
3534 symndx = sec_data->this_hdr.sh_info;
3535 extsymoff = 0;
bcacc0f5
AM
3536 if (!elf_bad_symtab (igroup->owner))
3537 {
3538 Elf_Internal_Shdr *symtab_hdr;
3539
3540 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3541 extsymoff = symtab_hdr->sh_info;
3542 }
3543 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3544 while (h->root.type == bfd_link_hash_indirect
3545 || h->root.type == bfd_link_hash_warning)
3546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3547
3548 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3549 }
dbb410c3 3550
1126897b 3551 /* The contents won't be allocated for "ld -r" or objcopy. */
0a1b45a2 3552 gas = true;
dbb410c3
AM
3553 if (sec->contents == NULL)
3554 {
0a1b45a2 3555 gas = false;
a50b1753 3556 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3557
3558 /* Arrange for the section to be written out. */
3559 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3560 if (sec->contents == NULL)
3561 {
0a1b45a2 3562 *failedptr = true;
dbb410c3
AM
3563 return;
3564 }
3565 }
3566
eea6121a 3567 loc = sec->contents + sec->size;
dbb410c3 3568
9dce4196
AM
3569 /* Get the pointer to the first section in the group that gas
3570 squirreled away here. objcopy arranges for this to be set to the
3571 start of the input section group. */
3572 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3573
3574 /* First element is a flag word. Rest of section is elf section
3575 indices for all the sections of the group. Write them backwards
3576 just to keep the group in the same order as given in .section
3577 directives, not that it matters. */
3578 while (elt != NULL)
3579 {
9dce4196 3580 asection *s;
9dce4196 3581
9dce4196 3582 s = elt;
415f38a6
AM
3583 if (!gas)
3584 s = s->output_section;
3585 if (s != NULL
3586 && !bfd_is_abs_section (s))
01e1a5bc 3587 {
db4677b8 3588 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3589 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3590
3591 if (elf_sec->rel.hdr != NULL
3592 && (gas
3593 || (input_elf_sec->rel.hdr != NULL
3594 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3595 {
28e07a05 3596 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8 3597 loc -= 4;
6ecc36f7
AM
3598 if (loc == sec->contents)
3599 break;
db4677b8
AM
3600 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3601 }
28e07a05
AM
3602 if (elf_sec->rela.hdr != NULL
3603 && (gas
3604 || (input_elf_sec->rela.hdr != NULL
3605 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3606 {
28e07a05 3607 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8 3608 loc -= 4;
6ecc36f7
AM
3609 if (loc == sec->contents)
3610 break;
db4677b8
AM
3611 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3612 }
01e1a5bc 3613 loc -= 4;
6ecc36f7
AM
3614 if (loc == sec->contents)
3615 break;
db4677b8 3616 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3617 }
945906ff 3618 elt = elf_next_in_group (elt);
9dce4196
AM
3619 if (elt == first)
3620 break;
dbb410c3
AM
3621 }
3622
6ecc36f7
AM
3623 /* We should always get here with loc == sec->contents + 4, but it is
3624 possible to craft bogus SHT_GROUP sections that will cause segfaults
3625 in objcopy without checking loc here and in the loop above. */
3626 if (loc == sec->contents)
3627 BFD_ASSERT (0);
3628 else
7744e327 3629 {
6ecc36f7
AM
3630 loc -= 4;
3631 if (loc != sec->contents)
3632 {
3633 BFD_ASSERT (0);
3634 memset (sec->contents + 4, 0, loc - sec->contents);
3635 loc = sec->contents;
3636 }
7744e327 3637 }
dbb410c3 3638
9dce4196 3639 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3640}
3641
bce964aa
AM
3642/* Given NAME, the name of a relocation section stripped of its
3643 .rel/.rela prefix, return the section in ABFD to which the
3644 relocations apply. */
bd53a53a
L
3645
3646asection *
bce964aa
AM
3647_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3648{
3649 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3650 section likely apply to .got.plt or .got section. */
3651 if (get_elf_backend_data (abfd)->want_got_plt
3652 && strcmp (name, ".plt") == 0)
3653 {
3654 asection *sec;
3655
3656 name = ".got.plt";
3657 sec = bfd_get_section_by_name (abfd, name);
3658 if (sec != NULL)
3659 return sec;
3660 name = ".got";
3661 }
3662
3663 return bfd_get_section_by_name (abfd, name);
3664}
3665
3666/* Return the section to which RELOC_SEC applies. */
3667
3668static asection *
3669elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3670{
3671 const char *name;
3672 unsigned int type;
3673 bfd *abfd;
bce964aa 3674 const struct elf_backend_data *bed;
bd53a53a
L
3675
3676 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3677 if (type != SHT_REL && type != SHT_RELA)
3678 return NULL;
3679
3680 /* We look up the section the relocs apply to by name. */
3681 name = reloc_sec->name;
3f3328b8 3682 if (!startswith (name, ".rel"))
bce964aa
AM
3683 return NULL;
3684 name += 4;
3685 if (type == SHT_RELA && *name++ != 'a')
3686 return NULL;
bd53a53a 3687
bd53a53a 3688 abfd = reloc_sec->owner;
bce964aa
AM
3689 bed = get_elf_backend_data (abfd);
3690 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3691}
3692
252b5132
RH
3693/* Assign all ELF section numbers. The dummy first section is handled here
3694 too. The link/info pointers for the standard section types are filled
67411cbf 3695 in here too, while we're at it. LINK_INFO will be 0 when arriving
2ba2f096 3696 here for gas, objcopy, and when using the generic ELF linker. */
252b5132 3697
0a1b45a2 3698static bool
da9f89d4 3699assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3700{
3701 struct elf_obj_tdata *t = elf_tdata (abfd);
3702 asection *sec;
3e19fb8f 3703 unsigned int section_number;
252b5132 3704 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3705 struct bfd_elf_section_data *d;
0a1b45a2 3706 bool need_symtab;
446f7ed5 3707 size_t amt;
252b5132
RH
3708
3709 section_number = 1;
3710
2b0f7ef9
JJ
3711 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3712
da9f89d4 3713 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3714 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3715 {
ef53be89 3716 size_t reloc_count = 0;
14f2c699 3717
da9f89d4 3718 /* Put SHT_GROUP sections first. */
04dd1667 3719 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3720 {
5daa8fe7 3721 d = elf_section_data (sec);
da9f89d4
L
3722
3723 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3724 {
5daa8fe7 3725 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3726 {
3727 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3728 bfd_section_list_remove (abfd, sec);
da9f89d4 3729 abfd->section_count--;
da9f89d4 3730 }
08a40648 3731 else
4fbb74a6 3732 d->this_idx = section_number++;
da9f89d4 3733 }
14f2c699
L
3734
3735 /* Count relocations. */
3736 reloc_count += sec->reloc_count;
47cc2cf5 3737 }
14f2c699 3738
2ba2f096 3739 /* Set/clear HAS_RELOC depending on whether there are relocations. */
14f2c699
L
3740 if (reloc_count == 0)
3741 abfd->flags &= ~HAS_RELOC;
2ba2f096
JB
3742 else
3743 abfd->flags |= HAS_RELOC;
47cc2cf5
PB
3744 }
3745
3746 for (sec = abfd->sections; sec; sec = sec->next)
3747 {
3748 d = elf_section_data (sec);
3749
3750 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3751 d->this_idx = section_number++;
3e19fb8f
L
3752 if (d->this_hdr.sh_name != (unsigned int) -1)
3753 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3754 if (d->rel.hdr)
2b0f7ef9 3755 {
d4730f92 3756 d->rel.idx = section_number++;
3e19fb8f
L
3757 if (d->rel.hdr->sh_name != (unsigned int) -1)
3758 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3759 }
d4730f92
BS
3760 else
3761 d->rel.idx = 0;
23bc299b 3762
d4730f92 3763 if (d->rela.hdr)
2b0f7ef9 3764 {
d4730f92 3765 d->rela.idx = section_number++;
3e19fb8f
L
3766 if (d->rela.hdr->sh_name != (unsigned int) -1)
3767 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3768 }
23bc299b 3769 else
d4730f92 3770 d->rela.idx = 0;
252b5132
RH
3771 }
3772
3516e984 3773 need_symtab = (bfd_get_symcount (abfd) > 0
b6d1f70c
AM
3774 || (link_info == NULL
3775 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3776 == HAS_RELOC)));
3516e984 3777 if (need_symtab)
252b5132 3778 {
12bd6957 3779 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3780 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3781 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3782 {
7a6e0d89 3783 elf_section_list *entry;
6a40cf0c
NC
3784
3785 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3786
7a6e0d89 3787 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3788 entry->ndx = section_number++;
3789 elf_symtab_shndx_list (abfd) = entry;
3790 entry->hdr.sh_name
9ad5cbcf 3791 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3792 ".symtab_shndx", false);
6a40cf0c 3793 if (entry->hdr.sh_name == (unsigned int) -1)
0a1b45a2 3794 return false;
9ad5cbcf 3795 }
12bd6957 3796 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3797 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3798 }
3799
dd905818
NC
3800 elf_shstrtab_sec (abfd) = section_number++;
3801 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3802 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3803
1c52a645
L
3804 if (section_number >= SHN_LORESERVE)
3805 {
695344c0 3806 /* xgettext:c-format */
871b3ab2 3807 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645 3808 abfd, section_number);
0a1b45a2 3809 return false;
1c52a645
L
3810 }
3811
9ad5cbcf 3812 elf_numsections (abfd) = section_number;
252b5132
RH
3813 elf_elfheader (abfd)->e_shnum = section_number;
3814
3815 /* Set up the list of section header pointers, in agreement with the
3816 indices. */
446f7ed5
AM
3817 amt = section_number * sizeof (Elf_Internal_Shdr *);
3818 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3819 if (i_shdrp == NULL)
0a1b45a2 3820 return false;
252b5132 3821
a50b1753 3822 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3823 sizeof (Elf_Internal_Shdr));
252b5132
RH
3824 if (i_shdrp[0] == NULL)
3825 {
3826 bfd_release (abfd, i_shdrp);
0a1b45a2 3827 return false;
252b5132 3828 }
252b5132
RH
3829
3830 elf_elfsections (abfd) = i_shdrp;
3831
12bd6957 3832 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3833 if (need_symtab)
252b5132 3834 {
12bd6957 3835 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3836 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3837 {
6a40cf0c
NC
3838 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3839 BFD_ASSERT (entry != NULL);
3840 i_shdrp[entry->ndx] = & entry->hdr;
3841 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3842 }
12bd6957
AM
3843 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3844 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3845 }
38ce5b11 3846
252b5132
RH
3847 for (sec = abfd->sections; sec; sec = sec->next)
3848 {
252b5132 3849 asection *s;
252b5132 3850
91d6fa6a
NC
3851 d = elf_section_data (sec);
3852
252b5132 3853 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3854 if (d->rel.idx != 0)
3855 i_shdrp[d->rel.idx] = d->rel.hdr;
3856 if (d->rela.idx != 0)
3857 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3858
3859 /* Fill in the sh_link and sh_info fields while we're at it. */
3860
3861 /* sh_link of a reloc section is the section index of the symbol
3862 table. sh_info is the section index of the section to which
3863 the relocation entries apply. */
d4730f92 3864 if (d->rel.idx != 0)
252b5132 3865 {
12bd6957 3866 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3867 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3868 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3869 }
d4730f92 3870 if (d->rela.idx != 0)
23bc299b 3871 {
12bd6957 3872 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3873 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3874 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3875 }
252b5132 3876
38ce5b11
L
3877 /* We need to set up sh_link for SHF_LINK_ORDER. */
3878 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3879 {
3880 s = elf_linked_to_section (sec);
b71702f1
NC
3881 /* We can now have a NULL linked section pointer.
3882 This happens when the sh_link field is 0, which is done
3883 when a linked to section is discarded but the linking
3884 section has been retained for some reason. */
38ce5b11 3885 if (s)
38ce5b11 3886 {
67411cbf
AM
3887 /* Check discarded linkonce section. */
3888 if (discarded_section (s))
38ce5b11 3889 {
67411cbf
AM
3890 asection *kept;
3891 _bfd_error_handler
3892 /* xgettext:c-format */
3893 (_("%pB: sh_link of section `%pA' points to"
3894 " discarded section `%pA' of `%pB'"),
3895 abfd, d->this_hdr.bfd_section, s, s->owner);
3896 /* Point to the kept section if it has the same
3897 size as the discarded one. */
3898 kept = _bfd_elf_check_kept_section (s, link_info);
3899 if (kept == NULL)
f2876037 3900 {
f2876037 3901 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3902 return false;
f2876037 3903 }
67411cbf
AM
3904 s = kept;
3905 }
3906 /* Handle objcopy. */
3907 else if (s->output_section == NULL)
3908 {
3909 _bfd_error_handler
3910 /* xgettext:c-format */
3911 (_("%pB: sh_link of section `%pA' points to"
3912 " removed section `%pA' of `%pB'"),
3913 abfd, d->this_hdr.bfd_section, s, s->owner);
3914 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3915 return false;
f2876037 3916 }
67411cbf 3917 s = s->output_section;
ccd2ec6a
L
3918 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3919 }
38ce5b11
L
3920 }
3921
252b5132
RH
3922 switch (d->this_hdr.sh_type)
3923 {
3924 case SHT_REL:
3925 case SHT_RELA:
3926 /* A reloc section which we are treating as a normal BFD
3927 section. sh_link is the section index of the symbol
3928 table. sh_info is the section index of the section to
3929 which the relocation entries apply. We assume that an
b6d1f70c
AM
3930 allocated reloc section uses the dynamic symbol table
3931 if there is one. Otherwise we guess the normal symbol
3932 table. FIXME: How can we be sure? */
3933 if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3934 {
3935 s = bfd_get_section_by_name (abfd, ".dynsym");
3936 if (s != NULL)
3937 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3938 }
3939 if (d->this_hdr.sh_link == 0)
3940 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132 3941
bce964aa 3942 s = elf_get_reloc_section (sec);
252b5132 3943 if (s != NULL)
9ef5d938
L
3944 {
3945 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3946 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3947 }
252b5132
RH
3948 break;
3949
3950 case SHT_STRTAB:
3951 /* We assume that a section named .stab*str is a stabs
3952 string section. We look for a section with the same name
3953 but without the trailing ``str'', and set its sh_link
3954 field to point to this section. */
08dedd66 3955 if (startswith (sec->name, ".stab")
252b5132
RH
3956 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3957 {
3958 size_t len;
3959 char *alc;
3960
3961 len = strlen (sec->name);
a50b1753 3962 alc = (char *) bfd_malloc (len - 2);
252b5132 3963 if (alc == NULL)
0a1b45a2 3964 return false;
d4c88bbb 3965 memcpy (alc, sec->name, len - 3);
252b5132
RH
3966 alc[len - 3] = '\0';
3967 s = bfd_get_section_by_name (abfd, alc);
3968 free (alc);
3969 if (s != NULL)
3970 {
3971 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3972
3973 /* This is a .stab section. */
34ca5531 3974 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3975 }
3976 }
3977 break;
3978
3979 case SHT_DYNAMIC:
3980 case SHT_DYNSYM:
3981 case SHT_GNU_verneed:
3982 case SHT_GNU_verdef:
3983 /* sh_link is the section header index of the string table
3984 used for the dynamic entries, or the symbol table, or the
3985 version strings. */
3986 s = bfd_get_section_by_name (abfd, ".dynstr");
3987 if (s != NULL)
3988 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3989 break;
3990
7f1204bb
JJ
3991 case SHT_GNU_LIBLIST:
3992 /* sh_link is the section header index of the prelink library
08a40648
AM
3993 list used for the dynamic entries, or the symbol table, or
3994 the version strings. */
b6d1f70c
AM
3995 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3996 ? ".dynstr" : ".gnu.libstr"));
7f1204bb
JJ
3997 if (s != NULL)
3998 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3999 break;
4000
252b5132 4001 case SHT_HASH:
fdc90cb4 4002 case SHT_GNU_HASH:
252b5132
RH
4003 case SHT_GNU_versym:
4004 /* sh_link is the section header index of the symbol table
4005 this hash table or version table is for. */
4006 s = bfd_get_section_by_name (abfd, ".dynsym");
4007 if (s != NULL)
4008 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4009 break;
dbb410c3
AM
4010
4011 case SHT_GROUP:
12bd6957 4012 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4013 }
4014 }
4015
3e19fb8f
L
4016 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4017 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4018 debug section name from .debug_* to .zdebug_* if needed. */
4019
0a1b45a2 4020 return true;
252b5132
RH
4021}
4022
0a1b45a2 4023static bool
217aa764 4024sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4025{
4026 /* If the backend has a special mapping, use it. */
9c5bfbb7 4027 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4028 if (bed->elf_backend_sym_is_global)
4029 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4030
e47bf690 4031 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4032 || bfd_is_und_section (bfd_asymbol_section (sym))
4033 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4034}
4035
76359541
TP
4036/* Filter global symbols of ABFD to include in the import library. All
4037 SYMCOUNT symbols of ABFD can be examined from their pointers in
4038 SYMS. Pointers of symbols to keep should be stored contiguously at
4039 the beginning of that array.
4040
4041 Returns the number of symbols to keep. */
4042
4043unsigned int
4044_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4045 asymbol **syms, long symcount)
4046{
4047 long src_count, dst_count = 0;
4048
4049 for (src_count = 0; src_count < symcount; src_count++)
4050 {
4051 asymbol *sym = syms[src_count];
4052 char *name = (char *) bfd_asymbol_name (sym);
4053 struct bfd_link_hash_entry *h;
4054
4055 if (!sym_is_global (abfd, sym))
4056 continue;
4057
0a1b45a2 4058 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
5df1bc57
AM
4059 if (h == NULL)
4060 continue;
76359541
TP
4061 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4062 continue;
76359541
TP
4063 if (h->linker_def || h->ldscript_def)
4064 continue;
4065
4066 syms[dst_count++] = sym;
4067 }
4068
4069 syms[dst_count] = NULL;
4070
4071 return dst_count;
4072}
4073
5372391b 4074/* Don't output section symbols for sections that are not going to be
c6d8cab4 4075 output, that are duplicates or there is no BFD section. */
5372391b 4076
0a1b45a2 4077static bool
5372391b
AM
4078ignore_section_sym (bfd *abfd, asymbol *sym)
4079{
c6d8cab4
L
4080 elf_symbol_type *type_ptr;
4081
db0c309f 4082 if (sym == NULL)
0a1b45a2 4083 return false;
db0c309f 4084
c6d8cab4 4085 if ((sym->flags & BSF_SECTION_SYM) == 0)
0a1b45a2 4086 return false;
c6d8cab4 4087
d1bcae83
L
4088 /* Ignore the section symbol if it isn't used. */
4089 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
0a1b45a2 4090 return true;
d1bcae83 4091
db0c309f 4092 if (sym->section == NULL)
0a1b45a2 4093 return true;
db0c309f 4094
c1229f84 4095 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4096 return ((type_ptr != NULL
4097 && type_ptr->internal_elf_sym.st_shndx != 0
4098 && bfd_is_abs_section (sym->section))
4099 || !(sym->section->owner == abfd
db0c309f
NC
4100 || (sym->section->output_section != NULL
4101 && sym->section->output_section->owner == abfd
2633a79c
AM
4102 && sym->section->output_offset == 0)
4103 || bfd_is_abs_section (sym->section)));
5372391b
AM
4104}
4105
2633a79c
AM
4106/* Map symbol from it's internal number to the external number, moving
4107 all local symbols to be at the head of the list. */
4108
0a1b45a2 4109static bool
12bd6957 4110elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4111{
dc810e39 4112 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4113 asymbol **syms = bfd_get_outsymbols (abfd);
4114 asymbol **sect_syms;
dc810e39
AM
4115 unsigned int num_locals = 0;
4116 unsigned int num_globals = 0;
4117 unsigned int num_locals2 = 0;
4118 unsigned int num_globals2 = 0;
7292b3ac 4119 unsigned int max_index = 0;
dc810e39 4120 unsigned int idx;
252b5132
RH
4121 asection *asect;
4122 asymbol **new_syms;
446f7ed5 4123 size_t amt;
252b5132
RH
4124
4125#ifdef DEBUG
4126 fprintf (stderr, "elf_map_symbols\n");
4127 fflush (stderr);
4128#endif
4129
252b5132
RH
4130 for (asect = abfd->sections; asect; asect = asect->next)
4131 {
4132 if (max_index < asect->index)
4133 max_index = asect->index;
4134 }
4135
4136 max_index++;
446f7ed5
AM
4137 amt = max_index * sizeof (asymbol *);
4138 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4139 if (sect_syms == NULL)
0a1b45a2 4140 return false;
252b5132 4141 elf_section_syms (abfd) = sect_syms;
4e89ac30 4142 elf_num_section_syms (abfd) = max_index;
252b5132 4143
079e9a2f
AM
4144 /* Init sect_syms entries for any section symbols we have already
4145 decided to output. */
252b5132
RH
4146 for (idx = 0; idx < symcount; idx++)
4147 {
dc810e39 4148 asymbol *sym = syms[idx];
c044fabd 4149
252b5132 4150 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4151 && sym->value == 0
2633a79c
AM
4152 && !ignore_section_sym (abfd, sym)
4153 && !bfd_is_abs_section (sym->section))
252b5132 4154 {
5372391b 4155 asection *sec = sym->section;
252b5132 4156
5372391b
AM
4157 if (sec->owner != abfd)
4158 sec = sec->output_section;
252b5132 4159
5372391b 4160 sect_syms[sec->index] = syms[idx];
252b5132
RH
4161 }
4162 }
4163
252b5132
RH
4164 /* Classify all of the symbols. */
4165 for (idx = 0; idx < symcount; idx++)
4166 {
2633a79c 4167 if (sym_is_global (abfd, syms[idx]))
252b5132 4168 num_globals++;
2633a79c
AM
4169 else if (!ignore_section_sym (abfd, syms[idx]))
4170 num_locals++;
252b5132 4171 }
079e9a2f 4172
5372391b 4173 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4174 sections will already have a section symbol in outsymbols, but
4175 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4176 at least in that case. */
252b5132
RH
4177 for (asect = abfd->sections; asect; asect = asect->next)
4178 {
d1bcae83
L
4179 asymbol *sym = asect->symbol;
4180 /* Don't include ignored section symbols. */
4181 if (!ignore_section_sym (abfd, sym)
4182 && sect_syms[asect->index] == NULL)
252b5132 4183 {
079e9a2f 4184 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4185 num_locals++;
4186 else
4187 num_globals++;
252b5132
RH
4188 }
4189 }
4190
4191 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4192 amt = (num_locals + num_globals) * sizeof (asymbol *);
4193 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4194 if (new_syms == NULL)
0a1b45a2 4195 return false;
252b5132
RH
4196
4197 for (idx = 0; idx < symcount; idx++)
4198 {
4199 asymbol *sym = syms[idx];
dc810e39 4200 unsigned int i;
252b5132 4201
2633a79c
AM
4202 if (sym_is_global (abfd, sym))
4203 i = num_locals + num_globals2++;
d1bcae83 4204 /* Don't include ignored section symbols. */
2633a79c 4205 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4206 i = num_locals2++;
4207 else
2633a79c 4208 continue;
252b5132
RH
4209 new_syms[i] = sym;
4210 sym->udata.i = i + 1;
4211 }
4212 for (asect = abfd->sections; asect; asect = asect->next)
4213 {
d1bcae83
L
4214 asymbol *sym = asect->symbol;
4215 if (!ignore_section_sym (abfd, sym)
4216 && sect_syms[asect->index] == NULL)
252b5132 4217 {
dc810e39 4218 unsigned int i;
252b5132 4219
079e9a2f 4220 sect_syms[asect->index] = sym;
252b5132
RH
4221 if (!sym_is_global (abfd, sym))
4222 i = num_locals2++;
4223 else
4224 i = num_locals + num_globals2++;
4225 new_syms[i] = sym;
4226 sym->udata.i = i + 1;
4227 }
4228 }
4229
4230 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4231
12bd6957 4232 *pnum_locals = num_locals;
0a1b45a2 4233 return true;
252b5132
RH
4234}
4235
4236/* Align to the maximum file alignment that could be required for any
4237 ELF data structure. */
4238
268b6b39 4239static inline file_ptr
217aa764 4240align_file_position (file_ptr off, int align)
252b5132
RH
4241{
4242 return (off + align - 1) & ~(align - 1);
4243}
4244
4245/* Assign a file position to a section, optionally aligning to the
4246 required section alignment. */
4247
217aa764
AM
4248file_ptr
4249_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4250 file_ptr offset,
0a1b45a2 4251 bool align)
252b5132 4252{
72de5009 4253 if (align && i_shdrp->sh_addralign > 1)
1f9b1a84 4254 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
252b5132
RH
4255 i_shdrp->sh_offset = offset;
4256 if (i_shdrp->bfd_section != NULL)
4257 i_shdrp->bfd_section->filepos = offset;
4258 if (i_shdrp->sh_type != SHT_NOBITS)
4259 offset += i_shdrp->sh_size;
4260 return offset;
4261}
4262
4263/* Compute the file positions we are going to put the sections at, and
4264 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4265 is not NULL, this is being called by the ELF backend linker. */
4266
0a1b45a2 4267bool
217aa764
AM
4268_bfd_elf_compute_section_file_positions (bfd *abfd,
4269 struct bfd_link_info *link_info)
252b5132 4270{
9c5bfbb7 4271 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4272 struct fake_section_arg fsargs;
0a1b45a2 4273 bool failed;
ef10c3ac 4274 struct elf_strtab_hash *strtab = NULL;
252b5132 4275 Elf_Internal_Shdr *shstrtab_hdr;
0a1b45a2 4276 bool need_symtab;
252b5132
RH
4277
4278 if (abfd->output_has_begun)
0a1b45a2 4279 return true;
252b5132
RH
4280
4281 /* Do any elf backend specific processing first. */
4282 if (bed->elf_backend_begin_write_processing)
4283 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4284
ed7e9d0b 4285 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
0a1b45a2 4286 return false;
252b5132 4287
0a1b45a2 4288 fsargs.failed = false;
d4730f92
BS
4289 fsargs.link_info = link_info;
4290 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4291 if (fsargs.failed)
0a1b45a2 4292 return false;
252b5132 4293
da9f89d4 4294 if (!assign_section_numbers (abfd, link_info))
0a1b45a2 4295 return false;
252b5132
RH
4296
4297 /* The backend linker builds symbol table information itself. */
3516e984
L
4298 need_symtab = (link_info == NULL
4299 && (bfd_get_symcount (abfd) > 0
4300 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4301 == HAS_RELOC)));
4302 if (need_symtab)
252b5132
RH
4303 {
4304 /* Non-zero if doing a relocatable link. */
4305 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4306
3d16b64e 4307 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
0a1b45a2 4308 return false;
252b5132
RH
4309 }
4310
0a1b45a2 4311 failed = false;
1126897b 4312 if (link_info == NULL)
dbb410c3 4313 {
1126897b 4314 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4315 if (failed)
0a1b45a2 4316 return false;
dbb410c3
AM
4317 }
4318
252b5132 4319 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4320 /* sh_name was set in init_file_header. */
252b5132 4321 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4322 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4323 shstrtab_hdr->sh_addr = 0;
946748d5 4324 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4325 shstrtab_hdr->sh_entsize = 0;
4326 shstrtab_hdr->sh_link = 0;
4327 shstrtab_hdr->sh_info = 0;
3e19fb8f 4328 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4329 shstrtab_hdr->sh_addralign = 1;
4330
c84fca4d 4331 if (!assign_file_positions_except_relocs (abfd, link_info))
0a1b45a2 4332 return false;
252b5132 4333
3516e984 4334 if (need_symtab)
252b5132
RH
4335 {
4336 file_ptr off;
4337 Elf_Internal_Shdr *hdr;
4338
12bd6957 4339 off = elf_next_file_pos (abfd);
252b5132 4340
6a40cf0c 4341 hdr = & elf_symtab_hdr (abfd);
0a1b45a2 4342 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4343
6a40cf0c
NC
4344 if (elf_symtab_shndx_list (abfd) != NULL)
4345 {
4346 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4347 if (hdr->sh_size != 0)
0a1b45a2 4348 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6a40cf0c
NC
4349 /* FIXME: What about other symtab_shndx sections in the list ? */
4350 }
9ad5cbcf 4351
252b5132 4352 hdr = &elf_tdata (abfd)->strtab_hdr;
0a1b45a2 4353 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4354
12bd6957 4355 elf_next_file_pos (abfd) = off;
252b5132
RH
4356
4357 /* Now that we know where the .strtab section goes, write it
08a40648 4358 out. */
252b5132 4359 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4360 || ! _bfd_elf_strtab_emit (abfd, strtab))
0a1b45a2 4361 return false;
ef10c3ac 4362 _bfd_elf_strtab_free (strtab);
252b5132
RH
4363 }
4364
0a1b45a2 4365 abfd->output_has_begun = true;
252b5132 4366
0a1b45a2 4367 return true;
252b5132
RH
4368}
4369
8df52eee
AM
4370/* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4371 function effectively returns whether --eh-frame-hdr is given on the
4372 command line. After size_dynamic_sections the result reflects
4373 whether .eh_frame_hdr will actually be output (sizing isn't done
4374 until ldemul_after_allocation). */
4375
4376static asection *
4377elf_eh_frame_hdr (const struct bfd_link_info *info)
4378{
4379 if (info != NULL && is_elf_hash_table (info->hash))
4380 return elf_hash_table (info)->eh_info.hdr_sec;
4381 return NULL;
4382}
4383
8ded5a0f
AM
4384/* Make an initial estimate of the size of the program header. If we
4385 get the number wrong here, we'll redo section placement. */
4386
4387static bfd_size_type
4388get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4389{
4390 size_t segs;
4391 asection *s;
2b05f1b7 4392 const struct elf_backend_data *bed;
8ded5a0f
AM
4393
4394 /* Assume we will need exactly two PT_LOAD segments: one for text
4395 and one for data. */
4396 segs = 2;
4397
4398 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4399 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4400 {
4401 /* If we have a loadable interpreter section, we need a
4402 PT_INTERP segment. In this case, assume we also need a
4403 PT_PHDR segment, although that may not be true for all
4404 targets. */
e9a38e0f 4405 segs += 2;
8ded5a0f
AM
4406 }
4407
4408 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4409 {
4410 /* We need a PT_DYNAMIC segment. */
4411 ++segs;
f210dcff 4412 }
08a40648 4413
ceae84aa 4414 if (info != NULL && info->relro)
f210dcff
L
4415 {
4416 /* We need a PT_GNU_RELRO segment. */
4417 ++segs;
8ded5a0f
AM
4418 }
4419
8df52eee 4420 if (elf_eh_frame_hdr (info))
8ded5a0f
AM
4421 {
4422 /* We need a PT_GNU_EH_FRAME segment. */
4423 ++segs;
4424 }
4425
12bd6957 4426 if (elf_stack_flags (abfd))
8ded5a0f 4427 {
2b05f1b7
L
4428 /* We need a PT_GNU_STACK segment. */
4429 ++segs;
4430 }
94b11780 4431
0a59decb
L
4432 s = bfd_get_section_by_name (abfd,
4433 NOTE_GNU_PROPERTY_SECTION_NAME);
4434 if (s != NULL && s->size != 0)
4435 {
4436 /* We need a PT_GNU_PROPERTY segment. */
4437 ++segs;
4438 }
4439
2b05f1b7
L
4440 for (s = abfd->sections; s != NULL; s = s->next)
4441 {
8ded5a0f 4442 if ((s->flags & SEC_LOAD) != 0
23e463ed 4443 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4444 {
23e463ed 4445 unsigned int alignment_power;
8ded5a0f
AM
4446 /* We need a PT_NOTE segment. */
4447 ++segs;
23e463ed
L
4448 /* Try to create just one PT_NOTE segment for all adjacent
4449 loadable SHT_NOTE sections. gABI requires that within a
4450 PT_NOTE segment (and also inside of each SHT_NOTE section)
4451 each note should have the same alignment. So we check
4452 whether the sections are correctly aligned. */
4453 alignment_power = s->alignment_power;
4454 while (s->next != NULL
4455 && s->next->alignment_power == alignment_power
4456 && (s->next->flags & SEC_LOAD) != 0
4457 && elf_section_type (s->next) == SHT_NOTE)
4458 s = s->next;
8ded5a0f
AM
4459 }
4460 }
4461
4462 for (s = abfd->sections; s != NULL; s = s->next)
4463 {
4464 if (s->flags & SEC_THREAD_LOCAL)
4465 {
4466 /* We need a PT_TLS segment. */
4467 ++segs;
4468 break;
4469 }
4470 }
4471
2b05f1b7 4472 bed = get_elf_backend_data (abfd);
a91e1603 4473
df3a023b
AM
4474 if ((abfd->flags & D_PAGED) != 0
4475 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4476 {
4477 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4478 bfd_vma commonpagesize;
4479 unsigned int page_align_power;
4480
4481 if (info != NULL)
4482 commonpagesize = info->commonpagesize;
4483 else
4484 commonpagesize = bed->commonpagesize;
4485 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4486 for (s = abfd->sections; s != NULL; s = s->next)
4487 if (elf_section_flags (s) & SHF_GNU_MBIND)
4488 {
4489 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4490 {
4491 _bfd_error_handler
4492 /* xgettext:c-format */
4493 (_("%pB: GNU_MBIND section `%pA' has invalid "
4494 "sh_info field: %d"),
4495 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4496 continue;
4497 }
4498 /* Align mbind section to page size. */
4499 if (s->alignment_power < page_align_power)
4500 s->alignment_power = page_align_power;
4501 segs ++;
4502 }
4503 }
4504
4505 /* Let the backend count up any program headers it might need. */
4506 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4507 {
4508 int a;
4509
4510 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4511 if (a == -1)
4512 abort ();
4513 segs += a;
4514 }
4515
4516 return segs * bed->s->sizeof_phdr;
4517}
4518
2ea37f1c
NC
4519/* Find the segment that contains the output_section of section. */
4520
4521Elf_Internal_Phdr *
4522_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4523{
4524 struct elf_segment_map *m;
4525 Elf_Internal_Phdr *p;
4526
12bd6957 4527 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4528 m != NULL;
4529 m = m->next, p++)
4530 {
4531 int i;
4532
4533 for (i = m->count - 1; i >= 0; i--)
4534 if (m->sections[i] == section)
4535 return p;
4536 }
4537
4538 return NULL;
4539}
4540
252b5132
RH
4541/* Create a mapping from a set of sections to a program segment. */
4542
217aa764
AM
4543static struct elf_segment_map *
4544make_mapping (bfd *abfd,
4545 asection **sections,
4546 unsigned int from,
4547 unsigned int to,
0a1b45a2 4548 bool phdr)
252b5132
RH
4549{
4550 struct elf_segment_map *m;
4551 unsigned int i;
4552 asection **hdrpp;
986f0783 4553 size_t amt;
252b5132 4554
00bee008
AM
4555 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4556 amt += (to - from) * sizeof (asection *);
a50b1753 4557 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4558 if (m == NULL)
4559 return NULL;
4560 m->next = NULL;
4561 m->p_type = PT_LOAD;
4562 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4563 m->sections[i - from] = *hdrpp;
4564 m->count = to - from;
4565
4566 if (from == 0 && phdr)
4567 {
4568 /* Include the headers in the first PT_LOAD segment. */
4569 m->includes_filehdr = 1;
4570 m->includes_phdrs = 1;
4571 }
4572
4573 return m;
4574}
4575
229fcec5
MM
4576/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4577 on failure. */
4578
4579struct elf_segment_map *
4580_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4581{
4582 struct elf_segment_map *m;
4583
a50b1753 4584 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4585 sizeof (struct elf_segment_map));
229fcec5
MM
4586 if (m == NULL)
4587 return NULL;
4588 m->next = NULL;
4589 m->p_type = PT_DYNAMIC;
4590 m->count = 1;
4591 m->sections[0] = dynsec;
08a40648 4592
229fcec5
MM
4593 return m;
4594}
4595
8ded5a0f 4596/* Possibly add or remove segments from the segment map. */
252b5132 4597
0a1b45a2 4598static bool
3dea8fca
AM
4599elf_modify_segment_map (bfd *abfd,
4600 struct bfd_link_info *info,
0a1b45a2 4601 bool remove_empty_load)
252b5132 4602{
252e386e 4603 struct elf_segment_map **m;
8ded5a0f 4604 const struct elf_backend_data *bed;
252b5132 4605
8ded5a0f
AM
4606 /* The placement algorithm assumes that non allocated sections are
4607 not in PT_LOAD segments. We ensure this here by removing such
4608 sections from the segment map. We also remove excluded
252e386e
AM
4609 sections. Finally, any PT_LOAD segment without sections is
4610 removed. */
12bd6957 4611 m = &elf_seg_map (abfd);
252e386e 4612 while (*m)
8ded5a0f
AM
4613 {
4614 unsigned int i, new_count;
252b5132 4615
252e386e 4616 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4617 {
252e386e
AM
4618 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4619 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4620 || (*m)->p_type != PT_LOAD))
8ded5a0f 4621 {
252e386e
AM
4622 (*m)->sections[new_count] = (*m)->sections[i];
4623 new_count++;
8ded5a0f
AM
4624 }
4625 }
252e386e 4626 (*m)->count = new_count;
252b5132 4627
1a9ccd70
NC
4628 if (remove_empty_load
4629 && (*m)->p_type == PT_LOAD
4630 && (*m)->count == 0
4631 && !(*m)->includes_phdrs)
252e386e
AM
4632 *m = (*m)->next;
4633 else
4634 m = &(*m)->next;
8ded5a0f 4635 }
252b5132 4636
8ded5a0f
AM
4637 bed = get_elf_backend_data (abfd);
4638 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4639 {
252e386e 4640 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
0a1b45a2 4641 return false;
252b5132 4642 }
252b5132 4643
0a1b45a2 4644 return true;
8ded5a0f 4645}
252b5132 4646
dbc88fc1
AM
4647#define IS_TBSS(s) \
4648 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4649
e2cbf4df
L
4650/* Set up a mapping from BFD sections to program segments. Update
4651 NEED_LAYOUT if the section layout is changed. */
252b5132 4652
0a1b45a2 4653bool
e2cbf4df
L
4654_bfd_elf_map_sections_to_segments (bfd *abfd,
4655 struct bfd_link_info *info,
23cc1de5 4656 bool *need_layout)
8ded5a0f
AM
4657{
4658 unsigned int count;
4659 struct elf_segment_map *m;
4660 asection **sections = NULL;
4661 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 4662 bool no_user_phdrs;
252b5132 4663
12bd6957 4664 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4665
4666 if (info != NULL)
23cc1de5
L
4667 {
4668 info->user_phdrs = !no_user_phdrs;
4669
4670 /* Size the relative relocations if DT_RELR is enabled. */
4671 if (info->enable_dt_relr
4672 && need_layout != NULL
4673 && bed->size_relative_relocs
4674 && !bed->size_relative_relocs (info, need_layout))
4675 info->callbacks->einfo
4676 (_("%F%P: failed to size relative relocations\n"));
4677 }
d324f6d6 4678
3dea8fca 4679 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4680 {
8ded5a0f
AM
4681 asection *s;
4682 unsigned int i;
4683 struct elf_segment_map *mfirst;
4684 struct elf_segment_map **pm;
4685 asection *last_hdr;
4686 bfd_vma last_size;
00bee008 4687 unsigned int hdr_index;
8ded5a0f
AM
4688 bfd_vma maxpagesize;
4689 asection **hdrpp;
0a1b45a2
AM
4690 bool phdr_in_segment;
4691 bool writable;
4692 bool executable;
446f7ed5 4693 unsigned int tls_count = 0;
8ded5a0f 4694 asection *first_tls = NULL;
a91e1603 4695 asection *first_mbind = NULL;
8ded5a0f 4696 asection *dynsec, *eh_frame_hdr;
446f7ed5 4697 size_t amt;
66631823
CE
4698 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4699 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4700 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4701
8ded5a0f 4702 /* Select the allocated sections, and sort them. */
252b5132 4703
446f7ed5
AM
4704 amt = bfd_count_sections (abfd) * sizeof (asection *);
4705 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4706 if (sections == NULL)
252b5132 4707 goto error_return;
252b5132 4708
8d06853e
AM
4709 /* Calculate top address, avoiding undefined behaviour of shift
4710 left operator when shift count is equal to size of type
4711 being shifted. */
4712 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4713 addr_mask = (addr_mask << 1) + 1;
4714
8ded5a0f
AM
4715 i = 0;
4716 for (s = abfd->sections; s != NULL; s = s->next)
4717 {
4718 if ((s->flags & SEC_ALLOC) != 0)
4719 {
48db3297
AM
4720 /* target_index is unused until bfd_elf_final_link
4721 starts output of section symbols. Use it to make
4722 qsort stable. */
4723 s->target_index = i;
8ded5a0f
AM
4724 sections[i] = s;
4725 ++i;
8d06853e 4726 /* A wrapping section potentially clashes with header. */
66631823
CE
4727 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4728 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4729 }
4730 }
4731 BFD_ASSERT (i <= bfd_count_sections (abfd));
4732 count = i;
252b5132 4733
8ded5a0f 4734 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4735
64029e93
AM
4736 phdr_size = elf_program_header_size (abfd);
4737 if (phdr_size == (bfd_size_type) -1)
4738 phdr_size = get_program_header_size (abfd, info);
4739 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4740 /* phdr_size is compared to LMA values which are in bytes. */
4741 phdr_size /= opb;
c410035d
AM
4742 if (info != NULL)
4743 maxpagesize = info->maxpagesize;
4744 else
4745 maxpagesize = bed->maxpagesize;
64029e93
AM
4746 if (maxpagesize == 0)
4747 maxpagesize = 1;
4748 phdr_in_segment = info != NULL && info->load_phdrs;
4749 if (count != 0
4750 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4751 >= (phdr_size & (maxpagesize - 1))))
4752 /* For compatibility with old scripts that may not be using
4753 SIZEOF_HEADERS, add headers when it looks like space has
4754 been left for them. */
0a1b45a2 4755 phdr_in_segment = true;
252b5132 4756
64029e93 4757 /* Build the mapping. */
8ded5a0f
AM
4758 mfirst = NULL;
4759 pm = &mfirst;
252b5132 4760
8ded5a0f
AM
4761 /* If we have a .interp section, then create a PT_PHDR segment for
4762 the program headers and a PT_INTERP segment for the .interp
4763 section. */
4764 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4765 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4766 {
4767 amt = sizeof (struct elf_segment_map);
a50b1753 4768 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4769 if (m == NULL)
4770 goto error_return;
4771 m->next = NULL;
4772 m->p_type = PT_PHDR;
f882209d 4773 m->p_flags = PF_R;
8ded5a0f
AM
4774 m->p_flags_valid = 1;
4775 m->includes_phdrs = 1;
0a1b45a2 4776 phdr_in_segment = true;
8ded5a0f
AM
4777 *pm = m;
4778 pm = &m->next;
252b5132 4779
8ded5a0f 4780 amt = sizeof (struct elf_segment_map);
a50b1753 4781 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4782 if (m == NULL)
4783 goto error_return;
4784 m->next = NULL;
4785 m->p_type = PT_INTERP;
4786 m->count = 1;
4787 m->sections[0] = s;
4788
4789 *pm = m;
4790 pm = &m->next;
252b5132 4791 }
8ded5a0f
AM
4792
4793 /* Look through the sections. We put sections in the same program
4794 segment when the start of the second section can be placed within
4795 a few bytes of the end of the first section. */
4796 last_hdr = NULL;
4797 last_size = 0;
00bee008 4798 hdr_index = 0;
0a1b45a2
AM
4799 writable = false;
4800 executable = false;
8ded5a0f
AM
4801 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4802 if (dynsec != NULL
4803 && (dynsec->flags & SEC_LOAD) == 0)
4804 dynsec = NULL;
4805
64029e93 4806 if ((abfd->flags & D_PAGED) == 0)
0a1b45a2 4807 phdr_in_segment = false;
64029e93 4808
8ded5a0f
AM
4809 /* Deal with -Ttext or something similar such that the first section
4810 is not adjacent to the program headers. This is an
4811 approximation, since at this point we don't know exactly how many
4812 program headers we will need. */
64029e93 4813 if (phdr_in_segment && count > 0)
252b5132 4814 {
66631823 4815 bfd_vma phdr_lma; /* Bytes. */
0a1b45a2 4816 bool separate_phdr = false;
64029e93
AM
4817
4818 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4819 if (info != NULL
4820 && info->separate_code
4821 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4822 {
64029e93
AM
4823 /* If data sections should be separate from code and
4824 thus not executable, and the first section is
4825 executable then put the file and program headers in
4826 their own PT_LOAD. */
0a1b45a2 4827 separate_phdr = true;
64029e93
AM
4828 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4829 == (sections[0]->lma & addr_mask & -maxpagesize)))
4830 {
4831 /* The file and program headers are currently on the
4832 same page as the first section. Put them on the
4833 previous page if we can. */
4834 if (phdr_lma >= maxpagesize)
4835 phdr_lma -= maxpagesize;
4836 else
0a1b45a2 4837 separate_phdr = false;
64029e93
AM
4838 }
4839 }
4840 if ((sections[0]->lma & addr_mask) < phdr_lma
4841 || (sections[0]->lma & addr_mask) < phdr_size)
4842 /* If file and program headers would be placed at the end
4843 of memory then it's probably better to omit them. */
0a1b45a2 4844 phdr_in_segment = false;
64029e93
AM
4845 else if (phdr_lma < wrap_to)
4846 /* If a section wraps around to where we'll be placing
4847 file and program headers, then the headers will be
4848 overwritten. */
0a1b45a2 4849 phdr_in_segment = false;
64029e93
AM
4850 else if (separate_phdr)
4851 {
4852 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4853 if (m == NULL)
4854 goto error_return;
66631823 4855 m->p_paddr = phdr_lma * opb;
64029e93
AM
4856 m->p_vaddr_offset
4857 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4858 m->p_paddr_valid = 1;
4859 *pm = m;
4860 pm = &m->next;
0a1b45a2 4861 phdr_in_segment = false;
1a9ccd70 4862 }
252b5132
RH
4863 }
4864
8ded5a0f 4865 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4866 {
8ded5a0f 4867 asection *hdr;
0a1b45a2 4868 bool new_segment;
8ded5a0f
AM
4869
4870 hdr = *hdrpp;
4871
4872 /* See if this section and the last one will fit in the same
4873 segment. */
4874
4875 if (last_hdr == NULL)
4876 {
4877 /* If we don't have a segment yet, then we don't need a new
4878 one (we build the last one after this loop). */
0a1b45a2 4879 new_segment = false;
8ded5a0f
AM
4880 }
4881 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4882 {
4883 /* If this section has a different relation between the
4884 virtual address and the load address, then we need a new
4885 segment. */
0a1b45a2 4886 new_segment = true;
8ded5a0f 4887 }
b5599592
AM
4888 else if (hdr->lma < last_hdr->lma + last_size
4889 || last_hdr->lma + last_size < last_hdr->lma)
4890 {
4891 /* If this section has a load address that makes it overlap
4892 the previous section, then we need a new segment. */
0a1b45a2 4893 new_segment = true;
b5599592 4894 }
76cb3a89
AM
4895 else if ((abfd->flags & D_PAGED) != 0
4896 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4897 == (hdr->lma & -maxpagesize)))
4898 {
4899 /* If we are demand paged then we can't map two disk
4900 pages onto the same memory page. */
0a1b45a2 4901 new_segment = false;
76cb3a89 4902 }
39948a60
NC
4903 /* In the next test we have to be careful when last_hdr->lma is close
4904 to the end of the address space. If the aligned address wraps
4905 around to the start of the address space, then there are no more
4906 pages left in memory and it is OK to assume that the current
4907 section can be included in the current segment. */
76cb3a89
AM
4908 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4909 + maxpagesize > last_hdr->lma)
4910 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4911 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4912 {
4913 /* If putting this section in this segment would force us to
4914 skip a page in the segment, then we need a new segment. */
0a1b45a2 4915 new_segment = true;
8ded5a0f
AM
4916 }
4917 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4918 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4919 {
e5654c0f
AM
4920 /* We don't want to put a loaded section after a
4921 nonloaded (ie. bss style) section in the same segment
4922 as that will force the non-loaded section to be loaded.
76cb3a89 4923 Consider .tbss sections as loaded for this purpose. */
0a1b45a2 4924 new_segment = true;
8ded5a0f
AM
4925 }
4926 else if ((abfd->flags & D_PAGED) == 0)
4927 {
4928 /* If the file is not demand paged, which means that we
4929 don't require the sections to be correctly aligned in the
4930 file, then there is no other reason for a new segment. */
0a1b45a2 4931 new_segment = false;
8ded5a0f 4932 }
2888249f
L
4933 else if (info != NULL
4934 && info->separate_code
4935 && executable != ((hdr->flags & SEC_CODE) != 0))
4936 {
0a1b45a2 4937 new_segment = true;
2888249f 4938 }
8ded5a0f 4939 else if (! writable
76cb3a89 4940 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4941 {
4942 /* We don't want to put a writable section in a read only
76cb3a89 4943 segment. */
0a1b45a2 4944 new_segment = true;
8ded5a0f
AM
4945 }
4946 else
4947 {
4948 /* Otherwise, we can use the same segment. */
0a1b45a2 4949 new_segment = false;
8ded5a0f
AM
4950 }
4951
2889e75b 4952 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4953 if (last_hdr != NULL
4954 && info != NULL
4955 && info->callbacks->override_segment_assignment != NULL)
4956 new_segment
4957 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4958 last_hdr,
4959 new_segment);
2889e75b 4960
8ded5a0f
AM
4961 if (! new_segment)
4962 {
4963 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4964 writable = true;
2888249f 4965 if ((hdr->flags & SEC_CODE) != 0)
0a1b45a2 4966 executable = true;
8ded5a0f
AM
4967 last_hdr = hdr;
4968 /* .tbss sections effectively have zero size. */
502794d4 4969 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4970 continue;
4971 }
4972
4973 /* We need a new program segment. We must create a new program
00bee008 4974 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4975
00bee008 4976 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4977 if (m == NULL)
4978 goto error_return;
4979
4980 *pm = m;
4981 pm = &m->next;
4982
252b5132 4983 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4984 writable = true;
8ded5a0f 4985 else
0a1b45a2 4986 writable = false;
8ded5a0f 4987
2888249f 4988 if ((hdr->flags & SEC_CODE) == 0)
0a1b45a2 4989 executable = false;
2888249f 4990 else
0a1b45a2 4991 executable = true;
2888249f 4992
baaff79e
JJ
4993 last_hdr = hdr;
4994 /* .tbss sections effectively have zero size. */
502794d4 4995 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4996 hdr_index = i;
0a1b45a2 4997 phdr_in_segment = false;
252b5132
RH
4998 }
4999
86b2281f
AM
5000 /* Create a final PT_LOAD program segment, but not if it's just
5001 for .tbss. */
5002 if (last_hdr != NULL
00bee008 5003 && (i - hdr_index != 1
dbc88fc1 5004 || !IS_TBSS (last_hdr)))
8ded5a0f 5005 {
00bee008 5006 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
5007 if (m == NULL)
5008 goto error_return;
252b5132 5009
8ded5a0f
AM
5010 *pm = m;
5011 pm = &m->next;
5012 }
252b5132 5013
8ded5a0f
AM
5014 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5015 if (dynsec != NULL)
5016 {
5017 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5018 if (m == NULL)
5019 goto error_return;
5020 *pm = m;
5021 pm = &m->next;
5022 }
252b5132 5023
23e463ed 5024 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
5025 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5026 because if we link together nonloadable .note sections and
5027 loadable .note sections, we will generate two .note sections
23e463ed 5028 in the output file. */
8ded5a0f
AM
5029 for (s = abfd->sections; s != NULL; s = s->next)
5030 {
5031 if ((s->flags & SEC_LOAD) != 0
23e463ed 5032 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5033 {
1c5265b5 5034 asection *s2;
23e463ed 5035 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5036
5037 count = 1;
23e463ed
L
5038 for (s2 = s; s2->next != NULL; s2 = s2->next)
5039 {
5040 if (s2->next->alignment_power == alignment_power
5041 && (s2->next->flags & SEC_LOAD) != 0
5042 && elf_section_type (s2->next) == SHT_NOTE
66631823 5043 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5044 alignment_power)
5045 == s2->next->lma)
5046 count++;
5047 else
5048 break;
5049 }
00bee008
AM
5050 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5051 amt += count * sizeof (asection *);
a50b1753 5052 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5053 if (m == NULL)
5054 goto error_return;
5055 m->next = NULL;
5056 m->p_type = PT_NOTE;
1c5265b5
JJ
5057 m->count = count;
5058 while (count > 1)
5059 {
5060 m->sections[m->count - count--] = s;
5061 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5062 s = s->next;
5063 }
5064 m->sections[m->count - 1] = s;
5065 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5066 *pm = m;
5067 pm = &m->next;
5068 }
5069 if (s->flags & SEC_THREAD_LOCAL)
5070 {
5071 if (! tls_count)
5072 first_tls = s;
5073 tls_count++;
5074 }
a91e1603
L
5075 if (first_mbind == NULL
5076 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5077 first_mbind = s;
8ded5a0f 5078 }
252b5132 5079
8ded5a0f
AM
5080 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5081 if (tls_count > 0)
5082 {
00bee008
AM
5083 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5084 amt += tls_count * sizeof (asection *);
a50b1753 5085 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5086 if (m == NULL)
5087 goto error_return;
5088 m->next = NULL;
5089 m->p_type = PT_TLS;
5090 m->count = tls_count;
5091 /* Mandated PF_R. */
5092 m->p_flags = PF_R;
5093 m->p_flags_valid = 1;
d923cae0 5094 s = first_tls;
446f7ed5 5095 for (i = 0; i < tls_count; ++i)
8ded5a0f 5096 {
d923cae0
L
5097 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5098 {
5099 _bfd_error_handler
871b3ab2 5100 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5101 s = first_tls;
5102 i = 0;
446f7ed5 5103 while (i < tls_count)
d923cae0
L
5104 {
5105 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5106 {
871b3ab2 5107 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5108 i++;
5109 }
5110 else
871b3ab2 5111 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5112 s = s->next;
5113 }
5114 bfd_set_error (bfd_error_bad_value);
5115 goto error_return;
5116 }
5117 m->sections[i] = s;
5118 s = s->next;
8ded5a0f 5119 }
252b5132 5120
8ded5a0f
AM
5121 *pm = m;
5122 pm = &m->next;
5123 }
252b5132 5124
df3a023b
AM
5125 if (first_mbind
5126 && (abfd->flags & D_PAGED) != 0
5127 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5128 for (s = first_mbind; s != NULL; s = s->next)
5129 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5130 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5131 {
5132 /* Mandated PF_R. */
5133 unsigned long p_flags = PF_R;
5134 if ((s->flags & SEC_READONLY) == 0)
5135 p_flags |= PF_W;
5136 if ((s->flags & SEC_CODE) != 0)
5137 p_flags |= PF_X;
5138
5139 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5140 m = bfd_zalloc (abfd, amt);
5141 if (m == NULL)
5142 goto error_return;
5143 m->next = NULL;
5144 m->p_type = (PT_GNU_MBIND_LO
5145 + elf_section_data (s)->this_hdr.sh_info);
5146 m->count = 1;
5147 m->p_flags_valid = 1;
5148 m->sections[0] = s;
5149 m->p_flags = p_flags;
5150
5151 *pm = m;
5152 pm = &m->next;
5153 }
5154
0a59decb
L
5155 s = bfd_get_section_by_name (abfd,
5156 NOTE_GNU_PROPERTY_SECTION_NAME);
5157 if (s != NULL && s->size != 0)
5158 {
5159 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5160 m = bfd_zalloc (abfd, amt);
5161 if (m == NULL)
5162 goto error_return;
5163 m->next = NULL;
5164 m->p_type = PT_GNU_PROPERTY;
5165 m->count = 1;
5166 m->p_flags_valid = 1;
5167 m->sections[0] = s;
5168 m->p_flags = PF_R;
5169 *pm = m;
5170 pm = &m->next;
5171 }
5172
8ded5a0f
AM
5173 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5174 segment. */
8df52eee 5175 eh_frame_hdr = elf_eh_frame_hdr (info);
8ded5a0f
AM
5176 if (eh_frame_hdr != NULL
5177 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5178 {
dc810e39 5179 amt = sizeof (struct elf_segment_map);
a50b1753 5180 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5181 if (m == NULL)
5182 goto error_return;
5183 m->next = NULL;
8ded5a0f 5184 m->p_type = PT_GNU_EH_FRAME;
252b5132 5185 m->count = 1;
8ded5a0f 5186 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5187
5188 *pm = m;
5189 pm = &m->next;
5190 }
13ae64f3 5191
12bd6957 5192 if (elf_stack_flags (abfd))
13ae64f3 5193 {
8ded5a0f 5194 amt = sizeof (struct elf_segment_map);
a50b1753 5195 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5196 if (m == NULL)
5197 goto error_return;
5198 m->next = NULL;
2b05f1b7 5199 m->p_type = PT_GNU_STACK;
12bd6957 5200 m->p_flags = elf_stack_flags (abfd);
04c3a755 5201 m->p_align = bed->stack_align;
8ded5a0f 5202 m->p_flags_valid = 1;
04c3a755
NS
5203 m->p_align_valid = m->p_align != 0;
5204 if (info->stacksize > 0)
5205 {
5206 m->p_size = info->stacksize;
5207 m->p_size_valid = 1;
5208 }
252b5132 5209
8ded5a0f
AM
5210 *pm = m;
5211 pm = &m->next;
5212 }
65765700 5213
ceae84aa 5214 if (info != NULL && info->relro)
8ded5a0f 5215 {
f210dcff
L
5216 for (m = mfirst; m != NULL; m = m->next)
5217 {
3832a4d8
AM
5218 if (m->p_type == PT_LOAD
5219 && m->count != 0
5220 && m->sections[0]->vma >= info->relro_start
5221 && m->sections[0]->vma < info->relro_end)
f210dcff 5222 {
3832a4d8
AM
5223 i = m->count;
5224 while (--i != (unsigned) -1)
ec2e748a
NC
5225 {
5226 if (m->sections[i]->size > 0
11c6a7c6
AM
5227 && (m->sections[i]->flags & SEC_LOAD) != 0
5228 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
ec2e748a
NC
5229 break;
5230 }
3832a4d8 5231
43a8475c 5232 if (i != (unsigned) -1)
f210dcff
L
5233 break;
5234 }
be01b344 5235 }
f210dcff
L
5236
5237 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5238 if (m != NULL)
5239 {
5240 amt = sizeof (struct elf_segment_map);
a50b1753 5241 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5242 if (m == NULL)
5243 goto error_return;
5244 m->next = NULL;
5245 m->p_type = PT_GNU_RELRO;
f210dcff
L
5246 *pm = m;
5247 pm = &m->next;
5248 }
8ded5a0f 5249 }
9ee5e499 5250
8ded5a0f 5251 free (sections);
12bd6957 5252 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5253 }
5254
3dea8fca 5255 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
0a1b45a2 5256 return false;
8c37241b 5257
12bd6957 5258 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5259 ++count;
12bd6957 5260 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5261
0a1b45a2 5262 return true;
252b5132
RH
5263
5264 error_return:
c9594989 5265 free (sections);
0a1b45a2 5266 return false;
252b5132
RH
5267}
5268
5269/* Sort sections by address. */
5270
5271static int
217aa764 5272elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5273{
5274 const asection *sec1 = *(const asection **) arg1;
5275 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5276 bfd_size_type size1, size2;
252b5132
RH
5277
5278 /* Sort by LMA first, since this is the address used to
5279 place the section into a segment. */
5280 if (sec1->lma < sec2->lma)
5281 return -1;
5282 else if (sec1->lma > sec2->lma)
5283 return 1;
5284
5285 /* Then sort by VMA. Normally the LMA and the VMA will be
5286 the same, and this will do nothing. */
5287 if (sec1->vma < sec2->vma)
5288 return -1;
5289 else if (sec1->vma > sec2->vma)
5290 return 1;
5291
5292 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5293
8d748d1d
AM
5294#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5295 && (x)->size != 0)
252b5132
RH
5296
5297 if (TOEND (sec1))
5298 {
48db3297 5299 if (!TOEND (sec2))
252b5132
RH
5300 return 1;
5301 }
00a7cdc5 5302 else if (TOEND (sec2))
252b5132
RH
5303 return -1;
5304
5305#undef TOEND
5306
00a7cdc5
NC
5307 /* Sort by size, to put zero sized sections
5308 before others at the same address. */
252b5132 5309
eea6121a
AM
5310 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5311 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5312
5313 if (size1 < size2)
252b5132 5314 return -1;
eecdbe52 5315 if (size1 > size2)
252b5132
RH
5316 return 1;
5317
5318 return sec1->target_index - sec2->target_index;
5319}
5320
30fe1832
AM
5321/* This qsort comparison functions sorts PT_LOAD segments first and
5322 by p_paddr, for assign_file_positions_for_load_sections. */
5323
5324static int
5325elf_sort_segments (const void *arg1, const void *arg2)
5326{
5327 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5328 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5329
5330 if (m1->p_type != m2->p_type)
5331 {
5332 if (m1->p_type == PT_NULL)
5333 return 1;
5334 if (m2->p_type == PT_NULL)
5335 return -1;
5336 return m1->p_type < m2->p_type ? -1 : 1;
5337 }
5338 if (m1->includes_filehdr != m2->includes_filehdr)
5339 return m1->includes_filehdr ? -1 : 1;
5340 if (m1->no_sort_lma != m2->no_sort_lma)
5341 return m1->no_sort_lma ? -1 : 1;
5342 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5343 {
4b3ecb3b 5344 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5345 lma1 = 0;
5346 if (m1->p_paddr_valid)
4b3ecb3b 5347 lma1 = m1->p_paddr;
30fe1832 5348 else if (m1->count != 0)
4b3ecb3b
AM
5349 {
5350 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5351 m1->sections[0]);
5352 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5353 }
30fe1832
AM
5354 lma2 = 0;
5355 if (m2->p_paddr_valid)
4b3ecb3b 5356 lma2 = m2->p_paddr;
30fe1832 5357 else if (m2->count != 0)
4b3ecb3b
AM
5358 {
5359 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5360 m2->sections[0]);
5361 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5362 }
30fe1832
AM
5363 if (lma1 != lma2)
5364 return lma1 < lma2 ? -1 : 1;
5365 }
5366 if (m1->idx != m2->idx)
5367 return m1->idx < m2->idx ? -1 : 1;
5368 return 0;
5369}
5370
340b6d91
AC
5371/* Ian Lance Taylor writes:
5372
5373 We shouldn't be using % with a negative signed number. That's just
5374 not good. We have to make sure either that the number is not
5375 negative, or that the number has an unsigned type. When the types
5376 are all the same size they wind up as unsigned. When file_ptr is a
5377 larger signed type, the arithmetic winds up as signed long long,
5378 which is wrong.
5379
5380 What we're trying to say here is something like ``increase OFF by
5381 the least amount that will cause it to be equal to the VMA modulo
5382 the page size.'' */
5383/* In other words, something like:
5384
5385 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5386 off_offset = off % bed->maxpagesize;
5387 if (vma_offset < off_offset)
5388 adjustment = vma_offset + bed->maxpagesize - off_offset;
5389 else
5390 adjustment = vma_offset - off_offset;
08a40648 5391
de194d85 5392 which can be collapsed into the expression below. */
340b6d91
AC
5393
5394static file_ptr
5395vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5396{
dc9155b2
NC
5397 /* PR binutils/16199: Handle an alignment of zero. */
5398 if (maxpagesize == 0)
5399 maxpagesize = 1;
340b6d91
AC
5400 return ((vma - off) % maxpagesize);
5401}
5402
6d33f217
L
5403static void
5404print_segment_map (const struct elf_segment_map *m)
5405{
5406 unsigned int j;
5407 const char *pt = get_segment_type (m->p_type);
5408 char buf[32];
5409
5410 if (pt == NULL)
5411 {
5412 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5413 sprintf (buf, "LOPROC+%7.7x",
5414 (unsigned int) (m->p_type - PT_LOPROC));
5415 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5416 sprintf (buf, "LOOS+%7.7x",
5417 (unsigned int) (m->p_type - PT_LOOS));
5418 else
5419 snprintf (buf, sizeof (buf), "%8.8x",
5420 (unsigned int) m->p_type);
5421 pt = buf;
5422 }
4a97a0e5 5423 fflush (stdout);
6d33f217
L
5424 fprintf (stderr, "%s:", pt);
5425 for (j = 0; j < m->count; j++)
5426 fprintf (stderr, " %s", m->sections [j]->name);
5427 putc ('\n',stderr);
4a97a0e5 5428 fflush (stderr);
6d33f217
L
5429}
5430
0a1b45a2 5431static bool
32812159
AM
5432write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5433{
5434 void *buf;
0a1b45a2 5435 bool ret;
32812159
AM
5436
5437 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
0a1b45a2 5438 return false;
32812159
AM
5439 buf = bfd_zmalloc (len);
5440 if (buf == NULL)
0a1b45a2 5441 return false;
32812159
AM
5442 ret = bfd_bwrite (buf, len, abfd) == len;
5443 free (buf);
5444 return ret;
5445}
5446
252b5132
RH
5447/* Assign file positions to the sections based on the mapping from
5448 sections to segments. This function also sets up some fields in
f3520d2f 5449 the file header. */
252b5132 5450
0a1b45a2 5451static bool
f3520d2f
AM
5452assign_file_positions_for_load_sections (bfd *abfd,
5453 struct bfd_link_info *link_info)
252b5132
RH
5454{
5455 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5456 struct elf_segment_map *m;
30fe1832 5457 struct elf_segment_map *phdr_load_seg;
252b5132 5458 Elf_Internal_Phdr *phdrs;
252b5132 5459 Elf_Internal_Phdr *p;
502794d4 5460 file_ptr off; /* Octets. */
3f570048 5461 bfd_size_type maxpagesize;
30fe1832 5462 unsigned int alloc, actual;
0920dee7 5463 unsigned int i, j;
30fe1832 5464 struct elf_segment_map **sorted_seg_map;
502794d4 5465 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5466
e36284ab 5467 if (link_info == NULL
e2cbf4df 5468 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
0a1b45a2 5469 return false;
252b5132 5470
8ded5a0f 5471 alloc = 0;
12bd6957 5472 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5473 m->idx = alloc++;
252b5132 5474
82f2dbf7
NC
5475 if (alloc)
5476 {
5477 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5478 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5479 }
5480 else
5481 {
5482 /* PR binutils/12467. */
5483 elf_elfheader (abfd)->e_phoff = 0;
5484 elf_elfheader (abfd)->e_phentsize = 0;
5485 }
d324f6d6 5486
8ded5a0f 5487 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5488
12bd6957 5489 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5490 {
5491 actual = alloc;
5492 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5493 }
8ded5a0f 5494 else
30fe1832
AM
5495 {
5496 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5497 BFD_ASSERT (elf_program_header_size (abfd)
5498 == actual * bed->s->sizeof_phdr);
5499 BFD_ASSERT (actual >= alloc);
5500 }
252b5132
RH
5501
5502 if (alloc == 0)
f3520d2f 5503 {
12bd6957 5504 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
0a1b45a2 5505 return true;
f3520d2f 5506 }
252b5132 5507
12bd6957 5508 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5509 see assign_file_positions_except_relocs, so make sure we have
5510 that amount allocated, with trailing space cleared.
12bd6957
AM
5511 The variable alloc contains the computed need, while
5512 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5513 layout.
5514 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5515 where the layout is forced to according to a larger size in the
5516 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5517 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5518 + alloc * sizeof (*sorted_seg_map)));
5519 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5520 elf_tdata (abfd)->phdr = phdrs;
252b5132 5521 if (phdrs == NULL)
0a1b45a2 5522 return false;
252b5132 5523
30fe1832 5524 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5525 {
30fe1832 5526 sorted_seg_map[j] = m;
252b5132 5527 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5528 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5529 not be done to the PT_NOTE section of a corefile, which may
5530 contain several pseudo-sections artificially created by bfd.
5531 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5532 if (m->count > 1
5533 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5534 && m->p_type == PT_NOTE))
48db3297
AM
5535 {
5536 for (i = 0; i < m->count; i++)
5537 m->sections[i]->target_index = i;
5538 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5539 elf_sort_sections);
5540 }
30fe1832
AM
5541 }
5542 if (alloc > 1)
5543 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5544 elf_sort_segments);
5545
5546 maxpagesize = 1;
5547 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5548 {
5549 if (link_info != NULL)
5550 maxpagesize = link_info->maxpagesize;
5551 else
5552 maxpagesize = bed->maxpagesize;
5553 }
30fe1832
AM
5554
5555 /* Sections must map to file offsets past the ELF file header. */
5556 off = bed->s->sizeof_ehdr;
5557 /* And if one of the PT_LOAD headers doesn't include the program
5558 headers then we'll be mapping program headers in the usual
5559 position after the ELF file header. */
5560 phdr_load_seg = NULL;
5561 for (j = 0; j < alloc; j++)
5562 {
5563 m = sorted_seg_map[j];
5564 if (m->p_type != PT_LOAD)
5565 break;
5566 if (m->includes_phdrs)
5567 {
5568 phdr_load_seg = m;
5569 break;
5570 }
5571 }
5572 if (phdr_load_seg == NULL)
5573 off += actual * bed->s->sizeof_phdr;
5574
5575 for (j = 0; j < alloc; j++)
5576 {
5577 asection **secpp;
502794d4 5578 bfd_vma off_adjust; /* Octets. */
0a1b45a2 5579 bool no_contents;
59f21454
L
5580 bfd_size_type p_align;
5581 bool p_align_p;
252b5132 5582
b301b248
AM
5583 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5584 number of sections with contents contributing to both p_filesz
5585 and p_memsz, followed by a number of sections with no contents
5586 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5587 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5588 m = sorted_seg_map[j];
5589 p = phdrs + m->idx;
252b5132 5590 p->p_type = m->p_type;
28a7f3e7 5591 p->p_flags = m->p_flags;
59f21454
L
5592 p_align = bed->p_align;
5593 p_align_p = false;
252b5132 5594
3f570048 5595 if (m->count == 0)
502794d4 5596 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5597 else
502794d4 5598 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5599
5600 if (m->p_paddr_valid)
5601 p->p_paddr = m->p_paddr;
5602 else if (m->count == 0)
5603 p->p_paddr = 0;
5604 else
502794d4 5605 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5606
5607 if (p->p_type == PT_LOAD
5608 && (abfd->flags & D_PAGED) != 0)
5609 {
5610 /* p_align in demand paged PT_LOAD segments effectively stores
5611 the maximum page size. When copying an executable with
5612 objcopy, we set m->p_align from the input file. Use this
5613 value for maxpagesize rather than bed->maxpagesize, which
5614 may be different. Note that we use maxpagesize for PT_TLS
5615 segment alignment later in this function, so we are relying
5616 on at least one PT_LOAD segment appearing before a PT_TLS
5617 segment. */
5618 if (m->p_align_valid)
5619 maxpagesize = m->p_align;
74e315db
L
5620 else if (p_align != 0
5621 && (link_info == NULL
5622 || !link_info->maxpagesize_is_set))
5623 /* Set p_align to the default p_align value while laying
5624 out segments aligning to the maximum page size or the
5625 largest section alignment. The run-time loader can
5626 align segments to the default p_align value or the
5627 maximum page size, depending on system page size. */
5628 p_align_p = true;
3f570048
AM
5629
5630 p->p_align = maxpagesize;
5631 }
3271a814
NS
5632 else if (m->p_align_valid)
5633 p->p_align = m->p_align;
e970b90a
DJ
5634 else if (m->count == 0)
5635 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5636
5637 if (m == phdr_load_seg)
5638 {
5639 if (!m->includes_filehdr)
5640 p->p_offset = off;
5641 off += actual * bed->s->sizeof_phdr;
5642 }
3f570048 5643
0a1b45a2 5644 no_contents = false;
bf988460 5645 off_adjust = 0;
252b5132 5646 if (p->p_type == PT_LOAD
b301b248 5647 && m->count > 0)
252b5132 5648 {
66631823 5649 bfd_size_type align; /* Bytes. */
a49e53ed 5650 unsigned int align_power = 0;
b301b248 5651
3271a814
NS
5652 if (m->p_align_valid)
5653 align = p->p_align;
5654 else
252b5132 5655 {
3271a814
NS
5656 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5657 {
5658 unsigned int secalign;
08a40648 5659
fd361982 5660 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5661 if (secalign > align_power)
5662 align_power = secalign;
5663 }
5664 align = (bfd_size_type) 1 << align_power;
5665 if (align < maxpagesize)
74e315db
L
5666 {
5667 /* If a section requires alignment higher than the
5668 default p_align value, don't set p_align to the
5669 default p_align value. */
5670 if (align > p_align)
5671 p_align_p = false;
5672 align = maxpagesize;
5673 }
5674 else
5675 {
5676 /* If a section requires alignment higher than the
5677 maximum page size, set p_align to the section
5678 alignment. */
5679 p_align_p = true;
5680 p_align = align;
5681 }
b301b248 5682 }
252b5132 5683
02bf8d82
AM
5684 for (i = 0; i < m->count; i++)
5685 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5686 /* If we aren't making room for this section, then
5687 it must be SHT_NOBITS regardless of what we've
5688 set via struct bfd_elf_special_section. */
5689 elf_section_type (m->sections[i]) = SHT_NOBITS;
5690
bf988460 5691 /* Find out whether this segment contains any loadable
aea274d3 5692 sections. */
0a1b45a2 5693 no_contents = true;
aea274d3
AM
5694 for (i = 0; i < m->count; i++)
5695 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5696 {
0a1b45a2 5697 no_contents = false;
aea274d3
AM
5698 break;
5699 }
bf988460 5700
66631823 5701 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5702
5703 /* Broken hardware and/or kernel require that files do not
5704 map the same page with different permissions on some hppa
5705 processors. */
30fe1832
AM
5706 if (j != 0
5707 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5708 && bed->no_page_alias
5709 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5710 && ((off & -maxpagesize)
5711 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5712 off_adjust += maxpagesize;
bf988460
AM
5713 off += off_adjust;
5714 if (no_contents)
5715 {
5716 /* We shouldn't need to align the segment on disk since
5717 the segment doesn't need file space, but the gABI
5718 arguably requires the alignment and glibc ld.so
5719 checks it. So to comply with the alignment
5720 requirement but not waste file space, we adjust
5721 p_offset for just this segment. (OFF_ADJUST is
5722 subtracted from OFF later.) This may put p_offset
5723 past the end of file, but that shouldn't matter. */
5724 }
5725 else
5726 off_adjust = 0;
252b5132 5727 }
b1a6d0b1
NC
5728 /* Make sure the .dynamic section is the first section in the
5729 PT_DYNAMIC segment. */
5730 else if (p->p_type == PT_DYNAMIC
5731 && m->count > 1
5732 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5733 {
5734 _bfd_error_handler
871b3ab2 5735 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5736 " is not the .dynamic section"),
b301b248 5737 abfd);
b1a6d0b1 5738 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5739 return false;
b1a6d0b1 5740 }
3f001e84
JK
5741 /* Set the note section type to SHT_NOTE. */
5742 else if (p->p_type == PT_NOTE)
5743 for (i = 0; i < m->count; i++)
5744 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5745
252b5132
RH
5746 if (m->includes_filehdr)
5747 {
bf988460 5748 if (!m->p_flags_valid)
252b5132 5749 p->p_flags |= PF_R;
252b5132
RH
5750 p->p_filesz = bed->s->sizeof_ehdr;
5751 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5752 if (p->p_type == PT_LOAD)
252b5132 5753 {
30fe1832 5754 if (m->count > 0)
252b5132 5755 {
30fe1832
AM
5756 if (p->p_vaddr < (bfd_vma) off
5757 || (!m->p_paddr_valid
5758 && p->p_paddr < (bfd_vma) off))
5759 {
5760 _bfd_error_handler
5761 (_("%pB: not enough room for program headers,"
5762 " try linking with -N"),
5763 abfd);
5764 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5765 return false;
30fe1832
AM
5766 }
5767 p->p_vaddr -= off;
5768 if (!m->p_paddr_valid)
5769 p->p_paddr -= off;
252b5132 5770 }
30fe1832
AM
5771 }
5772 else if (sorted_seg_map[0]->includes_filehdr)
5773 {
5774 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5775 p->p_vaddr = filehdr->p_vaddr;
bf988460 5776 if (!m->p_paddr_valid)
30fe1832 5777 p->p_paddr = filehdr->p_paddr;
252b5132 5778 }
252b5132
RH
5779 }
5780
5781 if (m->includes_phdrs)
5782 {
bf988460 5783 if (!m->p_flags_valid)
252b5132 5784 p->p_flags |= PF_R;
30fe1832
AM
5785 p->p_filesz += actual * bed->s->sizeof_phdr;
5786 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5787 if (!m->includes_filehdr)
252b5132 5788 {
30fe1832 5789 if (p->p_type == PT_LOAD)
252b5132 5790 {
30fe1832
AM
5791 elf_elfheader (abfd)->e_phoff = p->p_offset;
5792 if (m->count > 0)
5793 {
5794 p->p_vaddr -= off - p->p_offset;
5795 if (!m->p_paddr_valid)
5796 p->p_paddr -= off - p->p_offset;
5797 }
5798 }
5799 else if (phdr_load_seg != NULL)
5800 {
5801 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5802 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5803 if (phdr_load_seg->includes_filehdr)
5804 phdr_off = bed->s->sizeof_ehdr;
5805 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5806 if (!m->p_paddr_valid)
30fe1832
AM
5807 p->p_paddr = phdr->p_paddr + phdr_off;
5808 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5809 }
30fe1832
AM
5810 else
5811 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5812 }
252b5132
RH
5813 }
5814
5815 if (p->p_type == PT_LOAD
5816 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5817 {
bf988460 5818 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5819 {
5820 p->p_offset = off;
5821 if (no_contents)
67641dd3
AM
5822 {
5823 /* Put meaningless p_offset for PT_LOAD segments
5824 without file contents somewhere within the first
5825 page, in an attempt to not point past EOF. */
5826 bfd_size_type align = maxpagesize;
5827 if (align < p->p_align)
5828 align = p->p_align;
5829 if (align < 1)
5830 align = 1;
5831 p->p_offset = off % align;
5832 }
0bc3450e 5833 }
252b5132
RH
5834 else
5835 {
502794d4 5836 file_ptr adjust; /* Octets. */
252b5132
RH
5837
5838 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5839 if (!no_contents)
5840 p->p_filesz += adjust;
252b5132
RH
5841 p->p_memsz += adjust;
5842 }
5843 }
5844
1ea63fd2
AM
5845 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5846 maps. Set filepos for sections in PT_LOAD segments, and in
5847 core files, for sections in PT_NOTE segments.
5848 assign_file_positions_for_non_load_sections will set filepos
5849 for other sections and update p_filesz for other segments. */
252b5132
RH
5850 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5851 {
5852 asection *sec;
252b5132 5853 bfd_size_type align;
627b32bc 5854 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5855
5856 sec = *secpp;
02bf8d82 5857 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5858 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5859
88967714
AM
5860 if ((p->p_type == PT_LOAD
5861 || p->p_type == PT_TLS)
5862 && (this_hdr->sh_type != SHT_NOBITS
5863 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5864 && ((this_hdr->sh_flags & SHF_TLS) == 0
5865 || p->p_type == PT_TLS))))
252b5132 5866 {
11c6a7c6
AM
5867 bfd_vma p_start = p->p_paddr; /* Octets. */
5868 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5869 bfd_vma s_start = sec->lma * opb; /* Octets. */
5870 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5871
a2d1e028
L
5872 if (adjust != 0
5873 && (s_start < p_end
5874 || p_end < p_start))
252b5132 5875 {
4eca0228 5876 _bfd_error_handler
695344c0 5877 /* xgettext:c-format */
11c6a7c6
AM
5878 (_("%pB: section %pA lma %#" PRIx64
5879 " adjusted to %#" PRIx64),
502794d4
CE
5880 abfd, sec, (uint64_t) s_start / opb,
5881 (uint64_t) p_end / opb);
88967714 5882 adjust = 0;
502794d4 5883 sec->lma = p_end / opb;
1cfb7d1e 5884 }
3ac9b6c9 5885 p->p_memsz += adjust;
1cfb7d1e 5886
d16e3d2e 5887 if (p->p_type == PT_LOAD)
88967714 5888 {
d16e3d2e 5889 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5890 {
d16e3d2e 5891 off_adjust = 0;
30fe1832
AM
5892 if (p->p_filesz + adjust < p->p_memsz)
5893 {
5894 /* We have a PROGBITS section following NOBITS ones.
5895 Allocate file space for the NOBITS section(s) and
5896 zero it. */
5897 adjust = p->p_memsz - p->p_filesz;
5898 if (!write_zeros (abfd, off, adjust))
0a1b45a2 5899 return false;
30fe1832 5900 }
d16e3d2e
AM
5901 }
5902 /* We only adjust sh_offset in SHT_NOBITS sections
5903 as would seem proper for their address when the
5904 section is first in the segment. sh_offset
5905 doesn't really have any significance for
5906 SHT_NOBITS anyway, apart from a notional position
5907 relative to other sections. Historically we
5908 didn't bother with adjusting sh_offset and some
5909 programs depend on it not being adjusted. See
5910 pr12921 and pr25662. */
5911 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5912 {
30fe1832 5913 off += adjust;
d16e3d2e
AM
5914 if (this_hdr->sh_type == SHT_NOBITS)
5915 off_adjust += adjust;
32812159 5916 }
252b5132 5917 }
d16e3d2e
AM
5918 if (this_hdr->sh_type != SHT_NOBITS)
5919 p->p_filesz += adjust;
252b5132
RH
5920 }
5921
5922 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5923 {
b301b248
AM
5924 /* The section at i == 0 is the one that actually contains
5925 everything. */
4a938328
MS
5926 if (i == 0)
5927 {
627b32bc 5928 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5929 off += this_hdr->sh_size;
5930 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5931 p->p_memsz = 0;
5932 p->p_align = 1;
252b5132 5933 }
4a938328 5934 else
252b5132 5935 {
b301b248 5936 /* The rest are fake sections that shouldn't be written. */
252b5132 5937 sec->filepos = 0;
eea6121a 5938 sec->size = 0;
b301b248
AM
5939 sec->flags = 0;
5940 continue;
252b5132 5941 }
252b5132
RH
5942 }
5943 else
5944 {
1e951488 5945 if (p->p_type == PT_LOAD)
b301b248 5946 {
1e951488
AM
5947 this_hdr->sh_offset = sec->filepos = off;
5948 if (this_hdr->sh_type != SHT_NOBITS)
5949 off += this_hdr->sh_size;
5950 }
5951 else if (this_hdr->sh_type == SHT_NOBITS
5952 && (this_hdr->sh_flags & SHF_TLS) != 0
5953 && this_hdr->sh_offset == 0)
5954 {
5955 /* This is a .tbss section that didn't get a PT_LOAD.
5956 (See _bfd_elf_map_sections_to_segments "Create a
5957 final PT_LOAD".) Set sh_offset to the value it
5958 would have if we had created a zero p_filesz and
5959 p_memsz PT_LOAD header for the section. This
5960 also makes the PT_TLS header have the same
5961 p_offset value. */
5962 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5963 off, align);
5964 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5965 }
252b5132 5966
02bf8d82 5967 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5968 {
6a3cd2b4 5969 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5970 /* A load section without SHF_ALLOC is something like
5971 a note section in a PT_NOTE segment. These take
5972 file space but are not loaded into memory. */
5973 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5974 p->p_memsz += this_hdr->sh_size;
b301b248 5975 }
6a3cd2b4 5976 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5977 {
6a3cd2b4
AM
5978 if (p->p_type == PT_TLS)
5979 p->p_memsz += this_hdr->sh_size;
5980
5981 /* .tbss is special. It doesn't contribute to p_memsz of
5982 normal segments. */
5983 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5984 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5985 }
5986
b10a8ae0
L
5987 if (align > p->p_align
5988 && !m->p_align_valid
5989 && (p->p_type != PT_LOAD
5990 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5991 p->p_align = align;
5992 }
5993
bf988460 5994 if (!m->p_flags_valid)
252b5132
RH
5995 {
5996 p->p_flags |= PF_R;
02bf8d82 5997 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5998 p->p_flags |= PF_X;
02bf8d82 5999 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
6000 p->p_flags |= PF_W;
6001 }
6002 }
43a8475c 6003
bf988460 6004 off -= off_adjust;
0920dee7 6005
30fe1832
AM
6006 /* PR ld/20815 - Check that the program header segment, if
6007 present, will be loaded into memory. */
6008 if (p->p_type == PT_PHDR
6009 && phdr_load_seg == NULL
6010 && !(bed->elf_backend_allow_non_load_phdr != NULL
6011 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6012 {
6013 /* The fix for this error is usually to edit the linker script being
6014 used and set up the program headers manually. Either that or
6015 leave room for the headers at the start of the SECTIONS. */
6016 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6017 " by LOAD segment"),
6018 abfd);
7b3c2715 6019 if (link_info == NULL)
0a1b45a2 6020 return false;
7b3c2715
AM
6021 /* Arrange for the linker to exit with an error, deleting
6022 the output file unless --noinhibit-exec is given. */
6023 link_info->callbacks->info ("%X");
30fe1832
AM
6024 }
6025
7c928300
AM
6026 /* Check that all sections are in a PT_LOAD segment.
6027 Don't check funky gdb generated core files. */
6028 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553 6029 {
0a1b45a2 6030 bool check_vma = true;
9a83a553
AM
6031
6032 for (i = 1; i < m->count; i++)
6033 if (m->sections[i]->vma == m->sections[i - 1]->vma
6034 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6035 ->this_hdr), p) != 0
6036 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6037 ->this_hdr), p) != 0)
0920dee7 6038 {
9a83a553 6039 /* Looks like we have overlays packed into the segment. */
0a1b45a2 6040 check_vma = false;
9a83a553 6041 break;
0920dee7 6042 }
9a83a553
AM
6043
6044 for (i = 0; i < m->count; i++)
6045 {
6046 Elf_Internal_Shdr *this_hdr;
6047 asection *sec;
6048
6049 sec = m->sections[i];
6050 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
6051 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6052 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 6053 {
4eca0228 6054 _bfd_error_handler
695344c0 6055 /* xgettext:c-format */
871b3ab2 6056 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
6057 abfd, sec, j);
6058 print_segment_map (m);
6059 }
6060 }
74e315db
L
6061
6062 if (p_align_p)
6063 p->p_align = p_align;
9a83a553 6064 }
252b5132
RH
6065 }
6066
12bd6957 6067 elf_next_file_pos (abfd) = off;
30fe1832
AM
6068
6069 if (link_info != NULL
6070 && phdr_load_seg != NULL
6071 && phdr_load_seg->includes_filehdr)
6072 {
6073 /* There is a segment that contains both the file headers and the
6074 program headers, so provide a symbol __ehdr_start pointing there.
6075 A program can use this to examine itself robustly. */
6076
6077 struct elf_link_hash_entry *hash
6078 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
0a1b45a2 6079 false, false, true);
30fe1832
AM
6080 /* If the symbol was referenced and not defined, define it. */
6081 if (hash != NULL
6082 && (hash->root.type == bfd_link_hash_new
6083 || hash->root.type == bfd_link_hash_undefined
6084 || hash->root.type == bfd_link_hash_undefweak
6085 || hash->root.type == bfd_link_hash_common))
6086 {
6087 asection *s = NULL;
66631823 6088 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6089
6090 if (phdr_load_seg->count != 0)
6091 /* The segment contains sections, so use the first one. */
6092 s = phdr_load_seg->sections[0];
6093 else
6094 /* Use the first (i.e. lowest-addressed) section in any segment. */
6095 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6096 if (m->p_type == PT_LOAD && m->count != 0)
6097 {
6098 s = m->sections[0];
6099 break;
6100 }
6101
6102 if (s != NULL)
6103 {
6104 hash->root.u.def.value = filehdr_vaddr - s->vma;
6105 hash->root.u.def.section = s;
6106 }
6107 else
6108 {
6109 hash->root.u.def.value = filehdr_vaddr;
6110 hash->root.u.def.section = bfd_abs_section_ptr;
6111 }
6112
6113 hash->root.type = bfd_link_hash_defined;
6114 hash->def_regular = 1;
6115 hash->non_elf = 0;
6116 }
6117 }
6118
0a1b45a2 6119 return true;
f3520d2f
AM
6120}
6121
1faa385f
NC
6122/* Determine if a bfd is a debuginfo file. Unfortunately there
6123 is no defined method for detecting such files, so we have to
6124 use heuristics instead. */
6125
0a1b45a2 6126bool
1faa385f
NC
6127is_debuginfo_file (bfd *abfd)
6128{
6129 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
0a1b45a2 6130 return false;
1faa385f
NC
6131
6132 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6133 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6134 Elf_Internal_Shdr **headerp;
6135
6136 for (headerp = start_headers; headerp < end_headers; headerp ++)
6137 {
6138 Elf_Internal_Shdr *header = * headerp;
6139
6140 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6141 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6142 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6143 && header->sh_type != SHT_NOBITS
6144 && header->sh_type != SHT_NOTE)
0a1b45a2 6145 return false;
1faa385f
NC
6146 }
6147
0a1b45a2 6148 return true;
1faa385f
NC
6149}
6150
11c6a7c6
AM
6151/* Assign file positions for other sections, except for compressed debug
6152 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
f3520d2f 6153
0a1b45a2 6154static bool
f3520d2f
AM
6155assign_file_positions_for_non_load_sections (bfd *abfd,
6156 struct bfd_link_info *link_info)
6157{
6158 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6159 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6160 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6161 Elf_Internal_Phdr *phdrs;
6162 Elf_Internal_Phdr *p;
6163 struct elf_segment_map *m;
f3520d2f 6164 file_ptr off;
66631823 6165 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6166 bfd_vma maxpagesize;
f3520d2f 6167
c410035d
AM
6168 if (link_info != NULL)
6169 maxpagesize = link_info->maxpagesize;
6170 else
6171 maxpagesize = bed->maxpagesize;
5c182d5f 6172 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6173 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6174 off = elf_next_file_pos (abfd);
e06efbf1 6175 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6176 {
5c182d5f 6177 Elf_Internal_Shdr *hdr;
1f9b1a84 6178 bfd_vma align;
5c182d5f
AM
6179
6180 hdr = *hdrpp;
6181 if (hdr->bfd_section != NULL
252e386e
AM
6182 && (hdr->bfd_section->filepos != 0
6183 || (hdr->sh_type == SHT_NOBITS
6184 && hdr->contents == NULL)))
627b32bc 6185 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6186 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6187 {
1faa385f
NC
6188 if (hdr->sh_size != 0
6189 /* PR 24717 - debuginfo files are known to be not strictly
6190 compliant with the ELF standard. In particular they often
6191 have .note.gnu.property sections that are outside of any
6192 loadable segment. This is not a problem for such files,
6193 so do not warn about them. */
6194 && ! is_debuginfo_file (abfd))
4eca0228 6195 _bfd_error_handler
695344c0 6196 /* xgettext:c-format */
871b3ab2 6197 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6198 abfd,
6199 (hdr->bfd_section == NULL
6200 ? "*unknown*"
6201 : hdr->bfd_section->name));
3ba71138
L
6202 /* We don't need to page align empty sections. */
6203 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
1f9b1a84 6204 align = maxpagesize;
5c182d5f 6205 else
1f9b1a84
AM
6206 align = hdr->sh_addralign & -hdr->sh_addralign;
6207 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
5c182d5f 6208 off = _bfd_elf_assign_file_position_for_section (hdr, off,
0a1b45a2 6209 false);
5c182d5f
AM
6210 }
6211 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6212 && hdr->bfd_section == NULL)
11c6a7c6
AM
6213 /* We don't know the offset of these sections yet:
6214 their size has not been decided. */
0ce398f1 6215 || (hdr->bfd_section != NULL
1ff6de03
NA
6216 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6217 || (bfd_section_is_ctf (hdr->bfd_section)
6218 && abfd->is_linker_output)))
12bd6957 6219 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6220 || (elf_symtab_shndx_list (abfd) != NULL
6221 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6222 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6223 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6224 hdr->sh_offset = -1;
6225 else
0a1b45a2 6226 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
5c182d5f 6227 }
30fe1832 6228 elf_next_file_pos (abfd) = off;
5c182d5f 6229
252b5132
RH
6230 /* Now that we have set the section file positions, we can set up
6231 the file positions for the non PT_LOAD segments. */
f3520d2f 6232 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6233 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6234 {
129af99f 6235 if (p->p_type == PT_GNU_RELRO)
252b5132 6236 {
66631823 6237 bfd_vma start, end; /* Bytes. */
0a1b45a2 6238 bool ok;
1ea63fd2 6239
129af99f 6240 if (link_info != NULL)
8c37241b 6241 {
129af99f 6242 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6243 in link_info. Note that there may be padding between
6244 relro_start and the first RELRO section. */
6245 start = link_info->relro_start;
6246 end = link_info->relro_end;
6247 }
6248 else if (m->count != 0)
6249 {
6250 if (!m->p_size_valid)
6251 abort ();
6252 start = m->sections[0]->vma;
66631823 6253 end = start + m->p_size / opb;
f2731e0c
AM
6254 }
6255 else
6256 {
6257 start = 0;
6258 end = 0;
6259 }
6260
0a1b45a2 6261 ok = false;
f2731e0c
AM
6262 if (start < end)
6263 {
6264 struct elf_segment_map *lm;
6265 const Elf_Internal_Phdr *lp;
6266 unsigned int i;
6267
6268 /* Find a LOAD segment containing a section in the RELRO
6269 segment. */
12bd6957 6270 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6271 lm != NULL;
6272 lm = lm->next, lp++)
8c37241b
JJ
6273 {
6274 if (lp->p_type == PT_LOAD
3146fac4 6275 && lm->count != 0
dbc88fc1
AM
6276 && (lm->sections[lm->count - 1]->vma
6277 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6278 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6279 : 0)) > start
f2731e0c 6280 && lm->sections[0]->vma < end)
8c37241b
JJ
6281 break;
6282 }
f2731e0c 6283
01f7e10c 6284 if (lm != NULL)
129af99f 6285 {
01f7e10c
AM
6286 /* Find the section starting the RELRO segment. */
6287 for (i = 0; i < lm->count; i++)
6288 {
6289 asection *s = lm->sections[i];
6290 if (s->vma >= start
6291 && s->vma < end
6292 && s->size != 0)
6293 break;
6294 }
6295
6296 if (i < lm->count)
6297 {
502794d4
CE
6298 p->p_vaddr = lm->sections[i]->vma * opb;
6299 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6300 p->p_offset = lm->sections[i]->filepos;
66631823 6301 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6302 p->p_filesz = p->p_memsz;
6303
6304 /* The RELRO segment typically ends a few bytes
6305 into .got.plt but other layouts are possible.
6306 In cases where the end does not match any
6307 loaded section (for instance is in file
6308 padding), trim p_filesz back to correspond to
6309 the end of loaded section contents. */
6310 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6311 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6312
6313 /* Preserve the alignment and flags if they are
6314 valid. The gold linker generates RW/4 for
6315 the PT_GNU_RELRO section. It is better for
6316 objcopy/strip to honor these attributes
6317 otherwise gdb will choke when using separate
6318 debug files. */
6319 if (!m->p_align_valid)
6320 p->p_align = 1;
6321 if (!m->p_flags_valid)
6322 p->p_flags = PF_R;
0a1b45a2 6323 ok = true;
01f7e10c 6324 }
129af99f 6325 }
b84a33b5 6326 }
ee9e412f 6327
01f7e10c 6328 if (!ok)
ee9e412f
NC
6329 {
6330 if (link_info != NULL)
6331 _bfd_error_handler
11c6a7c6
AM
6332 (_("%pB: warning: unable to allocate any sections"
6333 " to PT_GNU_RELRO segment"),
ee9e412f
NC
6334 abfd);
6335 memset (p, 0, sizeof *p);
6336 }
129af99f 6337 }
04c3a755
NS
6338 else if (p->p_type == PT_GNU_STACK)
6339 {
6340 if (m->p_size_valid)
6341 p->p_memsz = m->p_size;
6342 }
129af99f
AS
6343 else if (m->count != 0)
6344 {
e06efbf1 6345 unsigned int i;
1a9ccd70 6346
129af99f
AS
6347 if (p->p_type != PT_LOAD
6348 && (p->p_type != PT_NOTE
6349 || bfd_get_format (abfd) != bfd_core))
6350 {
1a9ccd70
NC
6351 /* A user specified segment layout may include a PHDR
6352 segment that overlaps with a LOAD segment... */
6353 if (p->p_type == PT_PHDR)
6354 {
6355 m->count = 0;
6356 continue;
6357 }
6358
c86934ce
NC
6359 if (m->includes_filehdr || m->includes_phdrs)
6360 {
b1fa9dd6 6361 /* PR 17512: file: 2195325e. */
4eca0228 6362 _bfd_error_handler
871b3ab2 6363 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6364 "and/or program header"),
6365 abfd, (int) (p - phdrs));
0a1b45a2 6366 return false;
c86934ce 6367 }
129af99f 6368
86b2281f 6369 p->p_filesz = 0;
129af99f 6370 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6371 for (i = m->count; i-- != 0;)
6372 {
6373 asection *sect = m->sections[i];
6374 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6375 if (hdr->sh_type != SHT_NOBITS)
6376 {
aebb3d54 6377 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
9917b559
L
6378 /* NB: p_memsz of the loadable PT_NOTE segment
6379 should be the same as p_filesz. */
6380 if (p->p_type == PT_NOTE
6381 && (hdr->sh_flags & SHF_ALLOC) != 0)
6382 p->p_memsz = p->p_filesz;
86b2281f
AM
6383 break;
6384 }
6385 }
129af99f
AS
6386 }
6387 }
252b5132
RH
6388 }
6389
0a1b45a2 6390 return true;
252b5132
RH
6391}
6392
6a40cf0c
NC
6393static elf_section_list *
6394find_section_in_list (unsigned int i, elf_section_list * list)
6395{
6396 for (;list != NULL; list = list->next)
6397 if (list->ndx == i)
6398 break;
6399 return list;
6400}
6401
252b5132
RH
6402/* Work out the file positions of all the sections. This is called by
6403 _bfd_elf_compute_section_file_positions. All the section sizes and
6404 VMAs must be known before this is called.
6405
e0638f70 6406 Reloc sections come in two flavours: Those processed specially as
11c6a7c6
AM
6407 "side-channel" data attached to a section to which they apply, and
6408 those that bfd doesn't process as relocations. The latter sort are
6409 stored in a normal bfd section by bfd_section_from_shdr. We don't
6410 consider the former sort here, unless they form part of the loadable
6411 image. Reloc sections not assigned here (and compressed debugging
6412 sections and CTF sections which nothing else in the file can rely
6413 upon) will be handled later by assign_file_positions_for_relocs.
252b5132
RH
6414
6415 We also don't set the positions of the .symtab and .strtab here. */
6416
0a1b45a2 6417static bool
c84fca4d
AO
6418assign_file_positions_except_relocs (bfd *abfd,
6419 struct bfd_link_info *link_info)
252b5132 6420{
5c182d5f
AM
6421 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6422 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6423 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6424 unsigned int alloc;
252b5132
RH
6425
6426 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6427 && bfd_get_format (abfd) != bfd_core)
6428 {
5c182d5f
AM
6429 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6430 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6431 Elf_Internal_Shdr **hdrpp;
6432 unsigned int i;
a485e98e 6433 file_ptr off;
252b5132
RH
6434
6435 /* Start after the ELF header. */
6436 off = i_ehdrp->e_ehsize;
6437
6438 /* We are not creating an executable, which means that we are
6439 not creating a program header, and that the actual order of
6440 the sections in the file is unimportant. */
9ad5cbcf 6441 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6442 {
6443 Elf_Internal_Shdr *hdr;
6444
6445 hdr = *hdrpp;
e0638f70
AM
6446 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6447 && hdr->bfd_section == NULL)
1ff6de03
NA
6448 /* Do not assign offsets for these sections yet: we don't know
6449 their sizes. */
0ce398f1 6450 || (hdr->bfd_section != NULL
1ff6de03
NA
6451 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6452 || (bfd_section_is_ctf (hdr->bfd_section)
6453 && abfd->is_linker_output)))
12bd6957 6454 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6455 || (elf_symtab_shndx_list (abfd) != NULL
6456 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6457 || i == elf_strtab_sec (abfd)
6458 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6459 {
6460 hdr->sh_offset = -1;
252b5132 6461 }
9ad5cbcf 6462 else
0a1b45a2 6463 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 6464 }
a485e98e
AM
6465
6466 elf_next_file_pos (abfd) = off;
6d6c25c8 6467 elf_program_header_size (abfd) = 0;
252b5132
RH
6468 }
6469 else
6470 {
252b5132 6471 /* Assign file positions for the loaded sections based on the
08a40648 6472 assignment of sections to segments. */
f3520d2f 6473 if (!assign_file_positions_for_load_sections (abfd, link_info))
0a1b45a2 6474 return false;
f3520d2f
AM
6475
6476 /* And for non-load sections. */
6477 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
0a1b45a2 6478 return false;
6d6c25c8 6479 }
f3520d2f 6480
6d6c25c8 6481 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
0a1b45a2 6482 return false;
1a9ccd70 6483
6d6c25c8
AM
6484 /* Write out the program headers. */
6485 alloc = i_ehdrp->e_phnum;
6486 if (alloc != 0)
6487 {
ba951afb
NC
6488 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6489 {
11c6a7c6
AM
6490 /* Memory resident segments with non-zero size and RWX
6491 permissions are a security risk, so we generate a warning
6492 here if we are creating any. */
ba951afb
NC
6493 unsigned int i;
6494
6495 for (i = 0; i < alloc; i++)
6496 {
6497 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6498
6499 if (phdr->p_memsz == 0)
6500 continue;
6501
6502 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
11c6a7c6
AM
6503 _bfd_error_handler (_("warning: %pB has a TLS segment"
6504 " with execute permission"),
ba951afb
NC
6505 abfd);
6506 else if (phdr->p_type == PT_LOAD
11c6a7c6
AM
6507 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6508 == (PF_R | PF_W | PF_X)))
6509 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6510 " with RWX permissions"),
ba951afb
NC
6511 abfd);
6512 }
6513 }
11c6a7c6 6514
30fe1832 6515 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857 6516 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
0a1b45a2 6517 return false;
252b5132
RH
6518 }
6519
0a1b45a2 6520 return true;
252b5132
RH
6521}
6522
0a1b45a2 6523bool
ed7e9d0b
AM
6524_bfd_elf_init_file_header (bfd *abfd,
6525 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6526{
3d540e93 6527 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6528 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6529 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6530
6531 i_ehdrp = elf_elfheader (abfd);
252b5132 6532
2b0f7ef9 6533 shstrtab = _bfd_elf_strtab_init ();
252b5132 6534 if (shstrtab == NULL)
0a1b45a2 6535 return false;
252b5132
RH
6536
6537 elf_shstrtab (abfd) = shstrtab;
6538
6539 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6540 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6541 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6542 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6543
6544 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6545 i_ehdrp->e_ident[EI_DATA] =
6546 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6547 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6548
252b5132
RH
6549 if ((abfd->flags & DYNAMIC) != 0)
6550 i_ehdrp->e_type = ET_DYN;
6551 else if ((abfd->flags & EXEC_P) != 0)
6552 i_ehdrp->e_type = ET_EXEC;
6553 else if (bfd_get_format (abfd) == bfd_core)
6554 i_ehdrp->e_type = ET_CORE;
6555 else
6556 i_ehdrp->e_type = ET_REL;
6557
6558 switch (bfd_get_arch (abfd))
6559 {
6560 case bfd_arch_unknown:
6561 i_ehdrp->e_machine = EM_NONE;
6562 break;
aa4f99bb
AO
6563
6564 /* There used to be a long list of cases here, each one setting
6565 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6566 in the corresponding bfd definition. To avoid duplication,
6567 the switch was removed. Machines that need special handling
6568 can generally do it in elf_backend_final_write_processing(),
6569 unless they need the information earlier than the final write.
6570 Such need can generally be supplied by replacing the tests for
6571 e_machine with the conditions used to determine it. */
252b5132 6572 default:
9c5bfbb7
AM
6573 i_ehdrp->e_machine = bed->elf_machine_code;
6574 }
aa4f99bb 6575
252b5132
RH
6576 i_ehdrp->e_version = bed->s->ev_current;
6577 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6578
c044fabd 6579 /* No program header, for now. */
252b5132
RH
6580 i_ehdrp->e_phoff = 0;
6581 i_ehdrp->e_phentsize = 0;
6582 i_ehdrp->e_phnum = 0;
6583
c044fabd 6584 /* Each bfd section is section header entry. */
252b5132
RH
6585 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6586 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6587
252b5132 6588 elf_tdata (abfd)->symtab_hdr.sh_name =
0a1b45a2 6589 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 6590 elf_tdata (abfd)->strtab_hdr.sh_name =
0a1b45a2 6591 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 6592 elf_tdata (abfd)->shstrtab_hdr.sh_name =
0a1b45a2 6593 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132 6594 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6595 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6596 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
0a1b45a2 6597 return false;
252b5132 6598
0a1b45a2 6599 return true;
252b5132
RH
6600}
6601
6d6c25c8
AM
6602/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6603
6604 FIXME: We used to have code here to sort the PT_LOAD segments into
6605 ascending order, as per the ELF spec. But this breaks some programs,
6606 including the Linux kernel. But really either the spec should be
6607 changed or the programs updated. */
6608
0a1b45a2 6609bool
6d6c25c8
AM
6610_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6611{
6612 if (link_info != NULL && bfd_link_pie (link_info))
6613 {
6614 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6615 unsigned int num_segments = i_ehdrp->e_phnum;
6616 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6617 Elf_Internal_Phdr *segment = tdata->phdr;
6618 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6619
6620 /* Find the lowest p_vaddr in PT_LOAD segments. */
6621 bfd_vma p_vaddr = (bfd_vma) -1;
6622 for (; segment < end_segment; segment++)
6623 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6624 p_vaddr = segment->p_vaddr;
6625
6626 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6627 segments is non-zero. */
6628 if (p_vaddr)
6629 i_ehdrp->e_type = ET_EXEC;
6630 }
0a1b45a2 6631 return true;
6d6c25c8
AM
6632}
6633
252b5132 6634/* Assign file positions for all the reloc sections which are not part
a485e98e 6635 of the loadable file image, and the file position of section headers. */
252b5132 6636
0a1b45a2 6637static bool
0ce398f1 6638_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6639{
6640 file_ptr off;
e06efbf1 6641 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6642 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6643 Elf_Internal_Ehdr *i_ehdrp;
6644 const struct elf_backend_data *bed;
252b5132 6645
12bd6957 6646 off = elf_next_file_pos (abfd);
252b5132 6647
e06efbf1
L
6648 shdrpp = elf_elfsections (abfd);
6649 end_shdrpp = shdrpp + elf_numsections (abfd);
6650 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6651 {
252b5132 6652 shdrp = *shdrpp;
0ce398f1
L
6653 if (shdrp->sh_offset == -1)
6654 {
3e19fb8f 6655 asection *sec = shdrp->bfd_section;
0a1b45a2
AM
6656 bool is_rel = (shdrp->sh_type == SHT_REL
6657 || shdrp->sh_type == SHT_RELA);
6658 bool is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6659 if (is_rel
1ff6de03 6660 || is_ctf
3e19fb8f 6661 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6662 {
1ff6de03 6663 if (!is_rel && !is_ctf)
0ce398f1 6664 {
3e19fb8f
L
6665 const char *name = sec->name;
6666 struct bfd_elf_section_data *d;
6667
0ce398f1 6668 /* Compress DWARF debug sections. */
3e19fb8f 6669 if (!bfd_compress_section (abfd, sec,
0ce398f1 6670 shdrp->contents))
0a1b45a2 6671 return false;
3e19fb8f
L
6672
6673 if (sec->compress_status == COMPRESS_SECTION_DONE
6674 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6675 {
6676 /* If section is compressed with zlib-gnu, convert
6677 section name from .debug_* to .zdebug_*. */
6678 char *new_name
6679 = convert_debug_to_zdebug (abfd, name);
6680 if (new_name == NULL)
0a1b45a2 6681 return false;
3e19fb8f
L
6682 name = new_name;
6683 }
dd905818 6684 /* Add section name to section name section. */
3e19fb8f
L
6685 if (shdrp->sh_name != (unsigned int) -1)
6686 abort ();
6687 shdrp->sh_name
6688 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 6689 name, false);
3e19fb8f
L
6690 d = elf_section_data (sec);
6691
dd905818 6692 /* Add reloc section name to section name section. */
3e19fb8f 6693 if (d->rel.hdr
11c6a7c6 6694 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
0a1b45a2
AM
6695 name, false))
6696 return false;
3e19fb8f 6697 if (d->rela.hdr
11c6a7c6 6698 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
0a1b45a2
AM
6699 name, true))
6700 return false;
3e19fb8f 6701
0ce398f1 6702 /* Update section size and contents. */
3e19fb8f
L
6703 shdrp->sh_size = sec->size;
6704 shdrp->contents = sec->contents;
0ce398f1
L
6705 shdrp->bfd_section->contents = NULL;
6706 }
1ff6de03
NA
6707 else if (is_ctf)
6708 {
6709 /* Update section size and contents. */
6710 shdrp->sh_size = sec->size;
6711 shdrp->contents = sec->contents;
6712 }
6713
11c6a7c6 6714 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
0a1b45a2 6715 true);
0ce398f1
L
6716 }
6717 }
252b5132
RH
6718 }
6719
3e19fb8f
L
6720 /* Place section name section after DWARF debug sections have been
6721 compressed. */
6722 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6723 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6724 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
0a1b45a2 6725 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3e19fb8f
L
6726
6727 /* Place the section headers. */
a485e98e
AM
6728 i_ehdrp = elf_elfheader (abfd);
6729 bed = get_elf_backend_data (abfd);
6730 off = align_file_position (off, 1 << bed->s->log_file_align);
6731 i_ehdrp->e_shoff = off;
6732 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6733 elf_next_file_pos (abfd) = off;
0ce398f1 6734
0a1b45a2 6735 return true;
252b5132
RH
6736}
6737
0a1b45a2 6738bool
217aa764 6739_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6740{
9c5bfbb7 6741 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6742 Elf_Internal_Shdr **i_shdrp;
0a1b45a2 6743 bool failed;
9ad5cbcf 6744 unsigned int count, num_sec;
30e8ee25 6745 struct elf_obj_tdata *t;
252b5132
RH
6746
6747 if (! abfd->output_has_begun
217aa764 6748 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 6749 return false;
db727370 6750 /* Do not rewrite ELF data when the BFD has been opened for update.
11c6a7c6
AM
6751 abfd->output_has_begun was set to TRUE on opening, so creation of
6752 new sections, and modification of existing section sizes was
6753 restricted. This means the ELF header, program headers and
6754 section headers can't have changed. If the contents of any
6755 sections has been modified, then those changes have already been
6756 written to the BFD. */
db727370
JL
6757 else if (abfd->direction == both_direction)
6758 {
6759 BFD_ASSERT (abfd->output_has_begun);
0a1b45a2 6760 return true;
db727370 6761 }
252b5132
RH
6762
6763 i_shdrp = elf_elfsections (abfd);
252b5132 6764
0a1b45a2 6765 failed = false;
252b5132
RH
6766 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6767 if (failed)
0a1b45a2 6768 return false;
252b5132 6769
0ce398f1 6770 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
0a1b45a2 6771 return false;
252b5132 6772
c044fabd 6773 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6774 num_sec = elf_numsections (abfd);
6775 for (count = 1; count < num_sec; count++)
252b5132 6776 {
3e19fb8f
L
6777 i_shdrp[count]->sh_name
6778 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6779 i_shdrp[count]->sh_name);
252b5132 6780 if (bed->elf_backend_section_processing)
75506100 6781 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
0a1b45a2 6782 return false;
252b5132
RH
6783 if (i_shdrp[count]->contents)
6784 {
dc810e39
AM
6785 bfd_size_type amt = i_shdrp[count]->sh_size;
6786
252b5132 6787 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6788 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
0a1b45a2 6789 return false;
252b5132
RH
6790 }
6791 }
6792
6793 /* Write out the section header names. */
30e8ee25 6794 t = elf_tdata (abfd);
26ae6d5e 6795 if (elf_shstrtab (abfd) != NULL
30e8ee25 6796 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6797 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
0a1b45a2 6798 return false;
252b5132 6799
cc364be6 6800 if (!(*bed->elf_backend_final_write_processing) (abfd))
0a1b45a2 6801 return false;
252b5132 6802
ff59fc36 6803 if (!bed->s->write_shdrs_and_ehdr (abfd))
0a1b45a2 6804 return false;
ff59fc36
RM
6805
6806 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
9e2bb0cb
LB
6807 if (t->o->build_id.after_write_object_contents != NULL
6808 && !(*t->o->build_id.after_write_object_contents) (abfd))
6809 return false;
6810 if (t->o->package_metadata.after_write_object_contents != NULL
6811 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6812 return false;
ff59fc36 6813
0a1b45a2 6814 return true;
252b5132
RH
6815}
6816
0a1b45a2 6817bool
217aa764 6818_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6819{
c044fabd 6820 /* Hopefully this can be done just like an object file. */
252b5132
RH
6821 return _bfd_elf_write_object_contents (abfd);
6822}
c044fabd
KH
6823
6824/* Given a section, search the header to find them. */
6825
cb33740c 6826unsigned int
198beae2 6827_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6828{
9c5bfbb7 6829 const struct elf_backend_data *bed;
91d6fa6a 6830 unsigned int sec_index;
252b5132 6831
9ad5cbcf
AM
6832 if (elf_section_data (asect) != NULL
6833 && elf_section_data (asect)->this_idx != 0)
6834 return elf_section_data (asect)->this_idx;
6835
6836 if (bfd_is_abs_section (asect))
91d6fa6a 6837 sec_index = SHN_ABS;
af746e92 6838 else if (bfd_is_com_section (asect))
91d6fa6a 6839 sec_index = SHN_COMMON;
af746e92 6840 else if (bfd_is_und_section (asect))
91d6fa6a 6841 sec_index = SHN_UNDEF;
af746e92 6842 else
91d6fa6a 6843 sec_index = SHN_BAD;
252b5132 6844
af746e92 6845 bed = get_elf_backend_data (abfd);
252b5132
RH
6846 if (bed->elf_backend_section_from_bfd_section)
6847 {
91d6fa6a 6848 int retval = sec_index;
9ad5cbcf 6849
af746e92
AM
6850 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6851 return retval;
252b5132
RH
6852 }
6853
91d6fa6a 6854 if (sec_index == SHN_BAD)
af746e92 6855 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6856
91d6fa6a 6857 return sec_index;
252b5132
RH
6858}
6859
6860/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6861 on error. */
6862
6863int
217aa764 6864_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6865{
6866 asymbol *asym_ptr = *asym_ptr_ptr;
6867 int idx;
6868 flagword flags = asym_ptr->flags;
6869
6870 /* When gas creates relocations against local labels, it creates its
6871 own symbol for the section, but does put the symbol into the
6872 symbol chain, so udata is 0. When the linker is generating
6873 relocatable output, this section symbol may be for one of the
6874 input sections rather than the output section. */
6875 if (asym_ptr->udata.i == 0
6876 && (flags & BSF_SECTION_SYM)
6877 && asym_ptr->section)
6878 {
5372391b 6879 asection *sec;
252b5132 6880
5372391b
AM
6881 sec = asym_ptr->section;
6882 if (sec->owner != abfd && sec->output_section != NULL)
6883 sec = sec->output_section;
6884 if (sec->owner == abfd
27e3da31
AM
6885 && sec->index < elf_num_section_syms (abfd)
6886 && elf_section_syms (abfd)[sec->index] != NULL)
6887 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
252b5132
RH
6888 }
6889
6890 idx = asym_ptr->udata.i;
6891
6892 if (idx == 0)
6893 {
6894 /* This case can occur when using --strip-symbol on a symbol
08a40648 6895 which is used in a relocation entry. */
4eca0228 6896 _bfd_error_handler
695344c0 6897 /* xgettext:c-format */
871b3ab2 6898 (_("%pB: symbol `%s' required but not present"),
d003868e 6899 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6900 bfd_set_error (bfd_error_no_symbols);
6901 return -1;
6902 }
6903
6904#if DEBUG & 4
6905 {
6906 fprintf (stderr,
11c6a7c6
AM
6907 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6908 " flags = 0x%.8x\n",
cd9af601 6909 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6910 fflush (stderr);
6911 }
6912#endif
6913
6914 return idx;
6915}
6916
45d92439
AM
6917static inline bfd_vma
6918segment_size (Elf_Internal_Phdr *segment)
6919{
6920 return (segment->p_memsz > segment->p_filesz
6921 ? segment->p_memsz : segment->p_filesz);
6922}
6923
6924
6925/* Returns the end address of the segment + 1. */
6926static inline bfd_vma
6927segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6928{
6929 return start + segment_size (segment);
6930}
6931
6932static inline bfd_size_type
6933section_size (asection *section, Elf_Internal_Phdr *segment)
6934{
6935 if ((section->flags & SEC_HAS_CONTENTS) != 0
6936 || (section->flags & SEC_THREAD_LOCAL) == 0
6937 || segment->p_type == PT_TLS)
6938 return section->size;
6939 return 0;
6940}
6941
6942/* Returns TRUE if the given section is contained within the given
6943 segment. LMA addresses are compared against PADDR when
6944 bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
6945static bool
6946is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6947 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6948 const struct elf_backend_data *bed)
6949{
6950 bfd_vma seg_addr = !bed->want_p_paddr_set_to_zero ? paddr : vaddr;
6951 bfd_vma addr = !bed->want_p_paddr_set_to_zero ? section->lma : section->vma;
6952 bfd_vma octet;
6953 if (_bfd_mul_overflow (addr, opb, &octet))
6954 return false;
6955 /* The third and fourth lines below are testing that the section end
6956 address is within the segment. It's written this way to avoid
6957 overflow. Add seg_addr + section_size to both sides of the
6958 inequality to make it obvious. */
6959 return (octet >= seg_addr
6960 && segment_size (segment) >= section_size (section, segment)
6961 && (octet - seg_addr
6962 <= segment_size (segment) - section_size (section, segment)));
6963}
6964
6965/* Handle PT_NOTE segment. */
6966static bool
11c6a7c6 6967is_note (asection *s, Elf_Internal_Phdr *p)
45d92439
AM
6968{
6969 return (p->p_type == PT_NOTE
6970 && elf_section_type (s) == SHT_NOTE
6971 && (ufile_ptr) s->filepos >= p->p_offset
6972 && p->p_filesz >= s->size
11c6a7c6 6973 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
45d92439
AM
6974}
6975
84d1d650 6976/* Rewrite program header information. */
252b5132 6977
0a1b45a2 6978static bool
c410035d 6979rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6980{
b34976b6
AM
6981 Elf_Internal_Ehdr *iehdr;
6982 struct elf_segment_map *map;
6983 struct elf_segment_map *map_first;
6984 struct elf_segment_map **pointer_to_map;
6985 Elf_Internal_Phdr *segment;
6986 asection *section;
6987 unsigned int i;
6988 unsigned int num_segments;
0a1b45a2
AM
6989 bool phdr_included = false;
6990 bool p_paddr_valid;
b34976b6
AM
6991 struct elf_segment_map *phdr_adjust_seg = NULL;
6992 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6993 const struct elf_backend_data *bed;
502794d4 6994 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6995
caf47ea6 6996 bed = get_elf_backend_data (ibfd);
252b5132
RH
6997 iehdr = elf_elfheader (ibfd);
6998
bc67d8a6 6999 map_first = NULL;
c044fabd 7000 pointer_to_map = &map_first;
252b5132
RH
7001
7002 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6 7003
252b5132
RH
7004 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7005 linker, which generates a PT_INTERP section with p_vaddr and
7006 p_memsz set to 0. */
aecc8f8a
AM
7007#define IS_SOLARIS_PT_INTERP(p, s) \
7008 (p->p_vaddr == 0 \
7009 && p->p_paddr == 0 \
7010 && p->p_memsz == 0 \
7011 && p->p_filesz > 0 \
7012 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 7013 && s->size > 0 \
aecc8f8a 7014 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 7015 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 7016 <= p->p_offset + p->p_filesz))
5c440b1e 7017
bc67d8a6
NC
7018 /* Decide if the given section should be included in the given segment.
7019 A section will be included if:
f5ffc919 7020 1. It is within the address space of the segment -- we use the LMA
08a40648 7021 if that is set for the segment and the VMA otherwise,
0efc80c8 7022 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 7023 segment.
bc67d8a6 7024 3. There is an output section associated with it,
eecdbe52 7025 4. The section has not already been allocated to a previous segment.
2b05f1b7 7026 5. PT_GNU_STACK segments do not include any sections.
03394ac9 7027 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
7028 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7029 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 7030 (with the possible exception of .dynamic). */
502794d4 7031#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
45d92439
AM
7032 (((is_contained_by (section, segment, segment->p_paddr, \
7033 segment->p_vaddr, opb, bed) \
2b05f1b7 7034 && (section->flags & SEC_ALLOC) != 0) \
11c6a7c6 7035 || is_note (section, segment)) \
2b05f1b7
L
7036 && segment->p_type != PT_GNU_STACK \
7037 && (segment->p_type != PT_TLS \
7038 || (section->flags & SEC_THREAD_LOCAL)) \
7039 && (segment->p_type == PT_LOAD \
7040 || segment->p_type == PT_TLS \
7041 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7042 && (segment->p_type != PT_DYNAMIC \
45d92439 7043 || section_size (section, segment) > 0 \
2b05f1b7 7044 || (segment->p_paddr \
502794d4
CE
7045 ? segment->p_paddr != section->lma * (opb) \
7046 : segment->p_vaddr != section->vma * (opb)) \
fd361982 7047 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 7048 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 7049
9f17e2a6
L
7050/* If the output section of a section in the input segment is NULL,
7051 it is removed from the corresponding output segment. */
502794d4
CE
7052#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7053 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
7054 && section->output_section != NULL)
7055
b34976b6 7056 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea 7057#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
45d92439 7058 (seg1->field >= segment_end (seg2, seg2->field))
b5f852ea
NC
7059
7060 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7061 their VMA address ranges and their LMA address ranges overlap.
7062 It is possible to have overlapping VMA ranges without overlapping LMA
7063 ranges. RedBoot images for example can have both .data and .bss mapped
7064 to the same VMA range, but with the .data section mapped to a different
7065 LMA. */
aecc8f8a 7066#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 7067 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 7068 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 7069 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 7070 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6 7071
c82ebeb7 7072 /* Initialise the segment mark field, and discard stupid alignment. */
bc67d8a6 7073 for (section = ibfd->sections; section != NULL; section = section->next)
c82ebeb7
AM
7074 {
7075 asection *o = section->output_section;
7076 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7077 o->alignment_power = 0;
7078 section->segment_mark = false;
7079 }
bc67d8a6 7080
5c44b38e
AM
7081 /* The Solaris linker creates program headers in which all the
7082 p_paddr fields are zero. When we try to objcopy or strip such a
7083 file, we get confused. Check for this case, and if we find it
7084 don't set the p_paddr_valid fields. */
0a1b45a2 7085 p_paddr_valid = false;
5c44b38e
AM
7086 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7087 i < num_segments;
7088 i++, segment++)
7089 if (segment->p_paddr != 0)
7090 {
0a1b45a2 7091 p_paddr_valid = true;
5c44b38e
AM
7092 break;
7093 }
7094
252b5132 7095 /* Scan through the segments specified in the program header
bc67d8a6 7096 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 7097 in the loadable segments. These can be created by weird
aecc8f8a 7098 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
7099 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7100 i < num_segments;
c044fabd 7101 i++, segment++)
252b5132 7102 {
252b5132 7103 unsigned int j;
c044fabd 7104 Elf_Internal_Phdr *segment2;
252b5132 7105
aecc8f8a
AM
7106 if (segment->p_type == PT_INTERP)
7107 for (section = ibfd->sections; section; section = section->next)
7108 if (IS_SOLARIS_PT_INTERP (segment, section))
7109 {
7110 /* Mininal change so that the normal section to segment
4cc11e76 7111 assignment code will work. */
502794d4 7112 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
7113 break;
7114 }
7115
bc67d8a6 7116 if (segment->p_type != PT_LOAD)
b10a8ae0
L
7117 {
7118 /* Remove PT_GNU_RELRO segment. */
7119 if (segment->p_type == PT_GNU_RELRO)
7120 segment->p_type = PT_NULL;
7121 continue;
7122 }
c044fabd 7123
bc67d8a6 7124 /* Determine if this segment overlaps any previous segments. */
0067a569 7125 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7126 {
7127 bfd_signed_vma extra_length;
c044fabd 7128
bc67d8a6 7129 if (segment2->p_type != PT_LOAD
0067a569 7130 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7131 continue;
c044fabd 7132
bc67d8a6
NC
7133 /* Merge the two segments together. */
7134 if (segment2->p_vaddr < segment->p_vaddr)
7135 {
c044fabd 7136 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7137 SEGMENT. */
45d92439
AM
7138 extra_length = (segment_end (segment, segment->p_vaddr)
7139 - segment_end (segment2, segment2->p_vaddr));
c044fabd 7140
bc67d8a6
NC
7141 if (extra_length > 0)
7142 {
0067a569 7143 segment2->p_memsz += extra_length;
bc67d8a6
NC
7144 segment2->p_filesz += extra_length;
7145 }
c044fabd 7146
bc67d8a6 7147 segment->p_type = PT_NULL;
c044fabd 7148
bc67d8a6
NC
7149 /* Since we have deleted P we must restart the outer loop. */
7150 i = 0;
7151 segment = elf_tdata (ibfd)->phdr;
7152 break;
7153 }
7154 else
7155 {
c044fabd 7156 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7157 SEGMENT2. */
45d92439
AM
7158 extra_length = (segment_end (segment2, segment2->p_vaddr)
7159 - segment_end (segment, segment->p_vaddr));
c044fabd 7160
bc67d8a6
NC
7161 if (extra_length > 0)
7162 {
0067a569 7163 segment->p_memsz += extra_length;
bc67d8a6
NC
7164 segment->p_filesz += extra_length;
7165 }
c044fabd 7166
bc67d8a6
NC
7167 segment2->p_type = PT_NULL;
7168 }
7169 }
7170 }
c044fabd 7171
bc67d8a6
NC
7172 /* The second scan attempts to assign sections to segments. */
7173 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7174 i < num_segments;
0067a569 7175 i++, segment++)
bc67d8a6 7176 {
0067a569
AM
7177 unsigned int section_count;
7178 asection **sections;
7179 asection *output_section;
7180 unsigned int isec;
9933dc52
AM
7181 asection *matching_lma;
7182 asection *suggested_lma;
0067a569 7183 unsigned int j;
446f7ed5 7184 size_t amt;
0067a569 7185 asection *first_section;
bc67d8a6
NC
7186
7187 if (segment->p_type == PT_NULL)
7188 continue;
c044fabd 7189
9f17e2a6 7190 first_section = NULL;
bc67d8a6 7191 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7192 for (section = ibfd->sections, section_count = 0;
7193 section != NULL;
7194 section = section->next)
9f17e2a6
L
7195 {
7196 /* Find the first section in the input segment, which may be
7197 removed from the corresponding output segment. */
502794d4 7198 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7199 {
7200 if (first_section == NULL)
7201 first_section = section;
7202 if (section->output_section != NULL)
7203 ++section_count;
7204 }
7205 }
811072d8 7206
b5f852ea
NC
7207 /* Allocate a segment map big enough to contain
7208 all of the sections we have selected. */
00bee008 7209 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7210 amt += section_count * sizeof (asection *);
a50b1753 7211 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7212 if (map == NULL)
0a1b45a2 7213 return false;
252b5132
RH
7214
7215 /* Initialise the fields of the segment map. Default to
7216 using the physical address of the segment in the input BFD. */
0067a569
AM
7217 map->next = NULL;
7218 map->p_type = segment->p_type;
7219 map->p_flags = segment->p_flags;
bc67d8a6 7220 map->p_flags_valid = 1;
55d55ac7 7221
c410035d
AM
7222 if (map->p_type == PT_LOAD
7223 && (ibfd->flags & D_PAGED) != 0
7224 && maxpagesize > 1
7225 && segment->p_align > 1)
7226 {
7227 map->p_align = segment->p_align;
7228 if (segment->p_align > maxpagesize)
7229 map->p_align = maxpagesize;
7230 map->p_align_valid = 1;
7231 }
7232
9f17e2a6
L
7233 /* If the first section in the input segment is removed, there is
7234 no need to preserve segment physical address in the corresponding
7235 output segment. */
945c025a 7236 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7237 {
7238 map->p_paddr = segment->p_paddr;
5c44b38e 7239 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7240 }
252b5132
RH
7241
7242 /* Determine if this segment contains the ELF file header
7243 and if it contains the program headers themselves. */
bc67d8a6
NC
7244 map->includes_filehdr = (segment->p_offset == 0
7245 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7246 map->includes_phdrs = 0;
252b5132 7247
0067a569 7248 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7249 {
bc67d8a6
NC
7250 map->includes_phdrs =
7251 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7252 && (segment->p_offset + segment->p_filesz
252b5132
RH
7253 >= ((bfd_vma) iehdr->e_phoff
7254 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7255
bc67d8a6 7256 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7257 phdr_included = true;
252b5132
RH
7258 }
7259
bc67d8a6 7260 if (section_count == 0)
252b5132
RH
7261 {
7262 /* Special segments, such as the PT_PHDR segment, may contain
7263 no sections, but ordinary, loadable segments should contain
1ed89aa9 7264 something. They are allowed by the ELF spec however, so only
07d6d2b8 7265 a warning is produced.
f98450c6
NC
7266 There is however the valid use case of embedded systems which
7267 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7268 flash memory with zeros. No warning is shown for that case. */
7269 if (segment->p_type == PT_LOAD
7270 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7271 /* xgettext:c-format */
9793eb77
AM
7272 _bfd_error_handler
7273 (_("%pB: warning: empty loadable segment detected"
7274 " at vaddr=%#" PRIx64 ", is this intentional?"),
7275 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7276
502794d4 7277 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7278 map->count = 0;
c044fabd
KH
7279 *pointer_to_map = map;
7280 pointer_to_map = &map->next;
252b5132
RH
7281
7282 continue;
7283 }
7284
7285 /* Now scan the sections in the input BFD again and attempt
7286 to add their corresponding output sections to the segment map.
7287 The problem here is how to handle an output section which has
7288 been moved (ie had its LMA changed). There are four possibilities:
7289
7290 1. None of the sections have been moved.
7291 In this case we can continue to use the segment LMA from the
7292 input BFD.
7293
7294 2. All of the sections have been moved by the same amount.
7295 In this case we can change the segment's LMA to match the LMA
7296 of the first section.
7297
7298 3. Some of the sections have been moved, others have not.
7299 In this case those sections which have not been moved can be
7300 placed in the current segment which will have to have its size,
7301 and possibly its LMA changed, and a new segment or segments will
7302 have to be created to contain the other sections.
7303
b5f852ea 7304 4. The sections have been moved, but not by the same amount.
252b5132
RH
7305 In this case we can change the segment's LMA to match the LMA
7306 of the first section and we will have to create a new segment
7307 or segments to contain the other sections.
7308
7309 In order to save time, we allocate an array to hold the section
7310 pointers that we are interested in. As these sections get assigned
7311 to a segment, they are removed from this array. */
7312
446f7ed5
AM
7313 amt = section_count * sizeof (asection *);
7314 sections = (asection **) bfd_malloc (amt);
252b5132 7315 if (sections == NULL)
0a1b45a2 7316 return false;
252b5132
RH
7317
7318 /* Step One: Scan for segment vs section LMA conflicts.
7319 Also add the sections to the section array allocated above.
7320 Also add the sections to the current segment. In the common
7321 case, where the sections have not been moved, this means that
7322 we have completely filled the segment, and there is nothing
7323 more to do. */
252b5132 7324 isec = 0;
9933dc52
AM
7325 matching_lma = NULL;
7326 suggested_lma = NULL;
252b5132 7327
461c4b2e 7328 for (section = first_section, j = 0;
bc67d8a6
NC
7329 section != NULL;
7330 section = section->next)
252b5132 7331 {
502794d4 7332 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7333 {
bc67d8a6
NC
7334 output_section = section->output_section;
7335
0067a569 7336 sections[j++] = section;
252b5132
RH
7337
7338 /* The Solaris native linker always sets p_paddr to 0.
7339 We try to catch that case here, and set it to the
5e8d7549
NC
7340 correct value. Note - some backends require that
7341 p_paddr be left as zero. */
5c44b38e 7342 if (!p_paddr_valid
4455705d 7343 && segment->p_vaddr != 0
0067a569 7344 && !bed->want_p_paddr_set_to_zero
252b5132 7345 && isec == 0
bc67d8a6 7346 && output_section->lma != 0
9933dc52
AM
7347 && (align_power (segment->p_vaddr
7348 + (map->includes_filehdr
7349 ? iehdr->e_ehsize : 0)
7350 + (map->includes_phdrs
7351 ? iehdr->e_phnum * iehdr->e_phentsize
7352 : 0),
66631823
CE
7353 output_section->alignment_power * opb)
7354 == (output_section->vma * opb)))
bc67d8a6 7355 map->p_paddr = segment->p_vaddr;
252b5132
RH
7356
7357 /* Match up the physical address of the segment with the
7358 LMA address of the output section. */
45d92439
AM
7359 if (is_contained_by (output_section, segment, map->p_paddr,
7360 map->p_paddr + map->p_vaddr_offset, opb, bed)
11c6a7c6 7361 || is_note (section, segment))
252b5132 7362 {
9933dc52
AM
7363 if (matching_lma == NULL
7364 || output_section->lma < matching_lma->lma)
7365 matching_lma = output_section;
252b5132
RH
7366
7367 /* We assume that if the section fits within the segment
bc67d8a6 7368 then it does not overlap any other section within that
252b5132 7369 segment. */
0067a569
AM
7370 map->sections[isec++] = output_section;
7371 }
9933dc52
AM
7372 else if (suggested_lma == NULL)
7373 suggested_lma = output_section;
147d51c2
L
7374
7375 if (j == section_count)
7376 break;
252b5132
RH
7377 }
7378 }
7379
bc67d8a6 7380 BFD_ASSERT (j == section_count);
252b5132
RH
7381
7382 /* Step Two: Adjust the physical address of the current segment,
7383 if necessary. */
bc67d8a6 7384 if (isec == section_count)
252b5132
RH
7385 {
7386 /* All of the sections fitted within the segment as currently
7387 specified. This is the default case. Add the segment to
7388 the list of built segments and carry on to process the next
7389 program header in the input BFD. */
bc67d8a6 7390 map->count = section_count;
c044fabd
KH
7391 *pointer_to_map = map;
7392 pointer_to_map = &map->next;
08a40648 7393
5c44b38e 7394 if (p_paddr_valid
30fe1832
AM
7395 && !bed->want_p_paddr_set_to_zero)
7396 {
7397 bfd_vma hdr_size = 0;
7398 if (map->includes_filehdr)
7399 hdr_size = iehdr->e_ehsize;
7400 if (map->includes_phdrs)
7401 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7402
7403 /* Account for padding before the first section in the
7404 segment. */
502794d4
CE
7405 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7406 - matching_lma->lma);
30fe1832 7407 }
08a40648 7408
252b5132
RH
7409 free (sections);
7410 continue;
7411 }
252b5132
RH
7412 else
7413 {
9933dc52
AM
7414 /* Change the current segment's physical address to match
7415 the LMA of the first section that fitted, or if no
7416 section fitted, the first section. */
7417 if (matching_lma == NULL)
7418 matching_lma = suggested_lma;
7419
66631823 7420 map->p_paddr = matching_lma->lma * opb;
72730e0c 7421
bc67d8a6
NC
7422 /* Offset the segment physical address from the lma
7423 to allow for space taken up by elf headers. */
9933dc52 7424 if (map->includes_phdrs)
010c8431 7425 {
9933dc52
AM
7426 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7427
7428 /* iehdr->e_phnum is just an estimate of the number
7429 of program headers that we will need. Make a note
7430 here of the number we used and the segment we chose
7431 to hold these headers, so that we can adjust the
7432 offset when we know the correct value. */
7433 phdr_adjust_num = iehdr->e_phnum;
7434 phdr_adjust_seg = map;
010c8431 7435 }
252b5132 7436
9933dc52 7437 if (map->includes_filehdr)
bc67d8a6 7438 {
9933dc52
AM
7439 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7440 map->p_paddr -= iehdr->e_ehsize;
7441 /* We've subtracted off the size of headers from the
7442 first section lma, but there may have been some
7443 alignment padding before that section too. Try to
7444 account for that by adjusting the segment lma down to
7445 the same alignment. */
7446 if (segment->p_align != 0 && segment->p_align < align)
7447 align = segment->p_align;
66631823 7448 map->p_paddr &= -(align * opb);
bc67d8a6 7449 }
252b5132
RH
7450 }
7451
7452 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7453 those that fit to the current segment and removing them from the
252b5132
RH
7454 sections array; but making sure not to leave large gaps. Once all
7455 possible sections have been assigned to the current segment it is
7456 added to the list of built segments and if sections still remain
7457 to be assigned, a new segment is constructed before repeating
7458 the loop. */
7459 isec = 0;
7460 do
7461 {
bc67d8a6 7462 map->count = 0;
9933dc52 7463 suggested_lma = NULL;
252b5132
RH
7464
7465 /* Fill the current segment with sections that fit. */
bc67d8a6 7466 for (j = 0; j < section_count; j++)
252b5132 7467 {
bc67d8a6 7468 section = sections[j];
252b5132 7469
bc67d8a6 7470 if (section == NULL)
252b5132
RH
7471 continue;
7472
bc67d8a6 7473 output_section = section->output_section;
252b5132 7474
bc67d8a6 7475 BFD_ASSERT (output_section != NULL);
c044fabd 7476
45d92439
AM
7477 if (is_contained_by (output_section, segment, map->p_paddr,
7478 map->p_paddr + map->p_vaddr_offset, opb, bed)
11c6a7c6 7479 || is_note (section, segment))
252b5132 7480 {
bc67d8a6 7481 if (map->count == 0)
252b5132
RH
7482 {
7483 /* If the first section in a segment does not start at
bc67d8a6
NC
7484 the beginning of the segment, then something is
7485 wrong. */
9933dc52
AM
7486 if (align_power (map->p_paddr
7487 + (map->includes_filehdr
7488 ? iehdr->e_ehsize : 0)
7489 + (map->includes_phdrs
7490 ? iehdr->e_phnum * iehdr->e_phentsize
7491 : 0),
66631823
CE
7492 output_section->alignment_power * opb)
7493 != output_section->lma * opb)
9aea1e31 7494 goto sorry;
252b5132
RH
7495 }
7496 else
7497 {
0067a569 7498 asection *prev_sec;
252b5132 7499
bc67d8a6 7500 prev_sec = map->sections[map->count - 1];
252b5132
RH
7501
7502 /* If the gap between the end of the previous section
bc67d8a6
NC
7503 and the start of this section is more than
7504 maxpagesize then we need to start a new segment. */
eea6121a 7505 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7506 maxpagesize)
caf47ea6 7507 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7508 || (prev_sec->lma + prev_sec->size
079e9a2f 7509 > output_section->lma))
252b5132 7510 {
9933dc52
AM
7511 if (suggested_lma == NULL)
7512 suggested_lma = output_section;
252b5132
RH
7513
7514 continue;
7515 }
7516 }
7517
bc67d8a6 7518 map->sections[map->count++] = output_section;
252b5132
RH
7519 ++isec;
7520 sections[j] = NULL;
9933dc52 7521 if (segment->p_type == PT_LOAD)
0a1b45a2 7522 section->segment_mark = true;
0067a569 7523 }
9933dc52
AM
7524 else if (suggested_lma == NULL)
7525 suggested_lma = output_section;
252b5132
RH
7526 }
7527
beab4532
NC
7528 /* PR 23932. A corrupt input file may contain sections that cannot
7529 be assigned to any segment - because for example they have a
9984857c
NC
7530 negative size - or segments that do not contain any sections.
7531 But there are also valid reasons why a segment can be empty.
7532 So allow a count of zero. */
252b5132
RH
7533
7534 /* Add the current segment to the list of built segments. */
c044fabd
KH
7535 *pointer_to_map = map;
7536 pointer_to_map = &map->next;
252b5132 7537
bc67d8a6 7538 if (isec < section_count)
252b5132
RH
7539 {
7540 /* We still have not allocated all of the sections to
7541 segments. Create a new segment here, initialise it
7542 and carry on looping. */
00bee008 7543 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7544 amt += section_count * sizeof (asection *);
5964fc3a 7545 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7546 if (map == NULL)
5ed6aba4
NC
7547 {
7548 free (sections);
0a1b45a2 7549 return false;
5ed6aba4 7550 }
252b5132
RH
7551
7552 /* Initialise the fields of the segment map. Set the physical
7553 physical address to the LMA of the first section that has
7554 not yet been assigned. */
0067a569
AM
7555 map->next = NULL;
7556 map->p_type = segment->p_type;
7557 map->p_flags = segment->p_flags;
7558 map->p_flags_valid = 1;
66631823 7559 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7560 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7561 map->includes_filehdr = 0;
0067a569 7562 map->includes_phdrs = 0;
252b5132 7563 }
9984857c
NC
7564
7565 continue;
7566 sorry:
7567 bfd_set_error (bfd_error_sorry);
7568 free (sections);
0a1b45a2 7569 return false;
252b5132 7570 }
bc67d8a6 7571 while (isec < section_count);
252b5132
RH
7572
7573 free (sections);
7574 }
7575
12bd6957 7576 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7577
7578 /* If we had to estimate the number of program headers that were
9ad5cbcf 7579 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7580 the offset if necessary. */
7581 if (phdr_adjust_seg != NULL)
7582 {
7583 unsigned int count;
c044fabd 7584
bc67d8a6 7585 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7586 count++;
252b5132 7587
bc67d8a6
NC
7588 if (count > phdr_adjust_num)
7589 phdr_adjust_seg->p_paddr
7590 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7591
7592 for (map = map_first; map != NULL; map = map->next)
7593 if (map->p_type == PT_PHDR)
7594 {
7595 bfd_vma adjust
7596 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7597 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7598 break;
7599 }
bc67d8a6 7600 }
c044fabd 7601
bc67d8a6 7602#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7603#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7604#undef INCLUDE_SECTION_IN_SEGMENT
7605#undef SEGMENT_AFTER_SEGMENT
7606#undef SEGMENT_OVERLAPS
0a1b45a2 7607 return true;
252b5132
RH
7608}
7609
74e315db
L
7610/* Return true if p_align in the ELF program header in ABFD is valid. */
7611
7612static bool
7613elf_is_p_align_valid (bfd *abfd)
7614{
7615 unsigned int i;
7616 Elf_Internal_Phdr *segment;
7617 unsigned int num_segments;
7618 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7619 bfd_size_type maxpagesize = bed->maxpagesize;
7620 bfd_size_type p_align = bed->p_align;
7621
7622 /* Return true if the default p_align value isn't set or the maximum
7623 page size is the same as the minimum page size. */
7624 if (p_align == 0 || maxpagesize == bed->minpagesize)
7625 return true;
7626
7627 /* When the default p_align value is set, p_align may be set to the
7628 default p_align value while segments are aligned to the maximum
7629 page size. In this case, the input p_align will be ignored and
7630 the maximum page size will be used to align the output segments. */
7631 segment = elf_tdata (abfd)->phdr;
7632 num_segments = elf_elfheader (abfd)->e_phnum;
7633 for (i = 0; i < num_segments; i++, segment++)
7634 if (segment->p_type == PT_LOAD
7635 && (segment->p_align != p_align
7636 || vma_page_aligned_bias (segment->p_vaddr,
7637 segment->p_offset,
7638 maxpagesize) != 0))
7639 return true;
7640
7641 return false;
7642}
7643
84d1d650
L
7644/* Copy ELF program header information. */
7645
0a1b45a2 7646static bool
84d1d650
L
7647copy_elf_program_header (bfd *ibfd, bfd *obfd)
7648{
7649 Elf_Internal_Ehdr *iehdr;
7650 struct elf_segment_map *map;
7651 struct elf_segment_map *map_first;
7652 struct elf_segment_map **pointer_to_map;
7653 Elf_Internal_Phdr *segment;
7654 unsigned int i;
7655 unsigned int num_segments;
0a1b45a2
AM
7656 bool phdr_included = false;
7657 bool p_paddr_valid;
74e315db 7658 bool p_palign_valid;
502794d4 7659 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7660
7661 iehdr = elf_elfheader (ibfd);
7662
7663 map_first = NULL;
7664 pointer_to_map = &map_first;
7665
88967714
AM
7666 /* If all the segment p_paddr fields are zero, don't set
7667 map->p_paddr_valid. */
0a1b45a2 7668 p_paddr_valid = false;
84d1d650 7669 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7670 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7671 i < num_segments;
7672 i++, segment++)
7673 if (segment->p_paddr != 0)
7674 {
0a1b45a2 7675 p_paddr_valid = true;
88967714
AM
7676 break;
7677 }
7678
74e315db
L
7679 p_palign_valid = elf_is_p_align_valid (ibfd);
7680
84d1d650
L
7681 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7682 i < num_segments;
7683 i++, segment++)
7684 {
7685 asection *section;
7686 unsigned int section_count;
986f0783 7687 size_t amt;
84d1d650 7688 Elf_Internal_Shdr *this_hdr;
53020534 7689 asection *first_section = NULL;
a76e6f2f 7690 asection *lowest_section;
84d1d650 7691
84d1d650
L
7692 /* Compute how many sections are in this segment. */
7693 for (section = ibfd->sections, section_count = 0;
7694 section != NULL;
7695 section = section->next)
7696 {
7697 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7698 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7699 {
a76e6f2f
AM
7700 if (first_section == NULL)
7701 first_section = section;
3271a814
NS
7702 section_count++;
7703 }
84d1d650
L
7704 }
7705
7706 /* Allocate a segment map big enough to contain
7707 all of the sections we have selected. */
00bee008 7708 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7709 amt += section_count * sizeof (asection *);
a50b1753 7710 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650 7711 if (map == NULL)
0a1b45a2 7712 return false;
84d1d650
L
7713
7714 /* Initialize the fields of the output segment map with the
7715 input segment. */
7716 map->next = NULL;
7717 map->p_type = segment->p_type;
7718 map->p_flags = segment->p_flags;
7719 map->p_flags_valid = 1;
7720 map->p_paddr = segment->p_paddr;
88967714 7721 map->p_paddr_valid = p_paddr_valid;
3f570048 7722 map->p_align = segment->p_align;
fb5a4a58
L
7723 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7724 map->p_align_valid = (map->p_type == PT_GNU_STACK
7725 || p_palign_valid);
3271a814 7726 map->p_vaddr_offset = 0;
84d1d650 7727
04c3a755
NS
7728 if (map->p_type == PT_GNU_RELRO
7729 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7730 {
7731 /* The PT_GNU_RELRO segment may contain the first a few
7732 bytes in the .got.plt section even if the whole .got.plt
7733 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7734 change the size of the PT_GNU_RELRO segment.
7735 Similarly, PT_GNU_STACK size is significant on uclinux
7736 systems. */
9433b9b1 7737 map->p_size = segment->p_memsz;
b10a8ae0
L
7738 map->p_size_valid = 1;
7739 }
7740
84d1d650
L
7741 /* Determine if this segment contains the ELF file header
7742 and if it contains the program headers themselves. */
7743 map->includes_filehdr = (segment->p_offset == 0
7744 && segment->p_filesz >= iehdr->e_ehsize);
7745
7746 map->includes_phdrs = 0;
7747 if (! phdr_included || segment->p_type != PT_LOAD)
7748 {
7749 map->includes_phdrs =
7750 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7751 && (segment->p_offset + segment->p_filesz
7752 >= ((bfd_vma) iehdr->e_phoff
7753 + iehdr->e_phnum * iehdr->e_phentsize)));
7754
7755 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7756 phdr_included = true;
84d1d650
L
7757 }
7758
bbefd0a9 7759 lowest_section = NULL;
84d1d650
L
7760 if (section_count != 0)
7761 {
7762 unsigned int isec = 0;
7763
53020534 7764 for (section = first_section;
84d1d650
L
7765 section != NULL;
7766 section = section->next)
7767 {
7768 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7769 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7770 {
7771 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7772 if ((section->flags & SEC_ALLOC) != 0)
7773 {
7774 bfd_vma seg_off;
7775
bbefd0a9
AM
7776 if (lowest_section == NULL
7777 || section->lma < lowest_section->lma)
fb8a5684
AM
7778 lowest_section = section;
7779
a76e6f2f
AM
7780 /* Section lmas are set up from PT_LOAD header
7781 p_paddr in _bfd_elf_make_section_from_shdr.
7782 If this header has a p_paddr that disagrees
7783 with the section lma, flag the p_paddr as
7784 invalid. */
7785 if ((section->flags & SEC_LOAD) != 0)
7786 seg_off = this_hdr->sh_offset - segment->p_offset;
7787 else
7788 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7789 if (section->lma * opb - segment->p_paddr != seg_off)
0a1b45a2 7790 map->p_paddr_valid = false;
a76e6f2f 7791 }
53020534
L
7792 if (isec == section_count)
7793 break;
7794 }
84d1d650
L
7795 }
7796 }
7797
5d695627 7798 if (section_count == 0)
502794d4 7799 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7800 else if (map->p_paddr_valid)
7801 {
7802 /* Account for padding before the first section in the segment. */
7803 bfd_vma hdr_size = 0;
7804 if (map->includes_filehdr)
7805 hdr_size = iehdr->e_ehsize;
7806 if (map->includes_phdrs)
7807 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7808
502794d4 7809 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7810 - (lowest_section ? lowest_section->lma : 0));
7811 }
a76e6f2f 7812
84d1d650
L
7813 map->count = section_count;
7814 *pointer_to_map = map;
7815 pointer_to_map = &map->next;
7816 }
7817
12bd6957 7818 elf_seg_map (obfd) = map_first;
0a1b45a2 7819 return true;
84d1d650
L
7820}
7821
7822/* Copy private BFD data. This copies or rewrites ELF program header
7823 information. */
7824
0a1b45a2 7825static bool
84d1d650
L
7826copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7827{
c410035d
AM
7828 bfd_vma maxpagesize;
7829
84d1d650
L
7830 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7831 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 7832 return true;
84d1d650
L
7833
7834 if (elf_tdata (ibfd)->phdr == NULL)
0a1b45a2 7835 return true;
84d1d650
L
7836
7837 if (ibfd->xvec == obfd->xvec)
7838 {
cb3ff1e5
NC
7839 /* Check to see if any sections in the input BFD
7840 covered by ELF program header have changed. */
d55ce4e2 7841 Elf_Internal_Phdr *segment;
84d1d650
L
7842 asection *section, *osec;
7843 unsigned int i, num_segments;
7844 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7845 const struct elf_backend_data *bed;
7846
7847 bed = get_elf_backend_data (ibfd);
7848
7849 /* Regenerate the segment map if p_paddr is set to 0. */
7850 if (bed->want_p_paddr_set_to_zero)
7851 goto rewrite;
84d1d650
L
7852
7853 /* Initialize the segment mark field. */
7854 for (section = obfd->sections; section != NULL;
7855 section = section->next)
0a1b45a2 7856 section->segment_mark = false;
84d1d650
L
7857
7858 num_segments = elf_elfheader (ibfd)->e_phnum;
7859 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7860 i < num_segments;
7861 i++, segment++)
7862 {
5f6999aa
NC
7863 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7864 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7865 which severly confuses things, so always regenerate the segment
7866 map in this case. */
7867 if (segment->p_paddr == 0
7868 && segment->p_memsz == 0
11c6a7c6
AM
7869 && (segment->p_type == PT_INTERP
7870 || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7871 goto rewrite;
5f6999aa 7872
84d1d650
L
7873 for (section = ibfd->sections;
7874 section != NULL; section = section->next)
7875 {
7876 /* We mark the output section so that we know it comes
7877 from the input BFD. */
7878 osec = section->output_section;
7879 if (osec)
0a1b45a2 7880 osec->segment_mark = true;
84d1d650
L
7881
7882 /* Check if this section is covered by the segment. */
7883 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7884 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7885 {
7886 /* FIXME: Check if its output section is changed or
7887 removed. What else do we need to check? */
7888 if (osec == NULL
7889 || section->flags != osec->flags
7890 || section->lma != osec->lma
7891 || section->vma != osec->vma
7892 || section->size != osec->size
7893 || section->rawsize != osec->rawsize
7894 || section->alignment_power != osec->alignment_power)
7895 goto rewrite;
7896 }
7897 }
7898 }
7899
cb3ff1e5 7900 /* Check to see if any output section do not come from the
84d1d650
L
7901 input BFD. */
7902 for (section = obfd->sections; section != NULL;
7903 section = section->next)
7904 {
535b785f 7905 if (!section->segment_mark)
84d1d650
L
7906 goto rewrite;
7907 else
0a1b45a2 7908 section->segment_mark = false;
84d1d650
L
7909 }
7910
7911 return copy_elf_program_header (ibfd, obfd);
7912 }
7913
dc1e8a47 7914 rewrite:
c410035d 7915 maxpagesize = 0;
f1d85785
L
7916 if (ibfd->xvec == obfd->xvec)
7917 {
7918 /* When rewriting program header, set the output maxpagesize to
7919 the maximum alignment of input PT_LOAD segments. */
7920 Elf_Internal_Phdr *segment;
7921 unsigned int i;
7922 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7923
7924 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7925 i < num_segments;
7926 i++, segment++)
7927 if (segment->p_type == PT_LOAD
7928 && maxpagesize < segment->p_align)
c86934ce
NC
7929 {
7930 /* PR 17512: file: f17299af. */
7931 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7932 /* xgettext:c-format */
2dcf00ce
AM
7933 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7934 PRIx64 " is too large"),
7935 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7936 else
7937 maxpagesize = segment->p_align;
7938 }
f1d85785 7939 }
c410035d
AM
7940 if (maxpagesize == 0)
7941 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7942
c410035d 7943 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7944}
7945
ccd2ec6a
L
7946/* Initialize private output section information from input section. */
7947
0a1b45a2 7948bool
ccd2ec6a
L
7949_bfd_elf_init_private_section_data (bfd *ibfd,
7950 asection *isec,
7951 bfd *obfd,
7952 asection *osec,
7953 struct bfd_link_info *link_info)
7954
7955{
7956 Elf_Internal_Shdr *ihdr, *ohdr;
0a1b45a2
AM
7957 bool final_link = (link_info != NULL
7958 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7959
7960 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7961 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7962 return true;
ccd2ec6a 7963
ba85c43e
NC
7964 BFD_ASSERT (elf_section_data (osec) != NULL);
7965
8c803a2d
AM
7966 /* If this is a known ABI section, ELF section type and flags may
7967 have been set up when OSEC was created. For normal sections we
7968 allow the user to override the type and flags other than
7969 SHF_MASKOS and SHF_MASKPROC. */
7970 if (elf_section_type (osec) == SHT_PROGBITS
7971 || elf_section_type (osec) == SHT_NOTE
7972 || elf_section_type (osec) == SHT_NOBITS)
7973 elf_section_type (osec) = SHT_NULL;
7974 /* For objcopy and relocatable link, copy the ELF section type from
7975 the input file if the BFD section flags are the same. (If they
7976 are different the user may be doing something like
7977 "objcopy --set-section-flags .text=alloc,data".) For a final
7978 link allow some flags that the linker clears to differ. */
42bb2e33 7979 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7980 && (osec->flags == isec->flags
7981 || (final_link
7982 && ((osec->flags ^ isec->flags)
0814be7d 7983 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7984 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7985
7986 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7987 elf_section_flags (osec) = (elf_section_flags (isec)
7988 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7989
a91e1603 7990 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7991 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7992 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7993 elf_section_data (osec)->this_hdr.sh_info
7994 = elf_section_data (isec)->this_hdr.sh_info;
7995
ccd2ec6a
L
7996 /* Set things up for objcopy and relocatable link. The output
7997 SHT_GROUP section will have its elf_next_in_group pointing back
7998 to the input group members. Ignore linker created group section.
7999 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
8000 if ((link_info == NULL
8001 || !link_info->resolve_section_groups)
8002 && (elf_sec_group (isec) == NULL
8003 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 8004 {
7bdf4127
AB
8005 if (elf_section_flags (isec) & SHF_GROUP)
8006 elf_section_flags (osec) |= SHF_GROUP;
8007 elf_next_in_group (osec) = elf_next_in_group (isec);
8008 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
8009 }
8010
7bdf4127
AB
8011 /* If not decompress, preserve SHF_COMPRESSED. */
8012 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8013 elf_section_flags (osec) |= (elf_section_flags (isec)
8014 & SHF_COMPRESSED);
8015
ccd2ec6a
L
8016 ihdr = &elf_section_data (isec)->this_hdr;
8017
8018 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8019 don't use the output section of the linked-to section since it
8020 may be NULL at this point. */
8021 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8022 {
8023 ohdr = &elf_section_data (osec)->this_hdr;
8024 ohdr->sh_flags |= SHF_LINK_ORDER;
8025 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8026 }
8027
8028 osec->use_rela_p = isec->use_rela_p;
8029
0a1b45a2 8030 return true;
ccd2ec6a
L
8031}
8032
252b5132
RH
8033/* Copy private section information. This copies over the entsize
8034 field, and sometimes the info field. */
8035
0a1b45a2 8036bool
217aa764
AM
8037_bfd_elf_copy_private_section_data (bfd *ibfd,
8038 asection *isec,
8039 bfd *obfd,
8040 asection *osec)
252b5132
RH
8041{
8042 Elf_Internal_Shdr *ihdr, *ohdr;
8043
8044 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8045 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 8046 return true;
252b5132 8047
252b5132
RH
8048 ihdr = &elf_section_data (isec)->this_hdr;
8049 ohdr = &elf_section_data (osec)->this_hdr;
8050
8051 ohdr->sh_entsize = ihdr->sh_entsize;
8052
8053 if (ihdr->sh_type == SHT_SYMTAB
8054 || ihdr->sh_type == SHT_DYNSYM
8055 || ihdr->sh_type == SHT_GNU_verneed
8056 || ihdr->sh_type == SHT_GNU_verdef)
8057 ohdr->sh_info = ihdr->sh_info;
8058
ccd2ec6a
L
8059 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8060 NULL);
252b5132
RH
8061}
8062
d0bf826b
AM
8063/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8064 necessary if we are removing either the SHT_GROUP section or any of
8065 the group member sections. DISCARDED is the value that a section's
8066 output_section has if the section will be discarded, NULL when this
8067 function is called from objcopy, bfd_abs_section_ptr when called
8068 from the linker. */
80fccad2 8069
0a1b45a2 8070bool
d0bf826b 8071_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 8072{
30288845
AM
8073 asection *isec;
8074
30288845 8075 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 8076 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
8077 {
8078 asection *first = elf_next_in_group (isec);
8079 asection *s = first;
d0bf826b
AM
8080 bfd_size_type removed = 0;
8081
30288845
AM
8082 while (s != NULL)
8083 {
415f38a6
AM
8084 /* If this member section is being output but the
8085 SHT_GROUP section is not, then clear the group info
8086 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
8087 if (s->output_section != discarded
8088 && isec->output_section == discarded)
30288845
AM
8089 {
8090 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8091 elf_group_name (s->output_section) = NULL;
8092 }
3349112e 8093 else
6e5e9d58
AM
8094 {
8095 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
8096 if (s->output_section == discarded
8097 && isec->output_section != discarded)
8098 {
8099 /* Conversely, if the member section is not being
8100 output but the SHT_GROUP section is, then adjust
8101 its size. */
8102 removed += 4;
8103 if (elf_sec->rel.hdr != NULL
8104 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8105 removed += 4;
8106 if (elf_sec->rela.hdr != NULL
8107 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8108 removed += 4;
8109 }
8110 else
8111 {
8112 /* Also adjust for zero-sized relocation member
8113 section. */
8114 if (elf_sec->rel.hdr != NULL
8115 && elf_sec->rel.hdr->sh_size == 0)
8116 removed += 4;
8117 if (elf_sec->rela.hdr != NULL
8118 && elf_sec->rela.hdr->sh_size == 0)
8119 removed += 4;
8120 }
6e5e9d58 8121 }
30288845
AM
8122 s = elf_next_in_group (s);
8123 if (s == first)
8124 break;
8125 }
d0bf826b
AM
8126 if (removed != 0)
8127 {
8128 if (discarded != NULL)
8129 {
8130 /* If we've been called for ld -r, then we need to
6e5e9d58 8131 adjust the input section size. */
d0bf826b
AM
8132 if (isec->rawsize == 0)
8133 isec->rawsize = isec->size;
8134 isec->size = isec->rawsize - removed;
6e5e9d58
AM
8135 if (isec->size <= 4)
8136 {
8137 isec->size = 0;
8138 isec->flags |= SEC_EXCLUDE;
8139 }
d0bf826b 8140 }
182ad375 8141 else if (isec->output_section != NULL)
d0bf826b
AM
8142 {
8143 /* Adjust the output section size when called from
8144 objcopy. */
8145 isec->output_section->size -= removed;
6e5e9d58
AM
8146 if (isec->output_section->size <= 4)
8147 {
8148 isec->output_section->size = 0;
8149 isec->output_section->flags |= SEC_EXCLUDE;
8150 }
d0bf826b
AM
8151 }
8152 }
30288845
AM
8153 }
8154
0a1b45a2 8155 return true;
80fccad2
BW
8156}
8157
d0bf826b
AM
8158/* Copy private header information. */
8159
0a1b45a2 8160bool
d0bf826b
AM
8161_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8162{
8163 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8164 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8165 return true;
d0bf826b
AM
8166
8167 /* Copy over private BFD data if it has not already been copied.
8168 This must be done here, rather than in the copy_private_bfd_data
8169 entry point, because the latter is called after the section
8170 contents have been set, which means that the program headers have
8171 already been worked out. */
12bd6957 8172 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8173 {
8174 if (! copy_private_bfd_data (ibfd, obfd))
0a1b45a2 8175 return false;
d0bf826b
AM
8176 }
8177
8178 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8179}
8180
252b5132
RH
8181/* Copy private symbol information. If this symbol is in a section
8182 which we did not map into a BFD section, try to map the section
8183 index correctly. We use special macro definitions for the mapped
8184 section indices; these definitions are interpreted by the
8185 swap_out_syms function. */
8186
9ad5cbcf
AM
8187#define MAP_ONESYMTAB (SHN_HIOS + 1)
8188#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8189#define MAP_STRTAB (SHN_HIOS + 3)
8190#define MAP_SHSTRTAB (SHN_HIOS + 4)
8191#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8192
0a1b45a2 8193bool
217aa764
AM
8194_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8195 asymbol *isymarg,
8196 bfd *obfd,
8197 asymbol *osymarg)
252b5132
RH
8198{
8199 elf_symbol_type *isym, *osym;
8200
8201 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8202 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8203 return true;
252b5132 8204
c1229f84
AM
8205 isym = elf_symbol_from (isymarg);
8206 osym = elf_symbol_from (osymarg);
252b5132
RH
8207
8208 if (isym != NULL
8424d8f5 8209 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8210 && osym != NULL
8211 && bfd_is_abs_section (isym->symbol.section))
8212 {
8213 unsigned int shndx;
8214
8215 shndx = isym->internal_elf_sym.st_shndx;
8216 if (shndx == elf_onesymtab (ibfd))
8217 shndx = MAP_ONESYMTAB;
8218 else if (shndx == elf_dynsymtab (ibfd))
8219 shndx = MAP_DYNSYMTAB;
12bd6957 8220 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8221 shndx = MAP_STRTAB;
12bd6957 8222 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8223 shndx = MAP_SHSTRTAB;
6a40cf0c 8224 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8225 shndx = MAP_SYM_SHNDX;
252b5132
RH
8226 osym->internal_elf_sym.st_shndx = shndx;
8227 }
8228
0a1b45a2 8229 return true;
252b5132
RH
8230}
8231
8232/* Swap out the symbols. */
8233
0a1b45a2 8234static bool
217aa764 8235swap_out_syms (bfd *abfd,
ef10c3ac 8236 struct elf_strtab_hash **sttp,
3d16b64e
NA
8237 int relocatable_p,
8238 struct bfd_link_info *info)
252b5132 8239{
9c5bfbb7 8240 const struct elf_backend_data *bed;
1f4361a7 8241 unsigned int symcount;
079e9a2f 8242 asymbol **syms;
ef10c3ac 8243 struct elf_strtab_hash *stt;
079e9a2f 8244 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8245 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8246 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8247 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8248 bfd_byte *outbound_syms;
8249 bfd_byte *outbound_shndx;
ef10c3ac 8250 unsigned long outbound_syms_index;
1f4361a7 8251 unsigned int idx;
12bd6957 8252 unsigned int num_locals;
1f4361a7 8253 size_t amt;
0a1b45a2 8254 bool name_local_sections;
252b5132 8255
12bd6957 8256 if (!elf_map_symbols (abfd, &num_locals))
0a1b45a2 8257 return false;
252b5132 8258
c044fabd 8259 /* Dump out the symtabs. */
ef10c3ac 8260 stt = _bfd_elf_strtab_init ();
079e9a2f 8261 if (stt == NULL)
0a1b45a2 8262 return false;
252b5132 8263
079e9a2f
AM
8264 bed = get_elf_backend_data (abfd);
8265 symcount = bfd_get_symcount (abfd);
8266 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8267 symtab_hdr->sh_type = SHT_SYMTAB;
8268 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8269 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8270 symtab_hdr->sh_info = num_locals + 1;
72de5009 8271 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8272
8273 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8274 symstrtab_hdr->sh_type = SHT_STRTAB;
8275
ef10c3ac 8276 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8277 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8278 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8279 {
1f4361a7 8280 bfd_set_error (bfd_error_no_memory);
ef10c3ac 8281 _bfd_elf_strtab_free (stt);
0a1b45a2 8282 return false;
ef10c3ac
L
8283 }
8284
1f4361a7
AM
8285 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8286 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8287 {
1f4361a7
AM
8288 error_no_mem:
8289 bfd_set_error (bfd_error_no_memory);
8290 error_return:
ef10c3ac 8291 free (symstrtab);
1f4361a7 8292 _bfd_elf_strtab_free (stt);
0a1b45a2 8293 return false;
5ed6aba4 8294 }
217aa764 8295 symtab_hdr->contents = outbound_syms;
ef10c3ac 8296 outbound_syms_index = 0;
252b5132 8297
9ad5cbcf 8298 outbound_shndx = NULL;
6a40cf0c
NC
8299
8300 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8301 {
6a40cf0c
NC
8302 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8303 if (symtab_shndx_hdr->sh_name != 0)
8304 {
1f4361a7
AM
8305 if (_bfd_mul_overflow (symcount + 1,
8306 sizeof (Elf_External_Sym_Shndx), &amt))
8307 goto error_no_mem;
8308 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8309 if (outbound_shndx == NULL)
8310 goto error_return;
5ed6aba4 8311
6a40cf0c
NC
8312 symtab_shndx_hdr->contents = outbound_shndx;
8313 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8314 symtab_shndx_hdr->sh_size = amt;
8315 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8316 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8317 }
8318 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8319 }
8320
589e6347 8321 /* Now generate the data (for "contents"). */
079e9a2f
AM
8322 {
8323 /* Fill in zeroth symbol and swap it out. */
8324 Elf_Internal_Sym sym;
8325 sym.st_name = 0;
8326 sym.st_value = 0;
8327 sym.st_size = 0;
8328 sym.st_info = 0;
8329 sym.st_other = 0;
8330 sym.st_shndx = SHN_UNDEF;
35fc36a8 8331 sym.st_target_internal = 0;
ef10c3ac
L
8332 symstrtab[0].sym = sym;
8333 symstrtab[0].dest_index = outbound_syms_index;
ef10c3ac 8334 outbound_syms_index++;
079e9a2f 8335 }
252b5132 8336
174fd7f9
RS
8337 name_local_sections
8338 = (bed->elf_backend_name_local_section_symbols
8339 && bed->elf_backend_name_local_section_symbols (abfd));
8340
079e9a2f 8341 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8342 for (idx = 0; idx < symcount;)
252b5132 8343 {
252b5132 8344 Elf_Internal_Sym sym;
079e9a2f
AM
8345 bfd_vma value = syms[idx]->value;
8346 elf_symbol_type *type_ptr;
8347 flagword flags = syms[idx]->flags;
8348 int type;
252b5132 8349
174fd7f9
RS
8350 if (!name_local_sections
8351 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8352 {
8353 /* Local section symbols have no name. */
ef10c3ac 8354 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8355 }
8356 else
8357 {
ef10c3ac
L
8358 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8359 to get the final offset for st_name. */
8360 sym.st_name
8361 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
0a1b45a2 8362 false);
079e9a2f 8363 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8364 goto error_return;
079e9a2f 8365 }
252b5132 8366
c1229f84 8367 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8368
079e9a2f
AM
8369 if ((flags & BSF_SECTION_SYM) == 0
8370 && bfd_is_com_section (syms[idx]->section))
8371 {
8372 /* ELF common symbols put the alignment into the `value' field,
8373 and the size into the `size' field. This is backwards from
8374 how BFD handles it, so reverse it here. */
8375 sym.st_size = value;
8376 if (type_ptr == NULL
8377 || type_ptr->internal_elf_sym.st_value == 0)
8378 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8379 else
8380 sym.st_value = type_ptr->internal_elf_sym.st_value;
8381 sym.st_shndx = _bfd_elf_section_from_bfd_section
8382 (abfd, syms[idx]->section);
8383 }
8384 else
8385 {
8386 asection *sec = syms[idx]->section;
cb33740c 8387 unsigned int shndx;
252b5132 8388
079e9a2f
AM
8389 if (sec->output_section)
8390 {
8391 value += sec->output_offset;
8392 sec = sec->output_section;
8393 }
589e6347 8394
079e9a2f
AM
8395 /* Don't add in the section vma for relocatable output. */
8396 if (! relocatable_p)
8397 value += sec->vma;
8398 sym.st_value = value;
8399 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8400
8401 if (bfd_is_abs_section (sec)
8402 && type_ptr != NULL
8403 && type_ptr->internal_elf_sym.st_shndx != 0)
8404 {
8405 /* This symbol is in a real ELF section which we did
8406 not create as a BFD section. Undo the mapping done
8407 by copy_private_symbol_data. */
8408 shndx = type_ptr->internal_elf_sym.st_shndx;
8409 switch (shndx)
8410 {
8411 case MAP_ONESYMTAB:
8412 shndx = elf_onesymtab (abfd);
8413 break;
8414 case MAP_DYNSYMTAB:
8415 shndx = elf_dynsymtab (abfd);
8416 break;
8417 case MAP_STRTAB:
12bd6957 8418 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8419 break;
8420 case MAP_SHSTRTAB:
12bd6957 8421 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8422 break;
9ad5cbcf 8423 case MAP_SYM_SHNDX:
6a40cf0c
NC
8424 if (elf_symtab_shndx_list (abfd))
8425 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8426 break;
00e49dff
NC
8427 case SHN_COMMON:
8428 case SHN_ABS:
15bc576a 8429 shndx = SHN_ABS;
079e9a2f 8430 break;
00e49dff
NC
8431 default:
8432 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8433 {
8434 if (bed->symbol_section_index)
8435 shndx = bed->symbol_section_index (abfd, type_ptr);
8436 /* Otherwise just leave the index alone. */
8437 }
8438 else
8439 {
8440 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8441 _bfd_error_handler (_("%pB: \
8442Unable to handle section index %x in ELF symbol. Using ABS instead."),
8443 abfd, shndx);
8444 shndx = SHN_ABS;
8445 }
8446 break;
079e9a2f
AM
8447 }
8448 }
8449 else
8450 {
8451 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8452
cb33740c 8453 if (shndx == SHN_BAD)
079e9a2f
AM
8454 {
8455 asection *sec2;
8456
8457 /* Writing this would be a hell of a lot easier if
8458 we had some decent documentation on bfd, and
8459 knew what to expect of the library, and what to
8460 demand of applications. For example, it
8461 appears that `objcopy' might not set the
8462 section of a symbol to be a section that is
8463 actually in the output file. */
8464 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8465 if (sec2 != NULL)
8466 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8467 if (shndx == SHN_BAD)
589e6347 8468 {
695344c0 8469 /* xgettext:c-format */
9793eb77
AM
8470 _bfd_error_handler
8471 (_("unable to find equivalent output section"
8472 " for symbol '%s' from section '%s'"),
8473 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8474 sec->name);
811072d8 8475 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8476 goto error_return;
589e6347 8477 }
079e9a2f
AM
8478 }
8479 }
252b5132 8480
079e9a2f
AM
8481 sym.st_shndx = shndx;
8482 }
252b5132 8483
13ae64f3
JJ
8484 if ((flags & BSF_THREAD_LOCAL) != 0)
8485 type = STT_TLS;
d8045f23
NC
8486 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8487 type = STT_GNU_IFUNC;
13ae64f3 8488 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8489 type = STT_FUNC;
8490 else if ((flags & BSF_OBJECT) != 0)
8491 type = STT_OBJECT;
d9352518
DB
8492 else if ((flags & BSF_RELC) != 0)
8493 type = STT_RELC;
8494 else if ((flags & BSF_SRELC) != 0)
8495 type = STT_SRELC;
079e9a2f
AM
8496 else
8497 type = STT_NOTYPE;
252b5132 8498
13ae64f3
JJ
8499 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8500 type = STT_TLS;
8501
589e6347 8502 /* Processor-specific types. */
079e9a2f
AM
8503 if (type_ptr != NULL
8504 && bed->elf_backend_get_symbol_type)
8505 type = ((*bed->elf_backend_get_symbol_type)
8506 (&type_ptr->internal_elf_sym, type));
252b5132 8507
079e9a2f
AM
8508 if (flags & BSF_SECTION_SYM)
8509 {
8510 if (flags & BSF_GLOBAL)
8511 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8512 else
8513 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8514 }
8515 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8516 {
b8871f35
L
8517 if (type != STT_TLS)
8518 {
8519 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8520 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8521 ? STT_COMMON : STT_OBJECT);
8522 else
8523 type = ((flags & BSF_ELF_COMMON) != 0
8524 ? STT_COMMON : STT_OBJECT);
8525 }
8526 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8527 }
079e9a2f
AM
8528 else if (bfd_is_und_section (syms[idx]->section))
8529 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8530 ? STB_WEAK
8531 : STB_GLOBAL),
8532 type);
8533 else if (flags & BSF_FILE)
8534 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8535 else
8536 {
8537 int bind = STB_LOCAL;
252b5132 8538
079e9a2f
AM
8539 if (flags & BSF_LOCAL)
8540 bind = STB_LOCAL;
3e7a7d11
NC
8541 else if (flags & BSF_GNU_UNIQUE)
8542 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8543 else if (flags & BSF_WEAK)
8544 bind = STB_WEAK;
8545 else if (flags & BSF_GLOBAL)
8546 bind = STB_GLOBAL;
252b5132 8547
079e9a2f
AM
8548 sym.st_info = ELF_ST_INFO (bind, type);
8549 }
252b5132 8550
079e9a2f 8551 if (type_ptr != NULL)
35fc36a8
RS
8552 {
8553 sym.st_other = type_ptr->internal_elf_sym.st_other;
8554 sym.st_target_internal
8555 = type_ptr->internal_elf_sym.st_target_internal;
8556 }
079e9a2f 8557 else
35fc36a8
RS
8558 {
8559 sym.st_other = 0;
8560 sym.st_target_internal = 0;
8561 }
252b5132 8562
ef10c3ac
L
8563 idx++;
8564 symstrtab[idx].sym = sym;
8565 symstrtab[idx].dest_index = outbound_syms_index;
ef10c3ac
L
8566
8567 outbound_syms_index++;
ef10c3ac
L
8568 }
8569
8570 /* Finalize the .strtab section. */
8571 _bfd_elf_strtab_finalize (stt);
8572
8573 /* Swap out the .strtab section. */
8574 for (idx = 0; idx <= symcount; idx++)
8575 {
8576 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8577 if (elfsym->sym.st_name == (unsigned long) -1)
8578 elfsym->sym.st_name = 0;
8579 else
8580 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8581 elfsym->sym.st_name);
3d16b64e
NA
8582 if (info && info->callbacks->ctf_new_symbol)
8583 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8584 &elfsym->sym);
8585
8586 /* Inform the linker of the addition of this symbol. */
8587
ef10c3ac
L
8588 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8589 (outbound_syms
8590 + (elfsym->dest_index
8591 * bed->s->sizeof_sym)),
b03b65e2
AM
8592 NPTR_ADD (outbound_shndx,
8593 (elfsym->dest_index
8594 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8595 }
ef10c3ac 8596 free (symstrtab);
252b5132 8597
079e9a2f 8598 *sttp = stt;
ef10c3ac 8599 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8600 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8601 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8602 symstrtab_hdr->sh_addr = 0;
8603 symstrtab_hdr->sh_entsize = 0;
8604 symstrtab_hdr->sh_link = 0;
8605 symstrtab_hdr->sh_info = 0;
8606 symstrtab_hdr->sh_addralign = 1;
252b5132 8607
0a1b45a2 8608 return true;
252b5132
RH
8609}
8610
8611/* Return the number of bytes required to hold the symtab vector.
8612
8613 Note that we base it on the count plus 1, since we will null terminate
8614 the vector allocated based on this size. However, the ELF symbol table
8615 always has a dummy entry as symbol #0, so it ends up even. */
8616
8617long
217aa764 8618_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8619{
3a551c7a 8620 bfd_size_type symcount;
252b5132
RH
8621 long symtab_size;
8622 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8623
8624 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8625 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8626 {
8627 bfd_set_error (bfd_error_file_too_big);
8628 return -1;
8629 }
b5f386d5
AM
8630 symtab_size = symcount * (sizeof (asymbol *));
8631 if (symcount == 0)
8632 symtab_size = sizeof (asymbol *);
8633 else if (!bfd_write_p (abfd))
8634 {
8635 ufile_ptr filesize = bfd_get_file_size (abfd);
8636
8637 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8638 {
8639 bfd_set_error (bfd_error_file_truncated);
8640 return -1;
8641 }
8642 }
252b5132
RH
8643
8644 return symtab_size;
8645}
8646
8647long
217aa764 8648_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8649{
3a551c7a 8650 bfd_size_type symcount;
252b5132
RH
8651 long symtab_size;
8652 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8653
8654 if (elf_dynsymtab (abfd) == 0)
8655 {
8656 bfd_set_error (bfd_error_invalid_operation);
8657 return -1;
8658 }
8659
8660 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8661 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8662 {
8663 bfd_set_error (bfd_error_file_too_big);
8664 return -1;
8665 }
b5f386d5
AM
8666 symtab_size = symcount * (sizeof (asymbol *));
8667 if (symcount == 0)
8668 symtab_size = sizeof (asymbol *);
8669 else if (!bfd_write_p (abfd))
8670 {
8671 ufile_ptr filesize = bfd_get_file_size (abfd);
8672
8673 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8674 {
8675 bfd_set_error (bfd_error_file_truncated);
8676 return -1;
8677 }
8678 }
252b5132
RH
8679
8680 return symtab_size;
8681}
8682
8683long
3c568b8a 8684_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8685{
b5f386d5 8686 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8687 {
8688 /* Sanity check reloc section size. */
8689 struct bfd_elf_section_data *d = elf_section_data (asect);
8690 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8691 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8692 ufile_ptr filesize = bfd_get_file_size (abfd);
8693
8694 if (filesize != 0 && ext_rel_size > filesize)
8695 {
8696 bfd_set_error (bfd_error_file_truncated);
8697 return -1;
8698 }
8699 }
8700
242a1159 8701#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8702 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8703 {
8704 bfd_set_error (bfd_error_file_too_big);
8705 return -1;
8706 }
242a1159 8707#endif
5cfe19e5 8708 return (asect->reloc_count + 1L) * sizeof (arelent *);
252b5132
RH
8709}
8710
8711/* Canonicalize the relocs. */
8712
8713long
217aa764
AM
8714_bfd_elf_canonicalize_reloc (bfd *abfd,
8715 sec_ptr section,
8716 arelent **relptr,
8717 asymbol **symbols)
252b5132
RH
8718{
8719 arelent *tblptr;
8720 unsigned int i;
9c5bfbb7 8721 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8722
0a1b45a2 8723 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
8724 return -1;
8725
8726 tblptr = section->relocation;
8727 for (i = 0; i < section->reloc_count; i++)
8728 *relptr++ = tblptr++;
8729
8730 *relptr = NULL;
8731
8732 return section->reloc_count;
8733}
8734
8735long
6cee3f79 8736_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8737{
9c5bfbb7 8738 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8739 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
252b5132
RH
8740
8741 if (symcount >= 0)
ed48ec2e 8742 abfd->symcount = symcount;
252b5132
RH
8743 return symcount;
8744}
8745
8746long
217aa764
AM
8747_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8748 asymbol **allocation)
252b5132 8749{
9c5bfbb7 8750 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8751 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
1f70368c
DJ
8752
8753 if (symcount >= 0)
ed48ec2e 8754 abfd->dynsymcount = symcount;
1f70368c 8755 return symcount;
252b5132
RH
8756}
8757
8615f3f2
AM
8758/* Return the size required for the dynamic reloc entries. Any loadable
8759 section that was actually installed in the BFD, and has type SHT_REL
8760 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8761 dynamic reloc section. */
252b5132
RH
8762
8763long
217aa764 8764_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8765{
3c568b8a 8766 bfd_size_type count, ext_rel_size;
252b5132
RH
8767 asection *s;
8768
8769 if (elf_dynsymtab (abfd) == 0)
8770 {
8771 bfd_set_error (bfd_error_invalid_operation);
8772 return -1;
8773 }
8774
3a551c7a 8775 count = 1;
3c568b8a 8776 ext_rel_size = 0;
252b5132 8777 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8778 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8779 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8780 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8781 {
3c568b8a
AM
8782 ext_rel_size += s->size;
8783 if (ext_rel_size < s->size)
8784 {
8785 bfd_set_error (bfd_error_file_truncated);
8786 return -1;
8787 }
3a551c7a
AM
8788 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8789 if (count > LONG_MAX / sizeof (arelent *))
8790 {
8791 bfd_set_error (bfd_error_file_too_big);
8792 return -1;
8793 }
8794 }
b5f386d5 8795 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8796 {
8797 /* Sanity check reloc section sizes. */
8798 ufile_ptr filesize = bfd_get_file_size (abfd);
8799 if (filesize != 0 && ext_rel_size > filesize)
8800 {
8801 bfd_set_error (bfd_error_file_truncated);
8802 return -1;
8803 }
8804 }
3a551c7a 8805 return count * sizeof (arelent *);
252b5132
RH
8806}
8807
8615f3f2
AM
8808/* Canonicalize the dynamic relocation entries. Note that we return the
8809 dynamic relocations as a single block, although they are actually
8810 associated with particular sections; the interface, which was
8811 designed for SunOS style shared libraries, expects that there is only
8812 one set of dynamic relocs. Any loadable section that was actually
8813 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8814 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8815
8816long
217aa764
AM
8817_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8818 arelent **storage,
8819 asymbol **syms)
252b5132 8820{
0a1b45a2 8821 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
252b5132
RH
8822 asection *s;
8823 long ret;
8824
8825 if (elf_dynsymtab (abfd) == 0)
8826 {
8827 bfd_set_error (bfd_error_invalid_operation);
8828 return -1;
8829 }
8830
8831 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8832 ret = 0;
8833 for (s = abfd->sections; s != NULL; s = s->next)
8834 {
266b05cf 8835 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8836 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8837 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8838 {
8839 arelent *p;
8840 long count, i;
8841
0a1b45a2 8842 if (! (*slurp_relocs) (abfd, s, syms, true))
252b5132 8843 return -1;
eea6121a 8844 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8845 p = s->relocation;
8846 for (i = 0; i < count; i++)
8847 *storage++ = p++;
8848 ret += count;
8849 }
8850 }
8851
8852 *storage = NULL;
8853
8854 return ret;
8855}
8856\f
8857/* Read in the version information. */
8858
0a1b45a2
AM
8859bool
8860_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
252b5132
RH
8861{
8862 bfd_byte *contents = NULL;
fc0e6df6 8863 unsigned int freeidx = 0;
1f4361a7 8864 size_t amt;
fc0e6df6
PB
8865
8866 if (elf_dynverref (abfd) != 0)
8867 {
8868 Elf_Internal_Shdr *hdr;
8869 Elf_External_Verneed *everneed;
8870 Elf_Internal_Verneed *iverneed;
8871 unsigned int i;
d0fb9a8d 8872 bfd_byte *contents_end;
fc0e6df6
PB
8873
8874 hdr = &elf_tdata (abfd)->dynverref_hdr;
8875
bd61e135
AM
8876 if (hdr->sh_info == 0
8877 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8878 {
dc1e8a47 8879 error_return_bad_verref:
4eca0228 8880 _bfd_error_handler
871b3ab2 8881 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8882 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8883 error_return_verref:
d0fb9a8d
JJ
8884 elf_tdata (abfd)->verref = NULL;
8885 elf_tdata (abfd)->cverrefs = 0;
8886 goto error_return;
8887 }
601a03ba 8888
2bb3687b
AM
8889 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8890 goto error_return_verref;
8891 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8892 if (contents == NULL)
d0fb9a8d 8893 goto error_return_verref;
fc0e6df6 8894
1f4361a7
AM
8895 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8896 {
8897 bfd_set_error (bfd_error_file_too_big);
8898 goto error_return_verref;
8899 }
8900 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8901 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8902 goto error_return_verref;
8903
8904 BFD_ASSERT (sizeof (Elf_External_Verneed)
8905 == sizeof (Elf_External_Vernaux));
8906 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8907 everneed = (Elf_External_Verneed *) contents;
8908 iverneed = elf_tdata (abfd)->verref;
8909 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8910 {
8911 Elf_External_Vernaux *evernaux;
8912 Elf_Internal_Vernaux *ivernaux;
8913 unsigned int j;
8914
8915 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8916
8917 iverneed->vn_bfd = abfd;
8918
8919 iverneed->vn_filename =
8920 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8921 iverneed->vn_file);
8922 if (iverneed->vn_filename == NULL)
601a03ba 8923 goto error_return_bad_verref;
fc0e6df6 8924
d0fb9a8d
JJ
8925 if (iverneed->vn_cnt == 0)
8926 iverneed->vn_auxptr = NULL;
8927 else
8928 {
1f4361a7
AM
8929 if (_bfd_mul_overflow (iverneed->vn_cnt,
8930 sizeof (Elf_Internal_Vernaux), &amt))
8931 {
8932 bfd_set_error (bfd_error_file_too_big);
8933 goto error_return_verref;
8934 }
a50b1753 8935 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8936 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8937 if (iverneed->vn_auxptr == NULL)
8938 goto error_return_verref;
8939 }
8940
8941 if (iverneed->vn_aux
8942 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8943 goto error_return_bad_verref;
fc0e6df6
PB
8944
8945 evernaux = ((Elf_External_Vernaux *)
8946 ((bfd_byte *) everneed + iverneed->vn_aux));
8947 ivernaux = iverneed->vn_auxptr;
8948 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8949 {
8950 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8951
8952 ivernaux->vna_nodename =
8953 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8954 ivernaux->vna_name);
8955 if (ivernaux->vna_nodename == NULL)
601a03ba 8956 goto error_return_bad_verref;
fc0e6df6 8957
25ff461f
AM
8958 if (ivernaux->vna_other > freeidx)
8959 freeidx = ivernaux->vna_other;
8960
8961 ivernaux->vna_nextptr = NULL;
8962 if (ivernaux->vna_next == 0)
8963 {
8964 iverneed->vn_cnt = j + 1;
8965 break;
8966 }
fc0e6df6
PB
8967 if (j + 1 < iverneed->vn_cnt)
8968 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8969
d0fb9a8d
JJ
8970 if (ivernaux->vna_next
8971 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8972 goto error_return_bad_verref;
d0fb9a8d 8973
fc0e6df6
PB
8974 evernaux = ((Elf_External_Vernaux *)
8975 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8976 }
8977
25ff461f
AM
8978 iverneed->vn_nextref = NULL;
8979 if (iverneed->vn_next == 0)
8980 break;
fc0e6df6
PB
8981 if (i + 1 < hdr->sh_info)
8982 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8983
d0fb9a8d
JJ
8984 if (iverneed->vn_next
8985 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8986 goto error_return_bad_verref;
d0fb9a8d 8987
fc0e6df6
PB
8988 everneed = ((Elf_External_Verneed *)
8989 ((bfd_byte *) everneed + iverneed->vn_next));
8990 }
25ff461f 8991 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8992
8993 free (contents);
8994 contents = NULL;
8995 }
252b5132
RH
8996
8997 if (elf_dynverdef (abfd) != 0)
8998 {
8999 Elf_Internal_Shdr *hdr;
9000 Elf_External_Verdef *everdef;
9001 Elf_Internal_Verdef *iverdef;
f631889e
UD
9002 Elf_Internal_Verdef *iverdefarr;
9003 Elf_Internal_Verdef iverdefmem;
252b5132 9004 unsigned int i;
062e2358 9005 unsigned int maxidx;
d0fb9a8d 9006 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
9007
9008 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9009
601a03ba
AM
9010 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
9011 {
9012 error_return_bad_verdef:
4eca0228 9013 _bfd_error_handler
871b3ab2 9014 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
9015 bfd_set_error (bfd_error_bad_value);
9016 error_return_verdef:
9017 elf_tdata (abfd)->verdef = NULL;
9018 elf_tdata (abfd)->cverdefs = 0;
9019 goto error_return;
9020 }
9021
2bb3687b 9022 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 9023 goto error_return_verdef;
2bb3687b
AM
9024 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9025 if (contents == NULL)
601a03ba 9026 goto error_return_verdef;
d0fb9a8d
JJ
9027
9028 BFD_ASSERT (sizeof (Elf_External_Verdef)
9029 >= sizeof (Elf_External_Verdaux));
9030 contents_end_def = contents + hdr->sh_size
9031 - sizeof (Elf_External_Verdef);
9032 contents_end_aux = contents + hdr->sh_size
9033 - sizeof (Elf_External_Verdaux);
9034
f631889e
UD
9035 /* We know the number of entries in the section but not the maximum
9036 index. Therefore we have to run through all entries and find
9037 the maximum. */
252b5132 9038 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
9039 maxidx = 0;
9040 for (i = 0; i < hdr->sh_info; ++i)
9041 {
9042 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9043
601a03ba
AM
9044 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9045 goto error_return_bad_verdef;
062e2358
AM
9046 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9047 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 9048
25ff461f
AM
9049 if (iverdefmem.vd_next == 0)
9050 break;
9051
d0fb9a8d
JJ
9052 if (iverdefmem.vd_next
9053 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 9054 goto error_return_bad_verdef;
d0fb9a8d 9055
f631889e
UD
9056 everdef = ((Elf_External_Verdef *)
9057 ((bfd_byte *) everdef + iverdefmem.vd_next));
9058 }
9059
fc0e6df6
PB
9060 if (default_imported_symver)
9061 {
9062 if (freeidx > maxidx)
9063 maxidx = ++freeidx;
9064 else
9065 freeidx = ++maxidx;
9066 }
1f4361a7
AM
9067 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9068 {
9069 bfd_set_error (bfd_error_file_too_big);
9070 goto error_return_verdef;
9071 }
9072 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 9073 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 9074 goto error_return_verdef;
f631889e
UD
9075
9076 elf_tdata (abfd)->cverdefs = maxidx;
9077
9078 everdef = (Elf_External_Verdef *) contents;
9079 iverdefarr = elf_tdata (abfd)->verdef;
9080 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
9081 {
9082 Elf_External_Verdaux *everdaux;
9083 Elf_Internal_Verdaux *iverdaux;
9084 unsigned int j;
9085
f631889e
UD
9086 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9087
d0fb9a8d 9088 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 9089 goto error_return_bad_verdef;
d0fb9a8d 9090
f631889e 9091 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 9092 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
9093
9094 iverdef->vd_bfd = abfd;
9095
d0fb9a8d
JJ
9096 if (iverdef->vd_cnt == 0)
9097 iverdef->vd_auxptr = NULL;
9098 else
9099 {
1f4361a7
AM
9100 if (_bfd_mul_overflow (iverdef->vd_cnt,
9101 sizeof (Elf_Internal_Verdaux), &amt))
9102 {
9103 bfd_set_error (bfd_error_file_too_big);
9104 goto error_return_verdef;
9105 }
a50b1753 9106 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 9107 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
9108 if (iverdef->vd_auxptr == NULL)
9109 goto error_return_verdef;
9110 }
9111
9112 if (iverdef->vd_aux
9113 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 9114 goto error_return_bad_verdef;
252b5132
RH
9115
9116 everdaux = ((Elf_External_Verdaux *)
9117 ((bfd_byte *) everdef + iverdef->vd_aux));
9118 iverdaux = iverdef->vd_auxptr;
9119 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9120 {
9121 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9122
9123 iverdaux->vda_nodename =
9124 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9125 iverdaux->vda_name);
9126 if (iverdaux->vda_nodename == NULL)
601a03ba 9127 goto error_return_bad_verdef;
252b5132 9128
25ff461f
AM
9129 iverdaux->vda_nextptr = NULL;
9130 if (iverdaux->vda_next == 0)
9131 {
9132 iverdef->vd_cnt = j + 1;
9133 break;
9134 }
252b5132
RH
9135 if (j + 1 < iverdef->vd_cnt)
9136 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 9137
d0fb9a8d
JJ
9138 if (iverdaux->vda_next
9139 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 9140 goto error_return_bad_verdef;
d0fb9a8d 9141
252b5132
RH
9142 everdaux = ((Elf_External_Verdaux *)
9143 ((bfd_byte *) everdaux + iverdaux->vda_next));
9144 }
9145
595bce75 9146 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
9147 if (iverdef->vd_cnt)
9148 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 9149
25ff461f
AM
9150 iverdef->vd_nextdef = NULL;
9151 if (iverdef->vd_next == 0)
9152 break;
d0fb9a8d 9153 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 9154 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
9155
9156 everdef = ((Elf_External_Verdef *)
9157 ((bfd_byte *) everdef + iverdef->vd_next));
9158 }
9159
9160 free (contents);
9161 contents = NULL;
9162 }
fc0e6df6 9163 else if (default_imported_symver)
252b5132 9164 {
fc0e6df6
PB
9165 if (freeidx < 3)
9166 freeidx = 3;
9167 else
9168 freeidx++;
252b5132 9169
1f4361a7
AM
9170 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9171 {
9172 bfd_set_error (bfd_error_file_too_big);
9173 goto error_return;
9174 }
9175 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9176 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9177 goto error_return;
9178
fc0e6df6
PB
9179 elf_tdata (abfd)->cverdefs = freeidx;
9180 }
252b5132 9181
fc0e6df6
PB
9182 /* Create a default version based on the soname. */
9183 if (default_imported_symver)
9184 {
9185 Elf_Internal_Verdef *iverdef;
9186 Elf_Internal_Verdaux *iverdaux;
252b5132 9187
5bb3703f 9188 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9189
fc0e6df6
PB
9190 iverdef->vd_version = VER_DEF_CURRENT;
9191 iverdef->vd_flags = 0;
9192 iverdef->vd_ndx = freeidx;
9193 iverdef->vd_cnt = 1;
252b5132 9194
fc0e6df6 9195 iverdef->vd_bfd = abfd;
252b5132 9196
fc0e6df6
PB
9197 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9198 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9199 goto error_return_verdef;
fc0e6df6 9200 iverdef->vd_nextdef = NULL;
601a03ba
AM
9201 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9202 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9203 if (iverdef->vd_auxptr == NULL)
9204 goto error_return_verdef;
252b5132 9205
fc0e6df6
PB
9206 iverdaux = iverdef->vd_auxptr;
9207 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9208 }
9209
0a1b45a2 9210 return true;
252b5132
RH
9211
9212 error_return:
c9594989 9213 free (contents);
0a1b45a2 9214 return false;
252b5132
RH
9215}
9216\f
9217asymbol *
217aa764 9218_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9219{
9220 elf_symbol_type *newsym;
9221
7a6e0d89 9222 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9223 if (!newsym)
9224 return NULL;
201159ec
NC
9225 newsym->symbol.the_bfd = abfd;
9226 return &newsym->symbol;
252b5132
RH
9227}
9228
9229void
217aa764
AM
9230_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9231 asymbol *symbol,
9232 symbol_info *ret)
252b5132
RH
9233{
9234 bfd_symbol_info (symbol, ret);
9235}
9236
9237/* Return whether a symbol name implies a local symbol. Most targets
9238 use this function for the is_local_label_name entry point, but some
9239 override it. */
9240
0a1b45a2 9241bool
217aa764
AM
9242_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9243 const char *name)
252b5132
RH
9244{
9245 /* Normal local symbols start with ``.L''. */
9246 if (name[0] == '.' && name[1] == 'L')
0a1b45a2 9247 return true;
252b5132
RH
9248
9249 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9250 DWARF debugging symbols starting with ``..''. */
9251 if (name[0] == '.' && name[1] == '.')
0a1b45a2 9252 return true;
252b5132
RH
9253
9254 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9255 emitting DWARF debugging output. I suspect this is actually a
9256 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9257 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9258 underscore to be emitted on some ELF targets). For ease of use,
9259 we treat such symbols as local. */
9260 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
0a1b45a2 9261 return true;
252b5132 9262
b1fa9dd6
NC
9263 /* Treat assembler generated fake symbols, dollar local labels and
9264 forward-backward labels (aka local labels) as locals.
9265 These labels have the form:
9266
07d6d2b8 9267 L0^A.* (fake symbols)
b1fa9dd6
NC
9268
9269 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9270
9271 Versions which start with .L will have already been matched above,
9272 so we only need to match the rest. */
9273 if (name[0] == 'L' && ISDIGIT (name[1]))
9274 {
0a1b45a2 9275 bool ret = false;
b1fa9dd6
NC
9276 const char * p;
9277 char c;
9278
9279 for (p = name + 2; (c = *p); p++)
9280 {
9281 if (c == 1 || c == 2)
9282 {
9283 if (c == 1 && p == name + 2)
9284 /* A fake symbol. */
0a1b45a2 9285 return true;
b1fa9dd6
NC
9286
9287 /* FIXME: We are being paranoid here and treating symbols like
9288 L0^Bfoo as if there were non-local, on the grounds that the
9289 assembler will never generate them. But can any symbol
9290 containing an ASCII value in the range 1-31 ever be anything
9291 other than some kind of local ? */
0a1b45a2 9292 ret = true;
b1fa9dd6
NC
9293 }
9294
9295 if (! ISDIGIT (c))
9296 {
0a1b45a2 9297 ret = false;
b1fa9dd6
NC
9298 break;
9299 }
9300 }
9301 return ret;
9302 }
ffa54770 9303
0a1b45a2 9304 return false;
252b5132
RH
9305}
9306
9307alent *
217aa764
AM
9308_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9309 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9310{
9311 abort ();
9312 return NULL;
9313}
9314
0a1b45a2 9315bool
217aa764
AM
9316_bfd_elf_set_arch_mach (bfd *abfd,
9317 enum bfd_architecture arch,
9318 unsigned long machine)
252b5132
RH
9319{
9320 /* If this isn't the right architecture for this backend, and this
9321 isn't the generic backend, fail. */
9322 if (arch != get_elf_backend_data (abfd)->arch
9323 && arch != bfd_arch_unknown
9324 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
0a1b45a2 9325 return false;
252b5132
RH
9326
9327 return bfd_default_set_arch_mach (abfd, arch, machine);
9328}
9329
d1fad7c6
NC
9330/* Find the nearest line to a particular section and offset,
9331 for error reporting. */
9332
0a1b45a2 9333bool
217aa764 9334_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9335 asymbol **symbols,
fb167eb2 9336 asection *section,
217aa764
AM
9337 bfd_vma offset,
9338 const char **filename_ptr,
9339 const char **functionname_ptr,
fb167eb2
AM
9340 unsigned int *line_ptr,
9341 unsigned int *discriminator_ptr)
6e7a29c7
AM
9342{
9343 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9344 offset, filename_ptr,
9345 functionname_ptr, line_ptr,
9346 discriminator_ptr);
9347}
9348
9349/* Find the nearest line to a particular section and offset,
9350 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9351 can be optionally specified. */
9352
9353bool
9354_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9355 const char *alt_filename,
9356 asymbol **symbols,
9357 asection *section,
9358 bfd_vma offset,
9359 const char **filename_ptr,
9360 const char **functionname_ptr,
9361 unsigned int *line_ptr,
9362 unsigned int *discriminator_ptr)
d1fad7c6 9363{
0a1b45a2 9364 bool found;
d1fad7c6 9365
6e7a29c7
AM
9366 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9367 section, offset, filename_ptr,
9368 functionname_ptr, line_ptr,
9369 discriminator_ptr,
9370 dwarf_debug_sections,
9371 &elf_tdata (abfd)->dwarf2_find_line_info))
0a1b45a2 9372 return true;
e7679060
AM
9373
9374 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9375 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9376 {
9377 if (!*functionname_ptr)
e00e8198
AM
9378 _bfd_elf_find_function (abfd, symbols, section, offset,
9379 *filename_ptr ? NULL : filename_ptr,
9380 functionname_ptr);
0a1b45a2 9381 return true;
d1fad7c6
NC
9382 }
9383
9384 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9385 &found, filename_ptr,
9386 functionname_ptr, line_ptr,
9387 &elf_tdata (abfd)->line_info))
0a1b45a2 9388 return false;
dc43ada5 9389 if (found && (*functionname_ptr || *line_ptr))
0a1b45a2 9390 return true;
d1fad7c6
NC
9391
9392 if (symbols == NULL)
0a1b45a2 9393 return false;
d1fad7c6 9394
e00e8198
AM
9395 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9396 filename_ptr, functionname_ptr))
0a1b45a2 9397 return false;
d1fad7c6 9398
252b5132 9399 *line_ptr = 0;
0a1b45a2 9400 return true;
252b5132
RH
9401}
9402
5420f73d
L
9403/* Find the line for a symbol. */
9404
0a1b45a2 9405bool
5420f73d
L
9406_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9407 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9408{
11c6a7c6 9409 struct elf_obj_tdata *tdata = elf_tdata (abfd);
fb167eb2
AM
9410 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9411 filename_ptr, NULL, line_ptr, NULL,
9defd221 9412 dwarf_debug_sections,
11c6a7c6 9413 &tdata->dwarf2_find_line_info);
5420f73d
L
9414}
9415
4ab527b0
FF
9416/* After a call to bfd_find_nearest_line, successive calls to
9417 bfd_find_inliner_info can be used to get source information about
9418 each level of function inlining that terminated at the address
9419 passed to bfd_find_nearest_line. Currently this is only supported
9420 for DWARF2 with appropriate DWARF3 extensions. */
9421
0a1b45a2 9422bool
4ab527b0
FF
9423_bfd_elf_find_inliner_info (bfd *abfd,
9424 const char **filename_ptr,
9425 const char **functionname_ptr,
9426 unsigned int *line_ptr)
9427{
11c6a7c6
AM
9428 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9429 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9430 functionname_ptr, line_ptr,
9431 &tdata->dwarf2_find_line_info);
4ab527b0
FF
9432}
9433
252b5132 9434int
a6b96beb 9435_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9436{
8ded5a0f
AM
9437 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9438 int ret = bed->s->sizeof_ehdr;
252b5132 9439
0e1862bb 9440 if (!bfd_link_relocatable (info))
8ded5a0f 9441 {
12bd6957 9442 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9443
62d7a5f6
AM
9444 if (phdr_size == (bfd_size_type) -1)
9445 {
9446 struct elf_segment_map *m;
9447
9448 phdr_size = 0;
12bd6957 9449 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9450 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9451
62d7a5f6
AM
9452 if (phdr_size == 0)
9453 phdr_size = get_program_header_size (abfd, info);
9454 }
8ded5a0f 9455
12bd6957 9456 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9457 ret += phdr_size;
9458 }
9459
252b5132
RH
9460 return ret;
9461}
9462
0a1b45a2 9463bool
217aa764
AM
9464_bfd_elf_set_section_contents (bfd *abfd,
9465 sec_ptr section,
0f867abe 9466 const void *location,
217aa764
AM
9467 file_ptr offset,
9468 bfd_size_type count)
252b5132
RH
9469{
9470 Elf_Internal_Shdr *hdr;
9471
9472 if (! abfd->output_has_begun
217aa764 9473 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 9474 return false;
252b5132 9475
0ce398f1 9476 if (!count)
0a1b45a2 9477 return true;
0ce398f1 9478
252b5132 9479 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9480 if (hdr->sh_offset == (file_ptr) -1)
9481 {
a0dcf297
NC
9482 unsigned char *contents;
9483
1ff6de03
NA
9484 if (bfd_section_is_ctf (section))
9485 /* Nothing to do with this section: the contents are generated
9486 later. */
0a1b45a2 9487 return true;
1ff6de03 9488
a0dcf297
NC
9489 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9490 {
9491 _bfd_error_handler
11c6a7c6
AM
9492 (_("%pB:%pA: error: attempting to write"
9493 " into an unallocated compressed section"),
a0dcf297
NC
9494 abfd, section);
9495 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9496 return false;
a0dcf297 9497 }
11c6a7c6 9498
a0dcf297
NC
9499 if ((offset + count) > hdr->sh_size)
9500 {
9501 _bfd_error_handler
11c6a7c6
AM
9502 (_("%pB:%pA: error: attempting to write"
9503 " over the end of the section"),
a0dcf297
NC
9504 abfd, section);
9505
9506 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9507 return false;
a0dcf297
NC
9508 }
9509
9510 contents = hdr->contents;
9511 if (contents == NULL)
9512 {
9513 _bfd_error_handler
11c6a7c6
AM
9514 (_("%pB:%pA: error: attempting to write"
9515 " section into an empty buffer"),
a0dcf297
NC
9516 abfd, section);
9517
9518 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9519 return false;
a0dcf297
NC
9520 }
9521
0ce398f1 9522 memcpy (contents + offset, location, count);
0a1b45a2 9523 return true;
0ce398f1 9524 }
a0dcf297 9525
169692ce
PA
9526 return _bfd_generic_set_section_contents (abfd, section,
9527 location, offset, count);
252b5132
RH
9528}
9529
0a1b45a2 9530bool
217aa764
AM
9531_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9532 arelent *cache_ptr ATTRIBUTE_UNUSED,
9533 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9534{
9535 abort ();
0a1b45a2 9536 return false;
252b5132
RH
9537}
9538
252b5132
RH
9539/* Try to convert a non-ELF reloc into an ELF one. */
9540
0a1b45a2 9541bool
217aa764 9542_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9543{
c044fabd 9544 /* Check whether we really have an ELF howto. */
252b5132
RH
9545
9546 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9547 {
9548 bfd_reloc_code_real_type code;
9549 reloc_howto_type *howto;
9550
9551 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9552 equivalent ELF reloc. */
252b5132
RH
9553
9554 if (areloc->howto->pc_relative)
9555 {
9556 switch (areloc->howto->bitsize)
9557 {
9558 case 8:
9559 code = BFD_RELOC_8_PCREL;
9560 break;
9561 case 12:
9562 code = BFD_RELOC_12_PCREL;
9563 break;
9564 case 16:
9565 code = BFD_RELOC_16_PCREL;
9566 break;
9567 case 24:
9568 code = BFD_RELOC_24_PCREL;
9569 break;
9570 case 32:
9571 code = BFD_RELOC_32_PCREL;
9572 break;
9573 case 64:
9574 code = BFD_RELOC_64_PCREL;
9575 break;
9576 default:
9577 goto fail;
9578 }
9579
9580 howto = bfd_reloc_type_lookup (abfd, code);
9581
94698d01 9582 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9583 {
9584 if (howto->pcrel_offset)
9585 areloc->addend += areloc->address;
9586 else
9587 areloc->addend -= areloc->address; /* addend is unsigned!! */
9588 }
9589 }
9590 else
9591 {
9592 switch (areloc->howto->bitsize)
9593 {
9594 case 8:
9595 code = BFD_RELOC_8;
9596 break;
9597 case 14:
9598 code = BFD_RELOC_14;
9599 break;
9600 case 16:
9601 code = BFD_RELOC_16;
9602 break;
9603 case 26:
9604 code = BFD_RELOC_26;
9605 break;
9606 case 32:
9607 code = BFD_RELOC_32;
9608 break;
9609 case 64:
9610 code = BFD_RELOC_64;
9611 break;
9612 default:
9613 goto fail;
9614 }
9615
9616 howto = bfd_reloc_type_lookup (abfd, code);
9617 }
9618
9619 if (howto)
9620 areloc->howto = howto;
9621 else
9622 goto fail;
9623 }
9624
0a1b45a2 9625 return true;
252b5132
RH
9626
9627 fail:
0aa13fee
AM
9628 /* xgettext:c-format */
9629 _bfd_error_handler (_("%pB: %s unsupported"),
9630 abfd, areloc->howto->name);
9aea1e31 9631 bfd_set_error (bfd_error_sorry);
0a1b45a2 9632 return false;
252b5132
RH
9633}
9634
0a1b45a2 9635bool
217aa764 9636_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9637{
d9071b0c 9638 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9639 if (tdata != NULL
9640 && (bfd_get_format (abfd) == bfd_object
9641 || bfd_get_format (abfd) == bfd_core))
252b5132 9642 {
c0355132 9643 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9644 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9645 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9646 }
9647
9648 return _bfd_generic_close_and_cleanup (abfd);
9649}
9650
9651/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9652 in the relocation's offset. Thus we cannot allow any sort of sanity
9653 range-checking to interfere. There is nothing else to do in processing
9654 this reloc. */
9655
9656bfd_reloc_status_type
217aa764
AM
9657_bfd_elf_rel_vtable_reloc_fn
9658 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9659 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9660 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9661 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9662{
9663 return bfd_reloc_ok;
9664}
252b5132
RH
9665\f
9666/* Elf core file support. Much of this only works on native
9667 toolchains, since we rely on knowing the
9668 machine-dependent procfs structure in order to pick
c044fabd 9669 out details about the corefile. */
252b5132
RH
9670
9671#ifdef HAVE_SYS_PROCFS_H
9672# include <sys/procfs.h>
9673#endif
9674
261b8d08
PA
9675/* Return a PID that identifies a "thread" for threaded cores, or the
9676 PID of the main process for non-threaded cores. */
252b5132
RH
9677
9678static int
217aa764 9679elfcore_make_pid (bfd *abfd)
252b5132 9680{
261b8d08
PA
9681 int pid;
9682
228e534f 9683 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9684 if (pid == 0)
228e534f 9685 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9686
9687 return pid;
252b5132
RH
9688}
9689
11c6a7c6
AM
9690/* If there isn't a section called NAME, make one, using data from
9691 SECT. Note, this function will generate a reference to NAME, so
9692 you shouldn't deallocate or overwrite it. */
252b5132 9693
0a1b45a2 9694static bool
217aa764 9695elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9696{
c044fabd 9697 asection *sect2;
252b5132
RH
9698
9699 if (bfd_get_section_by_name (abfd, name) != NULL)
0a1b45a2 9700 return true;
252b5132 9701
117ed4f8 9702 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9703 if (sect2 == NULL)
0a1b45a2 9704 return false;
252b5132 9705
eea6121a 9706 sect2->size = sect->size;
252b5132 9707 sect2->filepos = sect->filepos;
252b5132 9708 sect2->alignment_power = sect->alignment_power;
0a1b45a2 9709 return true;
252b5132
RH
9710}
9711
bb0082d6
AM
9712/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9713 actually creates up to two pseudosections:
9714 - For the single-threaded case, a section named NAME, unless
9715 such a section already exists.
9716 - For the multi-threaded case, a section named "NAME/PID", where
9717 PID is elfcore_make_pid (abfd).
24d3e51b 9718 Both pseudosections have identical contents. */
0a1b45a2 9719bool
217aa764
AM
9720_bfd_elfcore_make_pseudosection (bfd *abfd,
9721 char *name,
9722 size_t size,
9723 ufile_ptr filepos)
bb0082d6
AM
9724{
9725 char buf[100];
9726 char *threaded_name;
d4c88bbb 9727 size_t len;
bb0082d6
AM
9728 asection *sect;
9729
9730 /* Build the section name. */
9731
9732 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9733 len = strlen (buf) + 1;
a50b1753 9734 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9735 if (threaded_name == NULL)
0a1b45a2 9736 return false;
d4c88bbb 9737 memcpy (threaded_name, buf, len);
bb0082d6 9738
117ed4f8
AM
9739 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9740 SEC_HAS_CONTENTS);
bb0082d6 9741 if (sect == NULL)
0a1b45a2 9742 return false;
eea6121a 9743 sect->size = size;
bb0082d6 9744 sect->filepos = filepos;
bb0082d6
AM
9745 sect->alignment_power = 2;
9746
936e320b 9747 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9748}
9749
0a1b45a2 9750static bool
58e07198
CZ
9751elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9752 size_t offs)
9753{
9754 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9755 SEC_HAS_CONTENTS);
9756
9757 if (sect == NULL)
0a1b45a2 9758 return false;
58e07198
CZ
9759
9760 sect->size = note->descsz - offs;
9761 sect->filepos = note->descpos + offs;
9762 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9763
0a1b45a2 9764 return true;
58e07198
CZ
9765}
9766
252b5132 9767/* prstatus_t exists on:
4a938328 9768 solaris 2.5+
252b5132
RH
9769 linux 2.[01] + glibc
9770 unixware 4.2
9771*/
9772
9773#if defined (HAVE_PRSTATUS_T)
a7b97311 9774
0a1b45a2 9775static bool
217aa764 9776elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9777{
eea6121a 9778 size_t size;
7ee38065 9779 int offset;
252b5132 9780
4a938328
MS
9781 if (note->descsz == sizeof (prstatus_t))
9782 {
9783 prstatus_t prstat;
252b5132 9784
eea6121a 9785 size = sizeof (prstat.pr_reg);
7ee38065 9786 offset = offsetof (prstatus_t, pr_reg);
4a938328 9787 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9788
fa49d224
NC
9789 /* Do not overwrite the core signal if it
9790 has already been set by another thread. */
228e534f
AM
9791 if (elf_tdata (abfd)->core->signal == 0)
9792 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9793 if (elf_tdata (abfd)->core->pid == 0)
9794 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9795
4a938328
MS
9796 /* pr_who exists on:
9797 solaris 2.5+
9798 unixware 4.2
9799 pr_who doesn't exist on:
9800 linux 2.[01]
9801 */
252b5132 9802#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9803 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9804#else
228e534f 9805 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9806#endif
4a938328 9807 }
7ee38065 9808#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9809 else if (note->descsz == sizeof (prstatus32_t))
9810 {
9811 /* 64-bit host, 32-bit corefile */
9812 prstatus32_t prstat;
9813
eea6121a 9814 size = sizeof (prstat.pr_reg);
7ee38065 9815 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9816 memcpy (&prstat, note->descdata, sizeof (prstat));
9817
fa49d224
NC
9818 /* Do not overwrite the core signal if it
9819 has already been set by another thread. */
228e534f
AM
9820 if (elf_tdata (abfd)->core->signal == 0)
9821 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9822 if (elf_tdata (abfd)->core->pid == 0)
9823 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9824
9825 /* pr_who exists on:
9826 solaris 2.5+
9827 unixware 4.2
9828 pr_who doesn't exist on:
9829 linux 2.[01]
9830 */
7ee38065 9831#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9832 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9833#else
228e534f 9834 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9835#endif
9836 }
7ee38065 9837#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9838 else
9839 {
9840 /* Fail - we don't know how to handle any other
9841 note size (ie. data object type). */
0a1b45a2 9842 return true;
4a938328 9843 }
252b5132 9844
bb0082d6 9845 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9846 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9847 size, note->descpos + offset);
252b5132
RH
9848}
9849#endif /* defined (HAVE_PRSTATUS_T) */
9850
bb0082d6 9851/* Create a pseudosection containing the exact contents of NOTE. */
0a1b45a2 9852static bool
217aa764
AM
9853elfcore_make_note_pseudosection (bfd *abfd,
9854 char *name,
9855 Elf_Internal_Note *note)
252b5132 9856{
936e320b
AM
9857 return _bfd_elfcore_make_pseudosection (abfd, name,
9858 note->descsz, note->descpos);
252b5132
RH
9859}
9860
ff08c6bb
JB
9861/* There isn't a consistent prfpregset_t across platforms,
9862 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9863 data structure apart. */
9864
0a1b45a2 9865static bool
217aa764 9866elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9867{
9868 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9869}
9870
ff08c6bb 9871/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9872 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9873 literally. */
c044fabd 9874
0a1b45a2 9875static bool
217aa764 9876elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9877{
9878 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9879}
9880
4339cae0
L
9881/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9882 with a note type of NT_X86_XSTATE. Just include the whole note's
9883 contents literally. */
9884
0a1b45a2 9885static bool
4339cae0
L
9886elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9887{
9888 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9889}
9890
0a1b45a2 9891static bool
97753bd5
AM
9892elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9893{
9894 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9895}
9896
0a1b45a2 9897static bool
89eeb0bc
LM
9898elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9899{
9900 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9901}
97753bd5 9902
0a1b45a2 9903static bool
cb2366c1
EBM
9904elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9905{
9906 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9907}
9908
0a1b45a2 9909static bool
cb2366c1
EBM
9910elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9911{
9912 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9913}
9914
0a1b45a2 9915static bool
cb2366c1
EBM
9916elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9917{
9918 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9919}
9920
0a1b45a2 9921static bool
cb2366c1
EBM
9922elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9923{
9924 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9925}
9926
0a1b45a2 9927static bool
cb2366c1
EBM
9928elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9929{
9930 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9931}
9932
0a1b45a2 9933static bool
cb2366c1
EBM
9934elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9935{
9936 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9937}
9938
0a1b45a2 9939static bool
cb2366c1
EBM
9940elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9941{
9942 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9943}
9944
0a1b45a2 9945static bool
cb2366c1
EBM
9946elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9947{
9948 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9949}
9950
0a1b45a2 9951static bool
cb2366c1
EBM
9952elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9953{
9954 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9955}
9956
0a1b45a2 9957static bool
cb2366c1
EBM
9958elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9959{
9960 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9961}
9962
0a1b45a2 9963static bool
cb2366c1
EBM
9964elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9965{
9966 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9967}
9968
0a1b45a2 9969static bool
cb2366c1
EBM
9970elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9971{
9972 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9973}
9974
0a1b45a2 9975static bool
cb2366c1
EBM
9976elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9977{
9978 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9979}
9980
0a1b45a2 9981static bool
0675e188
UW
9982elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9983{
9984 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9985}
9986
0a1b45a2 9987static bool
d7eeb400
MS
9988elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9989{
9990 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9991}
9992
0a1b45a2 9993static bool
d7eeb400
MS
9994elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9995{
9996 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9997}
9998
0a1b45a2 9999static bool
d7eeb400
MS
10000elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10001{
10002 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10003}
10004
0a1b45a2 10005static bool
d7eeb400
MS
10006elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10007{
10008 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10009}
10010
0a1b45a2 10011static bool
d7eeb400
MS
10012elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10013{
10014 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10015}
10016
0a1b45a2 10017static bool
355b81d9
UW
10018elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10019{
10020 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10021}
10022
0a1b45a2 10023static bool
355b81d9
UW
10024elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10025{
10026 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10027}
10028
0a1b45a2 10029static bool
abb3f6cc
NC
10030elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10031{
10032 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10033}
10034
0a1b45a2 10035static bool
4ef9f41a
AA
10036elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10037{
10038 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10039}
10040
0a1b45a2 10041static bool
4ef9f41a
AA
10042elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10043{
10044 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10045}
10046
0a1b45a2 10047static bool
88ab90e8
AA
10048elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10049{
10050 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10051}
10052
0a1b45a2 10053static bool
88ab90e8
AA
10054elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10055{
10056 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10057}
10058
0a1b45a2 10059static bool
faa9a424
UW
10060elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10061{
10062 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10063}
10064
0a1b45a2 10065static bool
652451f8
YZ
10066elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10067{
10068 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10069}
10070
0a1b45a2 10071static bool
652451f8
YZ
10072elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10073{
10074 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10075}
10076
0a1b45a2 10077static bool
652451f8
YZ
10078elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10079{
10080 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10081}
10082
0a1b45a2 10083static bool
ad1cc4e4
AH
10084elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10085{
10086 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10087}
10088
0a1b45a2 10089static bool
e6c3b5bf
AH
10090elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10091{
10092 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10093}
10094
f0bbe8ba
LM
10095static bool
10096elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10097{
10098 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10099 note);
10100}
10101
0a1b45a2 10102static bool
27456742
AK
10103elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10104{
10105 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10106}
10107
db6092f3
AB
10108/* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10109 successful otherwise, return FALSE. */
10110
0a1b45a2 10111static bool
db6092f3
AB
10112elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10113{
10114 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10115}
10116
b63a5e38
AB
10117/* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10118 successful otherwise, return FALSE. */
10119
0a1b45a2 10120static bool
b63a5e38
AB
10121elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10122{
10123 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10124}
10125
e214f8db 10126static bool
10127elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10128{
10129 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10130}
10131
10132static bool
10133elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10134{
10135 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10136}
10137
10138static bool
10139elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10140{
10141 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10142}
10143
10144static bool
10145elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10146{
10147 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10148}
10149
252b5132 10150#if defined (HAVE_PRPSINFO_T)
4a938328 10151typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 10152#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10153typedef prpsinfo32_t elfcore_psinfo32_t;
10154#endif
252b5132
RH
10155#endif
10156
10157#if defined (HAVE_PSINFO_T)
4a938328 10158typedef psinfo_t elfcore_psinfo_t;
7ee38065 10159#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10160typedef psinfo32_t elfcore_psinfo32_t;
10161#endif
252b5132
RH
10162#endif
10163
252b5132
RH
10164/* return a malloc'ed copy of a string at START which is at
10165 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 10166 the copy will always have a terminating '\0'. */
252b5132 10167
936e320b 10168char *
217aa764 10169_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 10170{
dc810e39 10171 char *dups;
a50b1753 10172 char *end = (char *) memchr (start, '\0', max);
dc810e39 10173 size_t len;
252b5132
RH
10174
10175 if (end == NULL)
10176 len = max;
10177 else
10178 len = end - start;
10179
a50b1753 10180 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 10181 if (dups == NULL)
252b5132
RH
10182 return NULL;
10183
dc810e39
AM
10184 memcpy (dups, start, len);
10185 dups[len] = '\0';
252b5132 10186
dc810e39 10187 return dups;
252b5132
RH
10188}
10189
bb0082d6 10190#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
0a1b45a2 10191static bool
217aa764 10192elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 10193{
4a938328
MS
10194 if (note->descsz == sizeof (elfcore_psinfo_t))
10195 {
10196 elfcore_psinfo_t psinfo;
252b5132 10197
7ee38065 10198 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10199
335e41d4 10200#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 10201 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10202#endif
228e534f 10203 elf_tdata (abfd)->core->program
936e320b
AM
10204 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10205 sizeof (psinfo.pr_fname));
252b5132 10206
228e534f 10207 elf_tdata (abfd)->core->command
936e320b
AM
10208 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10209 sizeof (psinfo.pr_psargs));
4a938328 10210 }
7ee38065 10211#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
10212 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10213 {
10214 /* 64-bit host, 32-bit corefile */
10215 elfcore_psinfo32_t psinfo;
10216
7ee38065 10217 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10218
335e41d4 10219#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 10220 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10221#endif
228e534f 10222 elf_tdata (abfd)->core->program
936e320b
AM
10223 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10224 sizeof (psinfo.pr_fname));
4a938328 10225
228e534f 10226 elf_tdata (abfd)->core->command
936e320b
AM
10227 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10228 sizeof (psinfo.pr_psargs));
4a938328
MS
10229 }
10230#endif
10231
10232 else
10233 {
10234 /* Fail - we don't know how to handle any other
10235 note size (ie. data object type). */
0a1b45a2 10236 return true;
4a938328 10237 }
252b5132
RH
10238
10239 /* Note that for some reason, a spurious space is tacked
10240 onto the end of the args in some (at least one anyway)
c044fabd 10241 implementations, so strip it off if it exists. */
252b5132
RH
10242
10243 {
228e534f 10244 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10245 int n = strlen (command);
10246
10247 if (0 < n && command[n - 1] == ' ')
10248 command[n - 1] = '\0';
10249 }
10250
0a1b45a2 10251 return true;
252b5132
RH
10252}
10253#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10254
252b5132 10255#if defined (HAVE_PSTATUS_T)
0a1b45a2 10256static bool
217aa764 10257elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10258{
f572a39d
AM
10259 if (note->descsz == sizeof (pstatus_t)
10260#if defined (HAVE_PXSTATUS_T)
10261 || note->descsz == sizeof (pxstatus_t)
10262#endif
10263 )
4a938328
MS
10264 {
10265 pstatus_t pstat;
252b5132 10266
4a938328 10267 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10268
228e534f 10269 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10270 }
7ee38065 10271#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10272 else if (note->descsz == sizeof (pstatus32_t))
10273 {
10274 /* 64-bit host, 32-bit corefile */
10275 pstatus32_t pstat;
252b5132 10276
4a938328 10277 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10278
228e534f 10279 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10280 }
10281#endif
252b5132
RH
10282 /* Could grab some more details from the "representative"
10283 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10284 NT_LWPSTATUS note, presumably. */
252b5132 10285
0a1b45a2 10286 return true;
252b5132
RH
10287}
10288#endif /* defined (HAVE_PSTATUS_T) */
10289
252b5132 10290#if defined (HAVE_LWPSTATUS_T)
0a1b45a2 10291static bool
217aa764 10292elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10293{
10294 lwpstatus_t lwpstat;
10295 char buf[100];
c044fabd 10296 char *name;
d4c88bbb 10297 size_t len;
c044fabd 10298 asection *sect;
252b5132 10299
f572a39d
AM
10300 if (note->descsz != sizeof (lwpstat)
10301#if defined (HAVE_LWPXSTATUS_T)
10302 && note->descsz != sizeof (lwpxstatus_t)
10303#endif
10304 )
0a1b45a2 10305 return true;
252b5132
RH
10306
10307 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10308
228e534f 10309 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10310 /* Do not overwrite the core signal if it has already been set by
10311 another thread. */
228e534f
AM
10312 if (elf_tdata (abfd)->core->signal == 0)
10313 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10314
c044fabd 10315 /* Make a ".reg/999" section. */
252b5132
RH
10316
10317 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10318 len = strlen (buf) + 1;
217aa764 10319 name = bfd_alloc (abfd, len);
252b5132 10320 if (name == NULL)
0a1b45a2 10321 return false;
d4c88bbb 10322 memcpy (name, buf, len);
252b5132 10323
117ed4f8 10324 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10325 if (sect == NULL)
0a1b45a2 10326 return false;
252b5132
RH
10327
10328#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10329 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10330 sect->filepos = note->descpos
10331 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10332#endif
10333
10334#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10335 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10336 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10337#endif
10338
252b5132
RH
10339 sect->alignment_power = 2;
10340
10341 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10342 return false;
252b5132
RH
10343
10344 /* Make a ".reg2/999" section */
10345
10346 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10347 len = strlen (buf) + 1;
217aa764 10348 name = bfd_alloc (abfd, len);
252b5132 10349 if (name == NULL)
0a1b45a2 10350 return false;
d4c88bbb 10351 memcpy (name, buf, len);
252b5132 10352
117ed4f8 10353 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10354 if (sect == NULL)
0a1b45a2 10355 return false;
252b5132
RH
10356
10357#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10358 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10359 sect->filepos = note->descpos
10360 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10361#endif
10362
10363#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10364 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10365 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10366#endif
10367
252b5132
RH
10368 sect->alignment_power = 2;
10369
936e320b 10370 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10371}
10372#endif /* defined (HAVE_LWPSTATUS_T) */
10373
8fbac78b
JT
10374/* These constants, and the structure offsets used below, are defined by
10375 Cygwin's core_dump.h */
10376#define NOTE_INFO_PROCESS 1
10377#define NOTE_INFO_THREAD 2
10378#define NOTE_INFO_MODULE 3
d61f3d03 10379#define NOTE_INFO_MODULE64 4
8fbac78b 10380
0a1b45a2 10381static bool
217aa764 10382elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10383{
10384 char buf[30];
c044fabd 10385 char *name;
d4c88bbb 10386 size_t len;
3cdad084 10387 unsigned int name_size;
c044fabd 10388 asection *sect;
2fef9373 10389 unsigned int type;
4a6636fb
PA
10390 int is_active_thread;
10391 bfd_vma base_addr;
16e9c715 10392
04ec0fa2 10393 if (note->descsz < 4)
0a1b45a2 10394 return true;
16e9c715 10395
08dedd66 10396 if (! startswith (note->namedata, "win32"))
0a1b45a2 10397 return true;
4a6636fb
PA
10398
10399 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10400
7e0d77ef
NC
10401 struct
10402 {
404ec933
JT
10403 const char *type_name;
10404 unsigned long min_size;
10405 } size_check[] =
10406 {
10407 { "NOTE_INFO_PROCESS", 12 },
10408 { "NOTE_INFO_THREAD", 12 },
10409 { "NOTE_INFO_MODULE", 12 },
10410 { "NOTE_INFO_MODULE64", 16 },
10411 };
10412
7e0d77ef 10413 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
0a1b45a2 10414 return true;
404ec933
JT
10415
10416 if (note->descsz < size_check[type - 1].min_size)
10417 {
11c6a7c6
AM
10418 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10419 " is too small"),
10420 abfd, size_check[type - 1].type_name, note->descsz);
0a1b45a2 10421 return true;
404ec933
JT
10422 }
10423
4a6636fb 10424 switch (type)
16e9c715 10425 {
8fbac78b 10426 case NOTE_INFO_PROCESS:
228e534f 10427 /* FIXME: need to add ->core->command. */
ff2084b9 10428 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10429 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10430 break;
16e9c715 10431
8fbac78b 10432 case NOTE_INFO_THREAD:
ff2084b9 10433 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11c6a7c6 10434 structure. */
4a6636fb 10435 /* thread_info.tid */
ff2084b9 10436 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10437
d4c88bbb 10438 len = strlen (buf) + 1;
a50b1753 10439 name = (char *) bfd_alloc (abfd, len);
16e9c715 10440 if (name == NULL)
0a1b45a2 10441 return false;
c044fabd 10442
d4c88bbb 10443 memcpy (name, buf, len);
16e9c715 10444
117ed4f8 10445 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10446 if (sect == NULL)
0a1b45a2 10447 return false;
c044fabd 10448
4a6636fb 10449 /* sizeof (thread_info.thread_context) */
03c29a6f 10450 sect->size = note->descsz - 12;
4a6636fb
PA
10451 /* offsetof (thread_info.thread_context) */
10452 sect->filepos = note->descpos + 12;
16e9c715
NC
10453 sect->alignment_power = 2;
10454
4a6636fb
PA
10455 /* thread_info.is_active_thread */
10456 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10457
10458 if (is_active_thread)
16e9c715 10459 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10460 return false;
16e9c715
NC
10461 break;
10462
8fbac78b 10463 case NOTE_INFO_MODULE:
d61f3d03 10464 case NOTE_INFO_MODULE64:
16e9c715 10465 /* Make a ".module/xxxxxxxx" section. */
d61f3d03 10466 if (type == NOTE_INFO_MODULE)
11c6a7c6
AM
10467 {
10468 /* module_info.base_address */
10469 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10470 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10471 /* module_info.module_name_size */
10472 name_size = bfd_get_32 (abfd, note->descdata + 8);
10473 }
d61f3d03 10474 else /* NOTE_INFO_MODULE64 */
11c6a7c6
AM
10475 {
10476 /* module_info.base_address */
10477 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10478 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10479 /* module_info.module_name_size */
10480 name_size = bfd_get_32 (abfd, note->descdata + 12);
10481 }
c044fabd 10482
d4c88bbb 10483 len = strlen (buf) + 1;
a50b1753 10484 name = (char *) bfd_alloc (abfd, len);
16e9c715 10485 if (name == NULL)
0a1b45a2 10486 return false;
c044fabd 10487
d4c88bbb 10488 memcpy (name, buf, len);
252b5132 10489
117ed4f8 10490 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10491
16e9c715 10492 if (sect == NULL)
0a1b45a2 10493 return false;
c044fabd 10494
04ec0fa2 10495 if (note->descsz < 12 + name_size)
11c6a7c6
AM
10496 {
10497 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10498 " is too small to contain a name of size %u"),
10499 abfd, note->descsz, name_size);
10500 return true;
10501 }
04ec0fa2 10502
eea6121a 10503 sect->size = note->descsz;
16e9c715 10504 sect->filepos = note->descpos;
16e9c715
NC
10505 sect->alignment_power = 2;
10506 break;
10507
10508 default:
0a1b45a2 10509 return true;
16e9c715
NC
10510 }
10511
0a1b45a2 10512 return true;
16e9c715 10513}
252b5132 10514
0a1b45a2 10515static bool
217aa764 10516elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10517{
9c5bfbb7 10518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10519
252b5132
RH
10520 switch (note->type)
10521 {
10522 default:
0a1b45a2 10523 return true;
252b5132 10524
252b5132 10525 case NT_PRSTATUS:
bb0082d6
AM
10526 if (bed->elf_backend_grok_prstatus)
10527 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
0a1b45a2 10528 return true;
bb0082d6 10529#if defined (HAVE_PRSTATUS_T)
252b5132 10530 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10531#else
0a1b45a2 10532 return true;
252b5132
RH
10533#endif
10534
10535#if defined (HAVE_PSTATUS_T)
10536 case NT_PSTATUS:
10537 return elfcore_grok_pstatus (abfd, note);
10538#endif
10539
10540#if defined (HAVE_LWPSTATUS_T)
10541 case NT_LWPSTATUS:
10542 return elfcore_grok_lwpstatus (abfd, note);
10543#endif
10544
10545 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10546 return elfcore_grok_prfpreg (abfd, note);
10547
c044fabd 10548 case NT_WIN32PSTATUS:
16e9c715 10549 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10550
c044fabd 10551 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10552 if (note->namesz == 6
10553 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10554 return elfcore_grok_prxfpreg (abfd, note);
10555 else
0a1b45a2 10556 return true;
ff08c6bb 10557
4339cae0
L
10558 case NT_X86_XSTATE: /* Linux XSAVE extension */
10559 if (note->namesz == 6
10560 && strcmp (note->namedata, "LINUX") == 0)
10561 return elfcore_grok_xstatereg (abfd, note);
10562 else
0a1b45a2 10563 return true;
4339cae0 10564
97753bd5
AM
10565 case NT_PPC_VMX:
10566 if (note->namesz == 6
10567 && strcmp (note->namedata, "LINUX") == 0)
10568 return elfcore_grok_ppc_vmx (abfd, note);
10569 else
0a1b45a2 10570 return true;
97753bd5 10571
89eeb0bc
LM
10572 case NT_PPC_VSX:
10573 if (note->namesz == 6
07d6d2b8
AM
10574 && strcmp (note->namedata, "LINUX") == 0)
10575 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10576 else
0a1b45a2 10577 return true;
89eeb0bc 10578
cb2366c1
EBM
10579 case NT_PPC_TAR:
10580 if (note->namesz == 6
4b24dd1a
AM
10581 && strcmp (note->namedata, "LINUX") == 0)
10582 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10583 else
0a1b45a2 10584 return true;
cb2366c1
EBM
10585
10586 case NT_PPC_PPR:
10587 if (note->namesz == 6
4b24dd1a
AM
10588 && strcmp (note->namedata, "LINUX") == 0)
10589 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10590 else
0a1b45a2 10591 return true;
cb2366c1
EBM
10592
10593 case NT_PPC_DSCR:
10594 if (note->namesz == 6
4b24dd1a
AM
10595 && strcmp (note->namedata, "LINUX") == 0)
10596 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10597 else
0a1b45a2 10598 return true;
cb2366c1
EBM
10599
10600 case NT_PPC_EBB:
10601 if (note->namesz == 6
4b24dd1a
AM
10602 && strcmp (note->namedata, "LINUX") == 0)
10603 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10604 else
0a1b45a2 10605 return true;
cb2366c1
EBM
10606
10607 case NT_PPC_PMU:
10608 if (note->namesz == 6
4b24dd1a
AM
10609 && strcmp (note->namedata, "LINUX") == 0)
10610 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10611 else
0a1b45a2 10612 return true;
cb2366c1
EBM
10613
10614 case NT_PPC_TM_CGPR:
10615 if (note->namesz == 6
4b24dd1a
AM
10616 && strcmp (note->namedata, "LINUX") == 0)
10617 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10618 else
0a1b45a2 10619 return true;
cb2366c1
EBM
10620
10621 case NT_PPC_TM_CFPR:
10622 if (note->namesz == 6
4b24dd1a
AM
10623 && strcmp (note->namedata, "LINUX") == 0)
10624 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10625 else
0a1b45a2 10626 return true;
cb2366c1
EBM
10627
10628 case NT_PPC_TM_CVMX:
10629 if (note->namesz == 6
4b24dd1a
AM
10630 && strcmp (note->namedata, "LINUX") == 0)
10631 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10632 else
0a1b45a2 10633 return true;
cb2366c1
EBM
10634
10635 case NT_PPC_TM_CVSX:
10636 if (note->namesz == 6
4b24dd1a
AM
10637 && strcmp (note->namedata, "LINUX") == 0)
10638 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10639 else
0a1b45a2 10640 return true;
cb2366c1
EBM
10641
10642 case NT_PPC_TM_SPR:
10643 if (note->namesz == 6
4b24dd1a
AM
10644 && strcmp (note->namedata, "LINUX") == 0)
10645 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10646 else
0a1b45a2 10647 return true;
cb2366c1
EBM
10648
10649 case NT_PPC_TM_CTAR:
10650 if (note->namesz == 6
4b24dd1a
AM
10651 && strcmp (note->namedata, "LINUX") == 0)
10652 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10653 else
0a1b45a2 10654 return true;
cb2366c1
EBM
10655
10656 case NT_PPC_TM_CPPR:
10657 if (note->namesz == 6
4b24dd1a
AM
10658 && strcmp (note->namedata, "LINUX") == 0)
10659 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10660 else
0a1b45a2 10661 return true;
cb2366c1
EBM
10662
10663 case NT_PPC_TM_CDSCR:
10664 if (note->namesz == 6
4b24dd1a
AM
10665 && strcmp (note->namedata, "LINUX") == 0)
10666 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10667 else
0a1b45a2 10668 return true;
cb2366c1 10669
0675e188
UW
10670 case NT_S390_HIGH_GPRS:
10671 if (note->namesz == 6
07d6d2b8
AM
10672 && strcmp (note->namedata, "LINUX") == 0)
10673 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10674 else
0a1b45a2 10675 return true;
0675e188 10676
d7eeb400
MS
10677 case NT_S390_TIMER:
10678 if (note->namesz == 6
07d6d2b8
AM
10679 && strcmp (note->namedata, "LINUX") == 0)
10680 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10681 else
0a1b45a2 10682 return true;
d7eeb400
MS
10683
10684 case NT_S390_TODCMP:
10685 if (note->namesz == 6
07d6d2b8
AM
10686 && strcmp (note->namedata, "LINUX") == 0)
10687 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10688 else
0a1b45a2 10689 return true;
d7eeb400
MS
10690
10691 case NT_S390_TODPREG:
10692 if (note->namesz == 6
07d6d2b8
AM
10693 && strcmp (note->namedata, "LINUX") == 0)
10694 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10695 else
0a1b45a2 10696 return true;
d7eeb400
MS
10697
10698 case NT_S390_CTRS:
10699 if (note->namesz == 6
07d6d2b8
AM
10700 && strcmp (note->namedata, "LINUX") == 0)
10701 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10702 else
0a1b45a2 10703 return true;
d7eeb400
MS
10704
10705 case NT_S390_PREFIX:
10706 if (note->namesz == 6
07d6d2b8
AM
10707 && strcmp (note->namedata, "LINUX") == 0)
10708 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10709 else
0a1b45a2 10710 return true;
d7eeb400 10711
355b81d9
UW
10712 case NT_S390_LAST_BREAK:
10713 if (note->namesz == 6
07d6d2b8
AM
10714 && strcmp (note->namedata, "LINUX") == 0)
10715 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10716 else
0a1b45a2 10717 return true;
355b81d9
UW
10718
10719 case NT_S390_SYSTEM_CALL:
10720 if (note->namesz == 6
07d6d2b8
AM
10721 && strcmp (note->namedata, "LINUX") == 0)
10722 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10723 else
0a1b45a2 10724 return true;
355b81d9 10725
abb3f6cc
NC
10726 case NT_S390_TDB:
10727 if (note->namesz == 6
07d6d2b8
AM
10728 && strcmp (note->namedata, "LINUX") == 0)
10729 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10730 else
0a1b45a2 10731 return true;
abb3f6cc 10732
4ef9f41a
AA
10733 case NT_S390_VXRS_LOW:
10734 if (note->namesz == 6
10735 && strcmp (note->namedata, "LINUX") == 0)
10736 return elfcore_grok_s390_vxrs_low (abfd, note);
10737 else
0a1b45a2 10738 return true;
4ef9f41a
AA
10739
10740 case NT_S390_VXRS_HIGH:
10741 if (note->namesz == 6
10742 && strcmp (note->namedata, "LINUX") == 0)
10743 return elfcore_grok_s390_vxrs_high (abfd, note);
10744 else
0a1b45a2 10745 return true;
4ef9f41a 10746
88ab90e8
AA
10747 case NT_S390_GS_CB:
10748 if (note->namesz == 6
10749 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10750 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8 10751 else
0a1b45a2 10752 return true;
88ab90e8
AA
10753
10754 case NT_S390_GS_BC:
10755 if (note->namesz == 6
10756 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10757 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8 10758 else
0a1b45a2 10759 return true;
88ab90e8 10760
27456742
AK
10761 case NT_ARC_V2:
10762 if (note->namesz == 6
10763 && strcmp (note->namedata, "LINUX") == 0)
10764 return elfcore_grok_arc_v2 (abfd, note);
10765 else
0a1b45a2 10766 return true;
27456742 10767
faa9a424
UW
10768 case NT_ARM_VFP:
10769 if (note->namesz == 6
10770 && strcmp (note->namedata, "LINUX") == 0)
10771 return elfcore_grok_arm_vfp (abfd, note);
10772 else
0a1b45a2 10773 return true;
faa9a424 10774
652451f8
YZ
10775 case NT_ARM_TLS:
10776 if (note->namesz == 6
10777 && strcmp (note->namedata, "LINUX") == 0)
10778 return elfcore_grok_aarch_tls (abfd, note);
10779 else
0a1b45a2 10780 return true;
652451f8
YZ
10781
10782 case NT_ARM_HW_BREAK:
10783 if (note->namesz == 6
10784 && strcmp (note->namedata, "LINUX") == 0)
10785 return elfcore_grok_aarch_hw_break (abfd, note);
10786 else
0a1b45a2 10787 return true;
652451f8
YZ
10788
10789 case NT_ARM_HW_WATCH:
10790 if (note->namesz == 6
10791 && strcmp (note->namedata, "LINUX") == 0)
10792 return elfcore_grok_aarch_hw_watch (abfd, note);
10793 else
0a1b45a2 10794 return true;
652451f8 10795
ad1cc4e4
AH
10796 case NT_ARM_SVE:
10797 if (note->namesz == 6
10798 && strcmp (note->namedata, "LINUX") == 0)
10799 return elfcore_grok_aarch_sve (abfd, note);
10800 else
0a1b45a2 10801 return true;
ad1cc4e4 10802
e6c3b5bf
AH
10803 case NT_ARM_PAC_MASK:
10804 if (note->namesz == 6
10805 && strcmp (note->namedata, "LINUX") == 0)
10806 return elfcore_grok_aarch_pauth (abfd, note);
10807 else
0a1b45a2 10808 return true;
e6c3b5bf 10809
f0bbe8ba
LM
10810 case NT_ARM_TAGGED_ADDR_CTRL:
10811 if (note->namesz == 6
10812 && strcmp (note->namedata, "LINUX") == 0)
10813 return elfcore_grok_aarch_mte (abfd, note);
10814 else
10815 return true;
10816
b63a5e38
AB
10817 case NT_GDB_TDESC:
10818 if (note->namesz == 4
11c6a7c6
AM
10819 && strcmp (note->namedata, "GDB") == 0)
10820 return elfcore_grok_gdb_tdesc (abfd, note);
b63a5e38 10821 else
11c6a7c6 10822 return true;
b63a5e38 10823
db6092f3
AB
10824 case NT_RISCV_CSR:
10825 if (note->namesz == 4
11c6a7c6
AM
10826 && strcmp (note->namedata, "GDB") == 0)
10827 return elfcore_grok_riscv_csr (abfd, note);
db6092f3 10828 else
0a1b45a2 10829 return true;
db6092f3 10830
e214f8db 10831 case NT_LARCH_CPUCFG:
10832 if (note->namesz == 6
10833 && strcmp (note->namedata, "LINUX") == 0)
10834 return elfcore_grok_loongarch_cpucfg (abfd, note);
10835 else
10836 return true;
10837
10838 case NT_LARCH_LBT:
10839 if (note->namesz == 6
10840 && strcmp (note->namedata, "LINUX") == 0)
10841 return elfcore_grok_loongarch_lbt (abfd, note);
10842 else
10843 return true;
10844
10845 case NT_LARCH_LSX:
10846 if (note->namesz == 6
10847 && strcmp (note->namedata, "LINUX") == 0)
10848 return elfcore_grok_loongarch_lsx (abfd, note);
10849 else
10850 return true;
10851
10852 case NT_LARCH_LASX:
10853 if (note->namesz == 6
10854 && strcmp (note->namedata, "LINUX") == 0)
10855 return elfcore_grok_loongarch_lasx (abfd, note);
10856 else
10857 return true;
10858
252b5132
RH
10859 case NT_PRPSINFO:
10860 case NT_PSINFO:
bb0082d6
AM
10861 if (bed->elf_backend_grok_psinfo)
10862 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
0a1b45a2 10863 return true;
bb0082d6 10864#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10865 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10866#else
0a1b45a2 10867 return true;
252b5132 10868#endif
3333a7c3
RM
10869
10870 case NT_AUXV:
58e07198 10871 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10872
451b7c33
TT
10873 case NT_FILE:
10874 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10875 note);
10876
9015683b
TT
10877 case NT_SIGINFO:
10878 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10879 note);
5b2c414d 10880
252b5132
RH
10881 }
10882}
10883
0a1b45a2 10884static bool
718175fa
JK
10885elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10886{
c74f7d1c 10887 struct bfd_build_id* build_id;
30e8ee25
AM
10888
10889 if (note->descsz == 0)
0a1b45a2 10890 return false;
30e8ee25 10891
c74f7d1c
JT
10892 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10893 if (build_id == NULL)
0a1b45a2 10894 return false;
718175fa 10895
c74f7d1c
JT
10896 build_id->size = note->descsz;
10897 memcpy (build_id->data, note->descdata, note->descsz);
10898 abfd->build_id = build_id;
718175fa 10899
0a1b45a2 10900 return true;
718175fa
JK
10901}
10902
0a1b45a2 10903static bool
718175fa
JK
10904elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10905{
10906 switch (note->type)
10907 {
10908 default:
0a1b45a2 10909 return true;
718175fa 10910
46bed679
L
10911 case NT_GNU_PROPERTY_TYPE_0:
10912 return _bfd_elf_parse_gnu_properties (abfd, note);
10913
718175fa
JK
10914 case NT_GNU_BUILD_ID:
10915 return elfobj_grok_gnu_build_id (abfd, note);
10916 }
10917}
10918
0a1b45a2 10919static bool
e21e5835
NC
10920elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10921{
10922 struct sdt_note *cur =
7a6e0d89
AM
10923 (struct sdt_note *) bfd_alloc (abfd,
10924 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10925
10926 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10927 cur->size = (bfd_size_type) note->descsz;
10928 memcpy (cur->data, note->descdata, note->descsz);
10929
10930 elf_tdata (abfd)->sdt_note_head = cur;
10931
0a1b45a2 10932 return true;
e21e5835
NC
10933}
10934
0a1b45a2 10935static bool
e21e5835
NC
10936elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10937{
10938 switch (note->type)
10939 {
10940 case NT_STAPSDT:
10941 return elfobj_grok_stapsdt_note_1 (abfd, note);
10942
10943 default:
0a1b45a2 10944 return true;
e21e5835
NC
10945 }
10946}
10947
0a1b45a2 10948static bool
aa1ed4a9
JB
10949elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10950{
10951 size_t offset;
10952
b5430a3c 10953 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10954 {
b5430a3c 10955 case ELFCLASS32:
0064d223 10956 if (note->descsz < 108)
0a1b45a2 10957 return false;
aa1ed4a9
JB
10958 break;
10959
b5430a3c 10960 case ELFCLASS64:
0064d223 10961 if (note->descsz < 120)
0a1b45a2 10962 return false;
aa1ed4a9
JB
10963 break;
10964
10965 default:
0a1b45a2 10966 return false;
aa1ed4a9
JB
10967 }
10968
0064d223
JB
10969 /* Check for version 1 in pr_version. */
10970 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10971 return false;
80a04378 10972
0064d223
JB
10973 offset = 4;
10974
10975 /* Skip over pr_psinfosz. */
b5430a3c 10976 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10977 offset += 4;
10978 else
10979 {
10980 offset += 4; /* Padding before pr_psinfosz. */
10981 offset += 8;
10982 }
10983
aa1ed4a9
JB
10984 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10985 elf_tdata (abfd)->core->program
10986 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10987 offset += 17;
10988
10989 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10990 elf_tdata (abfd)->core->command
10991 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10992 offset += 81;
10993
10994 /* Padding before pr_pid. */
10995 offset += 2;
10996
10997 /* The pr_pid field was added in version "1a". */
10998 if (note->descsz < offset + 4)
0a1b45a2 10999 return true;
0064d223
JB
11000
11001 elf_tdata (abfd)->core->pid
11002 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9 11003
0a1b45a2 11004 return true;
aa1ed4a9
JB
11005}
11006
0a1b45a2 11007static bool
aa1ed4a9
JB
11008elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11009{
11010 size_t offset;
11011 size_t size;
24d3e51b 11012 size_t min_size;
aa1ed4a9 11013
24d3e51b
NC
11014 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11015 Also compute minimum size of this note. */
b5430a3c 11016 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 11017 {
b5430a3c 11018 case ELFCLASS32:
24d3e51b
NC
11019 offset = 4 + 4;
11020 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
11021 break;
11022
b5430a3c 11023 case ELFCLASS64:
24d3e51b
NC
11024 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11025 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
11026 break;
11027
11028 default:
0a1b45a2 11029 return false;
aa1ed4a9
JB
11030 }
11031
24d3e51b 11032 if (note->descsz < min_size)
0a1b45a2 11033 return false;
24d3e51b
NC
11034
11035 /* Check for version 1 in pr_version. */
11036 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 11037 return false;
aa1ed4a9 11038
24d3e51b
NC
11039 /* Extract size of pr_reg from pr_gregsetsz. */
11040 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 11041 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
11042 {
11043 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11044 offset += 4 * 2;
11045 }
b5430a3c 11046 else
24d3e51b
NC
11047 {
11048 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11049 offset += 8 * 2;
11050 }
aa1ed4a9 11051
24d3e51b 11052 /* Skip over pr_osreldate. */
aa1ed4a9
JB
11053 offset += 4;
11054
24d3e51b 11055 /* Read signal from pr_cursig. */
aa1ed4a9
JB
11056 if (elf_tdata (abfd)->core->signal == 0)
11057 elf_tdata (abfd)->core->signal
11058 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11059 offset += 4;
11060
24d3e51b 11061 /* Read TID from pr_pid. */
aa1ed4a9
JB
11062 elf_tdata (abfd)->core->lwpid
11063 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11064 offset += 4;
11065
24d3e51b 11066 /* Padding before pr_reg. */
b5430a3c 11067 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
11068 offset += 4;
11069
24d3e51b
NC
11070 /* Make sure that there is enough data remaining in the note. */
11071 if ((note->descsz - offset) < size)
0a1b45a2 11072 return false;
24d3e51b 11073
aa1ed4a9
JB
11074 /* Make a ".reg/999" section and a ".reg" section. */
11075 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11076 size, note->descpos + offset);
11077}
11078
0a1b45a2 11079static bool
aa1ed4a9
JB
11080elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11081{
544c67cd
JB
11082 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11083
aa1ed4a9
JB
11084 switch (note->type)
11085 {
11086 case NT_PRSTATUS:
544c67cd
JB
11087 if (bed->elf_backend_grok_freebsd_prstatus)
11088 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
0a1b45a2 11089 return true;
aa1ed4a9
JB
11090 return elfcore_grok_freebsd_prstatus (abfd, note);
11091
11092 case NT_FPREGSET:
11093 return elfcore_grok_prfpreg (abfd, note);
11094
11095 case NT_PRPSINFO:
11096 return elfcore_grok_freebsd_psinfo (abfd, note);
11097
11098 case NT_FREEBSD_THRMISC:
e330d4c0 11099 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
aa1ed4a9 11100
ddb2bbcf
JB
11101 case NT_FREEBSD_PROCSTAT_PROC:
11102 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11103 note);
11104
11105 case NT_FREEBSD_PROCSTAT_FILES:
11106 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11107 note);
11108
11109 case NT_FREEBSD_PROCSTAT_VMMAP:
11110 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11111 note);
11112
3350c5f5 11113 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 11114 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 11115
b5c2367c
JB
11116 case NT_FREEBSD_X86_SEGBASES:
11117 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11118
aa1ed4a9 11119 case NT_X86_XSTATE:
e330d4c0 11120 return elfcore_grok_xstatereg (abfd, note);
aa1ed4a9 11121
e6f3b9c3
JB
11122 case NT_FREEBSD_PTLWPINFO:
11123 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11124 note);
11125
8e6afe40
JB
11126 case NT_ARM_TLS:
11127 return elfcore_grok_aarch_tls (abfd, note);
11128
6d5be5d6
JB
11129 case NT_ARM_VFP:
11130 return elfcore_grok_arm_vfp (abfd, note);
11131
aa1ed4a9 11132 default:
0a1b45a2 11133 return true;
aa1ed4a9
JB
11134 }
11135}
11136
0a1b45a2 11137static bool
217aa764 11138elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
11139{
11140 char *cp;
11141
11142 cp = strchr (note->namedata, '@');
11143 if (cp != NULL)
11144 {
d2b64500 11145 *lwpidp = atoi(cp + 1);
0a1b45a2 11146 return true;
50b2bdb7 11147 }
0a1b45a2 11148 return false;
50b2bdb7
AM
11149}
11150
0a1b45a2 11151static bool
217aa764 11152elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 11153{
80a04378 11154 if (note->descsz <= 0x7c + 31)
0a1b45a2 11155 return false;
80a04378 11156
50b2bdb7 11157 /* Signal number at offset 0x08. */
228e534f 11158 elf_tdata (abfd)->core->signal
50b2bdb7
AM
11159 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11160
11161 /* Process ID at offset 0x50. */
228e534f 11162 elf_tdata (abfd)->core->pid
50b2bdb7
AM
11163 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11164
11165 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 11166 elf_tdata (abfd)->core->command
50b2bdb7
AM
11167 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11168
7720ba9f
MK
11169 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11170 note);
50b2bdb7
AM
11171}
11172
0a1b45a2 11173static bool
217aa764 11174elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
11175{
11176 int lwp;
11177
11178 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 11179 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 11180
58e07198 11181 switch (note->type)
50b2bdb7 11182 {
58e07198 11183 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 11184 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
11185 find this note before any of the others, which is fine,
11186 since the kernel writes this note out first when it
11187 creates a core file. */
50b2bdb7 11188 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
11189 case NT_NETBSDCORE_AUXV:
11190 /* NetBSD-specific Elf Auxiliary Vector data. */
11191 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
11192 case NT_NETBSDCORE_LWPSTATUS:
11193 return elfcore_make_note_pseudosection (abfd,
11194 ".note.netbsdcore.lwpstatus",
11195 note);
58e07198
CZ
11196 default:
11197 break;
50b2bdb7
AM
11198 }
11199
06d949ec 11200 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
11201 defined for NetBSD core files. If the note type is less
11202 than the start of the machine-dependent note types, we don't
11203 understand it. */
47d9a591 11204
b4db1224 11205 if (note->type < NT_NETBSDCORE_FIRSTMACH)
0a1b45a2 11206 return true;
50b2bdb7
AM
11207
11208
11209 switch (bfd_get_arch (abfd))
11210 {
08a40648
AM
11211 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11212 PT_GETFPREGS == mach+2. */
50b2bdb7 11213
015ec493 11214 case bfd_arch_aarch64:
50b2bdb7
AM
11215 case bfd_arch_alpha:
11216 case bfd_arch_sparc:
11217 switch (note->type)
08a40648
AM
11218 {
11219 case NT_NETBSDCORE_FIRSTMACH+0:
11220 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11221
08a40648
AM
11222 case NT_NETBSDCORE_FIRSTMACH+2:
11223 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11224
08a40648 11225 default:
0a1b45a2 11226 return true;
08a40648 11227 }
50b2bdb7 11228
58e07198
CZ
11229 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11230 There's also old PT___GETREGS40 == mach + 1 for old reg
11231 structure which lacks GBR. */
11232
11233 case bfd_arch_sh:
11234 switch (note->type)
11235 {
11236 case NT_NETBSDCORE_FIRSTMACH+3:
11237 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11238
11239 case NT_NETBSDCORE_FIRSTMACH+5:
11240 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11241
11242 default:
0a1b45a2 11243 return true;
58e07198
CZ
11244 }
11245
08a40648
AM
11246 /* On all other arch's, PT_GETREGS == mach+1 and
11247 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
11248
11249 default:
11250 switch (note->type)
08a40648
AM
11251 {
11252 case NT_NETBSDCORE_FIRSTMACH+1:
11253 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11254
08a40648
AM
11255 case NT_NETBSDCORE_FIRSTMACH+3:
11256 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11257
08a40648 11258 default:
0a1b45a2 11259 return true;
08a40648 11260 }
50b2bdb7
AM
11261 }
11262 /* NOTREACHED */
11263}
11264
0a1b45a2 11265static bool
67cc5033
MK
11266elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11267{
80a04378 11268 if (note->descsz <= 0x48 + 31)
0a1b45a2 11269 return false;
80a04378 11270
67cc5033 11271 /* Signal number at offset 0x08. */
228e534f 11272 elf_tdata (abfd)->core->signal
67cc5033
MK
11273 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11274
11275 /* Process ID at offset 0x20. */
228e534f 11276 elf_tdata (abfd)->core->pid
67cc5033
MK
11277 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11278
11279 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 11280 elf_tdata (abfd)->core->command
67cc5033
MK
11281 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11282
0a1b45a2 11283 return true;
67cc5033
MK
11284}
11285
6420dd27
LB
11286/* Processes Solaris's process status note.
11287 sig_off ~ offsetof(prstatus_t, pr_cursig)
11288 pid_off ~ offsetof(prstatus_t, pr_pid)
11289 lwpid_off ~ offsetof(prstatus_t, pr_who)
11290 gregset_size ~ sizeof(gregset_t)
11291 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11292
11293static bool
11294elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11295 int pid_off, int lwpid_off, size_t gregset_size,
11296 size_t gregset_offset)
11297{
11298 asection *sect = NULL;
11299 elf_tdata (abfd)->core->signal
11300 = bfd_get_16 (abfd, note->descdata + sig_off);
11301 elf_tdata (abfd)->core->pid
11302 = bfd_get_32 (abfd, note->descdata + pid_off);
11303 elf_tdata (abfd)->core->lwpid
11304 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11305
11306 sect = bfd_get_section_by_name (abfd, ".reg");
11307 if (sect != NULL)
11308 sect->size = gregset_size;
11309
11310 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11311 note->descpos + gregset_offset);
11312}
11313
11314/* Gets program and arguments from a core.
11315 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11316 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11317
11318static bool
11319elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11320 int prog_off, int comm_off)
11321{
11322 elf_tdata (abfd)->core->program
11323 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11324 elf_tdata (abfd)->core->command
11325 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11326
11327 return true;
11328}
11329
11330/* Processes Solaris's LWP status note.
11331 gregset_size ~ sizeof(gregset_t)
11332 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11333 fpregset_size ~ sizeof(fpregset_t)
11334 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11335
11336static bool
11337elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11338 size_t gregset_size, int gregset_off,
11339 size_t fpregset_size, int fpregset_off)
11340{
11341 asection *sect = NULL;
11342 char reg2_section_name[16] = { 0 };
11343
11344 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11345 elf_tdata (abfd)->core->lwpid);
11346
11347 /* offsetof(lwpstatus_t, pr_lwpid) */
11348 elf_tdata (abfd)->core->lwpid
11349 = bfd_get_32 (abfd, note->descdata + 4);
11350 /* offsetof(lwpstatus_t, pr_cursig) */
11351 elf_tdata (abfd)->core->signal
11352 = bfd_get_16 (abfd, note->descdata + 12);
11353
11354 sect = bfd_get_section_by_name (abfd, ".reg");
11355 if (sect != NULL)
11356 sect->size = gregset_size;
11357 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11358 note->descpos + gregset_off))
11359 return false;
11360
11361 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11362 if (sect != NULL)
11363 {
11364 sect->size = fpregset_size;
11365 sect->filepos = note->descpos + fpregset_off;
11366 sect->alignment_power = 2;
11367 }
11368 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11369 note->descpos + fpregset_off))
11370 return false;
11371
11372 return true;
11373}
11374
11375static bool
11376elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11377{
11378 if (note == NULL)
11379 return false;
11380
11381 /* core files are identified as 32- or 64-bit, SPARC or x86,
11382 by the size of the descsz which matches the sizeof()
11383 the type appropriate for that note type (e.g., prstatus_t for
11384 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11385 on Solaris. The core file bitness may differ from the bitness of
11386 gdb itself, so fixed values are used instead of sizeof().
11387 Appropriate fixed offsets are also used to obtain data from
11388 the note. */
11389
11390 switch ((int) note->type)
11391 {
11392 case SOLARIS_NT_PRSTATUS:
11393 switch (note->descsz)
11394 {
11395 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11396 return elfcore_grok_solaris_prstatus(abfd, note,
11397 136, 216, 308, 152, 356);
11398 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11399 return elfcore_grok_solaris_prstatus(abfd, note,
11400 264, 360, 520, 304, 600);
11401 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11402 return elfcore_grok_solaris_prstatus(abfd, note,
11403 136, 216, 308, 76, 356);
11404 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11405 return elfcore_grok_solaris_prstatus(abfd, note,
11406 264, 360, 520, 224, 600);
11407 default:
11408 return true;
11409 }
11410
11411 case SOLARIS_NT_PSINFO:
11412 case SOLARIS_NT_PRPSINFO:
11413 switch (note->descsz)
11414 {
11415 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11416 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11417 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11418 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11419 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11420 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11421 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11422 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11423 default:
11424 return true;
11425 }
11426
11427 case SOLARIS_NT_LWPSTATUS:
11428 switch (note->descsz)
11429 {
11430 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11431 return elfcore_grok_solaris_lwpstatus(abfd, note,
11432 152, 344, 400, 496);
11433 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11434 return elfcore_grok_solaris_lwpstatus(abfd, note,
11435 304, 544, 544, 848);
11436 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11437 return elfcore_grok_solaris_lwpstatus(abfd, note,
11438 76, 344, 380, 420);
11439 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11440 return elfcore_grok_solaris_lwpstatus(abfd, note,
11441 224, 544, 528, 768);
11442 default:
11443 return true;
11444 }
11445
11446 case SOLARIS_NT_LWPSINFO:
11447 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11448 if (note->descsz == 128 || note->descsz == 152)
11449 elf_tdata (abfd)->core->lwpid =
11450 bfd_get_32 (abfd, note->descdata + 4);
11451 break;
11452
11453 default:
11454 break;
11455 }
11456
11457 return true;
11458}
11459
11460/* For name starting with "CORE" this may be either a Solaris
11461 core file or a gdb-generated core file. Do Solaris-specific
11462 processing on selected note types first with
11463 elfcore_grok_solaris_note(), then process the note
11464 in elfcore_grok_note(). */
11465
11466static bool
11467elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11468{
11469 if (!elfcore_grok_solaris_note_impl (abfd, note))
11470 return false;
11471
11472 return elfcore_grok_note (abfd, note);
11473}
11474
0a1b45a2 11475static bool
67cc5033
MK
11476elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11477{
11478 if (note->type == NT_OPENBSD_PROCINFO)
11479 return elfcore_grok_openbsd_procinfo (abfd, note);
11480
11481 if (note->type == NT_OPENBSD_REGS)
11482 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11483
11484 if (note->type == NT_OPENBSD_FPREGS)
11485 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11486
11487 if (note->type == NT_OPENBSD_XFPREGS)
11488 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11489
11490 if (note->type == NT_OPENBSD_AUXV)
58e07198 11491 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11492
11493 if (note->type == NT_OPENBSD_WCOOKIE)
11494 {
11495 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11496 SEC_HAS_CONTENTS);
11497
11498 if (sect == NULL)
0a1b45a2 11499 return false;
67cc5033
MK
11500 sect->size = note->descsz;
11501 sect->filepos = note->descpos;
11502 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11503
0a1b45a2 11504 return true;
67cc5033
MK
11505 }
11506
0a1b45a2 11507 return true;
67cc5033
MK
11508}
11509
0a1b45a2 11510static bool
d3fd4074 11511elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11512{
11513 void *ddata = note->descdata;
11514 char buf[100];
11515 char *name;
11516 asection *sect;
f8843e87
AM
11517 short sig;
11518 unsigned flags;
07c6e936 11519
80a04378 11520 if (note->descsz < 16)
0a1b45a2 11521 return false;
80a04378 11522
07c6e936 11523 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11524 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11525
f8843e87
AM
11526 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11527 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11528
11529 /* nto_procfs_status 'flags' field is at offset 8. */
11530 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11531
11532 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11533 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11534 {
228e534f
AM
11535 elf_tdata (abfd)->core->signal = sig;
11536 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11537 }
07c6e936 11538
f8843e87
AM
11539 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11540 do not come from signals so we make sure we set the current
11541 thread just in case. */
11542 if (flags & 0x00000080)
228e534f 11543 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11544
11545 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11546 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11547
a50b1753 11548 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11549 if (name == NULL)
0a1b45a2 11550 return false;
07c6e936
NC
11551 strcpy (name, buf);
11552
117ed4f8 11553 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11554 if (sect == NULL)
0a1b45a2 11555 return false;
07c6e936 11556
07d6d2b8
AM
11557 sect->size = note->descsz;
11558 sect->filepos = note->descpos;
07c6e936
NC
11559 sect->alignment_power = 2;
11560
11561 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11562}
11563
0a1b45a2 11564static bool
d69f560c
KW
11565elfcore_grok_nto_regs (bfd *abfd,
11566 Elf_Internal_Note *note,
d3fd4074 11567 long tid,
d69f560c 11568 char *base)
07c6e936
NC
11569{
11570 char buf[100];
11571 char *name;
11572 asection *sect;
11573
d69f560c 11574 /* Make a "(base)/%d" section. */
d3fd4074 11575 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11576
a50b1753 11577 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11578 if (name == NULL)
0a1b45a2 11579 return false;
07c6e936
NC
11580 strcpy (name, buf);
11581
117ed4f8 11582 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11583 if (sect == NULL)
0a1b45a2 11584 return false;
07c6e936 11585
07d6d2b8
AM
11586 sect->size = note->descsz;
11587 sect->filepos = note->descpos;
07c6e936
NC
11588 sect->alignment_power = 2;
11589
f8843e87 11590 /* This is the current thread. */
228e534f 11591 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11592 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87 11593
0a1b45a2 11594 return true;
07c6e936
NC
11595}
11596
11597#define BFD_QNT_CORE_INFO 7
11598#define BFD_QNT_CORE_STATUS 8
11599#define BFD_QNT_CORE_GREG 9
11600#define BFD_QNT_CORE_FPREG 10
11601
0a1b45a2 11602static bool
217aa764 11603elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11604{
11605 /* Every GREG section has a STATUS section before it. Store the
811072d8 11606 tid from the previous call to pass down to the next gregs
07c6e936 11607 function. */
d3fd4074 11608 static long tid = 1;
07c6e936
NC
11609
11610 switch (note->type)
11611 {
d69f560c
KW
11612 case BFD_QNT_CORE_INFO:
11613 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11614 case BFD_QNT_CORE_STATUS:
11615 return elfcore_grok_nto_status (abfd, note, &tid);
11616 case BFD_QNT_CORE_GREG:
11617 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11618 case BFD_QNT_CORE_FPREG:
11619 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11620 default:
0a1b45a2 11621 return true;
07c6e936
NC
11622 }
11623}
11624
0a1b45a2 11625static bool
b15fa79e
AM
11626elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11627{
11628 char *name;
11629 asection *sect;
11630 size_t len;
11631
11632 /* Use note name as section name. */
11633 len = note->namesz;
a50b1753 11634 name = (char *) bfd_alloc (abfd, len);
b15fa79e 11635 if (name == NULL)
0a1b45a2 11636 return false;
b15fa79e
AM
11637 memcpy (name, note->namedata, len);
11638 name[len - 1] = '\0';
11639
11640 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11641 if (sect == NULL)
0a1b45a2 11642 return false;
b15fa79e 11643
07d6d2b8
AM
11644 sect->size = note->descsz;
11645 sect->filepos = note->descpos;
b15fa79e
AM
11646 sect->alignment_power = 1;
11647
0a1b45a2 11648 return true;
b15fa79e
AM
11649}
11650
7c76fa91
MS
11651/* Function: elfcore_write_note
11652
47d9a591 11653 Inputs:
a39f3346 11654 buffer to hold note, and current size of buffer
7c76fa91
MS
11655 name of note
11656 type of note
11657 data for note
11658 size of data for note
11659
a39f3346
AM
11660 Writes note to end of buffer. ELF64 notes are written exactly as
11661 for ELF32, despite the current (as of 2006) ELF gabi specifying
11662 that they ought to have 8-byte namesz and descsz field, and have
11663 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11664
7c76fa91 11665 Return:
a39f3346 11666 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11667
11668char *
a39f3346 11669elfcore_write_note (bfd *abfd,
217aa764 11670 char *buf,
a39f3346 11671 int *bufsiz,
217aa764 11672 const char *name,
a39f3346 11673 int type,
217aa764 11674 const void *input,
a39f3346 11675 int size)
7c76fa91
MS
11676{
11677 Elf_External_Note *xnp;
d4c88bbb 11678 size_t namesz;
d4c88bbb 11679 size_t newspace;
a39f3346 11680 char *dest;
7c76fa91 11681
d4c88bbb 11682 namesz = 0;
d4c88bbb 11683 if (name != NULL)
a39f3346 11684 namesz = strlen (name) + 1;
d4c88bbb 11685
a39f3346 11686 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11687
a50b1753 11688 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11689 if (buf == NULL)
11690 return buf;
a39f3346 11691 dest = buf + *bufsiz;
7c76fa91
MS
11692 *bufsiz += newspace;
11693 xnp = (Elf_External_Note *) dest;
11694 H_PUT_32 (abfd, namesz, xnp->namesz);
11695 H_PUT_32 (abfd, size, xnp->descsz);
11696 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11697 dest = xnp->name;
11698 if (name != NULL)
11699 {
11700 memcpy (dest, name, namesz);
11701 dest += namesz;
a39f3346 11702 while (namesz & 3)
d4c88bbb
AM
11703 {
11704 *dest++ = '\0';
a39f3346 11705 ++namesz;
d4c88bbb
AM
11706 }
11707 }
11708 memcpy (dest, input, size);
a39f3346
AM
11709 dest += size;
11710 while (size & 3)
11711 {
11712 *dest++ = '\0';
11713 ++size;
11714 }
11715 return buf;
7c76fa91
MS
11716}
11717
602f1657
AM
11718/* gcc-8 warns (*) on all the strncpy calls in this function about
11719 possible string truncation. The "truncation" is not a bug. We
11720 have an external representation of structs with fields that are not
11721 necessarily NULL terminated and corresponding internal
11722 representation fields that are one larger so that they can always
11723 be NULL terminated.
11724 gcc versions between 4.2 and 4.6 do not allow pragma control of
11725 diagnostics inside functions, giving a hard error if you try to use
11726 the finer control available with later versions.
11727 gcc prior to 4.2 warns about diagnostic push and pop.
11728 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11729 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11730 (*) Depending on your system header files! */
d99b4b92 11731#if GCC_VERSION >= 8000
602f1657
AM
11732# pragma GCC diagnostic push
11733# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11734#endif
7c76fa91 11735char *
217aa764
AM
11736elfcore_write_prpsinfo (bfd *abfd,
11737 char *buf,
11738 int *bufsiz,
11739 const char *fname,
11740 const char *psargs)
7c76fa91 11741{
183e98be
AM
11742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11743
11744 if (bed->elf_backend_write_core_note != NULL)
11745 {
11746 char *ret;
11747 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11748 NT_PRPSINFO, fname, psargs);
11749 if (ret != NULL)
11750 return ret;
11751 }
7c76fa91 11752
1f20dca5 11753#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11754# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11755 if (bed->s->elfclass == ELFCLASS32)
11756 {
602f1657 11757# if defined (HAVE_PSINFO32_T)
183e98be
AM
11758 psinfo32_t data;
11759 int note_type = NT_PSINFO;
602f1657 11760# else
183e98be
AM
11761 prpsinfo32_t data;
11762 int note_type = NT_PRPSINFO;
602f1657 11763# endif
183e98be
AM
11764
11765 memset (&data, 0, sizeof (data));
11766 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11767 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11768 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11769 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11770 }
11771 else
602f1657 11772# endif
183e98be 11773 {
602f1657 11774# if defined (HAVE_PSINFO_T)
183e98be
AM
11775 psinfo_t data;
11776 int note_type = NT_PSINFO;
602f1657 11777# else
183e98be
AM
11778 prpsinfo_t data;
11779 int note_type = NT_PRPSINFO;
602f1657 11780# endif
7c76fa91 11781
183e98be
AM
11782 memset (&data, 0, sizeof (data));
11783 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11784 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11785 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11786 "CORE", note_type, &data, sizeof (data));
183e98be 11787 }
7c76fa91
MS
11788#endif /* PSINFO_T or PRPSINFO_T */
11789
1f20dca5
UW
11790 free (buf);
11791 return NULL;
11792}
d99b4b92 11793#if GCC_VERSION >= 8000
602f1657 11794# pragma GCC diagnostic pop
d99b4b92 11795#endif
1f20dca5 11796
70a38d42
SDJ
11797char *
11798elfcore_write_linux_prpsinfo32
11799 (bfd *abfd, char *buf, int *bufsiz,
11800 const struct elf_internal_linux_prpsinfo *prpsinfo)
11801{
a2f63b2e
MR
11802 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11803 {
11804 struct elf_external_linux_prpsinfo32_ugid16 data;
11805
11806 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11807 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11808 &data, sizeof (data));
11809 }
11810 else
11811 {
11812 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11813
a2f63b2e
MR
11814 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11815 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11816 &data, sizeof (data));
11817 }
70a38d42
SDJ
11818}
11819
11820char *
11821elfcore_write_linux_prpsinfo64
11822 (bfd *abfd, char *buf, int *bufsiz,
11823 const struct elf_internal_linux_prpsinfo *prpsinfo)
11824{
3c9a7b0d
MR
11825 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11826 {
11827 struct elf_external_linux_prpsinfo64_ugid16 data;
11828
11829 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11830 return elfcore_write_note (abfd, buf, bufsiz,
11831 "CORE", NT_PRPSINFO, &data, sizeof (data));
11832 }
11833 else
11834 {
11835 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11836
3c9a7b0d
MR
11837 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11838 return elfcore_write_note (abfd, buf, bufsiz,
11839 "CORE", NT_PRPSINFO, &data, sizeof (data));
11840 }
70a38d42
SDJ
11841}
11842
7c76fa91 11843char *
217aa764
AM
11844elfcore_write_prstatus (bfd *abfd,
11845 char *buf,
11846 int *bufsiz,
11847 long pid,
11848 int cursig,
11849 const void *gregs)
7c76fa91 11850{
183e98be 11851 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11852
183e98be
AM
11853 if (bed->elf_backend_write_core_note != NULL)
11854 {
11855 char *ret;
11856 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11857 NT_PRSTATUS,
11858 pid, cursig, gregs);
11859 if (ret != NULL)
11860 return ret;
11861 }
11862
1f20dca5 11863#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11864#if defined (HAVE_PRSTATUS32_T)
11865 if (bed->s->elfclass == ELFCLASS32)
11866 {
11867 prstatus32_t prstat;
11868
11869 memset (&prstat, 0, sizeof (prstat));
11870 prstat.pr_pid = pid;
11871 prstat.pr_cursig = cursig;
11872 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11873 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11874 NT_PRSTATUS, &prstat, sizeof (prstat));
11875 }
11876 else
11877#endif
11878 {
11879 prstatus_t prstat;
11880
11881 memset (&prstat, 0, sizeof (prstat));
11882 prstat.pr_pid = pid;
11883 prstat.pr_cursig = cursig;
11884 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11885 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11886 NT_PRSTATUS, &prstat, sizeof (prstat));
11887 }
7c76fa91
MS
11888#endif /* HAVE_PRSTATUS_T */
11889
1f20dca5
UW
11890 free (buf);
11891 return NULL;
11892}
11893
51316059
MS
11894#if defined (HAVE_LWPSTATUS_T)
11895char *
217aa764
AM
11896elfcore_write_lwpstatus (bfd *abfd,
11897 char *buf,
11898 int *bufsiz,
11899 long pid,
11900 int cursig,
11901 const void *gregs)
51316059
MS
11902{
11903 lwpstatus_t lwpstat;
183e98be 11904 const char *note_name = "CORE";
51316059
MS
11905
11906 memset (&lwpstat, 0, sizeof (lwpstat));
11907 lwpstat.pr_lwpid = pid >> 16;
11908 lwpstat.pr_cursig = cursig;
11909#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11910 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11911#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11912#if !defined(gregs)
11913 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11914 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11915#else
11916 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11917 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11918#endif
11919#endif
47d9a591 11920 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11921 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11922}
11923#endif /* HAVE_LWPSTATUS_T */
11924
7c76fa91
MS
11925#if defined (HAVE_PSTATUS_T)
11926char *
217aa764
AM
11927elfcore_write_pstatus (bfd *abfd,
11928 char *buf,
11929 int *bufsiz,
11930 long pid,
6c10990d
NC
11931 int cursig ATTRIBUTE_UNUSED,
11932 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11933{
183e98be
AM
11934 const char *note_name = "CORE";
11935#if defined (HAVE_PSTATUS32_T)
11936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11937
183e98be
AM
11938 if (bed->s->elfclass == ELFCLASS32)
11939 {
11940 pstatus32_t pstat;
11941
11942 memset (&pstat, 0, sizeof (pstat));
11943 pstat.pr_pid = pid & 0xffff;
11944 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11945 NT_PSTATUS, &pstat, sizeof (pstat));
11946 return buf;
11947 }
11948 else
11949#endif
11950 {
11951 pstatus_t pstat;
11952
11953 memset (&pstat, 0, sizeof (pstat));
11954 pstat.pr_pid = pid & 0xffff;
11955 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11956 NT_PSTATUS, &pstat, sizeof (pstat));
11957 return buf;
11958 }
7c76fa91
MS
11959}
11960#endif /* HAVE_PSTATUS_T */
11961
11962char *
217aa764
AM
11963elfcore_write_prfpreg (bfd *abfd,
11964 char *buf,
11965 int *bufsiz,
11966 const void *fpregs,
11967 int size)
7c76fa91 11968{
183e98be 11969 const char *note_name = "CORE";
47d9a591 11970 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11971 note_name, NT_FPREGSET, fpregs, size);
11972}
11973
11974char *
217aa764
AM
11975elfcore_write_prxfpreg (bfd *abfd,
11976 char *buf,
11977 int *bufsiz,
11978 const void *xfpregs,
11979 int size)
7c76fa91
MS
11980{
11981 char *note_name = "LINUX";
47d9a591 11982 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11983 note_name, NT_PRXFPREG, xfpregs, size);
11984}
11985
4339cae0
L
11986char *
11987elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11988 const void *xfpregs, int size)
11989{
97de3545
JB
11990 char *note_name;
11991 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11992 note_name = "FreeBSD";
11993 else
11994 note_name = "LINUX";
4339cae0
L
11995 return elfcore_write_note (abfd, buf, bufsiz,
11996 note_name, NT_X86_XSTATE, xfpregs, size);
11997}
11998
b5c2367c
JB
11999char *
12000elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12001 const void *regs, int size)
12002{
12003 char *note_name = "FreeBSD";
12004 return elfcore_write_note (abfd, buf, bufsiz,
12005 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12006}
12007
97753bd5
AM
12008char *
12009elfcore_write_ppc_vmx (bfd *abfd,
12010 char *buf,
12011 int *bufsiz,
12012 const void *ppc_vmx,
12013 int size)
12014{
12015 char *note_name = "LINUX";
12016 return elfcore_write_note (abfd, buf, bufsiz,
12017 note_name, NT_PPC_VMX, ppc_vmx, size);
12018}
12019
89eeb0bc
LM
12020char *
12021elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
12022 char *buf,
12023 int *bufsiz,
12024 const void *ppc_vsx,
12025 int size)
89eeb0bc
LM
12026{
12027 char *note_name = "LINUX";
12028 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12029 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
12030}
12031
cb2366c1
EBM
12032char *
12033elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
12034 char *buf,
12035 int *bufsiz,
12036 const void *ppc_tar,
12037 int size)
cb2366c1
EBM
12038{
12039 char *note_name = "LINUX";
12040 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12041 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
12042}
12043
12044char *
12045elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
12046 char *buf,
12047 int *bufsiz,
12048 const void *ppc_ppr,
12049 int size)
cb2366c1
EBM
12050{
12051 char *note_name = "LINUX";
12052 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12053 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
12054}
12055
12056char *
12057elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
12058 char *buf,
12059 int *bufsiz,
12060 const void *ppc_dscr,
12061 int size)
cb2366c1
EBM
12062{
12063 char *note_name = "LINUX";
12064 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12065 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
12066}
12067
12068char *
12069elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
12070 char *buf,
12071 int *bufsiz,
12072 const void *ppc_ebb,
12073 int size)
cb2366c1
EBM
12074{
12075 char *note_name = "LINUX";
12076 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12077 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
12078}
12079
12080char *
12081elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
12082 char *buf,
12083 int *bufsiz,
12084 const void *ppc_pmu,
12085 int size)
cb2366c1
EBM
12086{
12087 char *note_name = "LINUX";
12088 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12089 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
12090}
12091
12092char *
12093elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
12094 char *buf,
12095 int *bufsiz,
12096 const void *ppc_tm_cgpr,
12097 int size)
cb2366c1
EBM
12098{
12099 char *note_name = "LINUX";
12100 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12101 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
12102}
12103
12104char *
12105elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
12106 char *buf,
12107 int *bufsiz,
12108 const void *ppc_tm_cfpr,
12109 int size)
cb2366c1
EBM
12110{
12111 char *note_name = "LINUX";
12112 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12113 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
12114}
12115
12116char *
12117elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
12118 char *buf,
12119 int *bufsiz,
12120 const void *ppc_tm_cvmx,
12121 int size)
cb2366c1
EBM
12122{
12123 char *note_name = "LINUX";
12124 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12125 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
12126}
12127
12128char *
12129elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
12130 char *buf,
12131 int *bufsiz,
12132 const void *ppc_tm_cvsx,
12133 int size)
cb2366c1
EBM
12134{
12135 char *note_name = "LINUX";
12136 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12137 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
12138}
12139
12140char *
12141elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
12142 char *buf,
12143 int *bufsiz,
12144 const void *ppc_tm_spr,
12145 int size)
cb2366c1
EBM
12146{
12147 char *note_name = "LINUX";
12148 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12149 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
12150}
12151
12152char *
12153elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
12154 char *buf,
12155 int *bufsiz,
12156 const void *ppc_tm_ctar,
12157 int size)
cb2366c1
EBM
12158{
12159 char *note_name = "LINUX";
12160 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12161 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
12162}
12163
12164char *
12165elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
12166 char *buf,
12167 int *bufsiz,
12168 const void *ppc_tm_cppr,
12169 int size)
cb2366c1
EBM
12170{
12171 char *note_name = "LINUX";
12172 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12173 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
12174}
12175
12176char *
12177elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
12178 char *buf,
12179 int *bufsiz,
12180 const void *ppc_tm_cdscr,
12181 int size)
cb2366c1
EBM
12182{
12183 char *note_name = "LINUX";
12184 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12185 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
12186}
12187
0675e188
UW
12188static char *
12189elfcore_write_s390_high_gprs (bfd *abfd,
12190 char *buf,
12191 int *bufsiz,
12192 const void *s390_high_gprs,
12193 int size)
12194{
12195 char *note_name = "LINUX";
12196 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12197 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
12198 s390_high_gprs, size);
12199}
12200
d7eeb400
MS
12201char *
12202elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
12203 char *buf,
12204 int *bufsiz,
12205 const void *s390_timer,
12206 int size)
d7eeb400
MS
12207{
12208 char *note_name = "LINUX";
12209 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12210 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
12211}
12212
12213char *
12214elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
12215 char *buf,
12216 int *bufsiz,
12217 const void *s390_todcmp,
12218 int size)
d7eeb400
MS
12219{
12220 char *note_name = "LINUX";
12221 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12222 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
12223}
12224
12225char *
12226elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
12227 char *buf,
12228 int *bufsiz,
12229 const void *s390_todpreg,
12230 int size)
d7eeb400
MS
12231{
12232 char *note_name = "LINUX";
12233 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12234 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
12235}
12236
12237char *
12238elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
12239 char *buf,
12240 int *bufsiz,
12241 const void *s390_ctrs,
12242 int size)
d7eeb400
MS
12243{
12244 char *note_name = "LINUX";
12245 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12246 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
12247}
12248
12249char *
12250elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
12251 char *buf,
12252 int *bufsiz,
12253 const void *s390_prefix,
12254 int size)
d7eeb400
MS
12255{
12256 char *note_name = "LINUX";
12257 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12258 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
12259}
12260
355b81d9
UW
12261char *
12262elfcore_write_s390_last_break (bfd *abfd,
12263 char *buf,
12264 int *bufsiz,
12265 const void *s390_last_break,
12266 int size)
12267{
12268 char *note_name = "LINUX";
12269 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12270 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
12271 s390_last_break, size);
12272}
12273
12274char *
12275elfcore_write_s390_system_call (bfd *abfd,
12276 char *buf,
12277 int *bufsiz,
12278 const void *s390_system_call,
12279 int size)
12280{
12281 char *note_name = "LINUX";
12282 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12283 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
12284 s390_system_call, size);
12285}
12286
abb3f6cc
NC
12287char *
12288elfcore_write_s390_tdb (bfd *abfd,
12289 char *buf,
12290 int *bufsiz,
12291 const void *s390_tdb,
12292 int size)
12293{
12294 char *note_name = "LINUX";
12295 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12296 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
12297}
12298
4ef9f41a
AA
12299char *
12300elfcore_write_s390_vxrs_low (bfd *abfd,
12301 char *buf,
12302 int *bufsiz,
12303 const void *s390_vxrs_low,
12304 int size)
12305{
12306 char *note_name = "LINUX";
12307 return elfcore_write_note (abfd, buf, bufsiz,
12308 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12309}
12310
12311char *
12312elfcore_write_s390_vxrs_high (bfd *abfd,
12313 char *buf,
12314 int *bufsiz,
12315 const void *s390_vxrs_high,
12316 int size)
12317{
12318 char *note_name = "LINUX";
12319 return elfcore_write_note (abfd, buf, bufsiz,
12320 note_name, NT_S390_VXRS_HIGH,
12321 s390_vxrs_high, size);
12322}
12323
88ab90e8
AA
12324char *
12325elfcore_write_s390_gs_cb (bfd *abfd,
12326 char *buf,
12327 int *bufsiz,
12328 const void *s390_gs_cb,
12329 int size)
12330{
12331 char *note_name = "LINUX";
12332 return elfcore_write_note (abfd, buf, bufsiz,
12333 note_name, NT_S390_GS_CB,
12334 s390_gs_cb, size);
12335}
12336
12337char *
12338elfcore_write_s390_gs_bc (bfd *abfd,
12339 char *buf,
12340 int *bufsiz,
12341 const void *s390_gs_bc,
12342 int size)
12343{
12344 char *note_name = "LINUX";
12345 return elfcore_write_note (abfd, buf, bufsiz,
12346 note_name, NT_S390_GS_BC,
12347 s390_gs_bc, size);
12348}
12349
faa9a424
UW
12350char *
12351elfcore_write_arm_vfp (bfd *abfd,
12352 char *buf,
12353 int *bufsiz,
12354 const void *arm_vfp,
12355 int size)
12356{
12357 char *note_name = "LINUX";
12358 return elfcore_write_note (abfd, buf, bufsiz,
12359 note_name, NT_ARM_VFP, arm_vfp, size);
12360}
12361
652451f8
YZ
12362char *
12363elfcore_write_aarch_tls (bfd *abfd,
12364 char *buf,
12365 int *bufsiz,
12366 const void *aarch_tls,
12367 int size)
12368{
12369 char *note_name = "LINUX";
12370 return elfcore_write_note (abfd, buf, bufsiz,
12371 note_name, NT_ARM_TLS, aarch_tls, size);
12372}
12373
12374char *
12375elfcore_write_aarch_hw_break (bfd *abfd,
12376 char *buf,
12377 int *bufsiz,
12378 const void *aarch_hw_break,
12379 int size)
12380{
12381 char *note_name = "LINUX";
12382 return elfcore_write_note (abfd, buf, bufsiz,
12383 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12384}
12385
12386char *
12387elfcore_write_aarch_hw_watch (bfd *abfd,
12388 char *buf,
12389 int *bufsiz,
12390 const void *aarch_hw_watch,
12391 int size)
12392{
12393 char *note_name = "LINUX";
12394 return elfcore_write_note (abfd, buf, bufsiz,
12395 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12396}
12397
ad1cc4e4
AH
12398char *
12399elfcore_write_aarch_sve (bfd *abfd,
12400 char *buf,
12401 int *bufsiz,
12402 const void *aarch_sve,
12403 int size)
12404{
12405 char *note_name = "LINUX";
12406 return elfcore_write_note (abfd, buf, bufsiz,
12407 note_name, NT_ARM_SVE, aarch_sve, size);
12408}
12409
e6c3b5bf
AH
12410char *
12411elfcore_write_aarch_pauth (bfd *abfd,
12412 char *buf,
12413 int *bufsiz,
12414 const void *aarch_pauth,
12415 int size)
12416{
12417 char *note_name = "LINUX";
12418 return elfcore_write_note (abfd, buf, bufsiz,
12419 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12420}
12421
f0bbe8ba
LM
12422char *
12423elfcore_write_aarch_mte (bfd *abfd,
12424 char *buf,
12425 int *bufsiz,
12426 const void *aarch_mte,
12427 int size)
12428{
12429 char *note_name = "LINUX";
12430 return elfcore_write_note (abfd, buf, bufsiz,
12431 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12432 aarch_mte,
12433 size);
12434}
12435
27456742
AK
12436char *
12437elfcore_write_arc_v2 (bfd *abfd,
12438 char *buf,
12439 int *bufsiz,
12440 const void *arc_v2,
12441 int size)
12442{
12443 char *note_name = "LINUX";
12444 return elfcore_write_note (abfd, buf, bufsiz,
12445 note_name, NT_ARC_V2, arc_v2, size);
12446}
12447
e214f8db 12448char *
12449elfcore_write_loongarch_cpucfg (bfd *abfd,
12450 char *buf,
12451 int *bufsiz,
12452 const void *loongarch_cpucfg,
12453 int size)
12454{
12455 char *note_name = "LINUX";
12456 return elfcore_write_note (abfd, buf, bufsiz,
12457 note_name, NT_LARCH_CPUCFG,
12458 loongarch_cpucfg, size);
12459}
12460
12461char *
12462elfcore_write_loongarch_lbt (bfd *abfd,
12463 char *buf,
12464 int *bufsiz,
12465 const void *loongarch_lbt,
12466 int size)
12467{
12468 char *note_name = "LINUX";
12469 return elfcore_write_note (abfd, buf, bufsiz,
12470 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12471}
12472
12473char *
12474elfcore_write_loongarch_lsx (bfd *abfd,
12475 char *buf,
12476 int *bufsiz,
12477 const void *loongarch_lsx,
12478 int size)
12479{
12480 char *note_name = "LINUX";
12481 return elfcore_write_note (abfd, buf, bufsiz,
12482 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12483}
12484
12485char *
12486elfcore_write_loongarch_lasx (bfd *abfd,
12487 char *buf,
12488 int *bufsiz,
12489 const void *loongarch_lasx,
12490 int size)
12491{
12492 char *note_name = "LINUX";
12493 return elfcore_write_note (abfd, buf, bufsiz,
12494 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12495}
12496
db6092f3
AB
12497/* Write the buffer of csr values in CSRS (length SIZE) into the note
12498 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12499 written into. Return a pointer to the new start of the note buffer, to
12500 replace BUF which may no longer be valid. */
12501
12502char *
12503elfcore_write_riscv_csr (bfd *abfd,
11c6a7c6
AM
12504 char *buf,
12505 int *bufsiz,
12506 const void *csrs,
12507 int size)
db6092f3
AB
12508{
12509 const char *note_name = "GDB";
12510 return elfcore_write_note (abfd, buf, bufsiz,
12511 note_name, NT_RISCV_CSR, csrs, size);
12512}
12513
b63a5e38
AB
12514/* Write the target description (a string) pointed to by TDESC, length
12515 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12516 note is being written into. Return a pointer to the new start of the
12517 note buffer, to replace BUF which may no longer be valid. */
12518
12519char *
12520elfcore_write_gdb_tdesc (bfd *abfd,
12521 char *buf,
12522 int *bufsiz,
12523 const void *tdesc,
12524 int size)
12525{
12526 const char *note_name = "GDB";
12527 return elfcore_write_note (abfd, buf, bufsiz,
11c6a7c6 12528 note_name, NT_GDB_TDESC, tdesc, size);
b63a5e38
AB
12529}
12530
bb864ac1
CES
12531char *
12532elfcore_write_register_note (bfd *abfd,
12533 char *buf,
12534 int *bufsiz,
12535 const char *section,
12536 const void *data,
12537 int size)
12538{
12539 if (strcmp (section, ".reg2") == 0)
12540 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12541 if (strcmp (section, ".reg-xfp") == 0)
12542 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
12543 if (strcmp (section, ".reg-xstate") == 0)
12544 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
b5c2367c
JB
12545 if (strcmp (section, ".reg-x86-segbases") == 0)
12546 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12547 if (strcmp (section, ".reg-ppc-vmx") == 0)
12548 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
12549 if (strcmp (section, ".reg-ppc-vsx") == 0)
12550 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
12551 if (strcmp (section, ".reg-ppc-tar") == 0)
12552 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12553 if (strcmp (section, ".reg-ppc-ppr") == 0)
12554 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12555 if (strcmp (section, ".reg-ppc-dscr") == 0)
12556 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12557 if (strcmp (section, ".reg-ppc-ebb") == 0)
12558 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12559 if (strcmp (section, ".reg-ppc-pmu") == 0)
12560 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12561 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12562 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12563 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12564 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12565 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12566 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12567 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12568 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12569 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12570 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12571 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12572 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12573 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12574 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12575 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12576 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
12577 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12578 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12579 if (strcmp (section, ".reg-s390-timer") == 0)
12580 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12581 if (strcmp (section, ".reg-s390-todcmp") == 0)
12582 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12583 if (strcmp (section, ".reg-s390-todpreg") == 0)
12584 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12585 if (strcmp (section, ".reg-s390-ctrs") == 0)
12586 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12587 if (strcmp (section, ".reg-s390-prefix") == 0)
12588 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12589 if (strcmp (section, ".reg-s390-last-break") == 0)
12590 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12591 if (strcmp (section, ".reg-s390-system-call") == 0)
12592 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12593 if (strcmp (section, ".reg-s390-tdb") == 0)
12594 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12595 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12596 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12597 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12598 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12599 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12600 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12601 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12602 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12603 if (strcmp (section, ".reg-arm-vfp") == 0)
12604 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12605 if (strcmp (section, ".reg-aarch-tls") == 0)
12606 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12607 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12608 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12609 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12610 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12611 if (strcmp (section, ".reg-aarch-sve") == 0)
12612 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12613 if (strcmp (section, ".reg-aarch-pauth") == 0)
12614 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
f0bbe8ba
LM
12615 if (strcmp (section, ".reg-aarch-mte") == 0)
12616 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
27456742
AK
12617 if (strcmp (section, ".reg-arc-v2") == 0)
12618 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
b63a5e38
AB
12619 if (strcmp (section, ".gdb-tdesc") == 0)
12620 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
db6092f3
AB
12621 if (strcmp (section, ".reg-riscv-csr") == 0)
12622 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
e214f8db 12623 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12624 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12625 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12626 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12627 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12628 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12629 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12630 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12631 return NULL;
12632}
12633
4cb1265b
MS
12634char *
12635elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12636 const void *buf, int bufsiz)
12637{
12638 return elfcore_write_note (obfd, note_data, note_size,
12639 "CORE", NT_FILE, buf, bufsiz);
12640}
12641
0a1b45a2 12642static bool
276da9b3
L
12643elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12644 size_t align)
252b5132 12645{
c044fabd 12646 char *p;
252b5132 12647
276da9b3
L
12648 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12649 gABI specifies that PT_NOTE alignment should be aligned to 4
12650 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12651 align is less than 4, we use 4 byte alignment. */
12652 if (align < 4)
12653 align = 4;
ef135d43 12654 if (align != 4 && align != 8)
0a1b45a2 12655 return false;
276da9b3 12656
252b5132
RH
12657 p = buf;
12658 while (p < buf + size)
12659 {
c044fabd 12660 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12661 Elf_Internal_Note in;
12662
baea7ef1 12663 if (offsetof (Elf_External_Note, name) > buf - p + size)
0a1b45a2 12664 return false;
baea7ef1 12665
dc810e39 12666 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12667
dc810e39 12668 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12669 in.namedata = xnp->name;
baea7ef1 12670 if (in.namesz > buf - in.namedata + size)
0a1b45a2 12671 return false;
252b5132 12672
dc810e39 12673 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12674 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12675 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12676 if (in.descsz != 0
12677 && (in.descdata >= buf + size
12678 || in.descsz > buf - in.descdata + size))
0a1b45a2 12679 return false;
252b5132 12680
718175fa 12681 switch (bfd_get_format (abfd))
07d6d2b8 12682 {
718175fa 12683 default:
0a1b45a2 12684 return true;
718175fa
JK
12685
12686 case bfd_core:
f64e188b 12687 {
8acbedd6 12688#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12689 struct
718175fa 12690 {
f64e188b 12691 const char * string;
8acbedd6 12692 size_t len;
0a1b45a2 12693 bool (*func) (bfd *, Elf_Internal_Note *);
718175fa 12694 }
f64e188b 12695 grokers[] =
b15fa79e 12696 {
8acbedd6 12697 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12698 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6 12699 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
6420dd27 12700 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
8acbedd6 12701 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb 12702 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
6420dd27
LB
12703 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12704 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
f64e188b 12705 };
8acbedd6 12706#undef GROKER_ELEMENT
f64e188b
NC
12707 int i;
12708
12709 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12710 {
12711 if (in.namesz >= grokers[i].len
12712 && strncmp (in.namedata, grokers[i].string,
12713 grokers[i].len) == 0)
12714 {
12715 if (! grokers[i].func (abfd, & in))
0a1b45a2 12716 return false;
8acbedd6
KS
12717 break;
12718 }
12719 }
f64e188b
NC
12720 break;
12721 }
718175fa
JK
12722
12723 case bfd_object:
12724 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12725 {
12726 if (! elfobj_grok_gnu_note (abfd, &in))
0a1b45a2 12727 return false;
718175fa 12728 }
e21e5835
NC
12729 else if (in.namesz == sizeof "stapsdt"
12730 && strcmp (in.namedata, "stapsdt") == 0)
12731 {
12732 if (! elfobj_grok_stapsdt_note (abfd, &in))
0a1b45a2 12733 return false;
e21e5835 12734 }
718175fa 12735 break;
08a40648 12736 }
252b5132 12737
276da9b3 12738 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12739 }
12740
0a1b45a2 12741 return true;
718175fa
JK
12742}
12743
0a1b45a2 12744bool
276da9b3
L
12745elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12746 size_t align)
718175fa
JK
12747{
12748 char *buf;
12749
957e1fc1 12750 if (size == 0 || (size + 1) == 0)
0a1b45a2 12751 return true;
718175fa
JK
12752
12753 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
0a1b45a2 12754 return false;
718175fa 12755
2bb3687b 12756 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa 12757 if (buf == NULL)
0a1b45a2 12758 return false;
718175fa 12759
f64e188b
NC
12760 /* PR 17512: file: ec08f814
12761 0-termintate the buffer so that string searches will not overflow. */
12762 buf[size] = 0;
12763
2bb3687b 12764 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12765 {
12766 free (buf);
0a1b45a2 12767 return false;
718175fa
JK
12768 }
12769
252b5132 12770 free (buf);
0a1b45a2 12771 return true;
252b5132 12772}
98d8431c
JB
12773\f
12774/* Providing external access to the ELF program header table. */
12775
12776/* Return an upper bound on the number of bytes required to store a
12777 copy of ABFD's program header table entries. Return -1 if an error
12778 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12779
98d8431c 12780long
217aa764 12781bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12782{
12783 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12784 {
12785 bfd_set_error (bfd_error_wrong_format);
12786 return -1;
12787 }
12788
936e320b 12789 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12790}
12791
98d8431c
JB
12792/* Copy ABFD's program header table entries to *PHDRS. The entries
12793 will be stored as an array of Elf_Internal_Phdr structures, as
12794 defined in include/elf/internal.h. To find out how large the
12795 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12796
12797 Return the number of program header table entries read, or -1 if an
12798 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12799
98d8431c 12800int
217aa764 12801bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12802{
12803 int num_phdrs;
12804
12805 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12806 {
12807 bfd_set_error (bfd_error_wrong_format);
12808 return -1;
12809 }
12810
12811 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12812 if (num_phdrs != 0)
12813 memcpy (phdrs, elf_tdata (abfd)->phdr,
12814 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12815
12816 return num_phdrs;
12817}
ae4221d7 12818
db6751f2 12819enum elf_reloc_type_class
7e612e98
AM
12820_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12821 const asection *rel_sec ATTRIBUTE_UNUSED,
12822 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12823{
12824 return reloc_class_normal;
12825}
f8df10f4 12826
47d9a591 12827/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12828 relocation against a local symbol. */
12829
12830bfd_vma
217aa764
AM
12831_bfd_elf_rela_local_sym (bfd *abfd,
12832 Elf_Internal_Sym *sym,
8517fae7 12833 asection **psec,
217aa764 12834 Elf_Internal_Rela *rel)
f8df10f4 12835{
8517fae7 12836 asection *sec = *psec;
f8df10f4
JJ
12837 bfd_vma relocation;
12838
6835821b
AM
12839 relocation = (sec->output_section->vma
12840 + sec->output_offset
12841 + sym->st_value);
f8df10f4 12842 if ((sec->flags & SEC_MERGE)
c629eae0 12843 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12844 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12845 {
f8df10f4 12846 rel->r_addend =
8517fae7 12847 _bfd_merged_section_offset (abfd, psec,
65765700 12848 elf_section_data (sec)->sec_info,
753731ee
AM
12849 sym->st_value + rel->r_addend);
12850 if (sec != *psec)
12851 {
12852 /* If we have changed the section, and our original section is
12853 marked with SEC_EXCLUDE, it means that the original
12854 SEC_MERGE section has been completely subsumed in some
12855 other SEC_MERGE section. In this case, we need to leave
12856 some info around for --emit-relocs. */
12857 if ((sec->flags & SEC_EXCLUDE) != 0)
12858 sec->kept_section = *psec;
12859 sec = *psec;
12860 }
8517fae7
AM
12861 rel->r_addend -= relocation;
12862 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12863 }
12864 return relocation;
12865}
c629eae0
JJ
12866
12867bfd_vma
217aa764
AM
12868_bfd_elf_rel_local_sym (bfd *abfd,
12869 Elf_Internal_Sym *sym,
12870 asection **psec,
12871 bfd_vma addend)
47d9a591 12872{
c629eae0
JJ
12873 asection *sec = *psec;
12874
6835821b 12875 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12876 return sym->st_value + addend;
12877
12878 return _bfd_merged_section_offset (abfd, psec,
65765700 12879 elf_section_data (sec)->sec_info,
753731ee 12880 sym->st_value + addend);
c629eae0
JJ
12881}
12882
37b01f6a
DG
12883/* Adjust an address within a section. Given OFFSET within SEC, return
12884 the new offset within the section, based upon changes made to the
12885 section. Returns -1 if the offset is now invalid.
12886 The offset (in abnd out) is in target sized bytes, however big a
12887 byte may be. */
12888
c629eae0 12889bfd_vma
217aa764 12890_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12891 struct bfd_link_info *info,
217aa764
AM
12892 asection *sec,
12893 bfd_vma offset)
c629eae0 12894{
68bfbfcc 12895 switch (sec->sec_info_type)
65765700 12896 {
dbaa2011 12897 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12898 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12899 offset);
dbaa2011 12900 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12901 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12902
65765700 12903 default:
310fd250
L
12904 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12905 {
37b01f6a 12906 /* Reverse the offset. */
310fd250
L
12907 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12908 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12909
12910 /* address_size and sec->size are in octets. Convert
12911 to bytes before subtracting the original offset. */
61826503 12912 offset = ((sec->size - address_size)
bb294208 12913 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12914 }
65765700
JJ
12915 return offset;
12916 }
c629eae0 12917}
3333a7c3
RM
12918\f
12919/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12920 reconstruct an ELF file by reading the segments out of remote memory
12921 based on the ELF file header at EHDR_VMA and the ELF program headers it
12922 points to. If not null, *LOADBASEP is filled in with the difference
12923 between the VMAs from which the segments were read, and the VMAs the
12924 file headers (and hence BFD's idea of each section's VMA) put them at.
12925
12926 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12927 remote memory at target address VMA into the local buffer at MYADDR; it
12928 should return zero on success or an `errno' code on failure. TEMPL must
12929 be a BFD for an ELF target with the word size and byte order found in
12930 the remote memory. */
12931
12932bfd *
217aa764
AM
12933bfd_elf_bfd_from_remote_memory
12934 (bfd *templ,
12935 bfd_vma ehdr_vma,
f0a5d95a 12936 bfd_size_type size,
217aa764 12937 bfd_vma *loadbasep,
fe78531d 12938 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12939{
12940 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12941 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12942}
4c45e5c9
JJ
12943\f
12944long
c9727e01
AM
12945_bfd_elf_get_synthetic_symtab (bfd *abfd,
12946 long symcount ATTRIBUTE_UNUSED,
12947 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12948 long dynsymcount,
c9727e01
AM
12949 asymbol **dynsyms,
12950 asymbol **ret)
4c45e5c9
JJ
12951{
12952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12953 asection *relplt;
12954 asymbol *s;
12955 const char *relplt_name;
0a1b45a2 12956 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
4c45e5c9
JJ
12957 arelent *p;
12958 long count, i, n;
12959 size_t size;
12960 Elf_Internal_Shdr *hdr;
12961 char *names;
12962 asection *plt;
12963
8615f3f2
AM
12964 *ret = NULL;
12965
90e3cdf2
JJ
12966 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12967 return 0;
12968
8615f3f2
AM
12969 if (dynsymcount <= 0)
12970 return 0;
12971
4c45e5c9
JJ
12972 if (!bed->plt_sym_val)
12973 return 0;
12974
12975 relplt_name = bed->relplt_name;
12976 if (relplt_name == NULL)
d35fd659 12977 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12978 relplt = bfd_get_section_by_name (abfd, relplt_name);
12979 if (relplt == NULL)
12980 return 0;
12981
12982 hdr = &elf_section_data (relplt)->this_hdr;
12983 if (hdr->sh_link != elf_dynsymtab (abfd)
12984 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12985 return 0;
12986
12987 plt = bfd_get_section_by_name (abfd, ".plt");
12988 if (plt == NULL)
12989 return 0;
12990
12991 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 12992 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
4c45e5c9
JJ
12993 return -1;
12994
eea6121a 12995 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12996 size = count * sizeof (asymbol);
12997 p = relplt->relocation;
cb53bf42 12998 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12999 {
13000 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13001 if (p->addend != 0)
13002 {
13003#ifdef BFD64
13004 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13005#else
13006 size += sizeof ("+0x") - 1 + 8;
13007#endif
13008 }
13009 }
4c45e5c9 13010
a50b1753 13011 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
13012 if (s == NULL)
13013 return -1;
13014
13015 names = (char *) (s + count);
13016 p = relplt->relocation;
13017 n = 0;
cb53bf42 13018 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
13019 {
13020 size_t len;
13021 bfd_vma addr;
13022
13023 addr = bed->plt_sym_val (i, plt, p);
13024 if (addr == (bfd_vma) -1)
13025 continue;
13026
13027 *s = **p->sym_ptr_ptr;
65a7a66f
AM
13028 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13029 we are defining a symbol, ensure one of them is set. */
13030 if ((s->flags & BSF_LOCAL) == 0)
13031 s->flags |= BSF_GLOBAL;
6ba2a415 13032 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
13033 s->section = plt;
13034 s->value = addr - plt->vma;
13035 s->name = names;
8f39ba8e 13036 s->udata.p = NULL;
4c45e5c9
JJ
13037 len = strlen ((*p->sym_ptr_ptr)->name);
13038 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13039 names += len;
041de40d
AM
13040 if (p->addend != 0)
13041 {
1d770845 13042 char buf[30], *a;
d324f6d6 13043
041de40d
AM
13044 memcpy (names, "+0x", sizeof ("+0x") - 1);
13045 names += sizeof ("+0x") - 1;
1d770845
L
13046 bfd_sprintf_vma (abfd, buf, p->addend);
13047 for (a = buf; *a == '0'; ++a)
13048 ;
13049 len = strlen (a);
13050 memcpy (names, a, len);
13051 names += len;
041de40d 13052 }
4c45e5c9
JJ
13053 memcpy (names, "@plt", sizeof ("@plt"));
13054 names += sizeof ("@plt");
8f39ba8e 13055 ++s, ++n;
4c45e5c9
JJ
13056 }
13057
13058 return n;
13059}
3d7f7666 13060
821e6ff6
AM
13061/* It is only used by x86-64 so far.
13062 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
13063 but current usage would allow all of _bfd_std_section to be zero. */
13064static const asymbol lcomm_sym
13065 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 13066asection _bfd_elf_large_com_section
7eacd66b 13067 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 13068 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 13069
0a1b45a2 13070bool
cc364be6 13071_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
13072{
13073 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
13074
13075 i_ehdrp = elf_elfheader (abfd);
13076
06f44071
AM
13077 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13078 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 13079
df3a023b 13080 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
13081 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13082 or STB_GNU_UNIQUE binding. */
cc364be6
AM
13083 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13084 {
13085 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13086 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13087 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13088 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13089 {
13090 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
13091 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13092 "and FreeBSD targets"));
cc364be6 13093 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
13094 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13095 "only by GNU and FreeBSD targets"));
cc364be6 13096 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
13097 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13098 "only by GNU and FreeBSD targets"));
13099 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13100 _bfd_error_handler (_("GNU_RETAIN section is supported "
13101 "only by GNU and FreeBSD targets"));
9aea1e31 13102 bfd_set_error (bfd_error_sorry);
0a1b45a2 13103 return false;
cc364be6
AM
13104 }
13105 }
0a1b45a2 13106 return true;
d1036acb 13107}
fcb93ecf
PB
13108
13109
13110/* Return TRUE for ELF symbol types that represent functions.
13111 This is the default version of this function, which is sufficient for
d8045f23 13112 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf 13113
0a1b45a2 13114bool
fcb93ecf
PB
13115_bfd_elf_is_function_type (unsigned int type)
13116{
d8045f23
NC
13117 return (type == STT_FUNC
13118 || type == STT_GNU_IFUNC);
fcb93ecf 13119}
9f296da3 13120
aef36ac1
AM
13121/* If the ELF symbol SYM might be a function in SEC, return the
13122 function size and set *CODE_OFF to the function's entry point,
13123 otherwise return zero. */
9f296da3 13124
aef36ac1
AM
13125bfd_size_type
13126_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13127 bfd_vma *code_off)
9f296da3 13128{
aef36ac1 13129 bfd_size_type size;
24aebc79 13130 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1 13131
ff9e0f5b 13132 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
13133 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13134 || sym->section != sec)
13135 return 0;
ff9e0f5b 13136
24aebc79 13137 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
11c6a7c6 13138
24aebc79
NC
13139 /* In theory we should check that the symbol's type satisfies
13140 _bfd_elf_is_function_type(), but there are some function-like
13141 symbols which would fail this test. (eg _start). Instead
13142 we check for hidden, local, notype symbols with zero size.
13143 This type of symbol is generated by the annobin plugin for gcc
13144 and clang, and should not be considered to be a function symbol. */
13145 if (size == 0
13146 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13147 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13148 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13149 return 0;
11c6a7c6 13150
ff9e0f5b 13151 *code_off = sym->value;
24aebc79
NC
13152 /* Do not return 0 for the function's size. */
13153 return size ? size : 1;
9f296da3 13154}
a8e14f4c
NC
13155
13156/* Set to non-zero to enable some debug messages. */
13157#define DEBUG_SECONDARY_RELOCS 0
13158
13159/* An internal-to-the-bfd-library only section type
13160 used to indicate a cached secondary reloc section. */
13161#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13162
13163/* Create a BFD section to hold a secondary reloc section. */
13164
0a1b45a2 13165bool
a8e14f4c
NC
13166_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13167 Elf_Internal_Shdr *hdr,
13168 const char * name,
13169 unsigned int shindex)
13170{
13171 /* We only support RELA secondary relocs. */
13172 if (hdr->sh_type != SHT_RELA)
0a1b45a2 13173 return false;
a8e14f4c
NC
13174
13175#if DEBUG_SECONDARY_RELOCS
13176 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13177#endif
13178 hdr->sh_type = SHT_SECONDARY_RELOC;
13179 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13180}
13181
13182/* Read in any secondary relocs associated with SEC. */
13183
0a1b45a2 13184bool
f60742b2
NC
13185_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13186 asection * sec,
13187 asymbol ** symbols,
0a1b45a2 13188 bool dynamic)
a8e14f4c
NC
13189{
13190 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13191 asection * relsec;
0a1b45a2 13192 bool result = true;
a8e14f4c
NC
13193 bfd_vma (*r_sym) (bfd_vma);
13194
13195#if BFD_DEFAULT_TARGET_SIZE > 32
13196 if (bfd_arch_bits_per_address (abfd) != 32)
13197 r_sym = elf64_r_sym;
13198 else
13199#endif
13200 r_sym = elf32_r_sym;
11c6a7c6 13201
956ea65c
MM
13202 if (!elf_section_data (sec)->has_secondary_relocs)
13203 return true;
13204
a8e14f4c
NC
13205 /* Discover if there are any secondary reloc sections
13206 associated with SEC. */
13207 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13208 {
13209 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13210
13211 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
13212 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13213 && (hdr->sh_entsize == ebd->s->sizeof_rel
13214 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
13215 {
13216 bfd_byte * native_relocs;
13217 bfd_byte * native_reloc;
13218 arelent * internal_relocs;
13219 arelent * internal_reloc;
13220 unsigned int i;
13221 unsigned int entsize;
13222 unsigned int symcount;
13223 unsigned int reloc_count;
13224 size_t amt;
13225
13226 if (ebd->elf_info_to_howto == NULL)
0a1b45a2 13227 return false;
a8e14f4c
NC
13228
13229#if DEBUG_SECONDARY_RELOCS
13230 fprintf (stderr, "read secondary relocs for %s from %s\n",
13231 sec->name, relsec->name);
13232#endif
13233 entsize = hdr->sh_entsize;
13234
13235 native_relocs = bfd_malloc (hdr->sh_size);
13236 if (native_relocs == NULL)
13237 {
0a1b45a2 13238 result = false;
a8e14f4c
NC
13239 continue;
13240 }
13241
13242 reloc_count = NUM_SHDR_ENTRIES (hdr);
13243 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13244 {
ecbbbdba 13245 free (native_relocs);
a8e14f4c 13246 bfd_set_error (bfd_error_file_too_big);
0a1b45a2 13247 result = false;
a8e14f4c
NC
13248 continue;
13249 }
13250
13251 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13252 if (internal_relocs == NULL)
13253 {
13254 free (native_relocs);
0a1b45a2 13255 result = false;
a8e14f4c
NC
13256 continue;
13257 }
13258
13259 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13260 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13261 != hdr->sh_size))
13262 {
13263 free (native_relocs);
ecbbbdba
NC
13264 /* The internal_relocs will be freed when
13265 the memory for the bfd is released. */
0a1b45a2 13266 result = false;
a8e14f4c
NC
13267 continue;
13268 }
13269
f60742b2
NC
13270 if (dynamic)
13271 symcount = bfd_get_dynamic_symcount (abfd);
13272 else
13273 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
13274
13275 for (i = 0, internal_reloc = internal_relocs,
13276 native_reloc = native_relocs;
13277 i < reloc_count;
13278 i++, internal_reloc++, native_reloc += entsize)
13279 {
0a1b45a2 13280 bool res;
a8e14f4c
NC
13281 Elf_Internal_Rela rela;
13282
8ee54925
NC
13283 if (entsize == ebd->s->sizeof_rel)
13284 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13285 else /* entsize == ebd->s->sizeof_rela */
13286 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
13287
13288 /* The address of an ELF reloc is section relative for an object
13289 file, and absolute for an executable file or shared library.
13290 The address of a normal BFD reloc is always section relative,
13291 and the address of a dynamic reloc is absolute.. */
13292 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13293 internal_reloc->address = rela.r_offset;
13294 else
13295 internal_reloc->address = rela.r_offset - sec->vma;
13296
13297 if (r_sym (rela.r_info) == STN_UNDEF)
13298 {
13299 /* FIXME: This and the error case below mean that we
13300 have a symbol on relocs that is not elf_symbol_type. */
13301 internal_reloc->sym_ptr_ptr =
13302 bfd_abs_section_ptr->symbol_ptr_ptr;
13303 }
13304 else if (r_sym (rela.r_info) > symcount)
13305 {
13306 _bfd_error_handler
13307 /* xgettext:c-format */
13308 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13309 abfd, sec, i, (long) r_sym (rela.r_info));
13310 bfd_set_error (bfd_error_bad_value);
13311 internal_reloc->sym_ptr_ptr =
13312 bfd_abs_section_ptr->symbol_ptr_ptr;
0a1b45a2 13313 result = false;
a8e14f4c
NC
13314 }
13315 else
13316 {
13317 asymbol **ps;
13318
13319 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
13320 internal_reloc->sym_ptr_ptr = ps;
13321 /* Make sure that this symbol is not removed by strip. */
13322 (*ps)->flags |= BSF_KEEP;
13323 }
13324
13325 internal_reloc->addend = rela.r_addend;
13326
13327 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13328 if (! res || internal_reloc->howto == NULL)
13329 {
13330#if DEBUG_SECONDARY_RELOCS
11c6a7c6
AM
13331 fprintf (stderr,
13332 "there is no howto associated with reloc %lx\n",
a8e14f4c
NC
13333 rela.r_info);
13334#endif
0a1b45a2 13335 result = false;
a8e14f4c
NC
13336 }
13337 }
13338
13339 free (native_relocs);
13340 /* Store the internal relocs. */
13341 elf_section_data (relsec)->sec_info = internal_relocs;
13342 }
13343 }
13344
13345 return result;
13346}
13347
13348/* Set the ELF section header fields of an output secondary reloc section. */
13349
0a1b45a2 13350bool
11c6a7c6
AM
13351_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13352 bfd *obfd ATTRIBUTE_UNUSED,
13353 const Elf_Internal_Shdr *isection,
13354 Elf_Internal_Shdr *osection)
a8e14f4c
NC
13355{
13356 asection * isec;
13357 asection * osec;
44466e45 13358 struct bfd_elf_section_data * esd;
a8e14f4c
NC
13359
13360 if (isection == NULL)
0a1b45a2 13361 return false;
a8e14f4c
NC
13362
13363 if (isection->sh_type != SHT_SECONDARY_RELOC)
0a1b45a2 13364 return true;
a8e14f4c
NC
13365
13366 isec = isection->bfd_section;
13367 if (isec == NULL)
0a1b45a2 13368 return false;
a8e14f4c
NC
13369
13370 osec = osection->bfd_section;
13371 if (osec == NULL)
0a1b45a2 13372 return false;
a8e14f4c 13373
44466e45
NC
13374 esd = elf_section_data (osec);
13375 BFD_ASSERT (esd->sec_info == NULL);
13376 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
13377 osection->sh_type = SHT_RELA;
13378 osection->sh_link = elf_onesymtab (obfd);
13379 if (osection->sh_link == 0)
13380 {
13381 /* There is no symbol table - we are hosed... */
13382 _bfd_error_handler
13383 /* xgettext:c-format */
11c6a7c6
AM
13384 (_("%pB(%pA): link section cannot be set"
13385 " because the output file does not have a symbol table"),
a8e14f4c
NC
13386 obfd, osec);
13387 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13388 return false;
a8e14f4c
NC
13389 }
13390
11c6a7c6
AM
13391 /* Find the output section that corresponds to the isection's
13392 sh_info link. */
327ef784
NC
13393 if (isection->sh_info == 0
13394 || isection->sh_info >= elf_numsections (ibfd))
13395 {
13396 _bfd_error_handler
13397 /* xgettext:c-format */
13398 (_("%pB(%pA): info section index is invalid"),
13399 obfd, osec);
13400 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13401 return false;
327ef784
NC
13402 }
13403
a8e14f4c
NC
13404 isection = elf_elfsections (ibfd)[isection->sh_info];
13405
327ef784
NC
13406 if (isection == NULL
13407 || isection->bfd_section == NULL
13408 || isection->bfd_section->output_section == NULL)
13409 {
13410 _bfd_error_handler
13411 /* xgettext:c-format */
11c6a7c6
AM
13412 (_("%pB(%pA): info section index cannot be set"
13413 " because the section is not in the output"),
327ef784
NC
13414 obfd, osec);
13415 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13416 return false;
327ef784
NC
13417 }
13418
44466e45
NC
13419 esd = elf_section_data (isection->bfd_section->output_section);
13420 BFD_ASSERT (esd != NULL);
13421 osection->sh_info = esd->this_idx;
0a1b45a2 13422 esd->has_secondary_relocs = true;
a8e14f4c
NC
13423#if DEBUG_SECONDARY_RELOCS
13424 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13425 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
13426 fprintf (stderr, "mark section %s as having secondary relocs\n",
13427 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
13428#endif
13429
0a1b45a2 13430 return true;
a8e14f4c
NC
13431}
13432
44466e45
NC
13433/* Write out a secondary reloc section.
13434
13435 FIXME: Currently this function can result in a serious performance penalty
13436 for files with secondary relocs and lots of sections. The proper way to
13437 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13438 relocs together and then to have this function just walk that chain. */
a8e14f4c 13439
0a1b45a2 13440bool
a8e14f4c
NC
13441_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13442{
13443 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13444 bfd_vma addr_offset;
13445 asection * relsec;
13446 bfd_vma (*r_info) (bfd_vma, bfd_vma);
0a1b45a2 13447 bool result = true;
ac4bf06c
NC
13448
13449 if (sec == NULL)
0a1b45a2 13450 return false;
a8e14f4c
NC
13451
13452#if BFD_DEFAULT_TARGET_SIZE > 32
13453 if (bfd_arch_bits_per_address (abfd) != 32)
13454 r_info = elf64_r_info;
13455 else
13456#endif
13457 r_info = elf32_r_info;
13458
a8e14f4c
NC
13459 /* The address of an ELF reloc is section relative for an object
13460 file, and absolute for an executable file or shared library.
13461 The address of a BFD reloc is always section relative. */
13462 addr_offset = 0;
13463 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13464 addr_offset = sec->vma;
13465
13466 /* Discover if there are any secondary reloc sections
13467 associated with SEC. */
13468 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13469 {
13470 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13471 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13472
13473 if (hdr->sh_type == SHT_RELA
13474 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13475 {
13476 asymbol * last_sym;
13477 int last_sym_idx;
13478 unsigned int reloc_count;
13479 unsigned int idx;
8ee54925 13480 unsigned int entsize;
a8e14f4c
NC
13481 arelent * src_irel;
13482 bfd_byte * dst_rela;
13483
ac4bf06c
NC
13484 if (hdr->contents != NULL)
13485 {
13486 _bfd_error_handler
13487 /* xgettext:c-format */
13488 (_("%pB(%pA): error: secondary reloc section processed twice"),
13489 abfd, relsec);
13490 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13491 result = false;
ac4bf06c
NC
13492 continue;
13493 }
a8e14f4c 13494
8ee54925
NC
13495 entsize = hdr->sh_entsize;
13496 if (entsize == 0)
ac267c75
NC
13497 {
13498 _bfd_error_handler
13499 /* xgettext:c-format */
11c6a7c6
AM
13500 (_("%pB(%pA): error: secondary reloc section"
13501 " has zero sized entries"),
ac267c75
NC
13502 abfd, relsec);
13503 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13504 result = false;
ac267c75
NC
13505 continue;
13506 }
8ee54925
NC
13507 else if (entsize != ebd->s->sizeof_rel
13508 && entsize != ebd->s->sizeof_rela)
13509 {
13510 _bfd_error_handler
13511 /* xgettext:c-format */
11c6a7c6
AM
13512 (_("%pB(%pA): error: secondary reloc section"
13513 " has non-standard sized entries"),
8ee54925
NC
13514 abfd, relsec);
13515 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13516 result = false;
8ee54925
NC
13517 continue;
13518 }
ac267c75 13519
8ee54925 13520 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
13521 if (reloc_count <= 0)
13522 {
13523 _bfd_error_handler
13524 /* xgettext:c-format */
13525 (_("%pB(%pA): error: secondary reloc section is empty!"),
13526 abfd, relsec);
13527 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13528 result = false;
ac4bf06c
NC
13529 continue;
13530 }
a8e14f4c
NC
13531
13532 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13533 if (hdr->contents == NULL)
13534 continue;
13535
13536#if DEBUG_SECONDARY_RELOCS
13537 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13538 reloc_count, sec->name, relsec->name);
13539#endif
13540 last_sym = NULL;
13541 last_sym_idx = 0;
13542 dst_rela = hdr->contents;
13543 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
13544 if (src_irel == NULL)
13545 {
13546 _bfd_error_handler
13547 /* xgettext:c-format */
11c6a7c6
AM
13548 (_("%pB(%pA): error: internal relocs missing"
13549 " for secondary reloc section"),
ac4bf06c
NC
13550 abfd, relsec);
13551 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13552 result = false;
ac4bf06c
NC
13553 continue;
13554 }
a8e14f4c 13555
8ee54925 13556 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
13557 {
13558 Elf_Internal_Rela src_rela;
13559 arelent *ptr;
13560 asymbol *sym;
13561 int n;
13562
13563 ptr = src_irel + idx;
ac4bf06c
NC
13564 if (ptr == NULL)
13565 {
13566 _bfd_error_handler
13567 /* xgettext:c-format */
13568 (_("%pB(%pA): error: reloc table entry %u is empty"),
13569 abfd, relsec, idx);
13570 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13571 result = false;
ac4bf06c
NC
13572 break;
13573 }
a8e14f4c 13574
ac4bf06c
NC
13575 if (ptr->sym_ptr_ptr == NULL)
13576 {
13577 /* FIXME: Is this an error ? */
13578 n = 0;
13579 }
a8e14f4c
NC
13580 else
13581 {
ac4bf06c
NC
13582 sym = *ptr->sym_ptr_ptr;
13583
13584 if (sym == last_sym)
13585 n = last_sym_idx;
13586 else
a8e14f4c 13587 {
ac4bf06c
NC
13588 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13589 if (n < 0)
13590 {
13591 _bfd_error_handler
13592 /* xgettext:c-format */
11c6a7c6
AM
13593 (_("%pB(%pA): error: secondary reloc %u"
13594 " references a missing symbol"),
ac4bf06c
NC
13595 abfd, relsec, idx);
13596 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13597 result = false;
ac4bf06c
NC
13598 n = 0;
13599 }
13600
13601 last_sym = sym;
13602 last_sym_idx = n;
a8e14f4c 13603 }
a8e14f4c 13604
ac4bf06c
NC
13605 if (sym->the_bfd != NULL
13606 && sym->the_bfd->xvec != abfd->xvec
13607 && ! _bfd_elf_validate_reloc (abfd, ptr))
13608 {
13609 _bfd_error_handler
13610 /* xgettext:c-format */
11c6a7c6
AM
13611 (_("%pB(%pA): error: secondary reloc %u"
13612 " references a deleted symbol"),
ac4bf06c
NC
13613 abfd, relsec, idx);
13614 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13615 result = false;
ac4bf06c
NC
13616 n = 0;
13617 }
a8e14f4c
NC
13618 }
13619
ac4bf06c 13620 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13621 if (ptr->howto == NULL)
13622 {
ac4bf06c
NC
13623 _bfd_error_handler
13624 /* xgettext:c-format */
11c6a7c6
AM
13625 (_("%pB(%pA): error: secondary reloc %u"
13626 " is of an unknown type"),
ac4bf06c
NC
13627 abfd, relsec, idx);
13628 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13629 result = false;
ac4bf06c 13630 src_rela.r_info = r_info (0, 0);
a8e14f4c 13631 }
ac4bf06c
NC
13632 else
13633 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13634 src_rela.r_addend = ptr->addend;
8ee54925
NC
13635
13636 if (entsize == ebd->s->sizeof_rel)
13637 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13638 else /* entsize == ebd->s->sizeof_rela */
13639 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13640 }
13641 }
13642 }
13643
ac4bf06c 13644 return result;
a8e14f4c 13645}