]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
Add note about adding ChangeLog.git to src-release.sh
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
d87bef3a 3 Copyright (C) 1993-2023 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{
0481d5dc 199 uint32_t h = 0;
252b5132 200
0481d5dc
NS
201 for (const unsigned char *name = (const unsigned char *) namearg;
202 *name; name++)
252b5132 203 {
0481d5dc
NS
204 h = (h << 4) + *name;
205 h ^= (h >> 24) & 0xf0;
252b5132 206 }
0481d5dc 207 return h & 0x0fffffff;
252b5132
RH
208}
209
fdc90cb4
JJ
210/* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
212
213unsigned long
214bfd_elf_gnu_hash (const char *namearg)
215{
0481d5dc 216 uint32_t h = 5381;
fdc90cb4 217
0481d5dc
NS
218 for (const unsigned char *name = (const unsigned char *) namearg;
219 *name; name++)
220 h = (h << 5) + h + *name;
221 return h;
fdc90cb4
JJ
222}
223
0c8d6e5c
AM
224/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
0a1b45a2 226bool
0c8d6e5c 227bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 228 size_t object_size,
4dfe6ac6 229 enum elf_target_id object_id)
252b5132 230{
0ffa91dd
NC
231 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232 abfd->tdata.any = bfd_zalloc (abfd, object_size);
233 if (abfd->tdata.any == NULL)
0a1b45a2 234 return false;
252b5132 235
0ffa91dd 236 elf_object_id (abfd) = object_id;
c0355132
AM
237 if (abfd->direction != read_direction)
238 {
239 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240 if (o == NULL)
0a1b45a2 241 return false;
c0355132
AM
242 elf_tdata (abfd)->o = o;
243 elf_program_header_size (abfd) = (bfd_size_type) -1;
244 }
0a1b45a2 245 return true;
252b5132
RH
246}
247
0ffa91dd 248
0a1b45a2 249bool
ae95ffa6 250bfd_elf_make_object (bfd *abfd)
0ffa91dd 251{
ae95ffa6 252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 253 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 254 bed->target_id);
0ffa91dd
NC
255}
256
0a1b45a2 257bool
217aa764 258bfd_elf_mkcorefile (bfd *abfd)
252b5132 259{
c044fabd 260 /* I think this can be done just like an object file. */
228e534f 261 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
0a1b45a2 262 return false;
228e534f
AM
263 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264 return elf_tdata (abfd)->core != NULL;
252b5132
RH
265}
266
6d5944fc 267char *
217aa764 268bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
269{
270 Elf_Internal_Shdr **i_shdrp;
f075ee0c 271 bfd_byte *shstrtab = NULL;
dc810e39
AM
272 file_ptr offset;
273 bfd_size_type shstrtabsize;
252b5132
RH
274
275 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
276 if (i_shdrp == 0
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
f075ee0c 279 return NULL;
252b5132 280
f075ee0c 281 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
282 if (shstrtab == NULL)
283 {
c044fabd 284 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
287
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
3471d59d 290 if (shstrtabsize + 1 <= 1
06614111 291 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
292 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293 shstrtabsize)) == NULL)
294 {
3471d59d
CC
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
299 }
300 else
301 shstrtab[shstrtabsize] = '\0';
217aa764 302 i_shdrp[shindex]->contents = shstrtab;
252b5132 303 }
f075ee0c 304 return (char *) shstrtab;
252b5132
RH
305}
306
307char *
217aa764
AM
308bfd_elf_string_from_elf_section (bfd *abfd,
309 unsigned int shindex,
310 unsigned int strindex)
252b5132
RH
311{
312 Elf_Internal_Shdr *hdr;
313
314 if (strindex == 0)
315 return "";
316
74f2e02b
AM
317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 return NULL;
319
252b5132
RH
320 hdr = elf_elfsections (abfd)[shindex];
321
06614111
NC
322 if (hdr->contents == NULL)
323 {
324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325 {
326 /* PR 17512: file: f057ec89. */
695344c0 327 /* xgettext:c-format */
871b3ab2 328 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 329 " a non-string section (number %d)"),
06614111
NC
330 abfd, shindex);
331 return NULL;
332 }
b1fa9dd6 333
06614111
NC
334 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 return NULL;
336 }
eed5def8
NC
337 else
338 {
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 return NULL;
346 }
252b5132
RH
347
348 if (strindex >= hdr->sh_size)
349 {
1b3a8575 350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 351 _bfd_error_handler
695344c0 352 /* xgettext:c-format */
2dcf00ce
AM
353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 355 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 356 ? ".shstrtab"
1b3a8575 357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 358 return NULL;
252b5132
RH
359 }
360
361 return ((char *) hdr->contents) + strindex;
362}
363
6cdc0ccc
AM
364/* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
371
372Elf_Internal_Sym *
217aa764
AM
373bfd_elf_get_elf_syms (bfd *ibfd,
374 Elf_Internal_Shdr *symtab_hdr,
375 size_t symcount,
376 size_t symoffset,
377 Elf_Internal_Sym *intsym_buf,
378 void *extsym_buf,
379 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
380{
381 Elf_Internal_Shdr *shndx_hdr;
217aa764 382 void *alloc_ext;
df622259 383 const bfd_byte *esym;
6cdc0ccc
AM
384 Elf_External_Sym_Shndx *alloc_extshndx;
385 Elf_External_Sym_Shndx *shndx;
4dd07732 386 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
387 Elf_Internal_Sym *isym;
388 Elf_Internal_Sym *isymend;
9c5bfbb7 389 const struct elf_backend_data *bed;
6cdc0ccc 390 size_t extsym_size;
1f4361a7 391 size_t amt;
6cdc0ccc
AM
392 file_ptr pos;
393
e44a2c9c
AM
394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 abort ();
396
6cdc0ccc
AM
397 if (symcount == 0)
398 return intsym_buf;
399
400 /* Normal syms might have section extension entries. */
401 shndx_hdr = NULL;
6a40cf0c
NC
402 if (elf_symtab_shndx_list (ibfd) != NULL)
403 {
404 elf_section_list * entry;
405 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
406
407 /* Find an index section that is linked to this symtab section. */
408 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
409 {
410 /* PR 20063. */
411 if (entry->hdr.sh_link >= elf_numsections (ibfd))
412 continue;
413
414 if (sections[entry->hdr.sh_link] == symtab_hdr)
415 {
416 shndx_hdr = & entry->hdr;
417 break;
418 };
419 }
6a40cf0c
NC
420
421 if (shndx_hdr == NULL)
422 {
11c6a7c6
AM
423 if (symtab_hdr == &elf_symtab_hdr (ibfd))
424 /* Not really accurate, but this was how the old code used
425 to work. */
426 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
6a40cf0c
NC
427 /* Otherwise we do nothing. The assumption is that
428 the index table will not be needed. */
429 }
430 }
6cdc0ccc
AM
431
432 /* Read the symbols. */
433 alloc_ext = NULL;
434 alloc_extshndx = NULL;
4dd07732 435 alloc_intsym = NULL;
6cdc0ccc
AM
436 bed = get_elf_backend_data (ibfd);
437 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
438 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
439 {
440 bfd_set_error (bfd_error_file_too_big);
441 intsym_buf = NULL;
442 goto out;
443 }
6cdc0ccc
AM
444 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445 if (extsym_buf == NULL)
446 {
1f4361a7 447 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
448 extsym_buf = alloc_ext;
449 }
450 if (extsym_buf == NULL
451 || bfd_seek (ibfd, pos, SEEK_SET) != 0
452 || bfd_bread (extsym_buf, amt, ibfd) != amt)
453 {
454 intsym_buf = NULL;
455 goto out;
456 }
457
458 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
459 extshndx_buf = NULL;
460 else
461 {
1f4361a7
AM
462 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
463 {
464 bfd_set_error (bfd_error_file_too_big);
465 intsym_buf = NULL;
466 goto out;
467 }
6cdc0ccc
AM
468 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
469 if (extshndx_buf == NULL)
470 {
1f4361a7 471 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
472 extshndx_buf = alloc_extshndx;
473 }
474 if (extshndx_buf == NULL
475 || bfd_seek (ibfd, pos, SEEK_SET) != 0
476 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
477 {
478 intsym_buf = NULL;
479 goto out;
480 }
481 }
482
483 if (intsym_buf == NULL)
484 {
1f4361a7
AM
485 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
486 {
487 bfd_set_error (bfd_error_file_too_big);
488 goto out;
489 }
490 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 491 intsym_buf = alloc_intsym;
6cdc0ccc
AM
492 if (intsym_buf == NULL)
493 goto out;
494 }
495
496 /* Convert the symbols to internal form. */
497 isymend = intsym_buf + symcount;
a50b1753 498 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 499 shndx = extshndx_buf;
6cdc0ccc
AM
500 isym < isymend;
501 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
502 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
503 {
504 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 505 /* xgettext:c-format */
871b3ab2 506 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 507 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 508 ibfd, (unsigned long) symoffset);
c9594989 509 free (alloc_intsym);
8384fb8f
AM
510 intsym_buf = NULL;
511 goto out;
512 }
6cdc0ccc
AM
513
514 out:
c9594989
AM
515 free (alloc_ext);
516 free (alloc_extshndx);
6cdc0ccc
AM
517
518 return intsym_buf;
519}
520
5cab59f6
AM
521/* Look up a symbol name. */
522const char *
be8dd2ca
AM
523bfd_elf_sym_name (bfd *abfd,
524 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
525 Elf_Internal_Sym *isym,
526 asection *sym_sec)
5cab59f6 527{
26c61ae5 528 const char *name;
5cab59f6 529 unsigned int iname = isym->st_name;
be8dd2ca 530 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 531
138f35cc
JJ
532 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
533 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 534 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
535 {
536 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
537 shindex = elf_elfheader (abfd)->e_shstrndx;
538 }
539
26c61ae5
L
540 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
541 if (name == NULL)
542 name = "(null)";
543 else if (sym_sec && *name == '\0')
fd361982 544 name = bfd_section_name (sym_sec);
26c61ae5
L
545
546 return name;
5cab59f6
AM
547}
548
dbb410c3
AM
549/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
550 sections. The first element is the flags, the rest are section
551 pointers. */
552
553typedef union elf_internal_group {
554 Elf_Internal_Shdr *shdr;
555 unsigned int flags;
556} Elf_Internal_Group;
557
b885599b
AM
558/* Return the name of the group signature symbol. Why isn't the
559 signature just a string? */
560
561static const char *
217aa764 562group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 563{
9dce4196 564 Elf_Internal_Shdr *hdr;
9dce4196
AM
565 unsigned char esym[sizeof (Elf64_External_Sym)];
566 Elf_External_Sym_Shndx eshndx;
567 Elf_Internal_Sym isym;
b885599b 568
13792e9d
L
569 /* First we need to ensure the symbol table is available. Make sure
570 that it is a symbol table section. */
4fbb74a6
AM
571 if (ghdr->sh_link >= elf_numsections (abfd))
572 return NULL;
13792e9d
L
573 hdr = elf_elfsections (abfd) [ghdr->sh_link];
574 if (hdr->sh_type != SHT_SYMTAB
575 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
576 return NULL;
577
9dce4196
AM
578 /* Go read the symbol. */
579 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
580 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
581 &isym, esym, &eshndx) == NULL)
b885599b 582 return NULL;
9dce4196 583
26c61ae5 584 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
585}
586
dbb410c3
AM
587/* Set next_in_group list pointer, and group name for NEWSECT. */
588
0a1b45a2 589static bool
217aa764 590setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
591{
592 unsigned int num_group = elf_tdata (abfd)->num_group;
593
594 /* If num_group is zero, read in all SHT_GROUP sections. The count
595 is set to -1 if there are no SHT_GROUP sections. */
596 if (num_group == 0)
597 {
598 unsigned int i, shnum;
599
600 /* First count the number of groups. If we have a SHT_GROUP
601 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 602 shnum = elf_numsections (abfd);
dbb410c3 603 num_group = 0;
08a40648 604
44534af3 605#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 606 ( (shdr)->sh_type == SHT_GROUP \
44534af3 607 && (shdr)->sh_size >= minsize \
1783205a
NC
608 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
609 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 610
dbb410c3
AM
611 for (i = 0; i < shnum; i++)
612 {
613 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 614
44534af3 615 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
616 num_group += 1;
617 }
618
619 if (num_group == 0)
20dbb49d
L
620 {
621 num_group = (unsigned) -1;
622 elf_tdata (abfd)->num_group = num_group;
ce497010 623 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
624 }
625 else
dbb410c3
AM
626 {
627 /* We keep a list of elf section headers for group sections,
628 so we can find them quickly. */
1f4361a7 629 size_t amt;
d0fb9a8d 630
20dbb49d 631 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
632 amt = num_group * sizeof (Elf_Internal_Shdr *);
633 elf_tdata (abfd)->group_sect_ptr
634 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 635 if (elf_tdata (abfd)->group_sect_ptr == NULL)
0a1b45a2 636 return false;
dbb410c3 637 num_group = 0;
ce497010 638
dbb410c3
AM
639 for (i = 0; i < shnum; i++)
640 {
641 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 642
44534af3 643 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 644 {
973ffd63 645 unsigned char *src;
dbb410c3
AM
646 Elf_Internal_Group *dest;
647
07d6d2b8
AM
648 /* Make sure the group section has a BFD section
649 attached to it. */
650 if (!bfd_section_from_shdr (abfd, i))
0a1b45a2 651 return false;
07d6d2b8 652
dbb410c3
AM
653 /* Add to list of sections. */
654 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
655 num_group += 1;
656
657 /* Read the raw contents. */
1f4361a7
AM
658 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
659 shdr->contents = NULL;
660 if (_bfd_mul_overflow (shdr->sh_size,
661 sizeof (*dest) / 4, &amt)
1f4361a7 662 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
663 || !(shdr->contents
664 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
665 {
666 _bfd_error_handler
695344c0 667 /* xgettext:c-format */
871b3ab2 668 (_("%pB: invalid size field in group section"
2dcf00ce
AM
669 " header: %#" PRIx64 ""),
670 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
671 bfd_set_error (bfd_error_bad_value);
672 -- num_group;
493a3386
NC
673 continue;
674 }
708d7d0d 675
dbb410c3
AM
676 /* Translate raw contents, a flag word followed by an
677 array of elf section indices all in target byte order,
678 to the flag word followed by an array of elf section
679 pointers. */
680 src = shdr->contents + shdr->sh_size;
681 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 682
dbb410c3
AM
683 while (1)
684 {
685 unsigned int idx;
686
687 src -= 4;
688 --dest;
689 idx = H_GET_32 (abfd, src);
690 if (src == shdr->contents)
691 {
327301a4 692 dest->shdr = NULL;
dbb410c3 693 dest->flags = idx;
b885599b
AM
694 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
695 shdr->bfd_section->flags
696 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
697 break;
698 }
4bba0fb1 699 if (idx < shnum)
bae363f1
L
700 {
701 dest->shdr = elf_elfsections (abfd)[idx];
702 /* PR binutils/23199: All sections in a
703 section group should be marked with
704 SHF_GROUP. But some tools generate
705 broken objects without SHF_GROUP. Fix
706 them up here. */
707 dest->shdr->sh_flags |= SHF_GROUP;
708 }
4bba0fb1
AM
709 if (idx >= shnum
710 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 711 {
4eca0228 712 _bfd_error_handler
4bba0fb1
AM
713 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
714 abfd, i);
715 dest->shdr = NULL;
dbb410c3 716 }
dbb410c3
AM
717 }
718 }
719 }
493a3386
NC
720
721 /* PR 17510: Corrupt binaries might contain invalid groups. */
722 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
723 {
724 elf_tdata (abfd)->num_group = num_group;
725
726 /* If all groups are invalid then fail. */
727 if (num_group == 0)
728 {
729 elf_tdata (abfd)->group_sect_ptr = NULL;
730 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 731 _bfd_error_handler
871b3ab2 732 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
733 bfd_set_error (bfd_error_bad_value);
734 }
735 }
dbb410c3
AM
736 }
737 }
738
739 if (num_group != (unsigned) -1)
740 {
564e11c9
JW
741 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
742 unsigned int j;
dbb410c3 743
564e11c9 744 for (j = 0; j < num_group; j++)
dbb410c3 745 {
564e11c9
JW
746 /* Begin search from previous found group. */
747 unsigned i = (j + search_offset) % num_group;
748
dbb410c3 749 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 750 Elf_Internal_Group *idx;
0c54f692 751 bfd_size_type n_elt;
ce497010
NC
752
753 if (shdr == NULL)
754 continue;
755
756 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
757 if (idx == NULL || shdr->sh_size < 4)
758 {
759 /* See PR 21957 for a reproducer. */
760 /* xgettext:c-format */
871b3ab2 761 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
762 abfd, shdr->bfd_section);
763 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
764 bfd_set_error (bfd_error_bad_value);
0a1b45a2 765 return false;
0c54f692 766 }
ce497010 767 n_elt = shdr->sh_size / 4;
dbb410c3
AM
768
769 /* Look through this group's sections to see if current
770 section is a member. */
771 while (--n_elt != 0)
772 if ((++idx)->shdr == hdr)
773 {
e0e8c97f 774 asection *s = NULL;
dbb410c3
AM
775
776 /* We are a member of this group. Go looking through
777 other members to see if any others are linked via
778 next_in_group. */
779 idx = (Elf_Internal_Group *) shdr->contents;
780 n_elt = shdr->sh_size / 4;
781 while (--n_elt != 0)
4bba0fb1
AM
782 if ((++idx)->shdr != NULL
783 && (s = idx->shdr->bfd_section) != NULL
945906ff 784 && elf_next_in_group (s) != NULL)
dbb410c3
AM
785 break;
786 if (n_elt != 0)
787 {
dbb410c3
AM
788 /* Snarf the group name from other member, and
789 insert current section in circular list. */
945906ff
AM
790 elf_group_name (newsect) = elf_group_name (s);
791 elf_next_in_group (newsect) = elf_next_in_group (s);
792 elf_next_in_group (s) = newsect;
dbb410c3
AM
793 }
794 else
795 {
dbb410c3
AM
796 const char *gname;
797
b885599b
AM
798 gname = group_signature (abfd, shdr);
799 if (gname == NULL)
0a1b45a2 800 return false;
945906ff 801 elf_group_name (newsect) = gname;
dbb410c3
AM
802
803 /* Start a circular list with one element. */
945906ff 804 elf_next_in_group (newsect) = newsect;
dbb410c3 805 }
b885599b 806
9dce4196
AM
807 /* If the group section has been created, point to the
808 new member. */
dbb410c3 809 if (shdr->bfd_section != NULL)
945906ff 810 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 811
564e11c9
JW
812 elf_tdata (abfd)->group_search_offset = i;
813 j = num_group - 1;
dbb410c3
AM
814 break;
815 }
816 }
817 }
818
945906ff 819 if (elf_group_name (newsect) == NULL)
dbb410c3 820 {
695344c0 821 /* xgettext:c-format */
871b3ab2 822 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 823 abfd, newsect);
efc1521e
NC
824 /* PR 29532: Return true here, even though the group info has not been
825 read. Separate debug info files can have empty group sections, but
826 we do not want this to prevent them from being loaded as otherwise
827 GDB will not be able to use them. */
828 return true;
dbb410c3 829 }
0a1b45a2 830 return true;
dbb410c3
AM
831}
832
0a1b45a2 833bool
dd863624 834_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
835{
836 unsigned int i;
837 unsigned int num_group = elf_tdata (abfd)->num_group;
0a1b45a2 838 bool result = true;
dd863624
L
839 asection *s;
840
841 /* Process SHF_LINK_ORDER. */
842 for (s = abfd->sections; s != NULL; s = s->next)
843 {
844 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
845 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
846 {
847 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
848 /* An sh_link value of 0 is now allowed. It indicates that linked
849 to section has already been discarded, but that the current
850 section has been retained for some other reason. This linking
851 section is still a candidate for later garbage collection
852 however. */
dd863624
L
853 if (elfsec == 0)
854 {
b71702f1 855 elf_linked_to_section (s) = NULL;
dd863624
L
856 }
857 else
858 {
91d6fa6a 859 asection *linksec = NULL;
25bbc984 860
4fbb74a6
AM
861 if (elfsec < elf_numsections (abfd))
862 {
863 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 864 linksec = this_hdr->bfd_section;
4fbb74a6 865 }
25bbc984
L
866
867 /* PR 1991, 2008:
868 Some strip/objcopy may leave an incorrect value in
869 sh_link. We don't want to proceed. */
91d6fa6a 870 if (linksec == NULL)
25bbc984 871 {
4eca0228 872 _bfd_error_handler
695344c0 873 /* xgettext:c-format */
871b3ab2 874 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 875 s->owner, elfsec, s);
0a1b45a2 876 result = false;
25bbc984
L
877 }
878
91d6fa6a 879 elf_linked_to_section (s) = linksec;
dd863624
L
880 }
881 }
53720c49
AM
882 else if (this_hdr->sh_type == SHT_GROUP
883 && elf_next_in_group (s) == NULL)
884 {
4eca0228 885 _bfd_error_handler
695344c0 886 /* xgettext:c-format */
871b3ab2 887 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49 888 abfd, elf_section_data (s)->this_idx);
0a1b45a2 889 result = false;
53720c49 890 }
dd863624 891 }
3d7f7666 892
dd863624 893 /* Process section groups. */
3d7f7666
L
894 if (num_group == (unsigned) -1)
895 return result;
896
897 for (i = 0; i < num_group; i++)
898 {
899 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
900 Elf_Internal_Group *idx;
901 unsigned int n_elt;
3d7f7666 902
11c6a7c6
AM
903 /* PR binutils/18758: Beware of corrupt binaries with invalid
904 group data. */
4b0e8a5f
NC
905 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
906 {
4eca0228 907 _bfd_error_handler
695344c0 908 /* xgettext:c-format */
871b3ab2 909 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f 910 abfd, i);
0a1b45a2 911 result = false;
4b0e8a5f
NC
912 continue;
913 }
914
915 idx = (Elf_Internal_Group *) shdr->contents;
916 n_elt = shdr->sh_size / 4;
1b786873 917
3d7f7666 918 while (--n_elt != 0)
24d3e51b
NC
919 {
920 ++ idx;
921
922 if (idx->shdr == NULL)
923 continue;
924 else if (idx->shdr->bfd_section)
925 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
926 else if (idx->shdr->sh_type != SHT_RELA
927 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
928 {
929 /* There are some unknown sections in the group. */
930 _bfd_error_handler
931 /* xgettext:c-format */
871b3ab2 932 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
933 abfd,
934 idx->shdr->sh_type,
935 bfd_elf_string_from_elf_section (abfd,
936 (elf_elfheader (abfd)
937 ->e_shstrndx),
938 idx->shdr->sh_name),
939 shdr->bfd_section);
0a1b45a2 940 result = false;
24d3e51b
NC
941 }
942 }
3d7f7666 943 }
24d3e51b 944
3d7f7666
L
945 return result;
946}
947
0a1b45a2 948bool
72adc230
AM
949bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
950{
951 return elf_next_in_group (sec) != NULL;
952}
953
cb7f4b29
AM
954const char *
955bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
956{
957 if (elf_sec_group (sec) != NULL)
958 return elf_group_name (sec);
959 return NULL;
960}
961
cc5277b1
ML
962/* This a copy of lto_section defined in GCC (lto-streamer.h). */
963
964struct lto_section
965{
966 int16_t major_version;
967 int16_t minor_version;
968 unsigned char slim_object;
969
970 /* Flags is a private field that is not defined publicly. */
971 uint16_t flags;
972};
973
252b5132
RH
974/* Make a BFD section from an ELF section. We store a pointer to the
975 BFD section in the bfd_section field of the header. */
976
0a1b45a2 977bool
217aa764
AM
978_bfd_elf_make_section_from_shdr (bfd *abfd,
979 Elf_Internal_Shdr *hdr,
6dc132d9
L
980 const char *name,
981 int shindex)
252b5132
RH
982{
983 asection *newsect;
984 flagword flags;
9c5bfbb7 985 const struct elf_backend_data *bed;
502794d4 986 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
987
988 if (hdr->bfd_section != NULL)
0a1b45a2 989 return true;
252b5132
RH
990
991 newsect = bfd_make_section_anyway (abfd, name);
992 if (newsect == NULL)
0a1b45a2 993 return false;
252b5132 994
1829f4b2
AM
995 hdr->bfd_section = newsect;
996 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 997 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 998
2f89ff8d
L
999 /* Always use the real type/flags. */
1000 elf_section_type (newsect) = hdr->sh_type;
1001 elf_section_flags (newsect) = hdr->sh_flags;
1002
252b5132
RH
1003 newsect->filepos = hdr->sh_offset;
1004
252b5132
RH
1005 flags = SEC_NO_FLAGS;
1006 if (hdr->sh_type != SHT_NOBITS)
1007 flags |= SEC_HAS_CONTENTS;
dbb410c3 1008 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1009 flags |= SEC_GROUP;
252b5132
RH
1010 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1011 {
1012 flags |= SEC_ALLOC;
1013 if (hdr->sh_type != SHT_NOBITS)
1014 flags |= SEC_LOAD;
1015 }
1016 if ((hdr->sh_flags & SHF_WRITE) == 0)
1017 flags |= SEC_READONLY;
1018 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1019 flags |= SEC_CODE;
1020 else if ((flags & SEC_LOAD) != 0)
1021 flags |= SEC_DATA;
f5fa8ca2
JJ
1022 if ((hdr->sh_flags & SHF_MERGE) != 0)
1023 {
1024 flags |= SEC_MERGE;
1025 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1026 }
84865015
NC
1027 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1028 flags |= SEC_STRINGS;
dbb410c3
AM
1029 if (hdr->sh_flags & SHF_GROUP)
1030 if (!setup_group (abfd, hdr, newsect))
0a1b45a2 1031 return false;
13ae64f3
JJ
1032 if ((hdr->sh_flags & SHF_TLS) != 0)
1033 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1034 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1035 flags |= SEC_EXCLUDE;
252b5132 1036
df3a023b
AM
1037 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1038 {
1039 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1040 but binutils as of 2019-07-23 did not set the EI_OSABI header
1041 byte. */
df3a023b
AM
1042 case ELFOSABI_GNU:
1043 case ELFOSABI_FREEBSD:
99fabbc9
JL
1044 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1045 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1046 /* Fall through */
1047 case ELFOSABI_NONE:
df3a023b
AM
1048 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1049 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1050 break;
1051 }
1052
3d2b39cf 1053 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1054 {
3d2b39cf
L
1055 /* The debugging sections appear to be recognized only by name,
1056 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1057 if (name [0] == '.')
1058 {
3f3328b8
ML
1059 if (startswith (name, ".debug")
1060 || startswith (name, ".gnu.debuglto_.debug_")
1061 || startswith (name, ".gnu.linkonce.wi.")
1062 || startswith (name, ".zdebug"))
bb294208 1063 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
3f3328b8
ML
1064 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1065 || startswith (name, ".note.gnu"))
502794d4
CE
1066 {
1067 flags |= SEC_ELF_OCTETS;
1068 opb = 1;
1069 }
3f3328b8
ML
1070 else if (startswith (name, ".line")
1071 || startswith (name, ".stab")
bb294208 1072 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1073 flags |= SEC_DEBUGGING;
1074 }
1075 }
252b5132 1076
502794d4
CE
1077 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1078 || !bfd_set_section_size (newsect, hdr->sh_size)
1f9b1a84
AM
1079 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1080 & -hdr->sh_addralign)))
0a1b45a2 1081 return false;
502794d4 1082
252b5132
RH
1083 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1084 only link a single copy of the section. This is used to support
1085 g++. g++ will emit each template expansion in its own section.
1086 The symbols will be defined as weak, so that multiple definitions
1087 are permitted. The GNU linker extension is to actually discard
1088 all but one of the sections. */
08dedd66 1089 if (startswith (name, ".gnu.linkonce")
b885599b 1090 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1091 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1092
8c803a2d 1093 if (!bfd_set_section_flags (newsect, flags))
0a1b45a2 1094 return false;
8c803a2d 1095
fa152c49
JW
1096 bed = get_elf_backend_data (abfd);
1097 if (bed->elf_backend_section_flags)
8c803a2d 1098 if (!bed->elf_backend_section_flags (hdr))
0a1b45a2 1099 return false;
fa152c49 1100
718175fa
JK
1101 /* We do not parse the PT_NOTE segments as we are interested even in the
1102 separate debug info files which may have the segments offsets corrupted.
1103 PT_NOTEs from the core files are currently not parsed using BFD. */
6f00d50e 1104 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
718175fa 1105 {
baea7ef1 1106 bfd_byte *contents;
718175fa 1107
baea7ef1 1108 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
0a1b45a2 1109 return false;
718175fa 1110
276da9b3
L
1111 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1112 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1113 free (contents);
1114 }
1115
8c803a2d 1116 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1117 {
1118 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1119 unsigned int i, nload;
1120
1121 /* Some ELF linkers produce binaries with all the program header
1122 p_paddr fields zero. If we have such a binary with more than
1123 one PT_LOAD header, then leave the section lma equal to vma
1124 so that we don't create sections with overlapping lma. */
1125 phdr = elf_tdata (abfd)->phdr;
1126 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1127 if (phdr->p_paddr != 0)
1128 break;
1129 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1130 ++nload;
1131 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
0a1b45a2 1132 return true;
252b5132 1133
252b5132
RH
1134 phdr = elf_tdata (abfd)->phdr;
1135 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1136 {
86b2281f
AM
1137 if (((phdr->p_type == PT_LOAD
1138 && (hdr->sh_flags & SHF_TLS) == 0)
1139 || phdr->p_type == PT_TLS)
9a83a553 1140 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1141 {
8c803a2d 1142 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1143 newsect->lma = (phdr->p_paddr
502794d4 1144 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1145 else
1146 /* We used to use the same adjustment for SEC_LOAD
1147 sections, but that doesn't work if the segment
1148 is packed with code from multiple VMAs.
1149 Instead we calculate the section LMA based on
1150 the segment LMA. It is assumed that the
1151 segment will contain sections with contiguous
1152 LMAs, even if the VMAs are not. */
1153 newsect->lma = (phdr->p_paddr
502794d4 1154 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1155
1156 /* With contiguous segments, we can't tell from file
1157 offsets whether a section with zero size should
1158 be placed at the end of one segment or the
1159 beginning of the next. Decide based on vaddr. */
1160 if (hdr->sh_addr >= phdr->p_vaddr
1161 && (hdr->sh_addr + hdr->sh_size
1162 <= phdr->p_vaddr + phdr->p_memsz))
1163 break;
252b5132
RH
1164 }
1165 }
1166 }
1167
7afbac7d
ML
1168 /* Compress/decompress DWARF debug sections with names: .debug_*,
1169 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
4bea06d7
AM
1170 if ((newsect->flags & SEC_DEBUGGING) != 0
1171 && (newsect->flags & SEC_HAS_CONTENTS) != 0
7afbac7d 1172 && (newsect->flags & SEC_ELF_OCTETS) != 0)
4a114e3e
L
1173 {
1174 enum { nothing, compress, decompress } action = nothing;
151411f8 1175 int compression_header_size;
dab394de 1176 bfd_size_type uncompressed_size;
4207142d 1177 unsigned int uncompressed_align_power;
89dbeac7 1178 enum compression_type ch_type = ch_none;
0a1b45a2 1179 bool compressed
758dd750
AM
1180 = bfd_is_section_compressed_info (abfd, newsect,
1181 &compression_header_size,
1182 &uncompressed_size,
1183 &uncompressed_align_power,
1184 &ch_type);
1185
1186 /* Should we decompress? */
1187 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1188 action = decompress;
1189
1190 /* Should we compress? Or convert to a different compression? */
1191 else if ((abfd->flags & BFD_COMPRESS) != 0
1192 && newsect->size != 0
1193 && compression_header_size >= 0
1194 && uncompressed_size > 0)
4a114e3e 1195 {
758dd750 1196 if (!compressed)
4a114e3e 1197 action = compress;
151411f8 1198 else
758dd750 1199 {
89dbeac7 1200 enum compression_type new_ch_type = ch_none;
758dd750
AM
1201 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1202 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
89dbeac7 1203 ? ch_compress_zstd : ch_compress_zlib);
758dd750
AM
1204 if (new_ch_type != ch_type)
1205 action = compress;
1206 }
4a114e3e
L
1207 }
1208
151411f8 1209 if (action == compress)
4a114e3e 1210 {
4a114e3e
L
1211 if (!bfd_init_section_compress_status (abfd, newsect))
1212 {
4eca0228 1213 _bfd_error_handler
695344c0 1214 /* xgettext:c-format */
758dd750 1215 (_("%pB: unable to compress section %s"), abfd, name);
0a1b45a2 1216 return false;
4a114e3e 1217 }
151411f8 1218 }
758dd750 1219 else if (action == decompress)
151411f8 1220 {
4a114e3e
L
1221 if (!bfd_init_section_decompress_status (abfd, newsect))
1222 {
4eca0228 1223 _bfd_error_handler
695344c0 1224 /* xgettext:c-format */
758dd750 1225 (_("%pB: unable to decompress section %s"), abfd, name);
0a1b45a2 1226 return false;
4a114e3e 1227 }
2cac01e3
FS
1228#ifndef HAVE_ZSTD
1229 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1230 {
1231 _bfd_error_handler
1232 /* xgettext:c-format */
1233 (_ ("%pB: section %s is compressed with zstd, but BFD "
1234 "is not built with zstd support"),
1235 abfd, name);
1236 newsect->compress_status = COMPRESS_SECTION_NONE;
1237 return false;
1238 }
1239#endif
7b5f66a1
AM
1240 if (abfd->is_linker_input
1241 && name[1] == 'z')
4e011fb5 1242 {
7b5f66a1
AM
1243 /* Rename section from .zdebug_* to .debug_* so that ld
1244 scripts will see this section as a debug section. */
1245 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1246 if (new_name == NULL)
1247 return false;
1248 bfd_rename_section (newsect, new_name);
151411f8 1249 }
4a114e3e
L
1250 }
1251 }
1252
cc5277b1
ML
1253 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1254 section. */
3f3328b8 1255 if (startswith (name, ".gnu.lto_.lto."))
cc5277b1
ML
1256 {
1257 struct lto_section lsection;
1258 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1259 sizeof (struct lto_section)))
1260 abfd->lto_slim_object = lsection.slim_object;
1261 }
1262
0a1b45a2 1263 return true;
252b5132
RH
1264}
1265
84865015
NC
1266const char *const bfd_elf_section_type_names[] =
1267{
252b5132
RH
1268 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1269 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1270 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1271};
1272
1049f94e 1273/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1274 output, and the reloc is against an external symbol, and nothing
1275 has given us any additional addend, the resulting reloc will also
1276 be against the same symbol. In such a case, we don't want to
1277 change anything about the way the reloc is handled, since it will
1278 all be done at final link time. Rather than put special case code
1279 into bfd_perform_relocation, all the reloc types use this howto
2dfa8341 1280 function, or should call this function for relocatable output. */
252b5132 1281
252b5132 1282bfd_reloc_status_type
217aa764
AM
1283bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1284 arelent *reloc_entry,
1285 asymbol *symbol,
1286 void *data ATTRIBUTE_UNUSED,
1287 asection *input_section,
1288 bfd *output_bfd,
1289 char **error_message ATTRIBUTE_UNUSED)
1290{
1291 if (output_bfd != NULL
252b5132
RH
1292 && (symbol->flags & BSF_SECTION_SYM) == 0
1293 && (! reloc_entry->howto->partial_inplace
1294 || reloc_entry->addend == 0))
1295 {
1296 reloc_entry->address += input_section->output_offset;
1297 return bfd_reloc_ok;
1298 }
1299
2dfa8341
AM
1300 /* In some cases the relocation should be treated as output section
1301 relative, as when linking ELF DWARF into PE COFF. Many ELF
1302 targets lack section relative relocations and instead use
1303 ordinary absolute relocations for references between DWARF
1304 sections. That is arguably a bug in those targets but it happens
1305 to work for the usual case of linking to non-loaded ELF debug
1306 sections with VMAs forced to zero. PE COFF on the other hand
1307 doesn't allow a section VMA of zero. */
1308 if (output_bfd == NULL
1309 && !reloc_entry->howto->pc_relative
1310 && (symbol->section->flags & SEC_DEBUGGING) != 0
1311 && (input_section->flags & SEC_DEBUGGING) != 0)
1312 reloc_entry->addend -= symbol->section->output_section->vma;
1313
252b5132
RH
1314 return bfd_reloc_continue;
1315}
1316\f
84865015
NC
1317/* Returns TRUE if section A matches section B.
1318 Names, addresses and links may be different, but everything else
1319 should be the same. */
1320
0a1b45a2 1321static bool
5522f910
NC
1322section_match (const Elf_Internal_Shdr * a,
1323 const Elf_Internal_Shdr * b)
84865015 1324{
ac85e67c
AM
1325 if (a->sh_type != b->sh_type
1326 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1327 || a->sh_addralign != b->sh_addralign
1328 || a->sh_entsize != b->sh_entsize)
0a1b45a2 1329 return false;
ac85e67c
AM
1330 if (a->sh_type == SHT_SYMTAB
1331 || a->sh_type == SHT_STRTAB)
0a1b45a2 1332 return true;
ac85e67c 1333 return a->sh_size == b->sh_size;
84865015
NC
1334}
1335
1336/* Find a section in OBFD that has the same characteristics
1337 as IHEADER. Return the index of this section or SHN_UNDEF if
1338 none can be found. Check's section HINT first, as this is likely
1339 to be the correct section. */
1340
1341static unsigned int
5cc4ca83
ST
1342find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1343 const unsigned int hint)
84865015
NC
1344{
1345 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1346 unsigned int i;
1347
a55c9876
NC
1348 BFD_ASSERT (iheader != NULL);
1349
1350 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1351 if (hint < elf_numsections (obfd)
1352 && oheaders[hint] != NULL
a55c9876 1353 && section_match (oheaders[hint], iheader))
84865015
NC
1354 return hint;
1355
1356 for (i = 1; i < elf_numsections (obfd); i++)
1357 {
1358 Elf_Internal_Shdr * oheader = oheaders[i];
1359
a55c9876
NC
1360 if (oheader == NULL)
1361 continue;
84865015
NC
1362 if (section_match (oheader, iheader))
1363 /* FIXME: Do we care if there is a potential for
1364 multiple matches ? */
1365 return i;
1366 }
1367
1368 return SHN_UNDEF;
1369}
1370
5522f910
NC
1371/* PR 19938: Attempt to set the ELF section header fields of an OS or
1372 Processor specific section, based upon a matching input section.
1373 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1374
0a1b45a2 1375static bool
5522f910
NC
1376copy_special_section_fields (const bfd *ibfd,
1377 bfd *obfd,
1378 const Elf_Internal_Shdr *iheader,
1379 Elf_Internal_Shdr *oheader,
1380 const unsigned int secnum)
1381{
1382 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11c6a7c6
AM
1383 const Elf_Internal_Shdr **iheaders
1384 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
0a1b45a2 1385 bool changed = false;
5522f910
NC
1386 unsigned int sh_link;
1387
1388 if (oheader->sh_type == SHT_NOBITS)
1389 {
1390 /* This is a feature for objcopy --only-keep-debug:
1391 When a section's type is changed to NOBITS, we preserve
1392 the sh_link and sh_info fields so that they can be
1393 matched up with the original.
1394
1395 Note: Strictly speaking these assignments are wrong.
1396 The sh_link and sh_info fields should point to the
1397 relevent sections in the output BFD, which may not be in
1398 the same location as they were in the input BFD. But
1399 the whole point of this action is to preserve the
1400 original values of the sh_link and sh_info fields, so
1401 that they can be matched up with the section headers in
1402 the original file. So strictly speaking we may be
1403 creating an invalid ELF file, but it is only for a file
1404 that just contains debug info and only for sections
1405 without any contents. */
1406 if (oheader->sh_link == 0)
1407 oheader->sh_link = iheader->sh_link;
1408 if (oheader->sh_info == 0)
1409 oheader->sh_info = iheader->sh_info;
0a1b45a2 1410 return true;
5522f910
NC
1411 }
1412
1413 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1414 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1415 iheader, oheader))
0a1b45a2 1416 return true;
5522f910
NC
1417
1418 /* We have an iheader which might match oheader, and which has non-zero
1419 sh_info and/or sh_link fields. Attempt to follow those links and find
1420 the section in the output bfd which corresponds to the linked section
1421 in the input bfd. */
1422 if (iheader->sh_link != SHN_UNDEF)
1423 {
4f3ca05b
NC
1424 /* See PR 20931 for a reproducer. */
1425 if (iheader->sh_link >= elf_numsections (ibfd))
1426 {
76cfced5 1427 _bfd_error_handler
4f3ca05b 1428 /* xgettext:c-format */
9793eb77 1429 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b 1430 ibfd, iheader->sh_link, secnum);
0a1b45a2 1431 return false;
4f3ca05b
NC
1432 }
1433
5522f910
NC
1434 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1435 if (sh_link != SHN_UNDEF)
1436 {
1437 oheader->sh_link = sh_link;
0a1b45a2 1438 changed = true;
5522f910
NC
1439 }
1440 else
1441 /* FIXME: Should we install iheader->sh_link
1442 if we could not find a match ? */
76cfced5 1443 _bfd_error_handler
695344c0 1444 /* xgettext:c-format */
9793eb77 1445 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1446 }
1447
1448 if (iheader->sh_info)
1449 {
1450 /* The sh_info field can hold arbitrary information, but if the
1451 SHF_LINK_INFO flag is set then it should be interpreted as a
1452 section index. */
1453 if (iheader->sh_flags & SHF_INFO_LINK)
1454 {
1455 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1456 iheader->sh_info);
1457 if (sh_link != SHN_UNDEF)
1458 oheader->sh_flags |= SHF_INFO_LINK;
1459 }
1460 else
1461 /* No idea what it means - just copy it. */
1462 sh_link = iheader->sh_info;
1463
1464 if (sh_link != SHN_UNDEF)
1465 {
1466 oheader->sh_info = sh_link;
0a1b45a2 1467 changed = true;
5522f910
NC
1468 }
1469 else
76cfced5 1470 _bfd_error_handler
695344c0 1471 /* xgettext:c-format */
9793eb77 1472 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1473 }
1474
1475 return changed;
1476}
07d6d2b8 1477
0ac4564e
L
1478/* Copy the program header and other data from one object module to
1479 another. */
252b5132 1480
0a1b45a2 1481bool
217aa764 1482_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1483{
11c6a7c6
AM
1484 const Elf_Internal_Shdr **iheaders
1485 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
5522f910
NC
1486 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1487 const struct elf_backend_data *bed;
84865015
NC
1488 unsigned int i;
1489
2d502050 1490 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1491 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 1492 return true;
2d502050 1493
57b828ef
L
1494 if (!elf_flags_init (obfd))
1495 {
1496 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
0a1b45a2 1497 elf_flags_init (obfd) = true;
57b828ef 1498 }
2d502050 1499
0ac4564e 1500 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1501
1502 /* Also copy the EI_OSABI field. */
1503 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1504 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1505
5522f910
NC
1506 /* If set, copy the EI_ABIVERSION field. */
1507 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1508 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1509 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1510
104d59d1
JM
1511 /* Copy object attributes. */
1512 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1513
84865015 1514 if (iheaders == NULL || oheaders == NULL)
0a1b45a2 1515 return true;
63b9bbb7 1516
5522f910
NC
1517 bed = get_elf_backend_data (obfd);
1518
1519 /* Possibly copy other fields in the section header. */
84865015 1520 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1521 {
84865015
NC
1522 unsigned int j;
1523 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1524
5522f910
NC
1525 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1526 because of a special case need for generating separate debug info
1527 files. See below for more details. */
84865015
NC
1528 if (oheader == NULL
1529 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1530 && oheader->sh_type < SHT_LOOS))
1531 continue;
1532
1533 /* Ignore empty sections, and sections whose
1534 fields have already been initialised. */
1535 if (oheader->sh_size == 0
84865015
NC
1536 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1537 continue;
63b9bbb7 1538
84865015 1539 /* Scan for the matching section in the input bfd.
11c6a7c6
AM
1540 First we try for a direct mapping between the input and
1541 output sections. */
5522f910
NC
1542 for (j = 1; j < elf_numsections (ibfd); j++)
1543 {
1544 const Elf_Internal_Shdr * iheader = iheaders[j];
1545
1546 if (iheader == NULL)
1547 continue;
1548
1549 if (oheader->bfd_section != NULL
1550 && iheader->bfd_section != NULL
1551 && iheader->bfd_section->output_section != NULL
1552 && iheader->bfd_section->output_section == oheader->bfd_section)
1553 {
11c6a7c6
AM
1554 /* We have found a connection from the input section to
1555 the output section. Attempt to copy the header fields.
1556 If this fails then do not try any further sections -
1557 there should only be a one-to-one mapping between
1558 input and output. */
1559 if (!copy_special_section_fields (ibfd, obfd,
1560 iheader, oheader, i))
5522f910
NC
1561 j = elf_numsections (ibfd);
1562 break;
1563 }
1564 }
1565
1566 if (j < elf_numsections (ibfd))
1567 continue;
1568
1569 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1570 Unfortunately we cannot compare names as the output string table
1571 is empty, so instead we check size, address and type. */
1572 for (j = 1; j < elf_numsections (ibfd); j++)
1573 {
5522f910 1574 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1575
5522f910
NC
1576 if (iheader == NULL)
1577 continue;
1578
1579 /* Try matching fields in the input section's header.
1580 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1581 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1582 input type. */
1583 if ((oheader->sh_type == SHT_NOBITS
1584 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1585 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1586 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1587 && iheader->sh_addralign == oheader->sh_addralign
1588 && iheader->sh_entsize == oheader->sh_entsize
1589 && iheader->sh_size == oheader->sh_size
1590 && iheader->sh_addr == oheader->sh_addr
1591 && (iheader->sh_info != oheader->sh_info
1592 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1593 {
5522f910
NC
1594 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1595 break;
63b9bbb7
NC
1596 }
1597 }
5522f910
NC
1598
1599 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1600 {
1601 /* Final attempt. Call the backend copy function
1602 with a NULL input section. */
a859124d
AM
1603 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1604 NULL, oheader);
5522f910 1605 }
63b9bbb7
NC
1606 }
1607
0a1b45a2 1608 return true;
2d502050
L
1609}
1610
cedc298e
L
1611static const char *
1612get_segment_type (unsigned int p_type)
1613{
1614 const char *pt;
1615 switch (p_type)
1616 {
1617 case PT_NULL: pt = "NULL"; break;
1618 case PT_LOAD: pt = "LOAD"; break;
1619 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1620 case PT_INTERP: pt = "INTERP"; break;
1621 case PT_NOTE: pt = "NOTE"; break;
1622 case PT_SHLIB: pt = "SHLIB"; break;
1623 case PT_PHDR: pt = "PHDR"; break;
1624 case PT_TLS: pt = "TLS"; break;
1625 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1626 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e 1627 case PT_GNU_RELRO: pt = "RELRO"; break;
cf0e0a0b 1628 case PT_GNU_SFRAME: pt = "SFRAME"; break;
cedc298e
L
1629 default: pt = NULL; break;
1630 }
1631 return pt;
1632}
1633
f0b79d91
L
1634/* Print out the program headers. */
1635
0a1b45a2 1636bool
217aa764 1637_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1638{
a50b1753 1639 FILE *f = (FILE *) farg;
252b5132
RH
1640 Elf_Internal_Phdr *p;
1641 asection *s;
1642 bfd_byte *dynbuf = NULL;
1643
1644 p = elf_tdata (abfd)->phdr;
1645 if (p != NULL)
1646 {
1647 unsigned int i, c;
1648
1649 fprintf (f, _("\nProgram Header:\n"));
1650 c = elf_elfheader (abfd)->e_phnum;
1651 for (i = 0; i < c; i++, p++)
1652 {
cedc298e 1653 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1654 char buf[20];
1655
cedc298e 1656 if (pt == NULL)
252b5132 1657 {
cedc298e
L
1658 sprintf (buf, "0x%lx", p->p_type);
1659 pt = buf;
252b5132 1660 }
dc810e39 1661 fprintf (f, "%8s off 0x", pt);
60b89a18 1662 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1663 fprintf (f, " vaddr 0x");
60b89a18 1664 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1665 fprintf (f, " paddr 0x");
60b89a18 1666 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1667 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1668 fprintf (f, " filesz 0x");
60b89a18 1669 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1670 fprintf (f, " memsz 0x");
60b89a18 1671 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1672 fprintf (f, " flags %c%c%c",
1673 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1674 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1675 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1676 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1677 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1678 fprintf (f, "\n");
1679 }
1680 }
1681
1682 s = bfd_get_section_by_name (abfd, ".dynamic");
81ff113f 1683 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
252b5132 1684 {
cb33740c 1685 unsigned int elfsec;
dc810e39 1686 unsigned long shlink;
252b5132
RH
1687 bfd_byte *extdyn, *extdynend;
1688 size_t extdynsize;
217aa764 1689 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1690
1691 fprintf (f, _("\nDynamic Section:\n"));
1692
eea6121a 1693 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1694 goto error_return;
1695
1696 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1697 if (elfsec == SHN_BAD)
252b5132 1698 goto error_return;
dc810e39 1699 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1700
1701 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1702 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1703
37c59664
AM
1704 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1705 (size_t) (extdynend - extdyn) >= extdynsize;
1706 extdyn += extdynsize)
252b5132
RH
1707 {
1708 Elf_Internal_Dyn dyn;
ad9563d6 1709 const char *name = "";
252b5132 1710 char ab[20];
0a1b45a2 1711 bool stringp;
ad9563d6 1712 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1713
217aa764 1714 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1715
1716 if (dyn.d_tag == DT_NULL)
1717 break;
1718
0a1b45a2 1719 stringp = false;
252b5132
RH
1720 switch (dyn.d_tag)
1721 {
1722 default:
ad9563d6
CM
1723 if (bed->elf_backend_get_target_dtag)
1724 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1725
1726 if (!strcmp (name, ""))
1727 {
b8281767 1728 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
ad9563d6
CM
1729 name = ab;
1730 }
252b5132
RH
1731 break;
1732
0a1b45a2 1733 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
252b5132
RH
1734 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1735 case DT_PLTGOT: name = "PLTGOT"; break;
1736 case DT_HASH: name = "HASH"; break;
1737 case DT_STRTAB: name = "STRTAB"; break;
1738 case DT_SYMTAB: name = "SYMTAB"; break;
1739 case DT_RELA: name = "RELA"; break;
1740 case DT_RELASZ: name = "RELASZ"; break;
1741 case DT_RELAENT: name = "RELAENT"; break;
1742 case DT_STRSZ: name = "STRSZ"; break;
1743 case DT_SYMENT: name = "SYMENT"; break;
1744 case DT_INIT: name = "INIT"; break;
1745 case DT_FINI: name = "FINI"; break;
0a1b45a2
AM
1746 case DT_SONAME: name = "SONAME"; stringp = true; break;
1747 case DT_RPATH: name = "RPATH"; stringp = true; break;
252b5132
RH
1748 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1749 case DT_REL: name = "REL"; break;
1750 case DT_RELSZ: name = "RELSZ"; break;
1751 case DT_RELENT: name = "RELENT"; break;
dd207c13
FS
1752 case DT_RELR: name = "RELR"; break;
1753 case DT_RELRSZ: name = "RELRSZ"; break;
1754 case DT_RELRENT: name = "RELRENT"; break;
252b5132
RH
1755 case DT_PLTREL: name = "PLTREL"; break;
1756 case DT_DEBUG: name = "DEBUG"; break;
1757 case DT_TEXTREL: name = "TEXTREL"; break;
1758 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1759 case DT_BIND_NOW: name = "BIND_NOW"; break;
1760 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1761 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1762 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1763 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
0a1b45a2 1764 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
94558834
L
1765 case DT_FLAGS: name = "FLAGS"; break;
1766 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1767 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1768 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1769 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1770 case DT_MOVEENT: name = "MOVEENT"; break;
1771 case DT_MOVESZ: name = "MOVESZ"; break;
1772 case DT_FEATURE: name = "FEATURE"; break;
1773 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1774 case DT_SYMINSZ: name = "SYMINSZ"; break;
1775 case DT_SYMINENT: name = "SYMINENT"; break;
0a1b45a2
AM
1776 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1777 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1778 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1779 case DT_PLTPAD: name = "PLTPAD"; break;
1780 case DT_MOVETAB: name = "MOVETAB"; break;
1781 case DT_SYMINFO: name = "SYMINFO"; break;
1782 case DT_RELACOUNT: name = "RELACOUNT"; break;
1783 case DT_RELCOUNT: name = "RELCOUNT"; break;
1784 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1785 case DT_VERSYM: name = "VERSYM"; break;
1786 case DT_VERDEF: name = "VERDEF"; break;
1787 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1788 case DT_VERNEED: name = "VERNEED"; break;
1789 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
0a1b45a2 1790 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
94558834 1791 case DT_USED: name = "USED"; break;
0a1b45a2 1792 case DT_FILTER: name = "FILTER"; stringp = true; break;
fdc90cb4 1793 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1794 }
1795
ad9563d6 1796 fprintf (f, " %-20s ", name);
252b5132 1797 if (! stringp)
a1f3c56e
AN
1798 {
1799 fprintf (f, "0x");
1800 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1801 }
252b5132
RH
1802 else
1803 {
1804 const char *string;
dc810e39 1805 unsigned int tagv = dyn.d_un.d_val;
252b5132 1806
dc810e39 1807 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1808 if (string == NULL)
1809 goto error_return;
1810 fprintf (f, "%s", string);
1811 }
1812 fprintf (f, "\n");
1813 }
1814
1815 free (dynbuf);
1816 dynbuf = NULL;
1817 }
1818
1819 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1820 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1821 {
0a1b45a2
AM
1822 if (! _bfd_elf_slurp_version_tables (abfd, false))
1823 return false;
252b5132
RH
1824 }
1825
1826 if (elf_dynverdef (abfd) != 0)
1827 {
1828 Elf_Internal_Verdef *t;
1829
1830 fprintf (f, _("\nVersion definitions:\n"));
1831 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1832 {
1833 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1834 t->vd_flags, t->vd_hash,
1835 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1836 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1837 {
1838 Elf_Internal_Verdaux *a;
1839
1840 fprintf (f, "\t");
1841 for (a = t->vd_auxptr->vda_nextptr;
1842 a != NULL;
1843 a = a->vda_nextptr)
d0fb9a8d
JJ
1844 fprintf (f, "%s ",
1845 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1846 fprintf (f, "\n");
1847 }
1848 }
1849 }
1850
1851 if (elf_dynverref (abfd) != 0)
1852 {
1853 Elf_Internal_Verneed *t;
1854
1855 fprintf (f, _("\nVersion References:\n"));
1856 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1857 {
1858 Elf_Internal_Vernaux *a;
1859
d0fb9a8d
JJ
1860 fprintf (f, _(" required from %s:\n"),
1861 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1862 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1864 a->vna_flags, a->vna_other,
1865 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1866 }
1867 }
1868
0a1b45a2 1869 return true;
252b5132
RH
1870
1871 error_return:
c9594989 1872 free (dynbuf);
0a1b45a2 1873 return false;
252b5132
RH
1874}
1875
7e6e972f
L
1876/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1877 and return symbol version for symbol version itself. */
bb4d2ac2
L
1878
1879const char *
1081065c 1880_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
0a1b45a2
AM
1881 bool base_p,
1882 bool *hidden)
bb4d2ac2
L
1883{
1884 const char *version_string = NULL;
1885 if (elf_dynversym (abfd) != 0
1886 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1887 {
1888 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1889
1890 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1891 vernum &= VERSYM_VERSION;
1892
1893 if (vernum == 0)
1894 version_string = "";
1f6f5dba
L
1895 else if (vernum == 1
1896 && (vernum > elf_tdata (abfd)->cverdefs
1897 || (elf_tdata (abfd)->verdef[0].vd_flags
1898 == VER_FLG_BASE)))
7e6e972f 1899 version_string = base_p ? "Base" : "";
bb4d2ac2 1900 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1901 {
1902 const char *nodename
1903 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1904 version_string = "";
1905 if (base_p
1906 || nodename == NULL
1907 || symbol->name == NULL
1908 || strcmp (symbol->name, nodename) != 0)
1909 version_string = nodename;
7e6e972f 1910 }
bb4d2ac2
L
1911 else
1912 {
1913 Elf_Internal_Verneed *t;
1914
7a815dd5 1915 version_string = _("<corrupt>");
bb4d2ac2
L
1916 for (t = elf_tdata (abfd)->verref;
1917 t != NULL;
1918 t = t->vn_nextref)
1919 {
1920 Elf_Internal_Vernaux *a;
1921
1922 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1923 {
1924 if (a->vna_other == vernum)
1925 {
f897689c 1926 *hidden = true;
bb4d2ac2
L
1927 version_string = a->vna_nodename;
1928 break;
1929 }
1930 }
1931 }
1932 }
1933 }
1934 return version_string;
1935}
1936
252b5132
RH
1937/* Display ELF-specific fields of a symbol. */
1938
1939void
217aa764
AM
1940bfd_elf_print_symbol (bfd *abfd,
1941 void *filep,
1942 asymbol *symbol,
1943 bfd_print_symbol_type how)
252b5132 1944{
a50b1753 1945 FILE *file = (FILE *) filep;
252b5132
RH
1946 switch (how)
1947 {
1948 case bfd_print_symbol_name:
1949 fprintf (file, "%s", symbol->name);
1950 break;
1951 case bfd_print_symbol_more:
1952 fprintf (file, "elf ");
60b89a18 1953 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1954 fprintf (file, " %x", symbol->flags);
252b5132
RH
1955 break;
1956 case bfd_print_symbol_all:
1957 {
4e8a9624
AM
1958 const char *section_name;
1959 const char *name = NULL;
9c5bfbb7 1960 const struct elf_backend_data *bed;
7a13edea 1961 unsigned char st_other;
dbb410c3 1962 bfd_vma val;
bb4d2ac2 1963 const char *version_string;
0a1b45a2 1964 bool hidden;
c044fabd 1965
252b5132 1966 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1967
1968 bed = get_elf_backend_data (abfd);
1969 if (bed->elf_backend_print_symbol_all)
c044fabd 1970 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1971
1972 if (name == NULL)
1973 {
7ee38065 1974 name = symbol->name;
217aa764 1975 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1976 }
1977
252b5132
RH
1978 fprintf (file, " %s\t", section_name);
1979 /* Print the "other" value for a symbol. For common symbols,
1980 we've already printed the size; now print the alignment.
1981 For other symbols, we have no specified alignment, and
1982 we've printed the address; now print the size. */
dcf6c779 1983 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1984 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1985 else
1986 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1987 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1988
1989 /* If we have version information, print it. */
60bb06bc
L
1990 version_string = _bfd_elf_get_symbol_version_string (abfd,
1991 symbol,
0a1b45a2 1992 true,
60bb06bc 1993 &hidden);
bb4d2ac2 1994 if (version_string)
252b5132 1995 {
bb4d2ac2 1996 if (!hidden)
252b5132
RH
1997 fprintf (file, " %-11s", version_string);
1998 else
1999 {
2000 int i;
2001
2002 fprintf (file, " (%s)", version_string);
2003 for (i = 10 - strlen (version_string); i > 0; --i)
2004 putc (' ', file);
2005 }
2006 }
2007
2008 /* If the st_other field is not zero, print it. */
7a13edea 2009 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2010
7a13edea
NC
2011 switch (st_other)
2012 {
2013 case 0: break;
2014 case STV_INTERNAL: fprintf (file, " .internal"); break;
2015 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2016 case STV_PROTECTED: fprintf (file, " .protected"); break;
2017 default:
2018 /* Some other non-defined flags are also present, so print
2019 everything hex. */
2020 fprintf (file, " 0x%02x", (unsigned int) st_other);
2021 }
252b5132 2022
587ff49e 2023 fprintf (file, " %s", name);
252b5132
RH
2024 }
2025 break;
2026 }
2027}
252b5132
RH
2028\f
2029/* ELF .o/exec file reading */
2030
c044fabd 2031/* Create a new bfd section from an ELF section header. */
252b5132 2032
0a1b45a2 2033bool
217aa764 2034bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2035{
4fbb74a6
AM
2036 Elf_Internal_Shdr *hdr;
2037 Elf_Internal_Ehdr *ehdr;
2038 const struct elf_backend_data *bed;
90937f86 2039 const char *name;
0a1b45a2 2040 bool ret = true;
252b5132 2041
4fbb74a6 2042 if (shindex >= elf_numsections (abfd))
0a1b45a2 2043 return false;
4fbb74a6 2044
a86c6c19
AM
2045 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2046 sh_link or sh_info. Detect this here, by refusing to load a
2047 section that we are already in the process of loading. */
2048 if (elf_tdata (abfd)->being_created[shindex])
bf67003b 2049 {
a86c6c19
AM
2050 _bfd_error_handler
2051 (_("%pB: warning: loop in section dependencies detected"), abfd);
0a1b45a2 2052 return false;
bf67003b 2053 }
0a1b45a2 2054 elf_tdata (abfd)->being_created[shindex] = true;
bf67003b 2055
4fbb74a6
AM
2056 hdr = elf_elfsections (abfd)[shindex];
2057 ehdr = elf_elfheader (abfd);
2058 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2059 hdr->sh_name);
933d961a 2060 if (name == NULL)
bf67003b 2061 goto fail;
252b5132 2062
4fbb74a6 2063 bed = get_elf_backend_data (abfd);
252b5132
RH
2064 switch (hdr->sh_type)
2065 {
2066 case SHT_NULL:
2067 /* Inactive section. Throw it away. */
bf67003b 2068 goto success;
252b5132 2069
bf67003b
NC
2070 case SHT_PROGBITS: /* Normal section with contents. */
2071 case SHT_NOBITS: /* .bss section. */
2072 case SHT_HASH: /* .hash section. */
2073 case SHT_NOTE: /* .note section. */
25e27870
L
2074 case SHT_INIT_ARRAY: /* .init_array section. */
2075 case SHT_FINI_ARRAY: /* .fini_array section. */
2076 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2077 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2078 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2079 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2080 goto success;
252b5132 2081
797fc050 2082 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2083 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2084 goto fail;
2085
cfcac11d
NC
2086 if (hdr->sh_link > elf_numsections (abfd))
2087 {
11c6a7c6
AM
2088 /* PR 10478: Accept Solaris binaries with a sh_link field
2089 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
cfcac11d
NC
2090 switch (bfd_get_arch (abfd))
2091 {
caa83f8b 2092 case bfd_arch_i386:
cfcac11d 2093 case bfd_arch_sparc:
11c6a7c6
AM
2094 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2095 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
cfcac11d
NC
2096 break;
2097 /* Otherwise fall through. */
2098 default:
bf67003b 2099 goto fail;
cfcac11d
NC
2100 }
2101 }
2102 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2103 goto fail;
cfcac11d 2104 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2105 {
2106 Elf_Internal_Shdr *dynsymhdr;
2107
2108 /* The shared libraries distributed with hpux11 have a bogus
2109 sh_link field for the ".dynamic" section. Find the
2110 string table for the ".dynsym" section instead. */
2111 if (elf_dynsymtab (abfd) != 0)
2112 {
2113 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2114 hdr->sh_link = dynsymhdr->sh_link;
2115 }
2116 else
2117 {
2118 unsigned int i, num_sec;
2119
2120 num_sec = elf_numsections (abfd);
2121 for (i = 1; i < num_sec; i++)
2122 {
2123 dynsymhdr = elf_elfsections (abfd)[i];
2124 if (dynsymhdr->sh_type == SHT_DYNSYM)
2125 {
2126 hdr->sh_link = dynsymhdr->sh_link;
2127 break;
2128 }
2129 }
2130 }
2131 }
bf67003b 2132 goto success;
797fc050 2133
bf67003b 2134 case SHT_SYMTAB: /* A symbol table. */
252b5132 2135 if (elf_onesymtab (abfd) == shindex)
bf67003b 2136 goto success;
252b5132 2137
a50b2160 2138 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2139 goto fail;
2140
3337c1e5 2141 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2142 {
2143 if (hdr->sh_size != 0)
bf67003b 2144 goto fail;
eee3b786
AM
2145 /* Some assemblers erroneously set sh_info to one with a
2146 zero sh_size. ld sees this as a global symbol count
2147 of (unsigned) -1. Fix it here. */
2148 hdr->sh_info = 0;
bf67003b 2149 goto success;
eee3b786 2150 }
bf67003b 2151
16ad13ec
NC
2152 /* PR 18854: A binary might contain more than one symbol table.
2153 Unusual, but possible. Warn, but continue. */
2154 if (elf_onesymtab (abfd) != 0)
2155 {
4eca0228 2156 _bfd_error_handler
695344c0 2157 /* xgettext:c-format */
871b3ab2 2158 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2159 " - ignoring the table in section %u"),
16ad13ec
NC
2160 abfd, shindex);
2161 goto success;
2162 }
252b5132 2163 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2164 elf_symtab_hdr (abfd) = *hdr;
2165 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2166 abfd->flags |= HAS_SYMS;
2167
2168 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2169 SHF_ALLOC is set, and this is a shared object, then we also
2170 treat this section as a BFD section. We can not base the
2171 decision purely on SHF_ALLOC, because that flag is sometimes
2172 set in a relocatable object file, which would confuse the
2173 linker. */
252b5132
RH
2174 if ((hdr->sh_flags & SHF_ALLOC) != 0
2175 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2176 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2177 shindex))
bf67003b 2178 goto fail;
252b5132 2179
1b3a8575
AM
2180 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2181 can't read symbols without that section loaded as well. It
2182 is most likely specified by the next section header. */
6a40cf0c
NC
2183 {
2184 elf_section_list * entry;
2185 unsigned int i, num_sec;
1b3a8575 2186
11c6a7c6 2187 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
6a40cf0c
NC
2188 if (entry->hdr.sh_link == shindex)
2189 goto success;
2190
2191 num_sec = elf_numsections (abfd);
2192 for (i = shindex + 1; i < num_sec; i++)
2193 {
2194 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2195
2196 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2197 && hdr2->sh_link == shindex)
2198 break;
2199 }
2200
2201 if (i == num_sec)
2202 for (i = 1; i < shindex; i++)
1b3a8575
AM
2203 {
2204 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2205
1b3a8575
AM
2206 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2207 && hdr2->sh_link == shindex)
2208 break;
2209 }
6a40cf0c
NC
2210
2211 if (i != shindex)
2212 ret = bfd_section_from_shdr (abfd, i);
11c6a7c6
AM
2213 /* else FIXME: we have failed to find the symbol table.
2214 Should we issue an error? */
6a40cf0c
NC
2215 goto success;
2216 }
252b5132 2217
bf67003b 2218 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2219 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2220 goto success;
252b5132 2221
a50b2160 2222 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2223 goto fail;
2224
eee3b786
AM
2225 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2226 {
2227 if (hdr->sh_size != 0)
bf67003b
NC
2228 goto fail;
2229
eee3b786
AM
2230 /* Some linkers erroneously set sh_info to one with a
2231 zero sh_size. ld sees this as a global symbol count
2232 of (unsigned) -1. Fix it here. */
2233 hdr->sh_info = 0;
bf67003b 2234 goto success;
eee3b786 2235 }
bf67003b 2236
16ad13ec
NC
2237 /* PR 18854: A binary might contain more than one dynamic symbol table.
2238 Unusual, but possible. Warn, but continue. */
2239 if (elf_dynsymtab (abfd) != 0)
2240 {
4eca0228 2241 _bfd_error_handler
695344c0 2242 /* xgettext:c-format */
871b3ab2 2243 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2244 " - ignoring the table in section %u"),
16ad13ec
NC
2245 abfd, shindex);
2246 goto success;
2247 }
252b5132
RH
2248 elf_dynsymtab (abfd) = shindex;
2249 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2250 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2251 abfd->flags |= HAS_SYMS;
2252
2253 /* Besides being a symbol table, we also treat this as a regular
2254 section, so that objcopy can handle it. */
bf67003b
NC
2255 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2256 goto success;
252b5132 2257
11c6a7c6 2258 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2259 {
2260 elf_section_list * entry;
9ad5cbcf 2261
11c6a7c6 2262 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
6a40cf0c
NC
2263 if (entry->ndx == shindex)
2264 goto success;
07d6d2b8 2265
7a6e0d89 2266 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2267 if (entry == NULL)
2268 goto fail;
2269 entry->ndx = shindex;
2270 entry->hdr = * hdr;
2271 entry->next = elf_symtab_shndx_list (abfd);
2272 elf_symtab_shndx_list (abfd) = entry;
2273 elf_elfsections (abfd)[shindex] = & entry->hdr;
2274 goto success;
2275 }
9ad5cbcf 2276
bf67003b 2277 case SHT_STRTAB: /* A string table. */
252b5132 2278 if (hdr->bfd_section != NULL)
bf67003b
NC
2279 goto success;
2280
252b5132
RH
2281 if (ehdr->e_shstrndx == shindex)
2282 {
2283 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2284 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2285 goto success;
252b5132 2286 }
bf67003b 2287
1b3a8575
AM
2288 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2289 {
2290 symtab_strtab:
2291 elf_tdata (abfd)->strtab_hdr = *hdr;
2292 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2293 goto success;
1b3a8575 2294 }
bf67003b 2295
1b3a8575
AM
2296 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2297 {
2298 dynsymtab_strtab:
2299 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2300 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2301 elf_elfsections (abfd)[shindex] = hdr;
2302 /* We also treat this as a regular section, so that objcopy
2303 can handle it. */
bf67003b
NC
2304 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2305 shindex);
2306 goto success;
1b3a8575 2307 }
252b5132 2308
1b3a8575
AM
2309 /* If the string table isn't one of the above, then treat it as a
2310 regular section. We need to scan all the headers to be sure,
2311 just in case this strtab section appeared before the above. */
2312 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2313 {
2314 unsigned int i, num_sec;
252b5132 2315
1b3a8575
AM
2316 num_sec = elf_numsections (abfd);
2317 for (i = 1; i < num_sec; i++)
2318 {
2319 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2320 if (hdr2->sh_link == shindex)
2321 {
933d961a
JJ
2322 /* Prevent endless recursion on broken objects. */
2323 if (i == shindex)
bf67003b 2324 goto fail;
1b3a8575 2325 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2326 goto fail;
1b3a8575
AM
2327 if (elf_onesymtab (abfd) == i)
2328 goto symtab_strtab;
2329 if (elf_dynsymtab (abfd) == i)
2330 goto dynsymtab_strtab;
2331 }
2332 }
2333 }
bf67003b
NC
2334 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2335 goto success;
252b5132
RH
2336
2337 case SHT_REL:
2338 case SHT_RELA:
a619b587 2339 case SHT_RELR:
252b5132
RH
2340 /* *These* do a lot of work -- but build no sections! */
2341 {
2342 asection *target_sect;
d4730f92 2343 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2344 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2345 struct bfd_elf_section_data *esdt;
a619b587 2346 bfd_size_type size;
252b5132 2347
a619b587
FS
2348 if (hdr->sh_type == SHT_REL)
2349 size = bed->s->sizeof_rel;
2350 else if (hdr->sh_type == SHT_RELA)
2351 size = bed->s->sizeof_rela;
2352 else
2353 size = bed->s->arch_size / 8;
2354 if (hdr->sh_entsize != size)
bf67003b 2355 goto fail;
a50b2160 2356
03ae5f59 2357 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2358 if (hdr->sh_link >= num_sec)
03ae5f59 2359 {
4eca0228 2360 _bfd_error_handler
695344c0 2361 /* xgettext:c-format */
871b3ab2 2362 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2363 abfd, hdr->sh_link, name, shindex);
11c6a7c6 2364 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b 2365 goto success;
03ae5f59
ILT
2366 }
2367
252b5132 2368 /* Get the symbol table. */
1b3a8575
AM
2369 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2370 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2371 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2372 goto fail;
252b5132 2373
a4bcd733
AM
2374 /* If this is an alloc section in an executable or shared
2375 library, or the reloc section does not use the main symbol
2376 table we don't treat it as a reloc section. BFD can't
2377 adequately represent such a section, so at least for now,
2378 we don't try. We just present it as a normal section. We
2379 also can't use it as a reloc section if it points to the
2380 null section, an invalid section, another reloc section, or
2381 its sh_link points to the null section. */
2382 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2383 && (hdr->sh_flags & SHF_ALLOC) != 0)
93c6e8c3 2384 || (hdr->sh_flags & SHF_COMPRESSED) != 0
37c49d0d 2385 || hdr->sh_type == SHT_RELR
83b89087 2386 || hdr->sh_link == SHN_UNDEF
a4bcd733 2387 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2388 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2389 || hdr->sh_info >= num_sec
2390 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2391 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b 2392 {
11c6a7c6 2393 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b
NC
2394 goto success;
2395 }
252b5132
RH
2396
2397 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2398 goto fail;
2399
252b5132
RH
2400 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2401 if (target_sect == NULL)
bf67003b 2402 goto fail;
252b5132 2403
d4730f92
BS
2404 esdt = elf_section_data (target_sect);
2405 if (hdr->sh_type == SHT_RELA)
2406 p_hdr = &esdt->rela.hdr;
252b5132 2407 else
d4730f92
BS
2408 p_hdr = &esdt->rel.hdr;
2409
a7ba3896
NC
2410 /* PR 17512: file: 0b4f81b7.
2411 Also see PR 24456, for a file which deliberately has two reloc
2412 sections. */
06614111 2413 if (*p_hdr != NULL)
a7ba3896 2414 {
a859124d 2415 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2416 {
2417 _bfd_error_handler
2418 /* xgettext:c-format */
a859124d
AM
2419 (_("%pB: warning: secondary relocation section '%s' "
2420 "for section %pA found - ignoring"),
a8e14f4c
NC
2421 abfd, name, target_sect);
2422 }
956ea65c
MM
2423 else
2424 esdt->has_secondary_relocs = true;
a7ba3896
NC
2425 goto success;
2426 }
a8e14f4c 2427
ef53be89 2428 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2429 if (hdr2 == NULL)
bf67003b 2430 goto fail;
252b5132 2431 *hdr2 = *hdr;
d4730f92 2432 *p_hdr = hdr2;
252b5132 2433 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2434 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2435 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2436 target_sect->flags |= SEC_RELOC;
2437 target_sect->relocation = NULL;
2438 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2439 /* In the section to which the relocations apply, mark whether
2440 its relocations are of the REL or RELA variety. */
72730e0c 2441 if (hdr->sh_size != 0)
d4730f92
BS
2442 {
2443 if (hdr->sh_type == SHT_RELA)
2444 target_sect->use_rela_p = 1;
2445 }
252b5132 2446 abfd->flags |= HAS_RELOC;
bf67003b 2447 goto success;
252b5132 2448 }
252b5132
RH
2449
2450 case SHT_GNU_verdef:
f9a59eea
AM
2451 if (hdr->sh_info != 0)
2452 elf_dynverdef (abfd) = shindex;
252b5132 2453 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2454 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2455 goto success;
252b5132
RH
2456
2457 case SHT_GNU_versym:
a50b2160 2458 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2459 goto fail;
2460
252b5132
RH
2461 elf_dynversym (abfd) = shindex;
2462 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2463 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2464 goto success;
252b5132
RH
2465
2466 case SHT_GNU_verneed:
f9a59eea
AM
2467 if (hdr->sh_info != 0)
2468 elf_dynverref (abfd) = shindex;
252b5132 2469 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2470 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2471 goto success;
252b5132
RH
2472
2473 case SHT_SHLIB:
bf67003b 2474 goto success;
252b5132 2475
dbb410c3 2476 case SHT_GROUP:
44534af3 2477 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2478 goto fail;
2479
6dc132d9 2480 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2481 goto fail;
2482
bf67003b 2483 goto success;
dbb410c3 2484
252b5132 2485 default:
104d59d1
JM
2486 /* Possibly an attributes section. */
2487 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2488 || hdr->sh_type == bed->obj_attrs_section_type)
2489 {
2490 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2491 goto fail;
104d59d1 2492 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2493 goto success;
104d59d1
JM
2494 }
2495
252b5132 2496 /* Check for any processor-specific section types. */
3eb70a79 2497 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2498 goto success;
3eb70a79
L
2499
2500 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2501 {
2502 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2503 /* FIXME: How to properly handle allocated section reserved
2504 for applications? */
4eca0228 2505 _bfd_error_handler
695344c0 2506 /* xgettext:c-format */
871b3ab2 2507 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2508 abfd, hdr->sh_type, name);
3eb70a79 2509 else
bf67003b
NC
2510 {
2511 /* Allow sections reserved for applications. */
11c6a7c6 2512 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
bf67003b
NC
2513 goto success;
2514 }
3eb70a79
L
2515 }
2516 else if (hdr->sh_type >= SHT_LOPROC
2517 && hdr->sh_type <= SHT_HIPROC)
2518 /* FIXME: We should handle this section. */
4eca0228 2519 _bfd_error_handler
695344c0 2520 /* xgettext:c-format */
871b3ab2 2521 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2522 abfd, hdr->sh_type, name);
3eb70a79 2523 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2524 {
2525 /* Unrecognised OS-specific sections. */
2526 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2527 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2528 required to correctly process the section and the file should
ff15b240 2529 be rejected with an error message. */
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);
ff15b240 2534 else
bf67003b
NC
2535 {
2536 /* Otherwise it should be processed. */
2537 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2538 goto success;
2539 }
ff15b240 2540 }
3eb70a79
L
2541 else
2542 /* FIXME: We should handle this section. */
4eca0228 2543 _bfd_error_handler
695344c0 2544 /* xgettext:c-format */
871b3ab2 2545 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2546 abfd, hdr->sh_type, name);
3eb70a79 2547
bf67003b 2548 goto fail;
252b5132
RH
2549 }
2550
bf67003b 2551 fail:
0a1b45a2 2552 ret = false;
bf67003b 2553 success:
0a1b45a2 2554 elf_tdata (abfd)->being_created[shindex] = false;
bf67003b 2555 return ret;
252b5132
RH
2556}
2557
87d72d41 2558/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2559
87d72d41
AM
2560Elf_Internal_Sym *
2561bfd_sym_from_r_symndx (struct sym_cache *cache,
2562 bfd *abfd,
2563 unsigned long r_symndx)
ec338859 2564{
ec338859
AM
2565 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2566
a5d1b3b5
AM
2567 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2568 {
2569 Elf_Internal_Shdr *symtab_hdr;
2570 unsigned char esym[sizeof (Elf64_External_Sym)];
2571 Elf_External_Sym_Shndx eshndx;
ec338859 2572
a5d1b3b5
AM
2573 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2574 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2575 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2576 return NULL;
9ad5cbcf 2577
a5d1b3b5
AM
2578 if (cache->abfd != abfd)
2579 {
2580 memset (cache->indx, -1, sizeof (cache->indx));
2581 cache->abfd = abfd;
2582 }
2583 cache->indx[ent] = r_symndx;
ec338859 2584 }
a5d1b3b5 2585
87d72d41 2586 return &cache->sym[ent];
ec338859
AM
2587}
2588
252b5132
RH
2589/* Given an ELF section number, retrieve the corresponding BFD
2590 section. */
2591
2592asection *
91d6fa6a 2593bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2594{
91d6fa6a 2595 if (sec_index >= elf_numsections (abfd))
252b5132 2596 return NULL;
91d6fa6a 2597 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2598}
2599
b35d266b 2600static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2601{
0112cd26 2602 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2603 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2604};
2605
b35d266b 2606static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2607{
0112cd26 2608 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2609 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2610 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2611};
2612
b35d266b 2613static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2614{
07d6d2b8
AM
2615 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2616 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2617 /* There are more DWARF sections than these, but they needn't be added here
2618 unless you have to cope with broken compilers that don't emit section
2619 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2620 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2621 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2622 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2623 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2624 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2625 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2626 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2627 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2628 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2629};
2630
b35d266b 2631static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2632{
07d6d2b8 2633 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2634 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2635 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2636};
2637
b35d266b 2638static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2639{
0112cd26 2640 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2641 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2642 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2643 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2644 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2645 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2646 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2647 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2648 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2649 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2650 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2651 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2652};
2653
b35d266b 2654static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2655{
07d6d2b8
AM
2656 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2657 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2658};
2659
b35d266b 2660static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2661{
07d6d2b8 2662 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2663 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2664 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2665 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2666};
2667
b35d266b 2668static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2669{
0112cd26 2670 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2671 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2672};
2673
b35d266b 2674static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2675{
2c6f3e56 2676 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2677 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2678 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2679 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2680};
2681
b35d266b 2682static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2683{
f5e98b7d 2684 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56 2685 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2686 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2687 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2688 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2689};
2690
b35d266b 2691static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2692{
0112cd26
NC
2693 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2694 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
fc07cf22 2695 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
07d6d2b8
AM
2696 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2697 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2698 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2699};
2700
b35d266b 2701static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2702{
0112cd26
NC
2703 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2704 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2705 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2706 /* See struct bfd_elf_special_section declaration for the semantics of
2707 this special case where .prefix_length != strlen (.prefix). */
2708 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2709 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2710};
2711
b35d266b 2712static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2713{
07d6d2b8
AM
2714 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2715 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2716 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2717 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2718};
2719
1b315056
CS
2720static const struct bfd_elf_special_section special_sections_z[] =
2721{
07d6d2b8
AM
2722 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2723 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2724 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2725 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2726 { NULL, 0, 0, 0, 0 }
1b315056
CS
2727};
2728
e4c93b56 2729static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2730{
7f4d3958 2731 special_sections_b, /* 'b' */
98ece1b3 2732 special_sections_c, /* 'c' */
7f4d3958
L
2733 special_sections_d, /* 'd' */
2734 NULL, /* 'e' */
2735 special_sections_f, /* 'f' */
2736 special_sections_g, /* 'g' */
2737 special_sections_h, /* 'h' */
2738 special_sections_i, /* 'i' */
2739 NULL, /* 'j' */
2740 NULL, /* 'k' */
2741 special_sections_l, /* 'l' */
2742 NULL, /* 'm' */
2743 special_sections_n, /* 'n' */
2744 NULL, /* 'o' */
2745 special_sections_p, /* 'p' */
2746 NULL, /* 'q' */
2747 special_sections_r, /* 'r' */
2748 special_sections_s, /* 's' */
2749 special_sections_t, /* 't' */
1b315056
CS
2750 NULL, /* 'u' */
2751 NULL, /* 'v' */
2752 NULL, /* 'w' */
2753 NULL, /* 'x' */
2754 NULL, /* 'y' */
2755 special_sections_z /* 'z' */
7f4d3958
L
2756};
2757
551b43fd
AM
2758const struct bfd_elf_special_section *
2759_bfd_elf_get_special_section (const char *name,
2760 const struct bfd_elf_special_section *spec,
2761 unsigned int rela)
2f89ff8d
L
2762{
2763 int i;
7f4d3958 2764 int len;
7f4d3958 2765
551b43fd 2766 len = strlen (name);
7f4d3958 2767
551b43fd 2768 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2769 {
2770 int suffix_len;
551b43fd 2771 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2772
2773 if (len < prefix_len)
2774 continue;
551b43fd 2775 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2776 continue;
2777
551b43fd 2778 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2779 if (suffix_len <= 0)
2780 {
2781 if (name[prefix_len] != 0)
2782 {
2783 if (suffix_len == 0)
2784 continue;
2785 if (name[prefix_len] != '.'
2786 && (suffix_len == -2
551b43fd 2787 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2788 continue;
2789 }
2790 }
2791 else
2792 {
2793 if (len < prefix_len + suffix_len)
2794 continue;
2795 if (memcmp (name + len - suffix_len,
551b43fd 2796 spec[i].prefix + prefix_len,
7dcb9820
AM
2797 suffix_len) != 0)
2798 continue;
2799 }
551b43fd 2800 return &spec[i];
7dcb9820 2801 }
2f89ff8d
L
2802
2803 return NULL;
2804}
2805
7dcb9820 2806const struct bfd_elf_special_section *
29ef7005 2807_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2808{
551b43fd
AM
2809 int i;
2810 const struct bfd_elf_special_section *spec;
29ef7005 2811 const struct elf_backend_data *bed;
2f89ff8d
L
2812
2813 /* See if this is one of the special sections. */
551b43fd
AM
2814 if (sec->name == NULL)
2815 return NULL;
2f89ff8d 2816
29ef7005
L
2817 bed = get_elf_backend_data (abfd);
2818 spec = bed->special_sections;
2819 if (spec)
2820 {
2821 spec = _bfd_elf_get_special_section (sec->name,
2822 bed->special_sections,
2823 sec->use_rela_p);
2824 if (spec != NULL)
2825 return spec;
2826 }
2827
551b43fd
AM
2828 if (sec->name[0] != '.')
2829 return NULL;
2f89ff8d 2830
551b43fd 2831 i = sec->name[1] - 'b';
1b315056 2832 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2833 return NULL;
2834
2835 spec = special_sections[i];
2f89ff8d 2836
551b43fd
AM
2837 if (spec == NULL)
2838 return NULL;
2839
2840 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2841}
2842
0a1b45a2 2843bool
217aa764 2844_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2845{
2846 struct bfd_elf_section_data *sdata;
551b43fd 2847 const struct elf_backend_data *bed;
7dcb9820 2848 const struct bfd_elf_special_section *ssect;
252b5132 2849
f0abc2a1
AM
2850 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2851 if (sdata == NULL)
2852 {
a50b1753 2853 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2854 sizeof (*sdata));
f0abc2a1 2855 if (sdata == NULL)
0a1b45a2 2856 return false;
217aa764 2857 sec->used_by_bfd = sdata;
f0abc2a1 2858 }
bf572ba0 2859
551b43fd
AM
2860 /* Indicate whether or not this section should use RELA relocations. */
2861 bed = get_elf_backend_data (abfd);
2862 sec->use_rela_p = bed->default_use_rela_p;
2863
8c803a2d
AM
2864 /* Set up ELF section type and flags for newly created sections, if
2865 there is an ABI mandated section. */
2866 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2867 if (ssect != NULL)
2f89ff8d 2868 {
8c803a2d
AM
2869 elf_section_type (sec) = ssect->type;
2870 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2871 }
2872
f592407e 2873 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2874}
2875
2876/* Create a new bfd section from an ELF program header.
2877
2878 Since program segments have no names, we generate a synthetic name
2879 of the form segment<NUM>, where NUM is generally the index in the
2880 program header table. For segments that are split (see below) we
2881 generate the names segment<NUM>a and segment<NUM>b.
2882
2883 Note that some program segments may have a file size that is different than
2884 (less than) the memory size. All this means is that at execution the
2885 system must allocate the amount of memory specified by the memory size,
2886 but only initialize it with the first "file size" bytes read from the
2887 file. This would occur for example, with program segments consisting
2888 of combined data+bss.
2889
2890 To handle the above situation, this routine generates TWO bfd sections
2891 for the single program segment. The first has the length specified by
2892 the file size of the segment, and the second has the length specified
2893 by the difference between the two sizes. In effect, the segment is split
11c6a7c6 2894 into its initialized and uninitialized parts. */
252b5132 2895
0a1b45a2 2896bool
217aa764
AM
2897_bfd_elf_make_section_from_phdr (bfd *abfd,
2898 Elf_Internal_Phdr *hdr,
91d6fa6a 2899 int hdr_index,
a50b1753 2900 const char *type_name)
252b5132
RH
2901{
2902 asection *newsect;
2903 char *name;
2904 char namebuf[64];
d4c88bbb 2905 size_t len;
252b5132 2906 int split;
502794d4 2907 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2908
2909 split = ((hdr->p_memsz > 0)
2910 && (hdr->p_filesz > 0)
2911 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2912
2913 if (hdr->p_filesz > 0)
252b5132 2914 {
91d6fa6a 2915 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2916 len = strlen (namebuf) + 1;
a50b1753 2917 name = (char *) bfd_alloc (abfd, len);
d5191d0c 2918 if (!name)
0a1b45a2 2919 return false;
d5191d0c
AM
2920 memcpy (name, namebuf, len);
2921 newsect = bfd_make_section (abfd, name);
2922 if (newsect == NULL)
0a1b45a2 2923 return false;
502794d4
CE
2924 newsect->vma = hdr->p_vaddr / opb;
2925 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2926 newsect->size = hdr->p_filesz;
2927 newsect->filepos = hdr->p_offset;
2928 newsect->flags |= SEC_HAS_CONTENTS;
2929 newsect->alignment_power = bfd_log2 (hdr->p_align);
2930 if (hdr->p_type == PT_LOAD)
252b5132 2931 {
d5191d0c
AM
2932 newsect->flags |= SEC_ALLOC;
2933 newsect->flags |= SEC_LOAD;
2934 if (hdr->p_flags & PF_X)
2935 {
2936 /* FIXME: all we known is that it has execute PERMISSION,
2937 may be data. */
2938 newsect->flags |= SEC_CODE;
2939 }
2940 }
2941 if (!(hdr->p_flags & PF_W))
2942 {
2943 newsect->flags |= SEC_READONLY;
252b5132 2944 }
252b5132
RH
2945 }
2946
d5191d0c 2947 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2948 {
d5191d0c
AM
2949 bfd_vma align;
2950
91d6fa6a 2951 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2952 len = strlen (namebuf) + 1;
a50b1753 2953 name = (char *) bfd_alloc (abfd, len);
252b5132 2954 if (!name)
0a1b45a2 2955 return false;
d4c88bbb 2956 memcpy (name, namebuf, len);
252b5132
RH
2957 newsect = bfd_make_section (abfd, name);
2958 if (newsect == NULL)
0a1b45a2 2959 return false;
502794d4
CE
2960 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2961 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 2962 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2963 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2964 align = newsect->vma & -newsect->vma;
2965 if (align == 0 || align > hdr->p_align)
2966 align = hdr->p_align;
2967 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2968 if (hdr->p_type == PT_LOAD)
2969 {
2970 newsect->flags |= SEC_ALLOC;
2971 if (hdr->p_flags & PF_X)
2972 newsect->flags |= SEC_CODE;
2973 }
2974 if (!(hdr->p_flags & PF_W))
2975 newsect->flags |= SEC_READONLY;
2976 }
2977
0a1b45a2 2978 return true;
252b5132
RH
2979}
2980
0a1b45a2 2981static bool
864619bb
KS
2982_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
2983{
2984 /* The return value is ignored. Build-ids are considered optional. */
2985 if (templ->xvec->flavour == bfd_target_elf_flavour)
2986 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
2987 (templ, offset);
0a1b45a2 2988 return false;
864619bb
KS
2989}
2990
0a1b45a2 2991bool
91d6fa6a 2992bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 2993{
9c5bfbb7 2994 const struct elf_backend_data *bed;
20cfcaae
NC
2995
2996 switch (hdr->p_type)
2997 {
2998 case PT_NULL:
91d6fa6a 2999 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3000
3001 case PT_LOAD:
864619bb 3002 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
0a1b45a2 3003 return false;
864619bb
KS
3004 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3005 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
0a1b45a2 3006 return true;
20cfcaae
NC
3007
3008 case PT_DYNAMIC:
91d6fa6a 3009 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3010
3011 case PT_INTERP:
91d6fa6a 3012 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3013
3014 case PT_NOTE:
91d6fa6a 3015 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
0a1b45a2 3016 return false;
276da9b3
L
3017 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3018 hdr->p_align))
0a1b45a2
AM
3019 return false;
3020 return true;
20cfcaae
NC
3021
3022 case PT_SHLIB:
91d6fa6a 3023 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3024
3025 case PT_PHDR:
91d6fa6a 3026 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3027
811072d8 3028 case PT_GNU_EH_FRAME:
91d6fa6a 3029 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3030 "eh_frame_hdr");
3031
2b05f1b7 3032 case PT_GNU_STACK:
91d6fa6a 3033 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3034
8c37241b 3035 case PT_GNU_RELRO:
91d6fa6a 3036 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3037
cf0e0a0b
IB
3038 case PT_GNU_SFRAME:
3039 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040 "sframe");
3041
20cfcaae 3042 default:
8c1acd09 3043 /* Check for any processor-specific program segment types. */
20cfcaae 3044 bed = get_elf_backend_data (abfd);
91d6fa6a 3045 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3046 }
3047}
3048
d4730f92
BS
3049/* Return the REL_HDR for SEC, assuming there is only a single one, either
3050 REL or RELA. */
3051
3052Elf_Internal_Shdr *
3053_bfd_elf_single_rel_hdr (asection *sec)
3054{
3055 if (elf_section_data (sec)->rel.hdr)
3056 {
3057 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3058 return elf_section_data (sec)->rel.hdr;
3059 }
3060 else
3061 return elf_section_data (sec)->rela.hdr;
3062}
3063
0a1b45a2 3064static bool
3e19fb8f
L
3065_bfd_elf_set_reloc_sh_name (bfd *abfd,
3066 Elf_Internal_Shdr *rel_hdr,
3067 const char *sec_name,
0a1b45a2 3068 bool use_rela_p)
3e19fb8f
L
3069{
3070 char *name = (char *) bfd_alloc (abfd,
3071 sizeof ".rela" + strlen (sec_name));
3072 if (name == NULL)
0a1b45a2 3073 return false;
3e19fb8f
L
3074
3075 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3076 rel_hdr->sh_name =
3077 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
0a1b45a2 3078 false);
3e19fb8f 3079 if (rel_hdr->sh_name == (unsigned int) -1)
0a1b45a2 3080 return false;
3e19fb8f 3081
0a1b45a2 3082 return true;
3e19fb8f
L
3083}
3084
d4730f92
BS
3085/* Allocate and initialize a section-header for a new reloc section,
3086 containing relocations against ASECT. It is stored in RELDATA. If
3087 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3088 relocations. */
23bc299b 3089
0a1b45a2 3090static bool
217aa764 3091_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3092 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3093 const char *sec_name,
0a1b45a2
AM
3094 bool use_rela_p,
3095 bool delay_st_name_p)
23bc299b 3096{
d4730f92 3097 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3098 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3099
d4730f92 3100 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3101 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3102 reldata->hdr = rel_hdr;
23bc299b 3103
3e19fb8f
L
3104 if (delay_st_name_p)
3105 rel_hdr->sh_name = (unsigned int) -1;
3106 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3107 use_rela_p))
0a1b45a2 3108 return false;
23bc299b
MM
3109 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3110 rel_hdr->sh_entsize = (use_rela_p
3111 ? bed->s->sizeof_rela
3112 : bed->s->sizeof_rel);
72de5009 3113 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3114 rel_hdr->sh_flags = 0;
23bc299b
MM
3115 rel_hdr->sh_addr = 0;
3116 rel_hdr->sh_size = 0;
3117 rel_hdr->sh_offset = 0;
3118
0a1b45a2 3119 return true;
23bc299b
MM
3120}
3121
94be91de
JB
3122/* Return the default section type based on the passed in section flags. */
3123
3124int
3125bfd_elf_get_default_section_type (flagword flags)
3126{
0e41bebb 3127 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3128 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3129 return SHT_NOBITS;
3130 return SHT_PROGBITS;
3131}
3132
d4730f92
BS
3133struct fake_section_arg
3134{
3135 struct bfd_link_info *link_info;
0a1b45a2 3136 bool failed;
d4730f92
BS
3137};
3138
252b5132
RH
3139/* Set up an ELF internal section header for a section. */
3140
252b5132 3141static void
d4730f92 3142elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3143{
d4730f92 3144 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3145 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3146 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3147 Elf_Internal_Shdr *this_hdr;
0414f35b 3148 unsigned int sh_type;
0ce398f1 3149 const char *name = asect->name;
0a1b45a2 3150 bool delay_st_name_p = false;
233bf4f8 3151 bfd_vma mask;
252b5132 3152
d4730f92 3153 if (arg->failed)
252b5132
RH
3154 {
3155 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3156 loop. */
252b5132
RH
3157 return;
3158 }
3159
d4730f92 3160 this_hdr = &esd->this_hdr;
252b5132 3161
7b5f66a1
AM
3162 /* ld: compress DWARF debug sections with names: .debug_*. */
3163 if (arg->link_info
c3620d6d
AM
3164 && (abfd->flags & BFD_COMPRESS) != 0
3165 && (asect->flags & SEC_DEBUGGING) != 0
7b5f66a1
AM
3166 && name[1] == 'd'
3167 && name[6] == '_')
f6fe1ccd 3168 {
7b5f66a1
AM
3169 /* If this section will be compressed, delay adding section
3170 name to section name section after it is compressed in
3171 _bfd_elf_assign_file_positions_for_non_load. */
3172 delay_st_name_p = true;
0ce398f1
L
3173 }
3174
3e19fb8f
L
3175 if (delay_st_name_p)
3176 this_hdr->sh_name = (unsigned int) -1;
3177 else
252b5132 3178 {
3e19fb8f
L
3179 this_hdr->sh_name
3180 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3181 name, false);
3e19fb8f
L
3182 if (this_hdr->sh_name == (unsigned int) -1)
3183 {
0a1b45a2 3184 arg->failed = true;
3e19fb8f
L
3185 return;
3186 }
252b5132
RH
3187 }
3188
a4d8e49b 3189 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3190
3191 if ((asect->flags & SEC_ALLOC) != 0
3192 || asect->user_set_vma)
502794d4 3193 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3194 else
3195 this_hdr->sh_addr = 0;
3196
3197 this_hdr->sh_offset = 0;
eea6121a 3198 this_hdr->sh_size = asect->size;
252b5132 3199 this_hdr->sh_link = 0;
c86934ce
NC
3200 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3201 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3202 {
4eca0228 3203 _bfd_error_handler
695344c0 3204 /* xgettext:c-format */
9793eb77 3205 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3206 abfd, asect->alignment_power, asect);
0a1b45a2 3207 arg->failed = true;
c86934ce
NC
3208 return;
3209 }
233bf4f8
AM
3210 /* Set sh_addralign to the highest power of two given by alignment
3211 consistent with the section VMA. Linker scripts can force VMA. */
3212 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3213 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3214 /* The sh_entsize and sh_info fields may have been set already by
3215 copy_private_section_data. */
3216
3217 this_hdr->bfd_section = asect;
3218 this_hdr->contents = NULL;
3219
3cddba1e
L
3220 /* If the section type is unspecified, we set it based on
3221 asect->flags. */
c212f39d
FS
3222 if (asect->type != 0)
3223 sh_type = asect->type;
3224 else if ((asect->flags & SEC_GROUP) != 0)
98ece1b3 3225 sh_type = SHT_GROUP;
98ece1b3 3226 else
94be91de 3227 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3228
3cddba1e 3229 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3230 this_hdr->sh_type = sh_type;
3231 else if (this_hdr->sh_type == SHT_NOBITS
3232 && sh_type == SHT_PROGBITS
3233 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3234 {
98ece1b3
AM
3235 /* Warn if we are changing a NOBITS section to PROGBITS, but
3236 allow the link to proceed. This can happen when users link
3237 non-bss input sections to bss output sections, or emit data
3238 to a bss output section via a linker script. */
4eca0228 3239 _bfd_error_handler
871b3ab2 3240 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3241 this_hdr->sh_type = sh_type;
3cddba1e
L
3242 }
3243
2f89ff8d 3244 switch (this_hdr->sh_type)
252b5132 3245 {
2f89ff8d 3246 default:
2f89ff8d
L
3247 break;
3248
3249 case SHT_STRTAB:
2f89ff8d
L
3250 case SHT_NOTE:
3251 case SHT_NOBITS:
3252 case SHT_PROGBITS:
3253 break;
606851fb
AM
3254
3255 case SHT_INIT_ARRAY:
3256 case SHT_FINI_ARRAY:
3257 case SHT_PREINIT_ARRAY:
3258 this_hdr->sh_entsize = bed->s->arch_size / 8;
3259 break;
2f89ff8d
L
3260
3261 case SHT_HASH:
c7ac6ff8 3262 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3263 break;
5de3bf90 3264
2f89ff8d 3265 case SHT_DYNSYM:
252b5132 3266 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3267 break;
3268
3269 case SHT_DYNAMIC:
252b5132 3270 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3271 break;
3272
3273 case SHT_RELA:
3274 if (get_elf_backend_data (abfd)->may_use_rela_p)
3275 this_hdr->sh_entsize = bed->s->sizeof_rela;
3276 break;
3277
3278 case SHT_REL:
3279 if (get_elf_backend_data (abfd)->may_use_rel_p)
3280 this_hdr->sh_entsize = bed->s->sizeof_rel;
3281 break;
3282
3283 case SHT_GNU_versym:
252b5132 3284 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3285 break;
3286
3287 case SHT_GNU_verdef:
252b5132
RH
3288 this_hdr->sh_entsize = 0;
3289 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3290 cverdefs. The linker will set cverdefs, but sh_info will be
3291 zero. */
252b5132
RH
3292 if (this_hdr->sh_info == 0)
3293 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3294 else
3295 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3296 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3297 break;
3298
3299 case SHT_GNU_verneed:
252b5132
RH
3300 this_hdr->sh_entsize = 0;
3301 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3302 cverrefs. The linker will set cverrefs, but sh_info will be
3303 zero. */
252b5132
RH
3304 if (this_hdr->sh_info == 0)
3305 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3306 else
3307 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3308 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3309 break;
3310
3311 case SHT_GROUP:
1783205a 3312 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3313 break;
fdc90cb4
JJ
3314
3315 case SHT_GNU_HASH:
3316 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3317 break;
dbb410c3 3318 }
252b5132
RH
3319
3320 if ((asect->flags & SEC_ALLOC) != 0)
3321 this_hdr->sh_flags |= SHF_ALLOC;
3322 if ((asect->flags & SEC_READONLY) == 0)
3323 this_hdr->sh_flags |= SHF_WRITE;
3324 if ((asect->flags & SEC_CODE) != 0)
3325 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3326 if ((asect->flags & SEC_MERGE) != 0)
3327 {
3328 this_hdr->sh_flags |= SHF_MERGE;
3329 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3330 }
84865015
NC
3331 if ((asect->flags & SEC_STRINGS) != 0)
3332 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3333 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3334 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3335 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3336 {
3337 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3338 if (asect->size == 0
3339 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3340 {
3a800eb9 3341 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3342
704afa60 3343 this_hdr->sh_size = 0;
3a800eb9
AM
3344 if (o != NULL)
3345 {
704afa60 3346 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3347 if (this_hdr->sh_size != 0)
3348 this_hdr->sh_type = SHT_NOBITS;
3349 }
704afa60
JJ
3350 }
3351 }
18ae9cc1
L
3352 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3353 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3354
d4730f92
BS
3355 /* If the section has relocs, set up a section header for the
3356 SHT_REL[A] section. If two relocation sections are required for
3357 this section, it is up to the processor-specific back-end to
3358 create the other. */
3359 if ((asect->flags & SEC_RELOC) != 0)
3360 {
3361 /* When doing a relocatable link, create both REL and RELA sections if
3362 needed. */
3363 if (arg->link_info
3364 /* Do the normal setup if we wouldn't create any sections here. */
3365 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3366 && (bfd_link_relocatable (arg->link_info)
3367 || arg->link_info->emitrelocations))
d4730f92
BS
3368 {
3369 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3370 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
0a1b45a2 3371 false, delay_st_name_p))
d4730f92 3372 {
0a1b45a2 3373 arg->failed = true;
d4730f92
BS
3374 return;
3375 }
3376 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3377 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
0a1b45a2 3378 true, delay_st_name_p))
d4730f92 3379 {
0a1b45a2 3380 arg->failed = true;
d4730f92
BS
3381 return;
3382 }
3383 }
3384 else if (!_bfd_elf_init_reloc_shdr (abfd,
3385 (asect->use_rela_p
3386 ? &esd->rela : &esd->rel),
f6fe1ccd 3387 name,
3e19fb8f
L
3388 asect->use_rela_p,
3389 delay_st_name_p))
db4677b8 3390 {
0a1b45a2 3391 arg->failed = true;
db4677b8
AM
3392 return;
3393 }
d4730f92
BS
3394 }
3395
252b5132 3396 /* Check for processor-specific section types. */
0414f35b 3397 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3398 if (bed->elf_backend_fake_sections
3399 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8 3400 {
0a1b45a2 3401 arg->failed = true;
db4677b8
AM
3402 return;
3403 }
252b5132 3404
42bb2e33 3405 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3406 {
3407 /* Don't change the header type from NOBITS if we are being
42bb2e33 3408 called for objcopy --only-keep-debug. */
0414f35b
AM
3409 this_hdr->sh_type = sh_type;
3410 }
252b5132
RH
3411}
3412
bcacc0f5
AM
3413/* Fill in the contents of a SHT_GROUP section. Called from
3414 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3415 when ELF targets use the generic linker, ld. Called for ld -r
3416 from bfd_elf_final_link. */
dbb410c3 3417
1126897b 3418void
217aa764 3419bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3420{
0a1b45a2 3421 bool *failedptr = (bool *) failedptrarg;
9dce4196 3422 asection *elt, *first;
dbb410c3 3423 unsigned char *loc;
0a1b45a2 3424 bool gas;
dbb410c3 3425
7e4111ad
L
3426 /* Ignore linker created group section. See elfNN_ia64_object_p in
3427 elfxx-ia64.c. */
ce5aecf8
AM
3428 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3429 || sec->size == 0
dbb410c3
AM
3430 || *failedptr)
3431 return;
3432
bcacc0f5
AM
3433 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3434 {
3435 unsigned long symindx = 0;
3436
3437 /* elf_group_id will have been set up by objcopy and the
3438 generic linker. */
3439 if (elf_group_id (sec) != NULL)
3440 symindx = elf_group_id (sec)->udata.i;
1126897b 3441
bcacc0f5
AM
3442 if (symindx == 0)
3443 {
3444 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3445 elf_section_syms.
3446 PR 25699: A corrupt input file could contain bogus group info. */
27e3da31
AM
3447 if (sec->index >= elf_num_section_syms (abfd)
3448 || elf_section_syms (abfd)[sec->index] == NULL)
6a541707 3449 {
0a1b45a2 3450 *failedptr = true;
6a541707
NC
3451 return;
3452 }
bcacc0f5
AM
3453 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3454 }
3455 elf_section_data (sec)->this_hdr.sh_info = symindx;
3456 }
3457 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3458 {
bcacc0f5
AM
3459 /* The ELF backend linker sets sh_info to -2 when the group
3460 signature symbol is global, and thus the index can't be
3461 set until all local symbols are output. */
53720c49
AM
3462 asection *igroup;
3463 struct bfd_elf_section_data *sec_data;
3464 unsigned long symndx;
3465 unsigned long extsymoff;
bcacc0f5
AM
3466 struct elf_link_hash_entry *h;
3467
53720c49
AM
3468 /* The point of this little dance to the first SHF_GROUP section
3469 then back to the SHT_GROUP section is that this gets us to
3470 the SHT_GROUP in the input object. */
3471 igroup = elf_sec_group (elf_next_in_group (sec));
3472 sec_data = elf_section_data (igroup);
3473 symndx = sec_data->this_hdr.sh_info;
3474 extsymoff = 0;
bcacc0f5
AM
3475 if (!elf_bad_symtab (igroup->owner))
3476 {
3477 Elf_Internal_Shdr *symtab_hdr;
3478
3479 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3480 extsymoff = symtab_hdr->sh_info;
3481 }
3482 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3483 while (h->root.type == bfd_link_hash_indirect
3484 || h->root.type == bfd_link_hash_warning)
3485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3486
3487 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3488 }
dbb410c3 3489
1126897b 3490 /* The contents won't be allocated for "ld -r" or objcopy. */
0a1b45a2 3491 gas = true;
dbb410c3
AM
3492 if (sec->contents == NULL)
3493 {
0a1b45a2 3494 gas = false;
a50b1753 3495 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3496
3497 /* Arrange for the section to be written out. */
3498 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3499 if (sec->contents == NULL)
3500 {
0a1b45a2 3501 *failedptr = true;
dbb410c3
AM
3502 return;
3503 }
3504 }
3505
eea6121a 3506 loc = sec->contents + sec->size;
dbb410c3 3507
9dce4196
AM
3508 /* Get the pointer to the first section in the group that gas
3509 squirreled away here. objcopy arranges for this to be set to the
3510 start of the input section group. */
3511 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3512
3513 /* First element is a flag word. Rest of section is elf section
3514 indices for all the sections of the group. Write them backwards
3515 just to keep the group in the same order as given in .section
3516 directives, not that it matters. */
3517 while (elt != NULL)
3518 {
9dce4196 3519 asection *s;
9dce4196 3520
9dce4196 3521 s = elt;
415f38a6
AM
3522 if (!gas)
3523 s = s->output_section;
3524 if (s != NULL
3525 && !bfd_is_abs_section (s))
01e1a5bc 3526 {
db4677b8 3527 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3528 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3529
3530 if (elf_sec->rel.hdr != NULL
3531 && (gas
3532 || (input_elf_sec->rel.hdr != NULL
3533 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3534 {
28e07a05 3535 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8 3536 loc -= 4;
6ecc36f7
AM
3537 if (loc == sec->contents)
3538 break;
db4677b8
AM
3539 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3540 }
28e07a05
AM
3541 if (elf_sec->rela.hdr != NULL
3542 && (gas
3543 || (input_elf_sec->rela.hdr != NULL
3544 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3545 {
28e07a05 3546 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8 3547 loc -= 4;
6ecc36f7
AM
3548 if (loc == sec->contents)
3549 break;
db4677b8
AM
3550 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3551 }
01e1a5bc 3552 loc -= 4;
6ecc36f7
AM
3553 if (loc == sec->contents)
3554 break;
db4677b8 3555 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3556 }
945906ff 3557 elt = elf_next_in_group (elt);
9dce4196
AM
3558 if (elt == first)
3559 break;
dbb410c3
AM
3560 }
3561
6ecc36f7
AM
3562 /* We should always get here with loc == sec->contents + 4, but it is
3563 possible to craft bogus SHT_GROUP sections that will cause segfaults
3564 in objcopy without checking loc here and in the loop above. */
3565 if (loc == sec->contents)
3566 BFD_ASSERT (0);
3567 else
7744e327 3568 {
6ecc36f7
AM
3569 loc -= 4;
3570 if (loc != sec->contents)
3571 {
3572 BFD_ASSERT (0);
3573 memset (sec->contents + 4, 0, loc - sec->contents);
3574 loc = sec->contents;
3575 }
7744e327 3576 }
dbb410c3 3577
9dce4196 3578 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3579}
3580
bce964aa
AM
3581/* Given NAME, the name of a relocation section stripped of its
3582 .rel/.rela prefix, return the section in ABFD to which the
3583 relocations apply. */
bd53a53a
L
3584
3585asection *
bce964aa
AM
3586_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3587{
3588 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3589 section likely apply to .got.plt or .got section. */
3590 if (get_elf_backend_data (abfd)->want_got_plt
3591 && strcmp (name, ".plt") == 0)
3592 {
3593 asection *sec;
3594
3595 name = ".got.plt";
3596 sec = bfd_get_section_by_name (abfd, name);
3597 if (sec != NULL)
3598 return sec;
3599 name = ".got";
3600 }
3601
3602 return bfd_get_section_by_name (abfd, name);
3603}
3604
3605/* Return the section to which RELOC_SEC applies. */
3606
3607static asection *
3608elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3609{
3610 const char *name;
3611 unsigned int type;
3612 bfd *abfd;
bce964aa 3613 const struct elf_backend_data *bed;
bd53a53a
L
3614
3615 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3616 if (type != SHT_REL && type != SHT_RELA)
3617 return NULL;
3618
3619 /* We look up the section the relocs apply to by name. */
3620 name = reloc_sec->name;
3f3328b8 3621 if (!startswith (name, ".rel"))
bce964aa
AM
3622 return NULL;
3623 name += 4;
3624 if (type == SHT_RELA && *name++ != 'a')
3625 return NULL;
bd53a53a 3626
bd53a53a 3627 abfd = reloc_sec->owner;
bce964aa
AM
3628 bed = get_elf_backend_data (abfd);
3629 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3630}
3631
252b5132
RH
3632/* Assign all ELF section numbers. The dummy first section is handled here
3633 too. The link/info pointers for the standard section types are filled
67411cbf 3634 in here too, while we're at it. LINK_INFO will be 0 when arriving
2ba2f096 3635 here for gas, objcopy, and when using the generic ELF linker. */
252b5132 3636
0a1b45a2 3637static bool
da9f89d4 3638assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3639{
3640 struct elf_obj_tdata *t = elf_tdata (abfd);
3641 asection *sec;
3e19fb8f 3642 unsigned int section_number;
252b5132 3643 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3644 struct bfd_elf_section_data *d;
0a1b45a2 3645 bool need_symtab;
446f7ed5 3646 size_t amt;
252b5132
RH
3647
3648 section_number = 1;
3649
2b0f7ef9
JJ
3650 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3651
da9f89d4 3652 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3653 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3654 {
ef53be89 3655 size_t reloc_count = 0;
14f2c699 3656
da9f89d4 3657 /* Put SHT_GROUP sections first. */
04dd1667 3658 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3659 {
5daa8fe7 3660 d = elf_section_data (sec);
da9f89d4
L
3661
3662 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3663 {
5daa8fe7 3664 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3665 {
3666 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3667 bfd_section_list_remove (abfd, sec);
da9f89d4 3668 abfd->section_count--;
da9f89d4 3669 }
08a40648 3670 else
4fbb74a6 3671 d->this_idx = section_number++;
da9f89d4 3672 }
14f2c699
L
3673
3674 /* Count relocations. */
3675 reloc_count += sec->reloc_count;
47cc2cf5 3676 }
14f2c699 3677
2ba2f096 3678 /* Set/clear HAS_RELOC depending on whether there are relocations. */
14f2c699
L
3679 if (reloc_count == 0)
3680 abfd->flags &= ~HAS_RELOC;
2ba2f096
JB
3681 else
3682 abfd->flags |= HAS_RELOC;
47cc2cf5
PB
3683 }
3684
3685 for (sec = abfd->sections; sec; sec = sec->next)
3686 {
3687 d = elf_section_data (sec);
3688
3689 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3690 d->this_idx = section_number++;
3e19fb8f
L
3691 if (d->this_hdr.sh_name != (unsigned int) -1)
3692 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3693 if (d->rel.hdr)
2b0f7ef9 3694 {
d4730f92 3695 d->rel.idx = section_number++;
3e19fb8f
L
3696 if (d->rel.hdr->sh_name != (unsigned int) -1)
3697 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3698 }
d4730f92
BS
3699 else
3700 d->rel.idx = 0;
23bc299b 3701
d4730f92 3702 if (d->rela.hdr)
2b0f7ef9 3703 {
d4730f92 3704 d->rela.idx = section_number++;
3e19fb8f
L
3705 if (d->rela.hdr->sh_name != (unsigned int) -1)
3706 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3707 }
23bc299b 3708 else
d4730f92 3709 d->rela.idx = 0;
252b5132
RH
3710 }
3711
3516e984 3712 need_symtab = (bfd_get_symcount (abfd) > 0
b6d1f70c
AM
3713 || (link_info == NULL
3714 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3715 == HAS_RELOC)));
3516e984 3716 if (need_symtab)
252b5132 3717 {
12bd6957 3718 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3719 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3720 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3721 {
7a6e0d89 3722 elf_section_list *entry;
6a40cf0c
NC
3723
3724 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3725
7a6e0d89 3726 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3727 entry->ndx = section_number++;
3728 elf_symtab_shndx_list (abfd) = entry;
3729 entry->hdr.sh_name
9ad5cbcf 3730 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3731 ".symtab_shndx", false);
6a40cf0c 3732 if (entry->hdr.sh_name == (unsigned int) -1)
0a1b45a2 3733 return false;
9ad5cbcf 3734 }
12bd6957 3735 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3736 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3737 }
3738
dd905818
NC
3739 elf_shstrtab_sec (abfd) = section_number++;
3740 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3741 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3742
1c52a645
L
3743 if (section_number >= SHN_LORESERVE)
3744 {
695344c0 3745 /* xgettext:c-format */
871b3ab2 3746 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645 3747 abfd, section_number);
0a1b45a2 3748 return false;
1c52a645
L
3749 }
3750
9ad5cbcf 3751 elf_numsections (abfd) = section_number;
252b5132
RH
3752 elf_elfheader (abfd)->e_shnum = section_number;
3753
3754 /* Set up the list of section header pointers, in agreement with the
3755 indices. */
446f7ed5
AM
3756 amt = section_number * sizeof (Elf_Internal_Shdr *);
3757 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3758 if (i_shdrp == NULL)
0a1b45a2 3759 return false;
252b5132 3760
a50b1753 3761 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3762 sizeof (Elf_Internal_Shdr));
252b5132
RH
3763 if (i_shdrp[0] == NULL)
3764 {
3765 bfd_release (abfd, i_shdrp);
0a1b45a2 3766 return false;
252b5132 3767 }
252b5132
RH
3768
3769 elf_elfsections (abfd) = i_shdrp;
3770
12bd6957 3771 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3772 if (need_symtab)
252b5132 3773 {
12bd6957 3774 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3775 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3776 {
6a40cf0c
NC
3777 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3778 BFD_ASSERT (entry != NULL);
3779 i_shdrp[entry->ndx] = & entry->hdr;
3780 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3781 }
12bd6957
AM
3782 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3783 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3784 }
38ce5b11 3785
252b5132
RH
3786 for (sec = abfd->sections; sec; sec = sec->next)
3787 {
252b5132 3788 asection *s;
252b5132 3789
91d6fa6a
NC
3790 d = elf_section_data (sec);
3791
252b5132 3792 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3793 if (d->rel.idx != 0)
3794 i_shdrp[d->rel.idx] = d->rel.hdr;
3795 if (d->rela.idx != 0)
3796 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3797
3798 /* Fill in the sh_link and sh_info fields while we're at it. */
3799
3800 /* sh_link of a reloc section is the section index of the symbol
3801 table. sh_info is the section index of the section to which
3802 the relocation entries apply. */
d4730f92 3803 if (d->rel.idx != 0)
252b5132 3804 {
12bd6957 3805 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3806 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3807 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3808 }
d4730f92 3809 if (d->rela.idx != 0)
23bc299b 3810 {
12bd6957 3811 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3812 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3813 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3814 }
252b5132 3815
38ce5b11
L
3816 /* We need to set up sh_link for SHF_LINK_ORDER. */
3817 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3818 {
3819 s = elf_linked_to_section (sec);
b71702f1
NC
3820 /* We can now have a NULL linked section pointer.
3821 This happens when the sh_link field is 0, which is done
3822 when a linked to section is discarded but the linking
3823 section has been retained for some reason. */
38ce5b11 3824 if (s)
38ce5b11 3825 {
67411cbf
AM
3826 /* Check discarded linkonce section. */
3827 if (discarded_section (s))
38ce5b11 3828 {
67411cbf
AM
3829 asection *kept;
3830 _bfd_error_handler
3831 /* xgettext:c-format */
3832 (_("%pB: sh_link of section `%pA' points to"
3833 " discarded section `%pA' of `%pB'"),
3834 abfd, d->this_hdr.bfd_section, s, s->owner);
3835 /* Point to the kept section if it has the same
3836 size as the discarded one. */
3837 kept = _bfd_elf_check_kept_section (s, link_info);
3838 if (kept == NULL)
f2876037 3839 {
f2876037 3840 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3841 return false;
f2876037 3842 }
67411cbf
AM
3843 s = kept;
3844 }
3845 /* Handle objcopy. */
3846 else if (s->output_section == NULL)
3847 {
3848 _bfd_error_handler
3849 /* xgettext:c-format */
3850 (_("%pB: sh_link of section `%pA' points to"
3851 " removed section `%pA' of `%pB'"),
3852 abfd, d->this_hdr.bfd_section, s, s->owner);
3853 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3854 return false;
f2876037 3855 }
67411cbf 3856 s = s->output_section;
ccd2ec6a
L
3857 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3858 }
38ce5b11
L
3859 }
3860
252b5132
RH
3861 switch (d->this_hdr.sh_type)
3862 {
3863 case SHT_REL:
3864 case SHT_RELA:
6b958fe3
AM
3865 /* sh_link is the section index of the symbol table.
3866 sh_info is the section index of the section to which the
3867 relocation entries apply. */
3868 if (d->this_hdr.sh_link == 0)
b6d1f70c 3869 {
6b958fe3
AM
3870 /* FIXME maybe: If this is a reloc section which we are
3871 treating as a normal section then we likely should
3872 not be assuming its sh_link is .dynsym or .symtab. */
3873 if ((sec->flags & SEC_ALLOC) != 0)
3874 {
3875 s = bfd_get_section_by_name (abfd, ".dynsym");
3876 if (s != NULL)
3877 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3878 }
3879 else
3880 d->this_hdr.sh_link = elf_onesymtab (abfd);
b6d1f70c 3881 }
252b5132 3882
bce964aa 3883 s = elf_get_reloc_section (sec);
252b5132 3884 if (s != NULL)
9ef5d938
L
3885 {
3886 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3887 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3888 }
252b5132
RH
3889 break;
3890
3891 case SHT_STRTAB:
3892 /* We assume that a section named .stab*str is a stabs
3893 string section. We look for a section with the same name
3894 but without the trailing ``str'', and set its sh_link
3895 field to point to this section. */
08dedd66 3896 if (startswith (sec->name, ".stab")
252b5132
RH
3897 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3898 {
3899 size_t len;
3900 char *alc;
3901
3902 len = strlen (sec->name);
a50b1753 3903 alc = (char *) bfd_malloc (len - 2);
252b5132 3904 if (alc == NULL)
0a1b45a2 3905 return false;
d4c88bbb 3906 memcpy (alc, sec->name, len - 3);
252b5132
RH
3907 alc[len - 3] = '\0';
3908 s = bfd_get_section_by_name (abfd, alc);
3909 free (alc);
3910 if (s != NULL)
3911 {
3912 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3913
3914 /* This is a .stab section. */
34ca5531 3915 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3916 }
3917 }
3918 break;
3919
3920 case SHT_DYNAMIC:
3921 case SHT_DYNSYM:
3922 case SHT_GNU_verneed:
3923 case SHT_GNU_verdef:
3924 /* sh_link is the section header index of the string table
3925 used for the dynamic entries, or the symbol table, or the
3926 version strings. */
3927 s = bfd_get_section_by_name (abfd, ".dynstr");
3928 if (s != NULL)
3929 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3930 break;
3931
7f1204bb
JJ
3932 case SHT_GNU_LIBLIST:
3933 /* sh_link is the section header index of the prelink library
08a40648
AM
3934 list used for the dynamic entries, or the symbol table, or
3935 the version strings. */
b6d1f70c
AM
3936 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3937 ? ".dynstr" : ".gnu.libstr"));
7f1204bb
JJ
3938 if (s != NULL)
3939 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3940 break;
3941
252b5132 3942 case SHT_HASH:
fdc90cb4 3943 case SHT_GNU_HASH:
252b5132
RH
3944 case SHT_GNU_versym:
3945 /* sh_link is the section header index of the symbol table
3946 this hash table or version table is for. */
3947 s = bfd_get_section_by_name (abfd, ".dynsym");
3948 if (s != NULL)
3949 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3950 break;
dbb410c3
AM
3951
3952 case SHT_GROUP:
12bd6957 3953 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3954 }
3955 }
3956
3e19fb8f
L
3957 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3958 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3959 debug section name from .debug_* to .zdebug_* if needed. */
3960
0a1b45a2 3961 return true;
252b5132
RH
3962}
3963
0a1b45a2 3964static bool
217aa764 3965sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
3966{
3967 /* If the backend has a special mapping, use it. */
9c5bfbb7 3968 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
3969 if (bed->elf_backend_sym_is_global)
3970 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 3971
e47bf690 3972 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
3973 || bfd_is_und_section (bfd_asymbol_section (sym))
3974 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
3975}
3976
76359541
TP
3977/* Filter global symbols of ABFD to include in the import library. All
3978 SYMCOUNT symbols of ABFD can be examined from their pointers in
3979 SYMS. Pointers of symbols to keep should be stored contiguously at
3980 the beginning of that array.
3981
3982 Returns the number of symbols to keep. */
3983
3984unsigned int
3985_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3986 asymbol **syms, long symcount)
3987{
3988 long src_count, dst_count = 0;
3989
3990 for (src_count = 0; src_count < symcount; src_count++)
3991 {
3992 asymbol *sym = syms[src_count];
3993 char *name = (char *) bfd_asymbol_name (sym);
3994 struct bfd_link_hash_entry *h;
3995
3996 if (!sym_is_global (abfd, sym))
3997 continue;
3998
0a1b45a2 3999 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
5df1bc57
AM
4000 if (h == NULL)
4001 continue;
76359541
TP
4002 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4003 continue;
76359541
TP
4004 if (h->linker_def || h->ldscript_def)
4005 continue;
4006
4007 syms[dst_count++] = sym;
4008 }
4009
4010 syms[dst_count] = NULL;
4011
4012 return dst_count;
4013}
4014
5372391b 4015/* Don't output section symbols for sections that are not going to be
c6d8cab4 4016 output, that are duplicates or there is no BFD section. */
5372391b 4017
0a1b45a2 4018static bool
5372391b
AM
4019ignore_section_sym (bfd *abfd, asymbol *sym)
4020{
c6d8cab4
L
4021 elf_symbol_type *type_ptr;
4022
db0c309f 4023 if (sym == NULL)
0a1b45a2 4024 return false;
db0c309f 4025
c6d8cab4 4026 if ((sym->flags & BSF_SECTION_SYM) == 0)
0a1b45a2 4027 return false;
c6d8cab4 4028
d1bcae83
L
4029 /* Ignore the section symbol if it isn't used. */
4030 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
0a1b45a2 4031 return true;
d1bcae83 4032
db0c309f 4033 if (sym->section == NULL)
0a1b45a2 4034 return true;
db0c309f 4035
c1229f84 4036 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4037 return ((type_ptr != NULL
4038 && type_ptr->internal_elf_sym.st_shndx != 0
4039 && bfd_is_abs_section (sym->section))
4040 || !(sym->section->owner == abfd
db0c309f
NC
4041 || (sym->section->output_section != NULL
4042 && sym->section->output_section->owner == abfd
2633a79c
AM
4043 && sym->section->output_offset == 0)
4044 || bfd_is_abs_section (sym->section)));
5372391b
AM
4045}
4046
2633a79c
AM
4047/* Map symbol from it's internal number to the external number, moving
4048 all local symbols to be at the head of the list. */
4049
0a1b45a2 4050static bool
12bd6957 4051elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4052{
dc810e39 4053 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4054 asymbol **syms = bfd_get_outsymbols (abfd);
4055 asymbol **sect_syms;
dc810e39
AM
4056 unsigned int num_locals = 0;
4057 unsigned int num_globals = 0;
4058 unsigned int num_locals2 = 0;
4059 unsigned int num_globals2 = 0;
7292b3ac 4060 unsigned int max_index = 0;
dc810e39 4061 unsigned int idx;
252b5132
RH
4062 asection *asect;
4063 asymbol **new_syms;
446f7ed5 4064 size_t amt;
252b5132
RH
4065
4066#ifdef DEBUG
4067 fprintf (stderr, "elf_map_symbols\n");
4068 fflush (stderr);
4069#endif
4070
252b5132
RH
4071 for (asect = abfd->sections; asect; asect = asect->next)
4072 {
4073 if (max_index < asect->index)
4074 max_index = asect->index;
4075 }
4076
4077 max_index++;
446f7ed5
AM
4078 amt = max_index * sizeof (asymbol *);
4079 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4080 if (sect_syms == NULL)
0a1b45a2 4081 return false;
252b5132 4082 elf_section_syms (abfd) = sect_syms;
4e89ac30 4083 elf_num_section_syms (abfd) = max_index;
252b5132 4084
079e9a2f
AM
4085 /* Init sect_syms entries for any section symbols we have already
4086 decided to output. */
252b5132
RH
4087 for (idx = 0; idx < symcount; idx++)
4088 {
dc810e39 4089 asymbol *sym = syms[idx];
c044fabd 4090
252b5132 4091 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4092 && sym->value == 0
2633a79c
AM
4093 && !ignore_section_sym (abfd, sym)
4094 && !bfd_is_abs_section (sym->section))
252b5132 4095 {
5372391b 4096 asection *sec = sym->section;
252b5132 4097
5372391b
AM
4098 if (sec->owner != abfd)
4099 sec = sec->output_section;
252b5132 4100
5372391b 4101 sect_syms[sec->index] = syms[idx];
252b5132
RH
4102 }
4103 }
4104
252b5132
RH
4105 /* Classify all of the symbols. */
4106 for (idx = 0; idx < symcount; idx++)
4107 {
2633a79c 4108 if (sym_is_global (abfd, syms[idx]))
252b5132 4109 num_globals++;
2633a79c
AM
4110 else if (!ignore_section_sym (abfd, syms[idx]))
4111 num_locals++;
252b5132 4112 }
079e9a2f 4113
5372391b 4114 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4115 sections will already have a section symbol in outsymbols, but
4116 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4117 at least in that case. */
252b5132
RH
4118 for (asect = abfd->sections; asect; asect = asect->next)
4119 {
d1bcae83
L
4120 asymbol *sym = asect->symbol;
4121 /* Don't include ignored section symbols. */
4122 if (!ignore_section_sym (abfd, sym)
4123 && sect_syms[asect->index] == NULL)
252b5132 4124 {
079e9a2f 4125 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4126 num_locals++;
4127 else
4128 num_globals++;
252b5132
RH
4129 }
4130 }
4131
4132 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4133 amt = (num_locals + num_globals) * sizeof (asymbol *);
4134 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4135 if (new_syms == NULL)
0a1b45a2 4136 return false;
252b5132
RH
4137
4138 for (idx = 0; idx < symcount; idx++)
4139 {
4140 asymbol *sym = syms[idx];
dc810e39 4141 unsigned int i;
252b5132 4142
2633a79c
AM
4143 if (sym_is_global (abfd, sym))
4144 i = num_locals + num_globals2++;
d1bcae83 4145 /* Don't include ignored section symbols. */
2633a79c 4146 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4147 i = num_locals2++;
4148 else
2633a79c 4149 continue;
252b5132
RH
4150 new_syms[i] = sym;
4151 sym->udata.i = i + 1;
4152 }
4153 for (asect = abfd->sections; asect; asect = asect->next)
4154 {
d1bcae83
L
4155 asymbol *sym = asect->symbol;
4156 if (!ignore_section_sym (abfd, sym)
4157 && sect_syms[asect->index] == NULL)
252b5132 4158 {
dc810e39 4159 unsigned int i;
252b5132 4160
079e9a2f 4161 sect_syms[asect->index] = sym;
252b5132
RH
4162 if (!sym_is_global (abfd, sym))
4163 i = num_locals2++;
4164 else
4165 i = num_locals + num_globals2++;
4166 new_syms[i] = sym;
4167 sym->udata.i = i + 1;
4168 }
4169 }
4170
4171 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4172
12bd6957 4173 *pnum_locals = num_locals;
0a1b45a2 4174 return true;
252b5132
RH
4175}
4176
4177/* Align to the maximum file alignment that could be required for any
4178 ELF data structure. */
4179
268b6b39 4180static inline file_ptr
217aa764 4181align_file_position (file_ptr off, int align)
252b5132
RH
4182{
4183 return (off + align - 1) & ~(align - 1);
4184}
4185
4186/* Assign a file position to a section, optionally aligning to the
4187 required section alignment. */
4188
217aa764
AM
4189file_ptr
4190_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4191 file_ptr offset,
0a1b45a2 4192 bool align)
252b5132 4193{
72de5009 4194 if (align && i_shdrp->sh_addralign > 1)
1f9b1a84 4195 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
252b5132
RH
4196 i_shdrp->sh_offset = offset;
4197 if (i_shdrp->bfd_section != NULL)
4198 i_shdrp->bfd_section->filepos = offset;
4199 if (i_shdrp->sh_type != SHT_NOBITS)
4200 offset += i_shdrp->sh_size;
4201 return offset;
4202}
4203
4204/* Compute the file positions we are going to put the sections at, and
4205 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4206 is not NULL, this is being called by the ELF backend linker. */
4207
0a1b45a2 4208bool
217aa764
AM
4209_bfd_elf_compute_section_file_positions (bfd *abfd,
4210 struct bfd_link_info *link_info)
252b5132 4211{
9c5bfbb7 4212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4213 struct fake_section_arg fsargs;
0a1b45a2 4214 bool failed;
ef10c3ac 4215 struct elf_strtab_hash *strtab = NULL;
252b5132 4216 Elf_Internal_Shdr *shstrtab_hdr;
0a1b45a2 4217 bool need_symtab;
252b5132
RH
4218
4219 if (abfd->output_has_begun)
0a1b45a2 4220 return true;
252b5132
RH
4221
4222 /* Do any elf backend specific processing first. */
4223 if (bed->elf_backend_begin_write_processing)
4224 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4225
ed7e9d0b 4226 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
0a1b45a2 4227 return false;
252b5132 4228
0a1b45a2 4229 fsargs.failed = false;
d4730f92
BS
4230 fsargs.link_info = link_info;
4231 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4232 if (fsargs.failed)
0a1b45a2 4233 return false;
252b5132 4234
da9f89d4 4235 if (!assign_section_numbers (abfd, link_info))
0a1b45a2 4236 return false;
252b5132
RH
4237
4238 /* The backend linker builds symbol table information itself. */
3516e984
L
4239 need_symtab = (link_info == NULL
4240 && (bfd_get_symcount (abfd) > 0
4241 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4242 == HAS_RELOC)));
4243 if (need_symtab)
252b5132
RH
4244 {
4245 /* Non-zero if doing a relocatable link. */
4246 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4247
3d16b64e 4248 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
0a1b45a2 4249 return false;
252b5132
RH
4250 }
4251
0a1b45a2 4252 failed = false;
1126897b 4253 if (link_info == NULL)
dbb410c3 4254 {
1126897b 4255 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4256 if (failed)
48375c36 4257 goto err_free_strtab;
dbb410c3
AM
4258 }
4259
252b5132 4260 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4261 /* sh_name was set in init_file_header. */
252b5132 4262 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4263 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4264 shstrtab_hdr->sh_addr = 0;
946748d5 4265 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4266 shstrtab_hdr->sh_entsize = 0;
4267 shstrtab_hdr->sh_link = 0;
4268 shstrtab_hdr->sh_info = 0;
3e19fb8f 4269 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4270 shstrtab_hdr->sh_addralign = 1;
4271
c84fca4d 4272 if (!assign_file_positions_except_relocs (abfd, link_info))
48375c36 4273 goto err_free_strtab;
252b5132 4274
48375c36 4275 if (strtab != NULL)
252b5132
RH
4276 {
4277 file_ptr off;
4278 Elf_Internal_Shdr *hdr;
4279
12bd6957 4280 off = elf_next_file_pos (abfd);
252b5132 4281
6a40cf0c 4282 hdr = & elf_symtab_hdr (abfd);
0a1b45a2 4283 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4284
6a40cf0c
NC
4285 if (elf_symtab_shndx_list (abfd) != NULL)
4286 {
4287 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4288 if (hdr->sh_size != 0)
0a1b45a2 4289 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6a40cf0c
NC
4290 /* FIXME: What about other symtab_shndx sections in the list ? */
4291 }
9ad5cbcf 4292
252b5132 4293 hdr = &elf_tdata (abfd)->strtab_hdr;
0a1b45a2 4294 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4295
12bd6957 4296 elf_next_file_pos (abfd) = off;
252b5132
RH
4297
4298 /* Now that we know where the .strtab section goes, write it
08a40648 4299 out. */
252b5132 4300 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4301 || ! _bfd_elf_strtab_emit (abfd, strtab))
48375c36 4302 goto err_free_strtab;
ef10c3ac 4303 _bfd_elf_strtab_free (strtab);
252b5132
RH
4304 }
4305
0a1b45a2 4306 abfd->output_has_begun = true;
0a1b45a2 4307 return true;
48375c36
AM
4308
4309 err_free_strtab:
4310 if (strtab != NULL)
4311 _bfd_elf_strtab_free (strtab);
4312 return false;
252b5132
RH
4313}
4314
8df52eee
AM
4315/* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4316 function effectively returns whether --eh-frame-hdr is given on the
4317 command line. After size_dynamic_sections the result reflects
4318 whether .eh_frame_hdr will actually be output (sizing isn't done
4319 until ldemul_after_allocation). */
4320
4321static asection *
4322elf_eh_frame_hdr (const struct bfd_link_info *info)
4323{
4324 if (info != NULL && is_elf_hash_table (info->hash))
4325 return elf_hash_table (info)->eh_info.hdr_sec;
4326 return NULL;
4327}
4328
8ded5a0f
AM
4329/* Make an initial estimate of the size of the program header. If we
4330 get the number wrong here, we'll redo section placement. */
4331
4332static bfd_size_type
4333get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4334{
4335 size_t segs;
4336 asection *s;
2b05f1b7 4337 const struct elf_backend_data *bed;
8ded5a0f
AM
4338
4339 /* Assume we will need exactly two PT_LOAD segments: one for text
4340 and one for data. */
4341 segs = 2;
4342
4343 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4344 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4345 {
4346 /* If we have a loadable interpreter section, we need a
4347 PT_INTERP segment. In this case, assume we also need a
4348 PT_PHDR segment, although that may not be true for all
4349 targets. */
e9a38e0f 4350 segs += 2;
8ded5a0f
AM
4351 }
4352
4353 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4354 {
4355 /* We need a PT_DYNAMIC segment. */
4356 ++segs;
f210dcff 4357 }
08a40648 4358
ceae84aa 4359 if (info != NULL && info->relro)
f210dcff
L
4360 {
4361 /* We need a PT_GNU_RELRO segment. */
4362 ++segs;
8ded5a0f
AM
4363 }
4364
8df52eee 4365 if (elf_eh_frame_hdr (info))
8ded5a0f
AM
4366 {
4367 /* We need a PT_GNU_EH_FRAME segment. */
4368 ++segs;
4369 }
4370
12bd6957 4371 if (elf_stack_flags (abfd))
8ded5a0f 4372 {
2b05f1b7
L
4373 /* We need a PT_GNU_STACK segment. */
4374 ++segs;
4375 }
94b11780 4376
cf0e0a0b
IB
4377 if (elf_sframe (abfd))
4378 {
4379 /* We need a PT_GNU_SFRAME segment. */
4380 ++segs;
4381 }
4382
0a59decb
L
4383 s = bfd_get_section_by_name (abfd,
4384 NOTE_GNU_PROPERTY_SECTION_NAME);
4385 if (s != NULL && s->size != 0)
4386 {
4387 /* We need a PT_GNU_PROPERTY segment. */
4388 ++segs;
4389 }
4390
2b05f1b7
L
4391 for (s = abfd->sections; s != NULL; s = s->next)
4392 {
8ded5a0f 4393 if ((s->flags & SEC_LOAD) != 0
23e463ed 4394 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4395 {
23e463ed 4396 unsigned int alignment_power;
8ded5a0f
AM
4397 /* We need a PT_NOTE segment. */
4398 ++segs;
23e463ed
L
4399 /* Try to create just one PT_NOTE segment for all adjacent
4400 loadable SHT_NOTE sections. gABI requires that within a
4401 PT_NOTE segment (and also inside of each SHT_NOTE section)
4402 each note should have the same alignment. So we check
4403 whether the sections are correctly aligned. */
4404 alignment_power = s->alignment_power;
4405 while (s->next != NULL
4406 && s->next->alignment_power == alignment_power
4407 && (s->next->flags & SEC_LOAD) != 0
4408 && elf_section_type (s->next) == SHT_NOTE)
4409 s = s->next;
8ded5a0f
AM
4410 }
4411 }
4412
4413 for (s = abfd->sections; s != NULL; s = s->next)
4414 {
4415 if (s->flags & SEC_THREAD_LOCAL)
4416 {
4417 /* We need a PT_TLS segment. */
4418 ++segs;
4419 break;
4420 }
4421 }
4422
2b05f1b7 4423 bed = get_elf_backend_data (abfd);
a91e1603 4424
df3a023b
AM
4425 if ((abfd->flags & D_PAGED) != 0
4426 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4427 {
4428 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4429 bfd_vma commonpagesize;
4430 unsigned int page_align_power;
4431
4432 if (info != NULL)
4433 commonpagesize = info->commonpagesize;
4434 else
4435 commonpagesize = bed->commonpagesize;
4436 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4437 for (s = abfd->sections; s != NULL; s = s->next)
4438 if (elf_section_flags (s) & SHF_GNU_MBIND)
4439 {
4440 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4441 {
4442 _bfd_error_handler
4443 /* xgettext:c-format */
4444 (_("%pB: GNU_MBIND section `%pA' has invalid "
4445 "sh_info field: %d"),
4446 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4447 continue;
4448 }
4449 /* Align mbind section to page size. */
4450 if (s->alignment_power < page_align_power)
4451 s->alignment_power = page_align_power;
4452 segs ++;
4453 }
4454 }
4455
4456 /* Let the backend count up any program headers it might need. */
4457 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4458 {
4459 int a;
4460
4461 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4462 if (a == -1)
4463 abort ();
4464 segs += a;
4465 }
4466
4467 return segs * bed->s->sizeof_phdr;
4468}
4469
2ea37f1c
NC
4470/* Find the segment that contains the output_section of section. */
4471
4472Elf_Internal_Phdr *
4473_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4474{
4475 struct elf_segment_map *m;
4476 Elf_Internal_Phdr *p;
4477
12bd6957 4478 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4479 m != NULL;
4480 m = m->next, p++)
4481 {
4482 int i;
4483
4484 for (i = m->count - 1; i >= 0; i--)
4485 if (m->sections[i] == section)
4486 return p;
4487 }
4488
4489 return NULL;
4490}
4491
252b5132
RH
4492/* Create a mapping from a set of sections to a program segment. */
4493
217aa764
AM
4494static struct elf_segment_map *
4495make_mapping (bfd *abfd,
4496 asection **sections,
4497 unsigned int from,
4498 unsigned int to,
0a1b45a2 4499 bool phdr)
252b5132
RH
4500{
4501 struct elf_segment_map *m;
4502 unsigned int i;
4503 asection **hdrpp;
986f0783 4504 size_t amt;
252b5132 4505
00bee008
AM
4506 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4507 amt += (to - from) * sizeof (asection *);
a50b1753 4508 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4509 if (m == NULL)
4510 return NULL;
4511 m->next = NULL;
4512 m->p_type = PT_LOAD;
4513 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4514 m->sections[i - from] = *hdrpp;
4515 m->count = to - from;
4516
4517 if (from == 0 && phdr)
4518 {
4519 /* Include the headers in the first PT_LOAD segment. */
4520 m->includes_filehdr = 1;
4521 m->includes_phdrs = 1;
4522 }
4523
4524 return m;
4525}
4526
229fcec5
MM
4527/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4528 on failure. */
4529
4530struct elf_segment_map *
4531_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4532{
4533 struct elf_segment_map *m;
4534
a50b1753 4535 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4536 sizeof (struct elf_segment_map));
229fcec5
MM
4537 if (m == NULL)
4538 return NULL;
4539 m->next = NULL;
4540 m->p_type = PT_DYNAMIC;
4541 m->count = 1;
4542 m->sections[0] = dynsec;
08a40648 4543
229fcec5
MM
4544 return m;
4545}
4546
8ded5a0f 4547/* Possibly add or remove segments from the segment map. */
252b5132 4548
0a1b45a2 4549static bool
3dea8fca
AM
4550elf_modify_segment_map (bfd *abfd,
4551 struct bfd_link_info *info,
0a1b45a2 4552 bool remove_empty_load)
252b5132 4553{
252e386e 4554 struct elf_segment_map **m;
8ded5a0f 4555 const struct elf_backend_data *bed;
252b5132 4556
8ded5a0f
AM
4557 /* The placement algorithm assumes that non allocated sections are
4558 not in PT_LOAD segments. We ensure this here by removing such
4559 sections from the segment map. We also remove excluded
252e386e
AM
4560 sections. Finally, any PT_LOAD segment without sections is
4561 removed. */
12bd6957 4562 m = &elf_seg_map (abfd);
252e386e 4563 while (*m)
8ded5a0f
AM
4564 {
4565 unsigned int i, new_count;
252b5132 4566
252e386e 4567 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4568 {
252e386e
AM
4569 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4570 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4571 || (*m)->p_type != PT_LOAD))
8ded5a0f 4572 {
252e386e
AM
4573 (*m)->sections[new_count] = (*m)->sections[i];
4574 new_count++;
8ded5a0f
AM
4575 }
4576 }
252e386e 4577 (*m)->count = new_count;
252b5132 4578
1a9ccd70
NC
4579 if (remove_empty_load
4580 && (*m)->p_type == PT_LOAD
4581 && (*m)->count == 0
4582 && !(*m)->includes_phdrs)
252e386e
AM
4583 *m = (*m)->next;
4584 else
4585 m = &(*m)->next;
8ded5a0f 4586 }
252b5132 4587
8ded5a0f
AM
4588 bed = get_elf_backend_data (abfd);
4589 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4590 {
252e386e 4591 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
0a1b45a2 4592 return false;
252b5132 4593 }
252b5132 4594
0a1b45a2 4595 return true;
8ded5a0f 4596}
252b5132 4597
dbc88fc1
AM
4598#define IS_TBSS(s) \
4599 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4600
e2cbf4df
L
4601/* Set up a mapping from BFD sections to program segments. Update
4602 NEED_LAYOUT if the section layout is changed. */
252b5132 4603
0a1b45a2 4604bool
e2cbf4df
L
4605_bfd_elf_map_sections_to_segments (bfd *abfd,
4606 struct bfd_link_info *info,
23cc1de5 4607 bool *need_layout)
8ded5a0f
AM
4608{
4609 unsigned int count;
4610 struct elf_segment_map *m;
4611 asection **sections = NULL;
4612 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 4613 bool no_user_phdrs;
252b5132 4614
12bd6957 4615 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4616
4617 if (info != NULL)
23cc1de5
L
4618 {
4619 info->user_phdrs = !no_user_phdrs;
4620
4621 /* Size the relative relocations if DT_RELR is enabled. */
4622 if (info->enable_dt_relr
4623 && need_layout != NULL
4624 && bed->size_relative_relocs
4625 && !bed->size_relative_relocs (info, need_layout))
4626 info->callbacks->einfo
4627 (_("%F%P: failed to size relative relocations\n"));
4628 }
d324f6d6 4629
3dea8fca 4630 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4631 {
8ded5a0f
AM
4632 asection *s;
4633 unsigned int i;
4634 struct elf_segment_map *mfirst;
4635 struct elf_segment_map **pm;
4636 asection *last_hdr;
4637 bfd_vma last_size;
00bee008 4638 unsigned int hdr_index;
8ded5a0f
AM
4639 bfd_vma maxpagesize;
4640 asection **hdrpp;
0a1b45a2
AM
4641 bool phdr_in_segment;
4642 bool writable;
4643 bool executable;
446f7ed5 4644 unsigned int tls_count = 0;
8ded5a0f 4645 asection *first_tls = NULL;
a91e1603 4646 asection *first_mbind = NULL;
8ded5a0f 4647 asection *dynsec, *eh_frame_hdr;
cf0e0a0b 4648 asection *sframe;
446f7ed5 4649 size_t amt;
66631823
CE
4650 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4651 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4652 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4653
8ded5a0f 4654 /* Select the allocated sections, and sort them. */
252b5132 4655
446f7ed5
AM
4656 amt = bfd_count_sections (abfd) * sizeof (asection *);
4657 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4658 if (sections == NULL)
252b5132 4659 goto error_return;
252b5132 4660
8d06853e
AM
4661 /* Calculate top address, avoiding undefined behaviour of shift
4662 left operator when shift count is equal to size of type
4663 being shifted. */
4664 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4665 addr_mask = (addr_mask << 1) + 1;
4666
8ded5a0f
AM
4667 i = 0;
4668 for (s = abfd->sections; s != NULL; s = s->next)
4669 {
4670 if ((s->flags & SEC_ALLOC) != 0)
4671 {
48db3297
AM
4672 /* target_index is unused until bfd_elf_final_link
4673 starts output of section symbols. Use it to make
4674 qsort stable. */
4675 s->target_index = i;
8ded5a0f
AM
4676 sections[i] = s;
4677 ++i;
8d06853e 4678 /* A wrapping section potentially clashes with header. */
66631823
CE
4679 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4680 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4681 }
4682 }
4683 BFD_ASSERT (i <= bfd_count_sections (abfd));
4684 count = i;
252b5132 4685
8ded5a0f 4686 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4687
64029e93
AM
4688 phdr_size = elf_program_header_size (abfd);
4689 if (phdr_size == (bfd_size_type) -1)
4690 phdr_size = get_program_header_size (abfd, info);
4691 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4692 /* phdr_size is compared to LMA values which are in bytes. */
4693 phdr_size /= opb;
c410035d
AM
4694 if (info != NULL)
4695 maxpagesize = info->maxpagesize;
4696 else
4697 maxpagesize = bed->maxpagesize;
64029e93
AM
4698 if (maxpagesize == 0)
4699 maxpagesize = 1;
4700 phdr_in_segment = info != NULL && info->load_phdrs;
4701 if (count != 0
4702 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4703 >= (phdr_size & (maxpagesize - 1))))
4704 /* For compatibility with old scripts that may not be using
4705 SIZEOF_HEADERS, add headers when it looks like space has
4706 been left for them. */
0a1b45a2 4707 phdr_in_segment = true;
252b5132 4708
64029e93 4709 /* Build the mapping. */
8ded5a0f
AM
4710 mfirst = NULL;
4711 pm = &mfirst;
252b5132 4712
8ded5a0f
AM
4713 /* If we have a .interp section, then create a PT_PHDR segment for
4714 the program headers and a PT_INTERP segment for the .interp
4715 section. */
4716 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4717 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4718 {
4719 amt = sizeof (struct elf_segment_map);
a50b1753 4720 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4721 if (m == NULL)
4722 goto error_return;
4723 m->next = NULL;
4724 m->p_type = PT_PHDR;
f882209d 4725 m->p_flags = PF_R;
8ded5a0f
AM
4726 m->p_flags_valid = 1;
4727 m->includes_phdrs = 1;
0a1b45a2 4728 phdr_in_segment = true;
8ded5a0f
AM
4729 *pm = m;
4730 pm = &m->next;
252b5132 4731
8ded5a0f 4732 amt = sizeof (struct elf_segment_map);
a50b1753 4733 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4734 if (m == NULL)
4735 goto error_return;
4736 m->next = NULL;
4737 m->p_type = PT_INTERP;
4738 m->count = 1;
4739 m->sections[0] = s;
4740
4741 *pm = m;
4742 pm = &m->next;
252b5132 4743 }
8ded5a0f
AM
4744
4745 /* Look through the sections. We put sections in the same program
4746 segment when the start of the second section can be placed within
4747 a few bytes of the end of the first section. */
4748 last_hdr = NULL;
4749 last_size = 0;
00bee008 4750 hdr_index = 0;
0a1b45a2
AM
4751 writable = false;
4752 executable = false;
8ded5a0f
AM
4753 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4754 if (dynsec != NULL
4755 && (dynsec->flags & SEC_LOAD) == 0)
4756 dynsec = NULL;
4757
64029e93 4758 if ((abfd->flags & D_PAGED) == 0)
0a1b45a2 4759 phdr_in_segment = false;
64029e93 4760
8ded5a0f
AM
4761 /* Deal with -Ttext or something similar such that the first section
4762 is not adjacent to the program headers. This is an
4763 approximation, since at this point we don't know exactly how many
4764 program headers we will need. */
64029e93 4765 if (phdr_in_segment && count > 0)
252b5132 4766 {
66631823 4767 bfd_vma phdr_lma; /* Bytes. */
0a1b45a2 4768 bool separate_phdr = false;
64029e93
AM
4769
4770 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4771 if (info != NULL
4772 && info->separate_code
4773 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4774 {
64029e93
AM
4775 /* If data sections should be separate from code and
4776 thus not executable, and the first section is
4777 executable then put the file and program headers in
4778 their own PT_LOAD. */
0a1b45a2 4779 separate_phdr = true;
64029e93
AM
4780 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4781 == (sections[0]->lma & addr_mask & -maxpagesize)))
4782 {
4783 /* The file and program headers are currently on the
4784 same page as the first section. Put them on the
4785 previous page if we can. */
4786 if (phdr_lma >= maxpagesize)
4787 phdr_lma -= maxpagesize;
4788 else
0a1b45a2 4789 separate_phdr = false;
64029e93
AM
4790 }
4791 }
4792 if ((sections[0]->lma & addr_mask) < phdr_lma
4793 || (sections[0]->lma & addr_mask) < phdr_size)
4794 /* If file and program headers would be placed at the end
4795 of memory then it's probably better to omit them. */
0a1b45a2 4796 phdr_in_segment = false;
64029e93
AM
4797 else if (phdr_lma < wrap_to)
4798 /* If a section wraps around to where we'll be placing
4799 file and program headers, then the headers will be
4800 overwritten. */
0a1b45a2 4801 phdr_in_segment = false;
64029e93
AM
4802 else if (separate_phdr)
4803 {
4804 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4805 if (m == NULL)
4806 goto error_return;
66631823 4807 m->p_paddr = phdr_lma * opb;
64029e93
AM
4808 m->p_vaddr_offset
4809 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4810 m->p_paddr_valid = 1;
4811 *pm = m;
4812 pm = &m->next;
0a1b45a2 4813 phdr_in_segment = false;
1a9ccd70 4814 }
252b5132
RH
4815 }
4816
8ded5a0f 4817 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4818 {
8ded5a0f 4819 asection *hdr;
0a1b45a2 4820 bool new_segment;
8ded5a0f
AM
4821
4822 hdr = *hdrpp;
4823
4824 /* See if this section and the last one will fit in the same
4825 segment. */
4826
4827 if (last_hdr == NULL)
4828 {
4829 /* If we don't have a segment yet, then we don't need a new
4830 one (we build the last one after this loop). */
0a1b45a2 4831 new_segment = false;
8ded5a0f
AM
4832 }
4833 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4834 {
4835 /* If this section has a different relation between the
4836 virtual address and the load address, then we need a new
4837 segment. */
0a1b45a2 4838 new_segment = true;
8ded5a0f 4839 }
b5599592
AM
4840 else if (hdr->lma < last_hdr->lma + last_size
4841 || last_hdr->lma + last_size < last_hdr->lma)
4842 {
4843 /* If this section has a load address that makes it overlap
4844 the previous section, then we need a new segment. */
0a1b45a2 4845 new_segment = true;
b5599592 4846 }
76cb3a89
AM
4847 else if ((abfd->flags & D_PAGED) != 0
4848 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4849 == (hdr->lma & -maxpagesize)))
4850 {
4851 /* If we are demand paged then we can't map two disk
4852 pages onto the same memory page. */
0a1b45a2 4853 new_segment = false;
76cb3a89 4854 }
39948a60
NC
4855 /* In the next test we have to be careful when last_hdr->lma is close
4856 to the end of the address space. If the aligned address wraps
4857 around to the start of the address space, then there are no more
4858 pages left in memory and it is OK to assume that the current
4859 section can be included in the current segment. */
76cb3a89
AM
4860 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4861 + maxpagesize > last_hdr->lma)
4862 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4863 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4864 {
4865 /* If putting this section in this segment would force us to
4866 skip a page in the segment, then we need a new segment. */
0a1b45a2 4867 new_segment = true;
8ded5a0f
AM
4868 }
4869 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4870 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4871 {
e5654c0f
AM
4872 /* We don't want to put a loaded section after a
4873 nonloaded (ie. bss style) section in the same segment
4874 as that will force the non-loaded section to be loaded.
76cb3a89 4875 Consider .tbss sections as loaded for this purpose. */
0a1b45a2 4876 new_segment = true;
8ded5a0f
AM
4877 }
4878 else if ((abfd->flags & D_PAGED) == 0)
4879 {
4880 /* If the file is not demand paged, which means that we
4881 don't require the sections to be correctly aligned in the
4882 file, then there is no other reason for a new segment. */
0a1b45a2 4883 new_segment = false;
8ded5a0f 4884 }
2888249f
L
4885 else if (info != NULL
4886 && info->separate_code
4887 && executable != ((hdr->flags & SEC_CODE) != 0))
4888 {
0a1b45a2 4889 new_segment = true;
2888249f 4890 }
8ded5a0f 4891 else if (! writable
76cb3a89 4892 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4893 {
4894 /* We don't want to put a writable section in a read only
76cb3a89 4895 segment. */
0a1b45a2 4896 new_segment = true;
8ded5a0f
AM
4897 }
4898 else
4899 {
4900 /* Otherwise, we can use the same segment. */
0a1b45a2 4901 new_segment = false;
8ded5a0f
AM
4902 }
4903
2889e75b 4904 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4905 if (last_hdr != NULL
4906 && info != NULL
4907 && info->callbacks->override_segment_assignment != NULL)
4908 new_segment
4909 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4910 last_hdr,
4911 new_segment);
2889e75b 4912
8ded5a0f
AM
4913 if (! new_segment)
4914 {
4915 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4916 writable = true;
2888249f 4917 if ((hdr->flags & SEC_CODE) != 0)
0a1b45a2 4918 executable = true;
8ded5a0f
AM
4919 last_hdr = hdr;
4920 /* .tbss sections effectively have zero size. */
502794d4 4921 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4922 continue;
4923 }
4924
4925 /* We need a new program segment. We must create a new program
00bee008 4926 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4927
00bee008 4928 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4929 if (m == NULL)
4930 goto error_return;
4931
4932 *pm = m;
4933 pm = &m->next;
4934
252b5132 4935 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4936 writable = true;
8ded5a0f 4937 else
0a1b45a2 4938 writable = false;
8ded5a0f 4939
2888249f 4940 if ((hdr->flags & SEC_CODE) == 0)
0a1b45a2 4941 executable = false;
2888249f 4942 else
0a1b45a2 4943 executable = true;
2888249f 4944
baaff79e
JJ
4945 last_hdr = hdr;
4946 /* .tbss sections effectively have zero size. */
502794d4 4947 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4948 hdr_index = i;
0a1b45a2 4949 phdr_in_segment = false;
252b5132
RH
4950 }
4951
86b2281f
AM
4952 /* Create a final PT_LOAD program segment, but not if it's just
4953 for .tbss. */
4954 if (last_hdr != NULL
00bee008 4955 && (i - hdr_index != 1
dbc88fc1 4956 || !IS_TBSS (last_hdr)))
8ded5a0f 4957 {
00bee008 4958 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4959 if (m == NULL)
4960 goto error_return;
252b5132 4961
8ded5a0f
AM
4962 *pm = m;
4963 pm = &m->next;
4964 }
252b5132 4965
8ded5a0f
AM
4966 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4967 if (dynsec != NULL)
4968 {
4969 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4970 if (m == NULL)
4971 goto error_return;
4972 *pm = m;
4973 pm = &m->next;
4974 }
252b5132 4975
23e463ed 4976 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4977 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4978 because if we link together nonloadable .note sections and
4979 loadable .note sections, we will generate two .note sections
23e463ed 4980 in the output file. */
8ded5a0f
AM
4981 for (s = abfd->sections; s != NULL; s = s->next)
4982 {
4983 if ((s->flags & SEC_LOAD) != 0
23e463ed 4984 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4985 {
1c5265b5 4986 asection *s2;
23e463ed 4987 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
4988
4989 count = 1;
23e463ed
L
4990 for (s2 = s; s2->next != NULL; s2 = s2->next)
4991 {
4992 if (s2->next->alignment_power == alignment_power
4993 && (s2->next->flags & SEC_LOAD) != 0
4994 && elf_section_type (s2->next) == SHT_NOTE
66631823 4995 && align_power (s2->lma + s2->size / opb,
23e463ed
L
4996 alignment_power)
4997 == s2->next->lma)
4998 count++;
4999 else
5000 break;
5001 }
00bee008
AM
5002 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5003 amt += count * sizeof (asection *);
a50b1753 5004 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5005 if (m == NULL)
5006 goto error_return;
5007 m->next = NULL;
5008 m->p_type = PT_NOTE;
1c5265b5
JJ
5009 m->count = count;
5010 while (count > 1)
5011 {
5012 m->sections[m->count - count--] = s;
5013 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5014 s = s->next;
5015 }
5016 m->sections[m->count - 1] = s;
5017 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5018 *pm = m;
5019 pm = &m->next;
5020 }
5021 if (s->flags & SEC_THREAD_LOCAL)
5022 {
5023 if (! tls_count)
5024 first_tls = s;
5025 tls_count++;
5026 }
a91e1603
L
5027 if (first_mbind == NULL
5028 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5029 first_mbind = s;
8ded5a0f 5030 }
252b5132 5031
8ded5a0f
AM
5032 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5033 if (tls_count > 0)
5034 {
00bee008
AM
5035 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5036 amt += tls_count * sizeof (asection *);
a50b1753 5037 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5038 if (m == NULL)
5039 goto error_return;
5040 m->next = NULL;
5041 m->p_type = PT_TLS;
5042 m->count = tls_count;
5043 /* Mandated PF_R. */
5044 m->p_flags = PF_R;
5045 m->p_flags_valid = 1;
d923cae0 5046 s = first_tls;
446f7ed5 5047 for (i = 0; i < tls_count; ++i)
8ded5a0f 5048 {
d923cae0
L
5049 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5050 {
5051 _bfd_error_handler
871b3ab2 5052 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5053 s = first_tls;
5054 i = 0;
446f7ed5 5055 while (i < tls_count)
d923cae0
L
5056 {
5057 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5058 {
871b3ab2 5059 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5060 i++;
5061 }
5062 else
871b3ab2 5063 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5064 s = s->next;
5065 }
5066 bfd_set_error (bfd_error_bad_value);
5067 goto error_return;
5068 }
5069 m->sections[i] = s;
5070 s = s->next;
8ded5a0f 5071 }
252b5132 5072
8ded5a0f
AM
5073 *pm = m;
5074 pm = &m->next;
5075 }
252b5132 5076
df3a023b
AM
5077 if (first_mbind
5078 && (abfd->flags & D_PAGED) != 0
5079 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5080 for (s = first_mbind; s != NULL; s = s->next)
5081 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5082 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5083 {
5084 /* Mandated PF_R. */
5085 unsigned long p_flags = PF_R;
5086 if ((s->flags & SEC_READONLY) == 0)
5087 p_flags |= PF_W;
5088 if ((s->flags & SEC_CODE) != 0)
5089 p_flags |= PF_X;
5090
5091 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5092 m = bfd_zalloc (abfd, amt);
5093 if (m == NULL)
5094 goto error_return;
5095 m->next = NULL;
5096 m->p_type = (PT_GNU_MBIND_LO
5097 + elf_section_data (s)->this_hdr.sh_info);
5098 m->count = 1;
5099 m->p_flags_valid = 1;
5100 m->sections[0] = s;
5101 m->p_flags = p_flags;
5102
5103 *pm = m;
5104 pm = &m->next;
5105 }
5106
0a59decb
L
5107 s = bfd_get_section_by_name (abfd,
5108 NOTE_GNU_PROPERTY_SECTION_NAME);
5109 if (s != NULL && s->size != 0)
5110 {
5111 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5112 m = bfd_zalloc (abfd, amt);
5113 if (m == NULL)
5114 goto error_return;
5115 m->next = NULL;
5116 m->p_type = PT_GNU_PROPERTY;
5117 m->count = 1;
5118 m->p_flags_valid = 1;
5119 m->sections[0] = s;
5120 m->p_flags = PF_R;
5121 *pm = m;
5122 pm = &m->next;
5123 }
5124
8ded5a0f
AM
5125 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5126 segment. */
8df52eee 5127 eh_frame_hdr = elf_eh_frame_hdr (info);
8ded5a0f
AM
5128 if (eh_frame_hdr != NULL
5129 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5130 {
dc810e39 5131 amt = sizeof (struct elf_segment_map);
a50b1753 5132 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5133 if (m == NULL)
5134 goto error_return;
5135 m->next = NULL;
8ded5a0f 5136 m->p_type = PT_GNU_EH_FRAME;
252b5132 5137 m->count = 1;
8ded5a0f 5138 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5139
5140 *pm = m;
5141 pm = &m->next;
5142 }
13ae64f3 5143
cf0e0a0b
IB
5144 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5145 segment. */
5146 sframe = elf_sframe (abfd);
5147 if (sframe != NULL
5148 && (sframe->output_section->flags & SEC_LOAD) != 0
5149 && sframe->size != 0)
5150 {
5151 amt = sizeof (struct elf_segment_map);
5152 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5153 if (m == NULL)
5154 goto error_return;
5155 m->next = NULL;
5156 m->p_type = PT_GNU_SFRAME;
5157 m->count = 1;
5158 m->sections[0] = sframe->output_section;
5159
5160 *pm = m;
5161 pm = &m->next;
5162 }
5163
12bd6957 5164 if (elf_stack_flags (abfd))
13ae64f3 5165 {
8ded5a0f 5166 amt = sizeof (struct elf_segment_map);
a50b1753 5167 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5168 if (m == NULL)
5169 goto error_return;
5170 m->next = NULL;
2b05f1b7 5171 m->p_type = PT_GNU_STACK;
12bd6957 5172 m->p_flags = elf_stack_flags (abfd);
04c3a755 5173 m->p_align = bed->stack_align;
8ded5a0f 5174 m->p_flags_valid = 1;
04c3a755
NS
5175 m->p_align_valid = m->p_align != 0;
5176 if (info->stacksize > 0)
5177 {
5178 m->p_size = info->stacksize;
5179 m->p_size_valid = 1;
5180 }
252b5132 5181
8ded5a0f
AM
5182 *pm = m;
5183 pm = &m->next;
5184 }
65765700 5185
ceae84aa 5186 if (info != NULL && info->relro)
8ded5a0f 5187 {
f210dcff
L
5188 for (m = mfirst; m != NULL; m = m->next)
5189 {
3832a4d8
AM
5190 if (m->p_type == PT_LOAD
5191 && m->count != 0
5192 && m->sections[0]->vma >= info->relro_start
5193 && m->sections[0]->vma < info->relro_end)
f210dcff 5194 {
3832a4d8
AM
5195 i = m->count;
5196 while (--i != (unsigned) -1)
ec2e748a
NC
5197 {
5198 if (m->sections[i]->size > 0
11c6a7c6
AM
5199 && (m->sections[i]->flags & SEC_LOAD) != 0
5200 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
ec2e748a
NC
5201 break;
5202 }
3832a4d8 5203
43a8475c 5204 if (i != (unsigned) -1)
f210dcff
L
5205 break;
5206 }
be01b344 5207 }
f210dcff
L
5208
5209 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5210 if (m != NULL)
5211 {
5212 amt = sizeof (struct elf_segment_map);
a50b1753 5213 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5214 if (m == NULL)
5215 goto error_return;
5216 m->next = NULL;
5217 m->p_type = PT_GNU_RELRO;
f210dcff
L
5218 *pm = m;
5219 pm = &m->next;
5220 }
8ded5a0f 5221 }
9ee5e499 5222
8ded5a0f 5223 free (sections);
12bd6957 5224 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5225 }
5226
3dea8fca 5227 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
0a1b45a2 5228 return false;
8c37241b 5229
12bd6957 5230 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5231 ++count;
12bd6957 5232 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5233
0a1b45a2 5234 return true;
252b5132
RH
5235
5236 error_return:
c9594989 5237 free (sections);
0a1b45a2 5238 return false;
252b5132
RH
5239}
5240
5241/* Sort sections by address. */
5242
5243static int
217aa764 5244elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5245{
5246 const asection *sec1 = *(const asection **) arg1;
5247 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5248 bfd_size_type size1, size2;
252b5132
RH
5249
5250 /* Sort by LMA first, since this is the address used to
5251 place the section into a segment. */
5252 if (sec1->lma < sec2->lma)
5253 return -1;
5254 else if (sec1->lma > sec2->lma)
5255 return 1;
5256
5257 /* Then sort by VMA. Normally the LMA and the VMA will be
5258 the same, and this will do nothing. */
5259 if (sec1->vma < sec2->vma)
5260 return -1;
5261 else if (sec1->vma > sec2->vma)
5262 return 1;
5263
5264 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5265
8d748d1d
AM
5266#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5267 && (x)->size != 0)
252b5132
RH
5268
5269 if (TOEND (sec1))
5270 {
48db3297 5271 if (!TOEND (sec2))
252b5132
RH
5272 return 1;
5273 }
00a7cdc5 5274 else if (TOEND (sec2))
252b5132
RH
5275 return -1;
5276
5277#undef TOEND
5278
00a7cdc5
NC
5279 /* Sort by size, to put zero sized sections
5280 before others at the same address. */
252b5132 5281
eea6121a
AM
5282 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5283 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5284
5285 if (size1 < size2)
252b5132 5286 return -1;
eecdbe52 5287 if (size1 > size2)
252b5132
RH
5288 return 1;
5289
5290 return sec1->target_index - sec2->target_index;
5291}
5292
30fe1832
AM
5293/* This qsort comparison functions sorts PT_LOAD segments first and
5294 by p_paddr, for assign_file_positions_for_load_sections. */
5295
5296static int
5297elf_sort_segments (const void *arg1, const void *arg2)
5298{
5299 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5300 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5301
5302 if (m1->p_type != m2->p_type)
5303 {
5304 if (m1->p_type == PT_NULL)
5305 return 1;
5306 if (m2->p_type == PT_NULL)
5307 return -1;
5308 return m1->p_type < m2->p_type ? -1 : 1;
5309 }
5310 if (m1->includes_filehdr != m2->includes_filehdr)
5311 return m1->includes_filehdr ? -1 : 1;
5312 if (m1->no_sort_lma != m2->no_sort_lma)
5313 return m1->no_sort_lma ? -1 : 1;
5314 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5315 {
4b3ecb3b 5316 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5317 lma1 = 0;
5318 if (m1->p_paddr_valid)
4b3ecb3b 5319 lma1 = m1->p_paddr;
30fe1832 5320 else if (m1->count != 0)
4b3ecb3b
AM
5321 {
5322 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5323 m1->sections[0]);
5324 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5325 }
30fe1832
AM
5326 lma2 = 0;
5327 if (m2->p_paddr_valid)
4b3ecb3b 5328 lma2 = m2->p_paddr;
30fe1832 5329 else if (m2->count != 0)
4b3ecb3b
AM
5330 {
5331 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5332 m2->sections[0]);
5333 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5334 }
30fe1832
AM
5335 if (lma1 != lma2)
5336 return lma1 < lma2 ? -1 : 1;
5337 }
5338 if (m1->idx != m2->idx)
5339 return m1->idx < m2->idx ? -1 : 1;
5340 return 0;
5341}
5342
340b6d91
AC
5343/* Ian Lance Taylor writes:
5344
5345 We shouldn't be using % with a negative signed number. That's just
5346 not good. We have to make sure either that the number is not
5347 negative, or that the number has an unsigned type. When the types
5348 are all the same size they wind up as unsigned. When file_ptr is a
5349 larger signed type, the arithmetic winds up as signed long long,
5350 which is wrong.
5351
5352 What we're trying to say here is something like ``increase OFF by
5353 the least amount that will cause it to be equal to the VMA modulo
5354 the page size.'' */
5355/* In other words, something like:
5356
5357 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5358 off_offset = off % bed->maxpagesize;
5359 if (vma_offset < off_offset)
5360 adjustment = vma_offset + bed->maxpagesize - off_offset;
5361 else
5362 adjustment = vma_offset - off_offset;
08a40648 5363
de194d85 5364 which can be collapsed into the expression below. */
340b6d91
AC
5365
5366static file_ptr
5367vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5368{
dc9155b2
NC
5369 /* PR binutils/16199: Handle an alignment of zero. */
5370 if (maxpagesize == 0)
5371 maxpagesize = 1;
340b6d91
AC
5372 return ((vma - off) % maxpagesize);
5373}
5374
6d33f217
L
5375static void
5376print_segment_map (const struct elf_segment_map *m)
5377{
5378 unsigned int j;
5379 const char *pt = get_segment_type (m->p_type);
5380 char buf[32];
5381
5382 if (pt == NULL)
5383 {
5384 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5385 sprintf (buf, "LOPROC+%7.7x",
5386 (unsigned int) (m->p_type - PT_LOPROC));
5387 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5388 sprintf (buf, "LOOS+%7.7x",
5389 (unsigned int) (m->p_type - PT_LOOS));
5390 else
5391 snprintf (buf, sizeof (buf), "%8.8x",
5392 (unsigned int) m->p_type);
5393 pt = buf;
5394 }
4a97a0e5 5395 fflush (stdout);
6d33f217
L
5396 fprintf (stderr, "%s:", pt);
5397 for (j = 0; j < m->count; j++)
5398 fprintf (stderr, " %s", m->sections [j]->name);
5399 putc ('\n',stderr);
4a97a0e5 5400 fflush (stderr);
6d33f217
L
5401}
5402
252b5132
RH
5403/* Assign file positions to the sections based on the mapping from
5404 sections to segments. This function also sets up some fields in
f3520d2f 5405 the file header. */
252b5132 5406
0a1b45a2 5407static bool
f3520d2f
AM
5408assign_file_positions_for_load_sections (bfd *abfd,
5409 struct bfd_link_info *link_info)
252b5132
RH
5410{
5411 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5412 struct elf_segment_map *m;
30fe1832 5413 struct elf_segment_map *phdr_load_seg;
252b5132 5414 Elf_Internal_Phdr *phdrs;
252b5132 5415 Elf_Internal_Phdr *p;
502794d4 5416 file_ptr off; /* Octets. */
3f570048 5417 bfd_size_type maxpagesize;
30fe1832 5418 unsigned int alloc, actual;
0920dee7 5419 unsigned int i, j;
30fe1832 5420 struct elf_segment_map **sorted_seg_map;
502794d4 5421 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5422
e36284ab 5423 if (link_info == NULL
e2cbf4df 5424 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
0a1b45a2 5425 return false;
252b5132 5426
8ded5a0f 5427 alloc = 0;
12bd6957 5428 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5429 m->idx = alloc++;
252b5132 5430
82f2dbf7
NC
5431 if (alloc)
5432 {
5433 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5434 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5435 }
5436 else
5437 {
5438 /* PR binutils/12467. */
5439 elf_elfheader (abfd)->e_phoff = 0;
5440 elf_elfheader (abfd)->e_phentsize = 0;
5441 }
d324f6d6 5442
8ded5a0f 5443 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5444
12bd6957 5445 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5446 {
5447 actual = alloc;
5448 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5449 }
8ded5a0f 5450 else
30fe1832
AM
5451 {
5452 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5453 BFD_ASSERT (elf_program_header_size (abfd)
5454 == actual * bed->s->sizeof_phdr);
5455 BFD_ASSERT (actual >= alloc);
5456 }
252b5132
RH
5457
5458 if (alloc == 0)
f3520d2f 5459 {
12bd6957 5460 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
0a1b45a2 5461 return true;
f3520d2f 5462 }
252b5132 5463
12bd6957 5464 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5465 see assign_file_positions_except_relocs, so make sure we have
5466 that amount allocated, with trailing space cleared.
12bd6957
AM
5467 The variable alloc contains the computed need, while
5468 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5469 layout.
5470 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5471 where the layout is forced to according to a larger size in the
5472 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5473 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5474 + alloc * sizeof (*sorted_seg_map)));
5475 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5476 elf_tdata (abfd)->phdr = phdrs;
252b5132 5477 if (phdrs == NULL)
0a1b45a2 5478 return false;
252b5132 5479
30fe1832 5480 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5481 {
30fe1832 5482 sorted_seg_map[j] = m;
252b5132 5483 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5484 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5485 not be done to the PT_NOTE section of a corefile, which may
5486 contain several pseudo-sections artificially created by bfd.
5487 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5488 if (m->count > 1
5489 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5490 && m->p_type == PT_NOTE))
48db3297
AM
5491 {
5492 for (i = 0; i < m->count; i++)
5493 m->sections[i]->target_index = i;
5494 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5495 elf_sort_sections);
5496 }
30fe1832
AM
5497 }
5498 if (alloc > 1)
5499 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5500 elf_sort_segments);
5501
5502 maxpagesize = 1;
5503 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5504 {
5505 if (link_info != NULL)
5506 maxpagesize = link_info->maxpagesize;
5507 else
5508 maxpagesize = bed->maxpagesize;
5509 }
30fe1832
AM
5510
5511 /* Sections must map to file offsets past the ELF file header. */
5512 off = bed->s->sizeof_ehdr;
5513 /* And if one of the PT_LOAD headers doesn't include the program
5514 headers then we'll be mapping program headers in the usual
5515 position after the ELF file header. */
5516 phdr_load_seg = NULL;
5517 for (j = 0; j < alloc; j++)
5518 {
5519 m = sorted_seg_map[j];
5520 if (m->p_type != PT_LOAD)
5521 break;
5522 if (m->includes_phdrs)
5523 {
5524 phdr_load_seg = m;
5525 break;
5526 }
5527 }
5528 if (phdr_load_seg == NULL)
5529 off += actual * bed->s->sizeof_phdr;
5530
5531 for (j = 0; j < alloc; j++)
5532 {
5533 asection **secpp;
502794d4 5534 bfd_vma off_adjust; /* Octets. */
0a1b45a2 5535 bool no_contents;
59f21454
L
5536 bfd_size_type p_align;
5537 bool p_align_p;
252b5132 5538
b301b248
AM
5539 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5540 number of sections with contents contributing to both p_filesz
5541 and p_memsz, followed by a number of sections with no contents
5542 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5543 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5544 m = sorted_seg_map[j];
5545 p = phdrs + m->idx;
252b5132 5546 p->p_type = m->p_type;
28a7f3e7 5547 p->p_flags = m->p_flags;
59f21454
L
5548 p_align = bed->p_align;
5549 p_align_p = false;
252b5132 5550
3f570048 5551 if (m->count == 0)
502794d4 5552 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5553 else
502794d4 5554 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5555
5556 if (m->p_paddr_valid)
5557 p->p_paddr = m->p_paddr;
5558 else if (m->count == 0)
5559 p->p_paddr = 0;
5560 else
502794d4 5561 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5562
5563 if (p->p_type == PT_LOAD
5564 && (abfd->flags & D_PAGED) != 0)
5565 {
5566 /* p_align in demand paged PT_LOAD segments effectively stores
5567 the maximum page size. When copying an executable with
5568 objcopy, we set m->p_align from the input file. Use this
5569 value for maxpagesize rather than bed->maxpagesize, which
5570 may be different. Note that we use maxpagesize for PT_TLS
5571 segment alignment later in this function, so we are relying
5572 on at least one PT_LOAD segment appearing before a PT_TLS
5573 segment. */
5574 if (m->p_align_valid)
5575 maxpagesize = m->p_align;
74e315db
L
5576 else if (p_align != 0
5577 && (link_info == NULL
5578 || !link_info->maxpagesize_is_set))
5579 /* Set p_align to the default p_align value while laying
5580 out segments aligning to the maximum page size or the
5581 largest section alignment. The run-time loader can
5582 align segments to the default p_align value or the
5583 maximum page size, depending on system page size. */
5584 p_align_p = true;
3f570048
AM
5585
5586 p->p_align = maxpagesize;
5587 }
3271a814
NS
5588 else if (m->p_align_valid)
5589 p->p_align = m->p_align;
e970b90a
DJ
5590 else if (m->count == 0)
5591 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5592
5593 if (m == phdr_load_seg)
5594 {
5595 if (!m->includes_filehdr)
5596 p->p_offset = off;
5597 off += actual * bed->s->sizeof_phdr;
5598 }
3f570048 5599
0a1b45a2 5600 no_contents = false;
bf988460 5601 off_adjust = 0;
252b5132 5602 if (p->p_type == PT_LOAD
b301b248 5603 && m->count > 0)
252b5132 5604 {
66631823 5605 bfd_size_type align; /* Bytes. */
a49e53ed 5606 unsigned int align_power = 0;
b301b248 5607
3271a814
NS
5608 if (m->p_align_valid)
5609 align = p->p_align;
5610 else
252b5132 5611 {
3271a814
NS
5612 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5613 {
5614 unsigned int secalign;
08a40648 5615
fd361982 5616 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5617 if (secalign > align_power)
5618 align_power = secalign;
5619 }
5620 align = (bfd_size_type) 1 << align_power;
5621 if (align < maxpagesize)
74e315db
L
5622 {
5623 /* If a section requires alignment higher than the
5624 default p_align value, don't set p_align to the
5625 default p_align value. */
5626 if (align > p_align)
5627 p_align_p = false;
5628 align = maxpagesize;
5629 }
5630 else
5631 {
5632 /* If a section requires alignment higher than the
5633 maximum page size, set p_align to the section
5634 alignment. */
5635 p_align_p = true;
5636 p_align = align;
5637 }
b301b248 5638 }
252b5132 5639
02bf8d82
AM
5640 for (i = 0; i < m->count; i++)
5641 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5642 /* If we aren't making room for this section, then
5643 it must be SHT_NOBITS regardless of what we've
5644 set via struct bfd_elf_special_section. */
5645 elf_section_type (m->sections[i]) = SHT_NOBITS;
5646
bf988460 5647 /* Find out whether this segment contains any loadable
aea274d3 5648 sections. */
0a1b45a2 5649 no_contents = true;
aea274d3
AM
5650 for (i = 0; i < m->count; i++)
5651 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5652 {
0a1b45a2 5653 no_contents = false;
aea274d3
AM
5654 break;
5655 }
bf988460 5656
66631823 5657 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5658
5659 /* Broken hardware and/or kernel require that files do not
5660 map the same page with different permissions on some hppa
5661 processors. */
30fe1832
AM
5662 if (j != 0
5663 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5664 && bed->no_page_alias
5665 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5666 && ((off & -maxpagesize)
5667 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5668 off_adjust += maxpagesize;
bf988460
AM
5669 off += off_adjust;
5670 if (no_contents)
5671 {
5672 /* We shouldn't need to align the segment on disk since
5673 the segment doesn't need file space, but the gABI
5674 arguably requires the alignment and glibc ld.so
5675 checks it. So to comply with the alignment
5676 requirement but not waste file space, we adjust
5677 p_offset for just this segment. (OFF_ADJUST is
5678 subtracted from OFF later.) This may put p_offset
5679 past the end of file, but that shouldn't matter. */
5680 }
5681 else
5682 off_adjust = 0;
252b5132 5683 }
b1a6d0b1
NC
5684 /* Make sure the .dynamic section is the first section in the
5685 PT_DYNAMIC segment. */
5686 else if (p->p_type == PT_DYNAMIC
5687 && m->count > 1
5688 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5689 {
5690 _bfd_error_handler
871b3ab2 5691 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5692 " is not the .dynamic section"),
b301b248 5693 abfd);
b1a6d0b1 5694 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5695 return false;
b1a6d0b1 5696 }
3f001e84
JK
5697 /* Set the note section type to SHT_NOTE. */
5698 else if (p->p_type == PT_NOTE)
5699 for (i = 0; i < m->count; i++)
5700 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5701
252b5132
RH
5702 if (m->includes_filehdr)
5703 {
bf988460 5704 if (!m->p_flags_valid)
252b5132 5705 p->p_flags |= PF_R;
252b5132
RH
5706 p->p_filesz = bed->s->sizeof_ehdr;
5707 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5708 if (p->p_type == PT_LOAD)
252b5132 5709 {
30fe1832 5710 if (m->count > 0)
252b5132 5711 {
30fe1832
AM
5712 if (p->p_vaddr < (bfd_vma) off
5713 || (!m->p_paddr_valid
5714 && p->p_paddr < (bfd_vma) off))
5715 {
5716 _bfd_error_handler
5717 (_("%pB: not enough room for program headers,"
5718 " try linking with -N"),
5719 abfd);
5720 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5721 return false;
30fe1832
AM
5722 }
5723 p->p_vaddr -= off;
5724 if (!m->p_paddr_valid)
5725 p->p_paddr -= off;
252b5132 5726 }
30fe1832
AM
5727 }
5728 else if (sorted_seg_map[0]->includes_filehdr)
5729 {
5730 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5731 p->p_vaddr = filehdr->p_vaddr;
bf988460 5732 if (!m->p_paddr_valid)
30fe1832 5733 p->p_paddr = filehdr->p_paddr;
252b5132 5734 }
252b5132
RH
5735 }
5736
5737 if (m->includes_phdrs)
5738 {
bf988460 5739 if (!m->p_flags_valid)
252b5132 5740 p->p_flags |= PF_R;
30fe1832
AM
5741 p->p_filesz += actual * bed->s->sizeof_phdr;
5742 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5743 if (!m->includes_filehdr)
252b5132 5744 {
30fe1832 5745 if (p->p_type == PT_LOAD)
252b5132 5746 {
30fe1832
AM
5747 elf_elfheader (abfd)->e_phoff = p->p_offset;
5748 if (m->count > 0)
5749 {
5750 p->p_vaddr -= off - p->p_offset;
5751 if (!m->p_paddr_valid)
5752 p->p_paddr -= off - p->p_offset;
5753 }
5754 }
5755 else if (phdr_load_seg != NULL)
5756 {
5757 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5758 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5759 if (phdr_load_seg->includes_filehdr)
5760 phdr_off = bed->s->sizeof_ehdr;
5761 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5762 if (!m->p_paddr_valid)
30fe1832
AM
5763 p->p_paddr = phdr->p_paddr + phdr_off;
5764 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5765 }
30fe1832
AM
5766 else
5767 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5768 }
252b5132
RH
5769 }
5770
5771 if (p->p_type == PT_LOAD
5772 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5773 {
bf988460 5774 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5775 {
5776 p->p_offset = off;
5777 if (no_contents)
67641dd3
AM
5778 {
5779 /* Put meaningless p_offset for PT_LOAD segments
5780 without file contents somewhere within the first
5781 page, in an attempt to not point past EOF. */
5782 bfd_size_type align = maxpagesize;
5783 if (align < p->p_align)
5784 align = p->p_align;
5785 if (align < 1)
5786 align = 1;
5787 p->p_offset = off % align;
5788 }
0bc3450e 5789 }
252b5132
RH
5790 else
5791 {
502794d4 5792 file_ptr adjust; /* Octets. */
252b5132
RH
5793
5794 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5795 if (!no_contents)
5796 p->p_filesz += adjust;
252b5132
RH
5797 p->p_memsz += adjust;
5798 }
5799 }
5800
1ea63fd2
AM
5801 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5802 maps. Set filepos for sections in PT_LOAD segments, and in
5803 core files, for sections in PT_NOTE segments.
5804 assign_file_positions_for_non_load_sections will set filepos
5805 for other sections and update p_filesz for other segments. */
252b5132
RH
5806 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5807 {
5808 asection *sec;
252b5132 5809 bfd_size_type align;
627b32bc 5810 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5811
5812 sec = *secpp;
02bf8d82 5813 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5814 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5815
88967714
AM
5816 if ((p->p_type == PT_LOAD
5817 || p->p_type == PT_TLS)
5818 && (this_hdr->sh_type != SHT_NOBITS
5819 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5820 && ((this_hdr->sh_flags & SHF_TLS) == 0
5821 || p->p_type == PT_TLS))))
252b5132 5822 {
11c6a7c6
AM
5823 bfd_vma p_start = p->p_paddr; /* Octets. */
5824 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5825 bfd_vma s_start = sec->lma * opb; /* Octets. */
5826 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5827
a2d1e028
L
5828 if (adjust != 0
5829 && (s_start < p_end
5830 || p_end < p_start))
252b5132 5831 {
4eca0228 5832 _bfd_error_handler
695344c0 5833 /* xgettext:c-format */
11c6a7c6
AM
5834 (_("%pB: section %pA lma %#" PRIx64
5835 " adjusted to %#" PRIx64),
502794d4
CE
5836 abfd, sec, (uint64_t) s_start / opb,
5837 (uint64_t) p_end / opb);
88967714 5838 adjust = 0;
502794d4 5839 sec->lma = p_end / opb;
1cfb7d1e 5840 }
3ac9b6c9 5841 p->p_memsz += adjust;
1cfb7d1e 5842
d16e3d2e 5843 if (p->p_type == PT_LOAD)
88967714 5844 {
d16e3d2e 5845 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5846 {
d16e3d2e 5847 off_adjust = 0;
30fe1832
AM
5848 if (p->p_filesz + adjust < p->p_memsz)
5849 {
5850 /* We have a PROGBITS section following NOBITS ones.
281309f3
AM
5851 Allocate file space for the NOBITS section(s).
5852 We don't need to write out the zeros, posix
5853 fseek past the end of data already written
5854 followed by a write at that location is
5855 guaranteed to result in zeros being read
5856 from the gap. */
30fe1832 5857 adjust = p->p_memsz - p->p_filesz;
30fe1832 5858 }
d16e3d2e
AM
5859 }
5860 /* We only adjust sh_offset in SHT_NOBITS sections
5861 as would seem proper for their address when the
5862 section is first in the segment. sh_offset
5863 doesn't really have any significance for
5864 SHT_NOBITS anyway, apart from a notional position
5865 relative to other sections. Historically we
5866 didn't bother with adjusting sh_offset and some
5867 programs depend on it not being adjusted. See
5868 pr12921 and pr25662. */
5869 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5870 {
30fe1832 5871 off += adjust;
d16e3d2e
AM
5872 if (this_hdr->sh_type == SHT_NOBITS)
5873 off_adjust += adjust;
32812159 5874 }
252b5132 5875 }
d16e3d2e
AM
5876 if (this_hdr->sh_type != SHT_NOBITS)
5877 p->p_filesz += adjust;
252b5132
RH
5878 }
5879
5880 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5881 {
b301b248
AM
5882 /* The section at i == 0 is the one that actually contains
5883 everything. */
4a938328
MS
5884 if (i == 0)
5885 {
627b32bc 5886 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5887 off += this_hdr->sh_size;
5888 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5889 p->p_memsz = 0;
5890 p->p_align = 1;
252b5132 5891 }
4a938328 5892 else
252b5132 5893 {
b301b248 5894 /* The rest are fake sections that shouldn't be written. */
252b5132 5895 sec->filepos = 0;
eea6121a 5896 sec->size = 0;
b301b248
AM
5897 sec->flags = 0;
5898 continue;
252b5132 5899 }
252b5132
RH
5900 }
5901 else
5902 {
1e951488 5903 if (p->p_type == PT_LOAD)
b301b248 5904 {
1e951488
AM
5905 this_hdr->sh_offset = sec->filepos = off;
5906 if (this_hdr->sh_type != SHT_NOBITS)
5907 off += this_hdr->sh_size;
5908 }
5909 else if (this_hdr->sh_type == SHT_NOBITS
5910 && (this_hdr->sh_flags & SHF_TLS) != 0
5911 && this_hdr->sh_offset == 0)
5912 {
5913 /* This is a .tbss section that didn't get a PT_LOAD.
5914 (See _bfd_elf_map_sections_to_segments "Create a
5915 final PT_LOAD".) Set sh_offset to the value it
5916 would have if we had created a zero p_filesz and
5917 p_memsz PT_LOAD header for the section. This
5918 also makes the PT_TLS header have the same
5919 p_offset value. */
5920 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5921 off, align);
5922 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5923 }
252b5132 5924
02bf8d82 5925 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5926 {
6a3cd2b4 5927 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5928 /* A load section without SHF_ALLOC is something like
5929 a note section in a PT_NOTE segment. These take
5930 file space but are not loaded into memory. */
5931 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5932 p->p_memsz += this_hdr->sh_size;
b301b248 5933 }
6a3cd2b4 5934 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5935 {
6a3cd2b4
AM
5936 if (p->p_type == PT_TLS)
5937 p->p_memsz += this_hdr->sh_size;
5938
5939 /* .tbss is special. It doesn't contribute to p_memsz of
5940 normal segments. */
5941 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5942 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5943 }
5944
b10a8ae0
L
5945 if (align > p->p_align
5946 && !m->p_align_valid
5947 && (p->p_type != PT_LOAD
5948 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5949 p->p_align = align;
5950 }
5951
bf988460 5952 if (!m->p_flags_valid)
252b5132
RH
5953 {
5954 p->p_flags |= PF_R;
02bf8d82 5955 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5956 p->p_flags |= PF_X;
02bf8d82 5957 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5958 p->p_flags |= PF_W;
5959 }
5960 }
43a8475c 5961
bf988460 5962 off -= off_adjust;
0920dee7 5963
30fe1832
AM
5964 /* PR ld/20815 - Check that the program header segment, if
5965 present, will be loaded into memory. */
5966 if (p->p_type == PT_PHDR
5967 && phdr_load_seg == NULL
5968 && !(bed->elf_backend_allow_non_load_phdr != NULL
5969 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5970 {
5971 /* The fix for this error is usually to edit the linker script being
5972 used and set up the program headers manually. Either that or
5973 leave room for the headers at the start of the SECTIONS. */
5974 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5975 " by LOAD segment"),
5976 abfd);
7b3c2715 5977 if (link_info == NULL)
0a1b45a2 5978 return false;
7b3c2715
AM
5979 /* Arrange for the linker to exit with an error, deleting
5980 the output file unless --noinhibit-exec is given. */
5981 link_info->callbacks->info ("%X");
30fe1832
AM
5982 }
5983
7c928300
AM
5984 /* Check that all sections are in a PT_LOAD segment.
5985 Don't check funky gdb generated core files. */
5986 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553 5987 {
0a1b45a2 5988 bool check_vma = true;
9a83a553
AM
5989
5990 for (i = 1; i < m->count; i++)
5991 if (m->sections[i]->vma == m->sections[i - 1]->vma
5992 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5993 ->this_hdr), p) != 0
5994 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5995 ->this_hdr), p) != 0)
0920dee7 5996 {
9a83a553 5997 /* Looks like we have overlays packed into the segment. */
0a1b45a2 5998 check_vma = false;
9a83a553 5999 break;
0920dee7 6000 }
9a83a553
AM
6001
6002 for (i = 0; i < m->count; i++)
6003 {
6004 Elf_Internal_Shdr *this_hdr;
6005 asection *sec;
6006
6007 sec = m->sections[i];
6008 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
6009 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6010 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 6011 {
4eca0228 6012 _bfd_error_handler
695344c0 6013 /* xgettext:c-format */
871b3ab2 6014 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
6015 abfd, sec, j);
6016 print_segment_map (m);
6017 }
6018 }
74e315db
L
6019
6020 if (p_align_p)
6021 p->p_align = p_align;
9a83a553 6022 }
252b5132
RH
6023 }
6024
12bd6957 6025 elf_next_file_pos (abfd) = off;
30fe1832
AM
6026
6027 if (link_info != NULL
6028 && phdr_load_seg != NULL
6029 && phdr_load_seg->includes_filehdr)
6030 {
6031 /* There is a segment that contains both the file headers and the
6032 program headers, so provide a symbol __ehdr_start pointing there.
6033 A program can use this to examine itself robustly. */
6034
6035 struct elf_link_hash_entry *hash
6036 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
0a1b45a2 6037 false, false, true);
30fe1832
AM
6038 /* If the symbol was referenced and not defined, define it. */
6039 if (hash != NULL
6040 && (hash->root.type == bfd_link_hash_new
6041 || hash->root.type == bfd_link_hash_undefined
6042 || hash->root.type == bfd_link_hash_undefweak
6043 || hash->root.type == bfd_link_hash_common))
6044 {
6045 asection *s = NULL;
66631823 6046 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6047
6048 if (phdr_load_seg->count != 0)
6049 /* The segment contains sections, so use the first one. */
6050 s = phdr_load_seg->sections[0];
6051 else
6052 /* Use the first (i.e. lowest-addressed) section in any segment. */
6053 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6054 if (m->p_type == PT_LOAD && m->count != 0)
6055 {
6056 s = m->sections[0];
6057 break;
6058 }
6059
6060 if (s != NULL)
6061 {
6062 hash->root.u.def.value = filehdr_vaddr - s->vma;
6063 hash->root.u.def.section = s;
6064 }
6065 else
6066 {
6067 hash->root.u.def.value = filehdr_vaddr;
6068 hash->root.u.def.section = bfd_abs_section_ptr;
6069 }
6070
6071 hash->root.type = bfd_link_hash_defined;
6072 hash->def_regular = 1;
6073 hash->non_elf = 0;
6074 }
6075 }
6076
0a1b45a2 6077 return true;
f3520d2f
AM
6078}
6079
1faa385f
NC
6080/* Determine if a bfd is a debuginfo file. Unfortunately there
6081 is no defined method for detecting such files, so we have to
6082 use heuristics instead. */
6083
0a1b45a2 6084bool
1faa385f
NC
6085is_debuginfo_file (bfd *abfd)
6086{
6087 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
0a1b45a2 6088 return false;
1faa385f
NC
6089
6090 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6091 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6092 Elf_Internal_Shdr **headerp;
6093
6094 for (headerp = start_headers; headerp < end_headers; headerp ++)
6095 {
6096 Elf_Internal_Shdr *header = * headerp;
6097
6098 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6099 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6100 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6101 && header->sh_type != SHT_NOBITS
6102 && header->sh_type != SHT_NOTE)
0a1b45a2 6103 return false;
1faa385f
NC
6104 }
6105
0a1b45a2 6106 return true;
1faa385f
NC
6107}
6108
11c6a7c6
AM
6109/* Assign file positions for other sections, except for compressed debug
6110 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
f3520d2f 6111
0a1b45a2 6112static bool
f3520d2f
AM
6113assign_file_positions_for_non_load_sections (bfd *abfd,
6114 struct bfd_link_info *link_info)
6115{
6116 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6117 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6118 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6119 Elf_Internal_Phdr *phdrs;
6120 Elf_Internal_Phdr *p;
6121 struct elf_segment_map *m;
f3520d2f 6122 file_ptr off;
66631823 6123 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6124 bfd_vma maxpagesize;
f3520d2f 6125
c410035d
AM
6126 if (link_info != NULL)
6127 maxpagesize = link_info->maxpagesize;
6128 else
6129 maxpagesize = bed->maxpagesize;
5c182d5f 6130 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6131 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6132 off = elf_next_file_pos (abfd);
e06efbf1 6133 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6134 {
5c182d5f 6135 Elf_Internal_Shdr *hdr;
1f9b1a84 6136 bfd_vma align;
5c182d5f
AM
6137
6138 hdr = *hdrpp;
6139 if (hdr->bfd_section != NULL
252e386e
AM
6140 && (hdr->bfd_section->filepos != 0
6141 || (hdr->sh_type == SHT_NOBITS
6142 && hdr->contents == NULL)))
627b32bc 6143 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6144 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6145 {
1faa385f
NC
6146 if (hdr->sh_size != 0
6147 /* PR 24717 - debuginfo files are known to be not strictly
6148 compliant with the ELF standard. In particular they often
6149 have .note.gnu.property sections that are outside of any
6150 loadable segment. This is not a problem for such files,
6151 so do not warn about them. */
6152 && ! is_debuginfo_file (abfd))
4eca0228 6153 _bfd_error_handler
695344c0 6154 /* xgettext:c-format */
871b3ab2 6155 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6156 abfd,
6157 (hdr->bfd_section == NULL
6158 ? "*unknown*"
6159 : hdr->bfd_section->name));
3ba71138
L
6160 /* We don't need to page align empty sections. */
6161 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
1f9b1a84 6162 align = maxpagesize;
5c182d5f 6163 else
1f9b1a84
AM
6164 align = hdr->sh_addralign & -hdr->sh_addralign;
6165 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
5c182d5f 6166 off = _bfd_elf_assign_file_position_for_section (hdr, off,
0a1b45a2 6167 false);
5c182d5f
AM
6168 }
6169 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6170 && hdr->bfd_section == NULL)
11c6a7c6
AM
6171 /* We don't know the offset of these sections yet:
6172 their size has not been decided. */
da16cc96
AM
6173 || (abfd->is_linker_output
6174 && hdr->bfd_section != NULL
6175 && (hdr->sh_name == -1u
6176 || bfd_section_is_ctf (hdr->bfd_section)))
12bd6957 6177 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6178 || (elf_symtab_shndx_list (abfd) != NULL
6179 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6180 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6181 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6182 hdr->sh_offset = -1;
6183 else
0a1b45a2 6184 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
5c182d5f 6185 }
30fe1832 6186 elf_next_file_pos (abfd) = off;
5c182d5f 6187
252b5132
RH
6188 /* Now that we have set the section file positions, we can set up
6189 the file positions for the non PT_LOAD segments. */
f3520d2f 6190 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6191 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6192 {
129af99f 6193 if (p->p_type == PT_GNU_RELRO)
252b5132 6194 {
66631823 6195 bfd_vma start, end; /* Bytes. */
0a1b45a2 6196 bool ok;
1ea63fd2 6197
129af99f 6198 if (link_info != NULL)
8c37241b 6199 {
129af99f 6200 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6201 in link_info. Note that there may be padding between
6202 relro_start and the first RELRO section. */
6203 start = link_info->relro_start;
6204 end = link_info->relro_end;
6205 }
6206 else if (m->count != 0)
6207 {
6208 if (!m->p_size_valid)
6209 abort ();
6210 start = m->sections[0]->vma;
66631823 6211 end = start + m->p_size / opb;
f2731e0c
AM
6212 }
6213 else
6214 {
6215 start = 0;
6216 end = 0;
6217 }
6218
0a1b45a2 6219 ok = false;
f2731e0c
AM
6220 if (start < end)
6221 {
6222 struct elf_segment_map *lm;
6223 const Elf_Internal_Phdr *lp;
6224 unsigned int i;
6225
6226 /* Find a LOAD segment containing a section in the RELRO
6227 segment. */
12bd6957 6228 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6229 lm != NULL;
6230 lm = lm->next, lp++)
8c37241b
JJ
6231 {
6232 if (lp->p_type == PT_LOAD
3146fac4 6233 && lm->count != 0
dbc88fc1
AM
6234 && (lm->sections[lm->count - 1]->vma
6235 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6236 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6237 : 0)) > start
f2731e0c 6238 && lm->sections[0]->vma < end)
8c37241b
JJ
6239 break;
6240 }
f2731e0c 6241
01f7e10c 6242 if (lm != NULL)
129af99f 6243 {
01f7e10c
AM
6244 /* Find the section starting the RELRO segment. */
6245 for (i = 0; i < lm->count; i++)
6246 {
6247 asection *s = lm->sections[i];
6248 if (s->vma >= start
6249 && s->vma < end
6250 && s->size != 0)
6251 break;
6252 }
6253
6254 if (i < lm->count)
6255 {
502794d4
CE
6256 p->p_vaddr = lm->sections[i]->vma * opb;
6257 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6258 p->p_offset = lm->sections[i]->filepos;
66631823 6259 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6260 p->p_filesz = p->p_memsz;
6261
6262 /* The RELRO segment typically ends a few bytes
6263 into .got.plt but other layouts are possible.
6264 In cases where the end does not match any
6265 loaded section (for instance is in file
6266 padding), trim p_filesz back to correspond to
6267 the end of loaded section contents. */
6268 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6269 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6270
6271 /* Preserve the alignment and flags if they are
6272 valid. The gold linker generates RW/4 for
6273 the PT_GNU_RELRO section. It is better for
6274 objcopy/strip to honor these attributes
6275 otherwise gdb will choke when using separate
6276 debug files. */
6277 if (!m->p_align_valid)
6278 p->p_align = 1;
6279 if (!m->p_flags_valid)
6280 p->p_flags = PF_R;
0a1b45a2 6281 ok = true;
01f7e10c 6282 }
129af99f 6283 }
b84a33b5 6284 }
ee9e412f 6285
01f7e10c 6286 if (!ok)
ee9e412f
NC
6287 {
6288 if (link_info != NULL)
6289 _bfd_error_handler
11c6a7c6
AM
6290 (_("%pB: warning: unable to allocate any sections"
6291 " to PT_GNU_RELRO segment"),
ee9e412f
NC
6292 abfd);
6293 memset (p, 0, sizeof *p);
6294 }
129af99f 6295 }
04c3a755
NS
6296 else if (p->p_type == PT_GNU_STACK)
6297 {
6298 if (m->p_size_valid)
6299 p->p_memsz = m->p_size;
6300 }
129af99f
AS
6301 else if (m->count != 0)
6302 {
e06efbf1 6303 unsigned int i;
1a9ccd70 6304
129af99f
AS
6305 if (p->p_type != PT_LOAD
6306 && (p->p_type != PT_NOTE
6307 || bfd_get_format (abfd) != bfd_core))
6308 {
1a9ccd70
NC
6309 /* A user specified segment layout may include a PHDR
6310 segment that overlaps with a LOAD segment... */
6311 if (p->p_type == PT_PHDR)
6312 {
6313 m->count = 0;
6314 continue;
6315 }
6316
c86934ce
NC
6317 if (m->includes_filehdr || m->includes_phdrs)
6318 {
b1fa9dd6 6319 /* PR 17512: file: 2195325e. */
4eca0228 6320 _bfd_error_handler
871b3ab2 6321 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6322 "and/or program header"),
6323 abfd, (int) (p - phdrs));
0a1b45a2 6324 return false;
c86934ce 6325 }
129af99f 6326
86b2281f 6327 p->p_filesz = 0;
129af99f 6328 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6329 for (i = m->count; i-- != 0;)
6330 {
6331 asection *sect = m->sections[i];
6332 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6333 if (hdr->sh_type != SHT_NOBITS)
6334 {
aebb3d54 6335 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
9917b559
L
6336 /* NB: p_memsz of the loadable PT_NOTE segment
6337 should be the same as p_filesz. */
6338 if (p->p_type == PT_NOTE
6339 && (hdr->sh_flags & SHF_ALLOC) != 0)
6340 p->p_memsz = p->p_filesz;
86b2281f
AM
6341 break;
6342 }
6343 }
129af99f
AS
6344 }
6345 }
252b5132
RH
6346 }
6347
0a1b45a2 6348 return true;
252b5132
RH
6349}
6350
6a40cf0c
NC
6351static elf_section_list *
6352find_section_in_list (unsigned int i, elf_section_list * list)
6353{
6354 for (;list != NULL; list = list->next)
6355 if (list->ndx == i)
6356 break;
6357 return list;
6358}
6359
252b5132
RH
6360/* Work out the file positions of all the sections. This is called by
6361 _bfd_elf_compute_section_file_positions. All the section sizes and
6362 VMAs must be known before this is called.
6363
e0638f70 6364 Reloc sections come in two flavours: Those processed specially as
11c6a7c6
AM
6365 "side-channel" data attached to a section to which they apply, and
6366 those that bfd doesn't process as relocations. The latter sort are
6367 stored in a normal bfd section by bfd_section_from_shdr. We don't
6368 consider the former sort here, unless they form part of the loadable
6369 image. Reloc sections not assigned here (and compressed debugging
6370 sections and CTF sections which nothing else in the file can rely
6371 upon) will be handled later by assign_file_positions_for_relocs.
252b5132
RH
6372
6373 We also don't set the positions of the .symtab and .strtab here. */
6374
0a1b45a2 6375static bool
c84fca4d
AO
6376assign_file_positions_except_relocs (bfd *abfd,
6377 struct bfd_link_info *link_info)
252b5132 6378{
5c182d5f
AM
6379 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6380 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6381 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6382 unsigned int alloc;
252b5132
RH
6383
6384 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6385 && bfd_get_format (abfd) != bfd_core)
6386 {
5c182d5f
AM
6387 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6388 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6389 Elf_Internal_Shdr **hdrpp;
6390 unsigned int i;
a485e98e 6391 file_ptr off;
252b5132
RH
6392
6393 /* Start after the ELF header. */
6394 off = i_ehdrp->e_ehsize;
6395
6396 /* We are not creating an executable, which means that we are
6397 not creating a program header, and that the actual order of
6398 the sections in the file is unimportant. */
9ad5cbcf 6399 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6400 {
6401 Elf_Internal_Shdr *hdr;
6402
6403 hdr = *hdrpp;
e0638f70
AM
6404 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6405 && hdr->bfd_section == NULL)
1ff6de03
NA
6406 /* Do not assign offsets for these sections yet: we don't know
6407 their sizes. */
da16cc96
AM
6408 || (abfd->is_linker_output
6409 && hdr->bfd_section != NULL
6410 && (hdr->sh_name == -1u
6411 || bfd_section_is_ctf (hdr->bfd_section)))
12bd6957 6412 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6413 || (elf_symtab_shndx_list (abfd) != NULL
6414 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6415 || i == elf_strtab_sec (abfd)
6416 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6417 {
6418 hdr->sh_offset = -1;
252b5132 6419 }
9ad5cbcf 6420 else
0a1b45a2 6421 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 6422 }
a485e98e
AM
6423
6424 elf_next_file_pos (abfd) = off;
6d6c25c8 6425 elf_program_header_size (abfd) = 0;
252b5132
RH
6426 }
6427 else
6428 {
252b5132 6429 /* Assign file positions for the loaded sections based on the
08a40648 6430 assignment of sections to segments. */
f3520d2f 6431 if (!assign_file_positions_for_load_sections (abfd, link_info))
0a1b45a2 6432 return false;
f3520d2f
AM
6433
6434 /* And for non-load sections. */
6435 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
0a1b45a2 6436 return false;
6d6c25c8 6437 }
f3520d2f 6438
6d6c25c8 6439 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
0a1b45a2 6440 return false;
1a9ccd70 6441
6d6c25c8
AM
6442 /* Write out the program headers. */
6443 alloc = i_ehdrp->e_phnum;
6444 if (alloc != 0)
6445 {
ba951afb
NC
6446 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6447 {
11c6a7c6
AM
6448 /* Memory resident segments with non-zero size and RWX
6449 permissions are a security risk, so we generate a warning
6450 here if we are creating any. */
ba951afb
NC
6451 unsigned int i;
6452
6453 for (i = 0; i < alloc; i++)
6454 {
6455 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6456
6457 if (phdr->p_memsz == 0)
6458 continue;
6459
6460 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
11c6a7c6
AM
6461 _bfd_error_handler (_("warning: %pB has a TLS segment"
6462 " with execute permission"),
ba951afb
NC
6463 abfd);
6464 else if (phdr->p_type == PT_LOAD
11c6a7c6
AM
6465 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6466 == (PF_R | PF_W | PF_X)))
6467 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6468 " with RWX permissions"),
ba951afb
NC
6469 abfd);
6470 }
6471 }
11c6a7c6 6472
30fe1832 6473 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857 6474 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
0a1b45a2 6475 return false;
252b5132
RH
6476 }
6477
0a1b45a2 6478 return true;
252b5132
RH
6479}
6480
0a1b45a2 6481bool
ed7e9d0b
AM
6482_bfd_elf_init_file_header (bfd *abfd,
6483 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6484{
3d540e93 6485 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6486 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6487 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6488
6489 i_ehdrp = elf_elfheader (abfd);
252b5132 6490
2b0f7ef9 6491 shstrtab = _bfd_elf_strtab_init ();
252b5132 6492 if (shstrtab == NULL)
0a1b45a2 6493 return false;
252b5132
RH
6494
6495 elf_shstrtab (abfd) = shstrtab;
6496
6497 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6498 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6499 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6500 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6501
6502 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6503 i_ehdrp->e_ident[EI_DATA] =
6504 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6505 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6506
252b5132
RH
6507 if ((abfd->flags & DYNAMIC) != 0)
6508 i_ehdrp->e_type = ET_DYN;
6509 else if ((abfd->flags & EXEC_P) != 0)
6510 i_ehdrp->e_type = ET_EXEC;
6511 else if (bfd_get_format (abfd) == bfd_core)
6512 i_ehdrp->e_type = ET_CORE;
6513 else
6514 i_ehdrp->e_type = ET_REL;
6515
6516 switch (bfd_get_arch (abfd))
6517 {
6518 case bfd_arch_unknown:
6519 i_ehdrp->e_machine = EM_NONE;
6520 break;
aa4f99bb
AO
6521
6522 /* There used to be a long list of cases here, each one setting
6523 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6524 in the corresponding bfd definition. To avoid duplication,
6525 the switch was removed. Machines that need special handling
6526 can generally do it in elf_backend_final_write_processing(),
6527 unless they need the information earlier than the final write.
6528 Such need can generally be supplied by replacing the tests for
6529 e_machine with the conditions used to determine it. */
252b5132 6530 default:
9c5bfbb7
AM
6531 i_ehdrp->e_machine = bed->elf_machine_code;
6532 }
aa4f99bb 6533
252b5132
RH
6534 i_ehdrp->e_version = bed->s->ev_current;
6535 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6536
c044fabd 6537 /* No program header, for now. */
252b5132
RH
6538 i_ehdrp->e_phoff = 0;
6539 i_ehdrp->e_phentsize = 0;
6540 i_ehdrp->e_phnum = 0;
6541
c044fabd 6542 /* Each bfd section is section header entry. */
252b5132
RH
6543 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6544 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6545
252b5132 6546 elf_tdata (abfd)->symtab_hdr.sh_name =
0a1b45a2 6547 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 6548 elf_tdata (abfd)->strtab_hdr.sh_name =
0a1b45a2 6549 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 6550 elf_tdata (abfd)->shstrtab_hdr.sh_name =
0a1b45a2 6551 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132 6552 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6553 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6554 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
0a1b45a2 6555 return false;
252b5132 6556
0a1b45a2 6557 return true;
252b5132
RH
6558}
6559
6d6c25c8
AM
6560/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6561
6562 FIXME: We used to have code here to sort the PT_LOAD segments into
6563 ascending order, as per the ELF spec. But this breaks some programs,
6564 including the Linux kernel. But really either the spec should be
6565 changed or the programs updated. */
6566
0a1b45a2 6567bool
6d6c25c8
AM
6568_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6569{
6570 if (link_info != NULL && bfd_link_pie (link_info))
6571 {
6572 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6573 unsigned int num_segments = i_ehdrp->e_phnum;
6574 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6575 Elf_Internal_Phdr *segment = tdata->phdr;
6576 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6577
6578 /* Find the lowest p_vaddr in PT_LOAD segments. */
6579 bfd_vma p_vaddr = (bfd_vma) -1;
6580 for (; segment < end_segment; segment++)
6581 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6582 p_vaddr = segment->p_vaddr;
6583
6584 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6585 segments is non-zero. */
6586 if (p_vaddr)
6587 i_ehdrp->e_type = ET_EXEC;
6588 }
0a1b45a2 6589 return true;
6d6c25c8
AM
6590}
6591
252b5132 6592/* Assign file positions for all the reloc sections which are not part
a485e98e 6593 of the loadable file image, and the file position of section headers. */
252b5132 6594
0a1b45a2 6595static bool
0ce398f1 6596_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6597{
6598 file_ptr off;
e06efbf1 6599 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6600 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6601 Elf_Internal_Ehdr *i_ehdrp;
6602 const struct elf_backend_data *bed;
252b5132 6603
12bd6957 6604 off = elf_next_file_pos (abfd);
252b5132 6605
e06efbf1
L
6606 shdrpp = elf_elfsections (abfd);
6607 end_shdrpp = shdrpp + elf_numsections (abfd);
6608 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6609 {
252b5132 6610 shdrp = *shdrpp;
0ce398f1
L
6611 if (shdrp->sh_offset == -1)
6612 {
3e19fb8f 6613 asection *sec = shdrp->bfd_section;
da16cc96
AM
6614 if (sec == NULL
6615 || shdrp->sh_type == SHT_REL
6616 || shdrp->sh_type == SHT_RELA)
6617 ;
6618 else if (bfd_section_is_ctf (sec))
0ce398f1 6619 {
da16cc96
AM
6620 /* Update section size and contents. */
6621 shdrp->sh_size = sec->size;
6622 shdrp->contents = sec->contents;
6623 }
6624 else if (shdrp->sh_name == -1u)
6625 {
6626 const char *name = sec->name;
6627 struct bfd_elf_section_data *d;
3e19fb8f 6628
da16cc96
AM
6629 /* Compress DWARF debug sections. */
6630 if (!bfd_compress_section (abfd, sec, shdrp->contents))
6631 return false;
3e19fb8f 6632
da16cc96
AM
6633 if (sec->compress_status == COMPRESS_SECTION_DONE
6634 && (abfd->flags & BFD_COMPRESS_GABI) == 0
6635 && name[1] == 'd')
1ff6de03 6636 {
da16cc96
AM
6637 /* If section is compressed with zlib-gnu, convert
6638 section name from .debug_* to .zdebug_*. */
6639 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
6640 if (new_name == NULL)
6641 return false;
6642 name = new_name;
1ff6de03 6643 }
da16cc96
AM
6644 /* Add section name to section name section. */
6645 shdrp->sh_name
6646 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6647 name, false);
6648 d = elf_section_data (sec);
6649
6650 /* Add reloc section name to section name section. */
6651 if (d->rel.hdr
6652 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6653 name, false))
6654 return false;
6655 if (d->rela.hdr
6656 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6657 name, true))
6658 return false;
1ff6de03 6659
da16cc96
AM
6660 /* Update section size and contents. */
6661 shdrp->sh_size = sec->size;
6662 shdrp->contents = sec->contents;
6663 sec->contents = NULL;
0ce398f1 6664 }
da16cc96
AM
6665
6666 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
0ce398f1 6667 }
252b5132
RH
6668 }
6669
3e19fb8f
L
6670 /* Place section name section after DWARF debug sections have been
6671 compressed. */
6672 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6673 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6674 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
0a1b45a2 6675 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3e19fb8f
L
6676
6677 /* Place the section headers. */
a485e98e
AM
6678 i_ehdrp = elf_elfheader (abfd);
6679 bed = get_elf_backend_data (abfd);
6680 off = align_file_position (off, 1 << bed->s->log_file_align);
6681 i_ehdrp->e_shoff = off;
6682 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6683 elf_next_file_pos (abfd) = off;
0ce398f1 6684
0a1b45a2 6685 return true;
252b5132
RH
6686}
6687
0a1b45a2 6688bool
217aa764 6689_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6690{
9c5bfbb7 6691 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6692 Elf_Internal_Shdr **i_shdrp;
0a1b45a2 6693 bool failed;
9ad5cbcf 6694 unsigned int count, num_sec;
30e8ee25 6695 struct elf_obj_tdata *t;
252b5132
RH
6696
6697 if (! abfd->output_has_begun
217aa764 6698 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 6699 return false;
db727370 6700 /* Do not rewrite ELF data when the BFD has been opened for update.
11c6a7c6
AM
6701 abfd->output_has_begun was set to TRUE on opening, so creation of
6702 new sections, and modification of existing section sizes was
6703 restricted. This means the ELF header, program headers and
6704 section headers can't have changed. If the contents of any
6705 sections has been modified, then those changes have already been
6706 written to the BFD. */
db727370
JL
6707 else if (abfd->direction == both_direction)
6708 {
6709 BFD_ASSERT (abfd->output_has_begun);
0a1b45a2 6710 return true;
db727370 6711 }
252b5132
RH
6712
6713 i_shdrp = elf_elfsections (abfd);
252b5132 6714
0a1b45a2 6715 failed = false;
252b5132
RH
6716 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6717 if (failed)
0a1b45a2 6718 return false;
252b5132 6719
0ce398f1 6720 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
0a1b45a2 6721 return false;
252b5132 6722
c044fabd 6723 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6724 num_sec = elf_numsections (abfd);
6725 for (count = 1; count < num_sec; count++)
252b5132 6726 {
3e19fb8f
L
6727 i_shdrp[count]->sh_name
6728 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6729 i_shdrp[count]->sh_name);
252b5132 6730 if (bed->elf_backend_section_processing)
75506100 6731 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
0a1b45a2 6732 return false;
252b5132
RH
6733 if (i_shdrp[count]->contents)
6734 {
dc810e39
AM
6735 bfd_size_type amt = i_shdrp[count]->sh_size;
6736
252b5132 6737 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6738 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
0a1b45a2 6739 return false;
252b5132
RH
6740 }
6741 }
6742
6743 /* Write out the section header names. */
30e8ee25 6744 t = elf_tdata (abfd);
26ae6d5e 6745 if (elf_shstrtab (abfd) != NULL
30e8ee25 6746 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6747 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
0a1b45a2 6748 return false;
252b5132 6749
cc364be6 6750 if (!(*bed->elf_backend_final_write_processing) (abfd))
0a1b45a2 6751 return false;
252b5132 6752
ff59fc36 6753 if (!bed->s->write_shdrs_and_ehdr (abfd))
0a1b45a2 6754 return false;
ff59fc36
RM
6755
6756 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
9e2bb0cb
LB
6757 if (t->o->build_id.after_write_object_contents != NULL
6758 && !(*t->o->build_id.after_write_object_contents) (abfd))
6759 return false;
6760 if (t->o->package_metadata.after_write_object_contents != NULL
6761 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6762 return false;
ff59fc36 6763
0a1b45a2 6764 return true;
252b5132
RH
6765}
6766
0a1b45a2 6767bool
217aa764 6768_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6769{
c044fabd 6770 /* Hopefully this can be done just like an object file. */
252b5132
RH
6771 return _bfd_elf_write_object_contents (abfd);
6772}
c044fabd
KH
6773
6774/* Given a section, search the header to find them. */
6775
cb33740c 6776unsigned int
198beae2 6777_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6778{
9c5bfbb7 6779 const struct elf_backend_data *bed;
91d6fa6a 6780 unsigned int sec_index;
252b5132 6781
9ad5cbcf
AM
6782 if (elf_section_data (asect) != NULL
6783 && elf_section_data (asect)->this_idx != 0)
6784 return elf_section_data (asect)->this_idx;
6785
6786 if (bfd_is_abs_section (asect))
91d6fa6a 6787 sec_index = SHN_ABS;
af746e92 6788 else if (bfd_is_com_section (asect))
91d6fa6a 6789 sec_index = SHN_COMMON;
af746e92 6790 else if (bfd_is_und_section (asect))
91d6fa6a 6791 sec_index = SHN_UNDEF;
af746e92 6792 else
91d6fa6a 6793 sec_index = SHN_BAD;
252b5132 6794
af746e92 6795 bed = get_elf_backend_data (abfd);
252b5132
RH
6796 if (bed->elf_backend_section_from_bfd_section)
6797 {
91d6fa6a 6798 int retval = sec_index;
9ad5cbcf 6799
af746e92
AM
6800 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6801 return retval;
252b5132
RH
6802 }
6803
91d6fa6a 6804 if (sec_index == SHN_BAD)
af746e92 6805 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6806
91d6fa6a 6807 return sec_index;
252b5132
RH
6808}
6809
6810/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6811 on error. */
6812
6813int
217aa764 6814_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6815{
6816 asymbol *asym_ptr = *asym_ptr_ptr;
6817 int idx;
6818 flagword flags = asym_ptr->flags;
6819
6820 /* When gas creates relocations against local labels, it creates its
6821 own symbol for the section, but does put the symbol into the
6822 symbol chain, so udata is 0. When the linker is generating
6823 relocatable output, this section symbol may be for one of the
6824 input sections rather than the output section. */
6825 if (asym_ptr->udata.i == 0
6826 && (flags & BSF_SECTION_SYM)
6827 && asym_ptr->section)
6828 {
5372391b 6829 asection *sec;
252b5132 6830
5372391b
AM
6831 sec = asym_ptr->section;
6832 if (sec->owner != abfd && sec->output_section != NULL)
6833 sec = sec->output_section;
6834 if (sec->owner == abfd
27e3da31
AM
6835 && sec->index < elf_num_section_syms (abfd)
6836 && elf_section_syms (abfd)[sec->index] != NULL)
6837 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
252b5132
RH
6838 }
6839
6840 idx = asym_ptr->udata.i;
6841
6842 if (idx == 0)
6843 {
6844 /* This case can occur when using --strip-symbol on a symbol
08a40648 6845 which is used in a relocation entry. */
4eca0228 6846 _bfd_error_handler
695344c0 6847 /* xgettext:c-format */
871b3ab2 6848 (_("%pB: symbol `%s' required but not present"),
d003868e 6849 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6850 bfd_set_error (bfd_error_no_symbols);
6851 return -1;
6852 }
6853
6854#if DEBUG & 4
6855 {
6856 fprintf (stderr,
11c6a7c6
AM
6857 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6858 " flags = 0x%.8x\n",
cd9af601 6859 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6860 fflush (stderr);
6861 }
6862#endif
6863
6864 return idx;
6865}
6866
45d92439
AM
6867static inline bfd_vma
6868segment_size (Elf_Internal_Phdr *segment)
6869{
6870 return (segment->p_memsz > segment->p_filesz
6871 ? segment->p_memsz : segment->p_filesz);
6872}
6873
6874
6875/* Returns the end address of the segment + 1. */
6876static inline bfd_vma
6877segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6878{
6879 return start + segment_size (segment);
6880}
6881
6882static inline bfd_size_type
6883section_size (asection *section, Elf_Internal_Phdr *segment)
6884{
6885 if ((section->flags & SEC_HAS_CONTENTS) != 0
6886 || (section->flags & SEC_THREAD_LOCAL) == 0
6887 || segment->p_type == PT_TLS)
6888 return section->size;
6889 return 0;
6890}
6891
6892/* Returns TRUE if the given section is contained within the given
6893 segment. LMA addresses are compared against PADDR when
5f51eb93 6894 USE_VADDR is false, VMA against VADDR when true. */
45d92439
AM
6895static bool
6896is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6897 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
5f51eb93 6898 bool use_vaddr)
45d92439 6899{
5f51eb93
AM
6900 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
6901 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
45d92439
AM
6902 bfd_vma octet;
6903 if (_bfd_mul_overflow (addr, opb, &octet))
6904 return false;
6905 /* The third and fourth lines below are testing that the section end
6906 address is within the segment. It's written this way to avoid
6907 overflow. Add seg_addr + section_size to both sides of the
6908 inequality to make it obvious. */
6909 return (octet >= seg_addr
6910 && segment_size (segment) >= section_size (section, segment)
6911 && (octet - seg_addr
6912 <= segment_size (segment) - section_size (section, segment)));
6913}
6914
6915/* Handle PT_NOTE segment. */
6916static bool
11c6a7c6 6917is_note (asection *s, Elf_Internal_Phdr *p)
45d92439
AM
6918{
6919 return (p->p_type == PT_NOTE
6920 && elf_section_type (s) == SHT_NOTE
6921 && (ufile_ptr) s->filepos >= p->p_offset
6922 && p->p_filesz >= s->size
11c6a7c6 6923 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
45d92439
AM
6924}
6925
84d1d650 6926/* Rewrite program header information. */
252b5132 6927
0a1b45a2 6928static bool
c410035d 6929rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6930{
b34976b6
AM
6931 Elf_Internal_Ehdr *iehdr;
6932 struct elf_segment_map *map;
6933 struct elf_segment_map *map_first;
6934 struct elf_segment_map **pointer_to_map;
6935 Elf_Internal_Phdr *segment;
6936 asection *section;
6937 unsigned int i;
6938 unsigned int num_segments;
0a1b45a2
AM
6939 bool phdr_included = false;
6940 bool p_paddr_valid;
b34976b6
AM
6941 struct elf_segment_map *phdr_adjust_seg = NULL;
6942 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6943 const struct elf_backend_data *bed;
502794d4 6944 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6945
caf47ea6 6946 bed = get_elf_backend_data (ibfd);
252b5132
RH
6947 iehdr = elf_elfheader (ibfd);
6948
bc67d8a6 6949 map_first = NULL;
c044fabd 6950 pointer_to_map = &map_first;
252b5132
RH
6951
6952 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6 6953
252b5132
RH
6954 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6955 linker, which generates a PT_INTERP section with p_vaddr and
6956 p_memsz set to 0. */
aecc8f8a
AM
6957#define IS_SOLARIS_PT_INTERP(p, s) \
6958 (p->p_vaddr == 0 \
6959 && p->p_paddr == 0 \
6960 && p->p_memsz == 0 \
6961 && p->p_filesz > 0 \
6962 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6963 && s->size > 0 \
aecc8f8a 6964 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6965 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6966 <= p->p_offset + p->p_filesz))
5c440b1e 6967
bc67d8a6
NC
6968 /* Decide if the given section should be included in the given segment.
6969 A section will be included if:
f5ffc919 6970 1. It is within the address space of the segment -- we use the LMA
08a40648 6971 if that is set for the segment and the VMA otherwise,
0efc80c8 6972 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6973 segment.
bc67d8a6 6974 3. There is an output section associated with it,
eecdbe52 6975 4. The section has not already been allocated to a previous segment.
2b05f1b7 6976 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6977 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6978 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6979 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6980 (with the possible exception of .dynamic). */
502794d4 6981#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
45d92439 6982 (((is_contained_by (section, segment, segment->p_paddr, \
5f51eb93
AM
6983 segment->p_vaddr, opb, \
6984 bed->want_p_paddr_set_to_zero) \
2b05f1b7 6985 && (section->flags & SEC_ALLOC) != 0) \
11c6a7c6 6986 || is_note (section, segment)) \
2b05f1b7
L
6987 && segment->p_type != PT_GNU_STACK \
6988 && (segment->p_type != PT_TLS \
6989 || (section->flags & SEC_THREAD_LOCAL)) \
6990 && (segment->p_type == PT_LOAD \
6991 || segment->p_type == PT_TLS \
6992 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6993 && (segment->p_type != PT_DYNAMIC \
45d92439 6994 || section_size (section, segment) > 0 \
2b05f1b7 6995 || (segment->p_paddr \
502794d4
CE
6996 ? segment->p_paddr != section->lma * (opb) \
6997 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6998 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6999 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 7000
9f17e2a6
L
7001/* If the output section of a section in the input segment is NULL,
7002 it is removed from the corresponding output segment. */
502794d4
CE
7003#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7004 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
7005 && section->output_section != NULL)
7006
b34976b6 7007 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea 7008#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
45d92439 7009 (seg1->field >= segment_end (seg2, seg2->field))
b5f852ea
NC
7010
7011 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7012 their VMA address ranges and their LMA address ranges overlap.
7013 It is possible to have overlapping VMA ranges without overlapping LMA
7014 ranges. RedBoot images for example can have both .data and .bss mapped
7015 to the same VMA range, but with the .data section mapped to a different
7016 LMA. */
aecc8f8a 7017#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 7018 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 7019 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 7020 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 7021 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6 7022
c82ebeb7 7023 /* Initialise the segment mark field, and discard stupid alignment. */
bc67d8a6 7024 for (section = ibfd->sections; section != NULL; section = section->next)
c82ebeb7
AM
7025 {
7026 asection *o = section->output_section;
7027 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7028 o->alignment_power = 0;
7029 section->segment_mark = false;
7030 }
bc67d8a6 7031
5c44b38e
AM
7032 /* The Solaris linker creates program headers in which all the
7033 p_paddr fields are zero. When we try to objcopy or strip such a
7034 file, we get confused. Check for this case, and if we find it
7035 don't set the p_paddr_valid fields. */
0a1b45a2 7036 p_paddr_valid = false;
5c44b38e
AM
7037 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7038 i < num_segments;
7039 i++, segment++)
7040 if (segment->p_paddr != 0)
7041 {
0a1b45a2 7042 p_paddr_valid = true;
5c44b38e
AM
7043 break;
7044 }
7045
252b5132 7046 /* Scan through the segments specified in the program header
bc67d8a6 7047 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 7048 in the loadable segments. These can be created by weird
aecc8f8a 7049 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
7050 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7051 i < num_segments;
c044fabd 7052 i++, segment++)
252b5132 7053 {
252b5132 7054 unsigned int j;
c044fabd 7055 Elf_Internal_Phdr *segment2;
252b5132 7056
aecc8f8a
AM
7057 if (segment->p_type == PT_INTERP)
7058 for (section = ibfd->sections; section; section = section->next)
7059 if (IS_SOLARIS_PT_INTERP (segment, section))
7060 {
7061 /* Mininal change so that the normal section to segment
4cc11e76 7062 assignment code will work. */
502794d4 7063 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
7064 break;
7065 }
7066
bc67d8a6 7067 if (segment->p_type != PT_LOAD)
b10a8ae0
L
7068 {
7069 /* Remove PT_GNU_RELRO segment. */
7070 if (segment->p_type == PT_GNU_RELRO)
7071 segment->p_type = PT_NULL;
7072 continue;
7073 }
c044fabd 7074
bc67d8a6 7075 /* Determine if this segment overlaps any previous segments. */
0067a569 7076 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7077 {
7078 bfd_signed_vma extra_length;
c044fabd 7079
bc67d8a6 7080 if (segment2->p_type != PT_LOAD
0067a569 7081 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7082 continue;
c044fabd 7083
bc67d8a6
NC
7084 /* Merge the two segments together. */
7085 if (segment2->p_vaddr < segment->p_vaddr)
7086 {
c044fabd 7087 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7088 SEGMENT. */
45d92439
AM
7089 extra_length = (segment_end (segment, segment->p_vaddr)
7090 - segment_end (segment2, segment2->p_vaddr));
c044fabd 7091
bc67d8a6
NC
7092 if (extra_length > 0)
7093 {
0067a569 7094 segment2->p_memsz += extra_length;
bc67d8a6
NC
7095 segment2->p_filesz += extra_length;
7096 }
c044fabd 7097
bc67d8a6 7098 segment->p_type = PT_NULL;
c044fabd 7099
bc67d8a6
NC
7100 /* Since we have deleted P we must restart the outer loop. */
7101 i = 0;
7102 segment = elf_tdata (ibfd)->phdr;
7103 break;
7104 }
7105 else
7106 {
c044fabd 7107 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7108 SEGMENT2. */
45d92439
AM
7109 extra_length = (segment_end (segment2, segment2->p_vaddr)
7110 - segment_end (segment, segment->p_vaddr));
c044fabd 7111
bc67d8a6
NC
7112 if (extra_length > 0)
7113 {
0067a569 7114 segment->p_memsz += extra_length;
bc67d8a6
NC
7115 segment->p_filesz += extra_length;
7116 }
c044fabd 7117
bc67d8a6
NC
7118 segment2->p_type = PT_NULL;
7119 }
7120 }
7121 }
c044fabd 7122
bc67d8a6
NC
7123 /* The second scan attempts to assign sections to segments. */
7124 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7125 i < num_segments;
0067a569 7126 i++, segment++)
bc67d8a6 7127 {
0067a569
AM
7128 unsigned int section_count;
7129 asection **sections;
7130 asection *output_section;
7131 unsigned int isec;
9933dc52
AM
7132 asection *matching_lma;
7133 asection *suggested_lma;
0067a569 7134 unsigned int j;
446f7ed5 7135 size_t amt;
0067a569 7136 asection *first_section;
bc67d8a6
NC
7137
7138 if (segment->p_type == PT_NULL)
7139 continue;
c044fabd 7140
9f17e2a6 7141 first_section = NULL;
bc67d8a6 7142 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7143 for (section = ibfd->sections, section_count = 0;
7144 section != NULL;
7145 section = section->next)
9f17e2a6
L
7146 {
7147 /* Find the first section in the input segment, which may be
7148 removed from the corresponding output segment. */
502794d4 7149 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7150 {
7151 if (first_section == NULL)
7152 first_section = section;
7153 if (section->output_section != NULL)
7154 ++section_count;
7155 }
7156 }
811072d8 7157
b5f852ea
NC
7158 /* Allocate a segment map big enough to contain
7159 all of the sections we have selected. */
00bee008 7160 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7161 amt += section_count * sizeof (asection *);
a50b1753 7162 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7163 if (map == NULL)
0a1b45a2 7164 return false;
252b5132
RH
7165
7166 /* Initialise the fields of the segment map. Default to
7167 using the physical address of the segment in the input BFD. */
0067a569
AM
7168 map->next = NULL;
7169 map->p_type = segment->p_type;
7170 map->p_flags = segment->p_flags;
bc67d8a6 7171 map->p_flags_valid = 1;
55d55ac7 7172
c410035d
AM
7173 if (map->p_type == PT_LOAD
7174 && (ibfd->flags & D_PAGED) != 0
7175 && maxpagesize > 1
7176 && segment->p_align > 1)
7177 {
7178 map->p_align = segment->p_align;
7179 if (segment->p_align > maxpagesize)
7180 map->p_align = maxpagesize;
7181 map->p_align_valid = 1;
7182 }
7183
9f17e2a6
L
7184 /* If the first section in the input segment is removed, there is
7185 no need to preserve segment physical address in the corresponding
7186 output segment. */
945c025a 7187 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7188 {
7189 map->p_paddr = segment->p_paddr;
5c44b38e 7190 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7191 }
252b5132
RH
7192
7193 /* Determine if this segment contains the ELF file header
7194 and if it contains the program headers themselves. */
bc67d8a6
NC
7195 map->includes_filehdr = (segment->p_offset == 0
7196 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7197 map->includes_phdrs = 0;
252b5132 7198
0067a569 7199 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7200 {
bc67d8a6
NC
7201 map->includes_phdrs =
7202 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7203 && (segment->p_offset + segment->p_filesz
252b5132
RH
7204 >= ((bfd_vma) iehdr->e_phoff
7205 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7206
bc67d8a6 7207 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7208 phdr_included = true;
252b5132
RH
7209 }
7210
bc67d8a6 7211 if (section_count == 0)
252b5132
RH
7212 {
7213 /* Special segments, such as the PT_PHDR segment, may contain
7214 no sections, but ordinary, loadable segments should contain
1ed89aa9 7215 something. They are allowed by the ELF spec however, so only
07d6d2b8 7216 a warning is produced.
3f60b982 7217 Don't warn if an empty PT_LOAD contains the program headers.
f98450c6
NC
7218 There is however the valid use case of embedded systems which
7219 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7220 flash memory with zeros. No warning is shown for that case. */
7221 if (segment->p_type == PT_LOAD
3f60b982 7222 && !map->includes_phdrs
f98450c6
NC
7223 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7224 /* xgettext:c-format */
9793eb77
AM
7225 _bfd_error_handler
7226 (_("%pB: warning: empty loadable segment detected"
7227 " at vaddr=%#" PRIx64 ", is this intentional?"),
7228 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7229
502794d4 7230 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7231 map->count = 0;
c044fabd
KH
7232 *pointer_to_map = map;
7233 pointer_to_map = &map->next;
252b5132
RH
7234
7235 continue;
7236 }
7237
7238 /* Now scan the sections in the input BFD again and attempt
7239 to add their corresponding output sections to the segment map.
7240 The problem here is how to handle an output section which has
7241 been moved (ie had its LMA changed). There are four possibilities:
7242
7243 1. None of the sections have been moved.
7244 In this case we can continue to use the segment LMA from the
7245 input BFD.
7246
7247 2. All of the sections have been moved by the same amount.
7248 In this case we can change the segment's LMA to match the LMA
7249 of the first section.
7250
7251 3. Some of the sections have been moved, others have not.
7252 In this case those sections which have not been moved can be
7253 placed in the current segment which will have to have its size,
7254 and possibly its LMA changed, and a new segment or segments will
7255 have to be created to contain the other sections.
7256
b5f852ea 7257 4. The sections have been moved, but not by the same amount.
252b5132
RH
7258 In this case we can change the segment's LMA to match the LMA
7259 of the first section and we will have to create a new segment
7260 or segments to contain the other sections.
7261
7262 In order to save time, we allocate an array to hold the section
7263 pointers that we are interested in. As these sections get assigned
7264 to a segment, they are removed from this array. */
7265
446f7ed5
AM
7266 amt = section_count * sizeof (asection *);
7267 sections = (asection **) bfd_malloc (amt);
252b5132 7268 if (sections == NULL)
0a1b45a2 7269 return false;
252b5132
RH
7270
7271 /* Step One: Scan for segment vs section LMA conflicts.
7272 Also add the sections to the section array allocated above.
7273 Also add the sections to the current segment. In the common
7274 case, where the sections have not been moved, this means that
7275 we have completely filled the segment, and there is nothing
7276 more to do. */
252b5132 7277 isec = 0;
9933dc52
AM
7278 matching_lma = NULL;
7279 suggested_lma = NULL;
252b5132 7280
461c4b2e 7281 for (section = first_section, j = 0;
bc67d8a6
NC
7282 section != NULL;
7283 section = section->next)
252b5132 7284 {
502794d4 7285 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7286 {
bc67d8a6
NC
7287 output_section = section->output_section;
7288
0067a569 7289 sections[j++] = section;
252b5132
RH
7290
7291 /* The Solaris native linker always sets p_paddr to 0.
7292 We try to catch that case here, and set it to the
5e8d7549
NC
7293 correct value. Note - some backends require that
7294 p_paddr be left as zero. */
5c44b38e 7295 if (!p_paddr_valid
4455705d 7296 && segment->p_vaddr != 0
0067a569 7297 && !bed->want_p_paddr_set_to_zero
252b5132 7298 && isec == 0
bc67d8a6 7299 && output_section->lma != 0
9933dc52
AM
7300 && (align_power (segment->p_vaddr
7301 + (map->includes_filehdr
7302 ? iehdr->e_ehsize : 0)
7303 + (map->includes_phdrs
7304 ? iehdr->e_phnum * iehdr->e_phentsize
7305 : 0),
66631823
CE
7306 output_section->alignment_power * opb)
7307 == (output_section->vma * opb)))
bc67d8a6 7308 map->p_paddr = segment->p_vaddr;
252b5132
RH
7309
7310 /* Match up the physical address of the segment with the
7311 LMA address of the output section. */
45d92439 7312 if (is_contained_by (output_section, segment, map->p_paddr,
5f51eb93 7313 0, opb, false)
11c6a7c6 7314 || is_note (section, segment))
252b5132 7315 {
9933dc52
AM
7316 if (matching_lma == NULL
7317 || output_section->lma < matching_lma->lma)
7318 matching_lma = output_section;
252b5132
RH
7319
7320 /* We assume that if the section fits within the segment
bc67d8a6 7321 then it does not overlap any other section within that
252b5132 7322 segment. */
0067a569
AM
7323 map->sections[isec++] = output_section;
7324 }
9933dc52
AM
7325 else if (suggested_lma == NULL)
7326 suggested_lma = output_section;
147d51c2
L
7327
7328 if (j == section_count)
7329 break;
252b5132
RH
7330 }
7331 }
7332
bc67d8a6 7333 BFD_ASSERT (j == section_count);
252b5132
RH
7334
7335 /* Step Two: Adjust the physical address of the current segment,
7336 if necessary. */
bc67d8a6 7337 if (isec == section_count)
252b5132
RH
7338 {
7339 /* All of the sections fitted within the segment as currently
7340 specified. This is the default case. Add the segment to
7341 the list of built segments and carry on to process the next
7342 program header in the input BFD. */
bc67d8a6 7343 map->count = section_count;
c044fabd
KH
7344 *pointer_to_map = map;
7345 pointer_to_map = &map->next;
08a40648 7346
5c44b38e 7347 if (p_paddr_valid
30fe1832
AM
7348 && !bed->want_p_paddr_set_to_zero)
7349 {
7350 bfd_vma hdr_size = 0;
7351 if (map->includes_filehdr)
7352 hdr_size = iehdr->e_ehsize;
7353 if (map->includes_phdrs)
7354 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7355
7356 /* Account for padding before the first section in the
7357 segment. */
502794d4
CE
7358 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7359 - matching_lma->lma);
30fe1832 7360 }
08a40648 7361
252b5132
RH
7362 free (sections);
7363 continue;
7364 }
252b5132
RH
7365 else
7366 {
9933dc52
AM
7367 /* Change the current segment's physical address to match
7368 the LMA of the first section that fitted, or if no
7369 section fitted, the first section. */
7370 if (matching_lma == NULL)
7371 matching_lma = suggested_lma;
7372
66631823 7373 map->p_paddr = matching_lma->lma * opb;
72730e0c 7374
bc67d8a6
NC
7375 /* Offset the segment physical address from the lma
7376 to allow for space taken up by elf headers. */
9933dc52 7377 if (map->includes_phdrs)
010c8431 7378 {
9933dc52
AM
7379 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7380
7381 /* iehdr->e_phnum is just an estimate of the number
7382 of program headers that we will need. Make a note
7383 here of the number we used and the segment we chose
7384 to hold these headers, so that we can adjust the
7385 offset when we know the correct value. */
7386 phdr_adjust_num = iehdr->e_phnum;
7387 phdr_adjust_seg = map;
010c8431 7388 }
252b5132 7389
9933dc52 7390 if (map->includes_filehdr)
bc67d8a6 7391 {
9933dc52
AM
7392 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7393 map->p_paddr -= iehdr->e_ehsize;
7394 /* We've subtracted off the size of headers from the
7395 first section lma, but there may have been some
7396 alignment padding before that section too. Try to
7397 account for that by adjusting the segment lma down to
7398 the same alignment. */
7399 if (segment->p_align != 0 && segment->p_align < align)
7400 align = segment->p_align;
66631823 7401 map->p_paddr &= -(align * opb);
bc67d8a6 7402 }
252b5132
RH
7403 }
7404
7405 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7406 those that fit to the current segment and removing them from the
252b5132
RH
7407 sections array; but making sure not to leave large gaps. Once all
7408 possible sections have been assigned to the current segment it is
7409 added to the list of built segments and if sections still remain
7410 to be assigned, a new segment is constructed before repeating
7411 the loop. */
7412 isec = 0;
7413 do
7414 {
bc67d8a6 7415 map->count = 0;
9933dc52 7416 suggested_lma = NULL;
252b5132
RH
7417
7418 /* Fill the current segment with sections that fit. */
bc67d8a6 7419 for (j = 0; j < section_count; j++)
252b5132 7420 {
bc67d8a6 7421 section = sections[j];
252b5132 7422
bc67d8a6 7423 if (section == NULL)
252b5132
RH
7424 continue;
7425
bc67d8a6 7426 output_section = section->output_section;
252b5132 7427
bc67d8a6 7428 BFD_ASSERT (output_section != NULL);
c044fabd 7429
45d92439 7430 if (is_contained_by (output_section, segment, map->p_paddr,
5f51eb93 7431 0, opb, false)
11c6a7c6 7432 || is_note (section, segment))
252b5132 7433 {
bc67d8a6 7434 if (map->count == 0)
252b5132
RH
7435 {
7436 /* If the first section in a segment does not start at
bc67d8a6
NC
7437 the beginning of the segment, then something is
7438 wrong. */
9933dc52
AM
7439 if (align_power (map->p_paddr
7440 + (map->includes_filehdr
7441 ? iehdr->e_ehsize : 0)
7442 + (map->includes_phdrs
7443 ? iehdr->e_phnum * iehdr->e_phentsize
7444 : 0),
66631823
CE
7445 output_section->alignment_power * opb)
7446 != output_section->lma * opb)
9aea1e31 7447 goto sorry;
252b5132
RH
7448 }
7449 else
7450 {
0067a569 7451 asection *prev_sec;
252b5132 7452
bc67d8a6 7453 prev_sec = map->sections[map->count - 1];
252b5132
RH
7454
7455 /* If the gap between the end of the previous section
bc67d8a6
NC
7456 and the start of this section is more than
7457 maxpagesize then we need to start a new segment. */
eea6121a 7458 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7459 maxpagesize)
caf47ea6 7460 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7461 || (prev_sec->lma + prev_sec->size
079e9a2f 7462 > output_section->lma))
252b5132 7463 {
9933dc52
AM
7464 if (suggested_lma == NULL)
7465 suggested_lma = output_section;
252b5132
RH
7466
7467 continue;
7468 }
7469 }
7470
bc67d8a6 7471 map->sections[map->count++] = output_section;
252b5132
RH
7472 ++isec;
7473 sections[j] = NULL;
9933dc52 7474 if (segment->p_type == PT_LOAD)
0a1b45a2 7475 section->segment_mark = true;
0067a569 7476 }
9933dc52
AM
7477 else if (suggested_lma == NULL)
7478 suggested_lma = output_section;
252b5132
RH
7479 }
7480
beab4532
NC
7481 /* PR 23932. A corrupt input file may contain sections that cannot
7482 be assigned to any segment - because for example they have a
9984857c
NC
7483 negative size - or segments that do not contain any sections.
7484 But there are also valid reasons why a segment can be empty.
7485 So allow a count of zero. */
252b5132
RH
7486
7487 /* Add the current segment to the list of built segments. */
c044fabd
KH
7488 *pointer_to_map = map;
7489 pointer_to_map = &map->next;
252b5132 7490
bc67d8a6 7491 if (isec < section_count)
252b5132
RH
7492 {
7493 /* We still have not allocated all of the sections to
7494 segments. Create a new segment here, initialise it
7495 and carry on looping. */
00bee008 7496 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7497 amt += section_count * sizeof (asection *);
5964fc3a 7498 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7499 if (map == NULL)
5ed6aba4
NC
7500 {
7501 free (sections);
0a1b45a2 7502 return false;
5ed6aba4 7503 }
252b5132
RH
7504
7505 /* Initialise the fields of the segment map. Set the physical
7506 physical address to the LMA of the first section that has
7507 not yet been assigned. */
0067a569
AM
7508 map->next = NULL;
7509 map->p_type = segment->p_type;
7510 map->p_flags = segment->p_flags;
7511 map->p_flags_valid = 1;
66631823 7512 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7513 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7514 map->includes_filehdr = 0;
0067a569 7515 map->includes_phdrs = 0;
252b5132 7516 }
9984857c
NC
7517
7518 continue;
7519 sorry:
7520 bfd_set_error (bfd_error_sorry);
7521 free (sections);
0a1b45a2 7522 return false;
252b5132 7523 }
bc67d8a6 7524 while (isec < section_count);
252b5132
RH
7525
7526 free (sections);
7527 }
7528
12bd6957 7529 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7530
7531 /* If we had to estimate the number of program headers that were
9ad5cbcf 7532 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7533 the offset if necessary. */
7534 if (phdr_adjust_seg != NULL)
7535 {
7536 unsigned int count;
c044fabd 7537
bc67d8a6 7538 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7539 count++;
252b5132 7540
bc67d8a6
NC
7541 if (count > phdr_adjust_num)
7542 phdr_adjust_seg->p_paddr
7543 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7544
7545 for (map = map_first; map != NULL; map = map->next)
7546 if (map->p_type == PT_PHDR)
7547 {
7548 bfd_vma adjust
7549 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7550 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7551 break;
7552 }
bc67d8a6 7553 }
c044fabd 7554
bc67d8a6 7555#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7556#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7557#undef INCLUDE_SECTION_IN_SEGMENT
7558#undef SEGMENT_AFTER_SEGMENT
7559#undef SEGMENT_OVERLAPS
0a1b45a2 7560 return true;
252b5132
RH
7561}
7562
74e315db
L
7563/* Return true if p_align in the ELF program header in ABFD is valid. */
7564
7565static bool
7566elf_is_p_align_valid (bfd *abfd)
7567{
7568 unsigned int i;
7569 Elf_Internal_Phdr *segment;
7570 unsigned int num_segments;
7571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7572 bfd_size_type maxpagesize = bed->maxpagesize;
7573 bfd_size_type p_align = bed->p_align;
7574
7575 /* Return true if the default p_align value isn't set or the maximum
7576 page size is the same as the minimum page size. */
7577 if (p_align == 0 || maxpagesize == bed->minpagesize)
7578 return true;
7579
7580 /* When the default p_align value is set, p_align may be set to the
7581 default p_align value while segments are aligned to the maximum
7582 page size. In this case, the input p_align will be ignored and
7583 the maximum page size will be used to align the output segments. */
7584 segment = elf_tdata (abfd)->phdr;
7585 num_segments = elf_elfheader (abfd)->e_phnum;
7586 for (i = 0; i < num_segments; i++, segment++)
7587 if (segment->p_type == PT_LOAD
7588 && (segment->p_align != p_align
7589 || vma_page_aligned_bias (segment->p_vaddr,
7590 segment->p_offset,
7591 maxpagesize) != 0))
7592 return true;
7593
7594 return false;
7595}
7596
84d1d650
L
7597/* Copy ELF program header information. */
7598
0a1b45a2 7599static bool
84d1d650
L
7600copy_elf_program_header (bfd *ibfd, bfd *obfd)
7601{
7602 Elf_Internal_Ehdr *iehdr;
7603 struct elf_segment_map *map;
7604 struct elf_segment_map *map_first;
7605 struct elf_segment_map **pointer_to_map;
7606 Elf_Internal_Phdr *segment;
7607 unsigned int i;
7608 unsigned int num_segments;
0a1b45a2
AM
7609 bool phdr_included = false;
7610 bool p_paddr_valid;
74e315db 7611 bool p_palign_valid;
502794d4 7612 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7613
7614 iehdr = elf_elfheader (ibfd);
7615
7616 map_first = NULL;
7617 pointer_to_map = &map_first;
7618
88967714
AM
7619 /* If all the segment p_paddr fields are zero, don't set
7620 map->p_paddr_valid. */
0a1b45a2 7621 p_paddr_valid = false;
84d1d650 7622 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7623 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7624 i < num_segments;
7625 i++, segment++)
7626 if (segment->p_paddr != 0)
7627 {
0a1b45a2 7628 p_paddr_valid = true;
88967714
AM
7629 break;
7630 }
7631
74e315db
L
7632 p_palign_valid = elf_is_p_align_valid (ibfd);
7633
84d1d650
L
7634 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7635 i < num_segments;
7636 i++, segment++)
7637 {
7638 asection *section;
7639 unsigned int section_count;
986f0783 7640 size_t amt;
84d1d650 7641 Elf_Internal_Shdr *this_hdr;
53020534 7642 asection *first_section = NULL;
a76e6f2f 7643 asection *lowest_section;
84d1d650 7644
84d1d650
L
7645 /* Compute how many sections are in this segment. */
7646 for (section = ibfd->sections, section_count = 0;
7647 section != NULL;
7648 section = section->next)
7649 {
7650 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7651 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7652 {
a76e6f2f
AM
7653 if (first_section == NULL)
7654 first_section = section;
3271a814
NS
7655 section_count++;
7656 }
84d1d650
L
7657 }
7658
7659 /* Allocate a segment map big enough to contain
7660 all of the sections we have selected. */
00bee008 7661 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7662 amt += section_count * sizeof (asection *);
a50b1753 7663 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650 7664 if (map == NULL)
0a1b45a2 7665 return false;
84d1d650
L
7666
7667 /* Initialize the fields of the output segment map with the
7668 input segment. */
7669 map->next = NULL;
7670 map->p_type = segment->p_type;
7671 map->p_flags = segment->p_flags;
7672 map->p_flags_valid = 1;
7673 map->p_paddr = segment->p_paddr;
88967714 7674 map->p_paddr_valid = p_paddr_valid;
3f570048 7675 map->p_align = segment->p_align;
fb5a4a58
L
7676 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7677 map->p_align_valid = (map->p_type == PT_GNU_STACK
7678 || p_palign_valid);
3271a814 7679 map->p_vaddr_offset = 0;
84d1d650 7680
04c3a755
NS
7681 if (map->p_type == PT_GNU_RELRO
7682 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7683 {
7684 /* The PT_GNU_RELRO segment may contain the first a few
7685 bytes in the .got.plt section even if the whole .got.plt
7686 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7687 change the size of the PT_GNU_RELRO segment.
7688 Similarly, PT_GNU_STACK size is significant on uclinux
7689 systems. */
9433b9b1 7690 map->p_size = segment->p_memsz;
b10a8ae0
L
7691 map->p_size_valid = 1;
7692 }
7693
84d1d650
L
7694 /* Determine if this segment contains the ELF file header
7695 and if it contains the program headers themselves. */
7696 map->includes_filehdr = (segment->p_offset == 0
7697 && segment->p_filesz >= iehdr->e_ehsize);
7698
7699 map->includes_phdrs = 0;
7700 if (! phdr_included || segment->p_type != PT_LOAD)
7701 {
7702 map->includes_phdrs =
7703 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7704 && (segment->p_offset + segment->p_filesz
7705 >= ((bfd_vma) iehdr->e_phoff
7706 + iehdr->e_phnum * iehdr->e_phentsize)));
7707
7708 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7709 phdr_included = true;
84d1d650
L
7710 }
7711
bbefd0a9 7712 lowest_section = NULL;
84d1d650
L
7713 if (section_count != 0)
7714 {
7715 unsigned int isec = 0;
7716
53020534 7717 for (section = first_section;
84d1d650
L
7718 section != NULL;
7719 section = section->next)
7720 {
7721 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7722 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7723 {
7724 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7725 if ((section->flags & SEC_ALLOC) != 0)
7726 {
7727 bfd_vma seg_off;
7728
bbefd0a9
AM
7729 if (lowest_section == NULL
7730 || section->lma < lowest_section->lma)
fb8a5684
AM
7731 lowest_section = section;
7732
a76e6f2f
AM
7733 /* Section lmas are set up from PT_LOAD header
7734 p_paddr in _bfd_elf_make_section_from_shdr.
7735 If this header has a p_paddr that disagrees
7736 with the section lma, flag the p_paddr as
7737 invalid. */
7738 if ((section->flags & SEC_LOAD) != 0)
7739 seg_off = this_hdr->sh_offset - segment->p_offset;
7740 else
7741 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7742 if (section->lma * opb - segment->p_paddr != seg_off)
0a1b45a2 7743 map->p_paddr_valid = false;
a76e6f2f 7744 }
53020534
L
7745 if (isec == section_count)
7746 break;
7747 }
84d1d650
L
7748 }
7749 }
7750
5d695627 7751 if (section_count == 0)
502794d4 7752 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7753 else if (map->p_paddr_valid)
7754 {
7755 /* Account for padding before the first section in the segment. */
7756 bfd_vma hdr_size = 0;
7757 if (map->includes_filehdr)
7758 hdr_size = iehdr->e_ehsize;
7759 if (map->includes_phdrs)
7760 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7761
502794d4 7762 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7763 - (lowest_section ? lowest_section->lma : 0));
7764 }
a76e6f2f 7765
84d1d650
L
7766 map->count = section_count;
7767 *pointer_to_map = map;
7768 pointer_to_map = &map->next;
7769 }
7770
12bd6957 7771 elf_seg_map (obfd) = map_first;
0a1b45a2 7772 return true;
84d1d650
L
7773}
7774
7775/* Copy private BFD data. This copies or rewrites ELF program header
7776 information. */
7777
0a1b45a2 7778static bool
84d1d650
L
7779copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7780{
c410035d
AM
7781 bfd_vma maxpagesize;
7782
84d1d650
L
7783 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7784 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 7785 return true;
84d1d650
L
7786
7787 if (elf_tdata (ibfd)->phdr == NULL)
0a1b45a2 7788 return true;
84d1d650
L
7789
7790 if (ibfd->xvec == obfd->xvec)
7791 {
cb3ff1e5
NC
7792 /* Check to see if any sections in the input BFD
7793 covered by ELF program header have changed. */
d55ce4e2 7794 Elf_Internal_Phdr *segment;
84d1d650
L
7795 asection *section, *osec;
7796 unsigned int i, num_segments;
7797 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7798 const struct elf_backend_data *bed;
7799
7800 bed = get_elf_backend_data (ibfd);
7801
7802 /* Regenerate the segment map if p_paddr is set to 0. */
7803 if (bed->want_p_paddr_set_to_zero)
7804 goto rewrite;
84d1d650
L
7805
7806 /* Initialize the segment mark field. */
7807 for (section = obfd->sections; section != NULL;
7808 section = section->next)
0a1b45a2 7809 section->segment_mark = false;
84d1d650
L
7810
7811 num_segments = elf_elfheader (ibfd)->e_phnum;
7812 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7813 i < num_segments;
7814 i++, segment++)
7815 {
5f6999aa
NC
7816 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7817 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7818 which severly confuses things, so always regenerate the segment
7819 map in this case. */
7820 if (segment->p_paddr == 0
7821 && segment->p_memsz == 0
11c6a7c6
AM
7822 && (segment->p_type == PT_INTERP
7823 || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7824 goto rewrite;
5f6999aa 7825
84d1d650
L
7826 for (section = ibfd->sections;
7827 section != NULL; section = section->next)
7828 {
7829 /* We mark the output section so that we know it comes
7830 from the input BFD. */
7831 osec = section->output_section;
7832 if (osec)
0a1b45a2 7833 osec->segment_mark = true;
84d1d650
L
7834
7835 /* Check if this section is covered by the segment. */
7836 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7837 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7838 {
7839 /* FIXME: Check if its output section is changed or
7840 removed. What else do we need to check? */
7841 if (osec == NULL
7842 || section->flags != osec->flags
7843 || section->lma != osec->lma
7844 || section->vma != osec->vma
7845 || section->size != osec->size
7846 || section->rawsize != osec->rawsize
7847 || section->alignment_power != osec->alignment_power)
7848 goto rewrite;
7849 }
7850 }
7851 }
7852
cb3ff1e5 7853 /* Check to see if any output section do not come from the
84d1d650
L
7854 input BFD. */
7855 for (section = obfd->sections; section != NULL;
7856 section = section->next)
7857 {
535b785f 7858 if (!section->segment_mark)
84d1d650
L
7859 goto rewrite;
7860 else
0a1b45a2 7861 section->segment_mark = false;
84d1d650
L
7862 }
7863
7864 return copy_elf_program_header (ibfd, obfd);
7865 }
7866
dc1e8a47 7867 rewrite:
c410035d 7868 maxpagesize = 0;
f1d85785
L
7869 if (ibfd->xvec == obfd->xvec)
7870 {
7871 /* When rewriting program header, set the output maxpagesize to
7872 the maximum alignment of input PT_LOAD segments. */
7873 Elf_Internal_Phdr *segment;
7874 unsigned int i;
7875 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7876
7877 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7878 i < num_segments;
7879 i++, segment++)
7880 if (segment->p_type == PT_LOAD
7881 && maxpagesize < segment->p_align)
c86934ce
NC
7882 {
7883 /* PR 17512: file: f17299af. */
7884 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7885 /* xgettext:c-format */
2dcf00ce
AM
7886 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7887 PRIx64 " is too large"),
7888 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7889 else
7890 maxpagesize = segment->p_align;
7891 }
f1d85785 7892 }
c410035d
AM
7893 if (maxpagesize == 0)
7894 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7895
c410035d 7896 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7897}
7898
ccd2ec6a
L
7899/* Initialize private output section information from input section. */
7900
0a1b45a2 7901bool
ccd2ec6a
L
7902_bfd_elf_init_private_section_data (bfd *ibfd,
7903 asection *isec,
7904 bfd *obfd,
7905 asection *osec,
7906 struct bfd_link_info *link_info)
7907
7908{
7909 Elf_Internal_Shdr *ihdr, *ohdr;
0a1b45a2
AM
7910 bool final_link = (link_info != NULL
7911 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7912
7913 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7914 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7915 return true;
ccd2ec6a 7916
ba85c43e
NC
7917 BFD_ASSERT (elf_section_data (osec) != NULL);
7918
8c803a2d
AM
7919 /* If this is a known ABI section, ELF section type and flags may
7920 have been set up when OSEC was created. For normal sections we
7921 allow the user to override the type and flags other than
7922 SHF_MASKOS and SHF_MASKPROC. */
7923 if (elf_section_type (osec) == SHT_PROGBITS
7924 || elf_section_type (osec) == SHT_NOTE
7925 || elf_section_type (osec) == SHT_NOBITS)
7926 elf_section_type (osec) = SHT_NULL;
7927 /* For objcopy and relocatable link, copy the ELF section type from
7928 the input file if the BFD section flags are the same. (If they
7929 are different the user may be doing something like
7930 "objcopy --set-section-flags .text=alloc,data".) For a final
7931 link allow some flags that the linker clears to differ. */
42bb2e33 7932 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7933 && (osec->flags == isec->flags
7934 || (final_link
7935 && ((osec->flags ^ isec->flags)
0814be7d 7936 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7937 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7938
7939 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7940 elf_section_flags (osec) = (elf_section_flags (isec)
7941 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7942
a91e1603 7943 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7944 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7945 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7946 elf_section_data (osec)->this_hdr.sh_info
7947 = elf_section_data (isec)->this_hdr.sh_info;
7948
ccd2ec6a
L
7949 /* Set things up for objcopy and relocatable link. The output
7950 SHT_GROUP section will have its elf_next_in_group pointing back
7951 to the input group members. Ignore linker created group section.
7952 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7953 if ((link_info == NULL
7954 || !link_info->resolve_section_groups)
7955 && (elf_sec_group (isec) == NULL
7956 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7957 {
7bdf4127
AB
7958 if (elf_section_flags (isec) & SHF_GROUP)
7959 elf_section_flags (osec) |= SHF_GROUP;
7960 elf_next_in_group (osec) = elf_next_in_group (isec);
7961 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7962 }
7963
7bdf4127
AB
7964 /* If not decompress, preserve SHF_COMPRESSED. */
7965 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7966 elf_section_flags (osec) |= (elf_section_flags (isec)
7967 & SHF_COMPRESSED);
7968
ccd2ec6a
L
7969 ihdr = &elf_section_data (isec)->this_hdr;
7970
7971 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7972 don't use the output section of the linked-to section since it
7973 may be NULL at this point. */
7974 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7975 {
7976 ohdr = &elf_section_data (osec)->this_hdr;
7977 ohdr->sh_flags |= SHF_LINK_ORDER;
7978 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7979 }
7980
7981 osec->use_rela_p = isec->use_rela_p;
7982
0a1b45a2 7983 return true;
ccd2ec6a
L
7984}
7985
252b5132
RH
7986/* Copy private section information. This copies over the entsize
7987 field, and sometimes the info field. */
7988
0a1b45a2 7989bool
217aa764
AM
7990_bfd_elf_copy_private_section_data (bfd *ibfd,
7991 asection *isec,
7992 bfd *obfd,
7993 asection *osec)
252b5132
RH
7994{
7995 Elf_Internal_Shdr *ihdr, *ohdr;
7996
7997 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7998 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7999 return true;
252b5132 8000
252b5132
RH
8001 ihdr = &elf_section_data (isec)->this_hdr;
8002 ohdr = &elf_section_data (osec)->this_hdr;
8003
8004 ohdr->sh_entsize = ihdr->sh_entsize;
8005
8006 if (ihdr->sh_type == SHT_SYMTAB
8007 || ihdr->sh_type == SHT_DYNSYM
8008 || ihdr->sh_type == SHT_GNU_verneed
8009 || ihdr->sh_type == SHT_GNU_verdef)
8010 ohdr->sh_info = ihdr->sh_info;
8011
ccd2ec6a
L
8012 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8013 NULL);
252b5132
RH
8014}
8015
d0bf826b
AM
8016/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8017 necessary if we are removing either the SHT_GROUP section or any of
8018 the group member sections. DISCARDED is the value that a section's
8019 output_section has if the section will be discarded, NULL when this
8020 function is called from objcopy, bfd_abs_section_ptr when called
8021 from the linker. */
80fccad2 8022
0a1b45a2 8023bool
d0bf826b 8024_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 8025{
30288845
AM
8026 asection *isec;
8027
30288845 8028 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 8029 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
8030 {
8031 asection *first = elf_next_in_group (isec);
8032 asection *s = first;
d0bf826b
AM
8033 bfd_size_type removed = 0;
8034
30288845
AM
8035 while (s != NULL)
8036 {
415f38a6
AM
8037 /* If this member section is being output but the
8038 SHT_GROUP section is not, then clear the group info
8039 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
8040 if (s->output_section != discarded
8041 && isec->output_section == discarded)
30288845
AM
8042 {
8043 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8044 elf_group_name (s->output_section) = NULL;
8045 }
3349112e 8046 else
6e5e9d58
AM
8047 {
8048 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
8049 if (s->output_section == discarded
8050 && isec->output_section != discarded)
8051 {
8052 /* Conversely, if the member section is not being
8053 output but the SHT_GROUP section is, then adjust
8054 its size. */
8055 removed += 4;
8056 if (elf_sec->rel.hdr != NULL
8057 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8058 removed += 4;
8059 if (elf_sec->rela.hdr != NULL
8060 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8061 removed += 4;
8062 }
8063 else
8064 {
8065 /* Also adjust for zero-sized relocation member
8066 section. */
8067 if (elf_sec->rel.hdr != NULL
8068 && elf_sec->rel.hdr->sh_size == 0)
8069 removed += 4;
8070 if (elf_sec->rela.hdr != NULL
8071 && elf_sec->rela.hdr->sh_size == 0)
8072 removed += 4;
8073 }
6e5e9d58 8074 }
30288845
AM
8075 s = elf_next_in_group (s);
8076 if (s == first)
8077 break;
8078 }
d0bf826b
AM
8079 if (removed != 0)
8080 {
8081 if (discarded != NULL)
8082 {
8083 /* If we've been called for ld -r, then we need to
6e5e9d58 8084 adjust the input section size. */
d0bf826b
AM
8085 if (isec->rawsize == 0)
8086 isec->rawsize = isec->size;
8087 isec->size = isec->rawsize - removed;
6e5e9d58
AM
8088 if (isec->size <= 4)
8089 {
8090 isec->size = 0;
8091 isec->flags |= SEC_EXCLUDE;
8092 }
d0bf826b 8093 }
182ad375 8094 else if (isec->output_section != NULL)
d0bf826b
AM
8095 {
8096 /* Adjust the output section size when called from
8097 objcopy. */
8098 isec->output_section->size -= removed;
6e5e9d58
AM
8099 if (isec->output_section->size <= 4)
8100 {
8101 isec->output_section->size = 0;
8102 isec->output_section->flags |= SEC_EXCLUDE;
8103 }
d0bf826b
AM
8104 }
8105 }
30288845
AM
8106 }
8107
0a1b45a2 8108 return true;
80fccad2
BW
8109}
8110
d0bf826b
AM
8111/* Copy private header information. */
8112
0a1b45a2 8113bool
d0bf826b
AM
8114_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8115{
8116 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8117 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8118 return true;
d0bf826b
AM
8119
8120 /* Copy over private BFD data if it has not already been copied.
8121 This must be done here, rather than in the copy_private_bfd_data
8122 entry point, because the latter is called after the section
8123 contents have been set, which means that the program headers have
8124 already been worked out. */
12bd6957 8125 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8126 {
8127 if (! copy_private_bfd_data (ibfd, obfd))
0a1b45a2 8128 return false;
d0bf826b
AM
8129 }
8130
8131 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8132}
8133
252b5132
RH
8134/* Copy private symbol information. If this symbol is in a section
8135 which we did not map into a BFD section, try to map the section
8136 index correctly. We use special macro definitions for the mapped
8137 section indices; these definitions are interpreted by the
8138 swap_out_syms function. */
8139
9ad5cbcf
AM
8140#define MAP_ONESYMTAB (SHN_HIOS + 1)
8141#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8142#define MAP_STRTAB (SHN_HIOS + 3)
8143#define MAP_SHSTRTAB (SHN_HIOS + 4)
8144#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8145
0a1b45a2 8146bool
217aa764
AM
8147_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8148 asymbol *isymarg,
8149 bfd *obfd,
8150 asymbol *osymarg)
252b5132
RH
8151{
8152 elf_symbol_type *isym, *osym;
8153
8154 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8155 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8156 return true;
252b5132 8157
c1229f84
AM
8158 isym = elf_symbol_from (isymarg);
8159 osym = elf_symbol_from (osymarg);
252b5132
RH
8160
8161 if (isym != NULL
8424d8f5 8162 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8163 && osym != NULL
8164 && bfd_is_abs_section (isym->symbol.section))
8165 {
8166 unsigned int shndx;
8167
8168 shndx = isym->internal_elf_sym.st_shndx;
8169 if (shndx == elf_onesymtab (ibfd))
8170 shndx = MAP_ONESYMTAB;
8171 else if (shndx == elf_dynsymtab (ibfd))
8172 shndx = MAP_DYNSYMTAB;
12bd6957 8173 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8174 shndx = MAP_STRTAB;
12bd6957 8175 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8176 shndx = MAP_SHSTRTAB;
6a40cf0c 8177 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8178 shndx = MAP_SYM_SHNDX;
252b5132
RH
8179 osym->internal_elf_sym.st_shndx = shndx;
8180 }
8181
0a1b45a2 8182 return true;
252b5132
RH
8183}
8184
8185/* Swap out the symbols. */
8186
0a1b45a2 8187static bool
217aa764 8188swap_out_syms (bfd *abfd,
ef10c3ac 8189 struct elf_strtab_hash **sttp,
3d16b64e
NA
8190 int relocatable_p,
8191 struct bfd_link_info *info)
252b5132 8192{
9c5bfbb7 8193 const struct elf_backend_data *bed;
1f4361a7 8194 unsigned int symcount;
079e9a2f 8195 asymbol **syms;
ef10c3ac 8196 struct elf_strtab_hash *stt;
079e9a2f 8197 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8198 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8199 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8200 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8201 bfd_byte *outbound_syms;
8202 bfd_byte *outbound_shndx;
ef10c3ac 8203 unsigned long outbound_syms_index;
1f4361a7 8204 unsigned int idx;
12bd6957 8205 unsigned int num_locals;
1f4361a7 8206 size_t amt;
0a1b45a2 8207 bool name_local_sections;
252b5132 8208
12bd6957 8209 if (!elf_map_symbols (abfd, &num_locals))
0a1b45a2 8210 return false;
252b5132 8211
c044fabd 8212 /* Dump out the symtabs. */
ef10c3ac 8213 stt = _bfd_elf_strtab_init ();
079e9a2f 8214 if (stt == NULL)
0a1b45a2 8215 return false;
252b5132 8216
079e9a2f
AM
8217 bed = get_elf_backend_data (abfd);
8218 symcount = bfd_get_symcount (abfd);
8219 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8220 symtab_hdr->sh_type = SHT_SYMTAB;
8221 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8222 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8223 symtab_hdr->sh_info = num_locals + 1;
72de5009 8224 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8225
8226 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8227 symstrtab_hdr->sh_type = SHT_STRTAB;
8228
ef10c3ac 8229 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8230 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8231 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8232 {
1f4361a7 8233 bfd_set_error (bfd_error_no_memory);
ef10c3ac 8234 _bfd_elf_strtab_free (stt);
0a1b45a2 8235 return false;
ef10c3ac
L
8236 }
8237
1f4361a7
AM
8238 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8239 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8240 {
1f4361a7
AM
8241 error_no_mem:
8242 bfd_set_error (bfd_error_no_memory);
8243 error_return:
ef10c3ac 8244 free (symstrtab);
1f4361a7 8245 _bfd_elf_strtab_free (stt);
0a1b45a2 8246 return false;
5ed6aba4 8247 }
217aa764 8248 symtab_hdr->contents = outbound_syms;
ef10c3ac 8249 outbound_syms_index = 0;
252b5132 8250
9ad5cbcf 8251 outbound_shndx = NULL;
6a40cf0c
NC
8252
8253 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8254 {
6a40cf0c
NC
8255 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8256 if (symtab_shndx_hdr->sh_name != 0)
8257 {
1f4361a7
AM
8258 if (_bfd_mul_overflow (symcount + 1,
8259 sizeof (Elf_External_Sym_Shndx), &amt))
8260 goto error_no_mem;
8261 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8262 if (outbound_shndx == NULL)
8263 goto error_return;
5ed6aba4 8264
6a40cf0c
NC
8265 symtab_shndx_hdr->contents = outbound_shndx;
8266 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8267 symtab_shndx_hdr->sh_size = amt;
8268 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8269 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8270 }
8271 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8272 }
8273
589e6347 8274 /* Now generate the data (for "contents"). */
079e9a2f
AM
8275 {
8276 /* Fill in zeroth symbol and swap it out. */
8277 Elf_Internal_Sym sym;
8278 sym.st_name = 0;
8279 sym.st_value = 0;
8280 sym.st_size = 0;
8281 sym.st_info = 0;
8282 sym.st_other = 0;
8283 sym.st_shndx = SHN_UNDEF;
35fc36a8 8284 sym.st_target_internal = 0;
ef10c3ac
L
8285 symstrtab[0].sym = sym;
8286 symstrtab[0].dest_index = outbound_syms_index;
ef10c3ac 8287 outbound_syms_index++;
079e9a2f 8288 }
252b5132 8289
174fd7f9
RS
8290 name_local_sections
8291 = (bed->elf_backend_name_local_section_symbols
8292 && bed->elf_backend_name_local_section_symbols (abfd));
8293
079e9a2f 8294 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8295 for (idx = 0; idx < symcount;)
252b5132 8296 {
252b5132 8297 Elf_Internal_Sym sym;
079e9a2f
AM
8298 bfd_vma value = syms[idx]->value;
8299 elf_symbol_type *type_ptr;
8300 flagword flags = syms[idx]->flags;
8301 int type;
252b5132 8302
174fd7f9
RS
8303 if (!name_local_sections
8304 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8305 {
8306 /* Local section symbols have no name. */
ef10c3ac 8307 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8308 }
8309 else
8310 {
ef10c3ac
L
8311 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8312 to get the final offset for st_name. */
8313 sym.st_name
8314 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
0a1b45a2 8315 false);
079e9a2f 8316 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8317 goto error_return;
079e9a2f 8318 }
252b5132 8319
c1229f84 8320 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8321
079e9a2f
AM
8322 if ((flags & BSF_SECTION_SYM) == 0
8323 && bfd_is_com_section (syms[idx]->section))
8324 {
8325 /* ELF common symbols put the alignment into the `value' field,
8326 and the size into the `size' field. This is backwards from
8327 how BFD handles it, so reverse it here. */
8328 sym.st_size = value;
8329 if (type_ptr == NULL
8330 || type_ptr->internal_elf_sym.st_value == 0)
8331 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8332 else
8333 sym.st_value = type_ptr->internal_elf_sym.st_value;
8334 sym.st_shndx = _bfd_elf_section_from_bfd_section
8335 (abfd, syms[idx]->section);
8336 }
8337 else
8338 {
8339 asection *sec = syms[idx]->section;
cb33740c 8340 unsigned int shndx;
252b5132 8341
079e9a2f
AM
8342 if (sec->output_section)
8343 {
8344 value += sec->output_offset;
8345 sec = sec->output_section;
8346 }
589e6347 8347
079e9a2f
AM
8348 /* Don't add in the section vma for relocatable output. */
8349 if (! relocatable_p)
8350 value += sec->vma;
8351 sym.st_value = value;
8352 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8353
8354 if (bfd_is_abs_section (sec)
8355 && type_ptr != NULL
8356 && type_ptr->internal_elf_sym.st_shndx != 0)
8357 {
8358 /* This symbol is in a real ELF section which we did
8359 not create as a BFD section. Undo the mapping done
8360 by copy_private_symbol_data. */
8361 shndx = type_ptr->internal_elf_sym.st_shndx;
8362 switch (shndx)
8363 {
8364 case MAP_ONESYMTAB:
8365 shndx = elf_onesymtab (abfd);
8366 break;
8367 case MAP_DYNSYMTAB:
8368 shndx = elf_dynsymtab (abfd);
8369 break;
8370 case MAP_STRTAB:
12bd6957 8371 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8372 break;
8373 case MAP_SHSTRTAB:
12bd6957 8374 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8375 break;
9ad5cbcf 8376 case MAP_SYM_SHNDX:
6a40cf0c
NC
8377 if (elf_symtab_shndx_list (abfd))
8378 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8379 break;
00e49dff
NC
8380 case SHN_COMMON:
8381 case SHN_ABS:
15bc576a 8382 shndx = SHN_ABS;
079e9a2f 8383 break;
00e49dff
NC
8384 default:
8385 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8386 {
8387 if (bed->symbol_section_index)
8388 shndx = bed->symbol_section_index (abfd, type_ptr);
8389 /* Otherwise just leave the index alone. */
8390 }
8391 else
8392 {
8393 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8394 _bfd_error_handler (_("%pB: \
8395Unable to handle section index %x in ELF symbol. Using ABS instead."),
8396 abfd, shndx);
8397 shndx = SHN_ABS;
8398 }
8399 break;
079e9a2f
AM
8400 }
8401 }
8402 else
8403 {
8404 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8405
cb33740c 8406 if (shndx == SHN_BAD)
079e9a2f
AM
8407 {
8408 asection *sec2;
8409
8410 /* Writing this would be a hell of a lot easier if
8411 we had some decent documentation on bfd, and
8412 knew what to expect of the library, and what to
8413 demand of applications. For example, it
8414 appears that `objcopy' might not set the
8415 section of a symbol to be a section that is
8416 actually in the output file. */
8417 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8418 if (sec2 != NULL)
8419 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8420 if (shndx == SHN_BAD)
589e6347 8421 {
695344c0 8422 /* xgettext:c-format */
9793eb77
AM
8423 _bfd_error_handler
8424 (_("unable to find equivalent output section"
8425 " for symbol '%s' from section '%s'"),
8426 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8427 sec->name);
811072d8 8428 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8429 goto error_return;
589e6347 8430 }
079e9a2f
AM
8431 }
8432 }
252b5132 8433
079e9a2f
AM
8434 sym.st_shndx = shndx;
8435 }
252b5132 8436
13ae64f3
JJ
8437 if ((flags & BSF_THREAD_LOCAL) != 0)
8438 type = STT_TLS;
d8045f23
NC
8439 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8440 type = STT_GNU_IFUNC;
13ae64f3 8441 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8442 type = STT_FUNC;
8443 else if ((flags & BSF_OBJECT) != 0)
8444 type = STT_OBJECT;
d9352518
DB
8445 else if ((flags & BSF_RELC) != 0)
8446 type = STT_RELC;
8447 else if ((flags & BSF_SRELC) != 0)
8448 type = STT_SRELC;
079e9a2f
AM
8449 else
8450 type = STT_NOTYPE;
252b5132 8451
13ae64f3
JJ
8452 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8453 type = STT_TLS;
8454
589e6347 8455 /* Processor-specific types. */
079e9a2f
AM
8456 if (type_ptr != NULL
8457 && bed->elf_backend_get_symbol_type)
8458 type = ((*bed->elf_backend_get_symbol_type)
8459 (&type_ptr->internal_elf_sym, type));
252b5132 8460
079e9a2f
AM
8461 if (flags & BSF_SECTION_SYM)
8462 {
8463 if (flags & BSF_GLOBAL)
8464 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8465 else
8466 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8467 }
8468 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8469 {
b8871f35
L
8470 if (type != STT_TLS)
8471 {
8472 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8473 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8474 ? STT_COMMON : STT_OBJECT);
8475 else
8476 type = ((flags & BSF_ELF_COMMON) != 0
8477 ? STT_COMMON : STT_OBJECT);
8478 }
8479 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8480 }
079e9a2f
AM
8481 else if (bfd_is_und_section (syms[idx]->section))
8482 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8483 ? STB_WEAK
8484 : STB_GLOBAL),
8485 type);
8486 else if (flags & BSF_FILE)
8487 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8488 else
8489 {
8490 int bind = STB_LOCAL;
252b5132 8491
079e9a2f
AM
8492 if (flags & BSF_LOCAL)
8493 bind = STB_LOCAL;
3e7a7d11
NC
8494 else if (flags & BSF_GNU_UNIQUE)
8495 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8496 else if (flags & BSF_WEAK)
8497 bind = STB_WEAK;
8498 else if (flags & BSF_GLOBAL)
8499 bind = STB_GLOBAL;
252b5132 8500
079e9a2f
AM
8501 sym.st_info = ELF_ST_INFO (bind, type);
8502 }
252b5132 8503
079e9a2f 8504 if (type_ptr != NULL)
35fc36a8
RS
8505 {
8506 sym.st_other = type_ptr->internal_elf_sym.st_other;
8507 sym.st_target_internal
8508 = type_ptr->internal_elf_sym.st_target_internal;
8509 }
079e9a2f 8510 else
35fc36a8
RS
8511 {
8512 sym.st_other = 0;
8513 sym.st_target_internal = 0;
8514 }
252b5132 8515
ef10c3ac
L
8516 idx++;
8517 symstrtab[idx].sym = sym;
8518 symstrtab[idx].dest_index = outbound_syms_index;
ef10c3ac
L
8519
8520 outbound_syms_index++;
ef10c3ac
L
8521 }
8522
8523 /* Finalize the .strtab section. */
8524 _bfd_elf_strtab_finalize (stt);
8525
8526 /* Swap out the .strtab section. */
8527 for (idx = 0; idx <= symcount; idx++)
8528 {
8529 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8530 if (elfsym->sym.st_name == (unsigned long) -1)
8531 elfsym->sym.st_name = 0;
8532 else
8533 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8534 elfsym->sym.st_name);
3d16b64e
NA
8535 if (info && info->callbacks->ctf_new_symbol)
8536 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8537 &elfsym->sym);
8538
8539 /* Inform the linker of the addition of this symbol. */
8540
ef10c3ac
L
8541 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8542 (outbound_syms
8543 + (elfsym->dest_index
8544 * bed->s->sizeof_sym)),
b03b65e2
AM
8545 NPTR_ADD (outbound_shndx,
8546 (elfsym->dest_index
8547 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8548 }
ef10c3ac 8549 free (symstrtab);
252b5132 8550
079e9a2f 8551 *sttp = stt;
ef10c3ac 8552 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8553 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8554 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8555 symstrtab_hdr->sh_addr = 0;
8556 symstrtab_hdr->sh_entsize = 0;
8557 symstrtab_hdr->sh_link = 0;
8558 symstrtab_hdr->sh_info = 0;
8559 symstrtab_hdr->sh_addralign = 1;
252b5132 8560
0a1b45a2 8561 return true;
252b5132
RH
8562}
8563
8564/* Return the number of bytes required to hold the symtab vector.
8565
8566 Note that we base it on the count plus 1, since we will null terminate
8567 the vector allocated based on this size. However, the ELF symbol table
8568 always has a dummy entry as symbol #0, so it ends up even. */
8569
8570long
217aa764 8571_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8572{
3a551c7a 8573 bfd_size_type symcount;
252b5132
RH
8574 long symtab_size;
8575 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8576
8577 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8578 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8579 {
8580 bfd_set_error (bfd_error_file_too_big);
8581 return -1;
8582 }
b5f386d5
AM
8583 symtab_size = symcount * (sizeof (asymbol *));
8584 if (symcount == 0)
8585 symtab_size = sizeof (asymbol *);
8586 else if (!bfd_write_p (abfd))
8587 {
8588 ufile_ptr filesize = bfd_get_file_size (abfd);
8589
8590 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8591 {
8592 bfd_set_error (bfd_error_file_truncated);
8593 return -1;
8594 }
8595 }
252b5132
RH
8596
8597 return symtab_size;
8598}
8599
8600long
217aa764 8601_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8602{
3a551c7a 8603 bfd_size_type symcount;
252b5132
RH
8604 long symtab_size;
8605 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8606
8607 if (elf_dynsymtab (abfd) == 0)
8608 {
8609 bfd_set_error (bfd_error_invalid_operation);
8610 return -1;
8611 }
8612
8613 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8614 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8615 {
8616 bfd_set_error (bfd_error_file_too_big);
8617 return -1;
8618 }
b5f386d5
AM
8619 symtab_size = symcount * (sizeof (asymbol *));
8620 if (symcount == 0)
8621 symtab_size = sizeof (asymbol *);
8622 else if (!bfd_write_p (abfd))
8623 {
8624 ufile_ptr filesize = bfd_get_file_size (abfd);
8625
8626 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8627 {
8628 bfd_set_error (bfd_error_file_truncated);
8629 return -1;
8630 }
8631 }
252b5132
RH
8632
8633 return symtab_size;
8634}
8635
8636long
3c568b8a 8637_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8638{
b5f386d5 8639 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8640 {
8641 /* Sanity check reloc section size. */
3c568b8a
AM
8642 ufile_ptr filesize = bfd_get_file_size (abfd);
8643
5dcae8f6 8644 if (filesize != 0)
3c568b8a 8645 {
5dcae8f6
AM
8646 struct bfd_elf_section_data *d = elf_section_data (asect);
8647 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8648 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8649
8650 if (rel_size + rela_size > filesize
8651 || rel_size + rela_size < rel_size)
8652 {
8653 bfd_set_error (bfd_error_file_truncated);
8654 return -1;
8655 }
3c568b8a
AM
8656 }
8657 }
8658
242a1159 8659#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8660 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8661 {
8662 bfd_set_error (bfd_error_file_too_big);
8663 return -1;
8664 }
242a1159 8665#endif
5cfe19e5 8666 return (asect->reloc_count + 1L) * sizeof (arelent *);
252b5132
RH
8667}
8668
8669/* Canonicalize the relocs. */
8670
8671long
217aa764
AM
8672_bfd_elf_canonicalize_reloc (bfd *abfd,
8673 sec_ptr section,
8674 arelent **relptr,
8675 asymbol **symbols)
252b5132
RH
8676{
8677 arelent *tblptr;
8678 unsigned int i;
9c5bfbb7 8679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8680
0a1b45a2 8681 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
8682 return -1;
8683
8684 tblptr = section->relocation;
8685 for (i = 0; i < section->reloc_count; i++)
8686 *relptr++ = tblptr++;
8687
8688 *relptr = NULL;
8689
8690 return section->reloc_count;
8691}
8692
8693long
6cee3f79 8694_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8695{
9c5bfbb7 8696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8697 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
252b5132
RH
8698
8699 if (symcount >= 0)
ed48ec2e 8700 abfd->symcount = symcount;
252b5132
RH
8701 return symcount;
8702}
8703
8704long
217aa764
AM
8705_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8706 asymbol **allocation)
252b5132 8707{
9c5bfbb7 8708 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8709 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
1f70368c
DJ
8710
8711 if (symcount >= 0)
ed48ec2e 8712 abfd->dynsymcount = symcount;
1f70368c 8713 return symcount;
252b5132
RH
8714}
8715
8615f3f2
AM
8716/* Return the size required for the dynamic reloc entries. Any loadable
8717 section that was actually installed in the BFD, and has type SHT_REL
8718 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8719 dynamic reloc section. */
252b5132
RH
8720
8721long
217aa764 8722_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8723{
3c568b8a 8724 bfd_size_type count, ext_rel_size;
252b5132
RH
8725 asection *s;
8726
8727 if (elf_dynsymtab (abfd) == 0)
8728 {
8729 bfd_set_error (bfd_error_invalid_operation);
8730 return -1;
8731 }
8732
3a551c7a 8733 count = 1;
3c568b8a 8734 ext_rel_size = 0;
252b5132 8735 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8736 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132 8737 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
93c6e8c3
AM
8738 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8739 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
3a551c7a 8740 {
93c6e8c3
AM
8741 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
8742 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
3c568b8a
AM
8743 {
8744 bfd_set_error (bfd_error_file_truncated);
8745 return -1;
8746 }
93c6e8c3 8747 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
3a551c7a
AM
8748 if (count > LONG_MAX / sizeof (arelent *))
8749 {
8750 bfd_set_error (bfd_error_file_too_big);
8751 return -1;
8752 }
8753 }
b5f386d5 8754 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8755 {
8756 /* Sanity check reloc section sizes. */
8757 ufile_ptr filesize = bfd_get_file_size (abfd);
8758 if (filesize != 0 && ext_rel_size > filesize)
8759 {
8760 bfd_set_error (bfd_error_file_truncated);
8761 return -1;
8762 }
8763 }
3a551c7a 8764 return count * sizeof (arelent *);
252b5132
RH
8765}
8766
8615f3f2
AM
8767/* Canonicalize the dynamic relocation entries. Note that we return the
8768 dynamic relocations as a single block, although they are actually
8769 associated with particular sections; the interface, which was
8770 designed for SunOS style shared libraries, expects that there is only
8771 one set of dynamic relocs. Any loadable section that was actually
8772 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8773 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8774
8775long
217aa764
AM
8776_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8777 arelent **storage,
8778 asymbol **syms)
252b5132 8779{
0a1b45a2 8780 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
252b5132
RH
8781 asection *s;
8782 long ret;
8783
8784 if (elf_dynsymtab (abfd) == 0)
8785 {
8786 bfd_set_error (bfd_error_invalid_operation);
8787 return -1;
8788 }
8789
8790 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8791 ret = 0;
8792 for (s = abfd->sections; s != NULL; s = s->next)
8793 {
266b05cf 8794 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132 8795 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
93c6e8c3
AM
8796 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8797 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
252b5132
RH
8798 {
8799 arelent *p;
8800 long count, i;
8801
0a1b45a2 8802 if (! (*slurp_relocs) (abfd, s, syms, true))
252b5132 8803 return -1;
93c6e8c3 8804 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
252b5132
RH
8805 p = s->relocation;
8806 for (i = 0; i < count; i++)
8807 *storage++ = p++;
8808 ret += count;
8809 }
8810 }
8811
8812 *storage = NULL;
8813
8814 return ret;
8815}
8816\f
8817/* Read in the version information. */
8818
0a1b45a2
AM
8819bool
8820_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
252b5132
RH
8821{
8822 bfd_byte *contents = NULL;
fc0e6df6 8823 unsigned int freeidx = 0;
1f4361a7 8824 size_t amt;
fc0e6df6
PB
8825
8826 if (elf_dynverref (abfd) != 0)
8827 {
8828 Elf_Internal_Shdr *hdr;
8829 Elf_External_Verneed *everneed;
8830 Elf_Internal_Verneed *iverneed;
8831 unsigned int i;
d0fb9a8d 8832 bfd_byte *contents_end;
fc0e6df6
PB
8833
8834 hdr = &elf_tdata (abfd)->dynverref_hdr;
8835
f9a59eea 8836 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8837 {
dc1e8a47 8838 error_return_bad_verref:
4eca0228 8839 _bfd_error_handler
871b3ab2 8840 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8841 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8842 error_return_verref:
d0fb9a8d
JJ
8843 elf_tdata (abfd)->verref = NULL;
8844 elf_tdata (abfd)->cverrefs = 0;
8845 goto error_return;
8846 }
601a03ba 8847
2bb3687b
AM
8848 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8849 goto error_return_verref;
8850 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8851 if (contents == NULL)
d0fb9a8d 8852 goto error_return_verref;
fc0e6df6 8853
1f4361a7
AM
8854 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8855 {
8856 bfd_set_error (bfd_error_file_too_big);
8857 goto error_return_verref;
8858 }
5c831a3c
NC
8859 if (amt == 0)
8860 goto error_return_verref;
8861 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
601a03ba 8862 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8863 goto error_return_verref;
8864
8865 BFD_ASSERT (sizeof (Elf_External_Verneed)
8866 == sizeof (Elf_External_Vernaux));
8867 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8868 everneed = (Elf_External_Verneed *) contents;
8869 iverneed = elf_tdata (abfd)->verref;
8870 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8871 {
8872 Elf_External_Vernaux *evernaux;
8873 Elf_Internal_Vernaux *ivernaux;
8874 unsigned int j;
8875
8876 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8877
8878 iverneed->vn_bfd = abfd;
8879
8880 iverneed->vn_filename =
8881 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8882 iverneed->vn_file);
8883 if (iverneed->vn_filename == NULL)
601a03ba 8884 goto error_return_bad_verref;
fc0e6df6 8885
d0fb9a8d
JJ
8886 if (iverneed->vn_cnt == 0)
8887 iverneed->vn_auxptr = NULL;
8888 else
8889 {
1f4361a7
AM
8890 if (_bfd_mul_overflow (iverneed->vn_cnt,
8891 sizeof (Elf_Internal_Vernaux), &amt))
8892 {
8893 bfd_set_error (bfd_error_file_too_big);
8894 goto error_return_verref;
8895 }
a50b1753 8896 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8897 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8898 if (iverneed->vn_auxptr == NULL)
8899 goto error_return_verref;
8900 }
8901
8902 if (iverneed->vn_aux
8903 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8904 goto error_return_bad_verref;
fc0e6df6
PB
8905
8906 evernaux = ((Elf_External_Vernaux *)
8907 ((bfd_byte *) everneed + iverneed->vn_aux));
8908 ivernaux = iverneed->vn_auxptr;
8909 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8910 {
8911 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8912
8913 ivernaux->vna_nodename =
8914 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8915 ivernaux->vna_name);
8916 if (ivernaux->vna_nodename == NULL)
601a03ba 8917 goto error_return_bad_verref;
fc0e6df6 8918
25ff461f
AM
8919 if (ivernaux->vna_other > freeidx)
8920 freeidx = ivernaux->vna_other;
8921
8922 ivernaux->vna_nextptr = NULL;
8923 if (ivernaux->vna_next == 0)
8924 {
8925 iverneed->vn_cnt = j + 1;
8926 break;
8927 }
fc0e6df6
PB
8928 if (j + 1 < iverneed->vn_cnt)
8929 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8930
d0fb9a8d
JJ
8931 if (ivernaux->vna_next
8932 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8933 goto error_return_bad_verref;
d0fb9a8d 8934
fc0e6df6
PB
8935 evernaux = ((Elf_External_Vernaux *)
8936 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8937 }
8938
25ff461f
AM
8939 iverneed->vn_nextref = NULL;
8940 if (iverneed->vn_next == 0)
8941 break;
fc0e6df6
PB
8942 if (i + 1 < hdr->sh_info)
8943 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8944
d0fb9a8d
JJ
8945 if (iverneed->vn_next
8946 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8947 goto error_return_bad_verref;
d0fb9a8d 8948
fc0e6df6
PB
8949 everneed = ((Elf_External_Verneed *)
8950 ((bfd_byte *) everneed + iverneed->vn_next));
8951 }
25ff461f 8952 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8953
8954 free (contents);
8955 contents = NULL;
8956 }
252b5132
RH
8957
8958 if (elf_dynverdef (abfd) != 0)
8959 {
8960 Elf_Internal_Shdr *hdr;
8961 Elf_External_Verdef *everdef;
8962 Elf_Internal_Verdef *iverdef;
f631889e
UD
8963 Elf_Internal_Verdef *iverdefarr;
8964 Elf_Internal_Verdef iverdefmem;
252b5132 8965 unsigned int i;
062e2358 8966 unsigned int maxidx;
d0fb9a8d 8967 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8968
8969 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8970
f9a59eea 8971 if (hdr->sh_size < sizeof (Elf_External_Verdef))
601a03ba
AM
8972 {
8973 error_return_bad_verdef:
4eca0228 8974 _bfd_error_handler
871b3ab2 8975 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8976 bfd_set_error (bfd_error_bad_value);
8977 error_return_verdef:
8978 elf_tdata (abfd)->verdef = NULL;
8979 elf_tdata (abfd)->cverdefs = 0;
8980 goto error_return;
8981 }
8982
2bb3687b 8983 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8984 goto error_return_verdef;
2bb3687b
AM
8985 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8986 if (contents == NULL)
601a03ba 8987 goto error_return_verdef;
d0fb9a8d
JJ
8988
8989 BFD_ASSERT (sizeof (Elf_External_Verdef)
8990 >= sizeof (Elf_External_Verdaux));
8991 contents_end_def = contents + hdr->sh_size
8992 - sizeof (Elf_External_Verdef);
8993 contents_end_aux = contents + hdr->sh_size
8994 - sizeof (Elf_External_Verdaux);
8995
f631889e
UD
8996 /* We know the number of entries in the section but not the maximum
8997 index. Therefore we have to run through all entries and find
8998 the maximum. */
252b5132 8999 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
9000 maxidx = 0;
9001 for (i = 0; i < hdr->sh_info; ++i)
9002 {
9003 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9004
601a03ba
AM
9005 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9006 goto error_return_bad_verdef;
062e2358
AM
9007 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9008 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 9009
25ff461f
AM
9010 if (iverdefmem.vd_next == 0)
9011 break;
9012
d0fb9a8d
JJ
9013 if (iverdefmem.vd_next
9014 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 9015 goto error_return_bad_verdef;
d0fb9a8d 9016
f631889e
UD
9017 everdef = ((Elf_External_Verdef *)
9018 ((bfd_byte *) everdef + iverdefmem.vd_next));
9019 }
9020
fc0e6df6
PB
9021 if (default_imported_symver)
9022 {
9023 if (freeidx > maxidx)
9024 maxidx = ++freeidx;
9025 else
9026 freeidx = ++maxidx;
9027 }
1f4361a7
AM
9028 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9029 {
9030 bfd_set_error (bfd_error_file_too_big);
9031 goto error_return_verdef;
9032 }
c22d38ba
NC
9033
9034 if (amt == 0)
9035 goto error_return_verdef;
1f4361a7 9036 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 9037 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 9038 goto error_return_verdef;
f631889e
UD
9039
9040 elf_tdata (abfd)->cverdefs = maxidx;
9041
9042 everdef = (Elf_External_Verdef *) contents;
9043 iverdefarr = elf_tdata (abfd)->verdef;
9044 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
9045 {
9046 Elf_External_Verdaux *everdaux;
9047 Elf_Internal_Verdaux *iverdaux;
9048 unsigned int j;
9049
f631889e
UD
9050 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9051
d0fb9a8d 9052 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 9053 goto error_return_bad_verdef;
d0fb9a8d 9054
f631889e 9055 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 9056 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
9057
9058 iverdef->vd_bfd = abfd;
9059
d0fb9a8d
JJ
9060 if (iverdef->vd_cnt == 0)
9061 iverdef->vd_auxptr = NULL;
9062 else
9063 {
1f4361a7
AM
9064 if (_bfd_mul_overflow (iverdef->vd_cnt,
9065 sizeof (Elf_Internal_Verdaux), &amt))
9066 {
9067 bfd_set_error (bfd_error_file_too_big);
9068 goto error_return_verdef;
9069 }
a50b1753 9070 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 9071 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
9072 if (iverdef->vd_auxptr == NULL)
9073 goto error_return_verdef;
9074 }
9075
9076 if (iverdef->vd_aux
9077 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 9078 goto error_return_bad_verdef;
252b5132
RH
9079
9080 everdaux = ((Elf_External_Verdaux *)
9081 ((bfd_byte *) everdef + iverdef->vd_aux));
9082 iverdaux = iverdef->vd_auxptr;
9083 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9084 {
9085 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9086
9087 iverdaux->vda_nodename =
9088 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9089 iverdaux->vda_name);
9090 if (iverdaux->vda_nodename == NULL)
601a03ba 9091 goto error_return_bad_verdef;
252b5132 9092
25ff461f
AM
9093 iverdaux->vda_nextptr = NULL;
9094 if (iverdaux->vda_next == 0)
9095 {
9096 iverdef->vd_cnt = j + 1;
9097 break;
9098 }
252b5132
RH
9099 if (j + 1 < iverdef->vd_cnt)
9100 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 9101
d0fb9a8d
JJ
9102 if (iverdaux->vda_next
9103 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 9104 goto error_return_bad_verdef;
d0fb9a8d 9105
252b5132
RH
9106 everdaux = ((Elf_External_Verdaux *)
9107 ((bfd_byte *) everdaux + iverdaux->vda_next));
9108 }
9109
595bce75 9110 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
9111 if (iverdef->vd_cnt)
9112 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 9113
25ff461f
AM
9114 iverdef->vd_nextdef = NULL;
9115 if (iverdef->vd_next == 0)
9116 break;
d0fb9a8d 9117 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 9118 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
9119
9120 everdef = ((Elf_External_Verdef *)
9121 ((bfd_byte *) everdef + iverdef->vd_next));
9122 }
9123
9124 free (contents);
9125 contents = NULL;
9126 }
fc0e6df6 9127 else if (default_imported_symver)
252b5132 9128 {
fc0e6df6
PB
9129 if (freeidx < 3)
9130 freeidx = 3;
9131 else
9132 freeidx++;
252b5132 9133
1f4361a7
AM
9134 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9135 {
9136 bfd_set_error (bfd_error_file_too_big);
9137 goto error_return;
9138 }
c22d38ba
NC
9139 if (amt == 0)
9140 goto error_return;
1f4361a7 9141 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9142 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9143 goto error_return;
9144
fc0e6df6
PB
9145 elf_tdata (abfd)->cverdefs = freeidx;
9146 }
252b5132 9147
fc0e6df6
PB
9148 /* Create a default version based on the soname. */
9149 if (default_imported_symver)
9150 {
9151 Elf_Internal_Verdef *iverdef;
9152 Elf_Internal_Verdaux *iverdaux;
252b5132 9153
5bb3703f 9154 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9155
fc0e6df6
PB
9156 iverdef->vd_version = VER_DEF_CURRENT;
9157 iverdef->vd_flags = 0;
9158 iverdef->vd_ndx = freeidx;
9159 iverdef->vd_cnt = 1;
252b5132 9160
fc0e6df6 9161 iverdef->vd_bfd = abfd;
252b5132 9162
fc0e6df6
PB
9163 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9164 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9165 goto error_return_verdef;
fc0e6df6 9166 iverdef->vd_nextdef = NULL;
601a03ba
AM
9167 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9168 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9169 if (iverdef->vd_auxptr == NULL)
9170 goto error_return_verdef;
252b5132 9171
fc0e6df6
PB
9172 iverdaux = iverdef->vd_auxptr;
9173 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9174 }
9175
0a1b45a2 9176 return true;
252b5132
RH
9177
9178 error_return:
c9594989 9179 free (contents);
0a1b45a2 9180 return false;
252b5132
RH
9181}
9182\f
9183asymbol *
217aa764 9184_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9185{
9186 elf_symbol_type *newsym;
9187
7a6e0d89 9188 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9189 if (!newsym)
9190 return NULL;
201159ec
NC
9191 newsym->symbol.the_bfd = abfd;
9192 return &newsym->symbol;
252b5132
RH
9193}
9194
9195void
217aa764
AM
9196_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9197 asymbol *symbol,
9198 symbol_info *ret)
252b5132
RH
9199{
9200 bfd_symbol_info (symbol, ret);
9201}
9202
9203/* Return whether a symbol name implies a local symbol. Most targets
9204 use this function for the is_local_label_name entry point, but some
9205 override it. */
9206
0a1b45a2 9207bool
217aa764
AM
9208_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9209 const char *name)
252b5132
RH
9210{
9211 /* Normal local symbols start with ``.L''. */
9212 if (name[0] == '.' && name[1] == 'L')
0a1b45a2 9213 return true;
252b5132
RH
9214
9215 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9216 DWARF debugging symbols starting with ``..''. */
9217 if (name[0] == '.' && name[1] == '.')
0a1b45a2 9218 return true;
252b5132
RH
9219
9220 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9221 emitting DWARF debugging output. I suspect this is actually a
9222 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9223 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9224 underscore to be emitted on some ELF targets). For ease of use,
9225 we treat such symbols as local. */
9226 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
0a1b45a2 9227 return true;
252b5132 9228
b1fa9dd6
NC
9229 /* Treat assembler generated fake symbols, dollar local labels and
9230 forward-backward labels (aka local labels) as locals.
9231 These labels have the form:
9232
07d6d2b8 9233 L0^A.* (fake symbols)
b1fa9dd6
NC
9234
9235 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9236
9237 Versions which start with .L will have already been matched above,
9238 so we only need to match the rest. */
9239 if (name[0] == 'L' && ISDIGIT (name[1]))
9240 {
0a1b45a2 9241 bool ret = false;
b1fa9dd6
NC
9242 const char * p;
9243 char c;
9244
9245 for (p = name + 2; (c = *p); p++)
9246 {
9247 if (c == 1 || c == 2)
9248 {
9249 if (c == 1 && p == name + 2)
9250 /* A fake symbol. */
0a1b45a2 9251 return true;
b1fa9dd6
NC
9252
9253 /* FIXME: We are being paranoid here and treating symbols like
9254 L0^Bfoo as if there were non-local, on the grounds that the
9255 assembler will never generate them. But can any symbol
9256 containing an ASCII value in the range 1-31 ever be anything
9257 other than some kind of local ? */
0a1b45a2 9258 ret = true;
b1fa9dd6
NC
9259 }
9260
9261 if (! ISDIGIT (c))
9262 {
0a1b45a2 9263 ret = false;
b1fa9dd6
NC
9264 break;
9265 }
9266 }
9267 return ret;
9268 }
ffa54770 9269
0a1b45a2 9270 return false;
252b5132
RH
9271}
9272
9273alent *
217aa764
AM
9274_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9275 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9276{
9277 abort ();
9278 return NULL;
9279}
9280
0a1b45a2 9281bool
217aa764
AM
9282_bfd_elf_set_arch_mach (bfd *abfd,
9283 enum bfd_architecture arch,
9284 unsigned long machine)
252b5132
RH
9285{
9286 /* If this isn't the right architecture for this backend, and this
9287 isn't the generic backend, fail. */
9288 if (arch != get_elf_backend_data (abfd)->arch
9289 && arch != bfd_arch_unknown
9290 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
0a1b45a2 9291 return false;
252b5132
RH
9292
9293 return bfd_default_set_arch_mach (abfd, arch, machine);
9294}
9295
d1fad7c6
NC
9296/* Find the nearest line to a particular section and offset,
9297 for error reporting. */
9298
0a1b45a2 9299bool
217aa764 9300_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9301 asymbol **symbols,
fb167eb2 9302 asection *section,
217aa764
AM
9303 bfd_vma offset,
9304 const char **filename_ptr,
9305 const char **functionname_ptr,
fb167eb2
AM
9306 unsigned int *line_ptr,
9307 unsigned int *discriminator_ptr)
6e7a29c7
AM
9308{
9309 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9310 offset, filename_ptr,
9311 functionname_ptr, line_ptr,
9312 discriminator_ptr);
9313}
9314
9315/* Find the nearest line to a particular section and offset,
9316 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9317 can be optionally specified. */
9318
9319bool
9320_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9321 const char *alt_filename,
9322 asymbol **symbols,
9323 asection *section,
9324 bfd_vma offset,
9325 const char **filename_ptr,
9326 const char **functionname_ptr,
9327 unsigned int *line_ptr,
9328 unsigned int *discriminator_ptr)
d1fad7c6 9329{
0a1b45a2 9330 bool found;
d1fad7c6 9331
6e7a29c7
AM
9332 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9333 section, offset, filename_ptr,
9334 functionname_ptr, line_ptr,
9335 discriminator_ptr,
9336 dwarf_debug_sections,
9337 &elf_tdata (abfd)->dwarf2_find_line_info))
0a1b45a2 9338 return true;
e7679060
AM
9339
9340 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9341 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9342 {
9343 if (!*functionname_ptr)
e00e8198
AM
9344 _bfd_elf_find_function (abfd, symbols, section, offset,
9345 *filename_ptr ? NULL : filename_ptr,
9346 functionname_ptr);
0a1b45a2 9347 return true;
d1fad7c6
NC
9348 }
9349
9350 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9351 &found, filename_ptr,
9352 functionname_ptr, line_ptr,
9353 &elf_tdata (abfd)->line_info))
0a1b45a2 9354 return false;
dc43ada5 9355 if (found && (*functionname_ptr || *line_ptr))
0a1b45a2 9356 return true;
d1fad7c6
NC
9357
9358 if (symbols == NULL)
0a1b45a2 9359 return false;
d1fad7c6 9360
e00e8198
AM
9361 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9362 filename_ptr, functionname_ptr))
0a1b45a2 9363 return false;
d1fad7c6 9364
252b5132 9365 *line_ptr = 0;
0a1b45a2 9366 return true;
252b5132
RH
9367}
9368
5420f73d
L
9369/* Find the line for a symbol. */
9370
0a1b45a2 9371bool
5420f73d
L
9372_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9373 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9374{
11c6a7c6 9375 struct elf_obj_tdata *tdata = elf_tdata (abfd);
fb167eb2
AM
9376 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9377 filename_ptr, NULL, line_ptr, NULL,
9defd221 9378 dwarf_debug_sections,
11c6a7c6 9379 &tdata->dwarf2_find_line_info);
5420f73d
L
9380}
9381
4ab527b0
FF
9382/* After a call to bfd_find_nearest_line, successive calls to
9383 bfd_find_inliner_info can be used to get source information about
9384 each level of function inlining that terminated at the address
9385 passed to bfd_find_nearest_line. Currently this is only supported
9386 for DWARF2 with appropriate DWARF3 extensions. */
9387
0a1b45a2 9388bool
4ab527b0
FF
9389_bfd_elf_find_inliner_info (bfd *abfd,
9390 const char **filename_ptr,
9391 const char **functionname_ptr,
9392 unsigned int *line_ptr)
9393{
11c6a7c6
AM
9394 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9395 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9396 functionname_ptr, line_ptr,
9397 &tdata->dwarf2_find_line_info);
4ab527b0
FF
9398}
9399
252b5132 9400int
a6b96beb 9401_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9402{
8ded5a0f
AM
9403 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9404 int ret = bed->s->sizeof_ehdr;
252b5132 9405
0e1862bb 9406 if (!bfd_link_relocatable (info))
8ded5a0f 9407 {
12bd6957 9408 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9409
62d7a5f6
AM
9410 if (phdr_size == (bfd_size_type) -1)
9411 {
9412 struct elf_segment_map *m;
9413
9414 phdr_size = 0;
12bd6957 9415 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9416 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9417
62d7a5f6
AM
9418 if (phdr_size == 0)
9419 phdr_size = get_program_header_size (abfd, info);
9420 }
8ded5a0f 9421
12bd6957 9422 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9423 ret += phdr_size;
9424 }
9425
252b5132
RH
9426 return ret;
9427}
9428
0a1b45a2 9429bool
217aa764
AM
9430_bfd_elf_set_section_contents (bfd *abfd,
9431 sec_ptr section,
0f867abe 9432 const void *location,
217aa764
AM
9433 file_ptr offset,
9434 bfd_size_type count)
252b5132
RH
9435{
9436 Elf_Internal_Shdr *hdr;
9437
9438 if (! abfd->output_has_begun
217aa764 9439 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 9440 return false;
252b5132 9441
0ce398f1 9442 if (!count)
0a1b45a2 9443 return true;
0ce398f1 9444
252b5132 9445 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9446 if (hdr->sh_offset == (file_ptr) -1)
9447 {
a0dcf297
NC
9448 unsigned char *contents;
9449
1ff6de03
NA
9450 if (bfd_section_is_ctf (section))
9451 /* Nothing to do with this section: the contents are generated
9452 later. */
0a1b45a2 9453 return true;
1ff6de03 9454
a0dcf297
NC
9455 if ((offset + count) > hdr->sh_size)
9456 {
9457 _bfd_error_handler
11c6a7c6
AM
9458 (_("%pB:%pA: error: attempting to write"
9459 " over the end of the section"),
a0dcf297
NC
9460 abfd, section);
9461
9462 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9463 return false;
a0dcf297
NC
9464 }
9465
9466 contents = hdr->contents;
9467 if (contents == NULL)
9468 {
9469 _bfd_error_handler
11c6a7c6
AM
9470 (_("%pB:%pA: error: attempting to write"
9471 " section into an empty buffer"),
a0dcf297
NC
9472 abfd, section);
9473
9474 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9475 return false;
a0dcf297
NC
9476 }
9477
0ce398f1 9478 memcpy (contents + offset, location, count);
0a1b45a2 9479 return true;
0ce398f1 9480 }
a0dcf297 9481
169692ce
PA
9482 return _bfd_generic_set_section_contents (abfd, section,
9483 location, offset, count);
252b5132
RH
9484}
9485
0a1b45a2 9486bool
217aa764
AM
9487_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9488 arelent *cache_ptr ATTRIBUTE_UNUSED,
9489 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9490{
9491 abort ();
0a1b45a2 9492 return false;
252b5132
RH
9493}
9494
252b5132
RH
9495/* Try to convert a non-ELF reloc into an ELF one. */
9496
0a1b45a2 9497bool
217aa764 9498_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9499{
c044fabd 9500 /* Check whether we really have an ELF howto. */
252b5132
RH
9501
9502 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9503 {
9504 bfd_reloc_code_real_type code;
9505 reloc_howto_type *howto;
9506
9507 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9508 equivalent ELF reloc. */
252b5132
RH
9509
9510 if (areloc->howto->pc_relative)
9511 {
9512 switch (areloc->howto->bitsize)
9513 {
9514 case 8:
9515 code = BFD_RELOC_8_PCREL;
9516 break;
9517 case 12:
9518 code = BFD_RELOC_12_PCREL;
9519 break;
9520 case 16:
9521 code = BFD_RELOC_16_PCREL;
9522 break;
9523 case 24:
9524 code = BFD_RELOC_24_PCREL;
9525 break;
9526 case 32:
9527 code = BFD_RELOC_32_PCREL;
9528 break;
9529 case 64:
9530 code = BFD_RELOC_64_PCREL;
9531 break;
9532 default:
9533 goto fail;
9534 }
9535
9536 howto = bfd_reloc_type_lookup (abfd, code);
9537
94698d01 9538 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9539 {
9540 if (howto->pcrel_offset)
9541 areloc->addend += areloc->address;
9542 else
9543 areloc->addend -= areloc->address; /* addend is unsigned!! */
9544 }
9545 }
9546 else
9547 {
9548 switch (areloc->howto->bitsize)
9549 {
9550 case 8:
9551 code = BFD_RELOC_8;
9552 break;
9553 case 14:
9554 code = BFD_RELOC_14;
9555 break;
9556 case 16:
9557 code = BFD_RELOC_16;
9558 break;
9559 case 26:
9560 code = BFD_RELOC_26;
9561 break;
9562 case 32:
9563 code = BFD_RELOC_32;
9564 break;
9565 case 64:
9566 code = BFD_RELOC_64;
9567 break;
9568 default:
9569 goto fail;
9570 }
9571
9572 howto = bfd_reloc_type_lookup (abfd, code);
9573 }
9574
9575 if (howto)
9576 areloc->howto = howto;
9577 else
9578 goto fail;
9579 }
9580
0a1b45a2 9581 return true;
252b5132
RH
9582
9583 fail:
0aa13fee
AM
9584 /* xgettext:c-format */
9585 _bfd_error_handler (_("%pB: %s unsupported"),
9586 abfd, areloc->howto->name);
9aea1e31 9587 bfd_set_error (bfd_error_sorry);
0a1b45a2 9588 return false;
252b5132
RH
9589}
9590
0a1b45a2 9591bool
ba75d1c5 9592_bfd_elf_free_cached_info (bfd *abfd)
252b5132 9593{
ba75d1c5
AM
9594 struct elf_obj_tdata *tdata;
9595
9596 if ((bfd_get_format (abfd) == bfd_object
9597 || bfd_get_format (abfd) == bfd_core)
9598 && (tdata = elf_tdata (abfd)) != NULL)
252b5132 9599 {
ba75d1c5 9600 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9601 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9602 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
17749b39 9603 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
f3bc6035 9604 _bfd_stab_cleanup (abfd, &tdata->line_info);
252b5132
RH
9605 }
9606
ba75d1c5 9607 return _bfd_generic_bfd_free_cached_info (abfd);
252b5132
RH
9608}
9609
9610/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9611 in the relocation's offset. Thus we cannot allow any sort of sanity
9612 range-checking to interfere. There is nothing else to do in processing
9613 this reloc. */
9614
9615bfd_reloc_status_type
217aa764
AM
9616_bfd_elf_rel_vtable_reloc_fn
9617 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9618 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9619 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9620 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9621{
9622 return bfd_reloc_ok;
9623}
252b5132
RH
9624\f
9625/* Elf core file support. Much of this only works on native
9626 toolchains, since we rely on knowing the
9627 machine-dependent procfs structure in order to pick
c044fabd 9628 out details about the corefile. */
252b5132
RH
9629
9630#ifdef HAVE_SYS_PROCFS_H
9631# include <sys/procfs.h>
9632#endif
9633
261b8d08
PA
9634/* Return a PID that identifies a "thread" for threaded cores, or the
9635 PID of the main process for non-threaded cores. */
252b5132
RH
9636
9637static int
217aa764 9638elfcore_make_pid (bfd *abfd)
252b5132 9639{
261b8d08
PA
9640 int pid;
9641
228e534f 9642 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9643 if (pid == 0)
228e534f 9644 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9645
9646 return pid;
252b5132
RH
9647}
9648
11c6a7c6
AM
9649/* If there isn't a section called NAME, make one, using data from
9650 SECT. Note, this function will generate a reference to NAME, so
9651 you shouldn't deallocate or overwrite it. */
252b5132 9652
0a1b45a2 9653static bool
217aa764 9654elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9655{
c044fabd 9656 asection *sect2;
252b5132
RH
9657
9658 if (bfd_get_section_by_name (abfd, name) != NULL)
0a1b45a2 9659 return true;
252b5132 9660
117ed4f8 9661 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9662 if (sect2 == NULL)
0a1b45a2 9663 return false;
252b5132 9664
eea6121a 9665 sect2->size = sect->size;
252b5132 9666 sect2->filepos = sect->filepos;
252b5132 9667 sect2->alignment_power = sect->alignment_power;
0a1b45a2 9668 return true;
252b5132
RH
9669}
9670
bb0082d6
AM
9671/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9672 actually creates up to two pseudosections:
9673 - For the single-threaded case, a section named NAME, unless
9674 such a section already exists.
9675 - For the multi-threaded case, a section named "NAME/PID", where
9676 PID is elfcore_make_pid (abfd).
24d3e51b 9677 Both pseudosections have identical contents. */
0a1b45a2 9678bool
217aa764
AM
9679_bfd_elfcore_make_pseudosection (bfd *abfd,
9680 char *name,
9681 size_t size,
9682 ufile_ptr filepos)
bb0082d6
AM
9683{
9684 char buf[100];
9685 char *threaded_name;
d4c88bbb 9686 size_t len;
bb0082d6
AM
9687 asection *sect;
9688
9689 /* Build the section name. */
9690
9691 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9692 len = strlen (buf) + 1;
a50b1753 9693 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9694 if (threaded_name == NULL)
0a1b45a2 9695 return false;
d4c88bbb 9696 memcpy (threaded_name, buf, len);
bb0082d6 9697
117ed4f8
AM
9698 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9699 SEC_HAS_CONTENTS);
bb0082d6 9700 if (sect == NULL)
0a1b45a2 9701 return false;
eea6121a 9702 sect->size = size;
bb0082d6 9703 sect->filepos = filepos;
bb0082d6
AM
9704 sect->alignment_power = 2;
9705
936e320b 9706 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9707}
9708
0a1b45a2 9709static bool
58e07198
CZ
9710elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9711 size_t offs)
9712{
9713 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9714 SEC_HAS_CONTENTS);
9715
9716 if (sect == NULL)
0a1b45a2 9717 return false;
58e07198
CZ
9718
9719 sect->size = note->descsz - offs;
9720 sect->filepos = note->descpos + offs;
9721 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9722
0a1b45a2 9723 return true;
58e07198
CZ
9724}
9725
252b5132 9726/* prstatus_t exists on:
4a938328 9727 solaris 2.5+
252b5132
RH
9728 linux 2.[01] + glibc
9729 unixware 4.2
9730*/
9731
9732#if defined (HAVE_PRSTATUS_T)
a7b97311 9733
0a1b45a2 9734static bool
217aa764 9735elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9736{
eea6121a 9737 size_t size;
7ee38065 9738 int offset;
252b5132 9739
4a938328
MS
9740 if (note->descsz == sizeof (prstatus_t))
9741 {
9742 prstatus_t prstat;
252b5132 9743
eea6121a 9744 size = sizeof (prstat.pr_reg);
7ee38065 9745 offset = offsetof (prstatus_t, pr_reg);
4a938328 9746 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9747
fa49d224
NC
9748 /* Do not overwrite the core signal if it
9749 has already been set by another thread. */
228e534f
AM
9750 if (elf_tdata (abfd)->core->signal == 0)
9751 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9752 if (elf_tdata (abfd)->core->pid == 0)
9753 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9754
4a938328
MS
9755 /* pr_who exists on:
9756 solaris 2.5+
9757 unixware 4.2
9758 pr_who doesn't exist on:
9759 linux 2.[01]
9760 */
252b5132 9761#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9762 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9763#else
228e534f 9764 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9765#endif
4a938328 9766 }
7ee38065 9767#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9768 else if (note->descsz == sizeof (prstatus32_t))
9769 {
9770 /* 64-bit host, 32-bit corefile */
9771 prstatus32_t prstat;
9772
eea6121a 9773 size = sizeof (prstat.pr_reg);
7ee38065 9774 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9775 memcpy (&prstat, note->descdata, sizeof (prstat));
9776
fa49d224
NC
9777 /* Do not overwrite the core signal if it
9778 has already been set by another thread. */
228e534f
AM
9779 if (elf_tdata (abfd)->core->signal == 0)
9780 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9781 if (elf_tdata (abfd)->core->pid == 0)
9782 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9783
9784 /* pr_who exists on:
9785 solaris 2.5+
9786 unixware 4.2
9787 pr_who doesn't exist on:
9788 linux 2.[01]
9789 */
7ee38065 9790#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9791 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9792#else
228e534f 9793 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9794#endif
9795 }
7ee38065 9796#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9797 else
9798 {
9799 /* Fail - we don't know how to handle any other
9800 note size (ie. data object type). */
0a1b45a2 9801 return true;
4a938328 9802 }
252b5132 9803
bb0082d6 9804 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9805 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9806 size, note->descpos + offset);
252b5132
RH
9807}
9808#endif /* defined (HAVE_PRSTATUS_T) */
9809
bb0082d6 9810/* Create a pseudosection containing the exact contents of NOTE. */
0a1b45a2 9811static bool
217aa764
AM
9812elfcore_make_note_pseudosection (bfd *abfd,
9813 char *name,
9814 Elf_Internal_Note *note)
252b5132 9815{
936e320b
AM
9816 return _bfd_elfcore_make_pseudosection (abfd, name,
9817 note->descsz, note->descpos);
252b5132
RH
9818}
9819
ff08c6bb
JB
9820/* There isn't a consistent prfpregset_t across platforms,
9821 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9822 data structure apart. */
9823
0a1b45a2 9824static bool
217aa764 9825elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9826{
9827 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9828}
9829
ff08c6bb 9830/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9831 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9832 literally. */
c044fabd 9833
0a1b45a2 9834static bool
217aa764 9835elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9836{
9837 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9838}
9839
4339cae0
L
9840/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9841 with a note type of NT_X86_XSTATE. Just include the whole note's
9842 contents literally. */
9843
0a1b45a2 9844static bool
4339cae0
L
9845elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9846{
9847 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9848}
9849
0a1b45a2 9850static bool
97753bd5
AM
9851elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9852{
9853 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9854}
9855
0a1b45a2 9856static bool
89eeb0bc
LM
9857elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9858{
9859 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9860}
97753bd5 9861
0a1b45a2 9862static bool
cb2366c1
EBM
9863elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9864{
9865 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9866}
9867
0a1b45a2 9868static bool
cb2366c1
EBM
9869elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9870{
9871 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9872}
9873
0a1b45a2 9874static bool
cb2366c1
EBM
9875elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9876{
9877 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9878}
9879
0a1b45a2 9880static bool
cb2366c1
EBM
9881elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9882{
9883 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9884}
9885
0a1b45a2 9886static bool
cb2366c1
EBM
9887elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9888{
9889 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9890}
9891
0a1b45a2 9892static bool
cb2366c1
EBM
9893elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9894{
9895 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9896}
9897
0a1b45a2 9898static bool
cb2366c1
EBM
9899elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9900{
9901 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9902}
9903
0a1b45a2 9904static bool
cb2366c1
EBM
9905elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9906{
9907 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9908}
9909
0a1b45a2 9910static bool
cb2366c1
EBM
9911elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9912{
9913 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9914}
9915
0a1b45a2 9916static bool
cb2366c1
EBM
9917elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9918{
9919 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9920}
9921
0a1b45a2 9922static bool
cb2366c1
EBM
9923elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9924{
9925 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9926}
9927
0a1b45a2 9928static bool
cb2366c1
EBM
9929elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9930{
9931 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9932}
9933
0a1b45a2 9934static bool
cb2366c1
EBM
9935elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9936{
9937 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9938}
9939
0a1b45a2 9940static bool
0675e188
UW
9941elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9942{
9943 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9944}
9945
0a1b45a2 9946static bool
d7eeb400
MS
9947elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9948{
9949 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9950}
9951
0a1b45a2 9952static bool
d7eeb400
MS
9953elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9954{
9955 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9956}
9957
0a1b45a2 9958static bool
d7eeb400
MS
9959elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9960{
9961 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9962}
9963
0a1b45a2 9964static bool
d7eeb400
MS
9965elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9966{
9967 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9968}
9969
0a1b45a2 9970static bool
d7eeb400
MS
9971elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9972{
9973 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9974}
9975
0a1b45a2 9976static bool
355b81d9
UW
9977elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9978{
9979 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9980}
9981
0a1b45a2 9982static bool
355b81d9
UW
9983elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9984{
9985 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9986}
9987
0a1b45a2 9988static bool
abb3f6cc
NC
9989elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9990{
9991 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9992}
9993
0a1b45a2 9994static bool
4ef9f41a
AA
9995elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9996{
9997 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9998}
9999
0a1b45a2 10000static bool
4ef9f41a
AA
10001elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10002{
10003 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10004}
10005
0a1b45a2 10006static bool
88ab90e8
AA
10007elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10008{
10009 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10010}
10011
0a1b45a2 10012static bool
88ab90e8
AA
10013elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10014{
10015 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10016}
10017
0a1b45a2 10018static bool
faa9a424
UW
10019elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10020{
10021 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10022}
10023
0a1b45a2 10024static bool
652451f8
YZ
10025elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10026{
10027 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10028}
10029
0a1b45a2 10030static bool
652451f8
YZ
10031elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10032{
10033 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10034}
10035
0a1b45a2 10036static bool
652451f8
YZ
10037elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10038{
10039 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10040}
10041
0a1b45a2 10042static bool
ad1cc4e4
AH
10043elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10044{
10045 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10046}
10047
0a1b45a2 10048static bool
e6c3b5bf
AH
10049elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10050{
10051 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10052}
10053
f0bbe8ba
LM
10054static bool
10055elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10056{
10057 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10058 note);
10059}
10060
0a1b45a2 10061static bool
27456742
AK
10062elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10063{
10064 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10065}
10066
db6092f3
AB
10067/* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10068 successful otherwise, return FALSE. */
10069
0a1b45a2 10070static bool
db6092f3
AB
10071elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10072{
10073 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10074}
10075
b63a5e38
AB
10076/* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10077 successful otherwise, return FALSE. */
10078
0a1b45a2 10079static bool
b63a5e38
AB
10080elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10081{
10082 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10083}
10084
e214f8db 10085static bool
10086elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10087{
10088 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10089}
10090
10091static bool
10092elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10093{
10094 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10095}
10096
10097static bool
10098elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10099{
10100 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10101}
10102
10103static bool
10104elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10105{
10106 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10107}
10108
252b5132 10109#if defined (HAVE_PRPSINFO_T)
4a938328 10110typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 10111#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10112typedef prpsinfo32_t elfcore_psinfo32_t;
10113#endif
252b5132
RH
10114#endif
10115
10116#if defined (HAVE_PSINFO_T)
4a938328 10117typedef psinfo_t elfcore_psinfo_t;
7ee38065 10118#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10119typedef psinfo32_t elfcore_psinfo32_t;
10120#endif
252b5132
RH
10121#endif
10122
252b5132
RH
10123/* return a malloc'ed copy of a string at START which is at
10124 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 10125 the copy will always have a terminating '\0'. */
252b5132 10126
936e320b 10127char *
217aa764 10128_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 10129{
dc810e39 10130 char *dups;
a50b1753 10131 char *end = (char *) memchr (start, '\0', max);
dc810e39 10132 size_t len;
252b5132
RH
10133
10134 if (end == NULL)
10135 len = max;
10136 else
10137 len = end - start;
10138
a50b1753 10139 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 10140 if (dups == NULL)
252b5132
RH
10141 return NULL;
10142
dc810e39
AM
10143 memcpy (dups, start, len);
10144 dups[len] = '\0';
252b5132 10145
dc810e39 10146 return dups;
252b5132
RH
10147}
10148
bb0082d6 10149#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
0a1b45a2 10150static bool
217aa764 10151elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 10152{
4a938328
MS
10153 if (note->descsz == sizeof (elfcore_psinfo_t))
10154 {
10155 elfcore_psinfo_t psinfo;
252b5132 10156
7ee38065 10157 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10158
335e41d4 10159#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 10160 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10161#endif
228e534f 10162 elf_tdata (abfd)->core->program
936e320b
AM
10163 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10164 sizeof (psinfo.pr_fname));
252b5132 10165
228e534f 10166 elf_tdata (abfd)->core->command
936e320b
AM
10167 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10168 sizeof (psinfo.pr_psargs));
4a938328 10169 }
7ee38065 10170#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
10171 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10172 {
10173 /* 64-bit host, 32-bit corefile */
10174 elfcore_psinfo32_t psinfo;
10175
7ee38065 10176 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10177
335e41d4 10178#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 10179 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10180#endif
228e534f 10181 elf_tdata (abfd)->core->program
936e320b
AM
10182 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10183 sizeof (psinfo.pr_fname));
4a938328 10184
228e534f 10185 elf_tdata (abfd)->core->command
936e320b
AM
10186 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10187 sizeof (psinfo.pr_psargs));
4a938328
MS
10188 }
10189#endif
10190
10191 else
10192 {
10193 /* Fail - we don't know how to handle any other
10194 note size (ie. data object type). */
0a1b45a2 10195 return true;
4a938328 10196 }
252b5132
RH
10197
10198 /* Note that for some reason, a spurious space is tacked
10199 onto the end of the args in some (at least one anyway)
c044fabd 10200 implementations, so strip it off if it exists. */
252b5132
RH
10201
10202 {
228e534f 10203 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10204 int n = strlen (command);
10205
10206 if (0 < n && command[n - 1] == ' ')
10207 command[n - 1] = '\0';
10208 }
10209
0a1b45a2 10210 return true;
252b5132
RH
10211}
10212#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10213
252b5132 10214#if defined (HAVE_PSTATUS_T)
0a1b45a2 10215static bool
217aa764 10216elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10217{
f572a39d
AM
10218 if (note->descsz == sizeof (pstatus_t)
10219#if defined (HAVE_PXSTATUS_T)
10220 || note->descsz == sizeof (pxstatus_t)
10221#endif
10222 )
4a938328
MS
10223 {
10224 pstatus_t pstat;
252b5132 10225
4a938328 10226 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10227
228e534f 10228 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10229 }
7ee38065 10230#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10231 else if (note->descsz == sizeof (pstatus32_t))
10232 {
10233 /* 64-bit host, 32-bit corefile */
10234 pstatus32_t pstat;
252b5132 10235
4a938328 10236 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10237
228e534f 10238 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10239 }
10240#endif
252b5132
RH
10241 /* Could grab some more details from the "representative"
10242 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10243 NT_LWPSTATUS note, presumably. */
252b5132 10244
0a1b45a2 10245 return true;
252b5132
RH
10246}
10247#endif /* defined (HAVE_PSTATUS_T) */
10248
252b5132 10249#if defined (HAVE_LWPSTATUS_T)
0a1b45a2 10250static bool
217aa764 10251elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10252{
10253 lwpstatus_t lwpstat;
10254 char buf[100];
c044fabd 10255 char *name;
d4c88bbb 10256 size_t len;
c044fabd 10257 asection *sect;
252b5132 10258
f572a39d
AM
10259 if (note->descsz != sizeof (lwpstat)
10260#if defined (HAVE_LWPXSTATUS_T)
10261 && note->descsz != sizeof (lwpxstatus_t)
10262#endif
10263 )
0a1b45a2 10264 return true;
252b5132
RH
10265
10266 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10267
228e534f 10268 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10269 /* Do not overwrite the core signal if it has already been set by
10270 another thread. */
228e534f
AM
10271 if (elf_tdata (abfd)->core->signal == 0)
10272 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10273
c044fabd 10274 /* Make a ".reg/999" section. */
252b5132
RH
10275
10276 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10277 len = strlen (buf) + 1;
217aa764 10278 name = bfd_alloc (abfd, len);
252b5132 10279 if (name == NULL)
0a1b45a2 10280 return false;
d4c88bbb 10281 memcpy (name, buf, len);
252b5132 10282
117ed4f8 10283 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10284 if (sect == NULL)
0a1b45a2 10285 return false;
252b5132
RH
10286
10287#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10288 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10289 sect->filepos = note->descpos
10290 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10291#endif
10292
10293#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10294 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10295 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10296#endif
10297
252b5132
RH
10298 sect->alignment_power = 2;
10299
10300 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10301 return false;
252b5132
RH
10302
10303 /* Make a ".reg2/999" section */
10304
10305 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10306 len = strlen (buf) + 1;
217aa764 10307 name = bfd_alloc (abfd, len);
252b5132 10308 if (name == NULL)
0a1b45a2 10309 return false;
d4c88bbb 10310 memcpy (name, buf, len);
252b5132 10311
117ed4f8 10312 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10313 if (sect == NULL)
0a1b45a2 10314 return false;
252b5132
RH
10315
10316#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10317 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10318 sect->filepos = note->descpos
10319 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10320#endif
10321
10322#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10323 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10324 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10325#endif
10326
252b5132
RH
10327 sect->alignment_power = 2;
10328
936e320b 10329 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10330}
10331#endif /* defined (HAVE_LWPSTATUS_T) */
10332
8fbac78b
JT
10333/* These constants, and the structure offsets used below, are defined by
10334 Cygwin's core_dump.h */
10335#define NOTE_INFO_PROCESS 1
10336#define NOTE_INFO_THREAD 2
10337#define NOTE_INFO_MODULE 3
d61f3d03 10338#define NOTE_INFO_MODULE64 4
8fbac78b 10339
0a1b45a2 10340static bool
217aa764 10341elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10342{
10343 char buf[30];
c044fabd 10344 char *name;
d4c88bbb 10345 size_t len;
3cdad084 10346 unsigned int name_size;
c044fabd 10347 asection *sect;
2fef9373 10348 unsigned int type;
4a6636fb
PA
10349 int is_active_thread;
10350 bfd_vma base_addr;
16e9c715 10351
04ec0fa2 10352 if (note->descsz < 4)
0a1b45a2 10353 return true;
16e9c715 10354
08dedd66 10355 if (! startswith (note->namedata, "win32"))
0a1b45a2 10356 return true;
4a6636fb
PA
10357
10358 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10359
7e0d77ef
NC
10360 struct
10361 {
404ec933
JT
10362 const char *type_name;
10363 unsigned long min_size;
10364 } size_check[] =
10365 {
10366 { "NOTE_INFO_PROCESS", 12 },
10367 { "NOTE_INFO_THREAD", 12 },
10368 { "NOTE_INFO_MODULE", 12 },
10369 { "NOTE_INFO_MODULE64", 16 },
10370 };
10371
7e0d77ef 10372 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
0a1b45a2 10373 return true;
404ec933
JT
10374
10375 if (note->descsz < size_check[type - 1].min_size)
10376 {
11c6a7c6
AM
10377 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10378 " is too small"),
10379 abfd, size_check[type - 1].type_name, note->descsz);
0a1b45a2 10380 return true;
404ec933
JT
10381 }
10382
4a6636fb 10383 switch (type)
16e9c715 10384 {
8fbac78b 10385 case NOTE_INFO_PROCESS:
228e534f 10386 /* FIXME: need to add ->core->command. */
ff2084b9 10387 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10388 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10389 break;
16e9c715 10390
8fbac78b 10391 case NOTE_INFO_THREAD:
ff2084b9 10392 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11c6a7c6 10393 structure. */
4a6636fb 10394 /* thread_info.tid */
ff2084b9 10395 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10396
d4c88bbb 10397 len = strlen (buf) + 1;
a50b1753 10398 name = (char *) bfd_alloc (abfd, len);
16e9c715 10399 if (name == NULL)
0a1b45a2 10400 return false;
c044fabd 10401
d4c88bbb 10402 memcpy (name, buf, len);
16e9c715 10403
117ed4f8 10404 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10405 if (sect == NULL)
0a1b45a2 10406 return false;
c044fabd 10407
4a6636fb 10408 /* sizeof (thread_info.thread_context) */
03c29a6f 10409 sect->size = note->descsz - 12;
4a6636fb
PA
10410 /* offsetof (thread_info.thread_context) */
10411 sect->filepos = note->descpos + 12;
16e9c715
NC
10412 sect->alignment_power = 2;
10413
4a6636fb
PA
10414 /* thread_info.is_active_thread */
10415 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10416
10417 if (is_active_thread)
16e9c715 10418 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10419 return false;
16e9c715
NC
10420 break;
10421
8fbac78b 10422 case NOTE_INFO_MODULE:
d61f3d03 10423 case NOTE_INFO_MODULE64:
16e9c715 10424 /* Make a ".module/xxxxxxxx" section. */
d61f3d03 10425 if (type == NOTE_INFO_MODULE)
11c6a7c6
AM
10426 {
10427 /* module_info.base_address */
10428 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10429 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10430 /* module_info.module_name_size */
10431 name_size = bfd_get_32 (abfd, note->descdata + 8);
10432 }
d61f3d03 10433 else /* NOTE_INFO_MODULE64 */
11c6a7c6
AM
10434 {
10435 /* module_info.base_address */
10436 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10437 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10438 /* module_info.module_name_size */
10439 name_size = bfd_get_32 (abfd, note->descdata + 12);
10440 }
c044fabd 10441
d4c88bbb 10442 len = strlen (buf) + 1;
a50b1753 10443 name = (char *) bfd_alloc (abfd, len);
16e9c715 10444 if (name == NULL)
0a1b45a2 10445 return false;
c044fabd 10446
d4c88bbb 10447 memcpy (name, buf, len);
252b5132 10448
117ed4f8 10449 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10450
16e9c715 10451 if (sect == NULL)
0a1b45a2 10452 return false;
c044fabd 10453
04ec0fa2 10454 if (note->descsz < 12 + name_size)
11c6a7c6
AM
10455 {
10456 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10457 " is too small to contain a name of size %u"),
10458 abfd, note->descsz, name_size);
10459 return true;
10460 }
04ec0fa2 10461
eea6121a 10462 sect->size = note->descsz;
16e9c715 10463 sect->filepos = note->descpos;
16e9c715
NC
10464 sect->alignment_power = 2;
10465 break;
10466
10467 default:
0a1b45a2 10468 return true;
16e9c715
NC
10469 }
10470
0a1b45a2 10471 return true;
16e9c715 10472}
252b5132 10473
0a1b45a2 10474static bool
217aa764 10475elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10476{
9c5bfbb7 10477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10478
252b5132
RH
10479 switch (note->type)
10480 {
10481 default:
0a1b45a2 10482 return true;
252b5132 10483
252b5132 10484 case NT_PRSTATUS:
bb0082d6
AM
10485 if (bed->elf_backend_grok_prstatus)
10486 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
0a1b45a2 10487 return true;
bb0082d6 10488#if defined (HAVE_PRSTATUS_T)
252b5132 10489 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10490#else
0a1b45a2 10491 return true;
252b5132
RH
10492#endif
10493
10494#if defined (HAVE_PSTATUS_T)
10495 case NT_PSTATUS:
10496 return elfcore_grok_pstatus (abfd, note);
10497#endif
10498
10499#if defined (HAVE_LWPSTATUS_T)
10500 case NT_LWPSTATUS:
10501 return elfcore_grok_lwpstatus (abfd, note);
10502#endif
10503
10504 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10505 return elfcore_grok_prfpreg (abfd, note);
10506
c044fabd 10507 case NT_WIN32PSTATUS:
16e9c715 10508 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10509
c044fabd 10510 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10511 if (note->namesz == 6
10512 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10513 return elfcore_grok_prxfpreg (abfd, note);
10514 else
0a1b45a2 10515 return true;
ff08c6bb 10516
4339cae0
L
10517 case NT_X86_XSTATE: /* Linux XSAVE extension */
10518 if (note->namesz == 6
10519 && strcmp (note->namedata, "LINUX") == 0)
10520 return elfcore_grok_xstatereg (abfd, note);
10521 else
0a1b45a2 10522 return true;
4339cae0 10523
97753bd5
AM
10524 case NT_PPC_VMX:
10525 if (note->namesz == 6
10526 && strcmp (note->namedata, "LINUX") == 0)
10527 return elfcore_grok_ppc_vmx (abfd, note);
10528 else
0a1b45a2 10529 return true;
97753bd5 10530
89eeb0bc
LM
10531 case NT_PPC_VSX:
10532 if (note->namesz == 6
07d6d2b8
AM
10533 && strcmp (note->namedata, "LINUX") == 0)
10534 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10535 else
0a1b45a2 10536 return true;
89eeb0bc 10537
cb2366c1
EBM
10538 case NT_PPC_TAR:
10539 if (note->namesz == 6
4b24dd1a
AM
10540 && strcmp (note->namedata, "LINUX") == 0)
10541 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10542 else
0a1b45a2 10543 return true;
cb2366c1
EBM
10544
10545 case NT_PPC_PPR:
10546 if (note->namesz == 6
4b24dd1a
AM
10547 && strcmp (note->namedata, "LINUX") == 0)
10548 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10549 else
0a1b45a2 10550 return true;
cb2366c1
EBM
10551
10552 case NT_PPC_DSCR:
10553 if (note->namesz == 6
4b24dd1a
AM
10554 && strcmp (note->namedata, "LINUX") == 0)
10555 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10556 else
0a1b45a2 10557 return true;
cb2366c1
EBM
10558
10559 case NT_PPC_EBB:
10560 if (note->namesz == 6
4b24dd1a
AM
10561 && strcmp (note->namedata, "LINUX") == 0)
10562 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10563 else
0a1b45a2 10564 return true;
cb2366c1
EBM
10565
10566 case NT_PPC_PMU:
10567 if (note->namesz == 6
4b24dd1a
AM
10568 && strcmp (note->namedata, "LINUX") == 0)
10569 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10570 else
0a1b45a2 10571 return true;
cb2366c1
EBM
10572
10573 case NT_PPC_TM_CGPR:
10574 if (note->namesz == 6
4b24dd1a
AM
10575 && strcmp (note->namedata, "LINUX") == 0)
10576 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10577 else
0a1b45a2 10578 return true;
cb2366c1
EBM
10579
10580 case NT_PPC_TM_CFPR:
10581 if (note->namesz == 6
4b24dd1a
AM
10582 && strcmp (note->namedata, "LINUX") == 0)
10583 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10584 else
0a1b45a2 10585 return true;
cb2366c1
EBM
10586
10587 case NT_PPC_TM_CVMX:
10588 if (note->namesz == 6
4b24dd1a
AM
10589 && strcmp (note->namedata, "LINUX") == 0)
10590 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10591 else
0a1b45a2 10592 return true;
cb2366c1
EBM
10593
10594 case NT_PPC_TM_CVSX:
10595 if (note->namesz == 6
4b24dd1a
AM
10596 && strcmp (note->namedata, "LINUX") == 0)
10597 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10598 else
0a1b45a2 10599 return true;
cb2366c1
EBM
10600
10601 case NT_PPC_TM_SPR:
10602 if (note->namesz == 6
4b24dd1a
AM
10603 && strcmp (note->namedata, "LINUX") == 0)
10604 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10605 else
0a1b45a2 10606 return true;
cb2366c1
EBM
10607
10608 case NT_PPC_TM_CTAR:
10609 if (note->namesz == 6
4b24dd1a
AM
10610 && strcmp (note->namedata, "LINUX") == 0)
10611 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10612 else
0a1b45a2 10613 return true;
cb2366c1
EBM
10614
10615 case NT_PPC_TM_CPPR:
10616 if (note->namesz == 6
4b24dd1a
AM
10617 && strcmp (note->namedata, "LINUX") == 0)
10618 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10619 else
0a1b45a2 10620 return true;
cb2366c1
EBM
10621
10622 case NT_PPC_TM_CDSCR:
10623 if (note->namesz == 6
4b24dd1a
AM
10624 && strcmp (note->namedata, "LINUX") == 0)
10625 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10626 else
0a1b45a2 10627 return true;
cb2366c1 10628
0675e188
UW
10629 case NT_S390_HIGH_GPRS:
10630 if (note->namesz == 6
07d6d2b8
AM
10631 && strcmp (note->namedata, "LINUX") == 0)
10632 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10633 else
0a1b45a2 10634 return true;
0675e188 10635
d7eeb400
MS
10636 case NT_S390_TIMER:
10637 if (note->namesz == 6
07d6d2b8
AM
10638 && strcmp (note->namedata, "LINUX") == 0)
10639 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10640 else
0a1b45a2 10641 return true;
d7eeb400
MS
10642
10643 case NT_S390_TODCMP:
10644 if (note->namesz == 6
07d6d2b8
AM
10645 && strcmp (note->namedata, "LINUX") == 0)
10646 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10647 else
0a1b45a2 10648 return true;
d7eeb400
MS
10649
10650 case NT_S390_TODPREG:
10651 if (note->namesz == 6
07d6d2b8
AM
10652 && strcmp (note->namedata, "LINUX") == 0)
10653 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10654 else
0a1b45a2 10655 return true;
d7eeb400
MS
10656
10657 case NT_S390_CTRS:
10658 if (note->namesz == 6
07d6d2b8
AM
10659 && strcmp (note->namedata, "LINUX") == 0)
10660 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10661 else
0a1b45a2 10662 return true;
d7eeb400
MS
10663
10664 case NT_S390_PREFIX:
10665 if (note->namesz == 6
07d6d2b8
AM
10666 && strcmp (note->namedata, "LINUX") == 0)
10667 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10668 else
0a1b45a2 10669 return true;
d7eeb400 10670
355b81d9
UW
10671 case NT_S390_LAST_BREAK:
10672 if (note->namesz == 6
07d6d2b8
AM
10673 && strcmp (note->namedata, "LINUX") == 0)
10674 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10675 else
0a1b45a2 10676 return true;
355b81d9
UW
10677
10678 case NT_S390_SYSTEM_CALL:
10679 if (note->namesz == 6
07d6d2b8
AM
10680 && strcmp (note->namedata, "LINUX") == 0)
10681 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10682 else
0a1b45a2 10683 return true;
355b81d9 10684
abb3f6cc
NC
10685 case NT_S390_TDB:
10686 if (note->namesz == 6
07d6d2b8
AM
10687 && strcmp (note->namedata, "LINUX") == 0)
10688 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10689 else
0a1b45a2 10690 return true;
abb3f6cc 10691
4ef9f41a
AA
10692 case NT_S390_VXRS_LOW:
10693 if (note->namesz == 6
10694 && strcmp (note->namedata, "LINUX") == 0)
10695 return elfcore_grok_s390_vxrs_low (abfd, note);
10696 else
0a1b45a2 10697 return true;
4ef9f41a
AA
10698
10699 case NT_S390_VXRS_HIGH:
10700 if (note->namesz == 6
10701 && strcmp (note->namedata, "LINUX") == 0)
10702 return elfcore_grok_s390_vxrs_high (abfd, note);
10703 else
0a1b45a2 10704 return true;
4ef9f41a 10705
88ab90e8
AA
10706 case NT_S390_GS_CB:
10707 if (note->namesz == 6
10708 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10709 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8 10710 else
0a1b45a2 10711 return true;
88ab90e8
AA
10712
10713 case NT_S390_GS_BC:
10714 if (note->namesz == 6
10715 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10716 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8 10717 else
0a1b45a2 10718 return true;
88ab90e8 10719
27456742
AK
10720 case NT_ARC_V2:
10721 if (note->namesz == 6
10722 && strcmp (note->namedata, "LINUX") == 0)
10723 return elfcore_grok_arc_v2 (abfd, note);
10724 else
0a1b45a2 10725 return true;
27456742 10726
faa9a424
UW
10727 case NT_ARM_VFP:
10728 if (note->namesz == 6
10729 && strcmp (note->namedata, "LINUX") == 0)
10730 return elfcore_grok_arm_vfp (abfd, note);
10731 else
0a1b45a2 10732 return true;
faa9a424 10733
652451f8
YZ
10734 case NT_ARM_TLS:
10735 if (note->namesz == 6
10736 && strcmp (note->namedata, "LINUX") == 0)
10737 return elfcore_grok_aarch_tls (abfd, note);
10738 else
0a1b45a2 10739 return true;
652451f8
YZ
10740
10741 case NT_ARM_HW_BREAK:
10742 if (note->namesz == 6
10743 && strcmp (note->namedata, "LINUX") == 0)
10744 return elfcore_grok_aarch_hw_break (abfd, note);
10745 else
0a1b45a2 10746 return true;
652451f8
YZ
10747
10748 case NT_ARM_HW_WATCH:
10749 if (note->namesz == 6
10750 && strcmp (note->namedata, "LINUX") == 0)
10751 return elfcore_grok_aarch_hw_watch (abfd, note);
10752 else
0a1b45a2 10753 return true;
652451f8 10754
ad1cc4e4
AH
10755 case NT_ARM_SVE:
10756 if (note->namesz == 6
10757 && strcmp (note->namedata, "LINUX") == 0)
10758 return elfcore_grok_aarch_sve (abfd, note);
10759 else
0a1b45a2 10760 return true;
ad1cc4e4 10761
e6c3b5bf
AH
10762 case NT_ARM_PAC_MASK:
10763 if (note->namesz == 6
10764 && strcmp (note->namedata, "LINUX") == 0)
10765 return elfcore_grok_aarch_pauth (abfd, note);
10766 else
0a1b45a2 10767 return true;
e6c3b5bf 10768
f0bbe8ba
LM
10769 case NT_ARM_TAGGED_ADDR_CTRL:
10770 if (note->namesz == 6
10771 && strcmp (note->namedata, "LINUX") == 0)
10772 return elfcore_grok_aarch_mte (abfd, note);
10773 else
10774 return true;
10775
b63a5e38
AB
10776 case NT_GDB_TDESC:
10777 if (note->namesz == 4
11c6a7c6
AM
10778 && strcmp (note->namedata, "GDB") == 0)
10779 return elfcore_grok_gdb_tdesc (abfd, note);
b63a5e38 10780 else
11c6a7c6 10781 return true;
b63a5e38 10782
db6092f3
AB
10783 case NT_RISCV_CSR:
10784 if (note->namesz == 4
11c6a7c6
AM
10785 && strcmp (note->namedata, "GDB") == 0)
10786 return elfcore_grok_riscv_csr (abfd, note);
db6092f3 10787 else
0a1b45a2 10788 return true;
db6092f3 10789
e214f8db 10790 case NT_LARCH_CPUCFG:
10791 if (note->namesz == 6
10792 && strcmp (note->namedata, "LINUX") == 0)
10793 return elfcore_grok_loongarch_cpucfg (abfd, note);
10794 else
10795 return true;
10796
10797 case NT_LARCH_LBT:
10798 if (note->namesz == 6
10799 && strcmp (note->namedata, "LINUX") == 0)
10800 return elfcore_grok_loongarch_lbt (abfd, note);
10801 else
10802 return true;
10803
10804 case NT_LARCH_LSX:
10805 if (note->namesz == 6
10806 && strcmp (note->namedata, "LINUX") == 0)
10807 return elfcore_grok_loongarch_lsx (abfd, note);
10808 else
10809 return true;
10810
10811 case NT_LARCH_LASX:
10812 if (note->namesz == 6
10813 && strcmp (note->namedata, "LINUX") == 0)
10814 return elfcore_grok_loongarch_lasx (abfd, note);
10815 else
10816 return true;
10817
252b5132
RH
10818 case NT_PRPSINFO:
10819 case NT_PSINFO:
bb0082d6
AM
10820 if (bed->elf_backend_grok_psinfo)
10821 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
0a1b45a2 10822 return true;
bb0082d6 10823#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10824 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10825#else
0a1b45a2 10826 return true;
252b5132 10827#endif
3333a7c3
RM
10828
10829 case NT_AUXV:
58e07198 10830 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10831
451b7c33
TT
10832 case NT_FILE:
10833 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10834 note);
10835
9015683b
TT
10836 case NT_SIGINFO:
10837 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10838 note);
5b2c414d 10839
252b5132
RH
10840 }
10841}
10842
0a1b45a2 10843static bool
718175fa
JK
10844elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10845{
c74f7d1c 10846 struct bfd_build_id* build_id;
30e8ee25
AM
10847
10848 if (note->descsz == 0)
0a1b45a2 10849 return false;
30e8ee25 10850
c74f7d1c
JT
10851 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10852 if (build_id == NULL)
0a1b45a2 10853 return false;
718175fa 10854
c74f7d1c
JT
10855 build_id->size = note->descsz;
10856 memcpy (build_id->data, note->descdata, note->descsz);
10857 abfd->build_id = build_id;
718175fa 10858
0a1b45a2 10859 return true;
718175fa
JK
10860}
10861
0a1b45a2 10862static bool
718175fa
JK
10863elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10864{
10865 switch (note->type)
10866 {
10867 default:
0a1b45a2 10868 return true;
718175fa 10869
46bed679
L
10870 case NT_GNU_PROPERTY_TYPE_0:
10871 return _bfd_elf_parse_gnu_properties (abfd, note);
10872
718175fa
JK
10873 case NT_GNU_BUILD_ID:
10874 return elfobj_grok_gnu_build_id (abfd, note);
10875 }
10876}
10877
0a1b45a2 10878static bool
e21e5835
NC
10879elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10880{
10881 struct sdt_note *cur =
7a6e0d89
AM
10882 (struct sdt_note *) bfd_alloc (abfd,
10883 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10884
10885 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10886 cur->size = (bfd_size_type) note->descsz;
10887 memcpy (cur->data, note->descdata, note->descsz);
10888
10889 elf_tdata (abfd)->sdt_note_head = cur;
10890
0a1b45a2 10891 return true;
e21e5835
NC
10892}
10893
0a1b45a2 10894static bool
e21e5835
NC
10895elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10896{
10897 switch (note->type)
10898 {
10899 case NT_STAPSDT:
10900 return elfobj_grok_stapsdt_note_1 (abfd, note);
10901
10902 default:
0a1b45a2 10903 return true;
e21e5835
NC
10904 }
10905}
10906
0a1b45a2 10907static bool
aa1ed4a9
JB
10908elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10909{
10910 size_t offset;
10911
b5430a3c 10912 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10913 {
b5430a3c 10914 case ELFCLASS32:
0064d223 10915 if (note->descsz < 108)
0a1b45a2 10916 return false;
aa1ed4a9
JB
10917 break;
10918
b5430a3c 10919 case ELFCLASS64:
0064d223 10920 if (note->descsz < 120)
0a1b45a2 10921 return false;
aa1ed4a9
JB
10922 break;
10923
10924 default:
0a1b45a2 10925 return false;
aa1ed4a9
JB
10926 }
10927
0064d223
JB
10928 /* Check for version 1 in pr_version. */
10929 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10930 return false;
80a04378 10931
0064d223
JB
10932 offset = 4;
10933
10934 /* Skip over pr_psinfosz. */
b5430a3c 10935 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10936 offset += 4;
10937 else
10938 {
10939 offset += 4; /* Padding before pr_psinfosz. */
10940 offset += 8;
10941 }
10942
aa1ed4a9
JB
10943 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10944 elf_tdata (abfd)->core->program
10945 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10946 offset += 17;
10947
10948 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10949 elf_tdata (abfd)->core->command
10950 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10951 offset += 81;
10952
10953 /* Padding before pr_pid. */
10954 offset += 2;
10955
10956 /* The pr_pid field was added in version "1a". */
10957 if (note->descsz < offset + 4)
0a1b45a2 10958 return true;
0064d223
JB
10959
10960 elf_tdata (abfd)->core->pid
10961 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9 10962
0a1b45a2 10963 return true;
aa1ed4a9
JB
10964}
10965
0a1b45a2 10966static bool
aa1ed4a9
JB
10967elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10968{
10969 size_t offset;
10970 size_t size;
24d3e51b 10971 size_t min_size;
aa1ed4a9 10972
24d3e51b
NC
10973 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10974 Also compute minimum size of this note. */
b5430a3c 10975 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10976 {
b5430a3c 10977 case ELFCLASS32:
24d3e51b
NC
10978 offset = 4 + 4;
10979 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10980 break;
10981
b5430a3c 10982 case ELFCLASS64:
24d3e51b
NC
10983 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10984 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10985 break;
10986
10987 default:
0a1b45a2 10988 return false;
aa1ed4a9
JB
10989 }
10990
24d3e51b 10991 if (note->descsz < min_size)
0a1b45a2 10992 return false;
24d3e51b
NC
10993
10994 /* Check for version 1 in pr_version. */
10995 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10996 return false;
aa1ed4a9 10997
24d3e51b
NC
10998 /* Extract size of pr_reg from pr_gregsetsz. */
10999 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 11000 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
11001 {
11002 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11003 offset += 4 * 2;
11004 }
b5430a3c 11005 else
24d3e51b
NC
11006 {
11007 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11008 offset += 8 * 2;
11009 }
aa1ed4a9 11010
24d3e51b 11011 /* Skip over pr_osreldate. */
aa1ed4a9
JB
11012 offset += 4;
11013
24d3e51b 11014 /* Read signal from pr_cursig. */
aa1ed4a9
JB
11015 if (elf_tdata (abfd)->core->signal == 0)
11016 elf_tdata (abfd)->core->signal
11017 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11018 offset += 4;
11019
24d3e51b 11020 /* Read TID from pr_pid. */
aa1ed4a9
JB
11021 elf_tdata (abfd)->core->lwpid
11022 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11023 offset += 4;
11024
24d3e51b 11025 /* Padding before pr_reg. */
b5430a3c 11026 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
11027 offset += 4;
11028
24d3e51b
NC
11029 /* Make sure that there is enough data remaining in the note. */
11030 if ((note->descsz - offset) < size)
0a1b45a2 11031 return false;
24d3e51b 11032
aa1ed4a9
JB
11033 /* Make a ".reg/999" section and a ".reg" section. */
11034 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11035 size, note->descpos + offset);
11036}
11037
0a1b45a2 11038static bool
aa1ed4a9
JB
11039elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11040{
544c67cd
JB
11041 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11042
aa1ed4a9
JB
11043 switch (note->type)
11044 {
11045 case NT_PRSTATUS:
544c67cd
JB
11046 if (bed->elf_backend_grok_freebsd_prstatus)
11047 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
0a1b45a2 11048 return true;
aa1ed4a9
JB
11049 return elfcore_grok_freebsd_prstatus (abfd, note);
11050
11051 case NT_FPREGSET:
11052 return elfcore_grok_prfpreg (abfd, note);
11053
11054 case NT_PRPSINFO:
11055 return elfcore_grok_freebsd_psinfo (abfd, note);
11056
11057 case NT_FREEBSD_THRMISC:
e330d4c0 11058 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
aa1ed4a9 11059
ddb2bbcf
JB
11060 case NT_FREEBSD_PROCSTAT_PROC:
11061 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11062 note);
11063
11064 case NT_FREEBSD_PROCSTAT_FILES:
11065 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11066 note);
11067
11068 case NT_FREEBSD_PROCSTAT_VMMAP:
11069 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11070 note);
11071
3350c5f5 11072 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 11073 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 11074
b5c2367c
JB
11075 case NT_FREEBSD_X86_SEGBASES:
11076 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11077
aa1ed4a9 11078 case NT_X86_XSTATE:
e330d4c0 11079 return elfcore_grok_xstatereg (abfd, note);
aa1ed4a9 11080
e6f3b9c3
JB
11081 case NT_FREEBSD_PTLWPINFO:
11082 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11083 note);
11084
8e6afe40
JB
11085 case NT_ARM_TLS:
11086 return elfcore_grok_aarch_tls (abfd, note);
11087
6d5be5d6
JB
11088 case NT_ARM_VFP:
11089 return elfcore_grok_arm_vfp (abfd, note);
11090
aa1ed4a9 11091 default:
0a1b45a2 11092 return true;
aa1ed4a9
JB
11093 }
11094}
11095
0a1b45a2 11096static bool
217aa764 11097elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
11098{
11099 char *cp;
11100
11101 cp = strchr (note->namedata, '@');
11102 if (cp != NULL)
11103 {
d2b64500 11104 *lwpidp = atoi(cp + 1);
0a1b45a2 11105 return true;
50b2bdb7 11106 }
0a1b45a2 11107 return false;
50b2bdb7
AM
11108}
11109
0a1b45a2 11110static bool
217aa764 11111elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 11112{
80a04378 11113 if (note->descsz <= 0x7c + 31)
0a1b45a2 11114 return false;
80a04378 11115
50b2bdb7 11116 /* Signal number at offset 0x08. */
228e534f 11117 elf_tdata (abfd)->core->signal
50b2bdb7
AM
11118 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11119
11120 /* Process ID at offset 0x50. */
228e534f 11121 elf_tdata (abfd)->core->pid
50b2bdb7
AM
11122 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11123
11124 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 11125 elf_tdata (abfd)->core->command
50b2bdb7
AM
11126 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11127
7720ba9f
MK
11128 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11129 note);
50b2bdb7
AM
11130}
11131
0a1b45a2 11132static bool
217aa764 11133elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
11134{
11135 int lwp;
11136
11137 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 11138 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 11139
58e07198 11140 switch (note->type)
50b2bdb7 11141 {
58e07198 11142 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 11143 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
11144 find this note before any of the others, which is fine,
11145 since the kernel writes this note out first when it
11146 creates a core file. */
50b2bdb7 11147 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
11148 case NT_NETBSDCORE_AUXV:
11149 /* NetBSD-specific Elf Auxiliary Vector data. */
11150 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
11151 case NT_NETBSDCORE_LWPSTATUS:
11152 return elfcore_make_note_pseudosection (abfd,
11153 ".note.netbsdcore.lwpstatus",
11154 note);
58e07198
CZ
11155 default:
11156 break;
50b2bdb7
AM
11157 }
11158
06d949ec 11159 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
11160 defined for NetBSD core files. If the note type is less
11161 than the start of the machine-dependent note types, we don't
11162 understand it. */
47d9a591 11163
b4db1224 11164 if (note->type < NT_NETBSDCORE_FIRSTMACH)
0a1b45a2 11165 return true;
50b2bdb7
AM
11166
11167
11168 switch (bfd_get_arch (abfd))
11169 {
08a40648
AM
11170 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11171 PT_GETFPREGS == mach+2. */
50b2bdb7 11172
015ec493 11173 case bfd_arch_aarch64:
50b2bdb7
AM
11174 case bfd_arch_alpha:
11175 case bfd_arch_sparc:
11176 switch (note->type)
08a40648
AM
11177 {
11178 case NT_NETBSDCORE_FIRSTMACH+0:
11179 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11180
08a40648
AM
11181 case NT_NETBSDCORE_FIRSTMACH+2:
11182 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11183
08a40648 11184 default:
0a1b45a2 11185 return true;
08a40648 11186 }
50b2bdb7 11187
58e07198
CZ
11188 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11189 There's also old PT___GETREGS40 == mach + 1 for old reg
11190 structure which lacks GBR. */
11191
11192 case bfd_arch_sh:
11193 switch (note->type)
11194 {
11195 case NT_NETBSDCORE_FIRSTMACH+3:
11196 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11197
11198 case NT_NETBSDCORE_FIRSTMACH+5:
11199 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11200
11201 default:
0a1b45a2 11202 return true;
58e07198
CZ
11203 }
11204
08a40648
AM
11205 /* On all other arch's, PT_GETREGS == mach+1 and
11206 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
11207
11208 default:
11209 switch (note->type)
08a40648
AM
11210 {
11211 case NT_NETBSDCORE_FIRSTMACH+1:
11212 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11213
08a40648
AM
11214 case NT_NETBSDCORE_FIRSTMACH+3:
11215 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11216
08a40648 11217 default:
0a1b45a2 11218 return true;
08a40648 11219 }
50b2bdb7
AM
11220 }
11221 /* NOTREACHED */
11222}
11223
0a1b45a2 11224static bool
67cc5033
MK
11225elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11226{
80a04378 11227 if (note->descsz <= 0x48 + 31)
0a1b45a2 11228 return false;
80a04378 11229
67cc5033 11230 /* Signal number at offset 0x08. */
228e534f 11231 elf_tdata (abfd)->core->signal
67cc5033
MK
11232 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11233
11234 /* Process ID at offset 0x20. */
228e534f 11235 elf_tdata (abfd)->core->pid
67cc5033
MK
11236 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11237
11238 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 11239 elf_tdata (abfd)->core->command
67cc5033
MK
11240 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11241
0a1b45a2 11242 return true;
67cc5033
MK
11243}
11244
6420dd27
LB
11245/* Processes Solaris's process status note.
11246 sig_off ~ offsetof(prstatus_t, pr_cursig)
11247 pid_off ~ offsetof(prstatus_t, pr_pid)
11248 lwpid_off ~ offsetof(prstatus_t, pr_who)
11249 gregset_size ~ sizeof(gregset_t)
11250 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11251
11252static bool
11253elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11254 int pid_off, int lwpid_off, size_t gregset_size,
11255 size_t gregset_offset)
11256{
11257 asection *sect = NULL;
11258 elf_tdata (abfd)->core->signal
11259 = bfd_get_16 (abfd, note->descdata + sig_off);
11260 elf_tdata (abfd)->core->pid
11261 = bfd_get_32 (abfd, note->descdata + pid_off);
11262 elf_tdata (abfd)->core->lwpid
11263 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11264
11265 sect = bfd_get_section_by_name (abfd, ".reg");
11266 if (sect != NULL)
11267 sect->size = gregset_size;
11268
11269 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11270 note->descpos + gregset_offset);
11271}
11272
11273/* Gets program and arguments from a core.
11274 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11275 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11276
11277static bool
11278elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11279 int prog_off, int comm_off)
11280{
11281 elf_tdata (abfd)->core->program
11282 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11283 elf_tdata (abfd)->core->command
11284 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11285
11286 return true;
11287}
11288
11289/* Processes Solaris's LWP status note.
11290 gregset_size ~ sizeof(gregset_t)
11291 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11292 fpregset_size ~ sizeof(fpregset_t)
11293 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11294
11295static bool
11296elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11297 size_t gregset_size, int gregset_off,
11298 size_t fpregset_size, int fpregset_off)
11299{
11300 asection *sect = NULL;
11301 char reg2_section_name[16] = { 0 };
11302
11303 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11304 elf_tdata (abfd)->core->lwpid);
11305
11306 /* offsetof(lwpstatus_t, pr_lwpid) */
11307 elf_tdata (abfd)->core->lwpid
11308 = bfd_get_32 (abfd, note->descdata + 4);
11309 /* offsetof(lwpstatus_t, pr_cursig) */
11310 elf_tdata (abfd)->core->signal
11311 = bfd_get_16 (abfd, note->descdata + 12);
11312
11313 sect = bfd_get_section_by_name (abfd, ".reg");
11314 if (sect != NULL)
11315 sect->size = gregset_size;
11316 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11317 note->descpos + gregset_off))
11318 return false;
11319
11320 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11321 if (sect != NULL)
11322 {
11323 sect->size = fpregset_size;
11324 sect->filepos = note->descpos + fpregset_off;
11325 sect->alignment_power = 2;
11326 }
11327 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11328 note->descpos + fpregset_off))
11329 return false;
11330
11331 return true;
11332}
11333
11334static bool
11335elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11336{
11337 if (note == NULL)
11338 return false;
11339
11340 /* core files are identified as 32- or 64-bit, SPARC or x86,
11341 by the size of the descsz which matches the sizeof()
11342 the type appropriate for that note type (e.g., prstatus_t for
11343 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11344 on Solaris. The core file bitness may differ from the bitness of
11345 gdb itself, so fixed values are used instead of sizeof().
11346 Appropriate fixed offsets are also used to obtain data from
11347 the note. */
11348
11349 switch ((int) note->type)
11350 {
11351 case SOLARIS_NT_PRSTATUS:
11352 switch (note->descsz)
11353 {
11354 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11355 return elfcore_grok_solaris_prstatus(abfd, note,
11356 136, 216, 308, 152, 356);
11357 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11358 return elfcore_grok_solaris_prstatus(abfd, note,
11359 264, 360, 520, 304, 600);
11360 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11361 return elfcore_grok_solaris_prstatus(abfd, note,
11362 136, 216, 308, 76, 356);
11363 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11364 return elfcore_grok_solaris_prstatus(abfd, note,
11365 264, 360, 520, 224, 600);
11366 default:
11367 return true;
11368 }
11369
11370 case SOLARIS_NT_PSINFO:
11371 case SOLARIS_NT_PRPSINFO:
11372 switch (note->descsz)
11373 {
11374 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11375 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11376 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11377 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11378 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11379 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11380 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11381 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11382 default:
11383 return true;
11384 }
11385
11386 case SOLARIS_NT_LWPSTATUS:
11387 switch (note->descsz)
11388 {
11389 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11390 return elfcore_grok_solaris_lwpstatus(abfd, note,
11391 152, 344, 400, 496);
11392 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11393 return elfcore_grok_solaris_lwpstatus(abfd, note,
11394 304, 544, 544, 848);
11395 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11396 return elfcore_grok_solaris_lwpstatus(abfd, note,
11397 76, 344, 380, 420);
11398 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11399 return elfcore_grok_solaris_lwpstatus(abfd, note,
11400 224, 544, 528, 768);
11401 default:
11402 return true;
11403 }
11404
11405 case SOLARIS_NT_LWPSINFO:
11406 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11407 if (note->descsz == 128 || note->descsz == 152)
11408 elf_tdata (abfd)->core->lwpid =
11409 bfd_get_32 (abfd, note->descdata + 4);
11410 break;
11411
11412 default:
11413 break;
11414 }
11415
11416 return true;
11417}
11418
11419/* For name starting with "CORE" this may be either a Solaris
11420 core file or a gdb-generated core file. Do Solaris-specific
11421 processing on selected note types first with
11422 elfcore_grok_solaris_note(), then process the note
11423 in elfcore_grok_note(). */
11424
11425static bool
11426elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11427{
11428 if (!elfcore_grok_solaris_note_impl (abfd, note))
11429 return false;
11430
11431 return elfcore_grok_note (abfd, note);
11432}
11433
0a1b45a2 11434static bool
67cc5033
MK
11435elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11436{
11437 if (note->type == NT_OPENBSD_PROCINFO)
11438 return elfcore_grok_openbsd_procinfo (abfd, note);
11439
11440 if (note->type == NT_OPENBSD_REGS)
11441 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11442
11443 if (note->type == NT_OPENBSD_FPREGS)
11444 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11445
11446 if (note->type == NT_OPENBSD_XFPREGS)
11447 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11448
11449 if (note->type == NT_OPENBSD_AUXV)
58e07198 11450 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11451
11452 if (note->type == NT_OPENBSD_WCOOKIE)
11453 {
11454 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11455 SEC_HAS_CONTENTS);
11456
11457 if (sect == NULL)
0a1b45a2 11458 return false;
67cc5033
MK
11459 sect->size = note->descsz;
11460 sect->filepos = note->descpos;
11461 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11462
0a1b45a2 11463 return true;
67cc5033
MK
11464 }
11465
0a1b45a2 11466 return true;
67cc5033
MK
11467}
11468
0a1b45a2 11469static bool
d3fd4074 11470elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11471{
11472 void *ddata = note->descdata;
11473 char buf[100];
11474 char *name;
11475 asection *sect;
f8843e87
AM
11476 short sig;
11477 unsigned flags;
07c6e936 11478
80a04378 11479 if (note->descsz < 16)
0a1b45a2 11480 return false;
80a04378 11481
07c6e936 11482 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11483 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11484
f8843e87
AM
11485 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11486 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11487
11488 /* nto_procfs_status 'flags' field is at offset 8. */
11489 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11490
11491 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11492 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11493 {
228e534f
AM
11494 elf_tdata (abfd)->core->signal = sig;
11495 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11496 }
07c6e936 11497
f8843e87
AM
11498 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11499 do not come from signals so we make sure we set the current
11500 thread just in case. */
11501 if (flags & 0x00000080)
228e534f 11502 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11503
11504 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11505 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11506
a50b1753 11507 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11508 if (name == NULL)
0a1b45a2 11509 return false;
07c6e936
NC
11510 strcpy (name, buf);
11511
117ed4f8 11512 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11513 if (sect == NULL)
0a1b45a2 11514 return false;
07c6e936 11515
07d6d2b8
AM
11516 sect->size = note->descsz;
11517 sect->filepos = note->descpos;
07c6e936
NC
11518 sect->alignment_power = 2;
11519
11520 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11521}
11522
0a1b45a2 11523static bool
d69f560c
KW
11524elfcore_grok_nto_regs (bfd *abfd,
11525 Elf_Internal_Note *note,
d3fd4074 11526 long tid,
d69f560c 11527 char *base)
07c6e936
NC
11528{
11529 char buf[100];
11530 char *name;
11531 asection *sect;
11532
d69f560c 11533 /* Make a "(base)/%d" section. */
d3fd4074 11534 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11535
a50b1753 11536 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11537 if (name == NULL)
0a1b45a2 11538 return false;
07c6e936
NC
11539 strcpy (name, buf);
11540
117ed4f8 11541 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11542 if (sect == NULL)
0a1b45a2 11543 return false;
07c6e936 11544
07d6d2b8
AM
11545 sect->size = note->descsz;
11546 sect->filepos = note->descpos;
07c6e936
NC
11547 sect->alignment_power = 2;
11548
f8843e87 11549 /* This is the current thread. */
228e534f 11550 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11551 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87 11552
0a1b45a2 11553 return true;
07c6e936
NC
11554}
11555
0a1b45a2 11556static bool
217aa764 11557elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11558{
11559 /* Every GREG section has a STATUS section before it. Store the
811072d8 11560 tid from the previous call to pass down to the next gregs
07c6e936 11561 function. */
d3fd4074 11562 static long tid = 1;
07c6e936
NC
11563
11564 switch (note->type)
11565 {
e263a66b 11566 case QNT_CORE_INFO:
d69f560c 11567 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
e263a66b 11568 case QNT_CORE_STATUS:
d69f560c 11569 return elfcore_grok_nto_status (abfd, note, &tid);
e263a66b 11570 case QNT_CORE_GREG:
d69f560c 11571 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
e263a66b 11572 case QNT_CORE_FPREG:
d69f560c
KW
11573 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11574 default:
0a1b45a2 11575 return true;
07c6e936
NC
11576 }
11577}
11578
0a1b45a2 11579static bool
b15fa79e
AM
11580elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11581{
11582 char *name;
11583 asection *sect;
11584 size_t len;
11585
11586 /* Use note name as section name. */
11587 len = note->namesz;
a50b1753 11588 name = (char *) bfd_alloc (abfd, len);
b15fa79e 11589 if (name == NULL)
0a1b45a2 11590 return false;
b15fa79e
AM
11591 memcpy (name, note->namedata, len);
11592 name[len - 1] = '\0';
11593
11594 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11595 if (sect == NULL)
0a1b45a2 11596 return false;
b15fa79e 11597
07d6d2b8
AM
11598 sect->size = note->descsz;
11599 sect->filepos = note->descpos;
b15fa79e
AM
11600 sect->alignment_power = 1;
11601
0a1b45a2 11602 return true;
b15fa79e
AM
11603}
11604
7c76fa91
MS
11605/* Function: elfcore_write_note
11606
47d9a591 11607 Inputs:
a39f3346 11608 buffer to hold note, and current size of buffer
7c76fa91
MS
11609 name of note
11610 type of note
11611 data for note
11612 size of data for note
11613
a39f3346
AM
11614 Writes note to end of buffer. ELF64 notes are written exactly as
11615 for ELF32, despite the current (as of 2006) ELF gabi specifying
11616 that they ought to have 8-byte namesz and descsz field, and have
11617 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11618
7c76fa91 11619 Return:
a39f3346 11620 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11621
11622char *
a39f3346 11623elfcore_write_note (bfd *abfd,
217aa764 11624 char *buf,
a39f3346 11625 int *bufsiz,
217aa764 11626 const char *name,
a39f3346 11627 int type,
217aa764 11628 const void *input,
a39f3346 11629 int size)
7c76fa91
MS
11630{
11631 Elf_External_Note *xnp;
d4c88bbb 11632 size_t namesz;
d4c88bbb 11633 size_t newspace;
a39f3346 11634 char *dest;
7c76fa91 11635
d4c88bbb 11636 namesz = 0;
d4c88bbb 11637 if (name != NULL)
a39f3346 11638 namesz = strlen (name) + 1;
d4c88bbb 11639
a39f3346 11640 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11641
a50b1753 11642 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11643 if (buf == NULL)
11644 return buf;
a39f3346 11645 dest = buf + *bufsiz;
7c76fa91
MS
11646 *bufsiz += newspace;
11647 xnp = (Elf_External_Note *) dest;
11648 H_PUT_32 (abfd, namesz, xnp->namesz);
11649 H_PUT_32 (abfd, size, xnp->descsz);
11650 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11651 dest = xnp->name;
11652 if (name != NULL)
11653 {
11654 memcpy (dest, name, namesz);
11655 dest += namesz;
a39f3346 11656 while (namesz & 3)
d4c88bbb
AM
11657 {
11658 *dest++ = '\0';
a39f3346 11659 ++namesz;
d4c88bbb
AM
11660 }
11661 }
11662 memcpy (dest, input, size);
a39f3346
AM
11663 dest += size;
11664 while (size & 3)
11665 {
11666 *dest++ = '\0';
11667 ++size;
11668 }
11669 return buf;
7c76fa91
MS
11670}
11671
602f1657
AM
11672/* gcc-8 warns (*) on all the strncpy calls in this function about
11673 possible string truncation. The "truncation" is not a bug. We
11674 have an external representation of structs with fields that are not
11675 necessarily NULL terminated and corresponding internal
11676 representation fields that are one larger so that they can always
11677 be NULL terminated.
11678 gcc versions between 4.2 and 4.6 do not allow pragma control of
11679 diagnostics inside functions, giving a hard error if you try to use
11680 the finer control available with later versions.
11681 gcc prior to 4.2 warns about diagnostic push and pop.
11682 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11683 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11684 (*) Depending on your system header files! */
d99b4b92 11685#if GCC_VERSION >= 8000
602f1657
AM
11686# pragma GCC diagnostic push
11687# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11688#endif
7c76fa91 11689char *
217aa764
AM
11690elfcore_write_prpsinfo (bfd *abfd,
11691 char *buf,
11692 int *bufsiz,
11693 const char *fname,
11694 const char *psargs)
7c76fa91 11695{
183e98be
AM
11696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11697
11698 if (bed->elf_backend_write_core_note != NULL)
11699 {
11700 char *ret;
11701 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11702 NT_PRPSINFO, fname, psargs);
11703 if (ret != NULL)
11704 return ret;
11705 }
7c76fa91 11706
1f20dca5 11707#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11708# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11709 if (bed->s->elfclass == ELFCLASS32)
11710 {
602f1657 11711# if defined (HAVE_PSINFO32_T)
183e98be
AM
11712 psinfo32_t data;
11713 int note_type = NT_PSINFO;
602f1657 11714# else
183e98be
AM
11715 prpsinfo32_t data;
11716 int note_type = NT_PRPSINFO;
602f1657 11717# endif
183e98be
AM
11718
11719 memset (&data, 0, sizeof (data));
11720 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11721 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11722 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11723 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11724 }
11725 else
602f1657 11726# endif
183e98be 11727 {
602f1657 11728# if defined (HAVE_PSINFO_T)
183e98be
AM
11729 psinfo_t data;
11730 int note_type = NT_PSINFO;
602f1657 11731# else
183e98be
AM
11732 prpsinfo_t data;
11733 int note_type = NT_PRPSINFO;
602f1657 11734# endif
7c76fa91 11735
183e98be
AM
11736 memset (&data, 0, sizeof (data));
11737 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11738 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11739 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11740 "CORE", note_type, &data, sizeof (data));
183e98be 11741 }
7c76fa91
MS
11742#endif /* PSINFO_T or PRPSINFO_T */
11743
1f20dca5
UW
11744 free (buf);
11745 return NULL;
11746}
d99b4b92 11747#if GCC_VERSION >= 8000
602f1657 11748# pragma GCC diagnostic pop
d99b4b92 11749#endif
1f20dca5 11750
70a38d42
SDJ
11751char *
11752elfcore_write_linux_prpsinfo32
11753 (bfd *abfd, char *buf, int *bufsiz,
11754 const struct elf_internal_linux_prpsinfo *prpsinfo)
11755{
a2f63b2e
MR
11756 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11757 {
11758 struct elf_external_linux_prpsinfo32_ugid16 data;
11759
11760 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11761 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11762 &data, sizeof (data));
11763 }
11764 else
11765 {
11766 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11767
a2f63b2e
MR
11768 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11769 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11770 &data, sizeof (data));
11771 }
70a38d42
SDJ
11772}
11773
11774char *
11775elfcore_write_linux_prpsinfo64
11776 (bfd *abfd, char *buf, int *bufsiz,
11777 const struct elf_internal_linux_prpsinfo *prpsinfo)
11778{
3c9a7b0d
MR
11779 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11780 {
11781 struct elf_external_linux_prpsinfo64_ugid16 data;
11782
11783 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11784 return elfcore_write_note (abfd, buf, bufsiz,
11785 "CORE", NT_PRPSINFO, &data, sizeof (data));
11786 }
11787 else
11788 {
11789 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11790
3c9a7b0d
MR
11791 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11792 return elfcore_write_note (abfd, buf, bufsiz,
11793 "CORE", NT_PRPSINFO, &data, sizeof (data));
11794 }
70a38d42
SDJ
11795}
11796
7c76fa91 11797char *
217aa764
AM
11798elfcore_write_prstatus (bfd *abfd,
11799 char *buf,
11800 int *bufsiz,
11801 long pid,
11802 int cursig,
11803 const void *gregs)
7c76fa91 11804{
183e98be 11805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11806
183e98be
AM
11807 if (bed->elf_backend_write_core_note != NULL)
11808 {
11809 char *ret;
11810 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11811 NT_PRSTATUS,
11812 pid, cursig, gregs);
11813 if (ret != NULL)
11814 return ret;
11815 }
11816
1f20dca5 11817#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11818#if defined (HAVE_PRSTATUS32_T)
11819 if (bed->s->elfclass == ELFCLASS32)
11820 {
11821 prstatus32_t prstat;
11822
11823 memset (&prstat, 0, sizeof (prstat));
11824 prstat.pr_pid = pid;
11825 prstat.pr_cursig = cursig;
11826 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11827 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11828 NT_PRSTATUS, &prstat, sizeof (prstat));
11829 }
11830 else
11831#endif
11832 {
11833 prstatus_t prstat;
11834
11835 memset (&prstat, 0, sizeof (prstat));
11836 prstat.pr_pid = pid;
11837 prstat.pr_cursig = cursig;
11838 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11839 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11840 NT_PRSTATUS, &prstat, sizeof (prstat));
11841 }
7c76fa91
MS
11842#endif /* HAVE_PRSTATUS_T */
11843
1f20dca5
UW
11844 free (buf);
11845 return NULL;
11846}
11847
51316059
MS
11848#if defined (HAVE_LWPSTATUS_T)
11849char *
217aa764
AM
11850elfcore_write_lwpstatus (bfd *abfd,
11851 char *buf,
11852 int *bufsiz,
11853 long pid,
11854 int cursig,
11855 const void *gregs)
51316059
MS
11856{
11857 lwpstatus_t lwpstat;
183e98be 11858 const char *note_name = "CORE";
51316059
MS
11859
11860 memset (&lwpstat, 0, sizeof (lwpstat));
11861 lwpstat.pr_lwpid = pid >> 16;
11862 lwpstat.pr_cursig = cursig;
11863#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11864 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11865#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11866#if !defined(gregs)
11867 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11868 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11869#else
11870 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11871 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11872#endif
11873#endif
47d9a591 11874 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11875 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11876}
11877#endif /* HAVE_LWPSTATUS_T */
11878
7c76fa91
MS
11879#if defined (HAVE_PSTATUS_T)
11880char *
217aa764
AM
11881elfcore_write_pstatus (bfd *abfd,
11882 char *buf,
11883 int *bufsiz,
11884 long pid,
6c10990d
NC
11885 int cursig ATTRIBUTE_UNUSED,
11886 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11887{
183e98be
AM
11888 const char *note_name = "CORE";
11889#if defined (HAVE_PSTATUS32_T)
11890 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11891
183e98be
AM
11892 if (bed->s->elfclass == ELFCLASS32)
11893 {
11894 pstatus32_t pstat;
11895
11896 memset (&pstat, 0, sizeof (pstat));
11897 pstat.pr_pid = pid & 0xffff;
11898 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11899 NT_PSTATUS, &pstat, sizeof (pstat));
11900 return buf;
11901 }
11902 else
11903#endif
11904 {
11905 pstatus_t pstat;
11906
11907 memset (&pstat, 0, sizeof (pstat));
11908 pstat.pr_pid = pid & 0xffff;
11909 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11910 NT_PSTATUS, &pstat, sizeof (pstat));
11911 return buf;
11912 }
7c76fa91
MS
11913}
11914#endif /* HAVE_PSTATUS_T */
11915
11916char *
217aa764
AM
11917elfcore_write_prfpreg (bfd *abfd,
11918 char *buf,
11919 int *bufsiz,
11920 const void *fpregs,
11921 int size)
7c76fa91 11922{
183e98be 11923 const char *note_name = "CORE";
47d9a591 11924 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11925 note_name, NT_FPREGSET, fpregs, size);
11926}
11927
11928char *
217aa764
AM
11929elfcore_write_prxfpreg (bfd *abfd,
11930 char *buf,
11931 int *bufsiz,
11932 const void *xfpregs,
11933 int size)
7c76fa91
MS
11934{
11935 char *note_name = "LINUX";
47d9a591 11936 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11937 note_name, NT_PRXFPREG, xfpregs, size);
11938}
11939
4339cae0
L
11940char *
11941elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11942 const void *xfpregs, int size)
11943{
97de3545
JB
11944 char *note_name;
11945 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11946 note_name = "FreeBSD";
11947 else
11948 note_name = "LINUX";
4339cae0
L
11949 return elfcore_write_note (abfd, buf, bufsiz,
11950 note_name, NT_X86_XSTATE, xfpregs, size);
11951}
11952
b5c2367c
JB
11953char *
11954elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11955 const void *regs, int size)
11956{
11957 char *note_name = "FreeBSD";
11958 return elfcore_write_note (abfd, buf, bufsiz,
11959 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11960}
11961
97753bd5
AM
11962char *
11963elfcore_write_ppc_vmx (bfd *abfd,
11964 char *buf,
11965 int *bufsiz,
11966 const void *ppc_vmx,
11967 int size)
11968{
11969 char *note_name = "LINUX";
11970 return elfcore_write_note (abfd, buf, bufsiz,
11971 note_name, NT_PPC_VMX, ppc_vmx, size);
11972}
11973
89eeb0bc
LM
11974char *
11975elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11976 char *buf,
11977 int *bufsiz,
11978 const void *ppc_vsx,
11979 int size)
89eeb0bc
LM
11980{
11981 char *note_name = "LINUX";
11982 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11983 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11984}
11985
cb2366c1
EBM
11986char *
11987elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11988 char *buf,
11989 int *bufsiz,
11990 const void *ppc_tar,
11991 int size)
cb2366c1
EBM
11992{
11993 char *note_name = "LINUX";
11994 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11995 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11996}
11997
11998char *
11999elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
12000 char *buf,
12001 int *bufsiz,
12002 const void *ppc_ppr,
12003 int size)
cb2366c1
EBM
12004{
12005 char *note_name = "LINUX";
12006 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12007 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
12008}
12009
12010char *
12011elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
12012 char *buf,
12013 int *bufsiz,
12014 const void *ppc_dscr,
12015 int size)
cb2366c1
EBM
12016{
12017 char *note_name = "LINUX";
12018 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12019 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
12020}
12021
12022char *
12023elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
12024 char *buf,
12025 int *bufsiz,
12026 const void *ppc_ebb,
12027 int size)
cb2366c1
EBM
12028{
12029 char *note_name = "LINUX";
12030 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12031 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
12032}
12033
12034char *
12035elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
12036 char *buf,
12037 int *bufsiz,
12038 const void *ppc_pmu,
12039 int size)
cb2366c1
EBM
12040{
12041 char *note_name = "LINUX";
12042 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12043 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
12044}
12045
12046char *
12047elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
12048 char *buf,
12049 int *bufsiz,
12050 const void *ppc_tm_cgpr,
12051 int size)
cb2366c1
EBM
12052{
12053 char *note_name = "LINUX";
12054 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12055 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
12056}
12057
12058char *
12059elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
12060 char *buf,
12061 int *bufsiz,
12062 const void *ppc_tm_cfpr,
12063 int size)
cb2366c1
EBM
12064{
12065 char *note_name = "LINUX";
12066 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12067 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
12068}
12069
12070char *
12071elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
12072 char *buf,
12073 int *bufsiz,
12074 const void *ppc_tm_cvmx,
12075 int size)
cb2366c1
EBM
12076{
12077 char *note_name = "LINUX";
12078 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12079 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
12080}
12081
12082char *
12083elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
12084 char *buf,
12085 int *bufsiz,
12086 const void *ppc_tm_cvsx,
12087 int size)
cb2366c1
EBM
12088{
12089 char *note_name = "LINUX";
12090 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12091 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
12092}
12093
12094char *
12095elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
12096 char *buf,
12097 int *bufsiz,
12098 const void *ppc_tm_spr,
12099 int size)
cb2366c1
EBM
12100{
12101 char *note_name = "LINUX";
12102 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12103 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
12104}
12105
12106char *
12107elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
12108 char *buf,
12109 int *bufsiz,
12110 const void *ppc_tm_ctar,
12111 int size)
cb2366c1
EBM
12112{
12113 char *note_name = "LINUX";
12114 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12115 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
12116}
12117
12118char *
12119elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
12120 char *buf,
12121 int *bufsiz,
12122 const void *ppc_tm_cppr,
12123 int size)
cb2366c1
EBM
12124{
12125 char *note_name = "LINUX";
12126 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12127 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
12128}
12129
12130char *
12131elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
12132 char *buf,
12133 int *bufsiz,
12134 const void *ppc_tm_cdscr,
12135 int size)
cb2366c1
EBM
12136{
12137 char *note_name = "LINUX";
12138 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12139 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
12140}
12141
0675e188
UW
12142static char *
12143elfcore_write_s390_high_gprs (bfd *abfd,
12144 char *buf,
12145 int *bufsiz,
12146 const void *s390_high_gprs,
12147 int size)
12148{
12149 char *note_name = "LINUX";
12150 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12151 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
12152 s390_high_gprs, size);
12153}
12154
d7eeb400
MS
12155char *
12156elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
12157 char *buf,
12158 int *bufsiz,
12159 const void *s390_timer,
12160 int size)
d7eeb400
MS
12161{
12162 char *note_name = "LINUX";
12163 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12164 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
12165}
12166
12167char *
12168elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
12169 char *buf,
12170 int *bufsiz,
12171 const void *s390_todcmp,
12172 int size)
d7eeb400
MS
12173{
12174 char *note_name = "LINUX";
12175 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12176 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
12177}
12178
12179char *
12180elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
12181 char *buf,
12182 int *bufsiz,
12183 const void *s390_todpreg,
12184 int size)
d7eeb400
MS
12185{
12186 char *note_name = "LINUX";
12187 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12188 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
12189}
12190
12191char *
12192elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
12193 char *buf,
12194 int *bufsiz,
12195 const void *s390_ctrs,
12196 int size)
d7eeb400
MS
12197{
12198 char *note_name = "LINUX";
12199 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12200 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
12201}
12202
12203char *
12204elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
12205 char *buf,
12206 int *bufsiz,
12207 const void *s390_prefix,
12208 int size)
d7eeb400
MS
12209{
12210 char *note_name = "LINUX";
12211 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12212 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
12213}
12214
355b81d9
UW
12215char *
12216elfcore_write_s390_last_break (bfd *abfd,
12217 char *buf,
12218 int *bufsiz,
12219 const void *s390_last_break,
12220 int size)
12221{
12222 char *note_name = "LINUX";
12223 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12224 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
12225 s390_last_break, size);
12226}
12227
12228char *
12229elfcore_write_s390_system_call (bfd *abfd,
12230 char *buf,
12231 int *bufsiz,
12232 const void *s390_system_call,
12233 int size)
12234{
12235 char *note_name = "LINUX";
12236 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12237 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
12238 s390_system_call, size);
12239}
12240
abb3f6cc
NC
12241char *
12242elfcore_write_s390_tdb (bfd *abfd,
12243 char *buf,
12244 int *bufsiz,
12245 const void *s390_tdb,
12246 int size)
12247{
12248 char *note_name = "LINUX";
12249 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12250 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
12251}
12252
4ef9f41a
AA
12253char *
12254elfcore_write_s390_vxrs_low (bfd *abfd,
12255 char *buf,
12256 int *bufsiz,
12257 const void *s390_vxrs_low,
12258 int size)
12259{
12260 char *note_name = "LINUX";
12261 return elfcore_write_note (abfd, buf, bufsiz,
12262 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12263}
12264
12265char *
12266elfcore_write_s390_vxrs_high (bfd *abfd,
12267 char *buf,
12268 int *bufsiz,
12269 const void *s390_vxrs_high,
12270 int size)
12271{
12272 char *note_name = "LINUX";
12273 return elfcore_write_note (abfd, buf, bufsiz,
12274 note_name, NT_S390_VXRS_HIGH,
12275 s390_vxrs_high, size);
12276}
12277
88ab90e8
AA
12278char *
12279elfcore_write_s390_gs_cb (bfd *abfd,
12280 char *buf,
12281 int *bufsiz,
12282 const void *s390_gs_cb,
12283 int size)
12284{
12285 char *note_name = "LINUX";
12286 return elfcore_write_note (abfd, buf, bufsiz,
12287 note_name, NT_S390_GS_CB,
12288 s390_gs_cb, size);
12289}
12290
12291char *
12292elfcore_write_s390_gs_bc (bfd *abfd,
12293 char *buf,
12294 int *bufsiz,
12295 const void *s390_gs_bc,
12296 int size)
12297{
12298 char *note_name = "LINUX";
12299 return elfcore_write_note (abfd, buf, bufsiz,
12300 note_name, NT_S390_GS_BC,
12301 s390_gs_bc, size);
12302}
12303
faa9a424
UW
12304char *
12305elfcore_write_arm_vfp (bfd *abfd,
12306 char *buf,
12307 int *bufsiz,
12308 const void *arm_vfp,
12309 int size)
12310{
12311 char *note_name = "LINUX";
12312 return elfcore_write_note (abfd, buf, bufsiz,
12313 note_name, NT_ARM_VFP, arm_vfp, size);
12314}
12315
652451f8
YZ
12316char *
12317elfcore_write_aarch_tls (bfd *abfd,
12318 char *buf,
12319 int *bufsiz,
12320 const void *aarch_tls,
12321 int size)
12322{
12323 char *note_name = "LINUX";
12324 return elfcore_write_note (abfd, buf, bufsiz,
12325 note_name, NT_ARM_TLS, aarch_tls, size);
12326}
12327
12328char *
12329elfcore_write_aarch_hw_break (bfd *abfd,
12330 char *buf,
12331 int *bufsiz,
12332 const void *aarch_hw_break,
12333 int size)
12334{
12335 char *note_name = "LINUX";
12336 return elfcore_write_note (abfd, buf, bufsiz,
12337 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12338}
12339
12340char *
12341elfcore_write_aarch_hw_watch (bfd *abfd,
12342 char *buf,
12343 int *bufsiz,
12344 const void *aarch_hw_watch,
12345 int size)
12346{
12347 char *note_name = "LINUX";
12348 return elfcore_write_note (abfd, buf, bufsiz,
12349 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12350}
12351
ad1cc4e4
AH
12352char *
12353elfcore_write_aarch_sve (bfd *abfd,
12354 char *buf,
12355 int *bufsiz,
12356 const void *aarch_sve,
12357 int size)
12358{
12359 char *note_name = "LINUX";
12360 return elfcore_write_note (abfd, buf, bufsiz,
12361 note_name, NT_ARM_SVE, aarch_sve, size);
12362}
12363
e6c3b5bf
AH
12364char *
12365elfcore_write_aarch_pauth (bfd *abfd,
12366 char *buf,
12367 int *bufsiz,
12368 const void *aarch_pauth,
12369 int size)
12370{
12371 char *note_name = "LINUX";
12372 return elfcore_write_note (abfd, buf, bufsiz,
12373 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12374}
12375
f0bbe8ba
LM
12376char *
12377elfcore_write_aarch_mte (bfd *abfd,
12378 char *buf,
12379 int *bufsiz,
12380 const void *aarch_mte,
12381 int size)
12382{
12383 char *note_name = "LINUX";
12384 return elfcore_write_note (abfd, buf, bufsiz,
12385 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12386 aarch_mte,
12387 size);
12388}
12389
27456742
AK
12390char *
12391elfcore_write_arc_v2 (bfd *abfd,
12392 char *buf,
12393 int *bufsiz,
12394 const void *arc_v2,
12395 int size)
12396{
12397 char *note_name = "LINUX";
12398 return elfcore_write_note (abfd, buf, bufsiz,
12399 note_name, NT_ARC_V2, arc_v2, size);
12400}
12401
e214f8db 12402char *
12403elfcore_write_loongarch_cpucfg (bfd *abfd,
12404 char *buf,
12405 int *bufsiz,
12406 const void *loongarch_cpucfg,
12407 int size)
12408{
12409 char *note_name = "LINUX";
12410 return elfcore_write_note (abfd, buf, bufsiz,
12411 note_name, NT_LARCH_CPUCFG,
12412 loongarch_cpucfg, size);
12413}
12414
12415char *
12416elfcore_write_loongarch_lbt (bfd *abfd,
12417 char *buf,
12418 int *bufsiz,
12419 const void *loongarch_lbt,
12420 int size)
12421{
12422 char *note_name = "LINUX";
12423 return elfcore_write_note (abfd, buf, bufsiz,
12424 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12425}
12426
12427char *
12428elfcore_write_loongarch_lsx (bfd *abfd,
12429 char *buf,
12430 int *bufsiz,
12431 const void *loongarch_lsx,
12432 int size)
12433{
12434 char *note_name = "LINUX";
12435 return elfcore_write_note (abfd, buf, bufsiz,
12436 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12437}
12438
12439char *
12440elfcore_write_loongarch_lasx (bfd *abfd,
12441 char *buf,
12442 int *bufsiz,
12443 const void *loongarch_lasx,
12444 int size)
12445{
12446 char *note_name = "LINUX";
12447 return elfcore_write_note (abfd, buf, bufsiz,
12448 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12449}
12450
db6092f3
AB
12451/* Write the buffer of csr values in CSRS (length SIZE) into the note
12452 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12453 written into. Return a pointer to the new start of the note buffer, to
12454 replace BUF which may no longer be valid. */
12455
12456char *
12457elfcore_write_riscv_csr (bfd *abfd,
11c6a7c6
AM
12458 char *buf,
12459 int *bufsiz,
12460 const void *csrs,
12461 int size)
db6092f3
AB
12462{
12463 const char *note_name = "GDB";
12464 return elfcore_write_note (abfd, buf, bufsiz,
12465 note_name, NT_RISCV_CSR, csrs, size);
12466}
12467
b63a5e38
AB
12468/* Write the target description (a string) pointed to by TDESC, length
12469 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12470 note is being written into. Return a pointer to the new start of the
12471 note buffer, to replace BUF which may no longer be valid. */
12472
12473char *
12474elfcore_write_gdb_tdesc (bfd *abfd,
12475 char *buf,
12476 int *bufsiz,
12477 const void *tdesc,
12478 int size)
12479{
12480 const char *note_name = "GDB";
12481 return elfcore_write_note (abfd, buf, bufsiz,
11c6a7c6 12482 note_name, NT_GDB_TDESC, tdesc, size);
b63a5e38
AB
12483}
12484
bb864ac1
CES
12485char *
12486elfcore_write_register_note (bfd *abfd,
12487 char *buf,
12488 int *bufsiz,
12489 const char *section,
12490 const void *data,
12491 int size)
12492{
12493 if (strcmp (section, ".reg2") == 0)
12494 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12495 if (strcmp (section, ".reg-xfp") == 0)
12496 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
12497 if (strcmp (section, ".reg-xstate") == 0)
12498 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
b5c2367c
JB
12499 if (strcmp (section, ".reg-x86-segbases") == 0)
12500 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12501 if (strcmp (section, ".reg-ppc-vmx") == 0)
12502 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
12503 if (strcmp (section, ".reg-ppc-vsx") == 0)
12504 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
12505 if (strcmp (section, ".reg-ppc-tar") == 0)
12506 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12507 if (strcmp (section, ".reg-ppc-ppr") == 0)
12508 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12509 if (strcmp (section, ".reg-ppc-dscr") == 0)
12510 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12511 if (strcmp (section, ".reg-ppc-ebb") == 0)
12512 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12513 if (strcmp (section, ".reg-ppc-pmu") == 0)
12514 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12515 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12516 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12517 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12518 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12519 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12520 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12521 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12522 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12523 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12524 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12525 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12526 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12527 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12528 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12529 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12530 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
12531 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12532 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12533 if (strcmp (section, ".reg-s390-timer") == 0)
12534 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12535 if (strcmp (section, ".reg-s390-todcmp") == 0)
12536 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12537 if (strcmp (section, ".reg-s390-todpreg") == 0)
12538 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12539 if (strcmp (section, ".reg-s390-ctrs") == 0)
12540 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12541 if (strcmp (section, ".reg-s390-prefix") == 0)
12542 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12543 if (strcmp (section, ".reg-s390-last-break") == 0)
12544 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12545 if (strcmp (section, ".reg-s390-system-call") == 0)
12546 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12547 if (strcmp (section, ".reg-s390-tdb") == 0)
12548 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12549 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12550 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12551 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12552 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12553 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12554 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12555 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12556 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12557 if (strcmp (section, ".reg-arm-vfp") == 0)
12558 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12559 if (strcmp (section, ".reg-aarch-tls") == 0)
12560 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12561 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12562 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12563 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12564 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12565 if (strcmp (section, ".reg-aarch-sve") == 0)
12566 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12567 if (strcmp (section, ".reg-aarch-pauth") == 0)
12568 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
f0bbe8ba
LM
12569 if (strcmp (section, ".reg-aarch-mte") == 0)
12570 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
27456742
AK
12571 if (strcmp (section, ".reg-arc-v2") == 0)
12572 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
b63a5e38
AB
12573 if (strcmp (section, ".gdb-tdesc") == 0)
12574 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
db6092f3
AB
12575 if (strcmp (section, ".reg-riscv-csr") == 0)
12576 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
e214f8db 12577 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12578 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12579 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12580 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12581 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12582 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12583 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12584 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12585 return NULL;
12586}
12587
4cb1265b
MS
12588char *
12589elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12590 const void *buf, int bufsiz)
12591{
12592 return elfcore_write_note (obfd, note_data, note_size,
12593 "CORE", NT_FILE, buf, bufsiz);
12594}
12595
0a1b45a2 12596static bool
276da9b3
L
12597elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12598 size_t align)
252b5132 12599{
c044fabd 12600 char *p;
252b5132 12601
276da9b3
L
12602 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12603 gABI specifies that PT_NOTE alignment should be aligned to 4
12604 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12605 align is less than 4, we use 4 byte alignment. */
12606 if (align < 4)
12607 align = 4;
ef135d43 12608 if (align != 4 && align != 8)
0a1b45a2 12609 return false;
276da9b3 12610
252b5132
RH
12611 p = buf;
12612 while (p < buf + size)
12613 {
c044fabd 12614 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12615 Elf_Internal_Note in;
12616
baea7ef1 12617 if (offsetof (Elf_External_Note, name) > buf - p + size)
0a1b45a2 12618 return false;
baea7ef1 12619
dc810e39 12620 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12621
dc810e39 12622 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12623 in.namedata = xnp->name;
baea7ef1 12624 if (in.namesz > buf - in.namedata + size)
0a1b45a2 12625 return false;
252b5132 12626
dc810e39 12627 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12628 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12629 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12630 if (in.descsz != 0
12631 && (in.descdata >= buf + size
12632 || in.descsz > buf - in.descdata + size))
0a1b45a2 12633 return false;
252b5132 12634
718175fa 12635 switch (bfd_get_format (abfd))
07d6d2b8 12636 {
718175fa 12637 default:
0a1b45a2 12638 return true;
718175fa
JK
12639
12640 case bfd_core:
f64e188b 12641 {
8acbedd6 12642#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12643 struct
718175fa 12644 {
f64e188b 12645 const char * string;
8acbedd6 12646 size_t len;
0a1b45a2 12647 bool (*func) (bfd *, Elf_Internal_Note *);
718175fa 12648 }
f64e188b 12649 grokers[] =
b15fa79e 12650 {
8acbedd6 12651 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12652 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6 12653 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
6420dd27 12654 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
8acbedd6 12655 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb 12656 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
6420dd27
LB
12657 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12658 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
f64e188b 12659 };
8acbedd6 12660#undef GROKER_ELEMENT
f64e188b
NC
12661 int i;
12662
12663 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12664 {
12665 if (in.namesz >= grokers[i].len
12666 && strncmp (in.namedata, grokers[i].string,
12667 grokers[i].len) == 0)
12668 {
12669 if (! grokers[i].func (abfd, & in))
0a1b45a2 12670 return false;
8acbedd6
KS
12671 break;
12672 }
12673 }
f64e188b
NC
12674 break;
12675 }
718175fa
JK
12676
12677 case bfd_object:
12678 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12679 {
12680 if (! elfobj_grok_gnu_note (abfd, &in))
0a1b45a2 12681 return false;
718175fa 12682 }
e21e5835
NC
12683 else if (in.namesz == sizeof "stapsdt"
12684 && strcmp (in.namedata, "stapsdt") == 0)
12685 {
12686 if (! elfobj_grok_stapsdt_note (abfd, &in))
0a1b45a2 12687 return false;
e21e5835 12688 }
718175fa 12689 break;
08a40648 12690 }
252b5132 12691
276da9b3 12692 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12693 }
12694
0a1b45a2 12695 return true;
718175fa
JK
12696}
12697
0a1b45a2 12698bool
276da9b3
L
12699elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12700 size_t align)
718175fa
JK
12701{
12702 char *buf;
12703
957e1fc1 12704 if (size == 0 || (size + 1) == 0)
0a1b45a2 12705 return true;
718175fa
JK
12706
12707 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
0a1b45a2 12708 return false;
718175fa 12709
2bb3687b 12710 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa 12711 if (buf == NULL)
0a1b45a2 12712 return false;
718175fa 12713
f64e188b
NC
12714 /* PR 17512: file: ec08f814
12715 0-termintate the buffer so that string searches will not overflow. */
12716 buf[size] = 0;
12717
2bb3687b 12718 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12719 {
12720 free (buf);
0a1b45a2 12721 return false;
718175fa
JK
12722 }
12723
252b5132 12724 free (buf);
0a1b45a2 12725 return true;
252b5132 12726}
98d8431c
JB
12727\f
12728/* Providing external access to the ELF program header table. */
12729
12730/* Return an upper bound on the number of bytes required to store a
12731 copy of ABFD's program header table entries. Return -1 if an error
12732 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12733
98d8431c 12734long
217aa764 12735bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12736{
12737 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12738 {
12739 bfd_set_error (bfd_error_wrong_format);
12740 return -1;
12741 }
12742
936e320b 12743 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12744}
12745
98d8431c
JB
12746/* Copy ABFD's program header table entries to *PHDRS. The entries
12747 will be stored as an array of Elf_Internal_Phdr structures, as
12748 defined in include/elf/internal.h. To find out how large the
12749 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12750
12751 Return the number of program header table entries read, or -1 if an
12752 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12753
98d8431c 12754int
217aa764 12755bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12756{
12757 int num_phdrs;
12758
12759 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12760 {
12761 bfd_set_error (bfd_error_wrong_format);
12762 return -1;
12763 }
12764
12765 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12766 if (num_phdrs != 0)
12767 memcpy (phdrs, elf_tdata (abfd)->phdr,
12768 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12769
12770 return num_phdrs;
12771}
ae4221d7 12772
db6751f2 12773enum elf_reloc_type_class
7e612e98
AM
12774_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12775 const asection *rel_sec ATTRIBUTE_UNUSED,
12776 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12777{
12778 return reloc_class_normal;
12779}
f8df10f4 12780
47d9a591 12781/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12782 relocation against a local symbol. */
12783
12784bfd_vma
217aa764
AM
12785_bfd_elf_rela_local_sym (bfd *abfd,
12786 Elf_Internal_Sym *sym,
8517fae7 12787 asection **psec,
217aa764 12788 Elf_Internal_Rela *rel)
f8df10f4 12789{
8517fae7 12790 asection *sec = *psec;
f8df10f4
JJ
12791 bfd_vma relocation;
12792
6835821b
AM
12793 relocation = (sec->output_section->vma
12794 + sec->output_offset
12795 + sym->st_value);
f8df10f4 12796 if ((sec->flags & SEC_MERGE)
c629eae0 12797 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12798 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12799 {
f8df10f4 12800 rel->r_addend =
8517fae7 12801 _bfd_merged_section_offset (abfd, psec,
65765700 12802 elf_section_data (sec)->sec_info,
753731ee
AM
12803 sym->st_value + rel->r_addend);
12804 if (sec != *psec)
12805 {
12806 /* If we have changed the section, and our original section is
12807 marked with SEC_EXCLUDE, it means that the original
12808 SEC_MERGE section has been completely subsumed in some
12809 other SEC_MERGE section. In this case, we need to leave
12810 some info around for --emit-relocs. */
12811 if ((sec->flags & SEC_EXCLUDE) != 0)
12812 sec->kept_section = *psec;
12813 sec = *psec;
12814 }
8517fae7
AM
12815 rel->r_addend -= relocation;
12816 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12817 }
12818 return relocation;
12819}
c629eae0
JJ
12820
12821bfd_vma
217aa764
AM
12822_bfd_elf_rel_local_sym (bfd *abfd,
12823 Elf_Internal_Sym *sym,
12824 asection **psec,
12825 bfd_vma addend)
47d9a591 12826{
c629eae0
JJ
12827 asection *sec = *psec;
12828
6835821b 12829 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12830 return sym->st_value + addend;
12831
12832 return _bfd_merged_section_offset (abfd, psec,
65765700 12833 elf_section_data (sec)->sec_info,
753731ee 12834 sym->st_value + addend);
c629eae0
JJ
12835}
12836
37b01f6a
DG
12837/* Adjust an address within a section. Given OFFSET within SEC, return
12838 the new offset within the section, based upon changes made to the
12839 section. Returns -1 if the offset is now invalid.
12840 The offset (in abnd out) is in target sized bytes, however big a
12841 byte may be. */
12842
c629eae0 12843bfd_vma
217aa764 12844_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12845 struct bfd_link_info *info,
217aa764
AM
12846 asection *sec,
12847 bfd_vma offset)
c629eae0 12848{
68bfbfcc 12849 switch (sec->sec_info_type)
65765700 12850 {
dbaa2011 12851 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12852 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12853 offset);
dbaa2011 12854 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12855 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12856
65765700 12857 default:
310fd250
L
12858 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12859 {
37b01f6a 12860 /* Reverse the offset. */
310fd250
L
12861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12862 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12863
12864 /* address_size and sec->size are in octets. Convert
12865 to bytes before subtracting the original offset. */
61826503 12866 offset = ((sec->size - address_size)
bb294208 12867 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12868 }
65765700
JJ
12869 return offset;
12870 }
c629eae0 12871}
3333a7c3 12872\f
4c45e5c9 12873long
c9727e01
AM
12874_bfd_elf_get_synthetic_symtab (bfd *abfd,
12875 long symcount ATTRIBUTE_UNUSED,
12876 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12877 long dynsymcount,
c9727e01
AM
12878 asymbol **dynsyms,
12879 asymbol **ret)
4c45e5c9
JJ
12880{
12881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12882 asection *relplt;
12883 asymbol *s;
12884 const char *relplt_name;
0a1b45a2 12885 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
4c45e5c9
JJ
12886 arelent *p;
12887 long count, i, n;
12888 size_t size;
12889 Elf_Internal_Shdr *hdr;
12890 char *names;
12891 asection *plt;
12892
8615f3f2
AM
12893 *ret = NULL;
12894
90e3cdf2
JJ
12895 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12896 return 0;
12897
8615f3f2
AM
12898 if (dynsymcount <= 0)
12899 return 0;
12900
4c45e5c9
JJ
12901 if (!bed->plt_sym_val)
12902 return 0;
12903
12904 relplt_name = bed->relplt_name;
12905 if (relplt_name == NULL)
d35fd659 12906 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12907 relplt = bfd_get_section_by_name (abfd, relplt_name);
12908 if (relplt == NULL)
12909 return 0;
12910
12911 hdr = &elf_section_data (relplt)->this_hdr;
12912 if (hdr->sh_link != elf_dynsymtab (abfd)
12913 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12914 return 0;
12915
12916 plt = bfd_get_section_by_name (abfd, ".plt");
12917 if (plt == NULL)
12918 return 0;
12919
12920 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 12921 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
4c45e5c9
JJ
12922 return -1;
12923
93c6e8c3 12924 count = NUM_SHDR_ENTRIES (hdr);
4c45e5c9
JJ
12925 size = count * sizeof (asymbol);
12926 p = relplt->relocation;
cb53bf42 12927 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12928 {
12929 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12930 if (p->addend != 0)
12931 {
12932#ifdef BFD64
12933 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12934#else
12935 size += sizeof ("+0x") - 1 + 8;
12936#endif
12937 }
12938 }
4c45e5c9 12939
a50b1753 12940 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12941 if (s == NULL)
12942 return -1;
12943
12944 names = (char *) (s + count);
12945 p = relplt->relocation;
12946 n = 0;
cb53bf42 12947 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12948 {
12949 size_t len;
12950 bfd_vma addr;
12951
12952 addr = bed->plt_sym_val (i, plt, p);
12953 if (addr == (bfd_vma) -1)
12954 continue;
12955
12956 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12957 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12958 we are defining a symbol, ensure one of them is set. */
12959 if ((s->flags & BSF_LOCAL) == 0)
12960 s->flags |= BSF_GLOBAL;
6ba2a415 12961 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12962 s->section = plt;
12963 s->value = addr - plt->vma;
12964 s->name = names;
8f39ba8e 12965 s->udata.p = NULL;
4c45e5c9
JJ
12966 len = strlen ((*p->sym_ptr_ptr)->name);
12967 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12968 names += len;
041de40d
AM
12969 if (p->addend != 0)
12970 {
1d770845 12971 char buf[30], *a;
d324f6d6 12972
041de40d
AM
12973 memcpy (names, "+0x", sizeof ("+0x") - 1);
12974 names += sizeof ("+0x") - 1;
1d770845
L
12975 bfd_sprintf_vma (abfd, buf, p->addend);
12976 for (a = buf; *a == '0'; ++a)
12977 ;
12978 len = strlen (a);
12979 memcpy (names, a, len);
12980 names += len;
041de40d 12981 }
4c45e5c9
JJ
12982 memcpy (names, "@plt", sizeof ("@plt"));
12983 names += sizeof ("@plt");
8f39ba8e 12984 ++s, ++n;
4c45e5c9
JJ
12985 }
12986
12987 return n;
12988}
3d7f7666 12989
821e6ff6
AM
12990/* It is only used by x86-64 so far.
12991 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12992 but current usage would allow all of _bfd_std_section to be zero. */
12993static const asymbol lcomm_sym
12994 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12995asection _bfd_elf_large_com_section
7eacd66b 12996 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12997 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12998
0a1b45a2 12999bool
cc364be6 13000_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
13001{
13002 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
13003
13004 i_ehdrp = elf_elfheader (abfd);
13005
06f44071
AM
13006 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13007 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 13008
df3a023b 13009 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
13010 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13011 or STB_GNU_UNIQUE binding. */
cc364be6
AM
13012 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13013 {
13014 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13015 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13016 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13017 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13018 {
13019 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
13020 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13021 "and FreeBSD targets"));
cc364be6 13022 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
13023 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13024 "only by GNU and FreeBSD targets"));
cc364be6 13025 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
13026 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13027 "only by GNU and FreeBSD targets"));
13028 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13029 _bfd_error_handler (_("GNU_RETAIN section is supported "
13030 "only by GNU and FreeBSD targets"));
9aea1e31 13031 bfd_set_error (bfd_error_sorry);
0a1b45a2 13032 return false;
cc364be6
AM
13033 }
13034 }
0a1b45a2 13035 return true;
d1036acb 13036}
fcb93ecf
PB
13037
13038
13039/* Return TRUE for ELF symbol types that represent functions.
13040 This is the default version of this function, which is sufficient for
d8045f23 13041 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf 13042
0a1b45a2 13043bool
fcb93ecf
PB
13044_bfd_elf_is_function_type (unsigned int type)
13045{
d8045f23
NC
13046 return (type == STT_FUNC
13047 || type == STT_GNU_IFUNC);
fcb93ecf 13048}
9f296da3 13049
aef36ac1
AM
13050/* If the ELF symbol SYM might be a function in SEC, return the
13051 function size and set *CODE_OFF to the function's entry point,
13052 otherwise return zero. */
9f296da3 13053
aef36ac1
AM
13054bfd_size_type
13055_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13056 bfd_vma *code_off)
9f296da3 13057{
aef36ac1 13058 bfd_size_type size;
24aebc79 13059 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1 13060
ff9e0f5b 13061 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
13062 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13063 || sym->section != sec)
13064 return 0;
ff9e0f5b 13065
24aebc79 13066 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
11c6a7c6 13067
24aebc79
NC
13068 /* In theory we should check that the symbol's type satisfies
13069 _bfd_elf_is_function_type(), but there are some function-like
13070 symbols which would fail this test. (eg _start). Instead
13071 we check for hidden, local, notype symbols with zero size.
13072 This type of symbol is generated by the annobin plugin for gcc
13073 and clang, and should not be considered to be a function symbol. */
13074 if (size == 0
13075 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13076 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13077 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13078 return 0;
11c6a7c6 13079
ff9e0f5b 13080 *code_off = sym->value;
24aebc79
NC
13081 /* Do not return 0 for the function's size. */
13082 return size ? size : 1;
9f296da3 13083}
a8e14f4c
NC
13084
13085/* Set to non-zero to enable some debug messages. */
13086#define DEBUG_SECONDARY_RELOCS 0
13087
13088/* An internal-to-the-bfd-library only section type
13089 used to indicate a cached secondary reloc section. */
13090#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13091
13092/* Create a BFD section to hold a secondary reloc section. */
13093
0a1b45a2 13094bool
a8e14f4c
NC
13095_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13096 Elf_Internal_Shdr *hdr,
13097 const char * name,
13098 unsigned int shindex)
13099{
13100 /* We only support RELA secondary relocs. */
13101 if (hdr->sh_type != SHT_RELA)
0a1b45a2 13102 return false;
a8e14f4c
NC
13103
13104#if DEBUG_SECONDARY_RELOCS
13105 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13106#endif
13107 hdr->sh_type = SHT_SECONDARY_RELOC;
13108 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13109}
13110
13111/* Read in any secondary relocs associated with SEC. */
13112
0a1b45a2 13113bool
f60742b2
NC
13114_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13115 asection * sec,
13116 asymbol ** symbols,
0a1b45a2 13117 bool dynamic)
a8e14f4c
NC
13118{
13119 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13120 asection * relsec;
0a1b45a2 13121 bool result = true;
a8e14f4c 13122 bfd_vma (*r_sym) (bfd_vma);
285b1d33 13123 ufile_ptr filesize;
a8e14f4c
NC
13124
13125#if BFD_DEFAULT_TARGET_SIZE > 32
13126 if (bfd_arch_bits_per_address (abfd) != 32)
13127 r_sym = elf64_r_sym;
13128 else
13129#endif
13130 r_sym = elf32_r_sym;
11c6a7c6 13131
956ea65c
MM
13132 if (!elf_section_data (sec)->has_secondary_relocs)
13133 return true;
13134
a8e14f4c
NC
13135 /* Discover if there are any secondary reloc sections
13136 associated with SEC. */
285b1d33 13137 filesize = bfd_get_file_size (abfd);
a8e14f4c
NC
13138 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13139 {
13140 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13141
13142 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
13143 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13144 && (hdr->sh_entsize == ebd->s->sizeof_rel
13145 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
13146 {
13147 bfd_byte * native_relocs;
13148 bfd_byte * native_reloc;
13149 arelent * internal_relocs;
13150 arelent * internal_reloc;
285b1d33 13151 size_t i;
a8e14f4c
NC
13152 unsigned int entsize;
13153 unsigned int symcount;
285b1d33 13154 bfd_size_type reloc_count;
a8e14f4c
NC
13155 size_t amt;
13156
13157 if (ebd->elf_info_to_howto == NULL)
0a1b45a2 13158 return false;
a8e14f4c
NC
13159
13160#if DEBUG_SECONDARY_RELOCS
13161 fprintf (stderr, "read secondary relocs for %s from %s\n",
13162 sec->name, relsec->name);
13163#endif
13164 entsize = hdr->sh_entsize;
13165
285b1d33
AM
13166 if (filesize != 0
13167 && ((ufile_ptr) hdr->sh_offset > filesize
13168 || hdr->sh_size > filesize - hdr->sh_offset))
13169 {
13170 bfd_set_error (bfd_error_file_truncated);
13171 result = false;
13172 continue;
13173 }
13174
a8e14f4c
NC
13175 native_relocs = bfd_malloc (hdr->sh_size);
13176 if (native_relocs == NULL)
13177 {
0a1b45a2 13178 result = false;
a8e14f4c
NC
13179 continue;
13180 }
13181
13182 reloc_count = NUM_SHDR_ENTRIES (hdr);
13183 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13184 {
ecbbbdba 13185 free (native_relocs);
a8e14f4c 13186 bfd_set_error (bfd_error_file_too_big);
0a1b45a2 13187 result = false;
a8e14f4c
NC
13188 continue;
13189 }
13190
13191 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13192 if (internal_relocs == NULL)
13193 {
13194 free (native_relocs);
0a1b45a2 13195 result = false;
a8e14f4c
NC
13196 continue;
13197 }
13198
13199 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13200 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13201 != hdr->sh_size))
13202 {
13203 free (native_relocs);
ecbbbdba
NC
13204 /* The internal_relocs will be freed when
13205 the memory for the bfd is released. */
0a1b45a2 13206 result = false;
a8e14f4c
NC
13207 continue;
13208 }
13209
f60742b2
NC
13210 if (dynamic)
13211 symcount = bfd_get_dynamic_symcount (abfd);
13212 else
13213 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
13214
13215 for (i = 0, internal_reloc = internal_relocs,
13216 native_reloc = native_relocs;
13217 i < reloc_count;
13218 i++, internal_reloc++, native_reloc += entsize)
13219 {
0a1b45a2 13220 bool res;
a8e14f4c
NC
13221 Elf_Internal_Rela rela;
13222
8ee54925
NC
13223 if (entsize == ebd->s->sizeof_rel)
13224 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13225 else /* entsize == ebd->s->sizeof_rela */
13226 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
13227
13228 /* The address of an ELF reloc is section relative for an object
13229 file, and absolute for an executable file or shared library.
13230 The address of a normal BFD reloc is always section relative,
13231 and the address of a dynamic reloc is absolute.. */
13232 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13233 internal_reloc->address = rela.r_offset;
13234 else
13235 internal_reloc->address = rela.r_offset - sec->vma;
13236
13237 if (r_sym (rela.r_info) == STN_UNDEF)
13238 {
13239 /* FIXME: This and the error case below mean that we
13240 have a symbol on relocs that is not elf_symbol_type. */
13241 internal_reloc->sym_ptr_ptr =
13242 bfd_abs_section_ptr->symbol_ptr_ptr;
13243 }
13244 else if (r_sym (rela.r_info) > symcount)
13245 {
13246 _bfd_error_handler
13247 /* xgettext:c-format */
285b1d33 13248 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
a8e14f4c
NC
13249 abfd, sec, i, (long) r_sym (rela.r_info));
13250 bfd_set_error (bfd_error_bad_value);
13251 internal_reloc->sym_ptr_ptr =
13252 bfd_abs_section_ptr->symbol_ptr_ptr;
0a1b45a2 13253 result = false;
a8e14f4c
NC
13254 }
13255 else
13256 {
13257 asymbol **ps;
13258
13259 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
13260 internal_reloc->sym_ptr_ptr = ps;
13261 /* Make sure that this symbol is not removed by strip. */
13262 (*ps)->flags |= BSF_KEEP;
13263 }
13264
13265 internal_reloc->addend = rela.r_addend;
13266
13267 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13268 if (! res || internal_reloc->howto == NULL)
13269 {
13270#if DEBUG_SECONDARY_RELOCS
11c6a7c6
AM
13271 fprintf (stderr,
13272 "there is no howto associated with reloc %lx\n",
a8e14f4c
NC
13273 rela.r_info);
13274#endif
0a1b45a2 13275 result = false;
a8e14f4c
NC
13276 }
13277 }
13278
13279 free (native_relocs);
13280 /* Store the internal relocs. */
13281 elf_section_data (relsec)->sec_info = internal_relocs;
13282 }
13283 }
13284
13285 return result;
13286}
13287
13288/* Set the ELF section header fields of an output secondary reloc section. */
13289
0a1b45a2 13290bool
11c6a7c6
AM
13291_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13292 bfd *obfd ATTRIBUTE_UNUSED,
13293 const Elf_Internal_Shdr *isection,
13294 Elf_Internal_Shdr *osection)
a8e14f4c
NC
13295{
13296 asection * isec;
13297 asection * osec;
44466e45 13298 struct bfd_elf_section_data * esd;
a8e14f4c
NC
13299
13300 if (isection == NULL)
0a1b45a2 13301 return false;
a8e14f4c
NC
13302
13303 if (isection->sh_type != SHT_SECONDARY_RELOC)
0a1b45a2 13304 return true;
a8e14f4c
NC
13305
13306 isec = isection->bfd_section;
13307 if (isec == NULL)
0a1b45a2 13308 return false;
a8e14f4c
NC
13309
13310 osec = osection->bfd_section;
13311 if (osec == NULL)
0a1b45a2 13312 return false;
a8e14f4c 13313
44466e45
NC
13314 esd = elf_section_data (osec);
13315 BFD_ASSERT (esd->sec_info == NULL);
13316 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
13317 osection->sh_type = SHT_RELA;
13318 osection->sh_link = elf_onesymtab (obfd);
13319 if (osection->sh_link == 0)
13320 {
13321 /* There is no symbol table - we are hosed... */
13322 _bfd_error_handler
13323 /* xgettext:c-format */
11c6a7c6
AM
13324 (_("%pB(%pA): link section cannot be set"
13325 " because the output file does not have a symbol table"),
a8e14f4c
NC
13326 obfd, osec);
13327 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13328 return false;
a8e14f4c
NC
13329 }
13330
11c6a7c6
AM
13331 /* Find the output section that corresponds to the isection's
13332 sh_info link. */
327ef784
NC
13333 if (isection->sh_info == 0
13334 || isection->sh_info >= elf_numsections (ibfd))
13335 {
13336 _bfd_error_handler
13337 /* xgettext:c-format */
13338 (_("%pB(%pA): info section index is invalid"),
13339 obfd, osec);
13340 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13341 return false;
327ef784
NC
13342 }
13343
a8e14f4c
NC
13344 isection = elf_elfsections (ibfd)[isection->sh_info];
13345
327ef784
NC
13346 if (isection == NULL
13347 || isection->bfd_section == NULL
13348 || isection->bfd_section->output_section == NULL)
13349 {
13350 _bfd_error_handler
13351 /* xgettext:c-format */
11c6a7c6
AM
13352 (_("%pB(%pA): info section index cannot be set"
13353 " because the section is not in the output"),
327ef784
NC
13354 obfd, osec);
13355 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13356 return false;
327ef784
NC
13357 }
13358
44466e45
NC
13359 esd = elf_section_data (isection->bfd_section->output_section);
13360 BFD_ASSERT (esd != NULL);
13361 osection->sh_info = esd->this_idx;
0a1b45a2 13362 esd->has_secondary_relocs = true;
a8e14f4c
NC
13363#if DEBUG_SECONDARY_RELOCS
13364 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13365 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
13366 fprintf (stderr, "mark section %s as having secondary relocs\n",
13367 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
13368#endif
13369
0a1b45a2 13370 return true;
a8e14f4c
NC
13371}
13372
44466e45
NC
13373/* Write out a secondary reloc section.
13374
13375 FIXME: Currently this function can result in a serious performance penalty
13376 for files with secondary relocs and lots of sections. The proper way to
13377 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13378 relocs together and then to have this function just walk that chain. */
a8e14f4c 13379
0a1b45a2 13380bool
a8e14f4c
NC
13381_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13382{
13383 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13384 bfd_vma addr_offset;
13385 asection * relsec;
13386 bfd_vma (*r_info) (bfd_vma, bfd_vma);
0a1b45a2 13387 bool result = true;
ac4bf06c
NC
13388
13389 if (sec == NULL)
0a1b45a2 13390 return false;
a8e14f4c
NC
13391
13392#if BFD_DEFAULT_TARGET_SIZE > 32
13393 if (bfd_arch_bits_per_address (abfd) != 32)
13394 r_info = elf64_r_info;
13395 else
13396#endif
13397 r_info = elf32_r_info;
13398
a8e14f4c
NC
13399 /* The address of an ELF reloc is section relative for an object
13400 file, and absolute for an executable file or shared library.
13401 The address of a BFD reloc is always section relative. */
13402 addr_offset = 0;
13403 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13404 addr_offset = sec->vma;
13405
13406 /* Discover if there are any secondary reloc sections
13407 associated with SEC. */
13408 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13409 {
13410 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13411 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13412
13413 if (hdr->sh_type == SHT_RELA
13414 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13415 {
13416 asymbol * last_sym;
13417 int last_sym_idx;
c7afb87b
AM
13418 size_t reloc_count;
13419 size_t idx;
13420 bfd_size_type entsize;
a8e14f4c
NC
13421 arelent * src_irel;
13422 bfd_byte * dst_rela;
13423
ac4bf06c
NC
13424 if (hdr->contents != NULL)
13425 {
13426 _bfd_error_handler
13427 /* xgettext:c-format */
13428 (_("%pB(%pA): error: secondary reloc section processed twice"),
13429 abfd, relsec);
13430 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13431 result = false;
ac4bf06c
NC
13432 continue;
13433 }
a8e14f4c 13434
8ee54925
NC
13435 entsize = hdr->sh_entsize;
13436 if (entsize == 0)
ac267c75
NC
13437 {
13438 _bfd_error_handler
13439 /* xgettext:c-format */
11c6a7c6
AM
13440 (_("%pB(%pA): error: secondary reloc section"
13441 " has zero sized entries"),
ac267c75
NC
13442 abfd, relsec);
13443 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13444 result = false;
ac267c75
NC
13445 continue;
13446 }
8ee54925
NC
13447 else if (entsize != ebd->s->sizeof_rel
13448 && entsize != ebd->s->sizeof_rela)
13449 {
13450 _bfd_error_handler
13451 /* xgettext:c-format */
11c6a7c6
AM
13452 (_("%pB(%pA): error: secondary reloc section"
13453 " has non-standard sized entries"),
8ee54925
NC
13454 abfd, relsec);
13455 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13456 result = false;
8ee54925
NC
13457 continue;
13458 }
ac267c75 13459
8ee54925 13460 reloc_count = hdr->sh_size / entsize;
c7afb87b
AM
13461 hdr->sh_size = entsize * reloc_count;
13462 if (reloc_count == 0)
ac4bf06c
NC
13463 {
13464 _bfd_error_handler
13465 /* xgettext:c-format */
13466 (_("%pB(%pA): error: secondary reloc section is empty!"),
13467 abfd, relsec);
13468 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13469 result = false;
ac4bf06c
NC
13470 continue;
13471 }
a8e14f4c
NC
13472
13473 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13474 if (hdr->contents == NULL)
13475 continue;
13476
13477#if DEBUG_SECONDARY_RELOCS
13478 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13479 reloc_count, sec->name, relsec->name);
13480#endif
13481 last_sym = NULL;
13482 last_sym_idx = 0;
13483 dst_rela = hdr->contents;
13484 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
13485 if (src_irel == NULL)
13486 {
13487 _bfd_error_handler
13488 /* xgettext:c-format */
11c6a7c6
AM
13489 (_("%pB(%pA): error: internal relocs missing"
13490 " for secondary reloc section"),
ac4bf06c
NC
13491 abfd, relsec);
13492 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13493 result = false;
ac4bf06c
NC
13494 continue;
13495 }
a8e14f4c 13496
8ee54925 13497 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
13498 {
13499 Elf_Internal_Rela src_rela;
13500 arelent *ptr;
13501 asymbol *sym;
13502 int n;
13503
13504 ptr = src_irel + idx;
ac4bf06c
NC
13505 if (ptr == NULL)
13506 {
13507 _bfd_error_handler
13508 /* xgettext:c-format */
c7afb87b 13509 (_("%pB(%pA): error: reloc table entry %zu is empty"),
ac4bf06c
NC
13510 abfd, relsec, idx);
13511 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13512 result = false;
ac4bf06c
NC
13513 break;
13514 }
a8e14f4c 13515
ac4bf06c
NC
13516 if (ptr->sym_ptr_ptr == NULL)
13517 {
13518 /* FIXME: Is this an error ? */
13519 n = 0;
13520 }
a8e14f4c
NC
13521 else
13522 {
ac4bf06c
NC
13523 sym = *ptr->sym_ptr_ptr;
13524
13525 if (sym == last_sym)
13526 n = last_sym_idx;
13527 else
a8e14f4c 13528 {
ac4bf06c
NC
13529 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13530 if (n < 0)
13531 {
13532 _bfd_error_handler
13533 /* xgettext:c-format */
c7afb87b 13534 (_("%pB(%pA): error: secondary reloc %zu"
11c6a7c6 13535 " references a missing symbol"),
ac4bf06c
NC
13536 abfd, relsec, idx);
13537 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13538 result = false;
ac4bf06c
NC
13539 n = 0;
13540 }
13541
13542 last_sym = sym;
13543 last_sym_idx = n;
a8e14f4c 13544 }
a8e14f4c 13545
ac4bf06c
NC
13546 if (sym->the_bfd != NULL
13547 && sym->the_bfd->xvec != abfd->xvec
13548 && ! _bfd_elf_validate_reloc (abfd, ptr))
13549 {
13550 _bfd_error_handler
13551 /* xgettext:c-format */
c7afb87b 13552 (_("%pB(%pA): error: secondary reloc %zu"
11c6a7c6 13553 " references a deleted symbol"),
ac4bf06c
NC
13554 abfd, relsec, idx);
13555 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13556 result = false;
ac4bf06c
NC
13557 n = 0;
13558 }
a8e14f4c
NC
13559 }
13560
ac4bf06c 13561 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13562 if (ptr->howto == NULL)
13563 {
ac4bf06c
NC
13564 _bfd_error_handler
13565 /* xgettext:c-format */
c7afb87b 13566 (_("%pB(%pA): error: secondary reloc %zu"
11c6a7c6 13567 " is of an unknown type"),
ac4bf06c
NC
13568 abfd, relsec, idx);
13569 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13570 result = false;
ac4bf06c 13571 src_rela.r_info = r_info (0, 0);
a8e14f4c 13572 }
ac4bf06c
NC
13573 else
13574 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13575 src_rela.r_addend = ptr->addend;
8ee54925
NC
13576
13577 if (entsize == ebd->s->sizeof_rel)
13578 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13579 else /* entsize == ebd->s->sizeof_rela */
13580 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13581 }
13582 }
13583 }
13584
ac4bf06c 13585 return result;
a8e14f4c 13586}